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), true) = (
873                         component_instance,
874                         store.engine().config().cm_concurrency_enabled(),
875                     ) {
876                         store.0.enter_sync_call(
877                             None,
878                             false,
879                             RuntimeInstance {
880                                 instance,
881                                 index: component_instance,
882                             },
883                         )?;
884                         true
885                     } else {
886                         false
887                     };
888 
889                     // Note that the unsafety here should be ok because the
890                     // validity of the component means that type-checks have
891                     // already been performed. This means that the unsafety due
892                     // to imports having the wrong type should not happen here.
893                     //
894                     // Also note we are calling new_started_impl because we have
895                     // already checked for asyncness and are running on a fiber
896                     // if required.
897 
898                     let i = unsafe {
899                         crate::Instance::new_started(store, module, imports.as_ref(), asyncness)
900                             .await?
901                     };
902 
903                     if exit {
904                         store.0.exit_sync_call(false)?;
905                     }
906 
907                     self.instance_mut(store.0).push_instance_id(i.id());
908                 }
909 
910                 GlobalInitializer::LowerImport { import, index } => {
911                     let func = match &self.imports[*import] {
912                         RuntimeImport::Func(func) => func,
913                         _ => unreachable!(),
914                     };
915                     self.instance_mut(store.0)
916                         .set_lowering(*index, func.lowering());
917                 }
918 
919                 GlobalInitializer::ExtractTable(table) => self.extract_table(store.0, table),
920 
921                 GlobalInitializer::ExtractMemory(mem) => self.extract_memory(store.0, mem),
922 
923                 GlobalInitializer::ExtractRealloc(realloc) => {
924                     self.extract_realloc(store.0, realloc)
925                 }
926 
927                 GlobalInitializer::ExtractCallback(callback) => {
928                     self.extract_callback(store.0, callback)
929                 }
930 
931                 GlobalInitializer::ExtractPostReturn(post_return) => {
932                     self.extract_post_return(store.0, post_return)
933                 }
934 
935                 GlobalInitializer::Resource(r) => self.resource(store.0, r),
936             }
937         }
938         Ok(())
939     }
940 
941     fn resource(&mut self, store: &mut StoreOpaque, resource: &Resource) {
942         let dtor = resource
943             .dtor
944             .as_ref()
945             .map(|dtor| lookup_vmdef(store, self.id, dtor));
946         let dtor = dtor.map(|export| match export {
947             crate::runtime::vm::Export::Function(f) => f.vm_func_ref(store),
948             _ => unreachable!(),
949         });
950         let index = self
951             .component
952             .env_component()
953             .resource_index(resource.index);
954         let instance = self.instance(store);
955         let ty = ResourceType::guest(store.id(), instance, resource.index);
956         self.instance_mut(store)
957             .set_resource_destructor(index, dtor);
958         let i = self.instance_resource_types_mut(store).push(ty);
959         debug_assert_eq!(i, index);
960     }
961 
962     fn extract_memory(&mut self, store: &mut StoreOpaque, memory: &ExtractMemory) {
963         let import = match lookup_vmexport(store, self.id, &memory.export) {
964             crate::runtime::vm::Export::Memory(memory) => memory.vmimport(store),
965             crate::runtime::vm::Export::SharedMemory(_, import) => import,
966             _ => unreachable!(),
967         };
968         self.instance_mut(store)
969             .set_runtime_memory(memory.index, import.from.as_non_null());
970     }
971 
972     fn extract_realloc(&mut self, store: &mut StoreOpaque, realloc: &ExtractRealloc) {
973         let func_ref = match lookup_vmdef(store, self.id, &realloc.def) {
974             crate::runtime::vm::Export::Function(f) => f.vm_func_ref(store),
975             _ => unreachable!(),
976         };
977         self.instance_mut(store)
978             .set_runtime_realloc(realloc.index, func_ref);
979     }
980 
981     fn extract_callback(&mut self, store: &mut StoreOpaque, callback: &ExtractCallback) {
982         let func_ref = match lookup_vmdef(store, self.id, &callback.def) {
983             crate::runtime::vm::Export::Function(f) => f.vm_func_ref(store),
984             _ => unreachable!(),
985         };
986         self.instance_mut(store)
987             .set_runtime_callback(callback.index, func_ref);
988     }
989 
990     fn extract_post_return(&mut self, store: &mut StoreOpaque, post_return: &ExtractPostReturn) {
991         let func_ref = match lookup_vmdef(store, self.id, &post_return.def) {
992             crate::runtime::vm::Export::Function(f) => f.vm_func_ref(store),
993             _ => unreachable!(),
994         };
995         self.instance_mut(store)
996             .set_runtime_post_return(post_return.index, func_ref);
997     }
998 
999     fn extract_table(&mut self, store: &mut StoreOpaque, table: &ExtractTable) {
1000         let export = match lookup_vmexport(store, self.id, &table.export) {
1001             crate::runtime::vm::Export::Table(t) => t,
1002             _ => unreachable!(),
1003         };
1004         let import = export.vmimport(store);
1005         self.instance_mut(store)
1006             .set_runtime_table(table.index, import);
1007     }
1008 
1009     fn build_imports<'b>(
1010         &mut self,
1011         store: &mut StoreOpaque,
1012         module: &Module,
1013         args: impl Iterator<Item = &'b CoreDef>,
1014     ) -> &OwnedImports {
1015         self.core_imports.clear();
1016         self.core_imports.reserve(module);
1017         let mut imports = module.compiled_module().module().imports();
1018 
1019         for arg in args {
1020             // The general idea of Wasmtime is that at runtime type-checks for
1021             // core wasm instantiations internally within a component are
1022             // unnecessary and superfluous. Naturally though mistakes may be
1023             // made, so double-check this property of wasmtime in debug mode.
1024 
1025             if cfg!(debug_assertions) {
1026                 let (imp_module, imp_name, expected) = imports.next().unwrap();
1027                 self.assert_type_matches(store, module, arg, imp_module, imp_name, expected);
1028             }
1029 
1030             // The unsafety here should be ok since the `export` is loaded
1031             // directly from an instance which should only give us valid export
1032             // items.
1033             let export = lookup_vmdef(store, self.id, arg);
1034             self.core_imports.push_export(store, &export);
1035         }
1036         debug_assert!(imports.next().is_none());
1037 
1038         &self.core_imports
1039     }
1040 
1041     fn assert_type_matches(
1042         &self,
1043         store: &mut StoreOpaque,
1044         module: &Module,
1045         arg: &CoreDef,
1046         imp_module: &str,
1047         imp_name: &str,
1048         expected: EntityType,
1049     ) {
1050         let export = lookup_vmdef(store, self.id, arg);
1051 
1052         // If this value is a core wasm function then the type check is inlined
1053         // here. This can otherwise fail `Extern::from_wasmtime_export` because
1054         // there's no guarantee that there exists a trampoline for `f` so this
1055         // can't fall through to the case below
1056         if let crate::runtime::vm::Export::Function(f) = &export {
1057             let expected = match expected.unwrap_func() {
1058                 EngineOrModuleTypeIndex::Engine(e) => Some(e),
1059                 EngineOrModuleTypeIndex::Module(m) => module.signatures().shared_type(m),
1060                 EngineOrModuleTypeIndex::RecGroup(_) => unreachable!(),
1061             };
1062             let actual = unsafe { f.vm_func_ref(store).as_ref().type_index };
1063             assert_eq!(
1064                 expected,
1065                 Some(actual),
1066                 "type mismatch for import {imp_module:?} {imp_name:?}!!!\n\n\
1067                  expected {:#?}\n\n\
1068                  found {:#?}",
1069                 expected.and_then(|e| store.engine().signatures().borrow(e)),
1070                 store.engine().signatures().borrow(actual)
1071             );
1072             return;
1073         }
1074 
1075         let val = crate::Extern::from_wasmtime_export(export, store);
1076         let ty = DefinitionType::from(store, &val);
1077         crate::types::matching::MatchCx::new(module.engine())
1078             .definition(&expected, &ty)
1079             .expect("unexpected typecheck failure");
1080     }
1081 
1082     /// Convenience helper to return the `&ComponentInstance` that's being
1083     /// instantiated.
1084     fn instance<'b>(&self, store: &'b StoreOpaque) -> &'b ComponentInstance {
1085         store.store_data().component_instance(self.id)
1086     }
1087 
1088     /// Same as [`Self::instance`], but for mutability.
1089     fn instance_mut<'b>(&self, store: &'b mut StoreOpaque) -> Pin<&'b mut ComponentInstance> {
1090         store.store_data_mut().component_instance_mut(self.id)
1091     }
1092 
1093     // NB: This method is only intended to be called during the instantiation
1094     // process because the `Arc::get_mut` here is fallible and won't generally
1095     // succeed once the instance has been handed to the embedder. Before that
1096     // though it should be guaranteed that the single owning reference currently
1097     // lives within the `ComponentInstance` that's being built.
1098     fn instance_resource_types_mut<'b>(
1099         &self,
1100         store: &'b mut StoreOpaque,
1101     ) -> &'b mut ImportedResources {
1102         Arc::get_mut(self.instance_mut(store).resource_types_mut()).unwrap()
1103     }
1104 }
1105 
1106 /// A "pre-instantiated" [`Instance`] which has all of its arguments already
1107 /// supplied and is ready to instantiate.
1108 ///
1109 /// This structure represents an efficient form of instantiation where import
1110 /// type-checking and import lookup has all been resolved by the time that this
1111 /// type is created. This type is primarily created through the
1112 /// [`Linker::instantiate_pre`](crate::component::Linker::instantiate_pre)
1113 /// method.
1114 pub struct InstancePre<T: 'static> {
1115     component: Component,
1116     imports: Arc<PrimaryMap<RuntimeImportIndex, RuntimeImport>>,
1117     resource_types: Arc<PrimaryMap<ResourceIndex, ResourceType>>,
1118     asyncness: Asyncness,
1119     _marker: marker::PhantomData<fn() -> T>,
1120 }
1121 
1122 // `InstancePre`'s clone does not require `T: Clone`
1123 impl<T: 'static> Clone for InstancePre<T> {
1124     fn clone(&self) -> Self {
1125         Self {
1126             component: self.component.clone(),
1127             imports: self.imports.clone(),
1128             resource_types: self.resource_types.clone(),
1129             asyncness: self.asyncness,
1130             _marker: self._marker,
1131         }
1132     }
1133 }
1134 
1135 impl<T: 'static> InstancePre<T> {
1136     /// This function is `unsafe` since there's no guarantee that the
1137     /// `RuntimeImport` items provided are guaranteed to work with the `T` of
1138     /// the store.
1139     ///
1140     /// Additionally there is no static guarantee that the `imports` provided
1141     /// satisfy the imports of the `component` provided.
1142     pub(crate) unsafe fn new_unchecked(
1143         component: Component,
1144         imports: Arc<PrimaryMap<RuntimeImportIndex, RuntimeImport>>,
1145         resource_types: Arc<PrimaryMap<ResourceIndex, ResourceType>>,
1146     ) -> InstancePre<T> {
1147         let mut asyncness = Asyncness::No;
1148         for (_, import) in imports.iter() {
1149             asyncness = asyncness
1150                 | match import {
1151                     RuntimeImport::Func(f) => f.asyncness(),
1152                     RuntimeImport::Module(_) => Asyncness::No,
1153                     RuntimeImport::Resource { dtor, .. } => dtor.asyncness(),
1154                 };
1155         }
1156         InstancePre {
1157             component,
1158             imports,
1159             resource_types,
1160             asyncness,
1161             _marker: marker::PhantomData,
1162         }
1163     }
1164 
1165     /// Returns the underlying component that will be instantiated.
1166     pub fn component(&self) -> &Component {
1167         &self.component
1168     }
1169 
1170     #[doc(hidden)]
1171     /// Returns the type at which the underlying component will be
1172     /// instantiated. This contains the instantiated type information which
1173     /// was determined by the Linker.
1174     pub fn instance_type(&self) -> InstanceType<'_> {
1175         InstanceType {
1176             types: &self.component.types(),
1177             resources: &self.resource_types,
1178         }
1179     }
1180 
1181     /// Returns the underlying engine.
1182     pub fn engine(&self) -> &Engine {
1183         self.component.engine()
1184     }
1185 
1186     /// Performs the instantiation process into the store specified.
1187     //
1188     // TODO: needs more docs
1189     pub fn instantiate(&self, mut store: impl AsContextMut<Data = T>) -> Result<Instance> {
1190         let store = store.as_context_mut();
1191 
1192         // If this instance requires an async host, set that flag in the store,
1193         // and then afterwards assert nothing else in the store, nor this
1194         // instance, required async.
1195         store.0.set_async_required(self.asyncness);
1196         store.0.validate_sync_call()?;
1197 
1198         vm::assert_ready(self._instantiate(store, Asyncness::No))
1199     }
1200     /// Performs the instantiation process into the store specified.
1201     ///
1202     /// Exactly like [`Self::instantiate`] except for use on async stores.
1203     //
1204     // TODO: needs more docs
1205     #[cfg(feature = "async")]
1206     pub async fn instantiate_async(&self, store: impl AsContextMut<Data = T>) -> Result<Instance> {
1207         self._instantiate(store, Asyncness::Yes).await
1208     }
1209 
1210     async fn _instantiate(
1211         &self,
1212         mut store: impl AsContextMut<Data = T>,
1213         asyncness: Asyncness,
1214     ) -> Result<Instance> {
1215         let mut store = store.as_context_mut();
1216         store.0.set_async_required(self.asyncness);
1217         store
1218             .engine()
1219             .allocator()
1220             .increment_component_instance_count()?;
1221         let mut instantiator = Instantiator::new(&self.component, store.0, &self.imports)?;
1222         instantiator.run(&mut store, asyncness).await.map_err(|e| {
1223             store
1224                 .engine()
1225                 .allocator()
1226                 .decrement_component_instance_count();
1227             e
1228         })?;
1229 
1230         let instance = Instance::from_wasmtime(store.0, instantiator.id);
1231         store.0.push_component_instance(instance);
1232         Ok(instance)
1233     }
1234 }
1235