Skip to content

Commit 282d30e

Browse files
authored
src: dump snapshot source with node:generate_default_snapshot_source
This helps diffing snapshots when the reproducibility gets broken. PR-URL: #61101 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
1 parent 7199a61 commit 282d30e

File tree

2 files changed

+21
-29
lines changed

2 files changed

+21
-29
lines changed

src/node.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,6 +1374,21 @@ ExitCode GenerateAndWriteSnapshotData(const SnapshotData** snapshot_data_ptr,
13741374
DCHECK(snapshot_config.builder_script_path.has_value());
13751375
const std::string& builder_script =
13761376
snapshot_config.builder_script_path.value();
1377+
1378+
// For the special builder node:generate_default_snapshot_source, generate
1379+
// the snapshot as C++ source and write it to snapshot.cc (for testing).
1380+
if (builder_script == "node:generate_default_snapshot_source") {
1381+
// Reset to empty to generate from scratch.
1382+
snapshot_config.builder_script_path = {};
1383+
exit_code =
1384+
node::SnapshotBuilder::GenerateAsSource("snapshot.cc",
1385+
args_maybe_patched,
1386+
result->exec_args(),
1387+
snapshot_config,
1388+
true /* use_array_literals */);
1389+
return exit_code;
1390+
}
1391+
13771392
// node:embedded_snapshot_main indicates that we are using the
13781393
// embedded snapshot and we are not supposed to clean it up.
13791394
if (builder_script == "node:embedded_snapshot_main") {

test/parallel/test-snapshot-reproducible.js

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function generateSnapshot() {
2121
'--random_seed=42',
2222
'--predictable',
2323
'--build-snapshot',
24-
'node:generate_default_snapshot',
24+
'node:generate_default_snapshot_source',
2525
],
2626
{
2727
env: { ...process.env, NODE_DEBUG_NATIVE: 'SNAPSHOT_SERDES' },
@@ -38,33 +38,10 @@ function generateSnapshot() {
3838
},
3939
}
4040
);
41-
const blobPath = tmpdir.resolve('snapshot.blob');
42-
return fs.readFileSync(blobPath);
41+
const outputPath = tmpdir.resolve('snapshot.cc');
42+
return fs.readFileSync(outputPath, 'utf-8').split('\n');
4343
}
4444

45-
const buf1 = generateSnapshot();
46-
const buf2 = generateSnapshot();
47-
48-
const diff = [];
49-
let offset = 0;
50-
const step = 16;
51-
do {
52-
const length = Math.min(buf1.length - offset, step);
53-
const slice1 = buf1.slice(offset, offset + length).toString('hex');
54-
const slice2 = buf2.slice(offset, offset + length).toString('hex');
55-
if (slice1 !== slice2) {
56-
diff.push({ offset: '0x' + (offset).toString(16), slice1, slice2 });
57-
}
58-
offset += length;
59-
} while (offset < buf1.length);
60-
61-
assert.strictEqual(offset, buf1.length);
62-
if (offset < buf2.length) {
63-
const length = Math.min(buf2.length - offset, step);
64-
const slice2 = buf2.slice(offset, offset + length).toString('hex');
65-
diff.push({ offset, slice1: '', slice2 });
66-
offset += length;
67-
} while (offset < buf2.length);
68-
69-
assert.deepStrictEqual(diff, []);
70-
assert.strictEqual(buf1.length, buf2.length);
45+
const source1 = generateSnapshot();
46+
const source2 = generateSnapshot();
47+
assert.deepStrictEqual(source1, source2);

0 commit comments

Comments
 (0)