dusk_wasmtime_environ/module_artifacts.rs
1//! Definitions of runtime structures and metadata which are serialized into ELF
2//! with `bincode` as part of a module's compilation process.
3
4use crate::{DefinedFuncIndex, FilePos, FuncIndex, Module, PrimaryMap, StackMap};
5use serde_derive::{Deserialize, Serialize};
6use std::fmt;
7use std::ops::Range;
8use std::str;
9use wasmtime_types::ModuleInternedTypeIndex;
10
11/// Secondary in-memory results of function compilation.
12#[derive(Serialize, Deserialize)]
13pub struct CompiledFunctionInfo {
14 /// The [`WasmFunctionInfo`] for this function.
15 pub wasm_func_info: WasmFunctionInfo,
16 /// The [`FunctionLoc`] indicating the location of this function in the text
17 /// section of the compition artifact.
18 pub wasm_func_loc: FunctionLoc,
19 /// A trampoline for array callers (e.g. `Func::new`) calling into this function (if needed).
20 pub array_to_wasm_trampoline: Option<FunctionLoc>,
21 /// A trampoline for native callers (e.g. `Func::wrap`) calling into this function (if needed).
22 pub native_to_wasm_trampoline: Option<FunctionLoc>,
23}
24
25/// Information about a function, such as trap information, address map,
26/// and stack maps.
27#[derive(Serialize, Deserialize, Default)]
28#[allow(missing_docs)]
29pub struct WasmFunctionInfo {
30 pub start_srcloc: FilePos,
31 pub stack_maps: Box<[StackMapInformation]>,
32}
33
34/// Description of where a function is located in the text section of a
35/// compiled image.
36#[derive(Copy, Clone, Serialize, Deserialize)]
37pub struct FunctionLoc {
38 /// The byte offset from the start of the text section where this
39 /// function starts.
40 pub start: u32,
41 /// The byte length of this function's function body.
42 pub length: u32,
43}
44
45/// The offset within a function of a GC safepoint, and its associated stack
46/// map.
47#[derive(Serialize, Deserialize, Debug)]
48pub struct StackMapInformation {
49 /// The offset of the GC safepoint within the function's native code. It is
50 /// relative to the beginning of the function.
51 pub code_offset: u32,
52
53 /// The stack map for identifying live GC refs at the GC safepoint.
54 pub stack_map: StackMap,
55}
56
57/// Secondary in-memory results of module compilation.
58///
59/// This opaque structure can be optionally passed back to
60/// `CompiledModule::from_artifacts` to avoid decoding extra information there.
61#[derive(Serialize, Deserialize)]
62pub struct CompiledModuleInfo {
63 /// Type information about the compiled WebAssembly module.
64 pub module: Module,
65
66 /// Metadata about each compiled function.
67 pub funcs: PrimaryMap<DefinedFuncIndex, CompiledFunctionInfo>,
68
69 /// Sorted list, by function index, of names we have for this module.
70 pub func_names: Vec<FunctionName>,
71
72 /// Metadata about wasm-to-native trampolines. Used when exposing a native
73 /// callee (e.g. `Func::wrap`) to a Wasm caller. Sorted by signature index.
74 pub wasm_to_native_trampolines: Vec<(ModuleInternedTypeIndex, FunctionLoc)>,
75
76 /// General compilation metadata.
77 pub meta: Metadata,
78}
79
80/// The name of a function stored in the
81/// [`ELF_NAME_DATA`](crate::obj::ELF_NAME_DATA) section.
82#[derive(Serialize, Deserialize)]
83pub struct FunctionName {
84 /// The Wasm function index of this function.
85 pub idx: FuncIndex,
86 /// The offset of the name in the
87 /// [`ELF_NAME_DATA`](crate::obj::ELF_NAME_DATA) section.
88 pub offset: u32,
89 /// The length of the name in bytes.
90 pub len: u32,
91}
92
93/// Metadata associated with a compiled ELF artifact.
94#[derive(Serialize, Deserialize)]
95pub struct Metadata {
96 /// Whether or not native debug information is available in `obj`
97 pub native_debug_info_present: bool,
98
99 /// Whether or not the original wasm module contained debug information that
100 /// we skipped and did not parse.
101 pub has_unparsed_debuginfo: bool,
102
103 /// Offset in the original wasm file to the code section.
104 pub code_section_offset: u64,
105
106 /// Whether or not custom wasm-specific dwarf sections were inserted into
107 /// the ELF image.
108 ///
109 /// Note that even if this flag is `true` sections may be missing if they
110 /// weren't found in the original wasm module itself.
111 pub has_wasm_debuginfo: bool,
112
113 /// Dwarf sections and the offsets at which they're stored in the
114 /// ELF_WASMTIME_DWARF
115 pub dwarf: Vec<(u8, Range<u64>)>,
116}
117
118/// Value of a configured setting for a [`Compiler`](crate::Compiler)
119#[derive(Serialize, Deserialize, Hash, Eq, PartialEq, Debug)]
120pub enum FlagValue<'a> {
121 /// Name of the value that has been configured for this setting.
122 Enum(&'a str),
123 /// The numerical value of the configured settings.
124 Num(u8),
125 /// Whether the setting is on or off.
126 Bool(bool),
127}
128
129impl fmt::Display for FlagValue<'_> {
130 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
131 match self {
132 Self::Enum(v) => v.fmt(f),
133 Self::Num(v) => v.fmt(f),
134 Self::Bool(v) => v.fmt(f),
135 }
136 }
137}
138
139/// Types of objects that can be created by `Compiler::object`
140pub enum ObjectKind {
141 /// A core wasm compilation artifact
142 Module,
143 /// A component compilation artifact
144 Component,
145}