diff options
| author | nsfisis <nsfisis@gmail.com> | 2024-02-26 12:46:54 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2024-02-26 12:47:13 +0900 |
| commit | 8a789bffe72e93ae24ea39648018e5fecf94c7fa (patch) | |
| tree | 21784ae2e306c1de3e1e5514f237805863eb3913 | |
| parent | 900377463a8264871ee42e488112b076b7973b6a (diff) | |
| download | php-waddiwasi-8a789bffe72e93ae24ea39648018e5fecf94c7fa.tar.gz php-waddiwasi-8a789bffe72e93ae24ea39648018e5fecf94c7fa.tar.zst php-waddiwasi-8a789bffe72e93ae24ea39648018e5fecf94c7fa.zip | |
fix: fix binary decoder
| -rw-r--r-- | src/BinaryFormat/Decoder.php | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/src/BinaryFormat/Decoder.php b/src/BinaryFormat/Decoder.php index be1b44d..ae6e9f2 100644 --- a/src/BinaryFormat/Decoder.php +++ b/src/BinaryFormat/Decoder.php @@ -727,7 +727,8 @@ final class Decoder case 0x3E: return Instr::I64Store32(...$this->decodeMemArg()); case 0x3F: if ($this->decodeByte() !== 0) { - throw new InvalidBinaryFormatException("memory size"); + $this->seekBy(-1); + throw new InvalidBinaryFormatException("Unexpected value while decoding an instruction `memory.size`, expected 0, but got " . $this->decodeByte()); } return Instr::MemorySize(); case 0x40: @@ -916,7 +917,10 @@ final class Decoder throw new InvalidBinaryFormatException("instr"); } // no break - default: throw new InvalidBinaryFormatException("instr"); + default: + $this->seekBy(-1); + $op = $this->decodeByte(); + throw new InvalidBinaryFormatException("Unexpected opcode $op while decoding an instruction"); } } @@ -1011,6 +1015,11 @@ final class Decoder return ord($this->input[$this->pos++]); } + private function seekBy(int $offset): void + { + $this->pos += $offset; + } + private function decodeU32(): int { return $this->decodeUnsignedLeb128(32); @@ -1042,7 +1051,11 @@ final class Decoder $result |= ($b & 0b01111111) << $shiftBits; if ($b < 0b10000000) { if (($b & 0b01000000) !== 0) { - $result |= -(1 << $shiftBits); + if ($shiftBits < $bits - 1) { + $result |= -(1 << $shiftBits); + } else { + $result |= 1 << $shiftBits; + } } return $result; } @@ -1084,6 +1097,7 @@ final class Decoder { $this->ensureNBytesRemains(4); $result = unpack('g', $this->input, $this->pos); + $this->pos += 4; if ($result === false) { throw new InvalidBinaryFormatException("f32"); } @@ -1098,6 +1112,7 @@ final class Decoder { $this->ensureNBytesRemains(8); $result = unpack('e', $this->input, $this->pos); + $this->pos += 8; if ($result === false) { throw new InvalidBinaryFormatException("f64"); } |
