Skip to content

Commit f37444e

Browse files
joyeecheungUlisesGascon
authored andcommitted
bootstrap: build code cache from deserialized isolate
V8 now requires the code cache to be compiled with a finalized read-only space, so we need to serialize the snapshot to get a finalized read-only space first, then deserialize it to compile the code cache. PR-URL: #49099 Refs: #47636 Refs: https://bugs.chromium.org/p/v8/issues/detail?id=13789 Reviewed-By: Yagiz Nizipli <[email protected]>
1 parent ab97523 commit f37444e

File tree

1 file changed

+73
-18
lines changed

1 file changed

+73
-18
lines changed

src/node_snapshotable.cc

+73-18
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,7 @@ void SnapshotBuilder::InitializeIsolateParams(const SnapshotData* data,
911911
const_cast<v8::StartupData*>(&(data->v8_snapshot_blob_data));
912912
}
913913

914-
ExitCode SnapshotBuilder::Generate(
914+
ExitCode BuildSnapshotWithoutCodeCache(
915915
SnapshotData* out,
916916
const std::vector<std::string>& args,
917917
const std::vector<std::string>& exec_args,
@@ -933,8 +933,8 @@ ExitCode SnapshotBuilder::Generate(
933933
fprintf(stderr, "%s: %s\n", args[0].c_str(), err.c_str());
934934
return ExitCode::kBootstrapFailure;
935935
}
936-
Isolate* isolate = setup->isolate();
937936

937+
Isolate* isolate = setup->isolate();
938938
{
939939
HandleScope scope(isolate);
940940
TryCatch bootstrapCatch(isolate);
@@ -968,7 +968,77 @@ ExitCode SnapshotBuilder::Generate(
968968
}
969969
}
970970

971-
return CreateSnapshot(out, setup.get(), static_cast<uint8_t>(snapshot_type));
971+
return SnapshotBuilder::CreateSnapshot(
972+
out, setup.get(), static_cast<uint8_t>(snapshot_type));
973+
}
974+
975+
ExitCode BuildCodeCacheFromSnapshot(SnapshotData* out,
976+
const std::vector<std::string>& args,
977+
const std::vector<std::string>& exec_args) {
978+
std::vector<std::string> errors;
979+
auto data_wrapper = out->AsEmbedderWrapper();
980+
auto setup = CommonEnvironmentSetup::CreateFromSnapshot(
981+
per_process::v8_platform.Platform(),
982+
&errors,
983+
data_wrapper.get(),
984+
args,
985+
exec_args);
986+
if (!setup) {
987+
for (const auto& err : errors)
988+
fprintf(stderr, "%s: %s\n", args[0].c_str(), err.c_str());
989+
return ExitCode::kBootstrapFailure;
990+
}
991+
992+
Isolate* isolate = setup->isolate();
993+
v8::Locker locker(isolate);
994+
Isolate::Scope isolate_scope(isolate);
995+
HandleScope handle_scope(isolate);
996+
TryCatch bootstrapCatch(isolate);
997+
998+
auto print_Exception = OnScopeLeave([&]() {
999+
if (bootstrapCatch.HasCaught()) {
1000+
PrintCaughtException(
1001+
isolate, isolate->GetCurrentContext(), bootstrapCatch);
1002+
}
1003+
});
1004+
1005+
Environment* env = setup->env();
1006+
// Regenerate all the code cache.
1007+
if (!env->builtin_loader()->CompileAllBuiltins(setup->context())) {
1008+
return ExitCode::kGenericUserError;
1009+
}
1010+
env->builtin_loader()->CopyCodeCache(&(out->code_cache));
1011+
if (per_process::enabled_debug_list.enabled(DebugCategory::MKSNAPSHOT)) {
1012+
for (const auto& item : out->code_cache) {
1013+
std::string size_str = FormatSize(item.data.length);
1014+
per_process::Debug(DebugCategory::MKSNAPSHOT,
1015+
"Generated code cache for %d: %s\n",
1016+
item.id.c_str(),
1017+
size_str.c_str());
1018+
}
1019+
}
1020+
return ExitCode::kNoFailure;
1021+
}
1022+
1023+
ExitCode SnapshotBuilder::Generate(
1024+
SnapshotData* out,
1025+
const std::vector<std::string>& args,
1026+
const std::vector<std::string>& exec_args,
1027+
std::optional<std::string_view> main_script) {
1028+
ExitCode code =
1029+
BuildSnapshotWithoutCodeCache(out, args, exec_args, main_script);
1030+
if (code != ExitCode::kNoFailure) {
1031+
return code;
1032+
}
1033+
1034+
#ifdef NODE_USE_NODE_CODE_CACHE
1035+
// Deserialize the snapshot to recompile code cache. We need to do this in the
1036+
// second pass because V8 requires the code cache to be compiled with a
1037+
// finalized read-only space.
1038+
return BuildCodeCacheFromSnapshot(out, args, exec_args);
1039+
#else
1040+
return ExitCode::kNoFailure;
1041+
#endif
9721042
}
9731043

9741044
ExitCode SnapshotBuilder::CreateSnapshot(SnapshotData* out,
@@ -1021,21 +1091,6 @@ ExitCode SnapshotBuilder::CreateSnapshot(SnapshotData* out,
10211091
out->isolate_data_info = setup->isolate_data()->Serialize(creator);
10221092
out->env_info = env->Serialize(creator);
10231093

1024-
#ifdef NODE_USE_NODE_CODE_CACHE
1025-
// Regenerate all the code cache.
1026-
if (!env->builtin_loader()->CompileAllBuiltins(main_context)) {
1027-
return ExitCode::kGenericUserError;
1028-
}
1029-
env->builtin_loader()->CopyCodeCache(&(out->code_cache));
1030-
for (const auto& item : out->code_cache) {
1031-
std::string size_str = FormatSize(item.data.length);
1032-
per_process::Debug(DebugCategory::MKSNAPSHOT,
1033-
"Generated code cache for %d: %s\n",
1034-
item.id.c_str(),
1035-
size_str.c_str());
1036-
}
1037-
#endif
1038-
10391094
ResetContextSettingsBeforeSnapshot(main_context);
10401095
}
10411096

0 commit comments

Comments
 (0)