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