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
fix(macho): keep slice facts and failures under large tables and clos…
…e errors

- Frame load commands through a visitor instead of retaining every
  command, restoring the pre-refactor memory profile (2M eight-byte
  commands: peak heap 370 MiB -> 0.9 MiB).
- Confirm a large FAT container from a valid slice's Mach-O header so a
  sibling's range fault stays per slice instead of hiding healthy slices.
- Name only sections whose bytes the range and their segment both cover.
- Throw inspection failures inside the owned descriptor callback so a
  close failure keeps them as its primary error.
  • Loading branch information
chordpli committed Oct 20, 2026
commit f210a4f8d12e8fdaf531e330dbf685737745865d
96 changes: 95 additions & 1 deletion src/artifacts/apple/MachoEncryptionReader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import { describe, expect, it } from "vitest";

import { CPU, fatImage, machoImage, readerOf } from "./MachoImage.fixture.js";
import { readMachoEncryption } from "./MachoEncryptionReader.js";
import { MAX_LOAD_COMMAND_BYTES } from "./MachoLoadCommandReader.js";
import {
MAX_LOAD_COMMAND_BYTES,
readMachoImage,
} from "./MachoLoadCommandReader.js";

const LC_SEGMENT = 0x1;
const LC_SEGMENT_64 = 0x19;
Expand Down Expand Up @@ -351,6 +354,43 @@ describe("Mach-O encryption reader: segment mapping", () => {
});
});

it("names only sections whose bytes the range and their segment both cover", async () => {
const spanning = segment("__TEXT", {
vmAddress: 0x1000n,
fileOffset: 0x4000,
fileSize: 0x2000,
sections: [
{ name: "__text", offset: 0x3000, size: 0x2000 },
// Declared past the segment's file-backed bytes.
{ name: "__stray", offset: 0x7000, size: 0x1000 },
],
});
const covered = await onlySlice(
padded(
machoImage({
commands: [
spanning,
encryption({ cryptoff: 0x4000, cryptsize: 0x4000, cryptid: 1 }),
],
}),
),
);
expect(covered.range?.mapping).toMatchObject({
sections: [{ segment: "__TEXT", section: "__text" }],
});
const empty = await onlySlice(
padded(
machoImage({
commands: [
spanning,
encryption({ cryptoff: 0x4000, cryptsize: 0, cryptid: 1 }),
],
}),
),
);
expect(empty.range?.mapping).toMatchObject({ fragments: [], sections: [] });
});

it("keeps segment name bytes 0x80-0x9f as their own code points", async () => {
const named = segment("__X", {
vmAddress: 0x1000n,
Expand Down Expand Up @@ -575,3 +615,57 @@ describe("Mach-O encryption reader: universal files", () => {
});
});
});

