Path: blob/main/contrib/llvm-project/llvm/lib/CodeGen/AsmPrinter/WinCFGuard.cpp
35271 views
//===-- CodeGen/AsmPrinter/WinCFGuard.cpp - Control Flow Guard Impl ------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file contains support for writing the metadata for Windows Control Flow9// Guard, including address-taken functions and valid longjmp targets.10//11//===----------------------------------------------------------------------===//1213#include "WinCFGuard.h"14#include "llvm/CodeGen/AsmPrinter.h"15#include "llvm/CodeGen/MachineFunction.h"16#include "llvm/CodeGen/MachineModuleInfo.h"17#include "llvm/IR/Constants.h"18#include "llvm/IR/InstrTypes.h"19#include "llvm/IR/Module.h"20#include "llvm/MC/MCObjectFileInfo.h"21#include "llvm/MC/MCStreamer.h"2223#include <vector>2425using namespace llvm;2627WinCFGuard::WinCFGuard(AsmPrinter *A) : Asm(A) {}2829WinCFGuard::~WinCFGuard() = default;3031void WinCFGuard::endFunction(const MachineFunction *MF) {3233// Skip functions without any longjmp targets.34if (MF->getLongjmpTargets().empty())35return;3637// Copy the function's longjmp targets to a module-level list.38llvm::append_range(LongjmpTargets, MF->getLongjmpTargets());39}4041/// Returns true if this function's address is escaped in a way that might make42/// it an indirect call target. Function::hasAddressTaken gives different43/// results when a function is called directly with a function prototype44/// mismatch, which requires a cast.45static bool isPossibleIndirectCallTarget(const Function *F) {46SmallVector<const Value *, 4> Users{F};47while (!Users.empty()) {48const Value *FnOrCast = Users.pop_back_val();49for (const Use &U : FnOrCast->uses()) {50const User *FnUser = U.getUser();51if (isa<BlockAddress>(FnUser))52continue;53if (const auto *Call = dyn_cast<CallBase>(FnUser)) {54if (!Call->isCallee(&U))55return true;56} else if (isa<Instruction>(FnUser)) {57// Consider any other instruction to be an escape. This has some weird58// consequences like no-op intrinsics being an escape or a store *to* a59// function address being an escape.60return true;61} else if (const auto *C = dyn_cast<Constant>(FnUser)) {62// If this is a constant pointer cast of the function, don't consider63// this escape. Analyze the uses of the cast as well. This ensures that64// direct calls with mismatched prototypes don't end up in the CFG65// table. Consider other constants, such as vtable initializers, to66// escape the function.67if (C->stripPointerCasts() == F)68Users.push_back(FnUser);69else70return true;71}72}73}74return false;75}7677MCSymbol *WinCFGuard::lookupImpSymbol(const MCSymbol *Sym) {78if (Sym->getName().starts_with("__imp_"))79return nullptr;80return Asm->OutContext.lookupSymbol(Twine("__imp_") + Sym->getName());81}8283void WinCFGuard::endModule() {84const Module *M = Asm->MMI->getModule();85std::vector<const MCSymbol *> GFIDsEntries;86std::vector<const MCSymbol *> GIATsEntries;87for (const Function &F : *M) {88if (isPossibleIndirectCallTarget(&F)) {89// If F is a dllimport and has an "__imp_" symbol already defined, add the90// "__imp_" symbol to the .giats section.91if (F.hasDLLImportStorageClass()) {92if (MCSymbol *impSym = lookupImpSymbol(Asm->getSymbol(&F))) {93GIATsEntries.push_back(impSym);94}95}96// Add the function's symbol to the .gfids section.97// Note: For dllimport functions, MSVC sometimes does not add this symbol98// to the .gfids section, but only adds the corresponding "__imp_" symbol99// to the .giats section. Here we always add the symbol to the .gfids100// section, since this does not introduce security risks.101GFIDsEntries.push_back(Asm->getSymbol(&F));102}103}104105if (GFIDsEntries.empty() && GIATsEntries.empty() && LongjmpTargets.empty())106return;107108// Emit the symbol index of each GFIDs entry to form the .gfids section.109auto &OS = *Asm->OutStreamer;110OS.switchSection(Asm->OutContext.getObjectFileInfo()->getGFIDsSection());111for (const MCSymbol *S : GFIDsEntries)112OS.emitCOFFSymbolIndex(S);113114// Emit the symbol index of each GIATs entry to form the .giats section.115OS.switchSection(Asm->OutContext.getObjectFileInfo()->getGIATsSection());116for (const MCSymbol *S : GIATsEntries) {117OS.emitCOFFSymbolIndex(S);118}119120// Emit the symbol index of each longjmp target to form the .gljmp section.121OS.switchSection(Asm->OutContext.getObjectFileInfo()->getGLJMPSection());122for (const MCSymbol *S : LongjmpTargets) {123OS.emitCOFFSymbolIndex(S);124}125}126127128