-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[libc] ensure tls dtors are called in main thread #133641
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
SchrodingerZhu
merged 7 commits into
llvm:main
from
SchrodingerZhu:libc/fix-tls-dtor-main
Sep 3, 2025
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
175ae9a
[libc] ensure tls dtors are called in main thread
SchrodingerZhu 0836f08
address CR
SchrodingerZhu a3bd867
remove extra check
SchrodingerZhu 517934a
revert
SchrodingerZhu eaec6c9
comment
SchrodingerZhu 77793f4
comment
SchrodingerZhu c716c00
Merge branch 'main' into libc/fix-tls-dtor-main
SchrodingerZhu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,7 +15,17 @@ namespace LIBC_NAMESPACE_DECL { | |
|
|
||
| extern "C" void __cxa_finalize(void *); | ||
|
|
||
| // exit needs to clean up TLS and call associated destructors. | ||
| // TODO: Strictly speaking, it is not valid to call exit in overlay mode | ||
| // as we have no way to ensure system libc will call the TLS destructors. | ||
| // We should run exit related tests in hermetic mode but this is currently | ||
| // blocked by https://github.com/llvm/llvm-project/issues/133925. | ||
| extern "C" [[gnu::weak]] void __cxa_thread_finalize(); | ||
|
|
||
| // TODO: use recursive mutex to protect this routine. | ||
| [[noreturn]] LLVM_LIBC_FUNCTION(void, exit, (int status)) { | ||
| if (__cxa_thread_finalize) | ||
|
||
| __cxa_thread_finalize(); | ||
| __cxa_finalize(nullptr); | ||
| internal::exit(status); | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
libc/test/integration/src/__support/threads/double_exit_test.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| //===-- Test handling of thread local data --------------------------------===// | ||
| // | ||
| // 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/threads/thread.h" | ||
| #include "src/stdlib/exit.h" | ||
| #include "test/IntegrationTest/test.h" | ||
|
|
||
| extern "C" { | ||
| [[gnu::weak]] | ||
| void *__dso_handle = nullptr; | ||
| int __cxa_thread_atexit_impl(void (*func)(void *), void *arg, void *dso); | ||
| } | ||
|
|
||
| int call_num = 0; | ||
|
|
||
| [[gnu::destructor]] | ||
| void check() { | ||
| // This destructor should be called only once. | ||
| if (call_num != 1) | ||
| __builtin_trap(); | ||
| } | ||
|
|
||
| TEST_MAIN() { | ||
| __cxa_thread_atexit_impl([](void *) { LIBC_NAMESPACE::exit(0); }, nullptr, | ||
| __dso_handle); | ||
| __cxa_thread_atexit_impl([](void *) { ++call_num; }, nullptr, __dso_handle); | ||
| LIBC_NAMESPACE::exit(1); | ||
| } |
30 changes: 30 additions & 0 deletions
30
libc/test/integration/src/__support/threads/main_exit_test.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| //===-- Test handling of thread local data --------------------------------===// | ||
| // | ||
| // 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/threads/thread.h" | ||
| #include "test/IntegrationTest/test.h" | ||
|
|
||
| bool called = false; | ||
|
|
||
| extern "C" { | ||
| [[gnu::weak]] | ||
| void *__dso_handle = nullptr; | ||
| int __cxa_thread_atexit_impl(void (*func)(void *), void *arg, void *dso); | ||
| } | ||
|
|
||
| [[gnu::destructor]] | ||
| void destructor() { | ||
| if (!called) | ||
| __builtin_trap(); | ||
| } | ||
|
|
||
| TEST_MAIN() { | ||
| __cxa_thread_atexit_impl([](void *) { called = true; }, nullptr, | ||
| __dso_handle); | ||
| return 0; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was wondering earlier if it would be possible to just make these routines lock free, since I'm pretty sure it's an append-only data structure, pretty trivial to, I could probably add that functionality to the existing blockstore.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAIK, the weak symbol approach is also used in __cxa_finalize (for tearing down TLS).
I remember the reason is that some code is supposed to be used in overlay mode.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are possibilities but I will need to check. The semantic of recursively adding exit hook or adding exit hook in parallel of running the hook is a grey area in the specification. We will need to work out the details.