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
313 let compiled_module = module.compiled_module();
314
315 // Register the module just before instantiation to ensure we keep the module
316 // properly referenced while in use by the store.
317 let (modules, engine, breakpoints) = store.modules_and_engine_and_breakpoints_mut();
318 let module_id = modules.register_module(module, engine, breakpoints)?;
319
320 // The first thing we do is issue an instance allocation request
321 // to the instance allocator. This, on success, will give us an
322 // instance handle.
323 //
324 // SAFETY: this module, by construction, was already validated within
325 // the store.
326 let id = unsafe {
327 store
328 .allocate_instance(
329 limiter.as_deref_mut(),
330 AllocateInstanceKind::Module(module_id),
331 &ModuleRuntimeInfo::Module(module.clone()),
332 imports,
333 )
334 .await?
335 };
336
337 // Additionally, before we start doing fallible instantiation, we
338 // do one more step which is to insert an `InstanceData`
339 // corresponding to this instance. This `InstanceData` can be used
340 // via `Caller::get_export` if our instance's state "leaks" into
341 // other instances, even if we don't return successfully from this
342 // function.
343 //
344 // We don't actually load all exports from the instance at this
345 // time, instead preferring to lazily load them as they're demanded.
346 // For module/instance exports, though, those aren't actually
347 // stored in the instance handle so we need to immediately handle
348 // those here.
349 let instance = Instance::from_wasmtime(id, store);
350
351 // Now that we've recorded all information we need to about this
352 // instance within a `Store` we can start performing fallible
353 // initialization. Note that we still defer the `start` function to
354 // later since that may need to run asynchronously.
355 //
356 // If this returns an error (or if the start function traps) then
357 // any other initialization which may have succeeded which placed
358 // items from this instance into other instances should be ok when
359 // those items are loaded and run we'll have all the metadata to
360 // look at them.
361 let bulk_memory = store
362 .engine()
363 .features()
364 .contains(WasmFeatures::BULK_MEMORY);
365
366 vm::initialize_instance(
367 store,
368 limiter,
369 id,
370 compiled_module.module(),
371 bulk_memory,
372 asyncness,
373 )
374 .await?;
375
376 Ok((instance, compiled_module.module().start_func))
377 }
378
379 pub(crate) fn from_wasmtime(id: InstanceId, store: &mut StoreOpaque) -> Instance {
380 Instance {
381 id: StoreInstanceId::new(store.id(), id),
382 }
383 }
384
385 fn start_raw<T>(&self, store: &mut StoreContextMut<'_, T>, start: FuncIndex) -> Result<()> {
386 // If a start function is present, invoke it. Make sure we use all the
387 // trap-handling configuration in `store` as well.
388 let store_id = store.0.id();
389 let (mut instance, registry) = self.id.get_mut_and_module_registry(store.0);
390 // SAFETY: the `store_id` is the id of the store that owns this
391 // instance and any function stored within the instance.
392 let f = unsafe {
393 instance
394 .as_mut()
395 .get_exported_func(registry, store_id, start)
396 };
397 let caller_vmctx = instance.vmctx();
398 unsafe {
399 let funcref = f.vm_func_ref(store.0);
400 super::func::invoke_wasm_and_catch_traps(store, |_default_caller, vm| {
401 VMFuncRef::array_call(funcref, vm, caller_vmctx, NonNull::from(&mut []))
402 })?;
403 }
404 Ok(())
405 }
406
407 /// Get this instance's module.
408 pub fn module<'a, T: 'static>(&self, store: impl Into<StoreContext<'a, T>>) -> &'a Module {
409 self._module(store.into().0)
410 }
411
412 pub(crate) fn _module<'a>(&self, store: &'a StoreOpaque) -> &'a Module {
413 store.module_for_instance(self.id).unwrap()
414 }
415
416 /// Returns the list of exported items from this [`Instance`].
417 ///
418 /// # Panics
419 ///
420 /// Panics if `store` does not own this instance, or if memory allocation
421 /// fails.
422 pub fn exports<'a, T: 'static>(
423 &'a self,
424 store: impl Into<StoreContextMut<'a, T>>,
425 ) -> impl ExactSizeIterator<Item = Export<'a>> + 'a {
426 let store = store.into().0;
427 let store_id = store.id();
428 let engine = store.engine().clone();
429
430 let (instance, registry) = store.instance_and_module_registry_mut(self.id());
431 let (module, mut instance) = instance.module_and_self();
432 module.exports.iter().map(move |(name, entity)| {
433 // SAFETY: the `store_id` owns this instance and all exports
434 // contained within.
435 let export = unsafe {
436 instance
437 .as_mut()
438 .get_export_by_index_mut(registry, store_id, *entity)
439 };
440
441 let ext = Extern::from_wasmtime_export(export, &engine);
442 Export::new(&module.strings[name], ext)
443 })
444 }
445
446 /// Looks up an exported [`Extern`] value by name.
447 ///
448 /// This method will search the module for an export named `name` and return
449 /// the value, if found.
450 ///
451 /// Returns `None` if there was no export named `name`.
452 ///
453 /// # Panics
454 ///
455 /// Panics if `store` does not own this instance.
456 ///
457 /// # Why does `get_export` take a mutable context?
458 ///
459 /// This method requires a mutable context because an instance's exports are
460 /// lazily populated, and we cache them as they are accessed. This makes
461 /// instantiating a module faster, but also means this method requires a
462 /// mutable context.
463 pub fn get_export(&self, mut store: impl AsContextMut, name: &str) -> Option<Extern> {
464 let store = store.as_context_mut().0;
465 let module = store[self.id].env_module();
466 let name = module.strings.get_atom(name)?;
467 let entity = *module.exports.get(&name)?;
468 Some(self._get_export(store, entity))
469 }
470
471 /// Looks up an exported [`Extern`] value by a [`ModuleExport`] value.
472 ///
473 /// This is similar to [`Instance::get_export`] but uses a [`ModuleExport`] value to avoid
474 /// string lookups where possible. [`ModuleExport`]s can be obtained by calling
475 /// [`Module::get_export_index`] on the [`Module`] that this instance was instantiated with.
476 ///
477 /// This method will search the module for an export with a matching entity index and return
478 /// the value, if found.
479 ///
480 /// Returns `None` if there was no export with a matching entity index.
481 ///
482 /// # Panics
483 ///
484 /// Panics if `store` does not own this instance.
485 pub fn get_module_export(
486 &self,
487 mut store: impl AsContextMut,
488 export: &ModuleExport,
489 ) -> Option<Extern> {
490 let store = store.as_context_mut().0;
491
492 // Verify the `ModuleExport` matches the module used in this instance.
493 if self._module(store).id() != export.module {
494 return None;
495 }
496
497 Some(self._get_export(store, export.entity))
498 }
499
500 fn _get_export(&self, store: &mut StoreOpaque, entity: EntityIndex) -> Extern {
501 let id = store.id();
502 // SAFETY: the store `id` owns this instance and all exports contained
503 // within.
504 let export = unsafe {
505 let (instance, registry) = self.id.get_mut_and_module_registry(store);
506 instance.get_export_by_index_mut(registry, id, entity)
507 };
508 Extern::from_wasmtime_export(export, store.engine())
509 }
510
511 /// Looks up an exported [`Func`] value by name.
512 ///
513 /// Returns `None` if there was no export named `name`, or if there was but
514 /// it wasn't a function.
515 ///
516 /// # Panics
517 ///
518 /// Panics if `store` does not own this instance.
519 pub fn get_func(&self, store: impl AsContextMut, name: &str) -> Option<Func> {
520 self.get_export(store, name)?.into_func()
521 }
522
523 /// Looks up an exported [`Func`] value by name and with its type.
524 ///
525 /// This function is a convenience wrapper over [`Instance::get_func`] and
526 /// [`Func::typed`]. For more information see the linked documentation.
527 ///
528 /// Returns an error if `name` isn't a function export or if the export's
529 /// type did not match `Params` or `Results`
530 ///
531 /// # Panics
532 ///
533 /// Panics if `store` does not own this instance.
534 ///
535 /// # Errors
536 ///
537 /// This function will return an [`OutOfMemory`][crate::OutOfMemory] error when
538 /// memory allocation fails. See the `OutOfMemory` type's documentation for
539 /// details on Wasmtime's out-of-memory handling.
540 pub fn get_typed_func<Params, Results>(
541 &self,
542 mut store: impl AsContextMut,
543 name: &str,
544 ) -> Result<TypedFunc<Params, Results>>
545 where
546 Params: crate::WasmParams,
547 Results: crate::WasmResults,
548 {
549 let f = self
550 .get_export(store.as_context_mut(), name)
551 .and_then(|f| f.into_func())
552 .ok_or_else(|| format_err!("failed to find function export `{name}`"))?;
553 Ok(f.typed::<Params, Results>(store)
554 .with_context(|| format!("failed to convert function `{name}` to given type"))?)
555 }
556
557 /// Looks up an exported [`Table`] value by name.
558 ///
559 /// Returns `None` if there was no export named `name`, or if there was but
560 /// it wasn't a table.
561 ///
562 /// # Panics
563 ///
564 /// Panics if `store` does not own this instance.
565 pub fn get_table(&self, store: impl AsContextMut, name: &str) -> Option<Table> {
566 self.get_export(store, name)?.into_table()
567 }
568
569 /// Looks up an exported [`Memory`] value by name.
570 ///
571 /// Returns `None` if there was no export named `name`, or if there was but
572 /// it wasn't a memory.
573 ///
574 /// # Panics
575 ///
576 /// Panics if `store` does not own this instance.
577 pub fn get_memory(&self, store: impl AsContextMut, name: &str) -> Option<Memory> {
578 self.get_export(store, name)?.into_memory()
579 }
580
581 /// Looks up an exported [`SharedMemory`] value by name.
582 ///
583 /// Returns `None` if there was no export named `name`, or if there was but
584 /// it wasn't a shared memory.
585 ///
586 /// # Panics
587 ///
588 /// Panics if `store` does not own this instance.
589 pub fn get_shared_memory(
590 &self,
591 mut store: impl AsContextMut,
592 name: &str,
593 ) -> Option<SharedMemory> {
594 let mut store = store.as_context_mut();
595 self.get_export(&mut store, name)?.into_shared_memory()
596 }
597
598 /// Looks up an exported [`Global`] value by name.
599 ///
600 /// Returns `None` if there was no export named `name`, or if there was but
601 /// it wasn't a global.
602 ///
603 /// # Panics
604 ///
605 /// Panics if `store` does not own this instance.
606 pub fn get_global(&self, store: impl AsContextMut, name: &str) -> Option<Global> {
607 self.get_export(store, name)?.into_global()
608 }
609
610 /// Looks up a tag [`Tag`] by name.
611 ///
612 /// Returns `None` if there was no export named `name`, or if there was but
613 /// it wasn't a tag.
614 ///
615 /// # Panics
616 ///
617 /// Panics if `store` does not own this instance.
618 pub fn get_tag(&self, store: impl AsContextMut, name: &str) -> Option<Tag> {
619 self.get_export(store, name)?.into_tag()
620 }
621
622 #[allow(
623 dead_code,
624 reason = "c-api crate does not yet support exnrefs and causes this method to be dead."
625 )]
626 pub(crate) fn id(&self) -> InstanceId {
627 self.id.instance()
628 }
629
630 /// Return a unique-within-Store index for this `Instance`.
631 ///
632 /// Allows distinguishing instance identities when introspecting
633 /// the `Store`, e.g. via debug APIs.
634 ///
635 /// This index will match the instance's position in the sequence
636 /// returned by `Store::debug_all_instances()`.
637 #[cfg(feature = "debug")]
638 pub fn debug_index_in_store(&self) -> u32 {
639 self.id.instance().as_u32()
640 }
641
642 /// Get all globals within this instance.
643 ///
644 /// Returns both import and defined globals.
645 ///
646 /// Returns both exported and non-exported globals.
647 ///
648 /// Gives access to the full globals space.
649 #[cfg(feature = "coredump")]
650 pub(crate) fn all_globals<'a>(
651 &'a self,
652 store: &'a mut StoreOpaque,
653 ) -> impl ExactSizeIterator<Item = (GlobalIndex, Global)> + 'a {
654 let store_id = store.id();
655 store[self.id].all_globals(store_id)
656 }
657
658 /// Get all memories within this instance.
659 ///
660 /// Returns both import and defined memories.
661 ///
662 /// Returns both exported and non-exported memories.
663 ///
664 /// Gives access to the full memories space.
665 #[cfg(feature = "coredump")]
666 pub(crate) fn all_memories<'a>(
667 &'a self,
668 store: &'a StoreOpaque,
669 ) -> impl ExactSizeIterator<Item = (MemoryIndex, vm::ExportMemory)> + 'a {
670 let store_id = store.id();
671 store[self.id].all_memories(store_id)
672 }
673}
674
675pub(crate) struct OwnedImports {
676 functions: TryPrimaryMap<FuncIndex, VMFunctionImport>,
677 tables: TryPrimaryMap<TableIndex, VMTableImport>,
678 memories: TryPrimaryMap<MemoryIndex, VMMemoryImport>,
679 globals: TryPrimaryMap<GlobalIndex, VMGlobalImport>,
680 tags: TryPrimaryMap<TagIndex, VMTagImport>,
681}
682
683impl OwnedImports {
684 fn new(module: &Module) -> Result<OwnedImports, OutOfMemory> {
685 let mut ret = OwnedImports::empty();
686 ret.reserve(module)?;
687 Ok(ret)
688 }
689
690 pub(crate) fn empty() -> OwnedImports {
691 OwnedImports {
692 functions: TryPrimaryMap::new(),
693 tables: TryPrimaryMap::new(),
694 memories: TryPrimaryMap::new(),
695 globals: TryPrimaryMap::new(),
696 tags: TryPrimaryMap::new(),
697 }
698 }
699
700 pub(crate) fn reserve(&mut self, module: &Module) -> Result<(), OutOfMemory> {
701 let raw = module.compiled_module().module();
702 self.functions.reserve(raw.num_imported_funcs)?;
703 self.tables.reserve(raw.num_imported_tables)?;
704 self.memories.reserve(raw.num_imported_memories)?;
705 self.globals.reserve(raw.num_imported_globals)?;
706 self.tags.reserve(raw.num_imported_tags)?;
707 Ok(())
708 }
709
710 #[cfg(feature = "component-model")]
711 pub(crate) fn clear(&mut self) {
712 self.functions.clear();
713 self.tables.clear();
714 self.memories.clear();
715 self.globals.clear();
716 self.tags.clear();
717 }
718
719 fn push(&mut self, item: &Extern, store: &mut StoreOpaque) -> Result<(), OutOfMemory> {
720 match item {
721 Extern::Func(i) => {
722 self.functions.push(i.vmimport(store))?;
723 }
724 Extern::Global(i) => {
725 self.globals.push(i.vmimport(store))?;
726 }
727 Extern::Table(i) => {
728 self.tables.push(i.vmimport(store))?;
729 }
730 Extern::Memory(i) => {
731 self.memories.push(i.vmimport(store))?;
732 }
733 Extern::SharedMemory(i) => {
734 self.memories.push(i.vmimport(store))?;
735 }
736 Extern::Tag(i) => {
737 self.tags.push(i.vmimport(store))?;
738 }
739 }
740 Ok(())
741 }
742
743 /// Note that this is unsafe as the validity of `item` is not verified and
744 /// it contains a bunch of raw pointers.
745 #[cfg(feature = "component-model")]
746 pub(crate) fn push_export(
747 &mut self,
748 store: &StoreOpaque,
749 item: &crate::runtime::vm::Export,
750 ) -> Result<(), OutOfMemory> {
751 match item {
752 crate::runtime::vm::Export::Function(f) => {
753 self.functions.push(f.vmimport(store))?;
754 }
755 crate::runtime::vm::Export::Global(g) => {
756 self.globals.push(g.vmimport(store))?;
757 }
758 crate::runtime::vm::Export::Table(t) => {
759 self.tables.push(t.vmimport(store))?;
760 }
761 crate::runtime::vm::Export::Memory(m) => {
762 self.memories.push(m.vmimport(store))?;
763 }
764 crate::runtime::vm::Export::SharedMemory(_, vmimport) => {
765 self.memories.push(*vmimport)?;
766 }
767 crate::runtime::vm::Export::Tag(t) => {
768 self.tags.push(t.vmimport(store))?;
769 }
770 }
771 Ok(())
772 }
773
774 pub(crate) fn as_ref(&self) -> Imports<'_> {
775 Imports {
776 tables: self.tables.values().as_slice(),
777 globals: self.globals.values().as_slice(),
778 memories: self.memories.values().as_slice(),
779 functions: self.functions.values().as_slice(),
780 tags: self.tags.values().as_slice(),
781 }
782 }
783}
784
785/// An instance, pre-instantiation, that is ready to be instantiated.
786///
787/// This structure represents an instance *just before* it was instantiated,
788/// after all type-checking and imports have been resolved. The only thing left
789/// to do for this instance is to actually run the process of instantiation.
790///
791/// Note that an `InstancePre` may not be tied to any particular [`Store`] if
792/// none of the imports it closed over are tied to any particular [`Store`].
793///
794/// This structure is created through the [`Linker::instantiate_pre`] method,
795/// which also has some more information and examples.
796///
797/// [`Store`]: crate::Store
798/// [`Linker::instantiate_pre`]: crate::Linker::instantiate_pre
799pub struct InstancePre<T> {
800 module: Module,
801
802 /// The items which this `InstancePre` use to instantiate the `module`
803 /// provided, passed to `Instance::new_started` after inserting them into a
804 /// `Store`.
805 ///
806 /// Note that this is stored as an `Arc` to quickly move a strong reference
807 /// to everything internally into a `Store<T>` without having to clone each
808 /// individual item.
809 items: Arc<TryVec<Definition>>,
810
811 /// A count of `Definition::HostFunc` entries in `items` above to
812 /// preallocate space in a `Store` up front for all entries to be inserted.
813 host_funcs: usize,
814
815 /// The `VMFuncRef`s for the functions in `items` that do not
816 /// have a `wasm_call` trampoline. We pre-allocate and pre-patch these
817 /// `VMFuncRef`s so that we don't have to do it at
818 /// instantiation time.
819 ///
820 /// This is an `Arc` for the same reason as `items`.
821 func_refs: Arc<TryVec<VMFuncRef>>,
822
823 /// Whether or not any import in `items` is flagged as needing async.
824 ///
825 /// This is used to update stores during instantiation as to whether they
826 /// require async entrypoints.
827 asyncness: Asyncness,
828
829 _marker: core::marker::PhantomData<fn() -> T>,
830}
831
832/// InstancePre's clone does not require T: Clone
833impl<T> Clone for InstancePre<T> {
834 fn clone(&self) -> Self {
835 Self {
836 module: self.module.clone(),
837 items: self.items.clone(),
838 host_funcs: self.host_funcs,
839 func_refs: self.func_refs.clone(),
840 asyncness: self.asyncness,
841 _marker: self._marker,
842 }
843 }
844}
845
846impl<T: 'static> InstancePre<T> {
847 /// Creates a new `InstancePre` which type-checks the `items` provided and
848 /// on success is ready to instantiate a new instance.
849 ///
850 /// # Unsafety
851 ///
852 /// This method is unsafe as the `T` of the `InstancePre<T>` is not
853 /// guaranteed to be the same as the `T` within the `Store`, the caller must
854 /// verify that.
855 pub(crate) unsafe fn new(module: &Module, items: TryVec<Definition>) -> Result<InstancePre<T>> {
856 typecheck(module, &items, |cx, ty, item| cx.definition(ty, &item.ty()))?;
857
858 let mut func_refs = TryVec::with_capacity(items.len())?;
859 let mut host_funcs = 0;
860 let mut asyncness = Asyncness::No;
861 for item in &items {
862 match item {
863 Definition::Extern(_, _) => {}
864 Definition::HostFunc(f) => {
865 host_funcs += 1;
866 if f.func_ref().wasm_call.is_none() {
867 func_refs.push(VMFuncRef {
868 wasm_call: module
869 .wasm_to_array_trampoline(f.sig_index())
870 .map(|f| f.into()),
871 ..*f.func_ref()
872 })?;
873 }
874 asyncness = asyncness | f.asyncness();
875 }
876 }
877 }
878
879 Ok(InstancePre {
880 module: module.clone(),
881 items: try_new::<Arc<_>>(items)?,
882 host_funcs,
883 func_refs: try_new::<Arc<_>>(func_refs)?,
884 asyncness,
885 _marker: core::marker::PhantomData,
886 })
887 }
888
889 /// Returns a reference to the module that this [`InstancePre`] will be
890 /// instantiating.
891 pub fn module(&self) -> &Module {
892 &self.module
893 }
894
895 /// Instantiates this instance, creating a new instance within the provided
896 /// `store`.
897 ///
898 /// This function will run the actual process of instantiation to
899 /// completion. This will use all of the previously-closed-over items as
900 /// imports to instantiate the module that this was originally created with.
901 ///
902 /// For more information about instantiation see [`Instance::new`].
903 ///
904 /// # Panics
905 ///
906 /// Panics if any import closed over by this [`InstancePre`] isn't owned by
907 /// `store`, or if `store` has async support enabled. Additionally this
908 /// function will panic if the `store` provided comes from a different
909 /// [`Engine`] than the [`InstancePre`] originally came from.
910 ///
911 /// # Errors
912 ///
913 /// This function will return an [`OutOfMemory`][crate::OutOfMemory] error when
914 /// memory allocation fails. See the `OutOfMemory` type's documentation for
915 /// details on Wasmtime's out-of-memory handling.
916 pub fn instantiate(&self, mut store: impl AsContextMut<Data = T>) -> Result<Instance> {
917 let mut store = store.as_context_mut();
918 let imports = pre_instantiate_raw(
919 &mut store.0,
920 &self.module,
921 &self.items,
922 self.host_funcs,
923 &self.func_refs,
924 self.asyncness,
925 )?;
926
927 // Note that this is specifically done after `pre_instantiate_raw` to
928 // handle the case that if any imports in this `InstancePre` require
929 // async that it's flagged in the store by that point which will reject
930 // this instantiation to say "use `instantiate_async` instead".
931 store.0.validate_sync_call()?;
932
933 // This unsafety should be handled by the type-checking performed by the
934 // constructor of `InstancePre` to assert that all the imports we're passing
935 // in match the module we're instantiating.
936 vm::assert_ready(unsafe {
937 Instance::new_started(&mut store, &self.module, imports.as_ref(), Asyncness::No)
938 })
939 }
940
941 /// Creates a new instance, running the start function asynchronously
942 /// instead of inline.
943 ///
944 /// For more information about asynchronous instantiation see the
945 /// documentation on [`Instance::new_async`].
946 ///
947 /// # Panics
948 ///
949 /// Panics if any import closed over by this [`InstancePre`] isn't owned by
950 /// `store`, or if `store` does not have async support enabled.
951 ///
952 /// # Errors
953 ///
954 /// This function will return an [`OutOfMemory`][crate::OutOfMemory] error when
955 /// memory allocation fails. See the `OutOfMemory` type's documentation for
956 /// details on Wasmtime's out-of-memory handling.
957 #[cfg(feature = "async")]
958 pub async fn instantiate_async(
959 &self,
960 mut store: impl AsContextMut<Data = T>,
961 ) -> Result<Instance> {
962 let mut store = store.as_context_mut();
963 let imports = pre_instantiate_raw(
964 &mut store.0,
965 &self.module,
966 &self.items,
967 self.host_funcs,
968 &self.func_refs,
969 self.asyncness,
970 )?;
971
972 // This unsafety should be handled by the type-checking performed by the
973 // constructor of `InstancePre` to assert that all the imports we're passing
974 // in match the module we're instantiating.
975 unsafe {
976 Instance::new_started(&mut store, &self.module, imports.as_ref(), Asyncness::Yes).await
977 }
978 }
979}
980
981/// Helper function shared between
982/// `InstancePre::{instantiate,instantiate_async}`
983///
984/// This is an out-of-line function to avoid the generic on `InstancePre` and
985/// get this compiled into the `wasmtime` crate to avoid having it monomorphized
986/// elsewhere.
987fn pre_instantiate_raw(
988 store: &mut StoreOpaque,
989 module: &Module,
990 items: &Arc<TryVec<Definition>>,
991 host_funcs: usize,
992 func_refs: &Arc<TryVec<VMFuncRef>>,
993 asyncness: Asyncness,
994) -> Result<OwnedImports> {
995 // Register this module and use it to fill out any funcref wasm_call holes
996 // we can. For more comments on this see `typecheck_externs`.
997 let (modules, engine, breakpoints) = store.modules_and_engine_and_breakpoints_mut();
998 modules.register_module(module, engine, breakpoints)?;
999 let (funcrefs, modules) = store.func_refs_and_modules();
1000 funcrefs.fill(modules);
1001
1002 if host_funcs > 0 {
1003 // Any linker-defined function of the `Definition::HostFunc` variant
1004 // will insert a function into the store automatically as part of
1005 // instantiation, so reserve space here to make insertion more efficient
1006 // as it won't have to realloc during the instantiation.
1007 funcrefs.reserve_storage(host_funcs)?;
1008
1009 // The usage of `to_extern_store_rooted` requires that the items are
1010 // rooted via another means, which happens here by cloning the list of
1011 // items into the store once. This avoids cloning each individual item
1012 // below.
1013 funcrefs.push_instance_pre_definitions(items.clone())?;
1014 funcrefs.push_instance_pre_func_refs(func_refs.clone())?;
1015 }
1016
1017 store.set_async_required(asyncness);
1018
1019 let mut func_refs = func_refs.iter().map(|f| NonNull::from(f));
1020 let mut imports = OwnedImports::new(module)?;
1021 for import in items.iter() {
1022 if !import.comes_from_same_store(store) {
1023 bail!("cross-`Store` instantiation is not currently supported");
1024 }
1025 // This unsafety should be encapsulated in the constructor of
1026 // `InstancePre` where the `T` of the original item should match the
1027 // `T` of the store. Additionally the rooting necessary has happened
1028 // above.
1029 let item = match import {
1030 Definition::Extern(e, _) => e.clone(),
1031 Definition::HostFunc(func) => unsafe {
1032 func.to_func_store_rooted(
1033 store,
1034 if func.func_ref().wasm_call.is_none() {
1035 Some(func_refs.next().unwrap())
1036 } else {
1037 None
1038 },
1039 )
1040 .into()
1041 },
1042 };
1043 imports.push(&item, store)?;
1044 }
1045
1046 Ok(imports)
1047}
1048
1049fn typecheck<I>(
1050 module: &Module,
1051 import_args: &[I],
1052 check: impl Fn(&matching::MatchCx<'_>, &EntityType, &I) -> Result<()>,
1053) -> Result<()> {
1054 let env_module = module.compiled_module().module();
1055 let expected_len = env_module.imports().count();
1056 let actual_len = import_args.len();
1057 if expected_len != actual_len {
1058 bail!("expected {expected_len} imports, found {actual_len}");
1059 }
1060 let cx = matching::MatchCx::new(module.engine());
1061 for ((name, field, expected_ty), actual) in env_module.imports().zip(import_args) {
1062 debug_assert!(expected_ty.is_canonicalized_for_runtime_usage());
1063 check(&cx, &expected_ty, actual)
1064 .with_context(|| format!("incompatible import type for `{name}::{field}`"))?;
1065 }
1066 Ok(())
1067}