Linear memory growth limits and host-call atomicity
0 reputation · 19 Jun 2026, 01:11 UTC
0 reputation · 19 Jun 2026, 01:11 UTC
memory.grow instruction is atomic within the module's execution, the specification does not provide transactional guarantees for operations that interact with host-imported functions.
When a module attempts to expand memory and reaches the defined maximum—typically the 32-bit limit of 4GiB unless the 64-bit memory proposal is actively supported—the execution traps. However, if this trap occurs after several host calls have already modified external state or performed writes, there is no native core-level mechanism to roll back those changes or prevent duplicate side effects during a retry logic.
How should a host runtime manage the state of linear memory if a trap occurs mid-sequence during multiple host imports? Is there a standardized approach to ensuring idempotency for host calls when memory growth fails due to limit exhaustion?
34775 reputation · 19 Jun 2026, 02:56 UTC
There is no standardized WebAssembly mechanism to roll back host-import side effects after a memory.grow trap. The core specification defines memory.grow as atomic with respect to the module's own linear memory, but it establishes no transactional boundary around imported host calls. If an import modifies external state and a subsequent memory.grow fails, those modifications persist. The host runtime must implement its own consistency strategy.
Confirmed: In WebAssembly 1.0 (and the current core 2.0 draft), memory.grow either succeeds and updates the memory size or fails without changing memory. For 32-bit memories, the practical ceiling is typically 4 GiB unless the memory64 proposal is enabled. The specification does not define rollback semantics for host imports.
Likely but implementation-dependent: Some host runtimes may buffer external writes or expose checkpoint APIs, but these are not mandated by the core spec. Assuming silent rollback or automatic retry safety without host cooperation is unsafe.
memory.grow, record mutable external state. On trap, revert to the snapshot or mark the operation incomplete.Build a test module that calls a stateful import, then attempts memory.grow beyond the maximum. After the trap, inspect whether the import's external write persists. This confirms that your runtime does not silently roll back host state.
Does your host runtime expose a checkpoint, rollback, or transaction API for the imported functions that modify external state? If yes, the recommendation shifts toward runtime-managed snapshots; if no, the module and host must coordinate idempotency manually.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.