Skip to content

Skip zero-byte copies when restoring a startup snapshot - #8590

Merged
littledivy merged 3 commits into
mainfrom
littledivy/2026-11-18-v8-snapshot-empty-typed-array
Nov 18, 2026
Merged

littledivy merged 3 commits into
mainfrom
littledivy/2026-11-18-v8-snapshot-empty-typed-array

Conversation

@littledivy

@littledivy littledivy commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

Blocks #8521: with Rust resources in the startup snapshot, node:http Workers now boot from the snapshot, and the asan job fails in http-client-ts@all-autogates:

v8/src/snapshot/snapshot-source-sink.h:61:12: runtime error: null pointer passed as argument 1, which is declared to never be null

V8 writes an empty typed array's off-heap backing store into the snapshot (the ArrayBuffer path skips empty ones). On restore, BackingStore::Allocate(0) gives a null data pointer, so SnapshotByteSource::CopyRaw() calls memcpy(nullptr, src, 0). Any Worker whose top level keeps one, like new Uint8Array(new ArrayBuffer(0)), hits it.

The V8 patch makes CopyRaw() return early for zero bytes, and startup-snapshot-globals-test gets a case that keeps an empty and a non-empty typed array across the snapshot.

A Worker's top level that keeps an empty typed array over an ArrayBuffer of its own, such as `new Uint8Array(new ArrayBuffer(0))`, fails UBSan when a Worker starts from the snapshot. V8's serializer writes the off-heap backing store of a typed array even when it is empty, and on restore `BackingStore::Allocate()` gives a zero-byte store whose data pointer is null, so `SnapshotByteSource::CopyRaw()` calls `memcpy()` with a null destination.

In this patch a V8 patch makes `CopyRaw()` return early for zero bytes, which covers every caller, and `startup-snapshot-globals-test` gets a case whose top level keeps an empty and a non-empty typed array across the snapshot.
@littledivy
littledivy requested review from a team as code owners November 18, 2026 05:54

@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 949e1f61.

This review, of 95915a64

✅ Approved — A small, well-scoped V8 patch that skips zero-byte copies, plus a test that round-trips empty and non-empty typed arrays through the snapshot. No issues found.

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


✅ All checks passed

📋 Code Quality: ✅ LGTM

The early return for number_of_bytes == 0 in CopyRaw() sits after the bounds DCHECK_LE and before memcpy(), so the null-destination memcpy() cannot happen. The patch follows the layout of the other V8 patches and is registered in order in build/deps/v8.MODULE.bazel. The new empty-typed-array test case is wired the same way in the .js file and the .wd-test config.


📝 Documentation: ✅ LGTM

The patch's commit message states the root cause and the reason for the fix clearly. The code comment describes the code as it is now.


Reviewed commit: 95915a64

@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 37bf1ca2.

This review, of 949e1f61

✅ Approved — A small, well-scoped V8 patch that skips zero-byte copies, plus a test that round-trips empty and non-empty typed arrays through the snapshot. No issues found.

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


✅ All checks passed

📋 Code Quality: ✅ LGTM

The early return for number_of_bytes == 0 in CopyRaw() sits after the bounds DCHECK_LE and before memcpy(), so the null-destination memcpy() cannot happen. The patch follows the layout of the other V8 patches and is registered in order in build/deps/v8.MODULE.bazel. The new empty-typed-array test case is wired the same way in the .js file and the .wd-test config.


📝 Documentation: ✅ LGTM

The patch's commit message states the root cause and the reason for the fix clearly. The code comment describes the code as it is now.


Reviewed commit: 949e1f61 — same changes as 95915a64, so this is that commit's review (no new review was run; line numbers are as of 95915a64)

…-v8-snapshot-empty-typed-array

# Conflicts:
#	src/workerd/api/tests/startup-snapshot-globals-test.js

@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 — This is a small, well-scoped V8 patch that skips zero-byte copies in CopyRaw(), plus a test that keeps an empty and a non-empty typed array across the snapshot. The merge-conflict resolution since the last review keeps the test case intact, and there are no findings.

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


✅ All checks passed

📋 Code Quality: ✅ LGTM

The merge-conflict resolution in src/workerd/api/tests/startup-snapshot-globals-test.js keeps the empty-typed-array case working:

  • The MODE case, the typedArrays report field and the assertion handler are all still there.
  • deepStrictEqual is imported.
  • The .wd-test file defines both the binding and the worker for the case.

The V8 patch adds an early return for zero bytes after the DCHECK_LE bounds check and before memcpy(), so memcpy() is never called with a null destination. The patch is listed in order in build/deps/v8.MODULE.bazel.


📝 Documentation: ✅ LGTM

The patch's commit message explains the root cause and the fix. The code comments describe the code as it is now.


Reviewed commit: 37bf1ca2

@erikcorry
erikcorry self-requested a review November 18, 2026 12:53
@littledivy
littledivy merged commit 6ee5f17 into main Nov 18, 2026
36 of 38 checks passed
@littledivy
littledivy deleted the littledivy/2026-11-18-v8-snapshot-empty-typed-array branch November 18, 2026 12:59
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.

3 participants