[libc++abi] Define __cxa_thread_atexit on platforms that need it - #186054
[libc++abi] Define __cxa_thread_atexit on platforms that need it#186054whitequark wants to merge 9 commits into
__cxa_thread_atexit on platforms that need it#186054Conversation
|
@llvm/pr-subscribers-libcxxabi Author: Catherine (whitequark) ChangesThis was done in the commit 3c100d5 with the description "Enable -Wmissing-prototypes" which seems incongruent to me. Since then it's made its way into a release and broke the use of // repro.cc
struct c { ~c() {} };
thread_local c v;
int main() { (void)v; }$ ./wasi-sdk-31.0-x86_64-linux/bin/clang++ repro.cc
wasm-ld: error: /tmp/repro-dd1ad7.o: undefined symbol: __cxa_thread_atexit
clang++: error: linker command failed with exit code 1 (use -v to see invocation)Full diff: https://github.com/llvm/llvm-project/pull/186054.diff 1 Files Affected:
diff --git a/libcxxabi/src/cxa_thread_atexit.cpp b/libcxxabi/src/cxa_thread_atexit.cpp
index 402a52c741012..370e76344e89c 100644
--- a/libcxxabi/src/cxa_thread_atexit.cpp
+++ b/libcxxabi/src/cxa_thread_atexit.cpp
@@ -106,7 +106,6 @@ namespace {
#endif // HAVE___CXA_THREAD_ATEXIT_IMPL
-#if defined(__linux__) || defined(__Fuchsia__)
extern "C" {
_LIBCXXABI_FUNC_VIS int __cxa_thread_atexit(Dtor dtor, void* obj, void* dso_symbol) throw() {
@@ -142,5 +141,4 @@ extern "C" {
#endif // HAVE___CXA_THREAD_ATEXIT_IMPL
}
} // extern "C"
-#endif // defined(__linux__) || defined(__Fuchsia__)
} // namespace __cxxabiv1
|
|
cc @philnik777; could you explain a bit more about the rationale for that |
You can test this locally with the following command:git-clang-format --diff origin/main HEAD --extensions h,cpp -- libcxxabi/include/__cxxabi_config.h libcxxabi/include/cxxabi.h libcxxabi/src/cxa_thread_atexit.cpp --diff_from_common_commit
View the diff from clang-format here.diff --git a/libcxxabi/include/cxxabi.h b/libcxxabi/include/cxxabi.h
index 5d0513c4c..d9d56f8ec 100644
--- a/libcxxabi/include/cxxabi.h
+++ b/libcxxabi/include/cxxabi.h
@@ -179,7 +179,7 @@ __cxa_decrement_exception_refcount(void *primary_exception) _LIBCXXABI_NOEXCEPT;
extern _LIBCXXABI_FUNC_VIS bool __cxa_uncaught_exception() _LIBCXXABI_NOEXCEPT;
extern _LIBCXXABI_FUNC_VIS unsigned int __cxa_uncaught_exceptions() _LIBCXXABI_NOEXCEPT;
-#if _LIBCXXABI_DEFINE_THREAD_ATEXIT
+# if _LIBCXXABI_DEFINE_THREAD_ATEXIT
// Register a thread local destructor. This is not yet an official part of the Itanium ABI.
// https://sourceware.org/glibc/wiki/Destructor%20support%20for%20thread_local%20variables
extern _LIBCXXABI_FUNC_VIS int __cxa_thread_atexit(void (*)(void *), void *,
diff --git a/libcxxabi/src/cxa_thread_atexit.cpp b/libcxxabi/src/cxa_thread_atexit.cpp
index 366480f94..309858a5a 100644
--- a/libcxxabi/src/cxa_thread_atexit.cpp
+++ b/libcxxabi/src/cxa_thread_atexit.cpp
@@ -10,14 +10,14 @@
#if !defined(_LIBCXXABI_HAS_NO_THREADS) && _LIBCXXABI_DEFINE_THREAD_ATEXIT
-#include <__thread/support.h>
-#include <stdlib.h>
+# include <__thread/support.h>
+# include <stdlib.h>
-#include "abort_message.h"
+# include "abort_message.h"
-#if defined(__ELF__) && defined(_LIBCXXABI_LINK_PTHREAD_LIB)
-#pragma comment(lib, "pthread")
-#endif
+# if defined(__ELF__) && defined(_LIBCXXABI_LINK_PTHREAD_LIB)
+# pragma comment(lib, "pthread")
+# endif
namespace __cxxabiv1 {
|
d263f09 to
f71fdfc
Compare
Pulls in llvm/llvm-project#186054 and adds a regression test which previously failed. Closes WebAssembly#610
Pulls in llvm/llvm-project#186054 and adds a regression test which previously failed. Closes #610
1924b87 to
a8cc009
Compare
|
FWIW (just trying to build clang 22 on a FreeBSD 10.2, whose stock clang is a 3.4.1…), I'm not really fond of the addition of every OS to the Following up my comment on #116261, I successfully built a full 22.1.2 ecosystem (llvm, clang, libc++, libc++abi, openmp, clang-rt, lld, lldb, flang, flang-rt), both on FreeBSD 10.2 and 15.0 by removing the
Thus the warning (that the CI errors) on non-Linux Unix platforms. So, with the example of my FreeBSD 10.2 and 15.0 sandboxes, wouldn't a cleaner solution be to |
|
My position is that right now an architecture is broken. We should fix that first and then discuss a cleaner solution later. |
|
@whitequark wrote:
I'm semi-agreeing with that: |
philnik777
left a comment
There was a problem hiding this comment.
This change was based on the fact that cxxabi.h guards this function behind #if defined(__linux__) || defined(__Fuchsia__). If this is wrong, that header needs to be updated as well. However, it's not clear to me at all which platforms actually expect this function if it's not declared on platforms that use it (probably through compiler magic?).
|
@philnik777 wrote:
Thanks for your feedback. This assymetry introduced in 2014 was what puzzled me in my previous comment.
That's not a very satisfying explanation… IMO removing it entirely (as you did @whitequark in your initial commit), or at least making the declaration in Note that if (LIBCXXABI_ENABLE_THREADS AND (UNIX OR FUCHSIA) AND NOT (APPLE OR CYGWIN)
AND NOT ("${CMAKE_SYSTEM_NAME}" MATCHES "AIX"))
list(APPEND LIBCXXABI_SOURCES
cxa_thread_atexit.cpp
)
endif()… So perhaps the complete solution would be to:
So for now we know that it is necessary on: |
This was done in the commit 3c100d5 with the description "Enable -Wmissing-prototypes" which seems incongruent to me. Since then it's made its way into a release and broke the use of `thread_local` variables with destructors on Wasm/WASI: ```cc // repro.cc struct c { ~c() {} }; thread_local c v; int main() { (void)v; } ``` ```console $ ./wasi-sdk-31.0-x86_64-linux/bin/clang++ repro.cc wasm-ld: error: /tmp/repro-dd1ad7.o: undefined symbol: __cxa_thread_atexit clang++: error: linker command failed with exit code 1 (use -v to see invocation) ```
a8cc009 to
46301f0
Compare
|
I've updated the PR so that tests should pass now. |
|
FWIW fixing this issue 'properly' as suggested by @outtersg would also remove a need for a patch on SerenityOS that looks remarkably similar to the current state of this PR: |
|
Upon further consideration, I think we should go with an opt-out instead of an opt-in. |
|
Alright folks, sorry for the traffic on this PR, but I finally got it to a place where I think it should address the original issue, and in addition improve the status quo:
|
|
Thanks for picking it up, @ldionne! |
|
@whitequark Could you confirm whether the PR in its current form fixes the original issue you were seeing? It would be sad if my refactoring ended up not solving the original problem :) |
|
Not easily, but I'm sure @alexcrichton would be able to! |
As a drive-by, add missing _LIBCPP_TLS_DESTRUCTOR_CC annotation. This was noticed when building on Windows. It's semantically the right thing to use here but it expands to nothing on all platforms where we provide that function (for now).
__cxa_thread_atexit on Linux||Fuchsia__cxa_thread_atexit on platforms that need it
LucasChollet
left a comment
There was a problem hiding this comment.
I applied the patch locally, and it solves the issue for SerenityOS. Thanks, @ldionne!
Locally this PR in its current state looks like it works for wasi-sdk, and I've posted WebAssembly/wasi-sdk#649 for a full test across platforms too. Thanks @ldionne! |
Accounting for changes to llvm/llvm-project#186054
Commit 3c100d5 removed the definition of __cxa_thread_atexit from all platforms except Fuchsia and Linux. This broke the use of
thread_localvariables with destructors on other downstream configurations that needed the definition.This patch restores __cxa_thread_atexit on (presumably) all platforms that need it.
Old PR description (before PR was overhauled):
This was done in the commit 3c100d5 with the description "Enable -Wmissing-prototypes" which seems incongruent to me.
Since then it's made its way into a release and broke the use of
thread_localvariables with destructors on Wasm/WASI: