xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/AsmPrinter/WinCFGuard.cpp (revision e8d8bef961a50d4dc22501cde4fb9fb0be1b2532)
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
10*e8d8bef9SDimitry 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/CodeGen/MachineOperand.h"
190b57cec5SDimitry Andric #include "llvm/IR/Constants.h"
20480093f4SDimitry Andric #include "llvm/IR/Instructions.h"
21*e8d8bef9SDimitry Andric #include "llvm/IR/Metadata.h"
220b57cec5SDimitry Andric #include "llvm/MC/MCAsmInfo.h"
230b57cec5SDimitry Andric #include "llvm/MC/MCObjectFileInfo.h"
240b57cec5SDimitry Andric #include "llvm/MC/MCStreamer.h"
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric #include <vector>
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric using namespace llvm;
290b57cec5SDimitry Andric 
300b57cec5SDimitry Andric WinCFGuard::WinCFGuard(AsmPrinter *A) : AsmPrinterHandler(), Asm(A) {}
310b57cec5SDimitry Andric 
320b57cec5SDimitry Andric WinCFGuard::~WinCFGuard() {}
330b57cec5SDimitry Andric 
34480093f4SDimitry Andric void WinCFGuard::endFunction(const MachineFunction *MF) {
35480093f4SDimitry Andric 
36480093f4SDimitry Andric   // Skip functions without any longjmp targets.
37480093f4SDimitry Andric   if (MF->getLongjmpTargets().empty())
38480093f4SDimitry Andric     return;
39480093f4SDimitry Andric 
40480093f4SDimitry Andric   // Copy the function's longjmp targets to a module-level list.
41*e8d8bef9SDimitry Andric   llvm::append_range(LongjmpTargets, MF->getLongjmpTargets());
42480093f4SDimitry Andric }
43480093f4SDimitry Andric 
44480093f4SDimitry Andric /// Returns true if this function's address is escaped in a way that might make
45480093f4SDimitry Andric /// it an indirect call target. Function::hasAddressTaken gives different
46480093f4SDimitry Andric /// results when a function is called directly with a function prototype
47480093f4SDimitry Andric /// mismatch, which requires a cast.
48480093f4SDimitry Andric static bool isPossibleIndirectCallTarget(const Function *F) {
49480093f4SDimitry Andric   SmallVector<const Value *, 4> Users{F};
50480093f4SDimitry Andric   while (!Users.empty()) {
51480093f4SDimitry Andric     const Value *FnOrCast = Users.pop_back_val();
52480093f4SDimitry Andric     for (const Use &U : FnOrCast->uses()) {
53480093f4SDimitry Andric       const User *FnUser = U.getUser();
54480093f4SDimitry Andric       if (isa<BlockAddress>(FnUser))
55480093f4SDimitry Andric         continue;
56480093f4SDimitry Andric       if (const auto *Call = dyn_cast<CallBase>(FnUser)) {
57480093f4SDimitry Andric         if (!Call->isCallee(&U))
58480093f4SDimitry Andric           return true;
59480093f4SDimitry Andric       } else if (isa<Instruction>(FnUser)) {
60480093f4SDimitry Andric         // Consider any other instruction to be an escape. This has some weird
61480093f4SDimitry Andric         // consequences like no-op intrinsics being an escape or a store *to* a
62480093f4SDimitry Andric         // function address being an escape.
63480093f4SDimitry Andric         return true;
64480093f4SDimitry Andric       } else if (const auto *C = dyn_cast<Constant>(FnUser)) {
65480093f4SDimitry Andric         // If this is a constant pointer cast of the function, don't consider
66480093f4SDimitry Andric         // this escape. Analyze the uses of the cast as well. This ensures that
67480093f4SDimitry Andric         // direct calls with mismatched prototypes don't end up in the CFG
68480093f4SDimitry Andric         // table. Consider other constants, such as vtable initializers, to
69480093f4SDimitry Andric         // escape the function.
70480093f4SDimitry Andric         if (C->stripPointerCasts() == F)
71480093f4SDimitry Andric           Users.push_back(FnUser);
72480093f4SDimitry Andric         else
73480093f4SDimitry Andric           return true;
74480093f4SDimitry Andric       }
75480093f4SDimitry Andric     }
76480093f4SDimitry Andric   }
77480093f4SDimitry Andric   return false;
78480093f4SDimitry Andric }
79480093f4SDimitry Andric 
80*e8d8bef9SDimitry Andric MCSymbol *WinCFGuard::lookupImpSymbol(const MCSymbol *Sym) {
81*e8d8bef9SDimitry Andric   if (Sym->getName().startswith("__imp_"))
82*e8d8bef9SDimitry Andric     return nullptr;
83*e8d8bef9SDimitry Andric   return Asm->OutContext.lookupSymbol(Twine("__imp_") + Sym->getName());
84*e8d8bef9SDimitry Andric }
85*e8d8bef9SDimitry Andric 
860b57cec5SDimitry Andric void WinCFGuard::endModule() {
870b57cec5SDimitry Andric   const Module *M = Asm->MMI->getModule();
88*e8d8bef9SDimitry Andric   std::vector<const MCSymbol *> GFIDsEntries;
89*e8d8bef9SDimitry Andric   std::vector<const MCSymbol *> GIATsEntries;
90*e8d8bef9SDimitry Andric   for (const Function &F : *M) {
91*e8d8bef9SDimitry Andric     if (isPossibleIndirectCallTarget(&F)) {
92*e8d8bef9SDimitry Andric       // If F is a dllimport and has an "__imp_" symbol already defined, add the
93*e8d8bef9SDimitry Andric       // "__imp_" symbol to the .giats section.
94*e8d8bef9SDimitry Andric       if (F.hasDLLImportStorageClass()) {
95*e8d8bef9SDimitry Andric         if (MCSymbol *impSym = lookupImpSymbol(Asm->getSymbol(&F))) {
96*e8d8bef9SDimitry Andric           GIATsEntries.push_back(impSym);
97*e8d8bef9SDimitry Andric         }
98*e8d8bef9SDimitry Andric       }
99*e8d8bef9SDimitry Andric       // Add the function's symbol to the .gfids section.
100*e8d8bef9SDimitry Andric       // Note: For dllimport functions, MSVC sometimes does not add this symbol
101*e8d8bef9SDimitry Andric       // to the .gfids section, but only adds the corresponding "__imp_" symbol
102*e8d8bef9SDimitry Andric       // to the .giats section. Here we always add the symbol to the .gfids
103*e8d8bef9SDimitry Andric       // section, since this does not introduce security risks.
104*e8d8bef9SDimitry Andric       GFIDsEntries.push_back(Asm->getSymbol(&F));
105*e8d8bef9SDimitry Andric     }
106*e8d8bef9SDimitry Andric   }
107*e8d8bef9SDimitry Andric 
108*e8d8bef9SDimitry Andric   if (GFIDsEntries.empty() && GIATsEntries.empty() && LongjmpTargets.empty())
1090b57cec5SDimitry Andric     return;
110*e8d8bef9SDimitry Andric 
111*e8d8bef9SDimitry Andric   // Emit the symbol index of each GFIDs entry to form the .gfids section.
1120b57cec5SDimitry Andric   auto &OS = *Asm->OutStreamer;
1130b57cec5SDimitry Andric   OS.SwitchSection(Asm->OutContext.getObjectFileInfo()->getGFIDsSection());
114*e8d8bef9SDimitry Andric   for (const MCSymbol *S : GFIDsEntries)
115*e8d8bef9SDimitry Andric     OS.EmitCOFFSymbolIndex(S);
116480093f4SDimitry Andric 
117*e8d8bef9SDimitry Andric   // Emit the symbol index of each GIATs entry to form the .giats section.
118*e8d8bef9SDimitry Andric   OS.SwitchSection(Asm->OutContext.getObjectFileInfo()->getGIATsSection());
119*e8d8bef9SDimitry Andric   for (const MCSymbol *S : GIATsEntries) {
120*e8d8bef9SDimitry Andric     OS.EmitCOFFSymbolIndex(S);
121*e8d8bef9SDimitry Andric   }
122*e8d8bef9SDimitry Andric 
123*e8d8bef9SDimitry Andric   // Emit the symbol index of each longjmp target to form the .gljmp section.
124480093f4SDimitry Andric   OS.SwitchSection(Asm->OutContext.getObjectFileInfo()->getGLJMPSection());
125480093f4SDimitry Andric   for (const MCSymbol *S : LongjmpTargets) {
126480093f4SDimitry Andric     OS.EmitCOFFSymbolIndex(S);
127480093f4SDimitry Andric   }
1280b57cec5SDimitry Andric }
129