blob: 35866d74edb005b0bca4443726f243b256ee5998 [file] [log] [blame]
Oren Ben Simhon1c6308e2018-01-09 08:51:18 +00001//===---- X86IndirectBranchTracking.cpp - Enables CET IBT mechanism -------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Oren Ben Simhon1c6308e2018-01-09 08:51:18 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file defines a pass that enables Indirect Branch Tracking (IBT) as part
10// of Control-Flow Enforcement Technology (CET).
11// The pass adds ENDBR (End Branch) machine instructions at the beginning of
12// each basic block or function that is referenced by an indrect jump/call
13// instruction.
14// The ENDBR instructions have a NOP encoding and as such are ignored in
15// targets that do not support CET IBT mechanism.
16//===----------------------------------------------------------------------===//
17
18#include "X86.h"
19#include "X86InstrInfo.h"
20#include "X86Subtarget.h"
21#include "llvm/ADT/Statistic.h"
22#include "llvm/CodeGen/MachineFunctionPass.h"
23#include "llvm/CodeGen/MachineInstrBuilder.h"
Oren Ben Simhon1c6308e2018-01-09 08:51:18 +000024#include "llvm/CodeGen/MachineModuleInfo.h"
25
26using namespace llvm;
27
28#define DEBUG_TYPE "x86-indirect-branch-tracking"
29
30static cl::opt<bool> IndirectBranchTracking(
31 "x86-indirect-branch-tracking", cl::init(false), cl::Hidden,
32 cl::desc("Enable X86 indirect branch tracking pass."));
33
34STATISTIC(NumEndBranchAdded, "Number of ENDBR instructions added");
35
36namespace {
37class X86IndirectBranchTrackingPass : public MachineFunctionPass {
38public:
39 X86IndirectBranchTrackingPass() : MachineFunctionPass(ID) {}
40
41 StringRef getPassName() const override {
42 return "X86 Indirect Branch Tracking";
43 }
44
45 bool runOnMachineFunction(MachineFunction &MF) override;
46
47private:
48 static char ID;
49
50 /// Machine instruction info used throughout the class.
51 const X86InstrInfo *TII;
52
53 /// Endbr opcode for the current machine function.
54 unsigned int EndbrOpcode;
55
Oren Ben Simhon1c6308e2018-01-09 08:51:18 +000056 /// Adds a new ENDBR instruction to the begining of the MBB.
57 /// The function will not add it if already exists.
58 /// It will add ENDBR32 or ENDBR64 opcode, depending on the target.
Oren Ben Simhonfdd72fd2018-03-17 13:29:46 +000059 /// \returns true if the ENDBR was added and false otherwise.
60 bool addENDBR(MachineBasicBlock &MBB) const;
Oren Ben Simhon1c6308e2018-01-09 08:51:18 +000061};
62
63} // end anonymous namespace
64
65char X86IndirectBranchTrackingPass::ID = 0;
66
67FunctionPass *llvm::createX86IndirectBranchTrackingPass() {
68 return new X86IndirectBranchTrackingPass();
69}
70
Oren Ben Simhonfdd72fd2018-03-17 13:29:46 +000071bool X86IndirectBranchTrackingPass::addENDBR(MachineBasicBlock &MBB) const {
Oren Ben Simhon1c6308e2018-01-09 08:51:18 +000072 assert(TII && "Target instruction info was not initialized");
73 assert((X86::ENDBR64 == EndbrOpcode || X86::ENDBR32 == EndbrOpcode) &&
74 "Unexpected Endbr opcode");
75
76 auto MI = MBB.begin();
77 // If the MBB is empty or the first instruction is not ENDBR,
78 // add the ENDBR instruction to the beginning of the MBB.
79 if (MI == MBB.end() || EndbrOpcode != MI->getOpcode()) {
80 BuildMI(MBB, MI, MBB.findDebugLoc(MI), TII->get(EndbrOpcode));
81 NumEndBranchAdded++;
Oren Ben Simhonfdd72fd2018-03-17 13:29:46 +000082 return true;
Oren Ben Simhon1c6308e2018-01-09 08:51:18 +000083 }
Oren Ben Simhonfdd72fd2018-03-17 13:29:46 +000084
85 return false;
Oren Ben Simhon1c6308e2018-01-09 08:51:18 +000086}
87
88bool X86IndirectBranchTrackingPass::runOnMachineFunction(MachineFunction &MF) {
89 const X86Subtarget &SubTarget = MF.getSubtarget<X86Subtarget>();
90
Oren Ben Simhon1c6308e2018-01-09 08:51:18 +000091 // Check that the cf-protection-branch is enabled.
92 Metadata *isCFProtectionSupported =
93 MF.getMMI().getModule()->getModuleFlag("cf-protection-branch");
94 if (!isCFProtectionSupported && !IndirectBranchTracking)
95 return false;
96
97 // True if the current MF was changed and false otherwise.
98 bool Changed = false;
99
100 TII = SubTarget.getInstrInfo();
101 EndbrOpcode = SubTarget.is64Bit() ? X86::ENDBR64 : X86::ENDBR32;
102
103 // Non-internal function or function whose address was taken, can be
Oren Ben Simhonfdd72fd2018-03-17 13:29:46 +0000104 // accessed through indirect calls. Mark the first BB with ENDBR instruction
105 // unless nocf_check attribute is used.
106 if ((MF.getFunction().hasAddressTaken() ||
107 !MF.getFunction().hasLocalLinkage()) &&
108 !MF.getFunction().doesNoCfCheck()) {
Oren Ben Simhon1c6308e2018-01-09 08:51:18 +0000109 auto MBB = MF.begin();
Oren Ben Simhonfdd72fd2018-03-17 13:29:46 +0000110 Changed |= addENDBR(*MBB);
Oren Ben Simhon1c6308e2018-01-09 08:51:18 +0000111 }
112
Oren Ben Simhonfdd72fd2018-03-17 13:29:46 +0000113 for (auto &MBB : MF)
114 // Find all basic blocks that their address was taken (for example
Oren Ben Simhon1c6308e2018-01-09 08:51:18 +0000115 // in the case of indirect jump) and add ENDBR instruction.
Oren Ben Simhonfdd72fd2018-03-17 13:29:46 +0000116 if (MBB.hasAddressTaken())
117 Changed |= addENDBR(MBB);
Oren Ben Simhon1c6308e2018-01-09 08:51:18 +0000118
119 return Changed;
120}