aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/CodeGen/StackProtector.cpp
diff options
context:
space:
mode:
authorXiang1 Zhang <xiang1.zhang@intel.com>2022-11-28 15:02:04 +0800
committerXiang1 Zhang <xiang1.zhang@intel.com>2022-12-01 13:20:36 +0800
commit416e8c6ad529c57f21f46c6f52ded96d3ed239fb (patch)
tree9e4cd928538883c813cdb03c9f20402eb589ea98 /llvm/lib/CodeGen/StackProtector.cpp
parentfc743f034a34d3fa0a6e4de3b34216e5ac5e4c93 (diff)
downloadllvm-416e8c6ad529c57f21f46c6f52ded96d3ed239fb.zip
llvm-416e8c6ad529c57f21f46c6f52ded96d3ed239fb.tar.gz
llvm-416e8c6ad529c57f21f46c6f52ded96d3ed239fb.tar.bz2
Enhance stack protector for calling no return function
Reviewed By: LuoYuanke, WangPengfei, lebedev.ri Differential Revision: https://reviews.llvm.org/D138774
Diffstat (limited to 'llvm/lib/CodeGen/StackProtector.cpp')
-rw-r--r--llvm/lib/CodeGen/StackProtector.cpp26
1 files changed, 19 insertions, 7 deletions
diff --git a/llvm/lib/CodeGen/StackProtector.cpp b/llvm/lib/CodeGen/StackProtector.cpp
index ff828f5..2eef88b 100644
--- a/llvm/lib/CodeGen/StackProtector.cpp
+++ b/llvm/lib/CodeGen/StackProtector.cpp
@@ -414,11 +414,11 @@ static Value *getStackGuard(const TargetLoweringBase *TLI, Module *M,
///
/// Returns true if the platform/triple supports the stackprotectorcreate pseudo
/// node.
-static bool CreatePrologue(Function *F, Module *M, ReturnInst *RI,
+static bool CreatePrologue(Function *F, Module *M, Instruction *CheckLoc,
const TargetLoweringBase *TLI, AllocaInst *&AI) {
bool SupportsSelectionDAGSP = false;
IRBuilder<> B(&F->getEntryBlock().front());
- PointerType *PtrTy = Type::getInt8PtrTy(RI->getContext());
+ PointerType *PtrTy = Type::getInt8PtrTy(CheckLoc->getContext());
AI = B.CreateAlloca(PtrTy, nullptr, "StackGuardSlot");
Value *GuardSlot = getStackGuard(TLI, M, B, &SupportsSelectionDAGSP);
@@ -443,14 +443,27 @@ bool StackProtector::InsertStackProtectors() {
AllocaInst *AI = nullptr; // Place on stack that stores the stack guard.
for (BasicBlock &BB : llvm::make_early_inc_range(*F)) {
- ReturnInst *RI = dyn_cast<ReturnInst>(BB.getTerminator());
- if (!RI)
+ Instruction *CheckLoc = dyn_cast<ReturnInst>(BB.getTerminator());
+ if (!CheckLoc) {
+ for (auto &Inst : BB) {
+ auto *CB = dyn_cast<CallBase>(&Inst);
+ if (!CB)
+ continue;
+ if (!CB->doesNotReturn())
+ continue;
+ // Do stack check before non-return calls (e.g: __cxa_throw)
+ CheckLoc = CB;
+ break;
+ }
+ }
+
+ if (!CheckLoc)
continue;
// Generate prologue instrumentation if not already generated.
if (!HasPrologue) {
HasPrologue = true;
- SupportsSelectionDAGSP &= CreatePrologue(F, M, RI, TLI, AI);
+ SupportsSelectionDAGSP &= CreatePrologue(F, M, CheckLoc, TLI, AI);
}
// SelectionDAG based code generation. Nothing else needs to be done here.
@@ -476,8 +489,7 @@ bool StackProtector::InsertStackProtectors() {
// verifier guarantees that a tail call is either directly before the
// return or with a single correct bitcast of the return value in between so
// we don't need to worry about many situations here.
- Instruction *CheckLoc = RI;
- Instruction *Prev = RI->getPrevNonDebugInstruction();
+ Instruction *Prev = CheckLoc->getPrevNonDebugInstruction();
if (Prev && isa<CallInst>(Prev) && cast<CallInst>(Prev)->isTailCall())
CheckLoc = Prev;
else if (Prev) {