Update wasmparser to 0.59.0 (#2013)

This commit is intended to update wasmparser to 0.59.0. This primarily
includes bytecodealliance/wasm-tools#40 which is a large update to how
parsing and validation works. The impact on Wasmtime is pretty small at
this time, but over time I'd like to refactor the internals here to lean
more heavily on that upstream wasmparser refactoring.

For now, though, the intention is to get on the train of wasmparser's
latest `main` branch to ensure we get bug fixes and such.

As part of this update a few other crates and such were updated. This is
primarily to handle the new encoding of `ref.is_null` where the type is
not part of the instruction encoding any more.
This commit is contained in:
Alex Crichton
2020-07-13 16:22:41 -05:00
committed by GitHub
parent 9bafb173a0
commit 1000f21338
23 changed files with 205 additions and 316 deletions

View File

@@ -2,13 +2,13 @@
//! to deal with each part of it.
use crate::environ::{ModuleEnvironment, WasmResult};
use crate::sections_translator::{
parse_code_section, 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,
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::{CustomSectionContent, ModuleReader, SectionContent};
use wasmparser::{NameSectionReader, Parser, Payload};
/// Translate a sequence of bytes forming a valid Wasm binary into a list of valid Cranelift IR
/// [`Function`](cranelift_codegen::ir::Function).
@@ -17,80 +17,85 @@ pub fn translate_module<'data>(
environ: &mut dyn ModuleEnvironment<'data>,
) -> WasmResult<ModuleTranslationState> {
let _tt = timing::wasm_translate_module();
let mut reader = ModuleReader::new(data)?;
let mut module_translation_state = ModuleTranslationState::new();
while !reader.eof() {
let section = reader.read()?;
match section.content()? {
SectionContent::Type(types) => {
for payload in Parser::new(0).parse_all(data) {
match payload? {
Payload::Version { .. } | Payload::End => {}
Payload::TypeSection(types) => {
parse_type_section(types, &mut module_translation_state, environ)?;
}
SectionContent::Import(imports) => {
Payload::ImportSection(imports) => {
parse_import_section(imports, environ)?;
}
SectionContent::Function(functions) => {
Payload::FunctionSection(functions) => {
parse_function_section(functions, environ)?;
}
SectionContent::Table(tables) => {
Payload::TableSection(tables) => {
parse_table_section(tables, environ)?;
}
SectionContent::Memory(memories) => {
Payload::MemorySection(memories) => {
parse_memory_section(memories, environ)?;
}
SectionContent::Global(globals) => {
Payload::GlobalSection(globals) => {
parse_global_section(globals, environ)?;
}
SectionContent::Export(exports) => {
Payload::ExportSection(exports) => {
parse_export_section(exports, environ)?;
}
SectionContent::Start(start) => {
parse_start_section(start, environ)?;
Payload::StartSection { func, .. } => {
parse_start_section(func, environ)?;
}
SectionContent::Element(elements) => {
Payload::ElementSection(elements) => {
parse_element_section(elements, environ)?;
}
SectionContent::Code(code) => {
parse_code_section(code, &module_translation_state, environ)?;
Payload::CodeSectionStart { .. } => {}
Payload::CodeSectionEntry(code) => {
let mut code = code.get_binary_reader();
let size = code.bytes_remaining();
let offset = code.original_position();
environ.define_function_body(
&module_translation_state,
code.read_bytes(size)?,
offset,
)?;
}
SectionContent::Data(data) => {
Payload::DataSection(data) => {
parse_data_section(data, environ)?;
}
SectionContent::DataCount(count) => {
Payload::DataCountSection { count, .. } => {
environ.reserve_passive_data(count)?;
}
SectionContent::Module(_)
| SectionContent::ModuleCode(_)
| SectionContent::Instance(_)
| SectionContent::Alias(_) => unimplemented!("module linking not implemented yet"),
Payload::ModuleSection(_)
| Payload::InstanceSection(_)
| Payload::AliasSection(_)
| Payload::ModuleCodeSectionStart { .. }
| Payload::ModuleCodeSectionEntry { .. } => {
unimplemented!("module linking not implemented yet")
}
SectionContent::Custom {
name,
binary,
content,
} => match content {
Some(CustomSectionContent::Name(names)) => {
parse_name_section(names, environ)?;
}
_ => {
let mut reader = binary.clone();
let len = reader.bytes_remaining();
let payload = reader.read_bytes(len)?;
environ.custom_section(name, payload)?;
}
},
Payload::CustomSection {
name: "name",
data,
data_offset,
} => parse_name_section(NameSectionReader::new(data, data_offset)?, environ)?,
Payload::CustomSection { name, data, .. } => environ.custom_section(name, data)?,
Payload::UnknownSection { .. } => unreachable!(),
}
}