perf(binary): avoid redundant evidence cloning and probe copies (#13636)

* perf(binary): avoid redundant evidence cloning and prefix probe allocations

* test(evidence): verify authentication before immutable construction
This commit is contained in:
morluto authored and GitHub committed 2026-11-22 17:12:55 -05:00
1 parent 841d41df31
commit c0c080176d
6 files changed
+76 -50

No files matched your search

+2 -2
View File
@@ -1608,7 +1608,7 @@ export class BinarySession
arguments_,
evidenceExecution,
"observed",
{ callerTarget: active.target },
{ callerTarget: active.target, immutable: true },
);
const recorded = this.recordEvidence(evidence);
if (!recorded.ok) {
@@ -1632,7 +1632,7 @@ export class BinarySession
operation: name,
parameters: arguments_,
execution: retained,
evidence: this.evidenceForAnalysis(evidence.evidence_id) ?? evidence,
evidence: authenticated ?? evidence,
});
return ok(retained);
}
@@ -132,7 +132,7 @@ export const completedOperationEvidence = (
parameters,
observation,
"observed",
{ callerTarget: options.target },
{ callerTarget: options.target, immutable: true },
),
);
};
@@ -1,7 +1,11 @@
import { unknownTarget } from "../../domain/targetIdentity.js";
import { expect, it, vi } from "vitest";
import { createEvidence, type Evidence } from "../../domain/evidence.js";
import {
createEvidence,
isImmutableEvidence,
type Evidence,
} from "../../domain/evidence.js";
import {
createEvidenceBundle,
createImmutableEvidenceBundle,
@@ -118,43 +122,55 @@ it("applies authentication admission to a fresh caller-selected alias", () => {
upstreamProfile: undefined,
upstreamLimitations: [],
});
const primitive = completedOperationEvidence(
"address_name",
{},
{
result: { value: 1 },
rawResult: null,
provider,
locations: [],
limitations: [],
},
{
target,
admitRecovery: (input) => {
expect(input).toMatchObject({
caller_input_path: target.callerInputPath,
});
return ok(undefined);
},
},
);
if (!primitive.ok) throw primitive.error;
const execution = {
result: { value: 1 },
rawResult: null,
provider,
locations: [],
limitations: [],
};
const constrained = new InvestigationRecords({
availableAuthenticationBytes: () => 0,
});
const admitRecovery = vi.fn(
constrained.admitRecoveryObservation.bind(constrained),
);
const refusedPrimitive = completedOperationEvidence(
"address_name",
{},
execution,
{ target, admitRecovery },
);
expect(admitRecovery).toHaveBeenCalledWith(
expect.objectContaining({ caller_input_path: target.callerInputPath }),
"address_name",
expect.anything(),
);
if (refusedPrimitive.ok)
throw new Error(
"Expected authentication refusal before Evidence construction",
);
expect(refusedPrimitive.error._tag).toBe("AnalysisResourceConstraintError");
expect(refusedPrimitive.error.partialObservation).toBeUndefined();
expect(constrained.statistics().retainedRepresentationBytes).toBe(0);
const refusedWorkflow = constrained.recordEvidence(workflow);
if (refusedWorkflow.ok)
throw new Error("Expected mutable Evidence authentication refusal");
expect(refusedWorkflow.error._tag).toBe("AnalysisResourceConstraintError");
expect(refusedWorkflow.error.partialObservation).toBeUndefined();
expect(constrained.hasEvidence(workflow.evidence_id)).toBe(false);
const control = new InvestigationRecords();
const primitive = completedOperationEvidence("address_name", {}, execution, {
target,
admitRecovery: control.admitRecoveryObservation.bind(control),
});
if (!primitive.ok) throw primitive.error;
expect(isImmutableEvidence(primitive.value)).toBe(true);
for (const evidence of [workflow, primitive.value]) {
expect(evidence.caller_input_path).toBe(target.callerInputPath);
const refused = constrained.recordEvidence(evidence);
if (refused.ok)
throw new Error(
`Bypassed authentication admission: ${evidence.operation}`,
);
expect(refused.error._tag).toBe("AnalysisResourceConstraintError");
expect(refused.error.partialObservation).toBeUndefined();
expect(constrained.hasEvidence(evidence.evidence_id)).toBe(false);
expect(new InvestigationRecords().recordEvidence(evidence)).toEqual(
ok("added"),
);
expect(control.recordEvidence(evidence)).toEqual(ok("added"));
}
});
+1 -1
View File
@@ -3434,7 +3434,7 @@ const readSpan = async (
{ integrityCheck: "member-identity" },
);
state.memberBytesRead += bytes.length - charged;
return Buffer.from(bytes);
return bytes;
},
);
} catch (cause) {
+20 -12
View File
@@ -26,22 +26,30 @@ const unsupported = (detail: string, at: FailureSite = {}): ParseFailure => ({
...at,
});
const readNesContainerFormat = (
reader: BinaryReader,
/** Byte signatures select a container family, never an executable architecture. */
export const nesContainerFormat = (
prefix: Buffer,
): "ines" | "nes-2.0" | "unif" | undefined => {
const prefix = reader.bytes;
if (prefix.subarray(0, 4).equals(Buffer.from("UNIF"))) return "unif";
if (!prefix.subarray(0, 4).equals(Buffer.from([0x4e, 0x45, 0x53, 0x1a])))
if (prefix.length < 4) return undefined;
if (
prefix[0] === 0x55 &&
prefix[1] === 0x4e &&
prefix[2] === 0x49 &&
prefix[3] === 0x46
)
return "unif";
if (
prefix[0] !== 0x4e ||
prefix[1] !== 0x45 ||
prefix[2] !== 0x53 ||
prefix[3] !== 0x1a
)
return undefined;
return prefix.length >= 8 && (reader.u8(7) & 0x0c) === 0x08
return prefix.length >= 8 && (prefix[7]! & 0x0c) === 0x08
? "nes-2.0"
: "ines";
};
/** Byte signatures select a container family, never an executable architecture. */
export const nesContainerFormat = (prefix: Buffer) =>
readNesContainerFormat(new BinaryReader(prefix, true, "ines"));
const headerHexSchema = z.string().regex(/^[a-f0-9]{32}$/u);
const byteSchema = z.number().int().min(0).max(255);
const nibbleSchema = z.number().int().min(0).max(15);
@@ -307,8 +315,7 @@ export const parseInesLayout = (
prefix: Buffer,
fileSize: number,
): Result<InesLayout, ParseFailure> => {
const reader = new BinaryReader(prefix, true, "ines");
const format = readNesContainerFormat(reader);
const format = nesContainerFormat(prefix);
if (format === undefined)
return err(
malformed("missing iNES byte signature", {
@@ -323,6 +330,7 @@ export const parseInesLayout = (
field: "signature",
}),
);
const reader = new BinaryReader(prefix, true, "ines");
if (!Number.isSafeInteger(fileSize) || fileSize < 0)
return err(
malformed(
+3 -1
View File
@@ -200,7 +200,9 @@ const UTF8 = strictUtf8Decoder("preserve");
/** True when the first four bytes are the U-Boot legacy magic. */
export const hasUImageMagic = (prefix: Buffer): boolean =>
prefix.length >= 4 &&
new BinaryReader(prefix, false, "uimage").u32(0) === UIMAGE_MAGIC;
((prefix[0]! << 24) | (prefix[1]! << 16) | (prefix[2]! << 8) | prefix[3]!) >>>
0 ===
UIMAGE_MAGIC;
/**
* Parse the 64-byte header and verify its CRC32 (computed over the header