Skip to content

Carry Rust JSG resources through the startup snapshot - #8521

Merged
littledivy merged 7 commits into
mainfrom
littledivy/2026-11-16-audit-rjsg-autogates
Nov 18, 2026
Merged

littledivy merged 7 commits into
mainfrom
littledivy/2026-11-16-audit-rjsg-autogates

Conversation

@littledivy

@littledivy littledivy commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

A Rust JSG resource could cross the startup snapshot only as a stateless type (#[jsg_resource(snapshot_stateless)]), recreated through its own clone hook. A resource with state, or one holding a jsg::Rc to another resource whose identity JavaScript can see, had no way through, so a Worker whose top level kept one started without the snapshot. The zygote also dropped the Rust resource templates before building the blob, so a restored resource had a different template from the new wrappers of its type.

In this patch:

  • jsg::SnapshotTransfer is the Rust counterpart of snapshotClone(), snapshotReferences() and snapshotRelink(). The zygote stores an isolate-independent Snapshot of the resource, and each Worker started from the snapshot rebuilds the resource from it with restore() and takes back the restored resources it refers to. snapshot_stateless now generates a SnapshotTransfer with an empty snapshot, so stateless and stateful types go through one snapshotClone() path, and GarbageCollected::snapshot_recreate is gone.
  • The zygote pins the Rust resource templates into the snapshot by TypeId, and a restored isolate fills its new Realm with them, so restored and new wrappers share their class.
  • snapshotRelink() runs with the new context entered, as a Rust Lock needs one.
  • jsg-test covers state, shared templates, relinked references and a type without SnapshotTransfer, and startup-snapshot-node-test covers node:url and node:dns in a Worker started from the snapshot.

@littledivy
littledivy requested review from a team as code owners November 17, 2026 05:52

@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 9b543dda.

This review, of 8d4628d2

💬 Approved with comments — No blocking or production-impact findings; one optional suggestion to add Send to the Snapshot bound and one documentation wording note.

Findings: 1 info · 1 suggestion (2 are inline comments on the diff)

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


📋 Code Quality: 💬 1 suggestion

Sound change; the Snapshot associated type requires Sync but not Send, although its docs say both.

Recommendation: 💬 Optional Nits

  • [SUGGESTION] SnapshotTransfer::Snapshot is bounded by Sync but not Send, while the docs say it is both (src/rust/jsg/snapshot.rs:55)
View full review
  • src/rust/jsg/snapshot.rs:55: the trait docs at lines 37-39 say the stored snapshot "is Send and Sync", but the bound is HoldsNoGcReferences + Sync + 'static, and Sync does not imply Send. The stored value is created on the zygote thread and shared by every Worker restored from the snapshot. It is dropped on whichever thread releases the last reference, so in practice it needs Send too. Adding Send would make the bound match the documented contract. The expected diagnostics in src/rust/jsg-compile-tests/snapshot-holds-no-gc-references.rs may then need updating.

📝 Documentation: 💬 1 info

One README sentence is ambiguous about who calls restore().

Recommendation: 💬 Optional Nits

  • [INFO] Clarify that each Worker started from the snapshot, not each thread, calls restore() (src/rust/jsg/README.md:729)
View full review
  • src/rust/jsg/README.md:729: "Each of those" reads as if it refers to the threads named just before it, so it suggests that each thread calls restore(). The call happens once per Worker.

✅ All other checks passed

🔒 Security: ✅ LGTM

No security issues found in this change.


📖 AGENTS.md: ✅ LGTM

No issues found on the changed lines.


⚡ Performance: ✅ LGTM

No performance issues found in this change.


Reviewed commit: 8d4628d2

Comment thread src/rust/jsg/README.md Resolved
Comment thread src/rust/jsg/snapshot.rs Resolved
@littledivy
littledivy force-pushed the littledivy/2026-11-16-audit-rjsg-autogates branch from 8d4628d to 9b543dd Compare November 17, 2026 06:01

@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 57caf083.

This review, of 9b543dda

💬 Approved with comments — No blocking findings and no production impact. One WARNING: the SnapshotTransfer doc comment says a panic makes the Worker fall back or fail to start, but the code aborts the process.

Findings: 1 warning (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.

🔧 Fix with your agent

Paste the following into your coding agent to investigate and fix these issues.

Fix the following code review issues found in pull request #8521.

Before making changes, review each issue and present a brief summary table:
- For each finding, state whether you agree, disagree, or need clarification
- If you disagree (e.g., the fix would require disproportionate changes
  for minimal ROI, or the finding is factually incorrect), explain why
- If you need to clarify something before deciding, ask those questions
- Then share your plan for which issues to tackle and in what order

## WARNING (1)

### [Code Quality] `SnapshotTransfer` docs say a panic falls back to a plain start or fails one Worker, but the FFI entry points have no `catch_unwind`, so a panic aborts the process.
- **File:** src/rust/jsg/snapshot.rs:50

📋 Code Quality: ⚠️ 1 warning

The earlier Send bound note is fixed. The SnapshotTransfer doc comment describes a panic outcome the code does not produce.

Recommendation: 📝 Minor Comments

  • [WARNING] SnapshotTransfer docs say a panic falls back to a plain start or fails one Worker, but the FFI entry points have no catch_unwind, so a panic aborts the process. (src/rust/jsg/snapshot.rs:50)
View full review

The earlier note about the Send bound is fixed: src/rust/jsg/snapshot.rs:55 now reads HoldsNoGcReferences + Send + Sync + 'static.

What the doc comment says a panic does is not what happens (src/rust/jsg/snapshot.rs:50-52)

The doc comment says a panic in the zygote makes the Worker start without the snapshot. It says a panic in a restored Worker makes that Worker fail to start.

The code does neither:

  • wrappable_invoke_snapshot_clone, wrappable_invoke_snapshot_references and wrappable_invoke_snapshot_relink (src/rust/jsg/v8.rs:4987-5016) are plain cxx extern "Rust" functions with no catch_unwind.
  • restore() also runs through wrappable_invoke_snapshot_clone, via snapshot_copy (src/rust/jsg/snapshot.rs:180).

So a panic in snapshot(), restore(), snapshot_references() or snapshot_relink() reaches the cxx shim and aborts the process.

No resource in this pull request panics.


✅ All other checks passed

🔒 Security: ✅ LGTM

No security issues found in this change.


📖 AGENTS.md: ✅ LGTM

No issues found on the changed lines.


⚡ Performance: ✅ LGTM

The new work runs once when the snapshot is built and once per Worker start, not on any request path. Pinning and restoring the templates takes time linear in the number of templates.


📝 Documentation: ✅ LGTM

The README now says that each Worker started from the snapshot calls restore() (src/rust/jsg/README.md:736).


Reviewed commit: 9b543dda

Comment thread src/rust/jsg/snapshot.rs Resolved
@littledivy
littledivy force-pushed the littledivy/2026-11-16-audit-rjsg-autogates branch from 9b543dd to 57caf08 Compare November 17, 2026 06:06

@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 f73465ce.

This review, of 57caf083

✅ Approved — The earlier WARNING about the panic behaviour in the SnapshotTransfer docs is fixed, and the change to snapshot.rs raises no new issues.

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

No security issues found in this change.


📖 AGENTS.md: ✅ LGTM

No issues found on the changed lines.


📋 Code Quality: ✅ LGTM

src/rust/jsg/snapshot.rs:48-50 now says that none of the SnapshotTransfer methods may panic and that a panic aborts the process. That matches wrappable_invoke_snapshot_clone, wrappable_invoke_snapshot_references and wrappable_invoke_snapshot_relink in src/rust/jsg/v8.rs, which are cxx extern "Rust" functions with no catch_unwind.


⚡ Performance: ✅ LGTM

The new work in src/rust/jsg/snapshot.rs runs once when the zygote builds the snapshot and once per Worker started from it, not on any request path. Its cost is linear in the number of resources carried.


📝 Documentation: ✅ LGTM

No documentation issues found.


Reviewed commit: 57caf083

A Worker's top level that imports `node:url`, `node:dns` or `node:fs`, or touches `process.stdout`, leaves a Rust JSG resource such as `UrlUtil` reachable. Rust resources had no `snapshotClone()`, so the zygote rejected the snapshot and these Workers always started without one. The zygote also dropped the Rust resource templates before building the blob, so a carried resource would have had a different template from the new wrappers of its type.

In this patch:

- `jsg::SnapshotTransfer` is the Rust counterpart of `snapshotClone()`, `snapshotReferences()` and `snapshotRelink()`. The zygote stores an isolate-independent `Snapshot` of the resource, and each Worker started from the snapshot rebuilds the resource from it with `restore()` and takes back the restored resources it refers to. `UrlUtil`, `DnsUtil` and `InspectorModule` implement it.
- The zygote pins the Rust resource templates into the snapshot by `TypeId`, and a restored isolate fills its new `Realm` with them, so restored and new wrappers share their class.
- `startup-snapshot-node-test` covers `node:url` and `node:dns`, and the `node:fs` and `process.stdout` cases now start from the snapshot. The node test reads `startedFromStartupSnapshot()` at request time, since its `typeof env.SELF` check was false whether or not the Worker was restored.
@littledivy
littledivy force-pushed the littledivy/2026-11-16-audit-rjsg-autogates branch from 57caf08 to f73465c Compare November 17, 2026 06:28

@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 d5cfdf1b.

This review, of f73465ce

✅ Approved — No issues found. The only change since the last approved review is a compile-fail test, and it checks the intended HoldsNoGcReferences bound.

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

The only change since the last review is a compile-fail test, and it has no security impact.


📖 AGENTS.md: ✅ LGTM

No issues found on the changed lines.


📋 Code Quality: ✅ LGTM

src/rust/jsg-compile-tests/snapshot-holds-no-gc-references.rs expects one E0277 on the type Snapshot line, where rustc reports the unmet HoldsNoGcReferences bound. MaybeHoldsReferences is Send and Sync, so the test fails for the intended reason. The panic wording fixed in src/rust/jsg/snapshot.rs is still correct.


⚡ Performance: ✅ LGTM

The changed file is a compile-time fixture and never runs. The snapshot work runs once when the zygote builds the snapshot and once per restored Worker. It is not on the request path.


📝 Documentation: ✅ LGTM

No documentation issues found.


Reviewed commit: f73465ce

@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 cb73f6de.

This review, of d5cfdf1b

✅ Approved — No issues found. The only change since the last approved review is a compile-fail test, and it checks the intended HoldsNoGcReferences bound.

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

The only change since the last review is a compile-fail test, and it has no security impact.


📖 AGENTS.md: ✅ LGTM

No issues found on the changed lines.


📋 Code Quality: ✅ LGTM

src/rust/jsg-compile-tests/snapshot-holds-no-gc-references.rs expects one E0277 on the type Snapshot line, where rustc reports the unmet HoldsNoGcReferences bound. MaybeHoldsReferences is Send and Sync, so the test fails for the intended reason. The panic wording fixed in src/rust/jsg/snapshot.rs is still correct.


⚡ Performance: ✅ LGTM

The changed file is a compile-time fixture and never runs. The snapshot work runs once when the zygote builds the snapshot and once per restored Worker. It is not on the request path.


📝 Documentation: ✅ LGTM

No documentation issues found.


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

@jasnell

jasnell commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator

Looks ok to me but I'm not yet 100% up to speed on the snapshot work (still reviewing it all myself) so let's get either @dcarney-cf or @erikcorry to stamp?

@guybedford guybedford left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Approving, conditional on making ErasedSnapshotTransfer::snapshot_relink an unsafe fn (inline).

Comment thread src/rust/jsg/snapshot.rs Resolved
ErasedSnapshotTransfer::snapshot_relink reads each entry of `restored` as the address of a Wrappable, so safe code could call it with any address and crash the process.

In this patch it is an `unsafe fn` whose caller guarantees each entry is 0 or a live Wrappable of the lock's isolate, and a compile test checks that safe code can't call it.

@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 1f5fafa5.

This review, of cb73f6de

✅ Approved — No issues found. The only change since the last approved review makes snapshot_relink an unsafe fn and adds a compile-fail test for it, as the approver asked.

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

Safe code outside the crate can no longer reach the raw Wrappable dereference in RestoredReferences::next. The unsafe API problem raised on the pull request is fixed.


📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

snapshot_relink is now an unsafe fn with a # Safety section, and its only caller in v8.rs calls it inside an unsafe block with a justification. The new compile-fail test expects E0133, the error for calling an unsafe fn outside unsafe.


⚡ Performance: ✅ LGTM

The snapshot work runs once in the zygote and once per restored Worker, so it is not on the request path.


📝 Documentation: ✅ LGTM

No documentation issues found.


Reviewed commit: cb73f6de

…-audit-rjsg-autogates

# Conflicts:
#	src/rust/jsg/ffi.h

@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 2677ef5e.

This review, of 1f5fafa5

✅ Approved — No issues found. The only changes since the last approved review are the merge from main in ffi.h and v8.rs, and the unsafe fn change the approver asked for is still in place.

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

Safe code still cannot reach the raw Wrappable dereference, because snapshot_relink is an unsafe fn with a # Safety contract.


📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

No issues in src/rust/jsg/ffi.h or src/rust/jsg/v8.rs:

  • The three Wrappable snapshot overrides in ffi.h match the bridge declarations in v8.rs.
  • The impl Vec<Global> {} workaround could be removed because realm_take_resource_templates now returns Vec<ResourceTemplate>.
  • The transmute calls in split_type_id and join_type_id are kept in one place, with SAFETY comments.
  • wrappable_invoke_snapshot_relink calls the unsafe fn snapshot_relink inside an unsafe block with a justification comment.

⚡ Performance: ✅ LGTM

The snapshot work runs once in the zygote and once per restored Worker, so none of it is on the request path. The latest changes add no cost to hot paths.


📝 Documentation: ✅ LGTM

No documentation issues found.


Reviewed commit: 1f5fafa5

…-audit-rjsg-autogates

# Conflicts:
#	docs/startup-snapshot.md
#	src/rust/jsg-macros/resource.rs
#	src/rust/jsg/ffi.c++
#	src/rust/jsg/lib.rs
#	src/workerd/api/tests/startup-snapshot-globals-test.js
#	src/workerd/api/tests/startup-snapshot-node-case-test.js
#	src/workerd/api/tests/startup-snapshot-node-test.js
#	src/workerd/api/tests/startup-snapshot-node-test.wd-test
#	src/workerd/api/tests/startup-snapshot-wrappables-test.js
#	src/workerd/jsg/setup.h

@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 2c53a30b.

This review, of 2677ef5e

✅ Approved — No issues found. The changes merged from main keep the unsafe fn contract on snapshot_relink and leave the template restore and relink paths sound.

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

  • Safe code still cannot reach the raw Wrappable dereference, because snapshot_relink is an unsafe fn with a # Safety contract.
  • Snapshot is bounded by HoldsNoGcReferences + Send + Sync + 'static, so it cannot hold a GC reference or a v8::Global.

📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

No correctness, test-coverage or convention problems.

  • snapshot_relink is still an unsafe fn, and a compile-fail test guards it.
  • Template restore runs right after realm_create, and restore_templates asserts that the template cache is empty.
  • The new startup_snapshot.rs tests cover state, shared templates, relinked references and a type without SnapshotTransfer. The rust-modules Worker in startup-snapshot-node-test covers node:url and node:dns.

⚡ Performance: ✅ LGTM

The snapshot work runs once in the zygote and once per restored Worker, so none of it is on the request path. Restoring templates costs time linear in the number of Rust resource types.


📝 Documentation: ✅ LGTM

No documentation issues found.


Reviewed commit: 2677ef5e

@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 cf5eed76.

This review, of 2c53a30b

✅ Approved — No issues found. The only change since the last review formats the case list in startup-snapshot-node-test.js. snapshot_relink is still an unsafe fn, as requested in the approving review.

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

  • Safe code cannot reach the raw Wrappable dereference. snapshot_relink is an unsafe fn, and it is called only from C++ with Wrappables from the same isolate.
  • The snapshot data is produced inside the process and never comes from untrusted input.
  • Snapshot is bounded by HoldsNoGcReferences, so it cannot hold a GC reference.

📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

The only change since the last review formats the case list in startup-snapshot-node-test.js.

  • ErasedSnapshotTransfer::snapshot_relink is still an unsafe fn with a # Safety section.
  • The compile-fail test erased-snapshot-relink-is-unsafe.rs guards that contract. The compile-tests target picks it up through glob(["*.rs"]).
  • Templates are restored right after realm_create. Pinning and restoring pair each template with its TypeId by slot index, so the two sides match.
  • No references to the removed snapshot_recreate API remain.
  • The tests cover the change:
    • startup_snapshot.rs covers state, shared templates, relinked references, a type without SnapshotTransfer, and an unreachable resource.
    • startup-snapshot-node-test covers node:url and node:dns in a Worker started from the snapshot.

⚡ Performance: ✅ LGTM

The snapshot work runs once in the zygote and once per Worker started from the snapshot, so none of it is on the request path. Restoring templates takes time linear in the number of Rust resource types.


📝 Documentation: ✅ LGTM

No documentation issues found.


Reviewed commit: 2c53a30b

@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 issues found. The only change since the last review formats the case list in startup-snapshot-node-test.js. snapshot_relink is still an unsafe fn, as requested in the approving review.

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

  • Safe code cannot reach the raw Wrappable dereference. snapshot_relink is an unsafe fn, and it is called only from C++ with Wrappables from the same isolate.
  • The snapshot data is produced inside the process and never comes from untrusted input.
  • Snapshot is bounded by HoldsNoGcReferences, so it cannot hold a GC reference.

📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

The only change since the last review formats the case list in startup-snapshot-node-test.js.

  • ErasedSnapshotTransfer::snapshot_relink is still an unsafe fn with a # Safety section.
  • The compile-fail test erased-snapshot-relink-is-unsafe.rs guards that contract. The compile-tests target picks it up through glob(["*.rs"]).
  • Templates are restored right after realm_create. Pinning and restoring pair each template with its TypeId by slot index, so the two sides match.
  • No references to the removed snapshot_recreate API remain.
  • The tests cover the change:
    • startup_snapshot.rs covers state, shared templates, relinked references, a type without SnapshotTransfer, and an unreachable resource.
    • startup-snapshot-node-test covers node:url and node:dns in a Worker started from the snapshot.

⚡ Performance: ✅ LGTM

The snapshot work runs once in the zygote and once per Worker started from the snapshot, so none of it is on the request path. Restoring templates takes time linear in the number of Rust resource types.


📝 Documentation: ✅ LGTM

No documentation issues found.


Reviewed commit: cf5eed76 — same changes as 2c53a30b, so this is that commit's review (no new review was run; line numbers are as of 2c53a30b)

@littledivy
littledivy merged commit 95ca48b into main Nov 18, 2026
23 checks passed
@littledivy
littledivy deleted the littledivy/2026-11-16-audit-rjsg-autogates branch November 18, 2026 18:12
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.

4 participants