[llvm-branch-commits] [llvm] StackProtector: Take libcall signatures from RuntimeLibcallsInfo (PR #241144)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Tue Nov 10 22:07:36 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-arm
Author: Matt Arsenault (arsenm)
<details>
<summary>Changes</summary>
Stop constructing the types by hand, which also missed setting the
calling convention.
This also maintains the special case setting of dso_local, which I
also think should be automatically applied but this requires
additional work.
Co-authored-by: Claude Opus 5.5 <noreply@<!-- -->anthropic.com>
---
Full diff: https://github.com/llvm/llvm-project/pull/241144.diff
7 Files Affected:
- (modified) llvm/lib/CodeGen/StackProtector.cpp (+22-18)
- (modified) llvm/lib/CodeGen/TargetLoweringBase.cpp (+2-6)
- (modified) llvm/lib/Target/AArch64/AArch64ISelLowering.cpp (+3-5)
- (modified) llvm/lib/Target/ARM/ARMISelLowering.cpp (+3-5)
- (modified) llvm/lib/Target/X86/X86ISelLoweringCall.cpp (+3-5)
- (modified) llvm/test/CodeGen/ARM/Windows/stack-protector-musttail.ll (+5-5)
- (added) llvm/test/Transforms/StackProtector/libcall-decls.ll (+51)
``````````diff
diff --git a/llvm/lib/CodeGen/StackProtector.cpp b/llvm/lib/CodeGen/StackProtector.cpp
index ec5144a73d97f7..b2558ffcc4dd93 100644
--- a/llvm/lib/CodeGen/StackProtector.cpp
+++ b/llvm/lib/CodeGen/StackProtector.cpp
@@ -756,29 +756,33 @@ BasicBlock *CreateFailBB(Function *F, const LibcallLoweringInfo &Libcalls) {
if (F->getSubprogram())
B.SetCurrentDebugLocation(
DILocation::get(Context, 0, 0, F->getSubprogram()));
- FunctionCallee StackChkFail;
- SmallVector<Value *, 1> Args;
-
- if (RTLIB::LibcallImpl ChkFailImpl =
- Libcalls.getLibcallImpl(RTLIB::STACKPROTECTOR_CHECK_FAIL)) {
- StackChkFail = M->getOrInsertFunction(
- RTLIB::RuntimeLibcallsInfo::getLibcallImplName(ChkFailImpl),
- Type::getVoidTy(Context));
- } else if (RTLIB::LibcallImpl SSHImpl =
- Libcalls.getLibcallImpl(RTLIB::STACK_SMASH_HANDLER)) {
- StackChkFail = M->getOrInsertFunction(
- RTLIB::RuntimeLibcallsInfo::getLibcallImplName(SSHImpl),
- Type::getVoidTy(Context), PointerType::getUnqual(Context));
- Args.push_back(B.CreateGlobalString(F->getName(), "SSH"));
- } else {
+ bool IsSmashHandler = false;
+ RTLIB::LibcallImpl ChkFailImpl =
+ Libcalls.getLibcallImpl(RTLIB::STACKPROTECTOR_CHECK_FAIL);
+ if (ChkFailImpl == RTLIB::Unsupported) {
+ ChkFailImpl = Libcalls.getLibcallImpl(RTLIB::STACK_SMASH_HANDLER);
+ IsSmashHandler = true;
+ }
+
+ if (ChkFailImpl == RTLIB::Unsupported) {
Context.emitError("no libcall available for stack protector");
+ B.CreateUnreachable();
+ return FailBB;
}
- if (StackChkFail) {
- CallInst *Call = B.CreateCall(StackChkFail, Args);
- Call->addFnAttr(Attribute::NoReturn);
+ FunctionCallee StackChkFail =
+ Libcalls.getRuntimeLibcallsInfo().getOrInsertLibcallDecl(*M, ChkFailImpl);
+
+ CallInst *Call;
+ if (IsSmashHandler) {
+ Call =
+ B.CreateCall(StackChkFail, B.CreateGlobalString(F->getName(), "SSH"));
+ } else {
+ Call = B.CreateCall(StackChkFail);
}
+ Call->setCallingConv(Libcalls.getLibcallImplCallingConv(ChkFailImpl));
+ Call->addFnAttr(Attribute::NoReturn);
B.CreateUnreachable();
return FailBB;
}
diff --git a/llvm/lib/CodeGen/TargetLoweringBase.cpp b/llvm/lib/CodeGen/TargetLoweringBase.cpp
index 45a657d1985e5e..d937edb2ffb434 100644
--- a/llvm/lib/CodeGen/TargetLoweringBase.cpp
+++ b/llvm/lib/CodeGen/TargetLoweringBase.cpp
@@ -2185,12 +2185,8 @@ TargetLoweringBase::getIRStackGuard(IRBuilderBase &IRB,
if (GuardLocalImpl != RTLIB::impl___guard_local)
return nullptr;
- Module &M = *IRB.getModule();
- const DataLayout &DL = M.getDataLayout();
- PointerType *PtrTy =
- PointerType::get(M.getContext(), DL.getDefaultGlobalsAddressSpace());
- GlobalVariable *G =
- M.getOrInsertGlobal(getLibcallImplName(GuardLocalImpl), PtrTy);
+ GlobalValue *G = Libcalls.getRuntimeLibcallsInfo().getOrInsertImplGlobal(
+ *IRB.getModule(), GuardLocalImpl);
G->setVisibility(GlobalValue::HiddenVisibility);
return G;
}
diff --git a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
index 7ff81371f81705..80e4c109e9b02f 100644
--- a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
+++ b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
@@ -34195,14 +34195,12 @@ void AArch64TargetLowering::insertSSPDeclarations(
if (SecurityCheckCookieLibcall != RTLIB::Unsupported &&
SecurityCookieVar != RTLIB::Unsupported) {
// MSVC CRT has a global variable holding security cookie.
- M.getOrInsertGlobal(getLibcallImplName(SecurityCookieVar),
- PointerType::getUnqual(M.getContext()));
+ const RTLIB::RuntimeLibcallsInfo &RTLCI = Libcalls.getRuntimeLibcallsInfo();
+ RTLCI.getOrInsertImplGlobal(M, SecurityCookieVar);
// MSVC CRT has a function to validate security cookie.
FunctionCallee SecurityCheckCookie =
- M.getOrInsertFunction(getLibcallImplName(SecurityCheckCookieLibcall),
- Type::getVoidTy(M.getContext()),
- PointerType::getUnqual(M.getContext()));
+ RTLCI.getOrInsertLibcallDecl(M, SecurityCheckCookieLibcall);
if (Function *F = dyn_cast<Function>(SecurityCheckCookie.getCallee())) {
F->setCallingConv(CallingConv::Win64);
F->addParamAttr(0, Attribute::AttrKind::InReg);
diff --git a/llvm/lib/Target/ARM/ARMISelLowering.cpp b/llvm/lib/Target/ARM/ARMISelLowering.cpp
index d910f9cc2e4ec3..d5f936befb9a97 100644
--- a/llvm/lib/Target/ARM/ARMISelLowering.cpp
+++ b/llvm/lib/Target/ARM/ARMISelLowering.cpp
@@ -21959,14 +21959,12 @@ void ARMTargetLowering::insertSSPDeclarations(
if (SecurityCheckCookieLibcall != RTLIB::Unsupported &&
SecurityCookieVar != RTLIB::Unsupported) {
// MSVC CRT has a global variable holding security cookie.
- M.getOrInsertGlobal(getLibcallImplName(SecurityCookieVar),
- PointerType::getUnqual(M.getContext()));
+ const RTLIB::RuntimeLibcallsInfo &RTLCI = Libcalls.getRuntimeLibcallsInfo();
+ RTLCI.getOrInsertImplGlobal(M, SecurityCookieVar);
// MSVC CRT has a function to validate security cookie.
FunctionCallee SecurityCheckCookie =
- M.getOrInsertFunction(getLibcallImplName(SecurityCheckCookieLibcall),
- Type::getVoidTy(M.getContext()),
- PointerType::getUnqual(M.getContext()));
+ RTLCI.getOrInsertLibcallDecl(M, SecurityCheckCookieLibcall);
if (Function *F = dyn_cast<Function>(SecurityCheckCookie.getCallee()))
F->addParamAttr(0, Attribute::AttrKind::InReg);
}
diff --git a/llvm/lib/Target/X86/X86ISelLoweringCall.cpp b/llvm/lib/Target/X86/X86ISelLoweringCall.cpp
index a78621fd897b35..dc425d638e7820 100644
--- a/llvm/lib/Target/X86/X86ISelLoweringCall.cpp
+++ b/llvm/lib/Target/X86/X86ISelLoweringCall.cpp
@@ -617,14 +617,12 @@ void X86TargetLowering::insertSSPDeclarations(
SecurityCookieVar != RTLIB::Unsupported) {
// MSVC CRT provides functionalities for stack protection.
// MSVC CRT has a global variable holding security cookie.
- M.getOrInsertGlobal(getLibcallImplName(SecurityCookieVar),
- PointerType::getUnqual(M.getContext()));
+ const RTLIB::RuntimeLibcallsInfo &RTLCI = Libcalls.getRuntimeLibcallsInfo();
+ RTLCI.getOrInsertImplGlobal(M, SecurityCookieVar);
// MSVC CRT has a function to validate security cookie.
FunctionCallee SecurityCheckCookie =
- M.getOrInsertFunction(getLibcallImplName(SecurityCheckCookieLibcall),
- Type::getVoidTy(M.getContext()),
- PointerType::getUnqual(M.getContext()));
+ RTLCI.getOrInsertLibcallDecl(M, SecurityCheckCookieLibcall);
if (Function *F = dyn_cast<Function>(SecurityCheckCookie.getCallee())) {
F->setCallingConv(CallingConv::X86_FastCall);
diff --git a/llvm/test/CodeGen/ARM/Windows/stack-protector-musttail.ll b/llvm/test/CodeGen/ARM/Windows/stack-protector-musttail.ll
index c35738c87c6c83..f3632dc4d1366b 100644
--- a/llvm/test/CodeGen/ARM/Windows/stack-protector-musttail.ll
+++ b/llvm/test/CodeGen/ARM/Windows/stack-protector-musttail.ll
@@ -8,7 +8,7 @@ define void @caller1() sspreq {
; CHECK-LABEL: define void @caller1()
; Prologue:
-; CHECK: call void @__security_check_cookie
+; CHECK: call arm_aapcs_vfpcc void @__security_check_cookie
; CHECK: musttail call void @callee()
; CHECK-NEXT: ret void
@@ -23,7 +23,7 @@ define void @justret() sspreq {
; Prologue:
; CHECK: @llvm.stackguard
-; CHECK: call void @__security_check_cookie
+; CHECK: call arm_aapcs_vfpcc void @__security_check_cookie
; CHECK: ret void
%var = alloca [2 x i64]
@@ -41,7 +41,7 @@ define ptr @caller2() sspreq {
; Prologue:
; CHECK: @llvm.stackguard
-; CHECK: call void @__security_check_cookie
+; CHECK: call arm_aapcs_vfpcc void @__security_check_cookie
; CHECK: [[TMP:%.*]] = musttail call ptr @callee2()
; CHECK-NEXT: ret ptr [[TMP]]
@@ -56,7 +56,7 @@ define void @caller3() sspreq {
; CHECK-LABEL: define void @caller3()
; Prologue:
-; CHECK: call void @__security_check_cookie
+; CHECK: call arm_aapcs_vfpcc void @__security_check_cookie
; CHECK: tail call void @callee()
; CHECK-NEXT: ret void
@@ -71,7 +71,7 @@ define ptr @caller4() sspreq {
; Prologue:
; CHECK: @llvm.stackguard
-; CHECK: call void @__security_check_cookie
+; CHECK: call arm_aapcs_vfpcc void @__security_check_cookie
; CHECK: [[TMP:%.*]] = tail call ptr @callee2()
; CHECK-NEXT: ret ptr [[TMP]]
diff --git a/llvm/test/Transforms/StackProtector/libcall-decls.ll b/llvm/test/Transforms/StackProtector/libcall-decls.ll
new file mode 100644
index 00000000000000..24f749e45705da
--- /dev/null
+++ b/llvm/test/Transforms/StackProtector/libcall-decls.ll
@@ -0,0 +1,51 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; REQUIRES: arm-registered-target, x86-registered-target
+; RUN: opt -mtriple=armv7-unknown-linux-gnueabi -enable-selectiondag-sp=false -passes='require<libcall-lowering-info>,stack-protector' -S %s | FileCheck --check-prefix=ARM %s
+; RUN: opt -mtriple=x86_64-unknown-openbsd -passes='require<libcall-lowering-info>,stack-protector' -S %s | FileCheck --check-prefix=OPENBSD %s
+
+define void @foo() sspreq {
+; ARM-LABEL: define void @foo(
+; ARM-SAME: ) #[[ATTR0:[0-9]+]] {
+; ARM-NEXT: [[STACKGUARDSLOT:%.*]] = alloca ptr, align 4
+; ARM-NEXT: [[TMP1:%.*]] = call ptr @llvm.stackguard()
+; ARM-NEXT: call void @llvm.stackprotector(ptr [[TMP1]], ptr [[STACKGUARDSLOT]])
+; ARM-NEXT: [[A:%.*]] = alloca i32, align 4
+; ARM-NEXT: call void @capture(ptr [[A]])
+; ARM-NEXT: [[TMP2:%.*]] = call ptr @llvm.stackguard()
+; ARM-NEXT: [[TMP3:%.*]] = load volatile ptr, ptr [[STACKGUARDSLOT]], align 4
+; ARM-NEXT: [[TMP4:%.*]] = icmp eq ptr [[TMP2]], [[TMP3]]
+; ARM-NEXT: br i1 [[TMP4]], label %[[SP_RETURN:.*]], label %[[CALLSTACKCHECKFAILBLK:.*]], !prof [[PROF0:![0-9]+]]
+; ARM: [[SP_RETURN]]:
+; ARM-NEXT: ret void
+; ARM: [[CALLSTACKCHECKFAILBLK]]:
+; ARM-NEXT: call arm_aapcscc void @__stack_chk_fail() #[[ATTR3:[0-9]+]]
+; ARM-NEXT: unreachable
+;
+; OPENBSD-LABEL: define void @foo(
+; OPENBSD-SAME: ) #[[ATTR0:[0-9]+]] {
+; OPENBSD-NEXT: [[STACKGUARDSLOT:%.*]] = alloca ptr, align 8
+; OPENBSD-NEXT: [[STACKGUARD:%.*]] = load volatile ptr, ptr @__guard_local, align 8
+; OPENBSD-NEXT: call void @llvm.stackprotector(ptr [[STACKGUARD]], ptr [[STACKGUARDSLOT]])
+; OPENBSD-NEXT: [[A:%.*]] = alloca i32, align 4
+; OPENBSD-NEXT: call void @capture(ptr [[A]])
+; OPENBSD-NEXT: [[STACKGUARD1:%.*]] = load volatile ptr, ptr @__guard_local, align 8
+; OPENBSD-NEXT: [[TMP1:%.*]] = load volatile ptr, ptr [[STACKGUARDSLOT]], align 8
+; OPENBSD-NEXT: [[TMP2:%.*]] = icmp eq ptr [[STACKGUARD1]], [[TMP1]]
+; OPENBSD-NEXT: br i1 [[TMP2]], label %[[SP_RETURN:.*]], label %[[CALLSTACKCHECKFAILBLK:.*]], !prof [[PROF0:![0-9]+]]
+; OPENBSD: [[SP_RETURN]]:
+; OPENBSD-NEXT: ret void
+; OPENBSD: [[CALLSTACKCHECKFAILBLK]]:
+; OPENBSD-NEXT: call void @__stack_smash_handler(ptr @SSH) #[[ATTR3:[0-9]+]]
+; OPENBSD-NEXT: unreachable
+;
+ %a = alloca i32, align 4
+ call void @capture(ptr %a)
+ ret void
+}
+
+declare void @capture(ptr)
+;.
+; ARM: [[PROF0]] = !{!"branch_weights", i32 2147481600, i32 2048}
+;.
+; OPENBSD: [[PROF0]] = !{!"branch_weights", i32 2147481600, i32 2048}
+;.
``````````
</details>
https://github.com/llvm/llvm-project/pull/241144
More information about the llvm-branch-commits
mailing list