xref: /freebsd-src/contrib/llvm-project/llvm/lib/CodeGen/AsmPrinter/WinCFGuard.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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"
1981ad6265SDimitry Andric #include "llvm/IR/InstrTypes.h"
20*0fca6ea1SDimitry Andric #include "llvm/IR/Module.h"
210b57cec5SDimitry Andric #include "llvm/MC/MCObjectFileInfo.h"
220b57cec5SDimitry Andric #include "llvm/MC/MCStreamer.h"
230b57cec5SDimitry Andric 
240b57cec5SDimitry Andric #include <vector>
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric using namespace llvm;
270b57cec5SDimitry Andric 
2804eeddc0SDimitry Andric WinCFGuard::WinCFGuard(AsmPrinter *A) : Asm(A) {}
290b57cec5SDimitry Andric 
3081ad6265SDimitry Andric WinCFGuard::~WinCFGuard() = default;
310b57cec5SDimitry Andric 
32480093f4SDimitry Andric void WinCFGuard::endFunction(const MachineFunction *MF) {
33480093f4SDimitry Andric 
34480093f4SDimitry Andric   // Skip functions without any longjmp targets.
35480093f4SDimitry Andric   if (MF->getLongjmpTargets().empty())
36480093f4SDimitry Andric     return;
37480093f4SDimitry Andric 
38480093f4SDimitry Andric   // Copy the function's longjmp targets to a module-level list.
39e8d8bef9SDimitry Andric   llvm::append_range(LongjmpTargets, MF->getLongjmpTargets());
40480093f4SDimitry Andric }
41480093f4SDimitry Andric 
42480093f4SDimitry Andric /// Returns true if this function's address is escaped in a way that might make
43480093f4SDimitry Andric /// it an indirect call target. Function::hasAddressTaken gives different
44480093f4SDimitry Andric /// results when a function is called directly with a function prototype
45480093f4SDimitry Andric /// mismatch, which requires a cast.
46480093f4SDimitry Andric static bool isPossibleIndirectCallTarget(const Function *F) {
47480093f4SDimitry Andric   SmallVector<const Value *, 4> Users{F};
48480093f4SDimitry Andric   while (!Users.empty()) {
49480093f4SDimitry Andric     const Value *FnOrCast = Users.pop_back_val();
50480093f4SDimitry Andric     for (const Use &U : FnOrCast->uses()) {
51480093f4SDimitry Andric       const User *FnUser = U.getUser();
52480093f4SDimitry Andric       if (isa<BlockAddress>(FnUser))
53480093f4SDimitry Andric         continue;
54480093f4SDimitry Andric       if (const auto *Call = dyn_cast<CallBase>(FnUser)) {
55480093f4SDimitry Andric         if (!Call->isCallee(&U))
56480093f4SDimitry Andric           return true;
57480093f4SDimitry Andric       } else if (isa<Instruction>(FnUser)) {
58480093f4SDimitry Andric         // Consider any other instruction to be an escape. This has some weird
59480093f4SDimitry Andric         // consequences like no-op intrinsics being an escape or a store *to* a
60480093f4SDimitry Andric         // function address being an escape.
61480093f4SDimitry Andric         return true;
62480093f4SDimitry Andric       } else if (const auto *C = dyn_cast<Constant>(FnUser)) {
63480093f4SDimitry Andric         // If this is a constant pointer cast of the function, don't consider
64480093f4SDimitry Andric         // this escape. Analyze the uses of the cast as well. This ensures that
65480093f4SDimitry Andric         // direct calls with mismatched prototypes don't end up in the CFG
66480093f4SDimitry Andric         // table. Consider other constants, such as vtable initializers, to
67480093f4SDimitry Andric         // escape the function.
68480093f4SDimitry Andric         if (C->stripPointerCasts() == F)
69480093f4SDimitry Andric           Users.push_back(FnUser);
70480093f4SDimitry Andric         else
71480093f4SDimitry Andric           return true;
72480093f4SDimitry Andric       }
73480093f4SDimitry Andric     }
74480093f4SDimitry Andric   }
75480093f4SDimitry Andric   return false;
76480093f4SDimitry Andric }
77480093f4SDimitry Andric 
78e8d8bef9SDimitry Andric MCSymbol *WinCFGuard::lookupImpSymbol(const MCSymbol *Sym) {
795f757f3fSDimitry Andric   if (Sym->getName().starts_with("__imp_"))
80e8d8bef9SDimitry Andric     return nullptr;
81e8d8bef9SDimitry Andric   return Asm->OutContext.lookupSymbol(Twine("__imp_") + Sym->getName());
82e8d8bef9SDimitry Andric }
83e8d8bef9SDimitry Andric 
840b57cec5SDimitry Andric void WinCFGuard::endModule() {
850b57cec5SDimitry Andric   const Module *M = Asm->MMI->getModule();
86e8d8bef9SDimitry Andric   std::vector<const MCSymbol *> GFIDsEntries;
87e8d8bef9SDimitry Andric   std::vector<const MCSymbol *> GIATsEntries;
88e8d8bef9SDimitry Andric   for (const Function &F : *M) {
89e8d8bef9SDimitry Andric     if (isPossibleIndirectCallTarget(&F)) {
90e8d8bef9SDimitry Andric       // If F is a dllimport and has an "__imp_" symbol already defined, add the
91e8d8bef9SDimitry Andric       // "__imp_" symbol to the .giats section.
92e8d8bef9SDimitry Andric       if (F.hasDLLImportStorageClass()) {
93e8d8bef9SDimitry Andric         if (MCSymbol *impSym = lookupImpSymbol(Asm->getSymbol(&F))) {
94e8d8bef9SDimitry Andric           GIATsEntries.push_back(impSym);
95e8d8bef9SDimitry Andric         }
96e8d8bef9SDimitry Andric       }
97e8d8bef9SDimitry Andric       // Add the function's symbol to the .gfids section.
98e8d8bef9SDimitry Andric       // Note: For dllimport functions, MSVC sometimes does not add this symbol
99e8d8bef9SDimitry Andric       // to the .gfids section, but only adds the corresponding "__imp_" symbol
100e8d8bef9SDimitry Andric       // to the .giats section. Here we always add the symbol to the .gfids
101e8d8bef9SDimitry Andric       // section, since this does not introduce security risks.
102e8d8bef9SDimitry Andric       GFIDsEntries.push_back(Asm->getSymbol(&F));
103e8d8bef9SDimitry Andric     }
104e8d8bef9SDimitry Andric   }
105e8d8bef9SDimitry Andric 
106e8d8bef9SDimitry Andric   if (GFIDsEntries.empty() && GIATsEntries.empty() && LongjmpTargets.empty())
1070b57cec5SDimitry Andric     return;
108e8d8bef9SDimitry Andric 
109e8d8bef9SDimitry Andric   // Emit the symbol index of each GFIDs entry to form the .gfids section.
1100b57cec5SDimitry Andric   auto &OS = *Asm->OutStreamer;
11181ad6265SDimitry Andric   OS.switchSection(Asm->OutContext.getObjectFileInfo()->getGFIDsSection());
112e8d8bef9SDimitry Andric   for (const MCSymbol *S : GFIDsEntries)
11381ad6265SDimitry Andric     OS.emitCOFFSymbolIndex(S);
114480093f4SDimitry Andric 
115e8d8bef9SDimitry Andric   // Emit the symbol index of each GIATs entry to form the .giats section.
11681ad6265SDimitry Andric   OS.switchSection(Asm->OutContext.getObjectFileInfo()->getGIATsSection());
117e8d8bef9SDimitry Andric   for (const MCSymbol *S : GIATsEntries) {
11881ad6265SDimitry Andric     OS.emitCOFFSymbolIndex(S);
119e8d8bef9SDimitry Andric   }
120e8d8bef9SDimitry Andric 
121e8d8bef9SDimitry Andric   // Emit the symbol index of each longjmp target to form the .gljmp section.
12281ad6265SDimitry Andric   OS.switchSection(Asm->OutContext.getObjectFileInfo()->getGLJMPSection());
123480093f4SDimitry Andric   for (const MCSymbol *S : LongjmpTargets) {
12481ad6265SDimitry Andric     OS.emitCOFFSymbolIndex(S);
125480093f4SDimitry Andric   }
1260b57cec5SDimitry Andric }
127