Skip to content

Commit

Permalink
Fix for PR30687. Avoid dereferencing MBB.end().
Browse files Browse the repository at this point in the history
We don't need to return a MachineInstr* from these stack probe insertion
calls anyway. If we ever need to add it back, we can return an iterator
instead.

Based on a patch by David Kreitzer

This bug is a consequence of

r279314 | dexonsmith | 2016-08-19 13:40:12 -0700 (Fri, 19 Aug 2016) | 110 lines

We hit the "Assertion `!NodePtr->isKnownSentinel()' failed" assertion,
but only when inserting a stack probe call at the end of an MBB, which
isn't necessarily a common situation.

Differential Revision: https://reviews.llvm.org/D25566

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@284130 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
rnk committed Oct 13, 2016
1 parent e0d080d commit dc99d00
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 38 deletions.
39 changes: 18 additions & 21 deletions lib/Target/X86/X86FrameLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,20 +447,19 @@ void X86FrameLowering::emitCalleeSavedFrameMoves(
}
}

MachineInstr *X86FrameLowering::emitStackProbe(MachineFunction &MF,
MachineBasicBlock &MBB,
MachineBasicBlock::iterator MBBI,
const DebugLoc &DL,
bool InProlog) const {
void X86FrameLowering::emitStackProbe(MachineFunction &MF,
MachineBasicBlock &MBB,
MachineBasicBlock::iterator MBBI,
const DebugLoc &DL, bool InProlog) const {
const X86Subtarget &STI = MF.getSubtarget<X86Subtarget>();
if (STI.isTargetWindowsCoreCLR()) {
if (InProlog) {
return emitStackProbeInlineStub(MF, MBB, MBBI, DL, true);
emitStackProbeInlineStub(MF, MBB, MBBI, DL, true);
} else {
return emitStackProbeInline(MF, MBB, MBBI, DL, false);
emitStackProbeInline(MF, MBB, MBBI, DL, false);
}
} else {
return emitStackProbeCall(MF, MBB, MBBI, DL, InProlog);
emitStackProbeCall(MF, MBB, MBBI, DL, InProlog);
}
}

Expand Down Expand Up @@ -489,9 +488,11 @@ void X86FrameLowering::inlineStackProbe(MachineFunction &MF,
}
}

