AArch64BranchTargets.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //===-- AArch64BranchTargets.cpp -- Harden code using v8.5-A BTI extension -==//
  2. //
  3. // 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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This pass inserts BTI instructions at the start of every function and basic
  10. // block which could be indirectly called. The hardware will (when enabled)
  11. // trap when an indirect branch or call instruction targets an instruction
  12. // which is not a valid BTI instruction. This is intended to guard against
  13. // control-flow hijacking attacks. Note that this does not do anything for RET
  14. // instructions, as they can be more precisely protected by return address
  15. // signing.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #include "AArch64MachineFunctionInfo.h"
  19. #include "AArch64Subtarget.h"
  20. #include "llvm/CodeGen/MachineFunctionPass.h"
  21. #include "llvm/CodeGen/MachineInstrBuilder.h"
  22. #include "llvm/CodeGen/MachineJumpTableInfo.h"
  23. #include "llvm/CodeGen/MachineModuleInfo.h"
  24. #include "llvm/Support/Debug.h"
  25. using namespace llvm;
  26. #define DEBUG_TYPE "aarch64-branch-targets"
  27. #define AARCH64_BRANCH_TARGETS_NAME "AArch64 Branch Targets"
  28. namespace {
  29. class AArch64BranchTargets : public MachineFunctionPass {
  30. public:
  31. static char ID;
  32. AArch64BranchTargets() : MachineFunctionPass(ID) {}
  33. void getAnalysisUsage(AnalysisUsage &AU) const override;
  34. bool runOnMachineFunction(MachineFunction &MF) override;
  35. StringRef getPassName() const override { return AARCH64_BRANCH_TARGETS_NAME; }
  36. private:
  37. void addBTI(MachineBasicBlock &MBB, bool CouldCall, bool CouldJump);
  38. };
  39. } // end anonymous namespace
  40. char AArch64BranchTargets::ID = 0;
  41. INITIALIZE_PASS(AArch64BranchTargets, "aarch64-branch-targets",
  42. AARCH64_BRANCH_TARGETS_NAME, false, false)
  43. void AArch64BranchTargets::getAnalysisUsage(AnalysisUsage &AU) const {
  44. AU.setPreservesCFG();
  45. MachineFunctionPass::getAnalysisUsage(AU);
  46. }
  47. FunctionPass *llvm::createAArch64BranchTargetsPass() {
  48. return new AArch64BranchTargets();
  49. }
  50. bool AArch64BranchTargets::runOnMachineFunction(MachineFunction &MF) {
  51. if (!MF.getInfo<AArch64FunctionInfo>()->branchTargetEnforcement())
  52. return false;
  53. LLVM_DEBUG(
  54. dbgs() << "********** AArch64 Branch Targets **********\n"
  55. << "********** Function: " << MF.getName() << '\n');
  56. // LLVM does not consider basic blocks which are the targets of jump tables
  57. // to be address-taken (the address can't escape anywhere else), but they are
  58. // used for indirect branches, so need BTI instructions.
  59. SmallPtrSet<MachineBasicBlock *, 8> JumpTableTargets;
  60. if (auto *JTI = MF.getJumpTableInfo())
  61. for (auto &JTE : JTI->getJumpTables())
  62. for (auto *MBB : JTE.MBBs)
  63. JumpTableTargets.insert(MBB);
  64. bool MadeChange = false;
  65. for (MachineBasicBlock &MBB : MF) {
  66. bool CouldCall = false, CouldJump = false;
  67. // Even in cases where a function has internal linkage and is only called
  68. // directly in its translation unit, it can still be called indirectly if
  69. // the linker decides to add a thunk to it for whatever reason (say, for
  70. // example, if it is finally placed far from its call site and a BL is not
  71. // long-range enough). PLT entries and tail-calls use BR, but when they are
  72. // are in guarded pages should all use x16 or x17 to hold the called
  73. // address, so we don't need to set CouldJump here. BR instructions in
  74. // non-guarded pages (which might be non-BTI-aware code) are allowed to
  75. // branch to a "BTI c" using any register.
  76. if (&MBB == &*MF.begin())
  77. CouldCall = true;
  78. // If the block itself is address-taken, it could be indirectly branched
  79. // to, but not called.
  80. if (MBB.hasAddressTaken() || JumpTableTargets.count(&MBB))
  81. CouldJump = true;
  82. if (CouldCall || CouldJump) {
  83. addBTI(MBB, CouldCall, CouldJump);
  84. MadeChange = true;
  85. }
  86. }
  87. return MadeChange;
  88. }
  89. void AArch64BranchTargets::addBTI(MachineBasicBlock &MBB, bool CouldCall,
  90. bool CouldJump) {
  91. LLVM_DEBUG(dbgs() << "Adding BTI " << (CouldJump ? "j" : "")
  92. << (CouldCall ? "c" : "") << " to " << MBB.getName()
  93. << "\n");
  94. const AArch64InstrInfo *TII = static_cast<const AArch64InstrInfo *>(
  95. MBB.getParent()->getSubtarget().getInstrInfo());
  96. unsigned HintNum = 32;
  97. if (CouldCall)
  98. HintNum |= 2;
  99. if (CouldJump)
  100. HintNum |= 4;
  101. assert(HintNum != 32 && "No target kinds!");
  102. auto MBBI = MBB.begin();
  103. // Skip the meta instructions, those will be removed anyway.
  104. for (; MBBI != MBB.end() &&
  105. (MBBI->isMetaInstruction() || MBBI->getOpcode() == AArch64::EMITBKEY);
  106. ++MBBI)
  107. ;
  108. // SCTLR_EL1.BT[01] is set to 0 by default which means
  109. // PACI[AB]SP are implicitly BTI C so no BTI C instruction is needed there.
  110. if (MBBI != MBB.end() && HintNum == 34 &&
  111. (MBBI->getOpcode() == AArch64::PACIASP ||
  112. MBBI->getOpcode() == AArch64::PACIBSP))
  113. return;
  114. BuildMI(MBB, MBB.begin(), MBB.findDebugLoc(MBB.begin()),
  115. TII->get(AArch64::HINT))
  116. .addImm(HintNum);
  117. }