diff --git a/lib/internal/stream_base_commons.js b/lib/internal/stream_base_commons.js index 1c5d935ff6a..69941cb2ee0 100644 --- a/lib/internal/stream_base_commons.js +++ b/lib/internal/stream_base_commons.js @@ -25,7 +25,10 @@ const { reuseOrCreateUnrefTimeout, getTimerDuration, } = require('internal/timers'); -const { isUint8Array } = require('internal/util/types'); +const { + isArrayBuffer, + isUint8Array, +} = require('internal/util/types'); const { clearTimeout } = require('timers'); const { validateFunction } = require('internal/validators'); @@ -192,9 +195,11 @@ function onStreamRead(arrayBuffer) { // If the buffer that will be reused for the next read was detached during // the callback (e.g. by transferring a view into a byte `ReadableStream`), // it can no longer be safely written to. Stop reading instead of handing - // the stale address back to the native read path. + // the stale address back to the native read path. A `SharedArrayBuffer` + // cannot be detached, and the getter throws on one. const reusableBuf = stream[kBuffer]; if (isUint8Array(reusableBuf) && + isArrayBuffer(reusableBuf.buffer) && ArrayBufferPrototypeGetDetached(reusableBuf.buffer)) { result = false; } diff --git a/test/parallel/test-net-onread-buffer-shared.js b/test/parallel/test-net-onread-buffer-shared.js new file mode 100644 index 00000000000..8796b3f4293 --- /dev/null +++ b/test/parallel/test-net-onread-buffer-shared.js @@ -0,0 +1,48 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const net = require('net'); + +// An `onread` buffer backed by a `SharedArrayBuffer` can never be detached. +// Reading into one must not throw from the detached-buffer check. + +const message = Buffer.from('hello world'); + +function test(buffer, check) { + net.createServer(common.mustCall(function(socket) { + this.close(); + socket.end(message); + })).listen(0, common.mustCall(function() { + const incoming = []; + net.connect({ + port: this.address().port, + onread: { + buffer, + callback: common.mustCallAtLeast((nread, buf) => { + check(buf); + assert.ok(buf.buffer instanceof SharedArrayBuffer); + incoming.push(Buffer.from(buf.subarray(0, nread))); + }), + }, + }).on('data', common.mustNotCall()) + .on('error', common.mustNotCall()) + .on('end', common.mustCall(() => { + assert.deepStrictEqual(Buffer.concat(incoming), message); + })); + })); +} + +// Static buffer. +{ + const sockBuf = new Uint8Array(new SharedArrayBuffer(4)); + test(sockBuf, (buf) => assert.strictEqual(buf, sockBuf)); +} + +// Buffer returned by a factory function. +{ + let last; + test(common.mustCallAtLeast(() => { + last = new Uint8Array(new SharedArrayBuffer(4)); + return last; + }), (buf) => assert.strictEqual(buf, last)); +} diff --git a/test/parallel/test-tls-onread-buffer-shared.js b/test/parallel/test-tls-onread-buffer-shared.js new file mode 100644 index 00000000000..2319efa2be3 --- /dev/null +++ b/test/parallel/test-tls-onread-buffer-shared.js @@ -0,0 +1,58 @@ +'use strict'; +const common = require('../common'); +if (!common.hasCrypto) + common.skip('missing crypto'); + +const assert = require('assert'); +const tls = require('tls'); +const fixtures = require('../common/fixtures'); + +// An `onread` buffer backed by a `SharedArrayBuffer` can never be detached. +// Reading into one must not throw from the detached-buffer check. + +const options = { + key: fixtures.readKey('agent2-key.pem'), + cert: fixtures.readKey('agent2-cert.pem'), +}; + +const message = Buffer.from('hello world'); + +function test(buffer, check) { + tls.createServer(options, common.mustCall(function(socket) { + this.close(); + socket.end(message); + })).listen(0, common.mustCall(function() { + const incoming = []; + tls.connect({ + port: this.address().port, + rejectUnauthorized: false, + onread: { + buffer, + callback: common.mustCallAtLeast((nread, buf) => { + check(buf); + assert.ok(buf.buffer instanceof SharedArrayBuffer); + incoming.push(Buffer.from(buf.subarray(0, nread))); + }), + }, + }).on('data', common.mustNotCall()) + .on('error', common.mustNotCall()) + .on('end', common.mustCall(() => { + assert.deepStrictEqual(Buffer.concat(incoming), message); + })); + })); +} + +// Static buffer. +{ + const sockBuf = new Uint8Array(new SharedArrayBuffer(4)); + test(sockBuf, (buf) => assert.strictEqual(buf, sockBuf)); +} + +// Buffer returned by a factory function. +{ + let last; + test(common.mustCallAtLeast(() => { + last = new Uint8Array(new SharedArrayBuffer(4)); + return last; + }), (buf) => assert.strictEqual(buf, last)); +}