AArch64BranchTargets.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. bool NeedsWinCFI);
  39. };
  40. } // end anonymous namespace
  41. char AArch64BranchTargets::ID = 0;
  42. INITIALIZE_PASS(AArch64BranchTargets, "aarch64-branch-targets",
  43. AARCH64_BRANCH_TARGETS_NAME, false, false)
  44. void AArch64BranchTargets::getAnalysisUsage(AnalysisUsage &AU) const {
  45. AU.setPreservesCFG();
  46. MachineFunctionPass::getAnalysisUsage(AU);
  47. }
  48. FunctionPass *llvm::createAArch64BranchTargetsPass() {
  49. return new AArch64BranchTargets();
  50. }
  51. bool AArch64BranchTargets::runOnMachineFunction(MachineFunction &MF) {
  52. if (!MF.getInfo<AArch64FunctionInfo>()->branchTargetEnforcement())
  53. return false;
  54. LLVM_DEBUG(
  55. dbgs() << "********** AArch64 Branch Targets **********\n"
  56. << "********** Function: " << MF.getName() << '\n');
  57. // LLVM does not consider basic blocks which are the targets of jump tables
  58. // to be address-taken (the address can't escape anywhere else), but they are
  59. // used for indirect branches, so need BTI instructions.
  60. SmallPtrSet<MachineBasicBlock *, 8> JumpTableTargets;
  61. if (auto *JTI = MF.getJumpTableInfo())
  62. for (auto &JTE : JTI->getJumpTables())
  63. for (auto *MBB : JTE.MBBs)
  64. JumpTableTargets.insert(MBB);
  65. bool MadeChange = false;
  66. bool HasWinCFI = MF.hasWinCFI();
  67. for (MachineBasicBlock &MBB : MF) {
  68. bool CouldCall = false, CouldJump = false;
  69. // Even in cases where a function has internal linkage and is only called
  70. // directly in its translation unit, it can still be called indirectly if
  71. // the linker decides to add a thunk to it for whatever reason (say, for
  72. // example, if it is finally placed far from its call site and a BL is not
  73. // long-range enough). PLT entries and tail-calls use BR, but when they are
  74. // are in guarded pages should all use x16 or x17 to hold the called
  75. // address, so we don't need to set CouldJump here. BR instructions in
  76. // non-guarded pages (which might be non-BTI-aware code) are allowed to
  77. // branch to a "BTI c" using any register.
  78. if (&MBB == &*MF.begin())
  79. CouldCall = true;
  80. // If the block itself is address-taken, it could be indirectly branched
  81. // to, but not called.
  82. if (MBB.hasAddressTaken() || JumpTableTargets.count(&MBB))
  83. CouldJump = true;
  84. if (CouldCall || CouldJump) {
  85. addBTI(MBB, CouldCall, CouldJump, HasWinCFI);
  86. MadeChange = true;
  87. }
  88. }
  89. return MadeChange;
  90. }
  91. void AArch64BranchTargets::addBTI(MachineBasicBlock &MBB, bool CouldCall,
  92. bool CouldJump, bool HasWinCFI) {
  93. LLVM_DEBUG(dbgs() << "Adding BTI " << (CouldJump ? "j" : "")
  94. << (CouldCall ? "c" : "") << " to " << MBB.getName()
  95. << "\n");
  96. const AArch64InstrInfo *TII = static_cast<const AArch64InstrInfo *>(
  97. MBB.getParent()->getSubtarget().getInstrInfo());
  98. unsigned HintNum = 32;
  99. if (CouldCall)
  100. HintNum |= 2;
  101. if (CouldJump)
  102. HintNum |= 4;
  103. assert(HintNum != 32 && "No target kinds!");
  104. auto MBBI = MBB.begin();
  105. // Skip the meta instructions, those will be removed anyway.
  106. for (; MBBI != MBB.end() &&
  107. (MBBI->isMetaInstruction() || MBBI->getOpcode() == AArch64::EMITBKEY);
  108. ++MBBI)
  109. ;
  110. // SCTLR_EL1.BT[01] is set to 0 by default which means
  111. // PACI[AB]SP are implicitly BTI C so no BTI C instruction is needed there.
  112. if (MBBI != MBB.end() && HintNum == 34 &&
  113. (MBBI->getOpcode() == AArch64::PACIASP ||
  114. MBBI->getOpcode() == AArch64::PACIBSP))
  115. return;
  116. if (HasWinCFI && MBBI->getFlag(MachineInstr::FrameSetup)) {
  117. BuildMI(MBB, MBB.begin(), MBB.findDebugLoc(MBB.begin()),
  118. TII->get(AArch64::SEH_Nop));
  119. }
  120. BuildMI(MBB, MBB.begin(), MBB.findDebugLoc(MBB.begin()),
  121. TII->get(AArch64::HINT))
  122. .addImm(HintNum);
  123. }