Skip to content

Commit 454caf1

Browse files
committed
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.
1 parent 0f2ecc3 commit 454caf1

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

lib/internal/main/mksnapshot.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ const { emitExperimentalWarning } = require('internal/util');
2121
const { emitWarningSync } = require('internal/process/warning');
2222

2323
const {
24-
initializeCallbacks,
2524
namespace: {
2625
addDeserializeCallback,
2726
isBuildingSnapshot,
@@ -139,7 +138,6 @@ function requireForUserSnapshot(id) {
139138

140139
function main() {
141140
prepareMainThreadExecution(false, false);
142-
initializeCallbacks();
143141

144142
// In a context created for building snapshots, V8 does not install Error.stackTraceLimit and as
145143
// a result, if an error is created during the snapshot building process, error.stack would be

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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Flags: --expose-internals
2+
// Verifies that Buffer.allocUnsafe() indeed allocates uninitialized memory by checking
3+
// 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 initialCount = getGenericUsageCount('NodeArrayBufferAllocator.Allocate.Uninitialized');
15+
Buffer.allocUnsafe(Buffer.poolSize + 1);
16+
const newCount = getGenericUsageCount('NodeArrayBufferAllocator.Allocate.Uninitialized');
17+
assert.notStrictEqual(newCount, initialCount);

0 commit comments

Comments
 (0)