|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// Flags: --expose-internals |
| 4 | + |
| 5 | +// Confirm that if a custom ArrayBufferAllocator does not define a zeroFill |
| 6 | +// property, that the buffer module will zero-fill when allocUnsafe() is called. |
| 7 | + |
| 8 | +require('../common'); |
| 9 | + |
| 10 | +const assert = require('assert'); |
| 11 | +const buffer = require('buffer'); |
| 12 | + |
| 13 | +// Monkey-patch setupBufferJS() to have an undefined zeroFill. |
| 14 | +const process = require('process'); |
| 15 | +const originalBinding = process.binding; |
| 16 | + |
| 17 | +const binding = originalBinding('buffer'); |
| 18 | +const originalSetup = binding.setupBufferJS; |
| 19 | + |
| 20 | +binding.setupBufferJS = (proto, obj) => { |
| 21 | + originalSetup(proto, obj); |
| 22 | + assert.strictEqual(obj.zeroFill[0], 1); |
| 23 | + delete obj.zeroFill; |
| 24 | +}; |
| 25 | + |
| 26 | +const bindingObj = {}; |
| 27 | + |
| 28 | +binding.setupBufferJS(Buffer.prototype, bindingObj); |
| 29 | +assert.strictEqual(bindingObj.zeroFill, undefined); |
| 30 | + |
| 31 | +process.binding = (bindee) => { |
| 32 | + if (bindee === 'buffer') |
| 33 | + return binding; |
| 34 | + return originalBinding(bindee); |
| 35 | +}; |
| 36 | + |
| 37 | +// Load from file system because internal buffer is already loaded and we're |
| 38 | +// testing code that runs on first load only. |
| 39 | +// Do not move this require() to top of file. It is important that |
| 40 | +// `process.binding('buffer').setupBufferJS` be monkey-patched before this runs. |
| 41 | +const monkeyPatchedBuffer = require('../../lib/buffer'); |
| 42 | + |
| 43 | +// On unpatched buffer, allocUnsafe() should not zero fill memory. It's always |
| 44 | +// possible that a segment of memory is already zeroed out, so try again and |
| 45 | +// again until we succeed or we time out. |
| 46 | +let uninitialized = buffer.Buffer.allocUnsafe(1024); |
| 47 | +while (uninitialized.some((val) => val !== 0)) |
| 48 | + uninitialized = buffer.Buffer.allocUnsafe(1024); |
| 49 | + |
| 50 | +// On monkeypatched buffer, zeroFill property is undefined. allocUnsafe() should |
| 51 | +// zero-fill in that case. |
| 52 | +const zeroFilled = monkeyPatchedBuffer.Buffer.allocUnsafe(1024); |
| 53 | +assert(zeroFilled.every((val) => val === 0)); |
0 commit comments