Skip to content

Commit 05d0446

Browse files
florianhartungwucke13
authored andcommitted
fix: multibyte instructions use variable-length integer encoding
Signed-off-by: Florian Hartung <florian.hartung@dlr.de>
1 parent 37c8bf7 commit 05d0446

File tree

5 files changed

+33
-35
lines changed

5 files changed

+33
-35
lines changed

src/core/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub enum Error {
7373
InvalidImportDesc(u8),
7474
ExprMissingEnd,
7575
InvalidInstr(u8),
76-
InvalidMultiByteInstr(u8, u8),
76+
InvalidMultiByteInstr(u8, u32),
7777
EndInvalidValueStack,
7878
InvalidLocalIdx,
7979
InvalidValidationStackValType(Option<ValType>),

src/core/reader/types/opcode.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -185,31 +185,31 @@ pub const I64_EXTEND16_S: u8 = 0xC3;
185185
pub const I64_EXTEND32_S: u8 = 0xC4;
186186

187187
pub mod fc_extensions {
188-
pub const I32_TRUNC_SAT_F32_S: u8 = 0x00;
189-
pub const I32_TRUNC_SAT_F32_U: u8 = 0x01;
190-
pub const I32_TRUNC_SAT_F64_S: u8 = 0x02;
191-
pub const I32_TRUNC_SAT_F64_U: u8 = 0x03;
192-
pub const I64_TRUNC_SAT_F32_S: u8 = 0x04;
193-
pub const I64_TRUNC_SAT_F32_U: u8 = 0x05;
194-
pub const I64_TRUNC_SAT_F64_S: u8 = 0x06;
195-
pub const I64_TRUNC_SAT_F64_U: u8 = 0x07;
196-
pub const MEMORY_INIT: u8 = 0x08;
197-
pub const DATA_DROP: u8 = 0x09;
198-
pub const MEMORY_COPY: u8 = 0x0A;
199-
pub const MEMORY_FILL: u8 = 0x0B;
200-
pub const TABLE_INIT: u8 = 0x0C;
201-
pub const ELEM_DROP: u8 = 0x0D;
202-
pub const TABLE_COPY: u8 = 0x0E;
203-
pub const TABLE_GROW: u8 = 0x0F;
204-
pub const TABLE_SIZE: u8 = 0x10;
205-
pub const TABLE_FILL: u8 = 0x11;
188+
pub const I32_TRUNC_SAT_F32_S: u32 = 0x00;
189+
pub const I32_TRUNC_SAT_F32_U: u32 = 0x01;
190+
pub const I32_TRUNC_SAT_F64_S: u32 = 0x02;
191+
pub const I32_TRUNC_SAT_F64_U: u32 = 0x03;
192+
pub const I64_TRUNC_SAT_F32_S: u32 = 0x04;
193+
pub const I64_TRUNC_SAT_F32_U: u32 = 0x05;
194+
pub const I64_TRUNC_SAT_F64_S: u32 = 0x06;
195+
pub const I64_TRUNC_SAT_F64_U: u32 = 0x07;
196+
pub const MEMORY_INIT: u32 = 0x08;
197+
pub const DATA_DROP: u32 = 0x09;
198+
pub const MEMORY_COPY: u32 = 0x0A;
199+
pub const MEMORY_FILL: u32 = 0x0B;
200+
pub const TABLE_INIT: u32 = 0x0C;
201+
pub const ELEM_DROP: u32 = 0x0D;
202+
pub const TABLE_COPY: u32 = 0x0E;
203+
pub const TABLE_GROW: u32 = 0x0F;
204+
pub const TABLE_SIZE: u32 = 0x10;
205+
pub const TABLE_FILL: u32 = 0x11;
206206
}
207207

208208
#[cfg(debug_assertions)]
209-
pub fn fc_extension_opcode_second_byte_to_str(byte: u8) -> alloc::string::String {
209+
pub fn fc_extension_opcode_second_byte_to_str(instr: u32) -> alloc::string::String {
210210
use alloc::{borrow::ToOwned, format};
211211

212-
let opcode = match byte {
212+
let opcode = match instr {
213213
0x00 => "I32_TRUNC_SAT_F32_S",
214214
0x01 => "I32_TRUNC_SAT_F32_U",
215215
0x02 => "I32_TRUNC_SAT_F64_S",
@@ -233,7 +233,7 @@ pub fn fc_extension_opcode_second_byte_to_str(byte: u8) -> alloc::string::String
233233
.to_owned();
234234

235235
if opcode == "UNKNOWN" {
236-
format!("UNKNOWN({:x})", byte)
236+
format!("UNKNOWN({:x})", instr)
237237
} else {
238238
opcode
239239
}

src/core/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub fn print_beautiful_instruction_name_1_byte(first_byte: u8, pc: usize) {
1010
}
1111

1212
#[cfg(debug_assertions)]
13-
pub fn print_beautiful_fc_extension(second_byte: u8, pc: usize) {
13+
pub fn print_beautiful_fc_extension(second_byte: u32, pc: usize) {
1414
use crate::core::reader::types::opcode::fc_extension_opcode_second_byte_to_str;
1515

1616
trace!(

src/execution/interpreter_loop.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1907,10 +1907,10 @@ pub(super) fn run<H: HookSet>(
19071907
}
19081908
FC_EXTENSIONS => {
19091909
// Should we call instruction hook here as well? Multibyte instruction
1910-
let second_instr_byte = wasm.read_u8().unwrap_validated();
1910+
let second_instr = wasm.read_var_u32().unwrap_validated();
19111911

19121912
use crate::core::reader::types::opcode::fc_extensions::*;
1913-
match second_instr_byte {
1913+
match second_instr {
19141914
I32_TRUNC_SAT_F32_S => {
19151915
let v1: value::F32 = stack.pop_value(ValType::NumType(NumType::F32)).into();
19161916
let res = {

src/validation/code.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,19 +1036,22 @@ fn read_instructions(
10361036
}
10371037

10381038
FC_EXTENSIONS => {
1039-
let Ok(second_instr_byte) = wasm.read_u8() else {
1039+
let Ok(second_instr) = wasm.read_var_u32() else {
10401040
// TODO only do this if EOF
10411041
return Err(Error::ExprMissingEnd);
10421042
};
10431043

10441044
#[cfg(debug_assertions)]
1045-
crate::core::utils::print_beautiful_fc_extension(second_instr_byte, wasm.pc);
1045+
crate::core::utils::print_beautiful_fc_extension(second_instr, wasm.pc);
10461046

10471047
#[cfg(not(debug_assertions))]
1048-
trace!("Read instruction byte {second_instr_byte:#04X?} ({second_instr_byte}) at wasm_binary[{}]", wasm.pc);
1048+
trace!(
1049+
"Read instruction byte {second_instr} at wasm_binary[{}]",
1050+
wasm.pc
1051+
);
10491052

10501053
use crate::core::reader::types::opcode::fc_extensions::*;
1051-
match second_instr_byte {
1054+
match second_instr {
10521055
I32_TRUNC_SAT_F32_S => {
10531056
stack.assert_pop_val_type(ValType::NumType(NumType::F32))?;
10541057
stack.push_valtype(ValType::NumType(NumType::I32));
@@ -1229,12 +1232,7 @@ fn read_instructions(
12291232
stack.assert_pop_ref_type(Some(t))?;
12301233
stack.assert_pop_val_type(ValType::NumType(NumType::I32))?;
12311234
}
1232-
_ => {
1233-
return Err(Error::InvalidMultiByteInstr(
1234-
first_instr_byte,
1235-
second_instr_byte,
1236-
))
1237-
}
1235+
_ => return Err(Error::InvalidMultiByteInstr(first_instr_byte, second_instr)),
12381236
}
12391237
}
12401238

0 commit comments

Comments
 (0)