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