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 leaking InstanceHandles 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 a VMExternRef reference, convert it into a proper VMExternRef with VMExternRef::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 proper VMExternRef!

    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

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 memory.grow for locally-defined 32-bit memories.

Implementation of memory.atomic.notify for locally defined memories.

Implementation of memory.atomic.wait32 for locally defined memories.

Implementation of memory.atomic.wait64 for locally defined memories.

Implementation of memory.copy for locally defined memories.

Implementation of memory.fill for locally defined memories.

Implementation of memory.init.

Hook for when an instance observes that the epoch has changed.

Hook for when an instance runs out of fuel.

Implementation of ref.func.

Implementation of table.copy.

Implementation of table.fill.

Returns a table entry after lazily initializing it.

Implementation of table.grow.

Implementation of table.init.

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