Skip to content

wasm2js: Fix memory.grow semantics and return -1 on failure - #9223

Open
tlively wants to merge 1 commit into
mainfrom
wasm2js-memory-grow-fix
Open

tlively wants to merge 1 commit into
mainfrom
wasm2js-memory-grow-fix

Conversation

@tlively

@tlively tlively commented Oct 6, 2026

Copy link
Copy Markdown
Member

Previously, visitMemoryGrow checked max > initial and emitted wasm2js_trap()
when false. This incorrectly dropped the delta expression's side effects and
caused memory.grow to trap at runtime, which is invalid WebAssembly semantics.
WebAssembly memory.grow never traps.

Unconditionally emit the WASM_MEMORY_GROW call in visitMemoryGrow, and add
needsMemoryGrow to emit the __wasm_memory_grow helper when memory.grow is used
in the module or when growable memory is imported or exported.

Also, when memory.grow fails at runtime (for example when exceeding declared
memory maximum or passing invalid delta operands), return -1 instead of the
previous memory size. Also ensure growing by 0 pages succeeds and returns the
previous memory size without reallocating the buffer.

Previously, visitMemoryGrow checked `max > initial` and emitted `wasm2js_trap()`
when false. This incorrectly dropped the delta expression's side effects and
caused memory.grow to trap at runtime, which is invalid WebAssembly semantics.
WebAssembly memory.grow never traps.

Unconditionally emit the WASM_MEMORY_GROW call in visitMemoryGrow, and add
needsMemoryGrow to emit the __wasm_memory_grow helper when memory.grow is used
in the module or when growable memory is imported or exported.

Also, when memory.grow fails at runtime (for example when exceeding declared
memory maximum or passing invalid delta operands), return -1 instead of the
previous memory size. Also ensure growing by 0 pages succeeds and returns the
previous memory size without reallocating the buffer.
@tlively
tlively requested a review from a team as a code owner October 6, 2026 23:39
@tlively
tlively requested review from aheejin and stevenfontanella and removed request for a team October 6, 2026 23:39

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant