GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — test_runner: do not crash on stdout that mimics a v8 frame · nodejs/node@d981dd3

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib/internal/test_runner
    • runner.js
  • test/parallel
    • test-runner-v8-deserializer.mjs
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/internal/test_runner/runner.js‎

Copy file name to clipboardExpand all lines: lib/internal/test_runner/runner.js
+65-39Lines changed: 65 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,6 @@ const {
8282
kTestTimeoutFailure,
8383
Test,
8484
} = require('internal/test_runner/test');
85-
const { FastBuffer } = require('internal/buffer');
86-
8785
const {
8886
createRandomSeed,
8987
convertStringToRegExp,
@@ -447,59 +445,87 @@ class FileTest extends Test {
447445
}
448446
}
449447
#processRawBuffer() {
450-
// This method is called when it is known that there is at least one message
451-
let bufferHead = this.#rawBuffer[0];
452-
let headerIndex = bufferHead.indexOf(v8Header);
453-
let nonSerialized = new FastBuffer();
454-
455-
while (bufferHead && headerIndex !== 0) {
456-
const nonSerializedData = headerIndex === -1 ?
457-
bufferHead :
458-
bufferHead.slice(0, headerIndex);
459-
nonSerialized = Buffer.concat([nonSerialized, nonSerializedData]);
460-
this.#rawBufferSize -= TypedArrayPrototypeGetLength(nonSerializedData);
461-
if (headerIndex === -1) {
462-
ArrayPrototypeShift(this.#rawBuffer);
463-
} else {
464-
this.#rawBuffer[0] = TypedArrayPrototypeSubarray(bufferHead, headerIndex);
448+
// This method is called when it is known that there is at least one message.
449+
// Each pass looks at the head of the buffer and handles exactly one of four
450+
// cases, then re-checks the new head. Recovery from stray stdout that only
451+
// mimics a frame happens inline here, so no separate resync pass is needed.
452+
while (this.#rawBuffer.length > 0) {
453+
const bufferHead = this.#rawBuffer[0];
454+
const headerIndex = bufferHead.indexOf(v8Header);
455+
456+
// 1. The head does not start with a frame header. Emit the bytes before
457+
// the next header, or the whole head when there is none, as stdout and
458+
// advance to the next header.
459+
if (headerIndex !== 0) {
460+
const nonSerialized = headerIndex === -1 ?
461+
bufferHead : TypedArrayPrototypeSubarray(bufferHead, 0, headerIndex);
462+
this.addToReport({
463+
__proto__: null,
464+
type: 'test:stdout',
465+
data: { __proto__: null, file: this.name, message: nonSerialized.toString('utf-8') },
466+
});
467+
this.#rawBufferSize -= TypedArrayPrototypeGetLength(nonSerialized);
468+
if (headerIndex === -1) {
469+
ArrayPrototypeShift(this.#rawBuffer);
470+
} else {
471+
this.#rawBuffer[0] = TypedArrayPrototypeSubarray(bufferHead, headerIndex);
472+
}
473+
continue;
465474
}
466-
bufferHead = this.#rawBuffer[0];
467-
headerIndex = bufferHead?.indexOf(v8Header);
468-
}
469-
470-
if (TypedArrayPrototypeGetLength(nonSerialized) > 0) {
471-
this.addToReport({
472-
__proto__: null,
473-
type: 'test:stdout',
474-
data: { __proto__: null, file: this.name, message: nonSerialized.toString('utf-8') },
475-
});
476-
}
477475

478-
while (bufferHead?.length >= kSerializedSizeHeader) {
479-
// We call `readUInt32BE` manually here, because this is faster than first converting
480-
// it to a buffer and using `readUInt32BE` on that.
476+
// 2. The head starts with a header but the whole frame has not arrived
477+
// yet. Stop and wait for more data.
478+
if (TypedArrayPrototypeGetLength(bufferHead) < kSerializedSizeHeader) {
479+
break;
480+
}
481+
// We call `readUInt32BE` manually here, because this is faster than first
482+
// converting it to a buffer and using `readUInt32BE` on that.
481483
const fullMessageSize = ((
482484
bufferHead[kV8HeaderLength] << 24 |
483485
bufferHead[kV8HeaderLength + 1] << 16 |
484486
bufferHead[kV8HeaderLength + 2] << 8 |
485487
bufferHead[kV8HeaderLength + 3]
486488
) >>> 0) + kSerializedSizeHeader;
487-
488-
if (this.#rawBufferSize < fullMessageSize) break;
489+
if (this.#rawBufferSize < fullMessageSize) {
490+
break;
491+
}
489492

490493
const concatenatedBuffer = this.#rawBuffer.length === 1 ?
491-
this.#rawBuffer[0] : Buffer.concat(this.#rawBuffer, this.#rawBufferSize);
494+
bufferHead : Buffer.concat(this.#rawBuffer, this.#rawBufferSize);
495+
496+
// 3. The head only mimics a frame. A real frame repeats the v8 header at
497+
// the start of its payload, right before the serialized value, so a
498+
// payload too short for that header, or one that does not start with
499+
// it, is stray stdout. Emit one byte and let the next pass resync on
500+
// the following header. A genuine frame that fails to deserialize is
501+
// left to throw, so real report-protocol regressions are not hidden.
502+
if (fullMessageSize - kSerializedSizeHeader < kV8HeaderLength ||
503+
concatenatedBuffer.indexOf(v8Header, kSerializedSizeHeader) !== kSerializedSizeHeader) {
504+
this.addToReport({
505+
__proto__: null,
506+
type: 'test:stdout',
507+
data: { __proto__: null, file: this.name, message: StringFromCharCode(bufferHead[0]) },
508+
});
509+
if (TypedArrayPrototypeGetLength(bufferHead) === 1) {
510+
ArrayPrototypeShift(this.#rawBuffer);
511+
} else {
512+
this.#rawBuffer[0] = TypedArrayPrototypeSubarray(bufferHead, 1);
513+
}
514+
this.#rawBufferSize--;
515+
continue;
516+
}
492517

518+
// 4. A real frame. Deserialize it and continue from the remaining bytes.
493519
const deserializer = new DefaultDeserializer(
494520
TypedArrayPrototypeSubarray(concatenatedBuffer, kSerializedSizeHeader, fullMessageSize),
495521
);
496-
497-
bufferHead = TypedArrayPrototypeSubarray(concatenatedBuffer, fullMessageSize);
498-
this.#rawBufferSize = TypedArrayPrototypeGetLength(bufferHead);
499-
this.#rawBuffer = this.#rawBufferSize !== 0 ? [bufferHead] : [];
500-
501522
deserializer.readHeader();
502523
const item = deserializer.readValue();
524+
525+
const remaining = TypedArrayPrototypeSubarray(concatenatedBuffer, fullMessageSize);
526+
this.#rawBufferSize = TypedArrayPrototypeGetLength(remaining);
527+
this.#rawBuffer = this.#rawBufferSize !== 0 ? [remaining] : [];
528+
503529
this.addToReport(item);
504530
}
505531
}
Collapse file

‎test/parallel/test-runner-v8-deserializer.mjs‎

Copy file name to clipboardExpand all lines: test/parallel/test-runner-v8-deserializer.mjs
+110Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,38 @@ const oversizedLengthStdout = String.fromCharCode(oversizedLengthHeader[0]) +
3939
Buffer.from(oversizedLengthHeader.subarray(1)).toString('utf-8');
4040
const unsignedOversizedLengthStdout = String.fromCharCode(unsignedOversizedLengthHeader[0]) +
4141
Buffer.from(unsignedOversizedLengthHeader.subarray(1)).toString('utf-8');
42+
// FF 0F followed by a small, plausible size (8) and 8 payload bytes. Unlike the
43+
// oversized headers above, this passes the size check, but its payload does not
44+
// begin with the inner v8 header a real frame carries, so it is treated as
45+
// stdout instead of reaching the deserializer.
46+
// Regression fixture for https://github.com/nodejs/node/issues/66164
47+
const plausibleSizeFalseHeader = Buffer.from([
48+
0xff, 0x0f, // V8 serializer header magic
49+
0x00, 0x00, 0x00, 0x08, // Payload size of 8 bytes
50+
0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, // "ABCDEFGH", not a real payload
51+
]);
52+
const plausibleSizeFalseHeaderStdout = String.fromCharCode(plausibleSizeFalseHeader[0]) +
53+
Buffer.from(plausibleSizeFalseHeader.subarray(1)).toString('utf-8');
54+
// FF 0F, a valid size, then the inner v8 header a real frame repeats, followed
55+
// by a byte that is not a valid serialized value. This passes the inner header
56+
// check and reaches the deserializer, which throws. This is what a genuine
57+
// report-protocol regression looks like, so the parser must let the error
58+
// surface instead of hiding it as stdout.
59+
const headeredCorruptFrame = Buffer.from([
60+
0xff, 0x0f, // Outer v8 serializer header magic
61+
0x00, 0x00, 0x00, 0x03, // Payload size of 3 bytes
62+
0xff, 0x0f, // Inner v8 header that a real frame repeats
63+
0xee, // Not a valid serialized value
64+
]);
65+
// FF 0F with a declared size of 1, then more header bytes. The payload is
66+
// shorter than the inner v8 header a real frame carries, so it can never be a
67+
// real frame. The length guard must reject it as stdout without reaching the
68+
// deserializer.
69+
const shortPayloadFalseHeader = Buffer.from([
70+
0xff, 0x0f, // Outer v8 serializer header magic
71+
0x00, 0x00, 0x00, 0x01, // Payload size of 1 byte, too short for a header
72+
0xff, 0x0f, // Trailing bytes that also look like a header
73+
]);
4274

4375
function collectStdout(reported) {
4476
return reported
@@ -169,6 +201,84 @@ describe('v8 deserializer', common.mustCall(() => {
169201
assert.strictEqual(collectStdout(reported), oversizedLengthStdout);
170202
});
171203

204+
it('should not crash when stdout mimics a v8 frame with a plausible size', async () => {
205+
// Regression test for https://github.com/nodejs/node/issues/66164
206+
// The payload does not start with the inner v8 header that a real frame
207+
// carries, so the parser treats the bytes as stdout instead of handing
208+
// them to the deserializer and aborting the whole run.
209+
const reported = await collectReported([plausibleSizeFalseHeader]);
210+
assert(reported.every((event) => event.type === 'test:stdout'));
211+
assert.strictEqual(collectStdout(reported), plausibleSizeFalseHeaderStdout);
212+
});
213+
214+
it('should resync live and report a real message after a false frame', async () => {
215+
// Feed the poison bytes then a real message but never call drain(). Recovery
216+
// must happen live, so the real event is reported right away. If resync only
217+
// ran at shutdown, the diagnostic would still be buffered and missing here.
218+
// The reporter is a stream, so flush it with end() and finished() before
219+
// asserting, rather than reading it synchronously.
220+
fileTest.parseMessage(plausibleSizeFalseHeader);
221+
chunks.forEach((chunk) => fileTest.parseMessage(chunk));
222+
fileTest.reporter.end();
223+
await finished(fileTest.reporter);
224+
assert.deepStrictEqual(reported.at(-1), reportedDiagnosticEvent);
225+
assert.strictEqual(reported.filter((event) => event.type === 'test:diagnostic').length, 1);
226+
assert.strictEqual(collectStdout(reported), plausibleSizeFalseHeaderStdout);
227+
});
228+
229+
it('should preserve real messages on both sides of a plausible-size false frame', async () => {
230+
// A real message, then the poison bytes, then another real message. Both
231+
// real messages must survive and the poison bytes must become stdout.
232+
const reported = await collectReported([
233+
...chunks,
234+
plausibleSizeFalseHeader,
235+
...chunks,
236+
]);
237+
const diagnostics = reported.filter((event) => event.type === 'test:diagnostic');
238+
assert.strictEqual(diagnostics.length, 2);
239+
diagnostics.forEach((event) => assert.deepStrictEqual(event, reportedDiagnosticEvent));
240+
assert.strictEqual(collectStdout(reported), plausibleSizeFalseHeaderStdout);
241+
});
242+
243+
it('should recover from a plausible-size false frame split across chunks', async () => {
244+
// The same poison bytes arriving in two chunks must still be treated as
245+
// stdout without crashing.
246+
const reported = await collectReported([
247+
plausibleSizeFalseHeader.subarray(0, 3),
248+
plausibleSizeFalseHeader.subarray(3),
249+
]);
250+
assert(reported.every((event) => event.type === 'test:stdout'));
251+
assert.strictEqual(collectStdout(reported), plausibleSizeFalseHeaderStdout);
252+
});
253+
254+
it('should resync through several stray frames in a row', async () => {
255+
// Two false frames back to back in one read, then a real one. The parser
256+
// must peel each stray frame off as stdout and still report the real event.
257+
const reported = await collectReported([
258+
Buffer.concat([plausibleSizeFalseHeader, plausibleSizeFalseHeader, ...chunks]),
259+
]);
260+
assert.deepStrictEqual(reported.at(-1), reportedDiagnosticEvent);
261+
assert.strictEqual(reported.filter((event) => event.type === 'test:diagnostic').length, 1);
262+
assert.strictEqual(collectStdout(reported),
263+
plausibleSizeFalseHeaderStdout + plausibleSizeFalseHeaderStdout);
264+
});
265+
266+
it('should surface a genuinely corrupt frame instead of hiding it', () => {
267+
// A frame with both v8 headers and a valid size but an invalid value is
268+
// what a real report-protocol regression looks like, not stray stdout.
269+
// The parser must let the deserialize error surface instead of silently
270+
// turning it into stdout.
271+
assert.throws(() => fileTest.parseMessage(headeredCorruptFrame), /deserialize/);
272+
});
273+
274+
it('should treat a frame whose payload is shorter than the header as stdout', async () => {
275+
// The declared size is smaller than the inner v8 header, so the length
276+
// guard must reject the bytes as stdout instead of reaching the
277+
// deserializer.
278+
const reported = await collectReported([shortPayloadFalseHeader]);
279+
assert(reported.every((event) => event.type === 'test:stdout'));
280+
});
281+
172282
const headerPosition = headerLength * 2 + 4;
173283
for (let i = 0; i < headerPosition + 5; i++) {
174284
const message = `should deserialize a serialized message split into two chunks {...${i},${i + 1}...}`;

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.