dusk_wasmtime/runtime/
module.rs

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