Skip to content

Commit bcd4f10

Browse files
authored
Fix Rust integration issues with Wasmtime (#861)
* Turn an assert into a debug assert to keep panic messages out of release mode. * Fix referring to `cabi_realloc` in release mode by referencing the symbol via a more robust mechanism.
1 parent a87d27d commit bcd4f10

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

crates/guest-rust/src/lib.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,18 @@ pub mod rt {
376376
new_len: usize,
377377
) -> *mut u8;
378378
}
379+
// Force the `cabi_realloc` symbol to be referenced from here. This
380+
// function isn't ever actually used at runtime but the symbol needs
381+
// to be used here to force it to get referenced all the way through
382+
// to the linker. By the time we get to the linker this symbol will
383+
// be discarded due to it never actually being used by
384+
// `cabi_realloc` will have already been referenced at which point
385+
// its export name annotation will ensure it's exported regardless.
379386
#[cfg(feature = "realloc")]
380-
static _X: unsafe extern "C" fn(*mut u8, usize, usize, usize) -> *mut u8 = cabi_realloc;
387+
unsafe {
388+
cabi_realloc(core::ptr::null_mut(), 0, 1, 1);
389+
}
390+
unreachable!();
381391
}
382392
}
383393

@@ -549,7 +559,7 @@ pub unsafe trait RustResource: WasmResource {
549559
impl<T: WasmResource> Resource<T> {
550560
#[doc(hidden)]
551561
pub unsafe fn from_handle(handle: u32) -> Self {
552-
assert!(handle != u32::MAX);
562+
debug_assert!(handle != u32::MAX);
553563
Self {
554564
handle: AtomicU32::new(handle),
555565
_marker: marker::PhantomData,

0 commit comments

Comments
 (0)