1 use crate::linker::{Definition, DefinitionType}; 2 use crate::prelude::*; 3 use crate::runtime::vm::{ 4 self, Imports, ModuleRuntimeInfo, VMFuncRef, VMFunctionImport, VMGlobalImport, VMMemoryImport, 5 VMStore, VMTableImport, VMTagImport, 6 }; 7 use crate::store::{ 8 AllocateInstanceKind, InstanceId, StoreInstanceId, StoreOpaque, StoreResourceLimiter, 9 }; 10 use crate::types::matching; 11 use crate::{ 12 AsContextMut, Engine, Export, Extern, Func, Global, Memory, Module, ModuleExport, SharedMemory, 13 StoreContext, StoreContextMut, Table, Tag, TypedFunc, 14 }; 15 use alloc::sync::Arc; 16 use core::ptr::NonNull; 17 use wasmparser::WasmFeatures; 18 use wasmtime_environ::{ 19 EntityIndex, EntityType, FuncIndex, GlobalIndex, MemoryIndex, PrimaryMap, TableIndex, TagIndex, 20 TypeTrace, 21 }; 22 23 /// An instantiated WebAssembly module. 24 /// 25 /// This type represents the instantiation of a [`Module`]. Once instantiated 26 /// you can access the [`exports`](Instance::exports) which are of type 27 /// [`Extern`] and provide the ability to call functions, set globals, read 28 /// memory, etc. When interacting with any wasm code you'll want to make an 29 /// [`Instance`] to call any code or execute anything. 30 /// 31 /// Instances are owned by a [`Store`](crate::Store) which is passed in at 32 /// creation time. It's recommended to create instances with 33 /// [`Linker::instantiate`](crate::Linker::instantiate) or similar 34 /// [`Linker`](crate::Linker) methods, but a more low-level constructor is also 35 /// available as [`Instance::new`]. 36 #[derive(Copy, Clone, Debug, PartialEq, Eq)] 37 #[repr(C)] 38 pub struct Instance { 39 pub(crate) id: StoreInstanceId, 40 } 41 42 // Double-check that the C representation in `instance.h` matches our in-Rust 43 // representation here in terms of size/alignment/etc. 44 const _: () = { 45 #[repr(C)] 46 struct C(u64, usize); 47 assert!(core::mem::size_of::<C>() == core::mem::size_of::<Instance>()); 48 assert!(core::mem::align_of::<C>() == core::mem::align_of::<Instance>()); 49 assert!(core::mem::offset_of!(Instance, id) == 0); 50 }; 51 52 impl Instance { 53 /// Creates a new [`Instance`] from the previously compiled [`Module`] and 54 /// list of `imports` specified. 55 /// 56 /// This method instantiates the `module` provided with the `imports`, 57 /// following the procedure in the [core specification][inst] to 58 /// instantiate. Instantiation can fail for a number of reasons (many 59 /// specified below), but if successful the `start` function will be 60 /// automatically run (if specified in the `module`) and then the 61 /// [`Instance`] will be returned. 62 /// 63 /// Per the WebAssembly spec, instantiation includes running the module's 64 /// start function, if it has one (not to be confused with the `_start` 65 /// function, which is not run). 66 /// 67 /// Note that this is a low-level function that just performs an 68 /// instantiation. See the [`Linker`](crate::Linker) struct for an API which 69 /// provides a convenient way to link imports and provides automatic Command 70 /// and Reactor behavior. 71 /// 72 /// ## Providing Imports 73 /// 74 /// The entries in the list of `imports` are intended to correspond 1:1 75 /// with the list of imports returned by [`Module::imports`]. Before 76 /// calling [`Instance::new`] you'll want to inspect the return value of 77 /// [`Module::imports`] and, for each import type, create an [`Extern`] 78 /// which corresponds to that type. These [`Extern`] values are all then 79 /// collected into a list and passed to this function. 80 /// 81 /// Note that this function is intentionally relatively low level. For an 82 /// easier time passing imports by doing name-based resolution it's 83 /// recommended to instead use the [`Linker`](crate::Linker) type. 84 /// 85 /// ## Errors 86 /// 87 /// This function can fail for a number of reasons, including, but not 88 /// limited to: 89 /// 90 /// * The number of `imports` provided doesn't match the number of imports 91 /// returned by the `module`'s [`Module::imports`] method. 92 /// * The type of any [`Extern`] doesn't match the corresponding 93 /// [`ExternType`] entry that it maps to. 94 /// * The `start` function in the instance, if present, traps. 95 /// * Module/instance resource limits are exceeded. 96 /// 97 /// When instantiation fails it's recommended to inspect the return value to 98 /// see why it failed, or bubble it upwards. If you'd like to specifically 99 /// check for trap errors, you can use `error.downcast::<Trap>()`. For more 100 /// about error handling see the [`Trap`] documentation. 101 /// 102 /// [`Trap`]: crate::Trap 103 /// 104 /// # Panics 105 /// 106 /// This function will panic if called with a store associated with a 107 /// [`asynchronous config`](crate::Config::async_support). This function 108 /// will also panic if any [`Extern`] supplied is not owned by `store`. 109 /// 110 /// [inst]: https://webassembly.github.io/spec/core/exec/modules.html#exec-instantiation 111 /// [`ExternType`]: crate::ExternType 112 pub fn new( 113 mut store: impl AsContextMut, 114 module: &Module, 115 imports: &[Extern], 116 ) -> Result<Instance> { 117 let mut store = store.as_context_mut(); 118 let imports = Instance::typecheck_externs(store.0, module, imports)?; 119 // Note that the unsafety here should be satisfied by the call to 120 // `typecheck_externs` above which satisfies the condition that all 121 // the imports are valid for this module. 122 assert!(!store.0.async_support()); 123 vm::assert_ready(unsafe { Instance::new_started(&mut store, module, imports.as_ref()) }) 124 } 125 126 /// Same as [`Instance::new`], except for usage in [asynchronous stores]. 127 /// 128 /// For more details about this function see the documentation on 129 /// [`Instance::new`]. The only difference between these two methods is that 130 /// this one will asynchronously invoke the wasm start function in case it 131 /// calls any imported function which is an asynchronous host function (e.g. 132 /// created with [`Func::new_async`](crate::Func::new_async). 133 /// 134 /// # Panics 135 /// 136 /// This function will panic if called with a store associated with a 137 /// [`synchronous config`](crate::Config::new). This is only compatible with 138 /// stores associated with an [`asynchronous 139 /// config`](crate::Config::async_support). 140 /// 141 /// This function will also panic, like [`Instance::new`], if any [`Extern`] 142 /// specified does not belong to `store`. 143 /// 144 /// # Examples 145 /// 146 /// An example of using this function: 147 /// 148 /// ``` 149 /// use wasmtime::{Result, Store, Engine, Config, Module, Instance}; 150 /// 151 /// #[tokio::main] 152 /// async fn main() -> Result<()> { 153 /// let mut config = Config::new(); 154 /// config.async_support(true); 155 /// let engine = Engine::new(&config)?; 156 /// 157 /// // For this example, a module with no imports is being used hence 158 /// // the empty array to `Instance::new_async`. 159 /// let module = Module::new(&engine, "(module)")?; 160 /// let mut store = Store::new(&engine, ()); 161 /// let instance = Instance::new_async(&mut store, &module, &[]).await?; 162 /// 163 /// // ... use `instance` and exports and such ... 164 /// 165 /// Ok(()) 166 /// } 167 /// ``` 168 /// 169 /// Note, though, that the future returned from this function is only 170 /// `Send` if the store's own data is `Send` meaning that this does not 171 /// compile for example: 172 /// 173 /// ```compile_fail 174 /// use wasmtime::{Result, Store, Engine, Config, Module, Instance}; 175 /// use std::rc::Rc; 176 /// 177 /// #[tokio::main] 178 /// async fn main() -> Result<()> { 179 /// let mut config = Config::new(); 180 /// config.async_support(true); 181 /// let engine = Engine::new(&config)?; 182 /// 183 /// let module = Module::new(&engine, "(module)")?; 184 /// 185 /// // Note that `Rc<()>` is NOT `Send`, which is what many future 186 /// // runtimes require and below will cause a failure. 187 /// let mut store = Store::new(&engine, Rc::new(())); 188 /// 189 /// // Compile failure because `Store<Rc<()>>` is not `Send` 190 /// assert_send(Instance::new_async(&mut store, &module, &[])).await?; 191 /// 192 /// Ok(()) 193 /// } 194 /// 195 /// fn assert_send<T: Send>(t: T) -> T { t } 196 /// ``` 197 #[cfg(feature = "async")] 198 pub async fn new_async( 199 mut store: impl AsContextMut, 200 module: &Module, 201 imports: &[Extern], 202 ) -> Result<Instance> { 203 let mut store = store.as_context_mut(); 204 let imports = Instance::typecheck_externs(store.0, module, imports)?; 205 // See `new` for notes on this unsafety 206 unsafe { Instance::new_started(&mut store, module, imports.as_ref()).await } 207 } 208 209 fn typecheck_externs( 210 store: &mut StoreOpaque, 211 module: &Module, 212 imports: &[Extern], 213 ) -> Result<OwnedImports> { 214 for import in imports { 215 if !import.comes_from_same_store(store) { 216 bail!("cross-`Store` instantiation is not currently supported"); 217 } 218 } 219 220 typecheck(module, imports, |cx, ty, item| { 221 let item = DefinitionType::from(store, item); 222 cx.definition(ty, &item) 223 })?; 224 225 // When pushing functions into `OwnedImports` it's required that their 226 // `wasm_call` fields are all filled out. This `module` is guaranteed 227 // to have any trampolines necessary for functions so register the 228 // module with the store and then attempt to fill out any outstanding 229 // holes. 230 // 231 // Note that under normal operation this shouldn't do much as the list 232 // of funcs-with-holes should generally be empty. As a result the 233 // process of filling this out is not super optimized at this point. 234 store.register_module(module)?; 235 let (funcrefs, modules) = store.func_refs_and_modules(); 236 funcrefs.fill(modules); 237 238 let mut owned_imports = OwnedImports::new(module); 239 for import in imports { 240 owned_imports.push(import, store); 241 } 242 Ok(owned_imports) 243 } 244 245 /// Internal function to create an instance and run the start function. 246 /// 247 /// This function's unsafety is the same as `Instance::new_raw`. 248 pub(crate) async unsafe fn new_started<T>( 249 store: &mut StoreContextMut<'_, T>, 250 module: &Module, 251 imports: Imports<'_>, 252 ) -> Result<Instance> { 253 let (instance, start) = { 254 let (mut limiter, store) = store.0.resource_limiter_and_store_opaque(); 255 // SAFETY: the safety contract of `new_raw` is the same as this 256 // function. 257 unsafe { Instance::new_raw(store, limiter.as_mut(), module, imports).await? } 258 }; 259 if let Some(start) = start { 260 if store.0.async_support() { 261 #[cfg(feature = "async")] 262 { 263 store 264 .on_fiber(|store| instance.start_raw(store, start)) 265 .await??; 266 } 267 #[cfg(not(feature = "async"))] 268 unreachable!(); 269 } else { 270 instance.start_raw(store, start)?; 271 } 272 } 273 Ok(instance) 274 } 275 276 /// Internal function to create an instance which doesn't have its `start` 277 /// function run yet. 278 /// 279 /// This is not intended to be exposed from Wasmtime, it's intended to 280 /// refactor out common code from `new_started` and `new_started_async`. 281 /// 282 /// Note that this step needs to be run on a fiber in async mode even 283 /// though it doesn't do any blocking work because an async resource 284 /// limiter may need to yield. 285 /// 286 /// # Unsafety 287 /// 288 /// This method is unsafe because it does not type-check the `imports` 289 /// provided. The `imports` provided must be suitable for the module 290 /// provided as well. 291 async unsafe fn new_raw( 292 store: &mut StoreOpaque, 293 mut limiter: Option<&mut StoreResourceLimiter<'_>>, 294 module: &Module, 295 imports: Imports<'_>, 296 ) -> Result<(Instance, Option<FuncIndex>)> { 297 if !Engine::same(store.engine(), module.engine()) { 298 bail!("cross-`Engine` instantiation is not currently supported"); 299 } 300 store.bump_resource_counts(module)?; 301 302 // Allocate the GC heap, if necessary. 303 if module.env_module().needs_gc_heap { 304 store.ensure_gc_store(limiter.as_deref_mut()).await?; 305 } 306 307 let compiled_module = module.compiled_module(); 308 309 // Register the module just before instantiation to ensure we keep the module 310 // properly referenced while in use by the store. 311 let module_id = store.register_module(module)?; 312 313 // The first thing we do is issue an instance allocation request 314 // to the instance allocator. This, on success, will give us an 315 // instance handle. 316 // 317 // SAFETY: this module, by construction, was already validated within 318 // the store. 319 let id = unsafe { 320 store 321 .allocate_instance( 322 limiter.as_deref_mut(), 323 AllocateInstanceKind::Module(module_id), 324 &ModuleRuntimeInfo::Module(module.clone()), 325 imports, 326 ) 327 .await? 328 }; 329 330 // Additionally, before we start doing fallible instantiation, we 331 // do one more step which is to insert an `InstanceData` 332 // corresponding to this instance. This `InstanceData` can be used 333 // via `Caller::get_export` if our instance's state "leaks" into 334 // other instances, even if we don't return successfully from this 335 // function. 336 // 337 // We don't actually load all exports from the instance at this 338 // time, instead preferring to lazily load them as they're demanded. 339 // For module/instance exports, though, those aren't actually 340 // stored in the instance handle so we need to immediately handle 341 // those here. 342 let instance = Instance::from_wasmtime(id, store); 343 344 // Now that we've recorded all information we need to about this 345 // instance within a `Store` we can start performing fallible 346 // initialization. Note that we still defer the `start` function to 347 // later since that may need to run asynchronously. 348 // 349 // If this returns an error (or if the start function traps) then 350 // any other initialization which may have succeeded which placed 351 // items from this instance into other instances should be ok when 352 // those items are loaded and run we'll have all the metadata to 353 // look at them. 354 let bulk_memory = store 355 .engine() 356 .features() 357 .contains(WasmFeatures::BULK_MEMORY); 358 359 vm::initialize_instance(store, limiter, id, compiled_module.module(), bulk_memory).await?; 360 361 Ok((instance, compiled_module.module().start_func)) 362 } 363 364 pub(crate) fn from_wasmtime(id: InstanceId, store: &mut StoreOpaque) -> Instance { 365 Instance { 366 id: StoreInstanceId::new(store.id(), id), 367 } 368 } 369 370 fn start_raw<T>(&self, store: &mut StoreContextMut<'_, T>, start: FuncIndex) -> Result<()> { 371 // If a start function is present, invoke it. Make sure we use all the 372 // trap-handling configuration in `store` as well. 373 let store_id = store.0.id(); 374 let (mut instance, registry) = self.id.get_mut_and_module_registry(store.0); 375 // SAFETY: the `store_id` is the id of the store that owns this 376 // instance and any function stored within the instance. 377 let f = unsafe { 378 instance 379 .as_mut() 380 .get_exported_func(registry, store_id, start) 381 }; 382 let caller_vmctx = instance.vmctx(); 383 unsafe { 384 let funcref = f.vm_func_ref(store.0); 385 super::func::invoke_wasm_and_catch_traps(store, |_default_caller, vm| { 386 VMFuncRef::array_call(funcref, vm, caller_vmctx, NonNull::from(&mut [])) 387 })?; 388 } 389 Ok(()) 390 } 391 392 /// Get this instance's module. 393 pub fn module<'a, T: 'static>(&self, store: impl Into<StoreContext<'a, T>>) -> &'a Module { 394 self._module(store.into().0) 395 } 396 397 pub(crate) fn _module<'a>(&self, store: &'a StoreOpaque) -> &'a Module { 398 store.module_for_instance(self.id).unwrap() 399 } 400 401 /// Returns the list of exported items from this [`Instance`]. 402 /// 403 /// # Panics 404 /// 405 /// Panics if `store` does not own this instance. 406 pub fn exports<'a, T: 'static>( 407 &'a self, 408 store: impl Into<StoreContextMut<'a, T>>, 409 ) -> impl ExactSizeIterator<Item = Export<'a>> + 'a { 410 self._exports(store.into().0) 411 } 412 413 fn _exports<'a>( 414 &'a self, 415 store: &'a mut StoreOpaque, 416 ) -> impl ExactSizeIterator<Item = Export<'a>> + 'a { 417 let module = store[self.id].env_module().clone(); 418 let mut items = Vec::new(); 419 for (_name, entity) in module.exports.iter() { 420 items.push(self._get_export(store, *entity)); 421 } 422 store[self.id] 423 .env_module() 424 .exports 425 .iter() 426 .zip(items) 427 .map(|((name, _), item)| Export::new(name, item)) 428 } 429 430 /// Looks up an exported [`Extern`] value by name. 431 /// 432 /// This method will search the module for an export named `name` and return 433 /// the value, if found. 434 /// 435 /// Returns `None` if there was no export named `name`. 436 /// 437 /// # Panics 438 /// 439 /// Panics if `store` does not own this instance. 440 /// 441 /// # Why does `get_export` take a mutable context? 442 /// 443 /// This method requires a mutable context because an instance's exports are 444 /// lazily populated, and we cache them as they are accessed. This makes 445 /// instantiating a module faster, but also means this method requires a 446 /// mutable context. 447 pub fn get_export(&self, mut store: impl AsContextMut, name: &str) -> Option<Extern> { 448 let store = store.as_context_mut().0; 449 let entity = *store[self.id].env_module().exports.get(name)?; 450 Some(self._get_export(store, entity)) 451 } 452 453 /// Looks up an exported [`Extern`] value by a [`ModuleExport`] value. 454 /// 455 /// This is similar to [`Instance::get_export`] but uses a [`ModuleExport`] value to avoid 456 /// string lookups where possible. [`ModuleExport`]s can be obtained by calling 457 /// [`Module::get_export_index`] on the [`Module`] that this instance was instantiated with. 458 /// 459 /// This method will search the module for an export with a matching entity index and return 460 /// the value, if found. 461 /// 462 /// Returns `None` if there was no export with a matching entity index. 463 /// 464 /// # Panics 465 /// 466 /// Panics if `store` does not own this instance. 467 pub fn get_module_export( 468 &self, 469 mut store: impl AsContextMut, 470 export: &ModuleExport, 471 ) -> Option<Extern> { 472 let store = store.as_context_mut().0; 473 474 // Verify the `ModuleExport` matches the module used in this instance. 475 if self._module(store).id() != export.module { 476 return None; 477 } 478 479 Some(self._get_export(store, export.entity)) 480 } 481 482 fn _get_export(&self, store: &mut StoreOpaque, entity: EntityIndex) -> Extern { 483 let id = store.id(); 484 // SAFETY: the store `id` owns this instance and all exports contained 485 // within. 486 let export = unsafe { 487 let (instance, registry) = self.id.get_mut_and_module_registry(store); 488 instance.get_export_by_index_mut(registry, id, entity) 489 }; 490 Extern::from_wasmtime_export(export, store) 491 } 492 493 /// Looks up an exported [`Func`] value by name. 494 /// 495 /// Returns `None` if there was no export named `name`, or if there was but 496 /// it wasn't a function. 497 /// 498 /// # Panics 499 /// 500 /// Panics if `store` does not own this instance. 501 pub fn get_func(&self, store: impl AsContextMut, name: &str) -> Option<Func> { 502 self.get_export(store, name)?.into_func() 503 } 504 505 /// Looks up an exported [`Func`] value by name and with its type. 506 /// 507 /// This function is a convenience wrapper over [`Instance::get_func`] and 508 /// [`Func::typed`]. For more information see the linked documentation. 509 /// 510 /// Returns an error if `name` isn't a function export or if the export's 511 /// type did not match `Params` or `Results` 512 /// 513 /// # Panics 514 /// 515 /// Panics if `store` does not own this instance. 516 pub fn get_typed_func<Params, Results>( 517 &self, 518 mut store: impl AsContextMut, 519 name: &str, 520 ) -> Result<TypedFunc<Params, Results>> 521 where 522 Params: crate::WasmParams, 523 Results: crate::WasmResults, 524 { 525 let f = self 526 .get_export(store.as_context_mut(), name) 527 .and_then(|f| f.into_func()) 528 .ok_or_else(|| format_err!("failed to find function export `{name}`"))?; 529 Ok(f.typed::<Params, Results>(store) 530 .with_context(|| format!("failed to convert function `{name}` to given type"))?) 531 } 532 533 /// Looks up an exported [`Table`] value by name. 534 /// 535 /// Returns `None` if there was no export named `name`, or if there was but 536 /// it wasn't a table. 537 /// 538 /// # Panics 539 /// 540 /// Panics if `store` does not own this instance. 541 pub fn get_table(&self, store: impl AsContextMut, name: &str) -> Option<Table> { 542 self.get_export(store, name)?.into_table() 543 } 544 545 /// Looks up an exported [`Memory`] value by name. 546 /// 547 /// Returns `None` if there was no export named `name`, or if there was but 548 /// it wasn't a memory. 549 /// 550 /// # Panics 551 /// 552 /// Panics if `store` does not own this instance. 553 pub fn get_memory(&self, store: impl AsContextMut, name: &str) -> Option<Memory> { 554 self.get_export(store, name)?.into_memory() 555 } 556 557 /// Looks up an exported [`SharedMemory`] value by name. 558 /// 559 /// Returns `None` if there was no export named `name`, or if there was but 560 /// it wasn't a shared memory. 561 /// 562 /// # Panics 563 /// 564 /// Panics if `store` does not own this instance. 565 pub fn get_shared_memory( 566 &self, 567 mut store: impl AsContextMut, 568 name: &str, 569 ) -> Option<SharedMemory> { 570 let mut store = store.as_context_mut(); 571 self.get_export(&mut store, name)?.into_shared_memory() 572 } 573 574 /// Looks up an exported [`Global`] value by name. 575 /// 576 /// Returns `None` if there was no export named `name`, or if there was but 577 /// it wasn't a global. 578 /// 579 /// # Panics 580 /// 581 /// Panics if `store` does not own this instance. 582 pub fn get_global(&self, store: impl AsContextMut, name: &str) -> Option<Global> { 583 self.get_export(store, name)?.into_global() 584 } 585 586 /// Looks up a tag [`Tag`] by name. 587 /// 588 /// Returns `None` if there was no export named `name`, or if there was but 589 /// it wasn't a tag. 590 /// 591 /// # Panics 592 /// 593 /// Panics if `store` does not own this instance. 594 pub fn get_tag(&self, store: impl AsContextMut, name: &str) -> Option<Tag> { 595 self.get_export(store, name)?.into_tag() 596 } 597 598 #[allow( 599 dead_code, 600 reason = "c-api crate does not yet support exnrefs and causes this method to be dead." 601 )] 602 pub(crate) fn id(&self) -> InstanceId { 603 self.id.instance() 604 } 605 606 /// Get all globals within this instance. 607 /// 608 /// Returns both import and defined globals. 609 /// 610 /// Returns both exported and non-exported globals. 611 /// 612 /// Gives access to the full globals space. 613 #[cfg(feature = "coredump")] 614 pub(crate) fn all_globals<'a>( 615 &'a self, 616 store: &'a mut StoreOpaque, 617 ) -> impl ExactSizeIterator<Item = (GlobalIndex, Global)> + 'a { 618 let store_id = store.id(); 619 store[self.id].all_globals(store_id) 620 } 621 622 /// Get all memories within this instance. 623 /// 624 /// Returns both import and defined memories. 625 /// 626 /// Returns both exported and non-exported memories. 627 /// 628 /// Gives access to the full memories space. 629 #[cfg(feature = "coredump")] 630 pub(crate) fn all_memories<'a>( 631 &'a self, 632 store: &'a StoreOpaque, 633 ) -> impl ExactSizeIterator<Item = (MemoryIndex, vm::ExportMemory)> + 'a { 634 let store_id = store.id(); 635 store[self.id].all_memories(store_id) 636 } 637 } 638 639 pub(crate) struct OwnedImports { 640 functions: PrimaryMap<FuncIndex, VMFunctionImport>, 641 tables: PrimaryMap<TableIndex, VMTableImport>, 642 memories: PrimaryMap<MemoryIndex, VMMemoryImport>, 643 globals: PrimaryMap<GlobalIndex, VMGlobalImport>, 644 tags: PrimaryMap<TagIndex, VMTagImport>, 645 } 646 647 impl OwnedImports { 648 fn new(module: &Module) -> OwnedImports { 649 let mut ret = OwnedImports::empty(); 650 ret.reserve(module); 651 return ret; 652 } 653 654 pub(crate) fn empty() -> OwnedImports { 655 OwnedImports { 656 functions: PrimaryMap::new(), 657 tables: PrimaryMap::new(), 658 memories: PrimaryMap::new(), 659 globals: PrimaryMap::new(), 660 tags: PrimaryMap::new(), 661 } 662 } 663 664 pub(crate) fn reserve(&mut self, module: &Module) { 665 let raw = module.compiled_module().module(); 666 self.functions.reserve(raw.num_imported_funcs); 667 self.tables.reserve(raw.num_imported_tables); 668 self.memories.reserve(raw.num_imported_memories); 669 self.globals.reserve(raw.num_imported_globals); 670 self.tags.reserve(raw.num_imported_tags); 671 } 672 673 #[cfg(feature = "component-model")] 674 pub(crate) fn clear(&mut self) { 675 self.functions.clear(); 676 self.tables.clear(); 677 self.memories.clear(); 678 self.globals.clear(); 679 self.tags.clear(); 680 } 681 682 fn push(&mut self, item: &Extern, store: &mut StoreOpaque) { 683 match item { 684 Extern::Func(i) => { 685 self.functions.push(i.vmimport(store)); 686 } 687 Extern::Global(i) => { 688 self.globals.push(i.vmimport(store)); 689 } 690 Extern::Table(i) => { 691 self.tables.push(i.vmimport(store)); 692 } 693 Extern::Memory(i) => { 694 self.memories.push(i.vmimport(store)); 695 } 696 Extern::SharedMemory(i) => { 697 self.memories.push(i.vmimport(store)); 698 } 699 Extern::Tag(i) => { 700 self.tags.push(i.vmimport(store)); 701 } 702 } 703 } 704 705 /// Note that this is unsafe as the validity of `item` is not verified and 706 /// it contains a bunch of raw pointers. 707 #[cfg(feature = "component-model")] 708 pub(crate) fn push_export(&mut self, store: &StoreOpaque, item: &crate::runtime::vm::Export) { 709 match item { 710 crate::runtime::vm::Export::Function(f) => { 711 // SAFETY: the funcref associated with a `Func` is valid to use 712 // under the `store` that owns the function. 713 let f = unsafe { f.vm_func_ref(store).as_ref() }; 714 self.functions.push(VMFunctionImport { 715 wasm_call: f.wasm_call.unwrap(), 716 array_call: f.array_call, 717 vmctx: f.vmctx, 718 }); 719 } 720 crate::runtime::vm::Export::Global(g) => { 721 self.globals.push(g.vmimport(store)); 722 } 723 crate::runtime::vm::Export::Table(t) => { 724 self.tables.push(t.vmimport(store)); 725 } 726 crate::runtime::vm::Export::Memory(m) => { 727 self.memories.push(m.vmimport(store)); 728 } 729 crate::runtime::vm::Export::SharedMemory(_, vmimport) => { 730 self.memories.push(*vmimport); 731 } 732 crate::runtime::vm::Export::Tag(t) => { 733 self.tags.push(t.vmimport(store)); 734 } 735 } 736 } 737 738 pub(crate) fn as_ref(&self) -> Imports<'_> { 739 Imports { 740 tables: self.tables.values().as_slice(), 741 globals: self.globals.values().as_slice(), 742 memories: self.memories.values().as_slice(), 743 functions: self.functions.values().as_slice(), 744 tags: self.tags.values().as_slice(), 745 } 746 } 747 } 748 749 /// An instance, pre-instantiation, that is ready to be instantiated. 750 /// 751 /// This structure represents an instance *just before* it was instantiated, 752 /// after all type-checking and imports have been resolved. The only thing left 753 /// to do for this instance is to actually run the process of instantiation. 754 /// 755 /// Note that an `InstancePre` may not be tied to any particular [`Store`] if 756 /// none of the imports it closed over are tied to any particular [`Store`]. 757 /// 758 /// This structure is created through the [`Linker::instantiate_pre`] method, 759 /// which also has some more information and examples. 760 /// 761 /// [`Store`]: crate::Store 762 /// [`Linker::instantiate_pre`]: crate::Linker::instantiate_pre 763 pub struct InstancePre<T> { 764 module: Module, 765 766 /// The items which this `InstancePre` use to instantiate the `module` 767 /// provided, passed to `Instance::new_started` after inserting them into a 768 /// `Store`. 769 /// 770 /// Note that this is stored as an `Arc<[T]>` to quickly move a strong 771 /// reference to everything internally into a `Store<T>` without having to 772 /// clone each individual item. 773 items: Arc<[Definition]>, 774 775 /// A count of `Definition::HostFunc` entries in `items` above to 776 /// preallocate space in a `Store` up front for all entries to be inserted. 777 host_funcs: usize, 778 779 /// The `VMFuncRef`s for the functions in `items` that do not 780 /// have a `wasm_call` trampoline. We pre-allocate and pre-patch these 781 /// `VMFuncRef`s so that we don't have to do it at 782 /// instantiation time. 783 /// 784 /// This is an `Arc<[T]>` for the same reason as `items`. 785 func_refs: Arc<[VMFuncRef]>, 786 787 _marker: core::marker::PhantomData<fn() -> T>, 788 } 789 790 /// InstancePre's clone does not require T: Clone 791 impl<T> Clone for InstancePre<T> { 792 fn clone(&self) -> Self { 793 Self { 794 module: self.module.clone(), 795 items: self.items.clone(), 796 host_funcs: self.host_funcs, 797 func_refs: self.func_refs.clone(), 798 _marker: self._marker, 799 } 800 } 801 } 802 803 impl<T: 'static> InstancePre<T> { 804 /// Creates a new `InstancePre` which type-checks the `items` provided and 805 /// on success is ready to instantiate a new instance. 806 /// 807 /// # Unsafety 808 /// 809 /// This method is unsafe as the `T` of the `InstancePre<T>` is not 810 /// guaranteed to be the same as the `T` within the `Store`, the caller must 811 /// verify that. 812 pub(crate) unsafe fn new(module: &Module, items: Vec<Definition>) -> Result<InstancePre<T>> { 813 typecheck(module, &items, |cx, ty, item| cx.definition(ty, &item.ty()))?; 814 815 let mut func_refs = vec![]; 816 let mut host_funcs = 0; 817 for item in &items { 818 match item { 819 Definition::Extern(_, _) => {} 820 Definition::HostFunc(f) => { 821 host_funcs += 1; 822 if f.func_ref().wasm_call.is_none() { 823 func_refs.push(VMFuncRef { 824 wasm_call: module 825 .wasm_to_array_trampoline(f.sig_index()) 826 .map(|f| f.into()), 827 ..*f.func_ref() 828 }); 829 } 830 } 831 } 832 } 833 834 Ok(InstancePre { 835 module: module.clone(), 836 items: items.into(), 837 host_funcs, 838 func_refs: func_refs.into(), 839 _marker: core::marker::PhantomData, 840 }) 841 } 842 843 /// Returns a reference to the module that this [`InstancePre`] will be 844 /// instantiating. 845 pub fn module(&self) -> &Module { 846 &self.module 847 } 848 849 /// Instantiates this instance, creating a new instance within the provided 850 /// `store`. 851 /// 852 /// This function will run the actual process of instantiation to 853 /// completion. This will use all of the previously-closed-over items as 854 /// imports to instantiate the module that this was originally created with. 855 /// 856 /// For more information about instantiation see [`Instance::new`]. 857 /// 858 /// # Panics 859 /// 860 /// Panics if any import closed over by this [`InstancePre`] isn't owned by 861 /// `store`, or if `store` has async support enabled. Additionally this 862 /// function will panic if the `store` provided comes from a different 863 /// [`Engine`] than the [`InstancePre`] originally came from. 864 pub fn instantiate(&self, mut store: impl AsContextMut<Data = T>) -> Result<Instance> { 865 let mut store = store.as_context_mut(); 866 let imports = pre_instantiate_raw( 867 &mut store.0, 868 &self.module, 869 &self.items, 870 self.host_funcs, 871 &self.func_refs, 872 )?; 873 874 // This unsafety should be handled by the type-checking performed by the 875 // constructor of `InstancePre` to assert that all the imports we're passing 876 // in match the module we're instantiating. 877 assert!(!store.0.async_support()); 878 vm::assert_ready(unsafe { 879 Instance::new_started(&mut store, &self.module, imports.as_ref()) 880 }) 881 } 882 883 /// Creates a new instance, running the start function asynchronously 884 /// instead of inline. 885 /// 886 /// For more information about asynchronous instantiation see the 887 /// documentation on [`Instance::new_async`]. 888 /// 889 /// # Panics 890 /// 891 /// Panics if any import closed over by this [`InstancePre`] isn't owned by 892 /// `store`, or if `store` does not have async support enabled. 893 #[cfg(feature = "async")] 894 pub async fn instantiate_async( 895 &self, 896 mut store: impl AsContextMut<Data = T>, 897 ) -> Result<Instance> { 898 let mut store = store.as_context_mut(); 899 let imports = pre_instantiate_raw( 900 &mut store.0, 901 &self.module, 902 &self.items, 903 self.host_funcs, 904 &self.func_refs, 905 )?; 906 907 // This unsafety should be handled by the type-checking performed by the 908 // constructor of `InstancePre` to assert that all the imports we're passing 909 // in match the module we're instantiating. 910 unsafe { Instance::new_started(&mut store, &self.module, imports.as_ref()).await } 911 } 912 } 913 914 /// Helper function shared between 915 /// `InstancePre::{instantiate,instantiate_async}` 916 /// 917 /// This is an out-of-line function to avoid the generic on `InstancePre` and 918 /// get this compiled into the `wasmtime` crate to avoid having it monomorphized 919 /// elsewhere. 920 fn pre_instantiate_raw( 921 store: &mut StoreOpaque, 922 module: &Module, 923 items: &Arc<[Definition]>, 924 host_funcs: usize, 925 func_refs: &Arc<[VMFuncRef]>, 926 ) -> Result<OwnedImports> { 927 // Register this module and use it to fill out any funcref wasm_call holes 928 // we can. For more comments on this see `typecheck_externs`. 929 store.register_module(module)?; 930 let (funcrefs, modules) = store.func_refs_and_modules(); 931 funcrefs.fill(modules); 932 933 if host_funcs > 0 { 934 // Any linker-defined function of the `Definition::HostFunc` variant 935 // will insert a function into the store automatically as part of 936 // instantiation, so reserve space here to make insertion more efficient 937 // as it won't have to realloc during the instantiation. 938 funcrefs.reserve_storage(host_funcs); 939 940 // The usage of `to_extern_store_rooted` requires that the items are 941 // rooted via another means, which happens here by cloning the list of 942 // items into the store once. This avoids cloning each individual item 943 // below. 944 funcrefs.push_instance_pre_definitions(items.clone()); 945 funcrefs.push_instance_pre_func_refs(func_refs.clone()); 946 } 947 948 let mut func_refs = func_refs.iter().map(|f| NonNull::from(f)); 949 let mut imports = OwnedImports::new(module); 950 for import in items.iter() { 951 if !import.comes_from_same_store(store) { 952 bail!("cross-`Store` instantiation is not currently supported"); 953 } 954 // This unsafety should be encapsulated in the constructor of 955 // `InstancePre` where the `T` of the original item should match the 956 // `T` of the store. Additionally the rooting necessary has happened 957 // above. 958 let item = match import { 959 Definition::Extern(e, _) => e.clone(), 960 Definition::HostFunc(func) => unsafe { 961 func.to_func_store_rooted( 962 store, 963 if func.func_ref().wasm_call.is_none() { 964 Some(func_refs.next().unwrap()) 965 } else { 966 None 967 }, 968 ) 969 .into() 970 }, 971 }; 972 imports.push(&item, store); 973 } 974 975 Ok(imports) 976 } 977 978 fn typecheck<I>( 979 module: &Module, 980 import_args: &[I], 981 check: impl Fn(&matching::MatchCx<'_>, &EntityType, &I) -> Result<()>, 982 ) -> Result<()> { 983 let env_module = module.compiled_module().module(); 984 let expected_len = env_module.imports().count(); 985 let actual_len = import_args.len(); 986 if expected_len != actual_len { 987 bail!("expected {expected_len} imports, found {actual_len}"); 988 } 989 let cx = matching::MatchCx::new(module.engine()); 990 for ((name, field, expected_ty), actual) in env_module.imports().zip(import_args) { 991 debug_assert!(expected_ty.is_canonicalized_for_runtime_usage()); 992 check(&cx, &expected_ty, actual) 993 .with_context(|| format!("incompatible import type for `{name}::{field}`"))?; 994 } 995 Ok(()) 996 } 997