Skip to content

Commit

Permalink
deps: Unify wasmparser usage to a single version
Browse files Browse the repository at this point in the history
Upgrade the wasmparser version used by all crates to 0.121.0, and switch
to a shared workspace dependency.
  • Loading branch information
theduke committed Jan 31, 2024
1 parent 7d650e3 commit 8150c5c
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 178 deletions.
19 changes: 3 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions lib/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ wasmer-compiler-llvm = { path = "../compiler-llvm", version = "=4.2.5", optional
wasm-bindgen = { version = "0.2.74", optional = true }
js-sys = { version = "0.3.51", optional = true }
rusty_jsc = { version = "0.1.0", optional = true }
wasmparser = { version = "0.83", default-features = false, optional = true }
wasmparser = { workspace = true, default-features = false, optional = true }

# - Mandatory dependencies for `sys` on Windows.
[target.'cfg(all(not(target_arch = "wasm32"), target_os = "windows"))'.dependencies]
Expand All @@ -72,7 +72,7 @@ wasm-bindgen = "0.2.74"
js-sys = "0.3.51"
wasmer-derive = { path = "../derive", version = "=4.2.5" }
# - Optional dependencies for `js`.
wasmparser = { version = "0.95", default-features = false, optional = true }
wasmparser = { workspace = true, default-features = false, optional = true }
hashbrown = { version = "0.11", optional = true }
serde-wasm-bindgen = { version = "0.4.5" }
serde = { version = "1.0", features = ["derive"] }
Expand Down
100 changes: 58 additions & 42 deletions lib/api/src/js/module_info_polyfill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,7 @@ pub fn translate_module<'data>(data: &'data [u8]) -> WasmResult<ModuleInfoPolyfi
let name = sectionreader.name();
if name == "name" {
parse_name_section(
NameSectionReader::new(sectionreader.data(), sectionreader.data_offset())
.map_err(transform_err)?,
NameSectionReader::new(sectionreader.data(), sectionreader.data_offset()),
&mut module_info,
)?;
}
Expand All @@ -310,41 +309,57 @@ pub fn wptype_to_type(ty: wasmparser::ValType) -> WasmResult<Type> {
wasmparser::ValType::F32 => Ok(Type::F32),
wasmparser::ValType::F64 => Ok(Type::F64),
wasmparser::ValType::V128 => Ok(Type::V128),
wasmparser::ValType::ExternRef => Ok(Type::ExternRef),
wasmparser::ValType::FuncRef => Ok(Type::FuncRef),
wasmparser::ValType::Ref(ty) => wpreftype_to_type(ty),
}
}

/// Converts a wasmparser ref type to a [`Type`].
pub fn wpreftype_to_type(ty: wasmparser::RefType) -> WasmResult<Type> {
if ty.is_extern_ref() {
Ok(Type::ExternRef)
} else if ty.is_func_ref() {
Ok(Type::FuncRef)
} else {
Err(format!("Unsupported ref type: {:?}", ty))
}
}

