[libc-commits] [libc] [libc] move __stack_chk_fail to src/ from startup/ (PR #75863)
Nick Desaulniers via libc-commits
libc-commits at lists.llvm.org
Tue Dec 19 10:59:20 PST 2023
https://github.com/nickdesaulniers updated https://github.com/llvm/llvm-project/pull/75863
>From 60958c1aadc2e89d08e30f698c4c23f8d89aa1b7 Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Mon, 18 Dec 2023 13:50:06 -0800
Subject: [PATCH 1/7] [libc] move __stack_chk_fail to src/ from startup/
__stack_chk_fail should be provided by libc.a, not startup files.
Add __stack_chk_fail to existing linux and arm entrypoints. On Windows (when
not targeting MinGW), it seems that the corresponding function identifier is
__security_check_cookie, so no entrypoint is added for Windows. Baremetal
targets also ought to be compileable with `-fstack-protector*`
There is no common header for this prototype, since calls to __stack_chk_fail
are meant to be inserted by the compiler upon function return when compiled
`-fstack-protector*`.
---
libc/config/baremetal/arm/entrypoints.txt | 5 ++++-
libc/config/baremetal/riscv/entrypoints.txt | 3 +++
libc/config/linux/aarch64/entrypoints.txt | 7 +++++--
libc/config/linux/arm/entrypoints.txt | 4 ++--
libc/config/linux/riscv/entrypoints.txt | 3 +++
libc/config/linux/x86_64/entrypoints.txt | 3 +++
libc/src/CMakeLists.txt | 3 ++-
libc/src/compiler/CMakeLists.txt | 18 ++++++++++++++++
libc/src/compiler/generic/CMakeLists.txt | 11 ++++++++++
.../src/compiler/generic/__stack_chk_fail.cpp | 21 +++++++++++++++++++
libc/startup/linux/x86_64/start.cpp | 5 -----
11 files changed, 72 insertions(+), 11 deletions(-)
create mode 100644 libc/src/compiler/CMakeLists.txt
create mode 100644 libc/src/compiler/generic/CMakeLists.txt
create mode 100644 libc/src/compiler/generic/__stack_chk_fail.cpp
diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index a88b7aa749e565..a0779c41652aeb 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -17,6 +17,9 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.ctype.tolower
libc.src.ctype.toupper
+ # compiler entrypoints (no corresponding header)
+ libc.src.compiler.__stack_chk_fail
+
# errno.h entrypoints
libc.src.errno.errno
@@ -69,7 +72,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdio.snprintf
libc.src.stdio.vsprintf
libc.src.stdio.vsnprintf
-
+
# stdlib.h entrypoints
libc.src.stdlib.abs
libc.src.stdlib.atoi
diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index 3b7ca513eb0965..3e15cc8901bddf 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -17,6 +17,9 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.ctype.tolower
libc.src.ctype.toupper
+ # compiler entrypoints (no corresponding header)
+ libc.src.compiler.__stack_chk_fail
+
# errno.h entrypoints
libc.src.errno.errno
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 60e0e2b29aed36..77c9a50b8b7e5d 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -16,7 +16,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.ctype.toascii
libc.src.ctype.tolower
libc.src.ctype.toupper
-
+
# errno.h entrypoints
libc.src.errno.errno
@@ -239,7 +239,7 @@ set(TARGET_LIBM_ENTRYPOINTS
libc.src.math.asinf
libc.src.math.asinhf
libc.src.math.atanf
- libc.src.math.atanhf
+ libc.src.math.atanhf
libc.src.math.copysign
libc.src.math.copysignf
libc.src.math.copysignl
@@ -353,6 +353,9 @@ set(TARGET_LIBM_ENTRYPOINTS
if(LLVM_LIBC_FULL_BUILD)
list(APPEND TARGET_LIBC_ENTRYPOINTS
+ # compiler entrypoints (no corresponding header)
+ libc.src.compiler.__stack_chk_fail
+
# network.h entrypoints
libc.src.network.htonl
libc.src.network.htons
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 123c7e33377ad1..274d5aa5a0057d 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -66,7 +66,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.inttypes.imaxdiv
libc.src.inttypes.strtoimax
libc.src.inttypes.strtoumax
-
+
# stdlib.h entrypoints
libc.src.stdlib.abs
libc.src.stdlib.atoi
@@ -88,7 +88,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdlib.strtoll
libc.src.stdlib.strtoul
libc.src.stdlib.strtoull
-
+
# sys/mman.h entrypoints
libc.src.sys.mman.mmap
libc.src.sys.mman.munmap
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 948708e35f45d2..e389936ffca1ef 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -362,6 +362,9 @@ set(TARGET_LIBM_ENTRYPOINTS
if(LLVM_LIBC_FULL_BUILD)
list(APPEND TARGET_LIBC_ENTRYPOINTS
+ # compiler entrypoints (no corresponding header)
+ libc.src.compiler.__stack_chk_fail
+
# assert.h entrypoints
libc.src.assert.__assert_fail
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 1c93063e25e90c..3adcd57d0c0849 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -375,6 +375,9 @@ if(LLVM_LIBC_FULL_BUILD)
# assert.h entrypoints
libc.src.assert.__assert_fail
+ # compiler entrypoints (no corresponding header)
+ libc.src.compiler.__stack_chk_fail
+
# dirent.h entrypoints
libc.src.dirent.closedir
libc.src.dirent.dirfd
diff --git a/libc/src/CMakeLists.txt b/libc/src/CMakeLists.txt
index 3ab62a4f667d26..492f9c5bd50f9b 100644
--- a/libc/src/CMakeLists.txt
+++ b/libc/src/CMakeLists.txt
@@ -29,10 +29,11 @@ if(NOT LLVM_LIBC_FULL_BUILD)
endif()
add_subdirectory(assert)
+add_subdirectory(compiler)
add_subdirectory(network)
+add_subdirectory(search)
add_subdirectory(setjmp)
add_subdirectory(signal)
add_subdirectory(spawn)
add_subdirectory(threads)
add_subdirectory(time)
-add_subdirectory(search)
diff --git a/libc/src/compiler/CMakeLists.txt b/libc/src/compiler/CMakeLists.txt
new file mode 100644
index 00000000000000..aa59d84e08d146
--- /dev/null
+++ b/libc/src/compiler/CMakeLists.txt
@@ -0,0 +1,18 @@
+if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+ add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+else()
+ add_subdirectory(generic)
+endif()
+
+if(TARGET libc.src.compiler.${LIBC_TARGET_OS}.__stack_chk_fail)
+ set(stack_chk_fail_dep libc.src.compiler.${LIBC_TARGET_OS}.__stack_chk_fail)
+else()
+ set(stack_chk_fail_dep libc.src.compiler.generic.__stack_chk_fail)
+endif()
+
+add_entrypoint_object(
+ __stack_chk_fail
+ ALIAS
+ DEPENDS
+ ${stack_chk_fail_dep}
+)
diff --git a/libc/src/compiler/generic/CMakeLists.txt b/libc/src/compiler/generic/CMakeLists.txt
new file mode 100644
index 00000000000000..0d869b72a12cf5
--- /dev/null
+++ b/libc/src/compiler/generic/CMakeLists.txt
@@ -0,0 +1,11 @@
+add_entrypoint_object(
+ __stack_chk_fail
+ SRCS
+ __stack_chk_fail.cpp
+ HDRS
+ ../__stack_chk_fail.h
+ DEPENDS
+ libc.include.assert
+ libc.src.__support.OSUtil.osutil
+ libc.src.stdlib.abort
+)
diff --git a/libc/src/compiler/generic/__stack_chk_fail.cpp b/libc/src/compiler/generic/__stack_chk_fail.cpp
new file mode 100644
index 00000000000000..076ed351e5fe0c
--- /dev/null
+++ b/libc/src/compiler/generic/__stack_chk_fail.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of __stack_chk_fail --------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/compiler/__stack_chk_fail.h"
+#include "src/__support/OSUtil/io.h"
+#include "src/stdlib/abort.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(void, __stack_chk_fail, (void)) {
+ LIBC_NAMESPACE::write_to_stderr("stack smashing detected");
+ LIBC_NAMESPACE::abort();
+}
+
+} // namespace LIBC_NAMESPACE
+
diff --git a/libc/startup/linux/x86_64/start.cpp b/libc/startup/linux/x86_64/start.cpp
index 496105dfd0b43a..c7d931d763a46f 100644
--- a/libc/startup/linux/x86_64/start.cpp
+++ b/libc/startup/linux/x86_64/start.cpp
@@ -25,11 +25,6 @@
extern "C" int main(int, char **, char **);
-extern "C" void __stack_chk_fail() {
- LIBC_NAMESPACE::write_to_stderr("stack smashing detected");
- LIBC_NAMESPACE::abort();
-}
-
namespace LIBC_NAMESPACE {
#ifdef SYS_mmap2
>From 932af76b717b4294d2c9533fabbb4090e8187436 Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Mon, 18 Dec 2023 14:00:11 -0800
Subject: [PATCH 2/7] git format
---
libc/src/compiler/generic/__stack_chk_fail.cpp | 1 -
1 file changed, 1 deletion(-)
diff --git a/libc/src/compiler/generic/__stack_chk_fail.cpp b/libc/src/compiler/generic/__stack_chk_fail.cpp
index 076ed351e5fe0c..0ca02071f2a997 100644
--- a/libc/src/compiler/generic/__stack_chk_fail.cpp
+++ b/libc/src/compiler/generic/__stack_chk_fail.cpp
@@ -18,4 +18,3 @@ LLVM_LIBC_FUNCTION(void, __stack_chk_fail, (void)) {
}
} // namespace LIBC_NAMESPACE
-
>From 7c26cda7a7681ec11e2ccc4c52f3dfb1f6685e98 Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Mon, 18 Dec 2023 14:09:37 -0800
Subject: [PATCH 3/7] add header
---
libc/src/compiler/__stack_chk_fail.h | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
create mode 100644 libc/src/compiler/__stack_chk_fail.h
diff --git a/libc/src/compiler/__stack_chk_fail.h b/libc/src/compiler/__stack_chk_fail.h
new file mode 100644
index 00000000000000..2e3d849ff8c67c
--- /dev/null
+++ b/libc/src/compiler/__stack_chk_fail.h
@@ -0,0 +1,18 @@
+//===-- Internal header for __stack_chk_fail --------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_COMPILER___STACK_CHK_FAIL_H
+#define LLVM_LIBC_SRC_COMPILER___STACK_CHK_FAIL_H
+
+namespace LIBC_NAMESPACE {
+
+[[noreturn]] void __stack_chk_fail();
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_COMPILER___STACK_CHK_FAIL_H
>From 35d07590efe6cedf908493d3083907e30dddc975 Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Mon, 18 Dec 2023 14:29:19 -0800
Subject: [PATCH 4/7] add death test
---
libc/test/src/CMakeLists.txt | 3 ++-
libc/test/src/compiler/CMakeLists.txt | 11 +++++++++++
libc/test/src/compiler/stack_chk_guard_test.cpp | 16 ++++++++++++++++
3 files changed, 29 insertions(+), 1 deletion(-)
create mode 100644 libc/test/src/compiler/CMakeLists.txt
create mode 100644 libc/test/src/compiler/stack_chk_guard_test.cpp
diff --git a/libc/test/src/CMakeLists.txt b/libc/test/src/CMakeLists.txt
index c45b94f364397e..6bd8ace9ea71af 100644
--- a/libc/test/src/CMakeLists.txt
+++ b/libc/test/src/CMakeLists.txt
@@ -60,8 +60,9 @@ if(NOT LLVM_LIBC_FULL_BUILD)
return()
endif()
-add_subdirectory(dirent)
add_subdirectory(assert)
+add_subdirectory(compiler)
+add_subdirectory(dirent)
add_subdirectory(network)
add_subdirectory(setjmp)
add_subdirectory(signal)
diff --git a/libc/test/src/compiler/CMakeLists.txt b/libc/test/src/compiler/CMakeLists.txt
new file mode 100644
index 00000000000000..d6163bdd5144bd
--- /dev/null
+++ b/libc/test/src/compiler/CMakeLists.txt
@@ -0,0 +1,11 @@
+add_custom_target(libc_stack_chk_guard_unittests)
+
+add_libc_unittest(
+ stack_chk_guard_test
+ SUITE
+ libc_stack_chk_guard_unittests
+ SRCS
+ stack_chk_guard_test.cpp
+ DEPENDS
+ libc.src.compiler.__stack_chk_fail
+ )
diff --git a/libc/test/src/compiler/stack_chk_guard_test.cpp b/libc/test/src/compiler/stack_chk_guard_test.cpp
new file mode 100644
index 00000000000000..57868641f7e1c4
--- /dev/null
+++ b/libc/test/src/compiler/stack_chk_guard_test.cpp
@@ -0,0 +1,16 @@
+//===-- Unittests for assert ----------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm-libc-macros/signal-macros.h"
+#include "src/compiler/__stack_chk_fail.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcStackChkFail, Death) {
+ EXPECT_DEATH([] { LIBC_NAMESPACE::__stack_chk_fail(); },
+ WITH_SIGNAL(SIGABRT));
+}
>From f5e86b34ff5b100114b98f9a60137e06fbc013d5 Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Tue, 19 Dec 2023 09:23:28 -0800
Subject: [PATCH 5/7] add explicit test for stack smashing
---
libc/test/src/compiler/CMakeLists.txt | 3 +++
libc/test/src/compiler/stack_chk_guard_test.cpp | 8 ++++++++
2 files changed, 11 insertions(+)
diff --git a/libc/test/src/compiler/CMakeLists.txt b/libc/test/src/compiler/CMakeLists.txt
index d6163bdd5144bd..b8d682c1c519a5 100644
--- a/libc/test/src/compiler/CMakeLists.txt
+++ b/libc/test/src/compiler/CMakeLists.txt
@@ -8,4 +8,7 @@ add_libc_unittest(
stack_chk_guard_test.cpp
DEPENDS
libc.src.compiler.__stack_chk_fail
+ libc.src.string.memset
+ COMPILE_OPTIONS
+ -fstack-protector-all
)
diff --git a/libc/test/src/compiler/stack_chk_guard_test.cpp b/libc/test/src/compiler/stack_chk_guard_test.cpp
index 57868641f7e1c4..d5d4838f6b0d59 100644
--- a/libc/test/src/compiler/stack_chk_guard_test.cpp
+++ b/libc/test/src/compiler/stack_chk_guard_test.cpp
@@ -9,8 +9,16 @@
#include "llvm-libc-macros/signal-macros.h"
#include "src/compiler/__stack_chk_fail.h"
#include "test/UnitTest/Test.h"
+#include "src/string/memset.h"
TEST(LlvmLibcStackChkFail, Death) {
EXPECT_DEATH([] { LIBC_NAMESPACE::__stack_chk_fail(); },
WITH_SIGNAL(SIGABRT));
}
+
+TEST(LlvmLibcStackChkFail, Smash) {
+ EXPECT_DEATH([] {
+ int arr [20];
+ LIBC_NAMESPACE::memset(arr, 0xAA, 9001);
+ }, WITH_SIGNAL(SIGABRT));
+}
>From e4acf15d8c0fb6c6eb1bdb2c03d3bc03655341ba Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Tue, 19 Dec 2023 09:25:14 -0800
Subject: [PATCH 6/7] git clang-format HEAD~
---
libc/test/src/compiler/stack_chk_guard_test.cpp | 12 +++++++-----
1 file changed, 7 insertions(+), 5 deletions(-)
diff --git a/libc/test/src/compiler/stack_chk_guard_test.cpp b/libc/test/src/compiler/stack_chk_guard_test.cpp
index d5d4838f6b0d59..5f1c056c1ca029 100644
--- a/libc/test/src/compiler/stack_chk_guard_test.cpp
+++ b/libc/test/src/compiler/stack_chk_guard_test.cpp
@@ -8,8 +8,8 @@
#include "llvm-libc-macros/signal-macros.h"
#include "src/compiler/__stack_chk_fail.h"
-#include "test/UnitTest/Test.h"
#include "src/string/memset.h"
+#include "test/UnitTest/Test.h"
TEST(LlvmLibcStackChkFail, Death) {
EXPECT_DEATH([] { LIBC_NAMESPACE::__stack_chk_fail(); },
@@ -17,8 +17,10 @@ TEST(LlvmLibcStackChkFail, Death) {
}
TEST(LlvmLibcStackChkFail, Smash) {
- EXPECT_DEATH([] {
- int arr [20];
- LIBC_NAMESPACE::memset(arr, 0xAA, 9001);
- }, WITH_SIGNAL(SIGABRT));
+ EXPECT_DEATH(
+ [] {
+ int arr[20];
+ LIBC_NAMESPACE::memset(arr, 0xAA, 9001);
+ },
+ WITH_SIGNAL(SIGABRT));
}
>From 13df4945c04e4b145fd0e60e25020c2743537812 Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Tue, 19 Dec 2023 10:59:06 -0800
Subject: [PATCH 7/7] fix comment in test
---
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 5f1c056c1ca029..1de2d1b7357f70 100644
--- a/libc/test/src/compiler/stack_chk_guard_test.cpp
+++ b/libc/test/src/compiler/stack_chk_guard_test.cpp
@@ -1,4 +1,4 @@
-//===-- Unittests for assert ----------------------------------------------===//
+//===-- Unittests for __stack_chk_fail ------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
More information about the libc-commits
mailing list