Skip to content

Commit 1000f21

Browse files
authored
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.
1 parent 9bafb17 commit 1000f21

File tree

23 files changed

+205
-316
lines changed

23 files changed

+205
-316
lines changed

Cargo.lock

Lines changed: 18 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cranelift/wasm/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ keywords = ["webassembly", "wasm"]
1212
edition = "2018"
1313

1414
[dependencies]
15-
wasmparser = { version = "0.58.0", default-features = false }
15+
wasmparser = { version = "0.59.0", default-features = false }
1616
cranelift-codegen = { path = "../codegen", version = "0.65.0", default-features = false }
1717
cranelift-entity = { path = "../entity", version = "0.65.0" }
1818
cranelift-frontend = { path = "../frontend", version = "0.65.0", default-features = false }

cranelift/wasm/src/code_translator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1043,7 +1043,7 @@ pub fn translate_operator<FE: FuncEnvironment + ?Sized>(
10431043
Operator::RefNull { ty } => {
10441044
state.push1(environ.translate_ref_null(builder.cursor(), (*ty).try_into()?)?)
10451045
}
1046-
Operator::RefIsNull { ty: _ } => {
1046+
Operator::RefIsNull => {
10471047
let value = state.pop1();
10481048
state.push1(environ.translate_ref_is_null(builder.cursor(), value)?);
10491049
}

cranelift/wasm/src/module_translator.rs

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
//! to deal with each part of it.
33
use crate::environ::{ModuleEnvironment, WasmResult};
44
use crate::sections_translator::{
5-
parse_code_section, parse_data_section, parse_element_section, parse_export_section,
6-
parse_function_section, parse_global_section, parse_import_section, parse_memory_section,
7-
parse_name_section, parse_start_section, parse_table_section, parse_type_section,
5+
parse_data_section, parse_element_section, parse_export_section, parse_function_section,
6+
parse_global_section, parse_import_section, parse_memory_section, parse_name_section,
7+
parse_start_section, parse_table_section, parse_type_section,
88
};
99
use crate::state::ModuleTranslationState;
1010
use cranelift_codegen::timing;
11-
use wasmparser::{CustomSectionContent, ModuleReader, SectionContent};
11+
use wasmparser::{NameSectionReader, Parser, Payload};
1212

1313
/// Translate a sequence of bytes forming a valid Wasm binary into a list of valid Cranelift IR
1414
/// [`Function`](cranelift_codegen::ir::Function).
@@ -17,80 +17,85 @@ pub fn translate_module<'data>(
1717
environ: &mut dyn ModuleEnvironment<'data>,
1818
) -> WasmResult<ModuleTranslationState> {
1919
let _tt = timing::wasm_translate_module();
20-
let mut reader = ModuleReader::new(data)?;
2120
let mut module_translation_state = ModuleTranslationState::new();
2221

23-
while !reader.eof() {
24-
let section = reader.read()?;
25-
match section.content()? {
26-
SectionContent::Type(types) => {
22+
for payload in Parser::new(0).parse_all(data) {
23+
match payload? {
24+
Payload::Version { .. } | Payload::End => {}
25+
26+
Payload::TypeSection(types) => {
2727
parse_type_section(types, &mut module_translation_state, environ)?;
2828
}
2929

30-
SectionContent::Import(imports) => {
30+
Payload::ImportSection(imports) => {
3131
parse_import_section(imports, environ)?;
3232
}
3333

34-
SectionContent::Function(functions) => {
34+
Payload::FunctionSection(functions) => {
3535
parse_function_section(functions, environ)?;
3636
}
3737

38-
SectionContent::Table(tables) => {
38+
Payload::TableSection(tables) => {
3939
parse_table_section(tables, environ)?;
4040
}
4141

42-
SectionContent::Memory(memories) => {
42+
Payload::MemorySection(memories) => {
4343
parse_memory_section(memories, environ)?;
4444
}
4545

46-
SectionContent::Global(globals) => {
46+
Payload::GlobalSection(globals) => {
4747
parse_global_section(globals, environ)?;
4848
}
4949

50-
SectionContent::Export(exports) => {
50+
Payload::ExportSection(exports) => {
5151
parse_export_section(exports, environ)?;
5252
}
5353

54-
SectionContent::Start(start) => {
55-
parse_start_section(start, environ)?;
54+
Payload::StartSection { func, .. } => {
55+
parse_start_section(func, environ)?;
5656
}
5757

58-
SectionContent::Element(elements) => {
58+
Payload::ElementSection(elements) => {
5959
parse_element_section(elements, environ)?;
6060
}
6161

62-
SectionContent::Code(code) => {
63-
parse_code_section(code, &module_translation_state, environ)?;
62+
Payload::CodeSectionStart { .. } => {}
63+
Payload::CodeSectionEntry(code) => {
64+
let mut code = code.get_binary_reader();
65+
let size = code.bytes_remaining();
66+
let offset = code.original_position();
67+
environ.define_function_body(
68+
&module_translation_state,
69+
code.read_bytes(size)?,
70+
offset,
71+
)?;
6472
}
6573

66-
SectionContent::Data(data) => {
74+
Payload::DataSection(data) => {
6775
parse_data_section(data, environ)?;
6876
}
6977

70-
SectionContent::DataCount(count) => {
78+
Payload::DataCountSection { count, .. } => {
7179
environ.reserve_passive_data(count)?;
7280
}
7381

74-
SectionContent::Module(_)
75-
| SectionContent::ModuleCode(_)
76-
| SectionContent::Instance(_)
77-
| SectionContent::Alias(_) => unimplemented!("module linking not implemented yet"),
78-
79-
SectionContent::Custom {
80-
name,
81-
binary,
82-
content,
83-
} => match content {
84-
Some(CustomSectionContent::Name(names)) => {
85-
parse_name_section(names, environ)?;
86-
}
87-
_ => {
88-
let mut reader = binary.clone();
89-
let len = reader.bytes_remaining();
90-
let payload = reader.read_bytes(len)?;
91-
environ.custom_section(name, payload)?;
92-
}
93-
},
82+
Payload::ModuleSection(_)
83+
| Payload::InstanceSection(_)
84+
| Payload::AliasSection(_)
85+
| Payload::ModuleCodeSectionStart { .. }
86+
| Payload::ModuleCodeSectionEntry { .. } => {
87+
unimplemented!("module linking not implemented yet")
88+
}
89+
90+
Payload::CustomSection {
91+
name: "name",
92+
data,
93+
data_offset,
94+
} => parse_name_section(NameSectionReader::new(data, data_offset)?, environ)?,
95+
96+
Payload::CustomSection { name, data, .. } => environ.custom_section(name, data)?,
97+
98+
Payload::UnknownSection { .. } => unreachable!(),
9499
}
95100
}
96101

cranelift/wasm/src/sections_translator.rs

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ use cranelift_entity::EntityRef;
2323
use std::boxed::Box;
2424
use std::vec::Vec;
2525
use wasmparser::{
26-
self, CodeSectionReader, Data, DataKind, DataSectionReader, Element, ElementItem, ElementItems,
27-
ElementKind, ElementSectionReader, Export, ExportSectionReader, ExternalKind,
28-
FunctionSectionReader, GlobalSectionReader, GlobalType, ImportSectionEntryType,
29-
ImportSectionReader, MemorySectionReader, MemoryType, NameSectionReader, Naming, NamingReader,
30-
Operator, TableSectionReader, Type, TypeDef, TypeSectionReader,
26+
self, Data, DataKind, DataSectionReader, Element, ElementItem, ElementItems, ElementKind,
27+
ElementSectionReader, Export, ExportSectionReader, ExternalKind, FunctionSectionReader,
28+
GlobalSectionReader, GlobalType, ImportSectionEntryType, ImportSectionReader,
29+
MemorySectionReader, MemoryType, NameSectionReader, Naming, NamingReader, Operator,
30+
TableSectionReader, Type, TypeDef, TypeSectionReader,
3131
};
3232

3333
/// Parses the Type section of the wasm module.
@@ -358,21 +358,6 @@ pub fn parse_element_section<'data>(
358358
Ok(())
359359
}
360360

361-
/// Parses the Code section of the wasm module.
362-
pub fn parse_code_section<'data>(
363-
code: CodeSectionReader<'data>,
364-
module_translation_state: &ModuleTranslationState,
365-
environ: &mut dyn ModuleEnvironment<'data>,
366-
) -> WasmResult<()> {
367-
for body in code {
368-
let mut reader = body?.get_binary_reader();
369-
let size = reader.bytes_remaining();
370-
let offset = reader.original_position();
371-
environ.define_function_body(module_translation_state, reader.read_bytes(size)?, offset)?;
372-
}
373-
Ok(())
374-
}
375-
376361
/// Parses the Data section of the wasm module.
377362
pub fn parse_data_section<'data>(
378363
data: DataSectionReader<'data>,

crates/debug/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ edition = "2018"
1313

1414
[dependencies]
1515
gimli = "0.21.0"
16-
wasmparser = "0.58.0"
16+
wasmparser = "0.59.0"
1717
object = { version = "0.20", default-features = false, features = ["read", "write"] }
1818
wasmtime-environ = { path = "../environ", version = "0.18.0" }
1919
target-lexicon = { version = "0.10.0", default-features = false }

0 commit comments

Comments
 (0)