Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Switch libunwind detection to using weak symbols #9479

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions crates/wasmtime/src/runtime/vm/helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#undef _FORTIFY_SOURCE

#include <setjmp.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

Expand Down Expand Up @@ -116,3 +117,14 @@ __attribute__((weak))
struct JITDescriptor *VERSIONED_SYMBOL(wasmtime_jit_debug_descriptor)() {
return &__jit_debug_descriptor;
}

// For more information about this see `unix/unwind.rs` and the
// `using_libunwind` function. The basic idea is that weak symbols aren't stable
// in Rust so we use a bit of C to work around that.
#ifndef CFG_TARGET_OS_windows
__attribute__((weak)) extern void __unw_add_dynamic_fde();

bool VERSIONED_SYMBOL(wasmtime_using_libunwind)() {
return __unw_add_dynamic_fde != NULL;
}
#endif
44 changes: 6 additions & 38 deletions crates/wasmtime/src/runtime/vm/sys/unix/unwind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

use crate::prelude::*;
use crate::runtime::vm::SendSyncPtr;
use core::ptr::{self, NonNull};
use core::sync::atomic::{AtomicUsize, Ordering::Relaxed};
use core::ptr::NonNull;

/// Represents a registration of function unwind information for System V ABI.
pub struct UnwindRegistration {
Expand All @@ -14,6 +13,8 @@ extern "C" {
// libunwind import
fn __register_frame(fde: *const u8);
fn __deregister_frame(fde: *const u8);
#[wasmtime_versioned_export_macros::versioned_link]
fn wasmtime_using_libunwind() -> bool;
}

/// There are two primary unwinders on Unix platforms: libunwind and libgcc.
Expand All @@ -33,43 +34,10 @@ extern "C" {
/// https://www.nongnu.org/libunwind/ but that doesn't appear to have
/// `__register_frame` so I don't think that interacts with this.
fn using_libunwind() -> bool {
static USING_LIBUNWIND: AtomicUsize = AtomicUsize::new(LIBUNWIND_UNKNOWN);

const LIBUNWIND_UNKNOWN: usize = 0;
const LIBUNWIND_YES: usize = 1;
const LIBUNWIND_NO: usize = 2;

// On macOS the libgcc interface is never used so libunwind is always used.
if cfg!(target_os = "macos") {
return true;
}

// On other platforms the unwinder can vary. Sometimes the unwinder is
// selected at build time and sometimes it differs at build time and runtime
// (or at least I think that's possible). Fall back to a `libc::dlsym` to
// figure out what we're using and branch based on that.
//
// Note that the result of `libc::dlsym` is cached to only look this up
// once.
match USING_LIBUNWIND.load(Relaxed) {
LIBUNWIND_YES => true,
LIBUNWIND_NO => false,
LIBUNWIND_UNKNOWN => {
let looks_like_libunwind = unsafe {
!libc::dlsym(ptr::null_mut(), c"__unw_add_dynamic_fde".as_ptr()).is_null()
};
USING_LIBUNWIND.store(
if looks_like_libunwind {
LIBUNWIND_YES
} else {
LIBUNWIND_NO
},
Relaxed,
);
looks_like_libunwind
}
_ => unreachable!(),
}
// Otherwise delegate to `helpers.c` since weak symbols can't be used from
// Rust at this time.
cfg!(target_os = "macos") || unsafe { wasmtime_using_libunwind() }
}

impl UnwindRegistration {
Expand Down