Skip to content

Commit c443d6a

Browse files
[libc] ensure tls dtors are called in main thread
1 parent eba3734 commit c443d6a

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

libc/src/stdlib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,7 @@ add_entrypoint_object(
598598
CXX_STANDARD
599599
20 # For constinit
600600
DEPENDS
601+
libc.src.__support.threads.thread
601602
.exit_handler
602603
)
603604

libc/src/stdlib/atexit.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "hdr/types/atexithandler_t.h"
1111
#include "src/__support/common.h"
1212
#include "src/__support/macros/config.h"
13+
#include "src/__support/threads/thread.h"
1314
#include "src/stdlib/exit_handler.h"
1415

1516
namespace LIBC_NAMESPACE_DECL {
@@ -26,6 +27,7 @@ int __cxa_atexit(AtExitCallback *callback, void *payload, void *) {
2627

2728
void __cxa_finalize(void *dso) {
2829
if (!dso) {
30+
internal::call_atexit_callbacks(self.attrib);
2931
call_exit_callbacks(atexit_callbacks);
3032
if (teardown_main_tls)
3133
teardown_main_tls();

libc/test/integration/src/__support/threads/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,13 @@ add_integration_test(
2525
DEPENDS
2626
libc.src.__support.threads.thread
2727
)
28+
29+
add_integration_test(
30+
main_exit_test
31+
SUITE
32+
libc-support-threads-integration-tests
33+
SRCS
34+
main_exit_test.cpp
35+
DEPENDS
36+
libc.src.__support.threads.thread
37+
)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//===-- Test handling of thread local data --------------------------------===//
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/__support/threads/thread.h"
10+
#include "test/IntegrationTest/test.h"
11+
12+
bool called = false;
13+
14+
extern "C" {
15+
[[gnu::weak]]
16+
void *__dso_handle = nullptr;
17+
18+
int __cxa_thread_atexit_impl(void (*func)(void *), void *arg, void *dso);
19+
}
20+
21+
[[gnu::destructor]]
22+
void destructor() {
23+
if (!called)
24+
__builtin_trap();
25+
}
26+
27+
TEST_MAIN() {
28+
__cxa_thread_atexit_impl([](void *) { called = true; }, nullptr,
29+
__dso_handle);
30+
return 0;
31+
}

0 commit comments

Comments
 (0)