Skip to content

Commit 315a5cc

Browse files
[libc] move __stack_chk_fail to src/ from startup/ (#75863)
__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*`.
1 parent 0768253 commit 315a5cc

File tree

15 files changed

+131
-12
lines changed

15 files changed

+131
-12
lines changed

libc/config/baremetal/arm/entrypoints.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ set(TARGET_LIBC_ENTRYPOINTS
1717
libc.src.ctype.tolower
1818
libc.src.ctype.toupper
1919

20+
# compiler entrypoints (no corresponding header)
21+
libc.src.compiler.__stack_chk_fail
22+
2023
# errno.h entrypoints
2124
libc.src.errno.errno
2225

@@ -69,7 +72,7 @@ set(TARGET_LIBC_ENTRYPOINTS
6972
libc.src.stdio.snprintf
7073
libc.src.stdio.vsprintf
7174
libc.src.stdio.vsnprintf
72-
75+
7376
# stdlib.h entrypoints
7477
libc.src.stdlib.abs
7578
libc.src.stdlib.atoi

libc/config/baremetal/riscv/entrypoints.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ set(TARGET_LIBC_ENTRYPOINTS
1717
libc.src.ctype.tolower
1818
libc.src.ctype.toupper
1919

20+
# compiler entrypoints (no corresponding header)
21+
libc.src.compiler.__stack_chk_fail
22+
2023
# errno.h entrypoints
2124
libc.src.errno.errno
2225

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ set(TARGET_LIBC_ENTRYPOINTS
1616
libc.src.ctype.toascii
1717
libc.src.ctype.tolower
1818
libc.src.ctype.toupper
19-
19+
2020
# errno.h entrypoints
2121
libc.src.errno.errno
2222

@@ -239,7 +239,7 @@ set(TARGET_LIBM_ENTRYPOINTS
239239
libc.src.math.asinf
240240
libc.src.math.asinhf
241241
libc.src.math.atanf
242-
libc.src.math.atanhf
242+
libc.src.math.atanhf
243243
libc.src.math.copysign
244244
libc.src.math.copysignf
245245
libc.src.math.copysignl
@@ -353,6 +353,9 @@ set(TARGET_LIBM_ENTRYPOINTS
353353

354354
if(LLVM_LIBC_FULL_BUILD)
355355
list(APPEND TARGET_LIBC_ENTRYPOINTS
356+
# compiler entrypoints (no corresponding header)
357+
libc.src.compiler.__stack_chk_fail
358+
356359
# network.h entrypoints
357360
libc.src.network.htonl
358361
libc.src.network.htons

libc/config/linux/arm/entrypoints.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ set(TARGET_LIBC_ENTRYPOINTS
6666
libc.src.inttypes.imaxdiv
6767
libc.src.inttypes.strtoimax
6868
libc.src.inttypes.strtoumax
69-
69+
7070
# stdlib.h entrypoints
7171
libc.src.stdlib.abs
7272
libc.src.stdlib.atoi
@@ -88,7 +88,7 @@ set(TARGET_LIBC_ENTRYPOINTS
8888
libc.src.stdlib.strtoll
8989
libc.src.stdlib.strtoul
9090
libc.src.stdlib.strtoull
91-
91+
9292
# sys/mman.h entrypoints
9393
libc.src.sys.mman.mmap
9494
libc.src.sys.mman.munmap

libc/config/linux/riscv/entrypoints.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,9 @@ set(TARGET_LIBM_ENTRYPOINTS
362362

363363
if(LLVM_LIBC_FULL_BUILD)
364364
list(APPEND TARGET_LIBC_ENTRYPOINTS
365+
# compiler entrypoints (no corresponding header)
366+
libc.src.compiler.__stack_chk_fail
367+
365368
# assert.h entrypoints
366369
libc.src.assert.__assert_fail
367370

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,9 @@ if(LLVM_LIBC_FULL_BUILD)
375375
# assert.h entrypoints
376376
libc.src.assert.__assert_fail
377377

378+
# compiler entrypoints (no corresponding header)
379+
libc.src.compiler.__stack_chk_fail
380+
378381
# dirent.h entrypoints
379382
libc.src.dirent.closedir
380383
libc.src.dirent.dirfd

libc/src/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ if(NOT LLVM_LIBC_FULL_BUILD)
2929
endif()
3030

3131
add_subdirectory(assert)
32+
add_subdirectory(compiler)
3233
add_subdirectory(network)
34+
add_subdirectory(search)
3335
add_subdirectory(setjmp)
3436
add_subdirectory(signal)
3537
add_subdirectory(spawn)
3638
add_subdirectory(threads)
3739
add_subdirectory(time)
38-
add_subdirectory(search)

libc/src/compiler/CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
2+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
3+
else()
4+
add_subdirectory(generic)
5+
endif()
6+
7+
if(TARGET libc.src.compiler.${LIBC_TARGET_OS}.__stack_chk_fail)
8+
set(stack_chk_fail_dep libc.src.compiler.${LIBC_TARGET_OS}.__stack_chk_fail)
9+
else()
10+
set(stack_chk_fail_dep libc.src.compiler.generic.__stack_chk_fail)
11+
endif()
12+
13+
add_entrypoint_object(
14+
__stack_chk_fail
15+
ALIAS
16+
DEPENDS
17+
${stack_chk_fail_dep}
18+
)

libc/src/compiler/__stack_chk_fail.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Internal header for __stack_chk_fail --------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_COMPILER___STACK_CHK_FAIL_H
10+
#define LLVM_LIBC_SRC_COMPILER___STACK_CHK_FAIL_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
[[noreturn]] void __stack_chk_fail();
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_COMPILER___STACK_CHK_FAIL_H
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
add_entrypoint_object(
2+
__stack_chk_fail
3+
SRCS
4+
__stack_chk_fail.cpp
5+
HDRS
6+
../__stack_chk_fail.h
7+
DEPENDS
8+
libc.include.assert
9+
libc.src.__support.OSUtil.osutil
10+
libc.src.stdlib.abort
11+
)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation of __stack_chk_fail --------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/compiler/__stack_chk_fail.h"
10+
#include "src/__support/OSUtil/io.h"
11+
#include "src/stdlib/abort.h"
12+
13+
namespace LIBC_NAMESPACE {
14+
15+
LLVM_LIBC_FUNCTION(void, __stack_chk_fail, (void)) {
16+
LIBC_NAMESPACE::write_to_stderr("stack smashing detected");
17+
LIBC_NAMESPACE::abort();
18+
}
19+
20+
} // namespace LIBC_NAMESPACE

libc/startup/linux/x86_64/start.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@
2525

2626
extern "C" int main(int, char **, char **);
2727

28-
extern "C" void __stack_chk_fail() {
29-
LIBC_NAMESPACE::write_to_stderr("stack smashing detected");
30-
LIBC_NAMESPACE::abort();
31-
}
32-
3328
namespace LIBC_NAMESPACE {
3429

3530
#ifdef SYS_mmap2

libc/test/src/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ if(NOT LLVM_LIBC_FULL_BUILD)
6060
return()
6161
endif()
6262

63-
add_subdirectory(dirent)
6463
add_subdirectory(assert)
64+
add_subdirectory(compiler)
65+
add_subdirectory(dirent)
6566
add_subdirectory(network)
6667
add_subdirectory(setjmp)
6768
add_subdirectory(signal)

libc/test/src/compiler/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
add_custom_target(libc_stack_chk_guard_unittests)
2+
3+
add_libc_unittest(
4+
stack_chk_guard_test
5+
SUITE
6+
libc_stack_chk_guard_unittests
7+
SRCS
8+
stack_chk_guard_test.cpp
9+
DEPENDS
10+
libc.src.compiler.__stack_chk_fail
11+
libc.src.string.memset
12+
COMPILE_OPTIONS
13+
-fstack-protector-all
14+
)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//===-- Unittests for __stack_chk_fail ------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "llvm-libc-macros/signal-macros.h"
10+
#include "src/compiler/__stack_chk_fail.h"
11+
#include "src/string/memset.h"
12+
#include "test/UnitTest/Test.h"
13+
14+
TEST(LlvmLibcStackChkFail, Death) {
15+
EXPECT_DEATH([] { LIBC_NAMESPACE::__stack_chk_fail(); },
16+
WITH_SIGNAL(SIGABRT));
17+
}
18+
19+
TEST(LlvmLibcStackChkFail, Smash) {
20+
EXPECT_DEATH(
21+
[] {
22+
int arr[20];
23+
LIBC_NAMESPACE::memset(arr, 0xAA, 9001);
24+
},
25+
WITH_SIGNAL(SIGABRT));
26+
}

0 commit comments

Comments
 (0)