Skip to content

Commit

Permalink
test: verify heap buffer allocations occur
Browse files Browse the repository at this point in the history
Check that small typed arrays, including `Buffer`s (unless allocated
by `Buffer.allocUnsafe()`), are indeed heap-allocated.

PR-URL: #26301
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
addaleax committed Mar 1, 2019
1 parent 5e4aa28 commit 4561cf3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/node_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace util {

using v8::ALL_PROPERTIES;
using v8::Array;
using v8::ArrayBufferView;
using v8::Boolean;
using v8::Context;
using v8::Function;
Expand Down Expand Up @@ -174,6 +175,11 @@ void WatchdogHasPendingSigint(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(ret);
}

void ArrayBufferViewHasBuffer(const FunctionCallbackInfo<Value>& args) {
CHECK(args[0]->IsArrayBufferView());
args.GetReturnValue().Set(args[0].As<ArrayBufferView>()->HasBuffer());
}

void EnqueueMicrotask(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Isolate* isolate = env->isolate();
Expand Down Expand Up @@ -254,6 +260,7 @@ void Initialize(Local<Object> target,
env->SetMethodNoSideEffect(target, "watchdogHasPendingSigint",
WatchdogHasPendingSigint);

env->SetMethod(target, "arrayBufferViewHasBuffer", ArrayBufferViewHasBuffer);
env->SetMethod(target, "enqueueMicrotask", EnqueueMicrotask);
env->SetMethod(target, "triggerFatalException", FatalException);
Local<Object> constants = Object::New(env->isolate());
Expand Down
37 changes: 37 additions & 0 deletions test/parallel/test-buffer-backing-arraybuffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Flags: --expose-internals
'use strict';
require('../common');
const assert = require('assert');
const { internalBinding } = require('internal/test/binding');
const { arrayBufferViewHasBuffer } = internalBinding('util');

const tests = [
{ length: 0, expectOnHeap: true },
{ length: 48, expectOnHeap: true },
{ length: 96, expectOnHeap: false },
{ length: 1024, expectOnHeap: false },
];

for (const { length, expectOnHeap } of tests) {
const arrays = [
new Uint8Array(length),
new Uint16Array(length / 2),
new Uint32Array(length / 4),
new Float32Array(length / 4),
new Float64Array(length / 8),
Buffer.alloc(length),
Buffer.allocUnsafeSlow(length)
// Buffer.allocUnsafe() is missing because it may use pooled allocations.
];

for (const array of arrays) {
const isOnHeap = !arrayBufferViewHasBuffer(array);
assert.strictEqual(isOnHeap, expectOnHeap,
`mismatch: ${isOnHeap} vs ${expectOnHeap} ` +
`for ${array.constructor.name}, length = ${length}`);

// Consistency check: Accessing .buffer should create it.
array.buffer;
assert(arrayBufferViewHasBuffer(array));
}
}

0 comments on commit 4561cf3

Please sign in to comment.