From 82fb037388f510fde52558c8e19746a5ebb63938 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Mon, 9 May 2022 18:22:48 +0800 Subject: [PATCH] src: move context snapshot index to SnapshotData Also added comments for the members of SnapshotData and renamed blob to v8_snapshot_blob_data for clarity. PR-URL: https://github.com/nodejs/node/pull/43023 Fixes: https://github.com/nodejs/node/issues/31074 Refs: https://github.com/nodejs/node/issues/35711 Reviewed-By: Matteo Collina Reviewed-By: Chengzhong Wu --- src/env.h | 10 +++++++++- src/node_main_instance.cc | 2 +- src/node_snapshot_builder.h | 3 --- src/node_snapshotable.cc | 19 +++++++++++-------- src/node_worker.cc | 4 ++-- 5 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/env.h b/src/env.h index caefcdb5e35f97..961d0a70c778fb 100644 --- a/src/env.h +++ b/src/env.h @@ -985,8 +985,16 @@ struct EnvSerializeInfo { }; struct SnapshotData { - v8::StartupData blob; + // The result of v8::SnapshotCreator::CreateBlob() during the snapshot + // building process. + v8::StartupData v8_snapshot_blob_data; + + static const size_t kNodeBaseContextIndex = 0; + static const size_t kNodeMainContextIndex = kNodeBaseContextIndex + 1; + std::vector isolate_data_indices; + // TODO(joyeecheung): there should be a vector of env_info once we snapshot + // the worker environments. EnvSerializeInfo env_info; }; diff --git a/src/node_main_instance.cc b/src/node_main_instance.cc index f5e7ef61fb57f6..951a55f3568a82 100644 --- a/src/node_main_instance.cc +++ b/src/node_main_instance.cc @@ -183,7 +183,7 @@ NodeMainInstance::CreateMainEnvironment(int* exit_code) { EnvironmentFlags::kDefaultFlags, {})); context = Context::FromSnapshot(isolate_, - SnapshotBuilder::kNodeMainContextIndex, + SnapshotData::kNodeMainContextIndex, {DeserializeNodeInternalFields, env.get()}) .ToLocalChecked(); diff --git a/src/node_snapshot_builder.h b/src/node_snapshot_builder.h index 2714293fbc9976..c5d2ee2a4bcd83 100644 --- a/src/node_snapshot_builder.h +++ b/src/node_snapshot_builder.h @@ -29,9 +29,6 @@ class NODE_EXTERN_PRIVATE SnapshotBuilder { static void InitializeIsolateParams(const SnapshotData* data, v8::Isolate::CreateParams* params); - static const size_t kNodeBaseContextIndex = 0; - static const size_t kNodeMainContextIndex = kNodeBaseContextIndex + 1; - private: // Used to synchronize access to the snapshot data static Mutex snapshot_data_mutex_; diff --git a/src/node_snapshotable.cc b/src/node_snapshotable.cc index 9cb5985ea20841..2c91c0a11ce78c 100644 --- a/src/node_snapshotable.cc +++ b/src/node_snapshotable.cc @@ -59,11 +59,13 @@ namespace node { static const char blob_data[] = { )"; - WriteVector(&ss, data->blob.data, data->blob.raw_size); + WriteVector(&ss, + data->v8_snapshot_blob_data.data, + data->v8_snapshot_blob_data.raw_size); ss << R"(}; static const int blob_size = )" - << data->blob.raw_size << R"(; + << data->v8_snapshot_blob_data.raw_size << R"(; SnapshotData snapshot_data { // -- blob begins -- @@ -103,7 +105,8 @@ const std::vector& SnapshotBuilder::CollectExternalReferences() { void SnapshotBuilder::InitializeIsolateParams(const SnapshotData* data, Isolate::CreateParams* params) { params->external_references = CollectExternalReferences().data(); - params->snapshot_blob = const_cast(&(data->blob)); + params->snapshot_blob = + const_cast(&(data->v8_snapshot_blob_data)); } void SnapshotBuilder::Generate(SnapshotData* out, @@ -153,7 +156,7 @@ void SnapshotBuilder::Generate(SnapshotData* out, // without breaking compatibility. { size_t index = creator.AddContext(CreateBaseContext()); - CHECK_EQ(index, SnapshotBuilder::kNodeBaseContextIndex); + CHECK_EQ(index, SnapshotData::kNodeBaseContextIndex); } // The main instance context. @@ -222,17 +225,17 @@ void SnapshotBuilder::Generate(SnapshotData* out, // Serialize the context size_t index = creator.AddContext( main_context, {SerializeNodeContextInternalFields, env}); - CHECK_EQ(index, SnapshotBuilder::kNodeMainContextIndex); + CHECK_EQ(index, SnapshotData::kNodeMainContextIndex); } } // Must be out of HandleScope - out->blob = + out->v8_snapshot_blob_data = creator.CreateBlob(SnapshotCreator::FunctionCodeHandling::kClear); // We must be able to rehash the blob when we restore it or otherwise // the hash seed would be fixed by V8, introducing a vulnerability. - CHECK(out->blob.CanBeRehashed()); + CHECK(out->v8_snapshot_blob_data.CanBeRehashed()); // We cannot resurrect the handles from the snapshot, so make sure that // no handles are left open in the environment after the blob is created @@ -260,7 +263,7 @@ std::string SnapshotBuilder::Generate( SnapshotData data; Generate(&data, args, exec_args); std::string result = FormatBlob(&data); - delete[] data.blob.data; + delete[] data.v8_snapshot_blob_data.data; return result; } diff --git a/src/node_worker.cc b/src/node_worker.cc index acc1d56d6ad329..1009c0788cc624 100644 --- a/src/node_worker.cc +++ b/src/node_worker.cc @@ -301,8 +301,8 @@ void Worker::Run() { // though. TryCatch try_catch(isolate_); if (snapshot_data_ != nullptr) { - context = Context::FromSnapshot( - isolate_, SnapshotBuilder::kNodeBaseContextIndex) + context = Context::FromSnapshot(isolate_, + SnapshotData::kNodeBaseContextIndex) .ToLocalChecked(); if (!context.IsEmpty() && !InitializeContextRuntime(context).IsJust()) {