Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

fix(native): admit thin arm64_32 Mach-O targets for static inspection (#5788)

Lewis Wang861e17a3a716
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 1588b642c..c03afbbeb 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -764,6 +764,7 @@ jobs:
tests/boundary/mcp/nativeDemangleMcp.test.ts
tests/boundary/providers/native/nativeSignatureTargetBinding.test.ts
tests/boundary/cli/nativeSignatureCli.test.ts
+ tests/boundary/cli/nativeMachoArm64_32Cli.test.ts
tests/boundary/mcp/nativeSignatureTargetBindingMcp.test.ts
src/process/capture/FilesystemSnapshot.test.ts
tests/process-global/filesystemSnapshotFallbackCancellation.test.ts
diff --git a/scripts/ci/scopes.mjs b/scripts/ci/scopes.mjs
index 4fcea051a..004521e4d 100644
--- a/scripts/ci/scopes.mjs
+++ b/scripts/ci/scopes.mjs
@@ -81,6 +81,7 @@ export const SCOPES = {
"tests/boundary/mcp/nativeDemangleMcp.test.ts",
"tests/boundary/providers/native/nativeSignatureTargetBinding.test.ts",
"tests/boundary/cli/nativeSignatureCli.test.ts",
+ "tests/boundary/cli/nativeMachoArm64_32Cli.test.ts",
"tests/boundary/mcp/nativeSignatureTargetBindingMcp.test.ts",
"src/process/capture/FilesystemSnapshot.test.ts",
"tests/process-global/filesystemSnapshotFallbackCancellation.test.ts",
diff --git a/src/domain/binaryTarget.test.ts b/src/domain/binaryTarget.test.ts
index e244e9cd0..0aec8e0a4 100644
--- a/src/domain/binaryTarget.test.ts
+++ b/src/domain/binaryTarget.test.ts
@@ -20,6 +20,7 @@ describe("executable header parsing", () => {
[elf(1, 1, 164), "elf", "hexagon"],
[thinMach(0xfeedfacf, 0x0100000c), "mach-o", "arm64"],
[thinMach(0xcefaedfe, 0x01000007), "mach-o", "x86_64"],
+ [thinMach(0xcefaedfe, 0x0200000c), "mach-o", "arm64_32"],
[pe(0x8664), "pe", "x86_64"],
[pe(0xaa64), "pe", "arm64"],
] as const)("parses metadata for %s", (bytes, format, architecture) => {
diff --git a/src/domain/binaryTarget.ts b/src/domain/binaryTarget.ts
index 406ff509e..10908f48a 100644
--- a/src/domain/binaryTarget.ts
+++ b/src/domain/binaryTarget.ts
@@ -49,6 +49,12 @@ export type ExecutableMetadata =
readonly architecture: "hexagon";
readonly availableArchitectures: readonly "hexagon"[];
}
+ | {
+ readonly format: "mach-o";
+ readonly architecture: "arm64_32";
+ readonly availableArchitectures: readonly "arm64_32"[];
+ readonly universalSlice?: never;
+ }
| {
readonly format: "mach-o" | "elf" | "dos-mz" | "dos-com";
readonly architecture: HeaderArchitecture;
@@ -120,6 +126,9 @@ export const parseExecutableHeader = (
return err("unsupported binary format");
};
+/** CPU_TYPE_ARM64_32: AArch64 instructions with 32-bit pointers (watchOS). */
+const CPU_TYPE_ARM64_32 = 0x0200000c;
+
const parseThinMachO = (
bytes: Buffer,
magic: number,
@@ -136,13 +145,22 @@ const parseThinMachO = (
const little = magic === 0xcefaedfe || magic === 0xcffaedfe;
const read = (offset: number): number =>
little ? bytes.readUInt32LE(offset) : bytes.readUInt32BE(offset);
- const architecture = machArchitecture(identity.cpuType);
+ const architecture =
+ identity.cpuType === CPU_TYPE_ARM64_32
+ ? "arm64_32"
+ : machArchitecture(identity.cpuType);
if (architecture === undefined) return err("unsupported Mach-O architecture");
const commandBytes = read(20);
if (headerSize + commandBytes > fileSize)
return err(
`truncated Mach-O load commands: the header declares ${commandBytes} bytes after its ${headerSize}-byte header; the file has ${fileSize}`,
);
+ if (architecture === "arm64_32")
+ return ok({
+ format: "mach-o",
+ architecture,
+ availableArchitectures: [architecture],
+ });
return ok({
format: "mach-o",
architecture,
diff --git a/src/domain/binaryTargetTypes.ts b/src/domain/binaryTargetTypes.ts
index 11fc4f846..f079c35f3 100644
--- a/src/domain/binaryTargetTypes.ts
+++ b/src/domain/binaryTargetTypes.ts
@@ -6,6 +6,7 @@ export const BINARY_ARCHITECTURES = [
"x86_64",
"arm",
"arm64",
+ "arm64_32",
"mips",
"6502",
"riscv",
@@ -63,7 +64,7 @@ export const isElfMetadataArchitecture = (
/** Recognized family with no ELF metadata member; the header alone is the fact. */
export type HeaderArchitecture = Exclude<
BinaryArchitecture,
- ElfMetadataArchitecture | RawOnlyArchitecture | "hexagon"
+ ElfMetadataArchitecture | RawOnlyArchitecture | "hexagon" | "arm64_32"
>;
/** Raw ELF facts; family recognition does not establish ISA or ABI support. */
@@ -184,6 +185,15 @@ export type BinaryTarget =
readonly executableRole?: never;
readonly managed?: never;
})
+ | (BinaryTargetIdentity & {
+ readonly kind: "executable";
+ readonly format: "mach-o";
+ readonly architecture: "arm64_32";
+ readonly availableArchitectures: readonly "arm64_32"[];
+ readonly universalSlice?: never;
+ readonly executableRole?: never;
+ readonly managed?: never;
+ })
| (ExecutableTarget & {
readonly format: "mach-o" | "elf" | "dos-mz" | "dos-com";
readonly universalSlice?: never;
diff --git a/src/domain/elfFixedHeader.ts b/src/domain/elfFixedHeader.ts
index 030d7952c..7cfb1e6e1 100644
--- a/src/domain/elfFixedHeader.ts
+++ b/src/domain/elfFixedHeader.ts
@@ -4,16 +4,19 @@ import type {
} from "./binaryTargetTypes.js";
import { err, ok, type Result } from "./result.js";
+/** CPU families an ELF e_machine can name; arm64_32 is a Mach-O-only CPU type. */
+type ElfArchitecture = Exclude<
+ BinaryArchitecture,
+ RawOnlyArchitecture | "arm64_32"
+>;
+
export interface ElfFixedHeader {
readonly elfClass: 32 | 64;
readonly byteOrder: "little" | "big";
readonly headerSize: number;
readonly machine: number;
readonly osAbi: number;
- readonly architecture: Exclude<
- BinaryArchitecture,
- RawOnlyArchitecture
- > | null;
+ readonly architecture: ElfArchitecture | null;
}
/**
@@ -72,7 +75,7 @@ export const readElfFixedHeader = (
export const elfArchitecture = (
machine: number,
-): Exclude<BinaryArchitecture, RawOnlyArchitecture> | undefined => {
+): ElfArchitecture | undefined => {
switch (machine) {
case 3:
return "x86";
diff --git a/src/ghidra/GhidraProvider.behavior.test.ts b/src/ghidra/GhidraProvider.behavior.test.ts
index 713346416..d0574c5c3 100644
--- a/src/ghidra/GhidraProvider.behavior.test.ts
+++ b/src/ghidra/GhidraProvider.behavior.test.ts
@@ -37,7 +37,7 @@ import { jsonValueSchema, type JsonValue } from "../domain/jsonValue.js";
import { parseConfig } from "../config/parseConfig.js";
import type { BinaryTarget } from "../domain/binaryTargetTypes.js";
import { parseExecutableHeader } from "../domain/binaryTarget.js";
-import { elf } from "../domain/binaryTarget.fixture.js";
+import { elf, thinMach } from "../domain/binaryTarget.fixture.js";
import { GhidraProvider } from "./GhidraProvider.js";
import type { GhidraTargetSnapshot } from "./GhidraClient.js";
import type { GhidraProviderClientFactory } from "./GhidraProviderClient.js";
@@ -512,6 +512,24 @@ describe("Ghidra raw-binary profile", () => {
});
describe("Ghidra Mach-O slice support", () => {
+ it("declines parsed thin arm64_32 Mach-O targets", () => {
+ const parsed = parseExecutableHeader(
+ thinMach(0xcefaedfe, 0x0200000c),
+ "arm64",
+ );
+ if (!parsed.ok) throw new Error("Expected valid arm64_32 Mach-O target");
+ expect(
+ provider().inspectTargetSupport({
+ path: "/tmp/watch.o",
+ sha256: "a".repeat(64),
+ kind: "executable",
+ ...parsed.value,
+ }),
+ ).toMatchObject({
+ status: "unsupported",
+ code: "architecture_unsupported",
+ });
+ });
it("refuses universal targets whose selected slice cannot be enforced", () => {
const ghidra = provider();
const universal: BinaryTarget = {
diff --git a/src/hopper/HopperAnalysisProfile.ts b/src/hopper/HopperAnalysisProfile.ts
index d94d5d9cd..9d007bb5a 100644
--- a/src/hopper/HopperAnalysisProfile.ts
+++ b/src/hopper/HopperAnalysisProfile.ts
@@ -10,6 +10,7 @@ import { createAnalysisProfile } from "../domain/analysisProfile.js";
import {
isMachOFormat,
isElfMetadataArchitecture,
+ type BinaryArchitecture,
type BinaryTarget,
type ElfMetadataArchitecture,
type HeaderArchitecture,
@@ -173,10 +174,25 @@ const HOPPER_DECLINED_ARCHITECTURE_LABELS: Record<
hexagon: "Hexagon",
};
+/** Families with no verified Hopper lane, declined before any loader selection. */
+export type HopperDeclinedArchitecture =
+ | ElfMetadataArchitecture
+ | "hexagon"
+ | "arm64_32";
+
+export const isHopperDeclinedArchitecture = (
+ architecture: BinaryArchitecture,
+): architecture is HopperDeclinedArchitecture =>
+ isElfMetadataArchitecture(architecture) ||
+ architecture === "hexagon" ||
+ architecture === "arm64_32";
+
export const hopperDeclinedArchitectureReason = (
- architecture: ElfMetadataArchitecture | "hexagon",
+ architecture: HopperDeclinedArchitecture,
): string =>
- `REA's Hopper adapter does not admit ${HOPPER_DECLINED_ARCHITECTURE_LABELS[architecture]} targets. Select a verified Ghidra ${HOPPER_DECLINED_ARCHITECTURE_LABELS[architecture]} profile instead.`;
+ architecture === "arm64_32"
+ ? "REA's Hopper adapter has no verified arm64_32 lane."
+ : `REA's Hopper adapter does not admit ${HOPPER_DECLINED_ARCHITECTURE_LABELS[architecture]} targets. Select a verified Ghidra ${HOPPER_DECLINED_ARCHITECTURE_LABELS[architecture]} profile instead.`;
/** Derive Hopper's complete non-interactive CLI loader selection. */
export const hopperLoaderArgsForTarget = (
@@ -192,7 +208,7 @@ export const hopperLoaderArgsForTarget = (
if (target.format === "raw-binary")
return err(new ProviderAdapterError("hopper", "resolve_analysis_profile"));
const architecture = target.architecture;
- if (isElfMetadataArchitecture(architecture) || architecture === "hexagon")
+ if (isHopperDeclinedArchitecture(architecture))
return err(
new AnalysisCapabilityUnavailableError(
"hopper",
diff --git a/src/hopper/HopperProvider.test.ts b/src/hopper/HopperProvider.test.ts
index 29a91d5a0..a0eba852e 100644
--- a/src/hopper/HopperProvider.test.ts
+++ b/src/hopper/HopperProvider.test.ts
@@ -66,4 +66,23 @@ describe("Hopper provider capabilities", () => {
expect(support.reason).toContain("Ghidra");
}
});
+ it("declines thin arm64_32 Mach-O targets without a verified lane", () => {
+ const config = parseConfig({});
+ if (!config.ok) throw new Error("expected valid configuration");
+ const provider = new HopperProvider(config.value, silentLogger, {});
+ expect(
+ provider.inspectTargetSupport({
+ path: "/tmp/watch.o",
+ sha256: "a".repeat(64),
+ kind: "executable",
+ format: "mach-o",
+ architecture: "arm64_32",
+ availableArchitectures: ["arm64_32"],
+ }),
+ ).toMatchObject({
+ status: "unsupported",
+ code: "architecture_unsupported",
+ reason: expect.stringContaining("arm64_32"),
+ });
+ });
});
diff --git a/src/hopper/HopperProvider.ts b/src/hopper/HopperProvider.ts
index 8f2c9a4f9..77a2dd7b2 100644
--- a/src/hopper/HopperProvider.ts
+++ b/src/hopper/HopperProvider.ts
@@ -16,10 +16,7 @@ import type {
} from "../application/AnalysisProvider.js";
import type { AnalysisProfileCommitment } from "../domain/analysisProfile.js";
import type { AppConfig } from "../config/types.js";
-import {
- isElfMetadataArchitecture,
- type BinaryTarget,
-} from "../domain/binaryTargetTypes.js";
+import type { BinaryTarget } from "../domain/binaryTargetTypes.js";
import type { JsonValue } from "../domain/jsonValue.js";
import type { Logger } from "pino";
import { analysisErrorWithCleanupFailure } from "../domain/analysisErrorCleanup.js";
@@ -41,6 +38,7 @@ import { HopperApplicationLauncher } from "./BridgeLauncher.js";
import {
hopperDeclinedArchitectureReason,
hopperLoaderArgsForTarget,
+ isHopperDeclinedArchitecture,
resolveHopperAnalysisProfile,
} from "./HopperAnalysisProfile.js";
import { mapHopperFileOffset } from "./HopperFileOffset.js";
@@ -159,10 +157,7 @@ export class HopperProvider implements AnalysisProviderCandidate {
reason: null,
diagnostics,
};
- if (
- isElfMetadataArchitecture(target.architecture) ||
- target.architecture === "hexagon"
- )
+ if (isHopperDeclinedArchitecture(target.architecture))
return {
status: "unsupported",
code: "architecture_unsupported",
diff --git a/src/ida/IdaProvider.test.ts b/src/ida/IdaProvider.test.ts
index 9ce6d8404..bcac7dd0e 100644
--- a/src/ida/IdaProvider.test.ts
+++ b/src/ida/IdaProvider.test.ts
@@ -120,4 +120,23 @@ describe("IDA provider target support", () => {
reason: expect.stringContaining("Hexagon"),
});
});
+ it("declines thin arm64_32 Mach-O targets without a verified lane", () => {
+ const config = parseConfig({});
+ if (!config.ok) throw new Error("expected valid configuration");
+ const provider = new IdaProvider(config.value, {});
+ expect(
+ provider.inspectTargetSupport({
+ path: "/tmp/watch.o",
+ sha256: "a".repeat(64),
+ kind: "executable",
+ format: "mach-o",
+ architecture: "arm64_32",
+ availableArchitectures: ["arm64_32"],
+ }),
+ ).toMatchObject({
+ status: "unsupported",
+ code: "architecture_unsupported",
+ reason: expect.stringContaining("arm64_32"),
+ });
+ });
});
diff --git a/src/ida/IdaProvider.ts b/src/ida/IdaProvider.ts
index 88bc8a3b3..d3a6ee84f 100644
--- a/src/ida/IdaProvider.ts
+++ b/src/ida/IdaProvider.ts
@@ -255,6 +255,13 @@ export class IdaProvider implements AnalysisProviderCandidate {
} lane. Select a supported Ghidra profile instead.`,
diagnostics,
};
+ if (target.kind === "executable" && target.architecture === "arm64_32")
+ return {
+ status: "unsupported",
+ code: "architecture_unsupported",
+ reason: "REA's IDA adapter has no verified arm64_32 lane.",
+ diagnostics,
+ };
if (target.kind !== "executable")
return {
status: "unsupported",
diff --git a/src/native/MachoArchitectureSelection.ts b/src/native/MachoArchitectureSelection.ts
index 14c5f696f..c694eab89 100644
--- a/src/native/MachoArchitectureSelection.ts
+++ b/src/native/MachoArchitectureSelection.ts
@@ -29,6 +29,10 @@ export const MACHO_ARCHITECTURE_POLICIES: Readonly<
],
variantName: /^arm64e\.[A-Za-z0-9_]+$/u,
},
+ arm64_32: {
+ cpuType: 0x0200000c,
+ subtypes: [{ subtype: 1, name: "arm64_32" }],
+ },
};
/** Unlisted subtypes follow the preferred slices in FAT table order. */
diff --git a/tests/boundary/cli/nativeMachoArm64_32Cli.test.ts b/tests/boundary/cli/nativeMachoArm64_32Cli.test.ts
new file mode 100644
index 000000000..4ebe08a92
--- /dev/null
+++ b/tests/boundary/cli/nativeMachoArm64_32Cli.test.ts
@@ -0,0 +1,77 @@
+import { writeFile } from "node:fs/promises";
+import { join } from "node:path";
+
+import { describe, expect } from "vitest";
+import { z } from "zod";
+
+import { listArchitecturesSchema } from "../../../src/domain/native/nativeInspection.js";
+import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
+import { cliTest } from "../../support/cli/cliFixture.js";
+
+const environment = {
+ REA_LOG_LEVEL: "silent",
+ REA_ANALYSIS_PROVIDER: "auto",
+} as const;
+
+const resultSchema = z.object({
+ subject: z.object({ format: z.string(), architecture: z.string() }),
+ normalized_result: z.record(z.string(), z.unknown()),
+});
+
+describe.skipIf(process.platform !== "darwin")(
+ "native Mach-O CLI with a thin arm64_32 object",
+ () => {
+ cliTest(
+ "admits the thin object for static Mach-O inspection",
+ async ({ cli, processes }) => {
+ const directory = await createTestTempDirectory(
+ "rea-native-cli-arm64-32-",
+ );
+ // A header-free source keeps the object independent of SDK slices.
+ const source = join(directory, "watch.c");
+ await writeFile(source, "int rea_watch_value(void) { return 42; }\n");
+ const object = join(directory, "watch.o");
+ const compiled = await processes.run(
+ "/usr/bin/xcrun",
+ ["clang", "-arch", "arm64_32", "-c", source, "-o", object],
+ { timeoutMs: 30_000 },
+ );
+ expect(compiled.exitCode, compiled.stderr).toBe(0);
+ for (const command of [
+ "list-architectures",
+ "inspect-macho",
+ "inspect-signature",
+ ]) {
+ const result = await cli.run({
+ arguments: [command, object, "--json"],
+ environment,
+ });
+ expect(result.exitCode, `${command}: ${result.stdout}`).toBe(0);
+ const parsed = resultSchema.parse(result.json);
+ expect(parsed.subject).toMatchObject({
+ format: "mach-o",
+ architecture: "arm64_32",
+ });
+ if (command === "list-architectures")
+ expect(
+ listArchitecturesSchema.shape.architectures
+ .parse(parsed.normalized_result.architectures)
+ .items.map(({ name }) => name),
+ ).toEqual(["arm64_32"]);
+ }
+ // 32-bit dispatch metadata is unsupported, and no deep-analysis
+ // provider admits arm64_32, so the fallback is a typed refusal.
+ const dispatch = await cli.run({
+ arguments: ["inspect-native-dispatch-metadata", object, "--json"],
+ environment,
+ });
+ expect(dispatch.json).toMatchObject({
+ code: "capability_unavailable",
+ retryable: false,
+ details: { selection_reason: "target_unsupported" },
+ });
+ },
+ 60_000,
+ );
+ },
+);
diff --git a/tests/fixtures/nativeCommands.ts b/tests/fixtures/nativeCommands.ts
index 594ef9a79..61098971b 100644
--- a/tests/fixtures/nativeCommands.ts
+++ b/tests/fixtures/nativeCommands.ts
@@ -1,6 +1,9 @@
import { createHash } from "node:crypto";
import { readFile } from "node:fs/promises";
-import type { BinaryTarget } from "../../src/domain/binaryTargetTypes.js";
+import type {
+ BinaryTarget,
+ HeaderArchitecture,
+} from "../../src/domain/binaryTargetTypes.js";
import type { InspectSignature } from "../../src/domain/native/nativeInspection.js";
import { ok } from "../../src/domain/result.js";
import type {
@@ -106,6 +109,7 @@ export const nativeMachoTarget = (
): BinaryTarget & {
readonly kind: "executable";
readonly format: "mach-o";
+ readonly architecture: HeaderArchitecture;
} => ({
path,
...(sourcePath === undefined ? {} : { sourcePath }),