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