src: keep the first snapshot blob alive for later isolates - #65779
Open
codebytere wants to merge 2 commits into
Open
src: keep the first snapshot blob alive for later isolates#65779codebytere wants to merge 2 commits into
codebytere wants to merge 2 commits into
Conversation
`NewIsolate()` creates every isolate from the snapshot blob the first isolate in the process used, because V8 shares the read-only heap between isolates, and did so by keeping a pointer to the first `CreateParams`. When that blob came from an `EmbedderSnapshotData` the embedder had since released, e.g. a second `CommonEnvironmentSetup::CreateFromSnapshot()` after the first setup and its snapshot were destroyed, V8 deserialized freed memory. Record the first blob and external references under a mutex instead of copying the caller's `CreateParams`, and make `~SnapshotData()` leave that one blob allocated, since its owner can go away before the last isolate is created. Nothing is copied and `node` itself is unaffected. embedtest grows an `--embedder-run-twice` switch so the sequence can be tested. Refs: nodejs#45885 Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com>
Collaborator
|
Review requested:
|
Collaborator
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #65779 +/- ##
==========================================
+ Coverage 89.99% 90.21% +0.21%
==========================================
Files 757 771 +14
Lines 257739 264638 +6899
Branches 48881 50233 +1352
==========================================
+ Hits 231961 238730 +6769
- Misses 16861 16914 +53
- Partials 8917 8994 +77
🚀 New features to boost your workflow:
|
Collaborator
Two threads creating their first isolate at the same time (two `CommonEnvironmentSetup`s on their own threads, or an embedder's setup racing a Worker) could corrupt or misread the external reference list handed to V8: `SnapshotBuilder::CollectExternalReferences()` creates its registry in a thread-safe function static, but then calls `external_references()` on every call, and that method appends the terminating nullptr and flips `is_finalized_` the first time through without any locking, so both threads can append, or one can read the vector while the other reallocates it. TSAN reports it for any two concurrent setups. Keep the finalized list in a second function static so finalization runs exactly once, under that static's initialization guard. Refs: nodejs#32984 Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com>
Collaborator
jasnell
approved these changes
Sep 6, 2026
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.
An embedder that creates a
CommonEnvironmentSetupfrom anEmbedderSnapshotData, tears it down, releases the snapshot data and later creates a second setup hands V8 freed memory, because every isolate in a process is created from the first isolate's snapshot blob andNewIsolate()only kept a pointer to the first caller'sCreateParams; ASAN reports a use-after-free inSnapshot::Initialize. A second commit fixes a race in the same first-isolate setup: two threads creating their first isolate at once could both finalize the external reference list.NewIsolate()now records the first blob and external references under a mutex and~SnapshotData()leaves that one blob allocated; the external reference list is finalized once in a function-local static. Nothing is copied andnodeitself is unaffected. node.h now states the lifetimesnapshot_dataneeds.Tests:
test/embedding/test-embedding-snapshot-twice.js(use-after-free under ASAN before) andEnvironmentTest.CollectExternalReferencesFromSeveralThreads(TSAN before); the rest oftest/embeddingpasses.Refs: #45885
Refs: #32984
Disclosure: the code, tests and this description were written by Claude Code, directed and reviewed by @codebytere.