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