Adds support for emitting Windows x64 Unwind V2 information, includes support `/d2epilogunwind` in clang-cl. Unwind v2 adds information about the epilogs in functions such that the unwinder can unwind even in the middle of an epilog, without having to disassembly the function to see what has or has not been cleaned up. Unwind v2 requires that all epilogs are in "canonical" form: * If there was a stack allocation (fixed or dynamic) in the prolog, then the first instruction in the epilog must be a stack deallocation. * Next, for each `PUSH` in the prolog there must be a corresponding `POP` instruction in exact reverse order. * Finally, the epilog must end with the terminator. This change adds a pass to validate epilogs in modules that have Unwind v2 enabled and, if they pass, emits new pseudo instructions to MC that 1) note that the function is using unwind v2 and 2) mark the start of the epilog (this is either the first `POP` if there is one, otherwise the terminator instruction). If a function does not meet these requirements, it is downgraded to Unwind v1 (i.e., these new pseudo instructions are not emitted). Note that the unwind v2 table only marks the size of the epilog in the "header" unwind code, but it's possible for epilogs to use different terminator instructions thus they are not all the same size. As a work around for this, MC will assume that all terminator instructions are 1-byte long - this still works correctly with the Windows unwinder as it is only using the size to do a range check to see if a thread is in an epilog or not, and since the instruction pointer will never be in the middle of an instruction and the terminator is always at the end of an epilog the range check will function correctly. This does mean, however, that the "at end" optimization (where an epilog unwind code can be elided if the last epilog is at the end of the function) can only be used if the terminator is 1-byte long. One other complication with the implementation is that the unwind table for a function is emitted during streaming, however we can't calculate the distance between an epilog and the end of the function at that time as layout hasn't been completed yet (thus some instructions may be relaxed). To work around this, epilog unwind codes are emitted via a fixup. This also means that we can't pre-emptively downgrade a function to Unwind v1 if one of these offsets is too large, so instead we raise an error (but I've passed through the location information, so the user will know which of their functions is problematic).
222 lines
7.4 KiB
C++
222 lines
7.4 KiB
C++
//===-- X86WinEHUnwindV2.cpp - Win x64 Unwind v2 ----------------*- C++ -*-===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
///
|
|
/// Implements the analysis required to detect if a function can use Unwind v2
|
|
/// information, and emits the neccesary pseudo instructions used by MC to
|
|
/// generate the unwind info.
|
|
///
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "MCTargetDesc/X86BaseInfo.h"
|
|
#include "X86.h"
|
|
#include "llvm/ADT/Statistic.h"
|
|
#include "llvm/CodeGen/MachineBasicBlock.h"
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
|
#include "llvm/CodeGen/TargetInstrInfo.h"
|
|
#include "llvm/CodeGen/TargetSubtargetInfo.h"
|
|
#include "llvm/IR/Module.h"
|
|
|
|
using namespace llvm;
|
|
|
|
#define DEBUG_TYPE "x86-wineh-unwindv2"
|
|
|
|
STATISTIC(MeetsUnwindV2Criteria,
|
|
"Number of functions that meet Unwind v2 criteria");
|
|
STATISTIC(FailsUnwindV2Criteria,
|
|
"Number of functions that fail Unwind v2 criteria");
|
|
|
|
namespace {
|
|
|
|
class X86WinEHUnwindV2 : public MachineFunctionPass {
|
|
public:
|
|
static char ID;
|
|
|
|
X86WinEHUnwindV2() : MachineFunctionPass(ID) {
|
|
initializeX86WinEHUnwindV2Pass(*PassRegistry::getPassRegistry());
|
|
}
|
|
|
|
StringRef getPassName() const override { return "WinEH Unwind V2"; }
|
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
|
bool rejectCurrentFunction() const {
|
|
FailsUnwindV2Criteria++;
|
|
return false;
|
|
}
|
|
};
|
|
|
|
enum class FunctionState {
|
|
InProlog,
|
|
HasProlog,
|
|
InEpilog,
|
|
FinishedEpilog,
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
char X86WinEHUnwindV2::ID = 0;
|
|
|
|
INITIALIZE_PASS(X86WinEHUnwindV2, "x86-wineh-unwindv2",
|
|
"Analyze and emit instructions for Win64 Unwind v2", false,
|
|
false)
|
|
|
|
FunctionPass *llvm::createX86WinEHUnwindV2Pass() {
|
|
return new X86WinEHUnwindV2();
|
|
}
|
|
|
|
bool X86WinEHUnwindV2::runOnMachineFunction(MachineFunction &MF) {
|
|
if (!MF.getFunction().getParent()->getModuleFlag("winx64-eh-unwindv2"))
|
|
return false;
|
|
|
|
// Current state of processing the function. We'll assume that all functions
|
|
// start with a prolog.
|
|
FunctionState State = FunctionState::InProlog;
|
|
|
|
// Prolog information.
|
|
SmallVector<int64_t> PushedRegs;
|
|
bool HasStackAlloc = false;
|
|
|
|
// Requested changes.
|
|
SmallVector<MachineInstr *> UnwindV2StartLocations;
|
|
|
|
for (MachineBasicBlock &MBB : MF) {
|
|
// Current epilog information. We assume that epilogs cannot cross basic
|
|
// block boundaries.
|
|
unsigned PoppedRegCount = 0;
|
|
bool HasStackDealloc = false;
|
|
MachineInstr *UnwindV2StartLocation = nullptr;
|
|
|
|
for (MachineInstr &MI : MBB) {
|
|
switch (MI.getOpcode()) {
|
|
//
|
|
// Prolog handling.
|
|
//
|
|
case X86::SEH_PushReg:
|
|
if (State != FunctionState::InProlog)
|
|
llvm_unreachable("SEH_PushReg outside of prolog");
|
|
PushedRegs.push_back(MI.getOperand(0).getImm());
|
|
break;
|
|
|
|
case X86::SEH_StackAlloc:
|
|
case X86::SEH_SetFrame:
|
|
if (State != FunctionState::InProlog)
|
|
llvm_unreachable("SEH_StackAlloc or SEH_SetFrame outside of prolog");
|
|
HasStackAlloc = true;
|
|
break;
|
|
|
|
case X86::SEH_EndPrologue:
|
|
if (State != FunctionState::InProlog)
|
|
llvm_unreachable("SEH_EndPrologue outside of prolog");
|
|
State = FunctionState::HasProlog;
|
|
break;
|
|
|
|
//
|
|
// Epilog handling.
|
|
//
|
|
case X86::SEH_BeginEpilogue:
|
|
if (State != FunctionState::HasProlog)
|
|
llvm_unreachable("SEH_BeginEpilogue in prolog or another epilog");
|
|
State = FunctionState::InEpilog;
|
|
break;
|
|
|
|
case X86::SEH_EndEpilogue:
|
|
if (State != FunctionState::InEpilog)
|
|
llvm_unreachable("SEH_EndEpilogue outside of epilog");
|
|
if ((HasStackAlloc != HasStackDealloc) ||
|
|
(PoppedRegCount != PushedRegs.size()))
|
|
// Non-canonical epilog, reject the function.
|
|
return rejectCurrentFunction();
|
|
|
|
// If we didn't find the start location, then use the end of the
|
|
// epilog.
|
|
if (!UnwindV2StartLocation)
|
|
UnwindV2StartLocation = &MI;
|
|
UnwindV2StartLocations.push_back(UnwindV2StartLocation);
|
|
State = FunctionState::FinishedEpilog;
|
|
break;
|
|
|
|
case X86::MOV64rr:
|
|
case X86::ADD64ri32:
|
|
if (State == FunctionState::InEpilog) {
|
|
// If the prolog contains a stack allocation, then the first
|
|
// instruction in the epilog must be to adjust the stack pointer.
|
|
if (!HasStackAlloc || HasStackDealloc || (PoppedRegCount > 0)) {
|
|
return rejectCurrentFunction();
|
|
}
|
|
HasStackDealloc = true;
|
|
} else if (State == FunctionState::FinishedEpilog)
|
|
// Unexpected instruction after the epilog.
|
|
return rejectCurrentFunction();
|
|
break;
|
|
|
|
case X86::POP64r:
|
|
if (State == FunctionState::InEpilog) {
|
|
// After the stack pointer has been adjusted, the epilog must
|
|
// POP each register in reverse order of the PUSHes in the prolog.
|
|
PoppedRegCount++;
|
|
if ((HasStackAlloc != HasStackDealloc) ||
|
|
(PoppedRegCount > PushedRegs.size()) ||
|
|
(PushedRegs[PushedRegs.size() - PoppedRegCount] !=
|
|
MI.getOperand(0).getReg())) {
|
|
return rejectCurrentFunction();
|
|
}
|
|
|
|
// Unwind v2 records the size of the epilog not from where we place
|
|
// SEH_BeginEpilogue (as that contains the instruction to adjust the
|
|
// stack pointer) but from the first POP instruction (if there is
|
|
// one).
|
|
if (!UnwindV2StartLocation) {
|
|
assert(PoppedRegCount == 1);
|
|
UnwindV2StartLocation = &MI;
|
|
}
|
|
} else if (State == FunctionState::FinishedEpilog)
|
|
// Unexpected instruction after the epilog.
|
|
return rejectCurrentFunction();
|
|
break;
|
|
|
|
default:
|
|
if (MI.isTerminator()) {
|
|
if (State == FunctionState::FinishedEpilog)
|
|
// Found the terminator after the epilog, we're now ready for
|
|
// another epilog.
|
|
State = FunctionState::HasProlog;
|
|
else if (State == FunctionState::InEpilog)
|
|
llvm_unreachable("Terminator in the middle of the epilog");
|
|
} else if (!MI.isDebugOrPseudoInstr()) {
|
|
if ((State == FunctionState::FinishedEpilog) ||
|
|
(State == FunctionState::InEpilog))
|
|
// Unknown instruction in or after the epilog.
|
|
return rejectCurrentFunction();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (UnwindV2StartLocations.empty()) {
|
|
assert(State == FunctionState::InProlog &&
|
|
"If there are no epilogs, then there should be no prolog");
|
|
return false;
|
|
}
|
|
|
|
MeetsUnwindV2Criteria++;
|
|
|
|
// Emit the pseudo instruction that marks the start of each epilog.
|
|
const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
|
|
for (MachineInstr *MI : UnwindV2StartLocations) {
|
|
BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
|
|
TII->get(X86::SEH_UnwindV2Start));
|
|
}
|
|
// Note that the function is using Unwind v2.
|
|
MachineBasicBlock &FirstMBB = MF.front();
|
|
BuildMI(FirstMBB, FirstMBB.front(), FirstMBB.front().getDebugLoc(),
|
|
TII->get(X86::SEH_UnwindVersion))
|
|
.addImm(2);
|
|
|
|
return true;
|
|
}
|