Skip to main content

wasmtime/runtime/component/
func.rs

1use crate::component::RuntimeInstance;
2use crate::component::instance::Instance;
3use crate::component::matching::InstanceType;
4use crate::component::storage::storage_as_slice;
5use crate::component::types::ComponentFunc;
6use crate::component::values::Val;
7use crate::prelude::*;
8use crate::runtime::vm::component::{ComponentInstance, InstanceFlags};
9use crate::runtime::vm::{Export, VMFuncRef};
10use crate::store::StoreOpaque;
11use crate::{AsContext, AsContextMut, StoreContextMut, ValRaw};
12use core::mem::{self, MaybeUninit};
13use core::ptr::NonNull;
14use wasmtime_environ::component::{
15    CanonicalOptions, ExportIndex, InterfaceType, MAX_FLAT_PARAMS, MAX_FLAT_RESULTS, OptionsIndex,
16    TypeFuncIndex, TypeTuple,
17};
18
19#[cfg(feature = "component-model-async")]
20use crate::component::concurrent::{self, AsAccessor, PreparedCall};
21
22mod host;
23mod options;
24mod typed;
25pub use self::host::*;
26pub use self::options::*;
27pub use self::typed::*;
28
29/// A WebAssembly component function which can be called.
30///
31/// This type is the dual of [`wasmtime::Func`](crate::Func) for component
32/// functions. An instance of [`Func`] represents a component function from a
33/// component [`Instance`](crate::component::Instance). Like with
34/// [`wasmtime::Func`](crate::Func) it's possible to call functions either
35/// synchronously or asynchronously and either typed or untyped.
36#[derive(Copy, Clone, Debug)]
37#[repr(C)] // here for the C API.
38pub struct Func {
39    instance: Instance,
40    index: ExportIndex,
41}
42
43// Double-check that the C representation in `component/instance.h` matches our
44// in-Rust representation here in terms of size/alignment/etc.
45const _: () = {
46    #[repr(C)]
47    struct T(u64, u32);
48    #[repr(C)]
49    struct C(T, u32);
50    assert!(core::mem::size_of::<C>() == core::mem::size_of::<Func>());
51    assert!(core::mem::align_of::<C>() == core::mem::align_of::<Func>());
52    assert!(core::mem::offset_of!(Func, instance) == 0);
53};
54
55impl Func {
56    pub(crate) fn from_lifted_func(instance: Instance, index: ExportIndex) -> Func {
57        Func { instance, index }
58    }
59
60    /// Attempt to cast this [`Func`] to a statically typed [`TypedFunc`] with
61    /// the provided `Params` and `Return`.
62    ///
63    /// This function will perform a type-check at runtime that the [`Func`]
64    /// takes `Params` as parameters and returns `Return`. If the type-check
65    /// passes then a [`TypedFunc`] will be returned which can be used to
66    /// invoke the function in an efficient, statically-typed, and ergonomic
67    /// manner.
68    ///
69    /// The `Params` type parameter here is a tuple of the parameters to the
70    /// function. A function which takes no arguments should use `()`, a
71    /// function with one argument should use `(T,)`, etc. Note that all
72    /// `Params` must also implement the [`Lower`] trait since they're going
73    /// into wasm.
74    ///
75    /// The `Return` type parameter is the return value of this function. A
76    /// return value of `()` means that there's no return (similar to a Rust
77    /// unit return) and otherwise a type `T` can be specified. Note that the
78    /// `Return` must also implement the [`Lift`] trait since it's coming from
79    /// wasm.
80    ///
81    /// Types specified here must implement the [`ComponentType`] trait. This
82    /// trait is implemented for built-in types to Rust such as integer
83    /// primitives, floats, `Option<T>`, `Result<T, E>`, strings, `Vec<T>`, and
84    /// more. As parameters you'll be passing native Rust types.
85    ///
86    /// See the documentation for [`ComponentType`] for more information about
87    /// supported types.
88    ///
89    /// # Errors
90    ///
91    /// If the function does not actually take `Params` as its parameters or
92    /// return `Return` then an error will be returned.
93    ///
94    /// # Panics
95    ///
96    /// This function will panic if `self` is not owned by the `store`
97    /// specified.
98    ///
99    /// # Examples
100    ///
101    /// Calling a function which takes no parameters and has no return value:
102    ///
103    /// ```
104    /// # use wasmtime::component::Func;
105    /// # use wasmtime::Store;
106    /// # fn foo(func: &Func, store: &mut Store<()>) -> wasmtime::Result<()> {
107    /// let typed = func.typed::<(), ()>(&store)?;
108    /// typed.call(store, ())?;
109    /// # Ok(())
110    /// # }
111    /// ```
112    ///
113    /// Calling a function which takes one string parameter and returns a
114    /// string:
115    ///
116    /// ```
117    /// # use wasmtime::component::Func;
118    /// # use wasmtime::Store;
119    /// # fn foo(func: &Func, mut store: Store<()>) -> wasmtime::Result<()> {
120    /// let typed = func.typed::<(&str,), (String,)>(&store)?;
121    /// let ret = typed.call(&mut store, ("Hello, ",))?.0;
122    /// println!("returned string was: {}", ret);
123    /// # Ok(())
124    /// # }
125    /// ```
126    ///
127    /// Calling a function which takes multiple parameters and returns a boolean:
128    ///
129    /// ```
130    /// # use wasmtime::component::Func;
131    /// # use wasmtime::Store;
132    /// # fn foo(func: &Func, mut store: Store<()>) -> wasmtime::Result<()> {
133    /// let typed = func.typed::<(u32, Option<&str>, &[u8]), (bool,)>(&store)?;
134    /// let ok: bool = typed.call(&mut store, (1, Some("hello"), b"bytes!"))?.0;
135    /// println!("return value was: {ok}");
136    /// # Ok(())
137    /// # }
138    /// ```
139    pub fn typed<Params, Return>(&self, store: impl AsContext) -> Result<TypedFunc<Params, Return>>
140    where
141        Params: ComponentNamedList + Lower,
142        Return: ComponentNamedList + Lift,
143    {
144        self._typed(store.as_context().0, None)
145    }
146
147    pub(crate) fn _typed<Params, Return>(
148        &self,
149        store: &StoreOpaque,
150        instance: Option<&ComponentInstance>,
151    ) -> Result<TypedFunc<Params, Return>>
152    where
153        Params: ComponentNamedList + Lower,
154        Return: ComponentNamedList + Lift,
155    {
156        self.typecheck::<Params, Return>(store, instance)?;
157        unsafe { Ok(TypedFunc::new_unchecked(*self)) }
158    }
159
160    fn typecheck<Params, Return>(
161        &self,
162        store: &StoreOpaque,
163        instance: Option<&ComponentInstance>,
164    ) -> Result<()>
165    where
166        Params: ComponentNamedList + Lower,
167        Return: ComponentNamedList + Lift,
168    {
169        let cx = InstanceType::new(instance.unwrap_or_else(|| self.instance.id().get(store)));
170        let ty = &cx.types[self.ty_index(store)];
171
172        Params::typecheck(&InterfaceType::Tuple(ty.params), &cx)
173            .context("type mismatch with parameters")?;
174        Return::typecheck(&InterfaceType::Tuple(ty.results), &cx)
175            .context("type mismatch with results")?;
176
177        Ok(())
178    }
179
180    /// Get the type of this function.
181    pub fn ty(&self, store: impl AsContext) -> ComponentFunc {
182        self.ty_(store.as_context().0)
183    }
184
185    fn ty_(&self, store: &StoreOpaque) -> ComponentFunc {
186        let cx = InstanceType::new(self.instance.id().get(store));
187        let ty = self.ty_index(store);
188        ComponentFunc::from(ty, &cx)
189    }
190
191    fn ty_index(&self, store: &StoreOpaque) -> TypeFuncIndex {
192        let instance = self.instance.id().get(store);
193        let (ty, _, _) = instance.component().export_lifted_function(self.index);
194        ty
195    }
196
197    /// Invokes this function with the `params` given and returns the result.
198    ///
199    /// The `params` provided must match the parameters that this function takes
200    /// in terms of their types and the number of parameters. Results will be
201    /// written to the `results` slice provided if the call completes
202    /// successfully. The initial types of the values in `results` are ignored
203    /// and values are overwritten to write the result. It's required that the
204    /// size of `results` exactly matches the number of results that this
205    /// function produces.
206    ///
207    /// This will also call the corresponding `post-return` function, if any.
208    ///
209    /// For more detailed information see the documentation of
210    /// [`TypedFunc::call`].
211    ///
212    /// # Errors
213    ///
214    /// Returns an error in situations including but not limited to:
215    ///
216    /// * `params` is not the right size or if the values have the wrong type
217    /// * `results` is not the right size
218    /// * A trap occurs while executing the function
219    /// * The function calls a host function which returns an error
220    /// * The `store` used requires the use of [`Func::call_async`] instead. See
221    ///   [store documentation](crate#async) for more information.
222    ///
223    /// See [`TypedFunc::call`] for more information in addition to
224    /// [`wasmtime::Func::call`](crate::Func::call).
225    ///
226    /// # Panics
227    ///
228    /// Panics if `store` does not own this function.
229    pub fn call(
230        &self,
231        mut store: impl AsContextMut,
232        params: &[Val],
233        results: &mut [Val],
234    ) -> Result<()> {
235        let mut store = store.as_context_mut();
236        store.0.validate_sync_call()?;
237        self.call_impl(store.as_context_mut(), params, results)?;
238        Ok(())
239    }
240
241    /// Exactly like [`Self::call`] except for use on async stores.
242    ///
243    /// # Panics
244    ///
245    /// Panics if `store` does not own this function.
246    #[cfg(feature = "async")]
247    pub async fn call_async(
248        &self,
249        mut store: impl AsContextMut<Data: Send>,
250        params: &[Val],
251        results: &mut [Val],
252    ) -> Result<()> {
253        let store = store.as_context_mut();
254
255        #[cfg(feature = "component-model-async")]
256        if store.0.concurrency_support() {
257            return store
258                .run_concurrent_trap_on_idle(async |store| {
259                    self.call_concurrent_dynamic(store, params, results)
260                        .await
261                        .map(drop)
262                })
263                .await?;
264        }
265
266        let mut store = store;
267        store
268            .on_fiber(|store| self.call_impl(store, params, results))
269            .await?
270    }
271
272    fn check_params_results<T>(
273        &self,
274        store: StoreContextMut<T>,
275        params: &[Val],
276        results: &mut [Val],
277    ) -> Result<()> {
278        let ty = self.ty(&store);
279        if ty.params().len() != params.len() {
280            bail!(
281                "expected {} argument(s), got {}",
282                ty.params().len(),
283                params.len(),
284            );
285        }
286
287        if ty.results().len() != results.len() {
288            bail!(
289                "expected {} result(s), got {}",
290                ty.results().len(),
291                results.len(),
292            );
293        }
294
295        Ok(())
296    }
297
298    /// Start a concurrent call to this function.
299    ///
300    /// Concurrency is achieved by relying on the [`Accessor`] argument, which
301    /// can be obtained by calling [`StoreContextMut::run_concurrent`].
302    ///
303    /// Unlike [`Self::call`] and [`Self::call_async`] (both of which require
304    /// exclusive access to the store until the completion of the call), calls
305    /// made using this method may run concurrently with other calls to the same
306    /// instance.  In addition, the runtime will call the `post-return` function
307    /// (if any) automatically when the guest task completes.
308    ///
309    /// # Progress
310    ///
311    /// For the wasm task being created in `call_concurrent` to make progress it
312    /// must be run within the scope of [`run_concurrent`]. If there are no
313    /// active calls to [`run_concurrent`] then the wasm task will appear as
314    /// stalled. This is typically not a concern as an [`Accessor`] is bound
315    /// by default to a scope of [`run_concurrent`].
316    ///
317    /// One situation in which this can arise, for example, is that if a
318    /// [`run_concurrent`] computation finishes its async closure before all
319    /// wasm tasks have completed, then there will be no scope of
320    /// [`run_concurrent`] anywhere. In this situation the wasm tasks that have
321    /// not yet completed will not make progress until [`run_concurrent`] is
322    /// called again.
323    ///
324    /// Embedders will need to ensure that this future is `await`'d within the
325    /// scope of [`run_concurrent`] to ensure that the value can be produced
326    /// during the `await` call.
327    ///
328    /// # Cancellation
329    ///
330    /// Cancelling an async task created via `call_concurrent`, at this time, is
331    /// only possible by dropping the store that the computation runs within.
332    /// With [#11833] implemented then it will be possible to request
333    /// cancellation of a task, but that is not yet implemented. Hard-cancelling
334    /// a task will only ever be possible by dropping the entire store and it is
335    /// not possible to remove just one task from a store.
336    ///
337    /// This async function behaves more like a "spawn" than a normal Rust async
338    /// function. When this function is invoked then metadata for the function
339    /// call is recorded in the store connected to the `accessor` argument and
340    /// the wasm invocation is from then on connected to the store. If the
341    /// future created by this function is dropped it does not cancel the
342    /// in-progress execution of the wasm task. Dropping the future
343    /// relinquishes the host's ability to learn about the result of the task
344    /// but the task will still progress and invoke callbacks and such until
345    /// completion.
346    ///
347    /// This function will return an error if [`Config::concurrency_support`] is
348    /// disabled.
349    ///
350    /// [`Config::concurrency_support`]: crate::Config::concurrency_support
351    /// [`run_concurrent`]: crate::Store::run_concurrent
352    /// [#11833]: https://github.com/bytecodealliance/wasmtime/issues/11833
353    /// [`Accessor`]: crate::component::Accessor
354    ///
355    /// # Panics
356    ///
357    /// Panics if the store that the [`Accessor`] is derived from does not own
358    /// this function.
359    ///
360    /// # Example
361    ///
362    /// Using [`StoreContextMut::run_concurrent`] to get an [`Accessor`]:
363    ///
364    /// ```
365    /// # use {
366    /// #   wasmtime::{
367    /// #     error::{Result},
368    /// #     component::{Component, Linker, ResourceTable},
369    /// #     Config, Engine, Store
370    /// #   },
371    /// # };
372    /// #
373    /// # struct Ctx { table: ResourceTable }
374    /// #
375    /// # async fn foo() -> Result<()> {
376    /// # let mut config = Config::new();
377    /// # let engine = Engine::new(&config)?;
378    /// # let mut store = Store::new(&engine, Ctx { table: ResourceTable::new() });
379    /// # let mut linker = Linker::new(&engine);
380    /// # let component = Component::new(&engine, "")?;
381    /// # let instance = linker.instantiate_async(&mut store, &component).await?;
382    /// let my_func = instance.get_func(&mut store, "my_func").unwrap();
383    /// store.run_concurrent(async |accessor| -> wasmtime::Result<_> {
384    ///    my_func.call_concurrent(accessor, &[], &mut Vec::new()).await?;
385    ///    Ok(())
386    /// }).await??;
387    /// # Ok(())
388    /// # }
389    /// ```
390    #[cfg(feature = "component-model-async")]
391    pub async fn call_concurrent(
392        self,
393        accessor: impl AsAccessor<Data: Send>,
394        params: &[Val],
395        results: &mut [Val],
396    ) -> Result<()> {
397        self.call_concurrent_dynamic(accessor, params, results)
398            .await
399    }
400
401    /// Internal helper function for `call_async` and `call_concurrent`.
402    #[cfg(feature = "component-model-async")]
403    async fn call_concurrent_dynamic(
404        self,
405        accessor: impl AsAccessor<Data: Send>,
406        params: &[Val],
407        results: &mut [Val],
408    ) -> Result<()> {
409        let result = accessor.as_accessor().with(|mut store| {
410            self.check_params_results(store.as_context_mut(), params, results)?;
411            let prepared = self.prepare_call_dynamic(store.as_context_mut(), params.to_vec())?;
412            concurrent::queue_call(store.as_context_mut(), prepared)
413        })?;
414
415        let run_results = result.await?;
416        assert_eq!(run_results.len(), results.len());
417        for (result, slot) in run_results.into_iter().zip(results) {
418            *slot = result;
419        }
420        Ok(())
421    }
422
423    /// Calls `concurrent::prepare_call` with monomorphized functions for
424    /// lowering the parameters and lifting the result.
425    #[cfg(feature = "component-model-async")]
426    fn prepare_call_dynamic<'a, T: Send + 'static>(
427        self,
428        mut store: StoreContextMut<'a, T>,
429        params: Vec<Val>,
430    ) -> Result<PreparedCall<Vec<Val>>> {
431        let store = store.as_context_mut();
432
433        concurrent::prepare_call(
434            store,
435            self,
436            MAX_FLAT_PARAMS,
437            false,
438            move |func, store, params_out| {
439                func.with_lower_context(store, |cx, ty| {
440                    Self::lower_args(cx, &params, ty, params_out)
441                })
442            },
443            move |func, store, results| {
444                let max_flat = if func.abi_async(store) {
445                    MAX_FLAT_PARAMS
446                } else {
447                    MAX_FLAT_RESULTS
448                };
449                let results = func.with_lift_context(store, |cx, ty| {
450                    Self::lift_results(cx, ty, results, max_flat)?.collect::<Result<Vec<_>>>()
451                })?;
452                Ok(Box::new(results))
453            },
454        )
455    }
456
457    fn call_impl(
458        &self,
459        mut store: impl AsContextMut,
460        params: &[Val],
461        results: &mut [Val],
462    ) -> Result<()> {
463        let mut store = store.as_context_mut();
464
465        self.check_params_results(store.as_context_mut(), params, results)?;
466
467        if self.abi_async(store.0) {
468            unreachable!(
469                "async-lifted exports should have failed validation \
470                 when `component-model-async` feature disabled"
471            );
472        }
473
474        // SAFETY: the chosen representations of type parameters to `call_raw`
475        // here should be generally safe to work with:
476        //
477        // * parameters use `MaybeUninit<[MaybeUninit<ValRaw>; MAX_FLAT_PARAMS]>`
478        //   which represents the maximal possible number of parameters that can
479        //   be passed to lifted component functions. This is modeled with
480        //   `MaybeUninit` to represent how it all starts as uninitialized and
481        //   thus can't be safely read during lowering.
482        //
483        // * results are modeled as `[ValRaw; MAX_FLAT_RESULTS]` which
484        //   represents the maximal size of values that can be returned. Note
485        //   that if the function doesn't actually have a return value then the
486        //   `ValRaw` inside the array will have undefined contents. That is
487        //   safe in Rust, however, due to `ValRaw` being a `union`. The
488        //   contents should dynamically not be read due to the type of the
489        //   function used here matching the actual lift.
490        let (_, post_return_arg) = unsafe {
491            self.call_raw(
492                store.as_context_mut(),
493                |cx, ty, dst: &mut MaybeUninit<[MaybeUninit<ValRaw>; MAX_FLAT_PARAMS]>| {
494                    // SAFETY: it's safe to assume that
495                    // `MaybeUninit<array-of-maybe-uninit>` is initialized because
496                    // each individual element is still considered uninitialized.
497                    let dst: &mut [MaybeUninit<ValRaw>] = dst.assume_init_mut();
498                    Self::lower_args(cx, params, ty, dst)
499                },
500                |cx, results_ty, src: &[ValRaw; MAX_FLAT_RESULTS]| {
501                    let max_flat = MAX_FLAT_RESULTS;
502                    for (result, slot) in
503                        Self::lift_results(cx, results_ty, src, max_flat)?.zip(results)
504                    {
505                        *slot = result?;
506                    }
507                    Ok(())
508                },
509            )?
510        };
511
512        self.post_return_impl(store, post_return_arg)
513    }
514
515    pub(crate) fn lifted_core_func(&self, store: &mut StoreOpaque) -> NonNull<VMFuncRef> {
516        let def = {
517            let instance = self.instance.id().get(store);
518            let (_ty, def, _options) = instance.component().export_lifted_function(self.index);
519            def.clone()
520        };
521        match self.instance.lookup_vmdef(store, &def) {
522            Export::Function(f) => f.vm_func_ref(store),
523            _ => unreachable!(),
524        }
525    }
526
527    pub(crate) fn post_return_core_func(&self, store: &StoreOpaque) -> Option<NonNull<VMFuncRef>> {
528        let instance = self.instance.id().get(store);
529        let component = instance.component();
530        let (_ty, _def, options) = component.export_lifted_function(self.index);
531        let post_return = component.env_component().options[options].post_return;
532        post_return.map(|i| instance.runtime_post_return(i))
533    }
534
535    pub(crate) fn abi_async(&self, store: &StoreOpaque) -> bool {
536        let instance = self.instance.id().get(store);
537        let component = instance.component();
538        let (_ty, _def, options) = component.export_lifted_function(self.index);
539        component.env_component().options[options].async_
540    }
541
542    pub(crate) fn abi_info<'a>(
543        &self,
544        store: &'a StoreOpaque,
545    ) -> (
546        OptionsIndex,
547        InstanceFlags,
548        TypeFuncIndex,
549        &'a CanonicalOptions,
550    ) {
551        let vminstance = self.instance.id().get(store);
552        let component = vminstance.component();
553        let (ty, _def, options_index) = component.export_lifted_function(self.index);
554        let raw_options = &component.env_component().options[options_index];
555        (
556            options_index,
557            vminstance.instance_flags(raw_options.instance),
558            ty,
559            raw_options,
560        )
561    }
562
563    /// Invokes the underlying wasm function, lowering arguments and lifting the
564    /// result.
565    ///
566    /// The `lower` function and `lift` function provided here are what actually
567    /// do the lowering and lifting. The `LowerParams` and `LowerReturn` types
568    /// are what will be allocated on the stack for this function call. They
569    /// should be appropriately sized for the lowering/lifting operation
570    /// happening.
571    ///
572    /// # Safety
573    ///
574    /// The safety of this function relies on the correct definitions of the
575    /// `LowerParams` and `LowerReturn` type. They must match the type of `self`
576    /// for the params/results that are going to be produced. Additionally
577    /// these types must be representable with a sequence of `ValRaw` values.
578    unsafe fn call_raw<T, Return, LowerParams, LowerReturn>(
579        &self,
580        mut store: StoreContextMut<'_, T>,
581        lower: impl FnOnce(
582            &mut LowerContext<'_, T>,
583            InterfaceType,
584            &mut MaybeUninit<LowerParams>,
585        ) -> Result<()>,
586        lift: impl FnOnce(&mut LiftContext<'_>, InterfaceType, &LowerReturn) -> Result<Return>,
587    ) -> Result<(Return, ValRaw)>
588    where
589        LowerParams: Copy,
590        LowerReturn: Copy,
591    {
592        let export = self.lifted_core_func(store.0);
593        let (_options, _flags, _ty, raw_options) = self.abi_info(store.0);
594        let instance = RuntimeInstance {
595            instance: self.instance.id().instance(),
596            index: raw_options.instance,
597        };
598
599        if !store.0.may_enter(instance) {
600            bail!(crate::Trap::CannotEnterComponent);
601        }
602
603        let async_type = self.abi_async(store.0);
604        store.0.enter_guest_sync_call(None, async_type, instance)?;
605
606        #[repr(C)]
607        union Union<Params: Copy, Return: Copy> {
608            params: Params,
609            ret: Return,
610        }
611
612        let space = &mut MaybeUninit::<Union<LowerParams, LowerReturn>>::uninit();
613
614        // Double-check the size/alignment of `space`, just in case.
615        //
616        // Note that this alone is not enough to guarantee the validity of the
617        // `unsafe` block below, but it's definitely required. In any case LLVM
618        // should be able to trivially see through these assertions and remove
619        // them in release mode.
620        let val_size = mem::size_of::<ValRaw>();
621        let val_align = mem::align_of::<ValRaw>();
622        assert!(mem::size_of_val(space) % val_size == 0);
623        assert!(mem::size_of_val(map_maybe_uninit!(space.params)) % val_size == 0);
624        assert!(mem::size_of_val(map_maybe_uninit!(space.ret)) % val_size == 0);
625        assert!(mem::align_of_val(space) == val_align);
626        assert!(mem::align_of_val(map_maybe_uninit!(space.params)) == val_align);
627        assert!(mem::align_of_val(map_maybe_uninit!(space.ret)) == val_align);
628
629        self.with_lower_context(store.as_context_mut(), |cx, ty| {
630            lower(cx, ty, map_maybe_uninit!(space.params))
631        })?;
632
633        // SAFETY: We are providing the guarantee that all the inputs are valid.
634        // The various pointers passed in for the function are all valid since
635        // they're coming from our store, and the `params_and_results` should
636        // have the correct layout for the core wasm function we're calling.
637        // Note that this latter point relies on the correctness of this module
638        // and `ComponentType` implementations, hence `ComponentType` being an
639        // `unsafe` trait.
640        unsafe {
641            crate::Func::call_unchecked_raw(
642                &mut store,
643                export,
644                NonNull::new(core::ptr::slice_from_raw_parts_mut(
645                    space.as_mut_ptr().cast(),
646                    mem::size_of_val(space) / mem::size_of::<ValRaw>(),
647                ))
648                .unwrap(),
649            )?;
650        }
651
652        // SAFETY: We're relying on the correctness of the structure of
653        // `LowerReturn` and the type-checking performed to acquire the
654        // `TypedFunc` to make this safe. It should be the case that
655        // `LowerReturn` is the exact representation of the return value when
656        // interpreted as `[ValRaw]`, and additionally they should have the
657        // correct types for the function we just called (which filled in the
658        // return values).
659        let ret: &LowerReturn = unsafe { map_maybe_uninit!(space.ret).assume_init_ref() };
660
661        // Lift the result into the host while managing post-return state
662        // here as well.
663        //
664        // After a successful lift the return value of the function, which
665        // is currently required to be 0 or 1 values according to the
666        // canonical ABI, is saved within the `Store`'s `FuncData`. This'll
667        // later get used in post-return.
668        let val = self.with_lift_context(store.0, |cx, ty| lift(cx, ty, ret))?;
669
670        // SAFETY: it's a contract of this function that `LowerReturn` is an
671        // appropriate representation of the result of this function.
672        let ret_slice = unsafe { storage_as_slice(ret) };
673
674        Ok((
675            val,
676            match ret_slice.len() {
677                0 => ValRaw::i32(0),
678                1 => ret_slice[0],
679                _ => unreachable!(),
680            },
681        ))
682    }
683
684    #[doc(hidden)]
685    #[deprecated(note = "no longer needs to be called; this function has no effect")]
686    pub fn post_return(&self, _store: impl AsContextMut) -> Result<()> {
687        Ok(())
688    }
689
690    #[doc(hidden)]
691    #[deprecated(note = "no longer needs to be called; this function has no effect")]
692    #[cfg(feature = "async")]
693    pub async fn post_return_async(&self, _store: impl AsContextMut<Data: Send>) -> Result<()> {
694        Ok(())
695    }
696
697    pub(crate) fn post_return_impl(&self, mut store: impl AsContextMut, arg: ValRaw) -> Result<()> {
698        let mut store = store.as_context_mut();
699
700        let index = self.index;
701        let vminstance = self.instance.id().get(store.0);
702        let component = vminstance.component();
703        let (_ty, _def, options) = component.export_lifted_function(index);
704        let post_return = self.post_return_core_func(store.0);
705        let flags = vminstance.instance_flags(component.env_component().options[options].instance);
706
707        unsafe {
708            call_post_return(&mut store, post_return, arg, flags)?;
709
710            store
711                .0
712                .component_resource_tables(Some(self.instance))
713                .validate_scope_exit()?;
714            store.0.exit_guest_sync_call(false)?;
715        }
716        Ok(())
717    }
718
719    fn lower_args<T>(
720        cx: &mut LowerContext<'_, T>,
721        params: &[Val],
722        params_ty: InterfaceType,
723        dst: &mut [MaybeUninit<ValRaw>],
724    ) -> Result<()> {
725        let params_ty = match params_ty {
726            InterfaceType::Tuple(i) => &cx.types[i],
727            _ => unreachable!(),
728        };
729        if params_ty.abi.flat_count(MAX_FLAT_PARAMS).is_some() {
730            let dst = &mut dst.iter_mut();
731
732            params
733                .iter()
734                .zip(params_ty.types.iter())
735                .try_for_each(|(param, ty)| param.lower(cx, *ty, dst))
736        } else {
737            Self::store_args(cx, &params_ty, params, dst)
738        }
739    }
740
741    fn store_args<T>(
742        cx: &mut LowerContext<'_, T>,
743        params_ty: &TypeTuple,
744        args: &[Val],
745        dst: &mut [MaybeUninit<ValRaw>],
746    ) -> Result<()> {
747        let size = usize::try_from(params_ty.abi.size32).unwrap();
748        let ptr = cx.realloc(0, 0, params_ty.abi.align32, size)?;
749        let mut offset = ptr;
750        for (ty, arg) in params_ty.types.iter().zip(args) {
751            let abi = cx.types.canonical_abi(ty);
752            arg.store(cx, *ty, abi.next_field32_size(&mut offset))?;
753        }
754
755        dst[0].write(ValRaw::i64(ptr as i64));
756
757        Ok(())
758    }
759
760    fn lift_results<'a, 'b>(
761        cx: &'a mut LiftContext<'b>,
762        results_ty: InterfaceType,
763        src: &'a [ValRaw],
764        max_flat: usize,
765    ) -> Result<Box<dyn Iterator<Item = Result<Val>> + 'a>> {
766        let results_ty = match results_ty {
767            InterfaceType::Tuple(i) => &cx.types[i],
768            _ => unreachable!(),
769        };
770        if results_ty.abi.flat_count(max_flat).is_some() {
771            let mut flat = src.iter();
772            Ok(Box::new(
773                results_ty
774                    .types
775                    .iter()
776                    .map(move |ty| Val::lift(cx, *ty, &mut flat)),
777            ))
778        } else {
779            let iter = Self::load_results(cx, results_ty, &mut src.iter())?;
780            Ok(Box::new(iter))
781        }
782    }
783
784    fn load_results<'a, 'b>(
785        cx: &'a mut LiftContext<'b>,
786        results_ty: &'a TypeTuple,
787        src: &mut core::slice::Iter<'_, ValRaw>,
788    ) -> Result<impl Iterator<Item = Result<Val>> + use<'a, 'b>> {
789        // FIXME(#4311): needs to read an i64 for memory64
790        let ptr = usize::try_from(src.next().unwrap().get_u32())?;
791        if ptr % usize::try_from(results_ty.abi.align32)? != 0 {
792            bail!("return pointer not aligned");
793        }
794
795        let bytes = cx
796            .memory()
797            .get(ptr..)
798            .and_then(|b| b.get(..usize::try_from(results_ty.abi.size32).unwrap()))
799            .ok_or_else(|| crate::format_err!("pointer out of bounds of memory"))?;
800
801        let mut offset = 0;
802        Ok(results_ty.types.iter().map(move |ty| {
803            let abi = cx.types.canonical_abi(ty);
804            let offset = abi.next_field32_size(&mut offset);
805            Val::load(cx, *ty, &bytes[offset..][..abi.size32 as usize])
806        }))
807    }
808
809    #[cfg(feature = "component-model-async")]
810    pub(crate) fn instance(self) -> Instance {
811        self.instance
812    }
813
814    /// Creates a `LowerContext` using the configuration values of this lifted
815    /// function.
816    ///
817    /// The `lower` closure provided should perform the actual lowering and
818    /// return the result of the lowering operation which is then returned from
819    /// this function as well.
820    fn with_lower_context<T>(
821        self,
822        mut store: StoreContextMut<T>,
823        lower: impl FnOnce(&mut LowerContext<T>, InterfaceType) -> Result<()>,
824    ) -> Result<()> {
825        let (options_idx, mut flags, ty, _) = self.abi_info(store.0);
826
827        // Perform the actual lowering, where while this is running the
828        // component is forbidden from calling imports.
829        unsafe {
830            debug_assert!(flags.may_leave());
831            flags.set_may_leave(false);
832        }
833        let mut cx = LowerContext::new(store.as_context_mut(), options_idx, self.instance);
834        let param_ty = InterfaceType::Tuple(cx.types[ty].params);
835        let result = lower(&mut cx, param_ty);
836        unsafe { flags.set_may_leave(true) };
837        result
838    }
839
840    /// Creates a `LiftContext` using the configuration values with this lifted
841    /// function.
842    ///
843    /// The closure `lift` provided should actually perform the lift itself and
844    /// the result of that closure is returned from this function call as well.
845    fn with_lift_context<R>(
846        self,
847        store: &mut StoreOpaque,
848        lift: impl FnOnce(&mut LiftContext, InterfaceType) -> Result<R>,
849    ) -> Result<R> {
850        let (options, _flags, ty, _) = self.abi_info(store);
851        let mut cx = LiftContext::new(store, options, self.instance);
852        let ty = InterfaceType::Tuple(cx.types[ty].results);
853        lift(&mut cx, ty)
854    }
855}
856
857pub(crate) unsafe fn call_post_return(
858    mut store: impl AsContextMut,
859    func: Option<NonNull<VMFuncRef>>,
860    arg: ValRaw,
861    mut flags: InstanceFlags,
862) -> Result<()> {
863    unsafe {
864        // Post return functions are forbidden from calling imports or
865        // intrinsics.
866        flags.set_may_leave(false);
867
868        // If the function actually had a `post-return` configured in its
869        // canonical options that's executed here.
870        if let Some(func) = func {
871            crate::Func::call_unchecked_raw(
872                &mut store.as_context_mut(),
873                func,
874                core::slice::from_ref(&arg).into(),
875            )?;
876        }
877
878        // And finally if everything completed successfully then the "may
879        // leave" flags is set to `true` again here which enables further
880        // use of the component.
881        flags.set_may_leave(true);
882    }
883
884    Ok(())
885}