Skip to content

fix: keep copies of resizable and growable buffers inside their wasm block - #242

Merged
toyobayashi merged 1 commit into
toyobayashi:mainfrom
Brooooooklyn:fix/resizable-arraybuffer-copy
Oct 15, 2026
Merged

toyobayashi merged 1 commit into
toyobayashi:mainfrom
Brooooooklyn:fix/resizable-arraybuffer-copy

Conversation

@Brooooooklyn

Copy link
Copy Markdown
Contributor

Problem

emnapi copies a JS ArrayBuffer into a malloc'd wasm block the first time native code asks for its data. The block has the byteLength of that time. A resizable ArrayBuffer or a growable SharedArrayBuffer can get longer later. Then:

  • the next napi_get_arraybuffer_info / napi_get_typedarray_info / napi_get_dataview_info copied the whole new length into the old block, and reported the new length;
  • emnapi_sync_memory used 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::sync on a length-tracking view).

first get-info            buffer grows            next get-info (f96c47eb)
JS  [aaaa]                JS  [aaaaaaaa]          wasm [aaaa|aaaa  <- next chunk overwritten
wasm [aaaa|next chunk]    wasm [aaaa|next chunk]

Change

One commit, fix: keep copies of resizable and growable buffers inside their wasm block.

memory.ts

  • ArrayBufferPointer has 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 (cached shouldCopy path):
    • With a FinalizationRegistry (kRuntime): when the buffer is longer than its block, it copies into a new block. The new block is twice the old one, so a buffer that grows in small steps keeps few blocks. The size is no bigger than maxByteLength, no bigger than 0x7f000000, and never smaller than the current length. When malloc returns 0, it retries with the exact size.
    • Without a FinalizationRegistry (kUserland): 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.
    • The old block is not freed early, because native code or a v8::BackingStore may still use it. kRuntime blocks stay registered and are freed when the ArrayBuffer is collected.
    • copied is 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.
  • maxByteLength is read through the intrinsic getters captured at init (bufferMaxByteLength). There is no limit when the getters are missing, or when the SharedArrayBuffer global was hidden at init.
  • The first copy copies exactly the byteLength it read. Another thread can grow a growable SharedArrayBuffer after the read.
  • New copyBytesFrom(arrayBuffer, byteOffset): the number of bytes of emnapi's copy from byteOffset. It is Infinity when emnapi did not make the copy.

emnapi.ts (emnapi_sync_memory)

  • The default length stops at the end of the copy.
  • A longer explicit length throws a RangeError.

convert2c.ts, v8/array_buffer.ts

  • 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/array_buffer.cc

  • v8::ArrayBufferView::CopyContents copies no more than store->ByteLength() - offset.

Buffers that emnapi creates are fixed-length. External buffers have no capacity. So nothing changes for them.

Behavior changes

Case Before After
Get-info after a grow writes past the block new block
emnapi_sync_memory with an explicit length longer than the copy reads/writes past the block RangeError
Sync after shrink → get-info → regrow (no get-info after the regrow) copies the dropped bytes back stops at the shrunk length, both ways
kUserland (no FinalizationRegistry), get-info after a regrow the same block: too small, and maybe freed already a new exact-size block. Native code gets more blocks to own: 9 instead of 1 for a 16-step grow/shrink sequence

Known limits

These limits were already there on f96c47e. This PR does not change them:

  • Without a FinalizationRegistry, a get-info on a buffer that did not grow returns the same block again. Native code may already have freed it.
  • A shrink and a regrow with no get-info in between are not visible to emnapi. A sync then uses the old copied.
  • 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). The exact-size retry does not run there.
  • The v8 view helpers read byteLength / byteOffset as 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 (+ syncMemory and dirtyFreedBlock in binding.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:
    • sync with the default length and with an explicit length
    • sync after the buffer grew into the rest of a bigger block
    • sync after a shrink and a grow back
    • that the old block is kept
  • 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): CopyContents on a view whose buffer grows after GetBackingStore made 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.yml job, with its own commands, on this commit and on f96c47e:

Lane Result
test: emscripten, wasm64, wasip1, wasip1-threads, wasm32 pass (full suite: 152 / 152 / 70 / 152 / 70 files)
test with 4GB memory pass
libemnapi-basic-mt pass
napi-versions, package-tests pass
browser (vitest, wasm32) pass; the userland files skip, as they should
eslint on memory.ts clean

The only change from f96c47e is the new tests, which pass.

Downstream: the napi-rs examples/napi threaded 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.

Finding Verified Outcome
The block size is used as the copied length, so a sync after a 4 → 5 → 8 grow copies old heap bytes into JS Yes Fixed: the new copied field
CopyContents can read past its BackingStore after another thread grows the buffer Yes Fixed: bound by store->ByteLength() - offset
CopyContents helpers read spoofable properties Yes, also on f96c47e Left as is (known limit)
The exact-size retry cannot run with ABORTING_MALLOC Yes Documented. Always exact size aborts sooner on small steps, and a fixed cap breaks large growth
copied never goes down, so shrink → regrow → sync brings back the dropped bytes Yes. The same bytes come back on f96c47e Fixed: copied is set on every cached get-info
Without a FinalizationRegistry, a growth inside the doubled block writes into a block that native code freed Yes. f96c47e fails one call earlier Fixed: a kUserland block is never written again

🤖 Generated with Claude Code

…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>
@toyobayashi
toyobayashi merged commit 314b7b8 into toyobayashi:main Oct 15, 2026
23 checks passed
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.

2 participants