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};
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 {
from_lifted_func(instance: Instance, index: ExportIndex) -> Func56     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     /// ```
typed<Params, Return>(&self, store: impl AsContext) -> Result<TypedFunc<Params, Return>> where Params: ComponentNamedList + Lower, Return: ComponentNamedList + Lift,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 
_typed<Params, Return>( &self, store: &StoreOpaque, instance: Option<&ComponentInstance>, ) -> Result<TypedFunc<Params, Return>> where Params: ComponentNamedList + Lower, Return: ComponentNamedList + Lift,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 
typecheck<Params, Return>( &self, store: &StoreOpaque, instance: Option<&ComponentInstance>, ) -> Result<()> where Params: ComponentNamedList + Lower, Return: ComponentNamedList + Lift,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.
ty(&self, store: impl AsContext) -> ComponentFunc181     pub fn ty(&self, store: impl AsContext) -> ComponentFunc {
182         self.ty_(store.as_context().0)
183     }
184 
ty_(&self, store: &StoreOpaque) -> ComponentFunc185     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 
ty_index(&self, store: &StoreOpaque) -> TypeFuncIndex191     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.
call( &self, mut store: impl AsContextMut, params: &[Val], results: &mut [Val], ) -> Result<()>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")]
call_async( &self, mut store: impl AsContextMut<Data: Send>, params: &[Val], results: &mut [Val], ) -> Result<()>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 
check_params_results<T>( &self, store: StoreContextMut<T>, params: &[Val], results: &mut [Val], ) -> Result<()>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")]
call_concurrent( self, accessor: impl AsAccessor<Data: Send>, params: &[Val], results: &mut [Val], ) -> Result<()>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")]
call_concurrent_dynamic( self, accessor: impl AsAccessor<Data: Send>, params: &[Val], results: &mut [Val], ) -> Result<()>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")]
prepare_call_dynamic<'a, T: Send + 'static>( self, mut store: StoreContextMut<'a, T>, params: Vec<Val>, ) -> Result<PreparedCall<Vec<Val>>>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 
call_impl( &self, mut store: impl AsContextMut, params: &[Val], results: &mut [Val], ) -> Result<()>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 
lifted_core_func(&self, store: &mut StoreOpaque) -> NonNull<VMFuncRef>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 
post_return_core_func(&self, store: &StoreOpaque) -> Option<NonNull<VMFuncRef>>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 
abi_async(&self, store: &StoreOpaque) -> bool535     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 
abi_info<'a>( &self, store: &'a StoreOpaque, ) -> ( OptionsIndex, InstanceFlags, TypeFuncIndex, &'a CanonicalOptions, )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.
call_raw<T, Return, LowerParams, LowerReturn>( &self, mut store: StoreContextMut<'_, T>, lower: impl FnOnce( &mut LowerContext<'_, T>, InterfaceType, &mut MaybeUninit<LowerParams>, ) -> Result<()>, lift: impl FnOnce(&mut LiftContext<'_>, InterfaceType, &LowerReturn) -> Result<Return>, ) -> Result<(Return, ValRaw)> where LowerParams: Copy, LowerReturn: Copy,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         // Validate that the task, after returning, has no more active borrows
653         // as they're required to have been dropped by this point.
654         store
655             .0
656             .component_resource_tables(Some(self.instance))
657             .validate_scope_exit()?;
658 
659         // SAFETY: We're relying on the correctness of the structure of
660         // `LowerReturn` and the type-checking performed to acquire the
661         // `TypedFunc` to make this safe. It should be the case that
662         // `LowerReturn` is the exact representation of the return value when
663         // interpreted as `[ValRaw]`, and additionally they should have the
664         // correct types for the function we just called (which filled in the
665         // return values).
666         let ret: &LowerReturn = unsafe { map_maybe_uninit!(space.ret).assume_init_ref() };
667 
668         // Lift the result into the host while managing post-return state
669         // here as well.
670         //
671         // After a successful lift the return value of the function, which
672         // is currently required to be 0 or 1 values according to the
673         // canonical ABI, is saved within the `Store`'s `FuncData`. This'll
674         // later get used in post-return.
675         let val = self.with_lift_context(store.0, |cx, ty| lift(cx, ty, ret))?;
676 
677         // SAFETY: it's a contract of this function that `LowerReturn` is an
678         // appropriate representation of the result of this function.
679         let ret_slice = unsafe { storage_as_slice(ret) };
680 
681         Ok((
682             val,
683             match ret_slice.len() {
684                 0 => ValRaw::i32(0),
685                 1 => ret_slice[0],
686                 _ => unreachable!(),
687             },
688         ))
689     }
690 
691     #[doc(hidden)]
692     #[deprecated(note = "no longer needs to be called; this function has no effect")]
post_return(&self, _store: impl AsContextMut) -> Result<()>693     pub fn post_return(&self, _store: impl AsContextMut) -> Result<()> {
694         Ok(())
695     }
696 
697     #[doc(hidden)]
698     #[deprecated(note = "no longer needs to be called; this function has no effect")]
699     #[cfg(feature = "async")]
post_return_async(&self, _store: impl AsContextMut<Data: Send>) -> Result<()>700     pub async fn post_return_async(&self, _store: impl AsContextMut<Data: Send>) -> Result<()> {
701         Ok(())
702     }
703 
post_return_impl(&self, mut store: impl AsContextMut, arg: ValRaw) -> Result<()>704     pub(crate) fn post_return_impl(&self, mut store: impl AsContextMut, arg: ValRaw) -> Result<()> {
705         let mut store = store.as_context_mut();
706 
707         let index = self.index;
708         let vminstance = self.instance.id().get(store.0);
709         let component = vminstance.component();
710         let (_ty, _def, options) = component.export_lifted_function(index);
711         let post_return = self.post_return_core_func(store.0);
712         let flags = vminstance.instance_flags(component.env_component().options[options].instance);
713 
714         unsafe {
715             call_post_return(&mut store, post_return, arg, flags)?;
716             store.0.exit_guest_sync_call()?;
717         }
718         Ok(())
719     }
720 
lower_args<T>( cx: &mut LowerContext<'_, T>, params: &[Val], params_ty: InterfaceType, dst: &mut [MaybeUninit<ValRaw>], ) -> Result<()>721     fn lower_args<T>(
722         cx: &mut LowerContext<'_, T>,
723         params: &[Val],
724         params_ty: InterfaceType,
725         dst: &mut [MaybeUninit<ValRaw>],
726     ) -> Result<()> {
727         let params_ty = match params_ty {
728             InterfaceType::Tuple(i) => &cx.types[i],
729             _ => unreachable!(),
730         };
731         if params_ty.abi.flat_count(MAX_FLAT_PARAMS).is_some() {
732             let dst = &mut dst.iter_mut();
733 
734             params
735                 .iter()
736                 .zip(params_ty.types.iter())
737                 .try_for_each(|(param, ty)| param.lower(cx, *ty, dst))
738         } else {
739             Self::store_args(cx, &params_ty, params, dst)
740         }
741     }
742 
store_args<T>( cx: &mut LowerContext<'_, T>, params_ty: &TypeTuple, args: &[Val], dst: &mut [MaybeUninit<ValRaw>], ) -> Result<()>743     fn store_args<T>(
744         cx: &mut LowerContext<'_, T>,
745         params_ty: &TypeTuple,
746         args: &[Val],
747         dst: &mut [MaybeUninit<ValRaw>],
748     ) -> Result<()> {
749         let size = usize::try_from(params_ty.abi.size32).unwrap();
750         let ptr = cx.realloc(0, 0, params_ty.abi.align32, size)?;
751         let mut offset = ptr;
752         for (ty, arg) in params_ty.types.iter().zip(args) {
753             let abi = cx.types.canonical_abi(ty);
754             arg.store(cx, *ty, abi.next_field32_size(&mut offset))?;
755         }
756 
757         dst[0].write(ValRaw::i64(ptr as i64));
758 
759         Ok(())
760     }
761 
lift_results<'a, 'b>( cx: &'a mut LiftContext<'b>, results_ty: InterfaceType, src: &'a [ValRaw], max_flat: usize, ) -> Result<Box<dyn Iterator<Item = Result<Val>> + 'a>>762     fn lift_results<'a, 'b>(
763         cx: &'a mut LiftContext<'b>,
764         results_ty: InterfaceType,
765         src: &'a [ValRaw],
766         max_flat: usize,
767     ) -> Result<Box<dyn Iterator<Item = Result<Val>> + 'a>> {
768         let results_ty = match results_ty {
769             InterfaceType::Tuple(i) => &cx.types[i],
770             _ => unreachable!(),
771         };
772         if results_ty.abi.flat_count(max_flat).is_some() {
773             let mut flat = src.iter();
774             Ok(Box::new(
775                 results_ty
776                     .types
777                     .iter()
778                     .map(move |ty| Val::lift(cx, *ty, &mut flat)),
779             ))
780         } else {
781             let iter = Self::load_results(cx, results_ty, &mut src.iter())?;
782             Ok(Box::new(iter))
783         }
784     }
785 
load_results<'a, 'b>( cx: &'a mut LiftContext<'b>, results_ty: &'a TypeTuple, src: &mut core::slice::Iter<'_, ValRaw>, ) -> Result<impl Iterator<Item = Result<Val>> + use<'a, 'b>>786     fn load_results<'a, 'b>(
787         cx: &'a mut LiftContext<'b>,
788         results_ty: &'a TypeTuple,
789         src: &mut core::slice::Iter<'_, ValRaw>,
790     ) -> Result<impl Iterator<Item = Result<Val>> + use<'a, 'b>> {
791         // FIXME(#4311): needs to read an i64 for memory64
792         let ptr = usize::try_from(src.next().unwrap().get_u32())?;
793         if ptr % usize::try_from(results_ty.abi.align32)? != 0 {
794             bail!("return pointer not aligned");
795         }
796 
797         let bytes = cx
798             .memory()
799             .get(ptr..)
800             .and_then(|b| b.get(..usize::try_from(results_ty.abi.size32).unwrap()))
801             .ok_or_else(|| crate::format_err!("pointer out of bounds of memory"))?;
802 
803         let mut offset = 0;
804         Ok(results_ty.types.iter().map(move |ty| {
805             let abi = cx.types.canonical_abi(ty);
806             let offset = abi.next_field32_size(&mut offset);
807             Val::load(cx, *ty, &bytes[offset..][..abi.size32 as usize])
808         }))
809     }
810 
811     #[cfg(feature = "component-model-async")]
instance(self) -> Instance812     pub(crate) fn instance(self) -> Instance {
813         self.instance
814     }
815 
816     /// Creates a `LowerContext` using the configuration values of this lifted
817     /// function.
818     ///
819     /// The `lower` closure provided should perform the actual lowering and
820     /// return the result of the lowering operation which is then returned from
821     /// this function as well.
with_lower_context<T>( self, mut store: StoreContextMut<T>, lower: impl FnOnce(&mut LowerContext<T>, InterfaceType) -> Result<()>, ) -> Result<()>822     fn with_lower_context<T>(
823         self,
824         mut store: StoreContextMut<T>,
825         lower: impl FnOnce(&mut LowerContext<T>, InterfaceType) -> Result<()>,
826     ) -> Result<()> {
827         let (options_idx, mut flags, ty, _) = self.abi_info(store.0);
828 
829         // Perform the actual lowering, where while this is running the
830         // component is forbidden from calling imports.
831         unsafe {
832             debug_assert!(flags.may_leave());
833             flags.set_may_leave(false);
834         }
835         let mut cx = LowerContext::new(store.as_context_mut(), options_idx, self.instance);
836         let param_ty = InterfaceType::Tuple(cx.types[ty].params);
837         let result = lower(&mut cx, param_ty);
838         unsafe { flags.set_may_leave(true) };
839         result
840     }
841 
842     /// Creates a `LiftContext` using the configuration values with this lifted
843     /// function.
844     ///
845     /// The closure `lift` provided should actually perform the lift itself and
846     /// the result of that closure is returned from this function call as well.
with_lift_context<R>( self, store: &mut StoreOpaque, lift: impl FnOnce(&mut LiftContext, InterfaceType) -> Result<R>, ) -> Result<R>847     fn with_lift_context<R>(
848         self,
849         store: &mut StoreOpaque,
850         lift: impl FnOnce(&mut LiftContext, InterfaceType) -> Result<R>,
851     ) -> Result<R> {
852         let (options, _flags, ty, _) = self.abi_info(store);
853         let mut cx = LiftContext::new(store, options, self.instance);
854         let ty = InterfaceType::Tuple(cx.types[ty].results);
855         lift(&mut cx, ty)
856     }
857 }
858 
call_post_return( mut store: impl AsContextMut, func: Option<NonNull<VMFuncRef>>, arg: ValRaw, mut flags: InstanceFlags, ) -> Result<()>859 pub(crate) unsafe fn call_post_return(
860     mut store: impl AsContextMut,
861     func: Option<NonNull<VMFuncRef>>,
862     arg: ValRaw,
863     mut flags: InstanceFlags,
864 ) -> Result<()> {
865     unsafe {
866         // Post return functions are forbidden from calling imports or
867         // intrinsics.
868         flags.set_may_leave(false);
869 
870         // If the function actually had a `post-return` configured in its
871         // canonical options that's executed here.
872         if let Some(func) = func {
873             crate::Func::call_unchecked_raw(
874                 &mut store.as_context_mut(),
875                 func,
876                 core::slice::from_ref(&arg).into(),
877             )?;
878         }
879 
880         // And finally if everything completed successfully then the "may
881         // leave" flags is set to `true` again here which enables further
882         // use of the component.
883         flags.set_may_leave(true);
884     }
885 
886     Ok(())
887 }
888