wasmtime/runtime/
module.rs

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