Skip to content

Commit 0991cf4

Browse files
authored
Fix C++ thread locals, add a test (#611)
Pulls in llvm/llvm-project#186054 and adds a regression test which previously failed. Closes #610
1 parent 5d5c6c0 commit 0991cf4

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

cmake/wasi-sdk-sysroot.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,9 @@ function(define_libcxx_sub target target_suffix extra_target_flags extra_libdir_
330330
PATCH_COMMAND
331331
${CMAKE_COMMAND} -E chdir .. bash -c
332332
"git apply ${CMAKE_SOURCE_DIR}/src/llvm-pr-168449.patch || git apply ${CMAKE_SOURCE_DIR}/src/llvm-pr-168449.patch -R --check"
333+
COMMAND
334+
${CMAKE_COMMAND} -E chdir .. bash -c
335+
"git apply ${CMAKE_SOURCE_DIR}/src/llvm-pr-186054.patch || git apply ${CMAKE_SOURCE_DIR}/src/llvm-pr-186054.patch -R --check"
333336
)
334337
endfunction()
335338

src/llvm-pr-186054.patch

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
From f71fdfcbd6fcc7b521c74b5856ebeacdd6cf55d9 Mon Sep 17 00:00:00 2001
2+
From: Catherine <whitequark@whitequark.org>
3+
Date: Thu, 12 Mar 2026 08:19:49 +0000
4+
Subject: [PATCH] [libc++abi] Revert gating of `__cxa_thread_atexit` on
5+
Linux||Fuchsia
6+
7+
This was done in the commit 3c100d5d548d with the description
8+
"Enable -Wmissing-prototypes" which seems incongruent to me.
9+
10+
Since then it's made its way into a release and broke the use of
11+
`thread_local` variables with destructors on Wasm/WASI:
12+
13+
```cc
14+
// repro.cc
15+
struct c { ~c() {} };
16+
thread_local c v;
17+
int main() { (void)v; }
18+
```
19+
20+
```console
21+
$ ./wasi-sdk-31.0-x86_64-linux/bin/clang++ repro.cc
22+
wasm-ld: error: /tmp/repro-dd1ad7.o: undefined symbol: __cxa_thread_atexit
23+
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
24+
```
25+
---
26+
libcxxabi/src/cxa_thread_atexit.cpp | 4 +---
27+
1 file changed, 1 insertion(+), 3 deletions(-)
28+
29+
diff --git a/libcxxabi/src/cxa_thread_atexit.cpp b/libcxxabi/src/cxa_thread_atexit.cpp
30+
index 402a52c741012..1bdcb4ef192b4 100644
31+
--- a/libcxxabi/src/cxa_thread_atexit.cpp
32+
+++ b/libcxxabi/src/cxa_thread_atexit.cpp
33+
@@ -106,7 +106,6 @@ namespace {
34+
35+
#endif // HAVE___CXA_THREAD_ATEXIT_IMPL
36+
37+
-#if defined(__linux__) || defined(__Fuchsia__)
38+
extern "C" {
39+
40+
_LIBCXXABI_FUNC_VIS int __cxa_thread_atexit(Dtor dtor, void* obj, void* dso_symbol) throw() {
41+
@@ -141,6 +140,5 @@ extern "C" {
42+
}
43+
#endif // HAVE___CXA_THREAD_ATEXIT_IMPL
44+
}
45+
-} // extern "C"
46+
-#endif // defined(__linux__) || defined(__Fuchsia__)
47+
+ } // extern "C"
48+
} // namespace __cxxabiv1

tests/general/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
file(GLOB c_general_tests RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "*.c")
2-
file(GLOB cxx_general_tests RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "*.cc")
1+
file(GLOB c_general_tests RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} CONFIGURE_DEPENDS "*.c")
2+
file(GLOB cxx_general_tests RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} CONFIGURE_DEPENDS "*.cc")
33

44
set(general_tests ${c_general_tests} ${cxx_general_tests})
55

tests/general/cpp_thread_local.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
struct c { ~c() {} };
2+
thread_local c v;
3+
int main() { (void)v; }

0 commit comments

Comments
 (0)