Reverse engineer anything with agents, from app behavior down to native binaries.
TypeScript agent-skillsai-agentsbinary-analysisclaude-codeclicodexcordisdecompilerdeveloper-toolsdshdsh-pluginghidrahopperllmmcpmodel-context-protocolreareverse-engineeringstatic-analysistouhou
fix(artifacts): inventory universal slices REA cannot structurally index (#3283)
Kaoru0822e5828596c500
diff --git a/src/artifacts/MachOSliceArtifactReader.test.ts b/src/artifacts/MachOSliceArtifactReader.test.tsindex 9e0d63b13..ba282e3b6 100644--- a/src/artifacts/MachOSliceArtifactReader.test.ts+++ b/src/artifacts/MachOSliceArtifactReader.test.ts@@ -226,3 +226,128 @@ describe("MachOSliceArtifactReader", () => {expect(Buffer.concat(verifiedChunks)).toEqual(arm);});});++describe("universal slices outside the structural index", () => {+ const PPC = { type: 18, subtype: 0 } as const;+ /** A minimal big-endian 32-bit Mach-O header, as a PowerPC slice stores it. */+ const bigEndianImage = (): Uint8Array => {+ const bytes = Buffer.alloc(32);+ bytes.writeUInt32BE(0xfeed_face, 0);+ bytes.writeUInt32BE(PPC.type, 4);+ bytes.writeUInt32BE(1, 12);+ return bytes;+ };+ /** An arm64 image whose load commands claim more bytes than the slice holds. */+ const malformedImage = (): Uint8Array => {+ const bytes = machoImage({ cpu: CPU.arm64 });+ new DataView(bytes.buffer).setUint32(20, 4096, true);+ return bytes;+ };+ const lipoRunner = (+ binary: string,+ slices: readonly {+ readonly name: string;+ readonly cpu: { readonly type: number; readonly subtype: number };+ readonly offset: number;+ readonly size: number;+ }[],+ withRanges = true,+ ): NativeCommandRunner => ({+ run: () =>+ Promise.resolve(+ ok({+ tool: "lipo",+ executable: "/usr/bin/lipo",+ executableSha256: "1".repeat(64),+ toolVersion: null,+ versionReason: "fixture",+ arguments: ["-detailed_info", binary],+ stdout: slices+ .map(+ ({ name, cpu, offset, size }) =>+ `architecture ${name}\n cputype ${String(cpu.type)}\n cpusubtype ${String(cpu.subtype)}\n${withRanges ? ` offset ${String(offset)}\n size ${String(size)}\n` : ""} align 2^12 (4096)\n`,+ )+ .join(""),+ stderr: "",+ stdoutBytes: 1,+ stderrBytes: 0,+ exitCode: 0,+ signal: null,+ }),+ ),+ });+ const readAll = async (+ slices: readonly {+ readonly name: string;+ readonly cpu: { readonly type: number; readonly subtype: number };+ readonly bytes: Uint8Array;+ }[],+ withRanges = true,+ ) => {+ const root = await createTestTempDirectory("rea-slices-unsupported-");+ const binary = join(root, "fat");+ await writeFile(binary, fatImage(slices));+ const reader = new MachOSliceArtifactReader(+ binary,+ {},+ lipoRunner(+ binary,+ slices.map(({ name, cpu, bytes }, index) => ({+ name,+ cpu,+ offset: 4096 * (index + 1),+ size: bytes.length,+ })),+ withRanges,+ ),+ );+ onTestFinished(() => reader.close());+ const entries = [];+ for await (const entry of reader.entries()) entries.push(entry);+ return entries;+ };++ it("inventories every lipo range beside a big-endian slice", async () => {+ const entries = await readAll([+ { name: "ppc", cpu: PPC, bytes: bigEndianImage() },+ { name: "arm64", cpu: CPU.arm64, bytes: machoImage({ cpu: CPU.arm64 }) },+ ]);+ expect(entries.map(({ path, byteOffset }) => [path, byteOffset])).toEqual([+ ["slices/ppc", 4096],+ ["slices/arm64", 8192],+ ]);+ expect(entries[0]?.limitations).toContain(+ "Structural Mach-O facts were unsupported (big-endian Mach-O slices are not supported); slice range is based on lipo output.",+ );+ });++ it("reports missing lipo ranges for an unindexed slice as unavailable support", async () => {+ await expect(+ readAll(+ [+ { name: "ppc", cpu: PPC, bytes: bigEndianImage() },+ {+ name: "arm64",+ cpu: CPU.arm64,+ bytes: machoImage({ cpu: CPU.arm64 }),+ },+ ],+ false,+ ),+ ).rejects.toMatchObject({ reason: "unavailable" });+ });++ it.each([+ ["before", true],+ ["after", false],+ ])(+ "rejects a malformed slice %s a big-endian slice",+ async (_order, malformedFirst) => {+ const ppc = { name: "ppc", cpu: PPC, bytes: bigEndianImage() };+ const broken = { name: "arm64", cpu: CPU.arm64, bytes: malformedImage() };+ await expect(+ readAll(malformedFirst ? [broken, ppc] : [ppc, broken]),+ ).rejects.toMatchObject({ reason: "integrity" });+ },+ );+});diff --git a/src/artifacts/MachOSliceArtifactReader.ts b/src/artifacts/MachOSliceArtifactReader.tsindex b57b0247e..466501568 100644--- a/src/artifacts/MachOSliceArtifactReader.ts+++ b/src/artifacts/MachOSliceArtifactReader.ts@@ -122,9 +122,16 @@ export class MachOSliceArtifactReader implements ArtifactReader {"integrity",`Mach-O structural slice index is malformed: ${structural.reason}`,);- if (structural.status === "unsupported")+ // A valid universal binary can hold slices REA cannot index, such as+ // big-endian PowerPC. lipo's own ranges still inventory every slice.+ if (+ structural.status === "unsupported" &&+ architectures.some(+ ({ file_offset, size }) => file_offset === null || size === null,+ )+ )throw new ArtifactReaderFailure(- "format",+ "unavailable",`Mach-O structural slice index is unsupported: ${structural.reason}`,);const slices = structural.status === "parsed" ? structural.slices : null;@@ -185,7 +192,11 @@ export class MachOSliceArtifactReader implements ArtifactReader {? ["Structural Mach-O facts were unavailable; slice range is based on lipo output.",]- : []),+ : structural.status === "unsupported"+ ? [+ `Structural Mach-O facts were unsupported (${structural.reason}); slice range is based on lipo output.`,+ ]+ : []),...(architecture.file_offset === null || architecture.size === null? ["Slice range was derived from structural Mach-O facts because lipo omitted it.",diff --git a/src/artifacts/apple/MachoLoadCommandReader.ts b/src/artifacts/apple/MachoLoadCommandReader.tsindex 55b134cf5..2c086ec0b 100644--- a/src/artifacts/apple/MachoLoadCommandReader.ts+++ b/src/artifacts/apple/MachoLoadCommandReader.ts@@ -198,13 +198,27 @@ const readFatSlices = async (declarations: readonly FatSliceDeclaration[],): Promise<MachoSlice[]> => {const slices: MachoSlice[] = [];+ // Keep validating after an unsupported slice so a later malformed slice is+ // reported as malformed whatever the slice order.+ let unsupported: MachoFormatIssue | undefined;for (const declaration of declarations) {const { offset, size: length, cpuType, cpuSubtype } = declaration;- const slice = await readSlice(readAt, offset, length, {- fatCpuType: cpuType,- fatCpuSubtype: cpuSubtype,- alignmentExponent: declaration.alignmentExponent,- });+ let slice: MachoSlice;+ try {+ slice = await readSlice(readAt, offset, length, {+ fatCpuType: cpuType,+ fatCpuSubtype: cpuSubtype,+ alignmentExponent: declaration.alignmentExponent,+ });+ } catch (cause: unknown) {+ if (+ !(cause instanceof MachoFormatIssue) ||+ cause.status !== "unsupported"+ )+ throw cause;+ unsupported ??= cause;+ continue;+ }const identityIssue = validateFatSliceIdentity(declaration, {cpuType: slice.cpu_type,cpuSubtype: slice.cpu_subtype,@@ -213,6 +227,7 @@ const readFatSlices = async (throw new MachoFormatIssue("malformed", identityIssue);slices.push(slice);}+ if (unsupported !== undefined) throw unsupported;return slices;};diff --git a/tests/boundary/providers/native/machoSliceReader.test.ts b/tests/boundary/providers/native/machoSliceReader.test.tsindex b2f1867a1..ad433a2cf 100644--- a/tests/boundary/providers/native/machoSliceReader.test.ts+++ b/tests/boundary/providers/native/machoSliceReader.test.ts@@ -115,4 +115,51 @@ describe("Mach-O slice reader with system lipo", () => {).toEqual(await fatSlices("/bin/ls"));},);++ it.skipIf(process.platform !== "darwin")(+ "inventories a universal binary whose big-endian slice cannot be indexed",+ async () => {+ const directory = await createTestTempDirectory("rea-lipo-ppc-");+ const path = join(directory, "mixed");+ const bytes = Buffer.alloc(8224);+ bytes.writeUInt32BE(0xca_fe_ba_be, 0);+ bytes.writeUInt32BE(2, 4);+ const slices = [+ { cpu: 0x0100_000c, offset: 4096, bigEndian: false },+ { cpu: 18, offset: 8192, bigEndian: true },+ ];+ slices.forEach(({ cpu, offset, bigEndian }, index) => {+ const entry = 8 + index * 20;+ bytes.writeUInt32BE(cpu, entry);+ bytes.writeUInt32BE(offset, entry + 8);+ bytes.writeUInt32BE(32, entry + 12);+ bytes.writeUInt32BE(12, entry + 16);+ const write = (value: number, at: number) =>+ bigEndian+ ? bytes.writeUInt32BE(value, at)+ : bytes.writeUInt32LE(value, at);+ write(cpu === 18 ? 0xfeed_face : 0xfeed_facf, offset);+ write(cpu, offset + 4);+ write(1, offset + 12);+ });+ await writeFile(path, bytes);+ const reader = new MachOSliceArtifactReader(path, {});+ onTestFinished(() => reader.close());+ const entries = [];+ for await (const entry of reader.entries()) entries.push(entry);+ expect(+ entries.map(({ path, byteOffset, declaredSize }) => [+ path,+ byteOffset,+ declaredSize,+ ]),+ ).toEqual([+ ["slices/arm64", 4096, 32],+ ["slices/ppc", 8192, 32],+ ]);+ expect(entries[1]?.limitations).toContain(+ "Structural Mach-O facts were unsupported (big-endian Mach-O slices are not supported); slice range is based on lipo output.",+ );+ },+ );});