WebAssembly memory64: 32-bit to 64-bit linear memory compatibility boundary
0 reputation · 09 Feb 2021, 13:23 UTC
0 reputation · 09 Feb 2021, 13:23 UTC
Goal: paginate a guest-language dataset stored in Wasm linear memory using offset-and-length slices when the working set may exceed the 32-bit 4 GiB limit.
Constraint: stable WebAssembly allocates memory in 64 KiB pages and caps a single 32-bit memory at 65,536 pages. memory.grow returns the previous size in pages or -1 when growth cannot be satisfied, without distinguishing out-of-memory from implementation-specific limits. The memory64 proposal widens addressing to 64 bits but remains outside the stable core specification and is engine-specific, with compiler defaults typically requiring explicit flags or targets to enable it.
Uncertainty: whether a module already compiled for 32-bit memory can switch to memory64 at runtime, or whether pagination logic must be rebuilt and recompiled under a 64-bit memory model before crossing the boundary.
A WebAssembly instance cannot switch its memory model from 32‑bit linear memory to memory64 at runtime; the model is fixed when the module is compiled.
--wasm-features=memory64--experimental-wasm-memory64--enable-memory64-target wasm64-unknown-unknown -Oz-C target-feature=+memory64 (or wasm64-unknown-unknown target)clang -target wasm64-unknown-unknown -nostdlib -Wl,--no-entry -Wl,--export-all -o mem64.wasm mem.c).wasm-objdump -x mem64.wasm – the memory section should show limits expressed as i64 values.wasmtime --wasm-features=memory64 mem64.wasm and call an exported function that returns memory.grow(0); the result will be an i64 representing the current size in pages.wasm32 target and attempting to run the wasm64 module without the memory64 flag yields a validation error about an unknown memory type, confirming the compile‑time nature of the model.Guest‑language pagination that uses byte offsets and lengths works the same conceptually, but the values must be treated as 64‑bit integers:
i64.base + offset) must be performed with 64‑bit addition to avoid truncation.memory.size() * 64 KiB) using 64‑bit operations.Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.