[llvm] [llvm][StackProtector] Add noreturn to __stack_chk_fail aliassee (PR #143976)

via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 12 14:31:37 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: None (PiJoules)

<details>
<summary>Changes</summary>

It's possible for __stack_chk_fail to be an alias when using CrossDSOCFI since it will make a jump table entry for this function and replace it with an alias. StackProtector can crash since it always expects this to be a regular function. I think it should be safe to continue treating this as an alias since we only call into it, and we can apply the noreturn attr to the underlying function if it is an alias.

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


2 Files Affected:

- (modified) llvm/lib/CodeGen/StackProtector.cpp (+3-1) 
- (added) llvm/test/Transforms/StackProtector/stack-chk-fail-alias.ll (+17) 


``````````diff
diff --git a/llvm/lib/CodeGen/StackProtector.cpp b/llvm/lib/CodeGen/StackProtector.cpp
index 5f866eea7d4e7..0e5c421d2fa5e 100644
--- a/llvm/lib/CodeGen/StackProtector.cpp
+++ b/llvm/lib/CodeGen/StackProtector.cpp
@@ -725,7 +725,9 @@ BasicBlock *CreateFailBB(Function *F, const Triple &Trip) {
     StackChkFail =
         M->getOrInsertFunction("__stack_chk_fail", Type::getVoidTy(Context));
   }
-  cast<Function>(StackChkFail.getCallee())->addFnAttr(Attribute::NoReturn);
+  cast<Function>(
+      cast<GlobalValue>(StackChkFail.getCallee())->getAliaseeObject())
+      ->addFnAttr(Attribute::NoReturn);
   B.CreateCall(StackChkFail, Args);
   B.CreateUnreachable();
   return FailBB;
diff --git a/llvm/test/Transforms/StackProtector/stack-chk-fail-alias.ll b/llvm/test/Transforms/StackProtector/stack-chk-fail-alias.ll
new file mode 100644
index 0000000000000..9314800cf0441
--- /dev/null
+++ b/llvm/test/Transforms/StackProtector/stack-chk-fail-alias.ll
@@ -0,0 +1,17 @@
+;; __stack_chk_fail should have the noreturn attr even if it is an alias
+; RUN: opt -triple=x86_64-pc-linux-gnu %s -passes=stack-protector -S | FileCheck %s
+
+define hidden void @__stack_chk_fail_impl() {
+  unreachable
+}
+
+ at __stack_chk_fail = hidden alias void (), ptr @__stack_chk_fail_impl
+
+; CHECK-LABEL: @store_captures(
+define void @store_captures() sspstrong {
+entry:
+  %a = alloca i32, align 4
+  %j = alloca ptr, align 8
+  store ptr %a, ptr %j, align 8
+  ret void
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/143976


More information about the llvm-commits mailing list