wasmtime/runtime/
module.rs

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