1 use crate::component::RuntimeInstance;
2 use crate::component::instance::Instance;
3 use crate::component::matching::InstanceType;
4 use crate::component::storage::storage_as_slice;
5 use crate::component::types::ComponentFunc;
6 use crate::component::values::Val;
7 use crate::prelude::*;
8 use crate::runtime::vm::component::{ComponentInstance, InstanceFlags, ResourceTables};
9 use crate::runtime::vm::{Export, VMFuncRef};
10 use crate::store::StoreOpaque;
11 use crate::{AsContext, AsContextMut, StoreContextMut, ValRaw};
12 use core::mem::{self, MaybeUninit};
13 use core::ptr::NonNull;
14 use 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")]
20 use crate::component::concurrent::{self, AsAccessor, PreparedCall};
21 
22 mod host;
23 mod options;
24 mod typed;
25 pub use self::host::*;
26 pub use self::options::*;
27 pub 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.
38 pub 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.
45 const _: () = {
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 
55 impl 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     /// Note that after a function is invoked the embedder needs to invoke
208     /// [`Func::post_return`] to execute any final cleanup required by the
209     /// guest. This function call is required to either call the function again
210     /// or to call another function.
211     ///
212     /// For more detailed information see the documentation of
213     /// [`TypedFunc::call`].
214     ///
215     /// # Errors
216     ///
217     /// Returns an error in situations including but not limited to:
218     ///
219     /// * `params` is not the right size or if the values have the wrong type
220     /// * `results` is not the right size
221     /// * A trap occurs while executing the function
222     /// * The function calls a host function which returns an error
223     /// * The `store` used requires the use of [`Func::call_async`] instead. See
224     ///   [store documentation](crate#async) for more information.
225     ///
226     /// See [`TypedFunc::call`] for more information in addition to
227     /// [`wasmtime::Func::call`](crate::Func::call).
228     ///
229     /// # Panics
230     ///
231     /// Panics if `store` does not own this function.
232     pub fn call(
233         &self,
234         mut store: impl AsContextMut,
235         params: &[Val],
236         results: &mut [Val],
237     ) -> Result<()> {
238         let mut store = store.as_context_mut();
239         store.0.validate_sync_call()?;
240         self.call_impl(&mut store.as_context_mut(), params, results)
241     }
242 
243     /// Exactly like [`Self::call`] except for use on async stores.
244     ///
245     /// Note that after this [`Func::post_return_async`] will be used instead of
246     /// the synchronous version at [`Func::post_return`].
247     ///
248     /// # Panics
249     ///
250     /// Panics if `store` does not own this function.
251     #[cfg(feature = "async")]
252     pub async fn call_async(
253         &self,
254         mut store: impl AsContextMut<Data: Send>,
255         params: &[Val],
256         results: &mut [Val],
257     ) -> Result<()> {
258         let store = store.as_context_mut();
259 
260         #[cfg(feature = "component-model-async")]
261         if store.0.concurrency_support() {
262             return store
263                 .run_concurrent_trap_on_idle(async |store| {
264                     self.call_concurrent_dynamic(store, params, results, false)
265                         .await
266                         .map(drop)
267                 })
268                 .await?;
269         }
270 
271         let mut store = store;
272         store
273             .on_fiber(|store| self.call_impl(store, params, results))
274             .await?
275     }
276 
277     fn check_params_results<T>(
278         &self,
279         store: StoreContextMut<T>,
280         params: &[Val],
281         results: &mut [Val],
282     ) -> Result<()> {
283         let ty = self.ty(&store);
284         if ty.params().len() != params.len() {
285             bail!(
286                 "expected {} argument(s), got {}",
287                 ty.params().len(),
288                 params.len(),
289             );
290         }
291 
292         if ty.results().len() != results.len() {
293             bail!(
294                 "expected {} result(s), got {}",
295                 ty.results().len(),
296                 results.len(),
297             );
298         }
299 
300         Ok(())
301     }
302 
303     /// Start a concurrent call to this function.
304     ///
305     /// Concurrency is achieved by relying on the [`Accessor`] argument, which
306     /// can be obtained by calling [`StoreContextMut::run_concurrent`].
307     ///
308     /// Unlike [`Self::call`] and [`Self::call_async`] (both of which require
309     /// exclusive access to the store until the completion of the call), calls
310     /// made using this method may run concurrently with other calls to the same
311     /// instance.  In addition, the runtime will call the `post-return` function
312     /// (if any) automatically when the guest task completes -- no need to
313     /// explicitly call `Func::post_return` afterward.
314     ///
315     /// This returns a [`TaskExit`] representing the completion of the guest
316     /// task and any transitive subtasks it might create.
317     ///
318     /// # Progress
319     ///
320     /// For the wasm task being created in `call_concurrent` to make progress it
321     /// must be run within the scope of [`run_concurrent`]. If there are no
322     /// active calls to [`run_concurrent`] then the wasm task will appear as
323     /// stalled. This is typically not a concern as an [`Accessor`] is bound
324     /// by default to a scope of [`run_concurrent`].
325     ///
326     /// One situation in which this can arise, for example, is that if a
327     /// [`run_concurrent`] computation finishes its async closure before all
328     /// wasm tasks have completed, then there will be no scope of
329     /// [`run_concurrent`] anywhere. In this situation the wasm tasks that have
330     /// not yet completed will not make progress until [`run_concurrent`] is
331     /// called again.
332     ///
333     /// Embedders will need to ensure that this future is `await`'d within the
334     /// scope of [`run_concurrent`] to ensure that the value can be produced
335     /// during the `await` call.
336     ///
337     /// # Cancellation
338     ///
339     /// Cancelling an async task created via `call_concurrent`, at this time, is
340     /// only possible by dropping the store that the computation runs within.
341     /// With [#11833] implemented then it will be possible to request
342     /// cancellation of a task, but that is not yet implemented. Hard-cancelling
343     /// a task will only ever be possible by dropping the entire store and it is
344     /// not possible to remove just one task from a store.
345     ///
346     /// This async function behaves more like a "spawn" than a normal Rust async
347     /// function. When this function is invoked then metadata for the function
348     /// call is recorded in the store connected to the `accessor` argument and
349     /// the wasm invocation is from then on connected to the store. If the
350     /// future created by this function is dropped it does not cancel the
351     /// in-progress execution of the wasm task. Dropping the future
352     /// relinquishes the host's ability to learn about the result of the task
353     /// but the task will still progress and invoke callbacks and such until
354     /// completion.
355     ///
356     /// This function will return an error if [`Config::concurrency_support`] is
357     /// disabled.
358     ///
359     /// [`Config::concurrency_support`]: crate::Config::concurrency_support
360     /// [`run_concurrent`]: crate::Store::run_concurrent
361     /// [#11833]: https://github.com/bytecodealliance/wasmtime/issues/11833
362     /// [`Accessor`]: crate::component::Accessor
363     ///
364     /// # Panics
365     ///
366     /// Panics if the store that the [`Accessor`] is derived from does not own
367     /// this function.
368     ///
369     /// # Example
370     ///
371     /// Using [`StoreContextMut::run_concurrent`] to get an [`Accessor`]:
372     ///
373     /// ```
374     /// # use {
375     /// #   wasmtime::{
376     /// #     error::{Result},
377     /// #     component::{Component, Linker, ResourceTable},
378     /// #     Config, Engine, Store
379     /// #   },
380     /// # };
381     /// #
382     /// # struct Ctx { table: ResourceTable }
383     /// #
384     /// # async fn foo() -> Result<()> {
385     /// # let mut config = Config::new();
386     /// # let engine = Engine::new(&config)?;
387     /// # let mut store = Store::new(&engine, Ctx { table: ResourceTable::new() });
388     /// # let mut linker = Linker::new(&engine);
389     /// # let component = Component::new(&engine, "")?;
390     /// # let instance = linker.instantiate_async(&mut store, &component).await?;
391     /// let my_func = instance.get_func(&mut store, "my_func").unwrap();
392     /// store.run_concurrent(async |accessor| -> wasmtime::Result<_> {
393     ///    my_func.call_concurrent(accessor, &[], &mut Vec::new()).await?;
394     ///    Ok(())
395     /// }).await??;
396     /// # Ok(())
397     /// # }
398     /// ```
399     #[cfg(feature = "component-model-async")]
400     pub async fn call_concurrent(
401         self,
402         accessor: impl AsAccessor<Data: Send>,
403         params: &[Val],
404         results: &mut [Val],
405     ) -> Result<TaskExit> {
406         self.call_concurrent_dynamic(accessor, params, results, true)
407             .await
408     }
409 
410     /// Internal helper function for `call_async` and `call_concurrent`.
411     #[cfg(feature = "component-model-async")]
412     async fn call_concurrent_dynamic(
413         self,
414         accessor: impl AsAccessor<Data: Send>,
415         params: &[Val],
416         results: &mut [Val],
417         call_post_return_automatically: bool,
418     ) -> Result<TaskExit> {
419         let result = accessor.as_accessor().with(|mut store| {
420             self.check_params_results(store.as_context_mut(), params, results)?;
421             let prepared = self.prepare_call_dynamic(
422                 store.as_context_mut(),
423                 params.to_vec(),
424                 call_post_return_automatically,
425             )?;
426             concurrent::queue_call(store.as_context_mut(), prepared)
427         })?;
428 
429         let (run_results, rx) = result.await?;
430         assert_eq!(run_results.len(), results.len());
431         for (result, slot) in run_results.into_iter().zip(results) {
432             *slot = result;
433         }
434         Ok(TaskExit(rx))
435     }
436 
437     /// Calls `concurrent::prepare_call` with monomorphized functions for
438     /// lowering the parameters and lifting the result.
439     #[cfg(feature = "component-model-async")]
440     fn prepare_call_dynamic<'a, T: Send + 'static>(
441         self,
442         mut store: StoreContextMut<'a, T>,
443         params: Vec<Val>,
444         call_post_return_automatically: bool,
445     ) -> Result<PreparedCall<Vec<Val>>> {
446         let store = store.as_context_mut();
447 
448         concurrent::prepare_call(
449             store,
450             self,
451             MAX_FLAT_PARAMS,
452             false,
453             call_post_return_automatically,
454             move |func, store, params_out| {
455                 func.with_lower_context(store, call_post_return_automatically, |cx, ty| {
456                     Self::lower_args(cx, &params, ty, params_out)
457                 })
458             },
459             move |func, store, results| {
460                 let max_flat = if func.abi_async(store) {
461                     MAX_FLAT_PARAMS
462                 } else {
463                     MAX_FLAT_RESULTS
464                 };
465                 let results = func.with_lift_context(store, |cx, ty| {
466                     Self::lift_results(cx, ty, results, max_flat)?.collect::<Result<Vec<_>>>()
467                 })?;
468                 Ok(Box::new(results))
469             },
470         )
471     }
472 
473     fn call_impl(
474         &self,
475         mut store: impl AsContextMut,
476         params: &[Val],
477         results: &mut [Val],
478     ) -> Result<()> {
479         let mut store = store.as_context_mut();
480 
481         self.check_params_results(store.as_context_mut(), params, results)?;
482 
483         if self.abi_async(store.0) {
484             unreachable!(
485                 "async-lifted exports should have failed validation \
486                  when `component-model-async` feature disabled"
487             );
488         }
489 
490         // SAFETY: the chosen representations of type parameters to `call_raw`
491         // here should be generally safe to work with:
492         //
493         // * parameters use `MaybeUninit<[MaybeUninit<ValRaw>; MAX_FLAT_PARAMS]>`
494         //   which represents the maximal possible number of parameters that can
495         //   be passed to lifted component functions. This is modeled with
496         //   `MaybeUninit` to represent how it all starts as uninitialized and
497         //   thus can't be safely read during lowering.
498         //
499         // * results are modeled as `[ValRaw; MAX_FLAT_RESULTS]` which
500         //   represents the maximal size of values that can be returned. Note
501         //   that if the function doesn't actually have a return value then the
502         //   `ValRaw` inside the array will have undefined contents. That is
503         //   safe in Rust, however, due to `ValRaw` being a `union`. The
504         //   contents should dynamically not be read due to the type of the
505         //   function used here matching the actual lift.
506         unsafe {
507             self.call_raw(
508                 store,
509                 |cx, ty, dst: &mut MaybeUninit<[MaybeUninit<ValRaw>; MAX_FLAT_PARAMS]>| {
510                     // SAFETY: it's safe to assume that
511                     // `MaybeUninit<array-of-maybe-uninit>` is initialized because
512                     // each individual element is still considered uninitialized.
513                     let dst: &mut [MaybeUninit<ValRaw>] = dst.assume_init_mut();
514                     Self::lower_args(cx, params, ty, dst)
515                 },
516                 |cx, results_ty, src: &[ValRaw; MAX_FLAT_RESULTS]| {
517                     let max_flat = MAX_FLAT_RESULTS;
518                     for (result, slot) in
519                         Self::lift_results(cx, results_ty, src, max_flat)?.zip(results)
520                     {
521                         *slot = result?;
522                     }
523                     Ok(())
524                 },
525             )
526         }
527     }
528 
529     pub(crate) fn lifted_core_func(&self, store: &mut StoreOpaque) -> NonNull<VMFuncRef> {
530         let def = {
531             let instance = self.instance.id().get(store);
532             let (_ty, def, _options) = instance.component().export_lifted_function(self.index);
533             def.clone()
534         };
535         match self.instance.lookup_vmdef(store, &def) {
536             Export::Function(f) => f.vm_func_ref(store),
537             _ => unreachable!(),
538         }
539     }
540 
541     pub(crate) fn post_return_core_func(&self, store: &StoreOpaque) -> Option<NonNull<VMFuncRef>> {
542         let instance = self.instance.id().get(store);
543         let component = instance.component();
544         let (_ty, _def, options) = component.export_lifted_function(self.index);
545         let post_return = component.env_component().options[options].post_return;
546         post_return.map(|i| instance.runtime_post_return(i))
547     }
548 
549     pub(crate) fn abi_async(&self, store: &StoreOpaque) -> bool {
550         let instance = self.instance.id().get(store);
551         let component = instance.component();
552         let (_ty, _def, options) = component.export_lifted_function(self.index);
553         component.env_component().options[options].async_
554     }
555 
556     pub(crate) fn abi_info<'a>(
557         &self,
558         store: &'a StoreOpaque,
559     ) -> (
560         OptionsIndex,
561         InstanceFlags,
562         TypeFuncIndex,
563         &'a CanonicalOptions,
564     ) {
565         let vminstance = self.instance.id().get(store);
566         let component = vminstance.component();
567         let (ty, _def, options_index) = component.export_lifted_function(self.index);
568         let raw_options = &component.env_component().options[options_index];
569         (
570             options_index,
571             vminstance.instance_flags(raw_options.instance),
572             ty,
573             raw_options,
574         )
575     }
576 
577     /// Invokes the underlying wasm function, lowering arguments and lifting the
578     /// result.
579     ///
580     /// The `lower` function and `lift` function provided here are what actually
581     /// do the lowering and lifting. The `LowerParams` and `LowerReturn` types
582     /// are what will be allocated on the stack for this function call. They
583     /// should be appropriately sized for the lowering/lifting operation
584     /// happening.
585     ///
586     /// # Safety
587     ///
588     /// The safety of this function relies on the correct definitions of the
589     /// `LowerParams` and `LowerReturn` type. They must match the type of `self`
590     /// for the params/results that are going to be produced. Additionally
591     /// these types must be representable with a sequence of `ValRaw` values.
592     unsafe fn call_raw<T, Return, LowerParams, LowerReturn>(
593         &self,
594         mut store: StoreContextMut<'_, T>,
595         lower: impl FnOnce(
596             &mut LowerContext<'_, T>,
597             InterfaceType,
598             &mut MaybeUninit<LowerParams>,
599         ) -> Result<()>,
600         lift: impl FnOnce(&mut LiftContext<'_>, InterfaceType, &LowerReturn) -> Result<Return>,
601     ) -> Result<Return>
602     where
603         LowerParams: Copy,
604         LowerReturn: Copy,
605     {
606         let export = self.lifted_core_func(store.0);
607         let (_options, _flags, _ty, raw_options) = self.abi_info(store.0);
608         let instance = RuntimeInstance {
609             instance: self.instance.id().instance(),
610             index: raw_options.instance,
611         };
612 
613         if !store.0.may_enter(instance) {
614             bail!(crate::Trap::CannotEnterComponent);
615         }
616 
617         if store.0.concurrency_support() {
618             let async_type = self.abi_async(store.0);
619             store.0.enter_sync_call(None, async_type, instance)?;
620         }
621 
622         #[repr(C)]
623         union Union<Params: Copy, Return: Copy> {
624             params: Params,
625             ret: Return,
626         }
627 
628         let space = &mut MaybeUninit::<Union<LowerParams, LowerReturn>>::uninit();
629 
630         // Double-check the size/alignment of `space`, just in case.
631         //
632         // Note that this alone is not enough to guarantee the validity of the
633         // `unsafe` block below, but it's definitely required. In any case LLVM
634         // should be able to trivially see through these assertions and remove
635         // them in release mode.
636         let val_size = mem::size_of::<ValRaw>();
637         let val_align = mem::align_of::<ValRaw>();
638         assert!(mem::size_of_val(space) % val_size == 0);
639         assert!(mem::size_of_val(map_maybe_uninit!(space.params)) % val_size == 0);
640         assert!(mem::size_of_val(map_maybe_uninit!(space.ret)) % val_size == 0);
641         assert!(mem::align_of_val(space) == val_align);
642         assert!(mem::align_of_val(map_maybe_uninit!(space.params)) == val_align);
643         assert!(mem::align_of_val(map_maybe_uninit!(space.ret)) == val_align);
644 
645         self.with_lower_context(store.as_context_mut(), false, |cx, ty| {
646             cx.enter_call();
647             lower(cx, ty, map_maybe_uninit!(space.params))
648         })?;
649 
650         // SAFETY: We are providing the guarantee that all the inputs are valid.
651         // The various pointers passed in for the function are all valid since
652         // they're coming from our store, and the `params_and_results` should
653         // have the correct layout for the core wasm function we're calling.
654         // Note that this latter point relies on the correctness of this module
655         // and `ComponentType` implementations, hence `ComponentType` being an
656         // `unsafe` trait.
657         unsafe {
658             crate::Func::call_unchecked_raw(
659                 &mut store,
660                 export,
661                 NonNull::new(core::ptr::slice_from_raw_parts_mut(
662                     space.as_mut_ptr().cast(),
663                     mem::size_of_val(space) / mem::size_of::<ValRaw>(),
664                 ))
665                 .unwrap(),
666             )?;
667         }
668 
669         // SAFETY: We're relying on the correctness of the structure of
670         // `LowerReturn` and the type-checking performed to acquire the
671         // `TypedFunc` to make this safe. It should be the case that
672         // `LowerReturn` is the exact representation of the return value when
673         // interpreted as `[ValRaw]`, and additionally they should have the
674         // correct types for the function we just called (which filled in the
675         // return values).
676         let ret: &LowerReturn = unsafe { map_maybe_uninit!(space.ret).assume_init_ref() };
677 
678         // Lift the result into the host while managing post-return state
679         // here as well.
680         //
681         // After a successful lift the return value of the function, which
682         // is currently required to be 0 or 1 values according to the
683         // canonical ABI, is saved within the `Store`'s `FuncData`. This'll
684         // later get used in post-return.
685         // flags.set_needs_post_return(true);
686         let val = self.with_lift_context(store.0, |cx, ty| lift(cx, ty, ret))?;
687 
688         // SAFETY: it's a contract of this function that `LowerReturn` is an
689         // appropriate representation of the result of this function.
690         let ret_slice = unsafe { storage_as_slice(ret) };
691 
692         self.instance.id().get_mut(store.0).post_return_arg_set(
693             self.index,
694             match ret_slice.len() {
695                 0 => ValRaw::i32(0),
696                 1 => ret_slice[0],
697                 _ => unreachable!(),
698             },
699         );
700 
701         return Ok(val);
702     }
703 
704     /// Invokes the `post-return` canonical ABI option, if specified, after a
705     /// [`Func::call`] has finished.
706     ///
707     /// This function is a required method call after a [`Func::call`] completes
708     /// successfully. After the embedder has finished processing the return
709     /// value then this function must be invoked.
710     ///
711     /// # Errors
712     ///
713     /// This function will return an error in the case of a WebAssembly trap
714     /// happening during the execution of the `post-return` function, if
715     /// specified.
716     ///
717     /// # Panics
718     ///
719     /// This function will panic if it's not called under the correct
720     /// conditions. This can only be called after a previous invocation of
721     /// [`Func::call`] completes successfully, and this function can only
722     /// be called for the same [`Func`] that was `call`'d.
723     ///
724     /// If this function is called when [`Func::call`] was not previously
725     /// called, then it will panic. If a different [`Func`] for the same
726     /// component instance was invoked then this function will also panic
727     /// because the `post-return` needs to happen for the other function.
728     #[inline]
729     pub fn post_return(&self, mut store: impl AsContextMut) -> Result<()> {
730         let store = store.as_context_mut();
731         store.0.validate_sync_call()?;
732         self.post_return_impl(store, false)
733     }
734 
735     /// Exactly like [`Self::post_return`] except for invoke WebAssembly
736     /// [asynchronously](crate::#async).
737     #[cfg(feature = "async")]
738     pub async fn post_return_async(&self, mut store: impl AsContextMut<Data: Send>) -> Result<()> {
739         let mut store = store.as_context_mut();
740         // Future optimization opportunity: conditionally use a fiber here since
741         // some func's post_return will not need the async context (i.e. end up
742         // calling async host functionality)
743         store
744             .on_fiber(|store| self.post_return_impl(store, true))
745             .await?
746     }
747 
748     fn post_return_impl(&self, mut store: impl AsContextMut, async_: bool) -> Result<()> {
749         let mut store = store.as_context_mut();
750 
751         let index = self.index;
752         let vminstance = self.instance.id().get(store.0);
753         let component = vminstance.component();
754         let (_ty, _def, options) = component.export_lifted_function(index);
755         let post_return = self.post_return_core_func(store.0);
756         let mut flags =
757             vminstance.instance_flags(component.env_component().options[options].instance);
758         let mut instance = self.instance.id().get_mut(store.0);
759         let post_return_arg = instance.as_mut().post_return_arg_take(index);
760 
761         unsafe {
762             // First assert that the instance is in a "needs post return" state.
763             // This will ensure that the previous action on the instance was a
764             // function call above. This flag is only set after a component
765             // function returns so this also can't be called (as expected)
766             // during a host import for example.
767             //
768             // Note, though, that this assert is not sufficient because it just
769             // means some function on this instance needs its post-return
770             // called. We need a precise post-return for a particular function
771             // which is the second assert here (the `.expect`). That will assert
772             // that this function itself needs to have its post-return called.
773             //
774             // The theory at least is that these two asserts ensure component
775             // model semantics are upheld where the host properly calls
776             // `post_return` on the right function despite the call being a
777             // separate step in the API.
778             assert!(
779                 flags.needs_post_return(),
780                 "post_return can only be called after a function has previously been called",
781             );
782             let post_return_arg = post_return_arg.expect("calling post_return on wrong function");
783 
784             // Unset the "needs post return" flag now that post-return is being
785             // processed. This will cause future invocations of this method to
786             // panic, even if the function call below traps.
787             flags.set_needs_post_return(false);
788 
789             // Post return functions are forbidden from calling imports or
790             // intrinsics.
791             flags.set_may_leave(false);
792 
793             // If the function actually had a `post-return` configured in its
794             // canonical options that's executed here.
795             if let Some(func) = post_return {
796                 crate::Func::call_unchecked_raw(
797                     &mut store,
798                     func,
799                     NonNull::new(core::ptr::slice_from_raw_parts(&post_return_arg, 1).cast_mut())
800                         .unwrap(),
801                 )?;
802             }
803 
804             // And finally if everything completed successfully then the "may
805             // leave" flags is set to `true` again here which enables further
806             // use of the component.
807             flags.set_may_leave(true);
808 
809             let (calls, host_table, _, instance) = store
810                 .0
811                 .component_resource_state_with_instance(self.instance);
812             ResourceTables {
813                 host_table: Some(host_table),
814                 calls,
815                 guest: Some(instance.instance_states()),
816             }
817             .exit_call()?;
818 
819             if !async_ && store.0.concurrency_support() {
820                 store.0.exit_sync_call(false)?;
821             }
822         }
823         Ok(())
824     }
825 
826     fn lower_args<T>(
827         cx: &mut LowerContext<'_, T>,
828         params: &[Val],
829         params_ty: InterfaceType,
830         dst: &mut [MaybeUninit<ValRaw>],
831     ) -> Result<()> {
832         let params_ty = match params_ty {
833             InterfaceType::Tuple(i) => &cx.types[i],
834             _ => unreachable!(),
835         };
836         if params_ty.abi.flat_count(MAX_FLAT_PARAMS).is_some() {
837             let dst = &mut dst.iter_mut();
838 
839             params
840                 .iter()
841                 .zip(params_ty.types.iter())
842                 .try_for_each(|(param, ty)| param.lower(cx, *ty, dst))
843         } else {
844             Self::store_args(cx, &params_ty, params, dst)
845         }
846     }
847 
848     fn store_args<T>(
849         cx: &mut LowerContext<'_, T>,
850         params_ty: &TypeTuple,
851         args: &[Val],
852         dst: &mut [MaybeUninit<ValRaw>],
853     ) -> Result<()> {
854         let size = usize::try_from(params_ty.abi.size32).unwrap();
855         let ptr = cx.realloc(0, 0, params_ty.abi.align32, size)?;
856         let mut offset = ptr;
857         for (ty, arg) in params_ty.types.iter().zip(args) {
858             let abi = cx.types.canonical_abi(ty);
859             arg.store(cx, *ty, abi.next_field32_size(&mut offset))?;
860         }
861 
862         dst[0].write(ValRaw::i64(ptr as i64));
863 
864         Ok(())
865     }
866 
867     fn lift_results<'a, 'b>(
868         cx: &'a mut LiftContext<'b>,
869         results_ty: InterfaceType,
870         src: &'a [ValRaw],
871         max_flat: usize,
872     ) -> Result<Box<dyn Iterator<Item = Result<Val>> + 'a>> {
873         let results_ty = match results_ty {
874             InterfaceType::Tuple(i) => &cx.types[i],
875             _ => unreachable!(),
876         };
877         if results_ty.abi.flat_count(max_flat).is_some() {
878             let mut flat = src.iter();
879             Ok(Box::new(
880                 results_ty
881                     .types
882                     .iter()
883                     .map(move |ty| Val::lift(cx, *ty, &mut flat)),
884             ))
885         } else {
886             let iter = Self::load_results(cx, results_ty, &mut src.iter())?;
887             Ok(Box::new(iter))
888         }
889     }
890 
891     fn load_results<'a, 'b>(
892         cx: &'a mut LiftContext<'b>,
893         results_ty: &'a TypeTuple,
894         src: &mut core::slice::Iter<'_, ValRaw>,
895     ) -> Result<impl Iterator<Item = Result<Val>> + use<'a, 'b>> {
896         // FIXME(#4311): needs to read an i64 for memory64
897         let ptr = usize::try_from(src.next().unwrap().get_u32())?;
898         if ptr % usize::try_from(results_ty.abi.align32)? != 0 {
899             bail!("return pointer not aligned");
900         }
901 
902         let bytes = cx
903             .memory()
904             .get(ptr..)
905             .and_then(|b| b.get(..usize::try_from(results_ty.abi.size32).unwrap()))
906             .ok_or_else(|| crate::format_err!("pointer out of bounds of memory"))?;
907 
908         let mut offset = 0;
909         Ok(results_ty.types.iter().map(move |ty| {
910             let abi = cx.types.canonical_abi(ty);
911             let offset = abi.next_field32_size(&mut offset);
912             Val::load(cx, *ty, &bytes[offset..][..abi.size32 as usize])
913         }))
914     }
915 
916     #[cfg(feature = "component-model-async")]
917     pub(crate) fn instance(self) -> Instance {
918         self.instance
919     }
920 
921     #[cfg(feature = "component-model-async")]
922     pub(crate) fn index(self) -> ExportIndex {
923         self.index
924     }
925 
926     /// Creates a `LowerContext` using the configuration values of this lifted
927     /// function.
928     ///
929     /// The `lower` closure provided should perform the actual lowering and
930     /// return the result of the lowering operation which is then returned from
931     /// this function as well.
932     fn with_lower_context<T>(
933         self,
934         mut store: StoreContextMut<T>,
935         call_post_return_automatically: bool,
936         lower: impl FnOnce(&mut LowerContext<T>, InterfaceType) -> Result<()>,
937     ) -> Result<()> {
938         let (options_idx, mut flags, ty, options) = self.abi_info(store.0);
939         let async_ = options.async_;
940 
941         // Perform the actual lowering, where while this is running the
942         // component is forbidden from calling imports.
943         unsafe {
944             debug_assert!(flags.may_leave());
945             flags.set_may_leave(false);
946         }
947         let mut cx = LowerContext::new(store.as_context_mut(), options_idx, self.instance);
948         let param_ty = InterfaceType::Tuple(cx.types[ty].params);
949         let result = lower(&mut cx, param_ty);
950         unsafe { flags.set_may_leave(true) };
951         result?;
952 
953         // If needed, flag a post-return call being required as we're about to
954         // enter wasm and afterwards need a post-return.
955         unsafe {
956             if !(call_post_return_automatically && async_) {
957                 flags.set_needs_post_return(true);
958             }
959         }
960 
961         Ok(())
962     }
963 
964     /// Creates a `LiftContext` using the configuration values with this lifted
965     /// function.
966     ///
967     /// The closure `lift` provided should actually perform the lift itself and
968     /// the result of that closure is returned from this function call as well.
969     fn with_lift_context<R>(
970         self,
971         store: &mut StoreOpaque,
972         lift: impl FnOnce(&mut LiftContext, InterfaceType) -> Result<R>,
973     ) -> Result<R> {
974         let (options, _flags, ty, _) = self.abi_info(store);
975         let mut cx = LiftContext::new(store, options, self.instance);
976         let ty = InterfaceType::Tuple(cx.types[ty].results);
977         lift(&mut cx, ty)
978     }
979 }
980 
981 /// Represents the completion of a task created using
982 /// `[Typed]Func::call_concurrent`.
983 ///
984 /// In general, a guest task may continue running after returning a value.
985 /// Moreover, any given guest task may create its own subtasks before or after
986 /// returning and may exit before some or all of those subtasks have finished
987 /// running.  In that case, the still-running subtasks will be "reparented" to
988 /// the nearest surviving caller, which may be the original host call.  The
989 /// future returned by `TaskExit::block` will resolve once all transitive
990 /// subtasks created directly or indirectly by the original call to
991 /// `Instance::call_concurrent` have exited.
992 #[cfg(feature = "component-model-async")]
993 pub struct TaskExit(futures::channel::oneshot::Receiver<()>);
994 
995 #[cfg(feature = "component-model-async")]
996 impl TaskExit {
997     /// Returns a future which will resolve once all transitive subtasks created
998     /// directly or indirectly by the original call to
999     /// `Instance::call_concurrent` have exited.
1000     pub async fn block(self, accessor: impl AsAccessor<Data: Send>) {
1001         // The current implementation makes no use of `accessor`, but future
1002         // implementations might (e.g. by using a more efficient mechanism than
1003         // a oneshot channel).
1004         _ = accessor;
1005 
1006         // We don't care whether the sender sent us a value or was dropped
1007         // first; either one counts as a notification, so we ignore the result
1008         // once the future resolves:
1009         _ = self.0.await;
1010     }
1011 }
1012