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 <matteo.collina@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
This commit is contained in:
parent
0ce4725330
commit
de0b6dc710
10
src/env.h
10
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<size_t> isolate_data_indices;
|
||||
// TODO(joyeecheung): there should be a vector of env_info once we snapshot
|
||||
// the worker environments.
|
||||
EnvSerializeInfo env_info;
|
||||
};
|
||||
|
||||
|
@ -183,7 +183,7 @@ NodeMainInstance::CreateMainEnvironment(int* exit_code) {
|
||||
EnvironmentFlags::kDefaultFlags,
|
||||
{}));
|
||||
context = Context::FromSnapshot(isolate_,
|
||||
SnapshotBuilder::kNodeMainContextIndex,
|
||||
SnapshotData::kNodeMainContextIndex,
|
||||
{DeserializeNodeInternalFields, env.get()})
|
||||
.ToLocalChecked();
|
||||
|
||||
|
@ -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_;
|
||||
|
@ -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<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,
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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()) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user