Skip to content

[llvm][StackProtector] Add noreturn to __stack_chk_fail call #143976

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
merged 1 commit into from
Jun 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions llvm/lib/CodeGen/StackProtector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -725,8 +725,8 @@ BasicBlock *CreateFailBB(Function *F, const Triple &Trip) {
StackChkFail =
M->getOrInsertFunction("__stack_chk_fail", Type::getVoidTy(Context));
}
cast<Function>(StackChkFail.getCallee())->addFnAttr(Attribute::NoReturn);
B.CreateCall(StackChkFail, Args);
CallInst *Call = B.CreateCall(StackChkFail, Args);
Call->addFnAttr(Attribute::NoReturn);
B.CreateUnreachable();
return FailBB;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
;; This is a minimal reproducer that caused StackProtector to crash with a bad cast when
;; CrossDSOCFI is used. This test just needs to not crash.
; RUN: opt -mtriple=x86_64-pc-linux-gnu %s -passes=lowertypetests,cross-dso-cfi,stack-protector

define hidden void @__stack_chk_fail() !type !1{
unreachable
}

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
}

define void @func(ptr %0) {
entry:
%1 = call i1 @llvm.type.test(ptr %0, metadata !"typeid")
br i1 %1, label %cont, label %trap

trap: ; preds = %entry
call void @llvm.trap()
unreachable

cont: ; preds = %entry
call void %0()
ret void
}

!llvm.module.flags = !{!0}
!0 = !{i32 4, !"Cross-DSO CFI", i32 1}
!1 = !{i64 0, !"typeid"}
21 changes: 21 additions & 0 deletions llvm/test/Transforms/StackProtector/stack-chk-fail-alias.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
;; __stack_chk_fail should have the noreturn attr even if it is an alias
; RUN: opt -mtriple=x86_64-pc-linux-gnu %s -passes=stack-protector -S | FileCheck %s

define hidden void @__stack_chk_fail_impl() {
unreachable
}

@__stack_chk_fail = hidden alias void (), ptr @__stack_chk_fail_impl

; CHECK-LABEL: @store_captures(
; CHECK: CallStackCheckFailBlk:
; CHECK-NEXT: call void @__stack_chk_fail() [[ATTRS:#.*]]
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
}

; CHECK: attributes [[ATTRS]] = { noreturn }
Loading