Skip to main content

wasmtime/runtime/component/
instance.rs

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