Skip to content

[libc] move __stack_chk_fail to src/ from startup/ #75863

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 7 commits into from
Dec 19, 2023
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
5 changes: 4 additions & 1 deletion libc/config/baremetal/arm/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions libc/config/baremetal/riscv/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 5 additions & 2 deletions libc/config/linux/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions libc/config/linux/arm/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions libc/config/linux/riscv/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion libc/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
18 changes: 18 additions & 0 deletions libc/src/compiler/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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}
)
18 changes: 18 additions & 0 deletions libc/src/compiler/__stack_chk_fail.h
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions libc/src/compiler/generic/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
)
20 changes: 20 additions & 0 deletions libc/src/compiler/generic/__stack_chk_fail.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- 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
5 changes: 0 additions & 5 deletions libc/startup/linux/x86_64/start.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion libc/test/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions libc/test/src/compiler/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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
libc.src.string.memset
COMPILE_OPTIONS
-fstack-protector-all
)
26 changes: 26 additions & 0 deletions libc/test/src/compiler/stack_chk_guard_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//===-- 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.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "llvm-libc-macros/signal-macros.h"
#include "src/compiler/__stack_chk_fail.h"
#include "src/string/memset.h"
#include "test/UnitTest/Test.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));
}