LLVM  12.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/InitializePasses.h"
25 
26 using namespace llvm;
27 
28 #define DEBUG_TYPE "cfguard-longjmp"
29 
30 STATISTIC(CFGuardLongjmpTargets,
31  "Number of Control Flow Guard longjmp targets");
32 
33 namespace {
34 
35 /// MachineFunction pass to insert a symbol after each call to _setjmp and store
36 /// this in the MachineFunction's LongjmpTargets vector.
37 class CFGuardLongjmp : public MachineFunctionPass {
38 public:
39  static char ID;
40 
41  CFGuardLongjmp() : MachineFunctionPass(ID) {
43  }
44 
45  StringRef getPassName() const override {
46  return "Control Flow Guard longjmp targets";
47  }
48 
49  bool runOnMachineFunction(MachineFunction &MF) override;
50 };
51 
52 } // end anonymous namespace
53 
54 char CFGuardLongjmp::ID = 0;
55 
56 INITIALIZE_PASS(CFGuardLongjmp, "CFGuardLongjmp",
57  "Insert symbols at valid longjmp targets for /guard:cf", false,
58  false)
59 FunctionPass *llvm::createCFGuardLongjmpPass() { return new CFGuardLongjmp(); }
60 
61 bool CFGuardLongjmp::runOnMachineFunction(MachineFunction &MF) {
62 
63  // Skip modules for which the cfguard flag is not set.
64  if (!MF.getMMI().getModule()->getModuleFlag("cfguard"))
65  return false;
66 
67  // Skip functions that do not have calls to _setjmp.
69  return false;
70 
72 
73  // Iterate over all instructions in the function and add calls to functions
74  // that return twice to the list of targets.
75  for (MachineBasicBlock &MBB : MF) {
76  for (MachineInstr &MI : MBB) {
77 
78  // Skip instructions that are not calls.
79  if (!MI.isCall() || MI.getNumOperands() < 1)
80  continue;
81 
82  // Iterate over operands to find calls to global functions.
83  for (MachineOperand &MO : MI.operands()) {
84  if (!MO.isGlobal())
85  continue;
86 
87  auto *F = dyn_cast<Function>(MO.getGlobal());
88  if (!F)
89  continue;
90 
91  // If the instruction calls a function that returns twice, add
92  // it to the list of targets.
93  if (F->hasFnAttribute(Attribute::ReturnsTwice)) {
94  SetjmpCalls.push_back(&MI);
95  break;
96  }
97  }
98  }
99  }
100 
101  if (SetjmpCalls.empty())
102  return false;
103 
104  unsigned SetjmpNum = 0;
105 
106  // For each possible target, create a new symbol and insert it immediately
107  // after the call to setjmp. Add this symbol to the MachineFunction's list
108  // of longjmp targets.
109  for (MachineInstr *Setjmp : SetjmpCalls) {
111  raw_svector_ostream(SymbolName) << "$cfgsj_" << MF.getName() << SetjmpNum++;
112  MCSymbol *SjSymbol = MF.getContext().getOrCreateSymbol(SymbolName);
113 
114  Setjmp->setPostInstrSymbol(MF, SjSymbol);
115  MF.addLongjmpTarget(SjSymbol);
116  CFGuardLongjmpTargets++;
117  }
118 
119  return true;
120 }
LLVM_NODISCARD std::enable_if_t< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type > dyn_cast(const Y &Val)
Definition: Casting.h:334
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
This class represents lattice values for constants.
Definition: AllocatorList.h:23
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
void initializeCFGuardLongjmpPass(PassRegistry &)
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:550
STATISTIC(NumFunctions, "Total number of functions")
F(f)
MachineModuleInfo & getMMI() const
Function & getFunction()
Return the LLVM function that this machine code represents.
MachineBasicBlock & MBB
FunctionPass * createCFGuardLongjmpPass()
Creates CFGuard longjmp target identification pass.
constexpr char SymbolName[]
Key for Kernel::Metadata::mSymbolName.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
Metadata * getModuleFlag(StringRef Key) const
Return the corresponding value if Key appears in module flags, otherwise return null.
Definition: Module.cpp:321
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:284
MachineOperand class - Representation of each machine instruction operand.
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:883
Representation of each machine instruction.
Definition: MachineInstr.h:62
INITIALIZE_PASS(CFGuardLongjmp, "CFGuardLongjmp", "Insert symbols at valid longjmp targets for /guard:cf", false, false) FunctionPass *llvm
const Module * getModule() const
bool callsFunctionThatReturnsTwice() const
callsFunctionThatReturnsTwice - Return true if the function has a call to setjmp or other function th...
Definition: Function.cpp:1534
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57