[libc-commits] [libc] [libc] __stack_chk_fail post submit test failures (PR #75962)

Nick Desaulniers via libc-commits libc-commits at lists.llvm.org
Tue Dec 19 11:58:59 PST 2023


https://github.com/nickdesaulniers updated https://github.com/llvm/llvm-project/pull/75962

>From 1e1e9cbd367a76e14059e8b0c1003bb2f409060f Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Tue, 19 Dec 2023 11:11:20 -0800
Subject: [PATCH 1/5] [libc] fix segfault in stack_chk_guard_test on arm

Use a size smaller than the smallest supported page size so that we don't
clobber over any guard pages, which may result in a segfault before
__stack_chk_fail can be called.
---
 libc/test/src/compiler/stack_chk_guard_test.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/test/src/compiler/stack_chk_guard_test.cpp b/libc/test/src/compiler/stack_chk_guard_test.cpp
index 1de2d1b7357f70..df15962acd9678 100644
--- a/libc/test/src/compiler/stack_chk_guard_test.cpp
+++ b/libc/test/src/compiler/stack_chk_guard_test.cpp
@@ -20,7 +20,7 @@ TEST(LlvmLibcStackChkFail, Smash) {
   EXPECT_DEATH(
       [] {
         int arr[20];
-        LIBC_NAMESPACE::memset(arr, 0xAA, 9001);
+        LIBC_NAMESPACE::memset(arr, 0xAA, 2001);
       },
       WITH_SIGNAL(SIGABRT));
 }

>From 5af362aec13c49a4678d039dbc19ace74b97ab59 Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Tue, 19 Dec 2023 11:16:46 -0800
Subject: [PATCH 2/5] fix stack_smashing_test

---
 libc/test/integration/src/unistd/CMakeLists.txt | 1 +
 1 file changed, 1 insertion(+)

diff --git a/libc/test/integration/src/unistd/CMakeLists.txt b/libc/test/integration/src/unistd/CMakeLists.txt
index 10aac212af355e..3f18231209512a 100644
--- a/libc/test/integration/src/unistd/CMakeLists.txt
+++ b/libc/test/integration/src/unistd/CMakeLists.txt
@@ -45,6 +45,7 @@ if((${LIBC_TARGET_OS} STREQUAL "linux") AND (${LIBC_TARGET_ARCHITECTURE_IS_X86})
       libc.include.signal
       libc.include.sys_wait
       libc.include.unistd
+      libc.src.compiler.__stack_chk_fail
       libc.src.pthread.pthread_atfork
       libc.src.signal.raise
       libc.src.sys.wait.wait

>From d29009c3a74159af6f4bb2bc18d8ec8cdf9831fb Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Tue, 19 Dec 2023 11:31:28 -0800
Subject: [PATCH 3/5] move __stack_chk_fail out of libc namespace

---
 libc/src/compiler/__stack_chk_fail.h            | 6 ++----
 libc/src/compiler/generic/__stack_chk_fail.cpp  | 6 +++---
 libc/test/src/compiler/stack_chk_guard_test.cpp | 2 +-
 3 files changed, 6 insertions(+), 8 deletions(-)

diff --git a/libc/src/compiler/__stack_chk_fail.h b/libc/src/compiler/__stack_chk_fail.h
index 2e3d849ff8c67c..0bd7e2eac4044f 100644
--- a/libc/src/compiler/__stack_chk_fail.h
+++ b/libc/src/compiler/__stack_chk_fail.h
@@ -9,10 +9,8 @@
 #ifndef LLVM_LIBC_SRC_COMPILER___STACK_CHK_FAIL_H
 #define LLVM_LIBC_SRC_COMPILER___STACK_CHK_FAIL_H
 
-namespace LIBC_NAMESPACE {
-
+extern "C" {
 [[noreturn]] void __stack_chk_fail();
-
-} // namespace LIBC_NAMESPACE
+} // extern "C"
 
 #endif // LLVM_LIBC_SRC_COMPILER___STACK_CHK_FAIL_H
diff --git a/libc/src/compiler/generic/__stack_chk_fail.cpp b/libc/src/compiler/generic/__stack_chk_fail.cpp
index 0ca02071f2a997..639204d29590ab 100644
--- a/libc/src/compiler/generic/__stack_chk_fail.cpp
+++ b/libc/src/compiler/generic/__stack_chk_fail.cpp
@@ -10,11 +10,11 @@
 #include "src/__support/OSUtil/io.h"
 #include "src/stdlib/abort.h"
 
-namespace LIBC_NAMESPACE {
+extern "C" {
 
-LLVM_LIBC_FUNCTION(void, __stack_chk_fail, (void)) {
+void __stack_chk_fail(void) {
   LIBC_NAMESPACE::write_to_stderr("stack smashing detected");
   LIBC_NAMESPACE::abort();
 }
 
-} // namespace LIBC_NAMESPACE
+} // extern "C"
diff --git a/libc/test/src/compiler/stack_chk_guard_test.cpp b/libc/test/src/compiler/stack_chk_guard_test.cpp
index df15962acd9678..1e444703af75cb 100644
--- a/libc/test/src/compiler/stack_chk_guard_test.cpp
+++ b/libc/test/src/compiler/stack_chk_guard_test.cpp
@@ -12,7 +12,7 @@
 #include "test/UnitTest/Test.h"
 
 TEST(LlvmLibcStackChkFail, Death) {
-  EXPECT_DEATH([] { LIBC_NAMESPACE::__stack_chk_fail(); },
+  EXPECT_DEATH([] { __stack_chk_fail(); },
                WITH_SIGNAL(SIGABRT));
 }
 

>From 36f90a6a73ddaaa5c7b51847986c5f7d4bfa9807 Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Tue, 19 Dec 2023 11:38:27 -0800
Subject: [PATCH 4/5] reformat

---
 libc/test/src/compiler/stack_chk_guard_test.cpp | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/libc/test/src/compiler/stack_chk_guard_test.cpp b/libc/test/src/compiler/stack_chk_guard_test.cpp
index 1e444703af75cb..84c54ddeccc956 100644
--- a/libc/test/src/compiler/stack_chk_guard_test.cpp
+++ b/libc/test/src/compiler/stack_chk_guard_test.cpp
@@ -12,8 +12,7 @@
 #include "test/UnitTest/Test.h"
 
 TEST(LlvmLibcStackChkFail, Death) {
-  EXPECT_DEATH([] { __stack_chk_fail(); },
-               WITH_SIGNAL(SIGABRT));
+  EXPECT_DEATH([] { __stack_chk_fail(); }, WITH_SIGNAL(SIGABRT));
 }
 
 TEST(LlvmLibcStackChkFail, Smash) {

>From 3fb24a8041df2858f40ed642e93a4080512e3c48 Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Tue, 19 Dec 2023 11:58:42 -0800
Subject: [PATCH 5/5] add todo/comment

---
 libc/src/compiler/__stack_chk_fail.h | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/libc/src/compiler/__stack_chk_fail.h b/libc/src/compiler/__stack_chk_fail.h
index 0bd7e2eac4044f..56b6042c37f5df 100644
--- a/libc/src/compiler/__stack_chk_fail.h
+++ b/libc/src/compiler/__stack_chk_fail.h
@@ -9,6 +9,10 @@
 #ifndef LLVM_LIBC_SRC_COMPILER___STACK_CHK_FAIL_H
 #define LLVM_LIBC_SRC_COMPILER___STACK_CHK_FAIL_H
 
+// The compiler will emit calls implicitly to a non-namespaced version.
+// TODO: can we additionally provide a namespaced alias so that tests can
+// explicitly call the namespaced variant rather than the non-namespaced
+// definition?
 extern "C" {
 [[noreturn]] void __stack_chk_fail();
 } // extern "C"



More information about the libc-commits mailing list