LLVM  8.0.1
AArch64BranchTargets.cpp
Go to the documentation of this file.
1 //===-- AArch64BranchTargets.cpp -- Harden code using v8.5-A BTI extension -==//
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 pass inserts BTI instructions at the start of every function and basic
11 // block which could be indirectly called. The hardware will (when enabled)
12 // trap when an indirect branch or call instruction targets an instruction
13 // which is not a valid BTI instruction. This is intended to guard against
14 // control-flow hijacking attacks. Note that this does not do anything for RET
15 // instructions, as they can be more precisely protected by return address
16 // signing.
17 //
18 //===----------------------------------------------------------------------===//
19 
20 #include "AArch64Subtarget.h"
25 #include "llvm/Support/Debug.h"
26 
27 using namespace llvm;
28 
29 #define DEBUG_TYPE "aarch64-branch-targets"
30 #define AARCH64_BRANCH_TARGETS_NAME "AArch64 Branch Targets"
31 
32 namespace {
33 class AArch64BranchTargets : public MachineFunctionPass {
34 public:
35  static char ID;
36  AArch64BranchTargets() : MachineFunctionPass(ID) {}
37  void getAnalysisUsage(AnalysisUsage &AU) const override;
38  bool runOnMachineFunction(MachineFunction &MF) override;
39  StringRef getPassName() const override { return AARCH64_BRANCH_TARGETS_NAME; }
40 
41 private:
42  void addBTI(MachineBasicBlock &MBB, bool CouldCall, bool CouldJump);
43 };
44 } // end anonymous namespace
45 
47 
48 INITIALIZE_PASS(AArch64BranchTargets, "aarch64-branch-targets",
49  AARCH64_BRANCH_TARGETS_NAME, false, false)
50 
51 void AArch64BranchTargets::getAnalysisUsage(AnalysisUsage &AU) const {
52  AU.setPreservesCFG();
54 }
55 
57  return new AArch64BranchTargets();
58 }
59 
60 bool AArch64BranchTargets::runOnMachineFunction(MachineFunction &MF) {
61  const Function &F = MF.getFunction();
62  if (!F.hasFnAttribute("branch-target-enforcement"))
63  return false;
64 
65  LLVM_DEBUG(
66  dbgs() << "********** AArch64 Branch Targets **********\n"
67  << "********** Function: " << MF.getName() << '\n');
68 
69  // LLVM does not consider basic blocks which are the targets of jump tables
70  // to be address-taken (the address can't escape anywhere else), but they are
71  // used for indirect branches, so need BTI instructions.
72  SmallPtrSet<MachineBasicBlock *, 8> JumpTableTargets;
73  if (auto *JTI = MF.getJumpTableInfo())
74  for (auto &JTE : JTI->getJumpTables())
75  for (auto *MBB : JTE.MBBs)
76  JumpTableTargets.insert(MBB);
77 
78  bool MadeChange = false;
79  for (MachineBasicBlock &MBB : MF) {
80  bool CouldCall = false, CouldJump = false;
81  // If the function is address-taken or externally-visible, it could be
82  // indirectly called. PLT entries and tail-calls use BR, but when they are
83  // are in guarded pages should all use x16 or x17 to hold the called
84  // address, so we don't need to set CouldJump here. BR instructions in
85  // non-guarded pages (which might be non-BTI-aware code) are allowed to
86  // branch to a "BTI c" using any register.
87  if (&MBB == &*MF.begin() && (F.hasAddressTaken() || !F.hasLocalLinkage()))
88  CouldCall = true;
89 
90  // If the block itself is address-taken, it could be indirectly branched
91  // to, but not called.
92  if (MBB.hasAddressTaken() || JumpTableTargets.count(&MBB))
93  CouldJump = true;
94 
95  if (CouldCall || CouldJump) {
96  addBTI(MBB, CouldCall, CouldJump);
97  MadeChange = true;
98  }
99  }
100 
101  return MadeChange;
102 }
103 
104 void AArch64BranchTargets::addBTI(MachineBasicBlock &MBB, bool CouldCall,
105  bool CouldJump) {
106  LLVM_DEBUG(dbgs() << "Adding BTI " << (CouldJump ? "j" : "")
107  << (CouldCall ? "c" : "") << " to " << MBB.getName()
108  << "\n");
109 
110  const AArch64InstrInfo *TII = static_cast<const AArch64InstrInfo *>(
111  MBB.getParent()->getSubtarget().getInstrInfo());
112 
113  unsigned HintNum = 32;
114  if (CouldCall)
115  HintNum |= 2;
116  if (CouldJump)
117  HintNum |= 4;
118  assert(HintNum != 32 && "No target kinds!");
119 
120  auto MBBI = MBB.begin();
121 
122  // PACI[AB]SP are implicitly BTI JC, so no BTI instruction needed there.
123  if (MBBI != MBB.end() && (MBBI->getOpcode() == AArch64::PACIASP ||
124  MBBI->getOpcode() == AArch64::PACIBSP))
125  return;
126 
127  BuildMI(MBB, MBB.begin(), MBB.findDebugLoc(MBB.begin()),
128  TII->get(AArch64::HINT))
129  .addImm(HintNum);
130 }
bool hasLocalLinkage() const
Definition: GlobalValue.h:436
This class represents lattice values for constants.
Definition: AllocatorList.h:24
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.h:321
F(f)
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
virtual const TargetInstrInfo * getInstrInfo() const
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata *> MDs)
Definition: Metadata.h:1166
StringRef getName() const
Return the name of the corresponding LLVM basic block, or an empty string.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
DebugLoc findDebugLoc(instr_iterator MBBI)
Find the next valid DebugLoc starting at MBBI, skipping any DBG_VALUE and DBG_LABEL instructions...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:371
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:285
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:418
#define AARCH64_BRANCH_TARGETS_NAME
const Function & getFunction() const
Return the LLVM function that this machine code represents.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:133
INITIALIZE_PASS(AArch64BranchTargets, "aarch64-branch-targets", AARCH64_BRANCH_TARGETS_NAME, false, false) void AArch64BranchTargets
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
FunctionPass * createAArch64BranchTargetsPass()
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())
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
#define LLVM_DEBUG(X)
Definition: Debug.h:123
const MachineJumpTableInfo * getJumpTableInfo() const
getJumpTableInfo - Return the jump table info object for the current function.