Skip to content

Rust JSG: add SharedArrayBuffer handles and copy buffer bytes in C++ - #8943

Open
bartlomieju wants to merge 2 commits into
mainfrom
biwanczuk/rust-jsg-shared-array-buffer
Open

bartlomieju wants to merge 2 commits into
mainfrom
biwanczuk/rust-jsg-shared-array-buffer

Conversation

@bartlomieju

@bartlomieju bartlomieju commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

Rust JSG can tell a SharedArrayBuffer apart from other values but has
no handle type for one, so a Rust binding cannot read its bytes. C++
JSG accepts a SharedArrayBuffer, or a view over one, wherever it takes
a kj::Array, and Rust bindings that take byte arguments need to
do the same to behave like the C++ ones they replace, starting with
the Worker Loader. This adds Local with
byte_length() and to_vec(), which copies in C++. The to_vec() of an
ArrayBufferView and of an ArrayBuffer now copy in C++ too: a view's
buffer may be shared, V8 returns a view's SharedArrayBuffer from
buffer() as an ArrayBuffer, and forming a &[u8] over memory that
another agent can write to is a data race.

No binding takes a SharedArrayBuffer yet, so a Worker sees no change.
jsg-test covers to_vec() of a SharedArrayBuffer and of views over one:
at an offset, a DataView, and a length-tracking view over a growable
SharedArrayBuffer, and of a view's shared buffer.

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of 79de21bf.

This review, of 43f704f6

💬 Approved with comments — The copy shims are correct and bounded by the destination length, and the tests cover shared, growable and view cases. There is one INFO note about a code comment whose stated reason is wrong.

Findings: 1 info (1 is an inline comment on the diff)

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


📝 Documentation: 💬 1 info

One comment gives the wrong reason why the copy-length assertion holds

Recommendation: 💬 Optional Nits

  • [INFO] The comment says a SharedArrayBuffer can't grow before the copy, but another agent can grow it concurrently. The assertion holds only because the copy is clamped to the output length. (src/rust/jsg/v8.rs:3215)
View full review
  • src/rust/jsg/v8.rs:3215: the comment says the shared buffer cannot grow because no JavaScript runs on this thread. Another agent can grow a growable SharedArrayBuffer at any time. The debug_assert_eq! holds for a different reason: the copy is clamped to bytes.len(), and a SharedArrayBuffer never shrinks.

✅ All other checks passed

🔒 Security: ✅ LGTM

Copying shared memory in C++ means Rust never holds a &[u8] over memory that another agent can write. Each copy is bounded by the size of the destination Vec.


📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

local_shared_array_buffer_copy_to and local_array_buffer_view_copy_to copy at most the destination length and skip memcpy when there is nothing to copy. Because viewBytes is reused, a detached or out-of-range view copies zero bytes. The new cast uses is_shared_array_buffer, and the tests confirm that an ArrayBuffer and a SharedArrayBuffer do not cast to each other. The tests cover a buffer that has grown, an empty buffer, a view at an offset, a DataView, a length-tracking view after grow, and a zero-length view.


⚡ Performance: ✅ LGTM

No performance concerns.


Reviewed commit: 43f704f6

Comment thread src/rust/jsg/v8.rs Resolved
Rust JSG could tell a SharedArrayBuffer apart from other values but
had no handle type for one, so Rust code could not read its bytes. C++
JSG takes a SharedArrayBuffer wherever it takes a kj::Array<byte>, and
the Rust Worker Loader needs to do the same for a module's data and
wasm fields.

This adds Local<SharedArrayBuffer>, which, as in V8, is not a
Local<ArrayBuffer>, with byte_length() and to_vec(). to_vec() copies
the bytes in C++, so that Rust never holds a reference to memory that
another agent may write to.
ArrayBufferView::to_vec() and ArrayBuffer::to_vec() formed a &[u8]
into the buffer and copied from it. The buffer may be a
SharedArrayBuffer, which another agent may write to at any time, so
that slice was formally a data race, which
Local<SharedArrayBuffer>::to_vec() already avoids by copying in C++. A
Local<ArrayBuffer> can be shared too: as in V8, a view's buffer() returns
a SharedArrayBuffer as an ArrayBuffer. The Rust Worker Loader takes a
module's bytes from a view over a SharedArrayBuffer through this path.

Both to_vec()s now copy in C++ as well, so they never borrow the bytes.
The README, buffer()'s docs and the docs of as_slice() on ArrayBuffer,
ArrayBufferView and the typed arrays say that as_slice() does borrow
and so suits only a buffer that isn't shared. jsg-tests pin to_vec() of
a view at an offset, a DataView, a length-tracking view over a growable
SharedArrayBuffer, and a view's shared buffer.
@bartlomieju
bartlomieju force-pushed the biwanczuk/rust-jsg-shared-array-buffer branch from 43f704f to 79de21b Compare November 26, 2026 10:33
@bartlomieju bartlomieju changed the title Rust JSG: add SharedArrayBuffer handles and copy view bytes in C++ Rust JSG: add SharedArrayBuffer handles and copy buffer bytes in C++ Nov 26, 2026

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Approved — No open findings: the copies into shared memory are bounded by the destination and well tested, and the comment flagged last time has been fixed.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

Copying shared memory in C++ means Rust never holds a &[u8] over memory that another agent may write. Every copy is bounded by the size of the destination.


📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

Each C++ copy shim copies at most min(source length, out.size()) bytes and skips memcpy when that is zero. Each Rust to_vec() sizes the Vec before the copy, so a SharedArrayBuffer that grows concurrently cannot overrun it. The new cast uses is_shared_array_buffer, and the tests cover the shared, growable, offset, DataView and empty cases.


⚡ Performance: ✅ LGTM

No performance concerns.


📝 Documentation: ✅ LGTM

The to_vec() comments now give the right reason the assertion holds: the copy stops at bytes.len(), and a SharedArrayBuffer never shrinks. The README's parameter-type list leaves out Local<SharedArrayBuffer>, which is correct, because it isn't a method parameter type yet.


Reviewed commit: 79de21bf

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