Skip to content

Commit 07bd439

Browse files
authored
[libc] added quick_exit function (#93620)
- In /libc/src/__support/ OSUtil, changed quick_exit to just exit, and put in namespace LIBC_NAMESPACE::internal. - In /libc/src/stdlib added quick_exit - Added test files for quick_exit
1 parent 6416958 commit 07bd439

File tree

20 files changed

+125
-44
lines changed

20 files changed

+125
-44
lines changed

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ set(TARGET_LIBC_ENTRYPOINTS
173173
libc.src.stdlib.atoll
174174
libc.src.stdlib.bsearch
175175
libc.src.stdlib.div
176+
libc.src.stdlib.quick_exit
176177
libc.src.stdlib.labs
177178
libc.src.stdlib.ldiv
178179
libc.src.stdlib.llabs

libc/spec/stdc.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,6 +1072,7 @@ def StdC : StandardSpec<"stdc"> {
10721072
FunctionSpec<"_Exit", RetValSpec<NoReturn>, [ArgSpec<IntType>]>,
10731073
FunctionSpec<"exit", RetValSpec<NoReturn>, [ArgSpec<IntType>]>,
10741074
FunctionSpec<"atexit", RetValSpec<IntType>, [ArgSpec<AtexitHandlerT>]>,
1075+
FunctionSpec<"quick_exit", RetValSpec<NoReturn>, [ArgSpec<IntType>]>,
10751076
]
10761077
>;
10771078

libc/src/__support/OSUtil/baremetal/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ add_object_library(
22
baremetal_util
33
SRCS
44
io.cpp
5-
quick_exit.cpp
5+
exit.cpp
66
HDRS
77
io.h
88
DEPENDS
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
//===----- Baremetal implementation of a quick exit function ----*- C++ -*-===//
1+
//===-------- Baremetal implementation of an exit function ------*- C++ -*-===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "src/__support/OSUtil/quick_exit.h"
9+
#include "src/__support/OSUtil/exit.h"
1010

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

14-
namespace LIBC_NAMESPACE {
14+
namespace LIBC_NAMESPACE::internal {
1515

16-
[[noreturn]] void quick_exit(int status) { __llvm_libc_quick_exit(status); }
16+
[[noreturn]] void exit(int status) { __llvm_libc_exit(status); }
1717

18-
} // namespace LIBC_NAMESPACE
18+
} // namespace LIBC_NAMESPACE::internal

libc/src/__support/OSUtil/exit.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===------------ Implementation of an exit function ------------*- 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___SUPPORT_OSUTIL_EXIT_H
10+
#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_EXIT_H
11+
12+
namespace LIBC_NAMESPACE::internal {
13+
14+
[[noreturn]] void exit(int status);
15+
16+
}
17+
18+
#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_EXIT_H

libc/src/__support/OSUtil/gpu/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
add_object_library(
22
gpu_util
33
SRCS
4-
quick_exit.cpp
4+
exit.cpp
55
io.cpp
66
HDRS
77
io.h
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
//===---------- GPU implementation of a quick exit function -----*- C++ -*-===//
1+
//===------------- GPU implementation of an exit function -------*- C++ -*-===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "src/__support/OSUtil/quick_exit.h"
9+
#include "src/__support/OSUtil/exit.h"
1010

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

14-
namespace LIBC_NAMESPACE {
14+
namespace LIBC_NAMESPACE::internal {
1515

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

28-
} // namespace LIBC_NAMESPACE
28+
} // namespace LIBC_NAMESPACE::internal

libc/src/__support/OSUtil/linux/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ add_subdirectory(${LIBC_TARGET_ARCHITECTURE})
77
add_object_library(
88
linux_util
99
SRCS
10-
quick_exit.cpp
10+
exit.cpp
1111
HDRS
1212
io.h
1313
syscall.h
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===---------- Linux implementation of a quick exit function ---*- C++ -*-===//
1+
//===------------ Linux implementation of an exit function ------*- C++ -*-===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
@@ -10,19 +10,19 @@
1010
#include "syscall.h" // For internal syscall function.
1111
#include <sys/syscall.h> // For syscall numbers.
1212

13-
namespace LIBC_NAMESPACE {
13+
namespace LIBC_NAMESPACE::internal {
1414

1515
// mark as no_stack_protector for x86 since TLS can be torn down before calling
16-
// quick_exit so that the stack protector canary cannot be loaded.
16+
// exit so that the stack protector canary cannot be loaded.
1717
#ifdef LIBC_TARGET_ARCH_IS_X86
18-
__attribute__((no_stack_protector))
18+
[[clang::no_stack_protector]]
1919
#endif
20-
__attribute__((noreturn)) void
21-
quick_exit(int status) {
20+
[[noreturn]] void
21+
exit(int status) {
2222
for (;;) {
2323
LIBC_NAMESPACE::syscall_impl<long>(SYS_exit_group, status);
2424
LIBC_NAMESPACE::syscall_impl<long>(SYS_exit, status);
2525
}
2626
}
2727

28-
} // namespace LIBC_NAMESPACE
28+
} // namespace LIBC_NAMESPACE::internal

libc/src/__support/libc_assert.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020

2121
#else // Not LIBC_COPT_USE_C_ASSERT
2222

23+
#include "src/__support/OSUtil/exit.h"
2324
#include "src/__support/OSUtil/io.h"
24-
#include "src/__support/OSUtil/quick_exit.h"
2525
#include "src/__support/integer_to_string.h"
2626
#include "src/__support/macros/attributes.h" // For LIBC_INLINE
2727

@@ -51,7 +51,7 @@ LIBC_INLINE void report_assertion_failure(const char *assertion,
5151

5252
// The public "assert" macro calls abort on failure. Should it be same here?
5353
// The libc internal assert can fire from anywhere inside the libc. So, to
54-
// avoid potential chicken-and-egg problems, it is simple to do a quick_exit
54+
// avoid potential chicken-and-egg problems, it is simple to do an exit
5555
// on assertion failure instead of calling abort. We also don't want to use
5656
// __builtin_trap as it could potentially be implemented using illegal
5757
// instructions which can be very misleading when debugging.
@@ -76,7 +76,7 @@ LIBC_INLINE void report_assertion_failure(const char *assertion,
7676
"' in function: '"); \
7777
LIBC_NAMESPACE::write_to_stderr(__PRETTY_FUNCTION__); \
7878
LIBC_NAMESPACE::write_to_stderr("'\n"); \
79-
LIBC_NAMESPACE::quick_exit(0xFF); \
79+
LIBC_NAMESPACE::internal::exit(0xFF); \
8080
} \
8181
} while (false)
8282
#endif // NDEBUG

libc/src/stdlib/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ add_entrypoint_object(
4242
libc.src.__support.str_to_integer
4343
)
4444

45+
add_entrypoint_object(
46+
quick_exit
47+
SRCS
48+
quick_exit.cpp
49+
HDRS
50+
quick_exit.h
51+
DEPENDS
52+
libc.src.__support.OSUtil.osutil
53+
)
54+
4555
add_entrypoint_object(
4656
getenv
4757
SRCS

libc/src/stdlib/_Exit.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "src/__support/OSUtil/quick_exit.h"
9+
#include "src/__support/OSUtil/exit.h"
1010
#include "src/__support/common.h"
1111

1212
#include "src/stdlib/_Exit.h"
1313

1414
namespace LIBC_NAMESPACE {
1515

1616
[[noreturn]] LLVM_LIBC_FUNCTION(void, _Exit, (int status)) {
17-
quick_exit(status);
17+
internal::exit(status);
1818
}
1919

2020
} // namespace LIBC_NAMESPACE

libc/src/stdlib/exit.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "src/stdlib/exit.h"
10-
#include "src/__support/OSUtil/quick_exit.h"
10+
#include "src/__support/OSUtil/exit.h"
1111
#include "src/__support/common.h"
1212

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

1717
[[noreturn]] LLVM_LIBC_FUNCTION(void, exit, (int status)) {
1818
__cxa_finalize(nullptr);
19-
quick_exit(status);
19+
internal::exit(status);
2020
}
2121

2222
} // namespace LIBC_NAMESPACE

libc/src/stdlib/quick_exit.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- Implementation of quick_exit --------------------------------------===//
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/stdlib/quick_exit.h"
10+
#include "src/__support/OSUtil/exit.h"
11+
#include "src/__support/common.h"
12+
13+
// extern "C" void __cxa_finalize(void *);
14+
15+
namespace LIBC_NAMESPACE {
16+
17+
[[noreturn]] LLVM_LIBC_FUNCTION(void, quick_exit, (int status)) {
18+
// __cxa_finalize(nullptr);
19+
internal::exit(status);
20+
}
21+
22+
} // namespace LIBC_NAMESPACE
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
//===---------- Implementation of a quick exit function ---------*- C++ -*-===//
1+
//===-- Implementation header for quick_exit --------------------*- C++ -*-===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_QUICK_EXIT_H
10-
#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_QUICK_EXIT_H
9+
#ifndef LLVM_LIBC_SRC_STDLIB_QUICK_EXIT_H
10+
#define LLVM_LIBC_SRC_STDLIB_QUICK_EXIT_H
1111

1212
namespace LIBC_NAMESPACE {
1313

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

16-
}
16+
} // namespace LIBC_NAMESPACE
1717

18-
#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_QUICK_EXIT_H
18+
#endif // LLVM_LIBC_SRC_STDLIB_QUICK_EXIT_H

libc/src/unistd/_exit.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "src/unistd/_exit.h"
10-
#include "src/__support/OSUtil/quick_exit.h"
10+
#include "src/__support/OSUtil/exit.h"
1111
#include "src/__support/common.h"
1212

1313
namespace LIBC_NAMESPACE {
1414

1515
[[noreturn]] LLVM_LIBC_FUNCTION(void, _exit, (int status)) {
16-
quick_exit(status);
16+
internal::exit(status);
1717
}
1818

1919
} // namespace LIBC_NAMESPACE

libc/test/IntegrationTest/test.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,40 @@
99
#ifndef LLVM_LIBC_UTILS_INTEGRATION_TEST_TEST_H
1010
#define LLVM_LIBC_UTILS_INTEGRATION_TEST_TEST_H
1111

12+
#include "src/__support/OSUtil/exit.h"
1213
#include "src/__support/OSUtil/io.h"
13-
#include "src/__support/OSUtil/quick_exit.h"
1414

1515
#define __AS_STRING(val) #val
1616
#define __CHECK_TRUE(file, line, val, should_exit) \
1717
if (!(val)) { \
1818
LIBC_NAMESPACE::write_to_stderr(file ":" __AS_STRING( \
1919
line) ": Expected '" #val "' to be true, but is false\n"); \
2020
if (should_exit) \
21-
LIBC_NAMESPACE::quick_exit(127); \
21+
LIBC_NAMESPACE::internal::exit(127); \
2222
}
2323

2424
#define __CHECK_FALSE(file, line, val, should_exit) \
2525
if (val) { \
2626
LIBC_NAMESPACE::write_to_stderr(file ":" __AS_STRING( \
2727
line) ": Expected '" #val "' to be false, but is true\n"); \
2828
if (should_exit) \
29-
LIBC_NAMESPACE::quick_exit(127); \
29+
LIBC_NAMESPACE::internal::exit(127); \
3030
}
3131

3232
#define __CHECK_EQ(file, line, val1, val2, should_exit) \
3333
if ((val1) != (val2)) { \
3434
LIBC_NAMESPACE::write_to_stderr(file ":" __AS_STRING( \
3535
line) ": Expected '" #val1 "' to be equal to '" #val2 "'\n"); \
3636
if (should_exit) \
37-
LIBC_NAMESPACE::quick_exit(127); \
37+
LIBC_NAMESPACE::internal::exit(127); \
3838
}
3939

4040
#define __CHECK_NE(file, line, val1, val2, should_exit) \
4141
if ((val1) == (val2)) { \
4242
LIBC_NAMESPACE::write_to_stderr(file ":" __AS_STRING( \
4343
line) ": Expected '" #val1 "' to not be equal to '" #val2 "'\n"); \
4444
if (should_exit) \
45-
LIBC_NAMESPACE::quick_exit(127); \
45+
LIBC_NAMESPACE::internal::exit(127); \
4646
}
4747

4848
////////////////////////////////////////////////////////////////////////////////

libc/test/src/stdlib/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,19 @@ if(LLVM_LIBC_FULL_BUILD)
373373
libc.src.signal.raise
374374
)
375375

376+
add_libc_test(
377+
quick_exit_test
378+
# The EXPECT_EXITS test is only availible for unit tests.
379+
UNIT_TEST_ONLY
380+
SUITE
381+
libc-stdlib-tests
382+
SRCS
383+
quick_exit_test.cpp
384+
DEPENDS
385+
libc.include.stdlib
386+
libc.src.stdlib.quick_exit
387+
)
388+
376389
# Only the GPU has an in-tree 'malloc' implementation.
377390
if(LIBC_TARGET_OS_IS_GPU)
378391
add_libc_test(
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//===-- Unittests for quick_exit ------------------------------------------===//
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/stdlib/exit.h"
10+
#include "src/stdlib/quick_exit.h"
11+
#include "test/UnitTest/Test.h"
12+
13+
TEST(LlvmLibcStdlib, quick_exit) {
14+
EXPECT_EXITS([] { LIBC_NAMESPACE::quick_exit(1); }, 1);
15+
EXPECT_EXITS([] { LIBC_NAMESPACE::quick_exit(65); }, 65);
16+
}

utils/bazel/llvm-project-overlay/libc/BUILD.bazel

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ libc_support_library(
641641
":__support_integer_to_string",
642642
":__support_macros_attributes",
643643
":__support_osutil_io",
644-
":__support_osutil_quick_exit",
644+
":__support_osutil_exit",
645645
],
646646
)
647647

@@ -1073,9 +1073,9 @@ libc_support_library(
10731073
)
10741074

10751075
libc_support_library(
1076-
name = "__support_osutil_quick_exit",
1077-
srcs = ["src/__support/OSUtil/linux/quick_exit.cpp"],
1078-
hdrs = ["src/__support/OSUtil/quick_exit.h"],
1076+
name = "__support_osutil_exit",
1077+
srcs = ["src/__support/OSUtil/linux/exit.cpp"],
1078+
hdrs = ["src/__support/OSUtil/exit.h"],
10791079
target_compatible_with = select({
10801080
"@platforms//os:linux": [],
10811081
"//conditions:default": ["@platforms//:incompatible"],

0 commit comments

Comments
 (0)