Skip to content

Commit 1e6968d

Browse files
committed
src: generate default snapshot with --predictable
To improve determinism of snapshot generation, add --predictable to the V8 flags used to initialize a process launched to generate snapshot. Also add a kGeneratePredictableSnapshot flag to ProcessInitializationFlags for this and moves the configuration of these flags into node::InitializeOncePerProcess() so that it can be shared by embedders.
1 parent 1d38dfd commit 1e6968d

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

src/node.cc

+7
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,13 @@ InitializeOncePerProcessInternal(const std::vector<std::string>& args,
945945
}
946946
}
947947

948+
if (!!(flags & ProcessInitializationFlags::kGeneratePredictableSnapshot) ||
949+
per_process::cli_options->per_isolate->build_snapshot) {
950+
v8::V8::SetFlagsFromString("--predictable");
951+
v8::V8::SetFlagsFromString("--random_seed=42");
952+
v8::V8::SetFlagsFromString("--harmony-import-assertions");
953+
}
954+
948955
if (!(flags & ProcessInitializationFlags::kNoUseLargePages) &&
949956
(per_process::cli_options->use_largepages == "on" ||
950957
per_process::cli_options->use_largepages == "silent")) {

src/node.h

+2
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,8 @@ enum Flags : uint32_t {
265265
// cppgc::InitializeProcess() before creating a Node.js environment
266266
// and call cppgc::ShutdownProcess() before process shutdown.
267267
kNoInitializeCppgc = 1 << 13,
268+
// Initialize the process for predictable snapshot generation.
269+
kGeneratePredictableSnapshot = 1 << 14,
268270

269271
// Emulate the behavior of InitializeNodeWithArgs() when passing
270272
// a flags argument to the InitializeOncePerProcess() replacement

tools/snapshot/node_mksnapshot.cc

+2-3
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ int main(int argc, char* argv[]) {
5151
setvbuf(stderr, nullptr, _IONBF, 0);
5252
#endif // _WIN32
5353

54-
v8::V8::SetFlagsFromString("--random_seed=42");
55-
v8::V8::SetFlagsFromString("--harmony-import-assertions");
5654
return BuildSnapshot(argc, argv);
5755
}
5856

@@ -66,7 +64,8 @@ int BuildSnapshot(int argc, char* argv[]) {
6664

6765
std::unique_ptr<node::InitializationResult> result =
6866
node::InitializeOncePerProcess(
69-
std::vector<std::string>(argv, argv + argc));
67+
std::vector<std::string>(argv, argv + argc),
68+
node::ProcessInitializationFlags::kGeneratePredictableSnapshot);
7069

7170
CHECK(!result->early_return());
7271
CHECK_EQ(result->exit_code(), 0);

0 commit comments

Comments
 (0)