1 //! Implementation of calling Rust-defined functions from components.
2 
3 #[cfg(feature = "component-model-async")]
4 use crate::component::RuntimeInstance;
5 #[cfg(feature = "component-model-async")]
6 use crate::component::concurrent;
7 #[cfg(feature = "component-model-async")]
8 use crate::component::concurrent::{Accessor, Status};
9 use crate::component::func::{LiftContext, LowerContext};
10 use crate::component::matching::InstanceType;
11 use crate::component::storage::{slice_to_storage, slice_to_storage_mut};
12 use crate::component::types::ComponentFunc;
13 use crate::component::{ComponentNamedList, Instance, Lift, Lower, Val};
14 use crate::prelude::*;
15 use crate::runtime::vm::component::{
16     ComponentInstance, VMComponentContext, VMLowering, VMLoweringCallee,
17 };
18 use crate::runtime::vm::{VMOpaqueContext, VMStore};
19 use crate::store::Asyncness;
20 use crate::{AsContextMut, CallHook, StoreContextMut, ValRaw};
21 use alloc::sync::Arc;
22 use core::any::Any;
23 use core::mem::{self, MaybeUninit};
24 #[cfg(feature = "async")]
25 use core::pin::Pin;
26 use core::ptr::NonNull;
27 use wasmtime_environ::component::{
28     CanonicalAbiInfo, InterfaceType, MAX_FLAT_PARAMS, MAX_FLAT_RESULTS, OptionsIndex, TypeFuncIndex,
29 };
30 
31 /// A host function suitable for passing into a component.
32 ///
33 /// This structure represents a monomorphic host function that can only be used
34 /// in the specific context of a particular store. This is generally not too
35 /// too safe to use and is only meant for internal use.
36 pub struct HostFunc {
37     /// The raw function pointer which Cranelift will invoke.
38     entrypoint: VMLoweringCallee,
39 
40     /// The implementation of type-checking to ensure that this function
41     /// ascribes to the provided function type.
42     ///
43     /// This is used, for example, when a component imports a host function and
44     /// this will determine if the host function can be imported with the given
45     /// type.
46     typecheck: fn(TypeFuncIndex, &InstanceType<'_>) -> Result<()>,
47 
48     /// The actual host function.
49     ///
50     /// This is frequently an empty allocation in the sense that the underlying
51     /// type is a zero-sized-type. Host functions are allowed, though, to close
52     /// over the environment as well.
53     func: Box<dyn Any + Send + Sync>,
54 
55     /// Whether or not this host function was defined in such a way that async
56     /// stack switching is required when calling it.
57     asyncness: Asyncness,
58 }
59 
60 impl core::fmt::Debug for HostFunc {
61     fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
62         f.debug_struct("HostFunc").finish_non_exhaustive()
63     }
64 }
65 
66 enum HostResult<T> {
67     Done(Result<T>),
68     #[cfg(feature = "component-model-async")]
69     Future(Pin<Box<dyn Future<Output = Result<T>> + Send>>),
70 }
71 
72 impl HostFunc {
73     /// Creates a new host function based on the implementation of `func`.
74     ///
75     /// The `asyncness` parameter indicates whether the `func` requires
76     /// wasm to be on a fiber. This is used to propagate to the `Store` during
77     /// instantiation to ensure that this guarantee is met.
78     ///
79     /// Note that if `asyncness` is mistaken then that'll result in panics
80     /// in Wasmtime, but not memory unsafety.
81     fn new<T, F, P, R>(asyncness: Asyncness, func: F) -> Arc<HostFunc>
82     where
83         T: 'static,
84         R: Send + Sync + 'static,
85         F: HostFn<T, P, R> + Send + Sync + 'static,
86     {
87         Arc::new(HostFunc {
88             entrypoint: F::cabi_entrypoint,
89             typecheck: F::typecheck,
90             func: Box::new(func),
91             asyncness,
92         })
93     }
94 
95     /// Equivalent for `Linker::func_wrap`
96     pub(crate) fn func_wrap<T, F, P, R>(func: F) -> Arc<HostFunc>
97     where
98         T: 'static,
99         F: Fn(StoreContextMut<T>, P) -> Result<R> + Send + Sync + 'static,
100         P: ComponentNamedList + Lift + 'static,
101         R: ComponentNamedList + Lower + 'static,
102     {
103         Self::new(
104             Asyncness::No,
105             StaticHostFn::<_, false>::new(move |store, params| {
106                 HostResult::Done(func(store, params))
107             }),
108         )
109     }
110 
111     /// Equivalent for `Linker::func_wrap_async`
112     #[cfg(feature = "async")]
113     pub(crate) fn func_wrap_async<T, F, P, R>(func: F) -> Arc<HostFunc>
114     where
115         T: 'static,
116         F: Fn(StoreContextMut<'_, T>, P) -> Box<dyn Future<Output = Result<R>> + Send + '_>
117             + Send
118             + Sync
119             + 'static,
120         P: ComponentNamedList + Lift + 'static,
121         R: ComponentNamedList + Lower + 'static,
122     {
123         Self::new(
124             Asyncness::Yes,
125             StaticHostFn::<_, false>::new(move |store, params| {
126                 HostResult::Done(
127                     store
128                         .block_on(|store| Pin::from(func(store, params)))
129                         .and_then(|r| r),
130                 )
131             }),
132         )
133     }
134 
135     /// Equivalent for `Linker::func_wrap_concurrent`
136     #[cfg(feature = "component-model-async")]
137     pub(crate) fn func_wrap_concurrent<T, F, P, R>(func: F) -> Arc<HostFunc>
138     where
139         T: 'static,
140         F: Fn(&Accessor<T>, P) -> Pin<Box<dyn Future<Output = Result<R>> + Send + '_>>
141             + Send
142             + Sync
143             + 'static,
144         P: ComponentNamedList + Lift + 'static,
145         R: ComponentNamedList + Lower + 'static,
146     {
147         let func = Arc::new(func);
148         Self::new(
149             Asyncness::Yes,
150             StaticHostFn::<_, true>::new(move |store, params| {
151                 let func = func.clone();
152                 HostResult::Future(Box::pin(
153                     store.wrap_call(move |accessor| func(accessor, params)),
154                 ))
155             }),
156         )
157     }
158 
159     /// Equivalent of `Linker::func_new`
160     pub(crate) fn func_new<T, F>(func: F) -> Arc<HostFunc>
161     where
162         T: 'static,
163         F: Fn(StoreContextMut<'_, T>, ComponentFunc, &[Val], &mut [Val]) -> Result<()>
164             + Send
165             + Sync
166             + 'static,
167     {
168         Self::new(
169             Asyncness::No,
170             DynamicHostFn::<_, false>::new(
171                 move |store, ty, mut params_and_results, result_start| {
172                     let (params, results) = params_and_results.split_at_mut(result_start);
173                     let result = func(store, ty, params, results).map(move |()| params_and_results);
174                     HostResult::Done(result)
175                 },
176             ),
177         )
178     }
179 
180     /// Equivalent of `Linker::func_new_async`
181     #[cfg(feature = "async")]
182     pub(crate) fn func_new_async<T, F>(func: F) -> Arc<HostFunc>
183     where
184         T: 'static,
185         F: for<'a> Fn(
186                 StoreContextMut<'a, T>,
187                 ComponentFunc,
188                 &'a [Val],
189                 &'a mut [Val],
190             ) -> Box<dyn Future<Output = Result<()>> + Send + 'a>
191             + Send
192             + Sync
193             + 'static,
194     {
195         Self::new(
196             Asyncness::Yes,
197             DynamicHostFn::<_, false>::new(
198                 move |store, ty, mut params_and_results, result_start| {
199                     let (params, results) = params_and_results.split_at_mut(result_start);
200                     let result = store
201                         .with_blocking(|store, cx| {
202                             cx.block_on(Pin::from(func(store, ty, params, results)))
203                         })
204                         .and_then(|r| r);
205                     let result = result.map(move |()| params_and_results);
206                     HostResult::Done(result)
207                 },
208             ),
209         )
210     }
211 
212     /// Equivalent of `Linker::func_new_concurrent`
213     #[cfg(feature = "component-model-async")]
214     pub(crate) fn func_new_concurrent<T, F>(func: F) -> Arc<HostFunc>
215     where
216         T: 'static,
217         F: for<'a> Fn(
218                 &'a Accessor<T>,
219                 ComponentFunc,
220                 &'a [Val],
221                 &'a mut [Val],
222             ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'a>>
223             + Send
224             + Sync
225             + 'static,
226     {
227         let func = Arc::new(func);
228         Self::new(
229             Asyncness::Yes,
230             DynamicHostFn::<_, true>::new(
231                 move |store, ty, mut params_and_results, result_start| {
232                     let func = func.clone();
233                     HostResult::Future(Box::pin(store.wrap_call(move |accessor| {
234                         Box::pin(async move {
235                             let (params, results) = params_and_results.split_at_mut(result_start);
236                             func(accessor, ty, params, results).await?;
237                             Ok(params_and_results)
238                         })
239                     })))
240                 },
241             ),
242         )
243     }
244 
245     pub fn typecheck(&self, ty: TypeFuncIndex, types: &InstanceType<'_>) -> Result<()> {
246         (self.typecheck)(ty, types)
247     }
248 
249     pub fn lowering(&self) -> VMLowering {
250         let data = NonNull::from(&*self.func).cast();
251         VMLowering {
252             callee: NonNull::new(self.entrypoint as *mut _).unwrap().into(),
253             data: data.into(),
254         }
255     }
256 
257     pub fn asyncness(&self) -> Asyncness {
258         self.asyncness
259     }
260 }
261 
262 /// Argument to [`HostFn::lift_params`]
263 enum Source<'a> {
264     /// The parameters come from flat wasm arguments which are provided here.
265     Flat(&'a [ValRaw]),
266     /// The parameters come from linear memory at the provided offset, which is
267     /// already validated to be in-bounds.
268     Memory(usize),
269 }
270 
271 /// Argument to [`HostFn::lower_result`]
272 enum Destination<'a> {
273     /// The result is stored in flat parameters whose storage is provided here.
274     Flat(&'a mut [MaybeUninit<ValRaw>]),
275     /// The result is stored in linear memory at the provided offset, which is
276     /// already validated to be in-bounds.
277     Memory(usize),
278 }
279 
280 /// Consolidation of functionality of invoking a host function.
281 ///
282 /// This trait primarily serves as a deduplication of the "static" and
283 /// "dynamic" host function paths where all default functions here are shared
284 /// (source-wise at least) across the two styles of host functions.
285 trait HostFn<T, P, R>
286 where
287     T: 'static,
288     R: Send + Sync + 'static,
289 {
290     /// Whether or not this is `async` function from the perspective of the
291     /// component model.
292     const ASYNC: bool;
293 
294     /// Performs a type-check to ensure that this host function can be imported
295     /// with the provided signature that a component is using.
296     fn typecheck(ty: TypeFuncIndex, types: &InstanceType<'_>) -> Result<()>;
297 
298     /// Execute this host function.
299     fn run(&self, store: StoreContextMut<'_, T>, params: P) -> HostResult<R>;
300 
301     /// Performs the lifting operation to convert arguments from the canonical
302     /// ABI in wasm memory/arguments into their Rust representation.
303     fn lift_params(cx: &mut LiftContext<'_>, ty: TypeFuncIndex, source: Source<'_>) -> Result<P>;
304 
305     /// Performs the lowering operation to convert the result from its Rust
306     /// representation to the canonical ABI representation.
307     fn lower_result(
308         cx: &mut LowerContext<'_, T>,
309         ty: TypeFuncIndex,
310         result: R,
311         dst: Destination<'_>,
312     ) -> Result<()>;
313 
314     /// Raw entrypoint invoked by Cranelift.
315     ///
316     /// # Safety
317     ///
318     /// This function is only safe when called from a trusted source which
319     /// upholds at least these invariants:
320     ///
321     /// * `cx` is a valid pointer which comes from calling wasm.
322     /// * `data` is a valid pointer to `Self`
323     /// * `ty` and `options` are valid within the context of `cx`
324     /// * `storage` and `storage_len` are valid pointers and correspond to
325     ///   correctly initialized wasm arguments/results according to the
326     ///   canonical ABI specified by `ty` and `options`.
327     ///
328     /// The code elsewhere in this trait is all downstream of this `unsafe`,
329     /// and upholding this `unsafe` invariant requires Cranelift, function
330     /// translation, the canonical ABI, and Wasmtime to all stay in sync.
331     /// Basically we can't statically rule out this `unsafe`, we just gotta
332     /// not have bugs.
333     unsafe extern "C" fn cabi_entrypoint(
334         cx: NonNull<VMOpaqueContext>,
335         data: NonNull<u8>,
336         ty: u32,
337         options: u32,
338         storage: NonNull<MaybeUninit<ValRaw>>,
339         storage_len: usize,
340     ) -> bool
341     where
342         Self: Sized,
343     {
344         let cx = unsafe { VMComponentContext::from_opaque(cx) };
345         unsafe {
346             ComponentInstance::enter_host_from_wasm(cx, |store, instance| {
347                 let mut store = store.unchecked_context_mut();
348                 let ty = TypeFuncIndex::from_u32(ty);
349                 let options = OptionsIndex::from_u32(options);
350                 let storage = NonNull::slice_from_raw_parts(storage, storage_len).as_mut();
351                 let data = data.cast::<Self>().as_ref();
352 
353                 store.0.call_hook(CallHook::CallingHost)?;
354                 let res = data.entrypoint(store.as_context_mut(), instance, ty, options, storage);
355                 store.0.call_hook(CallHook::ReturningFromHost)?;
356 
357                 res
358             })
359         }
360     }
361 
362     /// "Rust" entrypoint after panic-handling infrastructure is set up and raw
363     /// arguments are translated to Rust types.
364     fn entrypoint(
365         &self,
366         store: StoreContextMut<'_, T>,
367         instance: Instance,
368         ty: TypeFuncIndex,
369         options: OptionsIndex,
370         storage: &mut [MaybeUninit<ValRaw>],
371     ) -> Result<()> {
372         let vminstance = instance.id().get(store.0);
373         let opts = &vminstance.component().env_component().options[options];
374 
375         if opts.async_ {
376             #[cfg(feature = "component-model-async")]
377             return self.call_async_lower(store, instance, ty, options, storage);
378             #[cfg(not(feature = "component-model-async"))]
379             unreachable!(
380                 "async-lowered imports should have failed validation \
381                  when `component-model-async` feature disabled"
382             );
383         } else {
384             self.call_sync_lower(store, instance, ty, options, storage)
385         }
386     }
387 
388     /// Implementation of the "sync" ABI.
389     ///
390     /// This is the implementation of invoking a host function through the
391     /// synchronous ABI of the component model, or when a function doesn't have
392     /// the `async` option when lowered. Note that the host function itself
393     /// can still be async, in which case this will block here waiting for it
394     /// to finish.
395     fn call_sync_lower(
396         &self,
397         mut store: StoreContextMut<'_, T>,
398         instance: Instance,
399         ty: TypeFuncIndex,
400         options: OptionsIndex,
401         storage: &mut [MaybeUninit<ValRaw>],
402     ) -> Result<()> {
403         if Self::ASYNC {
404             // The caller has synchronously lowered an async function, meaning
405             // the caller can only call it from an async task (i.e. a task
406             // created via a call to an async export).  Otherwise, we'll trap.
407             store.0.check_blocking()?;
408         }
409 
410         let mut lift = LiftContext::new(store.0.store_opaque_mut(), options, instance);
411         let (params, rest) = self.load_params(&mut lift, ty, MAX_FLAT_PARAMS, storage)?;
412         #[cfg(feature = "component-model-async")]
413         let caller_instance = lift.options().instance;
414 
415         let ret = match self.run(store.as_context_mut(), params) {
416             HostResult::Done(result) => result?,
417             #[cfg(feature = "component-model-async")]
418             HostResult::Future(future) => concurrent::poll_and_block(
419                 store.0,
420                 future,
421                 RuntimeInstance {
422                     instance: instance.id().instance(),
423                     index: caller_instance,
424                 },
425             )?,
426         };
427 
428         let mut lower = LowerContext::new(store, options, instance);
429         let fty = &lower.types[ty];
430         let result_tys = &lower.types[fty.results];
431         let dst = if let Some(cnt) = result_tys.abi.flat_count(MAX_FLAT_RESULTS) {
432             Destination::Flat(&mut storage[..cnt])
433         } else {
434             // SAFETY: due to the contract of `entrypoint` we know that the
435             // return pointer, located after the parameters, is initialized
436             // by wasm and safe to read.
437             let ptr = unsafe { rest[0].assume_init_ref() };
438             Destination::Memory(validate_inbounds_dynamic(
439                 &result_tys.abi,
440                 lower.as_slice_mut(),
441                 ptr,
442             )?)
443         };
444         Self::lower_result_and_exit_call(&mut lower, ty, ret, dst)
445     }
446 
447     /// Implementation of the "async" ABI of the component model.
448     ///
449     /// This is invoked when a component has the `async` options specified on
450     /// its `canon lower` for a host function. Note that the host function may
451     /// be either sync or async, and that's handled here too.
452     #[cfg(feature = "component-model-async")]
453     fn call_async_lower(
454         &self,
455         store: StoreContextMut<'_, T>,
456         instance: Instance,
457         ty: TypeFuncIndex,
458         options: OptionsIndex,
459         storage: &mut [MaybeUninit<ValRaw>],
460     ) -> Result<()> {
461         use wasmtime_environ::component::MAX_FLAT_ASYNC_PARAMS;
462 
463         let (component, store) = instance.component_and_store_mut(store.0);
464         let mut store = StoreContextMut(store);
465         let types = component.types();
466         let fty = &types[ty];
467 
468         // Lift the parameters, either from flat storage or from linear
469         // memory.
470         let mut lift = LiftContext::new(store.0.store_opaque_mut(), options, instance);
471         let caller_instance = lift.options().instance;
472         let (params, rest) = self.load_params(&mut lift, ty, MAX_FLAT_ASYNC_PARAMS, storage)?;
473 
474         // Load/validate the return pointer, if present.
475         let retptr = if !lift.types[fty.results].types.is_empty() {
476             let mut lower = LowerContext::new(store.as_context_mut(), options, instance);
477             // SAFETY: see `load_params` below about how the return pointer
478             // should be safe to use.
479             let ptr = unsafe { rest[0].assume_init_ref() };
480             let result_tys = &lower.types[fty.results];
481             validate_inbounds_dynamic(&result_tys.abi, lower.as_slice_mut(), ptr)?
482         } else {
483             // If there's no return pointer then `R` should have an
484             // empty flat representation. In this situation pretend the return
485             // pointer was 0 so we have something to shepherd along into the
486             // closure below.
487             0
488         };
489 
490         let host_result = self.run(store.as_context_mut(), params);
491 
492         let task = match host_result {
493             HostResult::Done(result) => {
494                 Self::lower_result_and_exit_call(
495                     &mut LowerContext::new(store, options, instance),
496                     ty,
497                     result?,
498                     Destination::Memory(retptr),
499                 )?;
500                 None
501             }
502             #[cfg(feature = "component-model-async")]
503             HostResult::Future(future) => {
504                 instance.first_poll(store, future, caller_instance, move |store, ret| {
505                     Self::lower_result_and_exit_call(
506                         &mut LowerContext::new(store, options, instance),
507                         ty,
508                         ret,
509                         Destination::Memory(retptr),
510                     )
511                 })?
512             }
513         };
514 
515         storage[0].write(ValRaw::u32(if let Some(task) = task {
516             Status::Started.pack(Some(task))
517         } else {
518             Status::Returned.pack(None)
519         }));
520 
521         Ok(())
522     }
523 
524     /// Loads parameters the wasm arguments `storage`.
525     ///
526     /// This will internally decide the ABI source of the parameters and use
527     /// `storage` appropriately.
528     fn load_params<'a>(
529         &self,
530         lift: &mut LiftContext<'_>,
531         ty: TypeFuncIndex,
532         max_flat_params: usize,
533         storage: &'a [MaybeUninit<ValRaw>],
534     ) -> Result<(P, &'a [MaybeUninit<ValRaw>])> {
535         let fty = &lift.types[ty];
536         let param_tys = &lift.types[fty.params];
537         let param_flat_count = param_tys.abi.flat_count(max_flat_params);
538         lift.enter_call();
539         let src = match param_flat_count {
540             Some(cnt) => {
541                 let params = &storage[..cnt];
542                 // SAFETY: due to the contract of `entrypoint` we are
543                 // guaranteed that all flat parameters are initialized by
544                 // compiled wasm.
545                 Source::Flat(unsafe { mem::transmute::<&[MaybeUninit<ValRaw>], &[ValRaw]>(params) })
546             }
547             None => {
548                 // SAFETY: due to the contract of `entrypoint` we are
549                 // guaranteed that the return pointer is initialized by
550                 // compiled wasm.
551                 let ptr = unsafe { storage[0].assume_init_ref() };
552                 Source::Memory(validate_inbounds_dynamic(
553                     &param_tys.abi,
554                     lift.memory(),
555                     ptr,
556                 )?)
557             }
558         };
559         let params = Self::lift_params(lift, ty, src)?;
560         Ok((params, &storage[param_flat_count.unwrap_or(1)..]))
561     }
562 
563     /// Stores the result `ret` into `dst` which is calculated per the ABI.
564     fn lower_result_and_exit_call(
565         lower: &mut LowerContext<'_, T>,
566         ty: TypeFuncIndex,
567         ret: R,
568         dst: Destination<'_>,
569     ) -> Result<()> {
570         let caller_instance = lower.options().instance;
571         let mut flags = lower.instance_mut().instance_flags(caller_instance);
572         unsafe {
573             flags.set_may_leave(false);
574         }
575         Self::lower_result(lower, ty, ret, dst)?;
576         unsafe {
577             flags.set_may_leave(true);
578         }
579         lower.exit_call()?;
580         Ok(())
581     }
582 }
583 
584 /// Implementation of a "static" host function where the parameters and results
585 /// of a function are known at compile time.
586 #[repr(transparent)]
587 struct StaticHostFn<F, const ASYNC: bool>(F);
588 
589 impl<F, const ASYNC: bool> StaticHostFn<F, ASYNC> {
590     fn new<T, P, R>(func: F) -> Self
591     where
592         T: 'static,
593         P: ComponentNamedList + Lift + 'static,
594         R: ComponentNamedList + Lower + 'static,
595         F: Fn(StoreContextMut<'_, T>, P) -> HostResult<R>,
596     {
597         Self(func)
598     }
599 }
600 
601 impl<T, F, P, R, const ASYNC: bool> HostFn<T, P, R> for StaticHostFn<F, ASYNC>
602 where
603     T: 'static,
604     F: Fn(StoreContextMut<'_, T>, P) -> HostResult<R>,
605     P: ComponentNamedList + Lift + 'static,
606     R: ComponentNamedList + Lower + 'static,
607 {
608     const ASYNC: bool = ASYNC;
609 
610     fn typecheck(ty: TypeFuncIndex, types: &InstanceType<'_>) -> Result<()> {
611         let ty = &types.types[ty];
612         if ASYNC != ty.async_ {
613             bail!("type mismatch with async");
614         }
615         P::typecheck(&InterfaceType::Tuple(ty.params), types)
616             .context("type mismatch with parameters")?;
617         R::typecheck(&InterfaceType::Tuple(ty.results), types)
618             .context("type mismatch with results")?;
619         Ok(())
620     }
621 
622     fn run(&self, store: StoreContextMut<'_, T>, params: P) -> HostResult<R> {
623         (self.0)(store, params)
624     }
625 
626     fn lift_params(cx: &mut LiftContext<'_>, ty: TypeFuncIndex, src: Source<'_>) -> Result<P> {
627         let ty = InterfaceType::Tuple(cx.types[ty].params);
628         match src {
629             Source::Flat(storage) => {
630                 // SAFETY: the contract of `ComponentType` for `P` means that
631                 // it's safe to interpret the parameters `storage` as
632                 // `P::Lower`. The contract of `entrypoint` is that everything
633                 // is initialized correctly internally.
634                 let storage: &P::Lower = unsafe { slice_to_storage(storage) };
635                 P::linear_lift_from_flat(cx, ty, storage)
636             }
637             Source::Memory(offset) => {
638                 P::linear_lift_from_memory(cx, ty, &cx.memory()[offset..][..P::SIZE32])
639             }
640         }
641     }
642 
643     fn lower_result(
644         cx: &mut LowerContext<'_, T>,
645         ty: TypeFuncIndex,
646         ret: R,
647         dst: Destination<'_>,
648     ) -> Result<()> {
649         let fty = &cx.types[ty];
650         let ty = InterfaceType::Tuple(fty.results);
651         match dst {
652             Destination::Flat(storage) => {
653                 // SAFETY: the contract of `ComponentType` for `R` means that
654                 // it's safe to reinterpret `ValRaw` storage to initialize as
655                 // `R::Lower`.
656                 let storage: &mut MaybeUninit<R::Lower> = unsafe { slice_to_storage_mut(storage) };
657                 ret.linear_lower_to_flat(cx, ty, storage)
658             }
659             Destination::Memory(ptr) => ret.linear_lower_to_memory(cx, ty, ptr),
660         }
661     }
662 }
663 
664 /// Implementation of a "dynamic" host function where the number of parameters,
665 /// types of parameters, and result type/presence, are all not known at compile
666 /// time.
667 ///
668 /// This is intended for more-dynamic use cases than `StaticHostFn` above such
669 /// as demos, gluing things together quickly, and `wast` testing.
670 struct DynamicHostFn<F, const ASYNC: bool>(F);
671 
672 impl<F, const ASYNC: bool> DynamicHostFn<F, ASYNC> {
673     fn new<T>(func: F) -> Self
674     where
675         T: 'static,
676         F: Fn(StoreContextMut<'_, T>, ComponentFunc, Vec<Val>, usize) -> HostResult<Vec<Val>>,
677     {
678         Self(func)
679     }
680 }
681 
682 impl<T, F, const ASYNC: bool> HostFn<T, (ComponentFunc, Vec<Val>), Vec<Val>>
683     for DynamicHostFn<F, ASYNC>
684 where
685     T: 'static,
686     F: Fn(StoreContextMut<'_, T>, ComponentFunc, Vec<Val>, usize) -> HostResult<Vec<Val>>,
687 {
688     const ASYNC: bool = ASYNC;
689 
690     /// This function performs dynamic type checks on its parameters and
691     /// results and subsequently does not need to perform up-front type
692     /// checks. However, we _do_ verify async-ness here.
693     fn typecheck(ty: TypeFuncIndex, types: &InstanceType<'_>) -> Result<()> {
694         let ty = &types.types[ty];
695         if ASYNC != ty.async_ {
696             bail!("type mismatch with async");
697         }
698 
699         Ok(())
700     }
701 
702     fn run(
703         &self,
704         store: StoreContextMut<'_, T>,
705         (ty, mut params): (ComponentFunc, Vec<Val>),
706     ) -> HostResult<Vec<Val>> {
707         let offset = params.len();
708         for _ in 0..ty.results().len() {
709             params.push(Val::Bool(false));
710         }
711         (self.0)(store, ty, params, offset)
712     }
713 
714     fn lift_params(
715         cx: &mut LiftContext<'_>,
716         ty: TypeFuncIndex,
717         src: Source<'_>,
718     ) -> Result<(ComponentFunc, Vec<Val>)> {
719         let param_tys = &cx.types[cx.types[ty].params];
720         let mut params = Vec::new();
721         match src {
722             Source::Flat(storage) => {
723                 let mut iter = storage.iter();
724                 for ty in param_tys.types.iter() {
725                     params.push(Val::lift(cx, *ty, &mut iter)?);
726                 }
727                 assert!(iter.next().is_none());
728             }
729             Source::Memory(mut offset) => {
730                 for ty in param_tys.types.iter() {
731                     let abi = cx.types.canonical_abi(ty);
732                     let size = usize::try_from(abi.size32).unwrap();
733                     let memory = &cx.memory()[abi.next_field32_size(&mut offset)..][..size];
734                     params.push(Val::load(cx, *ty, memory)?);
735                 }
736             }
737         }
738 
739         Ok((ComponentFunc::from(ty, &cx.instance_type()), params))
740     }
741 
742     fn lower_result(
743         cx: &mut LowerContext<'_, T>,
744         ty: TypeFuncIndex,
745         result_vals: Vec<Val>,
746         dst: Destination<'_>,
747     ) -> Result<()> {
748         let fty = &cx.types[ty];
749         let param_tys = &cx.types[fty.params];
750         let result_tys = &cx.types[fty.results];
751         let result_vals = &result_vals[param_tys.types.len()..];
752         match dst {
753             Destination::Flat(storage) => {
754                 let mut dst = storage.iter_mut();
755                 for (val, ty) in result_vals.iter().zip(result_tys.types.iter()) {
756                     val.lower(cx, *ty, &mut dst)?;
757                 }
758                 assert!(dst.next().is_none());
759             }
760             Destination::Memory(mut ptr) => {
761                 for (val, ty) in result_vals.iter().zip(result_tys.types.iter()) {
762                     let offset = cx.types.canonical_abi(ty).next_field32_size(&mut ptr);
763                     val.store(cx, *ty, offset)?;
764                 }
765             }
766         }
767         Ok(())
768     }
769 }
770 
771 pub(crate) fn validate_inbounds_dynamic(
772     abi: &CanonicalAbiInfo,
773     memory: &[u8],
774     ptr: &ValRaw,
775 ) -> Result<usize> {
776     // FIXME(#4311): needs memory64 support
777     let ptr = usize::try_from(ptr.get_u32())?;
778     if ptr % usize::try_from(abi.align32)? != 0 {
779         bail!("pointer not aligned");
780     }
781     let end = match ptr.checked_add(usize::try_from(abi.size32).unwrap()) {
782         Some(n) => n,
783         None => bail!("pointer size overflow"),
784     };
785     if end > memory.len() {
786         bail!("pointer out of bounds")
787     }
788     Ok(ptr)
789 }
790