10b57cec5SDimitry Andric //===-- CodeGen/AsmPrinter/WinCFGuard.cpp - Control Flow Guard Impl ------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 9480093f4SDimitry Andric // This file contains support for writing the metadata for Windows Control Flow 10e8d8bef9SDimitry Andric // Guard, including address-taken functions and valid longjmp targets. 110b57cec5SDimitry Andric // 120b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 130b57cec5SDimitry Andric 140b57cec5SDimitry Andric #include "WinCFGuard.h" 150b57cec5SDimitry Andric #include "llvm/CodeGen/AsmPrinter.h" 160b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 170b57cec5SDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h" 180b57cec5SDimitry Andric #include "llvm/IR/Constants.h" 19*81ad6265SDimitry Andric #include "llvm/IR/InstrTypes.h" 200b57cec5SDimitry Andric #include "llvm/MC/MCObjectFileInfo.h" 210b57cec5SDimitry Andric #include "llvm/MC/MCStreamer.h" 220b57cec5SDimitry Andric 230b57cec5SDimitry Andric #include <vector> 240b57cec5SDimitry Andric 250b57cec5SDimitry Andric using namespace llvm; 260b57cec5SDimitry Andric 2704eeddc0SDimitry Andric WinCFGuard::WinCFGuard(AsmPrinter *A) : Asm(A) {} 280b57cec5SDimitry Andric 29*81ad6265SDimitry Andric WinCFGuard::~WinCFGuard() = default; 300b57cec5SDimitry Andric 31480093f4SDimitry Andric void WinCFGuard::endFunction(const MachineFunction *MF) { 32480093f4SDimitry Andric 33480093f4SDimitry Andric // Skip functions without any longjmp targets. 34480093f4SDimitry Andric if (MF->getLongjmpTargets().empty()) 35480093f4SDimitry Andric return; 36480093f4SDimitry Andric 37480093f4SDimitry Andric // Copy the function's longjmp targets to a module-level list. 38e8d8bef9SDimitry Andric llvm::append_range(LongjmpTargets, MF->getLongjmpTargets()); 39480093f4SDimitry Andric } 40480093f4SDimitry Andric 41480093f4SDimitry Andric /// Returns true if this function's address is escaped in a way that might make 42480093f4SDimitry Andric /// it an indirect call target. Function::hasAddressTaken gives different 43480093f4SDimitry Andric /// results when a function is called directly with a function prototype 44480093f4SDimitry Andric /// mismatch, which requires a cast. 45480093f4SDimitry Andric static bool isPossibleIndirectCallTarget(const Function *F) { 46480093f4SDimitry Andric SmallVector<const Value *, 4> Users{F}; 47480093f4SDimitry Andric while (!Users.empty()) { 48480093f4SDimitry Andric const Value *FnOrCast = Users.pop_back_val(); 49480093f4SDimitry Andric for (const Use &U : FnOrCast->uses()) { 50480093f4SDimitry Andric const User *FnUser = U.getUser(); 51480093f4SDimitry Andric if (isa<BlockAddress>(FnUser)) 52480093f4SDimitry Andric continue; 53480093f4SDimitry Andric if (const auto *Call = dyn_cast<CallBase>(FnUser)) { 54480093f4SDimitry Andric if (!Call->isCallee(&U)) 55480093f4SDimitry Andric return true; 56480093f4SDimitry Andric } else if (isa<Instruction>(FnUser)) { 57480093f4SDimitry Andric // Consider any other instruction to be an escape. This has some weird 58480093f4SDimitry Andric // consequences like no-op intrinsics being an escape or a store *to* a 59480093f4SDimitry Andric // function address being an escape. 60480093f4SDimitry Andric return true; 61480093f4SDimitry Andric } else if (const auto *C = dyn_cast<Constant>(FnUser)) { 62480093f4SDimitry Andric // If this is a constant pointer cast of the function, don't consider 63480093f4SDimitry Andric // this escape. Analyze the uses of the cast as well. This ensures that 64480093f4SDimitry Andric // direct calls with mismatched prototypes don't end up in the CFG 65480093f4SDimitry Andric // table. Consider other constants, such as vtable initializers, to 66480093f4SDimitry Andric // escape the function. 67480093f4SDimitry Andric if (C->stripPointerCasts() == F) 68480093f4SDimitry Andric Users.push_back(FnUser); 69480093f4SDimitry Andric else 70480093f4SDimitry Andric return true; 71480093f4SDimitry Andric } 72480093f4SDimitry Andric } 73480093f4SDimitry Andric } 74480093f4SDimitry Andric return false; 75480093f4SDimitry Andric } 76480093f4SDimitry Andric 77e8d8bef9SDimitry Andric MCSymbol *WinCFGuard::lookupImpSymbol(const MCSymbol *Sym) { 78e8d8bef9SDimitry Andric if (Sym->getName().startswith("__imp_")) 79e8d8bef9SDimitry Andric return nullptr; 80e8d8bef9SDimitry Andric return Asm->OutContext.lookupSymbol(Twine("__imp_") + Sym->getName()); 81e8d8bef9SDimitry Andric } 82e8d8bef9SDimitry Andric 830b57cec5SDimitry Andric void WinCFGuard::endModule() { 840b57cec5SDimitry Andric const Module *M = Asm->MMI->getModule(); 85e8d8bef9SDimitry Andric std::vector<const MCSymbol *> GFIDsEntries; 86e8d8bef9SDimitry Andric std::vector<const MCSymbol *> GIATsEntries; 87e8d8bef9SDimitry Andric for (const Function &F : *M) { 88e8d8bef9SDimitry Andric if (isPossibleIndirectCallTarget(&F)) { 89e8d8bef9SDimitry Andric // If F is a dllimport and has an "__imp_" symbol already defined, add the 90e8d8bef9SDimitry Andric // "__imp_" symbol to the .giats section. 91e8d8bef9SDimitry Andric if (F.hasDLLImportStorageClass()) { 92e8d8bef9SDimitry Andric if (MCSymbol *impSym = lookupImpSymbol(Asm->getSymbol(&F))) { 93e8d8bef9SDimitry Andric GIATsEntries.push_back(impSym); 94e8d8bef9SDimitry Andric } 95e8d8bef9SDimitry Andric } 96e8d8bef9SDimitry Andric // Add the function's symbol to the .gfids section. 97e8d8bef9SDimitry Andric // Note: For dllimport functions, MSVC sometimes does not add this symbol 98e8d8bef9SDimitry Andric // to the .gfids section, but only adds the corresponding "__imp_" symbol 99e8d8bef9SDimitry Andric // to the .giats section. Here we always add the symbol to the .gfids 100e8d8bef9SDimitry Andric // section, since this does not introduce security risks. 101e8d8bef9SDimitry Andric GFIDsEntries.push_back(Asm->getSymbol(&F)); 102e8d8bef9SDimitry Andric } 103e8d8bef9SDimitry Andric } 104e8d8bef9SDimitry Andric 105e8d8bef9SDimitry Andric if (GFIDsEntries.empty() && GIATsEntries.empty() && LongjmpTargets.empty()) 1060b57cec5SDimitry Andric return; 107e8d8bef9SDimitry Andric 108e8d8bef9SDimitry Andric // Emit the symbol index of each GFIDs entry to form the .gfids section. 1090b57cec5SDimitry Andric auto &OS = *Asm->OutStreamer; 110*81ad6265SDimitry Andric OS.switchSection(Asm->OutContext.getObjectFileInfo()->getGFIDsSection()); 111e8d8bef9SDimitry Andric for (const MCSymbol *S : GFIDsEntries) 112*81ad6265SDimitry Andric OS.emitCOFFSymbolIndex(S); 113480093f4SDimitry Andric 114e8d8bef9SDimitry Andric // Emit the symbol index of each GIATs entry to form the .giats section. 115*81ad6265SDimitry Andric OS.switchSection(Asm->OutContext.getObjectFileInfo()->getGIATsSection()); 116e8d8bef9SDimitry Andric for (const MCSymbol *S : GIATsEntries) { 117*81ad6265SDimitry Andric OS.emitCOFFSymbolIndex(S); 118e8d8bef9SDimitry Andric } 119e8d8bef9SDimitry Andric 120e8d8bef9SDimitry Andric // Emit the symbol index of each longjmp target to form the .gljmp section. 121*81ad6265SDimitry Andric OS.switchSection(Asm->OutContext.getObjectFileInfo()->getGLJMPSection()); 122480093f4SDimitry Andric for (const MCSymbol *S : LongjmpTargets) { 123*81ad6265SDimitry Andric OS.emitCOFFSymbolIndex(S); 124480093f4SDimitry Andric } 1250b57cec5SDimitry Andric } 126