src: fix timing of snapshot serialize callback · nodejs/node@2e93650 · GitHub
Skip to content

Commit 2e93650

Browse files
joyeecheungaduh95
authored andcommitted
src: fix timing of snapshot serialize callback
Previously the addAfterUserSerailizeCallback() wasn't ready to be used for building the built-in snapshot. This patch initializes the callbacks at the time lib/internal/v8/start_snapshot.js is loaded, so that these callbacks get run correctly when building the built-in snapshot. Currently when building the built-in snapshot, addAfterUserSerializeCallback() is only used by createUnsafeBuffer(), other usages can only come from user-land snapshots, which is covered by tests, but what gets run by the built-in snapshot building process is less visible, and the path used by createUnsafeBuffer() isn't reliably visible in user land either. This adds an internal usage counter in debug builds to verify this path when building the built-in snapshot. PR-URL: #60434 Fixes: #60423 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Richard Lau <richard.lau@ibm.com>
1 parent ece4acc commit 2e93650

4 files changed

Lines changed: 45 additions & 3 deletions

File tree

lib/internal/main/mksnapshot.js

Lines changed: 0 additions & 2 deletions

lib/internal/v8/startup_snapshot.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ function setDeserializeMainFunction(callback, data) {
115115
});
116116
}
117117

118+
initializeCallbacks();
118119
module.exports = {
119-
initializeCallbacks,
120120
runDeserializeCallbacks,
121121
throwIfBuildingSnapshot,
122122
// Exposed to require('v8').startupSnapshot
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Flags: --expose-internals --zero-fill-buffers
2+
// Verifies that Buffer.allocUnsafe() allocates initialized memory under --zero-fill-buffers by
3+
// checking the usage count of the relevant native allocator code path.
4+
'use strict';
5+
6+
const common = require('../common');
7+
if (!common.isDebug) {
8+
common.skip('Only works in debug mode');
9+
}
10+
const { internalBinding } = require('internal/test/binding');
11+
const { getGenericUsageCount } = internalBinding('debug');
12+
const assert = require('assert');
13+
14+
const initialUninitializedCount = getGenericUsageCount('NodeArrayBufferAllocator.Allocate.Uninitialized');
15+
const initialZeroFilledCount = getGenericUsageCount('NodeArrayBufferAllocator.Allocate.ZeroFilled');
16+
const buffer = Buffer.allocUnsafe(Buffer.poolSize + 1);
17+
assert(buffer.every((b) => b === 0));
18+
const newUninitializedCount = getGenericUsageCount('NodeArrayBufferAllocator.Allocate.Uninitialized');
19+
const newZeroFilledCount = getGenericUsageCount('NodeArrayBufferAllocator.Allocate.ZeroFilled');
20+
assert.strictEqual(newUninitializedCount, initialUninitializedCount);
21+
assert.notStrictEqual(newZeroFilledCount, initialZeroFilledCount);
Lines changed: 23 additions & 0 deletions

0 commit comments

Comments
 (0)