1db2736ddSAdrian McCarthy //===-- CodeGen/AsmPrinter/WinCFGuard.cpp - Control Flow Guard Impl ------===//
2db2736ddSAdrian McCarthy //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6db2736ddSAdrian McCarthy //
7db2736ddSAdrian McCarthy //===----------------------------------------------------------------------===//
8db2736ddSAdrian McCarthy //
9d157a9bcSAndrew Paverd // This file contains support for writing the metadata for Windows Control Flow
100139c8afSAndrew Paverd // Guard, including address-taken functions and valid longjmp targets.
11db2736ddSAdrian McCarthy //
12db2736ddSAdrian McCarthy //===----------------------------------------------------------------------===//
13db2736ddSAdrian McCarthy
14db2736ddSAdrian McCarthy #include "WinCFGuard.h"
15db2736ddSAdrian McCarthy #include "llvm/CodeGen/AsmPrinter.h"
16db2736ddSAdrian McCarthy #include "llvm/CodeGen/MachineFunction.h"
17db2736ddSAdrian McCarthy #include "llvm/CodeGen/MachineModuleInfo.h"
18db2736ddSAdrian McCarthy #include "llvm/IR/Constants.h"
19ed98c1b3Sserge-sans-paille #include "llvm/IR/InstrTypes.h"
20db2736ddSAdrian McCarthy #include "llvm/MC/MCObjectFileInfo.h"
21db2736ddSAdrian McCarthy #include "llvm/MC/MCStreamer.h"
22db2736ddSAdrian McCarthy
23db2736ddSAdrian McCarthy #include <vector>
24db2736ddSAdrian McCarthy
25db2736ddSAdrian McCarthy using namespace llvm;
26db2736ddSAdrian McCarthy
WinCFGuard(AsmPrinter * A)27b932bdf5SKazu Hirata WinCFGuard::WinCFGuard(AsmPrinter *A) : Asm(A) {}
28db2736ddSAdrian McCarthy
293a8c5148SKazu Hirata WinCFGuard::~WinCFGuard() = default;
30db2736ddSAdrian McCarthy
endFunction(const MachineFunction * MF)31d157a9bcSAndrew Paverd void WinCFGuard::endFunction(const MachineFunction *MF) {
32d157a9bcSAndrew Paverd
33d157a9bcSAndrew Paverd // Skip functions without any longjmp targets.
34d157a9bcSAndrew Paverd if (MF->getLongjmpTargets().empty())
35d157a9bcSAndrew Paverd return;
36d157a9bcSAndrew Paverd
37d157a9bcSAndrew Paverd // Copy the function's longjmp targets to a module-level list.
381e3ed091SKazu Hirata llvm::append_range(LongjmpTargets, MF->getLongjmpTargets());
39d157a9bcSAndrew Paverd }
40d157a9bcSAndrew Paverd
41f5d935c1SReid Kleckner /// Returns true if this function's address is escaped in a way that might make
42f5d935c1SReid Kleckner /// it an indirect call target. Function::hasAddressTaken gives different
43f5d935c1SReid Kleckner /// results when a function is called directly with a function prototype
44f5d935c1SReid Kleckner /// mismatch, which requires a cast.
isPossibleIndirectCallTarget(const Function * F)45f5d935c1SReid Kleckner static bool isPossibleIndirectCallTarget(const Function *F) {
46f5d935c1SReid Kleckner SmallVector<const Value *, 4> Users{F};
47f5d935c1SReid Kleckner while (!Users.empty()) {
48f5d935c1SReid Kleckner const Value *FnOrCast = Users.pop_back_val();
49f5d935c1SReid Kleckner for (const Use &U : FnOrCast->uses()) {
50f5d935c1SReid Kleckner const User *FnUser = U.getUser();
51f5d935c1SReid Kleckner if (isa<BlockAddress>(FnUser))
52f5d935c1SReid Kleckner continue;
53f5d935c1SReid Kleckner if (const auto *Call = dyn_cast<CallBase>(FnUser)) {
54f5d935c1SReid Kleckner if (!Call->isCallee(&U))
55f5d935c1SReid Kleckner return true;
56f5d935c1SReid Kleckner } else if (isa<Instruction>(FnUser)) {
57f5d935c1SReid Kleckner // Consider any other instruction to be an escape. This has some weird
58f5d935c1SReid Kleckner // consequences like no-op intrinsics being an escape or a store *to* a
59f5d935c1SReid Kleckner // function address being an escape.
60f5d935c1SReid Kleckner return true;
61f5d935c1SReid Kleckner } else if (const auto *C = dyn_cast<Constant>(FnUser)) {
62f5d935c1SReid Kleckner // If this is a constant pointer cast of the function, don't consider
63f5d935c1SReid Kleckner // this escape. Analyze the uses of the cast as well. This ensures that
64f5d935c1SReid Kleckner // direct calls with mismatched prototypes don't end up in the CFG
65f5d935c1SReid Kleckner // table. Consider other constants, such as vtable initializers, to
66f5d935c1SReid Kleckner // escape the function.
67f5d935c1SReid Kleckner if (C->stripPointerCasts() == F)
68f5d935c1SReid Kleckner Users.push_back(FnUser);
69f5d935c1SReid Kleckner else
70f5d935c1SReid Kleckner return true;
71f5d935c1SReid Kleckner }
72f5d935c1SReid Kleckner }
73f5d935c1SReid Kleckner }
74f5d935c1SReid Kleckner return false;
75f5d935c1SReid Kleckner }
76f5d935c1SReid Kleckner
lookupImpSymbol(const MCSymbol * Sym)770139c8afSAndrew Paverd MCSymbol *WinCFGuard::lookupImpSymbol(const MCSymbol *Sym) {
780139c8afSAndrew Paverd if (Sym->getName().startswith("__imp_"))
790139c8afSAndrew Paverd return nullptr;
800139c8afSAndrew Paverd return Asm->OutContext.lookupSymbol(Twine("__imp_") + Sym->getName());
810139c8afSAndrew Paverd }
820139c8afSAndrew Paverd
endModule()83db2736ddSAdrian McCarthy void WinCFGuard::endModule() {
84db2736ddSAdrian McCarthy const Module *M = Asm->MMI->getModule();
850139c8afSAndrew Paverd std::vector<const MCSymbol *> GFIDsEntries;
860139c8afSAndrew Paverd std::vector<const MCSymbol *> GIATsEntries;
870139c8afSAndrew Paverd for (const Function &F : *M) {
880139c8afSAndrew Paverd if (isPossibleIndirectCallTarget(&F)) {
890139c8afSAndrew Paverd // If F is a dllimport and has an "__imp_" symbol already defined, add the
900139c8afSAndrew Paverd // "__imp_" symbol to the .giats section.
910139c8afSAndrew Paverd if (F.hasDLLImportStorageClass()) {
920139c8afSAndrew Paverd if (MCSymbol *impSym = lookupImpSymbol(Asm->getSymbol(&F))) {
930139c8afSAndrew Paverd GIATsEntries.push_back(impSym);
940139c8afSAndrew Paverd }
950139c8afSAndrew Paverd }
960139c8afSAndrew Paverd // Add the function's symbol to the .gfids section.
970139c8afSAndrew Paverd // Note: For dllimport functions, MSVC sometimes does not add this symbol
980139c8afSAndrew Paverd // to the .gfids section, but only adds the corresponding "__imp_" symbol
990139c8afSAndrew Paverd // to the .giats section. Here we always add the symbol to the .gfids
1000139c8afSAndrew Paverd // section, since this does not introduce security risks.
1010139c8afSAndrew Paverd GFIDsEntries.push_back(Asm->getSymbol(&F));
1020139c8afSAndrew Paverd }
1030139c8afSAndrew Paverd }
1040139c8afSAndrew Paverd
1050139c8afSAndrew Paverd if (GFIDsEntries.empty() && GIATsEntries.empty() && LongjmpTargets.empty())
106db2736ddSAdrian McCarthy return;
1070139c8afSAndrew Paverd
1080139c8afSAndrew Paverd // Emit the symbol index of each GFIDs entry to form the .gfids section.
109db2736ddSAdrian McCarthy auto &OS = *Asm->OutStreamer;
110*adf4142fSFangrui Song OS.switchSection(Asm->OutContext.getObjectFileInfo()->getGFIDsSection());
1110139c8afSAndrew Paverd for (const MCSymbol *S : GFIDsEntries)
1129ee15bbaSFangrui Song OS.emitCOFFSymbolIndex(S);
113d157a9bcSAndrew Paverd
1140139c8afSAndrew Paverd // Emit the symbol index of each GIATs entry to form the .giats section.
115*adf4142fSFangrui Song OS.switchSection(Asm->OutContext.getObjectFileInfo()->getGIATsSection());
1160139c8afSAndrew Paverd for (const MCSymbol *S : GIATsEntries) {
1179ee15bbaSFangrui Song OS.emitCOFFSymbolIndex(S);
1180139c8afSAndrew Paverd }
1190139c8afSAndrew Paverd
1200139c8afSAndrew Paverd // Emit the symbol index of each longjmp target to form the .gljmp section.
121*adf4142fSFangrui Song OS.switchSection(Asm->OutContext.getObjectFileInfo()->getGLJMPSection());
122d157a9bcSAndrew Paverd for (const MCSymbol *S : LongjmpTargets) {
1239ee15bbaSFangrui Song OS.emitCOFFSymbolIndex(S);
124d157a9bcSAndrew Paverd }
125db2736ddSAdrian McCarthy }
126