diff options
| author | Oren Ben Simhon <oren.ben.simhon@intel.com> | 2018-01-09 08:51:18 +0000 |
|---|---|---|
| committer | Oren Ben Simhon <oren.ben.simhon@intel.com> | 2018-01-09 08:51:18 +0000 |
| commit | 1c6308ecd5e203650bf2d852361cdb1bd5e0e537 (patch) | |
| tree | 0fe1ae982f94894e2e04b97570e0a9b9d2a07d7b /llvm/lib | |
| parent | 1295088fba658f46b187ba613327d40210013d14 (diff) | |
| download | bcm5719-llvm-1c6308ecd5e203650bf2d852361cdb1bd5e0e537.tar.gz bcm5719-llvm-1c6308ecd5e203650bf2d852361cdb1bd5e0e537.zip | |
Instrument Control Flow For Indirect Branch Tracking
CET (Control-Flow Enforcement Technology) introduces a new mechanism called IBT (Indirect Branch Tracking).
According to IBT, each Indirect branch should land on dedicated ENDBR instruction (End Branch).
The new pass adds ENDBR instructions for every indirect jmp/call (including jumps using jump tables / switches).
For more information, please see the following:
https://software.intel.com/sites/default/files/managed/4d/2a/control-flow-enforcement-technology-preview.pdf
Differential Revision: https://reviews.llvm.org/D40482
Change-Id: Icb754489faf483a95248f96982a4e8b1009eb709
llvm-svn: 322062
Diffstat (limited to 'llvm/lib')
| -rw-r--r-- | llvm/lib/Target/X86/CMakeLists.txt | 3 | ||||
| -rw-r--r-- | llvm/lib/Target/X86/X86.h | 4 | ||||
| -rw-r--r-- | llvm/lib/Target/X86/X86IndirectBranchTracking.cpp | 163 | ||||
| -rw-r--r-- | llvm/lib/Target/X86/X86InstrSystem.td | 5 | ||||
| -rw-r--r-- | llvm/lib/Target/X86/X86TargetMachine.cpp | 2 |
5 files changed, 176 insertions, 1 deletions
diff --git a/llvm/lib/Target/X86/CMakeLists.txt b/llvm/lib/Target/X86/CMakeLists.txt index 7e0df294146..a36a941e37f 100644 --- a/llvm/lib/Target/X86/CMakeLists.txt +++ b/llvm/lib/Target/X86/CMakeLists.txt @@ -23,6 +23,7 @@ add_public_tablegen_target(X86CommonTableGen) set(sources X86AsmPrinter.cpp X86CallFrameOptimization.cpp + X86CallingConv.cpp X86CallLowering.cpp X86CmovConversion.cpp X86DomainReassignment.cpp @@ -36,6 +37,7 @@ set(sources X86InstructionSelector.cpp X86ISelDAGToDAG.cpp X86ISelLowering.cpp + X86IndirectBranchTracking.cpp X86InterleavedAccess.cpp X86InstrFMA3Info.cpp X86InstrInfo.cpp @@ -57,7 +59,6 @@ set(sources X86VZeroUpper.cpp X86WinAllocaExpander.cpp X86WinEHState.cpp - X86CallingConv.cpp ) add_llvm_target(X86CodeGen ${sources}) diff --git a/llvm/lib/Target/X86/X86.h b/llvm/lib/Target/X86/X86.h index 5631648d2dc..b8c97e3c301 100644 --- a/llvm/lib/Target/X86/X86.h +++ b/llvm/lib/Target/X86/X86.h @@ -49,6 +49,10 @@ FunctionPass *createX86FloatingPointStackifierPass(); /// transition penalty between functions encoded with AVX and SSE. FunctionPass *createX86IssueVZeroUpperPass(); +/// This pass inserts ENDBR instructions before indirect jump/call +/// destinations as part of CET IBT mechanism. +FunctionPass *createX86IndirectBranchTrackingPass(); + /// Return a pass that pads short functions with NOOPs. /// This will prevent a stall when returning on the Atom. FunctionPass *createX86PadShortFunctions(); diff --git a/llvm/lib/Target/X86/X86IndirectBranchTracking.cpp b/llvm/lib/Target/X86/X86IndirectBranchTracking.cpp new file mode 100644 index 00000000000..1570e7a0b2d --- /dev/null +++ b/llvm/lib/Target/X86/X86IndirectBranchTracking.cpp @@ -0,0 +1,163 @@ +//===---- X86IndirectBranchTracking.cpp - Enables CET IBT mechanism -------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file defines a pass that enables Indirect Branch Tracking (IBT) as part +// of Control-Flow Enforcement Technology (CET). +// The pass adds ENDBR (End Branch) machine instructions at the beginning of +// each basic block or function that is referenced by an indrect jump/call +// instruction. +// The ENDBR instructions have a NOP encoding and as such are ignored in +// targets that do not support CET IBT mechanism. +//===----------------------------------------------------------------------===// + +#include "X86.h" +#include "X86InstrInfo.h" +#include "X86Subtarget.h" +#include "llvm/ADT/Statistic.h" +#include "llvm/CodeGen/MachineFunctionPass.h" +#include "llvm/CodeGen/MachineInstrBuilder.h" +#include "llvm/CodeGen/MachineJumpTableInfo.h" +#include "llvm/CodeGen/MachineModuleInfo.h" + +using namespace llvm; + +#define DEBUG_TYPE "x86-indirect-branch-tracking" + +static cl::opt<bool> IndirectBranchTracking( + "x86-indirect-branch-tracking", cl::init(false), cl::Hidden, + cl::desc("Enable X86 indirect branch tracking pass.")); + +STATISTIC(NumEndBranchAdded, "Number of ENDBR instructions added"); + +namespace { +class X86IndirectBranchTrackingPass : public MachineFunctionPass { +public: + X86IndirectBranchTrackingPass() : MachineFunctionPass(ID) {} + + StringRef getPassName() const override { + return "X86 Indirect Branch Tracking"; + } + + bool runOnMachineFunction(MachineFunction &MF) override; + +private: + static char ID; + + /// Machine instruction info used throughout the class. + const X86InstrInfo *TII; + + /// Endbr opcode for the current machine function. + unsigned int EndbrOpcode; + + /// The function looks for an indirect jump terminator in MBB predecessors. + /// + /// Jump tables are generated when lowering switch-case statements or + /// setjmp/longjump functions. + /// As a result only indirect jumps use jump tables. + /// The function verifies this assumption. + /// + /// \return true if the input \p MBB has a predecessor MBB with indirect + /// branch terminator or false otherwise. + bool verifyIndirectJump(const MachineBasicBlock *MBB) const; + + /// Adds a new ENDBR instruction to the begining of the MBB. + /// The function will not add it if already exists. + /// It will add ENDBR32 or ENDBR64 opcode, depending on the target. + void addENDBR(MachineBasicBlock &MBB) const; +}; + +} // end anonymous namespace + +char X86IndirectBranchTrackingPass::ID = 0; + +FunctionPass *llvm::createX86IndirectBranchTrackingPass() { + return new X86IndirectBranchTrackingPass(); +} + +bool X86IndirectBranchTrackingPass::verifyIndirectJump( + const MachineBasicBlock *MBB) const { + for (auto &PredMBB : MBB->predecessors()) + for (auto &TermI : PredMBB->terminators()) + if (TermI.isIndirectBranch()) + return true; + + return false; +} + +void X86IndirectBranchTrackingPass::addENDBR(MachineBasicBlock &MBB) const { + assert(TII && "Target instruction info was not initialized"); + assert((X86::ENDBR64 == EndbrOpcode || X86::ENDBR32 == EndbrOpcode) && + "Unexpected Endbr opcode"); + + auto MI = MBB.begin(); + // If the MBB is empty or the first instruction is not ENDBR, + // add the ENDBR instruction to the beginning of the MBB. + if (MI == MBB.end() || EndbrOpcode != MI->getOpcode()) { + BuildMI(MBB, MI, MBB.findDebugLoc(MI), TII->get(EndbrOpcode)); + NumEndBranchAdded++; + } +} + +bool X86IndirectBranchTrackingPass::runOnMachineFunction(MachineFunction &MF) { + const X86Subtarget &SubTarget = MF.getSubtarget<X86Subtarget>(); + + // Make sure that the target supports ENDBR instruction. + if (!SubTarget.hasIBT()) + return false; + + // Check that the cf-protection-branch is enabled. + Metadata *isCFProtectionSupported = + MF.getMMI().getModule()->getModuleFlag("cf-protection-branch"); + if (!isCFProtectionSupported && !IndirectBranchTracking) + return false; + + // True if the current MF was changed and false otherwise. + bool Changed = false; + + TII = SubTarget.getInstrInfo(); + EndbrOpcode = SubTarget.is64Bit() ? X86::ENDBR64 : X86::ENDBR32; + + // Non-internal function or function whose address was taken, can be + // invoked through indirect calls. Mark the first BB with ENDBR instruction. + // TODO: Do not add ENDBR instruction in case notrack attribute is used. + if (MF.getFunction().hasAddressTaken() || + !MF.getFunction().hasLocalLinkage()) { + auto MBB = MF.begin(); + addENDBR(*MBB); + Changed = true; + } + + for (auto &MBB : MF) { + // Find all basic blocks that thier address was taken (for example + // in the case of indirect jump) and add ENDBR instruction. + if (MBB.hasAddressTaken()) { + addENDBR(MBB); + Changed = true; + } + } + + // Adds ENDBR instructions to MBB destinations of the jump table. + // TODO: In case of more than 50 destinations, do not add ENDBR and + // instead add DS_PREFIX. + if (MachineJumpTableInfo *JTI = MF.getJumpTableInfo()) { + for (const auto &JT : JTI->getJumpTables()) { + for (auto *MBB : JT.MBBs) { + // This assert verifies the assumption that this MBB has an indirect + // jump terminator in one of its predecessor. + assert(verifyIndirectJump(MBB) && + "The MBB is not the destination of an indirect jump"); + + addENDBR(*MBB); + Changed = true; + } + } + } + + return Changed; +} diff --git a/llvm/lib/Target/X86/X86InstrSystem.td b/llvm/lib/Target/X86/X86InstrSystem.td index 40d2dca4f9e..dfd94ab6ea5 100644 --- a/llvm/lib/Target/X86/X86InstrSystem.td +++ b/llvm/lib/Target/X86/X86InstrSystem.td @@ -536,6 +536,11 @@ let SchedRW = [WriteSystem], Predicates = [HasSHSTK] in{ } // Defs SSP } // SchedRW && HasSHSTK +let Predicates = [HasIBT] in { + def ENDBR64 : I<0x1E, MRM_FA, (outs), (ins), "endbr64", []>, XS; + def ENDBR32 : I<0x1E, MRM_FB, (outs), (ins), "endbr32", []>, XS; +} // HasIBT + //===----------------------------------------------------------------------===// // XSAVE instructions let SchedRW = [WriteSystem] in { diff --git a/llvm/lib/Target/X86/X86TargetMachine.cpp b/llvm/lib/Target/X86/X86TargetMachine.cpp index e95e6ecae09..48e2073c41c 100644 --- a/llvm/lib/Target/X86/X86TargetMachine.cpp +++ b/llvm/lib/Target/X86/X86TargetMachine.cpp @@ -426,6 +426,8 @@ void X86PassConfig::addPreEmitPass() { if (getOptLevel() != CodeGenOpt::None) addPass(new X86ExecutionDepsFix()); + addPass(createX86IndirectBranchTrackingPass()); + if (UseVZeroUpper) addPass(createX86IssueVZeroUpperPass()); |

