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 <[email protected]>
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:
|
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.
V8 shares the read-only heap between isolates, so
NewIsolate()creates every isolate from the snapshot blob that the first isolate in the process used. It did that by stashing a pointer to the firstCreateParamsin a function static (the comment next to it already said "this isn't really memory-safe"). When the first blob came from anEmbedderSnapshotDatathe embedder has since released, which is the natural thing to do after tearing down aCommonEnvironmentSetupcreated withCreateFromSnapshot(), the nextNewIsolate()hands V8 freed memory andSnapshot::Initialize()reads from it. It happens to work on glibc because the pages are still intact; ASAN reports the use-after-free.NewIsolate()now records the first blob and external references under a mutex instead of keeping 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 (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.embedtestgets an--embedder-run-twiceswitch to exercise two instances in one process.Tests:
test/embedding/test-embedding-snapshot-twice.jsruns a snapshot twice in one embedtest process, freeing theEmbedderSnapshotDatain between; heap-use-after-free inv8::internal::Snapshot::Initializeunder ASAN before, clean after. The rest oftest/embeddingpasses.Refs: #45885
Disclosure: the code, test and this description were written by Claude Code, directed and reviewed by @codebytere.