diff --git a/lib/internal/main/mksnapshot.js b/lib/internal/main/mksnapshot.js index 63df8c50087..b06b1a70bf4 100644 --- a/lib/internal/main/mksnapshot.js +++ b/lib/internal/main/mksnapshot.js @@ -21,7 +21,6 @@ const { emitExperimentalWarning } = require('internal/util'); const { emitWarningSync } = require('internal/process/warning'); const { - initializeCallbacks, namespace: { addDeserializeCallback, isBuildingSnapshot, @@ -139,7 +138,6 @@ function requireForUserSnapshot(id) { function main() { prepareMainThreadExecution(false, false); - initializeCallbacks(); // In a context created for building snapshots, V8 does not install Error.stackTraceLimit and as // a result, if an error is created during the snapshot building process, error.stack would be diff --git a/lib/internal/v8/startup_snapshot.js b/lib/internal/v8/startup_snapshot.js index 01204b96239..af5fb07925d 100644 --- a/lib/internal/v8/startup_snapshot.js +++ b/lib/internal/v8/startup_snapshot.js @@ -115,8 +115,8 @@ function setDeserializeMainFunction(callback, data) { }); } +initializeCallbacks(); module.exports = { - initializeCallbacks, runDeserializeCallbacks, throwIfBuildingSnapshot, // Exposed to require('v8').startupSnapshot diff --git a/test/parallel/test-buffer-alloc-unsafe-is-initialized-with-zero-fill-flag.js b/test/parallel/test-buffer-alloc-unsafe-is-initialized-with-zero-fill-flag.js new file mode 100644 index 00000000000..075fa94e418 --- /dev/null +++ b/test/parallel/test-buffer-alloc-unsafe-is-initialized-with-zero-fill-flag.js @@ -0,0 +1,21 @@ +// Flags: --expose-internals --zero-fill-buffers +// Verifies that Buffer.allocUnsafe() allocates initialized memory under --zero-fill-buffers by +// checking the usage count of the relevant native allocator code path. +'use strict'; + +const common = require('../common'); +if (!common.isDebug) { + common.skip('Only works in debug mode'); +} +const { internalBinding } = require('internal/test/binding'); +const { getGenericUsageCount } = internalBinding('debug'); +const assert = require('assert'); + +const initialUninitializedCount = getGenericUsageCount('NodeArrayBufferAllocator.Allocate.Uninitialized'); +const initialZeroFilledCount = getGenericUsageCount('NodeArrayBufferAllocator.Allocate.ZeroFilled'); +const buffer = Buffer.allocUnsafe(Buffer.poolSize + 1); +assert(buffer.every((b) => b === 0)); +const newUninitializedCount = getGenericUsageCount('NodeArrayBufferAllocator.Allocate.Uninitialized'); +const newZeroFilledCount = getGenericUsageCount('NodeArrayBufferAllocator.Allocate.ZeroFilled'); +assert.strictEqual(newUninitializedCount, initialUninitializedCount); +assert.notStrictEqual(newZeroFilledCount, initialZeroFilledCount); diff --git a/test/parallel/test-buffer-alloc-unsafe-is-uninitialized.js b/test/parallel/test-buffer-alloc-unsafe-is-uninitialized.js new file mode 100644 index 00000000000..557851c8a7c --- /dev/null +++ b/test/parallel/test-buffer-alloc-unsafe-is-uninitialized.js @@ -0,0 +1,23 @@ +// Flags: --expose-internals +// Verifies that Buffer.allocUnsafe() indeed allocates uninitialized memory by checking +// the usage count of the relevant native allocator code path. +'use strict'; + +const common = require('../common'); +if (!common.isDebug) { + common.skip('Only works in debug mode'); +} +const { internalBinding } = require('internal/test/binding'); +const { getGenericUsageCount } = internalBinding('debug'); +const assert = require('assert'); + +const initialUninitializedCount = getGenericUsageCount('NodeArrayBufferAllocator.Allocate.Uninitialized'); +const initialZeroFilledCount = getGenericUsageCount('NodeArrayBufferAllocator.Allocate.ZeroFilled'); +Buffer.allocUnsafe(Buffer.poolSize + 1); +// We can't reliably check the contents of the buffer here because the OS or memory allocator +// used might zero-fill memory for us, or they might happen to return reused memory that was +// previously used by a process that zero-filled it. So instead we just check the usage counts. +const newUninitializedCount = getGenericUsageCount('NodeArrayBufferAllocator.Allocate.Uninitialized'); +const newZeroFilledCount = getGenericUsageCount('NodeArrayBufferAllocator.Allocate.ZeroFilled'); +assert.notStrictEqual(newUninitializedCount, initialUninitializedCount); +assert.strictEqual(newZeroFilledCount, initialZeroFilledCount);