Skip to content

Commit efd34e2

Browse files
stream: skip detached check for shared onread buffers
PR #67542 introduced a small edge case regression for SharedArrayBuffers. The detached getter throws on a SAB, causing process crashes with an uncaught TypeError after the first read. Since onread.buffer is typed as including Uint8Array, we need to consider SAB-backed ones too. Refs: #67542 Signed-off-by: Ethan Arrowood <ethan@arrowood.dev>
1 parent 1a8cf16 commit efd34e2

3 files changed

Lines changed: 113 additions & 2 deletions

File tree

‎lib/internal/stream_base_commons.js‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ const {
2525
reuseOrCreateUnrefTimeout,
2626
getTimerDuration,
2727
} = require('internal/timers');
28-
const { isUint8Array } = require('internal/util/types');
28+
const {
29+
isArrayBuffer,
30+
isUint8Array,
31+
} = require('internal/util/types');
2932
const { clearTimeout } = require('timers');
3033
const { validateFunction } = require('internal/validators');
3134

@@ -192,9 +195,11 @@ function onStreamRead(arrayBuffer) {
192195
// If the buffer that will be reused for the next read was detached during
193196
// the callback (e.g. by transferring a view into a byte `ReadableStream`),
194197
// it can no longer be safely written to. Stop reading instead of handing
195-
// the stale address back to the native read path.
198+
// the stale address back to the native read path. A `SharedArrayBuffer`
199+
// cannot be detached, and the getter throws on one.
196200
const reusableBuf = stream[kBuffer];
197201
if (isUint8Array(reusableBuf) &&
202+
isArrayBuffer(reusableBuf.buffer) &&
198203
ArrayBufferPrototypeGetDetached(reusableBuf.buffer)) {
199204
result = false;
200205
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const net = require('net');
5+
6+
// An `onread` buffer backed by a `SharedArrayBuffer` can never be detached.
7+
// Reading into one must not throw from the detached-buffer check.
8+
9+
const message = Buffer.from('hello world');
10+
11+
function test(buffer, check) {
12+
net.createServer(common.mustCall(function(socket) {
13+
this.close();
14+
socket.end(message);
15+
})).listen(0, common.mustCall(function() {
16+
const incoming = [];
17+
net.connect({
18+
port: this.address().port,
19+
onread: {
20+
buffer,
21+
callback: common.mustCallAtLeast((nread, buf) => {
22+
check(buf);
23+
assert.ok(buf.buffer instanceof SharedArrayBuffer);
24+
incoming.push(Buffer.from(buf.subarray(0, nread)));
25+
}),
26+
},
27+
}).on('data', common.mustNotCall())
28+
.on('error', common.mustNotCall())
29+
.on('end', common.mustCall(() => {
30+
assert.deepStrictEqual(Buffer.concat(incoming), message);
31+
}));
32+
}));
33+
}
34+
35+
// Static buffer.
36+
{
37+
const sockBuf = new Uint8Array(new SharedArrayBuffer(4));
38+
test(sockBuf, (buf) => assert.strictEqual(buf, sockBuf));
39+
}
40+
41+
// Buffer returned by a factory function.
42+
{
43+
let last;
44+
test(common.mustCallAtLeast(() => {
45+
last = new Uint8Array(new SharedArrayBuffer(4));
46+
return last;
47+
}), (buf) => assert.strictEqual(buf, last));
48+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
'use strict';
2+
const common = require('../common');
3+
if (!common.hasCrypto)
4+
common.skip('missing crypto');
5+
6+
const assert = require('assert');
7+
const tls = require('tls');
8+
const fixtures = require('../common/fixtures');
9+
10+
// An `onread` buffer backed by a `SharedArrayBuffer` can never be detached.
11+
// Reading into one must not throw from the detached-buffer check.
12+
13+
const options = {
14+
key: fixtures.readKey('agent2-key.pem'),
15+
cert: fixtures.readKey('agent2-cert.pem'),
16+
};
17+
18+
const message = Buffer.from('hello world');
19+
20+
function test(buffer, check) {
21+
tls.createServer(options, common.mustCall(function(socket) {
22+
this.close();
23+
socket.end(message);
24+
})).listen(0, common.mustCall(function() {
25+
const incoming = [];
26+
tls.connect({
27+
port: this.address().port,
28+
rejectUnauthorized: false,
29+
onread: {
30+
buffer,
31+
callback: common.mustCallAtLeast((nread, buf) => {
32+
check(buf);
33+
assert.ok(buf.buffer instanceof SharedArrayBuffer);
34+
incoming.push(Buffer.from(buf.subarray(0, nread)));
35+
}),
36+
},
37+
}).on('data', common.mustNotCall())
38+
.on('error', common.mustNotCall())
39+
.on('end', common.mustCall(() => {
40+
assert.deepStrictEqual(Buffer.concat(incoming), message);
41+
}));
42+
}));
43+
}
44+
45+
// Static buffer.
46+
{
47+
const sockBuf = new Uint8Array(new SharedArrayBuffer(4));
48+
test(sockBuf, (buf) => assert.strictEqual(buf, sockBuf));
49+
}
50+
51+
// Buffer returned by a factory function.
52+
{
53+
let last;
54+
test(common.mustCallAtLeast(() => {
55+
last = new Uint8Array(new SharedArrayBuffer(4));
56+
return last;
57+
}), (buf) => assert.strictEqual(buf, last));
58+
}

0 commit comments

Comments
 (0)