MachineInstr *X86FrameLowering::emitStackProbeInline(
MachineFunction &MF, MachineBasicBlock &MBB,
MachineBasicBlock::iterator MBBI, const DebugLoc &DL, bool InProlog) const {
void X86FrameLowering::emitStackProbeInline(MachineFunction &MF,
MachineBasicBlock &MBB,
MachineBasicBlock::iterator MBBI,
const DebugLoc &DL,
bool InProlog) const {
const X86Subtarget &STI = MF.getSubtarget<X86Subtarget>();
assert(STI.is64Bit() && "different expansion needed for 32 bit");
assert(STI.isTargetWindowsCoreCLR() && "custom expansion expects CoreCLR");
Expand Down Expand Up @@ -701,13 +702,13 @@ MachineInstr *X86FrameLowering::emitStackProbeInline(
}

// Possible TODO: physreg liveness for InProlog case.

return &*ContinueMBBI;
}

MachineInstr *X86FrameLowering::emitStackProbeCall(
MachineFunction &MF, MachineBasicBlock &MBB,
MachineBasicBlock::iterator MBBI, const DebugLoc &DL, bool InProlog) const {
void X86FrameLowering::emitStackProbeCall(MachineFunction &MF,
MachineBasicBlock &MBB,
MachineBasicBlock::iterator MBBI,
const DebugLoc &DL,
bool InProlog) const {
bool IsLargeCodeModel = MF.getTarget().getCodeModel() == CodeModel::Large;

unsigned CallOp;
Expand Down Expand Up @@ -765,20 +766,16 @@ MachineInstr *X86FrameLowering::emitStackProbeCall(
for (++ExpansionMBBI; ExpansionMBBI != MBBI; ++ExpansionMBBI)
ExpansionMBBI->setFlag(MachineInstr::FrameSetup);
}

return &*MBBI;
}

MachineInstr *X86FrameLowering::emitStackProbeInlineStub(
void X86FrameLowering::emitStackProbeInlineStub(
MachineFunction &MF, MachineBasicBlock &MBB,
MachineBasicBlock::iterator MBBI, const DebugLoc &DL, bool InProlog) const {

assert(InProlog && "ChkStkStub called outside prolog!");

BuildMI(MBB, MBBI, DL, TII.get(X86::CALLpcrel32))
.addExternalSymbol("__chkstk_stub");

return &*MBBI;
}

static unsigned calculateSetFPREG(uint64_t SPAdjust) {
Expand Down
30 changes: 13 additions & 17 deletions lib/Target/X86/X86FrameLowering.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,10 @@ class X86FrameLowering : public TargetFrameLowering {

/// Emit target stack probe code. This is required for all
/// large stack allocations on Windows. The caller is required to materialize
/// the number of bytes to probe in RAX/EAX. Returns instruction just
/// after the expansion.
MachineInstr *emitStackProbe(MachineFunction &MF, MachineBasicBlock &MBB,
MachineBasicBlock::iterator MBBI,
const DebugLoc &DL, bool InProlog) const;
/// the number of bytes to probe in RAX/EAX.
void emitStackProbe(MachineFunction &MF, MachineBasicBlock &MBB,
MachineBasicBlock::iterator MBBI, const DebugLoc &DL,
bool InProlog) const;

/// Replace a StackProbe inline-stub with the actual probe code inline.
void inlineStackProbe(MachineFunction &MF,
Expand Down Expand Up @@ -179,22 +178,19 @@ class X86FrameLowering : public TargetFrameLowering {
uint64_t calculateMaxStackAlign(const MachineFunction &MF) const;

/// Emit target stack probe as a call to a helper function
MachineInstr *emitStackProbeCall(MachineFunction &MF, MachineBasicBlock &MBB,
MachineBasicBlock::iterator MBBI,
const DebugLoc &DL, bool InProlog) const;
void emitStackProbeCall(MachineFunction &MF, MachineBasicBlock &MBB,
MachineBasicBlock::iterator MBBI, const DebugLoc &DL,
bool InProlog) const;

/// Emit target stack probe as an inline sequence.
MachineInstr *emitStackProbeInline(MachineFunction &MF,
MachineBasicBlock &MBB,
MachineBasicBlock::iterator MBBI,
const DebugLoc &DL, bool InProlog) const;
void emitStackProbeInline(MachineFunction &MF, MachineBasicBlock &MBB,
MachineBasicBlock::iterator MBBI,
const DebugLoc &DL, bool InProlog) const;

/// Emit a stub to later inline the target stack probe.
MachineInstr *emitStackProbeInlineStub(MachineFunction &MF,
MachineBasicBlock &MBB,
MachineBasicBlock::iterator MBBI,
const DebugLoc &DL,
bool InProlog) const;
void emitStackProbeInlineStub(MachineFunction &MF, MachineBasicBlock &MBB,
MachineBasicBlock::iterator MBBI,
const DebugLoc &DL, bool InProlog) const;

/// Aligns the stack pointer by ANDing it with -MaxAlign.
void BuildStackAlignAND(MachineBasicBlock &MBB,
Expand Down
17 changes: 17 additions & 0 deletions test/CodeGen/X86/win_chkstk.ll
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,20 @@ entry:
%array4096 = alloca [4096 x i8], align 16 ; <[4096 x i8]*> [#uses=0]
ret i32 0
}

; PR30687: Avoid crashing when inserting a __chkstk call at the end of an MBB.
define void @dont_crash() {
entry:
; WIN_X32: calll __chkstk
; WIN_X64: callq __chkstk
; WIN64_LARGE: movabsq $__chkstk, %r11
; WIN64_LARGE: callq *%r11
; MINGW_X32: calll __alloca
; MINGW_X64: callq ___chkstk_ms
; LINUX-NOT: call __chkstk
%buffer = alloca [4096 x i8]
br label %ret

ret:
ret void
}

0 comments on commit dc99d00

Please sign in to comment.