describe("Mach-O encryption reader: large universal files", () => {
it("keeps a healthy slice when a sibling's range is invalid past the 16 MiB scan limit", async () => {
const slice = padded(
machoImage({
commands: [
text(),
encryption({ cryptoff: 0x4000, cryptsize: 0x4000, cryptid: 1 }),
],
}),
);
const size = MAX_LOAD_COMMAND_BYTES + 0x100000;
const table = new DataView(new ArrayBuffer(8 + 2 * 20));
table.setUint32(0, 0xcafebabe);
table.setUint32(4, 2);
const entry = (index: number, fields: readonly number[]) =>
fields.forEach((value, field) =>
table.setUint32(8 + index * 20 + field * 4, value),
);
entry(0, [CPU.arm64.type, CPU.arm64.subtype, 0x1000, slice.length, 12]);
// Declared past the end of the file.
entry(1, [CPU.x86_64.type, CPU.x86_64.subtype, size, 0x1000, 12]);
const prefix = new Uint8Array(0x1000 + slice.length);
prefix.set(new Uint8Array(table.buffer));
prefix.set(slice, 0x1000);
// The bytes past the slice are zero, so the reader never needs them.
const sparseReadAt = async (offset: number, length: number) => {
const result = new Uint8Array(
Math.max(0, Math.min(length, size - offset)),
);
if (offset < prefix.length)
result.set(prefix.subarray(offset, offset + result.length));
return result;
};

expect(await readMachoEncryption(sparseReadAt, size)).toMatchObject({
status: "parsed",
slices: [
{ container_file_offset: 0x1000, status: "encrypted" },
{
container_file_offset: size,
status: "malformed",
reason: "FAT architecture 1 slice range extends beyond the file",
},
],
});
// The confirmed container reports the sibling's real fault, not an
// unresolved JVM identity.
expect(await readMachoImage(sparseReadAt, size)).toEqual({
status: "malformed",
reason: "FAT architecture 1 slice range extends beyond the file",
});
});
});
88 changes: 51 additions & 37 deletions src/artifacts/apple/MachoEncryptionReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import type {
} from "../../domain/apple/machoEncryption.js";
import { validateFatSliceIdentity } from "../../domain/apple/machoContainer.js";
import {
frameLoadCommands,
readMachoContainer,
readSliceLoadCommands,
readSliceCommandTable,
type MachoLoadCommand,
type MachoReadIssue,
type MachoSliceHeader,
type ReadAt,
} from "./MachoLoadCommandReader.js";
Expand Down Expand Up @@ -85,34 +87,48 @@ const readSlice = async (
readonly fat: FatDeclaration | null;
},
): Promise<MachoSliceEncryption> => {
const framed = await readSliceLoadCommands(readAt, slice.offset, slice.size);
const { header } = framed;
const table = await readSliceCommandTable(readAt, slice.offset, slice.size);
const base = {
architecture: null,
cpu_type: header?.cpuType ?? slice.fat?.cpuType ?? null,
cpu_subtype: header?.cpuSubtype ?? slice.fat?.cpuSubtype ?? null,
cpu_type: table.header?.cpuType ?? slice.fat?.cpuType ?? null,
cpu_subtype: table.header?.cpuSubtype ?? slice.fat?.cpuSubtype ?? null,
container_file_offset: slice.offset,
size: slice.size,
};
const commands = framed.commands
.filter(({ command }) => isEncryptionCommand(command))
.map(decodeEncryptionCommand);
if (framed.status !== "framed")
// Commands seen before a failure stay as partial facts; an incomplete
// walk can never prove the command is absent.
return {
...base,
status: framed.status === "unsupported" ? "unknown" : "malformed",
reason: framed.reason,
commands,
range: null,
};
// Only encryption and segment commands are retained; the rest are framed
// and dropped so a large table of small commands stays one buffer.
const commands: MachoEncryptionCommand[] = [];
const segments: MachoLoadCommand[] = [];
// Commands seen before a failure stay as partial facts; an incomplete walk
// can never prove the command is absent.
const incomplete = ({
status,
reason,
}: MachoReadIssue): MachoSliceEncryption => ({
...base,
status: status === "unsupported" ? "unknown" : "malformed",
reason,
commands,
range: null,
});
if (table.status !== "read") return incomplete(table);
const { header } = table;
const framed = frameLoadCommands(table, (command) => {
if (isEncryptionCommand(command.command))
commands.push(decodeEncryptionCommand(command));
else if (
command.command === LC_SEGMENT ||
command.command === LC_SEGMENT_64
)
segments.push(command);
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
});
if (framed.status !== "framed") return incomplete(framed);
const identityIssue =
slice.fat === null
? null
: validateFatSliceIdentity(slice.fat, {
cpuType: framed.header.cpuType,
cpuSubtype: framed.header.cpuSubtype,
cpuType: header.cpuType,
cpuSubtype: header.cpuSubtype,
});
if (identityIssue !== null)
return {
Expand All @@ -122,15 +138,15 @@ const readSlice = async (
commands,
range: null,
};
const outcome = classify(commands, framed.header, slice.size);
const outcome = classify(commands, header, slice.size);
if (outcome.status !== "valid")
return { ...base, ...outcome, commands, range: null };
return {
...base,
status: outcome.command.cryptid === 0 ? "not-encrypted" : "encrypted",
reason: null,
commands,
range: encryptedRange(outcome.command, framed, slice.offset),
range: encryptedRange(outcome.command, header, segments, slice.offset),
};
};

Expand Down Expand Up @@ -220,10 +236,8 @@ const encryptedRange = (
readonly cryptsize: number;
readonly cryptid: number;
},
framed: {
readonly header: MachoSliceHeader;
readonly commands: readonly MachoLoadCommand[];
},
header: MachoSliceHeader,
segmentCommands: readonly MachoLoadCommand[],
containerOffset: number,
): MachoEncryptedRange => {
const start = command.cryptoff;
Expand All @@ -234,7 +248,7 @@ const encryptedRange = (
slice_end: end,
container_start: containerOffset + start,
container_end: containerOffset + end,
mapping: mapRange(start, end, framed),
mapping: mapRange(start, end, header, segmentCommands),
};
};

Expand All @@ -254,19 +268,15 @@ interface SegmentFacts {
const mapRange = (
start: number,
end: number,
framed: {
readonly header: MachoSliceHeader;
readonly commands: readonly MachoLoadCommand[];
},
header: MachoSliceHeader,
segmentCommands: readonly MachoLoadCommand[],
): MachoEncryptedRange["mapping"] => {
const segments: SegmentFacts[] = [];
for (const command of framed.commands) {
if (command.command !== LC_SEGMENT && command.command !== LC_SEGMENT_64)
continue;
if ((command.command === LC_SEGMENT_64) !== framed.header.wide)
for (const command of segmentCommands) {
if ((command.command === LC_SEGMENT_64) !== header.wide)
return {
status: "unknown",
reason: `load command ${command.index} ${command.command === LC_SEGMENT_64 ? "LC_SEGMENT_64" : "LC_SEGMENT"} appears in a ${framed.header.wide ? 64 : 32}-bit image`,
reason: `load command ${command.index} ${command.command === LC_SEGMENT_64 ? "LC_SEGMENT_64" : "LC_SEGMENT"} appears in a ${header.wide ? 64 : 32}-bit image`,
};
const segment = decodeSegment(command);
if (typeof segment === "string")
Expand Down Expand Up @@ -297,11 +307,15 @@ const mapRange = (
cursor = Math.max(cursor, to);
}
if (cursor < end) fragments.push(unmapped(cursor, end));
// A section counts only where the range, the section and its segment's
// file-backed bytes all overlap, so names agree with the fragments.
const sections = fileBacked.flatMap((segment) =>
segment.sections
.filter(
({ zerofill, size, offset }) =>
!zerofill && size > 0 && offset < end && offset + size > start,
!zerofill &&
Math.max(start, offset, segment.fileOffset) <
Math.min(end, offset + size, segment.fileOffset + segment.fileSize),
)
.map(({ name }) => ({ segment: segment.name, section: name })),
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
);
Expand Down
Loading