Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions benchmark/misc/webidl-buffer-source.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use strict';

const common = require('../common.js');

const bench = common.createBenchmark(main, {
input: [
'arraybuffer',
'buffer',
'dataview',
'uint8array',
'uint8clampedarray',
'int8array',
'uint16array',
'int16array',
'uint32array',
'int32array',
'float16array',
'float32array',
'float64array',
'bigint64array',
'biguint64array',
],
n: [1e6],
}, { flags: ['--expose-internals'] });

function main({ n, input }) {
const { converters } = require('internal/webidl');

let value;
switch (input) {
case 'arraybuffer': value = new ArrayBuffer(32); break;
case 'buffer': value = Buffer.alloc(32); break;
case 'dataview': value = new DataView(new ArrayBuffer(32)); break;
case 'uint8array': value = new Uint8Array(32); break;
case 'uint8clampedarray': value = new Uint8ClampedArray(32); break;
case 'int8array': value = new Int8Array(32); break;
case 'uint16array': value = new Uint16Array(16); break;
case 'int16array': value = new Int16Array(16); break;
case 'uint32array': value = new Uint32Array(8); break;
case 'int32array': value = new Int32Array(8); break;
case 'float16array': value = new Float16Array(16); break;
case 'float32array': value = new Float32Array(8); break;
case 'float64array': value = new Float64Array(4); break;
case 'bigint64array': value = new BigInt64Array(4); break;
case 'biguint64array': value = new BigUint64Array(4); break;
}

const opts = { prefix: 'test', context: 'test' };

bench.start();
for (let i = 0; i < n; i++)
converters.BufferSource(value, opts);
bench.end(n);
}
23 changes: 17 additions & 6 deletions lib/internal/webidl.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const {
ArrayBufferIsView,
ArrayBufferPrototype,
ArrayPrototypePush,
ArrayPrototypeToSorted,
DataViewPrototypeGetBuffer,
Expand Down Expand Up @@ -33,8 +34,8 @@ const {
const { kEmptyObject } = require('internal/util');
const {
isArrayBuffer,
isDataView,
isSharedArrayBuffer,
isTypedArray,
} = require('internal/util/types');

const converters = { __proto__: null };
Expand Down Expand Up @@ -390,9 +391,19 @@ function createInterfaceConverter(name, I) {
};
}

function getDataViewOrTypedArrayBuffer(V) {
return isDataView(V) ?
DataViewPrototypeGetBuffer(V) : TypedArrayPrototypeGetBuffer(V);
// Returns the [[ViewedArrayBuffer]] of an ArrayBufferView without leaving JS.
function getViewedArrayBuffer(V) {
return isTypedArray(V) ?
TypedArrayPrototypeGetBuffer(V) : DataViewPrototypeGetBuffer(V);
}

// Returns `true` if `buffer` is a `SharedArrayBuffer`. The prototype-chain
// check is a cheap V8 builtin that fast-accepts every non-tampered
// `ArrayBuffer`; only buffers whose prototype chain has been detached or
// reassigned (and real SABs) fall through to the authoritative brand check.
function isSharedArrayBufferBacking(buffer) {
return !ObjectPrototypeIsPrototypeOf(ArrayBufferPrototype, buffer) &&
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Theoretically non-compliant for Object.setPrototypeOf(new SharedArrayBuffer(), ArrayBuffer.prototype) – would presumably be a fairly useless object, I don't know how much we need to care about that sort of thing.

Copy link
Copy Markdown
Member Author

@panva panva Apr 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, I don't think I have a way of dealing with that. 🤷 nor do I have an opinion on caring about it. What do others think?

Copy link
Copy Markdown
Contributor

@MattiasBuelens MattiasBuelens Apr 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is only ever used on buffers returned by getViewedArrayBuffer, right? Since that function uses the primordial .buffer getters which already perform brand checks on the ArrayBufferView, I don't think we can get a forged array buffer here. Unless you first did Object.setPrototypeOf(view.buffer, ArrayBuffer.prototype)? Is that allowed, and does that persist on view.buffer? 🤔

Alternatively, we could call the getter of a property on SharedArrayBuffer.prototype and check if it throws or not, see webidl2js. I don't know if that's faster than calling into C++ though.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since that function uses the primordial .buffer getters which already perform brand checks on the ArrayBufferView, I don't think we can get a forged array buffer here. Unless you first did Object.setPrototypeOf(view.buffer, ArrayBuffer.prototype)? Is that allowed, and does that persist on view.buffer?

Technically there's nothing wrong with new Uint8Array(Object.setPrototypeOf(new SharedArrayBuffer(), ArrayBuffer.prototype)), since the ABV uses internal operations to manipulate the underlying buffer, not prototype methods. There would be no reason to do this, obviously, but it's theoretically valid.

Alternatively, we could call the getter of a property on SharedArrayBuffer.prototype and check if it throws or not

I would imagine that creating a try-catch scope is slower, but it's worth testing!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we'll go down from 8x improvement to 7x, i say it's worth it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated bench in the PR description.

isSharedArrayBuffer(buffer);
}

// https://webidl.spec.whatwg.org/#ArrayBufferView
Expand All @@ -402,7 +413,7 @@ converters.ArrayBufferView = (V, opts = kEmptyObject) => {
'is not an ArrayBufferView.',
opts);
}
if (isSharedArrayBuffer(getDataViewOrTypedArrayBuffer(V))) {
if (isSharedArrayBufferBacking(getViewedArrayBuffer(V))) {
throw makeException(
'is a view on a SharedArrayBuffer, which is not allowed.',
opts);
Expand All @@ -414,7 +425,7 @@ converters.ArrayBufferView = (V, opts = kEmptyObject) => {
// https://webidl.spec.whatwg.org/#BufferSource
converters.BufferSource = (V, opts = kEmptyObject) => {
if (ArrayBufferIsView(V)) {
if (isSharedArrayBuffer(getDataViewOrTypedArrayBuffer(V))) {
if (isSharedArrayBufferBacking(getViewedArrayBuffer(V))) {
throw makeException(
'is a view on a SharedArrayBuffer, which is not allowed.',
opts);
Expand Down
188 changes: 188 additions & 0 deletions test/parallel/test-internal-webidl-buffer-source.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
// Flags: --expose-internals
'use strict';

require('../common');
const assert = require('assert');
const { test } = require('node:test');

const { converters } = require('internal/webidl');

const TYPED_ARRAY_CTORS = [
Uint8Array, Int8Array, Uint8ClampedArray,
Uint16Array, Int16Array,
Uint32Array, Int32Array,
Float16Array, Float32Array, Float64Array,
BigInt64Array, BigUint64Array,
];

test('BufferSource accepts ArrayBuffer', () => {
const ab = new ArrayBuffer(8);
assert.strictEqual(converters.BufferSource(ab), ab);
});

test('BufferSource accepts all TypedArray kinds', () => {
for (const Ctor of TYPED_ARRAY_CTORS) {
const ta = new Ctor(4);
assert.strictEqual(converters.BufferSource(ta), ta);
}
});

test('BufferSource accepts Buffer', () => {
const buf = Buffer.alloc(8);
assert.strictEqual(converters.BufferSource(buf), buf);
});

test('BufferSource accepts DataView', () => {
const dv = new DataView(new ArrayBuffer(8));
assert.strictEqual(converters.BufferSource(dv), dv);
});

test('BufferSource accepts ArrayBuffer subclass instance', () => {
class MyAB extends ArrayBuffer {}
const sub = new MyAB(8);
assert.strictEqual(converters.BufferSource(sub), sub);
});

test('BufferSource accepts TypedArray with null prototype', () => {
const ta = new Uint8Array(4);
Object.setPrototypeOf(ta, null);
assert.strictEqual(converters.BufferSource(ta), ta);
});

test('BufferSource accepts DataView with null prototype', () => {
const dv = new DataView(new ArrayBuffer(4));
Object.setPrototypeOf(dv, null);
assert.strictEqual(converters.BufferSource(dv), dv);
});

test('BufferSource accepts ArrayBuffer with null prototype', () => {
const ab = new ArrayBuffer(4);
Object.setPrototypeOf(ab, null);
assert.strictEqual(converters.BufferSource(ab), ab);
});

test('BufferSource rejects SharedArrayBuffer', () => {
assert.throws(
() => converters.BufferSource(new SharedArrayBuffer(4)),
{ code: 'ERR_INVALID_ARG_TYPE' },
);
});

test('BufferSource rejects SAB-backed TypedArray', () => {
const view = new Uint8Array(new SharedArrayBuffer(4));
assert.throws(
() => converters.BufferSource(view),
{ code: 'ERR_INVALID_ARG_TYPE' },
);
});

test('BufferSource rejects SAB-backed DataView', () => {
const dv = new DataView(new SharedArrayBuffer(4));
assert.throws(
() => converters.BufferSource(dv),
{ code: 'ERR_INVALID_ARG_TYPE' },
);
});

test('BufferSource accepts a detached ArrayBuffer', () => {
const ab = new ArrayBuffer(8);
structuredClone(ab, { transfer: [ab] });
assert.strictEqual(ab.byteLength, 0);
assert.strictEqual(converters.BufferSource(ab), ab);
});

test('BufferSource rejects objects with a forged @@toStringTag', () => {
const fake = { [Symbol.toStringTag]: 'Uint8Array' };
assert.throws(
() => converters.BufferSource(fake),
{ code: 'ERR_INVALID_ARG_TYPE' },
);
});

for (const value of [null, undefined, 0, 1, 1n, '', 'x', true, Symbol('s'), [],
{}, () => {}]) {
test(`BufferSource rejects ${typeof value} ${String(value)}`, () => {
assert.throws(
() => converters.BufferSource(value),
{ code: 'ERR_INVALID_ARG_TYPE' },
);
});
}

test('ArrayBufferView accepts all TypedArray kinds', () => {
for (const Ctor of TYPED_ARRAY_CTORS) {
const ta = new Ctor(4);
assert.strictEqual(converters.ArrayBufferView(ta), ta);
}
});

test('ArrayBufferView accepts DataView', () => {
const dv = new DataView(new ArrayBuffer(8));
assert.strictEqual(converters.ArrayBufferView(dv), dv);
});

test('ArrayBufferView accepts TypedArray subclass instance', () => {
class MyU8 extends Uint8Array {}
const sub = new MyU8(4);
assert.strictEqual(converters.ArrayBufferView(sub), sub);
});

test('ArrayBufferView accepts TypedArray with null prototype', () => {
const ta = new Uint8Array(4);
Object.setPrototypeOf(ta, null);
assert.strictEqual(converters.ArrayBufferView(ta), ta);
});

test('ArrayBufferView accepts DataView with null prototype', () => {
const dv = new DataView(new ArrayBuffer(4));
Object.setPrototypeOf(dv, null);
assert.strictEqual(converters.ArrayBufferView(dv), dv);
});

test('ArrayBufferView rejects raw ArrayBuffer', () => {
assert.throws(
() => converters.ArrayBufferView(new ArrayBuffer(4)),
{ code: 'ERR_INVALID_ARG_TYPE' },
);
});

test('ArrayBufferView rejects raw SharedArrayBuffer', () => {
assert.throws(
() => converters.ArrayBufferView(new SharedArrayBuffer(4)),
{ code: 'ERR_INVALID_ARG_TYPE' },
);
});

test('ArrayBufferView rejects SAB-backed TypedArray', () => {
const view = new Uint8Array(new SharedArrayBuffer(4));
assert.throws(
() => converters.ArrayBufferView(view),
{ code: 'ERR_INVALID_ARG_TYPE' },
);
});

test('ArrayBufferView rejects SAB-backed DataView', () => {
const dv = new DataView(new SharedArrayBuffer(4));
assert.throws(
() => converters.ArrayBufferView(dv),
{ code: 'ERR_INVALID_ARG_TYPE' },
);
});

test('ArrayBufferView rejects objects with a forged @@toStringTag', () => {
const fake = { [Symbol.toStringTag]: 'Uint8Array' };
assert.throws(
() => converters.ArrayBufferView(fake),
{ code: 'ERR_INVALID_ARG_TYPE' },
);
});

for (const value of [null, undefined, 0, 1, 1n, '', 'x', true, Symbol('s'), [],
{}, () => {}]) {
test(`ArrayBufferView rejects ${typeof value} ${String(value)}`, () => {
assert.throws(
() => converters.ArrayBufferView(value),
{ code: 'ERR_INVALID_ARG_TYPE' },
);
});
}
Loading