Skip to content

[libc] restored original no_stack_protector syntax #93997

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

Closed
wants to merge 7 commits into from
Closed
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
1 change: 1 addition & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdlib.atoll
libc.src.stdlib.bsearch
libc.src.stdlib.div
libc.src.stdlib.quick_exit
libc.src.stdlib.labs
libc.src.stdlib.ldiv
libc.src.stdlib.llabs
Expand Down
1 change: 1 addition & 0 deletions libc/spec/stdc.td
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,7 @@ def StdC : StandardSpec<"stdc"> {
FunctionSpec<"_Exit", RetValSpec<NoReturn>, [ArgSpec<IntType>]>,
FunctionSpec<"exit", RetValSpec<NoReturn>, [ArgSpec<IntType>]>,
FunctionSpec<"atexit", RetValSpec<IntType>, [ArgSpec<AtexitHandlerT>]>,
FunctionSpec<"quick_exit", RetValSpec<NoReturn>, [ArgSpec<IntType>]>,
]
>;

Expand Down
2 changes: 1 addition & 1 deletion libc/src/__support/OSUtil/baremetal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ add_object_library(
baremetal_util
SRCS
io.cpp
quick_exit.cpp
exit.cpp
HDRS
io.h
DEPENDS
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
//===----- Baremetal implementation of a quick exit function ----*- C++ -*-===//
//===-------- Baremetal implementation of an exit function ------*- 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
//
//===----------------------------------------------------------------------===//

#include "src/__support/OSUtil/quick_exit.h"
#include "src/__support/OSUtil/exit.h"

// This is intended to be provided by the vendor.
extern "C" [[noreturn]] void __llvm_libc_quick_exit(int status);
extern "C" [[noreturn]] void __llvm_libc_exit(int status);

namespace LIBC_NAMESPACE {
namespace LIBC_NAMESPACE::internal {

[[noreturn]] void quick_exit(int status) { __llvm_libc_quick_exit(status); }
[[noreturn]] void exit(int status) { __llvm_libc_exit(status); }

} // namespace LIBC_NAMESPACE
} // namespace LIBC_NAMESPACE::internal
18 changes: 18 additions & 0 deletions libc/src/__support/OSUtil/exit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===------------ Implementation of an exit function ------------*- 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___SUPPORT_OSUTIL_EXIT_H
#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_EXIT_H

namespace LIBC_NAMESPACE::internal {

[[noreturn]] void exit(int status);

}

#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_EXIT_H
2 changes: 1 addition & 1 deletion libc/src/__support/OSUtil/gpu/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
add_object_library(
gpu_util
SRCS
quick_exit.cpp
exit.cpp
io.cpp
HDRS
io.h
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
//===---------- GPU implementation of a quick exit function -----*- C++ -*-===//
//===------------- GPU implementation of an exit function -------*- 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
//
//===----------------------------------------------------------------------===//

#include "src/__support/OSUtil/quick_exit.h"
#include "src/__support/OSUtil/exit.h"

#include "src/__support/RPC/rpc_client.h"
#include "src/__support/macros/properties/architectures.h"

namespace LIBC_NAMESPACE {
namespace LIBC_NAMESPACE::internal {

[[noreturn]] void quick_exit(int status) {
[[noreturn]] void exit(int status) {
// We want to first make sure the server is listening before we exit.
rpc::Client::Port port = rpc::client.open<RPC_EXIT>();
port.send_and_recv([](rpc::Buffer *) {}, [](rpc::Buffer *) {});
Expand All @@ -25,4 +25,4 @@ namespace LIBC_NAMESPACE {
gpu::end_program();
}

} // namespace LIBC_NAMESPACE
} // namespace LIBC_NAMESPACE::internal
2 changes: 1 addition & 1 deletion libc/src/__support/OSUtil/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ add_subdirectory(${LIBC_TARGET_ARCHITECTURE})
add_object_library(
linux_util
SRCS
quick_exit.cpp
exit.cpp
HDRS
io.h
syscall.h
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//===---------- Linux implementation of a quick exit function ---*- C++ -*-===//
//===------------ Linux implementation of an exit function ------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
Expand All @@ -10,19 +10,19 @@
#include "syscall.h" // For internal syscall function.
#include <sys/syscall.h> // For syscall numbers.

namespace LIBC_NAMESPACE {
namespace LIBC_NAMESPACE::internal {

// mark as no_stack_protector for x86 since TLS can be torn down before calling
// quick_exit so that the stack protector canary cannot be loaded.
// exit so that the stack protector canary cannot be loaded.
#ifdef LIBC_TARGET_ARCH_IS_X86
__attribute__((no_stack_protector))
#endif
__attribute__((noreturn)) void
quick_exit(int status) {
exit(int status) {
for (;;) {
LIBC_NAMESPACE::syscall_impl<long>(SYS_exit_group, status);
LIBC_NAMESPACE::syscall_impl<long>(SYS_exit, status);
}
}

} // namespace LIBC_NAMESPACE
} // namespace LIBC_NAMESPACE::internal
6 changes: 3 additions & 3 deletions libc/src/__support/libc_assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

#else // Not LIBC_COPT_USE_C_ASSERT

#include "src/__support/OSUtil/exit.h"
#include "src/__support/OSUtil/io.h"
#include "src/__support/OSUtil/quick_exit.h"
#include "src/__support/integer_to_string.h"
#include "src/__support/macros/attributes.h" // For LIBC_INLINE

Expand Down Expand Up @@ -51,7 +51,7 @@ LIBC_INLINE void report_assertion_failure(const char *assertion,

// The public "assert" macro calls abort on failure. Should it be same here?
// The libc internal assert can fire from anywhere inside the libc. So, to
// avoid potential chicken-and-egg problems, it is simple to do a quick_exit
// avoid potential chicken-and-egg problems, it is simple to do an exit
// on assertion failure instead of calling abort. We also don't want to use
// __builtin_trap as it could potentially be implemented using illegal
// instructions which can be very misleading when debugging.
Expand All @@ -76,7 +76,7 @@ LIBC_INLINE void report_assertion_failure(const char *assertion,
"' in function: '"); \
LIBC_NAMESPACE::write_to_stderr(__PRETTY_FUNCTION__); \
LIBC_NAMESPACE::write_to_stderr("'\n"); \
LIBC_NAMESPACE::quick_exit(0xFF); \
LIBC_NAMESPACE::internal::exit(0xFF); \
} \
} while (false)
#endif // NDEBUG
Expand Down
10 changes: 10 additions & 0 deletions libc/src/stdlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ add_entrypoint_object(
libc.src.__support.str_to_integer
)

add_entrypoint_object(
quick_exit
SRCS
quick_exit.cpp
HDRS
quick_exit.h
DEPENDS
libc.src.__support.OSUtil.osutil
)

add_entrypoint_object(
getenv
SRCS
Expand Down
4 changes: 2 additions & 2 deletions libc/src/stdlib/_Exit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
//
//===----------------------------------------------------------------------===//

#include "src/__support/OSUtil/quick_exit.h"
#include "src/__support/OSUtil/exit.h"
#include "src/__support/common.h"

#include "src/stdlib/_Exit.h"

namespace LIBC_NAMESPACE {

[[noreturn]] LLVM_LIBC_FUNCTION(void, _Exit, (int status)) {
quick_exit(status);
internal::exit(status);
}

} // namespace LIBC_NAMESPACE
4 changes: 2 additions & 2 deletions libc/src/stdlib/exit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//===----------------------------------------------------------------------===//

#include "src/stdlib/exit.h"
#include "src/__support/OSUtil/quick_exit.h"
#include "src/__support/OSUtil/exit.h"
#include "src/__support/common.h"

extern "C" void __cxa_finalize(void *);
Expand All @@ -16,7 +16,7 @@ namespace LIBC_NAMESPACE {

[[noreturn]] LLVM_LIBC_FUNCTION(void, exit, (int status)) {
__cxa_finalize(nullptr);
quick_exit(status);
internal::exit(status);
}

} // namespace LIBC_NAMESPACE
22 changes: 22 additions & 0 deletions libc/src/stdlib/quick_exit.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===-- Implementation of quick_exit --------------------------------------===//
//
// 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/stdlib/quick_exit.h"
#include "src/__support/OSUtil/exit.h"
#include "src/__support/common.h"

// extern "C" void __cxa_finalize(void *);

namespace LIBC_NAMESPACE {

[[noreturn]] LLVM_LIBC_FUNCTION(void, quick_exit, (int status)) {
// __cxa_finalize(nullptr);
internal::exit(status);
}

} // namespace LIBC_NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
//===---------- Implementation of a quick exit function ---------*- C++ -*-===//
//===-- Implementation header for quick_exit --------------------*- 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___SUPPORT_OSUTIL_QUICK_EXIT_H
#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_QUICK_EXIT_H
#ifndef LLVM_LIBC_SRC_STDLIB_QUICK_EXIT_H
#define LLVM_LIBC_SRC_STDLIB_QUICK_EXIT_H

namespace LIBC_NAMESPACE {

[[noreturn]] void quick_exit(int status);

}
} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_QUICK_EXIT_H
#endif // LLVM_LIBC_SRC_STDLIB_QUICK_EXIT_H
4 changes: 2 additions & 2 deletions libc/src/unistd/_exit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
//===----------------------------------------------------------------------===//

#include "src/unistd/_exit.h"
#include "src/__support/OSUtil/quick_exit.h"
#include "src/__support/OSUtil/exit.h"
#include "src/__support/common.h"

namespace LIBC_NAMESPACE {

[[noreturn]] LLVM_LIBC_FUNCTION(void, _exit, (int status)) {
quick_exit(status);
internal::exit(status);
}

} // namespace LIBC_NAMESPACE
10 changes: 5 additions & 5 deletions libc/test/IntegrationTest/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,40 @@
#ifndef LLVM_LIBC_UTILS_INTEGRATION_TEST_TEST_H
#define LLVM_LIBC_UTILS_INTEGRATION_TEST_TEST_H

#include "src/__support/OSUtil/exit.h"
#include "src/__support/OSUtil/io.h"
#include "src/__support/OSUtil/quick_exit.h"

#define __AS_STRING(val) #val
#define __CHECK_TRUE(file, line, val, should_exit) \
if (!(val)) { \
LIBC_NAMESPACE::write_to_stderr(file ":" __AS_STRING( \
line) ": Expected '" #val "' to be true, but is false\n"); \
if (should_exit) \
LIBC_NAMESPACE::quick_exit(127); \
LIBC_NAMESPACE::internal::exit(127); \
}

#define __CHECK_FALSE(file, line, val, should_exit) \
if (val) { \
LIBC_NAMESPACE::write_to_stderr(file ":" __AS_STRING( \
line) ": Expected '" #val "' to be false, but is true\n"); \
if (should_exit) \
LIBC_NAMESPACE::quick_exit(127); \
LIBC_NAMESPACE::internal::exit(127); \
}

#define __CHECK_EQ(file, line, val1, val2, should_exit) \
if ((val1) != (val2)) { \
LIBC_NAMESPACE::write_to_stderr(file ":" __AS_STRING( \
line) ": Expected '" #val1 "' to be equal to '" #val2 "'\n"); \
if (should_exit) \
LIBC_NAMESPACE::quick_exit(127); \
LIBC_NAMESPACE::internal::exit(127); \
}

#define __CHECK_NE(file, line, val1, val2, should_exit) \
if ((val1) == (val2)) { \
LIBC_NAMESPACE::write_to_stderr(file ":" __AS_STRING( \
line) ": Expected '" #val1 "' to not be equal to '" #val2 "'\n"); \
if (should_exit) \
LIBC_NAMESPACE::quick_exit(127); \
LIBC_NAMESPACE::internal::exit(127); \
}

////////////////////////////////////////////////////////////////////////////////
Expand Down
13 changes: 13 additions & 0 deletions libc/test/src/stdlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,19 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.signal.raise
)

add_libc_test(
quick_exit_test
# The EXPECT_EXITS test is only availible for unit tests.
UNIT_TEST_ONLY
SUITE
libc-stdlib-tests
SRCS
quick_exit_test.cpp
DEPENDS
libc.include.stdlib
libc.src.stdlib.quick_exit
)

# Only the GPU has an in-tree 'malloc' implementation.
if(LIBC_TARGET_OS_IS_GPU)
add_libc_test(
Expand Down
16 changes: 16 additions & 0 deletions libc/test/src/stdlib/quick_exit_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//===-- Unittests for quick_exit ------------------------------------------===//
//
// 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/stdlib/exit.h"
#include "src/stdlib/quick_exit.h"
#include "test/UnitTest/Test.h"

TEST(LlvmLibcStdlib, quick_exit) {
EXPECT_EXITS([] { LIBC_NAMESPACE::quick_exit(1); }, 1);
EXPECT_EXITS([] { LIBC_NAMESPACE::quick_exit(65); }, 65);
}
8 changes: 4 additions & 4 deletions utils/bazel/llvm-project-overlay/libc/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ libc_support_library(
":__support_integer_to_string",
":__support_macros_attributes",
":__support_osutil_io",
":__support_osutil_quick_exit",
":__support_osutil_exit",
],
)

Expand Down Expand Up @@ -1062,9 +1062,9 @@ libc_support_library(
)

libc_support_library(
name = "__support_osutil_quick_exit",
srcs = ["src/__support/OSUtil/linux/quick_exit.cpp"],
hdrs = ["src/__support/OSUtil/quick_exit.h"],
name = "__support_osutil_exit",
srcs = ["src/__support/OSUtil/linux/exit.cpp"],
hdrs = ["src/__support/OSUtil/exit.h"],
target_compatible_with = select({
"@platforms//os:linux": [],
"//conditions:default": ["@platforms//:incompatible"],
Expand Down
Loading