summaryrefslogtreecommitdiffstats
path: root/third_party/rust/lucet-module-wasmsbx/src/module_data.rs
blob: e55c33a18631b3b89633c8d18ce63e686c443bf2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
use crate::{
    functions::{
        ExportFunction, FunctionIndex, FunctionMetadata, ImportFunction, OwnedFunctionMetadata,
    },
    globals::GlobalSpec,
    linear_memory::{HeapSpec, LinearMemorySpec, SparseData},
    types::Signature,
    Error,
};
#[cfg(feature = "signature_checking")]
use minisign::SignatureBones;
use serde::{Deserialize, Serialize};

pub const MODULE_DATA_SYM: &str = "lucet_module_data";

/// The metadata (and some data) for a Lucet module.
///
/// The lifetime parameter exists to support zero-copy deserialization for the `&str` and `&[u8]`
/// fields at the leaves of the structure. For a variant with owned types at the leaves, see
/// [`OwnedModuleData`](owned/struct.OwnedModuleData.html).
///
/// The goal is for this structure to eventually include everything except the code for the guest
/// functions themselves.
#[derive(Debug, Serialize, Deserialize)]
pub struct ModuleData<'a> {
    #[serde(borrow)]
    linear_memory: Option<LinearMemorySpec<'a>>,
    #[serde(borrow)]
    globals_spec: Vec<GlobalSpec<'a>>,
    #[serde(borrow)]
    function_info: Vec<FunctionMetadata<'a>>,
    #[serde(borrow)]
    import_functions: Vec<ImportFunction<'a>>,
    #[serde(borrow)]
    export_functions: Vec<ExportFunction<'a>>,
    signatures: Vec<Signature>,
    module_signature: Vec<u8>,
    features: ModuleFeatures,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct ModuleFeatures {
    pub sse3: bool,
    pub ssse3: bool,
    pub sse41: bool,
    pub sse42: bool,
    pub avx: bool,
    pub bmi1: bool,
    pub bmi2: bool,
    pub lzcnt: bool,
    pub popcnt: bool,
    _hidden: (),
}

impl ModuleFeatures {
    pub fn none() -> Self {
        Self {
            sse3: false,
            ssse3: false,
            sse41: false,
            sse42: false,
            avx: false,
            bmi1: false,
            bmi2: false,
            lzcnt: false,
            popcnt: false,
            _hidden: (),
        }
    }
}

impl<'a> ModuleData<'a> {
    #[cfg(feature = "signature_checking")]
    pub fn new(
        linear_memory: Option<LinearMemorySpec<'a>>,
        globals_spec: Vec<GlobalSpec<'a>>,
        function_info: Vec<FunctionMetadata<'a>>,
        import_functions: Vec<ImportFunction<'a>>,
        export_functions: Vec<ExportFunction<'a>>,
        signatures: Vec<Signature>,
        features: ModuleFeatures,
    ) -> Self {
        let module_signature = vec![0u8; SignatureBones::BYTES];
        Self {
            linear_memory,
            globals_spec,
            function_info,
            import_functions,
            export_functions,
            signatures,
            module_signature,
            features,
        }
    }

    #[cfg(not(feature = "signature_checking"))]
    pub fn new(
        linear_memory: Option<LinearMemorySpec<'a>>,
        globals_spec: Vec<GlobalSpec<'a>>,
        function_info: Vec<FunctionMetadata<'a>>,
        import_functions: Vec<ImportFunction<'a>>,
        export_functions: Vec<ExportFunction<'a>>,
        signatures: Vec<Signature>,
        features: ModuleFeatures,
    ) -> Self {
        let module_signature = vec![0u8; 0];
        Self {
            linear_memory,
            globals_spec,
            function_info,
            import_functions,
            export_functions,
            signatures,
            module_signature,
            features,
        }
    }

    pub fn heap_spec(&self) -> Option<&HeapSpec> {
        if let Some(ref linear_memory) = self.linear_memory {
            Some(&linear_memory.heap)
        } else {
            None
        }
    }

