From c99e51ed1b7c8f3f570b23385fe30f7e9850875a Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Mon, 9 Oct 2023 04:53:19 +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. PR-URL: https://github.com/nodejs/node/pull/48749 Refs: https://github.com/nodejs/build/issues/3043 Reviewed-By: Richard Lau Reviewed-By: Yagiz Nizipli Reviewed-By: Chengzhong Wu --- src/node.cc | 6 ++++++ src/node.h | 2 ++ tools/snapshot/node_mksnapshot.cc | 5 ++--- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/node.cc b/src/node.cc index 89e0e5524c2102..14e27806a5d3b2 100644 --- a/src/node.cc +++ b/src/node.cc @@ -838,6 +838,12 @@ static ExitCode InitializeNodeWithArgsInternal( V8::SetFlagsFromString(NODE_V8_OPTIONS, sizeof(NODE_V8_OPTIONS) - 1); #endif + if (!!(flags & ProcessInitializationFlags::kGeneratePredictableSnapshot) || + per_process::cli_options->per_isolate->build_snapshot) { + v8::V8::SetFlagsFromString("--predictable"); + v8::V8::SetFlagsFromString("--random_seed=42"); + } + // Specify this explicitly to avoid being affected by V8 changes to the // default value. V8::SetFlagsFromString("--rehash-snapshot"); diff --git a/src/node.h b/src/node.h index ca01c42e8af484..f2740116a4710b 100644 --- a/src/node.h +++ b/src/node.h @@ -265,6 +265,8 @@ enum Flags : uint32_t { // cppgc::InitializeProcess() before creating a Node.js environment // and call cppgc::ShutdownProcess() before process shutdown. kNoInitializeCppgc = 1 << 13, + // Initialize the process for predictable snapshot generation. + kGeneratePredictableSnapshot = 1 << 14, // 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 a9df251700ea06..841a8ca743bcaa 100644 --- a/tools/snapshot/node_mksnapshot.cc +++ b/tools/snapshot/node_mksnapshot.cc @@ -50,8 +50,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); } @@ -65,7 +63,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);