Skip to content

Commit 4561cf3

Browse files
committed
test: verify heap buffer allocations occur
Check that small typed arrays, including `Buffer`s (unless allocated by `Buffer.allocUnsafe()`), are indeed heap-allocated. PR-URL: #26301 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 5e4aa28 commit 4561cf3

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/node_util.cc

+7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace util {
88

99
using v8::ALL_PROPERTIES;
1010
using v8::Array;
11+
using v8::ArrayBufferView;
1112
using v8::Boolean;
1213
using v8::Context;
1314
using v8::Function;
@@ -174,6 +175,11 @@ void WatchdogHasPendingSigint(const FunctionCallbackInfo<Value>& args) {
174175
args.GetReturnValue().Set(ret);
175176
}
176177

178+
void ArrayBufferViewHasBuffer(const FunctionCallbackInfo<Value>& args) {
179+
CHECK(args[0]->IsArrayBufferView());
180+
args.GetReturnValue().Set(args[0].As<ArrayBufferView>()->HasBuffer());
181+
}
182+
177183
void EnqueueMicrotask(const FunctionCallbackInfo<Value>& args) {
178184
Environment* env = Environment::GetCurrent(args);
179185
Isolate* isolate = env->isolate();
@@ -254,6 +260,7 @@ void Initialize(Local<Object> target,
254260
env->SetMethodNoSideEffect(target, "watchdogHasPendingSigint",
255261
WatchdogHasPendingSigint);
256262

263+
env->SetMethod(target, "arrayBufferViewHasBuffer", ArrayBufferViewHasBuffer);
257264
env->SetMethod(target, "enqueueMicrotask", EnqueueMicrotask);
258265
env->SetMethod(target, "triggerFatalException", FatalException);
259266
Local<Object> constants = Object::New(env->isolate());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Flags: --expose-internals
2+
'use strict';
3+
require('../common');
4+
const assert = require('assert');
5+
const { internalBinding } = require('internal/test/binding');
6+
const { arrayBufferViewHasBuffer } = internalBinding('util');
7+
8+
const tests = [
9+
{ length: 0, expectOnHeap: true },
10+
{ length: 48, expectOnHeap: true },
11+
{ length: 96, expectOnHeap: false },
12+
{ length: 1024, expectOnHeap: false },
13+
];
14+
15+
for (const { length, expectOnHeap } of tests) {
16+
const arrays = [
17+
new Uint8Array(length),
18+
new Uint16Array(length / 2),
19+
new Uint32Array(length / 4),
20+
new Float32Array(length / 4),
21+
new Float64Array(length / 8),
22+
Buffer.alloc(length),
23+
Buffer.allocUnsafeSlow(length)
24+
// Buffer.allocUnsafe() is missing because it may use pooled allocations.
25+
];
26+
27+
for (const array of arrays) {
28+
const isOnHeap = !arrayBufferViewHasBuffer(array);
29+
assert.strictEqual(isOnHeap, expectOnHeap,
30+
`mismatch: ${isOnHeap} vs ${expectOnHeap} ` +
31+
`for ${array.constructor.name}, length = ${length}`);
32+
33+
// Consistency check: Accessing .buffer should create it.
34+
array.buffer;
35+
assert(arrayBufferViewHasBuffer(array));
36+
}
37+
}

0 commit comments

Comments
 (0)