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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
use crate::{
backend::RunnableModule,
cache::{Artifact, Error as CacheError},
error,
import::ImportObject,
structures::{Map, TypedIndex},
types::{
ExportDescriptor, FuncIndex, FuncSig, GlobalDescriptor, GlobalIndex, GlobalInit,
ImportDescriptor, ImportedFuncIndex, ImportedGlobalIndex, ImportedMemoryIndex,
ImportedTableIndex, Initializer, LocalGlobalIndex, LocalMemoryIndex, LocalTableIndex,
MemoryDescriptor, MemoryIndex, SigIndex, TableDescriptor, TableIndex,
},
Instance,
};
use crate::backend::CacheGen;
#[cfg(feature = "generate-debug-information")]
use crate::jit_debug;
use indexmap::IndexMap;
use std::collections::HashMap;
use std::sync::Arc;
#[doc(hidden)]
pub struct ModuleInner {
pub runnable_module: Arc<Box<dyn RunnableModule>>,
pub cache_gen: Box<dyn CacheGen>,
pub info: ModuleInfo,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ModuleInfo {
pub memories: Map<LocalMemoryIndex, MemoryDescriptor>,
pub globals: Map<LocalGlobalIndex, GlobalInit>,
pub tables: Map<LocalTableIndex, TableDescriptor>,
pub imported_functions: Map<ImportedFuncIndex, ImportName>,
pub imported_memories: Map<ImportedMemoryIndex, (ImportName, MemoryDescriptor)>,
pub imported_tables: Map<ImportedTableIndex, (ImportName, TableDescriptor)>,
pub imported_globals: Map<ImportedGlobalIndex, (ImportName, GlobalDescriptor)>,
pub exports: IndexMap<String, ExportIndex>,
pub data_initializers: Vec<DataInitializer>,
pub elem_initializers: Vec<TableInitializer>,
pub start_func: Option<FuncIndex>,
pub func_assoc: Map<FuncIndex, SigIndex>,
pub signatures: Map<SigIndex, FuncSig>,
pub backend: String,
pub namespace_table: StringTable<NamespaceIndex>,
pub name_table: StringTable<NameIndex>,
pub em_symbol_map: Option<HashMap<u32, String>>,
pub custom_sections: HashMap<String, Vec<Vec<u8>>>,
pub generate_debug_info: bool,
#[cfg(feature = "generate-debug-information")]
#[serde(skip)]
pub(crate) debug_info_manager: jit_debug::JitCodeDebugInfoManager,
}
impl ModuleInfo {
pub fn import_custom_sections(&mut self, wasm: &[u8]) -> crate::error::ParseResult<()> {
let mut parser = wasmparser::ModuleReader::new(wasm)?;
while !parser.eof() {
let section = parser.read()?;
if let wasmparser::SectionCode::Custom { name, kind: _ } = section.code {
let mut reader = section.get_binary_reader();
let len = reader.bytes_remaining();
let bytes = reader.read_bytes(len)?;
let data = bytes.to_vec();
let name = name.to_string();
let entry: &mut Vec<Vec<u8>> = self.custom_sections.entry(name).or_default();
entry.push(data);
}
}
Ok(())
}
}
pub struct Module {
inner: Arc<ModuleInner>,
}
impl Module {
pub(crate) fn new(inner: Arc<ModuleInner>) -> Self {
Module { inner }
}
pub fn instantiate(&self, import_object: &ImportObject) -> error::Result<Instance> {
Instance::new(Arc::clone(&self.inner), import_object)
}
pub fn cache(&self) -> Result<Artifact, CacheError> {
let (backend_metadata, code) = self.inner.cache_gen.generate_cache()?;
Ok(Artifact::from_parts(
Box::new(self.inner.info.clone()),
backend_metadata,
code,
))
}
pub fn info(&self) -> &ModuleInfo {
&self.inner.info
}
pub fn exports(&self) -> Vec<ExportDescriptor> {
self.inner.exports()
}
pub fn imports(&self) -> Vec<ImportDescriptor> {
let mut out = Vec::with_capacity(
self.inner.info.imported_functions.len()
+ self.inner.info.imported_memories.len()
+ self.inner.info.imported_tables.len()
+ self.inner.info.imported_globals.len(),
);
fn get_import_name(
info: &ModuleInfo,
&ImportName {
namespace_index,
name_index,
}: &ImportName,
) -> (String, String) {
let namespace = info.namespace_table.get(namespace_index).to_string();
let name = info.name_table.get(name_index).to_string();
(namespace, name)
}
let info = &self.inner.info;
let imported_functions = info.imported_functions.iter().map(|(idx, import_name)| {
let (namespace, name) = get_import_name(info, import_name);
let sig = info
.signatures
.get(*info.func_assoc.get(FuncIndex::new(idx.index())).unwrap())
.unwrap();
ImportDescriptor {
namespace,
name,
ty: sig.into(),
}
});
let imported_memories =
info.imported_memories
.values()
.map(|(import_name, memory_descriptor)| {
let (namespace, name) = get_import_name(info, import_name);
ImportDescriptor {
namespace,
name,
ty: memory_descriptor.into(),
}
});
let imported_tables =
info.imported_tables
.values()
.map(|(import_name, table_descriptor)| {
let (namespace, name) = get_import_name(info, import_name);
ImportDescriptor {
namespace,
name,
ty: table_descriptor.into(),
}
});
let imported_globals =
info.imported_globals
.values()
.map(|(import_name, global_descriptor)| {
let (namespace, name) = get_import_name(info, import_name);
ImportDescriptor {
namespace,
name,
ty: global_descriptor.into(),
}
});
out.extend(imported_functions);
out.extend(imported_memories);
out.extend(imported_tables);
out.extend(imported_globals);
out
}
pub fn custom_sections(&self, key: impl AsRef<str>) -> Option<&[Vec<u8>]> {
let key = key.as_ref();
self.inner.info.custom_sections.get(key).map(|v| v.as_ref())
}
}
impl Clone for Module {
fn clone(&self) -> Self {
Self {
inner: Arc::clone(&self.inner),
}
}
}
impl ModuleInner {
pub(crate) fn exports_iter(&self) -> impl Iterator<Item = ExportDescriptor> + '_ {
self.info
.exports
.iter()
.map(move |(name, &ei)| ExportDescriptor {
name,
ty: match ei {
ExportIndex::Func(f_idx) => {
let sig_idx = self.info.func_assoc[f_idx].into();
self.info.signatures[sig_idx].clone().into()
}
ExportIndex::Global(g_idx) => {
let info = &self.info;
let local_global_idx =
LocalGlobalIndex::new(g_idx.index() - info.imported_globals.len());
info.globals[local_global_idx].desc.into()
}
ExportIndex::Memory(m_idx) => {
let info = &self.info;
let local_memory_idx =
LocalMemoryIndex::new(m_idx.index() - info.imported_memories.len());
info.memories[local_memory_idx].into()
}
ExportIndex::Table(t_idx) => {
let info = &self.info;
let local_table_idx =
LocalTableIndex::new(t_idx.index() - info.imported_tables.len());
info.tables[local_table_idx].into()
}
},
})
}
pub fn exports(&self) -> Vec<ExportDescriptor> {
self.exports_iter().collect()
}
}
#[doc(hidden)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ImportName {
pub namespace_index: NamespaceIndex,
pub name_index: NameIndex,
}
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
pub enum ExportIndex {
Func(FuncIndex),
Memory(MemoryIndex),
Global(GlobalIndex),
Table(TableIndex),
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct DataInitializer {
pub memory_index: MemoryIndex,
pub base: Initializer,
#[cfg_attr(feature = "cache", serde(with = "serde_bytes"))]
pub data: Vec<u8>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct TableInitializer {
pub table_index: TableIndex,
pub base: Initializer,
pub elements: Vec<FuncIndex>,
}
pub struct StringTableBuilder<K: TypedIndex> {
map: IndexMap<String, (K, u32, u32)>,
buffer: String,
count: u32,
}
impl<K: TypedIndex> StringTableBuilder<K> {
pub fn new() -> Self {
Self {
map: IndexMap::new(),
buffer: String::new(),
count: 0,
}
}
pub fn register<S>(&mut self, s: S) -> K
where
S: Into<String> + AsRef<str>,
{
let s_str = s.as_ref();
if self.map.contains_key(s_str) {
self.map[s_str].0
} else {
let offset = self.buffer.len();
let length = s_str.len();
let index = TypedIndex::new(self.count as _);
self.buffer.push_str(s_str);
self.map
.insert(s.into(), (index, offset as u32, length as u32));
self.count += 1;
index
}
}
pub fn finish(self) -> StringTable<K> {
let table = self
.map
.values()
.map(|(_, offset, length)| (*offset, *length))
.collect();
StringTable {
table,
buffer: self.buffer,
}
}
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct StringTable<K: TypedIndex> {
table: Map<K, (u32, u32)>,
buffer: String,
}
impl<K: TypedIndex> StringTable<K> {
pub fn new() -> Self {
Self {
table: Map::new(),
buffer: String::new(),
}
}
pub fn get(&self, index: K) -> &str {
let (offset, length) = self.table[index];
let offset = offset as usize;
let length = length as usize;
&self.buffer[offset..offset + length]
}
}
#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct NamespaceIndex(u32);
impl TypedIndex for NamespaceIndex {
#[doc(hidden)]
fn new(index: usize) -> Self {
NamespaceIndex(index as _)
}
#[doc(hidden)]
fn index(&self) -> usize {
self.0 as usize
}
}
#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct NameIndex(u32);
impl TypedIndex for NameIndex {
#[doc(hidden)]
fn new(index: usize) -> Self {
NameIndex(index as _)
}
#[doc(hidden)]
fn index(&self) -> usize {
self.0 as usize
}
}