LLVM  8.0.1
X86IndirectBranchTracking.cpp
Go to the documentation of this file.
1 //===---- X86IndirectBranchTracking.cpp - Enables CET IBT mechanism -------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines a pass that enables Indirect Branch Tracking (IBT) as part
11 // of Control-Flow Enforcement Technology (CET).
12 // The pass adds ENDBR (End Branch) machine instructions at the beginning of
13 // each basic block or function that is referenced by an indrect jump/call
14 // instruction.
15 // The ENDBR instructions have a NOP encoding and as such are ignored in
16 // targets that do not support CET IBT mechanism.
17 //===----------------------------------------------------------------------===//
18 
19 #include "X86.h"
20 #include "X86InstrInfo.h"
21 #include "X86Subtarget.h"
22 #include "llvm/ADT/Statistic.h"
26 
27 using namespace llvm;
28 
29 #define DEBUG_TYPE "x86-indirect-branch-tracking"
30 
32  "x86-indirect-branch-tracking", cl::init(false), cl::Hidden,
33  cl::desc("Enable X86 indirect branch tracking pass."));
34 
35 STATISTIC(NumEndBranchAdded, "Number of ENDBR instructions added");
36 
37 namespace {
38 class X86IndirectBranchTrackingPass : public MachineFunctionPass {
39 public:
40  X86IndirectBranchTrackingPass() : MachineFunctionPass(ID) {}
41 
42  StringRef getPassName() const override {
43  return "X86 Indirect Branch Tracking";
44  }
45 
46  bool runOnMachineFunction(MachineFunction &MF) override;
47 
48 private:
49  static char ID;
50 
51  /// Machine instruction info used throughout the class.
52  const X86InstrInfo *TII;
53 
54  /// Endbr opcode for the current machine function.
55  unsigned int EndbrOpcode;
56 
57  /// Adds a new ENDBR instruction to the begining of the MBB.
58  /// The function will not add it if already exists.
59  /// It will add ENDBR32 or ENDBR64 opcode, depending on the target.
60  /// \returns true if the ENDBR was added and false otherwise.
61  bool addENDBR(MachineBasicBlock &MBB) const;
62 };
63 
64 } // end anonymous namespace
65 
67 
69  return new X86IndirectBranchTrackingPass();
70 }
71 
72 bool X86IndirectBranchTrackingPass::addENDBR(MachineBasicBlock &MBB) const {
73  assert(TII && "Target instruction info was not initialized");
74  assert((X86::ENDBR64 == EndbrOpcode || X86::ENDBR32 == EndbrOpcode) &&
75  "Unexpected Endbr opcode");
76 
77  auto MI = MBB.begin();
78  // If the MBB is empty or the first instruction is not ENDBR,
79  // add the ENDBR instruction to the beginning of the MBB.
80  if (MI == MBB.end() || EndbrOpcode != MI->getOpcode()) {
81  BuildMI(MBB, MI, MBB.findDebugLoc(MI), TII->get(EndbrOpcode));
82  NumEndBranchAdded++;
83  return true;
84  }
85 
86  return false;
87 }
88 
89 bool X86IndirectBranchTrackingPass::runOnMachineFunction(MachineFunction &MF) {
91 
92  // Check that the cf-protection-branch is enabled.
93  Metadata *isCFProtectionSupported =
94  MF.getMMI().getModule()->getModuleFlag("cf-protection-branch");
95  if (!isCFProtectionSupported && !IndirectBranchTracking)
96  return false;
97 
98  // True if the current MF was changed and false otherwise.
99  bool Changed = false;
100 
101  TII = SubTarget.getInstrInfo();
102  EndbrOpcode = SubTarget.is64Bit() ? X86::ENDBR64 : X86::ENDBR32;
103 
104  // Non-internal function or function whose address was taken, can be
105  // accessed through indirect calls. Mark the first BB with ENDBR instruction
106  // unless nocf_check attribute is used.
107  if ((MF.getFunction().hasAddressTaken() ||
108  !MF.getFunction().hasLocalLinkage()) &&
109  !MF.getFunction().doesNoCfCheck()) {
110  auto MBB = MF.begin();
111  Changed |= addENDBR(*MBB);
112  }
113 
114  for (auto &MBB : MF)
115  // Find all basic blocks that their address was taken (for example
116  // in the case of indirect jump) and add ENDBR instruction.
117  if (MBB.hasAddressTaken())
118  Changed |= addENDBR(MBB);
119 
120  return Changed;
121 }
bool is64Bit() const
Is this x86_64? (disregarding specific ABI / programming model)
Definition: X86Subtarget.h:522
bool hasLocalLinkage() const
Definition: GlobalValue.h:436
This class represents lattice values for constants.
Definition: AllocatorList.h:24
const X86InstrInfo * getInstrInfo() const override
Definition: X86Subtarget.h:481
STATISTIC(NumFunctions, "Total number of functions")
MachineModuleInfo & getMMI() const
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
static cl::opt< bool > IndirectBranchTracking("x86-indirect-branch-tracking", cl::init(false), cl::Hidden, cl::desc("Enable X86 indirect branch tracking pass."))
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:423
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Metadata * getModuleFlag(StringRef Key) const
Return the corresponding value if Key appears in module flags, otherwise return null.
Definition: Module.cpp:312
DebugLoc findDebugLoc(instr_iterator MBBI)
Find the next valid DebugLoc starting at MBBI, skipping any DBG_VALUE and DBG_LABEL instructions...
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:285
bool hasAddressTaken() const
Test whether this block is potentially the target of an indirect branch.
FunctionPass * createX86IndirectBranchTrackingPass()
This pass inserts ENDBR instructions before indirect jump/call destinations as part of CET IBT mechan...
const Function & getFunction() const
Return the LLVM function that this machine code represents.
const Module * getModule() const
bool hasAddressTaken(const User **=nullptr) const
hasAddressTaken - returns true if there are any uses of this function other than direct calls or invo...
Definition: Function.cpp:1254
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
bool doesNoCfCheck() const
Determine if the function should not perform indirect branch tracking.
Definition: Function.h:517
Root of the metadata hierarchy.
Definition: Metadata.h:58