Skip to content
This repository was archived by the owner on Apr 23, 2020. It is now read-only.

Commit 64b2297

Browse files
committed
Patch by Vadim Chugunov
Win64 stack unwinder gets confused when execution flow "falls through" after a call to 'noreturn' function. This fixes the "missing epilogue" problem by emitting a trap instruction for IR 'unreachable' on x86_x64-pc-windows. A secondary use for it would be for anyone wanting to make double-sure that 'noreturn' functions, indeed, do not return. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@206684 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 2fa9e6c commit 64b2297

File tree

6 files changed

+37
-6
lines changed

6 files changed

+37
-6
lines changed

include/llvm/Target/TargetOptions.h

+5
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ namespace llvm {
5151
EnableFastISel(false), PositionIndependentExecutable(false),
5252
UseInitArray(false),
5353
DisableIntegratedAS(false), CompressDebugSections(false),
54+
TrapUnreachable(false),
5455
TrapFuncName(""), FloatABIType(FloatABI::Default),
5556
AllowFPOpFusion(FPOpFusion::Standard) {}
5657

@@ -162,6 +163,9 @@ namespace llvm {
162163
/// Compress DWARF debug sections.
163164
unsigned CompressDebugSections : 1;
164165

166+
/// Emit target-specific trap instruction for 'unreachable' IR instructions.
167+
unsigned TrapUnreachable : 1;
168+
165169
/// getTrapFunctionName - If this returns a non-empty string, this means
166170
/// isel should lower Intrinsic::trap to a call to the specified function
167171
/// name instead of an ISD::TRAP node.
@@ -216,6 +220,7 @@ inline bool operator==(const TargetOptions &LHS,
216220
ARE_EQUAL(EnableFastISel) &&
217221
ARE_EQUAL(PositionIndependentExecutable) &&
218222
ARE_EQUAL(UseInitArray) &&
223+
ARE_EQUAL(TrapUnreachable) &&
219224
ARE_EQUAL(TrapFuncName) &&
220225
ARE_EQUAL(FloatABIType) &&
221226
ARE_EQUAL(AllowFPOpFusion);

lib/CodeGen/SelectionDAG/FastISel.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -1041,8 +1041,10 @@ FastISel::SelectOperator(const User *I, unsigned Opcode) {
10411041
}
10421042

10431043
case Instruction::Unreachable:
1044-
// Nothing to emit.
1045-
return true;
1044+
if (TM.Options.TrapUnreachable)
1045+
return FastEmit_(MVT::Other, MVT::Other, ISD::TRAP) != 0;
1046+
else
1047+
return true;
10461048

10471049
case Instruction::Alloca:
10481050
// FunctionLowering has the static-sized case covered.

lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -2765,6 +2765,11 @@ void SelectionDAGBuilder::visitIndirectBr(const IndirectBrInst &I) {
27652765
getValue(I.getAddress())));
27662766
}
27672767

2768+
void SelectionDAGBuilder::visitUnreachable(const UnreachableInst &I) {
2769+
if (DAG.getTarget().Options.TrapUnreachable)
2770+
DAG.setRoot(DAG.getNode(ISD::TRAP, getCurSDLoc(), MVT::Other, DAG.getRoot()));
2771+
}
2772+
27682773
void SelectionDAGBuilder::visitFSub(const User &I) {
27692774
// -0.0 - X --> fneg
27702775
Type *Ty = I.getType();

lib/CodeGen/SelectionDAG/SelectionDAGBuilder.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ class SelectionDAGBuilder {
642642
void visitBr(const BranchInst &I);
643643
void visitSwitch(const SwitchInst &I);
644644
void visitIndirectBr(const IndirectBrInst &I);
645-
void visitUnreachable(const UnreachableInst &I) { /* noop */ }
645+
void visitUnreachable(const UnreachableInst &I);
646646

647647
// Helpers for visitSwitch
648648
bool handleSmallSwitchRange(CaseRec& CR,

lib/Target/X86/X86TargetMachine.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,13 @@ X86TargetMachine::X86TargetMachine(const Target &T, StringRef TT,
108108
if (Options.FloatABIType == FloatABI::Default)
109109
this->Options.FloatABIType = FloatABI::Hard;
110110

111+
// Windows stack unwinder gets confused when execution flow "falls through"
112+
// after a call to 'noreturn' function.
113+
// To prevent that, we emit a trap for 'unreachable' IR instructions.
114+
// (which on X86, happens to be the 'ud2' instruction)
115+
if (Subtarget.isTargetWin64())
116+
this->Options.TrapUnreachable = true;
117+
111118
initAsmInfo();
112119
}
113120

test/CodeGen/X86/br-fold.ll

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
1-
; RUN: llc -march=x86-64 < %s | FileCheck %s
1+
; RUN: llc -mtriple=x86_64-apple-darwin < %s | FileCheck -check-prefix=X64_DARWIN %s
2+
; RUN: llc -mtriple=x86_64-pc-linux < %s | FileCheck -check-prefix=X64_LINUX %s
3+
; RUN: llc -mtriple=x86_64-pc-windows < %s | FileCheck -check-prefix=X64_WINDOWS %s
4+
; RUN: llc -mtriple=x86_64-pc-windows-gnu < %s | FileCheck -check-prefix=X64_WINDOWS_GNU %s
25

3-
; CHECK: orq
4-
; CHECK-NEXT: %bb8.i329
6+
; X64_DARWIN: orq
7+
; X64_DARWIN-NEXT: %bb8.i329
8+
9+
; X64_LINUX: orq %rax, %rcx
10+
; X64_LINUX-NEXT: %bb8.i329
11+
12+
; X64_WINDOWS: orq %rax, %rcx
13+
; X64_WINDOWS-NEXT: ud2
14+
15+
; X64_WINDOWS_GNU: orq %rax, %rcx
16+
; X64_WINDOWS_GNU-NEXT: ud2
517

618
@_ZN11xercesc_2_513SchemaSymbols21fgURI_SCHEMAFORSCHEMAE = external constant [33 x i16], align 32 ; <[33 x i16]*> [#uses=1]
719
@_ZN11xercesc_2_56XMLUni16fgNotationStringE = external constant [9 x i16], align 16 ; <[9 x i16]*> [#uses=1]

0 commit comments

Comments
 (0)