Skip to content

Commit 29493c7

Browse files
committed
fix bugs in br_table translation
1 parent 1a42bd7 commit 29493c7

File tree

1 file changed

+7
-4
lines changed
  • crates/wasmi/src/engine/translator/func

1 file changed

+7
-4
lines changed

crates/wasmi/src/engine/translator/func/mod.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ use crate::{
7777
ValType,
7878
};
7979
use alloc::vec::Vec;
80+
use core::convert::identity;
8081
use wasmparser::{MemArg, WasmFeatures};
8182

8283
/// Type concerned with translating from Wasm bytecode to Wasmi bytecode.
@@ -852,9 +853,11 @@ impl FuncTranslator {
852853
///
853854
/// Upon call the `immediates` buffer contains all `br_table` target values.
854855
fn encode_br_table_0(&mut self, table: wasmparser::BrTable, index: Slot) -> Result<(), Error> {
855-
debug_assert_eq!(self.immediates.len(), (table.len() + 1) as usize);
856+
// We add +1 because we include the default target here.
857+
let len_targets = table.len() + 1;
858+
debug_assert_eq!(self.immediates.len(), len_targets as usize);
856859
self.push_instr(
857-
Op::branch_table(index, table.len() + 1),
860+
Op::branch_table(len_targets, index),
858861
FuelCostsProvider::base,
859862
)?;
860863
// Encode the `br_table` targets:
@@ -866,7 +869,7 @@ impl FuncTranslator {
866869
};
867870
let mut frame = self.stack.peek_control_mut(depth).control_frame();
868871
self.instrs
869-
.encode_branch(frame.label(), Op::branch, fuel_pos, 0)?;
872+
.encode_branch(frame.label(), identity, fuel_pos, 0)?;
870873
frame.branch_to();
871874
}
872875
Ok(())
@@ -887,7 +890,7 @@ impl FuncTranslator {
887890
let consume_fuel_instr = self.stack.consume_fuel_instr();
888891
let values = self.try_form_regspan_or_move(usize::from(len_values), consume_fuel_instr)?;
889892
self.push_instr(
890-
Op::branch_table_span(index, table.len() + 1, values, len_values),
893+
Op::branch_table_span(table.len() + 1, index, values, len_values),
891894
FuelCostsProvider::base,
892895
)?;
893896
// Encode the `br_table` targets:

0 commit comments

Comments
 (0)