diff --git a/src/frida/FridaInstrumentationManager.ts b/src/frida/FridaInstrumentationManager.ts index aadee842e10..732c96b300f 100644 --- a/src/frida/FridaInstrumentationManager.ts +++ b/src/frida/FridaInstrumentationManager.ts @@ -1387,27 +1387,6 @@ const CONTROL_CHAR_JSON_BYTES = (unit: number): number => ? 2 : 6; -const jsonCharByteSize = ( - input: string, - cursor: number, -): { readonly charBytes: number; readonly step: number } => { - const unit = input.charCodeAt(cursor); - if (unit >= 0x20 && unit < 0x80) - return { charBytes: unit === 0x22 || unit === 0x5c ? 2 : 1, step: 1 }; - if (unit < 0x20) return { charBytes: CONTROL_CHAR_JSON_BYTES(unit), step: 1 }; - if (unit < 0x800) return { charBytes: 2, step: 1 }; - if (unit >= 0xd800 && unit <= 0xdbff) { - const next = input.charCodeAt(cursor + 1); - return next >= 0xdc00 && next <= 0xdfff - ? { charBytes: 4, step: 2 } - : { charBytes: 6, step: 1 }; - } - return { - charBytes: unit >= 0xdc00 && unit <= 0xdfff ? 6 : 3, - step: 1, - }; -}; - const scanBoundedJsonBody = ( input: string, start: number, @@ -1417,7 +1396,26 @@ const scanBoundedJsonBody = ( let cursor = start; let bytes = 0; while (cursor < end) { - const { charBytes, step } = jsonCharByteSize(input, cursor); + const unit = input.charCodeAt(cursor); + let charBytes: number; + let step = 1; + if (unit >= 0x20 && unit < 0x80) { + charBytes = unit === 0x22 || unit === 0x5c ? 2 : 1; + } else if (unit < 0x20) { + charBytes = CONTROL_CHAR_JSON_BYTES(unit); + } else if (unit < 0x800) { + charBytes = 2; + } else if (unit >= 0xd800 && unit <= 0xdbff) { + const next = input.charCodeAt(cursor + 1); + if (next >= 0xdc00 && next <= 0xdfff) { + charBytes = 4; + step = 2; + } else { + charBytes = 6; + } + } else { + charBytes = unit >= 0xdc00 && unit <= 0xdfff ? 6 : 3; + } if (cursor + step > end || bytes + charBytes > remainingBytes) break; bytes += charBytes; cursor += step; diff --git a/src/gdb/GdbMiRecord.ts b/src/gdb/GdbMiRecord.ts index b7f6a94a9ff..be50c44d0eb 100644 --- a/src/gdb/GdbMiRecord.ts +++ b/src/gdb/GdbMiRecord.ts @@ -250,14 +250,22 @@ class MiParser { flushRun(); flushEscaped(); this.#cursor = index + 1; - const joined = Buffer.concat(chunks); + const joined = + chunks.length === 0 + ? EMPTY_BYTES + : chunks.length === 1 + ? chunks[0]! + : Buffer.concat(chunks); return ok({ kind: "const", - bytes: new Uint8Array( - joined.buffer, - joined.byteOffset, - joined.byteLength, - ), + bytes: + joined instanceof Buffer + ? new Uint8Array( + joined.buffer, + joined.byteOffset, + joined.byteLength, + ) + : joined, }); } if (code !== 0x5c) { @@ -334,10 +342,16 @@ export const parseGdbMiDoneRecord = ( return parsed; }; +const EMPTY_BYTES = new Uint8Array(0); + /** The text of a C-string constant, or undefined if it is not strict UTF-8. */ export const gdbMiConstText = (value: GdbMiValue): string | undefined => value.kind === "const" && isUtf8(value.bytes) - ? Buffer.from(value.bytes).toString("utf8") + ? Buffer.from( + value.bytes.buffer, + value.bytes.byteOffset, + value.bytes.byteLength, + ).toString("utf8") : undefined; /** Every result named `name` in a result sequence. */ diff --git a/src/gdb/GdbSessionManager.ts b/src/gdb/GdbSessionManager.ts index 1b992aec403..f877c531313 100644 --- a/src/gdb/GdbSessionManager.ts +++ b/src/gdb/GdbSessionManager.ts @@ -1639,8 +1639,11 @@ export class GdbSessionManager { } session.promptTail = prompt.slice(-4); } - const lines: { readonly value: string; readonly invalidUtf8: boolean }[] = - []; + const lines: { + readonly value: string; + readonly invalidUtf8: boolean; + readonly utf8Bytes: number; + }[] = []; let offset = 0; let framingFailure: string | undefined; while (offset < data.byteLength) { @@ -1682,31 +1685,52 @@ export class GdbSessionManager { framingFailure = `GDB emitted an MI line exceeding the ${String(MAX_MI_LINE_BYTES)} byte (16 MiB) protocol limit. This release has no runtime override.`; break; } - this.#appendLineBytes(session, piece); - if (newline < 0) break; - const record = Buffer.concat( - [ - ...session.bufferChunks, - ...(session.bufferTail === undefined - ? [] - : [session.bufferTail.subarray(0, session.bufferTailBytes)]), - ], - session.bufferBytes, - ); + let record: Buffer; + if ( + newline >= 0 && + session.bufferChunks.length === 0 && + session.bufferTailBytes === 0 + ) { + // Fast path: complete line sits contiguously inside `data`, avoiding a + // 64 KiB slab allocation and Buffer.concat copy per line. + record = piece; + } else { + this.#appendLineBytes(session, piece); + if (newline < 0) break; + if ( + session.bufferChunks.length === 0 && + session.bufferTail !== undefined + ) { + record = session.bufferTail.subarray(0, session.bufferTailBytes); + } else { + record = Buffer.concat( + [ + ...session.bufferChunks, + ...(session.bufferTail === undefined + ? [] + : [session.bufferTail.subarray(0, session.bufferTailBytes)]), + ], + session.bufferBytes, + ); + } + } const withoutCarriageReturn = record.at(-1) === 0x0d ? record.subarray(0, -1) : record; const invalidUtf8 = !isUtf8(withoutCarriageReturn); const line = withoutCarriageReturn.toString("utf8"); + const utf8Bytes = invalidUtf8 + ? Buffer.byteLength(line, "utf8") + : withoutCarriageReturn.byteLength; session.bufferChunks.length = 0; session.bufferTail = undefined; session.bufferTailBytes = 0; session.bufferBytes = 0; if (invalidUtf8) session.invalidUtf8Observed = true; - if (line.length > 0) lines.push({ value: line, invalidUtf8 }); + if (line.length > 0) lines.push({ value: line, invalidUtf8, utf8Bytes }); offset = newline + 1; } - for (const { value: line } of lines) { - if (Buffer.byteLength(line, "utf8") > MAX_RECENT_MI_LINE_BYTES) { + for (const { value: line, utf8Bytes } of lines) { + if (utf8Bytes > MAX_RECENT_MI_LINE_BYTES) { session.lines.push(truncateUtf8RecordPrefix(line)); session.recentRecordsTruncated = true; } else session.lines.push(line); @@ -1715,9 +1739,9 @@ export class GdbSessionManager { session.lines.splice(0, session.lines.length - 256); session.recentRecordsTruncated = true; } - for (const { value: line, invalidUtf8 } of lines) { + for (const { value: line, invalidUtf8, utf8Bytes } of lines) { for (const pending of session.pending.values()) { - const lineBytes = Buffer.byteLength(line, "utf8") + 1; + const lineBytes = utf8Bytes + 1; if ( pending.outputBytes + lineBytes <= pending.outputBudgetBytes && pending.lines.length < MAX_COMMAND_OUTPUT_RECORDS diff --git a/src/rizin/RizinDebugSessionManager.ts b/src/rizin/RizinDebugSessionManager.ts index 6b2dc79bb95..28b39713ec9 100644 --- a/src/rizin/RizinDebugSessionManager.ts +++ b/src/rizin/RizinDebugSessionManager.ts @@ -1347,6 +1347,12 @@ export class RizinDebugSessionManager { offset = nextOffset; continue; } + if (boundary >= 0 && session.frameChunks.length === 0) { + const captured = decodeRizinFrameBytes(session, piece, false); + this.#deliverFrame(session, captured.frame, captured.bytes); + offset = nextOffset; + continue; + } if (piece.length > 0) { session.frameChunks.push(Buffer.from(piece)); session.bufferedFrameBytes += piece.length; @@ -1866,9 +1872,11 @@ const hasUnclaimedRizinFrames = (session: RizinDebugSession): boolean => session.bufferedFrameBytes > 0 || session.discardingOversizedFrame; -/** Parse the receiver's bounded bytes once for normal, startup and interrupted reads. */ -const bufferedRizinFrame = (session: RizinDebugSession, truncated: boolean) => { - const bytes = Buffer.concat(session.frameChunks, session.bufferedFrameBytes); +const decodeRizinFrameBytes = ( + session: RizinDebugSession, + bytes: Buffer, + truncated: boolean, +) => { const invalidUtf8 = !isUtf8(bytes); session.invalidUtf8Observed ||= invalidUtf8; return { @@ -1877,6 +1885,15 @@ const bufferedRizinFrame = (session: RizinDebugSession, truncated: boolean) => { }; }; +/** Parse the receiver's bounded bytes once for normal, startup and interrupted reads. */ +const bufferedRizinFrame = (session: RizinDebugSession, truncated: boolean) => { + const bytes = + session.frameChunks.length === 1 + ? session.frameChunks[0]! + : Buffer.concat(session.frameChunks, session.bufferedFrameBytes); + return decodeRizinFrameBytes(session, bytes, truncated); +}; + const rizinCaptureStoppedSentence = ( limit: RizinOutputLimit | undefined, output: DebuggerAdmissionLimit, @@ -1958,9 +1975,10 @@ const waitForCommandFrames = async ( // following it, and apply the same decoded UTF-8 budget as normal frames. const interrupted = () => { let markerSeen = false; - for (;;) { - const queued = session.frames.shift(); - if (queued === undefined) break; + let consumed = 0; + while (consumed < session.frames.length) { + const queued = session.frames[consumed]!; + consumed += 1; session.queuedFrameBytes -= queued.queueChargeBytes; frameCount += 1; if (frameCount > MAX_COMMAND_FRAMES) { @@ -1970,6 +1988,7 @@ const waitForCommandFrames = async ( markerSeen = append(queued.frame); if (markerSeen || outputTruncated) break; } + if (consumed > 0) session.frames.splice(0, consumed); if (!markerSeen && !outputTruncated && session.bufferedFrameBytes > 0) append( bufferedRizinFrame(session, session.discardingOversizedFrame).frame,