fix(artifacts): distinguish Java classes from universal Mach-O

Classify FAT containers using shared architecture, range, and observed slice-header facts rather than the CAFEBABE signature alone. Preserve typed unsupported and malformed reasons, reject malformed sibling slices consistently, and remove repeated range parsing in consumers.

Validation: valid small and large JVM classes in public JAR/ZIP inspection, four FAT width/byte-order controls, and focused executable/Mach-O boundary tests. The class fixtures were checked with JDK javap; no new JVM analysis capability is advertised.

The Apple selection and Hopper readers retain their existing parsing because they also own oversized-count diagnostics, reserved fields, streaming final-size validation, or source freshness that the shared header result does not represent.

Representative public mixed-ZIP inventory probe, four fresh processes with an already warm filesystem cache: first process elapsed 626.8 -> 548.3 ms, CPU 740 -> 740 ms, sampled family peak RSS 375.5 -> 345.4 MiB. Subsequent three-process medians: elapsed 767.2 -> 454.0 ms, CPU 890 -> 640 ms, peak RSS 368.7 -> 346.2 MiB. Estimated output tokens 2310 -> 2292. Concurrent load was uncontrolled, so these observations do not establish a speed improvement. Whole-agent task tokens and schema/call overhead were not measured.

Fixes #2317
This commit is contained in:
morluto
2026-10-13 04:12:04 +08:00
parent c339f9ebe7
commit 921bb95755
8 changed files with 535 additions and 170 deletions
+62 -51
View File
@@ -9,6 +9,10 @@ import type {
import { err, ok, type Result } from "./result.js";
import { observeMzWindowsHeader, parseDosMzHeader } from "./dosMz.js";
import { decodePeMachine } from "./peMachine.js";
import {
classifyFatContainerIdentity,
readFatContainerHeader,
} from "./apple/machoContainer.js";
/** Format and architecture facts recovered from an executable header. */
export type ExecutableMetadata =
@@ -68,8 +72,9 @@ export const parseExecutableHeader = (
const magic = bytes.readUInt32BE(0);
if ([0xfeedface, 0xfeedfacf, 0xcefaedfe, 0xcffaedfe].includes(magic))
return parseThinMachO(bytes, magic, fileSize);
if ([0xcafebabe, 0xbebafeca, 0xcafebabf, 0xbfbafeca].includes(magic))
return parseFatMachO(bytes, magic, hostArchitecture, fileSize);
const fatHeader = readFatContainerHeader(bytes, fileSize);
if (fatHeader.state !== "not-fat")
return parseFatMachO(bytes, hostArchitecture, fatHeader);
return err("unsupported binary format");
};
@@ -103,79 +108,85 @@ const parseThinMachO = (
const parseFatMachO = (
bytes: Buffer,
magic: number,
host: NodeJS.Architecture,
fileSize: number,
header: Exclude<
ReturnType<typeof readFatContainerHeader>,
{ state: "not-fat" }
>,
): Result<ExecutableMetadata, string> => {
const little = magic === 0xbebafeca || magic === 0xbfbafeca;
const is64 = magic === 0xcafebabf || magic === 0xbfbafeca;
if (bytes.length < 8) return err("truncated FAT header");
const read = (offset: number): number =>
little ? bytes.readUInt32LE(offset) : bytes.readUInt32BE(offset);
const count = read(4);
const entrySize = is64 ? 32 : 20;
if (count === 0 || count > 128 || bytes.length < 8 + count * entrySize)
if (header.state === "needs-data")
return err("truncated or invalid FAT architecture table");
if (header.state === "malformed") return err(header.reason);
const identityError = fatIdentityError(bytes, header);
if (identityError !== null) return err(identityError);
const architectures: HeaderArchitecture[] = [];
const slices = new Map<
HeaderArchitecture,
{ readonly offset: bigint; readonly size: bigint }
{ readonly offset: number; readonly size: number }
>();
for (let index = 0; index < count; index += 1) {
const entry = 8 + index * entrySize;
const architecture = machArchitecture(read(entry));
for (const declaration of header.slices) {
const architecture = machArchitecture(declaration.cpuType);
if (architecture === undefined || architectures.includes(architecture))
continue;
architectures.push(architecture);
const offset = is64
? little
? bytes.readBigUInt64LE(entry + 8)
: bytes.readBigUInt64BE(entry + 8)
: BigInt(read(entry + 8));
const size = is64
? little
? bytes.readBigUInt64LE(entry + 16)
: bytes.readBigUInt64BE(entry + 16)
: BigInt(read(entry + 12));
slices.set(architecture, { offset, size });
slices.set(architecture, {
offset: declaration.offset,
size: declaration.size,
});
}
const preferred =
host === "arm64"
? "arm64"
: host === "x64"
? "x86_64"
: host === "ia32"
? "x86"
: host === "arm"
? "arm"
: undefined;
const preferred = fatHostArchitecture(host);
const slice = preferred === undefined ? undefined : slices.get(preferred);
if (preferred === undefined || slice === undefined)
return err(`FAT binary has no host-compatible ${host} architecture`);
const tableEnd = BigInt(8 + count * entrySize);
if (slice.offset < tableEnd)
return err(`FAT slice for ${preferred} overlaps the architecture table`);
const minimumHeaderSize =
preferred === "arm64" || preferred === "x86_64" ? 32n : 28n;
preferred === "arm64" || preferred === "x86_64" ? 32 : 28;
if (slice.size < minimumHeaderSize)
return err(`FAT slice for ${preferred} is shorter than its Mach-O header`);
const sliceEnd = slice.offset + slice.size;
if (sliceEnd > BigInt(fileSize))
return err(
`truncated FAT slice: the ${preferred} slice ends at byte ${sliceEnd}; the file has ${fileSize}`,
);
const sliceOffset = Number(slice.offset);
const sliceSize = Number(slice.size);
if (!Number.isSafeInteger(sliceOffset) || !Number.isSafeInteger(sliceSize))
return err(`FAT slice for ${preferred} exceeds addressable byte bounds`);
return ok({
format: "mach-o-universal",
architecture: preferred,
availableArchitectures: architectures,
universalSlice: { offset: sliceOffset, size: sliceSize },
universalSlice: { offset: slice.offset, size: slice.size },
});
};
const fatHostArchitecture = (
host: NodeJS.Architecture,
): HeaderArchitecture | undefined => {
switch (host) {
case "arm64":
return "arm64";
case "x64":
return "x86_64";
case "ia32":
return "x86";
case "arm":
return "arm";
default:
return undefined;
}
};
const fatIdentityError = (
bytes: Buffer,
header: Extract<
ReturnType<typeof readFatContainerHeader>,
{ state: "parsed" }
>,
): string | null => {
const identity = classifyFatContainerIdentity(header, (offset) => {
if (offset >= bytes.length) return undefined;
if (offset + 4 > bytes.length) return null;
return bytes.readUInt32BE(offset);
});
if (identity.state === "needs-slice-header")
return "FAT slice identity is outside the bounded prefix";
if (identity.state === "not-mach-o")
return "FAT table does not identify a Mach-O image";
if (identity.state === "malformed") return identity.reason;
return null;
};
const parseElf = (
bytes: Buffer,
fileSize: number,