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