Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tools: make code cache and snapshot deterministic #29142

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions test/parallel/test-math-random.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

require('../common');
const assert = require('assert');
const { spawnSync } = require('child_process');

const results = new Set();
for (let i = 0; i < 10; i++) {
const result = spawnSync(process.execPath, ['-p', 'Math.random()']);
assert.strictEqual(result.status, 0);
results.add(result.stdout.toString());
}
// It's theoretically possible if _very_ unlikely to see some duplicates.
// Therefore, don't expect that the size of the set is exactly 10 but do
// assume it's > 1 because if you get 10 duplicates in a row you should
// go out real quick and buy some lottery tickets, you lucky devil you!
assert(results.size > 1);
5 changes: 5 additions & 0 deletions tools/code_cache/mkcodecache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ int wmain(int argc, wchar_t* argv[]) {
int main(int argc, char* argv[]) {
#endif // _WIN32

v8::V8::SetFlagsFromString("--random_seed=42");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " <path/to/output.cc>\n";
return 1;
Expand Down Expand Up @@ -53,6 +55,9 @@ int main(int argc, char* argv[]) {
v8::Local<v8::Context> context = v8::Context::New(isolate);
v8::Context::Scope context_scope(context);

// The command line flags are part of the code cache's checksum so reset
// --random_seed= to its default value before creating the code cache.
v8::V8::SetFlagsFromString("--random_seed=0");
std::string cache = CodeCacheBuilder::Generate(context);
out << cache;
out.close();
Expand Down
2 changes: 2 additions & 0 deletions tools/snapshot/node_mksnapshot.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ int wmain(int argc, wchar_t* argv[]) {
int main(int argc, char* argv[]) {
#endif // _WIN32

v8::V8::SetFlagsFromString("--random_seed=42");

if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " <path/to/output.cc>\n";
return 1;
Expand Down