Skip to content

Commit

Permalink
Fix invalid local preservation overwrite (#1177)
Browse files Browse the repository at this point in the history
* properly assert for audit_1_execution test case

* fix invalid local preservation overwrite
  • Loading branch information
Robbepop committed Sep 18, 2024
1 parent 02621ad commit 415a919
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
4 changes: 2 additions & 2 deletions crates/wasmi/src/engine/translator/instr_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -796,10 +796,10 @@ impl InstrEncoder {
};
if matches!(
stack.get_register_space(returned_value),
RegisterSpace::Local
RegisterSpace::Local | RegisterSpace::Preserve
) {
// Can only apply the optimization if the returned value of `last_instr`
// is _NOT_ itself a local register due to observable behavior.
// is _NOT_ itself a local register due to observable behavior or already preserved.
return fallback_case(self, stack, local, value, preserved, fuel_info);
}
let Some(last_instr) = self.last_instr else {
Expand Down
35 changes: 35 additions & 0 deletions crates/wasmi/src/engine/translator/tests/fuzz/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
bytecode::{BranchOffset, BranchOffset16, GlobalIdx, RegisterSpan},
EngineFunc,
},
Val,
};

#[test]
Expand Down Expand Up @@ -510,3 +511,37 @@ fn fuzz_regression_17() {
])
.run()
}

#[test]
#[cfg_attr(miri, ignore)]
fn audit_2_codegen() {
let wasm = include_str!("wat/audit_2.wat");
TranslationTest::from_wat(wasm)
.expect_func_instrs([
Instruction::copy(2, 0),
Instruction::copy(0, 2),
Instruction::copy(1, 0),
Instruction::return_many(2, 1, 0),
Instruction::register(0),
])
.run()
}

#[test]
#[cfg_attr(miri, ignore)]
fn audit_2_execution() {
use crate::{Engine, Instance, Store};
let wat = include_str!("wat/audit_2.wat");
let wasm = wat::parse_str(wat).unwrap();
let engine = Engine::default();
let mut store = <Store<()>>::new(&engine, ());
let module = Module::new(&engine, &wasm[..]).unwrap();
let instance = Instance::new(&mut store, &module, &[]).unwrap();
let func = instance.get_func(&store, "").unwrap();
let inputs = [Val::I32(1)];
let mut results = [0_i32; 4].map(Val::from);
let expected = [1_i32; 4];
func.call(&mut store, &inputs[..], &mut results[..])
.unwrap();
assert_eq!(results.map(|v| v.i32().unwrap()), expected,);
}
15 changes: 15 additions & 0 deletions crates/wasmi/src/engine/translator/tests/fuzz/wat/audit_2.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
(module ;; different result on main than on Wasmtime
(func (export "") (param i32) (result i32 i32 i32 i32)
local.get 0
local.get 0
block (param i32 i32)
local.tee 0
block (param i32 i32)
local.get 0
local.get 0
br 2 ;; returns
end
end
unreachable
)
)

0 comments on commit 415a919

Please sign in to comment.