src: keep the first snapshot blob alive for later isolates - #65779
Open
codebytere wants to merge 1 commit into
Open
src: keep the first snapshot blob alive for later isolates#65779codebytere wants to merge 1 commit into
codebytere wants to merge 1 commit 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✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #65779 +/- ##
==========================================
+ Coverage 89.99% 90.06% +0.06%
==========================================
Files 757 769 +12
Lines 257739 261413 +3674
Branches 48881 49632 +751
==========================================
+ Hits 231961 235431 +3470
- Misses 16861 17022 +161
- Partials 8917 8960 +43
🚀 New features to boost your workflow:
|
Collaborator
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: the second isolate is deserialized from the first, already-freed blob. It happens to work with glibc because the pages are usually still intact; ASAN reports a heap-use-after-free inv8::internal::Snapshot::Initialize.V8 shares the read-only heap between isolates, so
NewIsolate()creates every isolate from the snapshot blob the first isolate in the process used. It did that by keeping a pointer to the first caller'sCreateParamsin a function-local static (the comment next to it already said "this isn't really memory-safe"), and nothing kept the blob those params point at alive.NewIsolate()now records the first blob and its external references under a mutex instead of the caller'sCreateParams, and~SnapshotData()leaves that one blob allocated, since its owner can go away before the last isolate is created. Nothing is copied, andnodeitself is unaffected because its snapshot already lives untilTearDownOncePerProcess(). node.h now says thatsnapshot_datahas to outlive the setup and that every setup in a process has to use the same snapshot.Tests:
test/embedding/test-embedding-snapshot-twice.jsruns a snapshot twice in one embedtest process (new--embedder-run-twiceswitch), freeing theEmbedderSnapshotDatain between; heap-use-after-free under ASAN before the change, clean after.test/embeddingpasses.Refs: #45885
Disclosure: the code, test and this description were written by Claude Code, directed and reviewed by @codebytere.