aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/CodeGen/StackProtector.cpp
diff options
context:
space:
mode:
authorMatt Arsenault <Matthew.Arsenault@amd.com>2025-07-15 18:37:59 +0900
committerGitHub <noreply@github.com>2025-07-15 18:37:59 +0900
commit259a11db5e8f558072a2253c02c775e39e23f05d (patch)
tree23473a41344b54e27e56a5e7073f3023c2a186be /llvm/lib/CodeGen/StackProtector.cpp
parenta1c61ac7563220f3b0180d4367937d910dc85849 (diff)
downloadllvm-259a11db5e8f558072a2253c02c775e39e23f05d.zip
llvm-259a11db5e8f558072a2253c02c775e39e23f05d.tar.gz
llvm-259a11db5e8f558072a2253c02c775e39e23f05d.tar.bz2
StackProtector: Use RuntimeLibcalls to query libcall names (#147913)
The compiler should not introduce calls to arbitrary strings that aren't defined in RuntimeLibcalls. Previously OpenBSD was disabling the default __stack_chk_fail, but there was no record of the alternative __stack_smash_handler function it emits instead. This also avoids a random triple check in the pass.
Diffstat (limited to 'llvm/lib/CodeGen/StackProtector.cpp')
-rw-r--r--llvm/lib/CodeGen/StackProtector.cpp28
1 files changed, 18 insertions, 10 deletions
diff --git a/llvm/lib/CodeGen/StackProtector.cpp b/llvm/lib/CodeGen/StackProtector.cpp
index 5f866ee..3ec7008 100644
--- a/llvm/lib/CodeGen/StackProtector.cpp
+++ b/llvm/lib/CodeGen/StackProtector.cpp
@@ -76,7 +76,7 @@ static bool InsertStackProtectors(const TargetMachine *TM, Function *F,
/// CreateFailBB - Create a basic block to jump to when the stack protector
/// check fails.
-static BasicBlock *CreateFailBB(Function *F, const Triple &Trip);
+static BasicBlock *CreateFailBB(Function *F, const TargetLowering &TLI);
bool SSPLayoutInfo::shouldEmitSDCheck(const BasicBlock &BB) const {
return HasPrologue && !HasIRCheck && isa<ReturnInst>(BB.getTerminator());
@@ -673,7 +673,7 @@ bool InsertStackProtectors(const TargetMachine *TM, Function *F,
// merge pass will merge together all of the various BB into one including
// fail BB generated by the stack protector pseudo instruction.
if (!FailBB)
- FailBB = CreateFailBB(F, TM->getTargetTriple());
+ FailBB = CreateFailBB(F, *TLI);
IRBuilder<> B(CheckLoc);
Value *Guard = getStackGuard(TLI, M, B);
@@ -706,7 +706,7 @@ bool InsertStackProtectors(const TargetMachine *TM, Function *F,
return HasPrologue;
}
-BasicBlock *CreateFailBB(Function *F, const Triple &Trip) {
+BasicBlock *CreateFailBB(Function *F, const TargetLowering &TLI) {
auto *M = F->getParent();
LLVMContext &Context = F->getContext();
BasicBlock *FailBB = BasicBlock::Create(Context, "CallStackCheckFailBlk", F);
@@ -716,17 +716,25 @@ BasicBlock *CreateFailBB(Function *F, const Triple &Trip) {
DILocation::get(Context, 0, 0, F->getSubprogram()));
FunctionCallee StackChkFail;
SmallVector<Value *, 1> Args;
- if (Trip.isOSOpenBSD()) {
- StackChkFail = M->getOrInsertFunction("__stack_smash_handler",
- Type::getVoidTy(Context),
+
+ if (const char *ChkFailName =
+ TLI.getLibcallName(RTLIB::STACKPROTECTOR_CHECK_FAIL)) {
+ StackChkFail =
+ M->getOrInsertFunction(ChkFailName, Type::getVoidTy(Context));
+ } else if (const char *SSHName =
+ TLI.getLibcallName(RTLIB::STACK_SMASH_HANDLER)) {
+ StackChkFail = M->getOrInsertFunction(SSHName, Type::getVoidTy(Context),
PointerType::getUnqual(Context));
Args.push_back(B.CreateGlobalString(F->getName(), "SSH"));
} else {
- StackChkFail =
- M->getOrInsertFunction("__stack_chk_fail", Type::getVoidTy(Context));
+ Context.emitError("no libcall available for stack protector");
}
- cast<Function>(StackChkFail.getCallee())->addFnAttr(Attribute::NoReturn);
- B.CreateCall(StackChkFail, Args);
+
+ if (StackChkFail) {
+ cast<Function>(StackChkFail.getCallee())->addFnAttr(Attribute::NoReturn);
+ B.CreateCall(StackChkFail, Args);
+ }
+
B.CreateUnreachable();
return FailBB;
}