1 use crate::component::RuntimeInstance;
2 use crate::component::func::HostFunc;
3 use crate::component::matching::InstanceType;
4 use crate::component::store::{ComponentInstanceId, StoreComponentInstanceId};
5 use crate::component::{
6     Component, ComponentExportIndex, ComponentNamedList, Func, Lift, Lower, ResourceType,
7     TypedFunc, types::ComponentItem,
8 };
9 use crate::instance::OwnedImports;
10 use crate::linker::DefinitionType;
11 use crate::prelude::*;
12 use crate::runtime::vm::component::{
13     CallContexts, ComponentInstance, ResourceTables, TypedResource, TypedResourceIndex,
14 };
15 use crate::runtime::vm::{self, VMFuncRef};
16 use crate::store::{AsStoreOpaque, Asyncness, StoreOpaque};
17 use crate::{AsContext, AsContextMut, Engine, Module, StoreContextMut};
18 use alloc::sync::Arc;
19 use core::marker;
20 use core::pin::Pin;
21 use core::ptr::NonNull;
22 use wasmtime_environ::{EngineOrModuleTypeIndex, component::*};
23 use wasmtime_environ::{EntityIndex, EntityType, PrimaryMap};
24 
25 /// An instantiated component.
26 ///
27 /// This type represents an instantiated [`Component`](super::Component).
28 /// Instances have exports which can be accessed through functions such as
29 /// [`Instance::get_func`] or [`Instance::get_export`]. Instances are owned by a
30 /// [`Store`](crate::Store) and all methods require a handle to the store.
31 ///
32 /// Component instances are created through
33 /// [`Linker::instantiate`](super::Linker::instantiate) and its family of
34 /// methods.
35 ///
36 /// This type is similar to the core wasm version
37 /// [`wasmtime::Instance`](crate::Instance) except that it represents an
38 /// instantiated component instead of an instantiated module.
39 #[derive(Copy, Clone, Debug)]
40 #[repr(transparent)]
41 pub struct Instance {
42     id: StoreComponentInstanceId,
43 }
44 
45 // Double-check that the C representation in `component/instance.h` matches our
46 // in-Rust representation here in terms of size/alignment/etc.
47 const _: () = {
48     #[repr(C)]
49     struct C(u64, u32);
50     assert!(core::mem::size_of::<C>() == core::mem::size_of::<Instance>());
51     assert!(core::mem::align_of::<C>() == core::mem::align_of::<Instance>());
52     assert!(core::mem::offset_of!(Instance, id) == 0);
53 };
54 
55 impl Instance {
56     /// Creates a raw `Instance` from the internal identifiers within the store.
57     pub(crate) fn from_wasmtime(store: &StoreOpaque, id: ComponentInstanceId) -> Instance {
58         Instance {
59             id: StoreComponentInstanceId::new(store.id(), id),
60         }
61     }
62 
63     /// Looks up an exported function by name within this [`Instance`].
64     ///
65     /// The `store` argument provided must be the store that this instance
66     /// lives within and the `name` argument is the lookup key by which to find
67     /// the exported function. If the function is found then `Some` is returned
68     /// and otherwise `None` is returned.
69     ///
70     /// The `name` here can be a string such as `&str` or it can be a
71     /// [`ComponentExportIndex`] which is loaded prior from a [`Component`].
72     ///
73     /// # Panics
74     ///
75     /// Panics if `store` does not own this instance.
76     ///
77     /// # Examples
78     ///
79     /// Looking up a function which is exported from the root of a component:
80     ///
81     /// ```
82     /// use wasmtime::{Engine, Store};
83     /// use wasmtime::component::{Component, Linker};
84     ///
85     /// # fn main() -> wasmtime::Result<()> {
86     /// let engine = Engine::default();
87     /// let component = Component::new(
88     ///     &engine,
89     ///     r#"
90     ///         (component
91     ///             (core module $m
92     ///                 (func (export "f"))
93     ///             )
94     ///             (core instance $i (instantiate $m))
95     ///             (func (export "f")
96     ///                 (canon lift (core func $i "f")))
97     ///         )
98     ///     "#,
99     /// )?;
100     ///
101     /// // Look up the function by name
102     /// let mut store = Store::new(&engine, ());
103     /// let instance = Linker::new(&engine).instantiate(&mut store, &component)?;
104     /// let func = instance.get_func(&mut store, "f").unwrap();
105     ///
106     /// // The function can also be looked up by an index via a precomputed index.
107     /// let export = component.get_export_index(None, "f").unwrap();
108     /// let func = instance.get_func(&mut store, &export).unwrap();
109     /// # Ok(())
110     /// # }
111     /// ```
112     ///
113     /// Looking up a function which is exported from a nested instance:
114     ///
115     /// ```
116     /// use wasmtime::{Engine, Store};
117     /// use wasmtime::component::{Component, Linker};
118     ///
119     /// # fn main() -> wasmtime::Result<()> {
120     /// let engine = Engine::default();
121     /// let component = Component::new(
122     ///     &engine,
123     ///     r#"
124     ///         (component
125     ///             (core module $m
126     ///                 (func (export "f"))
127     ///             )
128     ///             (core instance $i (instantiate $m))
129     ///             (func $f
130     ///                 (canon lift (core func $i "f")))
131     ///
132     ///             (instance $i
133     ///                 (export "f" (func $f)))
134     ///             (export "i" (instance $i))
135     ///         )
136     ///     "#,
137     /// )?;
138     ///
139     /// // First look up the exported instance, then use that to lookup the
140     /// // exported function.
141     /// let instance_index = component.get_export_index(None, "i").unwrap();
142     /// let func_index = component.get_export_index(Some(&instance_index), "f").unwrap();
143     ///
144     /// // Then use `func_index` at runtime.
145     /// let mut store = Store::new(&engine, ());
146     /// let instance = Linker::new(&engine).instantiate(&mut store, &component)?;
147     /// let func = instance.get_func(&mut store, &func_index).unwrap();
148     ///
149     /// // Alternatively the `instance` can be used directly in conjunction with
150     /// // the `get_export_index` method.
151     /// let instance_index = instance.get_export_index(&mut store, None, "i").unwrap();
152     /// let func_index = instance.get_export_index(&mut store, Some(&instance_index), "f").unwrap();
153     /// let func = instance.get_func(&mut store, &func_index).unwrap();
154     /// # Ok(())
155     /// # }
156     /// ```
157     pub fn get_func(
158         &self,
159         mut store: impl AsContextMut,
160         name: impl InstanceExportLookup,
161     ) -> Option<Func> {
162         let store = store.as_context_mut().0;
163         let instance = self.id.get(store);
164         let component = instance.component();
165 
166         // Validate that `name` exists within `self.`
167         let index = name.lookup(component)?;
168 
169         // Validate that `index` is indeed a lifted function.
170         match &component.env_component().export_items[index] {
171             Export::LiftedFunction { .. } => {}
172             _ => return None,
173         }
174 
175         // And package up the indices!
176         Some(Func::from_lifted_func(*self, index))
177     }
178 
179     /// Looks up an exported [`Func`] value by name and with its type.
180     ///
181     /// This function is a convenience wrapper over [`Instance::get_func`] and
182     /// [`Func::typed`]. For more information see the linked documentation.
183     ///
184     /// Returns an error if `name` isn't a function export or if the export's
185     /// type did not match `Params` or `Results`
186     ///
187     /// # Panics
188     ///
189     /// Panics if `store` does not own this instance.
190     pub fn get_typed_func<Params, Results>(
191         &self,
192         mut store: impl AsContextMut,
193         name: impl InstanceExportLookup,
194     ) -> Result<TypedFunc<Params, Results>>
195     where
196         Params: ComponentNamedList + Lower,
197         Results: ComponentNamedList + Lift,
198     {
199         let f = self
200             .get_func(store.as_context_mut(), name)
201             .ok_or_else(|| format_err!("failed to find function export"))?;
202         Ok(f.typed::<Params, Results>(store)
203             .with_context(|| format!("failed to convert function to given type"))?)
204     }
205 
206     /// Looks up an exported module by name within this [`Instance`].
207     ///
208     /// The `store` argument provided must be the store that this instance
209     /// lives within and the `name` argument is the lookup key by which to find
210     /// the exported module. If the module is found then `Some` is returned
211     /// and otherwise `None` is returned.
212     ///
213     /// The `name` here can be a string such as `&str` or it can be a
214     /// [`ComponentExportIndex`] which is loaded prior from a [`Component`].
215     ///
216     /// For some examples see [`Instance::get_func`] for loading values from a
217     /// component.
218     ///
219     /// # Panics
220     ///
221     /// Panics if `store` does not own this instance.
222     pub fn get_module(
223         &self,
224         mut store: impl AsContextMut,
225         name: impl InstanceExportLookup,
226     ) -> Option<Module> {
227         let store = store.as_context_mut().0;
228         let (instance, export) = self.lookup_export(store, name)?;
229         match export {
230             Export::ModuleStatic { index, .. } => {
231                 Some(instance.component().static_module(*index).clone())
232             }
233             Export::ModuleImport { import, .. } => match instance.runtime_import(*import) {
234                 RuntimeImport::Module(m) => Some(m.clone()),
235                 _ => unreachable!(),
236             },
237             _ => None,
238         }
239     }
240 
241     /// Looks up an exported resource type by name within this [`Instance`].
242     ///
243     /// The `store` argument provided must be the store that this instance
244     /// lives within and the `name` argument is the lookup key by which to find
245     /// the exported resource. If the resource is found then `Some` is returned
246     /// and otherwise `None` is returned.
247     ///
248     /// The `name` here can be a string such as `&str` or it can be a
249     /// [`ComponentExportIndex`] which is loaded prior from a [`Component`].
250     ///
251     /// For some examples see [`Instance::get_func`] for loading values from a
252     /// component.
253     ///
254     /// # Panics
255     ///
256     /// Panics if `store` does not own this instance.
257     pub fn get_resource(
258         &self,
259         mut store: impl AsContextMut,
260         name: impl InstanceExportLookup,
261     ) -> Option<ResourceType> {
262         let store = store.as_context_mut().0;
263         let (instance, export) = self.lookup_export(store, name)?;
264         match export {
265             Export::Type(TypeDef::Resource(id)) => {
266                 Some(InstanceType::new(instance).resource_type(*id))
267             }
268             Export::Type(_)
269             | Export::LiftedFunction { .. }
270             | Export::ModuleStatic { .. }
271             | Export::ModuleImport { .. }
272             | Export::Instance { .. } => None,
273         }
274     }
275 
276     /// A methods similar to [`Component::get_export`] except for this
277     /// instance.
278     ///
279     /// This method will lookup the `name` provided within the `instance`
280     /// provided and return a [`ComponentItem`] describing the export,
281     /// and [`ComponentExportIndex`] which can be passed other `get_*`
282     /// functions like [`Instance::get_func`].
283     ///
284     /// The [`ComponentItem`] is more expensive to compute than the
285     /// [`ComponentExportIndex`]. If you are not consuming the
286     /// [`ComponentItem`], use [`Instance::get_export_index`] instead.
287     ///
288     /// # Panics
289     ///
290     /// Panics if `store` does not own this instance.
291     pub fn get_export(
292         &self,
293         mut store: impl AsContextMut,
294         instance: Option<&ComponentExportIndex>,
295         name: &str,
296     ) -> Option<(ComponentItem, ComponentExportIndex)> {
297         self._get_export(store.as_context_mut().0, instance, name)
298     }
299 
300     fn _get_export(
301         &self,
302         store: &StoreOpaque,
303         instance: Option<&ComponentExportIndex>,
304         name: &str,
305     ) -> Option<(ComponentItem, ComponentExportIndex)> {
306         let data = self.id().get(store);
307         let component = data.component();
308         let index = component.lookup_export_index(instance, name)?;
309         let item = ComponentItem::from_export(
310             &store.engine(),
311             &component.env_component().export_items[index],
312             &InstanceType::new(data),
313         );
314         Some((
315             item,
316             ComponentExportIndex {
317                 id: data.component().id(),
318                 index,
319             },
320         ))
321     }
322 
323     /// A methods similar to [`Component::get_export_index`] except for this
324     /// instance.
325     ///
326     /// This method will lookup the `name` provided within the `instance`
327     /// provided and return a [`ComponentExportIndex`] which can be passed
328     /// other `get_*` functions like [`Instance::get_func`].
329     ///
330     /// If you need the [`ComponentItem`] corresponding to this export, use
331     /// the [`Instance::get_export`] instead.
332     ///
333     /// # Panics
334     ///
335     /// Panics if `store` does not own this instance.
336     pub fn get_export_index(
337         &self,
338         mut store: impl AsContextMut,
339         instance: Option<&ComponentExportIndex>,
340         name: &str,
341     ) -> Option<ComponentExportIndex> {
342         let data = self.id().get(store.as_context_mut().0);
343         let index = data.component().lookup_export_index(instance, name)?;
344         Some(ComponentExportIndex {
345             id: data.component().id(),
346             index,
347         })
348     }
349 
350     fn lookup_export<'a>(
351         &self,
352         store: &'a StoreOpaque,
353         name: impl InstanceExportLookup,
354     ) -> Option<(&'a ComponentInstance, &'a Export)> {
355         let data = self.id().get(store);
356         let index = name.lookup(data.component())?;
357         Some((data, &data.component().env_component().export_items[index]))
358     }
359 
360     /// Returns the [`InstancePre`] that was used to create this instance.
361     pub fn instance_pre<T>(&self, store: impl AsContext<Data = T>) -> InstancePre<T> {
362         // This indexing operation asserts the Store owns the Instance.
363         // Therefore, the InstancePre<T> must match the Store<T>.
364         let data = self.id().get(store.as_context().0);
365 
366         // SAFETY: calling this method safely here relies on matching the `T`
367         // in `InstancePre<T>` to the store itself, which is happening in the
368         // type signature just above by ensuring the store's data is `T` which
369         // matches the return value.
370         unsafe { data.instance_pre() }
371     }
372 
373     pub(crate) fn id(&self) -> StoreComponentInstanceId {
374         self.id
375     }
376 
377     /// Implementation of the `resource.new` intrinsic for `i32`
378     /// representations.
379     pub(crate) fn resource_new32(
380         self,
381         store: &mut StoreOpaque,
382         caller: RuntimeComponentInstanceIndex,
383         ty: TypeResourceTableIndex,
384         rep: u32,
385     ) -> Result<u32> {
386         self.id().get(store).check_may_leave(caller)?;
387         let (calls, _, _, instance) = store.component_resource_state_with_instance(self);
388         resource_tables(calls, instance).resource_new(TypedResource::Component { ty, rep })
389     }
390 
391     /// Implementation of the `resource.rep` intrinsic for `i32`
392     /// representations.
393     pub(crate) fn resource_rep32(
394         self,
395         store: &mut StoreOpaque,
396         caller: RuntimeComponentInstanceIndex,
397         ty: TypeResourceTableIndex,
398         index: u32,
399     ) -> Result<u32> {
400         self.id().get(store).check_may_leave(caller)?;
401         let (calls, _, _, instance) = store.component_resource_state_with_instance(self);
402         resource_tables(calls, instance).resource_rep(TypedResourceIndex::Component { ty, index })
403     }
404 
405     /// Implementation of the `resource.drop` intrinsic.
406     pub(crate) fn resource_drop(
407         self,
408         store: &mut StoreOpaque,
409         caller: RuntimeComponentInstanceIndex,
410         ty: TypeResourceTableIndex,
411         index: u32,
412     ) -> Result<Option<u32>> {
413         self.id().get(store).check_may_leave(caller)?;
414         let (calls, _, _, instance) = store.component_resource_state_with_instance(self);
415         resource_tables(calls, instance).resource_drop(TypedResourceIndex::Component { ty, index })
416     }
417 
418     pub(crate) fn resource_transfer_own(
419         self,
420         store: &mut StoreOpaque,
421         index: u32,
422         src: TypeResourceTableIndex,
423         dst: TypeResourceTableIndex,
424     ) -> Result<u32> {
425         let (calls, _, _, instance) = store.component_resource_state_with_instance(self);
426         let mut tables = resource_tables(calls, instance);
427         let rep = tables.resource_lift_own(TypedResourceIndex::Component { ty: src, index })?;
428         tables.resource_lower_own(TypedResource::Component { ty: dst, rep })
429     }
430 
431     pub(crate) fn resource_transfer_borrow(
432         self,
433         store: &mut StoreOpaque,
434         index: u32,
435         src: TypeResourceTableIndex,
436         dst: TypeResourceTableIndex,
437     ) -> Result<u32> {
438         let dst_owns_resource = self.id().get(store).resource_owned_by_own_instance(dst);
439         let (calls, _, _, instance) = store.component_resource_state_with_instance(self);
440         let mut tables = resource_tables(calls, instance);
441         let rep = tables.resource_lift_borrow(TypedResourceIndex::Component { ty: src, index })?;
442         // Implement `lower_borrow`'s special case here where if a borrow's
443         // resource type is owned by `dst` then the destination receives the
444         // representation directly rather than a handle to the representation.
445         //
446         // This can perhaps become a different libcall in the future to avoid
447         // this check at runtime since we know at compile time whether the
448         // destination type owns the resource, but that's left as a future
449         // refactoring if truly necessary.
450         if dst_owns_resource {
451             return Ok(rep);
452         }
453         tables.resource_lower_borrow(TypedResource::Component { ty: dst, rep })
454     }
455 
456     pub(crate) fn resource_enter_call(self, store: &mut StoreOpaque) {
457         let (calls, _, _, instance) = store.component_resource_state_with_instance(self);
458         resource_tables(calls, instance).enter_call()
459     }
460 
461     pub(crate) fn resource_exit_call(self, store: &mut StoreOpaque) -> Result<()> {
462         let (calls, _, _, instance) = store.component_resource_state_with_instance(self);
463         resource_tables(calls, instance).exit_call()
464     }
465 
466     pub(crate) fn lookup_vmdef(&self, store: &mut StoreOpaque, def: &CoreDef) -> vm::Export {
467         lookup_vmdef(store, self.id.instance(), def)
468     }
469 
470     pub(crate) fn options<'a>(
471         &self,
472         store: &'a StoreOpaque,
473         options: OptionsIndex,
474     ) -> &'a CanonicalOptions {
475         &self.id.get(store).component().env_component().options[options]
476     }
477 
478     fn options_memory_raw(
479         &self,
480         store: &StoreOpaque,
481         options: OptionsIndex,
482     ) -> Option<NonNull<vm::VMMemoryDefinition>> {
483         let instance = self.id.get(store);
484         let options = &instance.component().env_component().options[options];
485         let memory = match options.data_model {
486             CanonicalOptionsDataModel::Gc { .. } => return None,
487             CanonicalOptionsDataModel::LinearMemory(o) => match o.memory {
488                 Some(m) => m,
489                 None => return None,
490             },
491         };
492 
493         Some(instance.runtime_memory(memory))
494     }
495 
496     pub(crate) fn options_memory<'a>(
497         &self,
498         store: &'a StoreOpaque,
499         options: OptionsIndex,
500     ) -> &'a [u8] {
501         let memory = match self.options_memory_raw(store, options) {
502             Some(m) => m,
503             None => return &[],
504         };
505         // SAFETY: we're borrowing the entire `StoreOpaque` which owns the
506         // memory allocation to return the result of memory. That means that the
507         // lifetime connection here should be safe and the actual ptr/length are
508         // trusted parts of the runtime here.
509         unsafe {
510             let memory = memory.as_ref();
511             core::slice::from_raw_parts(memory.base.as_ptr(), memory.current_length())
512         }
513     }
514 
515     pub(crate) fn options_memory_mut<'a>(
516         &self,
517         store: &'a mut StoreOpaque,
518         options: OptionsIndex,
519     ) -> &'a mut [u8] {
520         let memory = match self.options_memory_raw(store, options) {
521             Some(m) => m,
522             None => return &mut [],
523         };
524         // SAFETY: See `options_memory` comment above, and note that this is
525         // taking `&mut StoreOpaque` to thread the lifetime through instead.
526         unsafe {
527             let memory = memory.as_ref();
528             core::slice::from_raw_parts_mut(memory.base.as_ptr(), memory.current_length())
529         }
530     }
531 
532     /// Helper function to simultaneously get a borrow to this instance's
533     /// component as well as the store that this component is contained within.
534     ///
535     /// Note that this function signature is not possible with safe Rust, so
536     /// this is using `unsafe` internally.
537     pub(crate) fn component_and_store_mut<'a, S>(
538         &self,
539         store: &'a mut S,
540     ) -> (&'a Component, &'a mut S)
541     where
542         S: AsStoreOpaque,
543     {
544         let store_opaque = store.as_store_opaque();
545         let instance = self.id.get_mut(store_opaque);
546         let component = instance.component();
547 
548         // SAFETY: the goal of this function is to derive a pointer from
549         // `&mut S`, here `&Component`, and then return both so they can both be
550         // used at the same time. In general this is not safe operation since
551         // the original mutable pointer could be mutated or overwritten which
552         // would invalidate the derived pointer.
553         //
554         // In this case though we have a few guarantees which should make this
555         // safe:
556         //
557         // * Embedders never have the ability to overwrite a `StoreOpaque`. For
558         //   example the closest thing of `StoreContextMut` wraps up the
559         //   reference internally so it's inaccessible to the outside world.
560         //   This means that while mutations can still happen it's not possible
561         //   to overwrite a `StoreOpaque` directly.
562         //
563         // * Components are referred to by `vm::ComponentInstance` which holds a
564         //   strong reference. All `ComponentInstance` structures are allocated
565         //   within the store and unconditionally live as long as the entire
566         //   store itself. This means that there's no worry of the rooting
567         //   container going away or otherwise getting deallocated.
568         //
569         // * The `ComponentInstance` container has an invariant that after
570         //   creation the component used to create it cannot be changed. This is
571         //   enforced through `Pin<&mut ComponentInstance>` which disallows
572         //   mutable access to the `component` field, instead only allowing
573         //   read-only access.
574         //
575         // Putting all of this together it's not possible for a component,
576         // within a component instance, within a store, to be deallocated or mutated while
577         // a store is in use. Consequently it should be safe to simultaneously
578         // have a borrow to both at the same time, even if the store has a
579         // mutable borrow itself.
580         unsafe {
581             let component: *const Component = component;
582             (&*component, store)
583         }
584     }
585 }
586 
587 /// Translates a `CoreDef`, a definition of a core wasm item, to an
588 /// [`Export`] which is the runtime core wasm definition.
589 pub(crate) fn lookup_vmdef(
590     store: &mut StoreOpaque,
591     id: ComponentInstanceId,
592     def: &CoreDef,
593 ) -> vm::Export {
594     match def {
595         CoreDef::Export(e) => lookup_vmexport(store, id, e),
596         CoreDef::Trampoline(idx) => {
597             let funcref = store
598                 .store_data_mut()
599                 .component_instance_mut(id)
600                 .trampoline_func_ref(*idx);
601             // SAFETY: the `funcref` is owned by `store` and is valid within
602             // that store, so it's safe to create a `Func`.
603             vm::Export::Function(unsafe { crate::Func::from_vm_func_ref(store.id(), funcref) })
604         }
605         CoreDef::InstanceFlags(idx) => {
606             let id = StoreComponentInstanceId::new(store.id(), id);
607             vm::Export::Global(crate::Global::from_component_flags(id, *idx))
608         }
609         CoreDef::UnsafeIntrinsic(intrinsic) => {
610             let funcref = store
611                 .store_data_mut()
612                 .component_instance_mut(id)
613                 .unsafe_intrinsic_func_ref(*intrinsic);
614             // SAFETY: as above, the `funcref` is owned by `store` and is valid
615             // within that store, so it's safe to create a `Func`.
616             vm::Export::Function(unsafe { crate::Func::from_vm_func_ref(store.id(), funcref) })
617         }
618         CoreDef::TaskMayBlock => vm::Export::Global(crate::Global::from_task_may_block(
619             StoreComponentInstanceId::new(store.id(), id),
620         )),
621     }
622 }
623 
624 /// Translates a `CoreExport<T>`, an export of some core instance within
625 /// this component, to the actual runtime definition of that item.
626 pub(crate) fn lookup_vmexport<T>(
627     store: &mut StoreOpaque,
628     id: ComponentInstanceId,
629     item: &CoreExport<T>,
630 ) -> vm::Export
631 where
632     T: Copy + Into<EntityIndex>,
633 {
634     let store_id = store.id();
635     let id = store
636         .store_data_mut()
637         .component_instance_mut(id)
638         .instance(item.instance);
639     let (instance, registry) = store.instance_and_module_registry_mut(id);
640     let idx = match &item.item {
641         ExportItem::Index(idx) => (*idx).into(),
642 
643         // FIXME: ideally at runtime we don't actually do any name lookups
644         // here. This will only happen when the host supplies an imported
645         // module so while the structure can't be known at compile time we
646         // do know at `InstancePre` time, for example, what all the host
647         // imports are. In theory we should be able to, as part of
648         // `InstancePre` construction, perform all name=>index mappings
649         // during that phase so the actual instantiation of an `InstancePre`
650         // skips all string lookups. This should probably only be
651         // investigated if this becomes a performance issue though.
652         ExportItem::Name(name) => instance.env_module().exports[name],
653     };
654     // SAFETY: the `store_id` owns this instance and all exports contained
655     // within.
656     unsafe { instance.get_export_by_index_mut(registry, store_id, idx) }
657 }
658 
659 fn resource_tables<'a>(
660     calls: &'a mut CallContexts,
661     instance: Pin<&'a mut ComponentInstance>,
662 ) -> ResourceTables<'a> {
663     ResourceTables {
664         host_table: None,
665         calls,
666         guest: Some(instance.instance_states()),
667     }
668 }
669 
670 /// Trait used to lookup the export of a component instance.
671 ///
672 /// This trait is used as an implementation detail of [`Instance::get_func`]
673 /// and related `get_*` methods. Notable implementors of this trait are:
674 ///
675 /// * `str`
676 /// * `String`
677 /// * [`ComponentExportIndex`]
678 ///
679 /// Note that this is intended to be a `wasmtime`-sealed trait so it shouldn't
680 /// need to be implemented externally.
681 pub trait InstanceExportLookup {
682     #[doc(hidden)]
683     fn lookup(&self, component: &Component) -> Option<ExportIndex>;
684 }
685 
686 impl<T> InstanceExportLookup for &T
687 where
688     T: InstanceExportLookup + ?Sized,
689 {
690     fn lookup(&self, component: &Component) -> Option<ExportIndex> {
691         T::lookup(self, component)
692     }
693 }
694 
695 impl InstanceExportLookup for str {
696     fn lookup(&self, component: &Component) -> Option<ExportIndex> {
697         component
698             .env_component()
699             .exports
700             .get(self, &NameMapNoIntern)
701             .copied()
702     }
703 }
704 
705 impl InstanceExportLookup for String {
706     fn lookup(&self, component: &Component) -> Option<ExportIndex> {
707         str::lookup(self, component)
708     }
709 }
710 
711 struct Instantiator<'a> {
712     component: &'a Component,
713     id: ComponentInstanceId,
714     core_imports: OwnedImports,
715     imports: &'a PrimaryMap<RuntimeImportIndex, RuntimeImport>,
716 }
717 
718 pub(crate) enum RuntimeImport {
719     Func(Arc<HostFunc>),
720     Module(Module),
721     Resource {
722         ty: ResourceType,
723 
724         // A strong reference to the host function that represents the
725         // destructor for this resource. At this time all resources here are
726         // host-defined resources. Note that this is itself never read because
727         // the funcref below points to it.
728         //
729         // Also note that the `Arc` here is used to support the same host
730         // function being used across multiple instances simultaneously. Or
731         // otherwise this makes `InstancePre::instantiate` possible to create
732         // separate instances all sharing the same host function.
733         dtor: Arc<crate::func::HostFunc>,
734 
735         // A raw function which is filled out (including `wasm_call`) which
736         // points to the internals of the `_dtor` field. This is read and
737         // possibly executed by wasm.
738         dtor_funcref: VMFuncRef,
739     },
740 }
741 
742 pub type ImportedResources = PrimaryMap<ResourceIndex, ResourceType>;
743 
744 impl<'a> Instantiator<'a> {
745     fn new(
746         component: &'a Component,
747         store: &mut StoreOpaque,
748         imports: &'a Arc<PrimaryMap<RuntimeImportIndex, RuntimeImport>>,
749     ) -> Result<Instantiator<'a>> {
750         let env_component = component.env_component();
751         store.register_component(component)?;
752         let imported_resources: ImportedResources =
753             PrimaryMap::with_capacity(env_component.imported_resources.len());
754 
755         let instance = ComponentInstance::new(
756             store.store_data().components.next_component_instance_id(),
757             component,
758             Arc::new(imported_resources),
759             imports,
760             store.traitobj(),
761         )?;
762         let id = store.store_data_mut().push_component_instance(instance);
763 
764         Ok(Instantiator {
765             component,
766             imports,
767             core_imports: OwnedImports::empty(),
768             id,
769         })
770     }
771 
772     async fn run<T>(
773         &mut self,
774         store: &mut StoreContextMut<'_, T>,
775         asyncness: Asyncness,
776     ) -> Result<()> {
777         let env_component = self.component.env_component();
778 
779         // Before all initializers are processed configure all destructors for
780         // host-defined resources. No initializer will correspond to these and
781         // it's required to happen before they're needed, so execute this first.
782         for (idx, import) in env_component.imported_resources.iter() {
783             let (ty, func_ref) = match &self.imports[*import] {
784                 RuntimeImport::Resource {
785                     ty, dtor_funcref, ..
786                 } => (*ty, NonNull::from(dtor_funcref)),
787                 _ => unreachable!(),
788             };
789             let i = self.instance_resource_types_mut(store.0).push(ty);
790             assert_eq!(i, idx);
791             self.instance_mut(store.0)
792                 .set_resource_destructor(idx, Some(func_ref));
793         }
794 
795         // Next configure all `VMFuncRef`s for trampolines that this component
796         // will require. These functions won't actually get used until their
797         // associated state has been initialized through the global initializers
798         // below, but the funcrefs can all be configured here.
799         for (idx, sig) in env_component.trampolines.iter() {
800             let ptrs = self.component.trampoline_ptrs(idx);
801             let signature = match self.component.signatures().shared_type(*sig) {
802                 Some(s) => s,
803                 None => panic!("found unregistered signature: {sig:?}"),
804             };
805 
806             self.instance_mut(store.0).set_trampoline(
807                 idx,
808                 ptrs.wasm_call,
809                 ptrs.array_call,
810                 signature,
811             );
812         }
813 
814         // Initialize the unsafe intrinsics used by this component, if any.
815         for (i, module_ty) in env_component
816             .unsafe_intrinsics
817             .iter()
818             .enumerate()
819             .filter_map(|(i, ty)| ty.expand().map(|ty| (i, ty)))
820         {
821             let i = u32::try_from(i).unwrap();
822             let intrinsic = UnsafeIntrinsic::from_u32(i);
823             let ptrs = self.component.unsafe_intrinsic_ptrs(intrinsic).expect(
824                 "should have intrinsic pointers given that we assigned the intrinsic a type",
825             );
826             let shared_ty = self
827                 .component
828                 .signatures()
829                 .shared_type(module_ty)
830                 .expect("should have a shared type");
831             self.instance_mut(store.0).set_intrinsic(
832                 intrinsic,
833                 ptrs.wasm_call,
834                 ptrs.array_call,
835                 shared_ty,
836             );
837         }
838 
839         for initializer in env_component.initializers.iter() {
840             match initializer {
841                 GlobalInitializer::InstantiateModule(m, component_instance) => {
842                     let instance = self.id;
843                     let module;
844                     let imports = match m {
845                         // Since upvars are statically know we know that the
846                         // `args` list is already in the right order.
847                         InstantiateModule::Static(idx, args) => {
848                             module = self.component.static_module(*idx);
849                             self.build_imports(store.0, module, args.iter())
850                         }
851 
852                         // With imports, unlike upvars, we need to do runtime
853                         // lookups with strings to determine the order of the
854                         // imports since it's whatever the actual module
855                         // requires.
856                         //
857                         // FIXME: see the note in `ExportItem::Name` handling
858                         // above for how we ideally shouldn't do string lookup
859                         // here.
860                         InstantiateModule::Import(idx, args) => {
861                             module = match &self.imports[*idx] {
862                                 RuntimeImport::Module(m) => m,
863                                 _ => unreachable!(),
864                             };
865                             let args = module
866                                 .imports()
867                                 .map(|import| &args[import.module()][import.name()]);
868                             self.build_imports(store.0, module, args)
869                         }
870                     };
871 
872                     let exit = if let Some(component_instance) = *component_instance
873                         && store.0.concurrency_support()
874                     {
875                         store.0.enter_sync_call(
876                             None,
877                             false,
878                             RuntimeInstance {
879                                 instance,
880                                 index: component_instance,
881                             },
882                         )?;
883                         true
884                     } else {
885                         false
886                     };
887 
888                     // Note that the unsafety here should be ok because the
889                     // validity of the component means that type-checks have
890                     // already been performed. This means that the unsafety due
891                     // to imports having the wrong type should not happen here.
892                     //
893                     // Also note we are calling new_started_impl because we have
894                     // already checked for asyncness and are running on a fiber
895                     // if required.
896 
897                     let i = unsafe {
898                         crate::Instance::new_started(store, module, imports.as_ref(), asyncness)
899                             .await?
900                     };
901 
902                     if exit {
903                         store.0.exit_sync_call(false)?;
904                     }
905 
906                     self.instance_mut(store.0).push_instance_id(i.id());
907                 }
908 
909                 GlobalInitializer::LowerImport { import, index } => {
910                     let func = match &self.imports[*import] {
911                         RuntimeImport::Func(func) => func,
912                         _ => unreachable!(),
913                     };
914                     self.instance_mut(store.0)
915                         .set_lowering(*index, func.lowering());
916                 }
917 
918                 GlobalInitializer::ExtractTable(table) => self.extract_table(store.0, table),
919 
920                 GlobalInitializer::ExtractMemory(mem) => self.extract_memory(store.0, mem),
921 
922                 GlobalInitializer::ExtractRealloc(realloc) => {
923                     self.extract_realloc(store.0, realloc)
924                 }
925 
926                 GlobalInitializer::ExtractCallback(callback) => {
927                     self.extract_callback(store.0, callback)
928                 }
929 
930                 GlobalInitializer::ExtractPostReturn(post_return) => {
931                     self.extract_post_return(store.0, post_return)
932                 }
933 
934                 GlobalInitializer::Resource(r) => self.resource(store.0, r),
935             }
936         }
937         Ok(())
938     }
939 
940     fn resource(&mut self, store: &mut StoreOpaque, resource: &Resource) {
941         let dtor = resource
942             .dtor
943             .as_ref()
944             .map(|dtor| lookup_vmdef(store, self.id, dtor));
945         let dtor = dtor.map(|export| match export {
946             crate::runtime::vm::Export::Function(f) => f.vm_func_ref(store),
947             _ => unreachable!(),
948         });
949         let index = self
950             .component
951             .env_component()
952             .resource_index(resource.index);
953         let instance = self.instance(store);
954         let ty = ResourceType::guest(store.id(), instance, resource.index);
955         self.instance_mut(store)
956             .set_resource_destructor(index, dtor);
957         let i = self.instance_resource_types_mut(store).push(ty);
958         debug_assert_eq!(i, index);
959     }
960 
961     fn extract_memory(&mut self, store: &mut StoreOpaque, memory: &ExtractMemory) {
962         let import = match lookup_vmexport(store, self.id, &memory.export) {
963             crate::runtime::vm::Export::Memory(memory) => memory.vmimport(store),
964             crate::runtime::vm::Export::SharedMemory(_, import) => import,
965             _ => unreachable!(),
966         };
967         self.instance_mut(store)
968             .set_runtime_memory(memory.index, import.from.as_non_null());
969     }
970 
971     fn extract_realloc(&mut self, store: &mut StoreOpaque, realloc: &ExtractRealloc) {
972         let func_ref = match lookup_vmdef(store, self.id, &realloc.def) {
973             crate::runtime::vm::Export::Function(f) => f.vm_func_ref(store),
974             _ => unreachable!(),
975         };
976         self.instance_mut(store)
977             .set_runtime_realloc(realloc.index, func_ref);
978     }
979 
980     fn extract_callback(&mut self, store: &mut StoreOpaque, callback: &ExtractCallback) {
981         let func_ref = match lookup_vmdef(store, self.id, &callback.def) {
982             crate::runtime::vm::Export::Function(f) => f.vm_func_ref(store),
983             _ => unreachable!(),
984         };
985         self.instance_mut(store)
986             .set_runtime_callback(callback.index, func_ref);
987     }
988 
989     fn extract_post_return(&mut self, store: &mut StoreOpaque, post_return: &ExtractPostReturn) {
990         let func_ref = match lookup_vmdef(store, self.id, &post_return.def) {
991             crate::runtime::vm::Export::Function(f) => f.vm_func_ref(store),
992             _ => unreachable!(),
993         };
994         self.instance_mut(store)
995             .set_runtime_post_return(post_return.index, func_ref);
996     }
997 
998     fn extract_table(&mut self, store: &mut StoreOpaque, table: &ExtractTable) {
999         let export = match lookup_vmexport(store, self.id, &table.export) {
1000             crate::runtime::vm::Export::Table(t) => t,
1001             _ => unreachable!(),
1002         };
1003         let import = export.vmimport(store);
1004         self.instance_mut(store)
1005             .set_runtime_table(table.index, import);
1006     }
1007 
1008     fn build_imports<'b>(
1009         &mut self,
1010         store: &mut StoreOpaque,
1011         module: &Module,
1012         args: impl Iterator<Item = &'b CoreDef>,
1013     ) -> &OwnedImports {
1014         self.core_imports.clear();
1015         self.core_imports.reserve(module);
1016         let mut imports = module.compiled_module().module().imports();
1017 
1018         for arg in args {
1019             // The general idea of Wasmtime is that at runtime type-checks for
1020             // core wasm instantiations internally within a component are
1021             // unnecessary and superfluous. Naturally though mistakes may be
1022             // made, so double-check this property of wasmtime in debug mode.
1023 
1024             if cfg!(debug_assertions) {
1025                 let (imp_module, imp_name, expected) = imports.next().unwrap();
1026                 self.assert_type_matches(store, module, arg, imp_module, imp_name, expected);
1027             }
1028 
1029             // The unsafety here should be ok since the `export` is loaded
1030             // directly from an instance which should only give us valid export
1031             // items.
1032             let export = lookup_vmdef(store, self.id, arg);
1033             self.core_imports.push_export(store, &export);
1034         }
1035         debug_assert!(imports.next().is_none());
1036 
1037         &self.core_imports
1038     }
1039 
1040     fn assert_type_matches(
1041         &self,
1042         store: &mut StoreOpaque,
1043         module: &Module,
1044         arg: &CoreDef,
1045         imp_module: &str,
1046         imp_name: &str,
1047         expected: EntityType,
1048     ) {
1049         let export = lookup_vmdef(store, self.id, arg);
1050 
1051         // If this value is a core wasm function then the type check is inlined
1052         // here. This can otherwise fail `Extern::from_wasmtime_export` because
1053         // there's no guarantee that there exists a trampoline for `f` so this
1054         // can't fall through to the case below
1055         if let crate::runtime::vm::Export::Function(f) = &export {
1056             let expected = match expected.unwrap_func() {
1057                 EngineOrModuleTypeIndex::Engine(e) => Some(e),
1058                 EngineOrModuleTypeIndex::Module(m) => module.signatures().shared_type(m),
1059                 EngineOrModuleTypeIndex::RecGroup(_) => unreachable!(),
1060             };
1061             let actual = unsafe { f.vm_func_ref(store).as_ref().type_index };
1062             assert_eq!(
1063                 expected,
1064                 Some(actual),
1065                 "type mismatch for import {imp_module:?} {imp_name:?}!!!\n\n\
1066                  expected {:#?}\n\n\
1067                  found {:#?}",
1068                 expected.and_then(|e| store.engine().signatures().borrow(e)),
1069                 store.engine().signatures().borrow(actual)
1070             );
1071             return;
1072         }
1073 
1074         let val = crate::Extern::from_wasmtime_export(export, store);
1075         let ty = DefinitionType::from(store, &val);
1076         crate::types::matching::MatchCx::new(module.engine())
1077             .definition(&expected, &ty)
1078             .expect("unexpected typecheck failure");
1079     }
1080 
1081     /// Convenience helper to return the `&ComponentInstance` that's being
1082     /// instantiated.
1083     fn instance<'b>(&self, store: &'b StoreOpaque) -> &'b ComponentInstance {
1084         store.store_data().component_instance(self.id)
1085     }
1086 
1087     /// Same as [`Self::instance`], but for mutability.
1088     fn instance_mut<'b>(&self, store: &'b mut StoreOpaque) -> Pin<&'b mut ComponentInstance> {
1089         store.store_data_mut().component_instance_mut(self.id)
1090     }
1091 
1092     // NB: This method is only intended to be called during the instantiation
1093     // process because the `Arc::get_mut` here is fallible and won't generally
1094     // succeed once the instance has been handed to the embedder. Before that
1095     // though it should be guaranteed that the single owning reference currently
1096     // lives within the `ComponentInstance` that's being built.
1097     fn instance_resource_types_mut<'b>(
1098         &self,
1099         store: &'b mut StoreOpaque,
1100     ) -> &'b mut ImportedResources {
1101         Arc::get_mut(self.instance_mut(store).resource_types_mut()).unwrap()
1102     }
1103 }
1104 
1105 /// A "pre-instantiated" [`Instance`] which has all of its arguments already
1106 /// supplied and is ready to instantiate.
1107 ///
1108 /// This structure represents an efficient form of instantiation where import
1109 /// type-checking and import lookup has all been resolved by the time that this
1110 /// type is created. This type is primarily created through the
1111 /// [`Linker::instantiate_pre`](crate::component::Linker::instantiate_pre)
1112 /// method.
1113 pub struct InstancePre<T: 'static> {
1114     component: Component,
1115     imports: Arc<PrimaryMap<RuntimeImportIndex, RuntimeImport>>,
1116     resource_types: Arc<PrimaryMap<ResourceIndex, ResourceType>>,
1117     asyncness: Asyncness,
1118     _marker: marker::PhantomData<fn() -> T>,
1119 }
1120 
1121 // `InstancePre`'s clone does not require `T: Clone`
1122 impl<T: 'static> Clone for InstancePre<T> {
1123     fn clone(&self) -> Self {
1124         Self {
1125             component: self.component.clone(),
1126             imports: self.imports.clone(),
1127             resource_types: self.resource_types.clone(),
1128             asyncness: self.asyncness,
1129             _marker: self._marker,
1130         }
1131     }
1132 }
1133 
1134 impl<T: 'static> InstancePre<T> {
1135     /// This function is `unsafe` since there's no guarantee that the
1136     /// `RuntimeImport` items provided are guaranteed to work with the `T` of
1137     /// the store.
1138     ///
1139     /// Additionally there is no static guarantee that the `imports` provided
1140     /// satisfy the imports of the `component` provided.
1141     pub(crate) unsafe fn new_unchecked(
1142         component: Component,
1143         imports: Arc<PrimaryMap<RuntimeImportIndex, RuntimeImport>>,
1144         resource_types: Arc<PrimaryMap<ResourceIndex, ResourceType>>,
1145     ) -> InstancePre<T> {
1146         let mut asyncness = Asyncness::No;
1147         for (_, import) in imports.iter() {
1148             asyncness = asyncness
1149                 | match import {
1150                     RuntimeImport::Func(f) => f.asyncness(),
1151                     RuntimeImport::Module(_) => Asyncness::No,
1152                     RuntimeImport::Resource { dtor, .. } => dtor.asyncness(),
1153                 };
1154         }
1155         InstancePre {
1156             component,
1157             imports,
1158             resource_types,
1159             asyncness,
1160             _marker: marker::PhantomData,
1161         }
1162     }
1163 
1164     /// Returns the underlying component that will be instantiated.
1165     pub fn component(&self) -> &Component {
1166         &self.component
1167     }
1168 
1169     #[doc(hidden)]
1170     /// Returns the type at which the underlying component will be
1171     /// instantiated. This contains the instantiated type information which
1172     /// was determined by the Linker.
1173     pub fn instance_type(&self) -> InstanceType<'_> {
1174         InstanceType {
1175             types: &self.component.types(),
1176             resources: &self.resource_types,
1177         }
1178     }
1179 
1180     /// Returns the underlying engine.
1181     pub fn engine(&self) -> &Engine {
1182         self.component.engine()
1183     }
1184 
1185     /// Performs the instantiation process into the store specified.
1186     //
1187     // TODO: needs more docs
1188     pub fn instantiate(&self, mut store: impl AsContextMut<Data = T>) -> Result<Instance> {
1189         let store = store.as_context_mut();
1190 
1191         // If this instance requires an async host, set that flag in the store,
1192         // and then afterwards assert nothing else in the store, nor this
1193         // instance, required async.
1194         store.0.set_async_required(self.asyncness);
1195         store.0.validate_sync_call()?;
1196 
1197         vm::assert_ready(self._instantiate(store, Asyncness::No))
1198     }
1199     /// Performs the instantiation process into the store specified.
1200     ///
1201     /// Exactly like [`Self::instantiate`] except for use on async stores.
1202     //
1203     // TODO: needs more docs
1204     #[cfg(feature = "async")]
1205     pub async fn instantiate_async(&self, store: impl AsContextMut<Data = T>) -> Result<Instance> {
1206         self._instantiate(store, Asyncness::Yes).await
1207     }
1208 
1209     async fn _instantiate(
1210         &self,
1211         mut store: impl AsContextMut<Data = T>,
1212         asyncness: Asyncness,
1213     ) -> Result<Instance> {
1214         let mut store = store.as_context_mut();
1215         store.0.set_async_required(self.asyncness);
1216         store
1217             .engine()
1218             .allocator()
1219             .increment_component_instance_count()?;
1220         let mut instantiator = Instantiator::new(&self.component, store.0, &self.imports)?;
1221         instantiator.run(&mut store, asyncness).await.map_err(|e| {
1222             store
1223                 .engine()
1224                 .allocator()
1225                 .decrement_component_instance_count();
1226             e
1227         })?;
1228 
1229         let instance = Instance::from_wasmtime(store.0, instantiator.id);
1230         store.0.push_component_instance(instance);
1231         Ok(instance)
1232     }
1233 }
1234