Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make module validation deterministic in case of multiple errors #9947

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions crates/wasmtime/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,14 @@ impl Engine {
#[cfg(feature = "parallel-compilation")]
{
use rayon::prelude::*;
// If we collect into Result<Vec<B>, E> directly, the returned error is not
// deterministic, because any error could be returned early. So we first materialize
// all results in order and then return the first error deterministically, or Ok(_).
return input
.into_par_iter()
.map(|a| f(a))
.collect::<Vec<Result<B, E>>>()
.into_iter()
.collect::<Result<Vec<B>, E>>();
}
}
Expand Down
24 changes: 24 additions & 0 deletions tests/all/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,3 +534,27 @@ fn concurrent_type_modifications_and_checks(config: &mut Config) -> Result<()> {

Ok(())
}

#[test]
#[cfg_attr(miri, ignore)]
fn validate_deterministic() {
let mut faulty_wat = "(module ".to_string();
for i in 0..100 {
faulty_wat.push_str(&format!(
"(func (export \"foo_{i}\") (result i64) (i64.add (i32.const 0) (i64.const 1)))"
));
}
faulty_wat.push_str(")");
let binary = wat::parse_str(faulty_wat).unwrap();

let engine_parallel = Engine::new(&Config::new().parallel_compilation(true)).unwrap();
let result_parallel = Module::validate(&engine_parallel, &binary)
.unwrap_err()
.to_string();

let engine_sequential = Engine::new(&Config::new().parallel_compilation(false)).unwrap();
let result_sequential = Module::validate(&engine_sequential, &binary)
.unwrap_err()
.to_string();
assert_eq!(result_parallel, result_sequential);
}
Loading