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

Commit 1a7750e

Browse files
committed
[SSP] Remove llvm.stackprotectorcheck.
This is a cleanup patch for SSP support in LLVM. There is no functional change. llvm.stackprotectorcheck is not needed, because SelectionDAG isn't actually lowering it in SelectBasicBlock; rather, it adds check code in FinishBasicBlock, ignoring the position where the intrinsic is inserted (See FindSplitPointForStackProtector()). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@265851 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent c2571ee commit 1a7750e

15 files changed

+144
-177
lines changed

docs/LangRef.rst

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11946,44 +11946,6 @@ checked against the original guard by ``llvm.stackprotectorcheck``. If they are
1194611946
different, then ``llvm.stackprotectorcheck`` causes the program to abort by
1194711947
calling the ``__stack_chk_fail()`` function.
1194811948

11949-
'``llvm.stackprotectorcheck``' Intrinsic
11950-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
11951-
11952-
Syntax:
11953-
"""""""
11954-
11955-
::
11956-
11957-
declare void @llvm.stackprotectorcheck(i8** <guard>)
11958-
11959-
Overview:
11960-
"""""""""
11961-
11962-
The ``llvm.stackprotectorcheck`` intrinsic compares ``guard`` against an already
11963-
created stack protector and if they are not equal calls the
11964-
``__stack_chk_fail()`` function.
11965-
11966-
Arguments:
11967-
""""""""""
11968-
11969-
The ``llvm.stackprotectorcheck`` intrinsic requires one pointer argument, the
11970-
the variable ``@__stack_chk_guard``.
11971-
11972-
Semantics:
11973-
""""""""""
11974-
11975-
This intrinsic is provided to perform the stack protector check by comparing
11976-
``guard`` with the stack slot created by ``llvm.stackprotector`` and if the
11977-
values do not match call the ``__stack_chk_fail()`` function.
11978-
11979-
The reason to provide this as an IR level intrinsic instead of implementing it
11980-
via other IR operations is that in order to perform this operation at the IR
11981-
level without an intrinsic, one would need to create additional basic blocks to
11982-
handle the success/failure cases. This makes it difficult to stop the stack
11983-
protector check from disrupting sibling tail calls in Codegen. With this
11984-
intrinsic, we are able to generate the stack protector basic blocks late in
11985-
codegen after the tail call decision has occurred.
11986-
1198711949
'``llvm.objectsize``' Intrinsic
1198811950
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1198911951

include/llvm/CodeGen/StackProtector.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ class StackProtector : public FunctionPass {
7575
/// times.
7676
SmallPtrSet<const PHINode *, 16> VisitedPHIs;
7777

78+
// A prologue is generated.
79+
bool HasPrologue = false;
80+
81+
// IR checking code is generated.
82+
bool HasIRCheck = false;
83+
7884
/// InsertStackProtectors - Insert code into the prologue and epilogue of
7985
/// the function.
8086
///
@@ -120,6 +126,10 @@ class StackProtector : public FunctionPass {
120126
}
121127

122128
SSPLayoutKind getSSPLayout(const AllocaInst *AI) const;
129+
130+
// Return true if StackProtector is supposed to be handled by SelectionDAG.
131+
bool shouldEmitSDCheck(const BasicBlock &BB) const;
132+
123133
void adjustForColoring(const AllocaInst *From, const AllocaInst *To);
124134

125135
bool runOnFunction(Function &Fn) override;

include/llvm/IR/Intrinsics.td

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,6 @@ def int_assume : Intrinsic<[], [llvm_i1_ty], []>;
324324
// Stack Protector Intrinsic - The stackprotector intrinsic writes the stack
325325
// guard to the correct place on the stack frame.
326326
def int_stackprotector : Intrinsic<[], [llvm_ptr_ty, llvm_ptrptr_ty], []>;
327-
def int_stackprotectorcheck : Intrinsic<[], [llvm_ptrptr_ty],
328-
[IntrReadWriteArgMem]>;
329327

330328
// A counter increment for instrumentation based profiling.
331329
def int_instrprof_increment : Intrinsic<[],

include/llvm/Target/TargetLowering.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,11 +1011,18 @@ class TargetLoweringBase {
10111011
return PrefLoopAlignment;
10121012
}
10131013

1014-
/// If the target has a standard location for the stack protector cookie,
1014+
/// If the target has a standard location for the stack protector guard,
10151015
/// returns the address of that location. Otherwise, returns nullptr.
1016-
virtual Value *getStackCookieLocation(IRBuilder<> &IRB) const {
1017-
return nullptr;
1018-
}
1016+
virtual Value *getIRStackGuard(IRBuilder<> &IRB) const;
1017+
1018+
/// Inserts necessary declarations for SSP purpose. Should be used only when
1019+
/// getIRStackGuard returns nullptr.
1020+
virtual void insertSSPDeclarations(Module &M) const;
1021+
1022+
/// Return the variable that's previously inserted by insertSSPDeclarations,
1023+
/// if any, otherwise return nullptr. Should be used only when
1024+
/// getIRStackGuard returns nullptr.
1025+
virtual Value *getSDStackGuard(const Module &M) const;
10191026

10201027
/// If the target has a standard location for the unsafe stack pointer,
10211028
/// returns the address of that location. Otherwise, returns nullptr.

lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2014,7 +2014,10 @@ void SelectionDAGBuilder::visitSPDescriptorParent(StackProtectorDescriptor &SPD,
20142014
MachineFrameInfo *MFI = ParentBB->getParent()->getFrameInfo();
20152015
int FI = MFI->getStackProtectorIndex();
20162016

2017-
const Value *IRGuard = SPD.getGuard();
2017+
const Module &M = *ParentBB->getParent()->getFunction()->getParent();
2018+
const Value *IRGuard = TLI.getSDStackGuard(M);
2019+
assert(IRGuard && "Currently there must be an IR guard in order to use "
2020+
"SelectionDAG SSP");
20182021
SDValue GuardPtr = getValue(IRGuard);
20192022
SDValue StackSlotPtr = DAG.getFrameIndex(FI, PtrTy);
20202023

@@ -5517,18 +5520,6 @@ SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I, unsigned Intrinsic) {
55175520
case Intrinsic::invariant_end:
55185521
// Discard region information.
55195522
return nullptr;
5520-
case Intrinsic::stackprotectorcheck: {
5521-
// Do not actually emit anything for this basic block. Instead we initialize
5522-
// the stack protector descriptor and export the guard variable so we can
5523-
// access it in FinishBasicBlock.
5524-
const BasicBlock *BB = I.getParent();
5525-
SPDescriptor.initialize(BB, FuncInfo.MBBMap[BB], I);
5526-
ExportFromCurrentBlock(SPDescriptor.getGuard());
5527-
5528-
// Flush our exports since we are going to process a terminator.
5529-
(void)getControlRoot();
5530-
return nullptr;
5531-
}
55325523
case Intrinsic::clear_cache:
55335524
return TLI.getClearCacheBuiltinName();
55345525
case Intrinsic::donothing:

lib/CodeGen/SelectionDAG/SelectionDAGBuilder.h

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -464,29 +464,25 @@ class SelectionDAGBuilder {
464464
/// the same function, use the same failure basic block).
465465
class StackProtectorDescriptor {
466466
public:
467-
StackProtectorDescriptor() : ParentMBB(nullptr), SuccessMBB(nullptr),
468-
FailureMBB(nullptr), Guard(nullptr),
469-
GuardReg(0) { }
467+
StackProtectorDescriptor()
468+
: ParentMBB(nullptr), SuccessMBB(nullptr), FailureMBB(nullptr),
469+
GuardReg(0) {}
470470

471471
/// Returns true if all fields of the stack protector descriptor are
472472
/// initialized implying that we should/are ready to emit a stack protector.
473473
bool shouldEmitStackProtector() const {
474-
return ParentMBB && SuccessMBB && FailureMBB && Guard;
474+
return ParentMBB && SuccessMBB && FailureMBB;
475475
}
476476

477477
/// Initialize the stack protector descriptor structure for a new basic
478478
/// block.
479-
void initialize(const BasicBlock *BB,
480-
MachineBasicBlock *MBB,
481-
const CallInst &StackProtCheckCall) {
479+
void initialize(const BasicBlock *BB, MachineBasicBlock *MBB) {
482480
// Make sure we are not initialized yet.
483481
assert(!shouldEmitStackProtector() && "Stack Protector Descriptor is "
484482
"already initialized!");
485483
ParentMBB = MBB;
486484
SuccessMBB = AddSuccessorMBB(BB, MBB, /* IsLikely */ true);
487485
FailureMBB = AddSuccessorMBB(BB, MBB, /* IsLikely */ false, FailureMBB);
488-
if (!Guard)
489-
Guard = StackProtCheckCall.getArgOperand(0);
490486
}
491487

492488
/// Reset state that changes when we handle different basic blocks.
@@ -515,14 +511,12 @@ class SelectionDAGBuilder {
515511
/// always the same.
516512
void resetPerFunctionState() {
517513
FailureMBB = nullptr;
518-
Guard = nullptr;
519514
GuardReg = 0;
520515
}
521516

522517
MachineBasicBlock *getParentMBB() { return ParentMBB; }
523518
MachineBasicBlock *getSuccessMBB() { return SuccessMBB; }
524519
MachineBasicBlock *getFailureMBB() { return FailureMBB; }
525-
const Value *getGuard() { return Guard; }
526520

527521
unsigned getGuardReg() const { return GuardReg; }
528522
void setGuardReg(unsigned R) { GuardReg = R; }
@@ -545,10 +539,6 @@ class SelectionDAGBuilder {
545539
/// contain a call to __stack_chk_fail().
546540
MachineBasicBlock *FailureMBB;
547541

548-
/// The guard variable which we will compare against the stored value in the
549-
/// stack protector stack slot.
550-
const Value *Guard;
551-
552542
/// The virtual register holding the stack guard value.
553543
unsigned GuardReg;
554544

lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14-
#include "llvm/CodeGen/GCStrategy.h"
14+
#include "llvm/CodeGen/SelectionDAG.h"
1515
#include "ScheduleDAGSDNodes.h"
1616
#include "SelectionDAGBuilder.h"
1717
#include "llvm/ADT/PostOrderIterator.h"
@@ -25,15 +25,16 @@
2525
#include "llvm/CodeGen/FastISel.h"
2626
#include "llvm/CodeGen/FunctionLoweringInfo.h"
2727
#include "llvm/CodeGen/GCMetadata.h"
28+
#include "llvm/CodeGen/GCStrategy.h"
2829
#include "llvm/CodeGen/MachineFrameInfo.h"
2930
#include "llvm/CodeGen/MachineFunction.h"
3031
#include "llvm/CodeGen/MachineInstrBuilder.h"
3132
#include "llvm/CodeGen/MachineModuleInfo.h"
3233
#include "llvm/CodeGen/MachineRegisterInfo.h"
3334
#include "llvm/CodeGen/ScheduleHazardRecognizer.h"
3435
#include "llvm/CodeGen/SchedulerRegistry.h"
35-
#include "llvm/CodeGen/SelectionDAG.h"
3636
#include "llvm/CodeGen/SelectionDAGISel.h"
37+
#include "llvm/CodeGen/StackProtector.h"
3738
#include "llvm/CodeGen/WinEHFuncInfo.h"
3839
#include "llvm/IR/Constants.h"
3940
#include "llvm/IR/DebugInfo.h"
@@ -377,6 +378,8 @@ SelectionDAGISel::~SelectionDAGISel() {
377378
void SelectionDAGISel::getAnalysisUsage(AnalysisUsage &AU) const {
378379
AU.addRequired<AAResultsWrapperPass>();
379380
AU.addRequired<GCModuleInfo>();
381+
AU.addRequired<StackProtector>();
382+
AU.addPreserved<StackProtector>();
380383
AU.addPreserved<GCModuleInfo>();
381384
AU.addRequired<TargetLibraryInfoWrapperPass>();
382385
if (UseMBPI && OptLevel != CodeGenOpt::None)
@@ -1476,6 +1479,8 @@ void SelectionDAGISel::SelectAllBasicBlocks(const Function &Fn) {
14761479
LowerArguments(Fn);
14771480
}
14781481
}
1482+
if (getAnalysis<StackProtector>().shouldEmitSDCheck(*LLVMBB))
1483+
SDB->SPDescriptor.initialize(LLVMBB, FuncInfo->MBBMap[LLVMBB]);
14791484

14801485
if (Begin != BI)
14811486
++NumDAGBlocks;

0 commit comments

Comments
 (0)