src: add IsolateScopes before using isolates · nodejs/node@faf6a04 · GitHub
Skip to content

Commit faf6a04

Browse files
kvakilRafaelGSS
authored andcommitted
src: add IsolateScopes before using isolates
The V8 API requires entering an isolate before using it. We were often not doing this, which worked fine in practice. However when (multi-cage) pointer compression is enabled, the correct isolate needs to be active in order to decompress pointers correctly, otherwise it causes crashes. Fix this by sprinkling in some calls to v8::Isolate::Scope::Scope where they were missing. This also introduces RAIIIsolateWithoutEntering which is used in JSONParser to avoid otherwise exposing the Isolate::Scope outside of the class. Tested by compiling with `--experimental-enable-pointer-compression` locally and running all tests. Refs: nodejs/build#3204 (comment) Refs: https://bugs.chromium.org/p/v8/issues/detail?id=14292 PR-URL: #50680 Refs: v8/v8@475c8cd Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 00dab30 commit faf6a04

7 files changed

Lines changed: 41 additions & 7 deletions

File tree

src/api/environment.cc

Lines changed: 7 additions & 0 deletions

src/env.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,8 @@ IsolateDataSerializeInfo IsolateData::Serialize(SnapshotCreator* creator) {
349349

350350
void IsolateData::DeserializeProperties(const IsolateDataSerializeInfo* info) {
351351
size_t i = 0;
352+
353+
v8::Isolate::Scope isolate_scope(isolate_);
352354
HandleScope handle_scope(isolate_);
353355

354356
if (per_process::enabled_debug_list.enabled(DebugCategory::MKSNAPSHOT)) {
@@ -431,6 +433,7 @@ void IsolateData::CreateProperties() {
431433
// One byte because our strings are ASCII and we can safely skip V8's UTF-8
432434
// decoding step.
433435

436+
v8::Isolate::Scope isolate_scope(isolate_);
434437
HandleScope handle_scope(isolate_);
435438

436439
#define V(PropertyName, StringValue) \

src/json_parser.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ bool JSONParser::Parse(const std::string& content) {
1717
DCHECK(!parsed_);
1818

1919
Isolate* isolate = isolate_.get();
20+
v8::Isolate::Scope isolate_scope(isolate);
2021
v8::HandleScope handle_scope(isolate);
2122

2223
Local<Context> context = Context::New(isolate);
@@ -45,6 +46,7 @@ bool JSONParser::Parse(const std::string& content) {
4546
std::optional<std::string> JSONParser::GetTopLevelStringField(
4647
std::string_view field) {
4748
Isolate* isolate = isolate_.get();
49+
v8::Isolate::Scope isolate_scope(isolate);
4850
v8::HandleScope handle_scope(isolate);
4951

5052
Local<Context> context = context_.Get(isolate);
@@ -70,6 +72,7 @@ std::optional<std::string> JSONParser::GetTopLevelStringField(
7072

7173
std::optional<bool> JSONParser::GetTopLevelBoolField(std::string_view field) {
7274
Isolate* isolate = isolate_.get();
75+
v8::Isolate::Scope isolate_scope(isolate);
7376
v8::HandleScope handle_scope(isolate);
7477

7578
Local<Context> context = context_.Get(isolate);

src/json_parser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class JSONParser {
2424
private:
2525
// We might want a lighter-weight JSON parser for this use case. But for now
2626
// using V8 is good enough.
27-
RAIIIsolate isolate_;
27+
RAIIIsolateWithoutEntering isolate_;
2828

2929
v8::Global<v8::Context> context_;
3030
v8::Global<v8::Object> content_;

src/node_sea.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,9 @@ std::optional<std::string> GenerateCodeCache(std::string_view main_path,
414414
RAIIIsolate raii_isolate(SnapshotBuilder::GetEmbeddedSnapshotData());
415415
Isolate* isolate = raii_isolate.get();
416416

417+
v8::Isolate::Scope isolate_scope(isolate);
417418
HandleScope handle_scope(isolate);
419+
418420
Local<Context> context = Context::New(isolate);
419421
Context::Scope context_scope(context);
420422

src/util.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ Local<String> UnionBytes::ToStringChecked(Isolate* isolate) const {
678678
}
679679
}
680680

681-
RAIIIsolate::RAIIIsolate(const SnapshotData* data)
681+
RAIIIsolateWithoutEntering::RAIIIsolateWithoutEntering(const SnapshotData* data)
682682
: allocator_{ArrayBuffer::Allocator::NewDefaultAllocator()} {
683683
isolate_ = Isolate::Allocate();
684684
CHECK_NOT_NULL(isolate_);
@@ -692,9 +692,14 @@ RAIIIsolate::RAIIIsolate(const SnapshotData* data)
692692
Isolate::Initialize(isolate_, params);
693693
}
694694

695-
RAIIIsolate::~RAIIIsolate() {
695+
RAIIIsolateWithoutEntering::~RAIIIsolateWithoutEntering() {
696696
per_process::v8_platform.Platform()->UnregisterIsolate(isolate_);
697697
isolate_->Dispose();
698698
}
699699

700+
RAIIIsolate::RAIIIsolate(const SnapshotData* data)
701+
: isolate_{data}, isolate_scope_{isolate_.get()} {}
702+
703+
RAIIIsolate::~RAIIIsolate() {}
704+
700705
} // namespace node

src/util.h

Lines changed: 18 additions & 4 deletions

0 commit comments

Comments
 (0)