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 authored Sep 14, 2024
1 parent 1e5a4ba commit e9c6acf
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 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 @@ -798,10 +798,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
37 changes: 36 additions & 1 deletion 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, Global, RegSpan},
EngineFunc,
},
Val,
};

#[test]
Expand Down Expand Up @@ -507,5 +508,39 @@ fn audit_1_execution() {
.typed::<(), (i32, i32, i32)>(&store)
.unwrap();
let result = func.call(&mut store, ()).unwrap_err();
std::println!("result = {result:?}");
assert_eq!(result.as_trap_code(), Some(TrapCode::IntegerOverflow));
}

#[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 e9c6acf

Please sign in to comment.