AArch64KCFI.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //===---- AArch64KCFI.cpp - Implements KCFI -------------------------------===//
  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 file implements KCFI indirect call checking.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "AArch64.h"
  13. #include "AArch64InstrInfo.h"
  14. #include "AArch64Subtarget.h"
  15. #include "AArch64TargetMachine.h"
  16. #include "llvm/ADT/Statistic.h"
  17. #include "llvm/CodeGen/MachineFunctionPass.h"
  18. #include "llvm/CodeGen/MachineInstrBuilder.h"
  19. #include "llvm/CodeGen/MachineInstrBundle.h"
  20. #include "llvm/CodeGen/MachineModuleInfo.h"
  21. using namespace llvm;
  22. #define DEBUG_TYPE "aarch64-kcfi"
  23. #define AARCH64_KCFI_PASS_NAME "Insert KCFI indirect call checks"
  24. STATISTIC(NumKCFIChecksAdded, "Number of indirect call checks added");
  25. namespace {
  26. class AArch64KCFI : public MachineFunctionPass {
  27. public:
  28. static char ID;
  29. AArch64KCFI() : MachineFunctionPass(ID) {}
  30. StringRef getPassName() const override { return AARCH64_KCFI_PASS_NAME; }
  31. bool runOnMachineFunction(MachineFunction &MF) override;
  32. private:
  33. /// Machine instruction info used throughout the class.
  34. const AArch64InstrInfo *TII = nullptr;
  35. /// Emits a KCFI check before an indirect call.
  36. /// \returns true if the check was added and false otherwise.
  37. bool emitCheck(MachineBasicBlock &MBB,
  38. MachineBasicBlock::instr_iterator I) const;
  39. };
  40. char AArch64KCFI::ID = 0;
  41. } // end anonymous namespace
  42. INITIALIZE_PASS(AArch64KCFI, DEBUG_TYPE, AARCH64_KCFI_PASS_NAME, false, false)
  43. FunctionPass *llvm::createAArch64KCFIPass() { return new AArch64KCFI(); }
  44. bool AArch64KCFI::emitCheck(MachineBasicBlock &MBB,
  45. MachineBasicBlock::instr_iterator MBBI) const {
  46. assert(TII && "Target instruction info was not initialized");
  47. // If the call instruction is bundled, we can only emit a check safely if
  48. // it's the first instruction in the bundle.
  49. if (MBBI->isBundled() && !std::prev(MBBI)->isBundle())
  50. report_fatal_error("Cannot emit a KCFI check for a bundled call");
  51. switch (MBBI->getOpcode()) {
  52. case AArch64::BLR:
  53. case AArch64::BLRNoIP:
  54. case AArch64::TCRETURNri:
  55. case AArch64::TCRETURNriBTI:
  56. break;
  57. default:
  58. llvm_unreachable("Unexpected CFI call opcode");
  59. }
  60. MachineOperand &Target = MBBI->getOperand(0);
  61. assert(Target.isReg() && "Invalid target operand for an indirect call");
  62. Target.setIsRenamable(false);
  63. MachineInstr *Check =
  64. BuildMI(MBB, MBBI, MBBI->getDebugLoc(), TII->get(AArch64::KCFI_CHECK))
  65. .addReg(Target.getReg())
  66. .addImm(MBBI->getCFIType())
  67. .getInstr();
  68. MBBI->setCFIType(*MBB.getParent(), 0);
  69. // If not already bundled, bundle the check and the call to prevent
  70. // further changes.
  71. if (!MBBI->isBundled())
  72. finalizeBundle(MBB, Check->getIterator(), std::next(MBBI->getIterator()));
  73. ++NumKCFIChecksAdded;
  74. return true;
  75. }
  76. bool AArch64KCFI::runOnMachineFunction(MachineFunction &MF) {
  77. const Module *M = MF.getMMI().getModule();
  78. if (!M->getModuleFlag("kcfi"))
  79. return false;
  80. const auto &SubTarget = MF.getSubtarget<AArch64Subtarget>();
  81. TII = SubTarget.getInstrInfo();
  82. bool Changed = false;
  83. for (MachineBasicBlock &MBB : MF) {
  84. for (MachineBasicBlock::instr_iterator MII = MBB.instr_begin(),
  85. MIE = MBB.instr_end();
  86. MII != MIE; ++MII) {
  87. if (MII->isCall() && MII->getCFIType())
  88. Changed |= emitCheck(MBB, MII);
  89. }
  90. }
  91. return Changed;
  92. }