Skip to content

Commit

Permalink
Avoid __cxa_thread_atexit_impl on Emscripten
Browse files Browse the repository at this point in the history
 - Fixes rust-lang#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
  • Loading branch information
RReverser authored Jan 12, 2023
1 parent 222d1ff commit 6155b9a
Showing 1 changed file with 2 additions and 8 deletions.
10 changes: 2 additions & 8 deletions library/std/src/sys/unix/thread_local_dtor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@
// Note, however, that we run on lots older linuxes, as well as cross
// compiling from a newer linux to an older linux, so we also have a
// fallback implementation to use as well.
#[cfg(any(
target_os = "linux",
target_os = "fuchsia",
target_os = "redox",
target_os = "emscripten"
))]
#[cfg_attr(target_family = "wasm", allow(unused))] // might remain unused depending on target details (e.g. wasm32-unknown-emscripten)
#[cfg(any(target_os = "linux", target_os = "fuchsia", target_os = "redox"))]
pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
use crate::mem;
use crate::sys_common::thread_local_dtor::register_dtor_fallback;
Expand Down Expand Up @@ -94,7 +88,7 @@ pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
}
}

#[cfg(any(target_os = "vxworks", target_os = "horizon"))]
#[cfg(any(target_os = "vxworks", target_os = "horizon", target_os = "emscripten"))]
pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
use crate::sys_common::thread_local_dtor::register_dtor_fallback;
register_dtor_fallback(t, dtor);
Expand Down

0 comments on commit 6155b9a

Please sign in to comment.