X86AvoidTrailingCall.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //===----- X86AvoidTrailingCall.cpp - Insert int3 after trailing calls ----===//
  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. // The Windows x64 unwinder decodes the instruction stream during unwinding.
  10. // The unwinder decodes forward from the current PC to detect epilogue code
  11. // patterns.
  12. //
  13. // First, this means that there must be an instruction after every
  14. // call instruction for the unwinder to decode. LLVM must maintain the invariant
  15. // that the last instruction of a function or funclet is not a call, or the
  16. // unwinder may decode into the next function. Similarly, a call may not
  17. // immediately precede an epilogue code pattern. As of this writing, the
  18. // SEH_Epilogue pseudo instruction takes care of that.
  19. //
  20. // Second, all non-tail call jump targets must be within the *half-open*
  21. // interval of the bounds of the function. The unwinder distinguishes between
  22. // internal jump instructions and tail calls in an epilogue sequence by checking
  23. // the jump target against the function bounds from the .pdata section. This
  24. // means that the last regular MBB of an LLVM function must not be empty if
  25. // there are regular jumps targeting it.
  26. //
  27. // This pass upholds these invariants by ensuring that blocks at the end of a
  28. // function or funclet are a) not empty and b) do not end in a CALL instruction.
  29. //
  30. // Unwinder implementation for reference:
  31. // https://github.com/dotnet/coreclr/blob/a9f3fc16483eecfc47fb79c362811d870be02249/src/unwinder/amd64/unwinder_amd64.cpp#L1015
  32. //
  33. //===----------------------------------------------------------------------===//
  34. #include "X86.h"
  35. #include "X86InstrInfo.h"
  36. #include "X86Subtarget.h"
  37. #include "llvm/CodeGen/MachineInstrBuilder.h"
  38. #define AVOIDCALL_DESC "X86 avoid trailing call pass"
  39. #define AVOIDCALL_NAME "x86-avoid-trailing-call"
  40. #define DEBUG_TYPE AVOIDCALL_NAME
  41. using namespace llvm;
  42. namespace {
  43. class X86AvoidTrailingCallPass : public MachineFunctionPass {
  44. public:
  45. X86AvoidTrailingCallPass() : MachineFunctionPass(ID) {}
  46. bool runOnMachineFunction(MachineFunction &MF) override;
  47. static char ID;
  48. private:
  49. StringRef getPassName() const override { return AVOIDCALL_DESC; }
  50. };
  51. } // end anonymous namespace
  52. char X86AvoidTrailingCallPass::ID = 0;
  53. FunctionPass *llvm::createX86AvoidTrailingCallPass() {
  54. return new X86AvoidTrailingCallPass();
  55. }
  56. INITIALIZE_PASS(X86AvoidTrailingCallPass, AVOIDCALL_NAME, AVOIDCALL_DESC, false, false)
  57. // A real instruction is a non-meta, non-pseudo instruction. Some pseudos
  58. // expand to nothing, and some expand to code. This logic conservatively assumes
  59. // they might expand to nothing.
  60. static bool isRealInstruction(MachineInstr &MI) {
  61. return !MI.isPseudo() && !MI.isMetaInstruction();
  62. }
  63. // Return true if this is a call instruction, but not a tail call.
  64. static bool isCallInstruction(const MachineInstr &MI) {
  65. return MI.isCall() && !MI.isReturn();
  66. }
  67. bool X86AvoidTrailingCallPass::runOnMachineFunction(MachineFunction &MF) {
  68. const X86Subtarget &STI = MF.getSubtarget<X86Subtarget>();
  69. const X86InstrInfo &TII = *STI.getInstrInfo();
  70. assert(STI.isTargetWin64() && "pass only runs on Win64");
  71. // We don't need to worry about any of the invariants described above if there
  72. // is no unwind info (CFI).
  73. if (!MF.hasWinCFI())
  74. return false;
  75. // FIXME: Perhaps this pass should also replace SEH_Epilogue by inserting nops
  76. // before epilogues.
  77. bool Changed = false;
  78. for (MachineBasicBlock &MBB : MF) {
  79. // Look for basic blocks that precede funclet entries or are at the end of
  80. // the function.
  81. MachineBasicBlock *NextMBB = MBB.getNextNode();
  82. if (NextMBB && !NextMBB->isEHFuncletEntry())
  83. continue;
  84. // Find the last real instruction in this block.
  85. auto LastRealInstr = llvm::find_if(reverse(MBB), isRealInstruction);
  86. // If the block is empty or the last real instruction is a call instruction,
  87. // insert an int3. If there is a call instruction, insert the int3 between
  88. // the call and any labels or other meta instructions. If the block is
  89. // empty, insert at block end.
  90. bool IsEmpty = LastRealInstr == MBB.rend();
  91. bool IsCall = !IsEmpty && isCallInstruction(*LastRealInstr);
  92. if (IsEmpty || IsCall) {
  93. LLVM_DEBUG({
  94. if (IsCall) {
  95. dbgs() << "inserting int3 after trailing call instruction:\n";
  96. LastRealInstr->dump();
  97. dbgs() << '\n';
  98. } else {
  99. dbgs() << "inserting int3 in trailing empty MBB:\n";
  100. MBB.dump();
  101. }
  102. });
  103. MachineBasicBlock::iterator MBBI = MBB.end();
  104. DebugLoc DL;
  105. if (IsCall) {
  106. MBBI = std::next(LastRealInstr.getReverse());
  107. DL = LastRealInstr->getDebugLoc();
  108. }
  109. BuildMI(MBB, MBBI, DL, TII.get(X86::INT3));
  110. Changed = true;
  111. }
  112. }
  113. return Changed;
  114. }