Skip to content

SafeStack: Emit call to __stack_chk_fail through RuntimeLibcalls #147915

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

Conversation

arsenm
Copy link
Contributor

@arsenm arsenm commented Jul 10, 2025

Avoid hardcoding the function name, and query if it's really
supported or not.

@llvmbot
Copy link
Member

llvmbot commented Jul 10, 2025

@llvm/pr-subscribers-llvm-transforms

@llvm/pr-subscribers-backend-nvptx

Author: Matt Arsenault (arsenm)

Changes

Avoid hardcoding the function name, and query if it's really
supported or not.


Full diff: https://github.com/llvm/llvm-project/pull/147915.diff

3 Files Affected:

  • (modified) llvm/lib/CodeGen/SafeStack.cpp (+9-1)
  • (added) llvm/test/Transforms/SafeStack/NVPTX/lit.local.cfg (+2)
  • (added) llvm/test/Transforms/SafeStack/NVPTX/safestack-libcall-error.ll (+12)
diff --git a/llvm/lib/CodeGen/SafeStack.cpp b/llvm/lib/CodeGen/SafeStack.cpp
index da229f86f24ce..a6c1a76f53f39 100644
--- a/llvm/lib/CodeGen/SafeStack.cpp
+++ b/llvm/lib/CodeGen/SafeStack.cpp
@@ -475,8 +475,16 @@ void SafeStack::checkStackGuard(IRBuilder<> &IRB, Function &F, Instruction &RI,
       SplitBlockAndInsertIfThen(Cmp, &RI, /* Unreachable */ true, Weights, DTU);
   IRBuilder<> IRBFail(CheckTerm);
   // FIXME: respect -fsanitize-trap / -ftrap-function here?
+  const char *StackChkFailName =
+      TL.getLibcallName(RTLIB::STACKPROTECTOR_CHECK_FAIL);
+  if (!StackChkFailName) {
+    F.getContext().emitError(
+        "no libcall available for stackprotector check fail");
+    return;
+  }
+
   FunctionCallee StackChkFail =
-      F.getParent()->getOrInsertFunction("__stack_chk_fail", IRB.getVoidTy());
+      F.getParent()->getOrInsertFunction(StackChkFailName, IRB.getVoidTy());
   IRBFail.CreateCall(StackChkFail, {});
 }
 
diff --git a/llvm/test/Transforms/SafeStack/NVPTX/lit.local.cfg b/llvm/test/Transforms/SafeStack/NVPTX/lit.local.cfg
new file mode 100644
index 0000000000000..0d37b86e1c8e6
--- /dev/null
+++ b/llvm/test/Transforms/SafeStack/NVPTX/lit.local.cfg
@@ -0,0 +1,2 @@
+if not "NVPTX" in config.root.targets:
+    config.unsupported = True
diff --git a/llvm/test/Transforms/SafeStack/NVPTX/safestack-libcall-error.ll b/llvm/test/Transforms/SafeStack/NVPTX/safestack-libcall-error.ll
new file mode 100644
index 0000000000000..a17a9dd867e3a
--- /dev/null
+++ b/llvm/test/Transforms/SafeStack/NVPTX/safestack-libcall-error.ll
@@ -0,0 +1,12 @@
+; RUN: not opt -disable-output -mtriple=nvptx64-- -mcpu=sm_90 -passes=safe-stack %s 2>&1 | FileCheck %s
+
+; CHECK: error: no libcall available for stackprotector check fail
+define void @foo(i32 %t) #0 {
+  %vla = alloca i32, i32 %t, align 4
+  call void @baz(ptr %vla)
+  ret void
+}
+
+declare void @baz(ptr)
+
+attributes #0 = { nounwind safestack sspstrong }

@arsenm arsenm force-pushed the users/arsenm/safestack/use-runtime-libcalls-__stack_chk_fail branch from cb3cf0b to a74db10 Compare July 15, 2025 08:04
@arsenm arsenm force-pushed the users/arsenm/runtime-libcalls/add-exception-libcall-entries branch from ed4c2aa to 562bee8 Compare July 15, 2025 08:04
Copy link
Collaborator

@RKSimon RKSimon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor Author

arsenm commented Jul 15, 2025

Merge activity

  • Jul 15, 9:34 AM UTC: A user started a stack merge that includes this pull request via Graphite.
  • Jul 15, 9:41 AM UTC: Graphite rebased this pull request as part of a merge.
  • Jul 15, 9:43 AM UTC: @arsenm merged this pull request with Graphite.

@arsenm arsenm force-pushed the users/arsenm/runtime-libcalls/add-exception-libcall-entries branch from 562bee8 to f467d13 Compare July 15, 2025 09:38
Base automatically changed from users/arsenm/runtime-libcalls/add-exception-libcall-entries to main July 15, 2025 09:40
Avoid hardcoding the function name, and query if it's really
supported or not.
@arsenm arsenm force-pushed the users/arsenm/safestack/use-runtime-libcalls-__stack_chk_fail branch from a74db10 to 3c4dcd4 Compare July 15, 2025 09:41
@arsenm arsenm merged commit d7dc536 into main Jul 15, 2025
5 of 9 checks passed
@arsenm arsenm deleted the users/arsenm/safestack/use-runtime-libcalls-__stack_chk_fail branch July 15, 2025 09:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants