Skip to content

SafeStack: Check if __safestack_pointer_address is available #147917

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

Start using RuntimeLibcalls in the base implementation of
getSafeStackPointerLocation instead of hardcoding the function
names.

@llvmbot
Copy link
Member

llvmbot commented Jul 10, 2025

@llvm/pr-subscribers-llvm-transforms

Author: Matt Arsenault (arsenm)

Changes

Start using RuntimeLibcalls in the base implementation of
getSafeStackPointerLocation instead of hardcoding the function
names.


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

2 Files Affected:

  • (modified) llvm/lib/CodeGen/TargetLoweringBase.cpp (+14-3)
  • (modified) llvm/test/Transforms/SafeStack/NVPTX/safestack-pointer-address-libcall-error.ll (+4-2)
diff --git a/llvm/lib/CodeGen/TargetLoweringBase.cpp b/llvm/lib/CodeGen/TargetLoweringBase.cpp
index 0a077b7b61437..7e730621e4203 100644
--- a/llvm/lib/CodeGen/TargetLoweringBase.cpp
+++ b/llvm/lib/CodeGen/TargetLoweringBase.cpp
@@ -1965,15 +1965,26 @@ TargetLoweringBase::getDefaultSafeStackPointerLocation(IRBuilderBase &IRB,
 
 Value *
 TargetLoweringBase::getSafeStackPointerLocation(IRBuilderBase &IRB) const {
+  // FIXME: Can this triple check be replaced with SAFESTACK_POINTER_ADDRESS
+  // being available?
   if (!TM.getTargetTriple().isAndroid())
     return getDefaultSafeStackPointerLocation(IRB, true);
 
-  // Android provides a libc function to retrieve the address of the current
-  // thread's unsafe stack pointer.
   Module *M = IRB.GetInsertBlock()->getParent()->getParent();
   auto *PtrTy = PointerType::getUnqual(M->getContext());
+
+  const char *SafestackPointerAddressName =
+      getLibcallName(RTLIB::SAFESTACK_POINTER_ADDRESS);
+  if (!SafestackPointerAddressName) {
+    M->getContext().emitError(
+        "no libcall available for safestack pointer address");
+    return PoisonValue::get(PtrTy);
+  }
+
+  // Android provides a libc function to retrieve the address of the current
+  // thread's unsafe stack pointer.
   FunctionCallee Fn =
-      M->getOrInsertFunction("__safestack_pointer_address", PtrTy);
+      M->getOrInsertFunction(SafestackPointerAddressName, PtrTy);
   return IRB.CreateCall(Fn);
 }
 
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
index 9bf84585e5468..d3ea974fca0cf 100644
--- a/llvm/test/Transforms/SafeStack/NVPTX/safestack-pointer-address-libcall-error.ll
+++ b/llvm/test/Transforms/SafeStack/NVPTX/safestack-pointer-address-libcall-error.ll
@@ -1,6 +1,8 @@
-; RUN: not opt -disable-output -mtriple=nvptx64-- -safestack-use-pointer-address -mcpu=sm_90 -passes=safe-stack %s 2>&1 | FileCheck %s
+; RUN: not opt -disable-output -mtriple=nvptx64-- -safestack-use-pointer-address -mcpu=sm_90 -passes=safe-stack %s 2>&1 | FileCheck -check-prefix=ERR1 %s
+; RUN: not opt -disable-output -mtriple=nvptx64-unknown-android -mcpu=sm_90 -passes=safe-stack %s 2>&1 | FileCheck -check-prefix=ERR2 %s
 
-; CHECK: error: no libcall available for safestack pointer address
+; ERR1: error: no libcall available for safestack pointer address
+; ERR2: 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)

@llvmbot
Copy link
Member

llvmbot commented Jul 10, 2025

@llvm/pr-subscribers-backend-nvptx

Author: Matt Arsenault (arsenm)

Changes

Start using RuntimeLibcalls in the base implementation of
getSafeStackPointerLocation instead of hardcoding the function
names.


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

2 Files Affected:

  • (modified) llvm/lib/CodeGen/TargetLoweringBase.cpp (+14-3)
  • (modified) llvm/test/Transforms/SafeStack/NVPTX/safestack-pointer-address-libcall-error.ll (+4-2)
