1 use crate::component::InstanceExportLookup;
2 use crate::component::matching::InstanceType;
3 use crate::component::types;
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     Engine, Module, ResourcesRequired, code::CodeObject, code_memory::CodeMemory,
13     type_registry::TypeCollection,
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::TypeTrace;
23 use wasmtime_environ::component::{
24     AllCallFunc, CompiledComponentInfo, ComponentArtifacts, ComponentTypes, Export, ExportIndex,
25     GlobalInitializer, InstantiateModule, NameMapNoIntern, StaticModuleIndex, TrampolineIndex,
26     TypeComponentIndex, VMComponentOffsets,
27 };
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     #[cfg_attr(not(feature = "profiling"), allow(dead_code))]
474     pub(crate) fn static_modules(&self) -> impl Iterator<Item = &Module> {
475         self.inner.static_modules.values()
476     }
477 
478     #[inline]
479     pub(crate) fn types(&self) -> &Arc<ComponentTypes> {
480         self.inner.component_types()
481     }
482 
483     pub(crate) fn signatures(&self) -> &TypeCollection {
484         self.inner.code.signatures()
485     }
486 
487     pub(crate) fn text(&self) -> &[u8] {
488         self.inner.code.code_memory().text()
489     }
490 
491     pub(crate) fn trampoline_ptrs(&self, index: TrampolineIndex) -> AllCallFuncPointers {
492         let AllCallFunc {
493             wasm_call,
494             array_call,
495         } = &self.inner.info.trampolines[index];
496         AllCallFuncPointers {
497             wasm_call: self.func(wasm_call).cast(),
498             array_call: self.func(array_call).cast(),
499         }
500     }
501 
502     fn func(&self, loc: &FunctionLoc) -> NonNull<VMFunctionBody> {
503         let text = self.text();
504         let trampoline = &text[loc.start as usize..][..loc.length as usize];
505         NonNull::new(trampoline.as_ptr() as *mut VMFunctionBody).unwrap()
506     }
507 
508     pub(crate) fn code_object(&self) -> &Arc<CodeObject> {
509         &self.inner.code
510     }
511 
512     /// Same as [`Module::serialize`], except for a component.
513     ///
514     /// Note that the artifact produced here must be passed to
515     /// [`Component::deserialize`] and is not compatible for use with
516     /// [`Module`].
517     ///
518     /// [`Module::serialize`]: crate::Module::serialize
519     /// [`Module`]: crate::Module
520     pub fn serialize(&self) -> Result<Vec<u8>> {
521         Ok(self.code_object().code_memory().mmap().to_vec())
522     }
523 
524     pub(crate) fn runtime_info(&self) -> Arc<dyn ComponentRuntimeInfo> {
525         self.inner.clone()
526     }
527 
528     /// Creates a new `VMFuncRef` with all fields filled out for the destructor
529     /// specified.
530     ///
531     /// The `dtor`'s own `VMFuncRef` won't have `wasm_call` filled out but this
532     /// component may have `resource_drop_wasm_to_native_trampoline` filled out
533     /// if necessary in which case it's filled in here.
534     pub(crate) fn resource_drop_func_ref(&self, dtor: &crate::func::HostFunc) -> VMFuncRef {
535         // Host functions never have their `wasm_call` filled in at this time.
536         assert!(dtor.func_ref().wasm_call.is_none());
537 
538         // Note that if `resource_drop_wasm_to_native_trampoline` is not present
539         // then this can't be called by the component, so it's ok to leave it
540         // blank.
541         let wasm_call = self
542             .inner
543             .info
544             .resource_drop_wasm_to_array_trampoline
545             .as_ref()
546             .map(|i| self.func(i).cast().into());
547         VMFuncRef {
548             wasm_call,
549             ..*dtor.func_ref()
550         }
551     }
552 
553     /// Returns a summary of the resources required to instantiate this
554     /// [`Component`][crate::component::Component].
555     ///
556     /// Note that when a component imports and instantiates another component or
557     /// core module, we cannot determine ahead of time how many resources
558     /// instantiating this component will require, and therefore this method
559     /// will return `None` in these scenarios.
560     ///
561     /// Potential uses of the returned information:
562     ///
563     /// * Determining whether your pooling allocator configuration supports
564     ///   instantiating this component.
565     ///
566     /// * Deciding how many of which `Component` you want to instantiate within
567     ///   a fixed amount of resources, e.g. determining whether to create 5
568     ///   instances of component X or 10 instances of component Y.
569     ///
570     /// # Example
571     ///
572     /// ```
573     /// # fn main() -> wasmtime::Result<()> {
574     /// use wasmtime::{Config, Engine, component::Component};
575     ///
576     /// let mut config = Config::new();
577     /// config.wasm_multi_memory(true);
578     /// config.wasm_component_model(true);
579     /// let engine = Engine::new(&config)?;
580     ///
581     /// let component = Component::new(&engine, &r#"
582     ///     (component
583     ///         ;; Define a core module that uses two memories.
584     ///         (core module $m
585     ///             (memory 1)
586     ///             (memory 6)
587     ///         )
588     ///
589     ///         ;; Instantiate that core module three times.
590     ///         (core instance $i1 (instantiate (module $m)))
591     ///         (core instance $i2 (instantiate (module $m)))
592     ///         (core instance $i3 (instantiate (module $m)))
593     ///     )
594     /// "#)?;
595     ///
596     /// let resources = component.resources_required()
597     ///     .expect("this component does not import any core modules or instances");
598     ///
599     /// // Instantiating the component will require allocating two memories per
600     /// // core instance, and there are three instances, so six total memories.
601     /// assert_eq!(resources.num_memories, 6);
602     /// assert_eq!(resources.max_initial_memory_size, Some(6));
603     ///
604     /// // The component doesn't need any tables.
605     /// assert_eq!(resources.num_tables, 0);
606     /// assert_eq!(resources.max_initial_table_size, None);
607     /// # Ok(()) }
608     /// ```
609     pub fn resources_required(&self) -> Option<ResourcesRequired> {
610         let mut resources = ResourcesRequired {
611             num_memories: 0,
612             max_initial_memory_size: None,
613             num_tables: 0,
614             max_initial_table_size: None,
615         };
616         for init in &self.env_component().initializers {
617             match init {
618                 GlobalInitializer::InstantiateModule(inst) => match inst {
619                     InstantiateModule::Static(index, _) => {
620                         let module = self.static_module(*index);
621                         resources.add(&module.resources_required());
622                     }
623                     InstantiateModule::Import(_, _) => {
624                         // We can't statically determine the resources required
625                         // to instantiate this component.
626                         return None;
627                     }
628                 },
629                 GlobalInitializer::LowerImport { .. }
630                 | GlobalInitializer::ExtractMemory(_)
631                 | GlobalInitializer::ExtractTable(_)
632                 | GlobalInitializer::ExtractRealloc(_)
633                 | GlobalInitializer::ExtractCallback(_)
634                 | GlobalInitializer::ExtractPostReturn(_)
635                 | GlobalInitializer::Resource(_) => {}
636             }
637         }
638         Some(resources)
639     }
640 
641     /// Returns the range, in the host's address space, that this module's
642     /// compiled code resides at.
643     ///
644     /// For more information see
645     /// [`Module::image_range`](crate::Module::image_range).
646     pub fn image_range(&self) -> Range<*const u8> {
647         self.inner.code.code_memory().mmap().image_range()
648     }
649 
650     /// Force initialization of copy-on-write images to happen here-and-now
651     /// instead of when they're requested during first instantiation.
652     ///
653     /// When [copy-on-write memory
654     /// initialization](crate::Config::memory_init_cow) is enabled then Wasmtime
655     /// will lazily create the initialization image for a component. This method
656     /// can be used to explicitly dictate when this initialization happens.
657     ///
658     /// Note that this largely only matters on Linux when memfd is used.
659     /// Otherwise the copy-on-write image typically comes from disk and in that
660     /// situation the creation of the image is trivial as the image is always
661     /// sourced from disk. On Linux, though, when memfd is used a memfd is
662     /// created and the initialization image is written to it.
663     ///
664     /// Also note that this method is not required to be called, it's available
665     /// as a performance optimization if required but is otherwise handled
666     /// automatically.
667     pub fn initialize_copy_on_write_image(&self) -> Result<()> {
668         for (_, module) in self.inner.static_modules.iter() {
669             module.initialize_copy_on_write_image()?;
670         }
671         Ok(())
672     }
673 
674     /// Looks up a specific export of this component by `name` optionally nested
675     /// within the `instance` provided.
676     ///
677     /// See related method [`Self::get_export`] for additional docs and
678     /// examples.
679     ///
680     /// This method is primarily used to acquire a [`ComponentExportIndex`]
681     /// which can be used with [`Instance`](crate::component::Instance) when
682     /// looking up exports. Export lookup with [`ComponentExportIndex`] can
683     /// skip string lookups at runtime and instead use a more efficient
684     /// index-based lookup.
685     ///
686     /// This method only returns the [`ComponentExportIndex`]. If you need the
687     /// corresponding [`types::ComponentItem`], use the related function
688     /// [`Self::get_export`].
689     ///
690     ///
691     /// [`Instance`](crate::component::Instance) has a corresponding method
692     /// [`Instance::get_export_index`](crate::component::Instance::get_export_index).
693     pub fn get_export_index(
694         &self,
695         instance: Option<&ComponentExportIndex>,
696         name: &str,
697     ) -> Option<ComponentExportIndex> {
698         let index = self.lookup_export_index(instance, name)?;
699         Some(ComponentExportIndex {
700             id: self.inner.id,
701             index,
702         })
703     }
704 
705     /// Looks up a specific export of this component by `name` optionally nested
706     /// within the `instance` provided.
707     ///
708     /// This method is primarily used to acquire a [`ComponentExportIndex`]
709     /// which can be used with [`Instance`](crate::component::Instance) when
710     /// looking up exports. Export lookup with [`ComponentExportIndex`] can
711     /// skip string lookups at runtime and instead use a more efficient
712     /// index-based lookup.
713     ///
714     /// This method takes a few arguments:
715     ///
716     /// * `engine` - the engine that was used to compile this component.
717     /// * `instance` - an optional "parent instance" for the export being looked
718     ///   up. If this is `None` then the export is looked up on the root of the
719     ///   component itself, and otherwise the export is looked up on the
720     ///   `instance` specified. Note that `instance` must have come from a
721     ///   previous invocation of this method.
722     /// * `name` - the name of the export that's being looked up.
723     ///
724     /// If the export is located then two values are returned: a
725     /// [`types::ComponentItem`] which enables introspection about the type of
726     /// the export and a [`ComponentExportIndex`]. The index returned notably
727     /// implements the [`InstanceExportLookup`] trait which enables using it
728     /// with [`Instance::get_func`](crate::component::Instance::get_func) for
729     /// example.
730     ///
731     /// The returned [`types::ComponentItem`] is more expensive to calculate
732     /// than the [`ComponentExportIndex`]. If you only consume the
733     /// [`ComponentExportIndex`], use the related method
734     /// [`Self::get_export_index`] instead.
735     ///
736     /// [`Instance`](crate::component::Instance) has a corresponding method
737     /// [`Instance::get_export`](crate::component::Instance::get_export).
738     ///
739     /// # Examples
740     ///
741     /// ```
742     /// use wasmtime::{Engine, Store};
743     /// use wasmtime::component::{Component, Linker};
744     /// use wasmtime::component::types::ComponentItem;
745     ///
746     /// # fn main() -> wasmtime::Result<()> {
747     /// let engine = Engine::default();
748     /// let component = Component::new(
749     ///     &engine,
750     ///     r#"
751     ///         (component
752     ///             (core module $m
753     ///                 (func (export "f"))
754     ///             )
755     ///             (core instance $i (instantiate $m))
756     ///             (func (export "f")
757     ///                 (canon lift (core func $i "f")))
758     ///         )
759     ///     "#,
760     /// )?;
761     ///
762     /// // Perform a lookup of the function "f" before instantiaton.
763     /// let (ty, export) = component.get_export(None, "f").unwrap();
764     /// assert!(matches!(ty, ComponentItem::ComponentFunc(_)));
765     ///
766     /// // After instantiation use `export` to lookup the function in question
767     /// // which notably does not do a string lookup at runtime.
768     /// let mut store = Store::new(&engine, ());
769     /// let instance = Linker::new(&engine).instantiate(&mut store, &component)?;
770     /// let func = instance.get_typed_func::<(), ()>(&mut store, &export)?;
771     /// // ...
772     /// # Ok(())
773     /// # }
774     /// ```
775     pub fn get_export(
776         &self,
777         instance: Option<&ComponentExportIndex>,
778         name: &str,
779     ) -> Option<(types::ComponentItem, ComponentExportIndex)> {
780         let info = self.env_component();
781         let index = self.lookup_export_index(instance, name)?;
782         let item = self.with_uninstantiated_instance_type(|instance| {
783             types::ComponentItem::from_export(
784                 &self.inner.engine,
785                 &info.export_items[index],
786                 instance,
787             )
788         });
789         Some((
790             item,
791             ComponentExportIndex {
792                 id: self.inner.id,
793                 index,
794             },
795         ))
796     }
797 
798     pub(crate) fn lookup_export_index(
799         &self,
800         instance: Option<&ComponentExportIndex>,
801         name: &str,
802     ) -> Option<ExportIndex> {
803         let info = self.env_component();
804         let exports = match instance {
805             Some(idx) => {
806                 if idx.id != self.inner.id {
807                     return None;
808                 }
809                 match &info.export_items[idx.index] {
810                     Export::Instance { exports, .. } => exports,
811                     _ => return None,
812                 }
813             }
814             None => &info.exports,
815         };
816         exports.get(name, &NameMapNoIntern).copied()
817     }
818 
819     pub(crate) fn id(&self) -> CompiledModuleId {
820         self.inner.id
821     }
822 
823     /// Returns the [`Engine`] that this [`Component`] was compiled by.
824     pub fn engine(&self) -> &Engine {
825         &self.inner.engine
826     }
827 }
828 
829 /// A value which represents a known export of a component.
830 ///
831 /// This is the return value of [`Component::get_export`] and implements the
832 /// [`InstanceExportLookup`] trait to work with lookups like
833 /// [`Instance::get_func`](crate::component::Instance::get_func).
834 #[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
835 pub struct ComponentExportIndex {
836     pub(crate) id: CompiledModuleId,
837     pub(crate) index: ExportIndex,
838 }
839 
840 impl InstanceExportLookup for ComponentExportIndex {
841     fn lookup(&self, component: &Component) -> Option<ExportIndex> {
842         if component.inner.id == self.id {
843             Some(self.index)
844         } else {
845             None
846         }
847     }
848 }
849 
850 impl ComponentRuntimeInfo for ComponentInner {
851     fn component(&self) -> &wasmtime_environ::component::Component {
852         &self.info.component
853     }
854 
855     fn component_types(&self) -> &Arc<ComponentTypes> {
856         match self.code.types() {
857             crate::code::Types::Component(types) => types,
858             // The only creator of a `Component` is itself which uses the other
859             // variant, so this shouldn't be possible.
860             crate::code::Types::Module(_) => unreachable!(),
861         }
862     }
863 
864     fn realloc_func_type(&self) -> &Arc<dyn Any + Send + Sync> {
865         &self.realloc_func_type
866     }
867 }
868 
869 #[cfg(test)]
870 mod tests {
871     use crate::component::Component;
872     use crate::{Config, Engine};
873     use wasmtime_environ::MemoryInitialization;
874 
875     #[test]
876     fn cow_on_by_default() {
877         let mut config = Config::new();
878         config.wasm_component_model(true);
879         let engine = Engine::new(&config).unwrap();
880         let component = Component::new(
881             &engine,
882             r#"
883                 (component
884                     (core module
885                         (memory 1)
886                         (data (i32.const 100) "abcd")
887                     )
888                 )
889             "#,
890         )
891         .unwrap();
892 
893         for (_, module) in component.inner.static_modules.iter() {
894             let init = &module.env_module().memory_initialization;
895             assert!(matches!(init, MemoryInitialization::Static { .. }));
896         }
897     }
898 }
899