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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
use super::{
CustomSectionsBuilder,
ElementSegment,
FuncIdx,
ModuleBuilder,
ModuleHeader,
builder::ModuleHeaderBuilder,
export::ExternIdx,
global::Global,
import::{FuncTypeIdx, Import},
utils::FromWasmparser as _,
};
use crate::{
Engine,
Error,
FuncType,
MemoryType,
TableType,
engine::{EnforcedLimitsError, EngineFunc},
};
use alloc::boxed::Box;
use core::ops::Range;
use wasmparser::{
CustomSectionReader,
DataSectionReader,
ElementSectionReader,
Encoding,
ExportSectionReader,
FunctionBody,
FunctionSectionReader,
GlobalSectionReader,
ImportSectionReader,
MemorySectionReader,
Parser as WasmParser,
Payload,
TableSectionReader,
TypeSectionReader,
Validator,
};
#[cfg(doc)]
use crate::Module;
mod buffered;
/// Context used to construct a WebAssembly module from a stream of bytes.
pub struct ModuleParser {
/// The engine used for translation.
engine: Engine,
/// The Wasm validator used throughout stream parsing.
validator: Option<Validator>,
/// The underlying Wasm parser.
parser: WasmParser,
/// The number of compiled or processed functions.
engine_funcs: u32,
}
impl ModuleParser {
/// Creates a new [`ModuleParser`] for the given [`Engine`].
pub fn new(engine: &Engine) -> Self {
let mut parser = WasmParser::new(0);
parser.set_features(engine.config().wasm_features());
Self {
engine: engine.clone(),
validator: None,
parser,
engine_funcs: 0,
}
}
/// Processes the end of the Wasm binary.
fn process_end(&mut self, offset: usize) -> Result<(), Error> {
if let Some(validator) = &mut self.validator {
// This only checks if the number of code section entries and data segments match
// their expected numbers thus we must avoid this check in header-only mode because
// otherwise we will receive errors for unmatched data section entries.
validator.end(offset)?;
}
Ok(())
}
/// Validates the Wasm version section.
fn process_version(
&mut self,
num: u16,
encoding: Encoding,
range: Range<usize>,
) -> Result<(), Error> {
if let Some(validator) = &mut self.validator {
validator.version(num, encoding, &range)?;
}
Ok(())
}
/// Processes the Wasm type section.
///
/// # Note
///
/// This extracts all function types into the [`Module`] under construction.
///
/// # Errors
///
/// If an unsupported function type is encountered.
fn process_types(
&mut self,
section: TypeSectionReader,
header: &mut ModuleHeaderBuilder,
) -> Result<(), Error> {
if let Some(validator) = &mut self.validator {
validator.type_section(§ion)?;
}
let limits = self.engine.config().get_enforced_limits();
let func_types = section.into_iter().map(|result| {
let ty = result?.into_types().next().unwrap();
let func_ty = ty.unwrap_func();
if let Some(limit) = limits.max_params {
if func_ty.params().len() > limit {
return Err(Error::from(EnforcedLimitsError::TooManyParameters {
limit,
}));
}
}
if let Some(limit) = limits.max_results {
if func_ty.results().len() > limit {
return Err(Error::from(EnforcedLimitsError::TooManyResults { limit }));
}
}
Ok(FuncType::from_wasmparser(func_ty))
});
header.push_func_types(func_types)?;
Ok(())
}
/// Processes the Wasm import section.
///
/// # Note
///
/// This extracts all imports into the [`Module`] under construction.
///
/// # Errors
///
/// - If an import fails to validate.
/// - If an unsupported import declaration is encountered.
fn process_imports(
&mut self,
section: ImportSectionReader,
header: &mut ModuleHeaderBuilder,
) -> Result<(), Error> {
if let Some(validator) = &mut self.validator {
validator.import_section(§ion)?;
}
let imports = section
.into_iter()
.map(|import| import.map(Import::from).map_err(Error::from));
header.push_imports(imports)?;
Ok(())
}
/// Process module function declarations.
///
/// # Note
///
/// This extracts all function declarations into the [`Module`] under construction.
///
/// # Errors
///
/// If a function declaration fails to validate.
fn process_functions(
&mut self,
section: FunctionSectionReader,
header: &mut ModuleHeaderBuilder,
) -> Result<(), Error> {
if let Some(limit) = self.engine.config().get_enforced_limits().max_functions {
if section.count() > limit {
return Err(Error::from(EnforcedLimitsError::TooManyFunctions { limit }));
}
}
if let Some(validator) = &mut self.validator {
validator.function_section(§ion)?;
}
let funcs = section
.into_iter()
.map(|func| func.map(FuncTypeIdx::from).map_err(Error::from));
header.push_funcs(funcs)?;
Ok(())
}
/// Process module table declarations.
///
/// # Note
///
/// This extracts all table declarations into the [`Module`] under construction.
///
/// # Errors
///
/// If a table declaration fails to validate.
fn process_tables(
&mut self,
section: TableSectionReader,
header: &mut ModuleHeaderBuilder,
) -> Result<(), Error> {
if let Some(limit) = self.engine.config().get_enforced_limits().max_tables {
if section.count() > limit {
return Err(Error::from(EnforcedLimitsError::TooManyTables { limit }));
}
}
if let Some(validator) = &mut self.validator {
validator.table_section(§ion)?;
}
let tables = section.into_iter().map(|table| match table {
Ok(table) => {
assert!(matches!(table.init, wasmparser::TableInit::RefNull));
Ok(TableType::from_wasmparser(table.ty))
}
Err(err) => Err(err.into()),
});
header.push_tables(tables)?;
Ok(())
}
/// Process module linear memory declarations.
///
/// # Note
///
/// This extracts all linear memory declarations into the [`Module`] under construction.
///
/// # Errors
///
/// If a linear memory declaration fails to validate.
fn process_memories(
&mut self,
section: MemorySectionReader,
header: &mut ModuleHeaderBuilder,
) -> Result<(), Error> {
if let Some(limit) = self.engine.config().get_enforced_limits().max_memories {
if section.count() > limit {
return Err(Error::from(EnforcedLimitsError::TooManyMemories { limit }));
}
}
if let Some(validator) = &mut self.validator {
validator.memory_section(§ion)?;
}
let memories = section
.into_iter()
.map(|memory| memory.map(MemoryType::from_wasmparser).map_err(Error::from));
header.push_memories(memories)?;
Ok(())
}
/// Process module global variable declarations.
///
/// # Note
///
/// This extracts all global variable declarations into the [`Module`] under construction.
///
/// # Errors
///
/// If a global variable declaration fails to validate.
fn process_globals(
&mut self,
section: GlobalSectionReader,
header: &mut ModuleHeaderBuilder,
) -> Result<(), Error> {
if let Some(limit) = self.engine.config().get_enforced_limits().max_globals {
if section.count() > limit {
return Err(Error::from(EnforcedLimitsError::TooManyGlobals { limit }));
}
}
if let Some(validator) = &mut self.validator {
validator.global_section(§ion)?;
}
let globals = section
.into_iter()
.map(|global| global.map(Global::from).map_err(Error::from));
header.push_globals(globals)?;
Ok(())
}
/// Process module export declarations.
///
/// # Note
///
/// This extracts all export declarations into the [`Module`] under construction.
///
/// # Errors
///
/// If an export declaration fails to validate.
fn process_exports(
&mut self,
section: ExportSectionReader,
header: &mut ModuleHeaderBuilder,
) -> Result<(), Error> {
if let Some(validator) = &mut self.validator {
validator.export_section(§ion)?;
}
let exports = section.into_iter().map(|export| {
let export = export?;
let field: Box<str> = export.name.into();
let idx = ExternIdx::new(export.kind, export.index)?;
Ok((field, idx))
});
header.push_exports(exports)?;
Ok(())
}
/// Process module start section.
///
/// # Note
///
/// This sets the start function for the [`Module`] under construction.
///
/// # Errors
///
/// If the start function declaration fails to validate.
fn process_start(
&mut self,
func: u32,
range: Range<usize>,
header: &mut ModuleHeaderBuilder,
) -> Result<(), Error> {
if let Some(validator) = &mut self.validator {
validator.start_section(func, &range)?;
}
header.set_start(FuncIdx::from(func));
Ok(())
}
/// Process module table element segments.
///
/// # Note
///
/// This extracts all table element segments into the [`Module`] under construction.
///
/// # Errors
///
/// If any of the table element segments fail to validate.
fn process_element(
&mut self,
section: ElementSectionReader,
header: &mut ModuleHeaderBuilder,
) -> Result<(), Error> {
if let Some(limit) = self
.engine
.config()
.get_enforced_limits()
.max_element_segments
{
if section.count() > limit {
return Err(Error::from(EnforcedLimitsError::TooManyElementSegments {
limit,
}));
}
}
if let Some(validator) = &mut self.validator {
validator.element_section(§ion)?;
}
let segments = section
.into_iter()
.map(|segment| segment.map(ElementSegment::from).map_err(Error::from));
header.push_element_segments(segments)?;
Ok(())
}
/// Process module data count section.
///
/// # Note
///
/// This is part of the bulk memory operations Wasm proposal and not yet supported
/// by Wasmi.
fn process_data_count(&mut self, count: u32, range: Range<usize>) -> Result<(), Error> {
if let Some(limit) = self.engine.config().get_enforced_limits().max_data_segments {
if count > limit {
return Err(Error::from(EnforcedLimitsError::TooManyDataSegments {
limit,
}));
}
}
if let Some(validator) = &mut self.validator {
validator.data_count_section(count, &range)?;
}
Ok(())
}
/// Process module linear memory data segments.
///
/// # Note
///
/// This extracts all table elements into the [`Module`] under construction.
///
/// # Errors
///
/// If any of the table elements fail to validate.
fn process_data(
&mut self,
section: DataSectionReader,
builder: &mut ModuleBuilder,
) -> Result<(), Error> {
if let Some(limit) = self.engine.config().get_enforced_limits().max_data_segments {
if section.count() > limit {
return Err(Error::from(EnforcedLimitsError::TooManyDataSegments {
limit,
}));
}
}
if let Some(validator) = &mut self.validator {
// Note: data section does not belong to the Wasm module header.
//
// Also benchmarks show that validation of the data section can be very costly.
validator.data_section(§ion)?;
}
builder.reserve_data_segments(section.count() as usize);
for segment in section {
builder.push_data_segment(segment?)?;
}
Ok(())
}
/// Process module code section start.
///
/// # Note
///
/// This currently does not do a lot but it might become important in the
/// future if we add parallel translation of function bodies to prepare for
/// the translation.
///
/// # Errors
///
/// If the code start section fails to validate.
fn process_code_start(
&mut self,
count: u32,
range: Range<usize>,
size: u32,
) -> Result<(), Error> {
let enforced_limits = self.engine.config().get_enforced_limits();
if let Some(limit) = enforced_limits.max_functions {
if count > limit {
return Err(Error::from(EnforcedLimitsError::TooManyFunctions { limit }));
}
}
if let Some(limit) = enforced_limits.min_avg_bytes_per_function {
if size >= limit.req_funcs_bytes {
debug_assert!(
count != 0,
"size and count are inter-dependent: if size > 0 then count != 0",
// note: `limit.req_funcs_bytes` cannot be 0 by construction
);
let limit = limit.min_avg_bytes_per_function;
let avg = size / count;
if avg < limit {
return Err(Error::from(EnforcedLimitsError::MinAvgBytesPerFunction {
limit,
avg,
}));
}
}
}
if let Some(validator) = &mut self.validator {
validator.code_section_start(count, &range)?;
}
Ok(())
}
/// Returns the next `FuncIdx` for processing of its function body.
fn next_func(&mut self, header: &ModuleHeader) -> (FuncIdx, EngineFunc) {
let index = self.engine_funcs;
let engine_func = header.inner.engine_funcs.get_or_panic(index);
self.engine_funcs += 1;
// We have to adjust the initial func reference to the first
// internal function before we process any of the internal functions.
let len_func_imports = u32::try_from(header.inner.imports.len_funcs())
.unwrap_or_else(|_| panic!("too many imported functions"));
let func_idx = FuncIdx::from(index + len_func_imports);
(func_idx, engine_func)
}
/// Process a single module code section entry.
///
/// # Note
///
/// This contains the local variables and Wasm instructions of
/// a single function body.
/// This procedure is translating the Wasm bytecode into Wasmi bytecode.
///
/// # Errors
///
/// If the function body fails to validate.
fn process_code_entry(
&mut self,
func_body: FunctionBody,
bytes: &[u8],
header: &ModuleHeader,
) -> Result<(), Error> {
let (func, engine_func) = self.next_func(header);
let module = header.clone();
let offset = func_body.get_binary_reader().original_position();
let func_to_validate = match &mut self.validator {
Some(validator) => Some(validator.code_section_entry(&func_body)?),
None => None,
};
self.engine
.translate_func(func, engine_func, offset, bytes, module, func_to_validate)?;
Ok(())
}
/// Process a single Wasm custom section.
fn process_custom_section(
&mut self,
custom_sections: &mut CustomSectionsBuilder,
reader: CustomSectionReader,
) -> Result<(), Error> {
if self.engine.config().get_ignore_custom_sections() {
return Ok(());
}
custom_sections.push(reader.name(), reader.data());
Ok(())
}
/// Process an unexpected, unsupported or malformed Wasm module section payload.
fn process_invalid_payload(&mut self, payload: Payload<'_>) -> Result<(), Error> {
if let Some(validator) = &mut self.validator {
if let Err(error) = validator.payload(&payload) {
return Err(Error::from(error));
}
}
panic!("encountered unsupported, unexpected or malformed Wasm payload: {payload:?}")
}
}