* Validate modules while translating This commit is a change to cranelift-wasm to validate each function body as it is translated. Additionally top-level module translation functions will perform module validation. This commit builds on changes in wasmparser to perform module validation interwtwined with parsing and translation. This will be necessary for future wasm features such as module linking where the type behind a function index, for example, can be far away in another module. Additionally this also brings a nice benefit where parsing the binary only happens once (instead of having an up-front serial validation step) and validation can happen in parallel for each function. Most of the changes in this commit are plumbing to make sure everything lines up right. The major functional change here is that module compilation should be faster by validating in parallel (or skipping function validation entirely in the case of a cache hit). Otherwise from a user-facing perspective nothing should be that different. This commit does mean that cranelift's translation now inherently validates the input wasm module. This means that the Spidermonkey integration of cranelift-wasm will also be validating the function as it's being translated with cranelift. The associated PR for wasmparser (bytecodealliance/wasmparser#62) provides the necessary tools to create a `FuncValidator` for Gecko, but this is something I'll want careful review for before landing! * Read function operators until EOF This way we can let the validator take care of any issues with mismatched `end` instructions and/or trailing operators/bytes.
147 lines
5.3 KiB
Rust
147 lines
5.3 KiB
Rust
//! Translation skeleton that traverses the whole WebAssembly module and call helper functions
|
|
//! to deal with each part of it.
|
|
use crate::environ::{ModuleEnvironment, WasmResult};
|
|
use crate::sections_translator::{
|
|
parse_data_section, parse_element_section, parse_export_section, parse_function_section,
|
|
parse_global_section, parse_import_section, parse_memory_section, parse_name_section,
|
|
parse_start_section, parse_table_section, parse_type_section,
|
|
};
|
|
use crate::state::ModuleTranslationState;
|
|
use cranelift_codegen::timing;
|
|
use wasmparser::{NameSectionReader, Parser, Payload, Validator};
|
|
|
|
/// Translate a sequence of bytes forming a valid Wasm binary into a list of valid Cranelift IR
|
|
/// [`Function`](cranelift_codegen::ir::Function).
|
|
pub fn translate_module<'data>(
|
|
data: &'data [u8],
|
|
environ: &mut dyn ModuleEnvironment<'data>,
|
|
) -> WasmResult<ModuleTranslationState> {
|
|
let _tt = timing::wasm_translate_module();
|
|
let mut module_translation_state = ModuleTranslationState::new();
|
|
let mut validator = Validator::new();
|
|
validator.wasm_features(environ.wasm_features());
|
|
|
|
for payload in Parser::new(0).parse_all(data) {
|
|
match payload? {
|
|
Payload::Version { num, range } => {
|
|
validator.version(num, &range)?;
|
|
}
|
|
Payload::End => {
|
|
validator.end()?;
|
|
}
|
|
|
|
Payload::TypeSection(types) => {
|
|
validator.type_section(&types)?;
|
|
parse_type_section(types, &mut module_translation_state, environ)?;
|
|
}
|
|
|
|
Payload::ImportSection(imports) => {
|
|
validator.import_section(&imports)?;
|
|
parse_import_section(imports, environ)?;
|
|
}
|
|
|
|
Payload::FunctionSection(functions) => {
|
|
validator.function_section(&functions)?;
|
|
parse_function_section(functions, environ)?;
|
|
}
|
|
|
|
Payload::TableSection(tables) => {
|
|
validator.table_section(&tables)?;
|
|
parse_table_section(tables, environ)?;
|
|
}
|
|
|
|
Payload::MemorySection(memories) => {
|
|
validator.memory_section(&memories)?;
|
|
parse_memory_section(memories, environ)?;
|
|
}
|
|
|
|
Payload::GlobalSection(globals) => {
|
|
validator.global_section(&globals)?;
|
|
parse_global_section(globals, environ)?;
|
|
}
|
|
|
|
Payload::ExportSection(exports) => {
|
|
validator.export_section(&exports)?;
|
|
parse_export_section(exports, environ)?;
|
|
}
|
|
|
|
Payload::StartSection { func, range } => {
|
|
validator.start_section(func, &range)?;
|
|
parse_start_section(func, environ)?;
|
|
}
|
|
|
|
Payload::ElementSection(elements) => {
|
|
validator.element_section(&elements)?;
|
|
parse_element_section(elements, environ)?;
|
|
}
|
|
|
|
Payload::CodeSectionStart { count, range, .. } => {
|
|
validator.code_section_start(count, &range)?;
|
|
environ.reserve_function_bodies(count, range.start as u64);
|
|
}
|
|
|
|
Payload::CodeSectionEntry(body) => {
|
|
let func_validator = validator.code_section_entry()?;
|
|
environ.define_function_body(func_validator, body)?;
|
|
}
|
|
|
|
Payload::DataSection(data) => {
|
|
validator.data_section(&data)?;
|
|
parse_data_section(data, environ)?;
|
|
}
|
|
|
|
Payload::DataCountSection { count, range } => {
|
|
validator.data_count_section(count, &range)?;
|
|
environ.reserve_passive_data(count)?;
|
|
}
|
|
|
|
Payload::ModuleSection(s) => {
|
|
validator.module_section(&s)?;
|
|
unimplemented!("module linking not implemented yet")
|
|
}
|
|
Payload::InstanceSection(s) => {
|
|
validator.instance_section(&s)?;
|
|
unimplemented!("module linking not implemented yet")
|
|
}
|
|
Payload::AliasSection(s) => {
|
|
validator.alias_section(&s)?;
|
|
unimplemented!("module linking not implemented yet")
|
|
}
|
|
Payload::ModuleCodeSectionStart {
|
|
count,
|
|
range,
|
|
size: _,
|
|
} => {
|
|
validator.module_code_section_start(count, &range)?;
|
|
unimplemented!("module linking not implemented yet")
|
|
}
|
|
|
|
Payload::ModuleCodeSectionEntry { .. } => {
|
|
unimplemented!("module linking not implemented yet")
|
|
}
|
|
|
|
Payload::CustomSection {
|
|
name: "name",
|
|
data,
|
|
data_offset,
|
|
} => {
|
|
let result = NameSectionReader::new(data, data_offset)
|
|
.map_err(|e| e.into())
|
|
.and_then(|s| parse_name_section(s, environ));
|
|
if let Err(e) = result {
|
|
log::warn!("failed to parse name section {:?}", e);
|
|
}
|
|
}
|
|
|
|
Payload::CustomSection { name, data, .. } => environ.custom_section(name, data)?,
|
|
|
|
Payload::UnknownSection { id, range, .. } => {
|
|
validator.unknown_section(id, &range)?;
|
|
unreachable!();
|
|
}
|
|
}
|
|
}
|
|
|
|
Ok(module_translation_state)
|
|
}
|