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