diff --git a/packages/compiler/src/wasm/binary.ts b/packages/compiler/src/wasm/binary.ts index ad52977399..eaedf25af5 100644 --- a/packages/compiler/src/wasm/binary.ts +++ b/packages/compiler/src/wasm/binary.ts @@ -1245,7 +1245,7 @@ class FunctionValidator { /** A memarg. A memory other than 0 stops typing (multi-memory). */ memarg(naturalAlign: number): void { const r = this.r; - const { align, memory } = readMemargFlags(r); + const { align, memory } = readMemargFlags(() => r.u32()); if (align < 0) r.malformed("malformed memop flags"); if (memory !== 0) this.otherMemory(memory); if (this.m.memory64) r.u64(); @@ -1831,7 +1831,7 @@ class FunctionValidator { scanMemarg(): void { const r = this.r; - const { align, memory } = readMemargFlags(r); + const { align, memory } = readMemargFlags(() => r.u32()); if (align < 0) r.malformed("malformed memop flags"); if (memory !== 0) this.m.feature("multi-memory"); if (this.m.memory64) r.u64(); diff --git a/packages/compiler/src/wasm/module.ts b/packages/compiler/src/wasm/module.ts index d7ec387013..587a20d817 100644 --- a/packages/compiler/src/wasm/module.ts +++ b/packages/compiler/src/wasm/module.ts @@ -289,11 +289,12 @@ function decodeUtf8(bytes: Uint8Array, start: number, end: number): string { * 0; flags of 128 or more are malformed and answer align -1 without * reading further. The offset follows. Validation (binary.ts), translation * and skipImmediates all read memargs through this function, so they agree - * on where each instruction ends. */ -export function readMemargFlags(r: { u32(): number }): { align: number; memory: number } { - const flags = r.u32(); + * on where each instruction ends; `u32` reads the next u32 immediate of + * their own reader. */ +export function readMemargFlags(u32: () => number): { align: number; memory: number } { + const flags = u32(); if (flags >= 128) return { align: -1, memory: 0 }; - if (flags >= 64) return { align: flags - 64, memory: r.u32() }; + if (flags >= 64) return { align: flags - 64, memory: u32() }; return { align: flags, memory: 0 }; } @@ -386,7 +387,7 @@ export function skipImmediates(c: WasmCursor, op: number): void { } default: if (op >= 0x28 && op <= 0x3e) { - const flags = readMemargFlags(c); + const flags = readMemargFlags(() => c.u32()); if (flags.align < 0 || flags.memory !== 0) throw new Error(`wasm: memarg of memory ${flags.memory} is not supported`); c.u32(); diff --git a/packages/compiler/src/wasm/translate.ts b/packages/compiler/src/wasm/translate.ts index 813dab84a7..f17c5dae32 100644 --- a/packages/compiler/src/wasm/translate.ts +++ b/packages/compiler/src/wasm/translate.ts @@ -851,7 +851,7 @@ class FunctionTranslator { /** The offset of a memarg over memory 0 (the only memory a translatable * module has). */ memargOffset(c: WasmCursor): number { - const flags = readMemargFlags(c); + const flags = readMemargFlags(() => c.u32()); if (flags.align < 0 || flags.memory !== 0) throw new WasmTranslateError(`memarg of memory ${flags.memory} is not supported`); return c.u32();