X86KCFI.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //===---- X86KCFI.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 "X86.h"
  13. #include "X86InstrInfo.h"
  14. #include "X86Subtarget.h"
  15. #include "X86TargetMachine.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 "x86-kcfi"
  23. #define X86_KCFI_PASS_NAME "Insert KCFI indirect call checks"
  24. STATISTIC(NumKCFIChecksAdded, "Number of indirect call checks added");
  25. namespace {
  26. class X86KCFI : public MachineFunctionPass {
  27. public:
  28. static char ID;
  29. X86KCFI() : MachineFunctionPass(ID) {}
  30. StringRef getPassName() const override { return X86_KCFI_PASS_NAME; }
  31. bool runOnMachineFunction(MachineFunction &MF) override;
  32. private:
  33. /// Machine instruction info used throughout the class.
  34. const X86InstrInfo *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 X86KCFI::ID = 0;
  41. } // end anonymous namespace
  42. INITIALIZE_PASS(X86KCFI, DEBUG_TYPE, X86_KCFI_PASS_NAME, false, false)
  43. FunctionPass *llvm::createX86KCFIPass() { return new X86KCFI(); }
  44. bool X86KCFI::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. MachineFunction &MF = *MBB.getParent();
  52. // If the call target is a memory operand, unfold it and use R11 for the
  53. // call, so KCFI_CHECK won't have to recompute the address.
  54. switch (MBBI->getOpcode()) {
  55. case X86::CALL64m:
  56. case X86::CALL64m_NT:
  57. case X86::TAILJMPm64:
  58. case X86::TAILJMPm64_REX: {
  59. MachineBasicBlock::instr_iterator OrigCall = MBBI;
  60. SmallVector<MachineInstr *, 2> NewMIs;
  61. if (!TII->unfoldMemoryOperand(MF, *OrigCall, X86::R11, /*UnfoldLoad=*/true,
  62. /*UnfoldStore=*/false, NewMIs))
  63. report_fatal_error("Failed to unfold memory operand for a KCFI check");
  64. for (auto *NewMI : NewMIs)
  65. MBBI = MBB.insert(OrigCall, NewMI);
  66. assert(MBBI->isCall() &&
  67. "Unexpected instruction after memory operand unfolding");
  68. if (OrigCall->shouldUpdateCallSiteInfo())
  69. MF.moveCallSiteInfo(&*OrigCall, &*MBBI);
  70. MBBI->setCFIType(MF, OrigCall->getCFIType());
  71. OrigCall->eraseFromParent();
  72. break;
  73. }
  74. default:
  75. break;
  76. }
  77. MachineInstr *Check =
  78. BuildMI(MBB, MBBI, MBBI->getDebugLoc(), TII->get(X86::KCFI_CHECK))
  79. .getInstr();
  80. MachineOperand &Target = MBBI->getOperand(0);
  81. switch (MBBI->getOpcode()) {
  82. case X86::CALL64r:
  83. case X86::CALL64r_NT:
  84. case X86::TAILJMPr64:
  85. case X86::TAILJMPr64_REX:
  86. assert(Target.isReg() && "Unexpected target operand for an indirect call");
  87. Check->addOperand(MachineOperand::CreateReg(Target.getReg(), false));
  88. Target.setIsRenamable(false);
  89. break;
  90. case X86::CALL64pcrel32:
  91. case X86::TAILJMPd64:
  92. assert(Target.isSymbol() && "Unexpected target operand for a direct call");
  93. // X86TargetLowering::EmitLoweredIndirectThunk always uses r11 for
  94. // 64-bit indirect thunk calls.
  95. assert(StringRef(Target.getSymbolName()).endswith("_r11") &&
  96. "Unexpected register for an indirect thunk call");
  97. Check->addOperand(MachineOperand::CreateReg(X86::R11, false));
  98. break;
  99. default:
  100. llvm_unreachable("Unexpected CFI call opcode");
  101. }
  102. Check->addOperand(MachineOperand::CreateImm(MBBI->getCFIType()));
  103. MBBI->setCFIType(MF, 0);
  104. // If not already bundled, bundle the check and the call to prevent
  105. // further changes.
  106. if (!MBBI->isBundled())
  107. finalizeBundle(MBB, Check->getIterator(), std::next(MBBI->getIterator()));
  108. ++NumKCFIChecksAdded;
  109. return true;
  110. }
  111. bool X86KCFI::runOnMachineFunction(MachineFunction &MF) {
  112. const Module *M = MF.getMMI().getModule();
  113. if (!M->getModuleFlag("kcfi"))
  114. return false;
  115. const auto &SubTarget = MF.getSubtarget<X86Subtarget>();
  116. TII = SubTarget.getInstrInfo();
  117. bool Changed = false;
  118. for (MachineBasicBlock &MBB : MF) {
  119. for (MachineBasicBlock::instr_iterator MII = MBB.instr_begin(),
  120. MIE = MBB.instr_end();
  121. MII != MIE; ++MII) {
  122. if (MII->isCall() && MII->getCFIType())
  123. Changed |= emitCheck(MBB, MII);
  124. }
  125. }
  126. return Changed;
  127. }