/// Parses the Type section of the wasm module.
pub fn parse_type_section(
types: TypeSectionReader,
reader: TypeSectionReader,
module_info: &mut ModuleInfoPolyfill,
) -> WasmResult<()> {
let count = types.get_count();
module_info.reserve_signatures(count)?;

for entry in types {
if let Ok(wasmparser::Type::Func(functype)) = entry {
let params = functype.params();
let returns = functype.results();
let sig_params: Vec<Type> = params
.iter()
.map(|ty| {
wptype_to_type(*ty)
.expect("only numeric types are supported in function signatures")
})
.collect();
let sig_returns: Vec<Type> = returns
.iter()
.map(|ty| {
wptype_to_type(*ty)
.expect("only numeric types are supported in function signatures")
})
.collect();
let sig = FunctionType::new(sig_params, sig_returns);
module_info.declare_signature(sig)?;
} else {
unimplemented!("module linking not implemented yet")
module_info.reserve_signatures(reader.count())?;

for res in reader {
let group = res.map_err(transform_err)?;

for ty in group.into_types() {
match ty.composite_type {
wasmparser::CompositeType::Func(functype) => {
let params = functype.params();
let returns = functype.results();
let sig_params: Vec<Type> = params
.iter()
.map(|ty| {
wptype_to_type(*ty)
.expect("only numeric types are supported in function signatures")
})
.collect();
let sig_returns: Vec<Type> = returns
.iter()
.map(|ty| {
wptype_to_type(*ty)
.expect("only numeric types are supported in function signatures")
})
.collect();
let sig = FunctionType::new(sig_params, sig_returns);
module_info.declare_signature(sig)?;
}
_ => {
unimplemented!("GC is not implemented yet")
}
}
}
}

Expand All @@ -356,7 +371,7 @@ pub fn parse_import_section<'data>(
imports: ImportSectionReader<'data>,
module_info: &mut ModuleInfoPolyfill,
) -> WasmResult<()> {
module_info.reserve_imports(imports.get_count())?;
module_info.reserve_imports(imports.count())?;

for entry in imports {
let import = entry.map_err(transform_err)?;
Expand Down Expand Up @@ -406,7 +421,7 @@ pub fn parse_import_section<'data>(
TypeRef::Table(ref tab) => {
module_info.declare_table_import(
TableType {
ty: wptype_to_type(tab.element_type).unwrap(),
ty: wpreftype_to_type(tab.element_type).unwrap(),
minimum: tab.initial,
maximum: tab.maximum,
},
Expand All @@ -424,7 +439,7 @@ pub fn parse_function_section(
functions: FunctionSectionReader,
module_info: &mut ModuleInfoPolyfill,
) -> WasmResult<()> {
let num_functions = functions.get_count();
let num_functions = functions.count();
module_info.reserve_func_types(num_functions)?;

for entry in functions {
Expand All @@ -440,14 +455,14 @@ pub fn parse_table_section(
tables: TableSectionReader,
module_info: &mut ModuleInfoPolyfill,
) -> WasmResult<()> {
module_info.reserve_tables(tables.get_count())?;
module_info.reserve_tables(tables.count())?;

for entry in tables {
let table = entry.map_err(transform_err)?;
module_info.declare_table(TableType {
ty: wptype_to_type(table.element_type).unwrap(),
minimum: table.initial,
maximum: table.maximum,
ty: wpreftype_to_type(table.ty.element_type).unwrap(),
minimum: table.ty.initial,
maximum: table.ty.maximum,
})?;
}

Expand All @@ -459,7 +474,7 @@ pub fn parse_memory_section(
memories: MemorySectionReader,
module_info: &mut ModuleInfoPolyfill,
) -> WasmResult<()> {
module_info.reserve_memories(memories.get_count())?;
module_info.reserve_memories(memories.count())?;

for entry in memories {
let WPMemoryType {
Expand All @@ -486,7 +501,7 @@ pub fn parse_global_section(
globals: GlobalSectionReader,
module_info: &mut ModuleInfoPolyfill,
) -> WasmResult<()> {
module_info.reserve_globals(globals.get_count())?;
module_info.reserve_globals(globals.count())?;

for entry in globals {
let WPGlobalType {
Expand All @@ -508,7 +523,7 @@ pub fn parse_export_section<'data>(
exports: ExportSectionReader<'data>,
module_info: &mut ModuleInfoPolyfill,
) -> WasmResult<()> {
module_info.reserve_exports(exports.get_count())?;
module_info.reserve_exports(exports.count())?;

for entry in exports {
let Export {
Expand Down Expand Up @@ -553,7 +568,7 @@ pub fn parse_name_section<'data>(
mut names: NameSectionReader<'data>,
module_info: &mut ModuleInfoPolyfill,
) -> WasmResult<()> {
while let Ok(subsection) = names.read() {
while let Some(Ok(subsection)) = names.next() {
match subsection {
wasmparser::Name::Function(_function_subsection) => {
//for naming in function_subsection.into_iter().flatten() {
Expand All @@ -579,6 +594,7 @@ pub fn parse_name_section<'data>(
| wasmparser::Name::Global(_)
| wasmparser::Name::Element(_)
| wasmparser::Name::Data(_)
| wasmparser::Name::Tag(_)
| wasmparser::Name::Unknown { .. } => {}
};
}
Expand All @@ -589,7 +605,7 @@ pub fn parse_name_section<'data>(
// mut naming_reader: NamingReader<'_>,
// ) -> Option<HashMap<FunctionIndex, &str>> {
// let mut function_names = HashMap::new();
// for _ in 0..naming_reader.get_count() {
// for _ in 0..naming_reader.count() {
// let Naming { index, name } = naming_reader.read().ok()?;
// if index == std::u32::MAX {
// // We reserve `u32::MAX` for our own use.
Expand Down
2 changes: 1 addition & 1 deletion lib/registry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ tracing = "0.1.40"
url = "2.3.1"
wasmer-toml = { workspace = true }
wasmer-wasm-interface = { version = "4.2.5", path = "../wasm-interface", optional = true }
wasmparser = { version = "0.51.4", optional = true }
wasmparser = { workspace = true, optional = true }
whoami = "1.2.3"

[dev-dependencies]
Expand Down
42 changes: 16 additions & 26 deletions lib/registry/src/package/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -786,32 +786,22 @@ mod validate {
wasm: &[u8],
file_name: String,
) -> Result<(), ValidationError> {
use wasmparser::WasmDecoder;
let mut parser = wasmparser::ValidatingParser::new(
wasm,
Some(wasmparser::ValidatingParserConfig {
operator_config: wasmparser::OperatorValidatorConfig {
enable_threads: true,
enable_reference_types: true,
enable_simd: true,
enable_bulk_memory: true,
enable_multi_value: true,
},
}),
);
loop {
let state = parser.read();
match state {
wasmparser::ParserState::EndWasm => return Ok(()),
wasmparser::ParserState::Error(e) => {
return Err(ValidationError::InvalidWasm {
file: file_name,
error: format!("{}", e),
});
}
_ => {}
}
}
let mut val = wasmparser::Validator::new_with_features(wasmparser::WasmFeatures {
threads: true,
reference_types: true,
simd: true,
bulk_memory: true,
multi_value: true,
..Default::default()
});

val.validate_all(wasm)
.map_err(|e| ValidationError::InvalidWasm {
file: file_name.clone(),
error: format!("{}", e),
})?;

Ok(())
}

/// How should validation be treated by the publishing process?
Expand Down
2 changes: 1 addition & 1 deletion lib/wasm-interface/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ bincode = { version = "1", optional = true }
either = "1.5"
nom = "5"
serde = { version = "1", features = ["derive"] }
wasmparser = { version = "0.51.4", optional = true }
wasmparser = { workspace = true, optional = true }

[dev-dependencies]
wat = "1.0"
Expand Down
Loading

0 comments on commit 8150c5c

Please sign in to comment.