Skip to main content

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