1*0b57cec5SDimitry Andric //===---- X86IndirectBranchTracking.cpp - Enables CET IBT mechanism -------===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric // 9*0b57cec5SDimitry Andric // This file defines a pass that enables Indirect Branch Tracking (IBT) as part 10*0b57cec5SDimitry Andric // of Control-Flow Enforcement Technology (CET). 11*0b57cec5SDimitry Andric // The pass adds ENDBR (End Branch) machine instructions at the beginning of 12*0b57cec5SDimitry Andric // each basic block or function that is referenced by an indrect jump/call 13*0b57cec5SDimitry Andric // instruction. 14*0b57cec5SDimitry Andric // The ENDBR instructions have a NOP encoding and as such are ignored in 15*0b57cec5SDimitry Andric // targets that do not support CET IBT mechanism. 16*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 17*0b57cec5SDimitry Andric 18*0b57cec5SDimitry Andric #include "X86.h" 19*0b57cec5SDimitry Andric #include "X86InstrInfo.h" 20*0b57cec5SDimitry Andric #include "X86Subtarget.h" 21*0b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h" 22*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h" 23*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h" 24*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h" 25*0b57cec5SDimitry Andric 26*0b57cec5SDimitry Andric using namespace llvm; 27*0b57cec5SDimitry Andric 28*0b57cec5SDimitry Andric #define DEBUG_TYPE "x86-indirect-branch-tracking" 29*0b57cec5SDimitry Andric 30*0b57cec5SDimitry Andric static cl::opt<bool> IndirectBranchTracking( 31*0b57cec5SDimitry Andric "x86-indirect-branch-tracking", cl::init(false), cl::Hidden, 32*0b57cec5SDimitry Andric cl::desc("Enable X86 indirect branch tracking pass.")); 33*0b57cec5SDimitry Andric 34*0b57cec5SDimitry Andric STATISTIC(NumEndBranchAdded, "Number of ENDBR instructions added"); 35*0b57cec5SDimitry Andric 36*0b57cec5SDimitry Andric namespace { 37*0b57cec5SDimitry Andric class X86IndirectBranchTrackingPass : public MachineFunctionPass { 38*0b57cec5SDimitry Andric public: 39*0b57cec5SDimitry Andric X86IndirectBranchTrackingPass() : MachineFunctionPass(ID) {} 40*0b57cec5SDimitry Andric 41*0b57cec5SDimitry Andric StringRef getPassName() const override { 42*0b57cec5SDimitry Andric return "X86 Indirect Branch Tracking"; 43*0b57cec5SDimitry Andric } 44*0b57cec5SDimitry Andric 45*0b57cec5SDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override; 46*0b57cec5SDimitry Andric 47*0b57cec5SDimitry Andric private: 48*0b57cec5SDimitry Andric static char ID; 49*0b57cec5SDimitry Andric 50*0b57cec5SDimitry Andric /// Machine instruction info used throughout the class. 51*0b57cec5SDimitry Andric const X86InstrInfo *TII; 52*0b57cec5SDimitry Andric 53*0b57cec5SDimitry Andric /// Endbr opcode for the current machine function. 54*0b57cec5SDimitry Andric unsigned int EndbrOpcode; 55*0b57cec5SDimitry Andric 56*0b57cec5SDimitry Andric /// Adds a new ENDBR instruction to the begining of the MBB. 57*0b57cec5SDimitry Andric /// The function will not add it if already exists. 58*0b57cec5SDimitry Andric /// It will add ENDBR32 or ENDBR64 opcode, depending on the target. 59*0b57cec5SDimitry Andric /// \returns true if the ENDBR was added and false otherwise. 60*0b57cec5SDimitry Andric bool addENDBR(MachineBasicBlock &MBB, MachineBasicBlock::iterator I) const; 61*0b57cec5SDimitry Andric }; 62*0b57cec5SDimitry Andric 63*0b57cec5SDimitry Andric } // end anonymous namespace 64*0b57cec5SDimitry Andric 65*0b57cec5SDimitry Andric char X86IndirectBranchTrackingPass::ID = 0; 66*0b57cec5SDimitry Andric 67*0b57cec5SDimitry Andric FunctionPass *llvm::createX86IndirectBranchTrackingPass() { 68*0b57cec5SDimitry Andric return new X86IndirectBranchTrackingPass(); 69*0b57cec5SDimitry Andric } 70*0b57cec5SDimitry Andric 71*0b57cec5SDimitry Andric bool X86IndirectBranchTrackingPass::addENDBR( 72*0b57cec5SDimitry Andric MachineBasicBlock &MBB, MachineBasicBlock::iterator I) const { 73*0b57cec5SDimitry Andric assert(TII && "Target instruction info was not initialized"); 74*0b57cec5SDimitry Andric assert((X86::ENDBR64 == EndbrOpcode || X86::ENDBR32 == EndbrOpcode) && 75*0b57cec5SDimitry Andric "Unexpected Endbr opcode"); 76*0b57cec5SDimitry Andric 77*0b57cec5SDimitry Andric // If the MBB/I is empty or the current instruction is not ENDBR, 78*0b57cec5SDimitry Andric // insert ENDBR instruction to the location of I. 79*0b57cec5SDimitry Andric if (I == MBB.end() || I->getOpcode() != EndbrOpcode) { 80*0b57cec5SDimitry Andric BuildMI(MBB, I, MBB.findDebugLoc(I), TII->get(EndbrOpcode)); 81*0b57cec5SDimitry Andric ++NumEndBranchAdded; 82*0b57cec5SDimitry Andric return true; 83*0b57cec5SDimitry Andric } 84*0b57cec5SDimitry Andric return false; 85*0b57cec5SDimitry Andric } 86*0b57cec5SDimitry Andric 87*0b57cec5SDimitry Andric bool IsCallReturnTwice(llvm::MachineOperand &MOp) { 88*0b57cec5SDimitry Andric if (!MOp.isGlobal()) 89*0b57cec5SDimitry Andric return false; 90*0b57cec5SDimitry Andric auto *CalleeFn = dyn_cast<Function>(MOp.getGlobal()); 91*0b57cec5SDimitry Andric if (!CalleeFn) 92*0b57cec5SDimitry Andric return false; 93*0b57cec5SDimitry Andric AttributeList Attrs = CalleeFn->getAttributes(); 94*0b57cec5SDimitry Andric if (Attrs.hasAttribute(AttributeList::FunctionIndex, Attribute::ReturnsTwice)) 95*0b57cec5SDimitry Andric return true; 96*0b57cec5SDimitry Andric return false; 97*0b57cec5SDimitry Andric } 98*0b57cec5SDimitry Andric 99*0b57cec5SDimitry Andric bool X86IndirectBranchTrackingPass::runOnMachineFunction(MachineFunction &MF) { 100*0b57cec5SDimitry Andric const X86Subtarget &SubTarget = MF.getSubtarget<X86Subtarget>(); 101*0b57cec5SDimitry Andric 102*0b57cec5SDimitry Andric // Check that the cf-protection-branch is enabled. 103*0b57cec5SDimitry Andric Metadata *isCFProtectionSupported = 104*0b57cec5SDimitry Andric MF.getMMI().getModule()->getModuleFlag("cf-protection-branch"); 105*0b57cec5SDimitry Andric if (!isCFProtectionSupported && !IndirectBranchTracking) 106*0b57cec5SDimitry Andric return false; 107*0b57cec5SDimitry Andric 108*0b57cec5SDimitry Andric // True if the current MF was changed and false otherwise. 109*0b57cec5SDimitry Andric bool Changed = false; 110*0b57cec5SDimitry Andric 111*0b57cec5SDimitry Andric TII = SubTarget.getInstrInfo(); 112*0b57cec5SDimitry Andric EndbrOpcode = SubTarget.is64Bit() ? X86::ENDBR64 : X86::ENDBR32; 113*0b57cec5SDimitry Andric 114*0b57cec5SDimitry Andric // Non-internal function or function whose address was taken, can be 115*0b57cec5SDimitry Andric // accessed through indirect calls. Mark the first BB with ENDBR instruction 116*0b57cec5SDimitry Andric // unless nocf_check attribute is used. 117*0b57cec5SDimitry Andric if ((MF.getFunction().hasAddressTaken() || 118*0b57cec5SDimitry Andric !MF.getFunction().hasLocalLinkage()) && 119*0b57cec5SDimitry Andric !MF.getFunction().doesNoCfCheck()) { 120*0b57cec5SDimitry Andric auto MBB = MF.begin(); 121*0b57cec5SDimitry Andric Changed |= addENDBR(*MBB, MBB->begin()); 122*0b57cec5SDimitry Andric } 123*0b57cec5SDimitry Andric 124*0b57cec5SDimitry Andric for (auto &MBB : MF) { 125*0b57cec5SDimitry Andric // Find all basic blocks that their address was taken (for example 126*0b57cec5SDimitry Andric // in the case of indirect jump) and add ENDBR instruction. 127*0b57cec5SDimitry Andric if (MBB.hasAddressTaken()) 128*0b57cec5SDimitry Andric Changed |= addENDBR(MBB, MBB.begin()); 129*0b57cec5SDimitry Andric 130*0b57cec5SDimitry Andric for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ++I) { 131*0b57cec5SDimitry Andric if (!I->isCall()) 132*0b57cec5SDimitry Andric continue; 133*0b57cec5SDimitry Andric if (IsCallReturnTwice(I->getOperand(0))) 134*0b57cec5SDimitry Andric Changed |= addENDBR(MBB, std::next(I)); 135*0b57cec5SDimitry Andric } 136*0b57cec5SDimitry Andric } 137*0b57cec5SDimitry Andric return Changed; 138*0b57cec5SDimitry Andric } 139