dusk_wasmtime/runtime/
instance.rs

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