1 use crate::error::OutOfMemory;
2 use crate::prelude::*;
3 use crate::runtime::vm::{
4     self, InterpreterRef, SendSyncPtr, StoreBox, VMArrayCallHostFuncContext,
5     VMCommonStackInformation, VMContext, VMFuncRef, VMFunctionImport, VMOpaqueContext,
6     VMStoreContext,
7 };
8 use crate::store::{Asyncness, AutoAssertNoGc, InstanceId, StoreId, StoreOpaque};
9 use crate::type_registry::RegisteredType;
10 use crate::{
11     AsContext, AsContextMut, CallHook, Engine, Extern, FuncType, Instance, ModuleExport, Ref,
12     StoreContext, StoreContextMut, Val, ValRaw, ValType,
13 };
14 use alloc::sync::Arc;
15 use core::convert::Infallible;
16 use core::ffi::c_void;
17 #[cfg(feature = "async")]
18 use core::future::Future;
19 use core::mem::{self, MaybeUninit};
20 use core::ptr::NonNull;
21 use wasmtime_environ::{PanicOnOom as _, VMSharedTypeIndex};
22 
23 /// A reference to the abstract `nofunc` heap value.
24 ///
25 /// The are no instances of `(ref nofunc)`: it is an uninhabited type.
26 ///
27 /// There is precisely one instance of `(ref null nofunc)`, aka `nullfuncref`:
28 /// the null reference.
29 ///
30 /// This `NoFunc` Rust type's sole purpose is for use with [`Func::wrap`]- and
31 /// [`Func::typed`]-style APIs for statically typing a function as taking or
32 /// returning a `(ref null nofunc)` (aka `Option<NoFunc>`) which is always
33 /// `None`.
34 ///
35 /// # Example
36 ///
37 /// ```
38 /// # use wasmtime::*;
39 /// # fn _foo() -> Result<()> {
40 /// let mut config = Config::new();
41 /// config.wasm_function_references(true);
42 /// let engine = Engine::new(&config)?;
43 ///
44 /// let module = Module::new(
45 ///     &engine,
46 ///     r#"
47 ///         (module
48 ///             (func (export "f") (param (ref null nofunc))
49 ///                 ;; If the reference is null, return.
50 ///                 local.get 0
51 ///                 ref.is_null nofunc
52 ///                 br_if 0
53 ///
54 ///                 ;; If the reference was not null (which is impossible)
55 ///                 ;; then raise a trap.
56 ///                 unreachable
57 ///             )
58 ///         )
59 ///     "#,
60 /// )?;
61 ///
62 /// let mut store = Store::new(&engine, ());
63 /// let instance = Instance::new(&mut store, &module, &[])?;
64 /// let f = instance.get_func(&mut store, "f").unwrap();
65 ///
66 /// // We can cast a `(ref null nofunc)`-taking function into a typed function that
67 /// // takes an `Option<NoFunc>` via the `Func::typed` method.
68 /// let f = f.typed::<Option<NoFunc>, ()>(&store)?;
69 ///
70 /// // We can call the typed function, passing the null `nofunc` reference.
71 /// let result = f.call(&mut store, NoFunc::null());
72 ///
73 /// // The function should not have trapped, because the reference we gave it was
74 /// // null (as it had to be, since `NoFunc` is uninhabited).
75 /// assert!(result.is_ok());
76 /// # Ok(())
77 /// # }
78 /// ```
79 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
80 pub struct NoFunc {
81     _inner: Infallible,
82 }
83 
84 impl NoFunc {
85     /// Get the null `(ref null nofunc)` (aka `nullfuncref`) reference.
86     #[inline]
87     pub fn null() -> Option<NoFunc> {
88         None
89     }
90 
91     /// Get the null `(ref null nofunc)` (aka `nullfuncref`) reference as a
92     /// [`Ref`].
93     #[inline]
94     pub fn null_ref() -> Ref {
95         Ref::Func(None)
96     }
97 
98     /// Get the null `(ref null nofunc)` (aka `nullfuncref`) reference as a
99     /// [`Val`].
100     #[inline]
101     pub fn null_val() -> Val {
102         Val::FuncRef(None)
103     }
104 }
105 
106 /// A WebAssembly function which can be called.
107 ///
108 /// This type typically represents an exported function from a WebAssembly
109 /// module instance. In this case a [`Func`] belongs to an [`Instance`] and is
110 /// loaded from there. A [`Func`] may also represent a host function as well in
111 /// some cases, too.
112 ///
113 /// Functions can be called in a few different ways, either synchronous or async
114 /// and either typed or untyped (more on this below). Note that host functions
115 /// are normally inserted directly into a [`Linker`](crate::Linker) rather than
116 /// using this directly, but both options are available.
117 ///
118 /// # `Func` and `async`
119 ///
120 /// Functions from the perspective of WebAssembly are always synchronous. You
121 /// might have an `async` function in Rust, however, which you'd like to make
122 /// available from WebAssembly. Wasmtime supports asynchronously calling
123 /// WebAssembly through native stack switching. You can get some more
124 /// information about [asynchronous configs](crate#async), but
125 /// from the perspective of `Func` it's important to know that whether or not
126 /// your [`Store`](crate::Store) is asynchronous will dictate whether you call
127 /// functions through [`Func::call`] or [`Func::call_async`] (or the typed
128 /// wrappers such as [`TypedFunc::call`] vs [`TypedFunc::call_async`]).
129 ///
130 /// # To `Func::call` or to `Func::typed().call()`
131 ///
132 /// There's a 2x2 matrix of methods to call [`Func`]. Invocations can either be
133 /// asynchronous or synchronous. They can also be statically typed or not.
134 /// Whether or not an invocation is asynchronous is indicated via the method
135 /// being `async` and [`call_async`](Func::call_async) being the entry point.
136 /// Otherwise for statically typed or not your options are:
137 ///
138 /// * Dynamically typed - if you don't statically know the signature of the
139 ///   function that you're calling you'll be using [`Func::call`] or
140 ///   [`Func::call_async`]. These functions take a variable-length slice of
141 ///   "boxed" arguments in their [`Val`] representation. Additionally the
142 ///   results are returned as an owned slice of [`Val`]. These methods are not
143 ///   optimized due to the dynamic type checks that must occur, in addition to
144 ///   some dynamic allocations for where to put all the arguments. While this
145 ///   allows you to call all possible wasm function signatures, if you're
146 ///   looking for a speedier alternative you can also use...
147 ///
148 /// * Statically typed - if you statically know the type signature of the wasm
149 ///   function you're calling, then you'll want to use the [`Func::typed`]
150 ///   method to acquire an instance of [`TypedFunc`]. This structure is static proof
151 ///   that the underlying wasm function has the ascripted type, and type
152 ///   validation is only done once up-front. The [`TypedFunc::call`] and
153 ///   [`TypedFunc::call_async`] methods are much more efficient than [`Func::call`]
154 ///   and [`Func::call_async`] because the type signature is statically known.
155 ///   This eschews runtime checks as much as possible to get into wasm as fast
156 ///   as possible.
157 ///
158 /// # Examples
159 ///
160 /// One way to get a `Func` is from an [`Instance`] after you've instantiated
161 /// it:
162 ///
163 /// ```
164 /// # use wasmtime::*;
165 /// # fn main() -> Result<()> {
166 /// let engine = Engine::default();
167 /// let module = Module::new(&engine, r#"(module (func (export "foo")))"#)?;
168 /// let mut store = Store::new(&engine, ());
169 /// let instance = Instance::new(&mut store, &module, &[])?;
170 /// let foo = instance.get_func(&mut store, "foo").expect("export wasn't a function");
171 ///
172 /// // Work with `foo` as a `Func` at this point, such as calling it
173 /// // dynamically...
174 /// match foo.call(&mut store, &[], &mut []) {
175 ///     Ok(()) => { /* ... */ }
176 ///     Err(trap) => {
177 ///         panic!("execution of `foo` resulted in a wasm trap: {}", trap);
178 ///     }
179 /// }
180 /// foo.call(&mut store, &[], &mut [])?;
181 ///
182 /// // ... or we can make a static assertion about its signature and call it.
183 /// // Our first call here can fail if the signatures don't match, and then the
184 /// // second call can fail if the function traps (like the `match` above).
185 /// let foo = foo.typed::<(), ()>(&store)?;
186 /// foo.call(&mut store, ())?;
187 /// # Ok(())
188 /// # }
189 /// ```
190 ///
191 /// You can also use the [`wrap` function](Func::wrap) to create a
192 /// `Func`
193 ///
194 /// ```
195 /// # use wasmtime::*;
196 /// # fn main() -> Result<()> {
197 /// let mut store = Store::<()>::default();
198 ///
199 /// // Create a custom `Func` which can execute arbitrary code inside of the
200 /// // closure.
201 /// let add = Func::wrap(&mut store, |a: i32, b: i32| -> i32 { a + b });
202 ///
203 /// // Next we can hook that up to a wasm module which uses it.
204 /// let module = Module::new(
205 ///     store.engine(),
206 ///     r#"
207 ///         (module
208 ///             (import "" "" (func $add (param i32 i32) (result i32)))
209 ///             (func (export "call_add_twice") (result i32)
210 ///                 i32.const 1
211 ///                 i32.const 2
212 ///                 call $add
213 ///                 i32.const 3
214 ///                 i32.const 4
215 ///                 call $add
216 ///                 i32.add))
217 ///     "#,
218 /// )?;
219 /// let instance = Instance::new(&mut store, &module, &[add.into()])?;
220 /// let call_add_twice = instance.get_typed_func::<(), i32>(&mut store, "call_add_twice")?;
221 ///
222 /// assert_eq!(call_add_twice.call(&mut store, ())?, 10);
223 /// # Ok(())
224 /// # }
225 /// ```
226 ///
227 /// Or you could also create an entirely dynamic `Func`!
228 ///
229 /// ```
230 /// # use wasmtime::*;
231 /// # fn main() -> Result<()> {
232 /// let mut store = Store::<()>::default();
233 ///
234 /// // Here we need to define the type signature of our `Double` function and
235 /// // then wrap it up in a `Func`
236 /// let double_type = wasmtime::FuncType::new(
237 ///     store.engine(),
238 ///     [wasmtime::ValType::I32].iter().cloned(),
239 ///     [wasmtime::ValType::I32].iter().cloned(),
240 /// );
241 /// let double = Func::new(&mut store, double_type, |_, params, results| {
242 ///     let mut value = params[0].unwrap_i32();
243 ///     value *= 2;
244 ///     results[0] = value.into();
245 ///     Ok(())
246 /// });
247 ///
248 /// let module = Module::new(
249 ///     store.engine(),
250 ///     r#"
251 ///         (module
252 ///             (import "" "" (func $double (param i32) (result i32)))
253 ///             (func $start
254 ///                 i32.const 1
255 ///                 call $double
256 ///                 drop)
257 ///             (start $start))
258 ///     "#,
259 /// )?;
260 /// let instance = Instance::new(&mut store, &module, &[double.into()])?;
261 /// // .. work with `instance` if necessary
262 /// # Ok(())
263 /// # }
264 /// ```
265 #[derive(Copy, Clone, Debug)]
266 #[repr(C)] // here for the C API
267 pub struct Func {
268     /// The store that the below pointer belongs to.
269     ///
270     /// It's only safe to look at the contents of the pointer below when the
271     /// `StoreOpaque` matching this id is in-scope.
272     store: StoreId,
273 
274     /// The raw `VMFuncRef`, whose lifetime is bound to the store this func
275     /// belongs to.
276     ///
277     /// Note that this field has an `unsafe_*` prefix to discourage use of it.
278     /// This is only safe to read/use if `self.store` is validated to belong to
279     /// an ambiently provided `StoreOpaque` or similar. Use the
280     /// `self.func_ref()` method instead of this field to perform this check.
281     unsafe_func_ref: SendSyncPtr<VMFuncRef>,
282 }
283 
284 // Double-check that the C representation in `extern.h` matches our in-Rust
285 // representation here in terms of size/alignment/etc.
286 const _: () = {
287     #[repr(C)]
288     struct C(u64, *mut u8);
289     assert!(core::mem::size_of::<C>() == core::mem::size_of::<Func>());
290     assert!(core::mem::align_of::<C>() == core::mem::align_of::<Func>());
291     assert!(core::mem::offset_of!(Func, store) == 0);
292 };
293 
294 macro_rules! for_each_function_signature {
295     ($mac:ident) => {
296         $mac!(0);
297         $mac!(1 A1);
298         $mac!(2 A1 A2);
299         $mac!(3 A1 A2 A3);
300         $mac!(4 A1 A2 A3 A4);
301         $mac!(5 A1 A2 A3 A4 A5);
302         $mac!(6 A1 A2 A3 A4 A5 A6);
303         $mac!(7 A1 A2 A3 A4 A5 A6 A7);
304         $mac!(8 A1 A2 A3 A4 A5 A6 A7 A8);
305         $mac!(9 A1 A2 A3 A4 A5 A6 A7 A8 A9);
306         $mac!(10 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10);
307         $mac!(11 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11);
308         $mac!(12 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12);
309         $mac!(13 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13);
310         $mac!(14 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13 A14);
311         $mac!(15 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13 A14 A15);
312         $mac!(16 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13 A14 A15 A16);
313         $mac!(17 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13 A14 A15 A16 A17);
314     };
315 }
316 
317 mod typed;
318 use crate::runtime::vm::VMStackChain;
319 pub use typed::*;
320 
321 impl Func {
322     /// Creates a new `Func` with the given arguments, typically to create a
323     /// host-defined function to pass as an import to a module.
324     ///
325     /// * `store` - the store in which to create this [`Func`], which will own
326     ///   the return value.
327     ///
328     /// * `ty` - the signature of this function, used to indicate what the
329     ///   inputs and outputs are.
330     ///
331     /// * `func` - the native code invoked whenever this `Func` will be called.
332     ///   This closure is provided a [`Caller`] as its first argument to learn
333     ///   information about the caller, and then it's passed a list of
334     ///   parameters as a slice along with a mutable slice of where to write
335     ///   results.
336     ///
337     /// Note that the implementation of `func` must adhere to the `ty` signature
338     /// given, error or traps may occur if it does not respect the `ty`
339     /// signature. For example if the function type declares that it returns one
340     /// i32 but the `func` closures does not write anything into the results
341     /// slice then a trap may be generated.
342     ///
343     /// Additionally note that this is quite a dynamic function since signatures
344     /// are not statically known. For a more performant and ergonomic `Func`
345     /// it's recommended to use [`Func::wrap`] if you can because with
346     /// statically known signatures Wasmtime can optimize the implementation
347     /// much more.
348     ///
349     /// For more information about `Send + Sync + 'static` requirements on the
350     /// `func`, see [`Func::wrap`](#why-send--sync--static).
351     ///
352     /// # Errors
353     ///
354     /// The host-provided function here returns a
355     /// [`Result<()>`](crate::Result). If the function returns `Ok(())` then
356     /// that indicates that the host function completed successfully and wrote
357     /// the result into the `&mut [Val]` argument.
358     ///
359     /// If the function returns `Err(e)`, however, then this is equivalent to
360     /// the host function triggering a trap for wasm. WebAssembly execution is
361     /// immediately halted and the original caller of [`Func::call`], for
362     /// example, will receive the error returned here (possibly with
363     /// [`WasmBacktrace`](crate::WasmBacktrace) context information attached).
364     ///
365     /// For more information about errors in Wasmtime see the [`Trap`]
366     /// documentation.
367     ///
368     /// [`Trap`]: crate::Trap
369     ///
370     /// # Panics
371     ///
372     /// Panics if the given function type is not associated with this store's
373     /// engine.
374     pub fn new<T: 'static>(
375         mut store: impl AsContextMut<Data = T>,
376         ty: FuncType,
377         func: impl Fn(Caller<'_, T>, &[Val], &mut [Val]) -> Result<()> + Send + Sync + 'static,
378     ) -> Self {
379         let store = store.as_context_mut().0;
380         let host = HostFunc::new(store.engine(), ty, func).panic_on_oom();
381 
382         // SAFETY: the `T` used by `func` matches the `T` of the store we're
383         // inserting into via this function's type signature.
384         unsafe { host.into_func(store).panic_on_oom() }
385     }
386 
387     /// Creates a new [`Func`] with the given arguments, although has fewer
388     /// runtime checks than [`Func::new`].
389     ///
390     /// This function takes a callback of a different signature than
391     /// [`Func::new`], instead receiving a raw pointer with a list of [`ValRaw`]
392     /// structures. These values have no type information associated with them
393     /// so it's up to the caller to provide a function that will correctly
394     /// interpret the list of values as those coming from the `ty` specified.
395     ///
396     /// If you're calling this from Rust it's recommended to either instead use
397     /// [`Func::new`] or [`Func::wrap`]. The [`Func::wrap`] API, in particular,
398     /// is both safer and faster than this API.
399     ///
400     /// # Errors
401     ///
402     /// See [`Func::new`] for the behavior of returning an error from the host
403     /// function provided here.
404     ///
405     /// # Unsafety
406     ///
407     /// This function is not safe because it's not known at compile time that
408     /// the `func` provided correctly interprets the argument types provided to
409     /// it, or that the results it produces will be of the correct type.
410     ///
411     /// # Panics
412     ///
413     /// Panics if the given function type is not associated with this store's
414     /// engine.
415     pub unsafe fn new_unchecked<T: 'static>(
416         mut store: impl AsContextMut<Data = T>,
417         ty: FuncType,
418         func: impl Fn(Caller<'_, T>, &mut [MaybeUninit<ValRaw>]) -> Result<()> + Send + Sync + 'static,
419     ) -> Self {
420         let store = store.as_context_mut().0;
421 
422         // SAFETY: the contract required by `new_unchecked` is the same as the
423         // contract required by this function itself.
424         let host = unsafe { HostFunc::new_unchecked(store.engine(), ty, func).panic_on_oom() };
425 
426         // SAFETY: the `T` used by `func` matches the `T` of the store we're
427         // inserting into via this function's type signature.
428         unsafe { host.into_func(store).panic_on_oom() }
429     }
430 
431     /// Creates a new host-defined WebAssembly function which, when called,
432     /// will run the asynchronous computation defined by `func` to completion
433     /// and then return the result to WebAssembly.
434     ///
435     /// This function is the asynchronous analogue of [`Func::new`] and much of
436     /// that documentation applies to this as well. The key difference is that
437     /// `func` returns a future instead of simply a `Result`. Note that the
438     /// returned future can close over any of the arguments, but it cannot close
439     /// over the state of the closure itself. It's recommended to store any
440     /// necessary async state in the `T` of the [`Store<T>`](crate::Store) which
441     /// can be accessed through [`Caller::data`] or [`Caller::data_mut`].
442     ///
443     /// For more information on `Send + Sync + 'static`, see
444     /// [`Func::wrap`](#why-send--sync--static).
445     ///
446     /// # Panics
447     ///
448     /// Panics if the given function type is not associated with this store's
449     /// engine.
450     ///
451     /// # Errors
452     ///
453     /// See [`Func::new`] for the behavior of returning an error from the host
454     /// function provided here.
455     ///
456     /// # Examples
457     ///
458     /// ```
459     /// # use wasmtime::*;
460     /// # fn main() -> Result<()> {
461     /// // Simulate some application-specific state as well as asynchronous
462     /// // functions to query that state.
463     /// struct MyDatabase {
464     ///     // ...
465     /// }
466     ///
467     /// impl MyDatabase {
468     ///     async fn get_row_count(&self) -> u32 {
469     ///         // ...
470     /// #       100
471     ///     }
472     /// }
473     ///
474     /// let my_database = MyDatabase {
475     ///     // ...
476     /// };
477     ///
478     /// // Using `new_async` we can hook up into calling our async
479     /// // `get_row_count` function.
480     /// let engine = Engine::default();
481     /// let mut store = Store::new(&engine, MyDatabase {
482     ///     // ...
483     /// });
484     /// let get_row_count_type = wasmtime::FuncType::new(
485     ///     &engine,
486     ///     None,
487     ///     Some(wasmtime::ValType::I32),
488     /// );
489     /// let get = Func::new_async(&mut store, get_row_count_type, |caller, _params, results| {
490     ///     Box::new(async move {
491     ///         let count = caller.data().get_row_count().await;
492     ///         results[0] = Val::I32(count as i32);
493     ///         Ok(())
494     ///     })
495     /// });
496     /// // ...
497     /// # Ok(())
498     /// # }
499     /// ```
500     #[cfg(feature = "async")]
501     pub fn new_async<T, F>(mut store: impl AsContextMut<Data = T>, ty: FuncType, func: F) -> Func
502     where
503         F: for<'a> Fn(
504                 Caller<'a, T>,
505                 &'a [Val],
506                 &'a mut [Val],
507             ) -> Box<dyn Future<Output = Result<()>> + Send + 'a>
508             + Send
509             + Sync
510             + 'static,
511         T: Send + 'static,
512     {
513         let store = store.as_context_mut().0;
514 
515         let host = HostFunc::new_async(store.engine(), ty, func).panic_on_oom();
516 
517         // SAFETY: the `T` used by `func` matches the `T` of the store we're
518         // inserting into via this function's type signature.
519         unsafe { host.into_func(store).panic_on_oom() }
520     }
521 
522     /// Creates a new `Func` from a store and a funcref within that store.
523     ///
524     /// # Safety
525     ///
526     /// The safety of this function requires that `func_ref` is a valid function
527     /// pointer owned by `store`.
528     pub(crate) unsafe fn from_vm_func_ref(store: StoreId, func_ref: NonNull<VMFuncRef>) -> Func {
529         // SAFETY: given the contract of this function it's safe to read the
530         // `type_index` field.
531         unsafe {
532             debug_assert!(func_ref.as_ref().type_index != VMSharedTypeIndex::default());
533         }
534         Func {
535             store,
536             unsafe_func_ref: func_ref.into(),
537         }
538     }
539 
540     /// Creates a new `Func` from the given Rust closure.
541     ///
542     /// This function will create a new `Func` which, when called, will
543     /// execute the given Rust closure. Unlike [`Func::new`] the target
544     /// function being called is known statically so the type signature can
545     /// be inferred. Rust types will map to WebAssembly types as follows:
546     ///
547     /// | Rust Argument Type                | WebAssembly Type                          |
548     /// |-----------------------------------|-------------------------------------------|
549     /// | `i32`                             | `i32`                                     |
550     /// | `u32`                             | `i32`                                     |
551     /// | `i64`                             | `i64`                                     |
552     /// | `u64`                             | `i64`                                     |
553     /// | `f32`                             | `f32`                                     |
554     /// | `f64`                             | `f64`                                     |
555     /// | `V128` on x86-64 and aarch64 only | `v128`                                    |
556     /// | `Option<Func>`                    | `funcref` aka `(ref null func)`           |
557     /// | `Func`                            | `(ref func)`                              |
558     /// | `Option<Nofunc>`                  | `nullfuncref` aka `(ref null nofunc)`     |
559     /// | `NoFunc`                          | `(ref nofunc)`                            |
560     /// | `Option<Rooted<ExternRef>>`       | `externref` aka `(ref null extern)`       |
561     /// | `Rooted<ExternRef>`               | `(ref extern)`                            |
562     /// | `Option<NoExtern>`                | `nullexternref` aka `(ref null noextern)` |
563     /// | `NoExtern`                        | `(ref noextern)`                          |
564     /// | `Option<Rooted<AnyRef>>`          | `anyref` aka `(ref null any)`             |
565     /// | `Rooted<AnyRef>`                  | `(ref any)`                               |
566     /// | `Option<Rooted<EqRef>>`           | `eqref` aka `(ref null eq)`               |
567     /// | `Rooted<EqRef>`                   | `(ref eq)`                                |
568     /// | `Option<I31>`                     | `i31ref` aka `(ref null i31)`             |
569     /// | `I31`                             | `(ref i31)`                               |
570     /// | `Option<Rooted<StructRef>>`       | `(ref null struct)`                       |
571     /// | `Rooted<StructRef>`               | `(ref struct)`                            |
572     /// | `Option<Rooted<ArrayRef>>`        | `(ref null array)`                        |
573     /// | `Rooted<ArrayRef>`                | `(ref array)`                             |
574     /// | `Option<NoneRef>`                 | `nullref` aka `(ref null none)`           |
575     /// | `NoneRef`                         | `(ref none)`                              |
576     ///
577     /// Note that anywhere a `Rooted<T>` appears, a `OwnedRooted<T>` may also
578     /// be used.
579     ///
580     /// Any of the Rust types can be returned from the closure as well, in
581     /// addition to some extra types
582     ///
583     /// | Rust Return Type  | WebAssembly Return Type | Meaning               |
584     /// |-------------------|-------------------------|-----------------------|
585     /// | `()`              | nothing                 | no return value       |
586     /// | `T`               | `T`                     | a single return value |
587     /// | `(T1, T2, ...)`   | `T1 T2 ...`             | multiple returns      |
588     ///
589     /// Note that all return types can also be wrapped in `Result<_>` to
590     /// indicate that the host function can generate a trap as well as possibly
591     /// returning a value.
592     ///
593     /// Finally you can also optionally take [`Caller`] as the first argument of
594     /// your closure. If inserted then you're able to inspect the caller's
595     /// state, for example the [`Memory`](crate::Memory) it has exported so you
596     /// can read what pointers point to.
597     ///
598     /// Note that when using this API, the intention is to create as thin of a
599     /// layer as possible for when WebAssembly calls the function provided. With
600     /// sufficient inlining and optimization the WebAssembly will call straight
601     /// into `func` provided, with no extra fluff entailed.
602     ///
603     /// # Why `Send + Sync + 'static`?
604     ///
605     /// All host functions defined in a [`Store`](crate::Store) (including
606     /// those from [`Func::new`] and other constructors) require that the
607     /// `func` provided is `Send + Sync + 'static`. Additionally host functions
608     /// always are `Fn` as opposed to `FnMut` or `FnOnce`. This can at-a-glance
609     /// feel restrictive since the closure cannot close over as many types as
610     /// before. The reason for this, though, is to ensure that
611     /// [`Store<T>`](crate::Store) can implement both the `Send` and `Sync`
612     /// traits.
613     ///
614     /// Fear not, however, because this isn't as restrictive as it seems! Host
615     /// functions are provided a [`Caller<'_, T>`](crate::Caller) argument which
616     /// allows access to the host-defined data within the
617     /// [`Store`](crate::Store). The `T` type is not required to be any of
618     /// `Send`, `Sync`, or `'static`! This means that you can store whatever
619     /// you'd like in `T` and have it accessible by all host functions.
620     /// Additionally mutable access to `T` is allowed through
621     /// [`Caller::data_mut`].
622     ///
623     /// Most host-defined [`Func`] values provide closures that end up not
624     /// actually closing over any values. These zero-sized types will use the
625     /// context from [`Caller`] for host-defined information.
626     ///
627     /// # Errors
628     ///
629     /// The closure provided here to `wrap` can optionally return a
630     /// [`Result<T>`](crate::Result). Returning `Ok(t)` represents the host
631     /// function successfully completing with the `t` result. Returning
632     /// `Err(e)`, however, is equivalent to raising a custom wasm trap.
633     /// Execution of WebAssembly does not resume and the stack is unwound to the
634     /// original caller of the function where the error is returned.
635     ///
636     /// For more information about errors in Wasmtime see the [`Trap`]
637     /// documentation.
638     ///
639     /// [`Trap`]: crate::Trap
640     ///
641     /// # Examples
642     ///
643     /// First up we can see how simple wasm imports can be implemented, such
644     /// as a function that adds its two arguments and returns the result.
645     ///
646     /// ```
647     /// # use wasmtime::*;
648     /// # fn main() -> Result<()> {
649     /// # let mut store = Store::<()>::default();
650     /// let add = Func::wrap(&mut store, |a: i32, b: i32| a + b);
651     /// let module = Module::new(
652     ///     store.engine(),
653     ///     r#"
654     ///         (module
655     ///             (import "" "" (func $add (param i32 i32) (result i32)))
656     ///             (func (export "foo") (param i32 i32) (result i32)
657     ///                 local.get 0
658     ///                 local.get 1
659     ///                 call $add))
660     ///     "#,
661     /// )?;
662     /// let instance = Instance::new(&mut store, &module, &[add.into()])?;
663     /// let foo = instance.get_typed_func::<(i32, i32), i32>(&mut store, "foo")?;
664     /// assert_eq!(foo.call(&mut store, (1, 2))?, 3);
665     /// # Ok(())
666     /// # }
667     /// ```
668     ///
669     /// We can also do the same thing, but generate a trap if the addition
670     /// overflows:
671     ///
672     /// ```
673     /// # use wasmtime::*;
674     /// # fn main() -> Result<()> {
675     /// # let mut store = Store::<()>::default();
676     /// let add = Func::wrap(&mut store, |a: i32, b: i32| {
677     ///     match a.checked_add(b) {
678     ///         Some(i) => Ok(i),
679     ///         None => bail!("overflow"),
680     ///     }
681     /// });
682     /// let module = Module::new(
683     ///     store.engine(),
684     ///     r#"
685     ///         (module
686     ///             (import "" "" (func $add (param i32 i32) (result i32)))
687     ///             (func (export "foo") (param i32 i32) (result i32)
688     ///                 local.get 0
689     ///                 local.get 1
690     ///                 call $add))
691     ///     "#,
692     /// )?;
693     /// let instance = Instance::new(&mut store, &module, &[add.into()])?;
694     /// let foo = instance.get_typed_func::<(i32, i32), i32>(&mut store, "foo")?;
695     /// assert_eq!(foo.call(&mut store, (1, 2))?, 3);
696     /// assert!(foo.call(&mut store, (i32::max_value(), 1)).is_err());
697     /// # Ok(())
698     /// # }
699     /// ```
700     ///
701     /// And don't forget all the wasm types are supported!
702     ///
703     /// ```
704     /// # use wasmtime::*;
705     /// # fn main() -> Result<()> {
706     /// # let mut store = Store::<()>::default();
707     /// let debug = Func::wrap(&mut store, |a: i32, b: u32, c: f32, d: i64, e: u64, f: f64| {
708     ///
709     ///     println!("a={}", a);
710     ///     println!("b={}", b);
711     ///     println!("c={}", c);
712     ///     println!("d={}", d);
713     ///     println!("e={}", e);
714     ///     println!("f={}", f);
715     /// });
716     /// let module = Module::new(
717     ///     store.engine(),
718     ///     r#"
719     ///         (module
720     ///             (import "" "" (func $debug (param i32 i32 f32 i64 i64 f64)))
721     ///             (func (export "foo")
722     ///                 i32.const -1
723     ///                 i32.const 1
724     ///                 f32.const 2
725     ///                 i64.const -3
726     ///                 i64.const 3
727     ///                 f64.const 4
728     ///                 call $debug))
729     ///     "#,
730     /// )?;
731     /// let instance = Instance::new(&mut store, &module, &[debug.into()])?;
732     /// let foo = instance.get_typed_func::<(), ()>(&mut store, "foo")?;
733     /// foo.call(&mut store, ())?;
734     /// # Ok(())
735     /// # }
736     /// ```
737     ///
738     /// Finally if you want to get really fancy you can also implement
739     /// imports that read/write wasm module's memory
740     ///
741     /// ```
742     /// use std::str;
743     ///
744     /// # use wasmtime::*;
745     /// # fn main() -> Result<()> {
746     /// # let mut store = Store::default();
747     /// let log_str = Func::wrap(&mut store, |mut caller: Caller<'_, ()>, ptr: i32, len: i32| {
748     ///     let mem = match caller.get_export("memory") {
749     ///         Some(Extern::Memory(mem)) => mem,
750     ///         _ => bail!("failed to find host memory"),
751     ///     };
752     ///     let data = mem.data(&caller)
753     ///         .get(ptr as u32 as usize..)
754     ///         .and_then(|arr| arr.get(..len as u32 as usize));
755     ///     let string = match data {
756     ///         Some(data) => match str::from_utf8(data) {
757     ///             Ok(s) => s,
758     ///             Err(_) => bail!("invalid utf-8"),
759     ///         },
760     ///         None => bail!("pointer/length out of bounds"),
761     ///     };
762     ///     assert_eq!(string, "Hello, world!");
763     ///     println!("{}", string);
764     ///     Ok(())
765     /// });
766     /// let module = Module::new(
767     ///     store.engine(),
768     ///     r#"
769     ///         (module
770     ///             (import "" "" (func $log_str (param i32 i32)))
771     ///             (func (export "foo")
772     ///                 i32.const 4   ;; ptr
773     ///                 i32.const 13  ;; len
774     ///                 call $log_str)
775     ///             (memory (export "memory") 1)
776     ///             (data (i32.const 4) "Hello, world!"))
777     ///     "#,
778     /// )?;
779     /// let instance = Instance::new(&mut store, &module, &[log_str.into()])?;
780     /// let foo = instance.get_typed_func::<(), ()>(&mut store, "foo")?;
781     /// foo.call(&mut store, ())?;
782     /// # Ok(())
783     /// # }
784     /// ```
785     pub fn wrap<T, Params, Results>(
786         store: impl AsContextMut<Data = T>,
787         func: impl IntoFunc<T, Params, Results>,
788     ) -> Func
789     where
790         T: 'static,
791     {
792         Self::try_wrap(store, func).expect(
793             "allocation failure during `Func::wrap` (use `Func::try_wrap` to handle such errors)",
794         )
795     }
796 
797     /// Fallible version of [`Func::wrap`] that returns an error on
798     /// out-of-memory instead of panicking.
799     pub fn try_wrap<T, Params, Results>(
800         mut store: impl AsContextMut<Data = T>,
801         func: impl IntoFunc<T, Params, Results>,
802     ) -> Result<Func>
803     where
804         T: 'static,
805     {
806         let store = store.as_context_mut().0;
807         let engine = store.engine();
808         let host = func.into_func(engine)?;
809 
810         // SAFETY: The `T` the closure takes is the same as the `T` of the store
811         // we're inserting into via the type signature above.
812         Ok(unsafe { host.into_func(store)? })
813     }
814 
815     /// Same as [`Func::wrap`], except the closure asynchronously produces the
816     /// result and the arguments are passed within a tuple. For more information
817     /// see the [`Func`] documentation.
818     #[cfg(feature = "async")]
819     pub fn wrap_async<T, F, P, R>(mut store: impl AsContextMut<Data = T>, func: F) -> Func
820     where
821         F: for<'a> Fn(Caller<'a, T>, P) -> Box<dyn Future<Output = R> + Send + 'a>
822             + Send
823             + Sync
824             + 'static,
825         P: WasmTyList,
826         R: WasmRet,
827         T: Send + 'static,
828     {
829         let store = store.as_context_mut().0;
830         let host = HostFunc::wrap_async(store.engine(), func).panic_on_oom();
831 
832         // SAFETY: The `T` the closure takes is the same as the `T` of the store
833         // we're inserting into via the type signature above.
834         unsafe { host.into_func(store).panic_on_oom() }
835     }
836 
837     /// Returns the underlying wasm type that this `Func` has.
838     ///
839     /// # Panics
840     ///
841     /// Panics if `store` does not own this function.
842     pub fn ty(&self, store: impl AsContext) -> FuncType {
843         self.load_ty(&store.as_context().0)
844     }
845 
846     /// Forcibly loads the type of this function from the `Engine`.
847     ///
848     /// Note that this is a somewhat expensive method since it requires taking a
849     /// lock as well as cloning a type.
850     pub(crate) fn load_ty(&self, store: &StoreOpaque) -> FuncType {
851         FuncType::from_shared_type_index(store.engine(), self.type_index(store))
852     }
853 
854     /// Does this function match the given type?
855     ///
856     /// That is, is this function's type a subtype of the given type?
857     ///
858     /// # Panics
859     ///
860     /// Panics if this function is not associated with the given store or if the
861     /// function type is not associated with the store's engine.
862     pub fn matches_ty(&self, store: impl AsContext, func_ty: &FuncType) -> bool {
863         self._matches_ty(store.as_context().0, func_ty)
864     }
865 
866     pub(crate) fn _matches_ty(&self, store: &StoreOpaque, func_ty: &FuncType) -> bool {
867         let actual_ty = self.load_ty(store);
868         actual_ty.matches(func_ty)
869     }
870 
871     pub(crate) fn ensure_matches_ty(&self, store: &StoreOpaque, func_ty: &FuncType) -> Result<()> {
872         if !self.comes_from_same_store(store) {
873             bail!("function used with wrong store");
874         }
875         if self._matches_ty(store, func_ty) {
876             Ok(())
877         } else {
878             let actual_ty = self.load_ty(store);
879             bail!("type mismatch: expected {func_ty}, found {actual_ty}")
880         }
881     }
882 
883     pub(crate) fn type_index(&self, data: &StoreOpaque) -> VMSharedTypeIndex {
884         unsafe { self.vm_func_ref(data).as_ref().type_index }
885     }
886 
887     /// Invokes this function with the `params` given and writes returned values
888     /// to `results`.
889     ///
890     /// The `params` here must match the type signature of this `Func`, or an
891     /// error will occur. Additionally `results` must have the same
892     /// length as the number of results for this function. Calling this function
893     /// will synchronously execute the WebAssembly function referenced to get
894     /// the results.
895     ///
896     /// This function will return `Ok(())` if execution completed without a trap
897     /// or error of any kind. In this situation the results will be written to
898     /// the provided `results` array.
899     ///
900     /// # Errors
901     ///
902     /// Any error which occurs throughout the execution of the function will be
903     /// returned as `Err(e)`. The [`Error`](crate::Error) type can be inspected
904     /// for the precise error cause such as:
905     ///
906     /// * [`Trap`] - indicates that a wasm trap happened and execution was
907     ///   halted.
908     /// * [`WasmBacktrace`] - optionally included on errors for backtrace
909     ///   information of the trap/error.
910     /// * Other string-based errors to indicate issues such as type errors with
911     ///   `params`.
912     /// * Any host-originating error originally returned from a function defined
913     ///   via [`Func::new`], for example.
914     /// * The `store` provided is configured to require `call_async` to be used
915     ///   instead, such as with epochs or fuel.
916     ///
917     /// Errors typically indicate that execution of WebAssembly was halted
918     /// mid-way and did not complete after the error condition happened.
919     ///
920     /// [`Trap`]: crate::Trap
921     ///
922     /// # Panics
923     ///
924     /// Panics if `store` does not own this function.
925     ///
926     /// [`WasmBacktrace`]: crate::WasmBacktrace
927     pub fn call(
928         &self,
929         mut store: impl AsContextMut,
930         params: &[Val],
931         results: &mut [Val],
932     ) -> Result<()> {
933         let mut store = store.as_context_mut();
934         store.0.validate_sync_call()?;
935 
936         self.call_impl_check_args(&mut store, params, results)?;
937 
938         unsafe { self.call_impl_do_call(&mut store, params, results) }
939     }
940 
941     /// Invokes this function in an "unchecked" fashion, reading parameters and
942     /// writing results to `params_and_returns`.
943     ///
944     /// This function is the same as [`Func::call`] except that the arguments
945     /// and results both use a different representation. If possible it's
946     /// recommended to use [`Func::call`] if safety isn't necessary or to use
947     /// [`Func::typed`] in conjunction with [`TypedFunc::call`] since that's
948     /// both safer and faster than this method of invoking a function.
949     ///
950     /// Note that if this function takes `externref` arguments then it will
951     /// **not** automatically GC unlike the [`Func::call`] and
952     /// [`TypedFunc::call`] functions. This means that if this function is
953     /// invoked many times with new `ExternRef` values and no other GC happens
954     /// via any other means then no values will get collected.
955     ///
956     /// # Errors
957     ///
958     /// For more information about errors see the [`Func::call`] documentation.
959     ///
960     /// # Unsafety
961     ///
962     /// This function is unsafe because the `params_and_returns` argument is not
963     /// validated at all. It must uphold invariants such as:
964     ///
965     /// * It's a valid pointer to an array
966     /// * It has enough space to store all parameters
967     /// * It has enough space to store all results (not at the same time as
968     ///   parameters)
969     /// * Parameters are initially written to the array and have the correct
970     ///   types and such.
971     /// * Reference types like `externref` and `funcref` are valid at the
972     ///   time of this call and for the `store` specified.
973     ///
974     /// These invariants are all upheld for you with [`Func::call`] and
975     /// [`TypedFunc::call`].
976     pub unsafe fn call_unchecked(
977         &self,
978         mut store: impl AsContextMut,
979         params_and_returns: *mut [ValRaw],
980     ) -> Result<()> {
981         let mut store = store.as_context_mut();
982         let func_ref = self.vm_func_ref(store.0);
983         let params_and_returns = NonNull::new(params_and_returns).unwrap_or(NonNull::from(&mut []));
984 
985         // SAFETY: the safety of this function call is the same as the contract
986         // of this function.
987         unsafe { Self::call_unchecked_raw(&mut store, func_ref, params_and_returns) }
988     }
989 
990     pub(crate) unsafe fn call_unchecked_raw<T>(
991         store: &mut StoreContextMut<'_, T>,
992         func_ref: NonNull<VMFuncRef>,
993         params_and_returns: NonNull<[ValRaw]>,
994     ) -> Result<()> {
995         // SAFETY: the safety of this function call is the same as the contract
996         // of this function.
997         invoke_wasm_and_catch_traps(store, |caller, vm| unsafe {
998             VMFuncRef::array_call(func_ref, vm, caller, params_and_returns)
999         })
1000     }
1001 
1002     /// Converts the raw representation of a `funcref` into an `Option<Func>`
1003     ///
1004     /// This is intended to be used in conjunction with [`Func::new_unchecked`],
1005     /// [`Func::call_unchecked`], and [`ValRaw`] with its `funcref` field. This
1006     /// is the dual of [`Func::to_raw`].
1007     ///
1008     /// # Unsafety
1009     ///
1010     /// This function is not safe because `raw` is not validated at all. The
1011     /// caller must guarantee that `raw` is owned by the `store` provided and is
1012     /// valid within the `store`.
1013     pub unsafe fn from_raw(mut store: impl AsContextMut, raw: *mut c_void) -> Option<Func> {
1014         // SAFETY: this function's own contract is that `raw` is owned by store
1015         // to make this safe.
1016         unsafe { Self::_from_raw(store.as_context_mut().0, raw) }
1017     }
1018 
1019     /// Same as `from_raw`, but with the internal `StoreOpaque` type.
1020     pub(crate) unsafe fn _from_raw(store: &mut StoreOpaque, raw: *mut c_void) -> Option<Func> {
1021         // SAFETY: this function's own contract is that `raw` is owned by store
1022         // to make this safe.
1023         unsafe {
1024             Some(Func::from_vm_func_ref(
1025                 store.id(),
1026                 NonNull::new(raw.cast())?,
1027             ))
1028         }
1029     }
1030 
1031     /// Extracts the raw value of this `Func`, which is owned by `store`.
1032     ///
1033     /// This function returns a value that's suitable for writing into the
1034     /// `funcref` field of the [`ValRaw`] structure.
1035     ///
1036     /// # Safety
1037     ///
1038     /// The returned value is only valid for as long as the store is alive.
1039     /// This value is safe to pass to [`Func::from_raw`] so long as the same
1040     /// `store` is provided.
1041     pub fn to_raw(&self, mut store: impl AsContextMut) -> *mut c_void {
1042         self.vm_func_ref(store.as_context_mut().0).as_ptr().cast()
1043     }
1044 
1045     /// Invokes this function with the `params` given, returning the results
1046     /// asynchronously.
1047     ///
1048     /// This function is the same as [`Func::call`] except that it is
1049     /// asynchronous.
1050     ///
1051     /// It's important to note that the execution of WebAssembly will happen
1052     /// synchronously in the `poll` method of the future returned from this
1053     /// function. Wasmtime does not manage its own thread pool or similar to
1054     /// execute WebAssembly in. Future `poll` methods are generally expected to
1055     /// resolve quickly, so it's recommended that you run or poll this future
1056     /// in a "blocking context".
1057     ///
1058     /// For more information see the documentation on [asynchronous
1059     /// configs](crate#async).
1060     ///
1061     /// # Errors
1062     ///
1063     /// For more information on errors see the [`Func::call`] documentation.
1064     ///
1065     /// # Panics
1066     ///
1067     /// Panics if this is called on a function in a synchronous store. This
1068     /// only works with functions defined within an asynchronous store. Also
1069     /// panics if `store` does not own this function.
1070     #[cfg(feature = "async")]
1071     pub async fn call_async(
1072         &self,
1073         mut store: impl AsContextMut<Data: Send>,
1074         params: &[Val],
1075         results: &mut [Val],
1076     ) -> Result<()> {
1077         let mut store = store.as_context_mut();
1078 
1079         self.call_impl_check_args(&mut store, params, results)?;
1080 
1081         let result = store
1082             .on_fiber(|store| unsafe { self.call_impl_do_call(store, params, results) })
1083             .await??;
1084         Ok(result)
1085     }
1086 
1087     /// Perform dynamic checks that the arguments given to us match
1088     /// the signature of this function and are appropriate to pass to this
1089     /// function.
1090     ///
1091     /// This involves checking to make sure we have the right number and types
1092     /// of arguments as well as making sure everything is from the same `Store`.
1093     ///
1094     /// This must be called just before `call_impl_do_call`.
1095     fn call_impl_check_args<T>(
1096         &self,
1097         store: &mut StoreContextMut<'_, T>,
1098         params: &[Val],
1099         results: &mut [Val],
1100     ) -> Result<()> {
1101         let ty = self.load_ty(store.0);
1102         if ty.params().len() != params.len() {
1103             bail!(
1104                 "expected {} arguments, got {}",
1105                 ty.params().len(),
1106                 params.len()
1107             );
1108         }
1109         if ty.results().len() != results.len() {
1110             bail!(
1111                 "expected {} results, got {}",
1112                 ty.results().len(),
1113                 results.len()
1114             );
1115         }
1116 
1117         for (ty, arg) in ty.params().zip(params) {
1118             arg.ensure_matches_ty(store.0, &ty)
1119                 .context("argument type mismatch")?;
1120             if !arg.comes_from_same_store(store.0) {
1121                 bail!("cross-`Store` values are not currently supported");
1122             }
1123         }
1124 
1125         Ok(())
1126     }
1127 
1128     /// Do the actual call into Wasm.
1129     ///
1130     /// # Safety
1131     ///
1132     /// You must have type checked the arguments by calling
1133     /// `call_impl_check_args` immediately before calling this function. It is
1134     /// only safe to call this function if that one did not return an error.
1135     unsafe fn call_impl_do_call<T>(
1136         &self,
1137         store: &mut StoreContextMut<'_, T>,
1138         params: &[Val],
1139         results: &mut [Val],
1140     ) -> Result<()> {
1141         // Store the argument values into `values_vec`.
1142         let ty = self.load_ty(store.0);
1143         let values_vec_size = params.len().max(ty.results().len());
1144         let mut values_vec = store.0.take_wasm_val_raw_storage();
1145         debug_assert!(values_vec.is_empty());
1146         values_vec.resize_with(values_vec_size, || ValRaw::v128(0));
1147         for (arg, slot) in params.iter().cloned().zip(&mut values_vec) {
1148             *slot = arg.to_raw(&mut *store)?;
1149         }
1150 
1151         unsafe {
1152             self.call_unchecked(
1153                 &mut *store,
1154                 core::ptr::slice_from_raw_parts_mut(values_vec.as_mut_ptr(), values_vec_size),
1155             )?;
1156         }
1157 
1158         for ((i, slot), val) in results.iter_mut().enumerate().zip(&values_vec) {
1159             let ty = ty.results().nth(i).unwrap();
1160             *slot = unsafe { Val::from_raw(&mut *store, *val, ty) };
1161         }
1162         values_vec.truncate(0);
1163         store.0.save_wasm_val_raw_storage(values_vec);
1164         Ok(())
1165     }
1166 
1167     #[inline]
1168     pub(crate) fn vm_func_ref(&self, store: &StoreOpaque) -> NonNull<VMFuncRef> {
1169         self.store.assert_belongs_to(store.id());
1170         self.unsafe_func_ref.as_non_null()
1171     }
1172 
1173     pub(crate) fn vmimport(&self, store: &StoreOpaque) -> VMFunctionImport {
1174         unsafe {
1175             let f = self.vm_func_ref(store);
1176             VMFunctionImport {
1177                 // Note that this is a load-bearing `unwrap` here, but is
1178                 // never expected to trip at runtime. The general problem is
1179                 // that host functions do not have a `wasm_call` function so
1180                 // the `VMFuncRef` type has an optional pointer there. This is
1181                 // only able to be filled out when a function is "paired" with
1182                 // a module where trampolines are present to fill out
1183                 // `wasm_call` pointers.
1184                 //
1185                 // This pairing of modules doesn't happen explicitly but is
1186                 // instead managed lazily throughout Wasmtime. Specifically the
1187                 // way this works is one of:
1188                 //
1189                 // * When a host function is created the store's list of
1190                 //   modules are searched for a wasm trampoline. If not found
1191                 //   the `wasm_call` field is left blank.
1192                 //
1193                 // * When a module instantiation happens, which uses this
1194                 //   function, the module will be used to fill any outstanding
1195                 //   holes that it has trampolines for.
1196                 //
1197                 // This means that by the time we get to this point any
1198                 // relevant holes should be filled out. Thus if this panic
1199                 // actually triggers then it's indicative of a missing `fill`
1200                 // call somewhere else.
1201                 wasm_call: f.as_ref().wasm_call.unwrap(),
1202                 array_call: f.as_ref().array_call,
1203                 vmctx: f.as_ref().vmctx,
1204             }
1205         }
1206     }
1207 
1208     pub(crate) fn comes_from_same_store(&self, store: &StoreOpaque) -> bool {
1209         self.store == store.id()
1210     }
1211 
1212     /// Attempts to extract a typed object from this `Func` through which the
1213     /// function can be called.
1214     ///
1215     /// This function serves as an alternative to [`Func::call`] and
1216     /// [`Func::call_async`]. This method performs a static type check (using
1217     /// the `Params` and `Results` type parameters on the underlying wasm
1218     /// function. If the type check passes then a `TypedFunc` object is returned,
1219     /// otherwise an error is returned describing the typecheck failure.
1220     ///
1221     /// The purpose of this relative to [`Func::call`] is that it's much more
1222     /// efficient when used to invoke WebAssembly functions. With the types
1223     /// statically known far less setup/teardown is required when invoking
1224     /// WebAssembly. If speed is desired then this function is recommended to be
1225     /// used instead of [`Func::call`] (which is more general, hence its
1226     /// slowdown).
1227     ///
1228     /// The `Params` type parameter is used to describe the parameters of the
1229     /// WebAssembly function. This can either be a single type (like `i32`), or
1230     /// a tuple of types representing the list of parameters (like `(i32, f32,
1231     /// f64)`). Additionally you can use `()` to represent that the function has
1232     /// no parameters.
1233     ///
1234     /// The `Results` type parameter is used to describe the results of the
1235     /// function. This behaves the same way as `Params`, but just for the
1236     /// results of the function.
1237     ///
1238     /// # Translating Between WebAssembly and Rust Types
1239     ///
1240     /// Translation between Rust types and WebAssembly types looks like:
1241     ///
1242     /// | WebAssembly                               | Rust                                  |
1243     /// |-------------------------------------------|---------------------------------------|
1244     /// | `i32`                                     | `i32` or `u32`                        |
1245     /// | `i64`                                     | `i64` or `u64`                        |
1246     /// | `f32`                                     | `f32`                                 |
1247     /// | `f64`                                     | `f64`                                 |
1248     /// | `externref` aka `(ref null extern)`       | `Option<Rooted<ExternRef>>`           |
1249     /// | `(ref extern)`                            | `Rooted<ExternRef>`                   |
1250     /// | `nullexternref` aka `(ref null noextern)` | `Option<NoExtern>`                    |
1251     /// | `(ref noextern)`                          | `NoExtern`                            |
1252     /// | `anyref` aka `(ref null any)`             | `Option<Rooted<AnyRef>>`              |
1253     /// | `(ref any)`                               | `Rooted<AnyRef>`                      |
1254     /// | `eqref` aka `(ref null eq)`               | `Option<Rooted<EqRef>>`               |
1255     /// | `(ref eq)`                                | `Rooted<EqRef>`                       |
1256     /// | `i31ref` aka `(ref null i31)`             | `Option<I31>`                         |
1257     /// | `(ref i31)`                               | `I31`                                 |
1258     /// | `structref` aka `(ref null struct)`       | `Option<Rooted<StructRef>>`           |
1259     /// | `(ref struct)`                            | `Rooted<StructRef>`                   |
1260     /// | `arrayref` aka `(ref null array)`         | `Option<Rooted<ArrayRef>>`            |
1261     /// | `(ref array)`                             | `Rooted<ArrayRef>`                    |
1262     /// | `nullref` aka `(ref null none)`           | `Option<NoneRef>`                     |
1263     /// | `(ref none)`                              | `NoneRef`                             |
1264     /// | `funcref` aka `(ref null func)`           | `Option<Func>`                        |
1265     /// | `(ref func)`                              | `Func`                                |
1266     /// | `(ref null <func type index>)`            | `Option<Func>`                        |
1267     /// | `(ref <func type index>)`                 | `Func`                                |
1268     /// | `nullfuncref` aka `(ref null nofunc)`     | `Option<NoFunc>`                      |
1269     /// | `(ref nofunc)`                            | `NoFunc`                              |
1270     /// | `v128`                                    | `V128` on `x86-64` and `aarch64` only |
1271     ///
1272     /// (Note that this mapping is the same as that of [`Func::wrap`], and that
1273     /// anywhere a `Rooted<T>` appears, a `OwnedRooted<T>` may also appear).
1274     ///
1275     /// Note that once the [`TypedFunc`] return value is acquired you'll use either
1276     /// [`TypedFunc::call`] or [`TypedFunc::call_async`] as necessary to actually invoke
1277     /// the function. This method does not invoke any WebAssembly code, it
1278     /// simply performs a typecheck before returning the [`TypedFunc`] value.
1279     ///
1280     /// This method also has a convenience wrapper as
1281     /// [`Instance::get_typed_func`](crate::Instance::get_typed_func) to
1282     /// directly get a typed function value from an
1283     /// [`Instance`](crate::Instance).
1284     ///
1285     /// ## Subtyping
1286     ///
1287     /// For result types, you can always use a supertype of the WebAssembly
1288     /// function's actual declared result type. For example, if the WebAssembly
1289     /// function was declared with type `(func (result nullfuncref))` you could
1290     /// successfully call `f.typed::<(), Option<Func>>()` because `Option<Func>`
1291     /// corresponds to `funcref`, which is a supertype of `nullfuncref`.
1292     ///
1293     /// For parameter types, you can always use a subtype of the WebAssembly
1294     /// function's actual declared parameter type. For example, if the
1295     /// WebAssembly function was declared with type `(func (param (ref null
1296     /// func)))` you could successfully call `f.typed::<Func, ()>()` because
1297     /// `Func` corresponds to `(ref func)`, which is a subtype of `(ref null
1298     /// func)`.
1299     ///
1300     /// Additionally, for functions which take a reference to a concrete type as
1301     /// a parameter, you can also use the concrete type's supertype. Consider a
1302     /// WebAssembly function that takes a reference to a function with a
1303     /// concrete type: `(ref null <func type index>)`. In this scenario, there
1304     /// is no static `wasmtime::Foo` Rust type that corresponds to that
1305     /// particular Wasm-defined concrete reference type because Wasm modules are
1306     /// loaded dynamically at runtime. You *could* do `f.typed::<Option<NoFunc>,
1307     /// ()>()`, and while that is correctly typed and valid, it is often overly
1308     /// restrictive. The only value you could call the resulting typed function
1309     /// with is the null function reference, but we'd like to call it with
1310     /// non-null function references that happen to be of the correct
1311     /// type. Therefore, `f.typed<Option<Func>, ()>()` is also allowed in this
1312     /// case, even though `Option<Func>` represents `(ref null func)` which is
1313     /// the supertype, not subtype, of `(ref null <func type index>)`. This does
1314     /// imply some minimal dynamic type checks in this case, but it is supported
1315     /// for better ergonomics, to enable passing non-null references into the
1316     /// function.
1317     ///
1318     /// # Errors
1319     ///
1320     /// This function will return an error if `Params` or `Results` does not
1321     /// match the native type of this WebAssembly function.
1322     ///
1323     /// # Panics
1324     ///
1325     /// This method will panic if `store` does not own this function.
1326     ///
1327     /// # Examples
1328     ///
1329     /// An end-to-end example of calling a function which takes no parameters
1330     /// and has no results:
1331     ///
1332     /// ```
1333     /// # use wasmtime::*;
1334     /// # fn main() -> Result<()> {
1335     /// let engine = Engine::default();
1336     /// let mut store = Store::new(&engine, ());
1337     /// let module = Module::new(&engine, r#"(module (func (export "foo")))"#)?;
1338     /// let instance = Instance::new(&mut store, &module, &[])?;
1339     /// let foo = instance.get_func(&mut store, "foo").expect("export wasn't a function");
1340     ///
1341     /// // Note that this call can fail due to the typecheck not passing, but
1342     /// // in our case we statically know the module so we know this should
1343     /// // pass.
1344     /// let typed = foo.typed::<(), ()>(&store)?;
1345     ///
1346     /// // Note that this can fail if the wasm traps at runtime.
1347     /// typed.call(&mut store, ())?;
1348     /// # Ok(())
1349     /// # }
1350     /// ```
1351     ///
1352     /// You can also pass in multiple parameters and get a result back
1353     ///
1354     /// ```
1355     /// # use wasmtime::*;
1356     /// # fn foo(add: &Func, mut store: Store<()>) -> Result<()> {
1357     /// let typed = add.typed::<(i32, i64), f32>(&store)?;
1358     /// assert_eq!(typed.call(&mut store, (1, 2))?, 3.0);
1359     /// # Ok(())
1360     /// # }
1361     /// ```
1362     ///
1363     /// and similarly if a function has multiple results you can bind that too
1364     ///
1365     /// ```
1366     /// # use wasmtime::*;
1367     /// # fn foo(add_with_overflow: &Func, mut store: Store<()>) -> Result<()> {
1368     /// let typed = add_with_overflow.typed::<(u32, u32), (u32, i32)>(&store)?;
1369     /// let (result, overflow) = typed.call(&mut store, (u32::max_value(), 2))?;
1370     /// assert_eq!(result, 1);
1371     /// assert_eq!(overflow, 1);
1372     /// # Ok(())
1373     /// # }
1374     /// ```
1375     pub fn typed<Params, Results>(
1376         &self,
1377         store: impl AsContext,
1378     ) -> Result<TypedFunc<Params, Results>>
1379     where
1380         Params: WasmParams,
1381         Results: WasmResults,
1382     {
1383         // Type-check that the params/results are all valid
1384         let store = store.as_context().0;
1385         let ty = self.load_ty(store);
1386         Params::typecheck(store.engine(), ty.params(), TypeCheckPosition::Param)
1387             .context("type mismatch with parameters")?;
1388         Results::typecheck(store.engine(), ty.results(), TypeCheckPosition::Result)
1389             .context("type mismatch with results")?;
1390 
1391         // and then we can construct the typed version of this function
1392         // (unsafely), which should be safe since we just did the type check above.
1393         unsafe { Ok(TypedFunc::_new_unchecked(store, *self)) }
1394     }
1395 
1396     /// Get a stable hash key for this function.
1397     ///
1398     /// Even if the same underlying function is added to the `StoreData`
1399     /// multiple times and becomes multiple `wasmtime::Func`s, this hash key
1400     /// will be consistent across all of these functions.
1401     #[cfg_attr(
1402         not(test),
1403         expect(dead_code, reason = "Not used yet, but added for consistency")
1404     )]
1405     pub(crate) fn hash_key(&self, store: &mut StoreOpaque) -> impl core::hash::Hash + Eq + use<> {
1406         self.vm_func_ref(store).as_ptr().addr()
1407     }
1408 }
1409 
1410 /// Prepares for entrance into WebAssembly.
1411 ///
1412 /// This function will set up context such that `closure` is allowed to call a
1413 /// raw trampoline or a raw WebAssembly function. This *must* be called to do
1414 /// things like catch traps and set up GC properly.
1415 ///
1416 /// The `closure` provided receives a default "caller" `VMContext` parameter it
1417 /// can pass to the called wasm function, if desired.
1418 pub(crate) fn invoke_wasm_and_catch_traps<T>(
1419     store: &mut StoreContextMut<'_, T>,
1420     closure: impl FnMut(NonNull<VMContext>, Option<InterpreterRef<'_>>) -> bool,
1421 ) -> Result<()> {
1422     // The `enter_wasm` call below will reset the store context's
1423     // `stack_chain` to a new `InitialStack`, pointing to the
1424     // stack-allocated `initial_stack_csi`.
1425     let mut initial_stack_csi = VMCommonStackInformation::running_default();
1426     // Stores some state of the runtime just before entering Wasm. Will be
1427     // restored upon exiting Wasm. Note that the `CallThreadState` that is
1428     // created by the `catch_traps` call below will store a pointer to this
1429     // stack-allocated `previous_runtime_state`.
1430     let mut previous_runtime_state = EntryStoreContext::enter_wasm(store, &mut initial_stack_csi);
1431 
1432     if let Err(trap) = store.0.call_hook(CallHook::CallingWasm) {
1433         // `previous_runtime_state` implicitly dropped here
1434         return Err(trap);
1435     }
1436     let result = crate::runtime::vm::catch_traps(store, &mut previous_runtime_state, closure);
1437     #[cfg(feature = "component-model")]
1438     if result.is_err() {
1439         store.0.set_trapped();
1440     }
1441     core::mem::drop(previous_runtime_state);
1442     store.0.call_hook(CallHook::ReturningFromWasm)?;
1443     result
1444 }
1445 
1446 /// This type helps managing the state of the runtime when entering and exiting
1447 /// Wasm. To this end, it contains a subset of the data in `VMStoreContext`.
1448 /// Upon entering Wasm, it updates various runtime fields and their
1449 /// original values saved in this struct. Upon exiting Wasm, the previous values
1450 /// are restored.
1451 pub(crate) struct EntryStoreContext {
1452     /// If set, contains value of `stack_limit` field to restore in
1453     /// `VMStoreContext` when exiting Wasm.
1454     pub stack_limit: Option<usize>,
1455     pub last_wasm_exit_pc: usize,
1456     pub last_wasm_exit_trampoline_fp: usize,
1457     pub last_wasm_entry_fp: usize,
1458     pub last_wasm_entry_sp: usize,
1459     pub last_wasm_entry_trap_handler: usize,
1460     pub stack_chain: VMStackChain,
1461 
1462     /// We need a pointer to the runtime limits, so we can update them from
1463     /// `drop`/`exit_wasm`.
1464     vm_store_context: *const VMStoreContext,
1465 }
1466 
1467 impl EntryStoreContext {
1468     /// This function is called to update and save state when
1469     /// WebAssembly is entered within the `Store`.
1470     ///
1471     /// This updates various fields such as:
1472     ///
1473     /// * The stack limit. This is what ensures that we limit the stack space
1474     ///   allocated by WebAssembly code and it's relative to the initial stack
1475     ///   pointer that called into wasm.
1476     ///
1477     /// It also saves the different last_wasm_* values in the `VMStoreContext`.
1478     pub fn enter_wasm<T>(
1479         store: &mut StoreContextMut<'_, T>,
1480         initial_stack_information: *mut VMCommonStackInformation,
1481     ) -> Self {
1482         let stack_limit;
1483 
1484         // If this is a recursive call, e.g. our stack limit is already set, then
1485         // we may be able to skip this function.
1486         //
1487         // For synchronous stores there's nothing else to do because all wasm calls
1488         // happen synchronously and on the same stack. This means that the previous
1489         // stack limit will suffice for the next recursive call.
1490         //
1491         // For asynchronous stores then each call happens on a separate native
1492         // stack. This means that the previous stack limit is no longer relevant
1493         // because we're on a separate stack.
1494         if unsafe { *store.0.vm_store_context().stack_limit.get() } != usize::MAX
1495             && !store.0.can_block()
1496         {
1497             stack_limit = None;
1498         }
1499         // Ignore this stack pointer business on miri since we can't execute wasm
1500         // anyway and the concept of a stack pointer on miri is a bit nebulous
1501         // regardless.
1502         else if cfg!(miri) {
1503             stack_limit = None;
1504         } else {
1505             // When Cranelift has support for the host then we might be running native
1506             // compiled code meaning we need to read the actual stack pointer. If
1507             // Cranelift can't be used though then we're guaranteed to be running pulley
1508             // in which case this stack pointer isn't actually used as Pulley has custom
1509             // mechanisms for stack overflow.
1510             #[cfg(has_host_compiler_backend)]
1511             let stack_pointer = crate::runtime::vm::get_stack_pointer();
1512             #[cfg(not(has_host_compiler_backend))]
1513             let stack_pointer = {
1514                 use wasmtime_environ::TripleExt;
1515                 debug_assert!(store.engine().target().is_pulley());
1516                 usize::MAX
1517             };
1518 
1519             // Determine the stack pointer where, after which, any wasm code will
1520             // immediately trap. This is checked on the entry to all wasm functions.
1521             //
1522             // Note that this isn't 100% precise. We are requested to give wasm
1523             // `max_wasm_stack` bytes, but what we're actually doing is giving wasm
1524             // probably a little less than `max_wasm_stack` because we're
1525             // calculating the limit relative to this function's approximate stack
1526             // pointer. Wasm will be executed on a frame beneath this one (or next
1527             // to it). In any case it's expected to be at most a few hundred bytes
1528             // of slop one way or another. When wasm is typically given a MB or so
1529             // (a million bytes) the slop shouldn't matter too much.
1530             //
1531             // After we've got the stack limit then we store it into the `stack_limit`
1532             // variable.
1533             let wasm_stack_limit = stack_pointer
1534                 .checked_sub(store.engine().config().max_wasm_stack)
1535                 .unwrap();
1536             let prev_stack = unsafe {
1537                 mem::replace(
1538                     &mut *store.0.vm_store_context().stack_limit.get(),
1539                     wasm_stack_limit,
1540                 )
1541             };
1542             stack_limit = Some(prev_stack);
1543         }
1544 
1545         unsafe {
1546             let vm_store_context = store.0.vm_store_context();
1547             let new_stack_chain = VMStackChain::InitialStack(initial_stack_information);
1548             *vm_store_context.stack_chain.get() = new_stack_chain;
1549 
1550             Self {
1551                 stack_limit,
1552                 last_wasm_exit_pc: *(*vm_store_context).last_wasm_exit_pc.get(),
1553                 last_wasm_exit_trampoline_fp: *(*vm_store_context)
1554                     .last_wasm_exit_trampoline_fp
1555                     .get(),
1556                 last_wasm_entry_fp: *(*vm_store_context).last_wasm_entry_fp.get(),
1557                 last_wasm_entry_sp: *(*vm_store_context).last_wasm_entry_sp.get(),
1558                 last_wasm_entry_trap_handler: *(*vm_store_context)
1559                     .last_wasm_entry_trap_handler
1560                     .get(),
1561                 stack_chain: (*(*vm_store_context).stack_chain.get()).clone(),
1562                 vm_store_context,
1563             }
1564         }
1565     }
1566 
1567     /// This function restores the values stored in this struct. We invoke this
1568     /// function through this type's `Drop` implementation. This ensures that we
1569     /// even restore the values if we unwind the stack (e.g., because we are
1570     /// panicking out of a Wasm execution).
1571     #[inline]
1572     fn exit_wasm(&mut self) {
1573         unsafe {
1574             if let Some(limit) = self.stack_limit {
1575                 *(&*self.vm_store_context).stack_limit.get() = limit;
1576             }
1577 
1578             *(*self.vm_store_context).last_wasm_exit_trampoline_fp.get() =
1579                 self.last_wasm_exit_trampoline_fp;
1580             *(*self.vm_store_context).last_wasm_exit_pc.get() = self.last_wasm_exit_pc;
1581             *(*self.vm_store_context).last_wasm_entry_fp.get() = self.last_wasm_entry_fp;
1582             *(*self.vm_store_context).last_wasm_entry_sp.get() = self.last_wasm_entry_sp;
1583             *(*self.vm_store_context).last_wasm_entry_trap_handler.get() =
1584                 self.last_wasm_entry_trap_handler;
1585             *(*self.vm_store_context).stack_chain.get() = self.stack_chain.clone();
1586         }
1587     }
1588 }
1589 
1590 impl Drop for EntryStoreContext {
1591     #[inline]
1592     fn drop(&mut self) {
1593         self.exit_wasm();
1594     }
1595 }
1596 
1597 /// A trait implemented for types which can be returned from closures passed to
1598 /// [`Func::wrap`] and friends.
1599 ///
1600 /// This trait should not be implemented by user types. This trait may change at
1601 /// any time internally. The types which implement this trait, however, are
1602 /// stable over time.
1603 ///
1604 /// For more information see [`Func::wrap`]
1605 pub unsafe trait WasmRet {
1606     // Same as `WasmTy::compatible_with_store`.
1607     #[doc(hidden)]
1608     fn compatible_with_store(&self, store: &StoreOpaque) -> bool;
1609 
1610     /// Stores this return value into the `ptr` specified using the rooted
1611     /// `store`.
1612     ///
1613     /// Traps are communicated through the `Result<_>` return value.
1614     ///
1615     /// # Unsafety
1616     ///
1617     /// This method is unsafe as `ptr` must have the correct length to store
1618     /// this result. This property is only checked in debug mode, not in release
1619     /// mode.
1620     #[doc(hidden)]
1621     unsafe fn store(
1622         self,
1623         store: &mut AutoAssertNoGc<'_>,
1624         ptr: &mut [MaybeUninit<ValRaw>],
1625     ) -> Result<()>;
1626 
1627     #[doc(hidden)]
1628     fn func_type(
1629         engine: &Engine,
1630         params: impl Iterator<Item = ValType>,
1631     ) -> Result<FuncType, OutOfMemory>;
1632     #[doc(hidden)]
1633     fn may_gc() -> bool;
1634 
1635     // Utilities used to convert an instance of this type to a `Result`
1636     // explicitly, used when wrapping async functions which always bottom-out
1637     // in a function that returns a trap because futures can be cancelled.
1638     #[doc(hidden)]
1639     type Fallible: WasmRet;
1640     #[doc(hidden)]
1641     fn into_fallible(self) -> Self::Fallible;
1642     #[doc(hidden)]
1643     fn fallible_from_error(error: Error) -> Self::Fallible;
1644 }
1645 
1646 unsafe impl<T> WasmRet for T
1647 where
1648     T: WasmTy,
1649 {
1650     type Fallible = Result<T>;
1651 
1652     fn compatible_with_store(&self, store: &StoreOpaque) -> bool {
1653         <Self as WasmTy>::compatible_with_store(self, store)
1654     }
1655 
1656     unsafe fn store(
1657         self,
1658         store: &mut AutoAssertNoGc<'_>,
1659         ptr: &mut [MaybeUninit<ValRaw>],
1660     ) -> Result<()> {
1661         debug_assert!(ptr.len() > 0);
1662         // SAFETY: the contract of this function/trait combo is such that `ptr`
1663         // is valid to store this type's value, thus this lookup should be safe.
1664         unsafe { <Self as WasmTy>::store(self, store, ptr.get_unchecked_mut(0)) }
1665     }
1666 
1667     fn may_gc() -> bool {
1668         T::may_gc()
1669     }
1670 
1671     fn func_type(
1672         engine: &Engine,
1673         params: impl Iterator<Item = ValType>,
1674     ) -> Result<FuncType, OutOfMemory> {
1675         FuncType::try_new(engine, params, Some(<Self as WasmTy>::valtype()))
1676     }
1677 
1678     fn into_fallible(self) -> Result<T> {
1679         Ok(self)
1680     }
1681 
1682     fn fallible_from_error(error: Error) -> Result<T> {
1683         Err(error)
1684     }
1685 }
1686 
1687 unsafe impl<T> WasmRet for Result<T>
1688 where
1689     T: WasmRet,
1690 {
1691     type Fallible = Self;
1692 
1693     fn compatible_with_store(&self, store: &StoreOpaque) -> bool {
1694         match self {
1695             Ok(x) => <T as WasmRet>::compatible_with_store(x, store),
1696             Err(_) => true,
1697         }
1698     }
1699 
1700     unsafe fn store(
1701         self,
1702         store: &mut AutoAssertNoGc<'_>,
1703         ptr: &mut [MaybeUninit<ValRaw>],
1704     ) -> Result<()> {
1705         // SAFETY: the safety of calling this function is the same as calling
1706         // the inner `store`.
1707         unsafe { self.and_then(|val| val.store(store, ptr)) }
1708     }
1709 
1710     fn may_gc() -> bool {
1711         T::may_gc()
1712     }
1713 
1714     fn func_type(
1715         engine: &Engine,
1716         params: impl Iterator<Item = ValType>,
1717     ) -> Result<FuncType, OutOfMemory> {
1718         T::func_type(engine, params)
1719     }
1720 
1721     fn into_fallible(self) -> Result<T> {
1722         self
1723     }
1724 
1725     fn fallible_from_error(error: Error) -> Result<T> {
1726         Err(error)
1727     }
1728 }
1729 
1730 macro_rules! impl_wasm_host_results {
1731     ($n:tt $($t:ident)*) => (
1732         #[allow(non_snake_case, reason = "macro-generated code")]
1733         unsafe impl<$($t),*> WasmRet for ($($t,)*)
1734         where
1735             $($t: WasmTy,)*
1736         {
1737             type Fallible = Result<Self>;
1738 
1739             #[inline]
1740             fn compatible_with_store(&self, _store: &StoreOpaque) -> bool {
1741                 let ($($t,)*) = self;
1742                 $( $t.compatible_with_store(_store) && )* true
1743             }
1744 
1745             #[inline]
1746             unsafe fn store(
1747                 self,
1748                 _store: &mut AutoAssertNoGc<'_>,
1749                 _ptr: &mut [MaybeUninit<ValRaw>],
1750             ) -> Result<()> {
1751                 let ($($t,)*) = self;
1752                 let mut _cur = 0;
1753                 $(
1754                     debug_assert!(_cur < _ptr.len());
1755                     // SAFETY: `store`'s unsafe contract is that `_ptr` is
1756                     // appropriately sized and additionally safe to call `store`
1757                     // for sub-types.
1758                     unsafe {
1759                         let val = _ptr.get_unchecked_mut(_cur);
1760                         _cur += 1;
1761                         WasmTy::store($t, _store, val)?;
1762                     }
1763                 )*
1764                 Ok(())
1765             }
1766 
1767             #[doc(hidden)]
1768             fn may_gc() -> bool {
1769                 $( $t::may_gc() || )* false
1770             }
1771 
1772             fn func_type(
1773                 engine: &Engine,
1774                 params: impl Iterator<Item = ValType>,
1775             ) -> Result<FuncType, OutOfMemory> {
1776                 FuncType::try_new(
1777                     engine,
1778                     params,
1779                     IntoIterator::into_iter([$($t::valtype(),)*]),
1780                 )
1781             }
1782 
1783             #[inline]
1784             fn into_fallible(self) -> Result<Self> {
1785                 Ok(self)
1786             }
1787 
1788             #[inline]
1789             fn fallible_from_error(error: Error) -> Result<Self> {
1790                 Err(error)
1791             }
1792         }
1793     )
1794 }
1795 
1796 for_each_function_signature!(impl_wasm_host_results);
1797 
1798 /// Internal trait implemented for all arguments that can be passed to
1799 /// [`Func::wrap`] and [`Linker::func_wrap`](crate::Linker::func_wrap).
1800 ///
1801 /// This trait should not be implemented by external users, it's only intended
1802 /// as an implementation detail of this crate.
1803 pub trait IntoFunc<T, Params, Results>: Send + Sync + 'static {
1804     /// Convert this function into a `VM{Array,Native}CallHostFuncContext` and
1805     /// internal `VMFuncRef`.
1806     #[doc(hidden)]
1807     fn into_func(self, engine: &Engine) -> Result<HostFunc, OutOfMemory>;
1808 }
1809 
1810 macro_rules! impl_into_func {
1811     ($num:tt $arg:ident) => {
1812         // Implement for functions without a leading `Caller` parameter,
1813         // delegating to the implementation below which does have the leading
1814         // `Caller` parameter.
1815         #[expect(non_snake_case, reason = "macro-generated code")]
1816         impl<T, F, $arg, R> IntoFunc<T, $arg, R> for F
1817         where
1818             F: Fn($arg) -> R + Send + Sync + 'static,
1819             $arg: WasmTy,
1820             R: WasmRet,
1821             T: 'static,
1822         {
1823             fn into_func(self, engine: &Engine) -> Result<HostFunc, OutOfMemory> {
1824                 let f = move |_: Caller<'_, T>, $arg: $arg| {
1825                     self($arg)
1826                 };
1827 
1828                 f.into_func(engine)
1829             }
1830         }
1831 
1832         #[expect(non_snake_case, reason = "macro-generated code")]
1833         impl<T, F, $arg, R> IntoFunc<T, (Caller<'_, T>, $arg), R> for F
1834         where
1835             F: Fn(Caller<'_, T>, $arg) -> R + Send + Sync + 'static,
1836             $arg: WasmTy,
1837             R: WasmRet,
1838             T: 'static,
1839         {
1840             fn into_func(self, engine: &Engine) -> Result<HostFunc, OutOfMemory> {
1841                 HostFunc::wrap(engine, move |caller: Caller<'_, T>, ($arg,)| {
1842                     self(caller, $arg)
1843                 })
1844             }
1845         }
1846     };
1847     ($num:tt $($args:ident)*) => {
1848         // Implement for functions without a leading `Caller` parameter,
1849         // delegating to the implementation below which does have the leading
1850         // `Caller` parameter.
1851         #[allow(non_snake_case, reason = "macro-generated code")]
1852         impl<T, F, $($args,)* R> IntoFunc<T, ($($args,)*), R> for F
1853         where
1854             F: Fn($($args),*) -> R + Send + Sync + 'static,
1855             $($args: WasmTy,)*
1856             R: WasmRet,
1857             T: 'static,
1858         {
1859             fn into_func(self, engine: &Engine) -> Result<HostFunc, OutOfMemory> {
1860                 let f = move |_: Caller<'_, T>, $($args:$args),*| {
1861                     self($($args),*)
1862                 };
1863 
1864                 f.into_func(engine)
1865             }
1866         }
1867 
1868         #[allow(non_snake_case, reason = "macro-generated code")]
1869         impl<T, F, $($args,)* R> IntoFunc<T, (Caller<'_, T>, $($args,)*), R> for F
1870         where
1871             F: Fn(Caller<'_, T>, $($args),*) -> R + Send + Sync + 'static,
1872             $($args: WasmTy,)*
1873             R: WasmRet,
1874             T: 'static,
1875         {
1876             fn into_func(self, engine: &Engine) -> Result<HostFunc, OutOfMemory> {
1877                 HostFunc::wrap(engine, move |caller: Caller<'_, T>, ( $( $args ),* )| {
1878                     self(caller, $( $args ),* )
1879                 })
1880             }
1881         }
1882     }
1883 }
1884 
1885 for_each_function_signature!(impl_into_func);
1886 
1887 /// Trait implemented for various tuples made up of types which implement
1888 /// [`WasmTy`] that can be passed to [`Func::wrap_async`].
1889 pub unsafe trait WasmTyList {
1890     /// Get the value type that each Type in the list represents.
1891     fn valtypes() -> impl Iterator<Item = ValType>;
1892 
1893     // Load a version of `Self` from the `values` provided.
1894     //
1895     // # Safety
1896     //
1897     // This function is unsafe as it's up to the caller to ensure that `values` are
1898     // valid for this given type.
1899     #[doc(hidden)]
1900     unsafe fn load(store: &mut AutoAssertNoGc<'_>, values: &mut [MaybeUninit<ValRaw>]) -> Self;
1901 
1902     #[doc(hidden)]
1903     fn may_gc() -> bool;
1904 }
1905 
1906 macro_rules! impl_wasm_ty_list {
1907     ($num:tt $($args:ident)*) => (
1908         #[allow(non_snake_case, reason = "macro-generated code")]
1909         unsafe impl<$($args),*> WasmTyList for ($($args,)*)
1910         where
1911             $($args: WasmTy,)*
1912         {
1913             fn valtypes() -> impl Iterator<Item = ValType> {
1914                 IntoIterator::into_iter([$($args::valtype(),)*])
1915             }
1916 
1917             unsafe fn load(_store: &mut AutoAssertNoGc<'_>, _values: &mut [MaybeUninit<ValRaw>]) -> Self {
1918                 let mut _cur = 0;
1919                 ($({
1920                     debug_assert!(_cur < _values.len());
1921                     // SAFETY: this function's own contract means that `_values`
1922                     // is appropriately sized/typed for the internal loads.
1923                     unsafe {
1924                         let ptr = _values.get_unchecked(_cur).assume_init_ref();
1925                         _cur += 1;
1926                         $args::load(_store, ptr)
1927                     }
1928                 },)*)
1929             }
1930 
1931             fn may_gc() -> bool {
1932                 $( $args::may_gc() || )* false
1933             }
1934         }
1935     );
1936 }
1937 
1938 for_each_function_signature!(impl_wasm_ty_list);
1939 
1940 /// A structure representing the caller's context when creating a function
1941 /// via [`Func::wrap`].
1942 ///
1943 /// This structure can be taken as the first parameter of a closure passed to
1944 /// [`Func::wrap`] or other constructors, and serves two purposes:
1945 ///
1946 /// * First consumers can use [`Caller<'_, T>`](crate::Caller) to get access to
1947 ///   [`StoreContextMut<'_, T>`](crate::StoreContextMut) and/or get access to
1948 ///   `T` itself. This means that the [`Caller`] type can serve as a proxy to
1949 ///   the original [`Store`](crate::Store) itself and is used to satisfy
1950 ///   [`AsContext`] and [`AsContextMut`] bounds.
1951 ///
1952 /// * Second a [`Caller`] can be used as the name implies, learning about the
1953 ///   caller's context, namely it's exported memory and exported functions. This
1954 ///   allows functions which take pointers as arguments to easily read the
1955 ///   memory the pointers point into, or if a function is expected to call
1956 ///   malloc in the wasm module to reserve space for the output you can do that.
1957 ///
1958 /// Host functions which want access to [`Store`](crate::Store)-level state are
1959 /// recommended to use this type.
1960 pub struct Caller<'a, T: 'static> {
1961     pub(crate) store: StoreContextMut<'a, T>,
1962     caller: Instance,
1963 }
1964 
1965 impl<T> Caller<'_, T> {
1966     fn sub_caller(&mut self) -> Caller<'_, T> {
1967         Caller {
1968             store: self.store.as_context_mut(),
1969             caller: self.caller,
1970         }
1971     }
1972 
1973     /// Looks up an export from the caller's module by the `name` given.
1974     ///
1975     /// This is a low-level function that's typically used to implement passing
1976     /// of pointers or indices between core Wasm instances, where the callee
1977     /// needs to consult the caller's exports to perform memory management and
1978     /// resolve the references.
1979     ///
1980     /// For comparison, in components, the component model handles translating
1981     /// arguments from one component instance to another and managing memory, so
1982     /// that callees don't need to be aware of their callers, which promotes
1983     /// virtualizability of APIs.
1984     ///
1985     /// # Return
1986     ///
1987     /// If an export with the `name` provided was found, then it is returned as an
1988     /// `Extern`. There are a number of situations, however, where the export may not
1989     /// be available:
1990     ///
1991     /// * The caller instance may not have an export named `name`
1992     /// * There may not be a caller available, for example if `Func` was called
1993     ///   directly from host code.
1994     ///
1995     /// It's recommended to take care when calling this API and gracefully
1996     /// handling a `None` return value.
1997     pub fn get_export(&mut self, name: &str) -> Option<Extern> {
1998         // All instances created have a `host_state` with a pointer pointing
1999         // back to themselves. If this caller doesn't have that `host_state`
2000         // then it probably means it was a host-created object like `Func::new`
2001         // which doesn't have any exports we want to return anyway.
2002         self.caller.get_export(&mut self.store, name)
2003     }
2004 
2005     /// Looks up an exported [`Extern`] value by a [`ModuleExport`] value.
2006     ///
2007     /// This is similar to [`Self::get_export`] but uses a [`ModuleExport`]
2008     /// value to avoid string lookups where possible. [`ModuleExport`]s can be
2009     /// obtained by calling [`Module::get_export_index`] on the [`Module`] that
2010     /// an instance was instantiated with.
2011     ///
2012     /// This method will search the module for an export with a matching entity index and return
2013     /// the value, if found.
2014     ///
2015     /// Returns `None` if there was no export with a matching entity index.
2016     ///
2017     /// [`Module::get_export_index`]: crate::Module::get_export_index
2018     /// [`Module`]: crate::Module
2019     ///
2020     /// # Panics
2021     ///
2022     /// Panics if `store` does not own this instance.
2023     ///
2024     /// # Usage
2025     /// ```
2026     /// use std::str;
2027     ///
2028     /// # use wasmtime::*;
2029     /// # fn main() -> Result<()> {
2030     /// # let mut store = Store::default();
2031     ///
2032     /// let module = Module::new(
2033     ///     store.engine(),
2034     ///     r#"
2035     ///         (module
2036     ///             (import "" "" (func $log_str (param i32 i32)))
2037     ///             (func (export "foo")
2038     ///                 i32.const 4   ;; ptr
2039     ///                 i32.const 13  ;; len
2040     ///                 call $log_str)
2041     ///             (memory (export "memory") 1)
2042     ///             (data (i32.const 4) "Hello, world!"))
2043     ///     "#,
2044     /// )?;
2045     ///
2046     /// let Some(module_export) = module.get_export_index("memory") else {
2047     ///    bail!("failed to find `memory` export in module");
2048     /// };
2049     ///
2050     /// let log_str = Func::wrap(&mut store, move |mut caller: Caller<'_, ()>, ptr: i32, len: i32| {
2051     ///     let mem = match caller.get_module_export(&module_export) {
2052     ///         Some(Extern::Memory(mem)) => mem,
2053     ///         _ => bail!("failed to find host memory"),
2054     ///     };
2055     ///     let data = mem.data(&caller)
2056     ///         .get(ptr as u32 as usize..)
2057     ///         .and_then(|arr| arr.get(..len as u32 as usize));
2058     ///     let string = match data {
2059     ///         Some(data) => match str::from_utf8(data) {
2060     ///             Ok(s) => s,
2061     ///             Err(_) => bail!("invalid utf-8"),
2062     ///         },
2063     ///         None => bail!("pointer/length out of bounds"),
2064     ///     };
2065     ///     assert_eq!(string, "Hello, world!");
2066     ///     println!("{}", string);
2067     ///     Ok(())
2068     /// });
2069     /// let instance = Instance::new(&mut store, &module, &[log_str.into()])?;
2070     /// let foo = instance.get_typed_func::<(), ()>(&mut store, "foo")?;
2071     /// foo.call(&mut store, ())?;
2072     /// # Ok(())
2073     /// # }
2074     /// ```
2075     pub fn get_module_export(&mut self, export: &ModuleExport) -> Option<Extern> {
2076         self.caller.get_module_export(&mut self.store, export)
2077     }
2078 
2079     /// Access the underlying data owned by this `Store`.
2080     ///
2081     /// Same as [`Store::data`](crate::Store::data)
2082     pub fn data(&self) -> &T {
2083         self.store.data()
2084     }
2085 
2086     /// Access the underlying data owned by this `Store`.
2087     ///
2088     /// Same as [`Store::data_mut`](crate::Store::data_mut)
2089     pub fn data_mut(&mut self) -> &mut T {
2090         self.store.data_mut()
2091     }
2092 
2093     /// Returns the underlying [`Engine`] this store is connected to.
2094     pub fn engine(&self) -> &Engine {
2095         self.store.engine()
2096     }
2097 
2098     /// Perform garbage collection.
2099     ///
2100     /// Same as [`Store::gc`](crate::Store::gc).
2101     #[cfg(feature = "gc")]
2102     pub fn gc(&mut self, why: Option<&crate::GcHeapOutOfMemory<()>>) -> Result<()> {
2103         self.store.gc(why)
2104     }
2105 
2106     /// Perform garbage collection asynchronously.
2107     ///
2108     /// Same as [`Store::gc_async`](crate::Store::gc_async).
2109     #[cfg(all(feature = "async", feature = "gc"))]
2110     pub async fn gc_async(&mut self, why: Option<&crate::GcHeapOutOfMemory<()>>)
2111     where
2112         T: Send + 'static,
2113     {
2114         self.store.gc_async(why).await;
2115     }
2116 
2117     /// Returns the remaining fuel in the store.
2118     ///
2119     /// For more information see [`Store::get_fuel`](crate::Store::get_fuel)
2120     pub fn get_fuel(&self) -> Result<u64> {
2121         self.store.get_fuel()
2122     }
2123 
2124     /// Set the amount of fuel in this store to be consumed when executing wasm code.
2125     ///
2126     /// For more information see [`Store::set_fuel`](crate::Store::set_fuel)
2127     pub fn set_fuel(&mut self, fuel: u64) -> Result<()> {
2128         self.store.set_fuel(fuel)
2129     }
2130 
2131     /// Configures this `Store` to yield while executing futures every N units of fuel.
2132     ///
2133     /// For more information see
2134     /// [`Store::fuel_async_yield_interval`](crate::Store::fuel_async_yield_interval)
2135     #[cfg(feature = "async")]
2136     pub fn fuel_async_yield_interval(&mut self, interval: Option<u64>) -> Result<()> {
2137         self.store.fuel_async_yield_interval(interval)
2138     }
2139 }
2140 
2141 impl<T: 'static> AsContext for Caller<'_, T> {
2142     type Data = T;
2143     fn as_context(&self) -> StoreContext<'_, T> {
2144         self.store.as_context()
2145     }
2146 }
2147 
2148 impl<T: 'static> AsContextMut for Caller<'_, T> {
2149     fn as_context_mut(&mut self) -> StoreContextMut<'_, T> {
2150         self.store.as_context_mut()
2151     }
2152 }
2153 
2154 impl<'a, T: 'static> From<Caller<'a, T>> for StoreContextMut<'a, T> {
2155     fn from(caller: Caller<'a, T>) -> Self {
2156         caller.store
2157     }
2158 }
2159 
2160 /// Representation of a host-defined function.
2161 ///
2162 /// This is used for `Func::new` but also for `Linker`-defined functions. For
2163 /// `Func::new` this is stored within a `Store`, and for `Linker`-defined
2164 /// functions they wrap this up in `Arc` to enable shared ownership of this
2165 /// across many stores.
2166 ///
2167 /// Technically this structure needs a `<T>` type parameter to connect to the
2168 /// `Store<T>` itself, but that's an unsafe contract of using this for now
2169 /// rather than part of the struct type (to avoid `Func<T>` in the API).
2170 #[doc(hidden)]
2171 pub struct HostFunc {
2172     ctx: StoreBox<VMArrayCallHostFuncContext>,
2173 
2174     /// Whether or not this function was defined with an `async` host function,
2175     /// meaning that it is only invocable when wasm is itself on a fiber to
2176     /// support suspension on `Poll::Pending`.
2177     ///
2178     /// This is used to propagate to an `InstancePre` and then eventually into a
2179     /// `Store` as to whether the store requires async entrypoints.
2180     asyncness: Asyncness,
2181 
2182     // Stored to unregister this function's signature with the engine when this
2183     // is dropped.
2184     engine: Engine,
2185 }
2186 
2187 // State stored inside a `VMArrayCallHostFuncContext`.
2188 struct HostFuncState<F> {
2189     // The actual host function.
2190     func: F,
2191 
2192     // NB: We have to keep our `VMSharedTypeIndex` registered in the engine for
2193     // as long as this function exists.
2194     _ty: RegisteredType,
2195 }
2196 
2197 impl core::fmt::Debug for HostFunc {
2198     fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
2199         f.debug_struct("HostFunc").finish_non_exhaustive()
2200     }
2201 }
2202 
2203 impl HostFunc {
2204     /// Requires that the signature in `ctx` is already registered
2205     /// within `Engine`, which is done by [`Self::vmctx_sync`] and
2206     /// [`Self::vmctx_async`].
2207     ///
2208     /// This is an internal private constructor for this type intended to only
2209     /// be used by other constructors of this type below.
2210     fn new_raw(
2211         engine: &Engine,
2212         ctx: StoreBox<VMArrayCallHostFuncContext>,
2213         asyncness: Asyncness,
2214     ) -> Self {
2215         HostFunc {
2216             ctx,
2217             engine: engine.clone(),
2218             asyncness,
2219         }
2220     }
2221 
2222     /// Constructor of a host function's `VMContext` for synchronous functions.
2223     ///
2224     /// This creates a `VMArrayCallHostFuncContext` which under the hood will
2225     /// be viewed as `VMContext` in the eventually created `VMFuncRef`.
2226     fn vmctx_sync<F, T>(
2227         engine: &Engine,
2228         ty: FuncType,
2229         func: F,
2230     ) -> Result<StoreBox<VMArrayCallHostFuncContext>, OutOfMemory>
2231     where
2232         F: Fn(Caller<'_, T>, &mut [MaybeUninit<ValRaw>]) -> Result<()> + Send + Sync + 'static,
2233         T: 'static,
2234     {
2235         assert!(ty.comes_from_same_engine(engine));
2236 
2237         unsafe {
2238             VMArrayCallHostFuncContext::new(
2239                 Self::array_call_trampoline::<T, F>,
2240                 ty.type_index(),
2241                 try_new::<Box<_>>(HostFuncState {
2242                     func,
2243                     _ty: ty.into_registered_type(),
2244                 })?,
2245             )
2246         }
2247     }
2248 
2249     /// Constructor of a host function's `VMContext` for asynchronous functions.
2250     ///
2251     /// This creates a `VMArrayCallHostFuncContext` which under the hood will
2252     /// be viewed as `VMContext` in the eventually created `VMFuncRef`.
2253     ///
2254     /// Note that `ctx: U` is forwarded directly to `func`. This is subtly
2255     /// different than closing over data in `F` because the future returned by
2256     /// `F` can't refer to any data it closes over. The purpose of `U` is to
2257     /// enable the returned future to be able to close over data generally
2258     /// captured in the closures generated here.
2259     #[cfg(feature = "async")]
2260     fn vmctx_async<F, T, U>(
2261         engine: &Engine,
2262         ty: FuncType,
2263         ctx: U,
2264         func: F,
2265     ) -> Result<StoreBox<VMArrayCallHostFuncContext>, OutOfMemory>
2266     where
2267         F: for<'a> Fn(
2268                 Caller<'a, T>,
2269                 &'a mut [MaybeUninit<ValRaw>],
2270                 &'a U,
2271             ) -> Box<dyn Future<Output = Result<()>> + Send + 'a>
2272             + Send
2273             + Sync
2274             + 'static,
2275         T: 'static,
2276         U: Send + Sync + 'static,
2277     {
2278         // Eventually we want to remove `with_blocking` + `block_on` from
2279         // Wasmtime. For now this is the attempt to keep it as low-level as
2280         // possible.
2281         //
2282         // Generally it's a lie to run async things on top of sync things and
2283         // it's caused many headaches. For now it's the best that can be done.
2284         Self::vmctx_sync(engine, ty, move |Caller { store, caller }, args| {
2285             store.with_blocking(|store, cx| {
2286                 cx.block_on(core::pin::Pin::from(func(
2287                     Caller { store, caller },
2288                     args,
2289                     &ctx,
2290                 )))
2291             })?
2292         })
2293     }
2294 
2295     /// Entrypoint of WebAssembly back into the host.
2296     ///
2297     /// This is the standard wasmtime "array call signature" which then
2298     /// delegates internally to the host. This assumes that `callee_vmctx` is a
2299     /// `VMArrayCallHostFuncContext` which was built above with
2300     /// `HostFuncState<F>` internally. This internal function is then used to
2301     /// dispatch based on the arguments.
2302     ///
2303     /// Details handled by this wrapper are:
2304     ///
2305     /// * Host panics are handled (`enter_host_from_wasm`)
2306     /// * `F` is loaded from `callee_vmctx`
2307     /// * `Caller` is constructed to pass to `F`
2308     /// * A GC LIFO scope is maintained around the execution of `F`.
2309     /// * Call hooks for entering/leaving the host are maintained.
2310     unsafe extern "C" fn array_call_trampoline<T, F>(
2311         callee_vmctx: NonNull<VMOpaqueContext>,
2312         caller_vmctx: NonNull<VMContext>,
2313         args: NonNull<ValRaw>,
2314         args_len: usize,
2315     ) -> bool
2316     where
2317         F: Fn(Caller<'_, T>, &mut [MaybeUninit<ValRaw>]) -> Result<()> + 'static,
2318         T: 'static,
2319     {
2320         let run = |store: &mut dyn crate::vm::VMStore, instance: InstanceId| {
2321             // SAFETY: correct usage of this trampoline requires correct
2322             // ascription of `T`, so it's the caller's responsibility to line
2323             // this up.
2324             let mut store = unsafe { store.unchecked_context_mut() };
2325 
2326             // Handle the entry call hook, with a corresponding exit call hook
2327             // below.
2328             store.0.call_hook(CallHook::CallingHost)?;
2329 
2330             // SAFETY: this function itself requires that the `vmctx` is
2331             // valid to use here.
2332             let state = unsafe {
2333                 let vmctx = VMArrayCallHostFuncContext::from_opaque(callee_vmctx);
2334                 vmctx.as_ref().host_state()
2335             };
2336 
2337             // Double-check ourselves in debug mode, but we control the
2338             // `Any` here so an unsafe downcast should also work.
2339             //
2340             // SAFETY: this function is only usable with `F`.
2341             let state = unsafe {
2342                 debug_assert!(state.is::<HostFuncState<F>>());
2343                 &*(state as *const _ as *const HostFuncState<F>)
2344             };
2345 
2346             let (gc_lifo_scope, ret) = {
2347                 let gc_lifo_scope = store.0.gc_roots().enter_lifo_scope();
2348 
2349                 let mut args = NonNull::slice_from_raw_parts(args.cast(), args_len);
2350                 // SAFETY: it's a contract of this function itself that the values
2351                 // provided are valid to view as a slice.
2352                 let args = unsafe { args.as_mut() };
2353 
2354                 let ret = (state.func)(
2355                     Caller {
2356                         caller: Instance::from_wasmtime(instance, store.0),
2357                         store: store.as_context_mut(),
2358                     },
2359                     args,
2360                 );
2361 
2362                 (gc_lifo_scope, ret)
2363             };
2364 
2365             store.0.exit_gc_lifo_scope(gc_lifo_scope);
2366 
2367             // Note that if this returns a trap then `ret` is discarded
2368             // entirely.
2369             store.0.call_hook(CallHook::ReturningFromHost)?;
2370 
2371             ret
2372         };
2373 
2374         // SAFETY: this is an entrypoint of wasm which requires correct type
2375         // ascription of `T` itself, meaning that this should be safe to call
2376         // both `enter_host_from_wasm` as well as `unchecked_context_mut`.
2377         unsafe { vm::Instance::enter_host_from_wasm(caller_vmctx, run) }
2378     }
2379     /// Analog of [`Func::new_unchecked`]
2380     ///
2381     /// # Panics
2382     ///
2383     /// Panics if the given function type is not associated with the given
2384     /// engine.
2385     ///
2386     /// # Safety
2387     ///
2388     /// The `func` provided must operate according to the `ty` provided to
2389     /// ensure it's reading the correctly-typed parameters and writing the
2390     /// correctly-typed results.
2391     pub unsafe fn new_unchecked<T>(
2392         engine: &Engine,
2393         ty: FuncType,
2394         func: impl Fn(Caller<'_, T>, &mut [MaybeUninit<ValRaw>]) -> Result<()> + Send + Sync + 'static,
2395     ) -> Result<Self, OutOfMemory>
2396     where
2397         T: 'static,
2398     {
2399         Ok(HostFunc::new_raw(
2400             engine,
2401             Self::vmctx_sync(engine, ty, func)?,
2402             Asyncness::No,
2403         ))
2404     }
2405 
2406     /// Analog of [`Func::new`]
2407     ///
2408     /// # Panics
2409     ///
2410     /// Panics if the given function type is not associated with the given
2411     /// engine.
2412     pub fn new<T>(
2413         engine: &Engine,
2414         ty: FuncType,
2415         func: impl Fn(Caller<'_, T>, &[Val], &mut [Val]) -> Result<()> + Send + Sync + 'static,
2416     ) -> Result<Self, OutOfMemory>
2417     where
2418         T: 'static,
2419     {
2420         // NB: this is "duplicated" below in `new_async`, so try to keep the
2421         // two in sync.
2422         Ok(HostFunc::new_raw(
2423             engine,
2424             Self::vmctx_sync(engine, ty.clone(), move |mut caller, values| {
2425                 // SAFETY: Wasmtime in general provides the guarantee that
2426                 // `values` matches `ty`, so this should be safe.
2427                 let mut vec = unsafe { Self::load_untyped_params(caller.store.0, &ty, values) };
2428                 let (params, results) = vec.split_at_mut(ty.params().len());
2429                 func(caller.sub_caller(), params, results)?;
2430                 Self::store_untyped_results(caller.store, &ty, vec, values)
2431             })?,
2432             Asyncness::No,
2433         ))
2434     }
2435 
2436     /// Analog of [`Func::new_async`]
2437     ///
2438     /// # Panics
2439     ///
2440     /// Panics if the given function type is not associated with the given
2441     /// engine.
2442     #[cfg(feature = "async")]
2443     pub fn new_async<T, F>(engine: &Engine, ty: FuncType, func: F) -> Result<Self, OutOfMemory>
2444     where
2445         F: for<'a> Fn(
2446                 Caller<'a, T>,
2447                 &'a [Val],
2448                 &'a mut [Val],
2449             ) -> Box<dyn Future<Output = Result<()>> + Send + 'a>
2450             + Send
2451             + Sync
2452             + 'static,
2453         T: Send + 'static,
2454     {
2455         // NB: this is "duplicated" above in `new`, so try to keep the two in
2456         // sync.
2457         Ok(HostFunc::new_raw(
2458             engine,
2459             Self::vmctx_async(
2460                 engine,
2461                 ty.clone(),
2462                 (ty, func),
2463                 move |mut caller, values, (ty, func)| {
2464                     Box::new(async move {
2465                         // SAFETY: Wasmtime in general provides the guarantee that
2466                         // `values` matches `ty`, so this should be safe.
2467                         let mut vec =
2468                             unsafe { Self::load_untyped_params(caller.store.0, &ty, values) };
2469                         let (params, results) = vec.split_at_mut(ty.params().len());
2470                         core::pin::Pin::from(func(caller.sub_caller(), params, results)).await?;
2471                         Self::store_untyped_results(caller.store, &ty, vec, values)
2472                     })
2473                 },
2474             )?,
2475             Asyncness::Yes,
2476         ))
2477     }
2478 
2479     /// Loads the the parameters of `ty` from `params` into a vector.
2480     ///
2481     /// This additionally pushes space onto the vector for all results to split
2482     /// the vector into params/results halves.
2483     ///
2484     /// # Safety
2485     ///
2486     /// Requires that `params` matches the parameters loaded by `P`.
2487     unsafe fn load_untyped_params(
2488         store: &mut StoreOpaque,
2489         ty: &FuncType,
2490         params: &mut [MaybeUninit<ValRaw>],
2491     ) -> Vec<Val> {
2492         let mut val_vec = store.take_hostcall_val_storage();
2493         debug_assert!(val_vec.is_empty());
2494         let nparams = ty.params().len();
2495         val_vec.reserve(nparams + ty.results().len());
2496         let mut store = AutoAssertNoGc::new(store);
2497         for (i, ty) in ty.params().enumerate() {
2498             val_vec.push(unsafe { Val::_from_raw(&mut store, params[i].assume_init(), &ty) })
2499         }
2500 
2501         val_vec.extend((0..ty.results().len()).map(|_| Val::null_func_ref()));
2502         val_vec
2503     }
2504 
2505     /// Stores the results, at the end of `args_then_results` according to `ty`,
2506     /// into `storage`.
2507     fn store_untyped_results<T>(
2508         mut store: StoreContextMut<'_, T>,
2509         ty: &FuncType,
2510         mut args_then_results: Vec<Val>,
2511         storage: &mut [MaybeUninit<ValRaw>],
2512     ) -> Result<()> {
2513         // Unlike our arguments we need to dynamically check that the return
2514         // values produced are correct. There could be a bug in `func` that
2515         // produces the wrong number, wrong types, or wrong stores of
2516         // values, and we need to catch that here.
2517         let results = &args_then_results[ty.params().len()..];
2518         for (i, (ret, ty)) in results.iter().zip(ty.results()).enumerate() {
2519             ret.ensure_matches_ty(store.0, &ty)
2520                 .context("function attempted to return an incompatible value")?;
2521             storage[i].write(ret.to_raw(store.as_context_mut())?);
2522         }
2523 
2524         // Restore our `val_vec` back into the store so it's usable for the next
2525         // hostcall to reuse our own storage.
2526         args_then_results.truncate(0);
2527         store.0.save_hostcall_val_storage(args_then_results);
2528         Ok(())
2529     }
2530 
2531     /// Analog of [`Func::wrap`]
2532     pub fn wrap<T, F, P, R>(engine: &Engine, func: F) -> Result<Self, OutOfMemory>
2533     where
2534         F: Fn(Caller<'_, T>, P) -> R + Send + Sync + 'static,
2535         P: WasmTyList,
2536         R: WasmRet,
2537         T: 'static,
2538     {
2539         // NB: this entire function is "duplicated" below in `wrap_async`, so
2540         // try to keep the two in sync.
2541         let ty = R::func_type(engine, None::<ValType>.into_iter().chain(P::valtypes()))?;
2542 
2543         let ctx = Self::vmctx_sync(engine, ty, move |mut caller, args| {
2544             // SAFETY: `args` matching `ty` is provided by `HostFunc` and
2545             // wasmtime's ambient correctness.
2546             let params = unsafe { Self::load_typed_params(caller.store.0, args) };
2547             let ret = func(caller.sub_caller(), params).into_fallible();
2548             // SAFETY: `args` matching `ty` is provided by `HostFunc` and
2549             // wasmtime's ambient correctness.
2550             unsafe { Self::store_typed_results(caller.store.0, ret, args) }
2551         })?;
2552         Ok(HostFunc::new_raw(engine, ctx, Asyncness::No))
2553     }
2554 
2555     /// Analog of [`Func::wrap_async`]
2556     #[cfg(feature = "async")]
2557     pub fn wrap_async<T, F, P, R>(engine: &Engine, func: F) -> Result<Self, OutOfMemory>
2558     where
2559         F: for<'a> Fn(Caller<'a, T>, P) -> Box<dyn Future<Output = R> + Send + 'a>
2560             + Send
2561             + Sync
2562             + 'static,
2563         P: WasmTyList,
2564         R: WasmRet,
2565         T: Send + 'static,
2566     {
2567         // NB: this entire function is "duplicated" above in `wrap`, so try to
2568         // keep the two in sync.
2569         let ty = R::func_type(engine, None::<ValType>.into_iter().chain(P::valtypes()))?;
2570 
2571         let ctx = Self::vmctx_async(engine, ty, func, move |mut caller, args, func| {
2572             Box::new(async move {
2573                 // SAFETY: `args` matching `ty` is provided by `HostFunc` and
2574                 // wasmtime's ambient correctness.
2575                 let params = unsafe { Self::load_typed_params(caller.store.0, args) };
2576                 let ret = core::pin::Pin::from(func(caller.sub_caller(), params)).await;
2577                 // SAFETY: `args` matching `ty` is provided by `HostFunc` and
2578                 // wasmtime's ambient correctness.
2579                 unsafe { Self::store_typed_results(caller.store.0, ret.into_fallible(), args) }
2580             })
2581         })?;
2582         Ok(HostFunc::new_raw(engine, ctx, Asyncness::Yes))
2583     }
2584 
2585     /// Loads the typed parameters from `params`
2586     ///
2587     /// # Safety
2588     ///
2589     /// Requires that `params` matches the parameters loaded by `P`.
2590     unsafe fn load_typed_params<P>(store: &mut StoreOpaque, params: &mut [MaybeUninit<ValRaw>]) -> P
2591     where
2592         P: WasmTyList,
2593     {
2594         let mut store = if P::may_gc() {
2595             AutoAssertNoGc::new(store)
2596         } else {
2597             unsafe { AutoAssertNoGc::disabled(store) }
2598         };
2599         // SAFETY: this function's own safety contract is the same as `P::load`.
2600         unsafe { P::load(&mut store, params) }
2601     }
2602 
2603     /// Stores the results of `R` into the array provided.
2604     ///
2605     /// # Safety
2606     ///
2607     /// Requires that `ret` matches the result `storage` space. See `WasmRet`
2608     /// for more safety info.
2609     unsafe fn store_typed_results<R>(
2610         store: &mut StoreOpaque,
2611         ret: R,
2612         storage: &mut [MaybeUninit<ValRaw>],
2613     ) -> Result<()>
2614     where
2615         R: WasmRet,
2616     {
2617         ensure!(
2618             ret.compatible_with_store(store),
2619             "host function attempted to return cross-`Store` value to Wasm",
2620         );
2621 
2622         let mut store = if R::may_gc() {
2623             AutoAssertNoGc::new(store)
2624         } else {
2625             unsafe { AutoAssertNoGc::disabled(store) }
2626         };
2627         // SAFETY: this safety contract is the same as this own function's
2628         // safety contract.
2629         unsafe {
2630             ret.store(&mut store, storage)?;
2631         }
2632         Ok(())
2633     }
2634 
2635     /// Inserts this `HostFunc` into a `Store`, returning the `Func` pointing to
2636     /// it.
2637     ///
2638     /// # Unsafety
2639     ///
2640     /// Can only be inserted into stores with a matching `T` relative to when
2641     /// this `HostFunc` was first created.
2642     pub unsafe fn to_func(self: &Arc<Self>, store: &mut StoreOpaque) -> Result<Func, OutOfMemory> {
2643         self.validate_store(store);
2644         let (funcrefs, modules) = store.func_refs_and_modules();
2645         let funcref = funcrefs.push_arc_host(self.clone(), modules)?;
2646         // SAFETY: this funcref was just pushed within the store, so it's safe
2647         // to say this store owns it.
2648         Ok(unsafe { Func::from_vm_func_ref(store.id(), funcref) })
2649     }
2650 
2651     /// Inserts this `HostFunc` into a `Store`, returning the `Func` pointing to
2652     /// it.
2653     ///
2654     /// This function is similar to, but not equivalent, to `HostFunc::to_func`.
2655     /// Notably this function requires that the `Arc<Self>` pointer is otherwise
2656     /// rooted within the `StoreOpaque` via another means. When in doubt use
2657     /// `to_func` above as it's safer.
2658     ///
2659     /// # Unsafety
2660     ///
2661     /// Can only be inserted into stores with a matching `T` relative to when
2662     /// this `HostFunc` was first created.
2663     ///
2664     /// Additionally the `&Arc<Self>` is not cloned in this function. Instead a
2665     /// raw pointer to `Self` is stored within the `Store` for this function.
2666     /// The caller must arrange for the `Arc<Self>` to be "rooted" in the store
2667     /// provided via another means, probably by pushing to
2668     /// `StoreOpaque::rooted_host_funcs`.
2669     ///
2670     /// Similarly, the caller must arrange for `rooted_func_ref` to be rooted in
2671     /// the same store and additionally be a valid pointer.
2672     pub unsafe fn to_func_store_rooted(
2673         self: &Arc<Self>,
2674         store: &mut StoreOpaque,
2675         rooted_func_ref: Option<NonNull<VMFuncRef>>,
2676     ) -> Func {
2677         self.validate_store(store);
2678 
2679         match rooted_func_ref {
2680             Some(funcref) => {
2681                 // SAFETY: it's a contract of this function itself that
2682                 // `funcref` is safe to read.
2683                 unsafe {
2684                     debug_assert!(funcref.as_ref().wasm_call.is_some());
2685                 }
2686                 // SAFETY: it's a contract of this function that `funcref` is
2687                 // owned by `store`.
2688                 unsafe { Func::from_vm_func_ref(store.id(), funcref) }
2689             }
2690             None => {
2691                 debug_assert!(self.func_ref().wasm_call.is_some());
2692 
2693                 // SAFETY: it's an unsafe contract of this function that we are
2694                 // rooted within the store to say that the store owns a copy of
2695                 // this funcref.
2696                 unsafe { Func::from_vm_func_ref(store.id(), self.func_ref().into()) }
2697             }
2698         }
2699     }
2700 
2701     /// Same as [`HostFunc::to_func`], different ownership.
2702     unsafe fn into_func(self, store: &mut StoreOpaque) -> Result<Func, OutOfMemory> {
2703         self.validate_store(store);
2704 
2705         // This function could be called by a guest at any time, and it requires
2706         // fibers, so the store now required async entrypoints.
2707         store.set_async_required(self.asyncness);
2708 
2709         let (funcrefs, modules) = store.func_refs_and_modules();
2710         let funcref = funcrefs.push_box_host(try_new::<Box<_>>(self)?, modules)?;
2711         // SAFETY: this funcref was just pushed within `store`, so it's safe to
2712         // say it's owned by the store's id.
2713         Ok(unsafe { Func::from_vm_func_ref(store.id(), funcref) })
2714     }
2715 
2716     fn validate_store(&self, store: &mut StoreOpaque) {
2717         // This assert is required to ensure that we can indeed safely insert
2718         // `self` into the `store` provided, otherwise the type information we
2719         // have listed won't be correct. This is possible to hit with the public
2720         // API of Wasmtime, and should be documented in relevant functions.
2721         assert!(
2722             Engine::same(&self.engine, store.engine()),
2723             "cannot use a store with a different engine than a linker was created with",
2724         );
2725     }
2726 
2727     pub(crate) fn sig_index(&self) -> VMSharedTypeIndex {
2728         self.func_ref().type_index
2729     }
2730 
2731     pub(crate) fn func_ref(&self) -> &VMFuncRef {
2732         unsafe { self.ctx.get().as_ref().func_ref() }
2733     }
2734 
2735     pub(crate) fn asyncness(&self) -> Asyncness {
2736         self.asyncness
2737     }
2738 }
2739 
2740 #[cfg(test)]
2741 mod tests {
2742     use super::*;
2743     use crate::{Module, Store};
2744 
2745     #[test]
2746     #[cfg_attr(miri, ignore)]
2747     fn hash_key_is_stable_across_duplicate_store_data_entries() -> Result<()> {
2748         let mut store = Store::<()>::default();
2749         let module = Module::new(
2750             store.engine(),
2751             r#"
2752                 (module
2753                     (func (export "f")
2754                         nop
2755                     )
2756                 )
2757             "#,
2758         )?;
2759         let instance = Instance::new(&mut store, &module, &[])?;
2760 
2761         // Each time we `get_func`, we call `Func::from_wasmtime` which adds a
2762         // new entry to `StoreData`, so `f1` and `f2` will have different
2763         // indices into `StoreData`.
2764         let f1 = instance.get_func(&mut store, "f").unwrap();
2765         let f2 = instance.get_func(&mut store, "f").unwrap();
2766 
2767         // But their hash keys are the same.
2768         assert!(
2769             f1.hash_key(&mut store.as_context_mut().0)
2770                 == f2.hash_key(&mut store.as_context_mut().0)
2771         );
2772 
2773         // But the hash keys are different from different funcs.
2774         let instance2 = Instance::new(&mut store, &module, &[])?;
2775         let f3 = instance2.get_func(&mut store, "f").unwrap();
2776         assert!(
2777             f1.hash_key(&mut store.as_context_mut().0)
2778                 != f3.hash_key(&mut store.as_context_mut().0)
2779         );
2780 
2781         Ok(())
2782     }
2783 }
2784