blob: ab401fa02586e236411fb0d6bcbca2cf155cee8e [file] [log] [blame]
Oren Ben Simhon1c6308e2018-01-09 08:51:18 +00001//===---- 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"
23#include "llvm/CodeGen/MachineFunctionPass.h"
24#include "llvm/CodeGen/MachineInstrBuilder.h"
Oren Ben Simhon1c6308e2018-01-09 08:51:18 +000025#include "llvm/CodeGen/MachineModuleInfo.h"
26
27using namespace llvm;
28
29#define DEBUG_TYPE "x86-indirect-branch-tracking"
30
31static cl::opt<bool> IndirectBranchTracking(
32 "x86-indirect-branch-tracking", cl::init(false), cl::Hidden,
33 cl::desc("Enable X86 indirect branch tracking pass."));
34
35STATISTIC(NumEndBranchAdded, "Number of ENDBR instructions added");
36
37namespace {
38class X86IndirectBranchTrackingPass : public MachineFunctionPass {
39public:
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
48private:
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
Oren Ben Simhon1c6308e2018-01-09 08:51:18 +000057 /// 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.
Oren Ben Simhonfdd72fd2018-03-17 13:29:46 +000060 /// \returns true if the ENDBR was added and false otherwise.
61 bool addENDBR(MachineBasicBlock &MBB) const;
Oren Ben Simhon1c6308e2018-01-09 08:51:18 +000062};
63
64} // end anonymous namespace
65
66char X86IndirectBranchTrackingPass::ID = 0;
67
68FunctionPass *llvm::createX86IndirectBranchTrackingPass() {
69 return new X86IndirectBranchTrackingPass();
70}
71
Oren Ben Simhonfdd72fd2018-03-17 13:29:46 +000072bool X86IndirectBranchTrackingPass::addENDBR(MachineBasicBlock &MBB) const {
Oren Ben Simhon1c6308e2018-01-09 08:51:18 +000073 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++;
Oren Ben Simhonfdd72fd2018-03-17 13:29:46 +000083 return true;
Oren Ben Simhon1c6308e2018-01-09 08:51:18 +000084 }
Oren Ben Simhonfdd72fd2018-03-17 13:29:46 +000085
86 return false;
Oren Ben Simhon1c6308e2018-01-09 08:51:18 +000087}
88
89bool X86IndirectBranchTrackingPass::runOnMachineFunction(MachineFunction &MF) {
90 const X86Subtarget &SubTarget = MF.getSubtarget<X86Subtarget>();
91
Oren Ben Simhonfdd72fd2018-03-17 13:29:46 +000092 // Make sure that the target supports IBT instruction.
Oren Ben Simhon1c6308e2018-01-09 08:51:18 +000093 if (!SubTarget.hasIBT())
94 return false;
95
96 // Check that the cf-protection-branch is enabled.
97 Metadata *isCFProtectionSupported =
98 MF.getMMI().getModule()->getModuleFlag("cf-protection-branch");
99 if (!isCFProtectionSupported && !IndirectBranchTracking)
100 return false;
101
102 // True if the current MF was changed and false otherwise.
103 bool Changed = false;
104
105 TII = SubTarget.getInstrInfo();
106 EndbrOpcode = SubTarget.is64Bit() ? X86::ENDBR64 : X86::ENDBR32;
107
108 // Non-internal function or function whose address was taken, can be
Oren Ben Simhonfdd72fd2018-03-17 13:29:46 +0000109 // accessed through indirect calls. Mark the first BB with ENDBR instruction
110 // unless nocf_check attribute is used.
111 if ((MF.getFunction().hasAddressTaken() ||
112 !MF.getFunction().hasLocalLinkage()) &&
113 !MF.getFunction().doesNoCfCheck()) {
Oren Ben Simhon1c6308e2018-01-09 08:51:18 +0000114 auto MBB = MF.begin();
Oren Ben Simhonfdd72fd2018-03-17 13:29:46 +0000115 Changed |= addENDBR(*MBB);
Oren Ben Simhon1c6308e2018-01-09 08:51:18 +0000116 }
117
Oren Ben Simhonfdd72fd2018-03-17 13:29:46 +0000118 for (auto &MBB : MF)
119 // Find all basic blocks that their address was taken (for example
Oren Ben Simhon1c6308e2018-01-09 08:51:18 +0000120 // in the case of indirect jump) and add ENDBR instruction.
Oren Ben Simhonfdd72fd2018-03-17 13:29:46 +0000121 if (MBB.hasAddressTaken())
122 Changed |= addENDBR(MBB);
Oren Ben Simhon1c6308e2018-01-09 08:51:18 +0000123
124 return Changed;
125}