Skip to content

Commit 6155b9a

Browse files
authored
Avoid __cxa_thread_atexit_impl on Emscripten
- Fixes #91628. - Fixes emscripten-core/emscripten#15722. See discussion in both issues. The TL;DR is that weak linkage causes LLVM to produce broken Wasm, presumably due to pointer mismatch. The code is casting a void pointer to a function pointer with specific signature, but Wasm is very strict about function pointer compatibility, so the resulting code is invalid. Ideally LLVM should catch this earlier in the process rather than emit invalid Wasm, but it currently doesn't and this is an easy and valid fix, given that Emcripten doesn't have `__cxa_thread_atexit_impl` these days anyway. Unfortunately, I can't add a regression test as even after looking into this issue for a long time, I couldn't reproduce it with any minimal Rust example, only with extracted LLVM IR or on a large project involving Rust + C++. r? @alexcrichton
1 parent 222d1ff commit 6155b9a

File tree

1 file changed

+2
-8
lines changed

1 file changed

+2
-8
lines changed

library/std/src/sys/unix/thread_local_dtor.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,7 @@
1111
// Note, however, that we run on lots older linuxes, as well as cross
1212
// compiling from a newer linux to an older linux, so we also have a
1313
// fallback implementation to use as well.
14-
#[cfg(any(
15-
target_os = "linux",
16-
target_os = "fuchsia",
17-
target_os = "redox",
18-
target_os = "emscripten"
19-
))]
20-
#[cfg_attr(target_family = "wasm", allow(unused))] // might remain unused depending on target details (e.g. wasm32-unknown-emscripten)
14+
#[cfg(any(target_os = "linux", target_os = "fuchsia", target_os = "redox"))]
2115
pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
2216
use crate::mem;
2317
use crate::sys_common::thread_local_dtor::register_dtor_fallback;
@@ -94,7 +88,7 @@ pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
9488
}
9589
}
9690

97-
#[cfg(any(target_os = "vxworks", target_os = "horizon"))]
91+
#[cfg(any(target_os = "vxworks", target_os = "horizon", target_os = "emscripten"))]
9892
pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
9993
use crate::sys_common::thread_local_dtor::register_dtor_fallback;
10094
register_dtor_fallback(t, dtor);

0 commit comments

Comments
 (0)