1 use crate::prelude::*; 2 use crate::runtime::vm::{ 3 self, InterpreterRef, SendSyncPtr, StoreBox, VMArrayCallHostFuncContext, 4 VMCommonStackInformation, VMContext, VMFuncRef, VMFunctionImport, VMOpaqueContext, 5 VMStoreContext, 6 }; 7 use crate::store::{AutoAssertNoGc, InstanceId, StoreId, StoreOpaque}; 8 use crate::type_registry::RegisteredType; 9 use crate::{ 10 AsContext, AsContextMut, CallHook, Engine, Extern, FuncType, Instance, ModuleExport, Ref, 11 StoreContext, StoreContextMut, Val, ValRaw, ValType, 12 }; 13 use alloc::sync::Arc; 14 use core::convert::Infallible; 15 use core::ffi::c_void; 16 #[cfg(feature = "async")] 17 use core::future::Future; 18 use core::mem::{self, MaybeUninit}; 19 use core::ptr::NonNull; 20 use wasmtime_environ::VMSharedTypeIndex; 21 22 /// A reference to the abstract `nofunc` heap value. 23 /// 24 /// The are no instances of `(ref nofunc)`: it is an uninhabited type. 25 /// 26 /// There is precisely one instance of `(ref null nofunc)`, aka `nullfuncref`: 27 /// the null reference. 28 /// 29 /// This `NoFunc` Rust type's sole purpose is for use with [`Func::wrap`]- and 30 /// [`Func::typed`]-style APIs for statically typing a function as taking or 31 /// returning a `(ref null nofunc)` (aka `Option<NoFunc>`) which is always 32 /// `None`. 33 /// 34 /// # Example 35 /// 36 /// ``` 37 /// # use wasmtime::*; 38 /// # fn _foo() -> Result<()> { 39 /// let mut config = Config::new(); 40 /// config.wasm_function_references(true); 41 /// let engine = Engine::new(&config)?; 42 /// 43 /// let module = Module::new( 44 /// &engine, 45 /// r#" 46 /// (module 47 /// (func (export "f") (param (ref null nofunc)) 48 /// ;; If the reference is null, return. 49 /// local.get 0 50 /// ref.is_null nofunc 51 /// br_if 0 52 /// 53 /// ;; If the reference was not null (which is impossible) 54 /// ;; then raise a trap. 55 /// unreachable 56 /// ) 57 /// ) 58 /// "#, 59 /// )?; 60 /// 61 /// let mut store = Store::new(&engine, ()); 62 /// let instance = Instance::new(&mut store, &module, &[])?; 63 /// let f = instance.get_func(&mut store, "f").unwrap(); 64 /// 65 /// // We can cast a `(ref null nofunc)`-taking function into a typed function that 66 /// // takes an `Option<NoFunc>` via the `Func::typed` method. 67 /// let f = f.typed::<Option<NoFunc>, ()>(&store)?; 68 /// 69 /// // We can call the typed function, passing the null `nofunc` reference. 70 /// let result = f.call(&mut store, NoFunc::null()); 71 /// 72 /// // The function should not have trapped, because the reference we gave it was 73 /// // null (as it had to be, since `NoFunc` is uninhabited). 74 /// assert!(result.is_ok()); 75 /// # Ok(()) 76 /// # } 77 /// ``` 78 #[derive(Copy, Clone, Debug, PartialEq, Eq)] 79 pub struct NoFunc { 80 _inner: Infallible, 81 } 82 83 impl NoFunc { 84 /// Get the null `(ref null nofunc)` (aka `nullfuncref`) reference. 85 #[inline] 86 pub fn null() -> Option<NoFunc> { 87 None 88 } 89 90 /// Get the null `(ref null nofunc)` (aka `nullfuncref`) reference as a 91 /// [`Ref`]. 92 #[inline] 93 pub fn null_ref() -> Ref { 94 Ref::Func(None) 95 } 96 97 /// Get the null `(ref null nofunc)` (aka `nullfuncref`) reference as a 98 /// [`Val`]. 99 #[inline] 100 pub fn null_val() -> Val { 101 Val::FuncRef(None) 102 } 103 } 104 105 /// A WebAssembly function which can be called. 106 /// 107 /// This type typically represents an exported function from a WebAssembly 108 /// module instance. In this case a [`Func`] belongs to an [`Instance`] and is 109 /// loaded from there. A [`Func`] may also represent a host function as well in 110 /// some cases, too. 111 /// 112 /// Functions can be called in a few different ways, either synchronous or async 113 /// and either typed or untyped (more on this below). Note that host functions 114 /// are normally inserted directly into a [`Linker`](crate::Linker) rather than 115 /// using this directly, but both options are available. 116 /// 117 /// # `Func` and `async` 118 /// 119 /// Functions from the perspective of WebAssembly are always synchronous. You 120 /// might have an `async` function in Rust, however, which you'd like to make 121 /// available from WebAssembly. Wasmtime supports asynchronously calling 122 /// WebAssembly through native stack switching. You can get some more 123 /// information about [asynchronous configs](crate::Config::async_support), but 124 /// from the perspective of `Func` it's important to know that whether or not 125 /// your [`Store`](crate::Store) is asynchronous will dictate whether you call 126 /// functions through [`Func::call`] or [`Func::call_async`] (or the typed 127 /// wrappers such as [`TypedFunc::call`] vs [`TypedFunc::call_async`]). 128 /// 129 /// # To `Func::call` or to `Func::typed().call()` 130 /// 131 /// There's a 2x2 matrix of methods to call [`Func`]. Invocations can either be 132 /// asynchronous or synchronous. They can also be statically typed or not. 133 /// Whether or not an invocation is asynchronous is indicated via the method 134 /// being `async` and [`call_async`](Func::call_async) being the entry point. 135 /// Otherwise for statically typed or not your options are: 136 /// 137 /// * Dynamically typed - if you don't statically know the signature of the 138 /// function that you're calling you'll be using [`Func::call`] or 139 /// [`Func::call_async`]. These functions take a variable-length slice of 140 /// "boxed" arguments in their [`Val`] representation. Additionally the 141 /// results are returned as an owned slice of [`Val`]. These methods are not 142 /// optimized due to the dynamic type checks that must occur, in addition to 143 /// some dynamic allocations for where to put all the arguments. While this 144 /// allows you to call all possible wasm function signatures, if you're 145 /// looking for a speedier alternative you can also use... 146 /// 147 /// * Statically typed - if you statically know the type signature of the wasm 148 /// function you're calling, then you'll want to use the [`Func::typed`] 149 /// method to acquire an instance of [`TypedFunc`]. This structure is static proof 150 /// that the underlying wasm function has the ascripted type, and type 151 /// validation is only done once up-front. The [`TypedFunc::call`] and 152 /// [`TypedFunc::call_async`] methods are much more efficient than [`Func::call`] 153 /// and [`Func::call_async`] because the type signature is statically known. 154 /// This eschews runtime checks as much as possible to get into wasm as fast 155 /// as possible. 156 /// 157 /// # Examples 158 /// 159 /// One way to get a `Func` is from an [`Instance`] after you've instantiated 160 /// it: 161 /// 162 /// ``` 163 /// # use wasmtime::*; 164 /// # fn main() -> Result<()> { 165 /// let engine = Engine::default(); 166 /// let module = Module::new(&engine, r#"(module (func (export "foo")))"#)?; 167 /// let mut store = Store::new(&engine, ()); 168 /// let instance = Instance::new(&mut store, &module, &[])?; 169 /// let foo = instance.get_func(&mut store, "foo").expect("export wasn't a function"); 170 /// 171 /// // Work with `foo` as a `Func` at this point, such as calling it 172 /// // dynamically... 173 /// match foo.call(&mut store, &[], &mut []) { 174 /// Ok(()) => { /* ... */ } 175 /// Err(trap) => { 176 /// panic!("execution of `foo` resulted in a wasm trap: {}", trap); 177 /// } 178 /// } 179 /// foo.call(&mut store, &[], &mut [])?; 180 /// 181 /// // ... or we can make a static assertion about its signature and call it. 182 /// // Our first call here can fail if the signatures don't match, and then the 183 /// // second call can fail if the function traps (like the `match` above). 184 /// let foo = foo.typed::<(), ()>(&store)?; 185 /// foo.call(&mut store, ())?; 186 /// # Ok(()) 187 /// # } 188 /// ``` 189 /// 190 /// You can also use the [`wrap` function](Func::wrap) to create a 191 /// `Func` 192 /// 193 /// ``` 194 /// # use wasmtime::*; 195 /// # fn main() -> Result<()> { 196 /// let mut store = Store::<()>::default(); 197 /// 198 /// // Create a custom `Func` which can execute arbitrary code inside of the 199 /// // closure. 200 /// let add = Func::wrap(&mut store, |a: i32, b: i32| -> i32 { a + b }); 201 /// 202 /// // Next we can hook that up to a wasm module which uses it. 203 /// let module = Module::new( 204 /// store.engine(), 205 /// r#" 206 /// (module 207 /// (import "" "" (func $add (param i32 i32) (result i32))) 208 /// (func (export "call_add_twice") (result i32) 209 /// i32.const 1 210 /// i32.const 2 211 /// call $add 212 /// i32.const 3 213 /// i32.const 4 214 /// call $add 215 /// i32.add)) 216 /// "#, 217 /// )?; 218 /// let instance = Instance::new(&mut store, &module, &[add.into()])?; 219 /// let call_add_twice = instance.get_typed_func::<(), i32>(&mut store, "call_add_twice")?; 220 /// 221 /// assert_eq!(call_add_twice.call(&mut store, ())?, 10); 222 /// # Ok(()) 223 /// # } 224 /// ``` 225 /// 226 /// Or you could also create an entirely dynamic `Func`! 227 /// 228 /// ``` 229 /// # use wasmtime::*; 230 /// # fn main() -> Result<()> { 231 /// let mut store = Store::<()>::default(); 232 /// 233 /// // Here we need to define the type signature of our `Double` function and 234 /// // then wrap it up in a `Func` 235 /// let double_type = wasmtime::FuncType::new( 236 /// store.engine(), 237 /// [wasmtime::ValType::I32].iter().cloned(), 238 /// [wasmtime::ValType::I32].iter().cloned(), 239 /// ); 240 /// let double = Func::new(&mut store, double_type, |_, params, results| { 241 /// let mut value = params[0].unwrap_i32(); 242 /// value *= 2; 243 /// results[0] = value.into(); 244 /// Ok(()) 245 /// }); 246 /// 247 /// let module = Module::new( 248 /// store.engine(), 249 /// r#" 250 /// (module 251 /// (import "" "" (func $double (param i32) (result i32))) 252 /// (func $start 253 /// i32.const 1 254 /// call $double 255 /// drop) 256 /// (start $start)) 257 /// "#, 258 /// )?; 259 /// let instance = Instance::new(&mut store, &module, &[double.into()])?; 260 /// // .. work with `instance` if necessary 261 /// # Ok(()) 262 /// # } 263 /// ``` 264 #[derive(Copy, Clone, Debug)] 265 #[repr(C)] // here for the C API 266 pub struct Func { 267 /// The store that the below pointer belongs to. 268 /// 269 /// It's only safe to look at the contents of the pointer below when the 270 /// `StoreOpaque` matching this id is in-scope. 271 store: StoreId, 272 273 /// The raw `VMFuncRef`, whose lifetime is bound to the store this func 274 /// belongs to. 275 /// 276 /// Note that this field has an `unsafe_*` prefix to discourage use of it. 277 /// This is only safe to read/use if `self.store` is validated to belong to 278 /// an ambiently provided `StoreOpaque` or similar. Use the 279 /// `self.func_ref()` method instead of this field to perform this check. 280 unsafe_func_ref: SendSyncPtr<VMFuncRef>, 281 } 282 283 // Double-check that the C representation in `extern.h` matches our in-Rust 284 // representation here in terms of size/alignment/etc. 285 const _: () = { 286 #[repr(C)] 287 struct C(u64, *mut u8); 288 assert!(core::mem::size_of::<C>() == core::mem::size_of::<Func>()); 289 assert!(core::mem::align_of::<C>() == core::mem::align_of::<Func>()); 290 assert!(core::mem::offset_of!(Func, store) == 0); 291 }; 292 293 macro_rules! for_each_function_signature { 294 ($mac:ident) => { 295 $mac!(0); 296 $mac!(1 A1); 297 $mac!(2 A1 A2); 298 $mac!(3 A1 A2 A3); 299 $mac!(4 A1 A2 A3 A4); 300 $mac!(5 A1 A2 A3 A4 A5); 301 $mac!(6 A1 A2 A3 A4 A5 A6); 302 $mac!(7 A1 A2 A3 A4 A5 A6 A7); 303 $mac!(8 A1 A2 A3 A4 A5 A6 A7 A8); 304 $mac!(9 A1 A2 A3 A4 A5 A6 A7 A8 A9); 305 $mac!(10 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10); 306 $mac!(11 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11); 307 $mac!(12 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12); 308 $mac!(13 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13); 309 $mac!(14 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13 A14); 310 $mac!(15 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13 A14 A15); 311 $mac!(16 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13 A14 A15 A16); 312 $mac!(17 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13 A14 A15 A16 A17); 313 }; 314 } 315 316 mod typed; 317 use crate::runtime::vm::VMStackChain; 318 pub use typed::*; 319 320 impl Func { 321 /// Creates a new `Func` with the given arguments, typically to create a 322 /// host-defined function to pass as an import to a module. 323 /// 324 /// * `store` - the store in which to create this [`Func`], which will own 325 /// the return value. 326 /// 327 /// * `ty` - the signature of this function, used to indicate what the 328 /// inputs and outputs are. 329 /// 330 /// * `func` - the native code invoked whenever this `Func` will be called. 331 /// This closure is provided a [`Caller`] as its first argument to learn 332 /// information about the caller, and then it's passed a list of 333 /// parameters as a slice along with a mutable slice of where to write 334 /// results. 335 /// 336 /// Note that the implementation of `func` must adhere to the `ty` signature 337 /// given, error or traps may occur if it does not respect the `ty` 338 /// signature. For example if the function type declares that it returns one 339 /// i32 but the `func` closures does not write anything into the results 340 /// slice then a trap may be generated. 341 /// 342 /// Additionally note that this is quite a dynamic function since signatures 343 /// are not statically known. For a more performant and ergonomic `Func` 344 /// it's recommended to use [`Func::wrap`] if you can because with 345 /// statically known signatures Wasmtime can optimize the implementation 346 /// much more. 347 /// 348 /// For more information about `Send + Sync + 'static` requirements on the 349 /// `func`, see [`Func::wrap`](#why-send--sync--static). 350 /// 351 /// # Errors 352 /// 353 /// The host-provided function here returns a 354 /// [`Result<()>`](crate::Result). If the function returns `Ok(())` then 355 /// that indicates that the host function completed successfully and wrote 356 /// the result into the `&mut [Val]` argument. 357 /// 358 /// If the function returns `Err(e)`, however, then this is equivalent to 359 /// the host function triggering a trap for wasm. WebAssembly execution is 360 /// immediately halted and the original caller of [`Func::call`], for 361 /// example, will receive the error returned here (possibly with 362 /// [`WasmBacktrace`](crate::WasmBacktrace) context information attached). 363 /// 364 /// For more information about errors in Wasmtime see the [`Trap`] 365 /// documentation. 366 /// 367 /// [`Trap`]: crate::Trap 368 /// 369 /// # Panics 370 /// 371 /// Panics if the given function type is not associated with this store's 372 /// engine. 373 pub fn new<T: 'static>( 374 mut store: impl AsContextMut<Data = T>, 375 ty: FuncType, 376 func: impl Fn(Caller<'_, T>, &[Val], &mut [Val]) -> Result<()> + Send + Sync + 'static, 377 ) -> Self { 378 let store = store.as_context_mut().0; 379 let host = HostFunc::new(store.engine(), ty, func); 380 381 // SAFETY: the `T` used by `func` matches the `T` of the store we're 382 // inserting into via this function's type signature. 383 unsafe { host.into_func(store) } 384 } 385 386 /// Creates a new [`Func`] with the given arguments, although has fewer 387 /// runtime checks than [`Func::new`]. 388 /// 389 /// This function takes a callback of a different signature than 390 /// [`Func::new`], instead receiving a raw pointer with a list of [`ValRaw`] 391 /// structures. These values have no type information associated with them 392 /// so it's up to the caller to provide a function that will correctly 393 /// interpret the list of values as those coming from the `ty` specified. 394 /// 395 /// If you're calling this from Rust it's recommended to either instead use 396 /// [`Func::new`] or [`Func::wrap`]. The [`Func::wrap`] API, in particular, 397 /// is both safer and faster than this API. 398 /// 399 /// # Errors 400 /// 401 /// See [`Func::new`] for the behavior of returning an error from the host 402 /// function provided here. 403 /// 404 /// # Unsafety 405 /// 406 /// This function is not safe because it's not known at compile time that 407 /// the `func` provided correctly interprets the argument types provided to 408 /// it, or that the results it produces will be of the correct type. 409 /// 410 /// # Panics 411 /// 412 /// Panics if the given function type is not associated with this store's 413 /// engine. 414 pub unsafe fn new_unchecked<T: 'static>( 415 mut store: impl AsContextMut<Data = T>, 416 ty: FuncType, 417 func: impl Fn(Caller<'_, T>, &mut [MaybeUninit<ValRaw>]) -> Result<()> + Send + Sync + 'static, 418 ) -> Self { 419 let store = store.as_context_mut().0; 420 421 // SAFETY: the contract required by `new_unchecked` is the same as the 422 // contract required by this function itself. 423 let host = unsafe { HostFunc::new_unchecked(store.engine(), ty, func) }; 424 425 // SAFETY: the `T` used by `func` matches the `T` of the store we're 426 // inserting into via this function's type signature. 427 unsafe { host.into_func(store) } 428 } 429 430 /// Creates a new host-defined WebAssembly function which, when called, 431 /// will run the asynchronous computation defined by `func` to completion 432 /// and then return the result to WebAssembly. 433 /// 434 /// This function is the asynchronous analogue of [`Func::new`] and much of 435 /// that documentation applies to this as well. The key difference is that 436 /// `func` returns a future instead of simply a `Result`. Note that the 437 /// returned future can close over any of the arguments, but it cannot close 438 /// over the state of the closure itself. It's recommended to store any 439 /// necessary async state in the `T` of the [`Store<T>`](crate::Store) which 440 /// can be accessed through [`Caller::data`] or [`Caller::data_mut`]. 441 /// 442 /// For more information on `Send + Sync + 'static`, see 443 /// [`Func::wrap`](#why-send--sync--static). 444 /// 445 /// # Panics 446 /// 447 /// This function will panic if `store` is not associated with an [async 448 /// config](crate::Config::async_support). 449 /// 450 /// Panics if the given function type is not associated with this store's 451 /// engine. 452 /// 453 /// # Errors 454 /// 455 /// See [`Func::new`] for the behavior of returning an error from the host 456 /// function provided here. 457 /// 458 /// # Examples 459 /// 460 /// ``` 461 /// # use wasmtime::*; 462 /// # fn main() -> Result<()> { 463 /// // Simulate some application-specific state as well as asynchronous 464 /// // functions to query that state. 465 /// struct MyDatabase { 466 /// // ... 467 /// } 468 /// 469 /// impl MyDatabase { 470 /// async fn get_row_count(&self) -> u32 { 471 /// // ... 472 /// # 100 473 /// } 474 /// } 475 /// 476 /// let my_database = MyDatabase { 477 /// // ... 478 /// }; 479 /// 480 /// // Using `new_async` we can hook up into calling our async 481 /// // `get_row_count` function. 482 /// let engine = Engine::new(Config::new().async_support(true))?; 483 /// let mut store = Store::new(&engine, MyDatabase { 484 /// // ... 485 /// }); 486 /// let get_row_count_type = wasmtime::FuncType::new( 487 /// &engine, 488 /// None, 489 /// Some(wasmtime::ValType::I32), 490 /// ); 491 /// let get = Func::new_async(&mut store, get_row_count_type, |caller, _params, results| { 492 /// Box::new(async move { 493 /// let count = caller.data().get_row_count().await; 494 /// results[0] = Val::I32(count as i32); 495 /// Ok(()) 496 /// }) 497 /// }); 498 /// // ... 499 /// # Ok(()) 500 /// # } 501 /// ``` 502 #[cfg(feature = "async")] 503 pub fn new_async<T, F>(mut store: impl AsContextMut<Data = T>, ty: FuncType, func: F) -> Func 504 where 505 F: for<'a> Fn( 506 Caller<'a, T>, 507 &'a [Val], 508 &'a mut [Val], 509 ) -> Box<dyn Future<Output = Result<()>> + Send + 'a> 510 + Send 511 + Sync 512 + 'static, 513 T: Send + 'static, 514 { 515 let store = store.as_context_mut().0; 516 assert!( 517 store.async_support(), 518 "cannot use `new_async` without enabling async support in the config" 519 ); 520 let host = HostFunc::new_async(store.engine(), ty, func); 521 522 // SAFETY: the `T` used by `func` matches the `T` of the store we're 523 // inserting into via this function's type signature. 524 unsafe { host.into_func(store) } 525 } 526 527 /// Creates a new `Func` from a store and a funcref within that store. 528 /// 529 /// # Safety 530 /// 531 /// The safety of this function requires that `func_ref` is a valid function 532 /// pointer owned by `store`. 533 pub(crate) unsafe fn from_vm_func_ref(store: StoreId, func_ref: NonNull<VMFuncRef>) -> Func { 534 // SAFETY: given the contract of this function it's safe to read the 535 // `type_index` field. 536 unsafe { 537 debug_assert!(func_ref.as_ref().type_index != VMSharedTypeIndex::default()); 538 } 539 Func { 540 store, 541 unsafe_func_ref: func_ref.into(), 542 } 543 } 544 545 /// Creates a new `Func` from the given Rust closure. 546 /// 547 /// This function will create a new `Func` which, when called, will 548 /// execute the given Rust closure. Unlike [`Func::new`] the target 549 /// function being called is known statically so the type signature can 550 /// be inferred. Rust types will map to WebAssembly types as follows: 551 /// 552 /// | Rust Argument Type | WebAssembly Type | 553 /// |-----------------------------------|-------------------------------------------| 554 /// | `i32` | `i32` | 555 /// | `u32` | `i32` | 556 /// | `i64` | `i64` | 557 /// | `u64` | `i64` | 558 /// | `f32` | `f32` | 559 /// | `f64` | `f64` | 560 /// | `V128` on x86-64 and aarch64 only | `v128` | 561 /// | `Option<Func>` | `funcref` aka `(ref null func)` | 562 /// | `Func` | `(ref func)` | 563 /// | `Option<Nofunc>` | `nullfuncref` aka `(ref null nofunc)` | 564 /// | `NoFunc` | `(ref nofunc)` | 565 /// | `Option<Rooted<ExternRef>>` | `externref` aka `(ref null extern)` | 566 /// | `Rooted<ExternRef>` | `(ref extern)` | 567 /// | `Option<NoExtern>` | `nullexternref` aka `(ref null noextern)` | 568 /// | `NoExtern` | `(ref noextern)` | 569 /// | `Option<Rooted<AnyRef>>` | `anyref` aka `(ref null any)` | 570 /// | `Rooted<AnyRef>` | `(ref any)` | 571 /// | `Option<Rooted<EqRef>>` | `eqref` aka `(ref null eq)` | 572 /// | `Rooted<EqRef>` | `(ref eq)` | 573 /// | `Option<I31>` | `i31ref` aka `(ref null i31)` | 574 /// | `I31` | `(ref i31)` | 575 /// | `Option<Rooted<StructRef>>` | `(ref null struct)` | 576 /// | `Rooted<StructRef>` | `(ref struct)` | 577 /// | `Option<Rooted<ArrayRef>>` | `(ref null array)` | 578 /// | `Rooted<ArrayRef>` | `(ref array)` | 579 /// | `Option<NoneRef>` | `nullref` aka `(ref null none)` | 580 /// | `NoneRef` | `(ref none)` | 581 /// 582 /// Note that anywhere a `Rooted<T>` appears, a `OwnedRooted<T>` may also 583 /// be used. 584 /// 585 /// Any of the Rust types can be returned from the closure as well, in 586 /// addition to some extra types 587 /// 588 /// | Rust Return Type | WebAssembly Return Type | Meaning | 589 /// |-------------------|-------------------------|-----------------------| 590 /// | `()` | nothing | no return value | 591 /// | `T` | `T` | a single return value | 592 /// | `(T1, T2, ...)` | `T1 T2 ...` | multiple returns | 593 /// 594 /// Note that all return types can also be wrapped in `Result<_>` to 595 /// indicate that the host function can generate a trap as well as possibly 596 /// returning a value. 597 /// 598 /// Finally you can also optionally take [`Caller`] as the first argument of 599 /// your closure. If inserted then you're able to inspect the caller's 600 /// state, for example the [`Memory`](crate::Memory) it has exported so you 601 /// can read what pointers point to. 602 /// 603 /// Note that when using this API, the intention is to create as thin of a 604 /// layer as possible for when WebAssembly calls the function provided. With 605 /// sufficient inlining and optimization the WebAssembly will call straight 606 /// into `func` provided, with no extra fluff entailed. 607 /// 608 /// # Why `Send + Sync + 'static`? 609 /// 610 /// All host functions defined in a [`Store`](crate::Store) (including 611 /// those from [`Func::new`] and other constructors) require that the 612 /// `func` provided is `Send + Sync + 'static`. Additionally host functions 613 /// always are `Fn` as opposed to `FnMut` or `FnOnce`. This can at-a-glance 614 /// feel restrictive since the closure cannot close over as many types as 615 /// before. The reason for this, though, is to ensure that 616 /// [`Store<T>`](crate::Store) can implement both the `Send` and `Sync` 617 /// traits. 618 /// 619 /// Fear not, however, because this isn't as restrictive as it seems! Host 620 /// functions are provided a [`Caller<'_, T>`](crate::Caller) argument which 621 /// allows access to the host-defined data within the 622 /// [`Store`](crate::Store). The `T` type is not required to be any of 623 /// `Send`, `Sync`, or `'static`! This means that you can store whatever 624 /// you'd like in `T` and have it accessible by all host functions. 625 /// Additionally mutable access to `T` is allowed through 626 /// [`Caller::data_mut`]. 627 /// 628 /// Most host-defined [`Func`] values provide closures that end up not 629 /// actually closing over any values. These zero-sized types will use the 630 /// context from [`Caller`] for host-defined information. 631 /// 632 /// # Errors 633 /// 634 /// The closure provided here to `wrap` can optionally return a 635 /// [`Result<T>`](crate::Result). Returning `Ok(t)` represents the host 636 /// function successfully completing with the `t` result. Returning 637 /// `Err(e)`, however, is equivalent to raising a custom wasm trap. 638 /// Execution of WebAssembly does not resume and the stack is unwound to the 639 /// original caller of the function where the error is returned. 640 /// 641 /// For more information about errors in Wasmtime see the [`Trap`] 642 /// documentation. 643 /// 644 /// [`Trap`]: crate::Trap 645 /// 646 /// # Examples 647 /// 648 /// First up we can see how simple wasm imports can be implemented, such 649 /// as a function that adds its two arguments and returns the result. 650 /// 651 /// ``` 652 /// # use wasmtime::*; 653 /// # fn main() -> Result<()> { 654 /// # let mut store = Store::<()>::default(); 655 /// let add = Func::wrap(&mut store, |a: i32, b: i32| a + b); 656 /// let module = Module::new( 657 /// store.engine(), 658 /// r#" 659 /// (module 660 /// (import "" "" (func $add (param i32 i32) (result i32))) 661 /// (func (export "foo") (param i32 i32) (result i32) 662 /// local.get 0 663 /// local.get 1 664 /// call $add)) 665 /// "#, 666 /// )?; 667 /// let instance = Instance::new(&mut store, &module, &[add.into()])?; 668 /// let foo = instance.get_typed_func::<(i32, i32), i32>(&mut store, "foo")?; 669 /// assert_eq!(foo.call(&mut store, (1, 2))?, 3); 670 /// # Ok(()) 671 /// # } 672 /// ``` 673 /// 674 /// We can also do the same thing, but generate a trap if the addition 675 /// overflows: 676 /// 677 /// ``` 678 /// # use wasmtime::*; 679 /// # fn main() -> Result<()> { 680 /// # let mut store = Store::<()>::default(); 681 /// let add = Func::wrap(&mut store, |a: i32, b: i32| { 682 /// match a.checked_add(b) { 683 /// Some(i) => Ok(i), 684 /// None => bail!("overflow"), 685 /// } 686 /// }); 687 /// let module = Module::new( 688 /// store.engine(), 689 /// r#" 690 /// (module 691 /// (import "" "" (func $add (param i32 i32) (result i32))) 692 /// (func (export "foo") (param i32 i32) (result i32) 693 /// local.get 0 694 /// local.get 1 695 /// call $add)) 696 /// "#, 697 /// )?; 698 /// let instance = Instance::new(&mut store, &module, &[add.into()])?; 699 /// let foo = instance.get_typed_func::<(i32, i32), i32>(&mut store, "foo")?; 700 /// assert_eq!(foo.call(&mut store, (1, 2))?, 3); 701 /// assert!(foo.call(&mut store, (i32::max_value(), 1)).is_err()); 702 /// # Ok(()) 703 /// # } 704 /// ``` 705 /// 706 /// And don't forget all the wasm types are supported! 707 /// 708 /// ``` 709 /// # use wasmtime::*; 710 /// # fn main() -> Result<()> { 711 /// # let mut store = Store::<()>::default(); 712 /// let debug = Func::wrap(&mut store, |a: i32, b: u32, c: f32, d: i64, e: u64, f: f64| { 713 /// 714 /// println!("a={}", a); 715 /// println!("b={}", b); 716 /// println!("c={}", c); 717 /// println!("d={}", d); 718 /// println!("e={}", e); 719 /// println!("f={}", f); 720 /// }); 721 /// let module = Module::new( 722 /// store.engine(), 723 /// r#" 724 /// (module 725 /// (import "" "" (func $debug (param i32 i32 f32 i64 i64 f64))) 726 /// (func (export "foo") 727 /// i32.const -1 728 /// i32.const 1 729 /// f32.const 2 730 /// i64.const -3 731 /// i64.const 3 732 /// f64.const 4 733 /// call $debug)) 734 /// "#, 735 /// )?; 736 /// let instance = Instance::new(&mut store, &module, &[debug.into()])?; 737 /// let foo = instance.get_typed_func::<(), ()>(&mut store, "foo")?; 738 /// foo.call(&mut store, ())?; 739 /// # Ok(()) 740 /// # } 741 /// ``` 742 /// 743 /// Finally if you want to get really fancy you can also implement 744 /// imports that read/write wasm module's memory 745 /// 746 /// ``` 747 /// use std::str; 748 /// 749 /// # use wasmtime::*; 750 /// # fn main() -> Result<()> { 751 /// # let mut store = Store::default(); 752 /// let log_str = Func::wrap(&mut store, |mut caller: Caller<'_, ()>, ptr: i32, len: i32| { 753 /// let mem = match caller.get_export("memory") { 754 /// Some(Extern::Memory(mem)) => mem, 755 /// _ => bail!("failed to find host memory"), 756 /// }; 757 /// let data = mem.data(&caller) 758 /// .get(ptr as u32 as usize..) 759 /// .and_then(|arr| arr.get(..len as u32 as usize)); 760 /// let string = match data { 761 /// Some(data) => match str::from_utf8(data) { 762 /// Ok(s) => s, 763 /// Err(_) => bail!("invalid utf-8"), 764 /// }, 765 /// None => bail!("pointer/length out of bounds"), 766 /// }; 767 /// assert_eq!(string, "Hello, world!"); 768 /// println!("{}", string); 769 /// Ok(()) 770 /// }); 771 /// let module = Module::new( 772 /// store.engine(), 773 /// r#" 774 /// (module 775 /// (import "" "" (func $log_str (param i32 i32))) 776 /// (func (export "foo") 777 /// i32.const 4 ;; ptr 778 /// i32.const 13 ;; len 779 /// call $log_str) 780 /// (memory (export "memory") 1) 781 /// (data (i32.const 4) "Hello, world!")) 782 /// "#, 783 /// )?; 784 /// let instance = Instance::new(&mut store, &module, &[log_str.into()])?; 785 /// let foo = instance.get_typed_func::<(), ()>(&mut store, "foo")?; 786 /// foo.call(&mut store, ())?; 787 /// # Ok(()) 788 /// # } 789 /// ``` 790 pub fn wrap<T, Params, Results>( 791 mut store: impl AsContextMut<Data = T>, 792 func: impl IntoFunc<T, Params, Results>, 793 ) -> Func 794 where 795 T: 'static, 796 { 797 let store = store.as_context_mut().0; 798 let engine = store.engine(); 799 let host = func.into_func(engine); 800 801 // SAFETY: The `T` the closure takes is the same as the `T` of the store 802 // we're inserting into via the type signature above. 803 unsafe { host.into_func(store) } 804 } 805 806 /// Same as [`Func::wrap`], except the closure asynchronously produces the 807 /// result and the arguments are passed within a tuple. For more information 808 /// see the [`Func`] documentation. 809 /// 810 /// # Panics 811 /// 812 /// This function will panic if called with a non-asynchronous store. 813 #[cfg(feature = "async")] 814 pub fn wrap_async<T, F, P, R>(mut store: impl AsContextMut<Data = T>, func: F) -> Func 815 where 816 F: for<'a> Fn(Caller<'a, T>, P) -> Box<dyn Future<Output = R> + Send + 'a> 817 + Send 818 + Sync 819 + 'static, 820 P: WasmTyList, 821 R: WasmRet, 822 T: Send + 'static, 823 { 824 let store = store.as_context_mut().0; 825 assert!( 826 store.async_support(), 827 concat!("cannot use `wrap_async` without enabling async support on the config") 828 ); 829 let host = HostFunc::wrap_async(store.engine(), func); 830 831 // SAFETY: The `T` the closure takes is the same as the `T` of the store 832 // we're inserting into via the type signature above. 833 unsafe { host.into_func(store) } 834 } 835 836 /// Returns the underlying wasm type that this `Func` has. 837 /// 838 /// # Panics 839 /// 840 /// Panics if `store` does not own this function. 841 pub fn ty(&self, store: impl AsContext) -> FuncType { 842 self.load_ty(&store.as_context().0) 843 } 844 845 /// Forcibly loads the type of this function from the `Engine`. 846 /// 847 /// Note that this is a somewhat expensive method since it requires taking a 848 /// lock as well as cloning a type. 849 pub(crate) fn load_ty(&self, store: &StoreOpaque) -> FuncType { 850 FuncType::from_shared_type_index(store.engine(), self.type_index(store)) 851 } 852 853 /// Does this function match the given type? 854 /// 855 /// That is, is this function's type a subtype of the given type? 856 /// 857 /// # Panics 858 /// 859 /// Panics if this function is not associated with the given store or if the 860 /// function type is not associated with the store's engine. 861 pub fn matches_ty(&self, store: impl AsContext, func_ty: &FuncType) -> bool { 862 self._matches_ty(store.as_context().0, func_ty) 863 } 864 865 pub(crate) fn _matches_ty(&self, store: &StoreOpaque, func_ty: &FuncType) -> bool { 866 let actual_ty = self.load_ty(store); 867 actual_ty.matches(func_ty) 868 } 869 870 pub(crate) fn ensure_matches_ty(&self, store: &StoreOpaque, func_ty: &FuncType) -> Result<()> { 871 if !self.comes_from_same_store(store) { 872 bail!("function used with wrong store"); 873 } 874 if self._matches_ty(store, func_ty) { 875 Ok(()) 876 } else { 877 let actual_ty = self.load_ty(store); 878 bail!("type mismatch: expected {func_ty}, found {actual_ty}") 879 } 880 } 881 882 pub(crate) fn type_index(&self, data: &StoreOpaque) -> VMSharedTypeIndex { 883 unsafe { self.vm_func_ref(data).as_ref().type_index } 884 } 885 886 /// Invokes this function with the `params` given and writes returned values 887 /// to `results`. 888 /// 889 /// The `params` here must match the type signature of this `Func`, or an 890 /// error will occur. Additionally `results` must have the same 891 /// length as the number of results for this function. Calling this function 892 /// will synchronously execute the WebAssembly function referenced to get 893 /// the results. 894 /// 895 /// This function will return `Ok(())` if execution completed without a trap 896 /// or error of any kind. In this situation the results will be written to 897 /// the provided `results` array. 898 /// 899 /// # Errors 900 /// 901 /// Any error which occurs throughout the execution of the function will be 902 /// returned as `Err(e)`. The [`Error`](crate::Error) type can be inspected 903 /// for the precise error cause such as: 904 /// 905 /// * [`Trap`] - indicates that a wasm trap happened and execution was 906 /// halted. 907 /// * [`WasmBacktrace`] - optionally included on errors for backtrace 908 /// information of the trap/error. 909 /// * Other string-based errors to indicate issues such as type errors with 910 /// `params`. 911 /// * Any host-originating error originally returned from a function defined 912 /// via [`Func::new`], for example. 913 /// 914 /// Errors typically indicate that execution of WebAssembly was halted 915 /// mid-way and did not complete after the error condition happened. 916 /// 917 /// [`Trap`]: crate::Trap 918 /// 919 /// # Panics 920 /// 921 /// This function will panic if called on a function belonging to an async 922 /// store. Asynchronous stores must always use `call_async`. Also panics if 923 /// `store` does not own this function. 924 /// 925 /// [`WasmBacktrace`]: crate::WasmBacktrace 926 pub fn call( 927 &self, 928 mut store: impl AsContextMut, 929 params: &[Val], 930 results: &mut [Val], 931 ) -> Result<()> { 932 assert!( 933 !store.as_context().async_support(), 934 "must use `call_async` when async support is enabled on the config", 935 ); 936 let mut store = store.as_context_mut(); 937 938 self.call_impl_check_args(&mut store, params, results)?; 939 940 unsafe { self.call_impl_do_call(&mut store, params, results) } 941 } 942 943 /// Invokes this function in an "unchecked" fashion, reading parameters and 944 /// writing results to `params_and_returns`. 945 /// 946 /// This function is the same as [`Func::call`] except that the arguments 947 /// and results both use a different representation. If possible it's 948 /// recommended to use [`Func::call`] if safety isn't necessary or to use 949 /// [`Func::typed`] in conjunction with [`TypedFunc::call`] since that's 950 /// both safer and faster than this method of invoking a function. 951 /// 952 /// Note that if this function takes `externref` arguments then it will 953 /// **not** automatically GC unlike the [`Func::call`] and 954 /// [`TypedFunc::call`] functions. This means that if this function is 955 /// invoked many times with new `ExternRef` values and no other GC happens 956 /// via any other means then no values will get collected. 957 /// 958 /// # Errors 959 /// 960 /// For more information about errors see the [`Func::call`] documentation. 961 /// 962 /// # Unsafety 963 /// 964 /// This function is unsafe because the `params_and_returns` argument is not 965 /// validated at all. It must uphold invariants such as: 966 /// 967 /// * It's a valid pointer to an array 968 /// * It has enough space to store all parameters 969 /// * It has enough space to store all results (not at the same time as 970 /// parameters) 971 /// * Parameters are initially written to the array and have the correct 972 /// types and such. 973 /// * Reference types like `externref` and `funcref` are valid at the 974 /// time of this call and for the `store` specified. 975 /// 976 /// These invariants are all upheld for you with [`Func::call`] and 977 /// [`TypedFunc::call`]. 978 pub unsafe fn call_unchecked( 979 &self, 980 mut store: impl AsContextMut, 981 params_and_returns: *mut [ValRaw], 982 ) -> Result<()> { 983 let mut store = store.as_context_mut(); 984 let func_ref = self.vm_func_ref(store.0); 985 let params_and_returns = NonNull::new(params_and_returns).unwrap_or(NonNull::from(&mut [])); 986 987 // SAFETY: the safety of this function call is the same as the contract 988 // of this function. 989 unsafe { Self::call_unchecked_raw(&mut store, func_ref, params_and_returns) } 990 } 991 992 pub(crate) unsafe fn call_unchecked_raw<T>( 993 store: &mut StoreContextMut<'_, T>, 994 func_ref: NonNull<VMFuncRef>, 995 params_and_returns: NonNull<[ValRaw]>, 996 ) -> Result<()> { 997 // SAFETY: the safety of this function call is the same as the contract 998 // of this function. 999 invoke_wasm_and_catch_traps(store, |caller, vm| unsafe { 1000 VMFuncRef::array_call(func_ref, vm, caller, params_and_returns) 1001 }) 1002 } 1003 1004 /// Converts the raw representation of a `funcref` into an `Option<Func>` 1005 /// 1006 /// This is intended to be used in conjunction with [`Func::new_unchecked`], 1007 /// [`Func::call_unchecked`], and [`ValRaw`] with its `funcref` field. This 1008 /// is the dual of [`Func::to_raw`]. 1009 /// 1010 /// # Unsafety 1011 /// 1012 /// This function is not safe because `raw` is not validated at all. The 1013 /// caller must guarantee that `raw` is owned by the `store` provided and is 1014 /// valid within the `store`. 1015 pub unsafe fn from_raw(mut store: impl AsContextMut, raw: *mut c_void) -> Option<Func> { 1016 // SAFETY: this function's own contract is that `raw` is owned by store 1017 // to make this safe. 1018 unsafe { Self::_from_raw(store.as_context_mut().0, raw) } 1019 } 1020 1021 /// Same as `from_raw`, but with the internal `StoreOpaque` type. 1022 pub(crate) unsafe fn _from_raw(store: &mut StoreOpaque, raw: *mut c_void) -> Option<Func> { 1023 // SAFETY: this function's own contract is that `raw` is owned by store 1024 // to make this safe. 1025 unsafe { 1026 Some(Func::from_vm_func_ref( 1027 store.id(), 1028 NonNull::new(raw.cast())?, 1029 )) 1030 } 1031 } 1032 1033 /// Extracts the raw value of this `Func`, which is owned by `store`. 1034 /// 1035 /// This function returns a value that's suitable for writing into the 1036 /// `funcref` field of the [`ValRaw`] structure. 1037 /// 1038 /// # Safety 1039 /// 1040 /// The returned value is only valid for as long as the store is alive. 1041 /// This value is safe to pass to [`Func::from_raw`] so long as the same 1042 /// `store` is provided. 1043 pub fn to_raw(&self, mut store: impl AsContextMut) -> *mut c_void { 1044 self.vm_func_ref(store.as_context_mut().0).as_ptr().cast() 1045 } 1046 1047 /// Invokes this function with the `params` given, returning the results 1048 /// asynchronously. 1049 /// 1050 /// This function is the same as [`Func::call`] except that it is 1051 /// asynchronous. This is only compatible with stores associated with an 1052 /// [asynchronous config](crate::Config::async_support). 1053 /// 1054 /// It's important to note that the execution of WebAssembly will happen 1055 /// synchronously in the `poll` method of the future returned from this 1056 /// function. Wasmtime does not manage its own thread pool or similar to 1057 /// execute WebAssembly in. Future `poll` methods are generally expected to 1058 /// resolve quickly, so it's recommended that you run or poll this future 1059 /// in a "blocking context". 1060 /// 1061 /// For more information see the documentation on [asynchronous 1062 /// configs](crate::Config::async_support). 1063 /// 1064 /// # Errors 1065 /// 1066 /// For more information on errors see the [`Func::call`] documentation. 1067 /// 1068 /// # Panics 1069 /// 1070 /// Panics if this is called on a function in a synchronous store. This 1071 /// only works with functions defined within an asynchronous store. Also 1072 /// panics if `store` does not own this function. 1073 #[cfg(feature = "async")] 1074 pub async fn call_async( 1075 &self, 1076 mut store: impl AsContextMut<Data: Send>, 1077 params: &[Val], 1078 results: &mut [Val], 1079 ) -> Result<()> { 1080 let mut store = store.as_context_mut(); 1081 assert!( 1082 store.0.async_support(), 1083 "cannot use `call_async` without enabling async support in the config", 1084 ); 1085 1086 self.call_impl_check_args(&mut store, params, results)?; 1087 1088 let result = store 1089 .on_fiber(|store| unsafe { self.call_impl_do_call(store, params, results) }) 1090 .await??; 1091 Ok(result) 1092 } 1093 1094 /// Perform dynamic checks that the arguments given to us match 1095 /// the signature of this function and are appropriate to pass to this 1096 /// function. 1097 /// 1098 /// This involves checking to make sure we have the right number and types 1099 /// of arguments as well as making sure everything is from the same `Store`. 1100 /// 1101 /// This must be called just before `call_impl_do_call`. 1102 fn call_impl_check_args<T>( 1103 &self, 1104 store: &mut StoreContextMut<'_, T>, 1105 params: &[Val], 1106 results: &mut [Val], 1107 ) -> Result<()> { 1108 let ty = self.load_ty(store.0); 1109 if ty.params().len() != params.len() { 1110 bail!( 1111 "expected {} arguments, got {}", 1112 ty.params().len(), 1113 params.len() 1114 ); 1115 } 1116 if ty.results().len() != results.len() { 1117 bail!( 1118 "expected {} results, got {}", 1119 ty.results().len(), 1120 results.len() 1121 ); 1122 } 1123 1124 for (ty, arg) in ty.params().zip(params) { 1125 arg.ensure_matches_ty(store.0, &ty) 1126 .context("argument type mismatch")?; 1127 if !arg.comes_from_same_store(store.0) { 1128 bail!("cross-`Store` values are not currently supported"); 1129 } 1130 } 1131 1132 Ok(()) 1133 } 1134 1135 /// Do the actual call into Wasm. 1136 /// 1137 /// # Safety 1138 /// 1139 /// You must have type checked the arguments by calling 1140 /// `call_impl_check_args` immediately before calling this function. It is 1141 /// only safe to call this function if that one did not return an error. 1142 unsafe fn call_impl_do_call<T>( 1143 &self, 1144 store: &mut StoreContextMut<'_, T>, 1145 params: &[Val], 1146 results: &mut [Val], 1147 ) -> Result<()> { 1148 // Store the argument values into `values_vec`. 1149 let ty = self.load_ty(store.0); 1150 let values_vec_size = params.len().max(ty.results().len()); 1151 let mut values_vec = store.0.take_wasm_val_raw_storage(); 1152 debug_assert!(values_vec.is_empty()); 1153 values_vec.resize_with(values_vec_size, || ValRaw::v128(0)); 1154 for (arg, slot) in params.iter().cloned().zip(&mut values_vec) { 1155 *slot = arg.to_raw(&mut *store)?; 1156 } 1157 1158 unsafe { 1159 self.call_unchecked( 1160 &mut *store, 1161 core::ptr::slice_from_raw_parts_mut(values_vec.as_mut_ptr(), values_vec_size), 1162 )?; 1163 } 1164 1165 for ((i, slot), val) in results.iter_mut().enumerate().zip(&values_vec) { 1166 let ty = ty.results().nth(i).unwrap(); 1167 *slot = unsafe { Val::from_raw(&mut *store, *val, ty) }; 1168 } 1169 values_vec.truncate(0); 1170 store.0.save_wasm_val_raw_storage(values_vec); 1171 Ok(()) 1172 } 1173 1174 #[inline] 1175 pub(crate) fn vm_func_ref(&self, store: &StoreOpaque) -> NonNull<VMFuncRef> { 1176 self.store.assert_belongs_to(store.id()); 1177 self.unsafe_func_ref.as_non_null() 1178 } 1179 1180 pub(crate) fn vmimport(&self, store: &StoreOpaque) -> VMFunctionImport { 1181 unsafe { 1182 let f = self.vm_func_ref(store); 1183 VMFunctionImport { 1184 // Note that this is a load-bearing `unwrap` here, but is 1185 // never expected to trip at runtime. The general problem is 1186 // that host functions do not have a `wasm_call` function so 1187 // the `VMFuncRef` type has an optional pointer there. This is 1188 // only able to be filled out when a function is "paired" with 1189 // a module where trampolines are present to fill out 1190 // `wasm_call` pointers. 1191 // 1192 // This pairing of modules doesn't happen explicitly but is 1193 // instead managed lazily throughout Wasmtime. Specifically the 1194 // way this works is one of: 1195 // 1196 // * When a host function is created the store's list of 1197 // modules are searched for a wasm trampoline. If not found 1198 // the `wasm_call` field is left blank. 1199 // 1200 // * When a module instantiation happens, which uses this 1201 // function, the module will be used to fill any outstanding 1202 // holes that it has trampolines for. 1203 // 1204 // This means that by the time we get to this point any 1205 // relevant holes should be filled out. Thus if this panic 1206 // actually triggers then it's indicative of a missing `fill` 1207 // call somewhere else. 1208 wasm_call: f.as_ref().wasm_call.unwrap(), 1209 array_call: f.as_ref().array_call, 1210 vmctx: f.as_ref().vmctx, 1211 } 1212 } 1213 } 1214 1215 pub(crate) fn comes_from_same_store(&self, store: &StoreOpaque) -> bool { 1216 self.store == store.id() 1217 } 1218 1219 /// Attempts to extract a typed object from this `Func` through which the 1220 /// function can be called. 1221 /// 1222 /// This function serves as an alternative to [`Func::call`] and 1223 /// [`Func::call_async`]. This method performs a static type check (using 1224 /// the `Params` and `Results` type parameters on the underlying wasm 1225 /// function. If the type check passes then a `TypedFunc` object is returned, 1226 /// otherwise an error is returned describing the typecheck failure. 1227 /// 1228 /// The purpose of this relative to [`Func::call`] is that it's much more 1229 /// efficient when used to invoke WebAssembly functions. With the types 1230 /// statically known far less setup/teardown is required when invoking 1231 /// WebAssembly. If speed is desired then this function is recommended to be 1232 /// used instead of [`Func::call`] (which is more general, hence its 1233 /// slowdown). 1234 /// 1235 /// The `Params` type parameter is used to describe the parameters of the 1236 /// WebAssembly function. This can either be a single type (like `i32`), or 1237 /// a tuple of types representing the list of parameters (like `(i32, f32, 1238 /// f64)`). Additionally you can use `()` to represent that the function has 1239 /// no parameters. 1240 /// 1241 /// The `Results` type parameter is used to describe the results of the 1242 /// function. This behaves the same way as `Params`, but just for the 1243 /// results of the function. 1244 /// 1245 /// # Translating Between WebAssembly and Rust Types 1246 /// 1247 /// Translation between Rust types and WebAssembly types looks like: 1248 /// 1249 /// | WebAssembly | Rust | 1250 /// |-------------------------------------------|---------------------------------------| 1251 /// | `i32` | `i32` or `u32` | 1252 /// | `i64` | `i64` or `u64` | 1253 /// | `f32` | `f32` | 1254 /// | `f64` | `f64` | 1255 /// | `externref` aka `(ref null extern)` | `Option<Rooted<ExternRef>>` | 1256 /// | `(ref extern)` | `Rooted<ExternRef>` | 1257 /// | `nullexternref` aka `(ref null noextern)` | `Option<NoExtern>` | 1258 /// | `(ref noextern)` | `NoExtern` | 1259 /// | `anyref` aka `(ref null any)` | `Option<Rooted<AnyRef>>` | 1260 /// | `(ref any)` | `Rooted<AnyRef>` | 1261 /// | `eqref` aka `(ref null eq)` | `Option<Rooted<EqRef>>` | 1262 /// | `(ref eq)` | `Rooted<EqRef>` | 1263 /// | `i31ref` aka `(ref null i31)` | `Option<I31>` | 1264 /// | `(ref i31)` | `I31` | 1265 /// | `structref` aka `(ref null struct)` | `Option<Rooted<StructRef>>` | 1266 /// | `(ref struct)` | `Rooted<StructRef>` | 1267 /// | `arrayref` aka `(ref null array)` | `Option<Rooted<ArrayRef>>` | 1268 /// | `(ref array)` | `Rooted<ArrayRef>` | 1269 /// | `nullref` aka `(ref null none)` | `Option<NoneRef>` | 1270 /// | `(ref none)` | `NoneRef` | 1271 /// | `funcref` aka `(ref null func)` | `Option<Func>` | 1272 /// | `(ref func)` | `Func` | 1273 /// | `(ref null <func type index>)` | `Option<Func>` | 1274 /// | `(ref <func type index>)` | `Func` | 1275 /// | `nullfuncref` aka `(ref null nofunc)` | `Option<NoFunc>` | 1276 /// | `(ref nofunc)` | `NoFunc` | 1277 /// | `v128` | `V128` on `x86-64` and `aarch64` only | 1278 /// 1279 /// (Note that this mapping is the same as that of [`Func::wrap`], and that 1280 /// anywhere a `Rooted<T>` appears, a `OwnedRooted<T>` may also appear). 1281 /// 1282 /// Note that once the [`TypedFunc`] return value is acquired you'll use either 1283 /// [`TypedFunc::call`] or [`TypedFunc::call_async`] as necessary to actually invoke 1284 /// the function. This method does not invoke any WebAssembly code, it 1285 /// simply performs a typecheck before returning the [`TypedFunc`] value. 1286 /// 1287 /// This method also has a convenience wrapper as 1288 /// [`Instance::get_typed_func`](crate::Instance::get_typed_func) to 1289 /// directly get a typed function value from an 1290 /// [`Instance`](crate::Instance). 1291 /// 1292 /// ## Subtyping 1293 /// 1294 /// For result types, you can always use a supertype of the WebAssembly 1295 /// function's actual declared result type. For example, if the WebAssembly 1296 /// function was declared with type `(func (result nullfuncref))` you could 1297 /// successfully call `f.typed::<(), Option<Func>>()` because `Option<Func>` 1298 /// corresponds to `funcref`, which is a supertype of `nullfuncref`. 1299 /// 1300 /// For parameter types, you can always use a subtype of the WebAssembly 1301 /// function's actual declared parameter type. For example, if the 1302 /// WebAssembly function was declared with type `(func (param (ref null 1303 /// func)))` you could successfully call `f.typed::<Func, ()>()` because 1304 /// `Func` corresponds to `(ref func)`, which is a subtype of `(ref null 1305 /// func)`. 1306 /// 1307 /// Additionally, for functions which take a reference to a concrete type as 1308 /// a parameter, you can also use the concrete type's supertype. Consider a 1309 /// WebAssembly function that takes a reference to a function with a 1310 /// concrete type: `(ref null <func type index>)`. In this scenario, there 1311 /// is no static `wasmtime::Foo` Rust type that corresponds to that 1312 /// particular Wasm-defined concrete reference type because Wasm modules are 1313 /// loaded dynamically at runtime. You *could* do `f.typed::<Option<NoFunc>, 1314 /// ()>()`, and while that is correctly typed and valid, it is often overly 1315 /// restrictive. The only value you could call the resulting typed function 1316 /// with is the null function reference, but we'd like to call it with 1317 /// non-null function references that happen to be of the correct 1318 /// type. Therefore, `f.typed<Option<Func>, ()>()` is also allowed in this 1319 /// case, even though `Option<Func>` represents `(ref null func)` which is 1320 /// the supertype, not subtype, of `(ref null <func type index>)`. This does 1321 /// imply some minimal dynamic type checks in this case, but it is supported 1322 /// for better ergonomics, to enable passing non-null references into the 1323 /// function. 1324 /// 1325 /// # Errors 1326 /// 1327 /// This function will return an error if `Params` or `Results` does not 1328 /// match the native type of this WebAssembly function. 1329 /// 1330 /// # Panics 1331 /// 1332 /// This method will panic if `store` does not own this function. 1333 /// 1334 /// # Examples 1335 /// 1336 /// An end-to-end example of calling a function which takes no parameters 1337 /// and has no results: 1338 /// 1339 /// ``` 1340 /// # use wasmtime::*; 1341 /// # fn main() -> Result<()> { 1342 /// let engine = Engine::default(); 1343 /// let mut store = Store::new(&engine, ()); 1344 /// let module = Module::new(&engine, r#"(module (func (export "foo")))"#)?; 1345 /// let instance = Instance::new(&mut store, &module, &[])?; 1346 /// let foo = instance.get_func(&mut store, "foo").expect("export wasn't a function"); 1347 /// 1348 /// // Note that this call can fail due to the typecheck not passing, but 1349 /// // in our case we statically know the module so we know this should 1350 /// // pass. 1351 /// let typed = foo.typed::<(), ()>(&store)?; 1352 /// 1353 /// // Note that this can fail if the wasm traps at runtime. 1354 /// typed.call(&mut store, ())?; 1355 /// # Ok(()) 1356 /// # } 1357 /// ``` 1358 /// 1359 /// You can also pass in multiple parameters and get a result back 1360 /// 1361 /// ``` 1362 /// # use wasmtime::*; 1363 /// # fn foo(add: &Func, mut store: Store<()>) -> Result<()> { 1364 /// let typed = add.typed::<(i32, i64), f32>(&store)?; 1365 /// assert_eq!(typed.call(&mut store, (1, 2))?, 3.0); 1366 /// # Ok(()) 1367 /// # } 1368 /// ``` 1369 /// 1370 /// and similarly if a function has multiple results you can bind that too 1371 /// 1372 /// ``` 1373 /// # use wasmtime::*; 1374 /// # fn foo(add_with_overflow: &Func, mut store: Store<()>) -> Result<()> { 1375 /// let typed = add_with_overflow.typed::<(u32, u32), (u32, i32)>(&store)?; 1376 /// let (result, overflow) = typed.call(&mut store, (u32::max_value(), 2))?; 1377 /// assert_eq!(result, 1); 1378 /// assert_eq!(overflow, 1); 1379 /// # Ok(()) 1380 /// # } 1381 /// ``` 1382 pub fn typed<Params, Results>( 1383 &self, 1384 store: impl AsContext, 1385 ) -> Result<TypedFunc<Params, Results>> 1386 where 1387 Params: WasmParams, 1388 Results: WasmResults, 1389 { 1390 // Type-check that the params/results are all valid 1391 let store = store.as_context().0; 1392 let ty = self.load_ty(store); 1393 Params::typecheck(store.engine(), ty.params(), TypeCheckPosition::Param) 1394 .context("type mismatch with parameters")?; 1395 Results::typecheck(store.engine(), ty.results(), TypeCheckPosition::Result) 1396 .context("type mismatch with results")?; 1397 1398 // and then we can construct the typed version of this function 1399 // (unsafely), which should be safe since we just did the type check above. 1400 unsafe { Ok(TypedFunc::_new_unchecked(store, *self)) } 1401 } 1402 1403 /// Get a stable hash key for this function. 1404 /// 1405 /// Even if the same underlying function is added to the `StoreData` 1406 /// multiple times and becomes multiple `wasmtime::Func`s, this hash key 1407 /// will be consistent across all of these functions. 1408 #[cfg_attr( 1409 not(test), 1410 expect(dead_code, reason = "Not used yet, but added for consistency") 1411 )] 1412 pub(crate) fn hash_key(&self, store: &mut StoreOpaque) -> impl core::hash::Hash + Eq + use<> { 1413 self.vm_func_ref(store).as_ptr().addr() 1414 } 1415 } 1416 1417 /// Prepares for entrance into WebAssembly. 1418 /// 1419 /// This function will set up context such that `closure` is allowed to call a 1420 /// raw trampoline or a raw WebAssembly function. This *must* be called to do 1421 /// things like catch traps and set up GC properly. 1422 /// 1423 /// The `closure` provided receives a default "caller" `VMContext` parameter it 1424 /// can pass to the called wasm function, if desired. 1425 pub(crate) fn invoke_wasm_and_catch_traps<T>( 1426 store: &mut StoreContextMut<'_, T>, 1427 closure: impl FnMut(NonNull<VMContext>, Option<InterpreterRef<'_>>) -> bool, 1428 ) -> Result<()> { 1429 // The `enter_wasm` call below will reset the store context's 1430 // `stack_chain` to a new `InitialStack`, pointing to the 1431 // stack-allocated `initial_stack_csi`. 1432 let mut initial_stack_csi = VMCommonStackInformation::running_default(); 1433 // Stores some state of the runtime just before entering Wasm. Will be 1434 // restored upon exiting Wasm. Note that the `CallThreadState` that is 1435 // created by the `catch_traps` call below will store a pointer to this 1436 // stack-allocated `previous_runtime_state`. 1437 let mut previous_runtime_state = EntryStoreContext::enter_wasm(store, &mut initial_stack_csi); 1438 1439 if let Err(trap) = store.0.call_hook(CallHook::CallingWasm) { 1440 // `previous_runtime_state` implicitly dropped here 1441 return Err(trap); 1442 } 1443 let result = crate::runtime::vm::catch_traps(store, &mut previous_runtime_state, closure); 1444 #[cfg(feature = "component-model")] 1445 if result.is_err() { 1446 store.0.set_trapped(); 1447 } 1448 core::mem::drop(previous_runtime_state); 1449 store.0.call_hook(CallHook::ReturningFromWasm)?; 1450 result 1451 } 1452 1453 /// This type helps managing the state of the runtime when entering and exiting 1454 /// Wasm. To this end, it contains a subset of the data in `VMStoreContext`. 1455 /// Upon entering Wasm, it updates various runtime fields and their 1456 /// original values saved in this struct. Upon exiting Wasm, the previous values 1457 /// are restored. 1458 pub(crate) struct EntryStoreContext { 1459 /// If set, contains value of `stack_limit` field to restore in 1460 /// `VMStoreContext` when exiting Wasm. 1461 pub stack_limit: Option<usize>, 1462 pub last_wasm_exit_pc: usize, 1463 pub last_wasm_exit_trampoline_fp: usize, 1464 pub last_wasm_entry_fp: usize, 1465 pub last_wasm_entry_sp: usize, 1466 pub last_wasm_entry_trap_handler: usize, 1467 pub stack_chain: VMStackChain, 1468 1469 /// We need a pointer to the runtime limits, so we can update them from 1470 /// `drop`/`exit_wasm`. 1471 vm_store_context: *const VMStoreContext, 1472 } 1473 1474 impl EntryStoreContext { 1475 /// This function is called to update and save state when 1476 /// WebAssembly is entered within the `Store`. 1477 /// 1478 /// This updates various fields such as: 1479 /// 1480 /// * The stack limit. This is what ensures that we limit the stack space 1481 /// allocated by WebAssembly code and it's relative to the initial stack 1482 /// pointer that called into wasm. 1483 /// 1484 /// It also saves the different last_wasm_* values in the `VMStoreContext`. 1485 pub fn enter_wasm<T>( 1486 store: &mut StoreContextMut<'_, T>, 1487 initial_stack_information: *mut VMCommonStackInformation, 1488 ) -> Self { 1489 let stack_limit; 1490 1491 // If this is a recursive call, e.g. our stack limit is already set, then 1492 // we may be able to skip this function. 1493 // 1494 // For synchronous stores there's nothing else to do because all wasm calls 1495 // happen synchronously and on the same stack. This means that the previous 1496 // stack limit will suffice for the next recursive call. 1497 // 1498 // For asynchronous stores then each call happens on a separate native 1499 // stack. This means that the previous stack limit is no longer relevant 1500 // because we're on a separate stack. 1501 if unsafe { *store.0.vm_store_context().stack_limit.get() } != usize::MAX 1502 && !store.0.async_support() 1503 { 1504 stack_limit = None; 1505 } 1506 // Ignore this stack pointer business on miri since we can't execute wasm 1507 // anyway and the concept of a stack pointer on miri is a bit nebulous 1508 // regardless. 1509 else if cfg!(miri) { 1510 stack_limit = None; 1511 } else { 1512 // When Cranelift has support for the host then we might be running native 1513 // compiled code meaning we need to read the actual stack pointer. If 1514 // Cranelift can't be used though then we're guaranteed to be running pulley 1515 // in which case this stack pointer isn't actually used as Pulley has custom 1516 // mechanisms for stack overflow. 1517 #[cfg(has_host_compiler_backend)] 1518 let stack_pointer = crate::runtime::vm::get_stack_pointer(); 1519 #[cfg(not(has_host_compiler_backend))] 1520 let stack_pointer = { 1521 use wasmtime_environ::TripleExt; 1522 debug_assert!(store.engine().target().is_pulley()); 1523 usize::MAX 1524 }; 1525 1526 // Determine the stack pointer where, after which, any wasm code will 1527 // immediately trap. This is checked on the entry to all wasm functions. 1528 // 1529 // Note that this isn't 100% precise. We are requested to give wasm 1530 // `max_wasm_stack` bytes, but what we're actually doing is giving wasm 1531 // probably a little less than `max_wasm_stack` because we're 1532 // calculating the limit relative to this function's approximate stack 1533 // pointer. Wasm will be executed on a frame beneath this one (or next 1534 // to it). In any case it's expected to be at most a few hundred bytes 1535 // of slop one way or another. When wasm is typically given a MB or so 1536 // (a million bytes) the slop shouldn't matter too much. 1537 // 1538 // After we've got the stack limit then we store it into the `stack_limit` 1539 // variable. 1540 let wasm_stack_limit = stack_pointer 1541 .checked_sub(store.engine().config().max_wasm_stack) 1542 .unwrap(); 1543 let prev_stack = unsafe { 1544 mem::replace( 1545 &mut *store.0.vm_store_context().stack_limit.get(), 1546 wasm_stack_limit, 1547 ) 1548 }; 1549 stack_limit = Some(prev_stack); 1550 } 1551 1552 unsafe { 1553 let vm_store_context = store.0.vm_store_context(); 1554 let new_stack_chain = VMStackChain::InitialStack(initial_stack_information); 1555 *vm_store_context.stack_chain.get() = new_stack_chain; 1556 1557 Self { 1558 stack_limit, 1559 last_wasm_exit_pc: *(*vm_store_context).last_wasm_exit_pc.get(), 1560 last_wasm_exit_trampoline_fp: *(*vm_store_context) 1561 .last_wasm_exit_trampoline_fp 1562 .get(), 1563 last_wasm_entry_fp: *(*vm_store_context).last_wasm_entry_fp.get(), 1564 last_wasm_entry_sp: *(*vm_store_context).last_wasm_entry_sp.get(), 1565 last_wasm_entry_trap_handler: *(*vm_store_context) 1566 .last_wasm_entry_trap_handler 1567 .get(), 1568 stack_chain: (*(*vm_store_context).stack_chain.get()).clone(), 1569 vm_store_context, 1570 } 1571 } 1572 } 1573 1574 /// This function restores the values stored in this struct. We invoke this 1575 /// function through this type's `Drop` implementation. This ensures that we 1576 /// even restore the values if we unwind the stack (e.g., because we are 1577 /// panicking out of a Wasm execution). 1578 #[inline] 1579 fn exit_wasm(&mut self) { 1580 unsafe { 1581 if let Some(limit) = self.stack_limit { 1582 *(&*self.vm_store_context).stack_limit.get() = limit; 1583 } 1584 1585 *(*self.vm_store_context).last_wasm_exit_trampoline_fp.get() = 1586 self.last_wasm_exit_trampoline_fp; 1587 *(*self.vm_store_context).last_wasm_exit_pc.get() = self.last_wasm_exit_pc; 1588 *(*self.vm_store_context).last_wasm_entry_fp.get() = self.last_wasm_entry_fp; 1589 *(*self.vm_store_context).last_wasm_entry_sp.get() = self.last_wasm_entry_sp; 1590 *(*self.vm_store_context).last_wasm_entry_trap_handler.get() = 1591 self.last_wasm_entry_trap_handler; 1592 *(*self.vm_store_context).stack_chain.get() = self.stack_chain.clone(); 1593 } 1594 } 1595 } 1596 1597 impl Drop for EntryStoreContext { 1598 #[inline] 1599 fn drop(&mut self) { 1600 self.exit_wasm(); 1601 } 1602 } 1603 1604 /// A trait implemented for types which can be returned from closures passed to 1605 /// [`Func::wrap`] and friends. 1606 /// 1607 /// This trait should not be implemented by user types. This trait may change at 1608 /// any time internally. The types which implement this trait, however, are 1609 /// stable over time. 1610 /// 1611 /// For more information see [`Func::wrap`] 1612 pub unsafe trait WasmRet { 1613 // Same as `WasmTy::compatible_with_store`. 1614 #[doc(hidden)] 1615 fn compatible_with_store(&self, store: &StoreOpaque) -> bool; 1616 1617 /// Stores this return value into the `ptr` specified using the rooted 1618 /// `store`. 1619 /// 1620 /// Traps are communicated through the `Result<_>` return value. 1621 /// 1622 /// # Unsafety 1623 /// 1624 /// This method is unsafe as `ptr` must have the correct length to store 1625 /// this result. This property is only checked in debug mode, not in release 1626 /// mode. 1627 #[doc(hidden)] 1628 unsafe fn store( 1629 self, 1630 store: &mut AutoAssertNoGc<'_>, 1631 ptr: &mut [MaybeUninit<ValRaw>], 1632 ) -> Result<()>; 1633 1634 #[doc(hidden)] 1635 fn func_type(engine: &Engine, params: impl Iterator<Item = ValType>) -> FuncType; 1636 #[doc(hidden)] 1637 fn may_gc() -> bool; 1638 1639 // Utilities used to convert an instance of this type to a `Result` 1640 // explicitly, used when wrapping async functions which always bottom-out 1641 // in a function that returns a trap because futures can be cancelled. 1642 #[doc(hidden)] 1643 type Fallible: WasmRet; 1644 #[doc(hidden)] 1645 fn into_fallible(self) -> Self::Fallible; 1646 #[doc(hidden)] 1647 fn fallible_from_error(error: Error) -> Self::Fallible; 1648 } 1649 1650 unsafe impl<T> WasmRet for T 1651 where 1652 T: WasmTy, 1653 { 1654 type Fallible = Result<T>; 1655 1656 fn compatible_with_store(&self, store: &StoreOpaque) -> bool { 1657 <Self as WasmTy>::compatible_with_store(self, store) 1658 } 1659 1660 unsafe fn store( 1661 self, 1662 store: &mut AutoAssertNoGc<'_>, 1663 ptr: &mut [MaybeUninit<ValRaw>], 1664 ) -> Result<()> { 1665 debug_assert!(ptr.len() > 0); 1666 // SAFETY: the contract of this function/trait combo is such that `ptr` 1667 // is valid to store this type's value, thus this lookup should be safe. 1668 unsafe { <Self as WasmTy>::store(self, store, ptr.get_unchecked_mut(0)) } 1669 } 1670 1671 fn may_gc() -> bool { 1672 T::may_gc() 1673 } 1674 1675 fn func_type(engine: &Engine, params: impl Iterator<Item = ValType>) -> FuncType { 1676 FuncType::new(engine, params, Some(<Self as WasmTy>::valtype())) 1677 } 1678 1679 fn into_fallible(self) -> Result<T> { 1680 Ok(self) 1681 } 1682 1683 fn fallible_from_error(error: Error) -> Result<T> { 1684 Err(error) 1685 } 1686 } 1687 1688 unsafe impl<T> WasmRet for Result<T> 1689 where 1690 T: WasmRet, 1691 { 1692 type Fallible = Self; 1693 1694 fn compatible_with_store(&self, store: &StoreOpaque) -> bool { 1695 match self { 1696 Ok(x) => <T as WasmRet>::compatible_with_store(x, store), 1697 Err(_) => true, 1698 } 1699 } 1700 1701 unsafe fn store( 1702 self, 1703 store: &mut AutoAssertNoGc<'_>, 1704 ptr: &mut [MaybeUninit<ValRaw>], 1705 ) -> Result<()> { 1706 // SAFETY: the safety of calling this function is the same as calling 1707 // the inner `store`. 1708 unsafe { self.and_then(|val| val.store(store, ptr)) } 1709 } 1710 1711 fn may_gc() -> bool { 1712 T::may_gc() 1713 } 1714 1715 fn func_type(engine: &Engine, params: impl Iterator<Item = ValType>) -> FuncType { 1716 T::func_type(engine, params) 1717 } 1718 1719 fn into_fallible(self) -> Result<T> { 1720 self 1721 } 1722 1723 fn fallible_from_error(error: Error) -> Result<T> { 1724 Err(error) 1725 } 1726 } 1727 1728 macro_rules! impl_wasm_host_results { 1729 ($n:tt $($t:ident)*) => ( 1730 #[allow(non_snake_case, reason = "macro-generated code")] 1731 unsafe impl<$($t),*> WasmRet for ($($t,)*) 1732 where 1733 $($t: WasmTy,)* 1734 { 1735 type Fallible = Result<Self>; 1736 1737 #[inline] 1738 fn compatible_with_store(&self, _store: &StoreOpaque) -> bool { 1739 let ($($t,)*) = self; 1740 $( $t.compatible_with_store(_store) && )* true 1741 } 1742 1743 #[inline] 1744 unsafe fn store( 1745 self, 1746 _store: &mut AutoAssertNoGc<'_>, 1747 _ptr: &mut [MaybeUninit<ValRaw>], 1748 ) -> Result<()> { 1749 let ($($t,)*) = self; 1750 let mut _cur = 0; 1751 $( 1752 debug_assert!(_cur < _ptr.len()); 1753 // SAFETY: `store`'s unsafe contract is that `_ptr` is 1754 // appropriately sized and additionally safe to call `store` 1755 // for sub-types. 1756 unsafe { 1757 let val = _ptr.get_unchecked_mut(_cur); 1758 _cur += 1; 1759 WasmTy::store($t, _store, val)?; 1760 } 1761 )* 1762 Ok(()) 1763 } 1764 1765 #[doc(hidden)] 1766 fn may_gc() -> bool { 1767 $( $t::may_gc() || )* false 1768 } 1769 1770 fn func_type(engine: &Engine, params: impl Iterator<Item = ValType>) -> FuncType { 1771 FuncType::new( 1772 engine, 1773 params, 1774 IntoIterator::into_iter([$($t::valtype(),)*]), 1775 ) 1776 } 1777 1778 #[inline] 1779 fn into_fallible(self) -> Result<Self> { 1780 Ok(self) 1781 } 1782 1783 #[inline] 1784 fn fallible_from_error(error: Error) -> Result<Self> { 1785 Err(error) 1786 } 1787 } 1788 ) 1789 } 1790 1791 for_each_function_signature!(impl_wasm_host_results); 1792 1793 /// Internal trait implemented for all arguments that can be passed to 1794 /// [`Func::wrap`] and [`Linker::func_wrap`](crate::Linker::func_wrap). 1795 /// 1796 /// This trait should not be implemented by external users, it's only intended 1797 /// as an implementation detail of this crate. 1798 pub trait IntoFunc<T, Params, Results>: Send + Sync + 'static { 1799 /// Convert this function into a `VM{Array,Native}CallHostFuncContext` and 1800 /// internal `VMFuncRef`. 1801 #[doc(hidden)] 1802 fn into_func(self, engine: &Engine) -> HostFunc; 1803 } 1804 1805 macro_rules! impl_into_func { 1806 ($num:tt $arg:ident) => { 1807 // Implement for functions without a leading `Caller` parameter, 1808 // delegating to the implementation below which does have the leading 1809 // `Caller` parameter. 1810 #[expect(non_snake_case, reason = "macro-generated code")] 1811 impl<T, F, $arg, R> IntoFunc<T, $arg, R> for F 1812 where 1813 F: Fn($arg) -> R + Send + Sync + 'static, 1814 $arg: WasmTy, 1815 R: WasmRet, 1816 T: 'static, 1817 { 1818 fn into_func(self, engine: &Engine) -> HostFunc { 1819 let f = move |_: Caller<'_, T>, $arg: $arg| { 1820 self($arg) 1821 }; 1822 1823 f.into_func(engine) 1824 } 1825 } 1826 1827 #[expect(non_snake_case, reason = "macro-generated code")] 1828 impl<T, F, $arg, R> IntoFunc<T, (Caller<'_, T>, $arg), R> for F 1829 where 1830 F: Fn(Caller<'_, T>, $arg) -> R + Send + Sync + 'static, 1831 $arg: WasmTy, 1832 R: WasmRet, 1833 T: 'static, 1834 { 1835 fn into_func(self, engine: &Engine) -> HostFunc { 1836 HostFunc::wrap(engine, move |caller: Caller<'_, T>, ($arg,)| { 1837 self(caller, $arg) 1838 }) 1839 } 1840 } 1841 }; 1842 ($num:tt $($args:ident)*) => { 1843 // Implement for functions without a leading `Caller` parameter, 1844 // delegating to the implementation below which does have the leading 1845 // `Caller` parameter. 1846 #[allow(non_snake_case, reason = "macro-generated code")] 1847 impl<T, F, $($args,)* R> IntoFunc<T, ($($args,)*), R> for F 1848 where 1849 F: Fn($($args),*) -> R + Send + Sync + 'static, 1850 $($args: WasmTy,)* 1851 R: WasmRet, 1852 T: 'static, 1853 { 1854 fn into_func(self, engine: &Engine) -> HostFunc { 1855 let f = move |_: Caller<'_, T>, $($args:$args),*| { 1856 self($($args),*) 1857 }; 1858 1859 f.into_func(engine) 1860 } 1861 } 1862 1863 #[allow(non_snake_case, reason = "macro-generated code")] 1864 impl<T, F, $($args,)* R> IntoFunc<T, (Caller<'_, T>, $($args,)*), R> for F 1865 where 1866 F: Fn(Caller<'_, T>, $($args),*) -> R + Send + Sync + 'static, 1867 $($args: WasmTy,)* 1868 R: WasmRet, 1869 T: 'static, 1870 { 1871 fn into_func(self, engine: &Engine) -> HostFunc { 1872 HostFunc::wrap(engine, move |caller: Caller<'_, T>, ( $( $args ),* )| { 1873 self(caller, $( $args ),* ) 1874 }) 1875 } 1876 } 1877 } 1878 } 1879 1880 for_each_function_signature!(impl_into_func); 1881 1882 /// Trait implemented for various tuples made up of types which implement 1883 /// [`WasmTy`] that can be passed to [`Func::wrap_async`] and 1884 /// [`HostContext::new_typed`]. 1885 pub unsafe trait WasmTyList { 1886 /// Get the value type that each Type in the list represents. 1887 fn valtypes() -> impl Iterator<Item = ValType>; 1888 1889 // Load a version of `Self` from the `values` provided. 1890 // 1891 // # Safety 1892 // 1893 // This function is unsafe as it's up to the caller to ensure that `values` are 1894 // valid for this given type. 1895 #[doc(hidden)] 1896 unsafe fn load(store: &mut AutoAssertNoGc<'_>, values: &mut [MaybeUninit<ValRaw>]) -> Self; 1897 1898 #[doc(hidden)] 1899 fn may_gc() -> bool; 1900 } 1901 1902 macro_rules! impl_wasm_ty_list { 1903 ($num:tt $($args:ident)*) => ( 1904 #[allow(non_snake_case, reason = "macro-generated code")] 1905 unsafe impl<$($args),*> WasmTyList for ($($args,)*) 1906 where 1907 $($args: WasmTy,)* 1908 { 1909 fn valtypes() -> impl Iterator<Item = ValType> { 1910 IntoIterator::into_iter([$($args::valtype(),)*]) 1911 } 1912 1913 unsafe fn load(_store: &mut AutoAssertNoGc<'_>, _values: &mut [MaybeUninit<ValRaw>]) -> Self { 1914 let mut _cur = 0; 1915 ($({ 1916 debug_assert!(_cur < _values.len()); 1917 // SAFETY: this function's own contract means that `_values` 1918 // is appropriately sized/typed for the internal loads. 1919 unsafe { 1920 let ptr = _values.get_unchecked(_cur).assume_init_ref(); 1921 _cur += 1; 1922 $args::load(_store, ptr) 1923 } 1924 },)*) 1925 } 1926 1927 fn may_gc() -> bool { 1928 $( $args::may_gc() || )* false 1929 } 1930 } 1931 ); 1932 } 1933 1934 for_each_function_signature!(impl_wasm_ty_list); 1935 1936 /// A structure representing the caller's context when creating a function 1937 /// via [`Func::wrap`]. 1938 /// 1939 /// This structure can be taken as the first parameter of a closure passed to 1940 /// [`Func::wrap`] or other constructors, and serves two purposes: 1941 /// 1942 /// * First consumers can use [`Caller<'_, T>`](crate::Caller) to get access to 1943 /// [`StoreContextMut<'_, T>`](crate::StoreContextMut) and/or get access to 1944 /// `T` itself. This means that the [`Caller`] type can serve as a proxy to 1945 /// the original [`Store`](crate::Store) itself and is used to satisfy 1946 /// [`AsContext`] and [`AsContextMut`] bounds. 1947 /// 1948 /// * Second a [`Caller`] can be used as the name implies, learning about the 1949 /// caller's context, namely it's exported memory and exported functions. This 1950 /// allows functions which take pointers as arguments to easily read the 1951 /// memory the pointers point into, or if a function is expected to call 1952 /// malloc in the wasm module to reserve space for the output you can do that. 1953 /// 1954 /// Host functions which want access to [`Store`](crate::Store)-level state are 1955 /// recommended to use this type. 1956 pub struct Caller<'a, T: 'static> { 1957 pub(crate) store: StoreContextMut<'a, T>, 1958 caller: Instance, 1959 } 1960 1961 impl<T> Caller<'_, T> { 1962 fn sub_caller(&mut self) -> Caller<'_, T> { 1963 Caller { 1964 store: self.store.as_context_mut(), 1965 caller: self.caller, 1966 } 1967 } 1968 1969 /// Looks up an export from the caller's module by the `name` given. 1970 /// 1971 /// This is a low-level function that's typically used to implement passing 1972 /// of pointers or indices between core Wasm instances, where the callee 1973 /// needs to consult the caller's exports to perform memory management and 1974 /// resolve the references. 1975 /// 1976 /// For comparison, in components, the component model handles translating 1977 /// arguments from one component instance to another and managing memory, so 1978 /// that callees don't need to be aware of their callers, which promotes 1979 /// virtualizability of APIs. 1980 /// 1981 /// # Return 1982 /// 1983 /// If an export with the `name` provided was found, then it is returned as an 1984 /// `Extern`. There are a number of situations, however, where the export may not 1985 /// be available: 1986 /// 1987 /// * The caller instance may not have an export named `name` 1988 /// * There may not be a caller available, for example if `Func` was called 1989 /// directly from host code. 1990 /// 1991 /// It's recommended to take care when calling this API and gracefully 1992 /// handling a `None` return value. 1993 pub fn get_export(&mut self, name: &str) -> Option<Extern> { 1994 // All instances created have a `host_state` with a pointer pointing 1995 // back to themselves. If this caller doesn't have that `host_state` 1996 // then it probably means it was a host-created object like `Func::new` 1997 // which doesn't have any exports we want to return anyway. 1998 self.caller.get_export(&mut self.store, name) 1999 } 2000 2001 /// Looks up an exported [`Extern`] value by a [`ModuleExport`] value. 2002 /// 2003 /// This is similar to [`Self::get_export`] but uses a [`ModuleExport`] value to avoid 2004 /// string lookups where possible. [`ModuleExport`]s can be obtained by calling 2005 /// [`Module::get_export_index`] on the [`Module`] that an instance was instantiated with. 2006 /// 2007 /// This method will search the module for an export with a matching entity index and return 2008 /// the value, if found. 2009 /// 2010 /// Returns `None` if there was no export with a matching entity index. 2011 /// # Panics 2012 /// 2013 /// Panics if `store` does not own this instance. 2014 /// 2015 /// # Usage 2016 /// ``` 2017 /// use std::str; 2018 /// 2019 /// # use wasmtime::*; 2020 /// # fn main() -> Result<()> { 2021 /// # let mut store = Store::default(); 2022 /// 2023 /// let module = Module::new( 2024 /// store.engine(), 2025 /// r#" 2026 /// (module 2027 /// (import "" "" (func $log_str (param i32 i32))) 2028 /// (func (export "foo") 2029 /// i32.const 4 ;; ptr 2030 /// i32.const 13 ;; len 2031 /// call $log_str) 2032 /// (memory (export "memory") 1) 2033 /// (data (i32.const 4) "Hello, world!")) 2034 /// "#, 2035 /// )?; 2036 /// 2037 /// let Some(module_export) = module.get_export_index("memory") else { 2038 /// bail!("failed to find `memory` export in module"); 2039 /// }; 2040 /// 2041 /// let log_str = Func::wrap(&mut store, move |mut caller: Caller<'_, ()>, ptr: i32, len: i32| { 2042 /// let mem = match caller.get_module_export(&module_export) { 2043 /// Some(Extern::Memory(mem)) => mem, 2044 /// _ => bail!("failed to find host memory"), 2045 /// }; 2046 /// let data = mem.data(&caller) 2047 /// .get(ptr as u32 as usize..) 2048 /// .and_then(|arr| arr.get(..len as u32 as usize)); 2049 /// let string = match data { 2050 /// Some(data) => match str::from_utf8(data) { 2051 /// Ok(s) => s, 2052 /// Err(_) => bail!("invalid utf-8"), 2053 /// }, 2054 /// None => bail!("pointer/length out of bounds"), 2055 /// }; 2056 /// assert_eq!(string, "Hello, world!"); 2057 /// println!("{}", string); 2058 /// Ok(()) 2059 /// }); 2060 /// let instance = Instance::new(&mut store, &module, &[log_str.into()])?; 2061 /// let foo = instance.get_typed_func::<(), ()>(&mut store, "foo")?; 2062 /// foo.call(&mut store, ())?; 2063 /// # Ok(()) 2064 /// # } 2065 /// ``` 2066 pub fn get_module_export(&mut self, export: &ModuleExport) -> Option<Extern> { 2067 self.caller.get_module_export(&mut self.store, export) 2068 } 2069 2070 /// Access the underlying data owned by this `Store`. 2071 /// 2072 /// Same as [`Store::data`](crate::Store::data) 2073 pub fn data(&self) -> &T { 2074 self.store.data() 2075 } 2076 2077 /// Access the underlying data owned by this `Store`. 2078 /// 2079 /// Same as [`Store::data_mut`](crate::Store::data_mut) 2080 pub fn data_mut(&mut self) -> &mut T { 2081 self.store.data_mut() 2082 } 2083 2084 /// Returns the underlying [`Engine`] this store is connected to. 2085 pub fn engine(&self) -> &Engine { 2086 self.store.engine() 2087 } 2088 2089 /// Perform garbage collection. 2090 /// 2091 /// Same as [`Store::gc`](crate::Store::gc). 2092 #[cfg(feature = "gc")] 2093 pub fn gc(&mut self, why: Option<&crate::GcHeapOutOfMemory<()>>) { 2094 self.store.gc(why); 2095 } 2096 2097 /// Perform garbage collection asynchronously. 2098 /// 2099 /// Same as [`Store::gc_async`](crate::Store::gc_async). 2100 #[cfg(all(feature = "async", feature = "gc"))] 2101 pub async fn gc_async(&mut self, why: Option<&crate::GcHeapOutOfMemory<()>>) 2102 where 2103 T: Send + 'static, 2104 { 2105 self.store.gc_async(why).await; 2106 } 2107 2108 /// Returns the remaining fuel in the store. 2109 /// 2110 /// For more information see [`Store::get_fuel`](crate::Store::get_fuel) 2111 pub fn get_fuel(&self) -> Result<u64> { 2112 self.store.get_fuel() 2113 } 2114 2115 /// Set the amount of fuel in this store to be consumed when executing wasm code. 2116 /// 2117 /// For more information see [`Store::set_fuel`](crate::Store::set_fuel) 2118 pub fn set_fuel(&mut self, fuel: u64) -> Result<()> { 2119 self.store.set_fuel(fuel) 2120 } 2121 2122 /// Configures this `Store` to yield while executing futures every N units of fuel. 2123 /// 2124 /// For more information see 2125 /// [`Store::fuel_async_yield_interval`](crate::Store::fuel_async_yield_interval) 2126 pub fn fuel_async_yield_interval(&mut self, interval: Option<u64>) -> Result<()> { 2127 self.store.fuel_async_yield_interval(interval) 2128 } 2129 2130 /// Provide an object that views Wasm stack state, including Wasm 2131 /// VM-level values (locals and operand stack), when debugging is 2132 /// enabled. 2133 /// 2134 /// See ['Store::debug_frames`] for more details. 2135 #[cfg(feature = "debug")] 2136 pub fn debug_frames(&mut self) -> Option<crate::DebugFrameCursor<'_, T>> { 2137 self.store.as_context_mut().debug_frames() 2138 } 2139 } 2140 2141 impl<T: 'static> AsContext for Caller<'_, T> { 2142 type Data = T; 2143 fn as_context(&self) -> StoreContext<'_, T> { 2144 self.store.as_context() 2145 } 2146 } 2147 2148 impl<T: 'static> AsContextMut for Caller<'_, T> { 2149 fn as_context_mut(&mut self) -> StoreContextMut<'_, T> { 2150 self.store.as_context_mut() 2151 } 2152 } 2153 2154 impl<'a, T: 'static> From<Caller<'a, T>> for StoreContextMut<'a, T> { 2155 fn from(caller: Caller<'a, T>) -> Self { 2156 caller.store 2157 } 2158 } 2159 2160 /// Representation of a host-defined function. 2161 /// 2162 /// This is used for `Func::new` but also for `Linker`-defined functions. For 2163 /// `Func::new` this is stored within a `Store`, and for `Linker`-defined 2164 /// functions they wrap this up in `Arc` to enable shared ownership of this 2165 /// across many stores. 2166 /// 2167 /// Technically this structure needs a `<T>` type parameter to connect to the 2168 /// `Store<T>` itself, but that's an unsafe contract of using this for now 2169 /// rather than part of the struct type (to avoid `Func<T>` in the API). 2170 #[doc(hidden)] 2171 pub struct HostFunc { 2172 ctx: StoreBox<VMArrayCallHostFuncContext>, 2173 2174 // Stored to unregister this function's signature with the engine when this 2175 // is dropped. 2176 engine: Engine, 2177 } 2178 2179 // State stored inside a `VMArrayCallHostFuncContext`. 2180 struct HostFuncState<F> { 2181 // The actual host function. 2182 func: F, 2183 2184 // NB: We have to keep our `VMSharedTypeIndex` registered in the engine for 2185 // as long as this function exists. 2186 _ty: RegisteredType, 2187 } 2188 2189 impl core::fmt::Debug for HostFunc { 2190 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 2191 f.debug_struct("HostFunc").finish_non_exhaustive() 2192 } 2193 } 2194 2195 impl HostFunc { 2196 /// Requires that the signatuer in `HostContext` is already registered 2197 /// within `Engine`, which is done by `HostContext`'s constructors. 2198 /// 2199 /// This is an internal private constructor for this type intended to only 2200 /// be used by other constructors of this type below. 2201 fn new_raw(engine: &Engine, ctx: StoreBox<VMArrayCallHostFuncContext>) -> Self { 2202 HostFunc { 2203 ctx, 2204 engine: engine.clone(), 2205 } 2206 } 2207 2208 /// Constructor of a host function's `VMContext` for synchronous functions. 2209 /// 2210 /// This creates a `VMArrayCallHostFuncContext` which under the hood will 2211 /// be viewed as `VMContext` in the eventually created `VMFuncRef`. 2212 fn vmctx_sync<F, T>( 2213 engine: &Engine, 2214 ty: FuncType, 2215 func: F, 2216 ) -> StoreBox<VMArrayCallHostFuncContext> 2217 where 2218 F: Fn(Caller<'_, T>, &mut [MaybeUninit<ValRaw>]) -> Result<()> + Send + Sync + 'static, 2219 T: 'static, 2220 { 2221 assert!(ty.comes_from_same_engine(engine)); 2222 2223 unsafe { 2224 VMArrayCallHostFuncContext::new( 2225 Self::array_call_trampoline::<T, F>, 2226 ty.type_index(), 2227 Box::new(HostFuncState { 2228 func, 2229 _ty: ty.into_registered_type(), 2230 }), 2231 ) 2232 } 2233 } 2234 2235 /// Constructor of a host function's `VMContext` for asynchronous functions. 2236 /// 2237 /// This creates a `VMArrayCallHostFuncContext` which under the hood will 2238 /// be viewed as `VMContext` in the eventually created `VMFuncRef`. 2239 /// 2240 /// Note that `ctx: U` is forwarded directly to `func`. This is subtly 2241 /// different than closing over data in `F` because the future returned by 2242 /// `F` can't refer to any data it closes over. The purpose of `U` is to 2243 /// enable the returned future to be able to close over data generally 2244 /// captured in the closures generated here. 2245 #[cfg(feature = "async")] 2246 fn vmctx_async<F, T, U>( 2247 engine: &Engine, 2248 ty: FuncType, 2249 ctx: U, 2250 func: F, 2251 ) -> StoreBox<VMArrayCallHostFuncContext> 2252 where 2253 F: for<'a> Fn( 2254 Caller<'a, T>, 2255 &'a mut [MaybeUninit<ValRaw>], 2256 &'a U, 2257 ) -> Box<dyn Future<Output = Result<()>> + Send + 'a> 2258 + Send 2259 + Sync 2260 + 'static, 2261 T: 'static, 2262 U: Send + Sync + 'static, 2263 { 2264 // Eventually we want to remove `with_blocking` + `block_on` from 2265 // Wasmtime. For now this is the attempt to keep it as low-level as 2266 // possible. 2267 // 2268 // Generally it's a lie to run async things on top of sync things and 2269 // it's caused many headaches. For now it's the best that can be done. 2270 Self::vmctx_sync(engine, ty, move |Caller { store, caller }, args| { 2271 store.with_blocking(|store, cx| { 2272 cx.block_on(core::pin::Pin::from(func( 2273 Caller { store, caller }, 2274 args, 2275 &ctx, 2276 ))) 2277 })? 2278 }) 2279 } 2280 2281 /// Entrypoint of WebAssembly back into the host. 2282 /// 2283 /// This is the standard wasmtime "array call signature" which then 2284 /// delegates internally to the host. This assumes that `callee_vmctx` is a 2285 /// `VMArrayCallHostFuncContext` which was built above with 2286 /// `HostFuncState<F>` internally. This internal function is then used to 2287 /// dispatch based on the arguments. 2288 /// 2289 /// Details handled by this wrapper are: 2290 /// 2291 /// * Host panics are handled (`enter_host_from_wasm`) 2292 /// * `F` is loaded from `callee_vmctx` 2293 /// * `Caller` is constructed to pass to `F` 2294 /// * A GC LIFO scope is maintained around the execution of `F`. 2295 /// * Call hooks for entering/leaving the host are maintained. 2296 unsafe extern "C" fn array_call_trampoline<T, F>( 2297 callee_vmctx: NonNull<VMOpaqueContext>, 2298 caller_vmctx: NonNull<VMContext>, 2299 args: NonNull<ValRaw>, 2300 args_len: usize, 2301 ) -> bool 2302 where 2303 F: Fn(Caller<'_, T>, &mut [MaybeUninit<ValRaw>]) -> Result<()> + 'static, 2304 T: 'static, 2305 { 2306 let run = |store: &mut dyn crate::vm::VMStore, instance: InstanceId| { 2307 // SAFETY: correct usage of this trampoline requires correct 2308 // ascription of `T`, so it's the caller's responsibility to line 2309 // this up. 2310 let mut store = unsafe { store.unchecked_context_mut() }; 2311 2312 // Handle the entry call hook, with a corresponding exit call hook 2313 // below. 2314 store.0.call_hook(CallHook::CallingHost)?; 2315 2316 // SAFETY: this function itself requires that the `vmctx` is 2317 // valid to use here. 2318 let state = unsafe { 2319 let vmctx = VMArrayCallHostFuncContext::from_opaque(callee_vmctx); 2320 vmctx.as_ref().host_state() 2321 }; 2322 2323 // Double-check ourselves in debug mode, but we control the 2324 // `Any` here so an unsafe downcast should also work. 2325 // 2326 // SAFETY: this function is only usable with `F`. 2327 let state = unsafe { 2328 debug_assert!(state.is::<HostFuncState<F>>()); 2329 &*(state as *const _ as *const HostFuncState<F>) 2330 }; 2331 2332 let (gc_lifo_scope, ret) = { 2333 let gc_lifo_scope = store.0.gc_roots().enter_lifo_scope(); 2334 2335 let mut args = NonNull::slice_from_raw_parts(args.cast(), args_len); 2336 // SAFETY: it's a contract of this function itself that the values 2337 // provided are valid to view as a slice. 2338 let args = unsafe { args.as_mut() }; 2339 2340 let ret = (state.func)( 2341 Caller { 2342 caller: Instance::from_wasmtime(instance, store.0), 2343 store: store.as_context_mut(), 2344 }, 2345 args, 2346 ); 2347 2348 (gc_lifo_scope, ret) 2349 }; 2350 2351 store.0.exit_gc_lifo_scope(gc_lifo_scope); 2352 2353 // Note that if this returns a trap then `ret` is discarded 2354 // entirely. 2355 store.0.call_hook(CallHook::ReturningFromHost)?; 2356 2357 ret 2358 }; 2359 2360 // SAFETY: this is an entrypoint of wasm which requires correct type 2361 // ascription of `T` itself, meaning that this should be safe to call 2362 // both `enter_host_from_wasm` as well as `unchecked_context_mut`. 2363 unsafe { vm::Instance::enter_host_from_wasm(caller_vmctx, run) } 2364 } 2365 /// Analog of [`Func::new_unchecked`] 2366 /// 2367 /// # Panics 2368 /// 2369 /// Panics if the given function type is not associated with the given 2370 /// engine. 2371 /// 2372 /// # Safety 2373 /// 2374 /// The `func` provided must operate according to the `ty` provided to 2375 /// ensure it's reading the correctly-typed parameters and writing the 2376 /// correctly-typed results. 2377 pub unsafe fn new_unchecked<T>( 2378 engine: &Engine, 2379 ty: FuncType, 2380 func: impl Fn(Caller<'_, T>, &mut [MaybeUninit<ValRaw>]) -> Result<()> + Send + Sync + 'static, 2381 ) -> Self 2382 where 2383 T: 'static, 2384 { 2385 HostFunc::new_raw(engine, Self::vmctx_sync(engine, ty, func)) 2386 } 2387 2388 /// Analog of [`Func::new`] 2389 /// 2390 /// # Panics 2391 /// 2392 /// Panics if the given function type is not associated with the given 2393 /// engine. 2394 pub fn new<T>( 2395 engine: &Engine, 2396 ty: FuncType, 2397 func: impl Fn(Caller<'_, T>, &[Val], &mut [Val]) -> Result<()> + Send + Sync + 'static, 2398 ) -> Self 2399 where 2400 T: 'static, 2401 { 2402 // NB: this is "duplicated" below in `new_async`, so try to keep the 2403 // two in sync. 2404 HostFunc::new_raw( 2405 engine, 2406 Self::vmctx_sync(engine, ty.clone(), move |mut caller, values| { 2407 // SAFETY: Wasmtime in general provides the guarantee that 2408 // `values` matches `ty`, so this should be safe. 2409 let mut vec = unsafe { Self::load_untyped_params(caller.store.0, &ty, values) }; 2410 let (params, results) = vec.split_at_mut(ty.params().len()); 2411 func(caller.sub_caller(), params, results)?; 2412 Self::store_untyped_results(caller.store, &ty, vec, values) 2413 }), 2414 ) 2415 } 2416 2417 /// Analog of [`Func::new_async`] 2418 /// 2419 /// # Panics 2420 /// 2421 /// Panics if the given function type is not associated with the given 2422 /// engine. 2423 #[cfg(feature = "async")] 2424 pub fn new_async<T, F>(engine: &Engine, ty: FuncType, func: F) -> Self 2425 where 2426 F: for<'a> Fn( 2427 Caller<'a, T>, 2428 &'a [Val], 2429 &'a mut [Val], 2430 ) -> Box<dyn Future<Output = Result<()>> + Send + 'a> 2431 + Send 2432 + Sync 2433 + 'static, 2434 T: Send + 'static, 2435 { 2436 // NB: this is "duplicated" above in `new`, so try to keep the two in 2437 // sync. 2438 HostFunc::new_raw( 2439 engine, 2440 Self::vmctx_async( 2441 engine, 2442 ty.clone(), 2443 (ty, func), 2444 move |mut caller, values, (ty, func)| { 2445 Box::new(async move { 2446 // SAFETY: Wasmtime in general provides the guarantee that 2447 // `values` matches `ty`, so this should be safe. 2448 let mut vec = 2449 unsafe { Self::load_untyped_params(caller.store.0, &ty, values) }; 2450 let (params, results) = vec.split_at_mut(ty.params().len()); 2451 core::pin::Pin::from(func(caller.sub_caller(), params, results)).await?; 2452 Self::store_untyped_results(caller.store, &ty, vec, values) 2453 }) 2454 }, 2455 ), 2456 ) 2457 } 2458 2459 /// Loads the the parameters of `ty` from `params` into a vector. 2460 /// 2461 /// This additionally pushes space onto the vector for all results to split 2462 /// the vector into params/results halves. 2463 /// 2464 /// # Safety 2465 /// 2466 /// Requires that `params` matches the parameters loaded by `P`. 2467 unsafe fn load_untyped_params( 2468 store: &mut StoreOpaque, 2469 ty: &FuncType, 2470 params: &mut [MaybeUninit<ValRaw>], 2471 ) -> Vec<Val> { 2472 let mut val_vec = store.take_hostcall_val_storage(); 2473 debug_assert!(val_vec.is_empty()); 2474 let nparams = ty.params().len(); 2475 val_vec.reserve(nparams + ty.results().len()); 2476 let mut store = AutoAssertNoGc::new(store); 2477 for (i, ty) in ty.params().enumerate() { 2478 val_vec.push(unsafe { Val::_from_raw(&mut store, params[i].assume_init(), &ty) }) 2479 } 2480 2481 val_vec.extend((0..ty.results().len()).map(|_| Val::null_func_ref())); 2482 val_vec 2483 } 2484 2485 /// Stores the results, at the end of `args_then_results` according to `ty`, 2486 /// into `storage`. 2487 fn store_untyped_results<T>( 2488 mut store: StoreContextMut<'_, T>, 2489 ty: &FuncType, 2490 mut args_then_results: Vec<Val>, 2491 storage: &mut [MaybeUninit<ValRaw>], 2492 ) -> Result<()> { 2493 // Unlike our arguments we need to dynamically check that the return 2494 // values produced are correct. There could be a bug in `func` that 2495 // produces the wrong number, wrong types, or wrong stores of 2496 // values, and we need to catch that here. 2497 let results = &args_then_results[ty.params().len()..]; 2498 for (i, (ret, ty)) in results.iter().zip(ty.results()).enumerate() { 2499 ret.ensure_matches_ty(store.0, &ty) 2500 .context("function attempted to return an incompatible value")?; 2501 storage[i].write(ret.to_raw(store.as_context_mut())?); 2502 } 2503 2504 // Restore our `val_vec` back into the store so it's usable for the next 2505 // hostcall to reuse our own storage. 2506 args_then_results.truncate(0); 2507 store.0.save_hostcall_val_storage(args_then_results); 2508 Ok(()) 2509 } 2510 2511 /// Analog of [`Func::wrap`] 2512 pub fn wrap<T, F, P, R>(engine: &Engine, func: F) -> Self 2513 where 2514 F: Fn(Caller<'_, T>, P) -> R + Send + Sync + 'static, 2515 P: WasmTyList, 2516 R: WasmRet, 2517 T: 'static, 2518 { 2519 // NB: this entire function is "duplicated" below in `wrap_async`, so 2520 // try to keep the two in sync. 2521 let ty = R::func_type(engine, None::<ValType>.into_iter().chain(P::valtypes())); 2522 2523 let ctx = Self::vmctx_sync(engine, ty, move |mut caller, args| { 2524 // SAFETY: `args` matching `ty` is provided by `HostFunc` and 2525 // wasmtime's ambient correctness. 2526 let params = unsafe { Self::load_typed_params(caller.store.0, args) }; 2527 let ret = func(caller.sub_caller(), params).into_fallible(); 2528 // SAFETY: `args` matching `ty` is provided by `HostFunc` and 2529 // wasmtime's ambient correctness. 2530 unsafe { Self::store_typed_results(caller.store.0, ret, args) } 2531 }); 2532 HostFunc::new_raw(engine, ctx) 2533 } 2534 2535 /// Analog of [`Func::wrap_async`] 2536 #[cfg(feature = "async")] 2537 pub fn wrap_async<T, F, P, R>(engine: &Engine, func: F) -> Self 2538 where 2539 F: for<'a> Fn(Caller<'a, T>, P) -> Box<dyn Future<Output = R> + Send + 'a> 2540 + Send 2541 + Sync 2542 + 'static, 2543 P: WasmTyList, 2544 R: WasmRet, 2545 T: Send + 'static, 2546 { 2547 // NB: this entire function is "duplicated" above in `wrap`, so try to 2548 // keep the two in sync. 2549 let ty = R::func_type(engine, None::<ValType>.into_iter().chain(P::valtypes())); 2550 2551 let ctx = Self::vmctx_async(engine, ty, func, move |mut caller, args, func| { 2552 Box::new(async move { 2553 // SAFETY: `args` matching `ty` is provided by `HostFunc` and 2554 // wasmtime's ambient correctness. 2555 let params = unsafe { Self::load_typed_params(caller.store.0, args) }; 2556 let ret = core::pin::Pin::from(func(caller.sub_caller(), params)).await; 2557 // SAFETY: `args` matching `ty` is provided by `HostFunc` and 2558 // wasmtime's ambient correctness. 2559 unsafe { Self::store_typed_results(caller.store.0, ret.into_fallible(), args) } 2560 }) 2561 }); 2562 HostFunc::new_raw(engine, ctx) 2563 } 2564 2565 /// Loads the typed parameters from `params` 2566 /// 2567 /// # Safety 2568 /// 2569 /// Requires that `params` matches the parameters loaded by `P`. 2570 unsafe fn load_typed_params<P>(store: &mut StoreOpaque, params: &mut [MaybeUninit<ValRaw>]) -> P 2571 where 2572 P: WasmTyList, 2573 { 2574 let mut store = if P::may_gc() { 2575 AutoAssertNoGc::new(store) 2576 } else { 2577 unsafe { AutoAssertNoGc::disabled(store) } 2578 }; 2579 // SAFETY: this function's own safety contract is the same as `P::load`. 2580 unsafe { P::load(&mut store, params) } 2581 } 2582 2583 /// Stores the results of `R` into the array provided. 2584 /// 2585 /// # Safety 2586 /// 2587 /// Requires that `ret` matches the result `storage` space. See `WasmRet` 2588 /// for more safety info. 2589 unsafe fn store_typed_results<R>( 2590 store: &mut StoreOpaque, 2591 ret: R, 2592 storage: &mut [MaybeUninit<ValRaw>], 2593 ) -> Result<()> 2594 where 2595 R: WasmRet, 2596 { 2597 ensure!( 2598 ret.compatible_with_store(store), 2599 "host function attempted to return cross-`Store` value to Wasm", 2600 ); 2601 2602 let mut store = if R::may_gc() { 2603 AutoAssertNoGc::new(store) 2604 } else { 2605 unsafe { AutoAssertNoGc::disabled(store) } 2606 }; 2607 // SAFETY: this safety contract is the same as this own function's 2608 // safety contract. 2609 unsafe { 2610 ret.store(&mut store, storage)?; 2611 } 2612 Ok(()) 2613 } 2614 2615 /// Inserts this `HostFunc` into a `Store`, returning the `Func` pointing to 2616 /// it. 2617 /// 2618 /// # Unsafety 2619 /// 2620 /// Can only be inserted into stores with a matching `T` relative to when 2621 /// this `HostFunc` was first created. 2622 pub unsafe fn to_func(self: &Arc<Self>, store: &mut StoreOpaque) -> Func { 2623 self.validate_store(store); 2624 let (funcrefs, modules) = store.func_refs_and_modules(); 2625 let funcref = funcrefs.push_arc_host(self.clone(), modules); 2626 // SAFETY: this funcref was just pushed within the store, so it's safe 2627 // to say this store owns it. 2628 unsafe { Func::from_vm_func_ref(store.id(), funcref) } 2629 } 2630 2631 /// Inserts this `HostFunc` into a `Store`, returning the `Func` pointing to 2632 /// it. 2633 /// 2634 /// This function is similar to, but not equivalent, to `HostFunc::to_func`. 2635 /// Notably this function requires that the `Arc<Self>` pointer is otherwise 2636 /// rooted within the `StoreOpaque` via another means. When in doubt use 2637 /// `to_func` above as it's safer. 2638 /// 2639 /// # Unsafety 2640 /// 2641 /// Can only be inserted into stores with a matching `T` relative to when 2642 /// this `HostFunc` was first created. 2643 /// 2644 /// Additionally the `&Arc<Self>` is not cloned in this function. Instead a 2645 /// raw pointer to `Self` is stored within the `Store` for this function. 2646 /// The caller must arrange for the `Arc<Self>` to be "rooted" in the store 2647 /// provided via another means, probably by pushing to 2648 /// `StoreOpaque::rooted_host_funcs`. 2649 /// 2650 /// Similarly, the caller must arrange for `rooted_func_ref` to be rooted in 2651 /// the same store and additionally be a valid pointer. 2652 pub unsafe fn to_func_store_rooted( 2653 self: &Arc<Self>, 2654 store: &mut StoreOpaque, 2655 rooted_func_ref: Option<NonNull<VMFuncRef>>, 2656 ) -> Func { 2657 self.validate_store(store); 2658 2659 match rooted_func_ref { 2660 Some(funcref) => { 2661 // SAFETY: it's a contract of this function itself that 2662 // `funcref` is safe to read. 2663 unsafe { 2664 debug_assert!(funcref.as_ref().wasm_call.is_some()); 2665 } 2666 // SAFETY: it's a contract of this function that `funcref` is 2667 // owned by `store`. 2668 unsafe { Func::from_vm_func_ref(store.id(), funcref) } 2669 } 2670 None => { 2671 debug_assert!(self.func_ref().wasm_call.is_some()); 2672 2673 // SAFETY: it's an unsafe contract of this function that we are 2674 // rooted within the store to say that the store owns a copy of 2675 // this funcref. 2676 unsafe { Func::from_vm_func_ref(store.id(), self.func_ref().into()) } 2677 } 2678 } 2679 } 2680 2681 /// Same as [`HostFunc::to_func`], different ownership. 2682 unsafe fn into_func(self, store: &mut StoreOpaque) -> Func { 2683 self.validate_store(store); 2684 let (funcrefs, modules) = store.func_refs_and_modules(); 2685 let funcref = funcrefs.push_box_host(Box::new(self), modules); 2686 // SAFETY: this funcref was just pushed within `store`, so it's safe to 2687 // say it's owned by the store's id. 2688 unsafe { Func::from_vm_func_ref(store.id(), funcref) } 2689 } 2690 2691 fn validate_store(&self, store: &mut StoreOpaque) { 2692 // This assert is required to ensure that we can indeed safely insert 2693 // `self` into the `store` provided, otherwise the type information we 2694 // have listed won't be correct. This is possible to hit with the public 2695 // API of Wasmtime, and should be documented in relevant functions. 2696 assert!( 2697 Engine::same(&self.engine, store.engine()), 2698 "cannot use a store with a different engine than a linker was created with", 2699 ); 2700 } 2701 2702 pub(crate) fn sig_index(&self) -> VMSharedTypeIndex { 2703 self.func_ref().type_index 2704 } 2705 2706 pub(crate) fn func_ref(&self) -> &VMFuncRef { 2707 unsafe { self.ctx.get().as_ref().func_ref() } 2708 } 2709 } 2710 2711 #[cfg(test)] 2712 mod tests { 2713 use super::*; 2714 use crate::{Module, Store}; 2715 2716 #[test] 2717 #[cfg_attr(miri, ignore)] 2718 fn hash_key_is_stable_across_duplicate_store_data_entries() -> Result<()> { 2719 let mut store = Store::<()>::default(); 2720 let module = Module::new( 2721 store.engine(), 2722 r#" 2723 (module 2724 (func (export "f") 2725 nop 2726 ) 2727 ) 2728 "#, 2729 )?; 2730 let instance = Instance::new(&mut store, &module, &[])?; 2731 2732 // Each time we `get_func`, we call `Func::from_wasmtime` which adds a 2733 // new entry to `StoreData`, so `f1` and `f2` will have different 2734 // indices into `StoreData`. 2735 let f1 = instance.get_func(&mut store, "f").unwrap(); 2736 let f2 = instance.get_func(&mut store, "f").unwrap(); 2737 2738 // But their hash keys are the same. 2739 assert!( 2740 f1.hash_key(&mut store.as_context_mut().0) 2741 == f2.hash_key(&mut store.as_context_mut().0) 2742 ); 2743 2744 // But the hash keys are different from different funcs. 2745 let instance2 = Instance::new(&mut store, &module, &[])?; 2746 let f3 = instance2.get_func(&mut store, "f").unwrap(); 2747 assert!( 2748 f1.hash_key(&mut store.as_context_mut().0) 2749 != f3.hash_key(&mut store.as_context_mut().0) 2750 ); 2751 2752 Ok(()) 2753 } 2754 } 2755