Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
cd38a59
refactor(macho): extract non-throwing container and slice load-comman…
chordpli Oct 20, 2026
2b41ca6
feat(macho): report per-slice encryption status in inspect_macho
chordpli Oct 20, 2026
f210a4f
fix(macho): keep slice facts and failures under large tables and clos…
chordpli Oct 20, 2026
ee6f565
Merge remote-tracking branch 'origin/main' into feat/macho-encryption…
chordpli Oct 20, 2026
c128c72
Merge remote-tracking branch 'origin/main' into feat/macho-encryption…
chordpli Oct 20, 2026
c6c8c75
Merge remote-tracking branch 'origin/main' into feat/macho-encryption…
chordpli Oct 21, 2026
15db4e6
Merge remote-tracking branch 'origin/main' into feat/macho-encryption…
chordpli Oct 22, 2026
c54ff26
Merge remote-tracking branch 'origin/main' into feat/macho-encryption…
chordpli Oct 24, 2026
0d506e2
fix(macho): map encrypted ranges only within segment VM extents
chordpli Oct 25, 2026
3bd1541
Merge remote-tracking branch 'origin/main' into HEAD
Oct 26, 2026
611ae1e
fix(macho): read encryption through the admitted descriptor facts
chordpli Oct 26, 2026
7e6d57f
fix(macho): retain a finished report when the target close fails
chordpli Oct 26, 2026
9c74d8d
Merge remote-tracking branch 'origin/main' into tmp5359
chordpli Oct 26, 2026
d95f4e3
Merge remote-tracking branch 'origin/main' into tmp5359
chordpli Oct 26, 2026
5a22d73
Merge remote-tracking branch 'origin/main' into tmp5359
chordpli Oct 26, 2026
811c47d
Merge remote-tracking branch 'origin/main' into tmp5359
chordpli Oct 26, 2026
08ed647
Merge remote-tracking branch 'origin/main' into tmp5359
chordpli Oct 27, 2026
29f37e6
Merge remote-tracking branch 'origin/main' into tmp5359
chordpli Oct 27, 2026
d3f63a8
test(native): supply readEncryption in merged dyld refusal cases
chordpli Oct 27, 2026
014a868
test(views): bind the Mach-O encryption page to its facet
chordpli Oct 27, 2026
c2c2e93
Merge remote-tracking branch 'origin/main' into tmp5359
chordpli Oct 28, 2026
7fb4355
Merge remote-tracking branch 'origin/main' into tmp5359
chordpli Oct 28, 2026
3476a90
Merge remote-tracking branch 'origin/main' into feat/macho-encryption…
chordpli Oct 29, 2026
13904db
fix(macho): bound encryption commands and range mapping across slices
chordpli Oct 29, 2026
1bc25f6
Merge remote-tracking branch 'origin/main' into feat/macho-encryption…
chordpli Oct 29, 2026
27edcc0
style(macho): format encryption reader and binding test
chordpli Oct 29, 2026
c806969
fix(macho): stop retaining segments past the mapping budget
chordpli Oct 29, 2026
26280c7
feat(macho): preserve per-slice encryption facts and mapping budgets
gensokyo-dev Oct 29, 2026
90ee1c6
chore(macho): align encryption reporting with current main
gensokyo-dev Oct 29, 2026
0eba2e7
docs(macho): clarify retained mapping allowance
gensokyo-dev Oct 29, 2026
5ffb40d
fix(ci): own the Mach-O encryption test in Apple scope
gensokyo-dev Oct 29, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Merge remote-tracking branch 'origin/main' into feat/macho-encryption…
…-info
  • Loading branch information
chordpli committed Oct 25, 2026
commit c54ff266cf14f226b58631df4afd804ee9b1b5e8
51 changes: 51 additions & 0 deletions src/artifacts/apple/MachoEncryptionReader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,57 @@ describe("Mach-O encryption reader: universal files", () => {
],
});
});

