-
Notifications
You must be signed in to change notification settings - Fork 819
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
reusing a Module
may trigger invalid memory access
#4949
Comments
This is indeed a bug. We need to check that modules are run within the same Note that modules should not be reused across different Edit: I had a typo, sorry for the misdirection here |
What's happening is that the engine is different each time. We should error when this happens. At the same time, something like this should work: let compiler = Singlepass::default();
let engine: Engine = compiler.into();
for i in 0..2 {
println!("Iteration {i}...");
let mut store = Store::new(&engine);
let module = match &module {
Some(module) => module.clone(),
None => {
module = Some(Module::new(&engine, &wasm_bytes).unwrap());
module.as_ref().unwrap().clone()
}
};
let import_object = imports! {};
println!("Instantiating module...");
// Let's instantiate the Wasm module.
let instance = Instance::new(&mut store, &module, &import_object)?;
let sum = instance.exports.get_function("sum")?;
println!("Calling `sum` function...");
// Let's call the `sum` exported function. The parameters are a
// slice of `Value`s. The results are a boxed slice of `Value`s.
// let _results = sum.call(&mut store, 0_i32, 0_i32);
if i == 1 {
println!("The next call will segfault");
}
let _results = sum.call(&mut store, &[Value::I32(1), Value::I32(2)]);
println!("Iteration {i} done.");
println!();
} |
@tzemanovic to clarify: A module is not tied to a store, it is just tied to an You can re-use a module just fine if you stick with one engine instead. Our code should of course fail if you try to re-use a module with the wrong engine, and I actually thought we had added that. |
Describe the bug
Related to #4377 (see comment #4377 (comment)) re-running the same
Module
that's created with aStore
which is dropped after the first run and newStore
created for the second run causes a segfault with invalid memory access.We manage to workaround this issue by keeping a
Store
that's used to compile cachedModules
alive and instantiate a newStore
when we want to run them.wasmer -vV; rustc -vV
Steps to reproduce
I've added a small example that reproduces this here https://github.com/heliaxdev/wasmer/blob/24024841309bff268115ff25cdbb86b1b2db0d52/examples/invalid_memory_access.rs
Run with e.g.
cargo run --example invalid-memory-access --features=singlepass
Expected behavior
Module
can be re-used for multiple runsActual behavior
segfaults on a second loop
The text was updated successfully, but these errors were encountered: