mirror of
https://github.com/morluto/rea.git
synced 2026-12-06 15:33:56 +08:00
* fix(macho)!: report a FAT header or table cut off by the file's end as truncated The universal Mach-O container reader measured the file's size but reported a FAT header shorter than 8 bytes as malformed, and a FAT table that runs past the file's end as not FAT at all. Callers could not tell a cut-off download from corrupt bytes. readFatContainerHeader now returns a typed truncated state carrying the measured file size and the length the header or table needs. A CAFEBABE count that reads as a JVM class version stays not FAT, because a short class file is not evidence of a universal file. Universal image selection, the native container reader and the lipo-selected slice resolver raise that fact as a truncated ParseFailure with an extent, and the malformed-target projection reports it as details.available_bytes and details.required_bytes. Complete but corrupt FAT data (zero count, overlapping slices, slices inside the table) stays malformed. BREAKING CHANGE: inspect_go_binary, inspect_rust_binary, inspect_dwarf_units, inspect_native_initializers, inspect_native_dispatch_metadata, the IL2CPP binary readers and the macOS provider's lipo-selected slice reads now report a FAT header or table cut off by the file's end with details.format_failure "truncated" (previously "malformed", or an unsupported or not-universal result for a table past the end), plus details.available_bytes and details.required_bytes. * test(macho): type every format-owner row with its asserted format failure
134 lines
3.9 KiB
TypeScript
134 lines
3.9 KiB
TypeScript
import {
|
|
readFatHeaderPrefix,
|
|
readFatSliceDeclarations,
|
|
validateFatSlice,
|
|
type FatSliceDeclaration,
|
|
} from "../domain/apple/machoContainer.js";
|
|
import {
|
|
decodeLimit,
|
|
malformedInput,
|
|
truncatedInput,
|
|
} from "../domain/parseFailure.js";
|
|
import type { MachoCommandNames } from "../domain/apple/machoCommandNames.js";
|
|
import type { NativeMachoSource } from "./NativeMachoSource.js";
|
|
import {
|
|
lipoDescribesCpu,
|
|
type LipoArchitecture,
|
|
} from "../domain/apple/lipo.js";
|
|
|
|
export interface ResolvedMachoSlice {
|
|
readonly offset: number;
|
|
readonly size: number;
|
|
readonly declaration: FatSliceDeclaration;
|
|
readonly tableEnd: number;
|
|
}
|
|
|
|
/** Resolve a unique FAT declaration without replacing observed lipo facts. */
|
|
export const resolveSelectedMachoSlice = async (
|
|
source: NativeMachoSource,
|
|
selected: LipoArchitecture,
|
|
maximumRecords: number,
|
|
): Promise<ResolvedMachoSlice> => {
|
|
if (
|
|
selected.cpu_type_code === null &&
|
|
(selected.file_offset === null || selected.size === null)
|
|
)
|
|
throw malformedInput(
|
|
"macho",
|
|
"Selected Mach-O slice CPU identity is unavailable",
|
|
);
|
|
const prefix = readFatHeaderPrefix(
|
|
await source.readSlice(0, source.bytes, 0, Math.min(8, source.bytes)),
|
|
);
|
|
if (prefix.state !== "parsed")
|
|
throw malformedInput(
|
|
"macho",
|
|
"Selected Mach-O universal header is unavailable",
|
|
);
|
|
const rows = prefix.count + (selected.hidden ? 1 : 0);
|
|
if (rows > maximumRecords)
|
|
throw decodeLimit(
|
|
"macho",
|
|
"Mach-O FAT slice records exceed the selected-slice table budget",
|
|
{ name: "macho_fat_slice_records", maximum: maximumRecords },
|
|
);
|
|
const entrySize = prefix.wide ? 32 : 20;
|
|
const tableEnd = 8 + rows * entrySize;
|
|
if (tableEnd > source.bytes)
|
|
throw truncatedInput(
|
|
"macho",
|
|
`Selected Mach-O universal table is truncated: the file has ${source.bytes} bytes and the table needs ${tableEnd}`,
|
|
{ extent: { availableBytes: source.bytes, requiredBytes: tableEnd } },
|
|
);
|
|
const matches: FatSliceDeclaration[] = [];
|
|
const declarations: FatSliceDeclaration[] = [];
|
|
for (let first = 0; first < rows; first += 256) {
|
|
const count = Math.min(256, rows - first);
|
|
const raw = await source.readSlice(
|
|
0,
|
|
source.bytes,
|
|
8 + first * entrySize,
|
|
count * entrySize,
|
|
);
|
|
const parsed = readFatSliceDeclarations(raw, {
|
|
count,
|
|
wide: prefix.wide,
|
|
littleEndian: prefix.littleEndian,
|
|
});
|
|
if (parsed.status !== "parsed")
|
|
throw malformedInput("macho", parsed.reason);
|
|
for (const row of parsed.slices) {
|
|
declarations.push(row);
|
|
if (
|
|
(selected.file_offset !== null &&
|
|
row.offset !== selected.file_offset) ||
|
|
(selected.size !== null && row.size !== selected.size) ||
|
|
!lipoDescribesCpu(selected, row.cpuType, row.cpuSubtype) ||
|
|
(selected.alignment !== null &&
|
|
selected.alignment !== 2 ** row.alignmentExponent)
|
|
)
|
|
continue;
|
|
matches.push(row);
|
|
}
|
|
}
|
|
if (matches.length !== 1)
|
|
throw malformedInput(
|
|
"macho",
|
|
"Selected lipo slice does not identify exactly one FAT entry",
|
|
);
|
|
const declaration = matches[0]!;
|
|
if (
|
|
declarations.some(
|
|
(row) =>
|
|
row !== declaration &&
|
|
row.offset < declaration.offset + declaration.size &&
|
|
declaration.offset < row.offset + row.size,
|
|
)
|
|
)
|
|
throw malformedInput(
|
|
"macho",
|
|
"Selected Mach-O slice overlaps another FAT entry",
|
|
);
|
|
return {
|
|
offset: declaration.offset,
|
|
size: declaration.size,
|
|
declaration,
|
|
tableEnd,
|
|
};
|
|
};
|
|
|
|
/** Bind the selected FAT declaration to the admitted thin header. */
|
|
export const assertSelectedMachoSlice = (
|
|
source: NativeMachoSource,
|
|
selected: ResolvedMachoSlice,
|
|
binary: MachoCommandNames,
|
|
): void => {
|
|
const failure = validateFatSlice(
|
|
selected.declaration,
|
|
source.bytes,
|
|
selected.tableEnd,
|
|
binary.header,
|
|
);
|
|
if (failure !== null) throw malformedInput("macho", failure);
|
|
};
|