Skip to content

Commit 82fb037

Browse files
joyeecheungbengl
authored andcommitted
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: #43023 Fixes: #31074 Refs: #35711 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]>
1 parent b318185 commit 82fb037

File tree

5 files changed

+23
-15
lines changed

5 files changed

+23
-15
lines changed

src/env.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -985,8 +985,16 @@ struct EnvSerializeInfo {
985985
};
986986

987987
struct SnapshotData {
988-
v8::StartupData blob;
988+
// The result of v8::SnapshotCreator::CreateBlob() during the snapshot
989+
// building process.
990+
v8::StartupData v8_snapshot_blob_data;
991+
992+
static const size_t kNodeBaseContextIndex = 0;
993+
static const size_t kNodeMainContextIndex = kNodeBaseContextIndex + 1;
994+
989995
std::vector<size_t> isolate_data_indices;
996+
// TODO(joyeecheung): there should be a vector of env_info once we snapshot
997+
// the worker environments.
990998
EnvSerializeInfo env_info;
991999
};
9921000

src/node_main_instance.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ NodeMainInstance::CreateMainEnvironment(int* exit_code) {
183183
EnvironmentFlags::kDefaultFlags,
184184
{}));
185185
context = Context::FromSnapshot(isolate_,
186-
SnapshotBuilder::kNodeMainContextIndex,
186+
SnapshotData::kNodeMainContextIndex,
187187
{DeserializeNodeInternalFields, env.get()})
188188
.ToLocalChecked();
189189

src/node_snapshot_builder.h

-3
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ class NODE_EXTERN_PRIVATE SnapshotBuilder {
2929
static void InitializeIsolateParams(const SnapshotData* data,
3030
v8::Isolate::CreateParams* params);
3131

32-
static const size_t kNodeBaseContextIndex = 0;
33-
static const size_t kNodeMainContextIndex = kNodeBaseContextIndex + 1;
34-
3532
private:
3633
// Used to synchronize access to the snapshot data
3734
static Mutex snapshot_data_mutex_;

src/node_snapshotable.cc

+11-8
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,13 @@ namespace node {
5959
6060
static const char blob_data[] = {
6161
)";
62-
WriteVector(&ss, data->blob.data, data->blob.raw_size);
62+
WriteVector(&ss,
63+
data->v8_snapshot_blob_data.data,
64+
data->v8_snapshot_blob_data.raw_size);
6365
ss << R"(};
6466
6567
static const int blob_size = )"
66-
<< data->blob.raw_size << R"(;
68+
<< data->v8_snapshot_blob_data.raw_size << R"(;
6769
6870
SnapshotData snapshot_data {
6971
// -- blob begins --
@@ -103,7 +105,8 @@ const std::vector<intptr_t>& SnapshotBuilder::CollectExternalReferences() {
103105
void SnapshotBuilder::InitializeIsolateParams(const SnapshotData* data,
104106
Isolate::CreateParams* params) {
105107
params->external_references = CollectExternalReferences().data();
106-
params->snapshot_blob = const_cast<v8::StartupData*>(&(data->blob));
108+
params->snapshot_blob =
109+
const_cast<v8::StartupData*>(&(data->v8_snapshot_blob_data));
107110
}
108111

109112
void SnapshotBuilder::Generate(SnapshotData* out,
@@ -153,7 +156,7 @@ void SnapshotBuilder::Generate(SnapshotData* out,
153156
// without breaking compatibility.
154157
{
155158
size_t index = creator.AddContext(CreateBaseContext());
156-
CHECK_EQ(index, SnapshotBuilder::kNodeBaseContextIndex);
159+
CHECK_EQ(index, SnapshotData::kNodeBaseContextIndex);
157160
}
158161

159162
// The main instance context.
@@ -222,17 +225,17 @@ void SnapshotBuilder::Generate(SnapshotData* out,
222225
// Serialize the context
223226
size_t index = creator.AddContext(
224227
main_context, {SerializeNodeContextInternalFields, env});
225-
CHECK_EQ(index, SnapshotBuilder::kNodeMainContextIndex);
228+
CHECK_EQ(index, SnapshotData::kNodeMainContextIndex);
226229
}
227230
}
228231

229232
// Must be out of HandleScope
230-
out->blob =
233+
out->v8_snapshot_blob_data =
231234
creator.CreateBlob(SnapshotCreator::FunctionCodeHandling::kClear);
232235

233236
// We must be able to rehash the blob when we restore it or otherwise
234237
// the hash seed would be fixed by V8, introducing a vulnerability.
235-
CHECK(out->blob.CanBeRehashed());
238+
CHECK(out->v8_snapshot_blob_data.CanBeRehashed());
236239

237240
// We cannot resurrect the handles from the snapshot, so make sure that
238241
// no handles are left open in the environment after the blob is created
@@ -260,7 +263,7 @@ std::string SnapshotBuilder::Generate(
260263
SnapshotData data;
261264
Generate(&data, args, exec_args);
262265
std::string result = FormatBlob(&data);
263-
delete[] data.blob.data;
266+
delete[] data.v8_snapshot_blob_data.data;
264267
return result;
265268
}
266269

src/node_worker.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,8 @@ void Worker::Run() {
301301
// though.
302302
TryCatch try_catch(isolate_);
303303
if (snapshot_data_ != nullptr) {
304-
context = Context::FromSnapshot(
305-
isolate_, SnapshotBuilder::kNodeBaseContextIndex)
304+
context = Context::FromSnapshot(isolate_,
305+
SnapshotData::kNodeBaseContextIndex)
306306
.ToLocalChecked();
307307
if (!context.IsEmpty() &&
308308
!InitializeContextRuntime(context).IsJust()) {

0 commit comments

Comments
 (0)