Skip to content

Commit 9dc6d7c

Browse files
committed
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. 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
1 parent d52c5bc commit 9dc6d7c

File tree

5 files changed

+17
-1
lines changed

5 files changed

+17
-1
lines changed

src/api/environment.cc

+7
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,8 @@ void SetIsolateMiscHandlers(v8::Isolate* isolate, const IsolateSettings& s) {
313313

314314
void SetIsolateUpForNode(v8::Isolate* isolate,
315315
const IsolateSettings& settings) {
316+
Isolate::Scope isolate_scope(isolate);
317+
316318
SetIsolateErrorHandlers(isolate, settings);
317319
SetIsolateMiscHandlers(isolate, settings);
318320
}
@@ -354,6 +356,9 @@ Isolate* NewIsolate(Isolate::CreateParams* params,
354356

355357
SetIsolateCreateParamsForNode(params);
356358
Isolate::Initialize(isolate, *params);
359+
360+
Isolate::Scope isolate_scope(isolate);
361+
357362
if (snapshot_data == nullptr) {
358363
// If in deserialize mode, delay until after the deserialization is
359364
// complete.
@@ -428,6 +433,8 @@ Environment* CreateEnvironment(
428433
ThreadId thread_id,
429434
std::unique_ptr<InspectorParentHandle> inspector_parent_handle) {
430435
Isolate* isolate = isolate_data->isolate();
436+
437+
Isolate::Scope isolate_scope(isolate);
431438
HandleScope handle_scope(isolate);
432439

433440
const bool use_snapshot = context.IsEmpty();

src/env.cc

+3
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

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ using v8::String;
1212
using v8::Value;
1313

1414
JSONParser::JSONParser()
15-
: handle_scope_(isolate_.get()),
15+
: isolate_scope_(isolate_.get()),
16+
handle_scope_(isolate_.get()),
1617
context_(isolate_.get(), Context::New(isolate_.get())),
1718
context_scope_(context_.Get(isolate_.get())) {}
1819

src/json_parser.h

+3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ class JSONParser {
2525
// We might want a lighter-weight JSON parser for this use case. But for now
2626
// using V8 is good enough.
2727
RAIIIsolate isolate_;
28+
29+
v8::Isolate::Scope isolate_scope_;
2830
v8::HandleScope handle_scope_;
31+
2932
v8::Global<v8::Context> context_;
3033
v8::Context::Scope context_scope_;
3134
v8::Global<v8::Object> content_;

src/node_sea.cc

+2
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

0 commit comments

Comments
 (0)