ARMBranchTargets.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //===-- ARMBranchTargets.cpp -- Harden code using v8.1-M 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.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #include "ARM.h"
  17. #include "ARMInstrInfo.h"
  18. #include "ARMMachineFunctionInfo.h"
  19. #include "llvm/CodeGen/MachineFunctionPass.h"
  20. #include "llvm/CodeGen/MachineInstrBuilder.h"
  21. #include "llvm/CodeGen/MachineJumpTableInfo.h"
  22. #include "llvm/CodeGen/MachineModuleInfo.h"
  23. #include "llvm/Support/Debug.h"
  24. using namespace llvm;
  25. #define DEBUG_TYPE "arm-branch-targets"
  26. #define ARM_BRANCH_TARGETS_NAME "ARM Branch Targets"
  27. namespace {
  28. class ARMBranchTargets : public MachineFunctionPass {
  29. public:
  30. static char ID;
  31. ARMBranchTargets() : MachineFunctionPass(ID) {}
  32. void getAnalysisUsage(AnalysisUsage &AU) const override;
  33. bool runOnMachineFunction(MachineFunction &MF) override;
  34. StringRef getPassName() const override { return ARM_BRANCH_TARGETS_NAME; }
  35. private:
  36. void addBTI(const ARMInstrInfo &TII, MachineBasicBlock &MBB, bool IsFirstBB);
  37. };
  38. } // end anonymous namespace
  39. char ARMBranchTargets::ID = 0;
  40. INITIALIZE_PASS(ARMBranchTargets, "arm-branch-targets", ARM_BRANCH_TARGETS_NAME,
  41. false, false)
  42. void ARMBranchTargets::getAnalysisUsage(AnalysisUsage &AU) const {
  43. AU.setPreservesCFG();
  44. MachineFunctionPass::getAnalysisUsage(AU);
  45. }
  46. FunctionPass *llvm::createARMBranchTargetsPass() {
  47. return new ARMBranchTargets();
  48. }
  49. bool ARMBranchTargets::runOnMachineFunction(MachineFunction &MF) {
  50. if (!MF.getInfo<ARMFunctionInfo>()->branchTargetEnforcement())
  51. return false;
  52. LLVM_DEBUG(dbgs() << "********** ARM Branch Targets **********\n"
  53. << "********** Function: " << MF.getName() << '\n');
  54. const ARMInstrInfo &TII =
  55. *static_cast<const ARMInstrInfo *>(MF.getSubtarget().getInstrInfo());
  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<const MachineBasicBlock *, 8> JumpTableTargets;
  60. if (const MachineJumpTableInfo *JTI = MF.getJumpTableInfo())
  61. for (const MachineJumpTableEntry &JTE : JTI->getJumpTables())
  62. for (const MachineBasicBlock *MBB : JTE.MBBs)
  63. JumpTableTargets.insert(MBB);
  64. bool MadeChange = false;
  65. for (MachineBasicBlock &MBB : MF) {
  66. bool NeedBTI = false;
  67. bool IsFirstBB = &MBB == &MF.front();
  68. // Every function can potentially be called indirectly (even if it has
  69. // static linkage, due to linker-generated veneers).
  70. if (IsFirstBB)
  71. NeedBTI = true;
  72. // If the block itself is address-taken, or is an exception landing pad, it
  73. // could be indirectly branched to.
  74. if (MBB.hasAddressTaken() || MBB.isEHPad() || JumpTableTargets.count(&MBB))
  75. NeedBTI = true;
  76. if (NeedBTI) {
  77. addBTI(TII, MBB, IsFirstBB);
  78. MadeChange = true;
  79. }
  80. }
  81. return MadeChange;
  82. }
  83. /// Insert a BTI/PACBTI instruction into a given basic block \c MBB. If
  84. /// \c IsFirstBB is true (meaning that this is the first BB in a function) try
  85. /// to find a PAC instruction and replace it with PACBTI. Otherwise just insert
  86. /// a BTI instruction.
  87. /// The point of insertion is in the beginning of the BB, immediately after meta
  88. /// instructions (such labels in exception handling landing pads).
  89. void ARMBranchTargets::addBTI(const ARMInstrInfo &TII, MachineBasicBlock &MBB,
  90. bool IsFirstBB) {
  91. // Which instruction to insert: BTI or PACBTI
  92. unsigned OpCode = ARM::t2BTI;
  93. unsigned MIFlags = 0;
  94. // Skip meta instructions, including EH labels
  95. auto MBBI = llvm::find_if_not(MBB.instrs(), [](const MachineInstr &MI) {
  96. return MI.isMetaInstruction();
  97. });
  98. // If this is the first BB in a function, check if it starts with a PAC
  99. // instruction and in that case remove the PAC instruction.
  100. if (IsFirstBB) {
  101. if (MBBI != MBB.instr_end() && MBBI->getOpcode() == ARM::t2PAC) {
  102. LLVM_DEBUG(dbgs() << "Removing a 'PAC' instr from BB '" << MBB.getName()
  103. << "' to replace with PACBTI\n");
  104. OpCode = ARM::t2PACBTI;
  105. MIFlags = MachineInstr::FrameSetup;
  106. auto NextMBBI = std::next(MBBI);
  107. MBBI->eraseFromParent();
  108. MBBI = NextMBBI;
  109. }
  110. }
  111. LLVM_DEBUG(dbgs() << "Inserting a '"
  112. << (OpCode == ARM::t2BTI ? "BTI" : "PACBTI")
  113. << "' instr into BB '" << MBB.getName() << "'\n");
  114. // Finally, insert a new instruction (either PAC or PACBTI)
  115. BuildMI(MBB, MBBI, MBB.findDebugLoc(MBBI), TII.get(OpCode))
  116. .setMIFlags(MIFlags);
  117. }