Skip to main content

wasmtime/runtime/
instance.rs

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