LLVM 20.0.0git
CFGuardLongjmp.cpp
Go to the documentation of this file.
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/ADT/Statistic.h"
23#include "llvm/CodeGen/Passes.h"
24#include "llvm/IR/Module.h"
26
27using namespace llvm;
28
29#define DEBUG_TYPE "cfguard-longjmp"
30
31STATISTIC(CFGuardLongjmpTargets,
32 "Number of Control Flow Guard longjmp targets");
33
34namespace {
35
36/// MachineFunction pass to insert a symbol after each call to _setjmp and store
37/// this in the MachineFunction's LongjmpTargets vector.
38class CFGuardLongjmp : public MachineFunctionPass {
39public:
40 static char ID;
41
42 CFGuardLongjmp() : MachineFunctionPass(ID) {
44 }
45
46 StringRef getPassName() const override {
47 return "Control Flow Guard longjmp targets";
48 }
49
50 bool runOnMachineFunction(MachineFunction &MF) override;
51};
52
53} // end anonymous namespace
54
55char CFGuardLongjmp::ID = 0;
56
57INITIALIZE_PASS(CFGuardLongjmp, "CFGuardLongjmp",
58 "Insert symbols at valid longjmp targets for /guard:cf", false,
59 false)
60FunctionPass *llvm::createCFGuardLongjmpPass() { return new CFGuardLongjmp(); }
61
62bool CFGuardLongjmp::runOnMachineFunction(MachineFunction &MF) {
63
64 // Skip modules for which the cfguard flag is not set.
65 if (!MF.getFunction().getParent()->getModuleFlag("cfguard"))
66 return false;
67
68 // Skip functions that do not have calls to _setjmp.
70 return false;
71
73
74 // Iterate over all instructions in the function and add calls to functions
75 // that return twice to the list of targets.
76 for (MachineBasicBlock &MBB : MF) {
77 for (MachineInstr &MI : MBB) {
78
79 // Skip instructions that are not calls.
80 if (!MI.isCall() || MI.getNumOperands() < 1)
81 continue;
82
83 // Iterate over operands to find calls to global functions.
84 for (MachineOperand &MO : MI.operands()) {
85 if (!MO.isGlobal())
86 continue;
87
88 auto *F = dyn_cast<Function>(MO.getGlobal());
89 if (!F)
90 continue;
91
92 // If the instruction calls a function that returns twice, add
93 // it to the list of targets.
94 if (F->hasFnAttribute(Attribute::ReturnsTwice)) {
95 SetjmpCalls.push_back(&MI);
96 break;
97 }
98 }
99 }
100 }
101
102 if (SetjmpCalls.empty())
103 return false;
104
105 unsigned SetjmpNum = 0;
106
107 // For each possible target, create a new symbol and insert it immediately
108 // after the call to setjmp. Add this symbol to the MachineFunction's list
109 // of longjmp targets.
110 for (MachineInstr *Setjmp : SetjmpCalls) {
112 raw_svector_ostream(SymbolName) << "$cfgsj_" << MF.getName() << SetjmpNum++;
113 MCSymbol *SjSymbol = MF.getContext().getOrCreateSymbol(SymbolName);
114
115 Setjmp->setPostInstrSymbol(MF, SjSymbol);
116 MF.addLongjmpTarget(SjSymbol);
117 CFGuardLongjmpTargets++;
118 }
119
120 return true;
121}
MachineBasicBlock & MBB
IRTranslator LLVM IR MI
Module.h This file contains the declarations for the Module class.
#define F(x, y, z)
Definition: MD5.cpp:55
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition: Statistic.h:166
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:310
bool callsFunctionThatReturnsTwice() const
callsFunctionThatReturnsTwice - Return true if the function has a call to setjmp or other function th...
Definition: Function.cpp:1039
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:656
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
Function & getFunction()
Return the LLVM function that this machine code represents.
Representation of each machine instruction.
Definition: MachineInstr.h:69
MachineOperand class - Representation of each machine instruction operand.
Metadata * getModuleFlag(StringRef Key) const
Return the corresponding value if Key appears in module flags, otherwise return null.
Definition: Module.cpp:354
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
bool empty() const
Definition: SmallVector.h:81
void push_back(const T &Elt)
Definition: SmallVector.h:413
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:691
constexpr char SymbolName[]
Key for Kernel::Metadata::mSymbolName.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void initializeCFGuardLongjmpPass(PassRegistry &)
FunctionPass * createCFGuardLongjmpPass()
Creates CFGuard longjmp target identification pass.