fix(process): decode bounded worker control frames as strict UTF-8 (#12162)

* fix(process): decode analyzer worker control frames as strict UTF-8

Refs #12145.

* fix(process): decode bounded control input without byte replacement

* fix(android): bound guardian readiness by the first UTF-8 frame (#12176)

Follows the readiness framing and lifecycle findings reported by @LcokHcak in #3048.
This commit is contained in:
morluto authored and GitHub committed 2026-11-19 04:00:02 +00:00
1 parent 85ac88fe87
commit 3493d6d252
15 files changed
+496 -208

No files matched your search

@@ -24,6 +24,10 @@ import { safeParseUtf8Json } from "../../domain/safeJson.js";
import { Budget } from "../../domain/budget.js";
import { analysisSettingLimit } from "../../domain/analysisSettingDefinitions.js";
import { WorkerFrameSignal } from "../../process/WorkerFrameSignal.js";
import {
Utf8NdjsonLineBuffer,
Utf8NdjsonOversizeError,
} from "../../process/Utf8NdjsonLineBuffer.js";
import type {
JavaScriptSemanticProjectionCapacity,
JavaScriptSemanticFileProjection,
@@ -32,7 +36,6 @@ import { randomUUID } from "node:crypto";
import { readFile, rm, stat } from "node:fs/promises";
import { join } from "node:path";
import { writeJavaScriptAnalysisSourceTransfer } from "./JavaScriptAnalysisSourceTransfer.js";
import { StringDecoder } from "node:string_decoder";
import { fileURLToPath } from "node:url";
import { getHeapStatistics } from "node:v8";
import { z } from "zod";
@@ -148,8 +151,7 @@ export class JavaScriptAnalysisWorker {
#heapLimitBytes = 0;
#frames: JavaScriptWorkerMessage[] = [];
readonly #frameSignal = new WorkerFrameSignal();
#pending = "";
#decoder = new StringDecoder("utf8");
readonly #lines = new Utf8NdjsonLineBuffer(() => DEFAULTS.protocolBytes);
#primaryFailure: WorkerPrimaryFailure | undefined;
#activeJobId: string | undefined;
#activeJobPhase = "startup";
@@ -181,33 +183,30 @@ export class JavaScriptAnalysisWorker {
if (this.#deadline?.interruption !== undefined) return;
this.#observeMemoryLimit();
try {
this.#pending +=
typeof chunk === "string" ? chunk : this.#decoder.write(chunk);
let newline: number;
while ((newline = this.#pending.indexOf("\n")) >= 0) {
const line = this.#pending.slice(0, newline);
this.#pending = this.#pending.slice(newline + 1);
if (
Buffer.byteLength(line) > DEFAULTS.protocolBytes ||
this.#frames.length >= 16
) {
for (const line of this.#lines.pushRaw(
typeof chunk === "string" ? Buffer.from(chunk) : chunk,
)) {
if (!line.ok) throw line.error;
if (this.#frames.length >= 16) {
this.#latchOutputFailure(
"JavaScript worker exceeded its control-frame budget",
);
return;
}
const raw: unknown = JSON.parse(line);
const raw: unknown = JSON.parse(line.value);
const frame = javaScriptWorkerMessageSchema.parse(raw);
this.#frames.push(frame);
this.#observeJobFrame(frame);
this.#frameSignal.wake("frame");
}
if (Buffer.byteLength(this.#pending) > DEFAULTS.protocolBytes)
this.#latchOutputFailure(
"JavaScript worker control frame is unterminated or oversized",
);
} catch (cause: unknown) {
this.#latchOutputFailure(errorMessage(cause));
this.#latchOutputFailure(
cause instanceof Utf8NdjsonOversizeError
? cause.boundary === "tail"
? "JavaScript worker control frame is unterminated or oversized"
: "JavaScript worker exceeded its control-frame budget"
: errorMessage(cause),
);
}
};
@@ -277,7 +276,7 @@ export class JavaScriptAnalysisWorker {
this.#primaryFailure === undefined &&
this.#lastFailure === undefined &&
this.#frames.length === 0 &&
this.#pending === "" &&
this.#lines.pendingBytes === 0 &&
this.#runtime.inputFailure === undefined &&
!this.#runtime.hasPendingCleanup &&
!this.#inspectionResources.hasPending &&
@@ -377,7 +376,7 @@ export class JavaScriptAnalysisWorker {
const supervisor = this.#runtime.supervisor;
if (rootPath === undefined || supervisor === undefined)
throw new TypeError("JavaScript worker resources were not acquired");
if (this.#frames.length !== 0 || this.#pending !== "")
if (this.#frames.length !== 0 || this.#lines.pendingBytes !== 0)
throw new AnalysisOutputError(
OPERATION,
"JavaScript worker has unsolicited or incomplete control frames",
@@ -1546,8 +1545,7 @@ export class JavaScriptAnalysisWorker {
this.#ready = false;
this.#frames = [];
this.#frameSignal.clear();
this.#pending = "";
this.#decoder = new StringDecoder("utf8");
this.#lines.reset();
this.#primaryFailure = undefined;
this.#activeJobId = undefined;
this.#staticResponseId = undefined;