From 697fef6744db851ac1613eb8eed1c91cf1f1817f Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Wed, 12 Jul 2023 19:12:00 +0200 Subject: [PATCH] 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. --- src/node.cc | 7 +++++++ src/node.h | 2 ++ tools/snapshot/node_mksnapshot.cc | 5 ++--- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/node.cc b/src/node.cc index f483e59dd155a8..9987d84521bd72 100644 --- a/src/node.cc +++ b/src/node.cc @@ -932,6 +932,13 @@ InitializeOncePerProcessInternal(const std::vector& args, } } + if (!!(flags & ProcessInitializationFlags::kGeneratePredictableSnapshot) || + per_process::cli_options->per_isolate->build_snapshot) { + v8::V8::SetFlagsFromString("--predictable"); + v8::V8::SetFlagsFromString("--random_seed=42"); + v8::V8::SetFlagsFromString("--harmony-import-assertions"); + } + if (!(flags & ProcessInitializationFlags::kNoUseLargePages) && (per_process::cli_options->use_largepages == "on" || per_process::cli_options->use_largepages == "silent")) { diff --git a/src/node.h b/src/node.h index 846ec413f8e1fc..21e8c6cdebf00e 100644 --- a/src/node.h +++ b/src/node.h @@ -261,6 +261,8 @@ enum Flags : uint32_t { kNoUseLargePages = 1 << 11, // Skip printing output for --help, --version, --v8-options. kNoPrintHelpOrVersionOutput = 1 << 12, + // Initialize the process for predictable snapshot generation. + kGeneratePredictableSnapshot = 1 << 13, // Emulate the behavior of InitializeNodeWithArgs() when passing // a flags argument to the InitializeOncePerProcess() replacement diff --git a/tools/snapshot/node_mksnapshot.cc b/tools/snapshot/node_mksnapshot.cc index ecc295acdbea32..67c48d381813c7 100644 --- a/tools/snapshot/node_mksnapshot.cc +++ b/tools/snapshot/node_mksnapshot.cc @@ -51,8 +51,6 @@ int main(int argc, char* argv[]) { setvbuf(stderr, nullptr, _IONBF, 0); #endif // _WIN32 - v8::V8::SetFlagsFromString("--random_seed=42"); - v8::V8::SetFlagsFromString("--harmony-import-assertions"); return BuildSnapshot(argc, argv); } @@ -66,7 +64,8 @@ int BuildSnapshot(int argc, char* argv[]) { std::unique_ptr result = node::InitializeOncePerProcess( - std::vector(argv, argv + argc)); + std::vector(argv, argv + argc), + node::ProcessInitializationFlags::kGeneratePredictableSnapshot); CHECK(!result->early_return()); CHECK_EQ(result->exit_code(), 0);