fix(artifacts): preserve FAT table coverage for unsupported Mach-O slices (#15802)

* fix(artifacts): preserve FAT table coverage for unsupported Mach-O slices

* test(artifacts): check retained FAT table extent on truncated slices
This commit is contained in:
Vincent Koc
2026-12-02 14:51:45 +08:00
committed by GitHub
parent 3cb459d8ad
commit 624d2f1ec1
8 changed files with 90 additions and 36 deletions
+4 -1
View File
@@ -2187,7 +2187,10 @@ count disagreement makes coverage `unknown`. A malformed FAT table still fails.
lipo places each slice at the first offset its declared alignment allows, so
only that gap is padding. Bytes after the last slice, or before a slice beyond
its padding, keep coverage `unknown`, and a limitation states how many there
are and where the first one starts.
are and where the first one starts. If slice decoding is unsupported, the reader
keeps the FAT table extent it already measured. If the table extent is unknown,
the reader reports the prefix as unknown and counts gaps between or after the
reported slices.
If native DMG child traversal is unavailable after carrier identification,
the typed failure retains a revalidated root-only inventory under
+12 -16
View File
@@ -39,9 +39,6 @@ import {
type LipoArchitecture,
} from "../domain/apple/lipo.js";
/** fat.h: the magic and architecture count that precede every FAT table. */
const FAT_HEADER_BYTES = 8;
type FatPlacedSlice = Parameters<typeof fatUnaccountedBytes>[2][number];
/** lipo reports alignment in bytes; a FAT row declares its power of two. */
@@ -232,11 +229,7 @@ export class MachOSliceArtifactReader implements ArtifactReader {
"Apple hidden ARM64 slice could not be verified against the FAT table",
);
const slices = structural.status === "parsed" ? structural.slices : null;
// Without a FAT parse only the fixed FAT header is known to precede the
// slices, which measures any bytes before the first slice conservatively.
const tableEnd =
(structural.status === "parsed" ? structural.fatTableEnd : undefined) ??
FAT_HEADER_BYTES;
const tableEnd = structural.fatTableEnd;
const hiddenSlices =
structural.status === "parsed" ? (structural.hiddenSlices ?? []) : [];
const matchingSlices = new Map<LipoArchitecture, MachoSlice>();
@@ -376,19 +369,22 @@ export class MachOSliceArtifactReader implements ArtifactReader {
/** Keep coverage unknown when bytes lie outside the FAT table and every slice. */
#recordUnaccountedBytes(
tableEnd: number,
tableEnd: number | undefined,
fileSize: number,
slices: readonly FatPlacedSlice[],
): void {
const unaccounted = fatUnaccountedBytes(tableEnd, fileSize, slices);
if (unaccounted.firstOffset === null) return;
this.#coverage = {
status: "unknown",
limitations: [
...this.#coverage.limitations,
if (tableEnd !== undefined && unaccounted.firstOffset === null) return;
const limitations = [...this.#coverage.limitations];
if (tableEnd === undefined)
limitations.push(
"The FAT table extent is unknown; bytes before the first slice could not be accounted for.",
);
if (unaccounted.firstOffset !== null)
limitations.push(
`${String(unaccounted.bytes)} byte(s) in ${String(unaccounted.ranges)} range(s), the first at offset ${String(unaccounted.firstOffset)}, lie outside the FAT table, every slice and its alignment padding and were not exposed as entries.`,
],
};
);
this.#coverage = { status: "unknown", limitations };
}
/** Inventory one slice range after checking it lies within the artifact. */
@@ -1186,6 +1186,7 @@ describe("Mach-O encryption reader: large universal files", () => {
// unresolved JVM identity.
expect(await readMachoImage(sparseReadAt, size)).toEqual({
status: "failed",
fatTableEnd: 48,
failure: expect.objectContaining({
kind: "truncated",
detail: expect.stringMatching(
@@ -852,12 +852,12 @@ it.each([false, true])(
bytes.writeUInt32BE(bytes.readUInt32BE(20), 40);
if (hidden) bytes.writeUInt32BE(1, 4);
const readAt = vi.fn(readerOf(bytes));
expect(await readMachoImage(readAt, bytes.length)).toEqual({
expect(await readMachoImage(readAt, bytes.length)).toMatchObject({
status: "failed",
failure: expect.objectContaining({
failure: {
kind: "malformed",
detail: "Mach-O slices 0 and 1 overlap.",
}),
},
});
// Header identity probes are allowed; load-command payloads are not.
const firstOffset = bytes.readUInt32BE(16);
@@ -2147,6 +2147,7 @@ describe("Mach-O load command reader defect precedence", () => {
const fat = fatImage(slices.map((bytes) => ({ cpu: CPU.arm64, bytes })));
expect(await read(fat)).toEqual({
status: "failed",
fatTableEnd: 48,
failure: expect.objectContaining({
kind: "malformed",
detail: "load commands extend beyond the Mach-O slice",
@@ -2163,6 +2164,7 @@ describe("Mach-O load command reader defect precedence", () => {
new DataView(ranged.buffer).setUint32(8 + 20 + 12, ranged.length, false);
expect(await read(ranged)).toEqual({
status: "failed",
fatTableEnd: 48,
failure: expect.objectContaining({
kind: "unsupported",
detail: expect.stringMatching(
@@ -2243,6 +2245,7 @@ it("does not borrow Mach-O header bytes beyond a short FAT declaration", async (
).toEqual([]);
expect(result).toEqual({
status: "failed",
fatTableEnd: 48,
failure: expect.objectContaining({
kind: "truncated",
detail: "Mach-O header is truncated",
+6 -7
View File
@@ -117,20 +117,19 @@ export type MachoDependency = z.infer<typeof dependencySchema>;
export type MachoSlice = z.infer<typeof machoSliceSchema>;
/** Header parse of one file; an unreadable image keeps the owner's failure. */
export type MachoImageFacts =
export type MachoImageFacts = {
/** End of a verified FAT table, including a declared hidden ARM64 row. */
readonly fatTableEnd?: number;
} & (
| {
readonly status: "parsed";
readonly slices: readonly MachoSlice[];
/** Apple lipo's ARM64 compatibility row, stored beyond raw nfat_arch. */
readonly hiddenSlices?: readonly MachoSlice[];
/**
* End of a universal container's FAT table, including Apple's hidden
* ARM64 row when one is declared. Absent for a thin image.
*/
readonly fatTableEnd?: number;
}
| { readonly status: "failed"; readonly failure: MachoReadFailure }
| { readonly status: "not-mach-o" };
| { readonly status: "not-mach-o" }
);
/** Return every slice that can participate in architecture selection. */
export const machoImageSlices = (
+5 -3
View File
@@ -585,7 +585,7 @@ export interface FatUnaccountedBytes {
* describes. A slice whose alignment is unknown admits no padding.
*/
export const fatUnaccountedBytes = (
tableEnd: number,
tableEnd: number | undefined,
fileSize: number,
slices: readonly {
readonly offset: number;
@@ -593,7 +593,9 @@ export const fatUnaccountedBytes = (
readonly alignmentExponent: number | null;
}[],
): FatUnaccountedBytes => {
let reach = tableEnd;
const ordered = [...slices].sort((a, b) => a.offset - b.offset);
// Without the table extent only gaps between or after slices are measurable.
let reach = tableEnd ?? ordered[0]?.offset ?? fileSize;
let bytes = 0;
let ranges = 0;
let firstOffset: number | null = null;
@@ -603,7 +605,7 @@ export const fatUnaccountedBytes = (
ranges += 1;
firstOffset ??= start;
};
for (const slice of [...slices].sort((a, b) => a.offset - b.offset)) {
for (const slice of ordered) {
const alignment = 2 ** (slice.alignmentExponent ?? 0);
gapUntil(Math.ceil(reach / alignment) * alignment, slice.offset);
reach = Math.max(reach, slice.offset + slice.size);
+9 -6
View File
@@ -270,6 +270,7 @@ export const readMachoImage = async (
readAt: ReadAt,
size: number,
): Promise<MachoImageFacts> => {
const layout: { fatTableEnd?: number } = {};
try {
const container = await readMachoContainer(readAt, size);
if (container.kind === "not-mach-o") return { status: "not-mach-o" };
@@ -277,23 +278,25 @@ export const readMachoImage = async (
return { status: "failed", failure: container.failure };
if (container.kind === "thin")
return { status: "parsed", slices: [await readSlice(readAt, 0, size)] };
const { header } = container;
const { header, hidden } = container;
if (hidden?.status !== "failed")
layout.fatTableEnd =
header.tableEnd + (hidden === null ? 0 : FAT_ARCH_ROW_BYTES);
const rangeIssue = header.rangeIssues.find(
(issue): issue is FatSliceRangeFailure => issue !== null,
);
if (rangeIssue !== undefined)
return { status: "failed", failure: rangeIssue };
const hiddenSlice = await readHiddenSlice(readAt, container.hidden);
return { status: "failed", failure: rangeIssue, ...layout };
const hiddenSlice = await readHiddenSlice(readAt, hidden);
return {
status: "parsed",
slices: await readFatSlices(readAt, header.slices),
...(hiddenSlice === null ? {} : { hiddenSlices: [hiddenSlice] }),
fatTableEnd:
header.tableEnd + (hiddenSlice === null ? 0 : FAT_ARCH_ROW_BYTES),
...layout,
};
} catch (cause: unknown) {
if (cause instanceof MachoReadError)
return { status: "failed", failure: cause.failure };
return { status: "failed", failure: cause.failure, ...layout };
throw cause;
}
};
@@ -433,6 +433,53 @@ describe("artifact Mach-O slices under record-and-continue", () => {
});
});
it.each([0, 23])(
"keeps the FAT table when a PowerPC slice needs lipo (trailer %i)",
async (trailerSize) => {
// FAT32 header and one row, followed immediately by a big-endian MH_OBJECT.
const words = [
0xcafebabe, 1, 18, 0, 28, 28, 2, 0xfeedface, 18, 0, 1, 0, 0, 0,
];
const bytes = Buffer.alloc(56 + trailerSize);
words.forEach((word, index) => bytes.writeUInt32BE(word, index * 4));
const { entries, coverage } = await recordedEntries(
bytes,
"architecture ppc\n cputype 18\n cpusubtype 0\n offset 28\n size 28\n align 2^2 (4)\n",
);
expect(entries.map(({ path }) => path)).toEqual(["slices/ppc"]);
expect(coverage).toEqual([
trailerSize === 0
? { status: "complete", limitations: [] }
: {
status: "unknown",
limitations: [
expect.stringContaining(
"23 byte(s) in 1 range(s), the first at offset 56",
),
],
},
]);
},
);
it("distinguishes an unverified prefix from a measured trailer when only lipo has ranges", async () => {
const { coverage } = await recordedEntries(
new Uint8Array(64),
"architecture ppc\n cputype 18\n cpusubtype 0\n offset 28\n size 28\n align 2^2 (4)\n",
);
expect(coverage).toEqual([
{
status: "unknown",
limitations: [
expect.stringContaining("FAT table extent is unknown"),
expect.stringContaining(
"8 byte(s) in 1 range(s), the first at offset 56",
),
],
},
]);
});
it("reports unknown coverage when lipo's counts disagree and no FAT parse is available", async () => {
// A big-endian 32-bit Mach-O header: REA's structural reader declines it.
const powerpc = new Uint8Array(4096);