    pub fn sparse_data(&self) -> Option<&SparseData<'a>> {
        if let Some(ref linear_memory) = self.linear_memory {
            Some(&linear_memory.initializer)
        } else {
            None
        }
    }

    pub fn globals_spec(&self) -> &[GlobalSpec<'a>] {
        &self.globals_spec
    }

    pub fn function_info(&self) -> &[FunctionMetadata<'a>] {
        &self.function_info
    }

    pub fn import_functions(&self) -> &[ImportFunction<'_>] {
        &self.import_functions
    }

    pub fn export_functions(&self) -> &[ExportFunction<'_>] {
        &self.export_functions
    }

    // Function index here is a different index space than `get_func_from_idx`, which
    // uses function index as an index into a table of function elements.
    //
    // This is an index of all functions in the module.
    pub fn get_signature(&self, fn_id: FunctionIndex) -> &Signature {
        let sig_idx = self.function_info[fn_id.as_u32() as usize].signature;
        &self.signatures[sig_idx.as_u32() as usize]
    }

    pub fn get_export_func_id(&self, name: &str) -> Option<FunctionIndex> {
        self.export_functions
            .iter()
            .find(|export| export.names.contains(&name))
            .map(|export| export.fn_idx)
    }

    pub fn signatures(&self) -> &[Signature] {
        &self.signatures
    }

    pub fn get_module_signature(&self) -> &[u8] {
        &self.module_signature
    }

    pub fn features(&self) -> &ModuleFeatures {
        &self.features
    }

    #[cfg(feature = "signature_checking")]
    pub fn patch_module_signature(
        module_data_bin: &'a [u8],
        module_signature: &[u8],
    ) -> Result<Vec<u8>, Error> {
        assert_eq!(module_signature.len(), SignatureBones::BYTES);
        let mut module_data = Self::deserialize(module_data_bin)?;
        module_data
            .module_signature
            .copy_from_slice(module_signature);
        let patched_module_data_bin = module_data.serialize()?;
        assert_eq!(patched_module_data_bin.len(), module_data_bin.len());
        Ok(patched_module_data_bin)
    }

    #[cfg(feature = "signature_checking")]
    pub fn clear_module_signature(module_data_bin: &'a [u8]) -> Result<Vec<u8>, Error> {
        let module_signature = vec![0u8; SignatureBones::BYTES];
        Self::patch_module_signature(module_data_bin, &module_signature)
    }

    /// Serialize to [`bincode`](https://github.com/TyOverby/bincode).
    pub fn serialize(&self) -> Result<Vec<u8>, Error> {
        bincode::serialize(self).map_err(Error::SerializationError)
    }

    /// Deserialize from [`bincode`](https://github.com/TyOverby/bincode).
    pub fn deserialize(buf: &'a [u8]) -> Result<ModuleData<'a>, Error> {
        bincode::deserialize(buf).map_err(Error::DeserializationError)
    }
}

use crate::{
    functions::{OwnedExportFunction, OwnedImportFunction},
    globals::OwnedGlobalSpec,
    linear_memory::{OwnedLinearMemorySpec, OwnedSparseData},
};

/// The metadata (and some data) for a Lucet module.
///
/// This is a version of [`ModuleData`](../struct.ModuleData.html) with owned types throughout,
/// rather than references to support zero-copy deserialization. This type is useful when directly
/// building up a value to be serialized.
pub struct OwnedModuleData {
    linear_memory: Option<OwnedLinearMemorySpec>,
    globals_spec: Vec<OwnedGlobalSpec>,
    function_info: Vec<OwnedFunctionMetadata>,
    imports: Vec<OwnedImportFunction>,
    exports: Vec<OwnedExportFunction>,
    signatures: Vec<Signature>,
    features: ModuleFeatures,
}

impl OwnedModuleData {
    pub fn new(
        linear_memory: Option<OwnedLinearMemorySpec>,
        globals_spec: Vec<OwnedGlobalSpec>,
        function_info: Vec<OwnedFunctionMetadata>,
        imports: Vec<OwnedImportFunction>,
        exports: Vec<OwnedExportFunction>,
        signatures: Vec<Signature>,
        features: ModuleFeatures,
    ) -> Self {
        Self {
            linear_memory,
            globals_spec,
            function_info,
            imports,
            exports,
            signatures,
            features,
        }
    }

    /// Create a [`ModuleData`](../struct.ModuleData.html) backed by the values in this
    /// `OwnedModuleData`.
    pub fn to_ref<'a>(&'a self) -> ModuleData<'a> {
        ModuleData::new(
            if let Some(ref owned_linear_memory) = self.linear_memory {
                Some(owned_linear_memory.to_ref())
            } else {
                None
            },
            self.globals_spec.iter().map(|gs| gs.to_ref()).collect(),
            self.function_info
                .iter()
                .map(|info| info.to_ref())
                .collect(),
            self.imports.iter().map(|imp| imp.to_ref()).collect(),
            self.exports.iter().map(|exp| exp.to_ref()).collect(),
            self.signatures.clone(),
            self.features.clone(),
        )
    }

    pub fn empty() -> Self {
        Self::new(
            None,
            vec![],
            vec![],
            vec![],
            vec![],
            vec![],
            ModuleFeatures::none(),
        )
    }

    pub fn with_heap_spec(mut self, heap_spec: HeapSpec) -> Self {
        if let Some(ref mut linear_memory) = self.linear_memory {
            linear_memory.heap = heap_spec;
        } else {
            self.linear_memory = Some(OwnedLinearMemorySpec {
                heap: heap_spec,
                initializer: OwnedSparseData::new(vec![]).unwrap(),
            });
        }
        self
    }
}

impl Default for OwnedModuleData {
    fn default() -> Self {
        OwnedModuleData::empty()
    }
}