wasmtime/runtime/component/component.rs
1use crate::component::InstanceExportLookup;
2use crate::component::matching::InstanceType;
3use crate::component::types;
4use crate::prelude::*;
5#[cfg(feature = "std")]
6use crate::runtime::vm::open_file_for_mmap;
7use crate::runtime::vm::{CompiledModuleId, VMArrayCallFunction, VMFuncRef, VMWasmCallFunction};
8use crate::{
9 Engine, Module, ResourcesRequired, code::CodeObject, code_memory::CodeMemory,
10 type_registry::TypeCollection,
11};
12use crate::{FuncType, ValType};
13use alloc::sync::Arc;
14use core::ops::Range;
15use core::ptr::NonNull;
16#[cfg(feature = "std")]
17use std::path::Path;
18use wasmtime_environ::component::{
19 CompiledComponentInfo, ComponentArtifacts, ComponentTypes, CoreDef, Export, ExportIndex,
20 GlobalInitializer, InstantiateModule, NameMapNoIntern, OptionsIndex, StaticModuleIndex,
21 TrampolineIndex, TypeComponentIndex, TypeFuncIndex, UnsafeIntrinsic, VMComponentOffsets,
22};
23use wasmtime_environ::{Abi, CompiledFunctionsTable, FuncKey, TypeTrace};
24use 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)]
58pub struct Component {
59 inner: Arc<ComponentInner>,
60}
61
62struct 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<CodeObject>,
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
99pub(crate) struct AllCallFuncPointers {
100 pub wasm_call: NonNull<VMWasmCallFunction>,
101 pub array_call: NonNull<VMArrayCallFunction>,
102}
103
104impl 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 // SAFETY: the contract required by `load_code_raw` is the same as this
231 // function.
232 let code = unsafe { engine.load_code_raw(memory, ObjectKind::Component)? };
233 Component::from_parts(engine, code, None)
234 }
235
236 /// Same as [`Module::deserialize_file`], but for components.
237 ///
238 /// Note that the file referenced here must contain contents previously
239 /// produced by [`Engine::precompile_component`] or
240 /// [`Component::serialize`].
241 ///
242 /// For more information see the [`Module::deserialize_file`] method.
243 ///
244 /// # Unsafety
245 ///
246 /// The unsafety of this method is the same as that of the
247 /// [`Module::deserialize_file`] method.
248 ///
249 /// [`Module::deserialize_file`]: crate::Module::deserialize_file
250 #[cfg(feature = "std")]
251 pub unsafe fn deserialize_file(engine: &Engine, path: impl AsRef<Path>) -> Result<Component> {
252 let file = open_file_for_mmap(path.as_ref())?;
253 let code = engine
254 .load_code_file(file, ObjectKind::Component)
255 .with_context(|| format!("failed to load code for: {}", path.as_ref().display()))?;
256 Component::from_parts(engine, code, None)
257 }
258
259 /// Returns the type of this component as a [`types::Component`].
260 ///
261 /// This method enables runtime introspection of the type of a component
262 /// before instantiation, if necessary.
263 ///
264 /// ## Component types and Resources
265 ///
266 /// An important point to note here is that the precise type of imports and
267 /// exports of a component change when it is instantiated with respect to
268 /// resources. For example a [`Component`] represents an un-instantiated
269 /// component meaning that its imported resources are represented as abstract
270 /// resource types. These abstract types are not equal to any other
271 /// component's types.
272 ///
273 /// For example:
274 ///
275 /// ```
276 /// # use wasmtime::Engine;
277 /// # use wasmtime::component::Component;
278 /// # use wasmtime::component::types::ComponentItem;
279 /// # fn main() -> wasmtime::Result<()> {
280 /// # let engine = Engine::default();
281 /// let a = Component::new(&engine, r#"
282 /// (component (import "x" (type (sub resource))))
283 /// "#)?;
284 /// let b = Component::new(&engine, r#"
285 /// (component (import "x" (type (sub resource))))
286 /// "#)?;
287 ///
288 /// let (_, a_ty) = a.component_type().imports(&engine).next().unwrap();
289 /// let (_, b_ty) = b.component_type().imports(&engine).next().unwrap();
290 ///
291 /// let a_ty = match a_ty {
292 /// ComponentItem::Resource(ty) => ty,
293 /// _ => unreachable!(),
294 /// };
295 /// let b_ty = match b_ty {
296 /// ComponentItem::Resource(ty) => ty,
297 /// _ => unreachable!(),
298 /// };
299 /// assert!(a_ty != b_ty);
300 /// # Ok(())
301 /// # }
302 /// ```
303 ///
304 /// Additionally, however, these abstract types are "substituted" during
305 /// instantiation meaning that a component type will appear to have changed
306 /// once it is instantiated.
307 ///
308 /// ```
309 /// # use wasmtime::{Engine, Store};
310 /// # use wasmtime::component::{Component, Linker, ResourceType};
311 /// # use wasmtime::component::types::ComponentItem;
312 /// # fn main() -> wasmtime::Result<()> {
313 /// # let engine = Engine::default();
314 /// // Here this component imports a resource and then exports it as-is
315 /// // which means that the export is equal to the import.
316 /// let a = Component::new(&engine, r#"
317 /// (component
318 /// (import "x" (type $x (sub resource)))
319 /// (export "x" (type $x))
320 /// )
321 /// "#)?;
322 ///
323 /// let (_, import) = a.component_type().imports(&engine).next().unwrap();
324 /// let (_, export) = a.component_type().exports(&engine).next().unwrap();
325 ///
326 /// let import = match import {
327 /// ComponentItem::Resource(ty) => ty,
328 /// _ => unreachable!(),
329 /// };
330 /// let export = match export {
331 /// ComponentItem::Resource(ty) => ty,
332 /// _ => unreachable!(),
333 /// };
334 /// assert_eq!(import, export);
335 ///
336 /// // However after instantiation the resource type "changes"
337 /// let mut store = Store::new(&engine, ());
338 /// let mut linker = Linker::new(&engine);
339 /// linker.root().resource("x", ResourceType::host::<()>(), |_, _| Ok(()))?;
340 /// let instance = linker.instantiate(&mut store, &a)?;
341 /// let instance_ty = instance.get_resource(&mut store, "x").unwrap();
342 ///
343 /// // Here `instance_ty` is not the same as either `import` or `export`,
344 /// // but it is equal to what we provided as an import.
345 /// assert!(instance_ty != import);
346 /// assert!(instance_ty != export);
347 /// assert!(instance_ty == ResourceType::host::<()>());
348 /// # Ok(())
349 /// # }
350 /// ```
351 ///
352 /// Finally, each instantiation of an exported resource from a component is
353 /// considered "fresh" for all instantiations meaning that different
354 /// instantiations will have different exported resource types:
355 ///
356 /// ```
357 /// # use wasmtime::{Engine, Store};
358 /// # use wasmtime::component::{Component, Linker};
359 /// # fn main() -> wasmtime::Result<()> {
360 /// # let engine = Engine::default();
361 /// let a = Component::new(&engine, r#"
362 /// (component
363 /// (type $x (resource (rep i32)))
364 /// (export "x" (type $x))
365 /// )
366 /// "#)?;
367 ///
368 /// let mut store = Store::new(&engine, ());
369 /// let linker = Linker::new(&engine);
370 /// let instance1 = linker.instantiate(&mut store, &a)?;
371 /// let instance2 = linker.instantiate(&mut store, &a)?;
372 ///
373 /// let x1 = instance1.get_resource(&mut store, "x").unwrap();
374 /// let x2 = instance2.get_resource(&mut store, "x").unwrap();
375 ///
376 /// // Despite these two resources being the same export of the same
377 /// // component they come from two different instances meaning that their
378 /// // types will be unique.
379 /// assert!(x1 != x2);
380 /// # Ok(())
381 /// # }
382 /// ```
383 pub fn component_type(&self) -> types::Component {
384 self.with_uninstantiated_instance_type(|ty| types::Component::from(self.inner.ty, ty))
385 }
386
387 fn with_uninstantiated_instance_type<R>(&self, f: impl FnOnce(&InstanceType<'_>) -> R) -> R {
388 let resources = Arc::new(PrimaryMap::new());
389 f(&InstanceType {
390 types: self.types(),
391 resources: &resources,
392 })
393 }
394
395 /// Final assembly step for a component from its in-memory representation.
396 ///
397 /// If the `artifacts` are specified as `None` here then they will be
398 /// deserialized from `code_memory`.
399 pub(crate) fn from_parts(
400 engine: &Engine,
401 code_memory: Arc<CodeMemory>,
402 artifacts: Option<ComponentArtifacts>,
403 ) -> Result<Component> {
404 let ComponentArtifacts {
405 ty,
406 info,
407 table: index,
408 mut types,
409 mut static_modules,
410 } = match artifacts {
411 Some(artifacts) => artifacts,
412 None => postcard::from_bytes(code_memory.wasmtime_info())?,
413 };
414 let index = Arc::new(index);
415
416 // Validate that the component can be used with the current instance
417 // allocator.
418 engine.allocator().validate_component(
419 &info.component,
420 &VMComponentOffsets::new(HostPtr, &info.component),
421 &|module_index| &static_modules[module_index].module,
422 )?;
423
424 // Create a signature registration with the `Engine` for all trampolines
425 // and core wasm types found within this component, both for the
426 // component and for all included core wasm modules.
427 let signatures = engine.register_and_canonicalize_types(
428 types.module_types_mut(),
429 static_modules.iter_mut().map(|(_, m)| &mut m.module),
430 );
431 types.canonicalize_for_runtime_usage(&mut |idx| signatures.shared_type(idx).unwrap());
432
433 // Assemble the `CodeObject` artifact which is shared by all core wasm
434 // modules as well as the final component.
435 let types = Arc::new(types);
436 let code = Arc::new(CodeObject::new(code_memory, signatures, types.into()));
437
438 // Convert all information about static core wasm modules into actual
439 // `Module` instances by converting each `CompiledModuleInfo`, the
440 // `types` type information, and the code memory to a runtime object.
441 let static_modules = static_modules
442 .into_iter()
443 .map(|(_, info)| {
444 Module::from_parts_raw(engine, code.clone(), info, index.clone(), false)
445 })
446 .collect::<Result<_>>()?;
447
448 let realloc_func_type = Arc::new(FuncType::new(
449 engine,
450 [ValType::I32, ValType::I32, ValType::I32, ValType::I32],
451 [ValType::I32],
452 ));
453
454 Ok(Component {
455 inner: Arc::new(ComponentInner {
456 id: CompiledModuleId::new(),
457 engine: engine.clone(),
458 ty,
459 static_modules,
460 code,
461 info,
462 index,
463 realloc_func_type,
464 }),
465 })
466 }
467
468 pub(crate) fn ty(&self) -> TypeComponentIndex {
469 self.inner.ty
470 }
471
472 pub(crate) fn env_component(&self) -> &wasmtime_environ::component::Component {
473 &self.inner.info.component
474 }
475
476 pub(crate) fn static_module(&self, idx: StaticModuleIndex) -> &Module {
477 &self.inner.static_modules[idx]
478 }
479
480 #[cfg(feature = "profiling")]
481 pub(crate) fn static_modules(&self) -> impl Iterator<Item = &Module> {
482 self.inner.static_modules.values()
483 }
484
485 #[inline]
486 pub(crate) fn types(&self) -> &Arc<ComponentTypes> {
487 match self.inner.code.types() {
488 crate::code::Types::Component(types) => types,
489 // The only creator of a `Component` is itself which uses the other
490 // variant, so this shouldn't be possible.
491 crate::code::Types::Module(_) => unreachable!(),
492 }
493 }
494
495 pub(crate) fn signatures(&self) -> &TypeCollection {
496 self.inner.code.signatures()
497 }
498
499 pub(crate) fn text(&self) -> &[u8] {
500 self.inner.code.code_memory().text()
501 }
502
503 pub(crate) fn trampoline_ptrs(&self, index: TrampolineIndex) -> AllCallFuncPointers {
504 let wasm_call = self
505 .func(FuncKey::ComponentTrampoline(Abi::Wasm, index))
506 .unwrap()
507 .cast();
508 let array_call = self
509 .func(FuncKey::ComponentTrampoline(Abi::Array, index))
510 .unwrap()
511 .cast();
512 AllCallFuncPointers {
513 wasm_call,
514 array_call,
515 }
516 }
517
518 pub(crate) fn unsafe_intrinsic_ptrs(
519 &self,
520 intrinsic: UnsafeIntrinsic,
521 ) -> Option<AllCallFuncPointers> {
522 let wasm_call = self
523 .func(FuncKey::UnsafeIntrinsic(Abi::Wasm, intrinsic))?
524 .cast();
525 let array_call = self
526 .func(FuncKey::UnsafeIntrinsic(Abi::Array, intrinsic))?
527 .cast();
528 Some(AllCallFuncPointers {
529 wasm_call,
530 array_call,
531 })
532 }
533
534 /// Look up a function in this component's text section by `FuncKey`.
535 fn func(&self, key: FuncKey) -> Option<NonNull<u8>> {
536 let loc = self.inner.index.func_loc(key)?;
537 Some(self.func_loc_to_pointer(loc))
538 }
539
540 /// Given a function location within this component's text section, get a
541 /// pointer to the function.
542 ///
543 /// Panics on out-of-bounds function locations.
544 fn func_loc_to_pointer(&self, loc: &FunctionLoc) -> NonNull<u8> {
545 let text = self.text();
546 let trampoline = &text[loc.start as usize..][..loc.length as usize];
547 NonNull::from(trampoline).cast()
548 }
549
550 pub(crate) fn code_object(&self) -> &Arc<CodeObject> {
551 &self.inner.code
552 }
553
554 /// Same as [`Module::serialize`], except for a component.
555 ///
556 /// Note that the artifact produced here must be passed to
557 /// [`Component::deserialize`] and is not compatible for use with
558 /// [`Module`].
559 ///
560 /// [`Module::serialize`]: crate::Module::serialize
561 /// [`Module`]: crate::Module
562 pub fn serialize(&self) -> Result<Vec<u8>> {
563 Ok(self.code_object().code_memory().mmap().to_vec())
564 }
565
566 /// Creates a new `VMFuncRef` with all fields filled out for the destructor
567 /// specified.
568 ///
569 /// The `dtor`'s own `VMFuncRef` won't have `wasm_call` filled out but this
570 /// component may have `resource_drop_wasm_to_native_trampoline` filled out
571 /// if necessary in which case it's filled in here.
572 pub(crate) fn resource_drop_func_ref(&self, dtor: &crate::func::HostFunc) -> VMFuncRef {
573 // Host functions never have their `wasm_call` filled in at this time.
574 assert!(dtor.func_ref().wasm_call.is_none());
575
576 // Note that if `resource_drop_wasm_to_native_trampoline` is not present
577 // then this can't be called by the component, so it's ok to leave it
578 // blank.
579 let wasm_call = self
580 .func(FuncKey::ResourceDropTrampoline)
581 .map(|f| f.cast().into());
582
583 VMFuncRef {
584 wasm_call,
585 ..*dtor.func_ref()
586 }
587 }
588
589 /// Returns a summary of the resources required to instantiate this
590 /// [`Component`][crate::component::Component].
591 ///
592 /// Note that when a component imports and instantiates another component or
593 /// core module, we cannot determine ahead of time how many resources
594 /// instantiating this component will require, and therefore this method
595 /// will return `None` in these scenarios.
596 ///
597 /// Potential uses of the returned information:
598 ///
599 /// * Determining whether your pooling allocator configuration supports
600 /// instantiating this component.
601 ///
602 /// * Deciding how many of which `Component` you want to instantiate within
603 /// a fixed amount of resources, e.g. determining whether to create 5
604 /// instances of component X or 10 instances of component Y.
605 ///
606 /// # Example
607 ///
608 /// ```
609 /// # fn main() -> wasmtime::Result<()> {
610 /// use wasmtime::{Config, Engine, component::Component};
611 ///
612 /// let mut config = Config::new();
613 /// config.wasm_multi_memory(true);
614 /// config.wasm_component_model(true);
615 /// let engine = Engine::new(&config)?;
616 ///
617 /// let component = Component::new(&engine, &r#"
618 /// (component
619 /// ;; Define a core module that uses two memories.
620 /// (core module $m
621 /// (memory 1)
622 /// (memory 6)
623 /// )
624 ///
625 /// ;; Instantiate that core module three times.
626 /// (core instance $i1 (instantiate (module $m)))
627 /// (core instance $i2 (instantiate (module $m)))
628 /// (core instance $i3 (instantiate (module $m)))
629 /// )
630 /// "#)?;
631 ///
632 /// let resources = component.resources_required()
633 /// .expect("this component does not import any core modules or instances");
634 ///
635 /// // Instantiating the component will require allocating two memories per
636 /// // core instance, and there are three instances, so six total memories.
637 /// assert_eq!(resources.num_memories, 6);
638 /// assert_eq!(resources.max_initial_memory_size, Some(6));
639 ///
640 /// // The component doesn't need any tables.
641 /// assert_eq!(resources.num_tables, 0);
642 /// assert_eq!(resources.max_initial_table_size, None);
643 /// # Ok(()) }
644 /// ```
645 pub fn resources_required(&self) -> Option<ResourcesRequired> {
646 let mut resources = ResourcesRequired {
647 num_memories: 0,
648 max_initial_memory_size: None,
649 num_tables: 0,
650 max_initial_table_size: None,
651 };
652 for init in &self.env_component().initializers {
653 match init {
654 GlobalInitializer::InstantiateModule(inst) => match inst {
655 InstantiateModule::Static(index, _) => {
656 let module = self.static_module(*index);
657 resources.add(&module.resources_required());
658 }
659 InstantiateModule::Import(_, _) => {
660 // We can't statically determine the resources required
661 // to instantiate this component.
662 return None;
663 }
664 },
665 GlobalInitializer::LowerImport { .. }
666 | GlobalInitializer::ExtractMemory(_)
667 | GlobalInitializer::ExtractTable(_)
668 | GlobalInitializer::ExtractRealloc(_)
669 | GlobalInitializer::ExtractCallback(_)
670 | GlobalInitializer::ExtractPostReturn(_)
671 | GlobalInitializer::Resource(_) => {}
672 }
673 }
674 Some(resources)
675 }
676
677 /// Returns the range, in the host's address space, that this module's
678 /// compiled code resides at.
679 ///
680 /// For more information see
681 /// [`Module::image_range`](crate::Module::image_range).
682 pub fn image_range(&self) -> Range<*const u8> {
683 self.inner.code.code_memory().mmap().image_range()
684 }
685
686 /// Force initialization of copy-on-write images to happen here-and-now
687 /// instead of when they're requested during first instantiation.
688 ///
689 /// When [copy-on-write memory
690 /// initialization](crate::Config::memory_init_cow) is enabled then Wasmtime
691 /// will lazily create the initialization image for a component. This method
692 /// can be used to explicitly dictate when this initialization happens.
693 ///
694 /// Note that this largely only matters on Linux when memfd is used.
695 /// Otherwise the copy-on-write image typically comes from disk and in that
696 /// situation the creation of the image is trivial as the image is always
697 /// sourced from disk. On Linux, though, when memfd is used a memfd is
698 /// created and the initialization image is written to it.
699 ///
700 /// Also note that this method is not required to be called, it's available
701 /// as a performance optimization if required but is otherwise handled
702 /// automatically.
703 pub fn initialize_copy_on_write_image(&self) -> Result<()> {
704 for (_, module) in self.inner.static_modules.iter() {
705 module.initialize_copy_on_write_image()?;
706 }
707 Ok(())
708 }
709
710 /// Looks up a specific export of this component by `name` optionally nested
711 /// within the `instance` provided.
712 ///
713 /// See related method [`Self::get_export`] for additional docs and
714 /// examples.
715 ///
716 /// This method is primarily used to acquire a [`ComponentExportIndex`]
717 /// which can be used with [`Instance`](crate::component::Instance) when
718 /// looking up exports. Export lookup with [`ComponentExportIndex`] can
719 /// skip string lookups at runtime and instead use a more efficient
720 /// index-based lookup.
721 ///
722 /// This method only returns the [`ComponentExportIndex`]. If you need the
723 /// corresponding [`types::ComponentItem`], use the related function
724 /// [`Self::get_export`].
725 ///
726 ///
727 /// [`Instance`](crate::component::Instance) has a corresponding method
728 /// [`Instance::get_export_index`](crate::component::Instance::get_export_index).
729 pub fn get_export_index(
730 &self,
731 instance: Option<&ComponentExportIndex>,
732 name: &str,
733 ) -> Option<ComponentExportIndex> {
734 let index = self.lookup_export_index(instance, name)?;
735 Some(ComponentExportIndex {
736 id: self.inner.id,
737 index,
738 })
739 }
740
741 /// Looks up a specific export of this component by `name` optionally nested
742 /// within the `instance` provided.
743 ///
744 /// This method is primarily used to acquire a [`ComponentExportIndex`]
745 /// which can be used with [`Instance`](crate::component::Instance) when
746 /// looking up exports. Export lookup with [`ComponentExportIndex`] can
747 /// skip string lookups at runtime and instead use a more efficient
748 /// index-based lookup.
749 ///
750 /// This method takes a few arguments:
751 ///
752 /// * `engine` - the engine that was used to compile this component.
753 /// * `instance` - an optional "parent instance" for the export being looked
754 /// up. If this is `None` then the export is looked up on the root of the
755 /// component itself, and otherwise the export is looked up on the
756 /// `instance` specified. Note that `instance` must have come from a
757 /// previous invocation of this method.
758 /// * `name` - the name of the export that's being looked up.
759 ///
760 /// If the export is located then two values are returned: a
761 /// [`types::ComponentItem`] which enables introspection about the type of
762 /// the export and a [`ComponentExportIndex`]. The index returned notably
763 /// implements the [`InstanceExportLookup`] trait which enables using it
764 /// with [`Instance::get_func`](crate::component::Instance::get_func) for
765 /// example.
766 ///
767 /// The returned [`types::ComponentItem`] is more expensive to calculate
768 /// than the [`ComponentExportIndex`]. If you only consume the
769 /// [`ComponentExportIndex`], use the related method
770 /// [`Self::get_export_index`] instead.
771 ///
772 /// [`Instance`](crate::component::Instance) has a corresponding method
773 /// [`Instance::get_export`](crate::component::Instance::get_export).
774 ///
775 /// # Examples
776 ///
777 /// ```
778 /// use wasmtime::{Engine, Store};
779 /// use wasmtime::component::{Component, Linker};
780 /// use wasmtime::component::types::ComponentItem;
781 ///
782 /// # fn main() -> wasmtime::Result<()> {
783 /// let engine = Engine::default();
784 /// let component = Component::new(
785 /// &engine,
786 /// r#"
787 /// (component
788 /// (core module $m
789 /// (func (export "f"))
790 /// )
791 /// (core instance $i (instantiate $m))
792 /// (func (export "f")
793 /// (canon lift (core func $i "f")))
794 /// )
795 /// "#,
796 /// )?;
797 ///
798 /// // Perform a lookup of the function "f" before instantiaton.
799 /// let (ty, export) = component.get_export(None, "f").unwrap();
800 /// assert!(matches!(ty, ComponentItem::ComponentFunc(_)));
801 ///
802 /// // After instantiation use `export` to lookup the function in question
803 /// // which notably does not do a string lookup at runtime.
804 /// let mut store = Store::new(&engine, ());
805 /// let instance = Linker::new(&engine).instantiate(&mut store, &component)?;
806 /// let func = instance.get_typed_func::<(), ()>(&mut store, &export)?;
807 /// // ...
808 /// # Ok(())
809 /// # }
810 /// ```
811 pub fn get_export(
812 &self,
813 instance: Option<&ComponentExportIndex>,
814 name: &str,
815 ) -> Option<(types::ComponentItem, ComponentExportIndex)> {
816 let info = self.env_component();
817 let index = self.lookup_export_index(instance, name)?;
818 let item = self.with_uninstantiated_instance_type(|instance| {
819 types::ComponentItem::from_export(
820 &self.inner.engine,
821 &info.export_items[index],
822 instance,
823 )
824 });
825 Some((
826 item,
827 ComponentExportIndex {
828 id: self.inner.id,
829 index,
830 },
831 ))
832 }
833
834 pub(crate) fn lookup_export_index(
835 &self,
836 instance: Option<&ComponentExportIndex>,
837 name: &str,
838 ) -> Option<ExportIndex> {
839 let info = self.env_component();
840 let exports = match instance {
841 Some(idx) => {
842 if idx.id != self.inner.id {
843 return None;
844 }
845 match &info.export_items[idx.index] {
846 Export::Instance { exports, .. } => exports,
847 _ => return None,
848 }
849 }
850 None => &info.exports,
851 };
852 exports.get(name, &NameMapNoIntern).copied()
853 }
854
855 pub(crate) fn id(&self) -> CompiledModuleId {
856 self.inner.id
857 }
858
859 /// Returns the [`Engine`] that this [`Component`] was compiled by.
860 pub fn engine(&self) -> &Engine {
861 &self.inner.engine
862 }
863
864 pub(crate) fn realloc_func_ty(&self) -> &Arc<FuncType> {
865 &self.inner.realloc_func_type
866 }
867
868 /// Returns the `Export::LiftedFunction` metadata associated with `export`.
869 ///
870 /// # Panics
871 ///
872 /// Panics if `export` is out of bounds or if it isn't a `LiftedFunction`.
873 pub(crate) fn export_lifted_function(
874 &self,
875 export: ExportIndex,
876 ) -> (TypeFuncIndex, &CoreDef, OptionsIndex) {
877 let component = self.env_component();
878 match &component.export_items[export] {
879 Export::LiftedFunction { ty, func, options } => (*ty, func, *options),
880 _ => unreachable!(),
881 }
882 }
883}
884
885/// A value which represents a known export of a component.
886///
887/// This is the return value of [`Component::get_export`] and implements the
888/// [`InstanceExportLookup`] trait to work with lookups like
889/// [`Instance::get_func`](crate::component::Instance::get_func).
890#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
891pub struct ComponentExportIndex {
892 pub(crate) id: CompiledModuleId,
893 pub(crate) index: ExportIndex,
894}
895
896impl InstanceExportLookup for ComponentExportIndex {
897 fn lookup(&self, component: &Component) -> Option<ExportIndex> {
898 if component.inner.id == self.id {
899 Some(self.index)
900 } else {
901 None
902 }
903 }
904}
905
906#[cfg(test)]
907mod tests {
908 use crate::component::Component;
909 use crate::{Config, Engine};
910 use wasmtime_environ::MemoryInitialization;
911
912 #[test]
913 fn cow_on_by_default() {
914 let mut config = Config::new();
915 config.wasm_component_model(true);
916 let engine = Engine::new(&config).unwrap();
917 let component = Component::new(
918 &engine,
919 r#"
920 (component
921 (core module
922 (memory 1)
923 (data (i32.const 100) "abcd")
924 )
925 )
926 "#,
927 )
928 .unwrap();
929
930 for (_, module) in component.inner.static_modules.iter() {
931 let init = &module.env_module().memory_initialization;
932 assert!(matches!(init, MemoryInitialization::Static { .. }));
933 }
934 }
935}