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