Skip to content

Commit 9cebc6a

Browse files
authored
wasmparser: optimize type section valiadation by specializing over Wasm feature subset (#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
1 parent 4e2b392 commit 9cebc6a

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

  • crates/wasmparser/src/validator

crates/wasmparser/src/validator/core.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,9 +569,69 @@ impl Module {
569569
offset,
570570
)?;
571571
}
572+
if self.try_fast_validation(&rec_group, features, types, offset)? {
573+
return Ok(());
574+
}
572575
self.canonicalize_and_intern_rec_group(features, types, rec_group, offset)
573576
}
574577

578+
#[cfg(not(feature = "features"))]
579+
fn try_fast_validation(
580+
&mut self,
581+
_rec_group: &RecGroup,
582+
_features: &WasmFeatures,
583+
_types: &mut TypeAlloc,
584+
_offset: usize,
585+
) -> Result<bool> {
586+
Ok(false)
587+
}
588+
589+
/// Performs fast type section validation if possible.
590+
///
591+
/// - Returns `Ok(true)` if fast validation was performed, else returns `Ok(false)`.
592+
/// - Returns `Err(_)` if a type section validation error was encountered.
593+
///
594+
/// # Note
595+
///
596+
/// Fast type section validation can only be performed on a
597+
/// statically known subset of `WasmFeatures`.
598+
#[cfg(feature = "features")]
599+
fn try_fast_validation(
600+
&mut self,
601+
rec_group: &RecGroup,
602+
features: &WasmFeatures,
603+
types: &mut TypeAlloc,
604+
offset: usize,
605+
) -> Result<bool> {
606+
/// The subset of `WasmFeatures` for which we know that the
607+
/// fast type section validation can be safely applied.
608+
///
609+
/// Fast type section validation does not have to canonicalize
610+
/// (deduplicate) types and does not have to perform sub-typing
611+
/// checks.
612+
const FAST_VALIDATION_FEATURES: WasmFeatures = WasmFeatures::WASM2
613+
.union(WasmFeatures::CUSTOM_PAGE_SIZES)
614+
.union(WasmFeatures::EXTENDED_CONST)
615+
.union(WasmFeatures::MEMORY64)
616+
.union(WasmFeatures::MULTI_MEMORY)
617+
.union(WasmFeatures::RELAXED_SIMD)
618+
.union(WasmFeatures::TAIL_CALL)
619+
.union(WasmFeatures::THREADS)
620+
.union(WasmFeatures::WIDE_ARITHMETIC);
621+
if !FAST_VALIDATION_FEATURES.contains(*features) {
622+
return Ok(false);
623+
}
624+
if rec_group.is_explicit_rec_group() {
625+
bail!(offset, "requires `gc` proposal to be enabled")
626+
}
627+
for ty in rec_group.types() {
628+
let id = types.push(ty.clone());
629+
self.add_type_id(id);
630+
self.check_composite_type(&ty.composite_type, features, &types, offset)?;
631+
}
632+
Ok(true)
633+
}
634+
575635
pub fn add_import(
576636
&mut self,
577637
mut import: crate::Import,

0 commit comments

Comments
 (0)