Skip to content

Commit

Permalink
[Win64] Insert int3 into trailing empty BBs
Browse files Browse the repository at this point in the history
Otherwise, the Win64 unwinder considers direct branches to such empty
trailing BBs to be a branch out of the function. It treats such a branch
as a tail call, which can only be part of an epilogue. If the unwinder
misclassifies such a branch as part of the epilogue, it will fail to
unwind the stack further. This can lead to bad stack traces, or failure
to handle exceptions properly. This is described in
https://llvm.org/PR45064#c4, and by the comment at the top of the
X86AvoidTrailingCallPass.cpp file.

It should be safe to insert int3 for such blocks. An empty trailing BB
that reaches this pass is pretty much guaranteed to be unreachable.  If
a program executed such a block, it would fall off the end of the
function.

Most of the complexity in this patch comes from threading through the
"EHFuncletEntry" boolean on the MIRParser and registering the pass so we
can stop and start codegen around it. I used an MIR test because we
should teach LLVM to optimize away these branches as a follow-up.

Reviewed By: hans

Differential Revision: https://reviews.llvm.org/D76531
  • Loading branch information
rnk committed Mar 23, 2020
1 parent ebf83c3 commit 5ff5ddd
Show file tree
Hide file tree
Showing 10 changed files with 305 additions and 40 deletions.
1 change: 1 addition & 0 deletions llvm/lib/CodeGen/MIRParser/MILexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ static MIToken::TokenKind getIdentifierKind(StringRef Identifier) {
.Case("liveout", MIToken::kw_liveout)
.Case("address-taken", MIToken::kw_address_taken)
.Case("landing-pad", MIToken::kw_landing_pad)
.Case("ehfunclet-entry", MIToken::kw_ehfunclet_entry)
.Case("liveins", MIToken::kw_liveins)
.Case("successors", MIToken::kw_successors)
.Case("floatpred", MIToken::kw_floatpred)
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/CodeGen/MIRParser/MILexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ struct MIToken {
kw_liveout,
kw_address_taken,
kw_landing_pad,
kw_ehfunclet_entry,
kw_liveins,
kw_successors,
kw_floatpred,
Expand Down
6 changes: 6 additions & 0 deletions llvm/lib/CodeGen/MIRParser/MIParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,7 @@ bool MIParser::parseBasicBlockDefinition(
lex();
bool HasAddressTaken = false;
bool IsLandingPad = false;
bool IsEHFuncletEntry = false;
MachineBasicBlockSection SectionType = MBBS_None;
unsigned Alignment = 0;
BasicBlock *BB = nullptr;
Expand All @@ -665,6 +666,10 @@ bool MIParser::parseBasicBlockDefinition(
IsLandingPad = true;
lex();
break;
case MIToken::kw_ehfunclet_entry:
IsEHFuncletEntry = true;
lex();
break;
case MIToken::kw_align:
if (parseAlignment(Alignment))
return true;
Expand Down Expand Up @@ -708,6 +713,7 @@ bool MIParser::parseBasicBlockDefinition(
if (HasAddressTaken)
MBB->setHasAddressTaken();
MBB->setIsEHPad(IsLandingPad);
MBB->setIsEHFuncletEntry(IsEHFuncletEntry);
if (SectionType != MBBS_None) {
MBB->setSectionType(SectionType);
MF.setBBSectionsType(BasicBlockSection::List);
Expand Down
5 changes: 5 additions & 0 deletions llvm/lib/CodeGen/MIRPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,11 @@ void MIPrinter::print(const MachineBasicBlock &MBB) {
OS << "landing-pad";
HasAttributes = true;
}
if (MBB.isEHFuncletEntry()) {
OS << (HasAttributes ? ", " : " (");
OS << "ehfunclet-entry";
HasAttributes = true;
}
if (MBB.getAlignment() != Align(1)) {
OS << (HasAttributes ? ", " : " (");
OS << "align " << MBB.getAlignment().value();
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/X86/X86.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ void initializeFixupLEAPassPass(PassRegistry &);
void initializeFPSPass(PassRegistry &);
void initializeWinEHStatePassPass(PassRegistry &);
void initializeX86AvoidSFBPassPass(PassRegistry &);
void initializeX86AvoidTrailingCallPassPass(PassRegistry &);
void initializeX86CallFrameOptimizationPass(PassRegistry &);
void initializeX86CmovConverterPassPass(PassRegistry &);
void initializeX86CondBrFoldingPassPass(PassRegistry &);
Expand Down
97 changes: 62 additions & 35 deletions llvm/lib/Target/X86/X86AvoidTrailingCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,29 @@
//
//===----------------------------------------------------------------------===//
//
// The Windows x64 unwinder has trouble unwinding the stack when a return
// address points to the end of the function. This pass maintains the invariant
// that every return address is inside the bounds of its parent function or
// funclet by inserting int3 if the last instruction would otherwise be a call.
// The Windows x64 unwinder decodes the instruction stream during unwinding.
// The unwinder decodes forward from the current PC to detect epilogue code
// patterns.
//
// First, this means that there must be an instruction after every
// call instruction for the unwinder to decode. LLVM must maintain the invariant
// that the last instruction of a function or funclet is not a call, or the
// unwinder may decode into the next function. Similarly, a call may not
// immediately precede an epilogue code pattern. As of this writing, the
// SEH_Epilogue pseudo instruction takes care of that.
//
// Second, all non-tail call jump targets must be within the *half-open*
// interval of the bounds of the function. The unwinder distinguishes between
// internal jump instructions and tail calls in an epilogue sequence by checking
// the jump target against the function bounds from the .pdata section. This
// means that the last regular MBB of an LLVM function must not be empty if
// there are regular jumps targeting it.
//
// This pass upholds these invariants by ensuring that blocks at the end of a
// function or funclet are a) not empty and b) do not end in a CALL instruction.
//
// Unwinder implementation for reference:
// https://github.com/dotnet/coreclr/blob/a9f3fc16483eecfc47fb79c362811d870be02249/src/unwinder/amd64/unwinder_amd64.cpp#L1015
//
//===----------------------------------------------------------------------===//

Expand All @@ -18,33 +37,35 @@
#include "X86Subtarget.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"

#define DEBUG_TYPE "x86-avoid-trailing-call"
#define AVOIDCALL_DESC "X86 avoid trailing call pass"
#define AVOIDCALL_NAME "x86-avoid-trailing-call"

#define DEBUG_TYPE AVOIDCALL_NAME

using namespace llvm;

namespace {

class X86AvoidTrailingCallPass : public MachineFunctionPass {
public:
X86AvoidTrailingCallPass() : MachineFunctionPass(ID) {}

bool runOnMachineFunction(MachineFunction &MF) override;

private:
StringRef getPassName() const override {
return "X86 avoid trailing call pass";
}
static char ID;

private:
StringRef getPassName() const override { return AVOIDCALL_DESC; }
};
} // end anonymous namespace

char X86AvoidTrailingCallPass::ID = 0;

} // end anonymous namespace

FunctionPass *llvm::createX86AvoidTrailingCallPass() {
return new X86AvoidTrailingCallPass();
}

INITIALIZE_PASS(X86AvoidTrailingCallPass, AVOIDCALL_NAME, AVOIDCALL_DESC, false, false)

// A real instruction is a non-meta, non-pseudo instruction. Some pseudos
// expand to nothing, and some expand to code. This logic conservatively assumes
// they might expand to nothing.
Expand All @@ -62,6 +83,11 @@ bool X86AvoidTrailingCallPass::runOnMachineFunction(MachineFunction &MF) {
const X86InstrInfo &TII = *STI.getInstrInfo();
assert(STI.isTargetWin64() && "pass only runs on Win64");

// We don't need to worry about any of the invariants described above if there
// is no unwind info (CFI).
if (!MF.hasWinCFI())
return false;

// FIXME: Perhaps this pass should also replace SEH_Epilogue by inserting nops
// before epilogues.

Expand All @@ -73,33 +99,34 @@ bool X86AvoidTrailingCallPass::runOnMachineFunction(MachineFunction &MF) {
if (NextMBB && !NextMBB->isEHFuncletEntry())
continue;

// Find the last real instruction in this block, or previous blocks if this
// block is empty.
MachineBasicBlock::reverse_iterator LastRealInstr;
for (MachineBasicBlock &RMBB :
make_range(MBB.getReverseIterator(), MF.rend())) {
LastRealInstr = llvm::find_if(reverse(RMBB), isRealInstruction);
if (LastRealInstr != RMBB.rend())
break;
}

// Do nothing if this function or funclet has no instructions.
if (LastRealInstr == MF.begin()->rend())
continue;
// Find the last real instruction in this block.
auto LastRealInstr = llvm::find_if(reverse(MBB), isRealInstruction);

// If this is a call instruction, insert int3 right after it with the same
// DebugLoc. Convert back to a forward iterator and advance the insertion
// position once.
if (isCallInstruction(*LastRealInstr)) {
// If the block is empty or the last real instruction is a call instruction,
// insert an int3. If there is a call instruction, insert the int3 between
// the call and any labels or other meta instructions. If the block is
// empty, insert at block end.
bool IsEmpty = LastRealInstr == MBB.rend();
bool IsCall = !IsEmpty && isCallInstruction(*LastRealInstr);
if (IsEmpty || IsCall) {
LLVM_DEBUG({
dbgs() << "inserting int3 after trailing call instruction:\n";
LastRealInstr->dump();
dbgs() << '\n';
if (IsCall) {
dbgs() << "inserting int3 after trailing call instruction:\n";
LastRealInstr->dump();
dbgs() << '\n';
} else {
dbgs() << "inserting int3 in trailing empty MBB:\n";
MBB.dump();
}
});

MachineBasicBlock::iterator MBBI = std::next(LastRealInstr.getReverse());
BuildMI(*LastRealInstr->getParent(), MBBI, LastRealInstr->getDebugLoc(),
TII.get(X86::INT3));
MachineBasicBlock::iterator MBBI = MBB.end();
DebugLoc DL;
if (IsCall) {
MBBI = std::next(LastRealInstr.getReverse());
DL = LastRealInstr->getDebugLoc();
}
BuildMI(MBB, MBBI, DL, TII.get(X86::INT3));
Changed = true;
}
}
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/X86/X86TargetMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeX86Target() {
initializeX86ExecutionDomainFixPass(PR);
initializeX86DomainReassignmentPass(PR);
initializeX86AvoidSFBPassPass(PR);
initializeX86AvoidTrailingCallPassPass(PR);
initializeX86SpeculativeLoadHardeningPassPass(PR);
initializeX86FlagsCopyLoweringPassPass(PR);
initializeX86CondBrFoldingPassPass(PR);
Expand Down
Loading

0 comments on commit 5ff5ddd

Please sign in to comment.