-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
lib: short-circuit WebIDL BufferSource SAB check #62833
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
panva
wants to merge
3
commits into
nodejs:main
Choose a base branch
from
panva:fast-buffersource
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+283
−7
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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' }, | ||
| ); | ||
| }); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.buffergetters which already perform brand checks on theArrayBufferView, I don't think we can get a forged array buffer here. Unless you first didObject.setPrototypeOf(view.buffer, ArrayBuffer.prototype)? Is that allowed, and does that persist onview.buffer? 🤔Alternatively, we could call the getter of a property on
SharedArrayBuffer.prototypeand check if it throws or not, see webidl2js. I don't know if that's faster than calling into C++ though.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.I would imagine that creating a try-catch scope is slower, but it's worth testing!
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.