[PATCH] D146339: [StackProtector] attribute __stack_chk_fail as NoReturn

Nick Desaulniers via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 17 16:51:11 PDT 2023


nickdesaulniers created this revision.
nickdesaulniers added reviewers: efriedma, jyknight.
Herald added subscribers: pengfei, hiraditya, krytarowski, arichardson, emaste.
Herald added a project: All.
nickdesaulniers requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

When GCC added support for stack smashing protections, it was defined
that:

> This hook returns a CALL_EXPR that alerts the runtime that the stack
> protect guard variable has been modified. This expression should
> involve a call to a noreturn function.
> The default version of this hook invokes a function called
> ‘__stack_chk_fail’, taking no arguments.

Do so as well for __stack_smash_handler for OpenBSD.

Every libc implementation I could find has __stack_chk_fail marked
noreturn, or the implementation calls abort, exit, or panic (which
themselves are noreturn).

Glibc: https://sourceware.org/git/?p=glibc.git;a=blob;f=debug/stack_chk_fail.c
Musl: https://git.musl-libc.org/cgit/musl/tree/src/env/__stack_chk_fail.c
Bionic: https://android.googlesource.com/platform/bionic/+/refs/heads/master/libc/bionic/__stack_chk_fail.cpp
FreeBSD: https://cgit.freebsd.org/src/tree/lib/libc/secure/stack_protector.c
OpenBSD: https://github.com/openbsd/src/blob/master/lib/libc/sys/stack_protector.c
NetBSD: https://github.com/NetBSD/src/blob/trunk/lib/libc/misc/stack_protector.c
Linux Kernel: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/kernel/panic.c
Apple: https://opensource.apple.com/source/Libc/Libc-1439.40.11/sys/OpenBSD/stack_protector.c.auto.html

Link: https://gcc.gnu.org/onlinedocs/gccint/Stack-Smashing-Protection.html#Stack-Smashing-Protection

This will later help us diagnose functions that fall through to other
functions vs end in calls to functions that are noreturn.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D146339

Files:
  llvm/lib/CodeGen/StackProtector.cpp
  llvm/test/CodeGen/X86/2009-04-14-IllegalRegs.ll
  llvm/test/CodeGen/X86/stack-protector-weight.ll


Index: llvm/test/CodeGen/X86/stack-protector-weight.ll
===================================================================
--- llvm/test/CodeGen/X86/stack-protector-weight.ll
+++ llvm/test/CodeGen/X86/stack-protector-weight.ll
@@ -10,7 +10,7 @@
 ; DARWIN-SELDAG: bb.[[SUCCESS]]{{[0-9a-zA-Z_.]+}}:
 
 ; DARWIN-IR: # Machine code for function test_branch_weights:
-; DARWIN-IR: successors: %bb.[[SUCCESS:[0-9]+]](0x7fffffff), %bb.[[FAILURE:[0-9]+]]
+; DARWIN-IR: successors: %bb.[[SUCCESS:[0-9]+]](0x7ffff800), %bb.[[FAILURE:[0-9]+]]
 ; DARWIN-IR: bb.[[SUCCESS]]{{[0-9a-zA-Z_.]+}}:
 ; DARWIN-IR: bb.[[FAILURE]]{{[0-9a-zA-Z_.]+}}:
 ; DARWIN-IR: CALL64pcrel32 @__stack_chk_fail
Index: llvm/test/CodeGen/X86/2009-04-14-IllegalRegs.ll
===================================================================
--- llvm/test/CodeGen/X86/2009-04-14-IllegalRegs.ll
+++ llvm/test/CodeGen/X86/2009-04-14-IllegalRegs.ll
@@ -53,7 +53,6 @@
 ; CHECK-NEXT:    retl
 ; CHECK-NEXT:  LBB0_3: ## %CallStackCheckFailBlk
 ; CHECK-NEXT:    calll ___stack_chk_fail
-; CHECK-NEXT:    ud2
 entry:
 	%retval = alloca i32		; <ptr> [#uses=2]
 	%xxx = alloca %struct.X		; <ptr> [#uses=6]
Index: llvm/lib/CodeGen/StackProtector.cpp
===================================================================
--- llvm/lib/CodeGen/StackProtector.cpp
+++ llvm/lib/CodeGen/StackProtector.cpp
@@ -598,11 +598,13 @@
     FunctionCallee StackChkFail = M->getOrInsertFunction(
         "__stack_smash_handler", Type::getVoidTy(Context),
         Type::getInt8PtrTy(Context));
+    cast<Function>(StackChkFail.getCallee())->addFnAttr(Attribute::NoReturn);
 
     B.CreateCall(StackChkFail, B.CreateGlobalStringPtr(F->getName(), "SSH"));
   } else {
     FunctionCallee StackChkFail =
         M->getOrInsertFunction("__stack_chk_fail", Type::getVoidTy(Context));
+    cast<Function>(StackChkFail.getCallee())->addFnAttr(Attribute::NoReturn);
 
     B.CreateCall(StackChkFail, {});
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D146339.506228.patch
Type: text/x-patch
Size: 1931 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230317/af6cafbd/attachment.bin>


More information about the llvm-commits mailing list