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
This commit is contained in:
morluto authored and GitHub committed 2026-11-22 17:21:50 -05:00
1 parent 867dacc946
commit d5f35cd162
4 files changed
+115 -33

No files matched your search

+21 -11
View File
@@ -56,6 +56,7 @@ export interface FitStructure {
interface FitLayout {
readonly fdt: Buffer;
readonly reader: BinaryReader;
readonly children: ReadonlyMap<string, readonly FitNode[]>;
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<Value> = (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<string[]> = (facts, node, name) => {
const fitU32: FitFact<number | null> = (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<string | null> = (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<string | null> = (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,
+2 -4
View File
@@ -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 };
+74 -17
View File
@@ -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<JsonValue>(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<string, JsonValue> = {};
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<Record<string, unknown>>,
): 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<string, JsonValue> = {};
for (const [key, child] of Object.entries(item)) {
if (
+18 -1
View File
@@ -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;
}