Skip to content

Commit

Permalink
Fix parsing of SIMD opcode. (#120)
Browse files Browse the repository at this point in the history
* Fix parsing of SIMD opcode.

The SIMD proposal declares its opcode with the grammar:
```
instr ::= ...
        | 0xFD simdop:varuint32 ...
```

`wabt` treats all instructions having prefixes as being LEB128, but I wasn't able to find such text in any 0xfc (Non-trapping Float-to-int Conversions) or 0xfe (Threading proposal for WebAssembly). Neither of them have allocated enough opcodes that it would matter yet.

* Use a u8 instead of a u32, it's faster.

* Add read_var_u8
  • Loading branch information
nlewycky authored and yurydelendik committed Jul 30, 2019
1 parent 77255f7 commit 319a2e2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wasmparser"
version = "0.35.0"
version = "0.35.1"
authors = ["Yury Delendik <ydelendik@mozilla.com>"]
license = "Apache-2.0 WITH LLVM-exception"
repository = "https://github.com/yurydelendik/wasmparser.rs"
Expand Down
19 changes: 18 additions & 1 deletion src/binary_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,23 @@ impl<'a> BinaryReader<'a> {
Ok(b)
}

pub fn read_var_u8(&mut self) -> Result<u32> {
// Optimization for single byte i32.
let byte = self.read_u8()?;
if (byte & 0x80) == 0 {
return Ok(byte);
}

let result = (self.read_u8()? << 7) | (byte & 0x7F);
if result >= 0x100 {
return Err(BinaryReaderError {
message: "Invalid var_u8",
offset: self.original_position() - 1,
});
}
Ok(result)
}

pub fn read_var_u32(&mut self) -> Result<u32> {
// Optimization for single byte i32.
let byte = self.read_u8()?;
Expand Down Expand Up @@ -1239,7 +1256,7 @@ impl<'a> BinaryReader<'a> {
}

fn read_0xfd_operator(&mut self) -> Result<Operator<'a>> {
let code = self.read_u8()? as u8;
let code = self.read_var_u8()? as u8;
Ok(match code {
0x00 => Operator::V128Load {
memarg: self.read_memarg()?,
Expand Down

0 comments on commit 319a2e2

Please sign in to comment.