1 use crate::component::matching::InstanceType;
2 use crate::component::types;
3 use crate::component::InstanceExportLookup;
4 use crate::prelude::*;
5 use crate::runtime::vm::component::ComponentRuntimeInfo;
6 #[cfg(feature = "std")]
7 use crate::runtime::vm::open_file_for_mmap;
8 use crate::runtime::vm::{
9     CompiledModuleId, VMArrayCallFunction, VMFuncRef, VMFunctionBody, VMWasmCallFunction,
10 };
11 use crate::{
12     code::CodeObject, code_memory::CodeMemory, type_registry::TypeCollection, Engine, Module,
13     ResourcesRequired,
14 };
15 use crate::{FuncType, ValType};
16 use alloc::sync::Arc;
17 use core::any::Any;
18 use core::ops::Range;
19 use core::ptr::NonNull;
20 #[cfg(feature = "std")]
21 use std::path::Path;
22 use wasmtime_environ::component::{
23     AllCallFunc, CompiledComponentInfo, ComponentArtifacts, ComponentTypes, Export, ExportIndex,
24     GlobalInitializer, InstantiateModule, NameMapNoIntern, StaticModuleIndex, TrampolineIndex,
25     TypeComponentIndex, TypeDef, VMComponentOffsets,
26 };
27 use wasmtime_environ::TypeTrace;
28 use wasmtime_environ::{FunctionLoc, HostPtr, ObjectKind, PrimaryMap};
29 
30 /// A compiled WebAssembly Component.
31 ///
32 /// This structure represents a compiled component that is ready to be
33 /// instantiated. This owns a region of virtual memory which contains executable
34 /// code compiled from a WebAssembly binary originally. This is the analog of
35 /// [`Module`](crate::Module) in the component embedding API.
36 ///
37 /// A [`Component`] can be turned into an
38 /// [`Instance`](crate::component::Instance) through a
39 /// [`Linker`](crate::component::Linker). [`Component`]s are safe to share
40 /// across threads. The compilation model of a component is the same as that of
41 /// [a module](crate::Module) which is to say:
42 ///
43 /// * Compilation happens synchronously during [`Component::new`].
44 /// * The result of compilation can be saved into storage with
45 ///   [`Component::serialize`].
46 /// * A previously compiled artifact can be parsed with
47 ///   [`Component::deserialize`].
48 /// * No compilation happens at runtime for a component — everything is done
49 ///   by the time [`Component::new`] returns.
50 ///
51 /// ## Components and `Clone`
52 ///
53 /// Using `clone` on a `Component` is a cheap operation. It will not create an
54 /// entirely new component, but rather just a new reference to the existing
55 /// component. In other words it's a shallow copy, not a deep copy.
56 ///
57 /// ## Examples
58 ///
59 /// For example usage see the documentation of [`Module`](crate::Module) as
60 /// [`Component`] has the same high-level API.
61 #[derive(Clone)]
62 pub struct Component {
63     inner: Arc<ComponentInner>,
64 }
65 
66 struct ComponentInner {
67     /// Unique id for this component within this process.
68     ///
69     /// Note that this is repurposing ids for modules intentionally as there
70     /// shouldn't be an issue overlapping them.
71     id: CompiledModuleId,
72 
73     /// The engine that this component belongs to.
74     engine: Engine,
75 
76     /// Component type index
77     ty: TypeComponentIndex,
78 
79     /// Core wasm modules that the component defined internally, indexed by the
80     /// compile-time-assigned `ModuleUpvarIndex`.
81     static_modules: PrimaryMap<StaticModuleIndex, Module>,
82 
83     /// Code-related information such as the compiled artifact, type
84     /// information, etc.
85     ///
86     /// Note that the `Arc` here is used to share this allocation with internal
87     /// modules.
88     code: Arc<CodeObject>,
89 
90     /// Metadata produced during compilation.
91     info: CompiledComponentInfo,
92 
93     /// A cached handle to the `wasmtime::FuncType` for the canonical ABI's
94     /// `realloc`, to avoid the need to look up types in the registry and take
95     /// locks when calling `realloc` via `TypedFunc::call_raw`.
96     realloc_func_type: Arc<dyn Any + Send + Sync>,
97 }
98 
99 pub(crate) struct AllCallFuncPointers {
100     pub wasm_call: NonNull<VMWasmCallFunction>,
101     pub array_call: NonNull<VMArrayCallFunction>,
102 }
103 
104 impl Component {
105     /// Compiles a new WebAssembly component from the in-memory list of bytes
106     /// provided.
107     ///
108     /// The `bytes` provided can either be the binary or text format of a
109     /// [WebAssembly component]. Note that the text format requires the `wat`
110     /// feature of this crate to be enabled. This API does not support
111     /// streaming compilation.
112     ///
113     /// This function will synchronously validate the entire component,
114     /// including all core modules, and then compile all components, modules,
115     /// etc., found within the provided bytes.
116     ///
117     /// [WebAssembly component]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/Binary.md
118     ///
119     /// # Errors
120     ///
121     /// This function may fail and return an error. Errors may include
122     /// situations such as:
123     ///
124     /// * The binary provided could not be decoded because it's not a valid
125     ///   WebAssembly binary
126     /// * The WebAssembly binary may not validate (e.g. contains type errors)
127     /// * Implementation-specific limits were exceeded with a valid binary (for
128     ///   example too many locals)
129     /// * The wasm binary may use features that are not enabled in the
130     ///   configuration of `engine`
131     /// * If the `wat` feature is enabled and the input is text, then it may be
132     ///   rejected if it fails to parse.
133     ///
134     /// The error returned should contain full information about why compilation
135     /// failed.
136     ///
137     /// # Examples
138     ///
139     /// The `new` function can be invoked with a in-memory array of bytes:
140     ///
141     /// ```no_run
142     /// # use wasmtime::*;
143     /// # use wasmtime::component::Component;
144     /// # fn main() -> anyhow::Result<()> {
145     /// # let engine = Engine::default();
146     /// # let wasm_bytes: Vec<u8> = Vec::new();
147     /// let component = Component::new(&engine, &wasm_bytes)?;
148     /// # Ok(())
149     /// # }
150     /// ```
151     ///
152     /// Or you can also pass in a string to be parsed as the wasm text
153     /// format:
154     ///
155     /// ```
156     /// # use wasmtime::*;
157     /// # use wasmtime::component::Component;
158     /// # fn main() -> anyhow::Result<()> {
159     /// # let engine = Engine::default();
160     /// let component = Component::new(&engine, "(component (core module))")?;
161     /// # Ok(())
162     /// # }
163     #[cfg(any(feature = "cranelift", feature = "winch"))]
164     pub fn new(engine: &Engine, bytes: impl AsRef<[u8]>) -> Result<Component> {
165         crate::CodeBuilder::new(engine)
166             .wasm_binary_or_text(bytes.as_ref(), None)?
167             .compile_component()
168     }
169 
170     /// Compiles a new WebAssembly component from a wasm file on disk pointed
171     /// to by `file`.
172     ///
173     /// This is a convenience function for reading the contents of `file` on
174     /// disk and then calling [`Component::new`].
175     #[cfg(all(feature = "std", any(feature = "cranelift", feature = "winch")))]
176     pub fn from_file(engine: &Engine, file: impl AsRef<Path>) -> Result<Component> {
177         crate::CodeBuilder::new(engine)
178             .wasm_binary_or_text_file(file.as_ref())?
179             .compile_component()
180     }
181 
182     /// Compiles a new WebAssembly component from the in-memory wasm image
183     /// provided.
184     ///
185     /// This function is the same as [`Component::new`] except that it does not
186     /// accept the text format of WebAssembly. Even if the `wat` feature
187     /// is enabled an error will be returned here if `binary` is the text
188     /// format.
189     ///
190     /// For more information on semantics and errors see [`Component::new`].
191     #[cfg(any(feature = "cranelift", feature = "winch"))]
192     pub fn from_binary(engine: &Engine, binary: &[u8]) -> Result<Component> {
193         crate::CodeBuilder::new(engine)
194             .wasm_binary(binary, None)?
195             .compile_component()
196     }
197 
198     /// Same as [`Module::deserialize`], but for components.
199     ///
200     /// Note that the bytes referenced here must contain contents previously
201     /// produced by [`Engine::precompile_component`] or
202     /// [`Component::serialize`].
203     ///
204     /// For more information see the [`Module::deserialize`] method.
205     ///
206     /// # Unsafety
207     ///
208     /// The unsafety of this method is the same as that of the
209     /// [`Module::deserialize`] method.
210     ///
211     /// [`Module::deserialize`]: crate::Module::deserialize
212     pub unsafe fn deserialize(engine: &Engine, bytes: impl AsRef<[u8]>) -> Result<Component> {
213         let code = engine.load_code_bytes(bytes.as_ref(), ObjectKind::Component)?;
214         Component::from_parts(engine, code, None)
215     }
216 
217     /// Same as [`Module::deserialize_raw`], but for components.
218     ///
219     /// See [`Component::deserialize`] for additional information; this method
220     /// works identically except that it will not create a copy of the provided
221     /// memory but will use it directly.
222     ///
223     /// # Unsafety
224     ///
225     /// All of the safety notes from [`Component::deserialize`] apply here as well
226     /// with the additional constraint that the code memory provide by `memory`
227     /// lives for as long as the module and is nevery externally modified for
228     /// the lifetime of the deserialized module.
229     pub unsafe fn deserialize_raw(engine: &Engine, memory: NonNull<[u8]>) -> Result<Component> {
230         let code = engine.load_code_raw(memory, ObjectKind::Component)?;
231         Component::from_parts(engine, code, None)
232     }
233 
234     /// Same as [`Module::deserialize_file`], but for components.
235     ///
236     /// Note that the file referenced here must contain contents previously
237     /// produced by [`Engine::precompile_component`] or
238     /// [`Component::serialize`].
239     ///
240     /// For more information see the [`Module::deserialize_file`] method.
241     ///
242     /// # Unsafety
243     ///
244     /// The unsafety of this method is the same as that of the
245     /// [`Module::deserialize_file`] method.
246     ///
247     /// [`Module::deserialize_file`]: crate::Module::deserialize_file
248     #[cfg(feature = "std")]
249     pub unsafe fn deserialize_file(engine: &Engine, path: impl AsRef<Path>) -> Result<Component> {
250         let file = open_file_for_mmap(path.as_ref())?;
251         let code = engine
252             .load_code_file(file, ObjectKind::Component)
253             .with_context(|| format!("failed to load code for: {}", path.as_ref().display()))?;
254         Component::from_parts(engine, code, None)
255     }
256 
257     /// Returns the type of this component as a [`types::Component`].
258     ///
259     /// This method enables runtime introspection of the type of a component
260     /// before instantiation, if necessary.
261     ///
262     /// ## Component types and Resources
263     ///
264     /// An important point to note here is that the precise type of imports and
265     /// exports of a component change when it is instantiated with respect to
266     /// resources. For example a [`Component`] represents an un-instantiated
267     /// component meaning that its imported resources are represented as abstract
268     /// resource types. These abstract types are not equal to any other
269     /// component's types.
270     ///
271     /// For example:
272     ///
273     /// ```
274     /// # use wasmtime::Engine;
275     /// # use wasmtime::component::Component;
276     /// # use wasmtime::component::types::ComponentItem;
277     /// # fn main() -> wasmtime::Result<()> {
278     /// # let engine = Engine::default();
279     /// let a = Component::new(&engine, r#"
280     ///     (component (import "x" (type (sub resource))))
281     /// "#)?;
282     /// let b = Component::new(&engine, r#"
283     ///     (component (import "x" (type (sub resource))))
284     /// "#)?;
285     ///
286     /// let (_, a_ty) = a.component_type().imports(&engine).next().unwrap();
287     /// let (_, b_ty) = b.component_type().imports(&engine).next().unwrap();
288     ///
289     /// let a_ty = match a_ty {
290     ///     ComponentItem::Resource(ty) => ty,
291     ///     _ => unreachable!(),
292     /// };
293     /// let b_ty = match b_ty {
294     ///     ComponentItem::Resource(ty) => ty,
295     ///     _ => unreachable!(),
296     /// };
297     /// assert!(a_ty != b_ty);
298     /// # Ok(())
299     /// # }
300     /// ```
301     ///
302     /// Additionally, however, these abstract types are "substituted" during
303     /// instantiation meaning that a component type will appear to have changed
304     /// once it is instantiated.
305     ///
306     /// ```
307     /// # use wasmtime::{Engine, Store};
308     /// # use wasmtime::component::{Component, Linker, ResourceType};
309     /// # use wasmtime::component::types::ComponentItem;
310     /// # fn main() -> wasmtime::Result<()> {
311     /// # let engine = Engine::default();
312     /// // Here this component imports a resource and then exports it as-is
313     /// // which means that the export is equal to the import.
314     /// let a = Component::new(&engine, r#"
315     ///     (component
316     ///         (import "x" (type $x (sub resource)))
317     ///         (export "x" (type $x))
318     ///     )
319     /// "#)?;
320     ///
321     /// let (_, import) = a.component_type().imports(&engine).next().unwrap();
322     /// let (_, export) = a.component_type().exports(&engine).next().unwrap();
323     ///
324     /// let import = match import {
325     ///     ComponentItem::Resource(ty) => ty,
326     ///     _ => unreachable!(),
327     /// };
328     /// let export = match export {
329     ///     ComponentItem::Resource(ty) => ty,
330     ///     _ => unreachable!(),
331     /// };
332     /// assert_eq!(import, export);
333     ///
334     /// // However after instantiation the resource type "changes"
335     /// let mut store = Store::new(&engine, ());
336     /// let mut linker = Linker::new(&engine);
337     /// linker.root().resource("x", ResourceType::host::<()>(), |_, _| Ok(()))?;
338     /// let instance = linker.instantiate(&mut store, &a)?;
339     /// let instance_ty = instance.get_resource(&mut store, "x").unwrap();
340     ///
341     /// // Here `instance_ty` is not the same as either `import` or `export`,
342     /// // but it is equal to what we provided as an import.
343     /// assert!(instance_ty != import);
344     /// assert!(instance_ty != export);
345     /// assert!(instance_ty == ResourceType::host::<()>());
346     /// # Ok(())
347     /// # }
348     /// ```
349     ///
350     /// Finally, each instantiation of an exported resource from a component is
351     /// considered "fresh" for all instantiations meaning that different
352     /// instantiations will have different exported resource types:
353     ///
354     /// ```
355     /// # use wasmtime::{Engine, Store};
356     /// # use wasmtime::component::{Component, Linker};
357     /// # fn main() -> wasmtime::Result<()> {
358     /// # let engine = Engine::default();
359     /// let a = Component::new(&engine, r#"
360     ///     (component
361     ///         (type $x (resource (rep i32)))
362     ///         (export "x" (type $x))
363     ///     )
364     /// "#)?;
365     ///
366     /// let mut store = Store::new(&engine, ());
367     /// let linker = Linker::new(&engine);
368     /// let instance1 = linker.instantiate(&mut store, &a)?;
369     /// let instance2 = linker.instantiate(&mut store, &a)?;
370     ///
371     /// let x1 = instance1.get_resource(&mut store, "x").unwrap();
372     /// let x2 = instance2.get_resource(&mut store, "x").unwrap();
373     ///
374     /// // Despite these two resources being the same export of the same
375     /// // component they come from two different instances meaning that their
376     /// // types will be unique.
377     /// assert!(x1 != x2);
378     /// # Ok(())
379     /// # }
380     /// ```
381     pub fn component_type(&self) -> types::Component {
382         self.with_uninstantiated_instance_type(|ty| types::Component::from(self.inner.ty, ty))
383     }
384 
385     fn with_uninstantiated_instance_type<R>(&self, f: impl FnOnce(&InstanceType<'_>) -> R) -> R {
386         let resources = Arc::new(PrimaryMap::new());
387         f(&InstanceType {
388             types: self.types(),
389             resources: &resources,
390         })
391     }
392 
393     /// Final assembly step for a component from its in-memory representation.
394     ///
395     /// If the `artifacts` are specified as `None` here then they will be
396     /// deserialized from `code_memory`.
397     pub(crate) fn from_parts(
398         engine: &Engine,
399         code_memory: Arc<CodeMemory>,
400         artifacts: Option<ComponentArtifacts>,
401     ) -> Result<Component> {
402         let ComponentArtifacts {
403             ty,
404             info,
405             mut types,
406             mut static_modules,
407         } = match artifacts {
408             Some(artifacts) => artifacts,
409             None => postcard::from_bytes(code_memory.wasmtime_info())?,
410         };
411 
412         // Validate that the component can be used with the current instance
413         // allocator.
414         engine.allocator().validate_component(
415             &info.component,
416             &VMComponentOffsets::new(HostPtr, &info.component),
417             &|module_index| &static_modules[module_index].module,
418         )?;
419 
420         // Create a signature registration with the `Engine` for all trampolines
421         // and core wasm types found within this component, both for the
422         // component and for all included core wasm modules.
423         let signatures = engine.register_and_canonicalize_types(
424             types.module_types_mut(),
425             static_modules.iter_mut().map(|(_, m)| &mut m.module),
426         );
427         types.canonicalize_for_runtime_usage(&mut |idx| signatures.shared_type(idx).unwrap());
428 
429         // Assemble the `CodeObject` artifact which is shared by all core wasm
430         // modules as well as the final component.
431         let types = Arc::new(types);
432         let code = Arc::new(CodeObject::new(code_memory, signatures, types.into()));
433 
434         // Convert all information about static core wasm modules into actual
435         // `Module` instances by converting each `CompiledModuleInfo`, the
436         // `types` type information, and the code memory to a runtime object.
437         let static_modules = static_modules
438             .into_iter()
439             .map(|(_, info)| Module::from_parts_raw(engine, code.clone(), info, false))
440             .collect::<Result<_>>()?;
441 
442         let realloc_func_type = Arc::new(FuncType::new(
443             engine,
444             [ValType::I32, ValType::I32, ValType::I32, ValType::I32],
445             [ValType::I32],
446         )) as _;
447 
448         Ok(Component {
449             inner: Arc::new(ComponentInner {
450                 id: CompiledModuleId::new(),
451                 engine: engine.clone(),
452                 ty,
453                 static_modules,
454                 code,
455                 info,
456                 realloc_func_type,
457             }),
458         })
459     }
460 
461     pub(crate) fn ty(&self) -> TypeComponentIndex {
462         self.inner.ty
463     }
464 
465     pub(crate) fn env_component(&self) -> &wasmtime_environ::component::Component {
466         &self.inner.info.component
467     }
468 
469     pub(crate) fn static_module(&self, idx: StaticModuleIndex) -> &Module {
470         &self.inner.static_modules[idx]
471     }
472 
473     #[inline]
474     pub(crate) fn types(&self) -> &Arc<ComponentTypes> {
475         self.inner.component_types()
476     }
477 
478     pub(crate) fn signatures(&self) -> &TypeCollection {
479         self.inner.code.signatures()
480     }
481 
482     pub(crate) fn text(&self) -> &[u8] {
483         self.inner.code.code_memory().text()
484     }
485 
486     pub(crate) fn trampoline_ptrs(&self, index: TrampolineIndex) -> AllCallFuncPointers {
487         let AllCallFunc {
488             wasm_call,
489             array_call,
490         } = &self.inner.info.trampolines[index];
491         AllCallFuncPointers {
492             wasm_call: self.func(wasm_call).cast(),
493             array_call: self.func(array_call).cast(),
494         }
495     }
496 
497     fn func(&self, loc: &FunctionLoc) -> NonNull<VMFunctionBody> {
498         let text = self.text();
499         let trampoline = &text[loc.start as usize..][..loc.length as usize];
500         NonNull::new(trampoline.as_ptr() as *mut VMFunctionBody).unwrap()
501     }
502 
503     pub(crate) fn code_object(&self) -> &Arc<CodeObject> {
504         &self.inner.code
505     }
506 
507     /// Same as [`Module::serialize`], except for a component.
508     ///
509     /// Note that the artifact produced here must be passed to
510     /// [`Component::deserialize`] and is not compatible for use with
511     /// [`Module`].
512     ///
513     /// [`Module::serialize`]: crate::Module::serialize
514     /// [`Module`]: crate::Module
515     pub fn serialize(&self) -> Result<Vec<u8>> {
516         Ok(self.code_object().code_memory().mmap().to_vec())
517     }
518 
519     pub(crate) fn runtime_info(&self) -> Arc<dyn ComponentRuntimeInfo> {
520         self.inner.clone()
521     }
522 
523     /// Creates a new `VMFuncRef` with all fields filled out for the destructor
524     /// specified.
525     ///
526     /// The `dtor`'s own `VMFuncRef` won't have `wasm_call` filled out but this
527     /// component may have `resource_drop_wasm_to_native_trampoline` filled out
528     /// if necessary in which case it's filled in here.
529     pub(crate) fn resource_drop_func_ref(&self, dtor: &crate::func::HostFunc) -> VMFuncRef {
530         // Host functions never have their `wasm_call` filled in at this time.
531         assert!(dtor.func_ref().wasm_call.is_none());
532 
533         // Note that if `resource_drop_wasm_to_native_trampoline` is not present
534         // then this can't be called by the component, so it's ok to leave it
535         // blank.
536         let wasm_call = self
537             .inner
538             .info
539             .resource_drop_wasm_to_array_trampoline
540             .as_ref()
541             .map(|i| self.func(i).cast().into());
542         VMFuncRef {
543             wasm_call,
544             ..*dtor.func_ref()
545         }
546     }
547 
548     /// Returns a summary of the resources required to instantiate this
549     /// [`Component`][crate::component::Component].
550     ///
551     /// Note that when a component imports and instantiates another component or
552     /// core module, we cannot determine ahead of time how many resources
553     /// instantiating this component will require, and therefore this method
554     /// will return `None` in these scenarios.
555     ///
556     /// Potential uses of the returned information:
557     ///
558     /// * Determining whether your pooling allocator configuration supports
559     ///   instantiating this component.
560     ///
561     /// * Deciding how many of which `Component` you want to instantiate within
562     ///   a fixed amount of resources, e.g. determining whether to create 5
563     ///   instances of component X or 10 instances of component Y.
564     ///
565     /// # Example
566     ///
567     /// ```
568     /// # fn main() -> wasmtime::Result<()> {
569     /// use wasmtime::{Config, Engine, component::Component};
570     ///
571     /// let mut config = Config::new();
572     /// config.wasm_multi_memory(true);
573     /// config.wasm_component_model(true);
574     /// let engine = Engine::new(&config)?;
575     ///
576     /// let component = Component::new(&engine, &r#"
577     ///     (component
578     ///         ;; Define a core module that uses two memories.
579     ///         (core module $m
580     ///             (memory 1)
581     ///             (memory 6)
582     ///         )
583     ///
584     ///         ;; Instantiate that core module three times.
585     ///         (core instance $i1 (instantiate (module $m)))
586     ///         (core instance $i2 (instantiate (module $m)))
587     ///         (core instance $i3 (instantiate (module $m)))
588     ///     )
589     /// "#)?;
590     ///
591     /// let resources = component.resources_required()
592     ///     .expect("this component does not import any core modules or instances");
593     ///
594     /// // Instantiating the component will require allocating two memories per
595     /// // core instance, and there are three instances, so six total memories.
596     /// assert_eq!(resources.num_memories, 6);
597     /// assert_eq!(resources.max_initial_memory_size, Some(6));
598     ///
599     /// // The component doesn't need any tables.
600     /// assert_eq!(resources.num_tables, 0);
601     /// assert_eq!(resources.max_initial_table_size, None);
602     /// # Ok(()) }
603     /// ```
604     pub fn resources_required(&self) -> Option<ResourcesRequired> {
605         let mut resources = ResourcesRequired {
606             num_memories: 0,
607             max_initial_memory_size: None,
608             num_tables: 0,
609             max_initial_table_size: None,
610         };
611         for init in &self.env_component().initializers {
612             match init {
613                 GlobalInitializer::InstantiateModule(inst) => match inst {
614                     InstantiateModule::Static(index, _) => {
615                         let module = self.static_module(*index);
616                         resources.add(&module.resources_required());
617                     }
618                     InstantiateModule::Import(_, _) => {
619                         // We can't statically determine the resources required
620                         // to instantiate this component.
621                         return None;
622                     }
623                 },
624                 GlobalInitializer::LowerImport { .. }
625                 | GlobalInitializer::ExtractMemory(_)
626                 | GlobalInitializer::ExtractRealloc(_)
627                 | GlobalInitializer::ExtractCallback(_)
628                 | GlobalInitializer::ExtractPostReturn(_)
629                 | GlobalInitializer::Resource(_) => {}
630             }
631         }
632         Some(resources)
633     }
634 
635     /// Returns the range, in the host's address space, that this module's
636     /// compiled code resides at.
637     ///
638     /// For more information see
639     /// [`Module::image_range`](crate::Module::image_range).
640     pub fn image_range(&self) -> Range<*const u8> {
641         self.inner.code.code_memory().mmap().image_range()
642     }
643 
644     /// Force initialization of copy-on-write images to happen here-and-now
645     /// instead of when they're requested during first instantiation.
646     ///
647     /// When [copy-on-write memory
648     /// initialization](crate::Config::memory_init_cow) is enabled then Wasmtime
649     /// will lazily create the initialization image for a component. This method
650     /// can be used to explicitly dictate when this initialization happens.
651     ///
652     /// Note that this largely only matters on Linux when memfd is used.
653     /// Otherwise the copy-on-write image typically comes from disk and in that
654     /// situation the creation of the image is trivial as the image is always
655     /// sourced from disk. On Linux, though, when memfd is used a memfd is
656     /// created and the initialization image is written to it.
657     ///
658     /// Also note that this method is not required to be called, it's available
659     /// as a performance optimization if required but is otherwise handled
660     /// automatically.
661     pub fn initialize_copy_on_write_image(&self) -> Result<()> {
662         for (_, module) in self.inner.static_modules.iter() {
663             module.initialize_copy_on_write_image()?;
664         }
665         Ok(())
666     }
667 
668     /// Looks up a specific export of this component by `name` optionally nested
669     /// within the `instance` provided.
670     ///
671     /// This method is primarily used to acquire a [`ComponentExportIndex`]
672     /// which can be used with [`Instance`](crate::component::Instance) when
673     /// looking up exports. Export lookup with [`ComponentExportIndex`] can
674     /// skip string lookups at runtime and instead use a more efficient
675     /// index-based lookup.
676     ///
677     /// This method takes a few arguments:
678     ///
679     /// * `engine` - the engine that was used to compile this component.
680     /// * `instance` - an optional "parent instance" for the export being looked
681     ///   up. If this is `None` then the export is looked up on the root of the
682     ///   component itself, and otherwise the export is looked up on the
683     ///   `instance` specified. Note that `instance` must have come from a
684     ///   previous invocation of this method.
685     /// * `name` - the name of the export that's being looked up.
686     ///
687     /// If the export is located then two values are returned: a
688     /// [`types::ComponentItem`] which enables introspection about the type of
689     /// the export and a [`ComponentExportIndex`]. The index returned notably
690     /// implements the [`InstanceExportLookup`] trait which enables using it
691     /// with [`Instance::get_func`](crate::component::Instance::get_func) for
692     /// example.
693     ///
694     /// # Examples
695     ///
696     /// ```
697     /// use wasmtime::{Engine, Store};
698     /// use wasmtime::component::{Component, Linker};
699     /// use wasmtime::component::types::ComponentItem;
700     ///
701     /// # fn main() -> wasmtime::Result<()> {
702     /// let engine = Engine::default();
703     /// let component = Component::new(
704     ///     &engine,
705     ///     r#"
706     ///         (component
707     ///             (core module $m
708     ///                 (func (export "f"))
709     ///             )
710     ///             (core instance $i (instantiate $m))
711     ///             (func (export "f")
712     ///                 (canon lift (core func $i "f")))
713     ///         )
714     ///     "#,
715     /// )?;
716     ///
717     /// // Perform a lookup of the function "f" before instantiaton.
718     /// let (ty, export) = component.export_index(None, "f").unwrap();
719     /// assert!(matches!(ty, ComponentItem::ComponentFunc(_)));
720     ///
721     /// // After instantiation use `export` to lookup the function in question
722     /// // which notably does not do a string lookup at runtime.
723     /// let mut store = Store::new(&engine, ());
724     /// let instance = Linker::new(&engine).instantiate(&mut store, &component)?;
725     /// let func = instance.get_typed_func::<(), ()>(&mut store, &export)?;
726     /// // ...
727     /// # Ok(())
728     /// # }
729     /// ```
730     pub fn export_index(
731         &self,
732         instance: Option<&ComponentExportIndex>,
733         name: &str,
734     ) -> Option<(types::ComponentItem, ComponentExportIndex)> {
735         let info = self.env_component();
736         let index = self.lookup_export_index(instance, name)?;
737         let ty = match info.export_items[index] {
738             Export::Instance { ty, .. } => TypeDef::ComponentInstance(ty),
739             Export::LiftedFunction { ty, .. } => TypeDef::ComponentFunc(ty),
740             Export::ModuleStatic { ty, .. } | Export::ModuleImport { ty, .. } => {
741                 TypeDef::Module(ty)
742             }
743             Export::Type(ty) => ty,
744         };
745         let item = self.with_uninstantiated_instance_type(|instance| {
746             types::ComponentItem::from(&self.inner.engine, &ty, instance)
747         });
748         Some((
749             item,
750             ComponentExportIndex {
751                 id: self.inner.id,
752                 index,
753             },
754         ))
755     }
756 
757     pub(crate) fn lookup_export_index(
758         &self,
759         instance: Option<&ComponentExportIndex>,
760         name: &str,
761     ) -> Option<ExportIndex> {
762         let info = self.env_component();
763         let exports = match instance {
764             Some(idx) => {
765                 if idx.id != self.inner.id {
766                     return None;
767                 }
768                 match &info.export_items[idx.index] {
769                     Export::Instance { exports, .. } => exports,
770                     _ => return None,
771                 }
772             }
773             None => &info.exports,
774         };
775         exports.get(name, &NameMapNoIntern).copied()
776     }
777 
778     pub(crate) fn id(&self) -> CompiledModuleId {
779         self.inner.id
780     }
781 
782     /// Returns the [`Engine`] that this [`Component`] was compiled by.
783     pub fn engine(&self) -> &Engine {
784         &self.inner.engine
785     }
786 }
787 
788 /// A value which represents a known export of a component.
789 ///
790 /// This is the return value of [`Component::export_index`] and implements the
791 /// [`InstanceExportLookup`] trait to work with lookups like
792 /// [`Instance::get_func`](crate::component::Instance::get_func).
793 #[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
794 pub struct ComponentExportIndex {
795     pub(crate) id: CompiledModuleId,
796     pub(crate) index: ExportIndex,
797 }
798 
799 impl InstanceExportLookup for ComponentExportIndex {
800     fn lookup(&self, component: &Component) -> Option<ExportIndex> {
801         if component.inner.id == self.id {
802             Some(self.index)
803         } else {
804             None
805         }
806     }
807 }
808 
809 impl ComponentRuntimeInfo for ComponentInner {
810     fn component(&self) -> &wasmtime_environ::component::Component {
811         &self.info.component
812     }
813 
814     fn component_types(&self) -> &Arc<ComponentTypes> {
815         match self.code.types() {
816             crate::code::Types::Component(types) => types,
817             // The only creator of a `Component` is itself which uses the other
818             // variant, so this shouldn't be possible.
819             crate::code::Types::Module(_) => unreachable!(),
820         }
821     }
822 
823     fn realloc_func_type(&self) -> &Arc<dyn Any + Send + Sync> {
824         &self.realloc_func_type
825     }
826 }
827 
828 #[cfg(test)]
829 mod tests {
830     use crate::component::Component;
831     use crate::{Config, Engine};
832     use wasmtime_environ::MemoryInitialization;
833 
834     #[test]
835     fn cow_on_by_default() {
836         let mut config = Config::new();
837         config.wasm_component_model(true);
838         let engine = Engine::new(&config).unwrap();
839         let component = Component::new(
840             &engine,
841             r#"
842                 (component
843                     (core module
844                         (memory 1)
845                         (data (i32.const 100) "abcd")
846                     )
847                 )
848             "#,
849         )
850         .unwrap();
851 
852         for (_, module) in component.inner.static_modules.iter() {
853             let init = &module.env_module().memory_initialization;
854             assert!(matches!(init, MemoryInitialization::Static { .. }));
855         }
856     }
857 }
858