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