Skip to content

Commit

Permalink
Fix local.set preservation bug (#834)
Browse files Browse the repository at this point in the history
* add testcase

* fix bug by avoiding no-op local.set translation

* apply rustfmt

* apply rustfmt (2)
  • Loading branch information
Robbepop authored Dec 5, 2023
1 parent 4a8725e commit fdd9364
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
11 changes: 11 additions & 0 deletions crates/wasmi/src/engine/translator/tests/regression/fuzz_2.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(module
(func (;0;) (param i32)
(local.tee 0 (local.get 0))
(local.tee 0 (local.get 0))
(if (param i32) ;; label = @1
(then (drop))
(else (drop))
)
)
(export "" (func 0))
)
15 changes: 15 additions & 0 deletions crates/wasmi/src/engine/translator/tests/regression/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::*;
use crate::engine::bytecode::{BranchOffset, BranchOffset16};

#[test]
#[cfg_attr(miri, ignore)]
Expand Down Expand Up @@ -27,3 +28,17 @@ fn fuzz_regression_1() {
])
.run()
}

#[test]
#[cfg_attr(miri, ignore)]
fn fuzz_regression_2() {
let wat = include_str!("fuzz_2.wat");
let wasm = wat2wasm(wat);
TranslationTest::new(wasm)
.expect_func_instrs([
Instruction::branch_i32_eq_imm(Register::from_i16(0), 0, BranchOffset16::from(2)),
Instruction::branch(BranchOffset::from(1)),
Instruction::Return,
])
.run()
}
9 changes: 9 additions & 0 deletions crates/wasmi/src/engine/translator/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,15 @@ impl<'a> VisitOperator<'a> for FuncTranslator<'a> {
bail_unreachable!(self);
let value = self.alloc.stack.pop();
let local = Register::try_from(local_index)?;
if let TypedProvider::Register(value) = value {
if value == local {
// Case: `(local.set $n (local.get $n))` is a no-op so we can ignore it.
//
// Note: This does not require any preservation since it won't change
// the value of `local $n`.
return Ok(());
}
}
let preserved = self.alloc.stack.preserve_locals(local_index)?;
let fuel_info = self.fuel_info();
self.alloc.instr_encoder.encode_local_set(
Expand Down

0 comments on commit fdd9364

Please sign in to comment.