it("covers Apple's hidden ARM64 slice after the declared rows", async () => {
const armv7 = { type: 12, subtype: 9 };
const bytes = fatImage([
{ cpu: armv7, bytes: machoImage({ cpu: armv7 }) },
{
cpu: CPU.arm64,
bytes: padded(
machoImage({
commands: [
text(),
encryption({ cryptoff: 0x4000, cryptsize: 0x4000, cryptid: 1 }),
],
}),
),
},
]);
// nfat_arch 1 leaves the ARM64 row hidden in the table-to-slice gap.
new DataView(bytes.buffer, bytes.byteOffset).setUint32(4, 1);
expect(await read(bytes)).toMatchObject({
status: "parsed",
slices: [
{ cpu_type: armv7.type, status: "absent" },
{ cpu_type: CPU.arm64.type, status: "encrypted" },
],
});
});

it("keeps the declared slice when the hidden ARM64 row's range is invalid", async () => {
const armv7 = { type: 12, subtype: 9 };
const bytes = fatImage([
{ cpu: armv7, bytes: machoImage({ cpu: armv7 }) },
{ cpu: CPU.arm64, bytes: padded(machoImage({ commands: [text()] })) },
]);
const view = new DataView(bytes.buffer, bytes.byteOffset);
view.setUint32(4, 1);
// The hidden row's size runs past the end of the file.
view.setUint32(8 + 20 + 12, bytes.byteLength);
expect(await read(bytes)).toMatchObject({
status: "parsed",
slices: [
{ cpu_type: armv7.type, status: "absent" },
{
cpu_type: CPU.arm64.type,
status: "malformed",
reason: "FAT architecture 1 slice range extends beyond the file",
commands: [],
},
],
});
});
});

