1 use crate::component::instance::Instance; 2 use crate::component::matching::InstanceType; 3 use crate::component::storage::storage_as_slice; 4 use crate::component::types::Type; 5 use crate::component::values::Val; 6 use crate::prelude::*; 7 use crate::runtime::vm::component::{ComponentInstance, ResourceTables}; 8 use crate::runtime::vm::{Export, ExportFunction}; 9 use crate::store::StoreOpaque; 10 use crate::{AsContext, AsContextMut, StoreContextMut, ValRaw}; 11 use core::mem::{self, MaybeUninit}; 12 use core::ptr::NonNull; 13 use wasmtime_environ::component::{ 14 ExportIndex, InterfaceType, MAX_FLAT_PARAMS, MAX_FLAT_RESULTS, TypeFuncIndex, TypeTuple, 15 }; 16 17 mod host; 18 mod options; 19 mod typed; 20 pub use self::host::*; 21 pub use self::options::*; 22 pub use self::typed::*; 23 24 #[repr(C)] 25 union 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. 39 pub 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. 46 const _: () = { 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 56 impl 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(|| self.instance.id().get(store))); 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 = self.instance.id().get(store.0); 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 = self.instance.id().get(store.0); 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 = self.instance.id().get(store); 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, ¶ms_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 = self.instance.id().get(store.0); 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 mut cx = LowerContext::new(store.as_context_mut(), &options, &types, self.instance); 430 cx.enter_call(); 431 let result = lower( 432 &mut cx, 433 params, 434 InterfaceType::Tuple(types[ty].params), 435 map_maybe_uninit!(space.params), 436 ); 437 flags.set_may_leave(true); 438 result?; 439 440 // This is unsafe as we are providing the guarantee that all the 441 // inputs are valid. The various pointers passed in for the function 442 // are all valid since they're coming from our store, and the 443 // `params_and_results` should have the correct layout for the core 444 // wasm function we're calling. Note that this latter point relies 445 // on the correctness of this module and `ComponentType` 446 // implementations, hence `ComponentType` being an `unsafe` trait. 447 crate::Func::call_unchecked_raw( 448 store, 449 export.func_ref, 450 NonNull::new(core::ptr::slice_from_raw_parts_mut( 451 space.as_mut_ptr().cast(), 452 mem::size_of_val(space) / mem::size_of::<ValRaw>(), 453 )) 454 .unwrap(), 455 )?; 456 457 // Note that `.assume_init_ref()` here is unsafe but we're relying 458 // on the correctness of the structure of `LowerReturn` and the 459 // type-checking performed to acquire the `TypedFunc` to make this 460 // safe. It should be the case that `LowerReturn` is the exact 461 // representation of the return value when interpreted as 462 // `[ValRaw]`, and additionally they should have the correct types 463 // for the function we just called (which filled in the return 464 // values). 465 let ret = map_maybe_uninit!(space.ret).assume_init_ref(); 466 467 // Lift the result into the host while managing post-return state 468 // here as well. 469 // 470 // After a successful lift the return value of the function, which 471 // is currently required to be 0 or 1 values according to the 472 // canonical ABI, is saved within the `Store`'s `FuncData`. This'll 473 // later get used in post-return. 474 flags.set_needs_post_return(true); 475 let val = lift( 476 &mut LiftContext::new(store.0, &options, &types, self.instance), 477 InterfaceType::Tuple(types[ty].results), 478 ret, 479 )?; 480 let ret_slice = storage_as_slice(ret); 481 self.instance.id().get_mut(store.0).post_return_arg_set( 482 self.index, 483 match ret_slice.len() { 484 0 => ValRaw::i32(0), 485 1 => ret_slice[0], 486 _ => unreachable!(), 487 }, 488 ); 489 return Ok(val); 490 } 491 } 492 493 /// Invokes the `post-return` canonical ABI option, if specified, after a 494 /// [`Func::call`] has finished. 495 /// 496 /// This function is a required method call after a [`Func::call`] completes 497 /// successfully. After the embedder has finished processing the return 498 /// value then this function must be invoked. 499 /// 500 /// # Errors 501 /// 502 /// This function will return an error in the case of a WebAssembly trap 503 /// happening during the execution of the `post-return` function, if 504 /// specified. 505 /// 506 /// # Panics 507 /// 508 /// This function will panic if it's not called under the correct 509 /// conditions. This can only be called after a previous invocation of 510 /// [`Func::call`] completes successfully, and this function can only 511 /// be called for the same [`Func`] that was `call`'d. 512 /// 513 /// If this function is called when [`Func::call`] was not previously 514 /// called, then it will panic. If a different [`Func`] for the same 515 /// component instance was invoked then this function will also panic 516 /// because the `post-return` needs to happen for the other function. 517 /// 518 /// Panics if this is called on a function in an asynchronous store. 519 /// This only works with functions defined within a synchronous store. 520 #[inline] 521 pub fn post_return(&self, mut store: impl AsContextMut) -> Result<()> { 522 let store = store.as_context_mut(); 523 assert!( 524 !store.0.async_support(), 525 "must use `post_return_async` when async support is enabled on the config" 526 ); 527 self.post_return_impl(store) 528 } 529 530 /// Exactly like [`Self::post_return`] except for use on async stores. 531 /// 532 /// # Panics 533 /// 534 /// Panics if this is called on a function in a synchronous store. This 535 /// only works with functions defined within an asynchronous store. 536 #[cfg(feature = "async")] 537 pub async fn post_return_async(&self, mut store: impl AsContextMut<Data: Send>) -> Result<()> { 538 let mut store = store.as_context_mut(); 539 assert!( 540 store.0.async_support(), 541 "cannot use `call_async` without enabling async support in the config" 542 ); 543 // Future optimization opportunity: conditionally use a fiber here since 544 // some func's post_return will not need the async context (i.e. end up 545 // calling async host functionality) 546 store.on_fiber(|store| self.post_return_impl(store)).await? 547 } 548 549 fn post_return_impl(&self, mut store: impl AsContextMut) -> Result<()> { 550 let mut store = store.as_context_mut(); 551 let index = self.index; 552 let vminstance = self.instance.id().get(store.0); 553 let (_ty, _def, options) = vminstance.component().export_lifted_function(index); 554 let post_return = options.post_return.map(|i| { 555 let func_ref = vminstance.runtime_post_return(i); 556 ExportFunction { func_ref } 557 }); 558 let mut flags = vminstance.instance_flags(options.instance); 559 let mut instance = self.instance.id().get_mut(store.0); 560 let post_return_arg = instance.as_mut().post_return_arg_take(index); 561 562 unsafe { 563 // First assert that the instance is in a "needs post return" state. 564 // This will ensure that the previous action on the instance was a 565 // function call above. This flag is only set after a component 566 // function returns so this also can't be called (as expected) 567 // during a host import for example. 568 // 569 // Note, though, that this assert is not sufficient because it just 570 // means some function on this instance needs its post-return 571 // called. We need a precise post-return for a particular function 572 // which is the second assert here (the `.expect`). That will assert 573 // that this function itself needs to have its post-return called. 574 // 575 // The theory at least is that these two asserts ensure component 576 // model semantics are upheld where the host properly calls 577 // `post_return` on the right function despite the call being a 578 // separate step in the API. 579 assert!( 580 flags.needs_post_return(), 581 "post_return can only be called after a function has previously been called", 582 ); 583 let post_return_arg = post_return_arg.expect("calling post_return on wrong function"); 584 585 // This is a sanity-check assert which shouldn't ever trip. 586 assert!(!flags.may_enter()); 587 588 // Unset the "needs post return" flag now that post-return is being 589 // processed. This will cause future invocations of this method to 590 // panic, even if the function call below traps. 591 flags.set_needs_post_return(false); 592 593 // If the function actually had a `post-return` configured in its 594 // canonical options that's executed here. 595 // 596 // Note that if this traps (returns an error) this function 597 // intentionally leaves the instance in a "poisoned" state where it 598 // can no longer be entered because `may_enter` is `false`. 599 if let Some(func) = post_return { 600 crate::Func::call_unchecked_raw( 601 &mut store, 602 func.func_ref, 603 NonNull::new(core::ptr::slice_from_raw_parts(&post_return_arg, 1).cast_mut()) 604 .unwrap(), 605 )?; 606 } 607 608 // And finally if everything completed successfully then the "may 609 // enter" flag is set to `true` again here which enables further use 610 // of the component. 611 flags.set_may_enter(true); 612 613 let (calls, host_table, _, instance) = store 614 .0 615 .component_resource_state_with_instance(self.instance); 616 ResourceTables { 617 host_table: Some(host_table), 618 calls, 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 } 673