Skip to content

Commit

Permalink
Fix bug with global.get translation of constant globals (#887)
Browse files Browse the repository at this point in the history
* add regression test found via fuzzing

* fix codegen bug
  • Loading branch information
Robbepop authored Jan 12, 2024
1 parent 9594504 commit 1e24915
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
10 changes: 10 additions & 0 deletions crates/wasmi/src/engine/translator/tests/regression/fuzz_14.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(module
(func (param i32 i32) (result i32 i32)
local.get 0
local.get 1
i32.and
global.get 0
i32.eqz
)
(global i32 (i32.const -2))
)
20 changes: 20 additions & 0 deletions crates/wasmi/src/engine/translator/tests/regression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,3 +333,23 @@ fn fuzz_regression_13_execute() {
let (x, y, z) = func.call(&mut store, ()).unwrap();
assert!(x == 0 && y == 0 && z == 0);
}

#[test]
#[cfg_attr(miri, ignore)]
fn fuzz_regression_14() {
let wat = include_str!("fuzz_14.wat");
let wasm = wat2wasm(wat);
TranslationTest::new(wasm)
.expect_func(
ExpectedFunc::new([
Instruction::i32_and(
Register::from_i16(2),
Register::from_i16(0),
Register::from_i16(1),
),
Instruction::return_reg2(2, -1),
])
.consts([0_i32]),
)
.run()
}
1 change: 1 addition & 0 deletions crates/wasmi/src/engine/translator/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,7 @@ impl<'a> VisitOperator<'a> for FuncTranslator {
// Optmization: Access to immutable internally defined global variables
// can be replaced with their constant initialization value.
self.alloc.stack.push_const(TypedValue::new(content, value));
self.alloc.instr_encoder.reset_last_instr();
return Ok(());
}
if let Some(func_index) = init_expr.funcref() {
Expand Down

0 comments on commit 1e24915

Please sign in to comment.