Skip to content

armgit / rea

Public Imported from GitHub
0

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

fix(cli): prefer native byte inspection for automatic dispatch metadata

morluto060587b4567a
diff --git a/src/application/DirectAnalysis.ts b/src/application/DirectAnalysis.ts
index 3d16b2609..10badb124 100644
--- a/src/application/DirectAnalysis.ts
+++ b/src/application/DirectAnalysis.ts
@@ -248,7 +248,11 @@ const runAnalysis = async (
} else {
const config = dependencies.readConfiguration();
if (!config.ok) return cliError(config.error);
- session = await dependencies.createBinarySession(config.value, logger);
+ session = await (
+ tool === "inspect_native_dispatch_metadata" && snapshotPath === undefined
+ ? dependencies.createNativeDispatchBinarySession
+ : dependencies.createBinarySession
+ )(config.value, logger);
}
return withSessionCleanup(
session,
diff --git a/src/application/DirectAnalysisDependencies.ts b/src/application/DirectAnalysisDependencies.ts
index d0410100d..16f311eb6 100644
--- a/src/application/DirectAnalysisDependencies.ts
+++ b/src/application/DirectAnalysisDependencies.ts
@@ -11,6 +11,11 @@ export interface DirectAnalysisDependencies {
config: AppConfig,
logger: Logger,
) => BinarySession | Promise<BinarySession>;
+ /** Prefer byte-backed dispatch inspection while preserving explicit selection. */
+ readonly createNativeDispatchBinarySession: (
+ config: AppConfig,
+ logger: Logger,
+ ) => BinarySession | Promise<BinarySession>;
/** Create an artifact-only session without selecting native analysis engines. */
readonly createArtifactBinarySession: () =>
| BinarySession
diff --git a/src/application/binary/AnalysisProviderRegistry.test.ts b/src/application/binary/AnalysisProviderRegistry.test.ts
index 60d3ab62a..38b540322 100644
--- a/src/application/binary/AnalysisProviderRegistry.test.ts
+++ b/src/application/binary/AnalysisProviderRegistry.test.ts
@@ -145,6 +145,88 @@ it("selects the sole available provider and honors explicit selection precedence
});
});
+describe("inspection without deep binding", () => {
+ it("retains discovery facts without resolving a deep profile for covered automatic inspection", async () => {
+ const alpha = candidate("alpha", { profile: "error" });
+ const beta = candidate("beta", { profile: "error" });
+ const registry = new AnalysisProviderRegistry(
+ [alpha.provider, beta.provider],
+ "auto",
+ (target) => target === DATABASE_TARGET,
+ );
+ expect(await registry.select(DATABASE_TARGET)).toMatchObject({
+ ok: true,
+ value: {
+ requestedProviderId: "auto",
+ binding: null,
+ candidates: [
+ {
+ provider: { id: "alpha" },
+ selected: false,
+ availability: { status: "available" },
+ },
+ {
+ provider: { id: "beta" },
+ selected: false,
+ availability: { status: "available" },
+ },
+ ],
+ },
+ });
+ expect(alpha.created).toEqual([]);
+ expect(beta.created).toEqual([]);
+ });
+
+ it("keeps explicit request and environment choices even when auxiliary inspection covers the target", async () => {
+ const alpha = candidate("alpha");
+ const beta = candidate("beta");
+ const registry = new AnalysisProviderRegistry(
+ [alpha.provider, beta.provider],
+ "beta",
+ () => true,
+ );
+ expect(await registry.select(DATABASE_TARGET)).toMatchObject({
+ ok: true,
+ value: {
+ binding: { identity: { id: "beta" }, selectionSource: "environment" },
+ },
+ });
+ expect(await registry.select(DATABASE_TARGET, "alpha")).toMatchObject({
+ ok: true,
+ value: {
+ binding: { identity: { id: "alpha" }, selectionSource: "request" },
+ },
+ });
+ expect(await registry.select(DATABASE_TARGET, "auto")).toMatchObject({
+ ok: true,
+ value: { binding: null },
+ });
+ expect(await registry.select(DATABASE_TARGET, "missing")).toMatchObject({
+ ok: false,
+ error: { reason: "unknown_provider" },
+ });
+ });
+
+ it("preserves ordinary ambiguity when auxiliary inspection does not cover the target", async () => {
+ const registry = new AnalysisProviderRegistry(
+ [candidate("alpha").provider, candidate("beta").provider],
+ "auto",
+ () => false,
+ );
+ expect(await registry.select(DATABASE_TARGET)).toMatchObject({
+ ok: false,
+ error: { reason: "ambiguous" },
+ });
+ const controller = new AbortController();
+ controller.abort();
+ expect(
+ await registry.select(DATABASE_TARGET, undefined, {
+ signal: controller.signal,
+ }),
+ ).toMatchObject({ ok: false, error: { _tag: "AnalysisCancelledError" } });
+ });
+});
+
describe("analysis provider registry: selection failures", () => {
it("returns actionable unknown, unavailable, and unsupported failures", async () => {
const unavailable = candidate("alpha", { available: false });
diff --git a/src/application/binary/AnalysisProviderRegistry.ts b/src/application/binary/AnalysisProviderRegistry.ts
index 427ceee16..f2a45bd29 100644
--- a/src/application/binary/AnalysisProviderRegistry.ts
+++ b/src/application/binary/AnalysisProviderRegistry.ts
@@ -83,6 +83,9 @@ export class AnalysisProviderRegistry {
constructor(
providers: readonly AnalysisProviderCandidate[],
defaultSelector: AnalysisProviderSelector = "auto",
+ private readonly canInspectWithoutDeepProvider?: (
+ target: BinaryTarget,
+ ) => boolean,
) {
this.#defaultSelector =
analysisProviderSelectorSchema.parse(defaultSelector);
@@ -209,6 +212,12 @@ export class AnalysisProviderRegistry {
const baseline = this.candidates(target);
if (signalIsAborted(options.signal))
return err(new AnalysisCancelledError("open_binary"));
+ if (selector === "auto" && this.canInspectWithoutDeepProvider?.(target))
+ return ok({
+ requestedProviderId: selector,
+ binding: null,
+ candidates: baseline,
+ });
if (
request === undefined &&
this.#byId.has(selector) &&
diff --git a/src/composition/binary.ts b/src/composition/binary.ts
index 8cfa4b8d0..03fb2aacd 100644
--- a/src/composition/binary.ts
+++ b/src/composition/binary.ts
@@ -1,3 +1,4 @@
+import { isAppleDispatchMetadataTarget } from "../domain/native/objcSwiftMetadata.js";
import { snapshotEnvironment } from "../process/snapshotEnvironment.js";
import type { AppConfig } from "../config/types.js";
import type { BinarySession } from "../application/binary/BinarySession.js";
@@ -21,18 +22,49 @@ export const createBinarySession = (
config: AppConfig,
logger: Logger,
selectedEnvironment: Readonly<NodeJS.ProcessEnv>,
+): BinarySession =>
+ composeDeepBinarySession(config, logger, selectedEnvironment);
+
+/** Prefer byte-backed dispatch inspection while retaining deep symbol fallback. */
+export const createNativeDispatchBinarySession = (
+ config: AppConfig,
+ logger: Logger,
+ selectedEnvironment: Readonly<NodeJS.ProcessEnv>,
+): BinarySession =>
+ composeDeepBinarySession(
+ config,
+ logger,
+ selectedEnvironment,
+ "inspect_native_dispatch_metadata",
+ );
+
+const composeDeepBinarySession = (
+ config: AppConfig,
+ logger: Logger,
+ selectedEnvironment: Readonly<NodeJS.ProcessEnv>,
+ inspection?: "inspect_native_dispatch_metadata",
): BinarySession => {
const environment = snapshotEnvironment(selectedEnvironment);
const hopper = new HopperProvider(config, logger, environment);
const ghidra = new GhidraProvider(config, logger, environment);
const ida = new IdaProvider(config, environment);
const kuna = new KunaProvider(environment);
+ const auxiliaryDeclarations =
+ auxiliaryAnalysisProviderDeclarations(environment);
+ const nativeInspectionAvailable =
+ inspection !== undefined &&
+ auxiliaryDeclarations.some(({ capabilities }) =>
+ capabilities.some(
+ ({ operation, available }) => operation === inspection && available,
+ ),
+ );
return composeBinarySession(
new AnalysisProviderRegistry(
[hopper, ghidra, ida, kuna],
config.analysisProvider,
+ nativeInspectionAvailable ? isAppleDispatchMetadataTarget : undefined,
),
- auxiliaryAnalysisProviderDeclarations(environment).map(
+ auxiliaryDeclarations.map(
(declaration) => new LazyAnalysisProvider(declaration),
),
);
diff --git a/src/composition/directAnalysis.ts b/src/composition/directAnalysis.ts
index 85854861e..2512e2e43 100644
--- a/src/composition/directAnalysis.ts
+++ b/src/composition/directAnalysis.ts
@@ -28,6 +28,10 @@ export const createDirectAnalysis = (
const { createManagedBinarySession } = await import("./binary.js");
return createManagedBinarySession();
},
+ createNativeDispatchBinarySession: async (config, logger) => {
+ const { createNativeDispatchBinarySession } = await import("./binary.js");
+ return createNativeDispatchBinarySession(config, logger, environment);
+ },
createNativeBinarySession: async () => {
const { createNativeBinarySession } = await import("./binary.js");
return createNativeBinarySession(environment);
diff --git a/src/domain/native/objcSwiftMetadata.ts b/src/domain/native/objcSwiftMetadata.ts
index 851b9f1b7..6c75b9b1f 100644
--- a/src/domain/native/objcSwiftMetadata.ts
+++ b/src/domain/native/objcSwiftMetadata.ts
@@ -1,4 +1,5 @@
import { z } from "zod";
+import { isMachOFormat, type BinaryTarget } from "../binaryTargetTypes.js";
import { providerSchema } from "../evidence.js";
import { isSwiftMangledSymbolName } from "../swiftSymbolClassification.js";
@@ -7,6 +8,10 @@ import {
compositeKey,
} from "../unicodeCodePointOrder.js";
+/** Targets admitted to the Apple byte reader; the reader validates slice support. */
+export const isAppleDispatchMetadataTarget = (target: BinaryTarget): boolean =>
+ target.kind === "executable" && isMachOFormat(target.format);
+
/** Objective-C method type. */
export const objcMethodTypeSchema = z.enum([
"instance",
diff --git a/src/native/NativeMacOSProvider.ts b/src/native/NativeMacOSProvider.ts
index f2d76fd7e..49b1b894a 100644
--- a/src/native/NativeMacOSProvider.ts
+++ b/src/native/NativeMacOSProvider.ts
@@ -28,6 +28,7 @@ import {
isMachOFormat,
type BinaryTarget,
} from "../domain/binaryTargetTypes.js";
+import { isAppleDispatchMetadataTarget } from "../domain/native/objcSwiftMetadata.js";
import { toolContract } from "../contracts/toolContracts.js";
import { analysisInputErrorFromIssues } from "../domain/inputIssueProjection.js";
import type { EvidenceLocation } from "../domain/evidence.js";
@@ -186,10 +187,7 @@ class NativeMacOSClient implements AnalysisClient {
},
),
);
- if (
- this.target.kind !== "executable" ||
- !isMachOFormat(this.target.format)
- )
+ if (!isAppleDispatchMetadataTarget(this.target))
return err(
new AnalysisCapabilityUnavailableError(
IDENTITY.id,
diff --git a/tests/boundary/cli/nativeDispatchPropertyAttributes.test.ts b/tests/boundary/cli/nativeDispatchPropertyAttributes.test.ts
index 6d90fb57f..d96f8deca 100644
--- a/tests/boundary/cli/nativeDispatchPropertyAttributes.test.ts
+++ b/tests/boundary/cli/nativeDispatchPropertyAttributes.test.ts
@@ -112,3 +112,53 @@ describe.skipIf(process.platform !== "darwin")(
);
},
);
+
+describe.skipIf(process.platform !== "darwin")(
+ "native dispatch provider choice",
+ () => {
+ cliTest.for([
+ [[], "missing", 1, "unknown_provider"],
+ [["--provider", "missing"], "auto", 1, "unknown_provider"],
+ [["--provider", "auto"], "missing", 0, null],
+ ] as const)(
+ "preserves explicit selection %j",
+ async ([options, configured, exitCode, reason], { cli }) => {
+ const directory = await createTestTempDirectory(
+ "rea-dispatch-provider-choice-",
+ );
+ const path = join(directory, "fixture.bin");
+ await writeFile(path, machoWithPropertyAttributes("Tq,N"));
+ const output = await cli.run({
+ arguments: [
+ "inspect-native-dispatch-metadata",
+ path,
+ ...options,
+ "--json",
+ ],
+ environment: {
+ REA_LOG_LEVEL: "silent",
+ REA_ANALYSIS_PROVIDER: configured,
+ },
+ });
+ expect(output.exitCode, `${output.stdout}\n${output.stderr}`).toBe(
+ exitCode,
+ );
+ if (reason !== null)
+ expect(output.json).toMatchObject({
+ code: "capability_unavailable",
+ details: {
+ selection_reason: reason,
+ requested_provider_id: "missing",
+ },
+ });
+ else
+ expect(output.json).toMatchObject({
+ normalized_result: {
+ provider: { id: "native-macos" },
+ result: { objc_classes: [{ name: "Fixture" }] },
+ },
+ });
+ },
+ );
+ },
+);
diff --git a/tests/boundary/filesystem/directAnalysisSnapshotReplay.test.ts b/tests/boundary/filesystem/directAnalysisSnapshotReplay.test.ts
index 08d33180e..75e4910fe 100644
--- a/tests/boundary/filesystem/directAnalysisSnapshotReplay.test.ts
+++ b/tests/boundary/filesystem/directAnalysisSnapshotReplay.test.ts
@@ -246,6 +246,8 @@ const createSnapshotReplayFixture = async (
readConfiguration: () => parseConfig({}),
createBinarySession: () => createTestBinarySession(provider),
createArtifactBinarySession: () => createTestBinarySession(provider),
+ createNativeDispatchBinarySession: () =>
+ createTestBinarySession(provider),
createNativeBinarySession: () => createTestBinarySession(provider),
createManagedBinarySession: () => createTestBinarySession(provider),
};
@@ -1003,6 +1005,7 @@ describe("workflow snapshot profile and cancellation binding", () => {
readConfiguration: () => parseConfig({}),
createBinarySession: () => session,
createArtifactBinarySession: () => session,
+ createNativeDispatchBinarySession: () => session,
createNativeBinarySession: () => session,
createManagedBinarySession: () => session,
};
diff --git a/tests/boundary/mcp/officialArguments.test.ts b/tests/boundary/mcp/officialArguments.test.ts
index eb91f2775..5af106f94 100644
--- a/tests/boundary/mcp/officialArguments.test.ts
+++ b/tests/boundary/mcp/officialArguments.test.ts
@@ -71,6 +71,7 @@ describe("official operation arguments", () => {
readConfiguration: () => parseConfig({}),
createBinarySession: createSession,
createArtifactBinarySession: createSession,
+ createNativeDispatchBinarySession: createSession,
createNativeBinarySession: createSession,
createManagedBinarySession: createSession,
};
diff --git a/tests/boundary/mcp/workflowSnapshotReplayMcp.test.ts b/tests/boundary/mcp/workflowSnapshotReplayMcp.test.ts
index 165f2bb26..29ab193f7 100644
--- a/tests/boundary/mcp/workflowSnapshotReplayMcp.test.ts
+++ b/tests/boundary/mcp/workflowSnapshotReplayMcp.test.ts
@@ -331,6 +331,7 @@ describe("MCP composed workflow snapshot replay", () => {
readConfiguration: () => parseConfig({}),
createBinarySession: () => createWorkflowSession(provider),
createArtifactBinarySession: () => createWorkflowSession(provider),
+ createNativeDispatchBinarySession: () => createWorkflowSession(provider),
createNativeBinarySession: () => createWorkflowSession(provider),
createManagedBinarySession: () => createWorkflowSession(provider),
};
diff --git a/tests/composition/directAnalysisCleanup.test.ts b/tests/composition/directAnalysisCleanup.test.ts
index 7678a8323..8735177d9 100644
--- a/tests/composition/directAnalysisCleanup.test.ts
+++ b/tests/composition/directAnalysisCleanup.test.ts
@@ -62,6 +62,7 @@ const factories = (
readConfiguration: () => parseConfig({}),
createBinarySession: () => session,
createArtifactBinarySession: () => session,
+ createNativeDispatchBinarySession: () => session,
createNativeBinarySession: () => session,
createManagedBinarySession: () => session,
};
diff --git a/tests/composition/directAnalysisFactories.test.ts b/tests/composition/directAnalysisFactories.test.ts
index 5d3b87016..57f3779b3 100644
--- a/tests/composition/directAnalysisFactories.test.ts
+++ b/tests/composition/directAnalysisFactories.test.ts
@@ -37,6 +37,7 @@ const recordingFactories = (failure?: Error) => {
readConfiguration: () => parseConfig({}),
createBinarySession: createSession,
createArtifactBinarySession: createSession,
+ createNativeDispatchBinarySession: createSession,
createNativeBinarySession: createSession,
createManagedBinarySession: createSession,
};
@@ -104,6 +105,36 @@ describe("one-shot analysis factory boundary", () => {
expect(factories.sessions[0]?.activeTarget()).toBeUndefined();
});
+ it.each([undefined, "/fixture/committed.snapshot"])(
+ "selects the dispatch inspection factory only without a requested snapshot (%s)",
+ async (snapshotPath) => {
+ const factories = recordingFactories();
+ const expected = new Error("selected factory");
+ const unexpected = (): never => {
+ throw new Error("wrong session policy");
+ };
+ const selected = (): never => {
+ throw expected;
+ };
+ await expect(
+ runDirectAnalysis(
+ {
+ ...factories.dependencies,
+ createBinarySession:
+ snapshotPath === undefined ? unexpected : selected,
+ createNativeDispatchBinarySession:
+ snapshotPath === undefined ? selected : unexpected,
+ },
+ "/fixture/target",
+ "inspect_native_dispatch_metadata",
+ {},
+ { snapshotPath },
+ ),
+ ).rejects.toBe(expected);
+ expect(factories.sessions).toEqual([]);
+ },
+ );
+
it.each([
"inspect_macho",
"inspect_signature",
diff --git a/tests/composition/directAnalysisLimitations.test.ts b/tests/composition/directAnalysisLimitations.test.ts
index 7c307e135..1975559f5 100644
--- a/tests/composition/directAnalysisLimitations.test.ts
+++ b/tests/composition/directAnalysisLimitations.test.ts
@@ -36,6 +36,7 @@ it("retains provider coverage caveats in a composed CLI result and closes the se
createBinarySession: createSession,
createManagedBinarySession: createSession,
createArtifactBinarySession: createSession,
+ createNativeDispatchBinarySession: createSession,
createNativeBinarySession: createSession,
},
path,
diff --git a/tests/composition/directAnalysisXrefs.test.ts b/tests/composition/directAnalysisXrefs.test.ts
index 35181bf0d..a67b97780 100644
--- a/tests/composition/directAnalysisXrefs.test.ts
+++ b/tests/composition/directAnalysisXrefs.test.ts
@@ -83,6 +83,7 @@ const fixture = (
readConfiguration: () => parseConfig({}),
createBinarySession: createSession,
createArtifactBinarySession: createSession,
+ createNativeDispatchBinarySession: createSession,
createNativeBinarySession: createSession,
createManagedBinarySession: createSession,
};