wasmtime/runtime/module.rs
1use crate::prelude::*;
2#[cfg(feature = "std")]
3use crate::runtime::vm::open_file_for_mmap;
4use crate::runtime::vm::{CompiledModuleId, MmapVec, ModuleMemoryImages, VMWasmCallFunction};
5use crate::sync::OnceLock;
6use crate::{
7 Engine,
8 code::EngineCode,
9 code_memory::CodeMemory,
10 instantiate::CompiledModule,
11 resources::ResourcesRequired,
12 types::{ExportType, ExternType, ImportType},
13};
14use alloc::sync::Arc;
15use core::fmt;
16use core::ops::Range;
17use core::ptr::NonNull;
18#[cfg(feature = "std")]
19use std::{fs::File, path::Path};
20use wasmparser::{Parser, ValidPayload, Validator};
21#[cfg(feature = "debug")]
22use wasmtime_environ::FrameTable;
23use wasmtime_environ::{
24 CompiledFunctionsTable, CompiledModuleInfo, EntityIndex, HostPtr, ModuleTypes, ObjectKind,
25 StaticModuleIndex, TypeTrace, VMOffsets, VMSharedTypeIndex, WasmChecksum,
26};
27#[cfg(feature = "gc")]
28use wasmtime_unwinder::ExceptionTable;
29mod registry;
30
31pub 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)]
132pub struct Module {
133 inner: Arc<ModuleInner>,
134}
135
136struct 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
169impl 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
177impl 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
185impl 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 ///
408 /// # Errors
409 ///
410 /// This function will return an [`OutOfMemory`][crate::OutOfMemory] error when
411 /// memory allocation fails. See the `OutOfMemory` type's documentation for
412 /// details on Wasmtime's out-of-memory handling.
413 pub unsafe fn deserialize(engine: &Engine, bytes: impl AsRef<[u8]>) -> Result<Module> {
414 let code = engine.load_code_bytes(bytes.as_ref(), ObjectKind::Module)?;
415 Module::from_parts(engine, code, None)
416 }
417
418 /// In-place deserialization of an in-memory compiled module previously
419 /// created with [`Module::serialize`] or [`Engine::precompile_module`].
420 ///
421 /// See [`Self::deserialize`] for additional information; this method
422 /// works identically except that it will not create a copy of the provided
423 /// memory but will use it directly.
424 ///
425 /// # Unsafety
426 ///
427 /// All of the safety notes from [`Self::deserialize`] apply here as well
428 /// with the additional constraint that the code memory provide by `memory`
429 /// lives for as long as the module and is nevery externally modified for
430 /// the lifetime of the deserialized module.
431 pub unsafe fn deserialize_raw(engine: &Engine, memory: NonNull<[u8]>) -> Result<Module> {
432 // SAFETY: the contract required by `load_code_raw` is the same as this
433 // function.
434 let code = unsafe { engine.load_code_raw(memory, ObjectKind::Module)? };
435 Module::from_parts(engine, code, None)
436 }
437
438 /// Same as [`deserialize`], except that the contents of `path` are read to
439 /// deserialize into a [`Module`].
440 ///
441 /// This method is provided because it can be faster than [`deserialize`]
442 /// since the data doesn't need to be copied around, but rather the module
443 /// can be used directly from an mmap'd view of the file provided.
444 ///
445 /// [`deserialize`]: Module::deserialize
446 ///
447 /// # Unsafety
448 ///
449 /// All of the reasons that [`deserialize`] is `unsafe` applies to this
450 /// function as well. Arbitrary data loaded from a file may trick Wasmtime
451 /// into arbitrary code execution since the contents of the file are not
452 /// validated to be a valid precompiled module.
453 ///
454 /// Additionally though this function is also `unsafe` because the file
455 /// referenced must remain unchanged and a valid precompiled module for the
456 /// entire lifetime of the [`Module`] returned. Any changes to the file on
457 /// disk may change future instantiations of the module to be incorrect.
458 /// This is because the file is mapped into memory and lazily loaded pages
459 /// reflect the current state of the file, not necessarily the original
460 /// state of the file.
461 #[cfg(feature = "std")]
462 pub unsafe fn deserialize_file(engine: &Engine, path: impl AsRef<Path>) -> Result<Module> {
463 let file = open_file_for_mmap(path.as_ref())?;
464 // SAFETY: the contract of `deserialize_open_file` is the samea s this
465 // function.
466 unsafe {
467 Self::deserialize_open_file(engine, file)
468 .with_context(|| format!("failed deserialization for: {}", path.as_ref().display()))
469 }
470 }
471
472 /// Same as [`deserialize_file`], except that it takes an open `File`
473 /// instead of a path.
474 ///
475 /// This method is provided because it can be used instead of
476 /// [`deserialize_file`] in situations where `wasmtime` is running with
477 /// limited file system permissions. In that case a process
478 /// with file system access can pass already opened files to `wasmtime`.
479 ///
480 /// [`deserialize_file`]: Module::deserialize_file
481 ///
482 /// Note that the corresponding will be mapped as private writeable
483 /// (copy-on-write) and executable. For `windows` this means the file needs
484 /// to be opened with at least `FILE_GENERIC_READ | FILE_GENERIC_EXECUTE`
485 /// [`access_mode`].
486 ///
487 /// [`access_mode`]: https://doc.rust-lang.org/std/os/windows/fs/trait.OpenOptionsExt.html#tymethod.access_mode
488 ///
489 /// # Unsafety
490 ///
491 /// All of the reasons that [`deserialize_file`] is `unsafe` applies to this
492 /// function as well.
493 #[cfg(feature = "std")]
494 pub unsafe fn deserialize_open_file(engine: &Engine, file: File) -> Result<Module> {
495 let code = engine.load_code_file(file, ObjectKind::Module)?;
496 Module::from_parts(engine, code, None)
497 }
498
499 /// Entrypoint for creating a `Module` for all above functions, both
500 /// of the AOT and jit-compiled categories.
501 ///
502 /// In all cases the compilation artifact, `code_memory`, is provided here.
503 /// The `info_and_types` argument is `None` when a module is being
504 /// deserialized from a precompiled artifact or it's `Some` if it was just
505 /// compiled and the values are already available.
506 pub(crate) fn from_parts(
507 engine: &Engine,
508 code_memory: Arc<CodeMemory>,
509 info_and_types: Option<(CompiledModuleInfo, CompiledFunctionsTable, ModuleTypes)>,
510 ) -> Result<Self> {
511 // Acquire this module's metadata and type information, deserializing
512 // it from the provided artifact if it wasn't otherwise provided
513 // already.
514 let (mut info, index, mut types) = match info_and_types {
515 Some((info, index, types)) => (info, index, types),
516 None => postcard::from_bytes(code_memory.wasmtime_info())?,
517 };
518
519 // Register function type signatures into the engine for the lifetime
520 // of the `Module` that will be returned. This notably also builds up
521 // maps for trampolines to be used for this module when inserted into
522 // stores.
523 //
524 // Note that the unsafety here should be ok since the `trampolines`
525 // field should only point to valid trampoline function pointers
526 // within the text section.
527 let signatures = engine
528 .register_and_canonicalize_types(&mut types, core::iter::once(&mut info.module))?;
529
530 // Package up all our data into an `EngineCode` and delegate to the final
531 // step of module compilation.
532 let code = try_new::<Arc<_>>(EngineCode::new(code_memory, signatures, types.into())?)?;
533 let index = try_new::<Arc<_>>(index)?;
534 Module::from_parts_raw(engine, code, info, index, true)
535 }
536
537 pub(crate) fn from_parts_raw(
538 engine: &Engine,
539 code: Arc<EngineCode>,
540 info: CompiledModuleInfo,
541 index: Arc<CompiledFunctionsTable>,
542 serializable: bool,
543 ) -> Result<Self> {
544 let checksum = info.checksum;
545 let module = CompiledModule::from_artifacts(code.clone(), info, index, engine.profiler())?;
546
547 // Validate the module can be used with the current instance allocator.
548 let offsets = VMOffsets::new(HostPtr, module.module());
549 engine
550 .allocator()
551 .validate_module(module.module(), &offsets)?;
552
553 let _ = serializable;
554
555 Ok(Self {
556 inner: try_new::<Arc<_>>(ModuleInner {
557 engine: engine.clone(),
558 code,
559 memory_images: OnceLock::new(),
560 module,
561 #[cfg(any(feature = "cranelift", feature = "winch"))]
562 serializable,
563 offsets,
564 checksum,
565 })?,
566 })
567 }
568
569 /// Validates `binary` input data as a WebAssembly binary given the
570 /// configuration in `engine`.
571 ///
572 /// This function will perform a speedy validation of the `binary` input
573 /// WebAssembly module (which is in [binary form][binary], the text format
574 /// is not accepted by this function) and return either `Ok` or `Err`
575 /// depending on the results of validation. The `engine` argument indicates
576 /// configuration for WebAssembly features, for example, which are used to
577 /// indicate what should be valid and what shouldn't be.
578 ///
579 /// Validation automatically happens as part of [`Module::new`].
580 ///
581 /// # Errors
582 ///
583 /// If validation fails for any reason (type check error, usage of a feature
584 /// that wasn't enabled, etc) then an error with a description of the
585 /// validation issue will be returned.
586 ///
587 /// [binary]: https://webassembly.github.io/spec/core/binary/index.html
588 pub fn validate(engine: &Engine, binary: &[u8]) -> Result<()> {
589 let mut validator = Validator::new_with_features(engine.features());
590
591 let mut functions = Vec::new();
592 for payload in Parser::new(0).parse_all(binary) {
593 let payload = payload?;
594 if let ValidPayload::Func(a, b) = validator.payload(&payload)? {
595 functions.push((a, b));
596 }
597 if let wasmparser::Payload::Version { encoding, .. } = &payload {
598 if let wasmparser::Encoding::Component = encoding {
599 bail!("component passed to module validation");
600 }
601 }
602 }
603
604 engine.run_maybe_parallel(functions, |(validator, body)| {
605 // FIXME: it would be best here to use a rayon-specific parallel
606 // iterator that maintains state-per-thread to share the function
607 // validator allocations (`Default::default` here) across multiple
608 // functions.
609 validator.into_validator(Default::default()).validate(&body)
610 })?;
611 Ok(())
612 }
613
614 /// Serializes this module to a vector of bytes.
615 ///
616 /// This function is similar to the [`Engine::precompile_module`] method
617 /// where it produces an artifact of Wasmtime which is suitable to later
618 /// pass into [`Module::deserialize`]. If a module is never instantiated
619 /// then it's recommended to use [`Engine::precompile_module`] instead of
620 /// this method, but if a module is both instantiated and serialized then
621 /// this method can be useful to get the serialized version without
622 /// compiling twice.
623 #[cfg(any(feature = "cranelift", feature = "winch"))]
624 pub fn serialize(&self) -> Result<Vec<u8>> {
625 // The current representation of compiled modules within a compiled
626 // component means that it cannot be serialized. The mmap returned here
627 // is the mmap for the entire component and while it contains all
628 // necessary data to deserialize this particular module it's all
629 // embedded within component-specific information.
630 //
631 // It's not the hardest thing in the world to support this but it's
632 // expected that there's not much of a use case at this time. In theory
633 // all that needs to be done is to edit the `.wasmtime.info` section
634 // to contains this module's metadata instead of the metadata for the
635 // whole component. The metadata itself is fairly trivially
636 // recreateable here it's more that there's no easy one-off API for
637 // editing the sections of an ELF object to use here.
638 //
639 // Overall for now this simply always returns an error in this
640 // situation. If you're reading this and feel that the situation should
641 // be different please feel free to open an issue.
642 if !self.inner.serializable {
643 bail!("cannot serialize a module exported from a component");
644 }
645 Ok(self.engine_code().image().to_vec())
646 }
647
648 pub(crate) fn compiled_module(&self) -> &CompiledModule {
649 &self.inner.module
650 }
651
652 pub(crate) fn engine_code(&self) -> &Arc<EngineCode> {
653 &self.inner.code
654 }
655
656 pub(crate) fn env_module(&self) -> &Arc<wasmtime_environ::Module> {
657 self.compiled_module().module()
658 }
659
660 pub(crate) fn types(&self) -> &ModuleTypes {
661 self.inner.code.module_types()
662 }
663
664 pub(crate) fn signatures(&self) -> &crate::type_registry::TypeCollection {
665 self.inner.code.signatures()
666 }
667
668 /// Returns identifier/name that this [`Module`] has. This name
669 /// is used in traps/backtrace details.
670 ///
671 /// Note that most LLVM/clang/Rust-produced modules do not have a name
672 /// associated with them, but other wasm tooling can be used to inject or
673 /// add a name.
674 ///
675 /// # Examples
676 ///
677 /// ```
678 /// # use wasmtime::*;
679 /// # fn main() -> Result<()> {
680 /// # let engine = Engine::default();
681 /// let module = Module::new(&engine, "(module $foo)")?;
682 /// assert_eq!(module.name(), Some("foo"));
683 ///
684 /// let module = Module::new(&engine, "(module)")?;
685 /// assert_eq!(module.name(), None);
686 ///
687 /// # Ok(())
688 /// # }
689 /// ```
690 pub fn name(&self) -> Option<&str> {
691 let module = self.compiled_module().module();
692 let name = module.name?;
693 Some(&module.strings[name])
694 }
695
696 /// Returns the original Wasm bytecode for this module, if it is
697 /// available.
698 ///
699 /// Bytecode is only retained when the [`Engine`] was configured with
700 /// `guest-debug` support enabled (see [`Config::guest_debug`]). Returns
701 /// `None` when the module was compiled without that option.
702 ///
703 /// [`Config::guest_debug`]: crate::Config::guest_debug
704 pub fn debug_bytecode(&self) -> Option<&[u8]> {
705 self.compiled_module().bytecode()
706 }
707
708 /// Returns the list of imports that this [`Module`] has and must be
709 /// satisfied.
710 ///
711 /// This function returns the list of imports that the wasm module has, but
712 /// only the types of each import. The type of each import is used to
713 /// typecheck the [`Instance::new`](crate::Instance::new) method's `imports`
714 /// argument. The arguments to that function must match up 1-to-1 with the
715 /// entries in the array returned here.
716 ///
717 /// The imports returned reflect the order of the imports in the wasm module
718 /// itself, and note that no form of deduplication happens.
719 ///
720 /// # Examples
721 ///
722 /// Modules with no imports return an empty list here:
723 ///
724 /// ```
725 /// # use wasmtime::*;
726 /// # fn main() -> Result<()> {
727 /// # let engine = Engine::default();
728 /// let module = Module::new(&engine, "(module)")?;
729 /// assert_eq!(module.imports().len(), 0);
730 /// # Ok(())
731 /// # }
732 /// ```
733 ///
734 /// and modules with imports will have a non-empty list:
735 ///
736 /// ```
737 /// # use wasmtime::*;
738 /// # fn main() -> Result<()> {
739 /// # let engine = Engine::default();
740 /// let wat = r#"
741 /// (module
742 /// (import "host" "foo" (func))
743 /// )
744 /// "#;
745 /// let module = Module::new(&engine, wat)?;
746 /// assert_eq!(module.imports().len(), 1);
747 /// let import = module.imports().next().unwrap();
748 /// assert_eq!(import.module(), "host");
749 /// assert_eq!(import.name(), "foo");
750 /// match import.ty() {
751 /// ExternType::Func(_) => { /* ... */ }
752 /// _ => panic!("unexpected import type!"),
753 /// }
754 /// # Ok(())
755 /// # }
756 /// ```
757 pub fn imports<'module>(
758 &'module self,
759 ) -> impl ExactSizeIterator<Item = ImportType<'module>> + 'module {
760 let module = self.compiled_module().module();
761 let types = self.types();
762 let engine = self.engine();
763 module.imports().map(move |(imp_mod, imp_field, ty)| {
764 debug_assert!(ty.is_canonicalized_for_runtime_usage());
765 ImportType::new(imp_mod, imp_field, ty, types, engine)
766 })
767 }
768
769 /// Returns the list of exports that this [`Module`] has and will be
770 /// available after instantiation.
771 ///
772 /// This function will return the type of each item that will be returned
773 /// from [`Instance::exports`](crate::Instance::exports). Each entry in this
774 /// list corresponds 1-to-1 with that list, and the entries here will
775 /// indicate the name of the export along with the type of the export.
776 ///
777 /// # Examples
778 ///
779 /// Modules might not have any exports:
780 ///
781 /// ```
782 /// # use wasmtime::*;
783 /// # fn main() -> Result<()> {
784 /// # let engine = Engine::default();
785 /// let module = Module::new(&engine, "(module)")?;
786 /// assert!(module.exports().next().is_none());
787 /// # Ok(())
788 /// # }
789 /// ```
790 ///
791 /// When the exports are not empty, you can inspect each export:
792 ///
793 /// ```
794 /// # use wasmtime::*;
795 /// # fn main() -> Result<()> {
796 /// # let engine = Engine::default();
797 /// let wat = r#"
798 /// (module
799 /// (func (export "foo"))
800 /// (memory (export "memory") 1)
801 /// )
802 /// "#;
803 /// let module = Module::new(&engine, wat)?;
804 /// assert_eq!(module.exports().len(), 2);
805 ///
806 /// let mut exports = module.exports();
807 /// let foo = exports.next().unwrap();
808 /// assert_eq!(foo.name(), "foo");
809 /// match foo.ty() {
810 /// ExternType::Func(_) => { /* ... */ }
811 /// _ => panic!("unexpected export type!"),
812 /// }
813 ///
814 /// let memory = exports.next().unwrap();
815 /// assert_eq!(memory.name(), "memory");
816 /// match memory.ty() {
817 /// ExternType::Memory(_) => { /* ... */ }
818 /// _ => panic!("unexpected export type!"),
819 /// }
820 /// # Ok(())
821 /// # }
822 /// ```
823 pub fn exports<'module>(
824 &'module self,
825 ) -> impl ExactSizeIterator<Item = ExportType<'module>> + 'module {
826 let module = self.compiled_module().module();
827 let types = self.types();
828 let engine = self.engine();
829 module.exports.iter().map(move |(name, entity_index)| {
830 ExportType::new(
831 &module.strings[name],
832 module.type_of(*entity_index),
833 types,
834 engine,
835 )
836 })
837 }
838
839 /// Looks up an export in this [`Module`] by name.
840 ///
841 /// This function will return the type of an export with the given name.
842 ///
843 /// # Examples
844 ///
845 /// There may be no export with that name:
846 ///
847 /// ```
848 /// # use wasmtime::*;
849 /// # fn main() -> Result<()> {
850 /// # let engine = Engine::default();
851 /// let module = Module::new(&engine, "(module)")?;
852 /// assert!(module.get_export("foo").is_none());
853 /// # Ok(())
854 /// # }
855 /// ```
856 ///
857 /// When there is an export with that name, it is returned:
858 ///
859 /// ```
860 /// # use wasmtime::*;
861 /// # fn main() -> Result<()> {
862 /// # let engine = Engine::default();
863 /// let wat = r#"
864 /// (module
865 /// (func (export "foo"))
866 /// (memory (export "memory") 1)
867 /// )
868 /// "#;
869 /// let module = Module::new(&engine, wat)?;
870 /// let foo = module.get_export("foo");
871 /// assert!(foo.is_some());
872 ///
873 /// let foo = foo.unwrap();
874 /// match foo {
875 /// ExternType::Func(_) => { /* ... */ }
876 /// _ => panic!("unexpected export type!"),
877 /// }
878 ///
879 /// # Ok(())
880 /// # }
881 /// ```
882 pub fn get_export(&self, name: &str) -> Option<ExternType> {
883 let module = self.compiled_module().module();
884 let name = module.strings.get_atom(name)?;
885 let entity_index = module.exports.get(&name)?;
886 Some(ExternType::from_wasmtime(
887 self.engine(),
888 self.types(),
889 &module.type_of(*entity_index),
890 ))
891 }
892
893 /// Looks up an export in this [`Module`] by name to get its index.
894 ///
895 /// This function will return the index of an export with the given name. This can be useful
896 /// to avoid the cost of looking up the export by name multiple times. Instead the
897 /// [`ModuleExport`] can be stored and used to look up the export on the
898 /// [`Instance`](crate::Instance) later.
899 pub fn get_export_index(&self, name: &str) -> Option<ModuleExport> {
900 let compiled_module = self.compiled_module();
901 let module = compiled_module.module();
902 let name = module.strings.get_atom(name)?;
903 let entity = *module.exports.get(&name)?;
904 Some(ModuleExport {
905 module: self.id(),
906 entity,
907 })
908 }
909
910 /// Returns the [`Engine`] that this [`Module`] was compiled by.
911 pub fn engine(&self) -> &Engine {
912 &self.inner.engine
913 }
914
915 #[allow(
916 unused,
917 reason = "used only for verification with wasmtime `rr` feature \
918 and requires a lot of unnecessary gating across crates"
919 )]
920 pub(crate) fn checksum(&self) -> &WasmChecksum {
921 &self.inner.checksum
922 }
923
924 /// Returns a summary of the resources required to instantiate this
925 /// [`Module`].
926 ///
927 /// Potential uses of the returned information:
928 ///
929 /// * Determining whether your pooling allocator configuration supports
930 /// instantiating this module.
931 ///
932 /// * Deciding how many of which `Module` you want to instantiate within a
933 /// fixed amount of resources, e.g. determining whether to create 5
934 /// instances of module X or 10 instances of module Y.
935 ///
936 /// # Example
937 ///
938 /// ```
939 /// # fn main() -> wasmtime::Result<()> {
940 /// use wasmtime::{Config, Engine, Module};
941 ///
942 /// let mut config = Config::new();
943 /// config.wasm_multi_memory(true);
944 /// let engine = Engine::new(&config)?;
945 ///
946 /// let module = Module::new(&engine, r#"
947 /// (module
948 /// ;; Import a memory. Doesn't count towards required resources.
949 /// (import "a" "b" (memory 10))
950 /// ;; Define two local memories. These count towards the required
951 /// ;; resources.
952 /// (memory 1)
953 /// (memory 6)
954 /// )
955 /// "#)?;
956 ///
957 /// let resources = module.resources_required();
958 ///
959 /// // Instantiating the module will require allocating two memories, and
960 /// // the maximum initial memory size is six Wasm pages.
961 /// assert_eq!(resources.num_memories, 2);
962 /// assert_eq!(resources.max_initial_memory_size, Some(6));
963 ///
964 /// // The module doesn't need any tables.
965 /// assert_eq!(resources.num_tables, 0);
966 /// assert_eq!(resources.max_initial_table_size, None);
967 /// # Ok(()) }
968 /// ```
969 pub fn resources_required(&self) -> ResourcesRequired {
970 let em = self.env_module();
971 let num_memories = u32::try_from(em.num_defined_memories()).unwrap();
972 let max_initial_memory_size = em
973 .memories
974 .values()
975 .skip(em.num_imported_memories)
976 .map(|memory| memory.limits.min)
977 .max();
978 let num_tables = u32::try_from(em.num_defined_tables()).unwrap();
979 let max_initial_table_size = em
980 .tables
981 .values()
982 .skip(em.num_imported_tables)
983 .map(|table| table.limits.min)
984 .max();
985 ResourcesRequired {
986 num_memories,
987 max_initial_memory_size,
988 num_tables,
989 max_initial_table_size,
990 }
991 }
992
993 /// Returns the range of bytes in memory where this module's compilation
994 /// image resides.
995 ///
996 /// The compilation image for a module contains executable code, data, debug
997 /// information, etc. This is roughly the same as the `Module::serialize`
998 /// but not the exact same.
999 ///
1000 /// The range of memory reported here is exposed to allow low-level
1001 /// manipulation of the memory in platform-specific manners such as using
1002 /// `mlock` to force the contents to be paged in immediately or keep them
1003 /// paged in after they're loaded.
1004 ///
1005 /// It is not safe to modify the memory in this range, nor is it safe to
1006 /// modify the protections of memory in this range.
1007 ///
1008 /// Note that depending on the engine configuration, this image
1009 /// range may not actually be the code that is directly executed.
1010 pub fn image_range(&self) -> Range<*const u8> {
1011 self.engine_code().image().as_ptr_range()
1012 }
1013
1014 /// Force initialization of copy-on-write images to happen here-and-now
1015 /// instead of when they're requested during first instantiation.
1016 ///
1017 /// When [copy-on-write memory
1018 /// initialization](crate::Config::memory_init_cow) is enabled then Wasmtime
1019 /// will lazily create the initialization image for a module. This method
1020 /// can be used to explicitly dictate when this initialization happens.
1021 ///
1022 /// Note that this largely only matters on Linux when memfd is used.
1023 /// Otherwise the copy-on-write image typically comes from disk and in that
1024 /// situation the creation of the image is trivial as the image is always
1025 /// sourced from disk. On Linux, though, when memfd is used a memfd is
1026 /// created and the initialization image is written to it.
1027 ///
1028 /// Also note that this method is not required to be called, it's available
1029 /// as a performance optimization if required but is otherwise handled
1030 /// automatically.
1031 pub fn initialize_copy_on_write_image(&self) -> Result<()> {
1032 self.memory_images()?;
1033 Ok(())
1034 }
1035
1036 /// Get the map from `.text` section offsets to Wasm binary offsets for this
1037 /// module.
1038 ///
1039 /// Each entry is a (`.text` section offset, Wasm binary offset) pair.
1040 ///
1041 /// Entries are yielded in order of `.text` section offset.
1042 ///
1043 /// Some entries are missing a Wasm binary offset. This is for code that is
1044 /// not associated with any single location in the Wasm binary, or for when
1045 /// source information was optimized away.
1046 ///
1047 /// Not every module has an address map, since address map generation can be
1048 /// turned off on `Config`.
1049 ///
1050 /// There is not an entry for every `.text` section offset. Every offset
1051 /// after an entry's offset, but before the next entry's offset, is
1052 /// considered to map to the same Wasm binary offset as the original
1053 /// entry. For example, the address map will not contain the following
1054 /// sequence of entries:
1055 ///
1056 /// ```ignore
1057 /// [
1058 /// // ...
1059 /// (10, Some(42)),
1060 /// (11, Some(42)),
1061 /// (12, Some(42)),
1062 /// (13, Some(43)),
1063 /// // ...
1064 /// ]
1065 /// ```
1066 ///
1067 /// Instead, it will drop the entries for offsets `11` and `12` since they
1068 /// are the same as the entry for offset `10`:
1069 ///
1070 /// ```ignore
1071 /// [
1072 /// // ...
1073 /// (10, Some(42)),
1074 /// (13, Some(43)),
1075 /// // ...
1076 /// ]
1077 /// ```
1078 pub fn address_map<'a>(&'a self) -> Option<impl Iterator<Item = (usize, Option<u32>)> + 'a> {
1079 Some(
1080 wasmtime_environ::iterate_address_map(self.engine_code().address_map_data())?
1081 .map(|(offset, file_pos)| (offset as usize, file_pos.file_offset())),
1082 )
1083 }
1084
1085 /// Get this module's code object's `.text` section, containing its compiled
1086 /// executable code.
1087 pub fn text(&self) -> &[u8] {
1088 self.engine_code().text()
1089 }
1090
1091 /// Get information about functions in this module's `.text` section: their
1092 /// index, name, and offset+length.
1093 ///
1094 /// Results are yielded in a ModuleFunction struct.
1095 pub fn functions<'a>(&'a self) -> impl ExactSizeIterator<Item = ModuleFunction> + 'a {
1096 let module = self.compiled_module();
1097 let module_index = self.env_module().module_index;
1098 self.env_module().defined_func_indices().map(move |idx| {
1099 let loc = module.func_loc(idx);
1100 let idx = module.module().func_index(idx);
1101 ModuleFunction {
1102 module: module_index,
1103 index: idx,
1104 name: module.func_name(idx).map(|n| n.to_string()),
1105 offset: loc.start as usize,
1106 len: loc.length as usize,
1107 }
1108 })
1109 }
1110
1111 pub(crate) fn id(&self) -> CompiledModuleId {
1112 self.inner.module.unique_id()
1113 }
1114
1115 pub(crate) fn offsets(&self) -> &VMOffsets<HostPtr> {
1116 &self.inner.offsets
1117 }
1118
1119 /// Return the unique-within-Engine ID for this module.
1120 ///
1121 /// Allows distinguishing module identities when introspecting
1122 /// modules, e.g. via debug APIs.
1123 #[cfg(feature = "debug")]
1124 pub fn debug_index_in_engine(&self) -> u64 {
1125 self.id().as_u64()
1126 }
1127
1128 /// Return the address, in memory, of the trampoline that allows Wasm to
1129 /// call a array function of the given signature.
1130 ///
1131 /// Note that unlike all other code-pointer-returning functions,
1132 /// this *can* be present on `Module` (without a `StoreCode`)
1133 /// because we can execute the `EngineCode` for trampolines that
1134 /// leave the store to call the host.
1135 pub(crate) fn wasm_to_array_trampoline(
1136 &self,
1137 signature: VMSharedTypeIndex,
1138 ) -> Option<NonNull<VMWasmCallFunction>> {
1139 log::trace!("Looking up trampoline for {signature:?}");
1140 let trampoline_shared_ty = self.inner.engine.signatures().trampoline_type(signature);
1141 let trampoline_module_ty = self
1142 .inner
1143 .code
1144 .signatures()
1145 .trampoline_type(trampoline_shared_ty)?;
1146 debug_assert!(
1147 self.inner
1148 .engine
1149 .signatures()
1150 .borrow(
1151 self.inner
1152 .code
1153 .signatures()
1154 .shared_type(trampoline_module_ty)
1155 .unwrap()
1156 )
1157 .unwrap()
1158 .unwrap_func()
1159 .is_trampoline_type()
1160 );
1161
1162 let ptr = self
1163 .compiled_module()
1164 .wasm_to_array_trampoline(trampoline_module_ty)
1165 .expect("always have a trampoline for the trampoline type")
1166 .as_ptr()
1167 .cast::<VMWasmCallFunction>()
1168 .cast_mut();
1169 Some(NonNull::new(ptr).unwrap())
1170 }
1171
1172 pub(crate) fn memory_images(&self) -> Result<Option<&ModuleMemoryImages>> {
1173 let images = self
1174 .inner
1175 .memory_images
1176 .get_or_try_init(|| memory_images(&self.inner))?
1177 .as_ref();
1178 Ok(images)
1179 }
1180
1181 /// Obtain an exception-table parser on this module's exception metadata.
1182 #[cfg(feature = "gc")]
1183 pub(crate) fn exception_table<'a>(&'a self) -> ExceptionTable<'a> {
1184 ExceptionTable::parse(self.inner.code.exception_tables())
1185 .expect("Exception tables were validated on module load")
1186 }
1187
1188 /// Obtain a frame-table parser on this module's frame state slot
1189 /// (debug instrumentation) metadata.
1190 #[cfg(feature = "debug")]
1191 pub(crate) fn frame_table<'a>(&'a self) -> Option<FrameTable<'a>> {
1192 let data = self.inner.code.frame_tables();
1193 if data.is_empty() {
1194 None
1195 } else {
1196 let orig_text = self.inner.code.text();
1197 Some(
1198 FrameTable::parse(data, orig_text)
1199 .expect("Frame tables were validated on module load"),
1200 )
1201 }
1202 }
1203
1204 /// Is this `Module` the same as another?
1205 ///
1206 /// Ordinarily, module identity does not matter: a Wasmtime user
1207 /// will create or obtain a module from some source and
1208 /// instantiate it, and any two `Module` objects created from the
1209 /// same source module are interchangeable. However, introspecting
1210 /// module identity may be useful when examining Wasm VM state,
1211 /// e.g. via debug APIs. It is guaranteed that `Module::same`
1212 /// returns true for `Module` objects that reference the same
1213 /// underlying module (e.g., one created via a `clone` of the
1214 /// other).
1215 #[inline]
1216 pub fn same(a: &Module, b: &Module) -> bool {
1217 Arc::ptr_eq(&a.inner, &b.inner)
1218 }
1219}
1220
1221/// Describes a function for a given module.
1222pub struct ModuleFunction {
1223 /// The static module index this function belongs to.
1224 pub module: StaticModuleIndex,
1225 /// The function index within the module.
1226 pub index: wasmtime_environ::FuncIndex,
1227 /// The display name of the function, if available.
1228 pub name: Option<String>,
1229 /// The byte offset of this function in the text section.
1230 pub offset: usize,
1231 /// The byte length of this function in the text section.
1232 pub len: usize,
1233}
1234
1235impl Drop for ModuleInner {
1236 fn drop(&mut self) {
1237 // When a `Module` is being dropped that means that it's no longer
1238 // present in any `Store` and it's additionally not longer held by any
1239 // embedder. Take this opportunity to purge any lingering instantiations
1240 // within a pooling instance allocator, if applicable.
1241 self.engine
1242 .allocator()
1243 .purge_module(self.module.unique_id());
1244 }
1245}
1246
1247/// Describes the location of an export in a module.
1248#[derive(Copy, Clone)]
1249pub struct ModuleExport {
1250 /// The module that this export is defined in.
1251 pub(crate) module: CompiledModuleId,
1252 /// A raw index into the wasm module.
1253 pub(crate) entity: EntityIndex,
1254}
1255
1256fn _assert_send_sync() {
1257 fn _assert<T: Send + Sync>() {}
1258 _assert::<Module>();
1259}
1260
1261/// Helper method to construct a `ModuleMemoryImages` for an associated
1262/// `CompiledModule`.
1263fn memory_images(inner: &Arc<ModuleInner>) -> Result<Option<ModuleMemoryImages>> {
1264 // If initialization via copy-on-write is explicitly disabled in
1265 // configuration then this path is skipped entirely.
1266 if !inner.engine.tunables().memory_init_cow {
1267 return Ok(None);
1268 }
1269
1270 // ... otherwise logic is delegated to the `ModuleMemoryImages::new`
1271 // constructor.
1272 ModuleMemoryImages::new(
1273 &inner.engine,
1274 inner.module.module(),
1275 inner.code.module_memory_image_source(),
1276 )
1277}
1278
1279impl crate::vm::ModuleMemoryImageSource for CodeMemory {
1280 fn wasm_data(&self) -> &[u8] {
1281 <Self>::wasm_data(self)
1282 }
1283
1284 fn mmap(&self) -> Option<&MmapVec> {
1285 Some(<Self>::mmap(self))
1286 }
1287}
1288
1289#[cfg(test)]
1290mod tests {
1291 use crate::{CodeBuilder, Engine, Module};
1292 use wasmtime_environ::MemoryInitialization;
1293
1294 #[test]
1295 fn cow_on_by_default() {
1296 let engine = Engine::default();
1297 let module = Module::new(
1298 &engine,
1299 r#"
1300 (module
1301 (memory 1)
1302 (data (i32.const 100) "abcd")
1303 )
1304 "#,
1305 )
1306 .unwrap();
1307
1308 let init = &module.env_module().memory_initialization;
1309 assert!(matches!(init, MemoryInitialization::Static { .. }));
1310 }
1311
1312 #[test]
1313 #[cfg_attr(miri, ignore)]
1314 fn image_range_is_whole_image() {
1315 let wat = r#"
1316 (module
1317 (memory 1)
1318 (data (i32.const 0) "1234")
1319 (func (export "f") (param i32) (result i32)
1320 local.get 0))
1321 "#;
1322 let engine = Engine::default();
1323 let mut builder = CodeBuilder::new(&engine);
1324 builder.wasm_binary_or_text(wat.as_bytes(), None).unwrap();
1325 let bytes = builder.compile_module_serialized().unwrap();
1326
1327 let module = unsafe { Module::deserialize(&engine, &bytes).unwrap() };
1328 let image_range = module.image_range();
1329 let len = image_range.end.addr() - image_range.start.addr();
1330 // Length may be strictly greater if it becomes page-aligned.
1331 assert!(len >= bytes.len());
1332 }
1333}