feat(ida): add read-only GUI and headless MCP providers (#754)
This commit is contained in:
@@ -159,6 +159,8 @@ type CapabilityAvailability =
|
||||
};
|
||||
|
||||
export type CapabilityDescriptor = CapabilityAvailability & {
|
||||
/** Live external state must be re-observed, including after snapshot import. */
|
||||
readonly cachePolicy?: "snapshot" | "live";
|
||||
readonly provider: ProviderIdentity;
|
||||
readonly operation: Exclude<AnalysisOperation, "health">;
|
||||
readonly effects: CapabilityEffects;
|
||||
|
||||
@@ -68,6 +68,7 @@ export const isSnapshotCacheable = (
|
||||
(!CURSOR_DEFAULT_OPERATIONS.has(operation) ||
|
||||
typeof parameters.address === "string") &&
|
||||
descriptor?.effects.mutatesArtifact === false &&
|
||||
descriptor.cachePolicy !== "live" &&
|
||||
descriptor.effects.mayWriteFilesystem === false &&
|
||||
descriptor.effects.changesPermissions === false;
|
||||
|
||||
|
||||
@@ -114,6 +114,16 @@ export class BinarySession
|
||||
);
|
||||
}
|
||||
|
||||
/** External live state cannot be replayed by a direct or composed CLI workflow. */
|
||||
allowsSnapshotReplay(operation: AnalysisOperation): boolean {
|
||||
const capabilities = this.#active?.route.capabilities;
|
||||
const descriptor = capabilities?.get(operation);
|
||||
if (descriptor !== undefined) return descriptor.cachePolicy !== "live";
|
||||
return ![...(capabilities?.values() ?? [])].some(
|
||||
({ cachePolicy }) => cachePolicy === "live",
|
||||
);
|
||||
}
|
||||
|
||||
/** Return the selected immutable profile, optionally scoped to an operation. */
|
||||
analysisProfile(
|
||||
operation?: AnalysisOperation,
|
||||
|
||||
@@ -2,6 +2,7 @@ import type { ExecutableFormatHint } from "../domain/dosCom.js";
|
||||
import { parseConfig } from "../config.js";
|
||||
import type { JsonValue } from "../domain/jsonValue.js";
|
||||
import { EnhancedTools } from "./EnhancedTools.js";
|
||||
import { executeFunctionAnalysisEvidence } from "./FunctionAnalysisEvidence.js";
|
||||
import { createBinarySession, createManagedBinarySession } from "./runtime.js";
|
||||
import { silentLogger, type Logger } from "../logger.js";
|
||||
import { createEvidence } from "../domain/evidence.js";
|
||||
@@ -201,7 +202,7 @@ const runAnalysis = async (
|
||||
const evidenceProfile = analysisProfileForEvidence(session, tool);
|
||||
const bindingProfile = session.analysisProfile();
|
||||
if (
|
||||
tool !== "trace_native_ui_action" &&
|
||||
canReplayDirectTool(session, tool) &&
|
||||
snapshot !== undefined &&
|
||||
evidenceProfile !== undefined &&
|
||||
bindingProfile !== undefined
|
||||
@@ -245,6 +246,12 @@ const runAnalysis = async (
|
||||
}
|
||||
};
|
||||
|
||||
const canReplayDirectTool = (
|
||||
session: ReturnType<typeof createBinarySession>,
|
||||
tool: Parameters<typeof runAnalysis>[1],
|
||||
): boolean =>
|
||||
tool !== "trace_native_ui_action" && session.allowsSnapshotReplay(tool);
|
||||
|
||||
const executeAnalysisTool = async (input: {
|
||||
readonly session: ReturnType<typeof createBinarySession>;
|
||||
readonly openedTarget: BinaryTarget;
|
||||
@@ -258,9 +265,19 @@ const executeAnalysisTool = async (input: {
|
||||
readonly evidenceProfile: AnalysisProfileCommitment | undefined;
|
||||
}): Promise<{ readonly output: JsonValue; readonly evidence?: Evidence }> => {
|
||||
const { session, tool, signal, evidenceProfile } = input;
|
||||
if (tool === "analyze_function") {
|
||||
const result = await executeFunctionAnalysisEvidence(
|
||||
session,
|
||||
input.arguments,
|
||||
input.openedTarget,
|
||||
{ signal },
|
||||
);
|
||||
return result.ok
|
||||
? { output: result.value, evidence: result.value }
|
||||
: { output: cliError(result.error) };
|
||||
}
|
||||
if (
|
||||
tool === "binary_overview" ||
|
||||
tool === "analyze_function" ||
|
||||
tool === "inspect_native_api" ||
|
||||
tool === "inspect_native_dispatch_metadata" ||
|
||||
tool === "trace_feature" ||
|
||||
@@ -273,22 +290,16 @@ const executeAnalysisTool = async (input: {
|
||||
signal,
|
||||
);
|
||||
if (!result.ok) return { output: cliError(result.error) };
|
||||
const evidence = createEvidence(
|
||||
input.openedTarget,
|
||||
tool === "analyze_function"
|
||||
? session.providerIdentity(tool)
|
||||
: REA_WORKFLOW_PROVIDER,
|
||||
{
|
||||
operation: tool,
|
||||
parameters: input.arguments,
|
||||
result: result.value,
|
||||
...(evidenceProfile === undefined
|
||||
? {}
|
||||
: { analysisProfile: evidenceProfile }),
|
||||
confidence: "derived",
|
||||
limitations: ["Derived by an REA composed workflow."],
|
||||
},
|
||||
);
|
||||
const evidence = createEvidence(input.openedTarget, REA_WORKFLOW_PROVIDER, {
|
||||
operation: tool,
|
||||
parameters: input.arguments,
|
||||
result: result.value,
|
||||
...(evidenceProfile === undefined
|
||||
? {}
|
||||
: { analysisProfile: evidenceProfile }),
|
||||
confidence: "derived",
|
||||
limitations: ["Derived by an REA composed workflow."],
|
||||
});
|
||||
return { output: evidence, evidence };
|
||||
}
|
||||
const result = await session.execute(tool, input.arguments, { signal });
|
||||
|
||||
@@ -32,7 +32,7 @@ import {
|
||||
} from "./DoctorScope.js";
|
||||
|
||||
/** Deep provider IDs whose readiness can be selected explicitly by doctor. */
|
||||
export const DOCTOR_PROVIDER_IDS = ["hopper", "ghidra"] as const;
|
||||
export const DOCTOR_PROVIDER_IDS = ["hopper", "ghidra", "ida"] as const;
|
||||
/** One actionable environment diagnostic. */
|
||||
export interface DoctorCheck {
|
||||
readonly name: string;
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
import type {
|
||||
AnalysisOperationPort,
|
||||
ExecutionOptions,
|
||||
} from "./AnalysisProvider.js";
|
||||
import { toolContract } from "../contracts/toolContracts.js";
|
||||
import { AnalysisInputError } from "../domain/analysisErrorCore.js";
|
||||
import type { BinaryTarget } from "../domain/binaryTarget.js";
|
||||
import { createEvidence } from "../domain/evidence.js";
|
||||
import { parseFunctionDossier } from "../domain/hopperValues.js";
|
||||
import { jsonObjectSchema, type JsonValue } from "../domain/jsonValue.js";
|
||||
import { err, ok } from "../domain/result.js";
|
||||
import { projectInputIssues } from "../domain/inputIssueProjection.js";
|
||||
|
||||
/** Preserve the provider's observations when presenting a validated function dossier. */
|
||||
export const executeFunctionAnalysisEvidence = async (
|
||||
analysis: AnalysisOperationPort,
|
||||
parameters: Readonly<Record<string, JsonValue>>,
|
||||
target: BinaryTarget | undefined,
|
||||
options?: ExecutionOptions,
|
||||
) => {
|
||||
const input =
|
||||
toolContract("analyze_function").inputSchema.safeParse(parameters);
|
||||
if (!input.success)
|
||||
return err(
|
||||
new AnalysisInputError(
|
||||
"analyze_function",
|
||||
{ cause: input.error },
|
||||
projectInputIssues(input.error.issues, parameters),
|
||||
),
|
||||
);
|
||||
const arguments_ = jsonObjectSchema.parse(input.data);
|
||||
const execution = await analysis.execute(
|
||||
"analyze_function",
|
||||
arguments_,
|
||||
options,
|
||||
);
|
||||
if (!execution.ok) return execution;
|
||||
const dossier = parseFunctionDossier(execution.value.result);
|
||||
if (!dossier.ok) return dossier;
|
||||
const observation = execution.value;
|
||||
return ok(
|
||||
createEvidence(observation.subject ?? target, observation.provider, {
|
||||
operation: "analyze_function",
|
||||
parameters: arguments_,
|
||||
result: dossier.value,
|
||||
rawResult: observation.rawResult,
|
||||
limitations: observation.limitations,
|
||||
locations: observation.locations,
|
||||
...(observation.analysisProfile === undefined
|
||||
? {}
|
||||
: { analysisProfile: observation.analysisProfile }),
|
||||
}),
|
||||
);
|
||||
};
|
||||
@@ -2,6 +2,7 @@ import type { AppConfig } from "../config.js";
|
||||
import type { BinarySession } from "./BinarySession.js";
|
||||
import { HopperProvider } from "../hopper/HopperProvider.js";
|
||||
import { GhidraProvider } from "../ghidra/GhidraProvider.js";
|
||||
import { IdaProvider } from "../ida/IdaProvider.js";
|
||||
import { silentLogger, type Logger } from "../logger.js";
|
||||
import { GENERATED_AUXILIARY_PROVIDERS } from "../generatedMcpToolCatalog.js";
|
||||
import { AnalysisProviderRegistry } from "./AnalysisProviderRegistry.js";
|
||||
@@ -22,6 +23,7 @@ export const createBinarySession = (
|
||||
): BinarySession => {
|
||||
const hopper = new HopperProvider(config, logger);
|
||||
const ghidra = new GhidraProvider(config, logger);
|
||||
const ida = new IdaProvider(config);
|
||||
const auxiliary = new Map(
|
||||
GENERATED_AUXILIARY_PROVIDERS.map((provider) => [
|
||||
provider.identity.id,
|
||||
@@ -42,7 +44,10 @@ export const createBinarySession = (
|
||||
});
|
||||
};
|
||||
return composeBinarySession(
|
||||
new AnalysisProviderRegistry([hopper, ghidra], config.analysisProvider),
|
||||
new AnalysisProviderRegistry(
|
||||
[hopper, ghidra, ida],
|
||||
config.analysisProvider,
|
||||
),
|
||||
[
|
||||
lazyProvider("rea-artifact-graph", async () => {
|
||||
const { ArtifactProvider } =
|
||||
|
||||
@@ -28,6 +28,16 @@ describe("runtime configuration", () => {
|
||||
});
|
||||
|
||||
it("parses one shared provider selector and rejects unstable IDs", () => {
|
||||
expect(
|
||||
parseConfig({
|
||||
REA_ANALYSIS_PROVIDER: "ida",
|
||||
REA_IDA_MCP_CONFIG: "/tmp/ida-mcp.json",
|
||||
}),
|
||||
).toMatchObject({
|
||||
ok: true,
|
||||
value: { analysisProvider: "ida", idaMcpConfigPath: "/tmp/ida-mcp.json" },
|
||||
});
|
||||
expect(parseConfig({ REA_IDA_MCP_CONFIG: "relative.json" }).ok).toBe(false);
|
||||
expect(parseConfig({ REA_ANALYSIS_PROVIDER: "ghidra" })).toMatchObject({
|
||||
ok: true,
|
||||
value: { analysisProvider: "ghidra" },
|
||||
|
||||
@@ -7,6 +7,11 @@ import { analysisProviderSelectorSchema } from "../contracts/providerSelection.j
|
||||
|
||||
const environmentSchema = z.object({
|
||||
REA_ANALYSIS_PROVIDER: analysisProviderSelectorSchema.default("auto"),
|
||||
REA_IDA_MCP_CONFIG: z
|
||||
.string()
|
||||
.min(1)
|
||||
.refine(isAbsolute, "REA_IDA_MCP_CONFIG must be absolute")
|
||||
.optional(),
|
||||
GHIDRA_INSTALL_DIR: z
|
||||
.string()
|
||||
.min(1)
|
||||
|
||||
@@ -25,6 +25,9 @@ export const parseConfig = (
|
||||
if (!secretPatterns.ok) return secretPatterns;
|
||||
return ok({
|
||||
analysisProvider: env.REA_ANALYSIS_PROVIDER,
|
||||
...(env.REA_IDA_MCP_CONFIG === undefined
|
||||
? {}
|
||||
: { idaMcpConfigPath: env.REA_IDA_MCP_CONFIG }),
|
||||
ghidraInstallDir: env.GHIDRA_INSTALL_DIR,
|
||||
ghidraJavaHome: env.JAVA_HOME,
|
||||
...(env.REA_GHIDRA_NATIVEAOT_JAR === undefined
|
||||
|
||||
@@ -4,6 +4,7 @@ import type { AnalysisProviderSelector } from "../contracts/providerSelection.js
|
||||
|
||||
export interface AppConfig {
|
||||
readonly analysisProvider: AnalysisProviderSelector;
|
||||
readonly idaMcpConfigPath?: string;
|
||||
readonly ghidraInstallDir: string | undefined;
|
||||
readonly ghidraJavaHome: string | undefined;
|
||||
readonly ghidraNativeAotJar?: string;
|
||||
|
||||
@@ -5,12 +5,16 @@ import {
|
||||
} from "./application/Doctor.js";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { inspectSystemGhidraProvider } from "./ghidra/GhidraDoctor.js";
|
||||
import { inspectIdaRegistration } from "./ida/IdaDoctor.js";
|
||||
import { selectLinuxPrivateDisplayStrategy } from "./hopper/LinuxPrivateDisplayProbe.js";
|
||||
|
||||
/** Compose provider diagnostics at the outer CLI adapter boundary. */
|
||||
export const createSystemDoctorHost = (): DoctorHost =>
|
||||
systemDoctorHost({
|
||||
providerInspections: async () => [await inspectSystemGhidraProvider()],
|
||||
providerInspections: async () => [
|
||||
await inspectSystemGhidraProvider(),
|
||||
inspectIdaRegistration(process.env.REA_IDA_MCP_CONFIG),
|
||||
],
|
||||
linuxDemoRuntimeCheck: inspectLinuxPrivateDisplay,
|
||||
});
|
||||
|
||||
|
||||
@@ -5,5 +5,5 @@ export const PACKAGE_METADATA = {
|
||||
serverSdkVersion: "2.3.1",
|
||||
clientSdkVersion: "2.3.1",
|
||||
coreSdkVersion: "2.3.1",
|
||||
skillVersion: "25",
|
||||
skillVersion: "26",
|
||||
} as const;
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
import { rm, writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
import { createIdaTarget } from "../../tests/fixtures/idaMcp.js";
|
||||
import { readIdaConfiguration } from "./IdaConfiguration.js";
|
||||
import { decodeIdaToolResult } from "./IdaMcpConnection.js";
|
||||
|
||||
const roots: string[] = [];
|
||||
afterEach(async () => {
|
||||
for (const root of roots.splice(0))
|
||||
await rm(root, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
describe("IDA registration and MCP result boundaries", () => {
|
||||
it("reuses a direct or upstream mcpServers registration with no installation", async () => {
|
||||
const { root } = await createIdaTarget();
|
||||
roots.push(root);
|
||||
const path = join(root, "mcp.json");
|
||||
for (const input of [
|
||||
{ command: "python", args: ["server.py"] },
|
||||
{
|
||||
mcpServers: {
|
||||
"ida-pro-mcp": { command: "python", args: ["server.py"] },
|
||||
unrelated: { command: "untouched" },
|
||||
},
|
||||
},
|
||||
]) {
|
||||
await writeFile(path, JSON.stringify(input));
|
||||
expect(readIdaConfiguration(path)).toMatchObject({
|
||||
ok: true,
|
||||
value: { command: "python", args: ["server.py"], mode: "attached" },
|
||||
});
|
||||
}
|
||||
});
|
||||
it("validates local transport and lifecycle inputs without leaking auth into a profile", async () => {
|
||||
const { root } = await createIdaTarget();
|
||||
roots.push(root);
|
||||
const path = join(root, "mcp.json");
|
||||
for (const input of [
|
||||
{ url: "https://example.com/mcp" },
|
||||
{ url: "http://token@127.0.0.1/mcp" },
|
||||
{ command: "python", mode: "force_gui" },
|
||||
{ command: "python", workspaceRoot: "relative" },
|
||||
{ command: "python", args: "server.py" },
|
||||
]) {
|
||||
await writeFile(path, JSON.stringify(input));
|
||||
expect(readIdaConfiguration(path).ok).toBe(false);
|
||||
}
|
||||
await writeFile(
|
||||
path,
|
||||
JSON.stringify({
|
||||
url: "http://127.0.0.1:8745/mcp",
|
||||
mode: "headless",
|
||||
headers: { Authorization: "Bearer fixture-secret" },
|
||||
}),
|
||||
);
|
||||
expect(readIdaConfiguration(path).ok).toBe(true);
|
||||
await writeFile(path, "malformed");
|
||||
expect(readIdaConfiguration(path).ok).toBe(false);
|
||||
expect(readIdaConfiguration(join(root, "absent")).ok).toBe(false);
|
||||
});
|
||||
it("parses FastMCP wrappers, structured dictionaries, text, arrays, and explicit errors", () => {
|
||||
expect(
|
||||
decodeIdaToolResult({
|
||||
content: [],
|
||||
structuredContent: { result: [{ address: "0x1" }] },
|
||||
}),
|
||||
).toEqual([{ address: "0x1" }]);
|
||||
expect(
|
||||
decodeIdaToolResult({
|
||||
content: [],
|
||||
structuredContent: { address: "0x1", name: "f" },
|
||||
}),
|
||||
).toEqual({ address: "0x1", name: "f" });
|
||||
expect(
|
||||
decodeIdaToolResult({ content: [{ type: "text", text: "int f() {}" }] }),
|
||||
).toBe("int f() {}");
|
||||
expect(
|
||||
decodeIdaToolResult({ content: [{ type: "text", text: '{"data":[]}' }] }),
|
||||
).toEqual({ data: [] });
|
||||
expect(() =>
|
||||
decodeIdaToolResult({
|
||||
content: [{ type: "text", text: "No function at 0x1" }],
|
||||
isError: true,
|
||||
}),
|
||||
).toThrow("No function at 0x1");
|
||||
expect(() => decodeIdaToolResult({ content: "invalid" })).toThrow();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,80 @@
|
||||
import { readFileSync, statSync } from "node:fs";
|
||||
import { isAbsolute } from "node:path";
|
||||
import { z } from "zod";
|
||||
import { ConfigurationError } from "../domain/configurationErrors.js";
|
||||
import { err, ok, type Result } from "../domain/result.js";
|
||||
|
||||
const common = {
|
||||
mode: z.enum(["attached", "headless"]).default("attached"),
|
||||
timeoutMs: z.number().int().positive().max(2_147_483_647).default(300_000),
|
||||
workspaceRoot: z
|
||||
.string()
|
||||
.refine(isAbsolute, "workspaceRoot must be absolute on the REA host")
|
||||
.optional(),
|
||||
};
|
||||
const registrationSchema = z.union([
|
||||
z
|
||||
.object({
|
||||
...common,
|
||||
type: z.literal("stdio").optional(),
|
||||
command: z.string().min(1),
|
||||
args: z.array(z.string()).default([]),
|
||||
env: z.record(z.string(), z.string()).default({}),
|
||||
})
|
||||
.strict(),
|
||||
z
|
||||
.object({
|
||||
...common,
|
||||
type: z.literal("http").optional(),
|
||||
url: z.url().refine((value) => {
|
||||
const url = new URL(value);
|
||||
return (
|
||||
["http:", "https:"].includes(url.protocol) &&
|
||||
["localhost", "127.0.0.1", "[::1]"].includes(url.hostname) &&
|
||||
url.username === "" &&
|
||||
url.password === ""
|
||||
);
|
||||
}, "IDA MCP must use a local HTTP endpoint without URL credentials"),
|
||||
headers: z.record(z.string(), z.string()).default({}),
|
||||
})
|
||||
.strict(),
|
||||
]);
|
||||
|
||||
/** An upstream MCP registration plus REA's database lifecycle selection. */
|
||||
export type IdaConfiguration = z.infer<typeof registrationSchema>;
|
||||
|
||||
/** Read one explicit registration without starting or installing a provider. */
|
||||
export const readIdaConfiguration = (
|
||||
path: string,
|
||||
): Result<IdaConfiguration, ConfigurationError> => {
|
||||
try {
|
||||
if (statSync(path).size > 65_536)
|
||||
return err(
|
||||
new ConfigurationError(
|
||||
"REA_IDA_MCP_CONFIG exceeds the 64 KiB registration size.",
|
||||
),
|
||||
);
|
||||
const input: unknown = JSON.parse(readFileSync(path, "utf8"));
|
||||
const envelope = z
|
||||
.object({ mcpServers: z.object({ "ida-pro-mcp": z.unknown() }) })
|
||||
.safeParse(input);
|
||||
const parsed = registrationSchema.safeParse(
|
||||
envelope.success ? envelope.data.mcpServers["ida-pro-mcp"] : input,
|
||||
);
|
||||
return parsed.success
|
||||
? ok(parsed.data)
|
||||
: err(
|
||||
new ConfigurationError(
|
||||
"REA_IDA_MCP_CONFIG must contain an IDA MCP command/args or local url registration; see docs/ida-provider.md.",
|
||||
{ cause: parsed.error },
|
||||
),
|
||||
);
|
||||
} catch (cause: unknown) {
|
||||
return err(
|
||||
new ConfigurationError(
|
||||
`Cannot read IDA MCP registration ${path}; provide a readable JSON configuration.`,
|
||||
{ cause },
|
||||
),
|
||||
);
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,45 @@
|
||||
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { afterEach, expect, it } from "vitest";
|
||||
import { inspectIdaRegistration } from "./IdaDoctor.js";
|
||||
import { providerRegistrationEnvironment } from "../application/SetupRegistrationEnvironment.js";
|
||||
|
||||
const roots: string[] = [];
|
||||
afterEach(async () => {
|
||||
for (const root of roots.splice(0))
|
||||
await rm(root, { recursive: true, force: true });
|
||||
});
|
||||
it("keeps IDA configuration for setup without copying transport credentials or claiming runtime verification", async () => {
|
||||
const root = await mkdtemp(join(tmpdir(), "rea-ida-doctor-"));
|
||||
roots.push(root);
|
||||
const path = join(root, "registration.json");
|
||||
await writeFile(
|
||||
path,
|
||||
JSON.stringify({
|
||||
url: "http://127.0.0.1:12345/mcp",
|
||||
headers: { Authorization: "Bearer fixture-secret" },
|
||||
mode: "headless",
|
||||
}),
|
||||
);
|
||||
const inspection = inspectIdaRegistration(path);
|
||||
expect(inspection).toMatchObject({
|
||||
configured: true,
|
||||
available: true,
|
||||
providerVersion: null,
|
||||
});
|
||||
expect(inspection.checks[0]?.detail).toContain("no IDA process was launched");
|
||||
expect(providerRegistrationEnvironment([inspection])).toEqual({
|
||||
REA_IDA_MCP_CONFIG: path,
|
||||
});
|
||||
expect(JSON.stringify(inspection)).not.toContain("fixture-secret");
|
||||
await writeFile(path, JSON.stringify({ url: "https://example.com/mcp" }));
|
||||
expect(inspectIdaRegistration(path)).toMatchObject({
|
||||
available: false,
|
||||
registrationEnvironment: {},
|
||||
});
|
||||
expect(inspectIdaRegistration(undefined)).toMatchObject({
|
||||
configured: false,
|
||||
available: false,
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,64 @@
|
||||
import type {
|
||||
DoctorProviderCheck,
|
||||
DoctorProviderInspection,
|
||||
} from "../application/Doctor.js";
|
||||
import { readIdaConfiguration } from "./IdaConfiguration.js";
|
||||
import { privateRuntimeRootCapability } from "../process/PrivateRuntimeRoot.js";
|
||||
|
||||
/** Check the registration without launching IDA, and retain only the configuration file reference. */
|
||||
export const inspectIdaRegistration = (
|
||||
path: string | undefined,
|
||||
): DoctorProviderInspection => {
|
||||
const parsed = path === undefined ? undefined : readIdaConfiguration(path);
|
||||
const valid = parsed?.ok === true;
|
||||
const workspaceChecks =
|
||||
parsed?.ok === true && parsed.value.mode === "headless"
|
||||
? [inspectPrivateWorkspace()]
|
||||
: [];
|
||||
return {
|
||||
id: "ida",
|
||||
configured: path !== undefined,
|
||||
available: valid && workspaceChecks.every(({ ok }) => ok),
|
||||
providerVersion: null,
|
||||
registrationEnvironment:
|
||||
valid && path !== undefined ? { REA_IDA_MCP_CONFIG: path } : {},
|
||||
checks: [
|
||||
{
|
||||
name: "registration",
|
||||
ok: valid,
|
||||
code: valid
|
||||
? null
|
||||
: path === undefined
|
||||
? "not_configured"
|
||||
: "invalid_configuration",
|
||||
detail: valid
|
||||
? "IDA MCP registration is valid. Upstream availability, compatibility, and target binding are checked when analysis starts; no IDA process was launched."
|
||||
: parsed !== undefined && !parsed.ok
|
||||
? parsed.error.message
|
||||
: "IDA MCP registration is not configured.",
|
||||
remediation: valid
|
||||
? null
|
||||
: "Set REA_IDA_MCP_CONFIG to your upstream IDA MCP registration JSON; see docs/ida-provider.md.",
|
||||
classification:
|
||||
path === undefined ? "missing_analysis_engine" : "config_drift",
|
||||
},
|
||||
...workspaceChecks,
|
||||
],
|
||||
};
|
||||
};
|
||||
|
||||
const inspectPrivateWorkspace = (): DoctorProviderCheck => {
|
||||
const privacy = privateRuntimeRootCapability();
|
||||
return {
|
||||
name: "private_workspace",
|
||||
ok: privacy.available,
|
||||
code: privacy.available ? null : "unsupported_host",
|
||||
detail: privacy.available
|
||||
? `Private workspace authority is available (${privacy.proof}).`
|
||||
: privacy.reason,
|
||||
remediation: privacy.available
|
||||
? null
|
||||
: "Use the matching Windows x64 REA package and a writable local NTFS temporary directory.",
|
||||
classification: "unsupported_host",
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,93 @@
|
||||
import { z } from "zod";
|
||||
import { TOOL_CONTRACTS } from "../contracts/toolContracts.js";
|
||||
import { AnalysisInputError } from "../domain/analysisErrorCore.js";
|
||||
import { jsonObjectSchema, type JsonValue } from "../domain/jsonValue.js";
|
||||
import { err, ok } from "../domain/result.js";
|
||||
import { idaAddressSchema } from "./IdaProtocolValues.js";
|
||||
import { projectInputIssues } from "../domain/inputIssueProjection.js";
|
||||
|
||||
/** Apply the named public contract and IDA's explicit-address boundary before startup. */
|
||||
export const parseIdaInput = (
|
||||
operation: string,
|
||||
parameters: Readonly<Record<string, JsonValue>>,
|
||||
) => {
|
||||
const contract = TOOL_CONTRACTS.find(({ name }) => name === operation);
|
||||
const schema = contract?.inputSchema;
|
||||
if (schema instanceof z.ZodObject) {
|
||||
const shape: Readonly<Record<string, z.ZodType>> = schema.shape;
|
||||
parameters = Object.fromEntries(
|
||||
Object.entries(parameters).filter(
|
||||
([key, value]) =>
|
||||
value !== null || shape[key]?.safeParse(undefined).success !== true,
|
||||
),
|
||||
);
|
||||
}
|
||||
const parsed = (
|
||||
schema instanceof z.ZodObject ? schema.strict() : schema
|
||||
)?.safeParse(parameters);
|
||||
if (parsed?.success !== true)
|
||||
return err(
|
||||
new AnalysisInputError(
|
||||
operation,
|
||||
{ cause: parsed?.error },
|
||||
parsed?.error === undefined
|
||||
? []
|
||||
: projectInputIssues(parsed.error.issues, parameters),
|
||||
),
|
||||
);
|
||||
if (operation === "xrefs" && typeof parameters.address !== "string")
|
||||
return err(
|
||||
new AnalysisInputError(operation, undefined, [
|
||||
{
|
||||
path: ["address"],
|
||||
reason: "missing_argument",
|
||||
message: "IDA requires an explicit hexadecimal address.",
|
||||
},
|
||||
]),
|
||||
);
|
||||
if (
|
||||
parameters.address !== undefined &&
|
||||
!idaAddressSchema.safeParse(parameters.address).success
|
||||
)
|
||||
return err(
|
||||
new AnalysisInputError(operation, undefined, [
|
||||
{
|
||||
path: ["address"],
|
||||
reason: "invalid_format",
|
||||
message: "IDA requires a hexadecimal address.",
|
||||
},
|
||||
]),
|
||||
);
|
||||
if (
|
||||
typeof parameters.procedure === "string" &&
|
||||
parameters.procedure.length === 0
|
||||
)
|
||||
return err(
|
||||
new AnalysisInputError(operation, undefined, [
|
||||
{
|
||||
path: ["procedure"],
|
||||
reason: "invalid_value",
|
||||
message: "Supply a function name or address.",
|
||||
},
|
||||
]),
|
||||
);
|
||||
if (parameters.mode === "regex" && typeof parameters.pattern === "string") {
|
||||
try {
|
||||
new RegExp(
|
||||
parameters.pattern,
|
||||
parameters.case_sensitive === true ? "u" : "iu",
|
||||
);
|
||||
} catch (cause: unknown) {
|
||||
return err(
|
||||
new AnalysisInputError(operation, { cause }, [
|
||||
{
|
||||
path: ["pattern"],
|
||||
reason: "invalid_format",
|
||||
message: "Supply a valid regular expression.",
|
||||
},
|
||||
]),
|
||||
);
|
||||
}
|
||||
}
|
||||
return ok(jsonObjectSchema.parse(parsed.data));
|
||||
};
|
||||
@@ -0,0 +1,81 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
decodeIdaToolResult,
|
||||
redactIdaTransportFailure,
|
||||
} from "./IdaMcpConnection.js";
|
||||
|
||||
describe("upstream IDA tool result decoding", () => {
|
||||
it("redacts configured transport authentication without redacting caller evidence", () => {
|
||||
const error = redactIdaTransportFailure(
|
||||
new Error(
|
||||
"Invalid Authorization value Bearer fixture-secret at /caller/input",
|
||||
),
|
||||
{
|
||||
url: "http://127.0.0.1:12345/mcp",
|
||||
headers: { Authorization: "Bearer fixture-secret" },
|
||||
mode: "attached",
|
||||
timeoutMs: 1000,
|
||||
},
|
||||
);
|
||||
expect(error.message).not.toContain("fixture-secret");
|
||||
expect(error.message).toContain("/caller/input");
|
||||
expect(
|
||||
decodeIdaToolResult({
|
||||
content: [
|
||||
{ type: "text", text: "Caller-selected fixture-secret evidence" },
|
||||
],
|
||||
}),
|
||||
).toBe("Caller-selected fixture-secret evidence");
|
||||
});
|
||||
it("decodes scalar wrappers and legacy text without dropping structured dictionaries", () => {
|
||||
expect(
|
||||
decodeIdaToolResult({
|
||||
content: [],
|
||||
structuredContent: { result: "code" },
|
||||
}),
|
||||
).toBe("code");
|
||||
expect(
|
||||
decodeIdaToolResult({
|
||||
content: [],
|
||||
structuredContent: { path: "input", result: "field" },
|
||||
}),
|
||||
).toEqual({ path: "input", result: "field" });
|
||||
expect(
|
||||
decodeIdaToolResult({
|
||||
content: [{ type: "text", text: '[{"address":"0x1000"}]' }],
|
||||
}),
|
||||
).toEqual([{ address: "0x1000" }]);
|
||||
expect(
|
||||
decodeIdaToolResult({
|
||||
content: [{ type: "text", text: "int main(void);" }],
|
||||
}),
|
||||
).toBe("int main(void);");
|
||||
});
|
||||
it("preserves upstream failures and rejects truncated previews that still satisfy item schemas", () => {
|
||||
expect(() => decodeIdaToolResult({ content: [] })).toThrow(
|
||||
"empty observation",
|
||||
);
|
||||
expect(() =>
|
||||
decodeIdaToolResult({
|
||||
isError: true,
|
||||
content: [{ type: "text", text: "Decompiler license is unavailable" }],
|
||||
}),
|
||||
).toThrow("Decompiler license is unavailable");
|
||||
expect(() =>
|
||||
decodeIdaToolResult({
|
||||
isError: false,
|
||||
structuredContent: {
|
||||
result: [{ addr: "0x1000", name: "main", size: "0x20" }],
|
||||
},
|
||||
content: [{ type: "text", text: "preview" }],
|
||||
_meta: {
|
||||
ida_mcp: {
|
||||
output_truncated: true,
|
||||
total_chars: 70000,
|
||||
download_hint: "Download the full upstream output",
|
||||
},
|
||||
},
|
||||
}),
|
||||
).toThrow("Download the full upstream output");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,154 @@
|
||||
import {
|
||||
Client,
|
||||
StreamableHTTPClientTransport,
|
||||
} from "@modelcontextprotocol/client";
|
||||
import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
|
||||
import { z } from "zod";
|
||||
import type { IdaConfiguration } from "./IdaConfiguration.js";
|
||||
import { jsonValueSchema, type JsonValue } from "../domain/jsonValue.js";
|
||||
import { AnalysisProtocolError } from "../domain/analysisErrorCore.js";
|
||||
|
||||
/** Injectable MCP boundary used by the provider and transport conformance tests. */
|
||||
export interface IdaMcpConnection {
|
||||
connect(): Promise<readonly string[]>;
|
||||
call(
|
||||
name: string,
|
||||
args: Readonly<Record<string, JsonValue>>,
|
||||
): Promise<JsonValue>;
|
||||
serverInfo(): JsonValue;
|
||||
close(): Promise<void>;
|
||||
}
|
||||
|
||||
const toolResultSchema = z.object({
|
||||
isError: z.boolean().optional(),
|
||||
structuredContent: z.record(z.string(), z.unknown()).optional(),
|
||||
content: z.array(
|
||||
z.object({ type: z.string(), text: z.string().optional() }).passthrough(),
|
||||
),
|
||||
_meta: z
|
||||
.object({
|
||||
ida_mcp: z
|
||||
.object({
|
||||
output_truncated: z.boolean().optional(),
|
||||
download_hint: z.string().optional(),
|
||||
})
|
||||
.passthrough()
|
||||
.optional(),
|
||||
})
|
||||
.passthrough()
|
||||
.optional(),
|
||||
});
|
||||
|
||||
/** Decode Python FastMCP's scalar wrapper without dropping structured dictionaries. */
|
||||
export const decodeIdaToolResult = (input: unknown): JsonValue => {
|
||||
const result = toolResultSchema.parse(input);
|
||||
const blocks = result.content.flatMap((block) =>
|
||||
block.type === "text" && block.text !== undefined ? [block.text] : [],
|
||||
);
|
||||
if (result.isError === true)
|
||||
throw new AnalysisProtocolError(
|
||||
`IDA MCP tool failed: ${blocks.join("\n")}`,
|
||||
);
|
||||
if (result._meta?.ida_mcp?.output_truncated === true)
|
||||
throw new AnalysisProtocolError(
|
||||
`IDA MCP truncated its output; the preview cannot establish a complete observation. ${result._meta.ida_mcp.download_hint ?? "Use the upstream output download workflow to retrieve the full result."}`,
|
||||
);
|
||||
if (result.structuredContent !== undefined) {
|
||||
const value = result.structuredContent;
|
||||
return jsonValueSchema.parse(
|
||||
Object.keys(value).length === 1 && "result" in value
|
||||
? value.result
|
||||
: value,
|
||||
);
|
||||
}
|
||||
if (blocks.length !== 1)
|
||||
throw new AnalysisProtocolError(
|
||||
"IDA MCP omitted a single structured or text result; unsupported content cannot establish an empty observation.",
|
||||
);
|
||||
const values = blocks.map((text): JsonValue => {
|
||||
try {
|
||||
return jsonValueSchema.parse(JSON.parse(text));
|
||||
} catch {
|
||||
return text;
|
||||
}
|
||||
});
|
||||
return values[0] ?? null;
|
||||
};
|
||||
|
||||
/** Redact configured transport authentication values from SDK diagnostics only. */
|
||||
export const redactIdaTransportFailure = (
|
||||
cause: unknown,
|
||||
config: IdaConfiguration,
|
||||
): Error => {
|
||||
let message =
|
||||
cause instanceof Error
|
||||
? cause.message
|
||||
: "Unknown IDA MCP transport failure";
|
||||
if ("headers" in config) {
|
||||
for (const [name, value] of Object.entries(config.headers)) {
|
||||
if (!/^(?:authorization|proxy-authorization|cookie)$/iu.test(name))
|
||||
continue;
|
||||
const credential = value.match(/^(?:Bearer|Basic)\s+([\s\S]+)$/iu)?.[1];
|
||||
for (const secret of [value, credential])
|
||||
if (secret !== undefined && secret.length > 0)
|
||||
message = message
|
||||
.split(secret)
|
||||
.join("[redacted transport credential]");
|
||||
}
|
||||
}
|
||||
return new Error(message, { cause });
|
||||
};
|
||||
|
||||
/** Create a standard SDK connection to an existing upstream registration. */
|
||||
export const createIdaMcpConnection = (
|
||||
config: IdaConfiguration,
|
||||
): IdaMcpConnection => {
|
||||
const client = new Client({ name: "rea-ida-provider", version: "1" });
|
||||
const transportFailure = (cause: unknown): never => {
|
||||
throw redactIdaTransportFailure(cause, config);
|
||||
};
|
||||
const transport =
|
||||
"command" in config
|
||||
? new StdioClientTransport({
|
||||
command: config.command,
|
||||
args: config.args,
|
||||
env: {
|
||||
...Object.fromEntries(
|
||||
Object.entries(process.env).filter(
|
||||
(entry): entry is [string, string] => entry[1] !== undefined,
|
||||
),
|
||||
),
|
||||
IDA_MCP_MAX_WORKERS: "1",
|
||||
...config.env,
|
||||
},
|
||||
stderr: "pipe",
|
||||
})
|
||||
: new StreamableHTTPClientTransport(new URL(config.url), {
|
||||
requestInit: { headers: config.headers, redirect: "error" },
|
||||
});
|
||||
// Drain proxy diagnostics without persisting ambient environment or transport credentials.
|
||||
if (transport instanceof StdioClientTransport)
|
||||
transport.stderr?.on("data", () => undefined);
|
||||
return {
|
||||
async connect() {
|
||||
await client
|
||||
.connect(transport, { timeout: config.timeoutMs })
|
||||
.catch(transportFailure);
|
||||
const catalog = await client
|
||||
.listTools(undefined, {
|
||||
timeout: config.timeoutMs,
|
||||
})
|
||||
.catch(transportFailure);
|
||||
return catalog.tools.map(({ name }) => name);
|
||||
},
|
||||
async call(name, args) {
|
||||
return decodeIdaToolResult(
|
||||
await client
|
||||
.callTool({ name, arguments: args }, { timeout: config.timeoutMs })
|
||||
.catch(transportFailure),
|
||||
);
|
||||
},
|
||||
serverInfo: () => jsonValueSchema.parse(client.getServerVersion() ?? null),
|
||||
close: () => client.close().catch(transportFailure),
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,437 @@
|
||||
import { z } from "zod";
|
||||
import { analysisSearchInput } from "../contracts/analysisSearchContract.js";
|
||||
import {
|
||||
AnalysisInputError,
|
||||
AnalysisProtocolError,
|
||||
} from "../domain/analysisErrorCore.js";
|
||||
import {
|
||||
functionDossierSchema,
|
||||
functionInstructionWindowSchema,
|
||||
} from "../domain/hopperValues.js";
|
||||
import type { JsonValue } from "../domain/jsonValue.js";
|
||||
import type { IdaMcpConnection } from "./IdaMcpConnection.js";
|
||||
import type { IdaOperation } from "./IdaProviderCapabilities.js";
|
||||
import { IDA_LIMITATIONS } from "./IdaProviderCapabilities.js";
|
||||
import type { EvidenceLocation } from "../domain/evidence.js";
|
||||
import {
|
||||
disassemblySchema,
|
||||
idaAddressSchema,
|
||||
idaFunctionSchema,
|
||||
legacyCalleesSchema,
|
||||
legacyPageSchema,
|
||||
legacyStringSchema,
|
||||
legacyXrefsSchema,
|
||||
modernCalleesSchema,
|
||||
modernDecompileSchema,
|
||||
modernDisassemblySchema,
|
||||
modernLookupSchema,
|
||||
modernPageSchema,
|
||||
modernStringsSchema,
|
||||
modernXrefsSchema,
|
||||
nextOffset,
|
||||
type IdaDisassembly,
|
||||
type IdaFunction,
|
||||
} from "./IdaProtocolValues.js";
|
||||
|
||||
/** One operation's raw source observations, retained alongside normalized results. */
|
||||
export interface IdaRawObservation {
|
||||
readonly tool: string;
|
||||
readonly arguments: Readonly<Record<string, JsonValue>>;
|
||||
readonly result: JsonValue;
|
||||
}
|
||||
|
||||
const unknownBody = {
|
||||
available: false,
|
||||
reason: "IDA MCP does not report complete function body ranges.",
|
||||
} as const;
|
||||
const identity = (fn: IdaFunction) => ({
|
||||
address: fn.address,
|
||||
name: fn.name,
|
||||
classification: null,
|
||||
body: unknownBody,
|
||||
});
|
||||
const lines = (asm: IdaDisassembly) =>
|
||||
asm.lines.map(({ address, instruction }) => `${address}: ${instruction}`);
|
||||
|
||||
/** Translate explicit analyst questions into a closed set of read-only upstream requests. */
|
||||
export class IdaOperationRunner {
|
||||
readonly raw: IdaRawObservation[] = [];
|
||||
readonly locations: EvidenceLocation[] = [];
|
||||
readonly limitations: string[] = [];
|
||||
constructor(
|
||||
private readonly connection: IdaMcpConnection,
|
||||
private readonly protocol: "legacy" | "modern",
|
||||
private readonly database?: string,
|
||||
) {}
|
||||
|
||||
async call(
|
||||
tool: string,
|
||||
args: Readonly<Record<string, JsonValue>>,
|
||||
): Promise<JsonValue> {
|
||||
const scoped =
|
||||
this.database === undefined ? args : { ...args, database: this.database };
|
||||
const result = await this.connection.call(tool, scoped);
|
||||
this.raw.push({ tool, arguments: scoped, result });
|
||||
return result;
|
||||
}
|
||||
|
||||
async functions(): Promise<IdaFunction[]> {
|
||||
const result: IdaFunction[] = [];
|
||||
let offset: number | null = 0;
|
||||
while (offset !== null) {
|
||||
const current: number = offset;
|
||||
const value: z.infer<typeof legacyPageSchema> | undefined =
|
||||
this.protocol === "legacy"
|
||||
? legacyPageSchema.parse(
|
||||
await this.call("list_functions", { offset, count: 100 }),
|
||||
)
|
||||
: modernPageSchema.parse(
|
||||
await this.call("list_funcs", {
|
||||
queries: { offset, count: 100 },
|
||||
}),
|
||||
)[0];
|
||||
if (value === undefined)
|
||||
throw new AnalysisProtocolError("IDA MCP omitted its function page.");
|
||||
result.push(...value.data.map((fn) => idaFunctionSchema.parse(fn)));
|
||||
offset = value.next_offset;
|
||||
if (offset !== null && offset <= current)
|
||||
throw new AnalysisProtocolError(
|
||||
"IDA MCP function pagination did not advance.",
|
||||
);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
async resolve(query: string): Promise<IdaFunction> {
|
||||
if (this.protocol === "modern") {
|
||||
const item = modernLookupSchema.parse(
|
||||
await this.call("lookup_funcs", { queries: [query] }),
|
||||
)[0];
|
||||
if (item?.fn == null || item.error !== null)
|
||||
throw new AnalysisProtocolError(
|
||||
`IDA function lookup failed for ${query}: ${item?.error ?? "missing function"}`,
|
||||
);
|
||||
return item.fn;
|
||||
}
|
||||
const address = idaAddressSchema.safeParse(
|
||||
/^0x/iu.test(query) ? query : undefined,
|
||||
);
|
||||
const found = idaFunctionSchema.parse(
|
||||
await this.call(
|
||||
address.success ? "get_function_by_address" : "get_function_by_name",
|
||||
address.success ? { address: address.data } : { name: query },
|
||||
),
|
||||
);
|
||||
// Legacy producers may echo an interior query or call site in address.
|
||||
// Resolve the reported symbol again to obtain its actual entry, never infer from size.
|
||||
const entry = idaFunctionSchema.parse(
|
||||
await this.call("get_function_by_name", { name: found.name }),
|
||||
);
|
||||
if (entry.name !== found.name)
|
||||
throw new AnalysisProtocolError(
|
||||
"IDA function identity changed during lookup.",
|
||||
);
|
||||
return entry;
|
||||
}
|
||||
|
||||
async strings(): Promise<{ address: string; value: string }[]> {
|
||||
const result: { address: string; value: string }[] = [];
|
||||
let offset: number | null = 0;
|
||||
while (offset !== null) {
|
||||
const current: number = offset;
|
||||
if (this.protocol === "legacy") {
|
||||
const page = legacyPageSchema.parse(
|
||||
await this.call("list_strings", { offset, count: 100 }),
|
||||
);
|
||||
result.push(
|
||||
...page.data.map((item) => {
|
||||
const s = legacyStringSchema.parse(item);
|
||||
return { address: s.address, value: s.string };
|
||||
}),
|
||||
);
|
||||
offset = page.next_offset;
|
||||
} else {
|
||||
const page = modernStringsSchema.parse(
|
||||
await this.call("find_regex", {
|
||||
pattern: "(?s).*",
|
||||
offset,
|
||||
limit: 100,
|
||||
}),
|
||||
);
|
||||
result.push(
|
||||
...page.matches.map((s) => ({ address: s.addr, value: s.string })),
|
||||
);
|
||||
offset = nextOffset(page.cursor, current);
|
||||
}
|
||||
if (offset !== null && offset <= current)
|
||||
throw new AnalysisProtocolError(
|
||||
"IDA MCP string pagination did not advance.",
|
||||
);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
async assembly(fn: IdaFunction): Promise<IdaDisassembly> {
|
||||
if (this.protocol === "legacy") {
|
||||
const result = disassemblySchema.parse(
|
||||
await this.call("disassemble_function", { start_address: fn.address }),
|
||||
);
|
||||
this.verifyAssembly(fn, result);
|
||||
return result;
|
||||
}
|
||||
let result: IdaDisassembly | undefined;
|
||||
let offset: number | null = 0;
|
||||
while (offset !== null) {
|
||||
const page = modernDisassemblySchema.parse(
|
||||
await this.call("disasm", {
|
||||
addr: fn.address,
|
||||
offset,
|
||||
max_instructions: 100,
|
||||
}),
|
||||
);
|
||||
if (page.asm === null || page.error !== undefined)
|
||||
throw new AnalysisProtocolError(
|
||||
`IDA disassembly failed: ${page.error ?? "missing assembly"}`,
|
||||
);
|
||||
this.verifyAssembly(fn, page.asm);
|
||||
if (result === undefined) result = page.asm;
|
||||
else result.lines.push(...page.asm.lines);
|
||||
offset = nextOffset(page.cursor, offset);
|
||||
}
|
||||
if (result === undefined)
|
||||
throw new AnalysisProtocolError("IDA omitted assembly.");
|
||||
return result;
|
||||
}
|
||||
|
||||
private verifyAssembly(fn: IdaFunction, asm: IdaDisassembly): void {
|
||||
if (asm.start_ea !== fn.address || asm.name !== fn.name)
|
||||
throw new AnalysisProtocolError(
|
||||
"IDA disassembly identifies a different function than the resolved entry.",
|
||||
);
|
||||
}
|
||||
|
||||
async pseudocode(fn: IdaFunction): Promise<string | null> {
|
||||
if (this.protocol === "legacy")
|
||||
return z
|
||||
.string()
|
||||
.nullable()
|
||||
.parse(await this.call("decompile_function", { address: fn.address }));
|
||||
const result: string[] = [];
|
||||
let offset: number | null = 0;
|
||||
while (offset !== null) {
|
||||
const page = modernDecompileSchema.parse(
|
||||
await this.call("decompile", {
|
||||
addr: fn.address,
|
||||
offset,
|
||||
max_lines: 100,
|
||||
}),
|
||||
);
|
||||
if (page.code === null)
|
||||
throw new AnalysisProtocolError(
|
||||
`IDA decompilation failed: ${page.error ?? "missing code"}`,
|
||||
);
|
||||
result.push(page.code);
|
||||
offset = nextOffset(page.cursor, offset);
|
||||
}
|
||||
return result.join("\n");
|
||||
}
|
||||
|
||||
async xrefs(
|
||||
address: string,
|
||||
): Promise<
|
||||
{ address: string; type: string; function: IdaFunction | null }[]
|
||||
> {
|
||||
if (this.protocol === "legacy")
|
||||
return legacyXrefsSchema.parse(
|
||||
await this.call("get_xrefs_to", { address }),
|
||||
);
|
||||
const page = modernXrefsSchema.parse(
|
||||
await this.call("xrefs_to", { addrs: [address], limit: 1000 }),
|
||||
)[0];
|
||||
if (page?.xrefs == null)
|
||||
throw new AnalysisProtocolError(
|
||||
`IDA xrefs failed: ${page?.error ?? "missing references"}`,
|
||||
);
|
||||
if (page.more === true)
|
||||
this.limitations.push(
|
||||
"Upstream xrefs_to capped results at 1000 references; additional references remain unknown.",
|
||||
);
|
||||
return page.xrefs.map((xref) => ({
|
||||
address: xref.addr,
|
||||
type: xref.type,
|
||||
function: xref.fn,
|
||||
}));
|
||||
}
|
||||
|
||||
async relationships(
|
||||
fn: IdaFunction,
|
||||
direction: "callers" | "callees",
|
||||
): Promise<ReturnType<typeof identity>[]> {
|
||||
const queries: string[] = [];
|
||||
const result = new Map<string, ReturnType<typeof identity>>();
|
||||
if (this.protocol === "legacy") {
|
||||
const value = await this.call(
|
||||
direction === "callers" ? "get_callers" : "get_callees",
|
||||
{ function_address: fn.address },
|
||||
);
|
||||
const items =
|
||||
direction === "callers"
|
||||
? z.array(idaFunctionSchema).parse(value)
|
||||
: legacyCalleesSchema.parse(value);
|
||||
for (const item of items) {
|
||||
if ("type" in item && item.type === "external")
|
||||
result.set(item.address, identity({ ...item, size: "unknown" }));
|
||||
else queries.push(item.name);
|
||||
}
|
||||
} else if (direction === "callers") {
|
||||
this.limitations.push(
|
||||
"Modern upstream xrefs classify references as code or data, without proving call edges. Direct callers are unavailable in this profile.",
|
||||
);
|
||||
return [];
|
||||
} else {
|
||||
const page = modernCalleesSchema.parse(
|
||||
await this.call("callees", { addrs: [fn.address], limit: 500 }),
|
||||
)[0];
|
||||
if (page?.callees == null)
|
||||
throw new AnalysisProtocolError(
|
||||
`IDA callees failed: ${page?.error ?? "missing callees"}`,
|
||||
);
|
||||
if (page.more === true)
|
||||
this.limitations.push(
|
||||
"Upstream callees capped results at 500; additional callees remain unknown.",
|
||||
);
|
||||
for (const item of page.callees) {
|
||||
if ("type" in item && item.type === "external")
|
||||
result.set(
|
||||
item.addr,
|
||||
identity({ address: item.addr, name: item.name, size: "unknown" }),
|
||||
);
|
||||
else queries.push(item.addr);
|
||||
}
|
||||
}
|
||||
for (const query of new Set(queries)) {
|
||||
const resolved = identity(await this.resolve(query));
|
||||
result.set(resolved.address, resolved);
|
||||
}
|
||||
return [...result.values()];
|
||||
}
|
||||
|
||||
async run(
|
||||
operation: IdaOperation,
|
||||
parameters: Readonly<Record<string, JsonValue>>,
|
||||
): Promise<JsonValue> {
|
||||
if (operation === "list_procedures" || operation === "search_procedures") {
|
||||
const items = (await this.functions()).map((fn) => ({
|
||||
address: fn.address,
|
||||
value: fn.name,
|
||||
}));
|
||||
return operation === "list_procedures"
|
||||
? items
|
||||
: this.search(items, parameters);
|
||||
}
|
||||
if (operation === "list_strings" || operation === "search_strings") {
|
||||
const items = await this.strings();
|
||||
return operation === "search_strings"
|
||||
? this.search(items, parameters)
|
||||
: parameters.address === undefined
|
||||
? items
|
||||
: items.filter(
|
||||
({ address }) =>
|
||||
address === idaAddressSchema.parse(parameters.address),
|
||||
);
|
||||
}
|
||||
if (operation === "xrefs") {
|
||||
const inspectedAddress = idaAddressSchema.parse(parameters.address);
|
||||
this.locations.push({ kind: "address", address: inspectedAddress });
|
||||
return (await this.xrefs(inspectedAddress)).map(({ address }) => address);
|
||||
}
|
||||
const fn = await this.resolve(
|
||||
z.string().min(1).parse(parameters.procedure),
|
||||
);
|
||||
this.locations.push({ kind: "address", address: fn.address });
|
||||
switch (operation) {
|
||||
case "procedure_address":
|
||||
return fn.address;
|
||||
case "procedure_pseudo_code":
|
||||
return this.pseudocode(fn);
|
||||
case "procedure_assembly":
|
||||
return lines(await this.assembly(fn)).join("\n");
|
||||
case "procedure_callers":
|
||||
if (this.protocol === "modern")
|
||||
throw new AnalysisProtocolError(
|
||||
"This IDA MCP profile cannot distinguish direct callers from other code references.",
|
||||
);
|
||||
return (await this.relationships(fn, "callers")).map(
|
||||
({ address }) => address,
|
||||
);
|
||||
case "procedure_callees":
|
||||
return (await this.relationships(fn, "callees")).map(
|
||||
({ address }) => address,
|
||||
);
|
||||
case "read_function_instructions":
|
||||
return functionInstructionWindowSchema.parse({
|
||||
procedure: identity(fn),
|
||||
instructions: lines(await this.assembly(fn)),
|
||||
limitations: IDA_LIMITATIONS,
|
||||
});
|
||||
case "analyze_function":
|
||||
return this.dossier(fn);
|
||||
}
|
||||
}
|
||||
|
||||
private search(
|
||||
items: { address: string; value: string }[],
|
||||
input: Readonly<Record<string, JsonValue>>,
|
||||
): JsonValue {
|
||||
const query = z.object(analysisSearchInput).parse(input);
|
||||
let matches: (value: string) => boolean;
|
||||
if (query.mode === "regex") {
|
||||
try {
|
||||
const regex = new RegExp(
|
||||
query.pattern,
|
||||
query.case_sensitive ? "u" : "iu",
|
||||
);
|
||||
matches = (value) => regex.test(value);
|
||||
} catch (cause: unknown) {
|
||||
throw new AnalysisInputError("search", { cause });
|
||||
}
|
||||
} else {
|
||||
const needle = query.case_sensitive
|
||||
? query.pattern
|
||||
: query.pattern.toLowerCase();
|
||||
matches = (value) =>
|
||||
(query.case_sensitive ? value : value.toLowerCase()).includes(needle);
|
||||
}
|
||||
return items.filter(({ value }) => matches(value));
|
||||
}
|
||||
|
||||
private async dossier(fn: IdaFunction): Promise<JsonValue> {
|
||||
const asm = await this.assembly(fn);
|
||||
const pseudocode = await this.pseudocode(fn);
|
||||
const callers = await this.relationships(fn, "callers");
|
||||
const callees = await this.relationships(fn, "callees");
|
||||
const signature =
|
||||
asm.return_type == null || asm.arguments == null
|
||||
? null
|
||||
: `${asm.return_type} ${fn.name}(${asm.arguments.map((arg) => `${arg.type} ${arg.name}`).join(", ")})`;
|
||||
return functionDossierSchema.parse({
|
||||
procedure: { ...identity(fn), signature, locals: [] },
|
||||
pseudocode: pseudocode ?? "",
|
||||
assembly: lines(asm),
|
||||
comments: [],
|
||||
callers,
|
||||
callees,
|
||||
incoming_references: [],
|
||||
outgoing_references: [],
|
||||
referenced_strings: [],
|
||||
referenced_names: [],
|
||||
basic_blocks: [],
|
||||
limitations: [
|
||||
...IDA_LIMITATIONS,
|
||||
...this.limitations,
|
||||
"Stack frame records are not evidence of source locals. Rendered comments do not identify regular versus inline origin.",
|
||||
...(pseudocode === null ? ["Pseudocode is unavailable."] : []),
|
||||
],
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,214 @@
|
||||
import { z } from "zod";
|
||||
import { jsonValueSchema } from "../domain/jsonValue.js";
|
||||
import { AnalysisProtocolError } from "../domain/analysisErrorCore.js";
|
||||
|
||||
/** Normalize an IDA hexadecimal coordinate without passing through a JavaScript number. */
|
||||
export const idaAddressSchema = z
|
||||
.string()
|
||||
.regex(/^(?:0x)?[0-9a-f]+$/iu)
|
||||
.transform(
|
||||
(value) =>
|
||||
`0x${BigInt(value.startsWith("0x") || value.startsWith("0X") ? value : `0x${value}`).toString(16)}`,
|
||||
);
|
||||
|
||||
/** A function observation; size is an enclosing span, never a complete body. */
|
||||
export const idaFunctionSchema = z.union([
|
||||
z.object({
|
||||
address: idaAddressSchema,
|
||||
name: z.string().min(1),
|
||||
size: z.string(),
|
||||
}),
|
||||
z
|
||||
.object({
|
||||
addr: idaAddressSchema,
|
||||
name: z.string().min(1),
|
||||
size: z.string(),
|
||||
})
|
||||
.transform(({ addr, ...rest }) => ({ address: addr, ...rest })),
|
||||
]);
|
||||
export type IdaFunction = z.infer<typeof idaFunctionSchema>;
|
||||
|
||||
/** Legacy metadata describes the recorded input, whereas modern health names live paths. */
|
||||
export const legacyMetadataSchema = z
|
||||
.object({
|
||||
path: z.string().min(1),
|
||||
module: z.string(),
|
||||
sha256: z.string().regex(/^[0-9a-f]{64}$/iu),
|
||||
base: idaAddressSchema,
|
||||
})
|
||||
.passthrough();
|
||||
export const modernHealthSchema = z
|
||||
.object({
|
||||
status: z.literal("ok"),
|
||||
input_path: z.string().min(1),
|
||||
idb_path: z.string().nullable(),
|
||||
imagebase: idaAddressSchema,
|
||||
auto_analysis_ready: z.boolean().nullable(),
|
||||
module: z.string(),
|
||||
})
|
||||
.passthrough();
|
||||
|
||||
/** An upstream cursor must either finish or advance strictly to avoid replay loops. */
|
||||
export const cursorSchema = z.union([
|
||||
z.object({ next: z.number().int().nonnegative() }),
|
||||
z.object({ done: z.literal(true) }),
|
||||
]);
|
||||
export const nextOffset = (
|
||||
cursor: z.infer<typeof cursorSchema>,
|
||||
current: number,
|
||||
): number | null => {
|
||||
if ("done" in cursor) return null;
|
||||
if (cursor.next <= current)
|
||||
throw new AnalysisProtocolError(
|
||||
"IDA MCP pagination did not advance its offset.",
|
||||
);
|
||||
return cursor.next;
|
||||
};
|
||||
|
||||
export const legacyPageSchema = z.object({
|
||||
data: z.array(jsonValueSchema),
|
||||
next_offset: z.number().int().nonnegative().nullable(),
|
||||
});
|
||||
export const modernPageSchema = z
|
||||
.array(
|
||||
z.object({
|
||||
data: z.array(jsonValueSchema),
|
||||
next_offset: z.number().int().nonnegative().nullable(),
|
||||
}),
|
||||
)
|
||||
.length(1);
|
||||
|
||||
export const modernLookupSchema = z
|
||||
.array(
|
||||
z.object({
|
||||
query: z.string(),
|
||||
fn: idaFunctionSchema.nullable(),
|
||||
error: z.string().nullable(),
|
||||
}),
|
||||
)
|
||||
.length(1);
|
||||
|
||||
const instructionSchema = z.union([
|
||||
z.object({
|
||||
address: idaAddressSchema,
|
||||
instruction: z.string(),
|
||||
comments: z.array(z.string()).nullish(),
|
||||
}),
|
||||
z
|
||||
.object({
|
||||
addr: idaAddressSchema,
|
||||
instruction: z.string(),
|
||||
comments: z.array(z.string()).nullish(),
|
||||
})
|
||||
.transform(({ addr, ...rest }) => ({ address: addr, ...rest })),
|
||||
]);
|
||||
export const disassemblySchema = z.object({
|
||||
name: z.string(),
|
||||
start_ea: idaAddressSchema,
|
||||
lines: z.array(instructionSchema),
|
||||
return_type: z.string().nullish(),
|
||||
arguments: z
|
||||
.array(z.object({ name: z.string(), type: z.string() }))
|
||||
.nullish(),
|
||||
});
|
||||
export type IdaDisassembly = z.infer<typeof disassemblySchema>;
|
||||
export const modernDisassemblySchema = z.object({
|
||||
asm: disassemblySchema.nullable(),
|
||||
error: z.string().optional(),
|
||||
cursor: cursorSchema,
|
||||
});
|
||||
export const modernDecompileSchema = z.object({
|
||||
code: z.string().nullable(),
|
||||
error: z.string().optional(),
|
||||
cursor: cursorSchema,
|
||||
line_count: z.number().int().nonnegative().optional(),
|
||||
total_lines: z.number().int().nonnegative().optional(),
|
||||
});
|
||||
|
||||
export const legacyStringSchema = z.object({
|
||||
address: idaAddressSchema,
|
||||
string: z.string(),
|
||||
length: z.number().int().nonnegative(),
|
||||
});
|
||||
export const modernStringsSchema = z.object({
|
||||
matches: z.array(z.object({ addr: idaAddressSchema, string: z.string() })),
|
||||
cursor: cursorSchema,
|
||||
});
|
||||
export const legacyXrefsSchema = z.array(
|
||||
z.object({
|
||||
address: idaAddressSchema,
|
||||
type: z.string(),
|
||||
function: idaFunctionSchema.nullable(),
|
||||
}),
|
||||
);
|
||||
export const modernXrefsSchema = z
|
||||
.array(
|
||||
z.object({
|
||||
xrefs: z
|
||||
.array(
|
||||
z.object({
|
||||
addr: idaAddressSchema,
|
||||
type: z.string(),
|
||||
fn: idaFunctionSchema.nullable(),
|
||||
}),
|
||||
)
|
||||
.nullable(),
|
||||
error: z.string().optional(),
|
||||
more: z.boolean().optional(),
|
||||
}),
|
||||
)
|
||||
.length(1);
|
||||
export const modernCalleesSchema = z
|
||||
.array(
|
||||
z.object({
|
||||
callees: z
|
||||
.array(
|
||||
z.object({
|
||||
addr: idaAddressSchema,
|
||||
name: z.string(),
|
||||
type: z.enum(["internal", "external"]),
|
||||
}),
|
||||
)
|
||||
.nullable(),
|
||||
error: z.string().optional(),
|
||||
more: z.boolean().optional(),
|
||||
}),
|
||||
)
|
||||
.length(1);
|
||||
export const legacyCalleesSchema = z.array(
|
||||
z.object({ address: idaAddressSchema, name: z.string() }).passthrough(),
|
||||
);
|
||||
|
||||
/** Parsed database lifecycle observations retain explicit ownership information. */
|
||||
export const openDatabaseSchema = z
|
||||
.object({
|
||||
success: z.literal(true),
|
||||
session: z
|
||||
.object({ session_id: z.string().min(1), input_path: z.string().min(1) })
|
||||
.passthrough(),
|
||||
})
|
||||
.passthrough();
|
||||
export const databaseListSchema = z
|
||||
.object({
|
||||
sessions: z.array(
|
||||
z
|
||||
.object({
|
||||
session_id: z.string(),
|
||||
input_path: z.string(),
|
||||
owned: z.boolean(),
|
||||
backend: z.enum(["worker", "gui"]),
|
||||
is_active: z.boolean(),
|
||||
})
|
||||
.passthrough(),
|
||||
),
|
||||
})
|
||||
.passthrough();
|
||||
export const closeDatabaseSchema = z
|
||||
.object({
|
||||
success: z.literal(true),
|
||||
session_id: z.string(),
|
||||
saved: z.boolean().nullable().optional(),
|
||||
owned: z.boolean(),
|
||||
backend: z.enum(["worker", "gui"]),
|
||||
})
|
||||
.passthrough();
|
||||
@@ -0,0 +1,171 @@
|
||||
import { createAnalysisProfile } from "../domain/analysisProfile.js";
|
||||
import type { AnalysisProfileCommitment } from "../domain/analysisProfile.js";
|
||||
import type { BinaryTarget } from "../domain/binaryTarget.js";
|
||||
import { err, ok } from "../domain/result.js";
|
||||
import { AnalysisCancelledError } from "../domain/analysisErrorCore.js";
|
||||
import type {
|
||||
AnalysisClient,
|
||||
AnalysisProfileResolutionOptions,
|
||||
AnalysisProviderCandidate,
|
||||
ProviderAvailability,
|
||||
ProviderTargetSupport,
|
||||
} from "../application/AnalysisProvider.js";
|
||||
import type { AppConfig } from "../config.js";
|
||||
import { ConfigurationError } from "../domain/configurationErrors.js";
|
||||
import { createHash } from "node:crypto";
|
||||
import canonicalize from "canonicalize";
|
||||
import type { Result } from "../domain/result.js";
|
||||
import {
|
||||
readIdaConfiguration,
|
||||
type IdaConfiguration,
|
||||
} from "./IdaConfiguration.js";
|
||||
import {
|
||||
createIdaMcpConnection,
|
||||
type IdaMcpConnection,
|
||||
} from "./IdaMcpConnection.js";
|
||||
import {
|
||||
idaCapabilities,
|
||||
IDA_PROVIDER_IDENTITY,
|
||||
} from "./IdaProviderCapabilities.js";
|
||||
import { IdaSessionClient } from "./IdaSessionClient.js";
|
||||
import { privateRuntimeRootCapability } from "../process/PrivateRuntimeRoot.js";
|
||||
|
||||
export { IDA_PROVIDER_IDENTITY } from "./IdaProviderCapabilities.js";
|
||||
|
||||
/** Bring-your-own IDA MCP candidate; discovery performs no provider startup. */
|
||||
export class IdaProvider implements AnalysisProviderCandidate {
|
||||
#registration: Result<IdaConfiguration, ConfigurationError> | undefined;
|
||||
constructor(
|
||||
private readonly config: AppConfig,
|
||||
private readonly connectionFactory: (
|
||||
config: IdaConfiguration,
|
||||
) => IdaMcpConnection = createIdaMcpConnection,
|
||||
) {}
|
||||
|
||||
identity() {
|
||||
return IDA_PROVIDER_IDENTITY;
|
||||
}
|
||||
capabilities() {
|
||||
const config = this.#configuration();
|
||||
return idaCapabilities(
|
||||
config.ok ? config.value.mode : "attached",
|
||||
config.ok && "command" in config.value,
|
||||
);
|
||||
}
|
||||
inspectAvailability(): ProviderAvailability {
|
||||
const config = this.#configuration();
|
||||
if (config.ok && config.value.mode === "headless") {
|
||||
const privacy = privateRuntimeRootCapability();
|
||||
if (!privacy.available)
|
||||
return {
|
||||
status: "unavailable",
|
||||
code: "unsupported_host",
|
||||
reason: privacy.reason,
|
||||
diagnostics: {
|
||||
configured: true,
|
||||
mode: "headless",
|
||||
live_connection_probed: false,
|
||||
private_workspace_available: false,
|
||||
},
|
||||
};
|
||||
}
|
||||
return config.ok
|
||||
? {
|
||||
status: "available",
|
||||
code: null,
|
||||
reason: null,
|
||||
diagnostics: {
|
||||
configured: true,
|
||||
mode: config.value.mode,
|
||||
transport: "command" in config.value ? "stdio" : "http",
|
||||
live_connection_probed: false,
|
||||
},
|
||||
}
|
||||
: {
|
||||
status: "unavailable",
|
||||
code:
|
||||
this.config.idaMcpConfigPath === undefined
|
||||
? "not_configured"
|
||||
: "open_options_invalid",
|
||||
reason: config.error.message,
|
||||
diagnostics: { configured: false, live_connection_probed: false },
|
||||
};
|
||||
}
|
||||
inspectTargetSupport(target: BinaryTarget): ProviderTargetSupport {
|
||||
const diagnostics = {
|
||||
target_kind: target.kind,
|
||||
target_format: target.format,
|
||||
};
|
||||
return target.kind === "executable"
|
||||
? { status: "supported", code: null, reason: null, diagnostics }
|
||||
: {
|
||||
status: "unsupported",
|
||||
code: "target_kind_unsupported",
|
||||
reason:
|
||||
"IDA MCP integration admits executable inputs, not analysis databases. Select the original input binary.",
|
||||
diagnostics,
|
||||
};
|
||||
}
|
||||
async resolveAnalysisProfile(
|
||||
_target: BinaryTarget,
|
||||
options?: AnalysisProfileResolutionOptions,
|
||||
) {
|
||||
if (options?.signal?.aborted === true)
|
||||
return err(new AnalysisCancelledError("ida:profile"));
|
||||
const config = this.#configuration();
|
||||
if (!config.ok) return config;
|
||||
return ok({
|
||||
profile: createAnalysisProfile(IDA_PROVIDER_IDENTITY, {
|
||||
version_scope: "rea-ida-adapter",
|
||||
engine_version: null,
|
||||
upstream_distribution_version: null,
|
||||
mode: config.value.mode,
|
||||
compatibility_profile:
|
||||
config.value.mode === "attached"
|
||||
? "legacy-1.4"
|
||||
: "database-supervisor",
|
||||
database_revision: null,
|
||||
cache_policy: "live",
|
||||
registration_digest: createHash("sha256")
|
||||
.update(canonicalize(config.value) ?? "")
|
||||
.digest("hex"),
|
||||
}),
|
||||
compatibility: {
|
||||
mode: config.value.mode,
|
||||
engine_version: null,
|
||||
upstream_distribution_version: null,
|
||||
},
|
||||
});
|
||||
}
|
||||
createClient(
|
||||
target: BinaryTarget,
|
||||
profile?: AnalysisProfileCommitment,
|
||||
): AnalysisClient {
|
||||
const config = this.#configuration();
|
||||
if (!config.ok)
|
||||
return {
|
||||
execute: async () => err(config.error),
|
||||
close: async () => undefined,
|
||||
};
|
||||
return new IdaSessionClient(
|
||||
config.value,
|
||||
target,
|
||||
this.connectionFactory(config.value),
|
||||
profile,
|
||||
);
|
||||
}
|
||||
#configuration() {
|
||||
this.#registration ??=
|
||||
this.config.idaMcpConfigPath === undefined
|
||||
? readIdaConfigurationNotConfigured()
|
||||
: readIdaConfiguration(this.config.idaMcpConfigPath);
|
||||
return this.#registration;
|
||||
}
|
||||
}
|
||||
|
||||
const readIdaConfigurationNotConfigured = () =>
|
||||
err(
|
||||
new ConfigurationError(
|
||||
"Set REA_IDA_MCP_CONFIG to an upstream IDA MCP JSON registration; see docs/ida-provider.md.",
|
||||
),
|
||||
);
|
||||
@@ -0,0 +1,76 @@
|
||||
import type { CapabilityDescriptor } from "../application/AnalysisProvider.js";
|
||||
import { GENERATED_MCP_TOOL_CATALOG } from "../generatedMcpToolCatalog.js";
|
||||
|
||||
/** This version identifies REA's adapter, not IDA or the upstream Python package. */
|
||||
export const IDA_PROVIDER_IDENTITY = Object.freeze({
|
||||
id: "ida",
|
||||
name: "IDA Pro MCP adapter",
|
||||
version: "1",
|
||||
});
|
||||
|
||||
/** Read-only analyst outcomes supported by both upstream compatibility profiles. */
|
||||
export const IDA_OPERATIONS = [
|
||||
"list_procedures",
|
||||
"search_procedures",
|
||||
"list_strings",
|
||||
"search_strings",
|
||||
"procedure_address",
|
||||
"procedure_pseudo_code",
|
||||
"procedure_assembly",
|
||||
"procedure_callers",
|
||||
"procedure_callees",
|
||||
"read_function_instructions",
|
||||
"xrefs",
|
||||
"analyze_function",
|
||||
] as const;
|
||||
export type IdaOperation = (typeof IDA_OPERATIONS)[number];
|
||||
|
||||
/** Shared limitations of observations from an externally mutable analysis database. */
|
||||
export const IDA_LIMITATIONS = Object.freeze([
|
||||
"Provider version identifies the REA IDA adapter. IDA engine and upstream distribution versions are unknown unless separately observed; MCP serverInfo is reported independently.",
|
||||
"Results describe the current IDA analysis database. Input identity does not prove an unmodified database; patches, annotations, analysis completeness, and database revision remain unknown.",
|
||||
"Live results are never replayed from analysis snapshots. Target checks before and after a request do not provide an atomic lock against external GUI changes.",
|
||||
"Caller cancellation drains the in-flight MCP request before cleanup; it does not prove that IDA engine work was interrupted.",
|
||||
"Headless workers are released through the upstream database lifecycle. Abrupt termination of REA, its proxy, or the supervisor cannot prove worker cleanup; inspect upstream inventory after an interrupted process.",
|
||||
"Complete function body ranges, typed references, unresolved indirect calls, referenced data, and basic blocks are unavailable in this adapter. Empty unsupported dossier facets are unknown, not observed absence.",
|
||||
]);
|
||||
|
||||
/** Existing REA contracts implemented by the adapter; no upstream tools are exposed directly. */
|
||||
export const IDA_PROVIDER_TOOL_CONTRACTS = Object.freeze(
|
||||
IDA_OPERATIONS.map((operation) => {
|
||||
const contract = GENERATED_MCP_TOOL_CATALOG.find(
|
||||
({ name }) => name === operation,
|
||||
);
|
||||
if (contract === undefined)
|
||||
throw new TypeError(`Missing contract for ${operation}`);
|
||||
return { ...contract, name: operation };
|
||||
}),
|
||||
);
|
||||
|
||||
/** Advertise live observations and configured lifecycle effects truthfully. */
|
||||
export const idaCapabilities = (
|
||||
mode: "attached" | "headless",
|
||||
stdio: boolean,
|
||||
): readonly CapabilityDescriptor[] =>
|
||||
IDA_PROVIDER_TOOL_CONTRACTS.map(({ name: operation }) => ({
|
||||
provider: IDA_PROVIDER_IDENTITY,
|
||||
operation,
|
||||
...(mode === "headless" && operation === "procedure_callers"
|
||||
? ({
|
||||
available: false,
|
||||
reason:
|
||||
"The modern upstream profile reports code references without proving direct call edges.",
|
||||
} as const)
|
||||
: ({ available: true, reason: null } as const)),
|
||||
cachePolicy: "live",
|
||||
effects: {
|
||||
mutatesArtifact: false,
|
||||
launchesProcess: stdio || mode === "headless",
|
||||
mayShowUi: false,
|
||||
mayAccessNetwork: true,
|
||||
mayWriteFilesystem: stdio || mode === "headless",
|
||||
changesPermissions: false,
|
||||
requiresRoot: false,
|
||||
},
|
||||
limitations: IDA_LIMITATIONS,
|
||||
}));
|
||||
@@ -0,0 +1,415 @@
|
||||
import { readFile, readdir, rm } from "node:fs/promises";
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
import { z } from "zod";
|
||||
import {
|
||||
createIdaTarget,
|
||||
RecordingIdaMcp,
|
||||
} from "../../tests/fixtures/idaMcp.js";
|
||||
import { IdaSessionClient } from "./IdaSessionClient.js";
|
||||
import { functionDossierSchema } from "../domain/hopperValues.js";
|
||||
import { jsonValueSchema } from "../domain/jsonValue.js";
|
||||
|
||||
const roots: string[] = [];
|
||||
afterEach(async () => {
|
||||
for (const root of roots.splice(0))
|
||||
await rm(root, { recursive: true, force: true });
|
||||
});
|
||||
const fixture = async (mode: "attached" | "headless" = "attached") => {
|
||||
const { root, target } = await createIdaTarget();
|
||||
roots.push(root);
|
||||
const producer = new RecordingIdaMcp(target, mode);
|
||||
const client = new IdaSessionClient(
|
||||
{
|
||||
command: "fixture",
|
||||
args: [],
|
||||
env: {},
|
||||
mode,
|
||||
timeoutMs: 1000,
|
||||
workspaceRoot: root,
|
||||
},
|
||||
target,
|
||||
producer,
|
||||
);
|
||||
return { root, target, producer, client };
|
||||
};
|
||||
|
||||
describe("headless IDA producer pagination", () => {
|
||||
it("follows reported cursors for modern inventories, pseudocode, and bare-hex assembly without losing pages", async () => {
|
||||
const { producer, client } = await fixture("headless");
|
||||
producer.overrideCall = (name, args) => {
|
||||
const first = args.offset === 0;
|
||||
const cursor = first ? { next: 1 } : { done: true };
|
||||
if (name === "decompile")
|
||||
return { code: first ? "first line" : "second line", cursor };
|
||||
if (name === "disasm")
|
||||
return {
|
||||
asm: {
|
||||
name: "main",
|
||||
start_ea: "0x1000",
|
||||
lines: [
|
||||
{
|
||||
addr: first ? "1000" : "1001",
|
||||
instruction: first ? "push rbp" : "ret",
|
||||
},
|
||||
],
|
||||
},
|
||||
cursor,
|
||||
};
|
||||
if (name === "find_regex")
|
||||
return {
|
||||
matches: [
|
||||
{
|
||||
addr: first ? "0x3000" : "0x3002",
|
||||
string: first ? "a.b" : "a+b",
|
||||
},
|
||||
],
|
||||
cursor,
|
||||
};
|
||||
if (name === "list_funcs") {
|
||||
const page = z
|
||||
.object({ offset: z.number() })
|
||||
.parse(args.queries).offset;
|
||||
return [
|
||||
{
|
||||
data: [
|
||||
{
|
||||
addr: page === 0 ? "0x1000" : "0x2000",
|
||||
name: page === 0 ? "main" : "other",
|
||||
size: "0x20",
|
||||
},
|
||||
],
|
||||
next_offset: page === 0 ? 1 : null,
|
||||
},
|
||||
];
|
||||
}
|
||||
return undefined;
|
||||
};
|
||||
const inventory = await client.execute("list_procedures", {});
|
||||
expect(inventory.ok && inventory.value.result).toHaveLength(2);
|
||||
const strings = await client.execute("list_strings", {});
|
||||
expect(strings.ok && strings.value.result).toHaveLength(2);
|
||||
const result = await client.execute("analyze_function", {
|
||||
procedure: "main",
|
||||
});
|
||||
if (!result.ok) throw result.error;
|
||||
const dossier = functionDossierSchema.parse(result.value.result);
|
||||
expect(dossier.pseudocode).toBe("first line\nsecond line");
|
||||
expect(dossier.assembly).toEqual(["0x1000: push rbp", "0x1001: ret"]);
|
||||
await client.close();
|
||||
});
|
||||
});
|
||||
|
||||
describe("attached IDA observation semantics", () => {
|
||||
it("rejects an unrelated document before connecting or opening a provider", async () => {
|
||||
const { producer, client } = await fixture();
|
||||
const result = await client.execute("list_procedures", {
|
||||
document: "/unrelated/input",
|
||||
});
|
||||
expect(result.ok).toBe(false);
|
||||
if (!result.ok) {
|
||||
expect(result.error._tag).toBe("AnalysisInputError");
|
||||
expect(result.error.message).toContain("list_procedures");
|
||||
}
|
||||
expect(producer.connects).toBe(0);
|
||||
expect(producer.calls).toEqual([]);
|
||||
await client.close();
|
||||
});
|
||||
it("preserves hexadecimal-looking function names as names unless the caller uses an explicit 0x address", async () => {
|
||||
const { producer, client } = await fixture();
|
||||
producer.overrideCall = (name, args) =>
|
||||
name === "get_function_by_name" && args.name === "f"
|
||||
? { address: "0x1000", name: "f", size: "0x20" }
|
||||
: undefined;
|
||||
const found = await client.execute("procedure_address", { procedure: "f" });
|
||||
expect(found.ok && found.value.result).toBe("0x1000");
|
||||
expect(
|
||||
producer.calls.some(({ name }) => name === "get_function_by_address"),
|
||||
).toBe(false);
|
||||
await client.close();
|
||||
});
|
||||
it("accepts MCP's null projection for omitted optional fields without accepting null required fields", async () => {
|
||||
const { producer, client } = await fixture();
|
||||
expect(
|
||||
(await client.execute("list_procedures", { document: null })).ok,
|
||||
).toBe(true);
|
||||
expect(
|
||||
(await client.execute("list_strings", { document: null, address: null }))
|
||||
.ok,
|
||||
).toBe(true);
|
||||
expect(
|
||||
(
|
||||
await client.execute("procedure_pseudo_code", {
|
||||
document: null,
|
||||
procedure: "main",
|
||||
})
|
||||
).ok,
|
||||
).toBe(true);
|
||||
const required = await client.execute("procedure_pseudo_code", {
|
||||
document: null,
|
||||
procedure: null,
|
||||
});
|
||||
expect(required.ok).toBe(false);
|
||||
if (!required.ok) expect(required.error._tag).toBe("AnalysisInputError");
|
||||
expect(
|
||||
producer.calls.filter(({ name }) => name === "decompile_function"),
|
||||
).toHaveLength(1);
|
||||
await client.close();
|
||||
});
|
||||
});
|
||||
|
||||
describe("attached IDA function observations", () => {
|
||||
it("resolves interior addresses and call sites to canonical entries, preserves externals, and reports unknown body evidence", async () => {
|
||||
const { producer, client } = await fixture();
|
||||
const result = await client.execute("analyze_function", {
|
||||
procedure: "0x1004",
|
||||
});
|
||||
expect(result.ok).toBe(true);
|
||||
if (!result.ok) throw result.error;
|
||||
const dossier = functionDossierSchema.parse(result.value.result);
|
||||
expect(dossier.procedure.address).toBe("0x1000");
|
||||
expect(dossier.callers.map((caller) => caller.address)).toEqual(["0x1000"]);
|
||||
expect(dossier.callees.map((callee) => callee.address)).toEqual(["0x2000"]);
|
||||
expect(dossier.procedure.body.available).toBe(false);
|
||||
expect(dossier.comments).toEqual([]);
|
||||
expect(result.value.rawResult).toMatchObject({
|
||||
observations: expect.arrayContaining([
|
||||
expect.objectContaining({ tool: "get_callers" }),
|
||||
]),
|
||||
});
|
||||
expect(result.value.provider.version).toBe("1");
|
||||
expect(producer.serverInfo()).toMatchObject({
|
||||
version: "sdk-version-is-not-ida",
|
||||
});
|
||||
await client.close();
|
||||
expect(producer.calls.some(({ name }) => name.startsWith("idb_"))).toBe(
|
||||
false,
|
||||
);
|
||||
});
|
||||
it("returns complete paged inventory and distinguishes literal from regex search", async () => {
|
||||
const { producer, client } = await fixture();
|
||||
producer.overrideCall = (name, args) =>
|
||||
name === "list_functions"
|
||||
? {
|
||||
data: [
|
||||
{
|
||||
address: args.offset === 0 ? "0x1000" : "0x2000",
|
||||
name: "main",
|
||||
size: "0x10",
|
||||
},
|
||||
],
|
||||
next_offset: args.offset === 0 ? 100 : null,
|
||||
}
|
||||
: undefined;
|
||||
const inventory = await client.execute("list_procedures", {});
|
||||
expect(inventory.ok && inventory.value.result).toHaveLength(2);
|
||||
for (const [pattern, mode, count] of [
|
||||
["a.b", "literal", 1],
|
||||
["a+b", "literal", 0],
|
||||
["a.b", "regex", 1],
|
||||
] as const) {
|
||||
const found = await client.execute("search_strings", { pattern, mode });
|
||||
expect(found.ok && found.value.result).toHaveLength(count);
|
||||
}
|
||||
expect(
|
||||
(await client.execute("search_strings", { pattern: "[", mode: "regex" }))
|
||||
.ok,
|
||||
).toBe(false);
|
||||
await client.close();
|
||||
});
|
||||
it("rejects mismatched identities and target switches rather than returning a successful observation", async () => {
|
||||
const { producer, client, target } = await fixture();
|
||||
producer.inputSha256 = "a".repeat(64);
|
||||
const mismatch = await client.execute("health", {});
|
||||
expect(mismatch.ok).toBe(false);
|
||||
if (!mismatch.ok) expect(mismatch.error.message).toContain("SHA-256");
|
||||
producer.inputSha256 = target.sha256;
|
||||
producer.beforeCall = async (name) => {
|
||||
if (name === "decompile_function") producer.imageBase = "0x2000";
|
||||
};
|
||||
const changed = await client.execute("procedure_pseudo_code", {
|
||||
procedure: "main",
|
||||
});
|
||||
expect(changed.ok).toBe(false);
|
||||
if (!changed.ok) expect(changed.error.message).toContain("changed");
|
||||
await client.close();
|
||||
});
|
||||
it("rejects malformed producer data, non-advancing cursors, unsupported mutation, and missing explicit addresses", async () => {
|
||||
const { producer, client } = await fixture();
|
||||
expect(
|
||||
(
|
||||
await client.execute("set_address_name", {
|
||||
address: "0x1000",
|
||||
name: "changed",
|
||||
})
|
||||
).ok,
|
||||
).toBe(false);
|
||||
expect(producer.connects).toBe(0);
|
||||
expect((await client.execute("xrefs", {})).ok).toBe(false);
|
||||
const invalidAddress = await client.execute("xrefs", {
|
||||
address: "not-an-address",
|
||||
});
|
||||
expect(invalidAddress.ok).toBe(false);
|
||||
if (!invalidAddress.ok)
|
||||
expect(invalidAddress.error._tag).toBe("AnalysisInputError");
|
||||
expect(producer.connects).toBe(0);
|
||||
producer.overrideCall = (name) =>
|
||||
name === "list_functions" ? { data: [], next_offset: 0 } : undefined;
|
||||
expect((await client.execute("list_procedures", {})).ok).toBe(false);
|
||||
producer.overrideCall = (name) =>
|
||||
name === "decompile_function" ? { unexpected: true } : undefined;
|
||||
const malformed = await client.execute("procedure_pseudo_code", {
|
||||
procedure: "main",
|
||||
});
|
||||
expect(malformed.ok).toBe(false);
|
||||
if (!malformed.ok) expect(malformed.error._tag).toBe("AnalysisOutputError");
|
||||
await client.close();
|
||||
});
|
||||
it("drains in-flight read-only work on cancellation before closing its proxy", async () => {
|
||||
const { producer, client } = await fixture();
|
||||
await client.execute("health", {});
|
||||
let release: (() => void) | undefined;
|
||||
let notify: (() => void) | undefined;
|
||||
const started = new Promise<void>((resolve) => {
|
||||
notify = resolve;
|
||||
});
|
||||
producer.beforeCall = async (name) => {
|
||||
if (name === "decompile_function") {
|
||||
notify?.();
|
||||
await new Promise<void>((resolve) => {
|
||||
release = resolve;
|
||||
});
|
||||
}
|
||||
};
|
||||
const controller = new AbortController();
|
||||
const call = client.execute(
|
||||
"procedure_pseudo_code",
|
||||
{ procedure: "main" },
|
||||
{ signal: controller.signal },
|
||||
);
|
||||
await started;
|
||||
controller.abort();
|
||||
const close = client.closeWithOutcome();
|
||||
expect(producer.closes).toBe(0);
|
||||
release?.();
|
||||
const result = await call;
|
||||
expect(result.ok).toBe(false);
|
||||
if (!result.ok) expect(result.error._tag).toBe("AnalysisCancelledError");
|
||||
expect((await close).ok).toBe(true);
|
||||
expect(producer.closes).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("headless IDA lifecycle boundaries", () => {
|
||||
it("never closes an unowned worker even when open echoes the requested private identity", async () => {
|
||||
const { producer, client } = await fixture("headless");
|
||||
producer.owned = false;
|
||||
expect((await client.execute("health", {})).ok).toBe(false);
|
||||
const closed = await client.closeWithOutcome();
|
||||
expect(closed.ok).toBe(false);
|
||||
if (!closed.ok) expect(closed.error.cleanupIncomplete).toBe(true);
|
||||
expect(producer.calls.some(({ name }) => name === "idb_close")).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("headless IDA session ownership and cleanup", () => {
|
||||
it("retains the workspace after an unconfirmed open, even when inventory is empty, and never retries startup implicitly", async () => {
|
||||
const { producer, client, root } = await fixture("headless");
|
||||
producer.beforeCall = async (name) => {
|
||||
if (name === "idb_open")
|
||||
throw new Error("Timed out while the worker may still be opening");
|
||||
};
|
||||
expect((await client.execute("health", {})).ok).toBe(false);
|
||||
expect((await client.execute("health", {})).ok).toBe(false);
|
||||
expect(
|
||||
producer.calls.filter(({ name }) => name === "idb_open"),
|
||||
).toHaveLength(1);
|
||||
const closed = await client.closeWithOutcome();
|
||||
expect(closed.ok).toBe(false);
|
||||
if (!closed.ok) expect(closed.error.cleanupIncomplete).toBe(true);
|
||||
expect(
|
||||
(await readdir(root)).some((name) => name.startsWith("rea-ida-")),
|
||||
).toBe(true);
|
||||
expect(producer.closes).toBe(1);
|
||||
});
|
||||
it("opens a private digest-verified copy, scopes requests, closes without saving, and removes only its workspace", async () => {
|
||||
const { producer, client, root, target } = await fixture("headless");
|
||||
const original = await readFile(target.path);
|
||||
const result = await client.execute("analyze_function", {
|
||||
procedure: "main",
|
||||
});
|
||||
expect(result.ok).toBe(true);
|
||||
if (!result.ok) throw result.error;
|
||||
expect(producer.inputPath).not.toBe(target.path);
|
||||
expect(await readFile(producer.inputPath)).toEqual(original);
|
||||
expect(
|
||||
functionDossierSchema
|
||||
.parse(result.value.result)
|
||||
.limitations.some((value) => value.includes("Direct callers")),
|
||||
).toBe(true);
|
||||
const open = producer.calls.find(({ name }) => name === "idb_open");
|
||||
expect(open?.args).toMatchObject({
|
||||
mode: "force_headless",
|
||||
run_auto_analysis: true,
|
||||
});
|
||||
for (const call of producer.calls.filter(
|
||||
({ name }) => !["idb_open", "idb_list"].includes(name),
|
||||
))
|
||||
expect(call.args.database).toBe(producer.database);
|
||||
expect((await client.closeWithOutcome()).ok).toBe(true);
|
||||
expect(
|
||||
producer.calls.find(({ name }) => name === "idb_close")?.args.save,
|
||||
).toBe(false);
|
||||
expect(await readdir(root)).toEqual(["target.elf"]);
|
||||
expect(await readFile(target.path)).toEqual(original);
|
||||
});
|
||||
it("retains the private workspace and reports cleanup failure when worker release is unverified", async () => {
|
||||
const { producer, client, root } = await fixture("headless");
|
||||
await client.execute("health", {});
|
||||
producer.failClose = true;
|
||||
const result = await client.closeWithOutcome();
|
||||
expect(result.ok).toBe(false);
|
||||
if (!result.ok) expect(result.error.cleanupIncomplete).toBe(true);
|
||||
expect(
|
||||
(await readdir(root)).some((name) => name.startsWith("rea-ida-")),
|
||||
).toBe(true);
|
||||
});
|
||||
it("never closes an unrelated database when startup returns a different input identity", async () => {
|
||||
const { producer, client, root } = await fixture("headless");
|
||||
producer.overrideCall = (name) =>
|
||||
name === "idb_open"
|
||||
? {
|
||||
success: true,
|
||||
session: {
|
||||
session_id: "user-database",
|
||||
input_path: "/unrelated/input",
|
||||
},
|
||||
}
|
||||
: undefined;
|
||||
expect((await client.execute("health", {})).ok).toBe(false);
|
||||
expect((await client.closeWithOutcome()).ok).toBe(true);
|
||||
expect(producer.calls.some(({ name }) => name === "idb_close")).toBe(false);
|
||||
expect(await readdir(root)).toEqual(["target.elf"]);
|
||||
});
|
||||
it("releases a privately owned worker after malformed open output using verified session inventory", async () => {
|
||||
const { producer, client, root } = await fixture("headless");
|
||||
producer.beforeCall = async (name, args) => {
|
||||
if (name === "idb_open") {
|
||||
producer.database = z.string().parse(args.preferred_session_id);
|
||||
producer.inputPath = z.string().parse(args.input_path);
|
||||
}
|
||||
};
|
||||
producer.overrideCall = (name) =>
|
||||
name === "idb_open"
|
||||
? jsonValueSchema.parse({
|
||||
success: true,
|
||||
session: { session_id: producer.database },
|
||||
})
|
||||
: undefined;
|
||||
expect((await client.execute("health", {})).ok).toBe(false);
|
||||
expect((await client.closeWithOutcome()).ok).toBe(true);
|
||||
expect(
|
||||
producer.calls.find(({ name }) => name === "idb_close")?.args.database,
|
||||
).toMatch(/^rea-/u);
|
||||
expect(await readdir(root)).toEqual(["target.elf"]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,472 @@
|
||||
import { z } from "zod";
|
||||
import {
|
||||
createAnalysisExecution,
|
||||
type AnalysisClient,
|
||||
type AnalysisOperation,
|
||||
type ExecutionOptions,
|
||||
} from "../application/AnalysisProvider.js";
|
||||
import {
|
||||
AnalysisCancelledError,
|
||||
AnalysisCapabilityUnavailableError,
|
||||
AnalysisProtocolError,
|
||||
AnalysisOutputError,
|
||||
AnalysisInputError,
|
||||
} from "../domain/analysisErrorCore.js";
|
||||
import { AnalysisError } from "../domain/analysisErrorBase.js";
|
||||
import { ProviderAdapterError } from "../domain/providerAdapterError.js";
|
||||
import type { BinaryTarget } from "../domain/binaryTarget.js";
|
||||
import type { AnalysisProfileCommitment } from "../domain/analysisProfile.js";
|
||||
import { jsonValueSchema, type JsonValue } from "../domain/jsonValue.js";
|
||||
import { err, ok, type Result } from "../domain/result.js";
|
||||
import type { IdaConfiguration } from "./IdaConfiguration.js";
|
||||
import type { IdaMcpConnection } from "./IdaMcpConnection.js";
|
||||
import { IdaOperationRunner } from "./IdaOperationRunner.js";
|
||||
import {
|
||||
IDA_LIMITATIONS,
|
||||
IDA_OPERATIONS,
|
||||
IDA_PROVIDER_IDENTITY,
|
||||
type IdaOperation,
|
||||
} from "./IdaProviderCapabilities.js";
|
||||
import {
|
||||
closeDatabaseSchema,
|
||||
databaseListSchema,
|
||||
legacyMetadataSchema,
|
||||
modernHealthSchema,
|
||||
openDatabaseSchema,
|
||||
} from "./IdaProtocolValues.js";
|
||||
import {
|
||||
idaFileDigest,
|
||||
IdaWorkspace,
|
||||
sameIdaHostPath,
|
||||
} from "./IdaWorkspace.js";
|
||||
import { parseIdaInput } from "./IdaInput.js";
|
||||
|
||||
const legacyTools = [
|
||||
"get_metadata",
|
||||
"list_functions",
|
||||
"list_strings",
|
||||
"get_function_by_name",
|
||||
"get_function_by_address",
|
||||
"decompile_function",
|
||||
"disassemble_function",
|
||||
"get_callers",
|
||||
"get_callees",
|
||||
"get_xrefs_to",
|
||||
];
|
||||
const modernTools = [
|
||||
"idb_open",
|
||||
"idb_list",
|
||||
"idb_close",
|
||||
"server_health",
|
||||
"list_funcs",
|
||||
"lookup_funcs",
|
||||
"find_regex",
|
||||
"decompile",
|
||||
"disasm",
|
||||
"callees",
|
||||
"xrefs_to",
|
||||
];
|
||||
const operationSet: ReadonlySet<string> = new Set(IDA_OPERATIONS);
|
||||
const isIdaOperation = (operation: string): operation is IdaOperation =>
|
||||
operationSet.has(operation);
|
||||
const wasCancelled = (options: ExecutionOptions | undefined): boolean =>
|
||||
options?.signal?.aborted === true;
|
||||
|
||||
/** A failed cleanup retains its private workspace and reports the remaining resource. */
|
||||
class IdaCleanupError extends ProviderAdapterError {
|
||||
override readonly cleanupIncomplete = true;
|
||||
override readonly cleanupResources: readonly string[];
|
||||
constructor(path: string, cause: unknown) {
|
||||
super("ida", "close", {
|
||||
cause,
|
||||
diagnostics: {
|
||||
reason:
|
||||
"IDA worker release could not be verified. The private workspace was retained.",
|
||||
workspace: path,
|
||||
},
|
||||
});
|
||||
this.cleanupResources = [path];
|
||||
}
|
||||
}
|
||||
|
||||
/** Serial read-only session whose lifecycle authority excludes an attached GUI. */
|
||||
export class IdaSessionClient implements AnalysisClient {
|
||||
#tail: Promise<void> = Promise.resolve();
|
||||
#startup: Promise<void> | undefined;
|
||||
#closed = false;
|
||||
#closing = false;
|
||||
#workspace: IdaWorkspace | undefined;
|
||||
#database: string | undefined;
|
||||
#openAttempted = false;
|
||||
#openCompleted = false;
|
||||
#initialIdentity: string | undefined;
|
||||
#metadata: JsonValue = null;
|
||||
#lifecycle: JsonValue[] = [];
|
||||
|
||||
constructor(
|
||||
private readonly config: IdaConfiguration,
|
||||
private readonly target: BinaryTarget,
|
||||
private readonly connection: IdaMcpConnection,
|
||||
private readonly profile?: AnalysisProfileCommitment,
|
||||
) {}
|
||||
|
||||
execute(
|
||||
operation: AnalysisOperation,
|
||||
parameters: Readonly<Record<string, JsonValue>>,
|
||||
options?: ExecutionOptions,
|
||||
) {
|
||||
const call = this.#tail.then(() =>
|
||||
this.#execute(operation, parameters, options),
|
||||
);
|
||||
this.#tail = call.then(
|
||||
() => undefined,
|
||||
() => undefined,
|
||||
);
|
||||
return call;
|
||||
}
|
||||
|
||||
async #execute(
|
||||
operation: AnalysisOperation,
|
||||
parameters: Readonly<Record<string, JsonValue>>,
|
||||
options?: ExecutionOptions,
|
||||
) {
|
||||
if (this.#closed || this.#closing)
|
||||
return err(
|
||||
new AnalysisProtocolError(
|
||||
"IDA session is closing or closed; reopen the target.",
|
||||
),
|
||||
);
|
||||
if (wasCancelled(options))
|
||||
return err(new AnalysisCancelledError(operation));
|
||||
if (operation !== "health" && !isIdaOperation(operation))
|
||||
return err(
|
||||
new AnalysisCapabilityUnavailableError(
|
||||
"ida",
|
||||
operation,
|
||||
"This integration admits read-only analysis operations only.",
|
||||
),
|
||||
);
|
||||
if (operation === "procedure_callers" && this.config.mode === "headless")
|
||||
return err(
|
||||
new AnalysisCapabilityUnavailableError(
|
||||
"ida",
|
||||
operation,
|
||||
"The modern upstream profile does not prove direct callers.",
|
||||
),
|
||||
);
|
||||
if (operation !== "health") {
|
||||
const parsed = parseIdaInput(operation, parameters);
|
||||
if (!parsed.ok) return parsed;
|
||||
parameters = parsed.value;
|
||||
}
|
||||
const document = parameters.document;
|
||||
if (
|
||||
document !== undefined &&
|
||||
document !== this.target.path &&
|
||||
document !== this.target.sourcePath
|
||||
)
|
||||
return err(
|
||||
new AnalysisInputError(operation, undefined, [
|
||||
{
|
||||
path: ["document"],
|
||||
reason: "invalid_value",
|
||||
expected: this.target.path,
|
||||
message:
|
||||
"IDA document must be the REA-bound target; omit document or use its admitted path.",
|
||||
},
|
||||
]),
|
||||
);
|
||||
try {
|
||||
await this.#start();
|
||||
const before = await this.#observe();
|
||||
if (wasCancelled(options))
|
||||
return err(new AnalysisCancelledError(operation));
|
||||
if (operation === "health") return this.#health(before);
|
||||
if (!isIdaOperation(operation))
|
||||
throw new AnalysisProtocolError(
|
||||
"IDA operation changed after validation.",
|
||||
);
|
||||
const runner = new IdaOperationRunner(
|
||||
this.connection,
|
||||
this.config.mode === "attached" ? "legacy" : "modern",
|
||||
this.#database,
|
||||
);
|
||||
const result = await runner.run(operation, parameters);
|
||||
const after = await this.#observe();
|
||||
if (wasCancelled(options))
|
||||
return err(new AnalysisCancelledError(operation));
|
||||
return ok(
|
||||
createAnalysisExecution(result, IDA_PROVIDER_IDENTITY, {
|
||||
rawResult: {
|
||||
upstream: this.connection.serverInfo(),
|
||||
metadata_before: before,
|
||||
metadata_after: after,
|
||||
observations: jsonValueSchema.parse(runner.raw),
|
||||
},
|
||||
limitations: [...IDA_LIMITATIONS, ...runner.limitations],
|
||||
locations: [
|
||||
{ kind: "artifact-path", path: this.target.path },
|
||||
...runner.locations,
|
||||
],
|
||||
...(this.profile === undefined
|
||||
? {}
|
||||
: { analysisProfile: this.profile }),
|
||||
}),
|
||||
);
|
||||
} catch (cause: unknown) {
|
||||
return this.#failure(operation, cause, options);
|
||||
}
|
||||
}
|
||||
|
||||
#health(metadata: JsonValue) {
|
||||
return ok(
|
||||
createAnalysisExecution(
|
||||
{
|
||||
ready: true,
|
||||
mode: this.config.mode,
|
||||
database: this.#database ?? null,
|
||||
upstream: this.connection.serverInfo(),
|
||||
metadata,
|
||||
lifecycle: this.#lifecycle,
|
||||
},
|
||||
IDA_PROVIDER_IDENTITY,
|
||||
{
|
||||
limitations: IDA_LIMITATIONS,
|
||||
...(this.profile === undefined
|
||||
? {}
|
||||
: { analysisProfile: this.profile }),
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
#failure(
|
||||
operation: string,
|
||||
cause: unknown,
|
||||
options: ExecutionOptions | undefined,
|
||||
) {
|
||||
if (wasCancelled(options))
|
||||
return err(new AnalysisCancelledError(operation));
|
||||
if (cause instanceof AnalysisError) return err(cause);
|
||||
if (cause instanceof z.ZodError)
|
||||
return err(
|
||||
new AnalysisOutputError(
|
||||
operation,
|
||||
`IDA MCP returned malformed or unsupported producer data: ${cause.issues.map((issue) => `${issue.path.join(".") || "$"}: ${issue.message}`).join("; ")}`,
|
||||
{ cause },
|
||||
),
|
||||
);
|
||||
return err(
|
||||
new ProviderAdapterError("ida", operation, {
|
||||
cause,
|
||||
diagnostics: {
|
||||
reason:
|
||||
cause instanceof Error ? cause.message : "Unknown upstream failure",
|
||||
recovery:
|
||||
"Check the configured upstream MCP registration and selected target; see docs/ida-provider.md.",
|
||||
},
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
#start(): Promise<void> {
|
||||
this.#startup ??= this.#initialize();
|
||||
return this.#startup;
|
||||
}
|
||||
|
||||
async #initialize(): Promise<void> {
|
||||
const tools = new Set(await this.connection.connect());
|
||||
const expected =
|
||||
this.config.mode === "attached" ? legacyTools : modernTools;
|
||||
const missing = expected.filter((name) => !tools.has(name));
|
||||
if (missing.length > 0)
|
||||
throw new AnalysisProtocolError(
|
||||
`IDA MCP ${this.config.mode} compatibility profile is missing tools: ${missing.join(", ")}. Attached mode requires the legacy 1.4 profile; headless mode requires the database supervisor profile. See docs/ida-provider.md.`,
|
||||
);
|
||||
if (this.config.mode === "headless") {
|
||||
this.#workspace = await IdaWorkspace.create(
|
||||
this.target,
|
||||
this.config.workspaceRoot,
|
||||
);
|
||||
this.#openAttempted = true;
|
||||
const raw = await this.connection.call("idb_open", {
|
||||
input_path: this.#workspace.inputPath,
|
||||
mode: "force_headless",
|
||||
run_auto_analysis: true,
|
||||
build_caches: true,
|
||||
init_hexrays: true,
|
||||
preferred_session_id: this.#workspace.sessionId,
|
||||
});
|
||||
this.#openCompleted = true;
|
||||
this.#lifecycle.push(raw);
|
||||
const opened = openDatabaseSchema.parse(raw);
|
||||
if (
|
||||
!sameIdaHostPath(opened.session.input_path, this.#workspace.inputPath)
|
||||
)
|
||||
throw new AnalysisProtocolError(
|
||||
"IDA opened a different input than the private admitted target copy.",
|
||||
);
|
||||
if (opened.session.session_id !== this.#workspace.sessionId)
|
||||
throw new AnalysisProtocolError(
|
||||
"IDA returned an adopted or unexpected session instead of the requested private worker.",
|
||||
);
|
||||
const inventory = databaseListSchema.parse(
|
||||
await this.connection.call("idb_list", {}),
|
||||
);
|
||||
this.#lifecycle.push(jsonValueSchema.parse(inventory));
|
||||
const entry = inventory.sessions.find(
|
||||
({ session_id }) => session_id === opened.session.session_id,
|
||||
);
|
||||
if (
|
||||
entry?.owned !== true ||
|
||||
entry.backend !== "worker" ||
|
||||
entry.is_active !== true ||
|
||||
!sameIdaHostPath(entry.input_path, this.#workspace.inputPath)
|
||||
)
|
||||
throw new AnalysisProtocolError(
|
||||
"IDA did not return an active owned headless worker for the private input copy.",
|
||||
);
|
||||
this.#database = opened.session.session_id;
|
||||
}
|
||||
}
|
||||
|
||||
async #observe(): Promise<JsonValue> {
|
||||
let identity: string;
|
||||
if (this.config.mode === "attached") {
|
||||
const raw = await this.connection.call("get_metadata", {});
|
||||
const metadata = legacyMetadataSchema.parse(raw);
|
||||
if (metadata.sha256.toLowerCase() !== this.target.sha256.toLowerCase())
|
||||
throw new AnalysisProtocolError(
|
||||
`The attached IDA input SHA-256 does not match the selected target ${this.target.path}; open that target in IDA or use headless mode.`,
|
||||
);
|
||||
identity = JSON.stringify([
|
||||
metadata.path,
|
||||
metadata.sha256.toLowerCase(),
|
||||
metadata.base,
|
||||
]);
|
||||
this.#metadata = raw;
|
||||
} else {
|
||||
if (this.#database === undefined || this.#workspace === undefined)
|
||||
throw new AnalysisProtocolError(
|
||||
"IDA headless session has no database binding.",
|
||||
);
|
||||
const raw = await this.connection.call("server_health", {
|
||||
database: this.#database,
|
||||
});
|
||||
const metadata = modernHealthSchema.parse(raw);
|
||||
if (
|
||||
!sameIdaHostPath(metadata.input_path, this.#workspace.inputPath) ||
|
||||
(await idaFileDigest(this.#workspace.inputPath)) !== this.target.sha256
|
||||
)
|
||||
throw new AnalysisProtocolError(
|
||||
"IDA headless input identity changed after target admission.",
|
||||
);
|
||||
if (metadata.auto_analysis_ready !== true)
|
||||
throw new AnalysisProtocolError(
|
||||
"IDA auto-analysis is incomplete or readiness is unknown; retry after analysis completes.",
|
||||
);
|
||||
identity = JSON.stringify([
|
||||
this.#database,
|
||||
metadata.input_path,
|
||||
metadata.idb_path,
|
||||
metadata.imagebase,
|
||||
]);
|
||||
this.#metadata = raw;
|
||||
}
|
||||
if (
|
||||
this.#initialIdentity !== undefined &&
|
||||
identity !== this.#initialIdentity
|
||||
)
|
||||
throw new AnalysisProtocolError(
|
||||
"The attached IDA target or image base changed during the REA session; reopen the target.",
|
||||
);
|
||||
this.#initialIdentity = identity;
|
||||
return this.#metadata;
|
||||
}
|
||||
|
||||
async closeWithOutcome(): Promise<Result<null, AnalysisError>> {
|
||||
this.#closing = true;
|
||||
await this.#tail;
|
||||
if (this.#closed) return ok(null);
|
||||
try {
|
||||
if (this.#workspace !== undefined && this.#openAttempted) {
|
||||
if (this.#database === undefined) {
|
||||
const inventory = databaseListSchema.parse(
|
||||
await this.connection.call("idb_list", {}),
|
||||
);
|
||||
const workspace = this.#workspace;
|
||||
this.#database = inventory.sessions.find(
|
||||
(entry) =>
|
||||
entry.owned &&
|
||||
entry.backend === "worker" &&
|
||||
entry.session_id === workspace.sessionId &&
|
||||
sameIdaHostPath(entry.input_path, workspace.inputPath),
|
||||
)?.session_id;
|
||||
if (
|
||||
this.#database === undefined &&
|
||||
inventory.sessions.some((entry) =>
|
||||
sameIdaHostPath(entry.input_path, workspace.inputPath),
|
||||
)
|
||||
)
|
||||
throw new AnalysisProtocolError(
|
||||
"An unowned worker still uses the private workspace; it was retained.",
|
||||
);
|
||||
}
|
||||
if (this.#database !== undefined) {
|
||||
const closed = closeDatabaseSchema.parse(
|
||||
await this.connection.call("idb_close", {
|
||||
database: this.#database,
|
||||
save: false,
|
||||
}),
|
||||
);
|
||||
if (
|
||||
closed.session_id !== this.#database ||
|
||||
closed.saved === true ||
|
||||
closed.owned !== true ||
|
||||
closed.backend !== "worker"
|
||||
)
|
||||
throw new AnalysisProtocolError(
|
||||
"IDA database close did not confirm the requested owned, unsaved session release.",
|
||||
);
|
||||
const inventory = databaseListSchema.parse(
|
||||
await this.connection.call("idb_list", {}),
|
||||
);
|
||||
if (
|
||||
inventory.sessions.some(
|
||||
(entry) =>
|
||||
entry.session_id === this.#database ||
|
||||
sameIdaHostPath(
|
||||
entry.input_path,
|
||||
this.#workspace?.inputPath ?? "",
|
||||
),
|
||||
)
|
||||
)
|
||||
throw new AnalysisProtocolError(
|
||||
"IDA worker remains registered after close.",
|
||||
);
|
||||
}
|
||||
if (!this.#openCompleted)
|
||||
throw new AnalysisProtocolError(
|
||||
"IDA database open did not return a completed response. Worker inventory cannot prove that the request has stopped; the private workspace was retained.",
|
||||
);
|
||||
}
|
||||
await this.connection.close();
|
||||
await this.#workspace?.remove();
|
||||
this.#closed = true;
|
||||
return ok(null);
|
||||
} catch (cause: unknown) {
|
||||
await this.connection.close().catch(() => undefined);
|
||||
return err(
|
||||
new IdaCleanupError(
|
||||
this.#workspace?.directory ?? "IDA MCP connection",
|
||||
cause,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async close(): Promise<void> {
|
||||
const result = await this.closeWithOutcome();
|
||||
if (!result.ok) throw result.error;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
import { createHash, randomUUID } from "node:crypto";
|
||||
import { createReadStream, constants } from "node:fs";
|
||||
import { copyFile } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { basename, join, normalize } from "node:path";
|
||||
import type { BinaryTarget } from "../domain/binaryTarget.js";
|
||||
import { AnalysisProtocolError } from "../domain/analysisErrorCore.js";
|
||||
import { PrivateRuntimeRoot } from "../process/PrivateRuntimeRoot.js";
|
||||
|
||||
/** Read a file digest with bounded memory rather than retaining binary bytes. */
|
||||
export const idaFileDigest = async (path: string): Promise<string> => {
|
||||
const hash = createHash("sha256");
|
||||
for await (const chunk of createReadStream(path)) hash.update(chunk);
|
||||
return hash.digest("hex");
|
||||
};
|
||||
|
||||
/** Compare host-local paths without confusing Windows paths with POSIX coordinates. */
|
||||
export const sameIdaHostPath = (left: string, right: string): boolean =>
|
||||
process.platform === "win32"
|
||||
? normalize(left).toLowerCase() === normalize(right).toLowerCase()
|
||||
: normalize(left) === normalize(right);
|
||||
|
||||
/** Private input copy isolates upstream database and sidecar writes from the original. */
|
||||
export class IdaWorkspace {
|
||||
readonly sessionId = `rea-${randomUUID()}`;
|
||||
private constructor(
|
||||
private readonly runtime: PrivateRuntimeRoot,
|
||||
readonly inputPath: string,
|
||||
) {}
|
||||
|
||||
/** Filesystem root owned exclusively by this headless session. */
|
||||
get directory(): string {
|
||||
return this.runtime.path;
|
||||
}
|
||||
|
||||
/** Allocate a protected workspace and copy the admitted input with digest verification. */
|
||||
static async create(
|
||||
target: BinaryTarget,
|
||||
root = tmpdir(),
|
||||
): Promise<IdaWorkspace> {
|
||||
const runtime = await PrivateRuntimeRoot.create({
|
||||
parent: normalize(root),
|
||||
prefix: "rea-ida-",
|
||||
});
|
||||
const inputPath = join(runtime.path, basename(target.path));
|
||||
try {
|
||||
await copyFile(target.path, inputPath, constants.COPYFILE_EXCL);
|
||||
if ((await idaFileDigest(inputPath)) !== target.sha256)
|
||||
throw new AnalysisProtocolError(
|
||||
"IDA target changed between admission and the private input copy; reopen the target.",
|
||||
);
|
||||
return new IdaWorkspace(runtime, inputPath);
|
||||
} catch (cause: unknown) {
|
||||
await runtime.close();
|
||||
throw cause;
|
||||
}
|
||||
}
|
||||
|
||||
/** Delete only this object's privately allocated directory after worker cleanup. */
|
||||
async remove(): Promise<void> {
|
||||
await this.runtime.close();
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,7 @@ import { mcpProgressReporter } from "./mcpProgress.js";
|
||||
import { logToolExecution } from "./toolLogging.js";
|
||||
import { toolRegistrationOptions } from "./toolRegistrationOptions.js";
|
||||
import { toCallToolResult } from "./toolResult.js";
|
||||
import { executeFunctionAnalysisEvidence } from "../application/FunctionAnalysisEvidence.js";
|
||||
|
||||
/** Optional session services used by enhanced tool registration. */
|
||||
export interface EnhancedToolRegistration {
|
||||
@@ -208,6 +209,11 @@ const executeEnhancedTool = async (
|
||||
});
|
||||
const parsedInput = validatedCall.input;
|
||||
const parameters = jsonParameters(parsedInput);
|
||||
if (name === "analyze_function")
|
||||
return executeFunctionTool(analysis, registration, contract, {
|
||||
context,
|
||||
parameters,
|
||||
});
|
||||
const services = new EnhancedTools({
|
||||
execute: (operation, operationParameters, executionOptions) =>
|
||||
analysis.execute(operation, operationParameters, {
|
||||
@@ -260,6 +266,46 @@ const executeEnhancedTool = async (
|
||||
return toCallToolResult(result, contract);
|
||||
};
|
||||
|
||||
const executeFunctionTool = async (
|
||||
analysis: AnalysisOperationPort,
|
||||
registration: EnhancedToolRegistration,
|
||||
contract: ToolContract,
|
||||
request: {
|
||||
readonly context: ServerContext;
|
||||
readonly parameters: Readonly<Record<string, JsonValue>>;
|
||||
},
|
||||
): Promise<CallToolResult> => {
|
||||
const { context, parameters } = request;
|
||||
const progress = mcpProgressReporter(context);
|
||||
const result = await logToolExecution(
|
||||
registration.logger,
|
||||
"analyze_function",
|
||||
() =>
|
||||
executeFunctionAnalysisEvidence(
|
||||
analysis,
|
||||
parameters,
|
||||
registration.activeTarget?.(),
|
||||
{
|
||||
signal: context.mcpReq.signal,
|
||||
progress,
|
||||
},
|
||||
),
|
||||
);
|
||||
await progress.report({
|
||||
phase: "analyze_function",
|
||||
completed: 1,
|
||||
total: 1,
|
||||
message: result.ok ? "completed" : "failed",
|
||||
terminal: true,
|
||||
});
|
||||
if (result.ok) {
|
||||
const recorded = registration.recordEvidence?.(result.value);
|
||||
if (recorded !== undefined && !recorded.ok)
|
||||
return toCallToolResult(recorded, contract);
|
||||
}
|
||||
return toCallToolResult(result, contract);
|
||||
};
|
||||
|
||||
const jsonParameters = (
|
||||
input: Readonly<Record<string, JsonValue | undefined>>,
|
||||
): Record<string, JsonValue> =>
|
||||
|
||||
Reference in New Issue
Block a user