Skip to content

Commit

Permalink
Fail deterministically on invalid modules (#1969)
Browse files Browse the repository at this point in the history
Inspired by bytecodealliance/wasmtime#9947 this helps keep the output of
`wasm-tools validate` more deterministic by ensuring that the first
error is reported instead of any error within a module.
  • Loading branch information
alexcrichton authored Jan 8, 2025
1 parent 31c5811 commit 645df68
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 14 deletions.
38 changes: 24 additions & 14 deletions src/bin/wasm-tools/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,21 +144,31 @@ impl Opts {
}
log::info!("module structure validated in {:?}", start.elapsed());

// After we've validate the entire wasm module we'll use `rayon` to iterate
// over all functions in parallel and perform parallel validation of the
// input wasm module.
// After we've validate the entire wasm module we'll use `rayon` to
// iterate over all functions in parallel and perform parallel
// validation of the input wasm module.
//
// Note that validation results for each function are collected into a
// vector to ensure that in the case of multiple errors the first is
// always reported. Otherwise `rayon` does not guarantee the order that
// failures show up in.
let start = Instant::now();
functions_to_validate.into_par_iter().try_for_each_init(
FuncValidatorAllocations::default,
|allocs, (to_validate, body)| -> Result<_> {
let mut validator = to_validate.into_validator(mem::take(allocs));
validator
.validate(&body)
.with_context(|| format!("func {} failed to validate", validator.index()))?;
*allocs = validator.into_allocations();
Ok(())
},
)?;
functions_to_validate
.into_par_iter()
.map_init(
FuncValidatorAllocations::default,
|allocs, (to_validate, body)| -> Result<_> {
let mut validator = to_validate.into_validator(mem::take(allocs));
validator.validate(&body).with_context(|| {
format!("func {} failed to validate", validator.index())
})?;
*allocs = validator.into_allocations();
Ok(())
},
)
.collect::<Vec<_>>()
.into_iter()
.collect::<Result<Vec<_>>>()?;
log::info!("functions validated in {:?}", start.elapsed());
Ok(())
}
Expand Down
105 changes: 105 additions & 0 deletions tests/cli/multiple-failures.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
;; FAIL: validate %

(module
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
(func (result i32))
)
4 changes: 4 additions & 0 deletions tests/cli/multiple-failures.wat.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
error: func 0 failed to validate

Caused by:
0: type mismatch: expected i32 but nothing on stack (at offset 0x7d)

0 comments on commit 645df68

Please sign in to comment.