describe("Mach-O encryption reader: large universal files", () => {
Expand Down
20 changes: 18 additions & 2 deletions src/artifacts/apple/MachoEncryptionReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,15 @@ export const readMachoEncryption = async (
status: "parsed",
slices: [await readSlice(readAt, { offset: 0, size, fat: null })],
};
// Apple's hidden ARM64 slice is part of the file's coverage, so an
// undecodable hidden row (a short read) leaves the slice set itself
// unknown before any declared slice is read.
const { hidden } = container;
if (hidden !== null && hidden.status !== "declared")
return { status: hidden.status, reason: hidden.reason };
const slices: MachoSliceEncryption[] = [];
for (const [index, declaration] of container.slices.entries()) {
const rangeIssue = container.rangeIssues[index] ?? null;
for (const [index, declaration] of container.header.slices.entries()) {
const rangeIssue = container.header.rangeIssues[index] ?? null;
slices.push(
rangeIssue === null
? await readSlice(readAt, {
Expand All @@ -58,6 +64,16 @@ export const readMachoEncryption = async (
),
);
}
if (hidden !== null)
slices.push(
hidden.rangeIssue === null
? await readSlice(readAt, {
offset: hidden.declaration.offset,
size: hidden.declaration.size,
fat: hidden.declaration,
})
: unread(hidden.declaration, "malformed", hidden.rangeIssue),
);
return { status: "parsed", slices };
};

Expand Down
121 changes: 69 additions & 52 deletions src/artifacts/apple/MachoLoadCommandReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
readMachoHeaderPrefixFacts,
validateFatSlice,
validateFatSliceIdentity,
type FatContainerHeader,
type FatSliceDeclaration,
type FatSliceRangeIssue,
} from "../../domain/apple/machoContainer.js";
Expand Down Expand Up @@ -149,10 +150,27 @@ export type MachoContainer =
| { readonly kind: "thin"; readonly size: number }
| {
readonly kind: "fat";
readonly slices: readonly FatSliceDeclaration[];
readonly rangeIssues: readonly (FatSliceRangeIssue | null)[];
readonly header: ParsedFatContainerHeader;
readonly hidden: HiddenFatRow | null;
};

/**
* Apple's hidden ARM64 row with its own range defect, or why its slot could
* not be decoded. It stays outside the raw `nfat_arch` declarations.
*/
export type HiddenFatRow =
| {
readonly status: "declared";
readonly declaration: FatSliceDeclaration;
readonly rangeIssue: string | null;
}
| MachoReadIssue;

type ParsedFatContainerHeader = Extract<
FatContainerHeader,
{ readonly state: "parsed" }
>;

/** Classify a file as thin, FAT or not Mach-O without decoding any slice. */
export const readMachoContainer = async (
readAt: ReadAt,
Expand All @@ -169,8 +187,8 @@ export const readMachoContainer = async (
}
: { kind: "not-mach-o" };
const magic = viewOf(head).getUint32(0, false);
let header: ReturnType<typeof readFatContainerHeader>;
if (magic === FAT_MAGIC && size <= MAX_LOAD_COMMAND_BYTES) {
let header: FatContainerHeader;
if (magic === FAT_MAGIC && size <= MAX_JVM_CLASS_IDENTITY_BYTES) {
const complete = await readAt(0, size);
header =
complete.byteLength === size
Expand Down Expand Up @@ -215,21 +233,20 @@ export const readMachoContainer = async (
if (
identity.state === "malformed" &&
magic === FAT_MAGIC &&
size > MAX_LOAD_COMMAND_BYTES &&
size > MAX_JVM_CLASS_IDENTITY_BYTES &&
!(await observesMachoSlice(readAt, header))
)
return {
kind: "issue",
status: "unsupported",
reason:
"FAT/JVM identity is unresolved because the class identity scan exceeds the 16 MiB limit",
reason: JVM_CLASS_IDENTITY_LIMIT_REASON,
};
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
// Identity is only malformed through a slice range issue, which the
// returned rangeIssues retain.
return {
kind: "fat",
slices: header.slices,
rangeIssues: header.rangeIssues,
header,
hidden: await readAppleHiddenArm64Row(readAt, size, header),
};
}
if (magic === MH_CIGAM || magic === MH_CIGAM_64)
Expand All @@ -254,10 +271,7 @@ const readSliceMagic = async (

const observesMachoSlice = async (
readAt: ReadAt,
header: {
readonly slices: readonly FatSliceDeclaration[];
readonly rangeIssues: readonly (FatSliceRangeIssue | null)[];
},
header: ParsedFatContainerHeader,
): Promise<boolean> => {
for (const [index, slice] of header.slices.entries())
if (
Expand All @@ -280,14 +294,17 @@ export const readMachoImage = async (
return { status: container.status, reason: container.reason };
if (container.kind === "thin")
return { status: "parsed", slices: [await readSlice(readAt, 0, size)] };
const rangeIssue = container.rangeIssues.find(
const { header } = container;
const rangeIssue = header.rangeIssues.find(
(issue): issue is FatSliceRangeIssue => issue !== null,
);
if (rangeIssue !== undefined)
return { status: rangeIssue.category, reason: rangeIssue.reason };
const hiddenSlice = await readHiddenSlice(readAt, container.hidden);
return {
status: "parsed",
slices: await readFatSlices(readAt, container.slices),
slices: await readFatSlices(readAt, header.slices),
...(hiddenSlice === null ? {} : { hiddenSlices: [hiddenSlice] }),
};
} catch (cause: unknown) {
if (cause instanceof MachoFormatIssue)
Expand All @@ -298,37 +315,45 @@ export const readMachoImage = async (

/**
* Apple lipo stores one hidden ARM64 row after the declared 32-bit FAT table.
* It remains separate from the raw nfat_arch declarations and is only admitted
* from the single table-to-first-slice slot cctools recognizes.
* It is only admitted from the single table-to-first-slice slot cctools
* recognizes.
*/
const readAppleHiddenArm64Slice = async (
const readAppleHiddenArm64Row = async (
readAt: ReadAt,
fileSize: number,
header: Extract<
ReturnType<typeof readFatContainerHeader>,
{ state: "parsed" }
>,
): Promise<MachoSlice | null> => {
header: ParsedFatContainerHeader,
): Promise<HiddenFatRow | null> => {
if (header.wide) return null;
const firstOffset = Math.min(...header.slices.map(({ offset }) => offset));
const end = header.tableEnd + 20;
if (end > fileSize || end > firstOffset) return null;
const bytes = await readAt(header.tableEnd, 20);
if (bytes.byteLength < 20)
throw new MachoFormatIssue(
"malformed",
"Hidden ARM64 FAT row is truncated",
);
return { status: "malformed", reason: "Hidden ARM64 FAT row is truncated" };
const row = readFatSliceDeclarations(bytes, {
count: 1,
wide: false,
littleEndian: header.littleEndian,
});
if (row.status !== "parsed")
throw new MachoFormatIssue("malformed", row.reason);
return { status: "malformed", reason: row.reason };
const declaration = { ...row.slices[0]!, index: header.count };
if (declaration.cpuType !== CPU_TYPE_ARM64) return null;
const rangeIssue = validateFatSlice(declaration, fileSize, end);
return {
status: "declared",
declaration,
rangeIssue: validateFatSlice(declaration, fileSize, end),
};
};

const readHiddenSlice = async (
readAt: ReadAt,
row: HiddenFatRow | null,
): Promise<MachoSlice | null> => {
if (row === null) return null;
if (row.status !== "declared")
throw new MachoFormatIssue(row.status, row.reason);
const { declaration, rangeIssue } = row;
if (rangeIssue !== null) throw new MachoFormatIssue("malformed", rangeIssue);
const slice = await readSlice(readAt, declaration.offset, declaration.size, {
fatCpuType: declaration.cpuType,
Expand Down Expand Up @@ -486,35 +511,27 @@ export const frameLoadCommands = (
}: { readonly header: MachoSliceHeader; readonly bytes: Uint8Array },
visit: (command: MachoLoadCommand) => void,
): { readonly status: "framed" } | MachoReadIssue => {
const alignment = machoLoadCommandAlignment(header.wide);
const view = viewOf(bytes);
let offset = 0;
for (let index = 0; index < header.commandCount; index++) {
if (offset + 8 > bytes.byteLength)
return {
status: "malformed",
reason: `load command ${index} starts beyond sizeofcmds`,
};
const command = view.getUint32(offset, true);
const size = view.getUint32(offset + 4, true);
if (size < 8 || size % alignment !== 0 || offset + size > bytes.byteLength)
return {
status: "malformed",
reason: `load command ${index} at offset ${offset} has invalid cmdsize ${size}`,
};
const cursor = new MachoLoadCommandCursor({
wide: header.wide,
littleEndian: true,
commandCount: header.commandCount,
commandBytes: bytes.byteLength,
});
while (cursor.nextOffset !== null) {
const frame = cursor.read(
bytes.subarray(cursor.nextOffset, cursor.nextOffset + 8),
);
if (!frame.ok) return { status: "malformed", reason: frame.error };
const { index, command, offset, size } = frame.value;
visit({
index,
command,
offset,
body: bytes.subarray(offset, offset + size),
});
offset += size;
}
if (offset !== bytes.byteLength)
return {
status: "malformed",
reason: `load commands declare ${header.commandCount} commands but sizeofcmds holds ${bytes.byteLength} bytes`,
};
const complete = cursor.finish();
if (!complete.ok) return { status: "malformed", reason: complete.error };
return { status: "framed" };
};

Expand All @@ -533,7 +550,7 @@ const readSlice = async (
throw new MachoFormatIssue(table.status, table.reason);
const { header } = table;
const slice: MachoSlice = {
architecture: architectureName(header.cpuType, header.cpuSubtype),
architecture: machoArchitectureName(header.cpuType, header.cpuSubtype),
slice_offset: base,
slice_size: length,
cpu_type: header.cpuType,
Expand Down
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.