Skip to content

Commit

Permalink
wasmparser: make TypeId hash differently for aliased types. (#775)
Browse files Browse the repository at this point in the history
* Fix warnings in `wasmparser`.

* wasmparser: add type index to `TypeId`.

This commit adds the original type index (if there is one) to `TypeId`.

The intention for doing so is making `TypeId` `Hash` and `PartialEq`
implementations take into account type aliases.

* wasmparser: add a test for `TypeId` aliasing.
  • Loading branch information
peterhuene authored Sep 23, 2022
1 parent e386e22 commit 1d03334
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 12 deletions.
10 changes: 2 additions & 8 deletions crates/wasmparser/benches/benchmark.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
#[macro_use]
extern crate criterion;

use anyhow::Result;
use criterion::Criterion;
use criterion::{criterion_group, criterion_main, Criterion};
use once_cell::unsync::Lazy;
use std::fs;
use std::path::Path;
use std::path::PathBuf;
use wasmparser::{
BlockType, BrTable, DataKind, ElementKind, Ieee32, Ieee64, MemArg, Parser, Payload, ValType,
Validator, VisitOperator, WasmFeatures, V128,
};
use wasmparser::{DataKind, ElementKind, Parser, Payload, Validator, VisitOperator, WasmFeatures};

/// A benchmark input.
pub struct BenchmarkInput {
Expand Down
2 changes: 1 addition & 1 deletion crates/wasmparser/src/readers/core/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub enum Type {
pub struct FuncType {
/// The combined parameters and result types.
params_results: Box<[ValType]>,
/// The number of paramter types.
/// The number of parameter types.
len_params: usize,
}

Expand Down
41 changes: 41 additions & 0 deletions crates/wasmparser/src/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1426,4 +1426,45 @@ mod tests {

Ok(())
}

#[test]
fn test_type_id_aliasing() -> Result<()> {
let bytes = wat::parse_str(
r#"
(component
(type $T (list string))
(alias outer 0 $T (type $A1))
(alias outer 0 $T (type $A2))
)
"#,
)?;

let mut validator = Validator::new_with_features(WasmFeatures {
component_model: true,
..Default::default()
});

let types = validator.validate_all(&bytes)?;

let t_id = types.id_from_type_index(0, false).unwrap();
let a1_id = types.id_from_type_index(1, false).unwrap();
let a2_id = types.id_from_type_index(2, false).unwrap();

// The ids should all be different
assert!(t_id != a1_id);
assert!(t_id != a2_id);
assert!(a1_id != a2_id);

// However, they should all point to the same type
assert!(std::ptr::eq(
types.type_from_id(t_id).unwrap(),
types.type_from_id(a1_id).unwrap()
));
assert!(std::ptr::eq(
types.type_from_id(t_id).unwrap(),
types.type_from_id(a2_id).unwrap()
));

Ok(())
}
}
34 changes: 32 additions & 2 deletions crates/wasmparser/src/validator/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ impl ComponentState {
current.core_types.push(TypeId {
type_size: ty.type_size(),
index: types.len(),
type_index: Some(current.core_types.len()),
is_core: true,
});
types.push(ty);

Expand All @@ -113,6 +115,8 @@ impl ComponentState {
self.core_modules.push(TypeId {
type_size: ty.type_size(),
index: types.len(),
type_index: None,
is_core: true,
});

types.push(ty);
Expand Down Expand Up @@ -182,6 +186,8 @@ impl ComponentState {
current.types.push(TypeId {
type_size: ty.type_size(),
index: types.len(),
type_index: Some(current.types.len()),
is_core: false,
});
types.push(ty);

Expand Down Expand Up @@ -327,6 +333,8 @@ impl ComponentState {
self.core_funcs.push(TypeId {
type_size: lowered_ty.type_size(),
index: types.len(),
type_index: None,
is_core: true,
});

types.push(lowered_ty);
Expand All @@ -344,6 +352,8 @@ impl ComponentState {
self.components.push(TypeId {
type_size: ty.type_size(),
index: types.len(),
type_index: None,
is_core: false,
});

types.push(ty);
Expand Down Expand Up @@ -1009,6 +1019,8 @@ impl ComponentState {
let id = TypeId {
type_size: ty.type_size(),
index: types.len(),
type_index: None,
is_core: true,
};

types.push(ty);
Expand Down Expand Up @@ -1144,6 +1156,8 @@ impl ComponentState {
let id = TypeId {
type_size: ty.type_size(),
index: types.len(),
type_index: None,
is_core: false,
};

types.push(ty);
Expand Down Expand Up @@ -1245,6 +1259,8 @@ impl ComponentState {
let id = TypeId {
type_size: ty.type_size(),
index: types.len(),
type_index: None,
is_core: false,
};

types.push(ty);
Expand Down Expand Up @@ -1331,6 +1347,8 @@ impl ComponentState {
let id = TypeId {
type_size: ty.type_size(),
index: types.len(),
type_index: None,
is_core: true,
};

types.push(ty);
Expand Down Expand Up @@ -1544,7 +1562,13 @@ impl ComponentState {
let current = components.last_mut().unwrap();
check_max(current.type_count(), 1, MAX_WASM_TYPES, "types", offset)?;

current.core_types.push(ty);
current.core_types.push(TypeId {
type_size: ty.type_size,
index: ty.index,
type_index: Some(current.core_types.len()),
is_core: true,
});

Ok(())
}

Expand All @@ -1555,7 +1579,13 @@ impl ComponentState {
let current = components.last_mut().unwrap();
check_max(current.type_count(), 1, MAX_WASM_TYPES, "types", offset)?;

current.types.push(ty);
current.types.push(TypeId {
type_size: ty.type_size,
index: ty.index,
type_index: Some(current.types.len()),
is_core: false,
});

Ok(())
}

Expand Down
2 changes: 2 additions & 0 deletions crates/wasmparser/src/validator/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,8 @@ impl Module {
self.types.push(TypeId {
type_size: ty.type_size(),
index: types.len(),
type_index: Some(self.types.len()),
is_core: true,
});
types.push(ty);
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion crates/wasmparser/src/validator/operators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ impl<R> Deref for OperatorValidatorTemp<'_, '_, R> {

impl<R> DerefMut for OperatorValidatorTemp<'_, '_, R> {
fn deref_mut(&mut self) -> &mut OperatorValidator {
&mut self.inner
self.inner
}
}

Expand Down
8 changes: 8 additions & 0 deletions crates/wasmparser/src/validator/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,14 @@ pub struct TypeId {
pub(crate) type_size: usize,
/// The index into the global list of types.
pub(crate) index: usize,
/// The original type index.
///
/// This will be `None` for implicitly defined types, e.g. types for
/// modules definitions, component definitions, instantiations, and function
/// lowerings.
pub(crate) type_index: Option<usize>,
/// Whether or not the type is a core type.
pub(crate) is_core: bool,
}

/// A unified type definition for validating WebAssembly modules and components.
Expand Down

0 comments on commit 1d03334

Please sign in to comment.