Skip to content

Commit

Permalink
src: move context snapshot index to SnapshotData
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
joyeecheung authored and bengl committed May 30, 2022
1 parent b318185 commit 82fb037
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 15 deletions.
10 changes: 9 additions & 1 deletion src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<size_t> isolate_data_indices;
// TODO(joyeecheung): there should be a vector of env_info once we snapshot
// the worker environments.
EnvSerializeInfo env_info;
};

Expand Down
2 changes: 1 addition & 1 deletion src/node_main_instance.cc
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ NodeMainInstance::CreateMainEnvironment(int* exit_code) {
EnvironmentFlags::kDefaultFlags,
{}));
context = Context::FromSnapshot(isolate_,
SnapshotBuilder::kNodeMainContextIndex,
SnapshotData::kNodeMainContextIndex,
{DeserializeNodeInternalFields, env.get()})
.ToLocalChecked();

Expand Down
3 changes: 0 additions & 3 deletions src/node_snapshot_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand Down
19 changes: 11 additions & 8 deletions src/node_snapshotable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 --
Expand Down Expand Up @@ -103,7 +105,8 @@ const std::vector<intptr_t>& SnapshotBuilder::CollectExternalReferences() {
void SnapshotBuilder::InitializeIsolateParams(const SnapshotData* data,
Isolate::CreateParams* params) {
params->external_references = CollectExternalReferences().data();
params->snapshot_blob = const_cast<v8::StartupData*>(&(data->blob));
params->snapshot_blob =
const_cast<v8::StartupData*>(&(data->v8_snapshot_blob_data));
}

void SnapshotBuilder::Generate(SnapshotData* out,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions src/node_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down

0 comments on commit 82fb037

Please sign in to comment.