From d5f35cd16230ebf6973c0f451baee1b2b4646ddc Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Sun, 22 Nov 2026 17:21:50 -0500 Subject: [PATCH] perf(parsers): optimize UEFI uniformity, FIT reader, and HAR validation (#13657) * perf(parsers): unroll UEFI uniformity, reuse FIT BinaryReader, and skip HAR validation pointers * refactor(har): remove the obsolete redaction mode --- src/artifacts/fit/FitStructure.ts | 32 ++++++--- src/browser/history/CaptureRedaction.ts | 6 +- src/browser/history/HarCapture.ts | 91 ++++++++++++++++++++----- src/uefi/UefiBytes.ts | 19 +++++- 4 files changed, 115 insertions(+), 33 deletions(-) diff --git a/src/artifacts/fit/FitStructure.ts b/src/artifacts/fit/FitStructure.ts index a687d5528cd..6a5c202f402 100644 --- a/src/artifacts/fit/FitStructure.ts +++ b/src/artifacts/fit/FitStructure.ts @@ -56,6 +56,7 @@ export interface FitStructure { interface FitLayout { readonly fdt: Buffer; + readonly reader: BinaryReader; readonly children: ReadonlyMap; readonly totalsize: number; readonly fileSize: number; @@ -64,6 +65,7 @@ interface FitLayout { /** Lenient decoding records malformed descriptive facts here instead of failing. */ interface FitFacts { readonly fdt: Buffer; + readonly reader: BinaryReader; readonly limitations: string[]; } type FitFact = (facts: FitFacts, node: FitNode, name: string) => Value; @@ -123,7 +125,7 @@ const valueOf = (fdt: Buffer, property: FitProperty): Buffer => const utf8 = strictUtf8Decoder("preserve"); const nulCount = (raw: Buffer): number => { let count = 0; - for (const byte of raw) if (byte === 0) count += 1; + for (let i = 0; i < raw.length; i += 1) if (raw[i] === 0) count += 1; return count; }; @@ -199,8 +201,7 @@ const fitStrings: FitFact = (facts, node, name) => { const fitU32: FitFact = (facts, node, name) => { const property = propertyOf(node, name); if (property === undefined) return null; - if (property.bytes === 4) - return new BinaryReader(facts.fdt, false, "fit").u32(property.offset); + if (property.bytes === 4) return facts.reader.u32(property.offset); facts.limitations.push(`${node.path}: ${name} is not a single 32-bit cell`); return null; }; @@ -215,7 +216,7 @@ const fitAddress: FitFact = (facts, node, name) => { ); return null; } - const reader = new BinaryReader(facts.fdt, false, "fit"); + const reader = facts.reader; let value = 0n; for (let offset = 0; offset < property.bytes; offset += 4) value = (value << 32n) | BigInt(reader.u32(property.offset + offset)); @@ -224,7 +225,7 @@ const fitAddress: FitFact = (facts, node, name) => { /** Byte-locating cells fail closed: a guessed range would misattribute bytes. */ const strictCell = ( - fdt: Buffer, + reader: BinaryReader, node: FitNode, property: FitProperty, ): number => { @@ -232,7 +233,7 @@ const strictCell = ( throw formatFailure( `${node.path}: ${property.name} is not a single 32-bit cell`, ); - return new BinaryReader(fdt, false, "fit").u32(property.offset); + return reader.u32(property.offset); }; const imageRange = ( @@ -258,11 +259,11 @@ const imageRange = ( data_source: "embedded", declared_offset: null, }; - const declaredOffset = strictCell(layout.fdt, node, source.property); + const declaredOffset = strictCell(layout.reader, node, source.property); const sizeProperty = propertyOf(node, "data-size"); if (sizeProperty === undefined) throw formatFailure(`${node.path}: ${source.name} requires data-size`); - const size = strictCell(layout.fdt, node, sizeProperty); + const size = strictCell(layout.reader, node, sizeProperty); // data-offset is relative to the 4-byte-aligned end of the devicetree; // data-position is an offset from the start of the FIT (fit + position). const start = @@ -355,7 +356,11 @@ const imageDeclaration = ( node: FitNode, index: number, ): FitImageDeclaration => { - const facts: FitFacts = { fdt: layout.fdt, limitations: [] }; + const facts: FitFacts = { + fdt: layout.fdt, + reader: layout.reader, + limitations: [], + }; const name = nodeName(node); const children = layout.children.get(node.path) ?? []; const range = imageRange(layout, node); @@ -610,7 +615,8 @@ export const readFitStructure = ( if (root === undefined || !byPath.has("/images")) throw formatFailure("/images node is missing"); const { totalsize } = tree.header; - const layout: FitLayout = { fdt, children, totalsize, fileSize }; + const reader = new BinaryReader(fdt, false, "fit"); + const layout: FitLayout = { fdt, reader, children, totalsize, fileSize }; const imageNodes = children.get("/images") ?? []; if (imageNodes.length > FIT_MAX_IMAGES) throw capacityFailure( @@ -629,7 +635,11 @@ export const readFitStructure = ( `FIT images declare ${String(declared)} bytes, exceeding this file's ${String(budget)}-byte expansion budget`, { boundary: "memory" }, ); - const facts: FitFacts = { fdt, limitations: [...structureLimitations] }; + const facts: FitFacts = { + fdt, + reader, + limitations: [...structureLimitations], + }; return { images, limitations: facts.limitations, diff --git a/src/browser/history/CaptureRedaction.ts b/src/browser/history/CaptureRedaction.ts index 383f9f0b9dc..e5856b691ae 100644 --- a/src/browser/history/CaptureRedaction.ts +++ b/src/browser/history/CaptureRedaction.ts @@ -88,13 +88,11 @@ export class CaptureRedaction { number( value: LosslessNumber, pointer: string, - redact = true, ): { reported: number | null; literal: string } | null { const reported = isSafeNumber(value.value) ? Number(value.value) : null; if ( - redact && - (this.text(value.value, pointer) === null || - (reported !== null && this.text(String(reported), pointer) === null)) + this.text(value.value, pointer) === null || + (reported !== null && this.text(String(reported), pointer) === null) ) return null; return { reported, literal: value.value }; diff --git a/src/browser/history/HarCapture.ts b/src/browser/history/HarCapture.ts index 022608609a8..b9bc9769e54 100644 --- a/src/browser/history/HarCapture.ts +++ b/src/browser/history/HarCapture.ts @@ -132,7 +132,68 @@ const projectHar = ( sensitiveValues: readonly string[], scope: "validation" | "container" | "entry", ): Projection => { - const redact = scope !== "validation"; + const maxDepth = captureBudgets().depth; + if (scope === "validation") { + const path: string[] = []; + const buildPointer = (): string => { + let ptr = ""; + for (let i = 0; i < path.length; i += 1) + ptr = capturePointer(ptr, path[i]!); + return ptr; + }; + const visitValidation = (item: unknown, depth: number): JsonValue => { + if (depth > maxDepth) + throw new CaptureFormatError( + { name: "nesting-depth", maximum: maxDepth }, + `HAR exceeds max_nesting_depth (${String(maxDepth)} levels).`, + buildPointer(), + ); + if (item instanceof LosslessNumber) return validationNumber(item.value); + if ( + typeof item === "string" || + item === null || + typeof item === "boolean" + ) + return item; + if (Array.isArray(item)) { + const out = new Array(item.length); + for (let index = 0; index < item.length; index += 1) { + path.push(String(index)); + out[index] = visitValidation(item[index], depth + 1); + path.pop(); + } + return out; + } + if (captureObject(item)) { + const out: Record = {}; + for (const [key, child] of Object.entries(item)) { + path.push(key); + const projected = visitValidation(child, depth + 1); + path.pop(); + if (key === "__proto__") + Object.defineProperty(out, key, { + value: projected, + enumerable: true, + writable: true, + configurable: true, + }); + else out[key] = projected; + } + return out; + } + throw new CaptureFormatError( + "malformed", + "HAR parser returned a non-JSON value.", + buildPointer(), + ); + }; + return { + reported: visitValidation(value, 0), + numeric_literals: [], + binary_fields: [], + redactions: [], + }; + } const redactor = new CaptureRedaction( sensitiveValues, scope === "entry" ? value : undefined, @@ -140,9 +201,7 @@ const projectHar = ( const numeric_literals: Projection["numeric_literals"] = []; const binary_fields: Projection["binary_fields"] = []; const projectNumber = (item: LosslessNumber, pointer: string): JsonValue => { - // Evidence publishes an unsafe number as null; the schema must still judge it as a number. - if (!redact) return validationNumber(item.value); - const number = redactor.number(item, pointer, redact); + const number = redactor.number(item, pointer); if (number === null) return null; numeric_literals.push({ pointer, @@ -157,10 +216,10 @@ const projectHar = ( depth: number, parent?: Readonly>, ): JsonValue => { - if (depth > captureBudgets().depth) + if (depth > maxDepth) throw new CaptureFormatError( - { name: "nesting-depth", maximum: captureBudgets().depth }, - `HAR exceeds max_nesting_depth (${String(captureBudgets().depth)} levels).`, + { name: "nesting-depth", maximum: maxDepth }, + `HAR exceeds max_nesting_depth (${String(maxDepth)} levels).`, pointer, ); if ( @@ -215,15 +274,13 @@ const projectHar = ( return null; } } - return redact - ? redactor.text( - item, - pointer, - scope === "entry" - ? redactor.harTransportUrl(pointer, parent) - : undefined, - ) - : item; + return redactor.text( + item, + pointer, + scope === "entry" + ? redactor.harTransportUrl(pointer, parent) + : undefined, + ); } if (item === null || typeof item === "boolean") return item; if (Array.isArray(item)) @@ -231,7 +288,7 @@ const projectHar = ( visit(child, capturePointer(pointer, String(index)), depth + 1), ); if (captureObject(item)) { - const checkSensitive = redact && sensitiveValues.length > 0; + const checkSensitive = sensitiveValues.length > 0; const out: Record = {}; for (const [key, child] of Object.entries(item)) { if ( diff --git a/src/uefi/UefiBytes.ts b/src/uefi/UefiBytes.ts index c01a422d18c..e018eee2563 100644 --- a/src/uefi/UefiBytes.ts +++ b/src/uefi/UefiBytes.ts @@ -79,7 +79,24 @@ export function isWindowUniform( if (boundedStart >= boundedEnd) { return false; } - for (let index = boundedStart; index < boundedEnd; index += 1) { + let index = boundedStart; + const unrolledEnd = boundedEnd - 7; + while (index < unrolledEnd) { + if ( + buffer[index] !== byte || + buffer[index + 1] !== byte || + buffer[index + 2] !== byte || + buffer[index + 3] !== byte || + buffer[index + 4] !== byte || + buffer[index + 5] !== byte || + buffer[index + 6] !== byte || + buffer[index + 7] !== byte + ) { + return false; + } + index += 8; + } + for (; index < boundedEnd; index += 1) { if (buffer[index] !== byte) { return false; }