From 6fb8253a4ce47482112572a997d42ab324779102 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 6 Jan 2025 15:56:15 -0800 Subject: [PATCH] Fix linkage issues --- crates/wasmtime/src/engine.rs | 2 ++ crates/wasmtime/src/runtime/vm/debug_builtins.rs | 15 +++++++++++++++ crates/wasmtime/src/runtime/vm/helpers.c | 11 +++++++++++ 3 files changed, 28 insertions(+) diff --git a/crates/wasmtime/src/engine.rs b/crates/wasmtime/src/engine.rs index 5575e8c12b3a..601062327a52 100644 --- a/crates/wasmtime/src/engine.rs +++ b/crates/wasmtime/src/engine.rs @@ -101,6 +101,8 @@ impl Engine { // handlers, etc. #[cfg(all(feature = "signals-based-traps", not(miri)))] crate::runtime::vm::init_traps(config.macos_use_mach_ports); + #[cfg(feature = "debug-builtins")] + crate::runtime::vm::debug_builtins::init(); } #[cfg(any(feature = "cranelift", feature = "winch"))] diff --git a/crates/wasmtime/src/runtime/vm/debug_builtins.rs b/crates/wasmtime/src/runtime/vm/debug_builtins.rs index 65c37e971949..cf5254423e6d 100644 --- a/crates/wasmtime/src/runtime/vm/debug_builtins.rs +++ b/crates/wasmtime/src/runtime/vm/debug_builtins.rs @@ -33,3 +33,18 @@ pub unsafe extern "C" fn set_vmctx_memory(vmctx_ptr: *mut VMContext) { // TODO multi-memory VMCTX_AND_MEMORY = (vmctx_ptr, 0); } + +/// A bit of a hack around various linkage things. The goal here is to force the +/// `wasmtime_*` symbols defined in `helpers.c` to actually get exported. That +/// means they need to be referenced for the linker to include them which is +/// what this function does with trickery in C. +pub fn init() { + extern "C" { + #[wasmtime_versioned_export_macros::versioned_link] + fn wasmtime_debug_builtins_init(); + } + + unsafe { + wasmtime_debug_builtins_init(); + } +} diff --git a/crates/wasmtime/src/runtime/vm/helpers.c b/crates/wasmtime/src/runtime/vm/helpers.c index 2ee3ac92e766..b35173405ca3 100644 --- a/crates/wasmtime/src/runtime/vm/helpers.c +++ b/crates/wasmtime/src/runtime/vm/helpers.c @@ -96,6 +96,17 @@ void VERSIONED_SYMBOL(set_vmctx_memory)(void *); DEBUG_BUILTIN_EXPORT void VERSIONED_SYMBOL(wasmtime_set_vmctx_memory)(void *p) { VERSIONED_SYMBOL(set_vmctx_memory)(p); } + +// Helper symbol called from Rust to force the above two functions to not get +// stripped by the linker. +void VERSIONED_SYMBOL(wasmtime_debug_builtins_init)() { +#ifndef CFG_TARGET_OS_windows + void *volatile p; + p = (void *)&VERSIONED_SYMBOL(wasmtime_resolve_vmctx_memory_ptr); + p = (void *)&VERSIONED_SYMBOL(wasmtime_set_vmctx_memory); + (void)p; +#endif +} #endif // FEATURE_DEBUG_BUILTINS // For more information about this see `unix/unwind.rs` and the