diff --git a/llvm/lib/CodeGen/TargetLoweringBase.cpp b/llvm/lib/CodeGen/TargetLoweringBase.cpp
index 0a077b7b61437..7e730621e4203 100644
--- a/llvm/lib/CodeGen/TargetLoweringBase.cpp
+++ b/llvm/lib/CodeGen/TargetLoweringBase.cpp
@@ -1965,15 +1965,26 @@ TargetLoweringBase::getDefaultSafeStackPointerLocation(IRBuilderBase &IRB,
 
 Value *
 TargetLoweringBase::getSafeStackPointerLocation(IRBuilderBase &IRB) const {
+  // FIXME: Can this triple check be replaced with SAFESTACK_POINTER_ADDRESS
+  // being available?
   if (!TM.getTargetTriple().isAndroid())
     return getDefaultSafeStackPointerLocation(IRB, true);
 
-  // Android provides a libc function to retrieve the address of the current
-  // thread's unsafe stack pointer.
   Module *M = IRB.GetInsertBlock()->getParent()->getParent();
   auto *PtrTy = PointerType::getUnqual(M->getContext());
+
+  const char *SafestackPointerAddressName =
+      getLibcallName(RTLIB::SAFESTACK_POINTER_ADDRESS);
+  if (!SafestackPointerAddressName) {
+    M->getContext().emitError(
+        "no libcall available for safestack pointer address");
+    return PoisonValue::get(PtrTy);
+  }
+
+  // Android provides a libc function to retrieve the address of the current
+  // thread's unsafe stack pointer.
   FunctionCallee Fn =
-      M->getOrInsertFunction("__safestack_pointer_address", PtrTy);
+      M->getOrInsertFunction(SafestackPointerAddressName, PtrTy);
   return IRB.CreateCall(Fn);
 }
 
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
index 9bf84585e5468..d3ea974fca0cf 100644
--- a/llvm/test/Transforms/SafeStack/NVPTX/safestack-pointer-address-libcall-error.ll
+++ b/llvm/test/Transforms/SafeStack/NVPTX/safestack-pointer-address-libcall-error.ll
@@ -1,6 +1,8 @@
-; RUN: not opt -disable-output -mtriple=nvptx64-- -safestack-use-pointer-address -mcpu=sm_90 -passes=safe-stack %s 2>&1 | FileCheck %s
+; RUN: not opt -disable-output -mtriple=nvptx64-- -safestack-use-pointer-address -mcpu=sm_90 -passes=safe-stack %s 2>&1 | FileCheck -check-prefix=ERR1 %s
+; RUN: not opt -disable-output -mtriple=nvptx64-unknown-android -mcpu=sm_90 -passes=safe-stack %s 2>&1 | FileCheck -check-prefix=ERR2 %s
 
-; CHECK: error: no libcall available for safestack pointer address
+; ERR1: error: no libcall available for safestack pointer address
+; ERR2: 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)

@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-__safestack_pointer_address-tli-base branch from 389ea4c to 5dd0f91 Compare July 15, 2025 08:04
@arsenm arsenm force-pushed the users/arsenm/safestack/use-runtime-libcalls-__safestack_pointer_address branch 5 times, most recently from 3ec4dc9 to b0f33a2 Compare July 15, 2025 09:57
Base automatically changed from users/arsenm/safestack/use-runtime-libcalls-__safestack_pointer_address to main July 15, 2025 10:01
@arsenm arsenm force-pushed the users/arsenm/safestack/use-runtime-libcalls-__safestack_pointer_address-tli-base branch from 5dd0f91 to cfb6e21 Compare July 15, 2025 10:02
Start using RuntimeLibcalls in the base implementation of
getSafeStackPointerLocation instead of hardcoding the function
names.
@arsenm arsenm force-pushed the users/arsenm/safestack/use-runtime-libcalls-__safestack_pointer_address-tli-base branch from cfb6e21 to 72eb929 Compare July 15, 2025 12:54
Copy link
Contributor Author

arsenm commented Jul 15, 2025

Merge activity

  • Jul 15, 2:26 PM UTC: A user started a stack merge that includes this pull request via Graphite.
  • Jul 15, 2:26 PM UTC: @arsenm merged this pull request with Graphite.

@arsenm arsenm merged commit f4a394f into main Jul 15, 2025
9 checks passed
@arsenm arsenm deleted the users/arsenm/safestack/use-runtime-libcalls-__safestack_pointer_address-tli-base branch July 15, 2025 14:26
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