Module wasmtime_runtime::libcalls
source · Expand description
Runtime library calls.
Note that Wasm compilers may sometimes perform these inline rather than calling them, particularly when CPUs have special instructions which compute them directly.
These functions are called by compiled Wasm code, and therefore must take certain care about some things:
-
They must always be
pub extern "C"
and should only contain basic, raw i32/i64/f32/f64/pointer parameters that are safe to pass across the system ABI! -
If any nested function propagates an
Err(trap)
out to the library function frame, we need to raise it. This involves some nasty and quite unsafe code under the covers! Notable, after raising the trap, drops will not be run for local variables! This can lead to things like leakingInstanceHandle
s which leads to never deallocating JIT code, instances, and modules! Therefore, always use nested blocks to ensure drops run before raising a trap:ⓘpub extern "C" fn my_lib_function(...) { let result = { // Do everything in here so drops run at the end of the block. ... }; if let Err(trap) = result { // Now we can safely raise the trap without leaking! raise_lib_trap(trap); } }
-
When receiving a raw
*mut u8
that is actually aVMExternRef
reference, convert it into a properVMExternRef
withVMExternRef::clone_from_raw
as soon as apossible. Any GC before raw pointer is converted into a reference can potentially collect the referenced object, which could lead to use after free. Avoid this by eagerly converting into a properVMExternRef
!ⓘpub unsafe extern "C" my_lib_takes_ref(raw_extern_ref: *mut u8) { // Before `clone_from_raw`, `raw_extern_ref` is potentially unrooted, // and doing GC here could lead to use after free! let my_extern_ref = if raw_extern_ref.is_null() { None } else { Some(VMExternRef::clone_from_raw(raw_extern_ref)) }; // Now that we did `clone_from_raw`, it is safe to do a GC (or do // anything else that might transitively GC, like call back into // Wasm!) }
Re-exports
pub use table_grow as table_grow_funcref;
pub use table_grow as table_grow_externref;
pub use table_fill as table_fill_funcref;
pub use table_fill as table_fill_externref;
Functions
externref
into the
VMExternRefActivationsTable
.data.drop
.VMExternRef
.elem.drop
.global.get
for externref
globals.global.set
for externref
globals.memory.atomic.notify
for locally defined memories.memory.atomic.wait32
for locally defined memories.memory.atomic.wait64
for locally defined memories.memory.copy
for locally defined memories.memory.fill
for locally defined memories.memory.init
.ref.func
.table.copy
.table.fill
.table.grow
.table.init
.