Skip to content

SafeStack: Emit __safestack_pointer_address call through RuntimeLibcalls #147916

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

Conversation

arsenm
Copy link
Contributor

@arsenm arsenm commented Jul 10, 2025

Stop using hardcoded function named and check availability. This only fixes
the forced usage via command line in the pass itself; the implementations
inside of TargetLoweringBase hide additional call emission.

@llvmbot
Copy link
Member

llvmbot commented Jul 10, 2025

@llvm/pr-subscribers-llvm-transforms

@llvm/pr-subscribers-backend-nvptx

Author: Matt Arsenault (arsenm)

Changes

Stop using hardcoded function named and check availability. This only fixes
the forced usage via command line in the pass itself; the implementations
inside of TargetLoweringBase hide additional call emission.


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

4 Files Affected:

  • (modified) llvm/include/llvm/IR/RuntimeLibcalls.td (+6)
  • (modified) llvm/lib/CodeGen/SafeStack.cpp (+9-1)
  • (modified) llvm/lib/IR/RuntimeLibcalls.cpp (+5)
  • (added) llvm/test/Transforms/SafeStack/NVPTX/safestack-pointer-address-libcall-error.ll (+12)
diff --git a/llvm/include/llvm/IR/RuntimeLibcalls.td b/llvm/include/llvm/IR/RuntimeLibcalls.td
index 29de1206a8974..2c26c05402e4e 100644
--- a/llvm/include/llvm/IR/RuntimeLibcalls.td
+++ b/llvm/include/llvm/IR/RuntimeLibcalls.td
@@ -339,6 +339,9 @@ defset list<RuntimeLibcall> LibCalls__OutOfLineAtomic = {
 def STACKPROTECTOR_CHECK_FAIL : RuntimeLibcall;
 def STACK_SMASH_HANDLER : RuntimeLibcall;
 
+// Safe stack
+def SAFESTACK_POINTER_ADDRESS : RuntimeLibcall;
+
 // Deoptimization
 def DEOPTIMIZE : RuntimeLibcall;
 
@@ -656,6 +659,9 @@ foreach lc = LibCalls__atomic in {
 // Stack Protector Fail
 def __stack_chk_fail : RuntimeLibcallImpl<STACKPROTECTOR_CHECK_FAIL>;
 
+// Safe stack.
+def __safestack_pointer_address : RuntimeLibcallImpl<SAFESTACK_POINTER_ADDRESS>;
+
 // Deoptimization
 def __llvm_deoptimize : RuntimeLibcallImpl<DEOPTIMIZE>;
 
diff --git a/llvm/lib/CodeGen/SafeStack.cpp b/llvm/lib/CodeGen/SafeStack.cpp
index a6c1a76f53f39..996207034d076 100644
--- a/llvm/lib/CodeGen/SafeStack.cpp
+++ b/llvm/lib/CodeGen/SafeStack.cpp
@@ -799,8 +799,16 @@ bool SafeStack::run() {
     IRB.SetCurrentDebugLocation(
         DILocation::get(SP->getContext(), SP->getScopeLine(), 0, SP));
   if (SafeStackUsePointerAddress) {
+    const char *SafestackPointerAddressName =
+        TL.getLibcallName(RTLIB::SAFESTACK_POINTER_ADDRESS);
+    if (!SafestackPointerAddressName) {
+      F.getContext().emitError(
+          "no libcall available for safestack pointer address");
+      return false;
+    }
+
     FunctionCallee Fn = F.getParent()->getOrInsertFunction(
-        "__safestack_pointer_address", IRB.getPtrTy(0));
+        SafestackPointerAddressName, IRB.getPtrTy(0));
     UnsafeStackPtr = IRB.CreateCall(Fn);
   } else {
     UnsafeStackPtr = TL.getSafeStackPointerLocation(IRB);
diff --git a/llvm/lib/IR/RuntimeLibcalls.cpp b/llvm/lib/IR/RuntimeLibcalls.cpp
index f9bd9b6029234..21e8fdb9f7feb 100644
--- a/llvm/lib/IR/RuntimeLibcalls.cpp
+++ b/llvm/lib/IR/RuntimeLibcalls.cpp
@@ -293,6 +293,11 @@ void RuntimeLibcallsInfo::initLibcalls(const Triple &TT,
     setLibcallImpl(RTLIB::POWI_F64, RTLIB::Unsupported);
   }
 
+  if (TT.isAndroid()) {
+    setLibcallImpl(RTLIB::SAFESTACK_POINTER_ADDRESS,
+                   RTLIB::__safestack_pointer_address);
+  }
+
   // Setup Windows compiler runtime calls.
   if (TT.getArch() == Triple::x86 &&
       (TT.isWindowsMSVCEnvironment() || TT.isWindowsItaniumEnvironment())) {
diff --git a/llvm/test/Transforms/SafeStack/NVPTX/safestack-pointer-address-libcall-error.ll b/llvm/test/Transforms/SafeStack/NVPTX/safestack-pointer-address-libcall-error.ll
new file mode 100644
index 0000000000000..9bf84585e5468
--- /dev/null
+++ b/llvm/test/Transforms/SafeStack/NVPTX/safestack-pointer-address-libcall-error.ll
@@ -0,0 +1,12 @@
+; RUN: not opt -disable-output -mtriple=nvptx64-- -safestack-use-pointer-address -mcpu=sm_90 -passes=safe-stack %s 2>&1 | FileCheck %s
+
+; CHECK: error: no libcall available for safestack pointer address
+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 }

@llvmbot
Copy link
Member

llvmbot commented Jul 10, 2025

@llvm/pr-subscribers-llvm-ir

Author: Matt Arsenault (arsenm)

Changes

Stop using hardcoded function named and check availability. This only fixes
the forced usage via command line in the pass itself; the implementations
inside of TargetLoweringBase hide additional call emission.


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

4 Files Affected:

  • (modified) llvm/include/llvm/IR/RuntimeLibcalls.td (+6)
  • (modified) llvm/lib/CodeGen/SafeStack.cpp (+9-1)
  • (modified) llvm/lib/IR/RuntimeLibcalls.cpp (+5)
  • (added) llvm/test/Transforms/SafeStack/NVPTX/safestack-pointer-address-libcall-error.ll (+12)
diff --git a/llvm/include/llvm/IR/RuntimeLibcalls.td b/llvm/include/llvm/IR/RuntimeLibcalls.td
index 29de1206a8974..2c26c05402e4e 100644
--- a/llvm/include/llvm/IR/RuntimeLibcalls.td
+++ b/llvm/include/llvm/IR/RuntimeLibcalls.td
@@ -339,6 +339,9 @@ defset list<RuntimeLibcall> LibCalls__OutOfLineAtomic = {
 def STACKPROTECTOR_CHECK_FAIL : RuntimeLibcall;
 def STACK_SMASH_HANDLER : RuntimeLibcall;
 
+// Safe stack
+def SAFESTACK_POINTER_ADDRESS : RuntimeLibcall;
+
 // Deoptimization
 def DEOPTIMIZE : RuntimeLibcall;
 
@@ -656,6 +659,9 @@ foreach lc = LibCalls__atomic in {
 // Stack Protector Fail
 def __stack_chk_fail : RuntimeLibcallImpl<STACKPROTECTOR_CHECK_FAIL>;
 
+// Safe stack.
+def __safestack_pointer_address : RuntimeLibcallImpl<SAFESTACK_POINTER_ADDRESS>;
+
 // Deoptimization
 def __llvm_deoptimize : RuntimeLibcallImpl<DEOPTIMIZE>;
 
diff --git a/llvm/lib/CodeGen/SafeStack.cpp b/llvm/lib/CodeGen/SafeStack.cpp
index a6c1a76f53f39..996207034d076 100644
--- a/llvm/lib/CodeGen/SafeStack.cpp
+++ b/llvm/lib/CodeGen/SafeStack.cpp
@@ -799,8 +799,16 @@ bool SafeStack::run() {
     IRB.SetCurrentDebugLocation(
         DILocation::get(SP->getContext(), SP->getScopeLine(), 0, SP));
   if (SafeStackUsePointerAddress) {
+    const char *SafestackPointerAddressName =
+        TL.getLibcallName(RTLIB::SAFESTACK_POINTER_ADDRESS);
+    if (!SafestackPointerAddressName) {
+      F.getContext().emitError(
+          "no libcall available for safestack pointer address");
+      return false;
+    }
+
     FunctionCallee Fn = F.getParent()->getOrInsertFunction(
-        "__safestack_pointer_address", IRB.getPtrTy(0));
+        SafestackPointerAddressName, IRB.getPtrTy(0));
     UnsafeStackPtr = IRB.CreateCall(Fn);
   } else {
     UnsafeStackPtr = TL.getSafeStackPointerLocation(IRB);
diff --git a/llvm/lib/IR/RuntimeLibcalls.cpp b/llvm/lib/IR/RuntimeLibcalls.cpp
index f9bd9b6029234..21e8fdb9f7feb 100644
--- a/llvm/lib/IR/RuntimeLibcalls.cpp
+++ b/llvm/lib/IR/RuntimeLibcalls.cpp
@@ -293,6 +293,11 @@ void RuntimeLibcallsInfo::initLibcalls(const Triple &TT,
     setLibcallImpl(RTLIB::POWI_F64, RTLIB::Unsupported);
   }
 
+  if (TT.isAndroid()) {
+    setLibcallImpl(RTLIB::SAFESTACK_POINTER_ADDRESS,
+                   RTLIB::__safestack_pointer_address);
+  }
+
   // Setup Windows compiler runtime calls.
   if (TT.getArch() == Triple::x86 &&
       (TT.isWindowsMSVCEnvironment() || TT.isWindowsItaniumEnvironment())) {
diff --git a/llvm/test/Transforms/SafeStack/NVPTX/safestack-pointer-address-libcall-error.ll b/llvm/test/Transforms/SafeStack/NVPTX/safestack-pointer-address-libcall-error.ll
new file mode 100644
index 0000000000000..9bf84585e5468
--- /dev/null
+++ b/llvm/test/Transforms/SafeStack/NVPTX/safestack-pointer-address-libcall-error.ll
@@ -0,0 +1,12 @@
+; RUN: not opt -disable-output -mtriple=nvptx64-- -safestack-use-pointer-address -mcpu=sm_90 -passes=safe-stack %s 2>&1 | FileCheck %s
+
+; CHECK: error: no libcall available for safestack pointer address
+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-__safestack_pointer_address branch from 28d4fc5 to fcf040c Compare July 15, 2025 08:04
@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
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:44 AM UTC: Graphite rebased this pull request as part of a merge.
  • Jul 15, 9:47 AM UTC: Graphite rebased this pull request as part of a merge.
  • Jul 15, 9:50 AM UTC: Graphite rebased this pull request as part of a merge.
  • Jul 15, 9:53 AM UTC: Graphite rebased this pull request as part of a merge.
  • Jul 15, 9:58 AM UTC: Graphite rebased this pull request as part of a merge.
  • Jul 15, 10:01 AM UTC: @arsenm merged this pull request with Graphite.

@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
Base automatically changed from users/arsenm/safestack/use-runtime-libcalls-__stack_chk_fail to main July 15, 2025 09:43
@arsenm arsenm force-pushed the users/arsenm/safestack/use-runtime-libcalls-__safestack_pointer_address branch 4 times, most recently from 4510a20 to 3ec4dc9 Compare July 15, 2025 09:53
Stop using hardcoded function named and check availability. This only fixes
the forced usage via command line in the pass itself; the implementations
inside of TargetLoweringBase hide additional call emission.
@arsenm arsenm force-pushed the users/arsenm/safestack/use-runtime-libcalls-__safestack_pointer_address branch from 3ec4dc9 to b0f33a2 Compare July 15, 2025 09:57
@arsenm arsenm merged commit 9250139 into main Jul 15, 2025
7 of 9 checks passed
@arsenm arsenm deleted the users/arsenm/safestack/use-runtime-libcalls-__safestack_pointer_address branch July 15, 2025 10:01
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.

3 participants