Skip to content

StackProtector: ensure protection does not interfere with tail call frame #2704

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1691,16 +1691,35 @@ static bool MIIsInTerminatorSequence(const MachineInstr &MI) {
/// terminator, but additionally the copies that move the vregs into the
/// physical registers.
static MachineBasicBlock::iterator
FindSplitPointForStackProtector(MachineBasicBlock *BB) {
FindSplitPointForStackProtector(MachineBasicBlock *BB,
const TargetInstrInfo &TII) {
MachineBasicBlock::iterator SplitPoint = BB->getFirstTerminator();
//
if (SplitPoint == BB->begin())
return SplitPoint;

MachineBasicBlock::iterator Start = BB->begin();
MachineBasicBlock::iterator Previous = SplitPoint;
--Previous;

if (TII.isTailCall(*SplitPoint) &&
Previous->getOpcode() == TII.getCallFrameDestroyOpcode()) {
// Call frames cannot be nested, so if this frame is describing the tail
// call itself then we must insert before the sequence even starts. On the
// other hand, it could be an unrelated call in which case this tail call
// has to register moves of its own and should be the split point.
bool DifferentCall = false;
MachineBasicBlock::iterator I = Previous;
do {
--I;
if (I->isCall()) {
DifferentCall = true;
break;
}
} while(I->getOpcode() != TII.getCallFrameSetupOpcode());

return DifferentCall ? SplitPoint : I;
}

while (MIIsInTerminatorSequence(*Previous)) {
SplitPoint = Previous;
if (Previous == Start)
Expand Down Expand Up @@ -1740,7 +1759,7 @@ SelectionDAGISel::FinishBasicBlock() {
// Add load and check to the basicblock.
FuncInfo->MBB = ParentMBB;
FuncInfo->InsertPt =
FindSplitPointForStackProtector(ParentMBB);
FindSplitPointForStackProtector(ParentMBB, *TII);
SDB->visitSPDescriptorParent(SDB->SPDescriptor, ParentMBB);
CurDAG->setRoot(SDB->getRoot());
SDB->clear();
Expand All @@ -1759,7 +1778,7 @@ SelectionDAGISel::FinishBasicBlock() {
// register allocation issues caused by us splitting the parent mbb. The
// register allocator will clean up said virtual copies later on.
MachineBasicBlock::iterator SplitPoint =
FindSplitPointForStackProtector(ParentMBB);
FindSplitPointForStackProtector(ParentMBB, *TII);

// Splice the terminator of ParentMBB into SuccessMBB.
SuccessMBB->splice(SuccessMBB->end(), ParentMBB,
Expand Down
26 changes: 26 additions & 0 deletions llvm/test/CodeGen/X86/tailcc-ssp.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
; RUN: llc -mtriple=x86_64-windows-msvc %s -o - -verify-machineinstrs | FileCheck %s

declare void @h(i8*, i64, i8*)

define tailcc void @tailcall_frame(i8* %0, i64 %1) sspreq {
; CHECK-LABEL: tailcall_frame:
; CHECK: callq __security_check_cookie
; CHECK: xorl %ecx, %ecx
; CHECK: jmp h

tail call tailcc void @h(i8* null, i64 0, i8* null)
ret void
}

declare void @bar()
define void @tailcall_unrelated_frame() sspreq {
; CHECK-LABEL: tailcall_unrelated_frame:
; CHECK: subq [[STACK:\$.*]], %rsp
; CHECK: callq bar
; CHECK: callq __security_check_cookie
; CHECK: addq [[STACK]], %rsp
; CHECK: jmp bar
call void @bar()
tail call void @bar()
ret void
}