1 use crate::prelude::*; 2 #[cfg(feature = "std")] 3 use crate::runtime::vm::open_file_for_mmap; 4 use crate::runtime::vm::{CompiledModuleId, MmapVec, ModuleMemoryImages, VMWasmCallFunction}; 5 use crate::sync::OnceLock; 6 use crate::{ 7 Engine, 8 code::EngineCode, 9 code_memory::CodeMemory, 10 instantiate::CompiledModule, 11 resources::ResourcesRequired, 12 types::{ExportType, ExternType, ImportType}, 13 }; 14 use alloc::sync::Arc; 15 use core::fmt; 16 use core::ops::Range; 17 use core::ptr::NonNull; 18 #[cfg(feature = "std")] 19 use std::{fs::File, path::Path}; 20 use wasmparser::{Parser, ValidPayload, Validator}; 21 #[cfg(feature = "debug")] 22 use wasmtime_environ::FrameTable; 23 use wasmtime_environ::{ 24 CompiledFunctionsTable, CompiledModuleInfo, EntityIndex, HostPtr, ModuleTypes, ObjectKind, 25 TypeTrace, VMOffsets, VMSharedTypeIndex, WasmChecksum, 26 }; 27 #[cfg(feature = "gc")] 28 use wasmtime_unwinder::ExceptionTable; 29 mod registry; 30 31 pub use registry::*; 32 33 /// A compiled WebAssembly module, ready to be instantiated. 34 /// 35 /// A `Module` is a compiled in-memory representation of an input WebAssembly 36 /// binary. A `Module` is then used to create an [`Instance`](crate::Instance) 37 /// through an instantiation process. You cannot call functions or fetch 38 /// globals, for example, on a `Module` because it's purely a code 39 /// representation. Instead you'll need to create an 40 /// [`Instance`](crate::Instance) to interact with the wasm module. 41 /// 42 /// A `Module` can be created by compiling WebAssembly code through APIs such as 43 /// [`Module::new`]. This would be a JIT-style use case where code is compiled 44 /// just before it's used. Alternatively a `Module` can be compiled in one 45 /// process and [`Module::serialize`] can be used to save it to storage. A later 46 /// call to [`Module::deserialize`] will quickly load the module to execute and 47 /// does not need to compile any code, representing a more AOT-style use case. 48 /// 49 /// Currently a `Module` does not implement any form of tiering or dynamic 50 /// optimization of compiled code. Creation of a `Module` via [`Module::new`] or 51 /// related APIs will perform the entire compilation step synchronously. When 52 /// finished no further compilation will happen at runtime or later during 53 /// execution of WebAssembly instances for example. 54 /// 55 /// Compilation of WebAssembly by default goes through Cranelift and is 56 /// recommended to be done once-per-module. The same WebAssembly binary need not 57 /// be compiled multiple times and can instead used an embedder-cached result of 58 /// the first call. 59 /// 60 /// `Module` is thread-safe and safe to share across threads. 61 /// 62 /// ## Modules and `Clone` 63 /// 64 /// Using `clone` on a `Module` is a cheap operation. It will not create an 65 /// entirely new module, but rather just a new reference to the existing module. 66 /// In other words it's a shallow copy, not a deep copy. 67 /// 68 /// ## Examples 69 /// 70 /// There are a number of ways you can create a `Module`, for example pulling 71 /// the bytes from a number of locations. One example is loading a module from 72 /// the filesystem: 73 /// 74 /// ```no_run 75 /// # use wasmtime::*; 76 /// # fn main() -> Result<()> { 77 /// let engine = Engine::default(); 78 /// let module = Module::from_file(&engine, "path/to/foo.wasm")?; 79 /// # Ok(()) 80 /// # } 81 /// ``` 82 /// 83 /// You can also load the wasm text format if more convenient too: 84 /// 85 /// ```no_run 86 /// # use wasmtime::*; 87 /// # fn main() -> Result<()> { 88 /// let engine = Engine::default(); 89 /// // Now we're using the WebAssembly text extension: `.wat`! 90 /// let module = Module::from_file(&engine, "path/to/foo.wat")?; 91 /// # Ok(()) 92 /// # } 93 /// ``` 94 /// 95 /// And if you've already got the bytes in-memory you can use the 96 /// [`Module::new`] constructor: 97 /// 98 /// ```no_run 99 /// # use wasmtime::*; 100 /// # fn main() -> Result<()> { 101 /// let engine = Engine::default(); 102 /// # let wasm_bytes: Vec<u8> = Vec::new(); 103 /// let module = Module::new(&engine, &wasm_bytes)?; 104 /// 105 /// // It also works with the text format! 106 /// let module = Module::new(&engine, "(module (func))")?; 107 /// # Ok(()) 108 /// # } 109 /// ``` 110 /// 111 /// Serializing and deserializing a module looks like: 112 /// 113 /// ```no_run 114 /// # use wasmtime::*; 115 /// # fn main() -> Result<()> { 116 /// let engine = Engine::default(); 117 /// # let wasm_bytes: Vec<u8> = Vec::new(); 118 /// let module = Module::new(&engine, &wasm_bytes)?; 119 /// let module_bytes = module.serialize()?; 120 /// 121 /// // ... can save `module_bytes` to disk or other storage ... 122 /// 123 /// // recreate the module from the serialized bytes. For the `unsafe` bits 124 /// // see the documentation of `deserialize`. 125 /// let module = unsafe { Module::deserialize(&engine, &module_bytes)? }; 126 /// # Ok(()) 127 /// # } 128 /// ``` 129 /// 130 /// [`Config`]: crate::Config 131 #[derive(Clone)] 132 pub struct Module { 133 inner: Arc<ModuleInner>, 134 } 135 136 struct ModuleInner { 137 engine: Engine, 138 /// The compiled artifacts for this module that will be instantiated and 139 /// executed. 140 module: CompiledModule, 141 142 /// Runtime information such as the underlying mmap, type information, etc. 143 /// 144 /// Note that this `Arc` is used to share information between compiled 145 /// modules within a component. For bare core wasm modules created with 146 /// `Module::new`, for example, this is a uniquely owned `Arc`. 147 code: Arc<EngineCode>, 148 149 /// A set of initialization images for memories, if any. 150 /// 151 /// Note that this is behind a `OnceCell` to lazily create this image. On 152 /// Linux where `memfd_create` may be used to create the backing memory 153 /// image this is a pretty expensive operation, so by deferring it this 154 /// improves memory usage for modules that are created but may not ever be 155 /// instantiated. 156 memory_images: OnceLock<Option<ModuleMemoryImages>>, 157 158 /// Flag indicating whether this module can be serialized or not. 159 #[cfg(any(feature = "cranelift", feature = "winch"))] 160 serializable: bool, 161 162 /// Runtime offset information for `VMContext`. 163 offsets: VMOffsets<HostPtr>, 164 165 /// The checksum of the source binary from which this module was compiled. 166 checksum: WasmChecksum, 167 } 168 169 impl fmt::Debug for Module { 170 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 171 f.debug_struct("Module") 172 .field("name", &self.name()) 173 .finish_non_exhaustive() 174 } 175 } 176 177 impl fmt::Debug for ModuleInner { 178 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 179 f.debug_struct("ModuleInner") 180 .field("name", &self.module.module().name.as_ref()) 181 .finish_non_exhaustive() 182 } 183 } 184 185 impl Module { 186 /// Creates a new WebAssembly `Module` from the given in-memory `bytes`. 187 /// 188 /// The `bytes` provided must be in one of the following formats: 189 /// 190 /// * A [binary-encoded][binary] WebAssembly module. This is always supported. 191 /// * A [text-encoded][text] instance of the WebAssembly text format. 192 /// This is only supported when the `wat` feature of this crate is enabled. 193 /// If this is supplied then the text format will be parsed before validation. 194 /// Note that the `wat` feature is enabled by default. 195 /// 196 /// The data for the wasm module must be loaded in-memory if it's present 197 /// elsewhere, for example on disk. This requires that the entire binary is 198 /// loaded into memory all at once, this API does not support streaming 199 /// compilation of a module. 200 /// 201 /// The WebAssembly binary will be decoded and validated. It will also be 202 /// compiled according to the configuration of the provided `engine`. 203 /// 204 /// # Errors 205 /// 206 /// This function may fail and return an error. Errors may include 207 /// situations such as: 208 /// 209 /// * The binary provided could not be decoded because it's not a valid 210 /// WebAssembly binary 211 /// * The WebAssembly binary may not validate (e.g. contains type errors) 212 /// * Implementation-specific limits were exceeded with a valid binary (for 213 /// example too many locals) 214 /// * The wasm binary may use features that are not enabled in the 215 /// configuration of `engine` 216 /// * If the `wat` feature is enabled and the input is text, then it may be 217 /// rejected if it fails to parse. 218 /// 219 /// The error returned should contain full information about why module 220 /// creation failed if one is returned. 221 /// 222 /// [binary]: https://webassembly.github.io/spec/core/binary/index.html 223 /// [text]: https://webassembly.github.io/spec/core/text/index.html 224 /// 225 /// # Examples 226 /// 227 /// The `new` function can be invoked with a in-memory array of bytes: 228 /// 229 /// ```no_run 230 /// # use wasmtime::*; 231 /// # fn main() -> Result<()> { 232 /// # let engine = Engine::default(); 233 /// # let wasm_bytes: Vec<u8> = Vec::new(); 234 /// let module = Module::new(&engine, &wasm_bytes)?; 235 /// # Ok(()) 236 /// # } 237 /// ``` 238 /// 239 /// Or you can also pass in a string to be parsed as the wasm text 240 /// format: 241 /// 242 /// ``` 243 /// # use wasmtime::*; 244 /// # fn main() -> Result<()> { 245 /// # let engine = Engine::default(); 246 /// let module = Module::new(&engine, "(module (func))")?; 247 /// # Ok(()) 248 /// # } 249 /// ``` 250 #[cfg(any(feature = "cranelift", feature = "winch"))] 251 pub fn new(engine: &Engine, bytes: impl AsRef<[u8]>) -> Result<Module> { 252 crate::CodeBuilder::new(engine) 253 .wasm_binary_or_text(bytes.as_ref(), None)? 254 .compile_module() 255 } 256 257 /// Creates a new WebAssembly `Module` from the contents of the given 258 /// `file` on disk. 259 /// 260 /// This is a convenience function that will read the `file` provided and 261 /// pass the bytes to the [`Module::new`] function. For more information 262 /// see [`Module::new`] 263 /// 264 /// # Examples 265 /// 266 /// ```no_run 267 /// # use wasmtime::*; 268 /// # fn main() -> Result<()> { 269 /// let engine = Engine::default(); 270 /// let module = Module::from_file(&engine, "./path/to/foo.wasm")?; 271 /// # Ok(()) 272 /// # } 273 /// ``` 274 /// 275 /// The `.wat` text format is also supported: 276 /// 277 /// ```no_run 278 /// # use wasmtime::*; 279 /// # fn main() -> Result<()> { 280 /// # let engine = Engine::default(); 281 /// let module = Module::from_file(&engine, "./path/to/foo.wat")?; 282 /// # Ok(()) 283 /// # } 284 /// ``` 285 #[cfg(all(feature = "std", any(feature = "cranelift", feature = "winch")))] 286 pub fn from_file(engine: &Engine, file: impl AsRef<Path>) -> Result<Module> { 287 crate::CodeBuilder::new(engine) 288 .wasm_binary_or_text_file(file.as_ref())? 289 .compile_module() 290 } 291 292 /// Creates a new WebAssembly `Module` from the given in-memory `binary` 293 /// data. 294 /// 295 /// This is similar to [`Module::new`] except that it requires that the 296 /// `binary` input is a WebAssembly binary, the text format is not supported 297 /// by this function. It's generally recommended to use [`Module::new`], but 298 /// if it's required to not support the text format this function can be 299 /// used instead. 300 /// 301 /// # Examples 302 /// 303 /// ``` 304 /// # use wasmtime::*; 305 /// # fn main() -> Result<()> { 306 /// # let engine = Engine::default(); 307 /// let wasm = b"\0asm\x01\0\0\0"; 308 /// let module = Module::from_binary(&engine, wasm)?; 309 /// # Ok(()) 310 /// # } 311 /// ``` 312 /// 313 /// Note that the text format is **not** accepted by this function: 314 /// 315 /// ``` 316 /// # use wasmtime::*; 317 /// # fn main() -> Result<()> { 318 /// # let engine = Engine::default(); 319 /// assert!(Module::from_binary(&engine, b"(module)").is_err()); 320 /// # Ok(()) 321 /// # } 322 /// ``` 323 #[cfg(any(feature = "cranelift", feature = "winch"))] 324 pub fn from_binary(engine: &Engine, binary: &[u8]) -> Result<Module> { 325 crate::CodeBuilder::new(engine) 326 .wasm_binary(binary, None)? 327 .compile_module() 328 } 329 330 /// Creates a new WebAssembly `Module` from the contents of the given `file` 331 /// on disk, but with assumptions that the file is from a trusted source. 332 /// The file should be a binary- or text-format WebAssembly module, or a 333 /// precompiled artifact generated by the same version of Wasmtime. 334 /// 335 /// # Unsafety 336 /// 337 /// All of the reasons that [`deserialize`] is `unsafe` apply to this 338 /// function as well. Arbitrary data loaded from a file may trick Wasmtime 339 /// into arbitrary code execution since the contents of the file are not 340 /// validated to be a valid precompiled module. 341 /// 342 /// [`deserialize`]: Module::deserialize 343 /// 344 /// Additionally though this function is also `unsafe` because the file 345 /// referenced must remain unchanged and a valid precompiled module for the 346 /// entire lifetime of the [`Module`] returned. Any changes to the file on 347 /// disk may change future instantiations of the module to be incorrect. 348 /// This is because the file is mapped into memory and lazily loaded pages 349 /// reflect the current state of the file, not necessarily the original 350 /// state of the file. 351 #[cfg(all(feature = "std", any(feature = "cranelift", feature = "winch")))] 352 pub unsafe fn from_trusted_file(engine: &Engine, file: impl AsRef<Path>) -> Result<Module> { 353 let open_file = open_file_for_mmap(file.as_ref())?; 354 let mmap = crate::runtime::vm::MmapVec::from_file(open_file)?; 355 if &mmap[0..4] == b"\x7fELF" { 356 let code = engine.load_code(mmap, ObjectKind::Module)?; 357 return Module::from_parts(engine, code, None); 358 } 359 360 crate::CodeBuilder::new(engine) 361 .wasm_binary_or_text(&mmap[..], Some(file.as_ref()))? 362 .compile_module() 363 } 364 365 /// Deserializes an in-memory compiled module previously created with 366 /// [`Module::serialize`] or [`Engine::precompile_module`]. 367 /// 368 /// This function will deserialize the binary blobs emitted by 369 /// [`Module::serialize`] and [`Engine::precompile_module`] back into an 370 /// in-memory [`Module`] that's ready to be instantiated. 371 /// 372 /// Note that the [`Module::deserialize_file`] method is more optimized than 373 /// this function, so if the serialized module is already present in a file 374 /// it's recommended to use that method instead. 375 /// 376 /// # Unsafety 377 /// 378 /// This function is marked as `unsafe` because if fed invalid input or used 379 /// improperly this could lead to memory safety vulnerabilities. This method 380 /// should not, for example, be exposed to arbitrary user input. 381 /// 382 /// The structure of the binary blob read here is only lightly validated 383 /// internally in `wasmtime`. This is intended to be an efficient 384 /// "rehydration" for a [`Module`] which has very few runtime checks beyond 385 /// deserialization. Arbitrary input could, for example, replace valid 386 /// compiled code with any other valid compiled code, meaning that this can 387 /// trivially be used to execute arbitrary code otherwise. 388 /// 389 /// For these reasons this function is `unsafe`. This function is only 390 /// designed to receive the previous input from [`Module::serialize`] and 391 /// [`Engine::precompile_module`]. If the exact output of those functions 392 /// (unmodified) is passed to this function then calls to this function can 393 /// be considered safe. It is the caller's responsibility to provide the 394 /// guarantee that only previously-serialized bytes are being passed in 395 /// here. 396 /// 397 /// Note that this function is designed to be safe receiving output from 398 /// *any* compiled version of `wasmtime` itself. This means that it is safe 399 /// to feed output from older versions of Wasmtime into this function, in 400 /// addition to newer versions of wasmtime (from the future!). These inputs 401 /// will deterministically and safely produce an `Err`. This function only 402 /// successfully accepts inputs from the same version of `wasmtime`, but the 403 /// safety guarantee only applies to externally-defined blobs of bytes, not 404 /// those defined by any version of wasmtime. (this means that if you cache 405 /// blobs across versions of wasmtime you can be safely guaranteed that 406 /// future versions of wasmtime will reject old cache entries). 407 pub unsafe fn deserialize(engine: &Engine, bytes: impl AsRef<[u8]>) -> Result<Module> { 408 let code = engine.load_code_bytes(bytes.as_ref(), ObjectKind::Module)?; 409 Module::from_parts(engine, code, None) 410 } 411 412 /// In-place deserialization of an in-memory compiled module previously 413 /// created with [`Module::serialize`] or [`Engine::precompile_module`]. 414 /// 415 /// See [`Self::deserialize`] for additional information; this method 416 /// works identically except that it will not create a copy of the provided 417 /// memory but will use it directly. 418 /// 419 /// # Unsafety 420 /// 421 /// All of the safety notes from [`Self::deserialize`] apply here as well 422 /// with the additional constraint that the code memory provide by `memory` 423 /// lives for as long as the module and is nevery externally modified for 424 /// the lifetime of the deserialized module. 425 pub unsafe fn deserialize_raw(engine: &Engine, memory: NonNull<[u8]>) -> Result<Module> { 426 // SAFETY: the contract required by `load_code_raw` is the same as this 427 // function. 428 let code = unsafe { engine.load_code_raw(memory, ObjectKind::Module)? }; 429 Module::from_parts(engine, code, None) 430 } 431 432 /// Same as [`deserialize`], except that the contents of `path` are read to 433 /// deserialize into a [`Module`]. 434 /// 435 /// This method is provided because it can be faster than [`deserialize`] 436 /// since the data doesn't need to be copied around, but rather the module 437 /// can be used directly from an mmap'd view of the file provided. 438 /// 439 /// [`deserialize`]: Module::deserialize 440 /// 441 /// # Unsafety 442 /// 443 /// All of the reasons that [`deserialize`] is `unsafe` applies to this 444 /// function as well. Arbitrary data loaded from a file may trick Wasmtime 445 /// into arbitrary code execution since the contents of the file are not 446 /// validated to be a valid precompiled module. 447 /// 448 /// Additionally though this function is also `unsafe` because the file 449 /// referenced must remain unchanged and a valid precompiled module for the 450 /// entire lifetime of the [`Module`] returned. Any changes to the file on 451 /// disk may change future instantiations of the module to be incorrect. 452 /// This is because the file is mapped into memory and lazily loaded pages 453 /// reflect the current state of the file, not necessarily the original 454 /// state of the file. 455 #[cfg(feature = "std")] 456 pub unsafe fn deserialize_file(engine: &Engine, path: impl AsRef<Path>) -> Result<Module> { 457 let file = open_file_for_mmap(path.as_ref())?; 458 // SAFETY: the contract of `deserialize_open_file` is the samea s this 459 // function. 460 unsafe { 461 Self::deserialize_open_file(engine, file) 462 .with_context(|| format!("failed deserialization for: {}", path.as_ref().display())) 463 } 464 } 465 466 /// Same as [`deserialize_file`], except that it takes an open `File` 467 /// instead of a path. 468 /// 469 /// This method is provided because it can be used instead of 470 /// [`deserialize_file`] in situations where `wasmtime` is running with 471 /// limited file system permissions. In that case a process 472 /// with file system access can pass already opened files to `wasmtime`. 473 /// 474 /// [`deserialize_file`]: Module::deserialize_file 475 /// 476 /// Note that the corresponding will be mapped as private writeable 477 /// (copy-on-write) and executable. For `windows` this means the file needs 478 /// to be opened with at least `FILE_GENERIC_READ | FILE_GENERIC_EXECUTE` 479 /// [`access_mode`]. 480 /// 481 /// [`access_mode`]: https://doc.rust-lang.org/std/os/windows/fs/trait.OpenOptionsExt.html#tymethod.access_mode 482 /// 483 /// # Unsafety 484 /// 485 /// All of the reasons that [`deserialize_file`] is `unsafe` applies to this 486 /// function as well. 487 #[cfg(feature = "std")] 488 pub unsafe fn deserialize_open_file(engine: &Engine, file: File) -> Result<Module> { 489 let code = engine.load_code_file(file, ObjectKind::Module)?; 490 Module::from_parts(engine, code, None) 491 } 492 493 /// Entrypoint for creating a `Module` for all above functions, both 494 /// of the AOT and jit-compiled categories. 495 /// 496 /// In all cases the compilation artifact, `code_memory`, is provided here. 497 /// The `info_and_types` argument is `None` when a module is being 498 /// deserialized from a precompiled artifact or it's `Some` if it was just 499 /// compiled and the values are already available. 500 pub(crate) fn from_parts( 501 engine: &Engine, 502 code_memory: Arc<CodeMemory>, 503 info_and_types: Option<(CompiledModuleInfo, CompiledFunctionsTable, ModuleTypes)>, 504 ) -> Result<Self> { 505 // Acquire this module's metadata and type information, deserializing 506 // it from the provided artifact if it wasn't otherwise provided 507 // already. 508 let (mut info, index, mut types) = match info_and_types { 509 Some((info, index, types)) => (info, index, types), 510 None => postcard::from_bytes(code_memory.wasmtime_info())?, 511 }; 512 513 // Register function type signatures into the engine for the lifetime 514 // of the `Module` that will be returned. This notably also builds up 515 // maps for trampolines to be used for this module when inserted into 516 // stores. 517 // 518 // Note that the unsafety here should be ok since the `trampolines` 519 // field should only point to valid trampoline function pointers 520 // within the text section. 521 let signatures = engine 522 .register_and_canonicalize_types(&mut types, core::iter::once(&mut info.module))?; 523 524 // Package up all our data into an `EngineCode` and delegate to the final 525 // step of module compilation. 526 let code = try_new::<Arc<_>>(EngineCode::new(code_memory, signatures, types.into())?)?; 527 let index = try_new::<Arc<_>>(index)?; 528 Module::from_parts_raw(engine, code, info, index, true) 529 } 530 531 pub(crate) fn from_parts_raw( 532 engine: &Engine, 533 code: Arc<EngineCode>, 534 info: CompiledModuleInfo, 535 index: Arc<CompiledFunctionsTable>, 536 serializable: bool, 537 ) -> Result<Self> { 538 let checksum = info.checksum; 539 let module = CompiledModule::from_artifacts(code.clone(), info, index, engine.profiler())?; 540 541 // Validate the module can be used with the current instance allocator. 542 let offsets = VMOffsets::new(HostPtr, module.module()); 543 engine 544 .allocator() 545 .validate_module(module.module(), &offsets)?; 546 547 let _ = serializable; 548 549 Ok(Self { 550 inner: try_new::<Arc<_>>(ModuleInner { 551 engine: engine.clone(), 552 code, 553 memory_images: OnceLock::new(), 554 module, 555 #[cfg(any(feature = "cranelift", feature = "winch"))] 556 serializable, 557 offsets, 558 checksum, 559 })?, 560 }) 561 } 562 563 /// Validates `binary` input data as a WebAssembly binary given the 564 /// configuration in `engine`. 565 /// 566 /// This function will perform a speedy validation of the `binary` input 567 /// WebAssembly module (which is in [binary form][binary], the text format 568 /// is not accepted by this function) and return either `Ok` or `Err` 569 /// depending on the results of validation. The `engine` argument indicates 570 /// configuration for WebAssembly features, for example, which are used to 571 /// indicate what should be valid and what shouldn't be. 572 /// 573 /// Validation automatically happens as part of [`Module::new`]. 574 /// 575 /// # Errors 576 /// 577 /// If validation fails for any reason (type check error, usage of a feature 578 /// that wasn't enabled, etc) then an error with a description of the 579 /// validation issue will be returned. 580 /// 581 /// [binary]: https://webassembly.github.io/spec/core/binary/index.html 582 pub fn validate(engine: &Engine, binary: &[u8]) -> Result<()> { 583 let mut validator = Validator::new_with_features(engine.features()); 584 585 let mut functions = Vec::new(); 586 for payload in Parser::new(0).parse_all(binary) { 587 let payload = payload?; 588 if let ValidPayload::Func(a, b) = validator.payload(&payload)? { 589 functions.push((a, b)); 590 } 591 if let wasmparser::Payload::Version { encoding, .. } = &payload { 592 if let wasmparser::Encoding::Component = encoding { 593 bail!("component passed to module validation"); 594 } 595 } 596 } 597 598 engine.run_maybe_parallel(functions, |(validator, body)| { 599 // FIXME: it would be best here to use a rayon-specific parallel 600 // iterator that maintains state-per-thread to share the function 601 // validator allocations (`Default::default` here) across multiple 602 // functions. 603 validator.into_validator(Default::default()).validate(&body) 604 })?; 605 Ok(()) 606 } 607 608 /// Serializes this module to a vector of bytes. 609 /// 610 /// This function is similar to the [`Engine::precompile_module`] method 611 /// where it produces an artifact of Wasmtime which is suitable to later 612 /// pass into [`Module::deserialize`]. If a module is never instantiated 613 /// then it's recommended to use [`Engine::precompile_module`] instead of 614 /// this method, but if a module is both instantiated and serialized then 615 /// this method can be useful to get the serialized version without 616 /// compiling twice. 617 #[cfg(any(feature = "cranelift", feature = "winch"))] 618 pub fn serialize(&self) -> Result<Vec<u8>> { 619 // The current representation of compiled modules within a compiled 620 // component means that it cannot be serialized. The mmap returned here 621 // is the mmap for the entire component and while it contains all 622 // necessary data to deserialize this particular module it's all 623 // embedded within component-specific information. 624 // 625 // It's not the hardest thing in the world to support this but it's 626 // expected that there's not much of a use case at this time. In theory 627 // all that needs to be done is to edit the `.wasmtime.info` section 628 // to contains this module's metadata instead of the metadata for the 629 // whole component. The metadata itself is fairly trivially 630 // recreateable here it's more that there's no easy one-off API for 631 // editing the sections of an ELF object to use here. 632 // 633 // Overall for now this simply always returns an error in this 634 // situation. If you're reading this and feel that the situation should 635 // be different please feel free to open an issue. 636 if !self.inner.serializable { 637 bail!("cannot serialize a module exported from a component"); 638 } 639 Ok(self.engine_code().image().to_vec()) 640 } 641 642 pub(crate) fn compiled_module(&self) -> &CompiledModule { 643 &self.inner.module 644 } 645 646 pub(crate) fn engine_code(&self) -> &Arc<EngineCode> { 647 &self.inner.code 648 } 649 650 pub(crate) fn env_module(&self) -> &Arc<wasmtime_environ::Module> { 651 self.compiled_module().module() 652 } 653 654 pub(crate) fn types(&self) -> &ModuleTypes { 655 self.inner.code.module_types() 656 } 657 658 #[cfg(any(feature = "component-model", feature = "gc-drc"))] 659 pub(crate) fn signatures(&self) -> &crate::type_registry::TypeCollection { 660 self.inner.code.signatures() 661 } 662 663 /// Returns identifier/name that this [`Module`] has. This name 664 /// is used in traps/backtrace details. 665 /// 666 /// Note that most LLVM/clang/Rust-produced modules do not have a name 667 /// associated with them, but other wasm tooling can be used to inject or 668 /// add a name. 669 /// 670 /// # Examples 671 /// 672 /// ``` 673 /// # use wasmtime::*; 674 /// # fn main() -> Result<()> { 675 /// # let engine = Engine::default(); 676 /// let module = Module::new(&engine, "(module $foo)")?; 677 /// assert_eq!(module.name(), Some("foo")); 678 /// 679 /// let module = Module::new(&engine, "(module)")?; 680 /// assert_eq!(module.name(), None); 681 /// 682 /// # Ok(()) 683 /// # } 684 /// ``` 685 pub fn name(&self) -> Option<&str> { 686 let module = self.compiled_module().module(); 687 let name = module.name?; 688 Some(&module.strings[name]) 689 } 690 691 /// Returns the original Wasm bytecode for this module, if it is 692 /// available. 693 /// 694 /// Bytecode is only retained when the [`Engine`] was configured with 695 /// `guest-debug` support enabled (see [`Config::guest_debug`]). Returns 696 /// `None` when the module was compiled without that option. 697 /// 698 /// [`Config::guest_debug`]: crate::Config::guest_debug 699 pub fn debug_bytecode(&self) -> Option<&[u8]> { 700 self.compiled_module().bytecode() 701 } 702 703 /// Returns the list of imports that this [`Module`] has and must be 704 /// satisfied. 705 /// 706 /// This function returns the list of imports that the wasm module has, but 707 /// only the types of each import. The type of each import is used to 708 /// typecheck the [`Instance::new`](crate::Instance::new) method's `imports` 709 /// argument. The arguments to that function must match up 1-to-1 with the 710 /// entries in the array returned here. 711 /// 712 /// The imports returned reflect the order of the imports in the wasm module 713 /// itself, and note that no form of deduplication happens. 714 /// 715 /// # Examples 716 /// 717 /// Modules with no imports return an empty list here: 718 /// 719 /// ``` 720 /// # use wasmtime::*; 721 /// # fn main() -> Result<()> { 722 /// # let engine = Engine::default(); 723 /// let module = Module::new(&engine, "(module)")?; 724 /// assert_eq!(module.imports().len(), 0); 725 /// # Ok(()) 726 /// # } 727 /// ``` 728 /// 729 /// and modules with imports will have a non-empty list: 730 /// 731 /// ``` 732 /// # use wasmtime::*; 733 /// # fn main() -> Result<()> { 734 /// # let engine = Engine::default(); 735 /// let wat = r#" 736 /// (module 737 /// (import "host" "foo" (func)) 738 /// ) 739 /// "#; 740 /// let module = Module::new(&engine, wat)?; 741 /// assert_eq!(module.imports().len(), 1); 742 /// let import = module.imports().next().unwrap(); 743 /// assert_eq!(import.module(), "host"); 744 /// assert_eq!(import.name(), "foo"); 745 /// match import.ty() { 746 /// ExternType::Func(_) => { /* ... */ } 747 /// _ => panic!("unexpected import type!"), 748 /// } 749 /// # Ok(()) 750 /// # } 751 /// ``` 752 pub fn imports<'module>( 753 &'module self, 754 ) -> impl ExactSizeIterator<Item = ImportType<'module>> + 'module { 755 let module = self.compiled_module().module(); 756 let types = self.types(); 757 let engine = self.engine(); 758 module.imports().map(move |(imp_mod, imp_field, ty)| { 759 debug_assert!(ty.is_canonicalized_for_runtime_usage()); 760 ImportType::new(imp_mod, imp_field, ty, types, engine) 761 }) 762 } 763 764 /// Returns the list of exports that this [`Module`] has and will be 765 /// available after instantiation. 766 /// 767 /// This function will return the type of each item that will be returned 768 /// from [`Instance::exports`](crate::Instance::exports). Each entry in this 769 /// list corresponds 1-to-1 with that list, and the entries here will 770 /// indicate the name of the export along with the type of the export. 771 /// 772 /// # Examples 773 /// 774 /// Modules might not have any exports: 775 /// 776 /// ``` 777 /// # use wasmtime::*; 778 /// # fn main() -> Result<()> { 779 /// # let engine = Engine::default(); 780 /// let module = Module::new(&engine, "(module)")?; 781 /// assert!(module.exports().next().is_none()); 782 /// # Ok(()) 783 /// # } 784 /// ``` 785 /// 786 /// When the exports are not empty, you can inspect each export: 787 /// 788 /// ``` 789 /// # use wasmtime::*; 790 /// # fn main() -> Result<()> { 791 /// # let engine = Engine::default(); 792 /// let wat = r#" 793 /// (module 794 /// (func (export "foo")) 795 /// (memory (export "memory") 1) 796 /// ) 797 /// "#; 798 /// let module = Module::new(&engine, wat)?; 799 /// assert_eq!(module.exports().len(), 2); 800 /// 801 /// let mut exports = module.exports(); 802 /// let foo = exports.next().unwrap(); 803 /// assert_eq!(foo.name(), "foo"); 804 /// match foo.ty() { 805 /// ExternType::Func(_) => { /* ... */ } 806 /// _ => panic!("unexpected export type!"), 807 /// } 808 /// 809 /// let memory = exports.next().unwrap(); 810 /// assert_eq!(memory.name(), "memory"); 811 /// match memory.ty() { 812 /// ExternType::Memory(_) => { /* ... */ } 813 /// _ => panic!("unexpected export type!"), 814 /// } 815 /// # Ok(()) 816 /// # } 817 /// ``` 818 pub fn exports<'module>( 819 &'module self, 820 ) -> impl ExactSizeIterator<Item = ExportType<'module>> + 'module { 821 let module = self.compiled_module().module(); 822 let types = self.types(); 823 let engine = self.engine(); 824 module.exports.iter().map(move |(name, entity_index)| { 825 ExportType::new( 826 &module.strings[name], 827 module.type_of(*entity_index), 828 types, 829 engine, 830 ) 831 }) 832 } 833 834 /// Looks up an export in this [`Module`] by name. 835 /// 836 /// This function will return the type of an export with the given name. 837 /// 838 /// # Examples 839 /// 840 /// There may be no export with that name: 841 /// 842 /// ``` 843 /// # use wasmtime::*; 844 /// # fn main() -> Result<()> { 845 /// # let engine = Engine::default(); 846 /// let module = Module::new(&engine, "(module)")?; 847 /// assert!(module.get_export("foo").is_none()); 848 /// # Ok(()) 849 /// # } 850 /// ``` 851 /// 852 /// When there is an export with that name, it is returned: 853 /// 854 /// ``` 855 /// # use wasmtime::*; 856 /// # fn main() -> Result<()> { 857 /// # let engine = Engine::default(); 858 /// let wat = r#" 859 /// (module 860 /// (func (export "foo")) 861 /// (memory (export "memory") 1) 862 /// ) 863 /// "#; 864 /// let module = Module::new(&engine, wat)?; 865 /// let foo = module.get_export("foo"); 866 /// assert!(foo.is_some()); 867 /// 868 /// let foo = foo.unwrap(); 869 /// match foo { 870 /// ExternType::Func(_) => { /* ... */ } 871 /// _ => panic!("unexpected export type!"), 872 /// } 873 /// 874 /// # Ok(()) 875 /// # } 876 /// ``` 877 pub fn get_export(&self, name: &str) -> Option<ExternType> { 878 let module = self.compiled_module().module(); 879 let name = module.strings.get_atom(name)?; 880 let entity_index = module.exports.get(&name)?; 881 Some(ExternType::from_wasmtime( 882 self.engine(), 883 self.types(), 884 &module.type_of(*entity_index), 885 )) 886 } 887 888 /// Looks up an export in this [`Module`] by name to get its index. 889 /// 890 /// This function will return the index of an export with the given name. This can be useful 891 /// to avoid the cost of looking up the export by name multiple times. Instead the 892 /// [`ModuleExport`] can be stored and used to look up the export on the 893 /// [`Instance`](crate::Instance) later. 894 pub fn get_export_index(&self, name: &str) -> Option<ModuleExport> { 895 let compiled_module = self.compiled_module(); 896 let module = compiled_module.module(); 897 let name = module.strings.get_atom(name)?; 898 let entity = *module.exports.get(&name)?; 899 Some(ModuleExport { 900 module: self.id(), 901 entity, 902 }) 903 } 904 905 /// Returns the [`Engine`] that this [`Module`] was compiled by. 906 pub fn engine(&self) -> &Engine { 907 &self.inner.engine 908 } 909 910 #[allow( 911 unused, 912 reason = "used only for verification with wasmtime `rr` feature \ 913 and requires a lot of unnecessary gating across crates" 914 )] 915 pub(crate) fn checksum(&self) -> &WasmChecksum { 916 &self.inner.checksum 917 } 918 919 /// Returns a summary of the resources required to instantiate this 920 /// [`Module`]. 921 /// 922 /// Potential uses of the returned information: 923 /// 924 /// * Determining whether your pooling allocator configuration supports 925 /// instantiating this module. 926 /// 927 /// * Deciding how many of which `Module` you want to instantiate within a 928 /// fixed amount of resources, e.g. determining whether to create 5 929 /// instances of module X or 10 instances of module Y. 930 /// 931 /// # Example 932 /// 933 /// ``` 934 /// # fn main() -> wasmtime::Result<()> { 935 /// use wasmtime::{Config, Engine, Module}; 936 /// 937 /// let mut config = Config::new(); 938 /// config.wasm_multi_memory(true); 939 /// let engine = Engine::new(&config)?; 940 /// 941 /// let module = Module::new(&engine, r#" 942 /// (module 943 /// ;; Import a memory. Doesn't count towards required resources. 944 /// (import "a" "b" (memory 10)) 945 /// ;; Define two local memories. These count towards the required 946 /// ;; resources. 947 /// (memory 1) 948 /// (memory 6) 949 /// ) 950 /// "#)?; 951 /// 952 /// let resources = module.resources_required(); 953 /// 954 /// // Instantiating the module will require allocating two memories, and 955 /// // the maximum initial memory size is six Wasm pages. 956 /// assert_eq!(resources.num_memories, 2); 957 /// assert_eq!(resources.max_initial_memory_size, Some(6)); 958 /// 959 /// // The module doesn't need any tables. 960 /// assert_eq!(resources.num_tables, 0); 961 /// assert_eq!(resources.max_initial_table_size, None); 962 /// # Ok(()) } 963 /// ``` 964 pub fn resources_required(&self) -> ResourcesRequired { 965 let em = self.env_module(); 966 let num_memories = u32::try_from(em.num_defined_memories()).unwrap(); 967 let max_initial_memory_size = em 968 .memories 969 .values() 970 .skip(em.num_imported_memories) 971 .map(|memory| memory.limits.min) 972 .max(); 973 let num_tables = u32::try_from(em.num_defined_tables()).unwrap(); 974 let max_initial_table_size = em 975 .tables 976 .values() 977 .skip(em.num_imported_tables) 978 .map(|table| table.limits.min) 979 .max(); 980 ResourcesRequired { 981 num_memories, 982 max_initial_memory_size, 983 num_tables, 984 max_initial_table_size, 985 } 986 } 987 988 /// Returns the range of bytes in memory where this module's compilation 989 /// image resides. 990 /// 991 /// The compilation image for a module contains executable code, data, debug 992 /// information, etc. This is roughly the same as the `Module::serialize` 993 /// but not the exact same. 994 /// 995 /// The range of memory reported here is exposed to allow low-level 996 /// manipulation of the memory in platform-specific manners such as using 997 /// `mlock` to force the contents to be paged in immediately or keep them 998 /// paged in after they're loaded. 999 /// 1000 /// It is not safe to modify the memory in this range, nor is it safe to 1001 /// modify the protections of memory in this range. 1002 /// 1003 /// Note that depending on the engine configuration, this image 1004 /// range may not actually be the code that is directly executed. 1005 pub fn image_range(&self) -> Range<*const u8> { 1006 self.engine_code().image().as_ptr_range() 1007 } 1008 1009 /// Force initialization of copy-on-write images to happen here-and-now 1010 /// instead of when they're requested during first instantiation. 1011 /// 1012 /// When [copy-on-write memory 1013 /// initialization](crate::Config::memory_init_cow) is enabled then Wasmtime 1014 /// will lazily create the initialization image for a module. This method 1015 /// can be used to explicitly dictate when this initialization happens. 1016 /// 1017 /// Note that this largely only matters on Linux when memfd is used. 1018 /// Otherwise the copy-on-write image typically comes from disk and in that 1019 /// situation the creation of the image is trivial as the image is always 1020 /// sourced from disk. On Linux, though, when memfd is used a memfd is 1021 /// created and the initialization image is written to it. 1022 /// 1023 /// Also note that this method is not required to be called, it's available 1024 /// as a performance optimization if required but is otherwise handled 1025 /// automatically. 1026 pub fn initialize_copy_on_write_image(&self) -> Result<()> { 1027 self.memory_images()?; 1028 Ok(()) 1029 } 1030 1031 /// Get the map from `.text` section offsets to Wasm binary offsets for this 1032 /// module. 1033 /// 1034 /// Each entry is a (`.text` section offset, Wasm binary offset) pair. 1035 /// 1036 /// Entries are yielded in order of `.text` section offset. 1037 /// 1038 /// Some entries are missing a Wasm binary offset. This is for code that is 1039 /// not associated with any single location in the Wasm binary, or for when 1040 /// source information was optimized away. 1041 /// 1042 /// Not every module has an address map, since address map generation can be 1043 /// turned off on `Config`. 1044 /// 1045 /// There is not an entry for every `.text` section offset. Every offset 1046 /// after an entry's offset, but before the next entry's offset, is 1047 /// considered to map to the same Wasm binary offset as the original 1048 /// entry. For example, the address map will not contain the following 1049 /// sequence of entries: 1050 /// 1051 /// ```ignore 1052 /// [ 1053 /// // ... 1054 /// (10, Some(42)), 1055 /// (11, Some(42)), 1056 /// (12, Some(42)), 1057 /// (13, Some(43)), 1058 /// // ... 1059 /// ] 1060 /// ``` 1061 /// 1062 /// Instead, it will drop the entries for offsets `11` and `12` since they 1063 /// are the same as the entry for offset `10`: 1064 /// 1065 /// ```ignore 1066 /// [ 1067 /// // ... 1068 /// (10, Some(42)), 1069 /// (13, Some(43)), 1070 /// // ... 1071 /// ] 1072 /// ``` 1073 pub fn address_map<'a>(&'a self) -> Option<impl Iterator<Item = (usize, Option<u32>)> + 'a> { 1074 Some( 1075 wasmtime_environ::iterate_address_map(self.engine_code().address_map_data())? 1076 .map(|(offset, file_pos)| (offset as usize, file_pos.file_offset())), 1077 ) 1078 } 1079 1080 /// Get this module's code object's `.text` section, containing its compiled 1081 /// executable code. 1082 pub fn text(&self) -> &[u8] { 1083 self.engine_code().text() 1084 } 1085 1086 /// Get information about functions in this module's `.text` section: their 1087 /// index, name, and offset+length. 1088 /// 1089 /// Results are yielded in a ModuleFunction struct. 1090 pub fn functions<'a>(&'a self) -> impl ExactSizeIterator<Item = ModuleFunction> + 'a { 1091 let module = self.compiled_module(); 1092 self.env_module().defined_func_indices().map(|idx| { 1093 let loc = module.func_loc(idx); 1094 let idx = module.module().func_index(idx); 1095 ModuleFunction { 1096 index: idx, 1097 name: module.func_name(idx).map(|n| n.to_string()), 1098 offset: loc.start as usize, 1099 len: loc.length as usize, 1100 } 1101 }) 1102 } 1103 1104 pub(crate) fn id(&self) -> CompiledModuleId { 1105 self.inner.module.unique_id() 1106 } 1107 1108 pub(crate) fn offsets(&self) -> &VMOffsets<HostPtr> { 1109 &self.inner.offsets 1110 } 1111 1112 /// Return the unique-within-Engine ID for this module. 1113 /// 1114 /// Allows distinguishing module identities when introspecting 1115 /// modules, e.g. via debug APIs. 1116 #[cfg(feature = "debug")] 1117 pub fn debug_index_in_engine(&self) -> u64 { 1118 self.id().as_u64() 1119 } 1120 1121 /// Return the address, in memory, of the trampoline that allows Wasm to 1122 /// call a array function of the given signature. 1123 /// 1124 /// Note that unlike all other code-pointer-returning functions, 1125 /// this *can* be present on `Module` (without a `StoreCode`) 1126 /// because we can execute the `EngineCode` for trampolines that 1127 /// leave the store to call the host. 1128 pub(crate) fn wasm_to_array_trampoline( 1129 &self, 1130 signature: VMSharedTypeIndex, 1131 ) -> Option<NonNull<VMWasmCallFunction>> { 1132 log::trace!("Looking up trampoline for {signature:?}"); 1133 let trampoline_shared_ty = self.inner.engine.signatures().trampoline_type(signature); 1134 let trampoline_module_ty = self 1135 .inner 1136 .code 1137 .signatures() 1138 .trampoline_type(trampoline_shared_ty)?; 1139 debug_assert!( 1140 self.inner 1141 .engine 1142 .signatures() 1143 .borrow( 1144 self.inner 1145 .code 1146 .signatures() 1147 .shared_type(trampoline_module_ty) 1148 .unwrap() 1149 ) 1150 .unwrap() 1151 .unwrap_func() 1152 .is_trampoline_type() 1153 ); 1154 1155 let ptr = self 1156 .compiled_module() 1157 .wasm_to_array_trampoline(trampoline_module_ty) 1158 .expect("always have a trampoline for the trampoline type") 1159 .as_ptr() 1160 .cast::<VMWasmCallFunction>() 1161 .cast_mut(); 1162 Some(NonNull::new(ptr).unwrap()) 1163 } 1164 1165 pub(crate) fn memory_images(&self) -> Result<Option<&ModuleMemoryImages>> { 1166 let images = self 1167 .inner 1168 .memory_images 1169 .get_or_try_init(|| memory_images(&self.inner))? 1170 .as_ref(); 1171 Ok(images) 1172 } 1173 1174 /// Obtain an exception-table parser on this module's exception metadata. 1175 #[cfg(feature = "gc")] 1176 pub(crate) fn exception_table<'a>(&'a self) -> ExceptionTable<'a> { 1177 ExceptionTable::parse(self.inner.code.exception_tables()) 1178 .expect("Exception tables were validated on module load") 1179 } 1180 1181 /// Obtain a frame-table parser on this module's frame state slot 1182 /// (debug instrumentation) metadata. 1183 #[cfg(feature = "debug")] 1184 pub(crate) fn frame_table<'a>(&'a self) -> Option<FrameTable<'a>> { 1185 let data = self.inner.code.frame_tables(); 1186 if data.is_empty() { 1187 None 1188 } else { 1189 let orig_text = self.inner.code.text(); 1190 Some( 1191 FrameTable::parse(data, orig_text) 1192 .expect("Frame tables were validated on module load"), 1193 ) 1194 } 1195 } 1196 1197 /// Is this `Module` the same as another? 1198 /// 1199 /// Ordinarily, module identity does not matter: a Wasmtime user 1200 /// will create or obtain a module from some source and 1201 /// instantiate it, and any two `Module` objects created from the 1202 /// same source module are interchangeable. However, introspecting 1203 /// module identity may be useful when examining Wasm VM state, 1204 /// e.g. via debug APIs. It is guaranteed that `Module::same` 1205 /// returns true for `Module` objects that reference the same 1206 /// underlying module (e.g., one created via a `clone` of the 1207 /// other). 1208 #[inline] 1209 pub fn same(a: &Module, b: &Module) -> bool { 1210 Arc::ptr_eq(&a.inner, &b.inner) 1211 } 1212 } 1213 1214 /// Describes a function for a given module. 1215 pub struct ModuleFunction { 1216 pub index: wasmtime_environ::FuncIndex, 1217 pub name: Option<String>, 1218 pub offset: usize, 1219 pub len: usize, 1220 } 1221 1222 impl Drop for ModuleInner { 1223 fn drop(&mut self) { 1224 // When a `Module` is being dropped that means that it's no longer 1225 // present in any `Store` and it's additionally not longer held by any 1226 // embedder. Take this opportunity to purge any lingering instantiations 1227 // within a pooling instance allocator, if applicable. 1228 self.engine 1229 .allocator() 1230 .purge_module(self.module.unique_id()); 1231 } 1232 } 1233 1234 /// Describes the location of an export in a module. 1235 #[derive(Copy, Clone)] 1236 pub struct ModuleExport { 1237 /// The module that this export is defined in. 1238 pub(crate) module: CompiledModuleId, 1239 /// A raw index into the wasm module. 1240 pub(crate) entity: EntityIndex, 1241 } 1242 1243 fn _assert_send_sync() { 1244 fn _assert<T: Send + Sync>() {} 1245 _assert::<Module>(); 1246 } 1247 1248 /// Helper method to construct a `ModuleMemoryImages` for an associated 1249 /// `CompiledModule`. 1250 fn memory_images(inner: &Arc<ModuleInner>) -> Result<Option<ModuleMemoryImages>> { 1251 // If initialization via copy-on-write is explicitly disabled in 1252 // configuration then this path is skipped entirely. 1253 if !inner.engine.tunables().memory_init_cow { 1254 return Ok(None); 1255 } 1256 1257 // ... otherwise logic is delegated to the `ModuleMemoryImages::new` 1258 // constructor. 1259 ModuleMemoryImages::new( 1260 &inner.engine, 1261 inner.module.module(), 1262 inner.code.module_memory_image_source(), 1263 ) 1264 } 1265 1266 impl crate::vm::ModuleMemoryImageSource for CodeMemory { 1267 fn wasm_data(&self) -> &[u8] { 1268 <Self>::wasm_data(self) 1269 } 1270 1271 fn mmap(&self) -> Option<&MmapVec> { 1272 Some(<Self>::mmap(self)) 1273 } 1274 } 1275 1276 #[cfg(test)] 1277 mod tests { 1278 use crate::{CodeBuilder, Engine, Module}; 1279 use wasmtime_environ::MemoryInitialization; 1280 1281 #[test] 1282 fn cow_on_by_default() { 1283 let engine = Engine::default(); 1284 let module = Module::new( 1285 &engine, 1286 r#" 1287 (module 1288 (memory 1) 1289 (data (i32.const 100) "abcd") 1290 ) 1291 "#, 1292 ) 1293 .unwrap(); 1294 1295 let init = &module.env_module().memory_initialization; 1296 assert!(matches!(init, MemoryInitialization::Static { .. })); 1297 } 1298 1299 #[test] 1300 #[cfg_attr(miri, ignore)] 1301 fn image_range_is_whole_image() { 1302 let wat = r#" 1303 (module 1304 (memory 1) 1305 (data (i32.const 0) "1234") 1306 (func (export "f") (param i32) (result i32) 1307 local.get 0)) 1308 "#; 1309 let engine = Engine::default(); 1310 let mut builder = CodeBuilder::new(&engine); 1311 builder.wasm_binary_or_text(wat.as_bytes(), None).unwrap(); 1312 let bytes = builder.compile_module_serialized().unwrap(); 1313 1314 let module = unsafe { Module::deserialize(&engine, &bytes).unwrap() }; 1315 let image_range = module.image_range(); 1316 let len = image_range.end.addr() - image_range.start.addr(); 1317 // Length may be strictly greater if it becomes page-aligned. 1318 assert!(len >= bytes.len()); 1319 } 1320 } 1321