pub struct VMOpaqueContext { /* private fields */ }
Expand description

An “opaque” version of VMContext which must be explicitly casted to a target context.

This context is used to represent that contexts specified in VMCallerCheckedAnyfunc can have any type and don’t have an implicit structure. Neither wasmtime nor cranelift-generated code can rely on the structure of an opaque context in general and only the code which configured the context is able to rely on a particular structure. This is because the context pointer configured for VMCallerCheckedAnyfunc is guaranteed to be the first parameter passed.

Note that Wasmtime currently has a layout where all contexts that are casted to an opaque context start with a 32-bit “magic” which can be used in debug mode to debug-assert that the casts here are correct and have at least a little protection against incorrect casts.

Implementations§

Helper function to clearly indicate that casts are desired.

Examples found in repository?
src/instance.rs (line 511)
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
    fn construct_anyfunc(
        &mut self,
        index: FuncIndex,
        sig: SignatureIndex,
        into: *mut VMCallerCheckedAnyfunc,
    ) {
        let type_index = unsafe {
            let base: *const VMSharedSignatureIndex =
                *self.vmctx_plus_offset(self.offsets().vmctx_signature_ids_array());
            *base.add(sig.index())
        };

        let (func_ptr, vmctx) = if let Some(def_index) = self.module().defined_func_index(index) {
            (
                self.runtime_info.function(def_index),
                VMOpaqueContext::from_vmcontext(self.vmctx_ptr()),
            )
        } else {
            let import = self.imported_function(index);
            (import.body.as_ptr(), import.vmctx)
        };

        // Safety: we have a `&mut self`, so we have exclusive access
        // to this Instance.
        unsafe {
            *into = VMCallerCheckedAnyfunc {
                vmctx,
                type_index,
                func_ptr: NonNull::new(func_ptr).expect("Non-null function pointer"),
            };
        }
    }

Helper function to clearly indicate that casts are desired.

Examples found in repository?
src/vmcontext/vm_host_func_context.rs (line 56)
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
    pub unsafe fn new(
        host_func: NonNull<VMFunctionBody>,
        signature: VMSharedSignatureIndex,
        host_state: Box<dyn Any + Send + Sync>,
    ) -> Box<VMHostFuncContext> {
        let wasm_to_host_trampoline = VMCallerCheckedAnyfunc {
            func_ptr: NonNull::new(crate::trampolines::wasm_to_host_trampoline as _).unwrap(),
            type_index: signature,
            vmctx: ptr::null_mut(),
        };
        let mut ctx = Box::new(VMHostFuncContext {
            magic: wasmtime_environ::VM_HOST_FUNC_MAGIC,
            host_func,
            wasm_to_host_trampoline,
            host_state,
        });
        ctx.wasm_to_host_trampoline.vmctx =
            VMOpaqueContext::from_vm_host_func_context(&*ctx as *const _ as *mut _);
        ctx
    }

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.