Skip to content

Commit 7949063

Browse files
authored
Fix some SIMD bugs found by fuzzing (#1429)
* fix panic in i8x16.shuffle translation This missed a trivial check if the current code path is reachable. * fix encoding bug in v128.storeN_lane instructions This only affected instructions with offsets that could not be 8-bit encoded. * fix bug in v128.shift translation and immediate `rhs` The error was that in case of an immediate `rhs` shift amount the translation pushed a result and `lhs` back to the stack instead of just `lhs`.
1 parent 2de7c09 commit 7949063

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::{
2222
use wasmparser::MemArg;
2323

2424
trait IntoLane {
25-
type LaneType: TryFrom<u8> + Into<u8>;
25+
type LaneType: Copy + TryFrom<u8> + Into<u8>;
2626
}
2727

2828
macro_rules! impl_into_lane_for {
@@ -170,19 +170,21 @@ impl FuncTranslator {
170170
return Ok(());
171171
}
172172
let lhs = self.alloc.stack.provider2reg(&lhs)?;
173-
let result = self.alloc.stack.push_dynamic()?;
174-
let instr = match rhs {
175-
Provider::Register(rhs) => make_instr(result, lhs, rhs),
173+
let rhs = match rhs {
174+
Provider::Register(rhs) => rhs,
176175
Provider::Const(rhs) => {
177176
let Some(rhs) = T::into_shift_amount(rhs.into()) else {
178177
// Case: the shift operation is a no-op
179178
self.alloc.stack.push_register(lhs)?;
180179
return Ok(());
181180
};
182-
make_instr_imm(result, lhs, rhs)
181+
let result = self.alloc.stack.push_dynamic()?;
182+
self.push_fueled_instr(make_instr_imm(result, lhs, rhs), FuelCosts::base)?;
183+
return Ok(());
183184
}
184185
};
185-
self.push_fueled_instr(instr, FuelCosts::base)?;
186+
let result = self.alloc.stack.push_dynamic()?;
187+
self.push_fueled_instr(make_instr(result, lhs, rhs), FuelCosts::base)?;
186188
Ok(())
187189
}
188190

@@ -295,12 +297,10 @@ impl FuncTranslator {
295297
let (offset_hi, offset_lo) = Offset64::split(offset);
296298
let instr = make_instr(ptr, offset_lo);
297299
let param = Instruction::register_and_offset_hi(v128, offset_hi);
298-
let memidx = Instruction::memory_index(memory);
300+
let param2 = Instruction::lane_and_memory_index(lane, memory);
299301
self.push_fueled_instr(instr, FuelCosts::store)?;
300302
self.append_instr(param)?;
301-
if !memory.is_default() {
302-
self.append_instr(memidx)?;
303-
}
303+
self.append_instr(param2)?;
304304
Ok(())
305305
}
306306

crates/wasmi/src/engine/translator/simd/visit.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ impl VisitSimdOperator<'_> for FuncTranslator {
297297
}
298298

299299
fn visit_i8x16_shuffle(&mut self, lanes: [u8; 16]) -> Self::Output {
300+
bail_unreachable!(self);
300301
let selector: [ImmLaneIdx32; 16] = array::from_fn(|i| {
301302
let Ok(lane) = ImmLaneIdx32::try_from(lanes[i]) else {
302303
panic!("encountered out of bounds lane at index {i}: {}", lanes[i])

0 commit comments

Comments
 (0)