1//===-- CFGuardLongjmp.cpp - Longjmp symbols for CFGuard --------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file contains a machine function pass to insert a symbol after each
11/// call to _setjmp and store this in the MachineFunction's LongjmpTargets
12/// vector. This will be used to emit the table of valid longjmp targets used
13/// by Control Flow Guard.
14///
15//===----------------------------------------------------------------------===//
16
17#include "llvm/CodeGen/CFGuardLongjmp.h"
18#include "llvm/ADT/Statistic.h"
19#include "llvm/CodeGen/MachineBasicBlock.h"
20#include "llvm/CodeGen/MachineFunctionAnalysisManager.h"
21#include "llvm/CodeGen/MachineFunctionPass.h"
22#include "llvm/CodeGen/MachineInstr.h"
23#include "llvm/CodeGen/MachineModuleInfo.h"
24#include "llvm/CodeGen/MachineOperand.h"
25#include "llvm/CodeGen/MachinePassManager.h"
26#include "llvm/CodeGen/Passes.h"
27#include "llvm/IR/Analysis.h"
28#include "llvm/IR/Module.h"
29#include "llvm/InitializePasses.h"
30#include "llvm/Pass.h"
31
32using namespace llvm;
33
34#define DEBUG_TYPE "cfguard-longjmp"
35
36STATISTIC(CFGuardLongjmpTargets,
37 "Number of Control Flow Guard longjmp targets");
38
39namespace {
40
41/// MachineFunction pass to insert a symbol after each call to _setjmp and store
42/// this in the MachineFunction's LongjmpTargets vector.
43class CFGuardLongjmpLegacy : public MachineFunctionPass {
44public:
45 static char ID;
46
47 CFGuardLongjmpLegacy() : MachineFunctionPass(ID) {}
48
49 StringRef getPassName() const override {
50 return "Control Flow Guard longjmp targets";
51 }
52
53 bool runOnMachineFunction(MachineFunction &MF) override;
54
55 void getAnalysisUsage(AnalysisUsage &AU) const override {
56 AU.setPreservesCFG();
57 MachineFunctionPass::getAnalysisUsage(AU);
58 }
59};
60
61} // end anonymous namespace
62
63char CFGuardLongjmpLegacy::ID = 0;
64
65INITIALIZE_PASS(CFGuardLongjmpLegacy, "CFGuardLongjmp",
66 "Insert symbols at valid longjmp targets for /guard:cf", false,
67 false)
68FunctionPass *llvm::createCFGuardLongjmpLegacyPass() {
69 return new CFGuardLongjmpLegacy();
70}
71
72static bool runCFGuardLongjmpOnMachineFunction(MachineFunction &MF) {
73
74 // Skip modules for which the cfguard flag is not set.
75 if (MF.getFunction().getParent()->getControlFlowGuardMode() ==
76 ControlFlowGuardMode::Disabled)
77 return false;
78
79 // Skip functions that do not have calls to _setjmp.
80 if (!MF.getFunction().callsFunctionThatReturnsTwice())
81 return false;
82
83 SmallVector<MachineInstr *, 8> SetjmpCalls;
84
85 // Iterate over all instructions in the function and add calls to functions
86 // that return twice to the list of targets.
87 for (MachineBasicBlock &MBB : MF) {
88 for (MachineInstr &MI : MBB) {
89
90 // Skip instructions that are not calls.
91 if (!MI.isCall() || MI.getNumOperands() < 1)
92 continue;
93
94 // Iterate over operands to find calls to global functions.
95 for (MachineOperand &MO : MI.operands()) {
96 if (!MO.isGlobal())
97 continue;
98
99 auto *F = dyn_cast<Function>(Val: MO.getGlobal());
100 if (!F)
101 continue;
102
103 // If the instruction calls a function that returns twice, add
104 // it to the list of targets.
105 if (F->hasFnAttribute(Kind: Attribute::ReturnsTwice)) {
106 SetjmpCalls.push_back(Elt: &MI);
107 break;
108 }
109 }
110 }
111 }
112
113 if (SetjmpCalls.empty())
114 return false;
115
116 unsigned SetjmpNum = 0;
117
118 // For each possible target, create a new symbol and insert it immediately
119 // after the call to setjmp. Add this symbol to the MachineFunction's list
120 // of longjmp targets.
121 for (MachineInstr *Setjmp : SetjmpCalls) {
122 SmallString<128> SymbolName;
123 raw_svector_ostream(SymbolName) << "$cfgsj_" << MF.getName() << SetjmpNum++;
124 MCSymbol *SjSymbol = MF.getContext().getOrCreateSymbol(Name: SymbolName);
125
126 Setjmp->setPostInstrSymbol(MF, Symbol: SjSymbol);
127 MF.addLongjmpTarget(Target: SjSymbol);
128 CFGuardLongjmpTargets++;
129 }
130
131 return true;
132}
133
134bool CFGuardLongjmpLegacy::runOnMachineFunction(MachineFunction &MF) {
135 return runCFGuardLongjmpOnMachineFunction(MF);
136}
137
138PreservedAnalyses
139CFGuardLongjmpPass::run(MachineFunction &MF,
140 MachineFunctionAnalysisManager &MFAM) {
141 return runCFGuardLongjmpOnMachineFunction(MF)
142 ? getMachineFunctionPassPreservedAnalyses()
143 .preserveSet<CFGAnalyses>()
144 : PreservedAnalyses::all();
145}
146