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() -> 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() -> 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 self.inner.code.image().as_ptr_range() 686 } 687 688 /// Force initialization of copy-on-write images to happen here-and-now 689 /// instead of when they're requested during first instantiation. 690 /// 691 /// When [copy-on-write memory 692 /// initialization](crate::Config::memory_init_cow) is enabled then Wasmtime 693 /// will lazily create the initialization image for a component. This method 694 /// can be used to explicitly dictate when this initialization happens. 695 /// 696 /// Note that this largely only matters on Linux when memfd is used. 697 /// Otherwise the copy-on-write image typically comes from disk and in that 698 /// situation the creation of the image is trivial as the image is always 699 /// sourced from disk. On Linux, though, when memfd is used a memfd is 700 /// created and the initialization image is written to it. 701 /// 702 /// Also note that this method is not required to be called, it's available 703 /// as a performance optimization if required but is otherwise handled 704 /// automatically. 705 pub fn initialize_copy_on_write_image(&self) -> Result<()> { 706 for (_, module) in self.inner.static_modules.iter() { 707 module.initialize_copy_on_write_image()?; 708 } 709 Ok(()) 710 } 711 712 /// Looks up a specific export of this component by `name` optionally nested 713 /// within the `instance` provided. 714 /// 715 /// See related method [`Self::get_export`] for additional docs and 716 /// examples. 717 /// 718 /// This method is primarily used to acquire a [`ComponentExportIndex`] 719 /// which can be used with [`Instance`](crate::component::Instance) when 720 /// looking up exports. Export lookup with [`ComponentExportIndex`] can 721 /// skip string lookups at runtime and instead use a more efficient 722 /// index-based lookup. 723 /// 724 /// This method only returns the [`ComponentExportIndex`]. If you need the 725 /// corresponding [`types::ComponentItem`], use the related function 726 /// [`Self::get_export`]. 727 /// 728 /// 729 /// [`Instance`](crate::component::Instance) has a corresponding method 730 /// [`Instance::get_export_index`](crate::component::Instance::get_export_index). 731 pub fn get_export_index( 732 &self, 733 instance: Option<&ComponentExportIndex>, 734 name: &str, 735 ) -> Option<ComponentExportIndex> { 736 let index = self.lookup_export_index(instance, name)?; 737 Some(ComponentExportIndex { 738 id: self.inner.id, 739 index, 740 }) 741 } 742 743 /// Looks up a specific export of this component by `name` optionally nested 744 /// within the `instance` provided. 745 /// 746 /// This method is primarily used to acquire a [`ComponentExportIndex`] 747 /// which can be used with [`Instance`](crate::component::Instance) when 748 /// looking up exports. Export lookup with [`ComponentExportIndex`] can 749 /// skip string lookups at runtime and instead use a more efficient 750 /// index-based lookup. 751 /// 752 /// This method takes a few arguments: 753 /// 754 /// * `engine` - the engine that was used to compile this component. 755 /// * `instance` - an optional "parent instance" for the export being looked 756 /// up. If this is `None` then the export is looked up on the root of the 757 /// component itself, and otherwise the export is looked up on the 758 /// `instance` specified. Note that `instance` must have come from a 759 /// previous invocation of this method. 760 /// * `name` - the name of the export that's being looked up. 761 /// 762 /// If the export is located then two values are returned: a 763 /// [`types::ComponentItem`] which enables introspection about the type of 764 /// the export and a [`ComponentExportIndex`]. The index returned notably 765 /// implements the [`InstanceExportLookup`] trait which enables using it 766 /// with [`Instance::get_func`](crate::component::Instance::get_func) for 767 /// example. 768 /// 769 /// The returned [`types::ComponentItem`] is more expensive to calculate 770 /// than the [`ComponentExportIndex`]. If you only consume the 771 /// [`ComponentExportIndex`], use the related method 772 /// [`Self::get_export_index`] instead. 773 /// 774 /// [`Instance`](crate::component::Instance) has a corresponding method 775 /// [`Instance::get_export`](crate::component::Instance::get_export). 776 /// 777 /// # Examples 778 /// 779 /// ``` 780 /// use wasmtime::{Engine, Store}; 781 /// use wasmtime::component::{Component, Linker}; 782 /// use wasmtime::component::types::ComponentItem; 783 /// 784 /// # fn main() -> wasmtime::Result<()> { 785 /// let engine = Engine::default(); 786 /// let component = Component::new( 787 /// &engine, 788 /// r#" 789 /// (component 790 /// (core module $m 791 /// (func (export "f")) 792 /// ) 793 /// (core instance $i (instantiate $m)) 794 /// (func (export "f") 795 /// (canon lift (core func $i "f"))) 796 /// ) 797 /// "#, 798 /// )?; 799 /// 800 /// // Perform a lookup of the function "f" before instantiaton. 801 /// let (ty, export) = component.get_export(None, "f").unwrap(); 802 /// assert!(matches!(ty, ComponentItem::ComponentFunc(_))); 803 /// 804 /// // After instantiation use `export` to lookup the function in question 805 /// // which notably does not do a string lookup at runtime. 806 /// let mut store = Store::new(&engine, ()); 807 /// let instance = Linker::new(&engine).instantiate(&mut store, &component)?; 808 /// let func = instance.get_typed_func::<(), ()>(&mut store, &export)?; 809 /// // ... 810 /// # Ok(()) 811 /// # } 812 /// ``` 813 pub fn get_export( 814 &self, 815 instance: Option<&ComponentExportIndex>, 816 name: &str, 817 ) -> Option<(types::ComponentItem, ComponentExportIndex)> { 818 let info = self.env_component(); 819 let index = self.lookup_export_index(instance, name)?; 820 let item = self.with_uninstantiated_instance_type(|instance| { 821 types::ComponentItem::from_export( 822 &self.inner.engine, 823 &info.export_items[index], 824 instance, 825 ) 826 }); 827 Some(( 828 item, 829 ComponentExportIndex { 830 id: self.inner.id, 831 index, 832 }, 833 )) 834 } 835 836 pub(crate) fn lookup_export_index( 837 &self, 838 instance: Option<&ComponentExportIndex>, 839 name: &str, 840 ) -> Option<ExportIndex> { 841 let info = self.env_component(); 842 let exports = match instance { 843 Some(idx) => { 844 if idx.id != self.inner.id { 845 return None; 846 } 847 match &info.export_items[idx.index] { 848 Export::Instance { exports, .. } => exports, 849 _ => return None, 850 } 851 } 852 None => &info.exports, 853 }; 854 exports.get(name, &NameMapNoIntern).copied() 855 } 856 857 pub(crate) fn id(&self) -> CompiledModuleId { 858 self.inner.id 859 } 860 861 /// Returns the [`Engine`] that this [`Component`] was compiled by. 862 pub fn engine(&self) -> &Engine { 863 &self.inner.engine 864 } 865 866 pub(crate) fn realloc_func_ty(&self) -> &Arc<FuncType> { 867 &self.inner.realloc_func_type 868 } 869 870 /// Returns the `Export::LiftedFunction` metadata associated with `export`. 871 /// 872 /// # Panics 873 /// 874 /// Panics if `export` is out of bounds or if it isn't a `LiftedFunction`. 875 pub(crate) fn export_lifted_function( 876 &self, 877 export: ExportIndex, 878 ) -> (TypeFuncIndex, &CoreDef, OptionsIndex) { 879 let component = self.env_component(); 880 match &component.export_items[export] { 881 Export::LiftedFunction { ty, func, options } => (*ty, func, *options), 882 _ => unreachable!(), 883 } 884 } 885 } 886 887 /// A value which represents a known export of a component. 888 /// 889 /// This is the return value of [`Component::get_export`] and implements the 890 /// [`InstanceExportLookup`] trait to work with lookups like 891 /// [`Instance::get_func`](crate::component::Instance::get_func). 892 #[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)] 893 pub struct ComponentExportIndex { 894 pub(crate) id: CompiledModuleId, 895 pub(crate) index: ExportIndex, 896 } 897 898 impl InstanceExportLookup for ComponentExportIndex { 899 fn lookup(&self, component: &Component) -> Option<ExportIndex> { 900 if component.inner.id == self.id { 901 Some(self.index) 902 } else { 903 None 904 } 905 } 906 } 907 908 #[cfg(test)] 909 mod tests { 910 use crate::component::Component; 911 use crate::{CodeBuilder, Config, Engine}; 912 use wasmtime_environ::MemoryInitialization; 913 914 #[test] 915 fn cow_on_by_default() { 916 let mut config = Config::new(); 917 config.wasm_component_model(true); 918 let engine = Engine::new(&config).unwrap(); 919 let component = Component::new( 920 &engine, 921 r#" 922 (component 923 (core module 924 (memory 1) 925 (data (i32.const 100) "abcd") 926 ) 927 ) 928 "#, 929 ) 930 .unwrap(); 931 932 for (_, module) in component.inner.static_modules.iter() { 933 let init = &module.env_module().memory_initialization; 934 assert!(matches!(init, MemoryInitialization::Static { .. })); 935 } 936 } 937 938 #[test] 939 #[cfg_attr(miri, ignore)] 940 fn image_range_is_whole_image() { 941 let wat = r#" 942 (component 943 (core module 944 (memory 1) 945 (data (i32.const 0) "1234") 946 (func (export "f") (param i32) (result i32) 947 local.get 0))) 948 "#; 949 let engine = Engine::default(); 950 let mut builder = CodeBuilder::new(&engine); 951 builder.wasm_binary_or_text(wat.as_bytes(), None).unwrap(); 952 let bytes = builder.compile_component_serialized().unwrap(); 953 954 let comp = unsafe { Component::deserialize(&engine, &bytes).unwrap() }; 955 let image_range = comp.image_range(); 956 let len = image_range.end.addr() - image_range.start.addr(); 957 // Length may be strictly greater if it becomes page-aligned. 958 assert!(len >= bytes.len()); 959 } 960 } 961