wasmtime/runtime/trampoline/
func.rs

1//! Support for a calling of an imported function.
2
3use crate::prelude::*;
4use crate::runtime::vm::{
5    Instance, StoreBox, VMArrayCallHostFuncContext, VMContext, VMOpaqueContext, VMStore,
6};
7use crate::store::InstanceId;
8use crate::type_registry::RegisteredType;
9use crate::{FuncType, ValRaw};
10use core::ptr::NonNull;
11
12struct TrampolineState<F> {
13    func: F,
14
15    // Need to keep our `VMSharedTypeIndex` registered in the engine.
16    _sig: RegisteredType,
17}
18
19/// Shim to call a host-defined function that uses the array calling convention.
20///
21/// Together with `VMArrayCallHostFuncContext`, this implements the transition
22/// from a raw, non-closure function pointer to a Rust closure that associates
23/// data and function together.
24///
25/// Also shepherds panics and traps across Wasm.
26///
27/// # Safety
28///
29/// Requires that all parameters are valid from a wasm function call and
30/// additionally that `vmctx` is backed by `VMArrayCallHostFuncContext`.
31unsafe extern "C" fn array_call_shim<F>(
32    vmctx: NonNull<VMOpaqueContext>,
33    caller_vmctx: NonNull<VMContext>,
34    values_vec: NonNull<ValRaw>,
35    values_vec_len: usize,
36) -> bool
37where
38    F: Fn(&mut dyn VMStore, InstanceId, &mut [ValRaw]) -> Result<()> + 'static,
39{
40    // SAFETY: this is an entrypoint of wasm calling a host and our parameters
41    // should reflect that making `enter_host_from_wasm` suitable. Further
42    // unsafe operations are commented below.
43    unsafe {
44        Instance::enter_host_from_wasm(caller_vmctx, |store, instance| {
45            // SAFETY: this function itself requires that the `vmctx` is valid to
46            // use here.
47            let state = {
48                let vmctx = VMArrayCallHostFuncContext::from_opaque(vmctx);
49                vmctx.as_ref().host_state()
50            };
51
52            // Double-check ourselves in debug mode, but we control
53            // the `Any` here so an unsafe downcast should also
54            // work.
55            //
56            // SAFETY: this function is only usable with `TrampolineState<F>`.
57            let state = {
58                debug_assert!(state.is::<TrampolineState<F>>());
59                &*(state as *const _ as *const TrampolineState<F>)
60            };
61            let mut values_vec = NonNull::slice_from_raw_parts(values_vec, values_vec_len);
62            // SAFETY: it's a contract of this function itself that the values
63            // provided are valid to view as a slice.
64            let values_vec = values_vec.as_mut();
65            (state.func)(store, instance, values_vec)
66        })
67    }
68}
69
70pub fn create_array_call_function<F>(
71    ft: &FuncType,
72    func: F,
73) -> Result<StoreBox<VMArrayCallHostFuncContext>>
74where
75    F: Fn(&mut dyn VMStore, InstanceId, &mut [ValRaw]) -> Result<()> + Send + Sync + 'static,
76{
77    let array_call = array_call_shim::<F>;
78
79    let sig = ft.clone().into_registered_type();
80
81    unsafe {
82        Ok(VMArrayCallHostFuncContext::new(
83            array_call,
84            sig.index(),
85            Box::new(TrampolineState { func, _sig: sig }),
86        ))
87    }
88}