Skip to content

Commit

Permalink
wasmparser: optimize type section valiadation by specializing over …
Browse files Browse the repository at this point in the history
…Wasm feature subset (bytecodealliance#1906)

* add specialized type section validation for Wasm feature subset

* use WasmFeatures getters

* apply rustfmt

* check first, then push types

* Revert "check first, then push types"

This reverts commit 213a72c.

* no longer apply fast validation when stack-switching is enabled

* apply rustfmt

* update comment

* allow-list features for fast type validation

* alphabetically order WasmFeatures in allow-list

* update comments about fast validation

* apply rustfmt

* fix fast validation compilation without features crate feature
  • Loading branch information
Robbepop authored Nov 16, 2024
1 parent 4e2b392 commit 9cebc6a
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions crates/wasmparser/src/validator/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,9 +569,69 @@ impl Module {
offset,
)?;
}
if self.try_fast_validation(&rec_group, features, types, offset)? {
return Ok(());
}
self.canonicalize_and_intern_rec_group(features, types, rec_group, offset)
}

#[cfg(not(feature = "features"))]
fn try_fast_validation(
&mut self,
_rec_group: &RecGroup,
_features: &WasmFeatures,
_types: &mut TypeAlloc,
_offset: usize,
) -> Result<bool> {
Ok(false)
}

/// Performs fast type section validation if possible.
///
/// - Returns `Ok(true)` if fast validation was performed, else returns `Ok(false)`.
/// - Returns `Err(_)` if a type section validation error was encountered.
///
/// # Note
///
/// Fast type section validation can only be performed on a
/// statically known subset of `WasmFeatures`.
#[cfg(feature = "features")]
fn try_fast_validation(
&mut self,
rec_group: &RecGroup,
features: &WasmFeatures,
types: &mut TypeAlloc,
offset: usize,
) -> Result<bool> {
/// The subset of `WasmFeatures` for which we know that the
/// fast type section validation can be safely applied.
///
/// Fast type section validation does not have to canonicalize
/// (deduplicate) types and does not have to perform sub-typing
/// checks.
const FAST_VALIDATION_FEATURES: WasmFeatures = WasmFeatures::WASM2
.union(WasmFeatures::CUSTOM_PAGE_SIZES)
.union(WasmFeatures::EXTENDED_CONST)
.union(WasmFeatures::MEMORY64)
.union(WasmFeatures::MULTI_MEMORY)
.union(WasmFeatures::RELAXED_SIMD)
.union(WasmFeatures::TAIL_CALL)
.union(WasmFeatures::THREADS)
.union(WasmFeatures::WIDE_ARITHMETIC);
if !FAST_VALIDATION_FEATURES.contains(*features) {
return Ok(false);
}
if rec_group.is_explicit_rec_group() {
bail!(offset, "requires `gc` proposal to be enabled")
}
for ty in rec_group.types() {
let id = types.push(ty.clone());
self.add_type_id(id);
self.check_composite_type(&ty.composite_type, features, &types, offset)?;
}
Ok(true)
}

pub fn add_import(
&mut self,
mut import: crate::Import,
Expand Down

0 comments on commit 9cebc6a

Please sign in to comment.