dusk_wasmtime/runtime/component/
func.rs

1use crate::component::instance::{Instance, InstanceData};
2use crate::component::storage::storage_as_slice;
3use crate::component::types::Type;
4use crate::component::values::Val;
5use crate::store::{StoreOpaque, Stored};
6use crate::{AsContext, AsContextMut, StoreContextMut, ValRaw};
7use anyhow::{bail, Context, Result};
8use std::mem::{self, MaybeUninit};
9use std::ptr::NonNull;
10use std::sync::Arc;
11use wasmtime_environ::component::{
12    CanonicalOptions, ComponentTypes, CoreDef, InterfaceType, RuntimeComponentInstanceIndex,
13    TypeFuncIndex, TypeTuple, MAX_FLAT_PARAMS, MAX_FLAT_RESULTS,
14};
15use wasmtime_runtime::component::ResourceTables;
16use wasmtime_runtime::{Export, ExportFunction};
17
18/// A helper macro to safely map `MaybeUninit<T>` to `MaybeUninit<U>` where `U`
19/// is a field projection within `T`.
20///
21/// This is intended to be invoked as:
22///
23/// ```ignore
24/// struct MyType {
25///     field: u32,
26/// }
27///
28/// let initial: &mut MaybeUninit<MyType> = ...;
29/// let field: &mut MaybeUninit<u32> = map_maybe_uninit!(initial.field);
30/// ```
31///
32/// Note that array accesses are also supported:
33///
34/// ```ignore
35///
36/// let initial: &mut MaybeUninit<[u32; 2]> = ...;
37/// let element: &mut MaybeUninit<u32> = map_maybe_uninit!(initial[1]);
38/// ```
39#[doc(hidden)]
40#[macro_export]
41macro_rules! map_maybe_uninit {
42    ($maybe_uninit:ident $($field:tt)*) => ({
43        #[allow(unused_unsafe)]
44        {
45            unsafe {
46                use $crate::component::__internal::MaybeUninitExt;
47
48                let m: &mut std::mem::MaybeUninit<_> = $maybe_uninit;
49                // Note the usage of `addr_of_mut!` here which is an attempt to "stay
50                // safe" here where we never accidentally create `&mut T` where `T` is
51                // actually uninitialized, hopefully appeasing the Rust unsafe
52                // guidelines gods.
53                m.map(|p| std::ptr::addr_of_mut!((*p)$($field)*))
54            }
55        }
56    })
57}
58
59#[doc(hidden)]
60pub trait MaybeUninitExt<T> {
61    /// Maps `MaybeUninit<T>` to `MaybeUninit<U>` using the closure provided.
62    ///
63    /// Note that this is `unsafe` as there is no guarantee that `U` comes from
64    /// `T`.
65    unsafe fn map<U>(&mut self, f: impl FnOnce(*mut T) -> *mut U) -> &mut MaybeUninit<U>;
66}
67
68impl<T> MaybeUninitExt<T> for MaybeUninit<T> {
69    unsafe fn map<U>(&mut self, f: impl FnOnce(*mut T) -> *mut U) -> &mut MaybeUninit<U> {
70        let new_ptr = f(self.as_mut_ptr());
71        std::mem::transmute::<*mut U, &mut MaybeUninit<U>>(new_ptr)
72    }
73}
74
75mod host;
76mod options;
77mod typed;
78pub use self::host::*;
79pub use self::options::*;
80pub use self::typed::*;
81
82#[repr(C)]
83union ParamsAndResults<Params: Copy, Return: Copy> {
84    params: Params,
85    ret: Return,
86}
87
88/// A WebAssembly component function which can be called.
89///
90/// This type is the dual of [`wasmtime::Func`](crate::Func) for component
91/// functions. An instance of [`Func`] represents a component function from a
92/// component [`Instance`](crate::component::Instance). Like with
93/// [`wasmtime::Func`](crate::Func) it's possible to call functions either
94/// synchronously or asynchronously and either typed or untyped.
95#[derive(Copy, Clone, Debug)]
96pub struct Func(Stored<FuncData>);
97
98#[doc(hidden)]
99pub struct FuncData {
100    export: ExportFunction,
101    ty: TypeFuncIndex,
102    types: Arc<ComponentTypes>,
103    options: Options,
104    instance: Instance,
105    component_instance: RuntimeComponentInstanceIndex,
106    post_return: Option<ExportFunction>,
107    post_return_arg: Option<ValRaw>,
108}
109
110impl Func {
111    pub(crate) fn from_lifted_func(
112        store: &mut StoreOpaque,
113        instance: &Instance,
114        data: &InstanceData,
115        ty: TypeFuncIndex,
116        func: &CoreDef,
117        options: &CanonicalOptions,
118    ) -> Func {
119        let export = match data.lookup_def(store, func) {
120            Export::Function(f) => f,
121            _ => unreachable!(),
122        };
123        let memory = options
124            .memory
125            .map(|i| NonNull::new(data.instance().runtime_memory(i)).unwrap());
126        let realloc = options.realloc.map(|i| data.instance().runtime_realloc(i));
127        let post_return = options.post_return.map(|i| {
128            let func_ref = data.instance().runtime_post_return(i);
129            ExportFunction { func_ref }
130        });
131        let component_instance = options.instance;
132        let options = unsafe { Options::new(store.id(), memory, realloc, options.string_encoding) };
133        Func(store.store_data_mut().insert(FuncData {
134            export,
135            options,
136            ty,
137            types: data.component_types().clone(),
138            instance: *instance,
139            component_instance,
140            post_return,
141            post_return_arg: None,
142        }))
143    }
144
145    /// Attempt to cast this [`Func`] to a statically typed [`TypedFunc`] with
146    /// the provided `Params` and `Return`.
147    ///
148    /// This function will perform a type-check at runtime that the [`Func`]
149    /// takes `Params` as parameters and returns `Return`. If the type-check
150    /// passes then a [`TypedFunc`] will be returned which can be used to
151    /// invoke the function in an efficient, statically-typed, and ergonomic
152    /// manner.
153    ///
154    /// The `Params` type parameter here is a tuple of the parameters to the
155    /// function. A function which takes no arguments should use `()`, a
156    /// function with one argument should use `(T,)`, etc. Note that all
157    /// `Params` must also implement the [`Lower`] trait since they're going
158    /// into wasm.
159    ///
160    /// The `Return` type parameter is the return value of this function. A
161    /// return value of `()` means that there's no return (similar to a Rust
162    /// unit return) and otherwise a type `T` can be specified. Note that the
163    /// `Return` must also implement the [`Lift`] trait since it's coming from
164    /// wasm.
165    ///
166    /// Types specified here must implement the [`ComponentType`] trait. This
167    /// trait is implemented for built-in types to Rust such as integer
168    /// primitives, floats, `Option<T>`, `Result<T, E>`, strings, `Vec<T>`, and
169    /// more. As parameters you'll be passing native Rust types.
170    ///
171    /// See the documentation for [`ComponentType`] for more information about
172    /// supported types.
173    ///
174    /// # Errors
175    ///
176    /// If the function does not actually take `Params` as its parameters or
177    /// return `Return` then an error will be returned.
178    ///
179    /// # Panics
180    ///
181    /// This function will panic if `self` is not owned by the `store`
182    /// specified.
183    ///
184    /// # Examples
185    ///
186    /// Calling a function which takes no parameters and has no return value:
187    ///
188    /// ```
189    /// # use wasmtime::component::Func;
190    /// # use wasmtime::Store;
191    /// # fn foo(func: &Func, store: &mut Store<()>) -> anyhow::Result<()> {
192    /// let typed = func.typed::<(), ()>(&store)?;
193    /// typed.call(store, ())?;
194    /// # Ok(())
195    /// # }
196    /// ```
197    ///
198    /// Calling a function which takes one string parameter and returns a
199    /// string:
200    ///
201    /// ```
202    /// # use wasmtime::component::Func;
203    /// # use wasmtime::Store;
204    /// # fn foo(func: &Func, mut store: Store<()>) -> anyhow::Result<()> {
205    /// let typed = func.typed::<(&str,), (String,)>(&store)?;
206    /// let ret = typed.call(&mut store, ("Hello, ",))?.0;
207    /// println!("returned string was: {}", ret);
208    /// # Ok(())
209    /// # }
210    /// ```
211    ///
212    /// Calling a function which takes multiple parameters and returns a boolean:
213    ///
214    /// ```
215    /// # use wasmtime::component::Func;
216    /// # use wasmtime::Store;
217    /// # fn foo(func: &Func, mut store: Store<()>) -> anyhow::Result<()> {
218    /// let typed = func.typed::<(u32, Option<&str>, &[u8]), (bool,)>(&store)?;
219    /// let ok: bool = typed.call(&mut store, (1, Some("hello"), b"bytes!"))?.0;
220    /// println!("return value was: {ok}");
221    /// # Ok(())
222    /// # }
223    /// ```
224    pub fn typed<Params, Return>(&self, store: impl AsContext) -> Result<TypedFunc<Params, Return>>
225    where
226        Params: ComponentNamedList + Lower,
227        Return: ComponentNamedList + Lift,
228    {
229        self._typed(store.as_context().0, None)
230    }
231
232    pub(crate) fn _typed<Params, Return>(
233        &self,
234        store: &StoreOpaque,
235        instance: Option<&InstanceData>,
236    ) -> Result<TypedFunc<Params, Return>>
237    where
238        Params: ComponentNamedList + Lower,
239        Return: ComponentNamedList + Lift,
240    {
241        self.typecheck::<Params, Return>(store, instance)?;
242        unsafe { Ok(TypedFunc::new_unchecked(*self)) }
243    }
244
245    fn typecheck<Params, Return>(
246        &self,
247        store: &StoreOpaque,
248        instance: Option<&InstanceData>,
249    ) -> Result<()>
250    where
251        Params: ComponentNamedList + Lower,
252        Return: ComponentNamedList + Lift,
253    {
254        let data = &store[self.0];
255        let cx = instance
256            .unwrap_or_else(|| &store[data.instance.0].as_ref().unwrap())
257            .ty();
258        let ty = &cx.types[data.ty];
259
260        Params::typecheck(&InterfaceType::Tuple(ty.params), &cx)
261            .context("type mismatch with parameters")?;
262        Return::typecheck(&InterfaceType::Tuple(ty.results), &cx)
263            .context("type mismatch with results")?;
264
265        Ok(())
266    }
267
268    /// Get the parameter types for this function.
269    pub fn params(&self, store: impl AsContext) -> Box<[Type]> {
270        let store = store.as_context();
271        let data = &store[self.0];
272        let instance = store[data.instance.0].as_ref().unwrap();
273        data.types[data.types[data.ty].params]
274            .types
275            .iter()
276            .map(|ty| Type::from(ty, &instance.ty()))
277            .collect()
278    }
279
280    /// Get the result types for this function.
281    pub fn results(&self, store: impl AsContext) -> Box<[Type]> {
282        let store = store.as_context();
283        let data = &store[self.0];
284        let instance = store[data.instance.0].as_ref().unwrap();
285        data.types[data.types[data.ty].results]
286            .types
287            .iter()
288            .map(|ty| Type::from(ty, &instance.ty()))
289            .collect()
290    }
291
292    /// Invokes this function with the `params` given and returns the result.
293    ///
294    /// The `params` provided must match the parameters that this function takes
295    /// in terms of their types and the number of parameters. Results will be
296    /// written to the `results` slice provided if the call completes
297    /// successfully. The initial types of the values in `results` are ignored
298    /// and values are overwritten to write the result. It's required that the
299    /// size of `results` exactly matches the number of results that this
300    /// function produces.
301    ///
302    /// Note that after a function is invoked the embedder needs to invoke
303    /// [`Func::post_return`] to execute any final cleanup required by the
304    /// guest. This function call is required to either call the function again
305    /// or to call another function.
306    ///
307    /// For more detailed information see the documentation of
308    /// [`TypedFunc::call`].
309    ///
310    /// # Errors
311    ///
312    /// Returns an error in situations including but not limited to:
313    ///
314    /// * `params` is not the right size or if the values have the wrong type
315    /// * `results` is not the right size
316    /// * A trap occurs while executing the function
317    /// * The function calls a host function which returns an error
318    ///
319    /// See [`TypedFunc::call`] for more information in addition to
320    /// [`wasmtime::Func::call`](crate::Func::call).
321    ///
322    /// # Panics
323    ///
324    /// Panics if this is called on a function in an asyncronous store. This
325    /// only works with functions defined within a synchronous store. Also
326    /// panics if `store` does not own this function.
327    pub fn call(
328        &self,
329        mut store: impl AsContextMut,
330        params: &[Val],
331        results: &mut [Val],
332    ) -> Result<()> {
333        let mut store = store.as_context_mut();
334        assert!(
335            !store.0.async_support(),
336            "must use `call_async` when async support is enabled on the config"
337        );
338        self.call_impl(&mut store.as_context_mut(), params, results)
339    }
340
341    /// Exactly like [`Self::call`] except for use on async stores.
342    ///
343    /// Note that after this [`Func::post_return_async`] will be used instead of
344    /// the synchronous version at [`Func::post_return`].
345    ///
346    /// # Panics
347    ///
348    /// Panics if this is called on a function in a synchronous store. This
349    /// only works with functions defined within an asynchronous store. Also
350    /// panics if `store` does not own this function.
351    #[cfg(feature = "async")]
352    #[cfg_attr(docsrs, doc(cfg(feature = "async")))]
353    pub async fn call_async<T>(
354        &self,
355        mut store: impl AsContextMut<Data = T>,
356        params: &[Val],
357        results: &mut [Val],
358    ) -> Result<()>
359    where
360        T: Send,
361    {
362        let mut store = store.as_context_mut();
363        assert!(
364            store.0.async_support(),
365            "cannot use `call_async` without enabling async support in the config"
366        );
367        store
368            .on_fiber(|store| self.call_impl(store, params, results))
369            .await?
370    }
371
372    fn call_impl(
373        &self,
374        mut store: impl AsContextMut,
375        params: &[Val],
376        results: &mut [Val],
377    ) -> Result<()> {
378        let store = &mut store.as_context_mut();
379
380        let param_tys = self.params(&store);
381        let result_tys = self.results(&store);
382
383        if param_tys.len() != params.len() {
384            bail!(
385                "expected {} argument(s), got {}",
386                param_tys.len(),
387                params.len()
388            );
389        }
390        if result_tys.len() != results.len() {
391            bail!(
392                "expected {} results(s), got {}",
393                result_tys.len(),
394                results.len()
395            );
396        }
397
398        self.call_raw(
399            store,
400            params,
401            |cx, params, params_ty, dst: &mut MaybeUninit<[ValRaw; MAX_FLAT_PARAMS]>| {
402                let params_ty = match params_ty {
403                    InterfaceType::Tuple(i) => &cx.types[i],
404                    _ => unreachable!(),
405                };
406                if params_ty.abi.flat_count(MAX_FLAT_PARAMS).is_some() {
407                    let dst = &mut unsafe {
408                        mem::transmute::<_, &mut [MaybeUninit<ValRaw>; MAX_FLAT_PARAMS]>(dst)
409                    }
410                    .iter_mut();
411
412                    params
413                        .iter()
414                        .zip(params_ty.types.iter())
415                        .try_for_each(|(param, ty)| param.lower(cx, *ty, dst))
416                } else {
417                    self.store_args(cx, &params_ty, params, dst)
418                }
419            },
420            |cx, results_ty, src: &[ValRaw; MAX_FLAT_RESULTS]| {
421                let results_ty = match results_ty {
422                    InterfaceType::Tuple(i) => &cx.types[i],
423                    _ => unreachable!(),
424                };
425                if results_ty.abi.flat_count(MAX_FLAT_RESULTS).is_some() {
426                    let mut flat = src.iter();
427                    for (ty, slot) in results_ty.types.iter().zip(results) {
428                        *slot = Val::lift(cx, *ty, &mut flat)?;
429                    }
430                    Ok(())
431                } else {
432                    Self::load_results(cx, results_ty, results, &mut src.iter())
433                }
434            },
435        )
436    }
437
438    /// Invokes the underlying wasm function, lowering arguments and lifting the
439    /// result.
440    ///
441    /// The `lower` function and `lift` function provided here are what actually
442    /// do the lowering and lifting. The `LowerParams` and `LowerReturn` types
443    /// are what will be allocated on the stack for this function call. They
444    /// should be appropriately sized for the lowering/lifting operation
445    /// happening.
446    fn call_raw<T, Params: ?Sized, Return, LowerParams, LowerReturn>(
447        &self,
448        store: &mut StoreContextMut<'_, T>,
449        params: &Params,
450        lower: impl FnOnce(
451            &mut LowerContext<'_, T>,
452            &Params,
453            InterfaceType,
454            &mut MaybeUninit<LowerParams>,
455        ) -> Result<()>,
456        lift: impl FnOnce(&mut LiftContext<'_>, InterfaceType, &LowerReturn) -> Result<Return>,
457    ) -> Result<Return>
458    where
459        LowerParams: Copy,
460        LowerReturn: Copy,
461    {
462        let FuncData {
463            export,
464            options,
465            instance,
466            component_instance,
467            ty,
468            ..
469        } = store.0[self.0];
470
471        let space = &mut MaybeUninit::<ParamsAndResults<LowerParams, LowerReturn>>::uninit();
472
473        // Double-check the size/alignemnt of `space`, just in case.
474        //
475        // Note that this alone is not enough to guarantee the validity of the
476        // `unsafe` block below, but it's definitely required. In any case LLVM
477        // should be able to trivially see through these assertions and remove
478        // them in release mode.
479        let val_size = mem::size_of::<ValRaw>();
480        let val_align = mem::align_of::<ValRaw>();
481        assert!(mem::size_of_val(space) % val_size == 0);
482        assert!(mem::size_of_val(map_maybe_uninit!(space.params)) % val_size == 0);
483        assert!(mem::size_of_val(map_maybe_uninit!(space.ret)) % val_size == 0);
484        assert!(mem::align_of_val(space) == val_align);
485        assert!(mem::align_of_val(map_maybe_uninit!(space.params)) == val_align);
486        assert!(mem::align_of_val(map_maybe_uninit!(space.ret)) == val_align);
487
488        let instance = store.0[instance.0].as_ref().unwrap();
489        let types = instance.component_types().clone();
490        let mut flags = instance.instance().instance_flags(component_instance);
491
492        unsafe {
493            // Test the "may enter" flag which is a "lock" on this instance.
494            // This is immediately set to `false` afterwards and note that
495            // there's no on-cleanup setting this flag back to true. That's an
496            // intentional design aspect where if anything goes wrong internally
497            // from this point on the instance is considered "poisoned" and can
498            // never be entered again. The only time this flag is set to `true`
499            // again is after post-return logic has completed successfully.
500            if !flags.may_enter() {
501                bail!(crate::Trap::CannotEnterComponent);
502            }
503            flags.set_may_enter(false);
504
505            debug_assert!(flags.may_leave());
506            flags.set_may_leave(false);
507            let instance_ptr = instance.instance_ptr();
508            let mut cx = LowerContext::new(store.as_context_mut(), &options, &types, instance_ptr);
509            cx.enter_call();
510            let result = lower(
511                &mut cx,
512                params,
513                InterfaceType::Tuple(types[ty].params),
514                map_maybe_uninit!(space.params),
515            );
516            flags.set_may_leave(true);
517            result?;
518
519            // This is unsafe as we are providing the guarantee that all the
520            // inputs are valid. The various pointers passed in for the function
521            // are all valid since they're coming from our store, and the
522            // `params_and_results` should have the correct layout for the core
523            // wasm function we're calling. Note that this latter point relies
524            // on the correctness of this module and `ComponentType`
525            // implementations, hence `ComponentType` being an `unsafe` trait.
526            crate::Func::call_unchecked_raw(
527                store,
528                export.func_ref,
529                space.as_mut_ptr().cast(),
530                mem::size_of_val(space) / mem::size_of::<ValRaw>(),
531            )?;
532
533            // Note that `.assume_init_ref()` here is unsafe but we're relying
534            // on the correctness of the structure of `LowerReturn` and the
535            // type-checking performed to acquire the `TypedFunc` to make this
536            // safe. It should be the case that `LowerReturn` is the exact
537            // representation of the return value when interpreted as
538            // `[ValRaw]`, and additionally they should have the correct types
539            // for the function we just called (which filled in the return
540            // values).
541            let ret = map_maybe_uninit!(space.ret).assume_init_ref();
542
543            // Lift the result into the host while managing post-return state
544            // here as well.
545            //
546            // After a successful lift the return value of the function, which
547            // is currently required to be 0 or 1 values according to the
548            // canonical ABI, is saved within the `Store`'s `FuncData`. This'll
549            // later get used in post-return.
550            flags.set_needs_post_return(true);
551            let val = lift(
552                &mut LiftContext::new(store.0, &options, &types, instance_ptr),
553                InterfaceType::Tuple(types[ty].results),
554                ret,
555            )?;
556            let ret_slice = storage_as_slice(ret);
557            let data = &mut store.0[self.0];
558            assert!(data.post_return_arg.is_none());
559            match ret_slice.len() {
560                0 => data.post_return_arg = Some(ValRaw::i32(0)),
561                1 => data.post_return_arg = Some(ret_slice[0]),
562                _ => unreachable!(),
563            }
564            return Ok(val);
565        }
566    }
567
568    /// Invokes the `post-return` canonical ABI option, if specified, after a
569    /// [`Func::call`] has finished.
570    ///
571    /// This function is a required method call after a [`Func::call`] completes
572    /// successfully. After the embedder has finished processing the return
573    /// value then this function must be invoked.
574    ///
575    /// # Errors
576    ///
577    /// This function will return an error in the case of a WebAssembly trap
578    /// happening during the execution of the `post-return` function, if
579    /// specified.
580    ///
581    /// # Panics
582    ///
583    /// This function will panic if it's not called under the correct
584    /// conditions. This can only be called after a previous invocation of
585    /// [`Func::call`] completes successfully, and this function can only
586    /// be called for the same [`Func`] that was `call`'d.
587    ///
588    /// If this function is called when [`Func::call`] was not previously
589    /// called, then it will panic. If a different [`Func`] for the same
590    /// component instance was invoked then this function will also panic
591    /// because the `post-return` needs to happen for the other function.
592    ///
593    /// Panics if this is called on a function in an asynchronous store.
594    /// This only works with functions defined within a synchronous store.
595    #[inline]
596    pub fn post_return(&self, mut store: impl AsContextMut) -> Result<()> {
597        let store = store.as_context_mut();
598        assert!(
599            !store.0.async_support(),
600            "must use `post_return_async` when async support is enabled on the config"
601        );
602        self.post_return_impl(store)
603    }
604
605    /// Exactly like [`Self::post_return`] except for use on async stores.
606    ///
607    /// # Panics
608    ///
609    /// Panics if this is called on a function in a synchronous store. This
610    /// only works with functions defined within an asynchronous store.
611    #[cfg(feature = "async")]
612    #[cfg_attr(docsrs, doc(cfg(feature = "async")))]
613    pub async fn post_return_async<T: Send>(
614        &self,
615        mut store: impl AsContextMut<Data = T>,
616    ) -> Result<()> {
617        let mut store = store.as_context_mut();
618        assert!(
619            store.0.async_support(),
620            "cannot use `call_async` without enabling async support in the config"
621        );
622        // Future optimization opportunity: conditionally use a fiber here since
623        // some func's post_return will not need the async context (i.e. end up
624        // calling async host functionality)
625        store.on_fiber(|store| self.post_return_impl(store)).await?
626    }
627
628    fn post_return_impl(&self, mut store: impl AsContextMut) -> Result<()> {
629        let mut store = store.as_context_mut();
630        let data = &mut store.0[self.0];
631        let instance = data.instance;
632        let post_return = data.post_return;
633        let component_instance = data.component_instance;
634        let post_return_arg = data.post_return_arg.take();
635        let instance = store.0[instance.0].as_ref().unwrap().instance_ptr();
636
637        unsafe {
638            let mut flags = (*instance).instance_flags(component_instance);
639
640            // First assert that the instance is in a "needs post return" state.
641            // This will ensure that the previous action on the instance was a
642            // function call above. This flag is only set after a component
643            // function returns so this also can't be called (as expected)
644            // during a host import for example.
645            //
646            // Note, though, that this assert is not sufficient because it just
647            // means some function on this instance needs its post-return
648            // called. We need a precise post-return for a particular function
649            // which is the second assert here (the `.expect`). That will assert
650            // that this function itself needs to have its post-return called.
651            //
652            // The theory at least is that these two asserts ensure component
653            // model semantics are upheld where the host properly calls
654            // `post_return` on the right function despite the call being a
655            // separate step in the API.
656            assert!(
657                flags.needs_post_return(),
658                "post_return can only be called after a function has previously been called",
659            );
660            let post_return_arg = post_return_arg.expect("calling post_return on wrong function");
661
662            // This is a sanity-check assert which shouldn't ever trip.
663            assert!(!flags.may_enter());
664
665            // Unset the "needs post return" flag now that post-return is being
666            // processed. This will cause future invocations of this method to
667            // panic, even if the function call below traps.
668            flags.set_needs_post_return(false);
669
670            // If the function actually had a `post-return` configured in its
671            // canonical options that's executed here.
672            //
673            // Note that if this traps (returns an error) this function
674            // intentionally leaves the instance in a "poisoned" state where it
675            // can no longer be entered because `may_enter` is `false`.
676            if let Some(func) = post_return {
677                crate::Func::call_unchecked_raw(
678                    &mut store,
679                    func.func_ref,
680                    &post_return_arg as *const ValRaw as *mut ValRaw,
681                    1,
682                )?;
683            }
684
685            // And finally if everything completed successfully then the "may
686            // enter" flag is set to `true` again here which enables further use
687            // of the component.
688            flags.set_may_enter(true);
689
690            let (calls, host_table, _) = store.0.component_resource_state();
691            ResourceTables {
692                calls,
693                host_table: Some(host_table),
694                tables: Some((*instance).component_resource_tables()),
695            }
696            .exit_call()?;
697        }
698        Ok(())
699    }
700
701    fn store_args<T>(
702        &self,
703        cx: &mut LowerContext<'_, T>,
704        params_ty: &TypeTuple,
705        args: &[Val],
706        dst: &mut MaybeUninit<[ValRaw; MAX_FLAT_PARAMS]>,
707    ) -> Result<()> {
708        let size = usize::try_from(params_ty.abi.size32).unwrap();
709        let ptr = cx.realloc(0, 0, params_ty.abi.align32, size)?;
710        let mut offset = ptr;
711        for (ty, arg) in params_ty.types.iter().zip(args) {
712            let abi = cx.types.canonical_abi(ty);
713            arg.store(cx, *ty, abi.next_field32_size(&mut offset))?;
714        }
715
716        map_maybe_uninit!(dst[0]).write(ValRaw::i64(ptr as i64));
717
718        Ok(())
719    }
720
721    fn load_results(
722        cx: &mut LiftContext<'_>,
723        results_ty: &TypeTuple,
724        results: &mut [Val],
725        src: &mut std::slice::Iter<'_, ValRaw>,
726    ) -> Result<()> {
727        // FIXME: needs to read an i64 for memory64
728        let ptr = usize::try_from(src.next().unwrap().get_u32())?;
729        if ptr % usize::try_from(results_ty.abi.align32)? != 0 {
730            bail!("return pointer not aligned");
731        }
732
733        let bytes = cx
734            .memory()
735            .get(ptr..)
736            .and_then(|b| b.get(..usize::try_from(results_ty.abi.size32).unwrap()))
737            .ok_or_else(|| anyhow::anyhow!("pointer out of bounds of memory"))?;
738
739        let mut offset = 0;
740        for (ty, slot) in results_ty.types.iter().zip(results) {
741            let abi = cx.types.canonical_abi(ty);
742            let offset = abi.next_field32_size(&mut offset);
743            *slot = Val::load(cx, *ty, &bytes[offset..][..abi.size32 as usize])?;
744        }
745        Ok(())
746    }
747}