LLVM  14.0.0git
X86AvoidTrailingCall.cpp
Go to the documentation of this file.
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 
35 #include "X86.h"
36 #include "X86InstrInfo.h"
37 #include "X86Subtarget.h"
39 
40 #define AVOIDCALL_DESC "X86 avoid trailing call pass"
41 #define AVOIDCALL_NAME "x86-avoid-trailing-call"
42 
43 #define DEBUG_TYPE AVOIDCALL_NAME
44 
45 using namespace llvm;
46 
47 namespace {
48 class X86AvoidTrailingCallPass : public MachineFunctionPass {
49 public:
50  X86AvoidTrailingCallPass() : MachineFunctionPass(ID) {}
51 
52  bool runOnMachineFunction(MachineFunction &MF) override;
53 
54  static char ID;
55 
56 private:
57  StringRef getPassName() const override { return AVOIDCALL_DESC; }
58 };
59 } // end anonymous namespace
60 
62 
64  return new X86AvoidTrailingCallPass();
65 }
66 
67 INITIALIZE_PASS(X86AvoidTrailingCallPass, AVOIDCALL_NAME, AVOIDCALL_DESC, false, false)
68 
69 // A real instruction is a non-meta, non-pseudo instruction. Some pseudos
70 // expand to nothing, and some expand to code. This logic conservatively assumes
71 // they might expand to nothing.
73  return !MI.isPseudo() && !MI.isMetaInstruction();
74 }
75 
76 // Return true if this is a call instruction, but not a tail call.
77 static bool isCallInstruction(const MachineInstr &MI) {
78  return MI.isCall() && !MI.isReturn();
79 }
80 
81 bool X86AvoidTrailingCallPass::runOnMachineFunction(MachineFunction &MF) {
82  const X86Subtarget &STI = MF.getSubtarget<X86Subtarget>();
83  const X86InstrInfo &TII = *STI.getInstrInfo();
84  assert(STI.isTargetWin64() && "pass only runs on Win64");
85 
86  // We don't need to worry about any of the invariants described above if there
87  // is no unwind info (CFI).
88  if (!MF.hasWinCFI())
89  return false;
90 
91  // FIXME: Perhaps this pass should also replace SEH_Epilogue by inserting nops
92  // before epilogues.
93 
94  bool Changed = false;
95  for (MachineBasicBlock &MBB : MF) {
96  // Look for basic blocks that precede funclet entries or are at the end of
97  // the function.
98  MachineBasicBlock *NextMBB = MBB.getNextNode();
99  if (NextMBB && !NextMBB->isEHFuncletEntry())
100  continue;
101 
102  // Find the last real instruction in this block.
103  auto LastRealInstr = llvm::find_if(reverse(MBB), isRealInstruction);
104 
105  // If the block is empty or the last real instruction is a call instruction,
106  // insert an int3. If there is a call instruction, insert the int3 between
107  // the call and any labels or other meta instructions. If the block is
108  // empty, insert at block end.
109  bool IsEmpty = LastRealInstr == MBB.rend();
110  bool IsCall = !IsEmpty && isCallInstruction(*LastRealInstr);
111  if (IsEmpty || IsCall) {
112  LLVM_DEBUG({
113  if (IsCall) {
114  dbgs() << "inserting int3 after trailing call instruction:\n";
115  LastRealInstr->dump();
116  dbgs() << '\n';
117  } else {
118  dbgs() << "inserting int3 in trailing empty MBB:\n";
119  MBB.dump();
120  }
121  });
122 
124  DebugLoc DL;
125  if (IsCall) {
126  MBBI = std::next(LastRealInstr.getReverse());
127  DL = LastRealInstr->getDebugLoc();
128  }
129  BuildMI(MBB, MBBI, DL, TII.get(X86::INT3));
130  Changed = true;
131  }
132  }
133 
134  return Changed;
135 }
llvm::MachineFunction::hasWinCFI
bool hasWinCFI() const
Definition: MachineFunction.h:708
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:103
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::createX86AvoidTrailingCallPass
FunctionPass * createX86AvoidTrailingCallPass()
Return a pass that inserts int3 at the end of the function if it ends with a CALL instruction.
Definition: X86AvoidTrailingCall.cpp:63
X86Subtarget.h
llvm::X86Subtarget::getInstrInfo
const X86InstrInfo * getInstrInfo() const override
Definition: X86Subtarget.h:560
llvm::MachineBasicBlock::isEHFuncletEntry
bool isEHFuncletEntry() const
Returns true if this is the entry block of an EH funclet.
Definition: MachineBasicBlock.h:552
llvm::ilist_node_with_parent::getNextNode
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
Definition: ilist_node.h:288
llvm::X86Subtarget
Definition: X86Subtarget.h:52
llvm::MachineFunctionPass
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
Definition: MachineFunctionPass.h:30
isCallInstruction
static bool isCallInstruction(const MachineInstr &MI)
Definition: X86AvoidTrailingCall.cpp:77
llvm::reverse
auto reverse(ContainerTy &&C, std::enable_if_t< has_rbegin< ContainerTy >::value > *=nullptr)
Definition: STLExtras.h:333
llvm::X86Subtarget::isTargetWin64
bool isTargetWin64() const
Definition: X86Subtarget.h:886
LLVM_DEBUG
#define LLVM_DEBUG(X)
Definition: Debug.h:101
llvm::MachineBasicBlock::dump
void dump() const
Definition: MachineBasicBlock.cpp:295
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
X86.h
INITIALIZE_PASS
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:37
TII
const HexagonInstrInfo * TII
Definition: HexagonCopyToCombine.cpp:129
llvm::MachineBasicBlock::rend
reverse_iterator rend()
Definition: MachineBasicBlock.h:278
llvm::MachineBasicBlock
Definition: MachineBasicBlock.h:95
llvm::MachineFunction::getSubtarget
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Definition: MachineFunction.h:626
llvm::MachineInstr
Representation of each machine instruction.
Definition: MachineInstr.h:64
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
AVOIDCALL_NAME
#define AVOIDCALL_NAME
Definition: X86AvoidTrailingCall.cpp:41
llvm::MachineFunction
Definition: MachineFunction.h:230
llvm::X86InstrInfo
Definition: X86InstrInfo.h:130
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
MBBI
MachineBasicBlock MachineBasicBlock::iterator MBBI
Definition: AArch64SLSHardening.cpp:75
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
llvm::find_if
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1578
MBB
MachineBasicBlock & MBB
Definition: AArch64SLSHardening.cpp:74
MachineInstrBuilder.h
llvm::BuildMI
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
Definition: MachineInstrBuilder.h:328
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
llvm::DebugLoc
A debug info location.
Definition: DebugLoc.h:33
X86InstrInfo.h
llvm::MachineInstrBundleIterator< MachineInstr >
AVOIDCALL_DESC
#define AVOIDCALL_DESC
Definition: X86AvoidTrailingCall.cpp:40
llvm::MachineBasicBlock::end
iterator end()
Definition: MachineBasicBlock.h:270
isRealInstruction
static bool isRealInstruction(MachineInstr &MI)
Definition: X86AvoidTrailingCall.cpp:72
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:37