diff --git a/packages/wasm-parser/src/decoder.js b/packages/wasm-parser/src/decoder.js index 6859a4e95..9f2f35d11 100644 --- a/packages/wasm-parser/src/decoder.js +++ b/packages/wasm-parser/src/decoder.js @@ -764,7 +764,13 @@ export function decode(ab: ArrayBuffer, opts: DecoderOpts): Program { [] ); - eatBytes(1); // 0x00 - reserved byte + const flagU32 = readU32(); + const flag = flagU32.value; // 0x00 - reserved byte + eatBytes(flagU32.nextIndex); + + if (flag !== 0) { + throw new CompileError("zero flag expected"); + } code.push(callNode); instructionAlreadyCreated = true; @@ -788,17 +794,33 @@ export function decode(ab: ArrayBuffer, opts: DecoderOpts): Program { /** * Memory instructions */ - const aligun32 = readU32(); - const align = aligun32.value; - eatBytes(aligun32.nextIndex); - dump([align], "align"); + if ( + instruction.name === "grow_memory" || + instruction.name === "current_memory" + ) { + const indexU32 = readU32(); + const index = indexU32.value; + eatBytes(indexU32.nextIndex); + + if (index !== 0) { + throw new Error("zero flag expected"); + } + + dump([index], "index"); + } else { + const aligun32 = readU32(); + const align = aligun32.value; + eatBytes(aligun32.nextIndex); - const offsetu32 = readU32(); - const offset = offsetu32.value; - eatBytes(offsetu32.nextIndex); + dump([align], "align"); - dump([offset], "offset"); + const offsetu32 = readU32(); + const offset = offsetu32.value; + eatBytes(offsetu32.nextIndex); + + dump([offset], "offset"); + } } else if (instructionByte >= 0x41 && instructionByte <= 0x44) { /** * Numeric instructions