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!) }
Functions
Do a GC and insert the given externref
into the
VMExternRefActivationsTable
.
Implementation of data.drop
.
Drop a VMExternRef
.
Implementation of elem.drop
.
Perform a Wasm global.get
for externref
globals.
Perform a Wasm global.set
for externref
globals.
Implementation of f32.ceil
Implementation of f32.floor
Implementation of f32.nearest
Implementation of f32.trunc
Implementation of f64.ceil
Implementation of f64.floor
Implementation of f64.nearest
Implementation of f64.trunc
Implementation of i64.ishl
Implementation of i64.sdiv
Implementation of i64.srem
Implementation of i64.sshr
Implementation of i64.udiv
Implementation of i64.urem
Implementation of i64.ushr
Implementation of memory.grow for imported 32-bit memories.
Implementation of memory.size for imported 32-bit memories.
Implementation of memory.fill
for imported memories.
Implementation of memory.grow for locally-defined 32-bit memories.
Implementation of memory.size for locally-defined 32-bit memories.
Implementation of memory.copy
for locally defined memories.
Implementation of memory.fill
for locally defined memories.
Implementation of memory.init
.
Implementation of table.copy
.
Implementation of table.fill
.
Implementation of table.grow
.
Implementation of table.init
.