Repository navigation
fix: keep copies of resizable and growable buffers inside their wasm block - #242
Merged
toyobayashi merged 1 commit intoOct 15, 2026
Merged
Conversation
…block emnapi copies a JS ArrayBuffer into a malloc'd wasm block the first time native code asks for its data, sized to its byteLength at that time. A resizable ArrayBuffer or a growable SharedArrayBuffer can get longer later. The next get-info call then copied the whole new length into the old block and reported the new length, and emnapi_sync_memory used the new length too. This wrote past the block and corrupted the wasm heap (later free() traps, or the threads build hangs). - getArrayBufferPointer keeps the block size (capacity). When the buffer is longer than its block (kRuntime) or its copy (kUserland, see below), it copies into a new block of at least the current length. The old block is not freed early, because native code or a v8::BackingStore may still use it. With a FinalizationRegistry (kRuntime) every block stays registered and is freed when the ArrayBuffer is collected. Without one (kUserland) emnapi never frees a block, and get-info gives native code only the address of the newest one. Every copy uses an exact length. - With a FinalizationRegistry the new block is twice the old one, so a buffer that grows in small steps keeps few blocks. It is no bigger than the buffer's maxByteLength (read through the intrinsic getters; no limit when they are missing, or when the SharedArrayBuffer global was hidden at init), no bigger than 0x7f000000 (wasm32 sbrk takes a signed size and emnapi's emmalloc traps on a request near 2 GiB), and never smaller than the current length. When malloc returns 0, it retries with the exact size; this runs in emscripten builds with memory growth and in the core builds. Near the 4 GiB end of the heap, the emmalloc used on wasm32-unknown-unknown can return a bad non-zero pointer instead of 0 (its fit check wraps), so the retry does not run there. With ABORTING_MALLOC (on by default in emscripten builds without memory growth) malloc aborts instead, so a growth whose bigger block does not fit aborts even if the exact size would fit. - Without a FinalizationRegistry, native code owns the block and may have freed it, so emnapi never writes into it again: a buffer longer than its copy gets a new block of exactly its length. - It also keeps how many bytes at the start of the block hold the buffer's data (copied). The rest of a doubled block is room to grow into and holds old heap data. A get-info call after a shrink lowers it, so sync does not bring back bytes the shrink dropped. - emnapi_sync_memory never copies more than the copy holds: the default length stops at the end of the copy, and a longer explicit length is a RangeError. - napi_get_arraybuffer_info and v8::ArrayBuffer::GetBackingStore read the length before the copy, so a SharedArrayBuffer grown by another thread in between cannot report more than was copied. v8::ArrayBufferView::CopyContents copies no more than the backing store holds. Buffers emnapi creates are fixed-length, and external buffers have no capacity, so nothing changes for them. Known limit, as before: without a FinalizationRegistry, a get-info call on a buffer that did not grow returns the same block again, which native code may already have freed. Tests: buffer.test.js grows a resizable ArrayBuffer and a growable SharedArrayBuffer (through the buffer and length-tracking, DataView and fixed-length views) after the first copy and checks the data and that the Buffers allocated after the copy are intact; it also covers sync with the default and an explicit length, sync after the buffer grew into the rest of a bigger block, sync after a shrink and a grow back, and that the old block is kept. buffer_userland.test.js and typedarray_userland.test.js run without a FinalizationRegistry: the shrink case, and typedarray Multiply (which frees its copies) on a buffer that grows. sharedarraybuffer.test.js has native code write a grown buffer and sync it back. v8_array_buffer.test.js calls CopyContents on a view whose buffer grows after GetBackingStore made its copy. The buffer and userland tests fail on the previous code on wasm32-wasip1, wasm32-wasip1-threads, wasm32 and wasm64 emscripten and wasm32-unknown-unknown; the v8 test fails on wasm32-wasip1-threads and wasm32 and wasm64 emscripten. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
emnapi copies a JS
ArrayBufferinto a malloc'd wasm block the first time native code asks for its data. The block has thebyteLengthof that time. A resizableArrayBufferor a growableSharedArrayBuffercan get longer later. Then:napi_get_arraybuffer_info/napi_get_typedarray_info/napi_get_dataview_infocopied the whole new length into the old block, and reported the new length;emnapi_sync_memoryused the new length too.Both write past the block and corrupt the wasm heap. A later
free()traps (RuntimeError: unreachable/Aborted(native code called abort())), or the threads build hangs. A probe on f96c47e shows the head of the next heap chunk overwritten with the buffer's bytes (0x77777777) after one grow.napi-rs found this in napi-rs/napi-rs#3607 (
TypedArray::syncon a length-tracking view).Change
One commit,
fix: keep copies of resizable and growable buffers inside their wasm block.memory.ts
ArrayBufferPointerhas two new fields:capacity: the size of the block emnapi malloc'd.copied: how many bytes at the start of the block hold the buffer's data. The rest of a bigger block is room to grow into, and holds old heap data.getArrayBufferPointer(cachedshouldCopypath):maxByteLength, no bigger than0x7f000000, and never smaller than the current length. When malloc returns 0, it retries with the exact size.v8::BackingStoremay still use it. kRuntime blocks stay registered and are freed when the ArrayBuffer is collected.copiedis set to the current length on every cached get-info. After a shrink, a sync does not bring back the bytes that the shrink dropped.maxByteLengthis read through the intrinsic getters captured at init (bufferMaxByteLength). There is no limit when the getters are missing, or when theSharedArrayBufferglobal was hidden at init.byteLengthit read. Another thread can grow a growableSharedArrayBufferafter the read.copyBytesFrom(arrayBuffer, byteOffset): the number of bytes of emnapi's copy frombyteOffset. It isInfinitywhen emnapi did not make the copy.emnapi.ts (
emnapi_sync_memory)RangeError.convert2c.ts, v8/array_buffer.ts
napi_get_arraybuffer_infoandv8::ArrayBuffer::GetBackingStoreread the length before the copy. So aSharedArrayBuffergrown by another thread in between cannot report more than was copied.v8/array_buffer.cc
v8::ArrayBufferView::CopyContentscopies no more thanstore->ByteLength() - offset.Buffers that emnapi creates are fixed-length. External buffers have no
capacity. So nothing changes for them.Behavior changes
emnapi_sync_memorywith an explicit length longer than the copyRangeErrorKnown limits
These limits were already there on f96c47e. This PR does not change them:
copied.wasm32-unknown-unknowncan return a bad non-zero pointer instead of 0 (its fit check wraps). The exact-size retry does not run there.byteLength/byteOffsetas ordinary properties, so own properties can spoof them.One new limit comes from the doubling: with
ABORTING_MALLOC(on by default in emscripten builds without memory growth), malloc aborts instead of returning 0. A growth whose doubled block does not fit therefore aborts, even when the exact size would fit. I kept the doubling there. Exact-size blocks abort sooner on a buffer that grows in small steps, because every kRuntime block stays until GC.Tests
buffer/buffer.test.js(+syncMemoryanddirtyFreedBlockinbinding.c): grows a resizable ArrayBuffer and a growable SharedArrayBuffer after the first copy, through the buffer and through length-tracking, DataView and fixed-length views. It checks the data, and that Buffers allocated after the copy are intact. It also covers:buffer/buffer_userland.test.js(new): the shrink case without a FinalizationRegistry.typedarray/typedarray_userland.test.js(new):typedarray.Multiply(which frees its copies) without a FinalizationRegistry, on a buffer that grows to 4, 5 and 8 bytes.sharedarraybuffer/sharedarraybuffer.test.js: native code writes a grown buffer and syncs it back.v8_array_buffer(new test target):CopyContentson a view whose buffer grows afterGetBackingStoremade its copy.The buffer and userland tests fail on f96c47e on wasm32-wasip1, wasm32-wasip1-threads, wasm32 and wasm64 emscripten, and wasm32-unknown-unknown. The v8 test fails on wasm32-wasip1-threads and on wasm32 and wasm64 emscripten.
Verification
Each
ci.ymljob, with its own commands, on this commit and on f96c47e:test: emscripten, wasm64, wasip1, wasip1-threads, wasm32testwith 4GB memorynapi-versions,package-testseslintonmemory.tsThe only change from f96c47e is the new tests, which pass.
Downstream: the napi-rs
examples/napithreaded WASI suite gives 359 pass / 33 skip with this build, the same as with f96c47e. The heap-corruption probes from napi-rs/napi-rs#3607 pass: 16 of 16 (f96c47e: 15 of 16, and the 16th hangs or exits 1).Local toolchain: node 24.22.0, emcc 6.0.10-git, wasi-sdk 32, macOS arm64.
Review rounds
Each finding of an adversarial review was reproduced before I acted on it.
copiedfieldCopyContentscan read past its BackingStore after another thread grows the bufferstore->ByteLength() - offsetCopyContentshelpers read spoofable propertiesABORTING_MALLOCcopiednever goes down, so shrink → regrow → sync brings back the dropped bytescopiedis set on every cached get-info🤖 Generated with Claude Code