fix(ida): preserve non-x86 call edges and workflow uncertainty (#1906)
* test(ida): expect callee edges from the call graph on non-x86 targets * fix(ida): read callee edges from the call graph on non-x86 targets * test(ida): pin address order of call graph additions * docs(ida): describe the call graph source for non-x86 callees * test(ida): pin the x86 baseline, the call graph reader and its failure modes * fix(ida): check the call graph root, scope truncation to the root and degrade without callgraph * test(ida): expect managed PE off the x86 path, possible-truncation wording and a complete ARM64 traversal * fix(ida): keep managed PE and x86 reads off the call graph path and qualify truncation * test(ida): answer only expected leaf roots and assert the requested call graph roots * test(ida): expect a call graph the producer cannot give consistently to fail the callee read * fix(ida): fail the callee read when the call graph cannot be obtained consistently * fix(ida): preserve call edge authority and workflow uncertainty * fix(ida): preserve explicit root call graph caps --------- Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
This commit is contained in:
co-authored by
morluto
parent
c2cf7dcb86
commit
0ae38e1e31
@@ -1,19 +1,82 @@
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
|
||||
import type { AnalysisOperationPort } from "../../../src/application/AnalysisProvider.js";
|
||||
import { createAnalysisExecution } from "../../../src/application/AnalysisProvider.js";
|
||||
import { EnhancedTools } from "../../../src/application/EnhancedTools.js";
|
||||
import {
|
||||
AnalysisCapabilityUnavailableError,
|
||||
AnalysisOutputError,
|
||||
} from "../../../src/domain/analysisErrorCore.js";
|
||||
import { err } from "../../../src/domain/result.js";
|
||||
import { err, ok as resultOk } from "../../../src/domain/result.js";
|
||||
|
||||
import { closeEnhancedToolResources, connect } from "./enhancedToolsHarness.js";
|
||||
import { observed as ok } from "../../fixtures/analysisExecution.js";
|
||||
|
||||
afterEach(closeEnhancedToolResources);
|
||||
|
||||
const relationshipExecution = (
|
||||
result: unknown,
|
||||
limitations: readonly string[] = [],
|
||||
) =>
|
||||
resultOk(
|
||||
createAnalysisExecution(
|
||||
result,
|
||||
{ id: "fixture", name: "Fixture analysis provider", version: "1" },
|
||||
{ limitations },
|
||||
),
|
||||
);
|
||||
|
||||
describe("enhanced MCP tools", () => {
|
||||
it("preserves per-node relationship limitations and keeps observed edges", async () => {
|
||||
const limitation = "ARM64 call targets are only partially resolved.";
|
||||
const client = await connect({
|
||||
execute: async (name, arguments_) => {
|
||||
if (name === "procedure_address")
|
||||
return relationshipExecution(String(arguments_.procedure));
|
||||
if (name !== "procedure_callees")
|
||||
throw new Error(`Unexpected operation: ${name}`);
|
||||
return relationshipExecution(
|
||||
arguments_.procedure === "0x1" ? ["0x2"] : [],
|
||||
arguments_.procedure === "0x1" ? [limitation] : [],
|
||||
);
|
||||
},
|
||||
});
|
||||
const graph = await client.callTool({
|
||||
name: "get_call_graph",
|
||||
arguments: { address: "0x1", direction: "forward" },
|
||||
});
|
||||
expect(graph.isError).not.toBe(true);
|
||||
expect(graph.structuredContent).toMatchObject({
|
||||
normalized_result: {
|
||||
"0": [
|
||||
{
|
||||
address: "0x1",
|
||||
status: "ok",
|
||||
calls: ["0x2"],
|
||||
limitations: [limitation],
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
const trace = await client.callTool({
|
||||
name: "trace_call_path",
|
||||
arguments: { start: "0x1", goal: "0x2" },
|
||||
});
|
||||
expect(trace.isError).not.toBe(true);
|
||||
expect(trace.structuredContent).toMatchObject({
|
||||
normalized_result: {
|
||||
goal_status: "reached",
|
||||
traversal_path: ["0x1", "0x2"],
|
||||
edges: [{ source_address: "0x1", target_address: "0x2" }],
|
||||
truncated: true,
|
||||
residual_unknowns: [
|
||||
`Call relationships for 0x1 are limited: ${limitation}`,
|
||||
],
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("returns every batch item in caller order with its own result", async () => {
|
||||
const addresses = Array.from(
|
||||
{ length: 37 },
|
||||
|
||||
@@ -0,0 +1,291 @@
|
||||
import { afterEach, expect, it } from "vitest";
|
||||
import { z } from "zod";
|
||||
import { EnhancedTools } from "../../../../src/application/EnhancedTools.js";
|
||||
import {
|
||||
callGraphSession,
|
||||
edge,
|
||||
graphRow,
|
||||
removeCallGraphSessions,
|
||||
} from "../../../fixtures/idaCallGraph.js";
|
||||
|
||||
afterEach(removeCallGraphSessions);
|
||||
|
||||
const requestedRoots = (producer: {
|
||||
calls: { name: string; args: unknown }[];
|
||||
}) =>
|
||||
producer.calls
|
||||
.filter(({ name }) => name === "callgraph")
|
||||
.map(
|
||||
({ args }) =>
|
||||
z.object({ roots: z.array(z.string()) }).parse(args).roots[0],
|
||||
)
|
||||
.sort();
|
||||
|
||||
const armSession = () =>
|
||||
callGraphSession({
|
||||
callees: [],
|
||||
callgraph: graphRow([edge("0x1000", "0x2000"), edge("0x1000", "0x3000")]),
|
||||
leafRoots: ["0x2000", "0x3000"],
|
||||
});
|
||||
|
||||
it("shows the callee edges of an ARM64 procedure in the call graph", async () => {
|
||||
const { client, producer } = await armSession();
|
||||
try {
|
||||
const result = await new EnhancedTools(client).execute("get_call_graph", {
|
||||
address: "main",
|
||||
});
|
||||
|
||||
if (!result.ok) throw result.error;
|
||||
expect(
|
||||
result.value,
|
||||
"the root lists both observed callees instead of an empty observed list",
|
||||
).toMatchObject({
|
||||
"0": [
|
||||
{
|
||||
address: "0x1000",
|
||||
status: "ok",
|
||||
calls: ["0x2000", "0x3000"],
|
||||
limitations: expect.arrayContaining([
|
||||
"This relationship read does not establish calls to targets IDA does not recognize as functions; imports and other non-function call targets remain unknown.",
|
||||
]),
|
||||
},
|
||||
],
|
||||
"1": [
|
||||
{
|
||||
address: "0x2000",
|
||||
status: "ok",
|
||||
calls: [],
|
||||
limitations: expect.arrayContaining([
|
||||
"This relationship read does not establish calls to targets IDA does not recognize as functions; imports and other non-function call targets remain unknown.",
|
||||
]),
|
||||
},
|
||||
{
|
||||
address: "0x3000",
|
||||
status: "ok",
|
||||
calls: [],
|
||||
limitations: expect.arrayContaining([
|
||||
"This relationship read does not establish calls to targets IDA does not recognize as functions; imports and other non-function call targets remain unknown.",
|
||||
]),
|
||||
},
|
||||
],
|
||||
});
|
||||
expect(
|
||||
requestedRoots(producer),
|
||||
"each reachable procedure is read once, with exactly its own root",
|
||||
).toEqual(["0x1000", "0x2000", "0x3000"]);
|
||||
} finally {
|
||||
expect((await client.close()).ok).toBe(true);
|
||||
}
|
||||
});
|
||||
|
||||
it("reaches an ARM64 callee in a call path", async () => {
|
||||
const { client } = await armSession();
|
||||
try {
|
||||
const result = await new EnhancedTools(client).execute("trace_call_path", {
|
||||
start: "main",
|
||||
goal: "0x3000",
|
||||
});
|
||||
|
||||
if (!result.ok) throw result.error;
|
||||
expect(
|
||||
result.value,
|
||||
"the goal is reached through the observed edge",
|
||||
).toMatchObject({
|
||||
goal_status: "reached",
|
||||
traversal_path: ["0x1000", "0x3000"],
|
||||
});
|
||||
} finally {
|
||||
expect((await client.close()).ok).toBe(true);
|
||||
}
|
||||
});
|
||||
|
||||
it("traverses every ARM64 callee in a call path without a goal and records no failure", async () => {
|
||||
const { client, producer } = await armSession();
|
||||
try {
|
||||
const result = await new EnhancedTools(client).execute("trace_call_path", {
|
||||
start: "main",
|
||||
});
|
||||
|
||||
if (!result.ok) throw result.error;
|
||||
expect(
|
||||
result.value,
|
||||
"the whole reachable graph is visited with exactly the observed edges and no failure",
|
||||
).toMatchObject({
|
||||
goal_status: "not_requested",
|
||||
nodes: [
|
||||
{ address: "0x1000", depth: 0 },
|
||||
{ address: "0x2000", depth: 1 },
|
||||
{ address: "0x3000", depth: 1 },
|
||||
],
|
||||
edges: [
|
||||
{ source_address: "0x1000", target_address: "0x2000" },
|
||||
{ source_address: "0x1000", target_address: "0x3000" },
|
||||
],
|
||||
failures: [],
|
||||
truncated: true,
|
||||
limitations: expect.arrayContaining([
|
||||
"This relationship read does not establish calls to targets IDA does not recognize as functions; imports and other non-function call targets remain unknown.",
|
||||
]),
|
||||
residual_unknowns: expect.arrayContaining([
|
||||
expect.stringContaining("Call relationships for 0x1000 are limited:"),
|
||||
]),
|
||||
});
|
||||
expect(
|
||||
requestedRoots(producer),
|
||||
"each reachable procedure is read once, with exactly its own root",
|
||||
).toEqual(["0x1000", "0x2000", "0x3000"]);
|
||||
} finally {
|
||||
expect((await client.close()).ok).toBe(true);
|
||||
}
|
||||
});
|
||||
|
||||
it("preserves missing callgraph coverage through direct, graph, and trace workflows", async () => {
|
||||
const { client, producer } = await callGraphSession({
|
||||
tools: [
|
||||
"idb_open",
|
||||
"idb_list",
|
||||
"idb_close",
|
||||
"server_health",
|
||||
"list_funcs",
|
||||
"lookup_funcs",
|
||||
"find_regex",
|
||||
"decompile",
|
||||
"disasm",
|
||||
"callees",
|
||||
"xrefs_to",
|
||||
],
|
||||
callees: [
|
||||
{ addr: "0x3000", name: "bogus_non_x86_match", type: "internal" },
|
||||
],
|
||||
});
|
||||
try {
|
||||
const direct = await client.execute("procedure_callees", {
|
||||
procedure: "main",
|
||||
});
|
||||
if (!direct.ok) throw direct.error;
|
||||
expect(
|
||||
direct.value.result,
|
||||
"the x86-only producer result is ignored",
|
||||
).toEqual([]);
|
||||
expect(direct.value.limitations).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.stringContaining("callgraph was unavailable"),
|
||||
expect.stringContaining("targets IDA does not recognize as functions"),
|
||||
]),
|
||||
);
|
||||
|
||||
const application = new EnhancedTools(client);
|
||||
const graph = await application.execute("get_call_graph", {
|
||||
address: "main",
|
||||
});
|
||||
if (!graph.ok) throw graph.error;
|
||||
expect(graph.value).toMatchObject({
|
||||
"0": [
|
||||
{
|
||||
status: "ok",
|
||||
calls: [],
|
||||
limitations: expect.arrayContaining([
|
||||
expect.stringContaining("callgraph was unavailable"),
|
||||
]),
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const trace = await application.execute("trace_call_path", {
|
||||
start: "main",
|
||||
goal: "0x3000",
|
||||
});
|
||||
if (!trace.ok) throw trace.error;
|
||||
expect(trace.value).toMatchObject({
|
||||
goal_status: "not_reached",
|
||||
edges: [],
|
||||
limitations: expect.arrayContaining([
|
||||
expect.stringContaining("callgraph was unavailable"),
|
||||
]),
|
||||
residual_unknowns: expect.arrayContaining([
|
||||
expect.stringContaining("Call relationships for 0x1000 are limited:"),
|
||||
]),
|
||||
});
|
||||
expect(
|
||||
producer.calls.some(({ name }) => name === "callees"),
|
||||
"the inaccurate fallback is never queried",
|
||||
).toBe(false);
|
||||
expect(
|
||||
producer.calls.some(({ name }) => name === "callgraph"),
|
||||
"an unadvertised tool is never requested",
|
||||
).toBe(false);
|
||||
} finally {
|
||||
expect((await client.close()).ok).toBe(true);
|
||||
}
|
||||
});
|
||||
|
||||
it("records a failed ARM64 call graph read as the node's error, not as a complete node", async () => {
|
||||
const { client } = await callGraphSession({
|
||||
callees: [],
|
||||
callgraphTruncatedOutput: true,
|
||||
});
|
||||
try {
|
||||
const result = await new EnhancedTools(client).execute("get_call_graph", {
|
||||
address: "main",
|
||||
});
|
||||
|
||||
if (!result.ok) throw result.error;
|
||||
expect(
|
||||
result.value,
|
||||
"the transport-truncated graph is a recorded failure and no callee list is invented",
|
||||
).toEqual({
|
||||
"0": [
|
||||
{
|
||||
address: "0x1000",
|
||||
status: "error",
|
||||
error: expect.objectContaining({ code: "unreadable_output" }),
|
||||
},
|
||||
],
|
||||
});
|
||||
} finally {
|
||||
expect((await client.close()).ok).toBe(true);
|
||||
}
|
||||
});
|
||||
|
||||
it("preserves an explicit root cap below the edge limit through every relationship workflow", async () => {
|
||||
const { client } = await callGraphSession({
|
||||
callees: [],
|
||||
callgraph: graphRow([edge("0x1000", "0x2000")], { per_func_capped: true }),
|
||||
leafRoots: ["0x2000"],
|
||||
});
|
||||
const limitation =
|
||||
"Upstream callgraph was truncated; additional callees remain unknown.";
|
||||
try {
|
||||
const direct = await client.execute("procedure_callees", {
|
||||
procedure: "main",
|
||||
});
|
||||
if (!direct.ok) throw direct.error;
|
||||
expect(direct.value.limitations).toContain(limitation);
|
||||
expect(direct.value.result).toEqual(["0x2000"]);
|
||||
const application = new EnhancedTools(client);
|
||||
const graph = await application.execute("get_call_graph", {
|
||||
address: "main",
|
||||
});
|
||||
if (!graph.ok) throw graph.error;
|
||||
expect(graph.value).toMatchObject({
|
||||
"0": [
|
||||
{
|
||||
calls: ["0x2000"],
|
||||
limitations: expect.arrayContaining([limitation]),
|
||||
},
|
||||
],
|
||||
});
|
||||
const trace = await application.execute("trace_call_path", {
|
||||
start: "main",
|
||||
});
|
||||
if (!trace.ok) throw trace.error;
|
||||
expect(trace.value).toMatchObject({
|
||||
truncated: true,
|
||||
residual_unknowns: expect.arrayContaining([
|
||||
`Call relationships for 0x1000 are limited: ${limitation}`,
|
||||
]),
|
||||
});
|
||||
} finally {
|
||||
expect((await client.close()).ok).toBe(true);
|
||||
}
|
||||
});
|
||||
Vendored
+138
@@ -0,0 +1,138 @@
|
||||
import { rm } from "node:fs/promises";
|
||||
import { decodeIdaToolResult } from "../../src/ida/IdaMcpConnection.js";
|
||||
import { IdaSessionClient } from "../../src/ida/IdaSessionClient.js";
|
||||
import type { JsonValue } from "../../src/domain/jsonValue.js";
|
||||
import { createIdaTarget, RecordingIdaMcp } from "./idaMcp.js";
|
||||
|
||||
const roots: string[] = [];
|
||||
|
||||
/** Remove the workspaces of every session opened through `callGraphSession`. */
|
||||
export const removeCallGraphSessions = async (): Promise<void> => {
|
||||
for (const root of roots.splice(0))
|
||||
await rm(root, { recursive: true, force: true });
|
||||
};
|
||||
|
||||
export const FUNCTIONS: Readonly<
|
||||
Record<string, { addr: string; name: string }>
|
||||
> = {
|
||||
main: { addr: "0x1000", name: "main" },
|
||||
"0x1000": { addr: "0x1000", name: "main" },
|
||||
"0x2000": { addr: "0x2000", name: "square" },
|
||||
"0x3000": { addr: "0x3000", name: "printf_stub" },
|
||||
};
|
||||
|
||||
export const edge = (from: string, to: string, type = "call") => ({
|
||||
from,
|
||||
to,
|
||||
type,
|
||||
});
|
||||
export const graphRow = (
|
||||
edges: readonly ReturnType<typeof edge>[],
|
||||
extra: Readonly<Record<string, JsonValue>> = {},
|
||||
): JsonValue => [
|
||||
{
|
||||
root: "0x1000",
|
||||
nodes: [{ addr: "0x1000", name: "main", depth: 0 }],
|
||||
edges: [...edges],
|
||||
max_depth: 0,
|
||||
truncated: false,
|
||||
limit_reason: null,
|
||||
max_edges_per_func: 200,
|
||||
per_func_capped: false,
|
||||
...extra,
|
||||
},
|
||||
];
|
||||
|
||||
/** A realistic reply for a callee that makes no further calls. */
|
||||
const leafGraphRow = (root: string): JsonValue => [
|
||||
{
|
||||
root,
|
||||
nodes: [{ addr: root, name: FUNCTIONS[root]?.name ?? null, depth: 0 }],
|
||||
edges: [],
|
||||
max_depth: 0,
|
||||
truncated: false,
|
||||
limit_reason: null,
|
||||
max_edges_per_func: 200,
|
||||
per_func_capped: false,
|
||||
},
|
||||
];
|
||||
|
||||
export interface Upstream {
|
||||
/** Roots, besides the procedure itself, that the fake answers with a leaf graph; any other root is rejected. */
|
||||
readonly leafRoots?: readonly string[];
|
||||
/** Advertise the fake server's tools; omit for the default full set. */
|
||||
readonly tools?: readonly string[];
|
||||
/** Items the fake `callees` tool reports for the procedure. */
|
||||
readonly callees: readonly JsonValue[];
|
||||
/** Raw `callgraph` reply, or undefined when the tool is absent/failing. */
|
||||
readonly callgraph?: JsonValue;
|
||||
readonly callgraphFailure?: string;
|
||||
/** Make the fake transport report a truncated preview for the `callgraph` reply. */
|
||||
readonly callgraphTruncatedOutput?: boolean;
|
||||
}
|
||||
|
||||
export const callGraphSession = async (
|
||||
upstream: Upstream,
|
||||
architecture: "x86_64" | "arm64" = "arm64",
|
||||
available: readonly ("x86_64" | "arm64")[] = [architecture],
|
||||
) => {
|
||||
const { root, target } = await createIdaTarget(architecture, available);
|
||||
roots.push(root);
|
||||
const producer = new RecordingIdaMcp(target, "headless");
|
||||
if (upstream.tools !== undefined) producer.tools = upstream.tools;
|
||||
producer.beforeCall = async (name) => {
|
||||
if (name !== "callgraph") return;
|
||||
if (upstream.callgraphFailure !== undefined)
|
||||
decodeIdaToolResult({
|
||||
content: [{ type: "text", text: upstream.callgraphFailure }],
|
||||
isError: true,
|
||||
});
|
||||
if (upstream.callgraphTruncatedOutput === true)
|
||||
decodeIdaToolResult({
|
||||
content: [{ type: "text", text: "preview" }],
|
||||
_meta: { ida_mcp: { output_truncated: true } },
|
||||
});
|
||||
};
|
||||
producer.overrideCall = (name, args): JsonValue | undefined => {
|
||||
if (name === "lookup_funcs") {
|
||||
const queries = args.queries;
|
||||
const addresses = Array.isArray(queries) ? queries.map(String) : [];
|
||||
return addresses.map((query) => {
|
||||
const fn = FUNCTIONS[query];
|
||||
return fn === undefined
|
||||
? { query, fn: null, error: "Not found" }
|
||||
: { query, fn: { ...fn, size: "0x20" }, error: null };
|
||||
});
|
||||
}
|
||||
if (name === "callees")
|
||||
return [{ callees: [...upstream.callees], more: false }];
|
||||
if (name === "callgraph") {
|
||||
const requested = Array.isArray(args.roots) ? args.roots : [];
|
||||
const rootKnown =
|
||||
JSON.stringify(requested) === JSON.stringify(["0x1000"]) ||
|
||||
(requested.length === 1 &&
|
||||
upstream.leafRoots?.includes(String(requested[0])) === true);
|
||||
if (!rootKnown || args.max_depth !== 0)
|
||||
throw new Error(
|
||||
`Unexpected callgraph request: ${JSON.stringify(args)}`,
|
||||
);
|
||||
return String(requested[0]) === "0x1000"
|
||||
? upstream.callgraph
|
||||
: leafGraphRow(String(requested[0]));
|
||||
}
|
||||
return undefined;
|
||||
};
|
||||
const client = new IdaSessionClient(
|
||||
{
|
||||
command: "fixture",
|
||||
args: [],
|
||||
env: {},
|
||||
mode: "headless",
|
||||
timeoutMs: 1000,
|
||||
workspaceRoot: root,
|
||||
},
|
||||
target,
|
||||
producer,
|
||||
);
|
||||
return { client, producer };
|
||||
};
|
||||
Vendored
+16
-5
@@ -7,19 +7,27 @@ import type { JsonValue } from "../../src/domain/jsonValue.js";
|
||||
import type { IdaMcpConnection } from "../../src/ida/IdaMcpConnection.js";
|
||||
import { elf } from "../../src/domain/binaryTarget.fixture.js";
|
||||
|
||||
/** ELF machine numbers for the architectures a fixture target can declare. */
|
||||
const ELF_MACHINE = { x86_64: 62, arm64: 183 } as const;
|
||||
|
||||
/** Create a test-owned executable and workspace parent without invoking an engine. */
|
||||
export const createIdaTarget = async () => {
|
||||
export const createIdaTarget = async (
|
||||
architecture: keyof typeof ELF_MACHINE = "x86_64",
|
||||
availableArchitectures: readonly (keyof typeof ELF_MACHINE)[] = [
|
||||
architecture,
|
||||
],
|
||||
) => {
|
||||
const root = await mkdtemp(join(tmpdir(), "rea-ida-test-"));
|
||||
const path = join(root, "target.elf");
|
||||
const bytes = elf(2, 1, 62);
|
||||
const bytes = elf(2, 1, ELF_MACHINE[architecture]);
|
||||
await writeFile(path, bytes);
|
||||
const target: BinaryTarget = {
|
||||
path,
|
||||
sha256: createHash("sha256").update(bytes).digest("hex"),
|
||||
kind: "executable",
|
||||
format: "elf",
|
||||
architecture: "x86_64",
|
||||
availableArchitectures: ["x86_64"],
|
||||
architecture,
|
||||
availableArchitectures: [...availableArchitectures],
|
||||
};
|
||||
return { root, target };
|
||||
};
|
||||
@@ -48,6 +56,7 @@ const modernNames = [
|
||||
"disasm",
|
||||
"callees",
|
||||
"xrefs_to",
|
||||
"callgraph",
|
||||
];
|
||||
|
||||
/** Stateful producer fixture at the real connection seam; no module mocking. */
|
||||
@@ -82,9 +91,11 @@ export class RecordingIdaMcp implements IdaMcpConnection {
|
||||
this.inputSha256 = target.sha256;
|
||||
this.inputPath = target.path;
|
||||
}
|
||||
/** Tool names the fake server advertises; defaults to the profile's full set. */
|
||||
tools?: readonly string[];
|
||||
async connect() {
|
||||
this.connects += 1;
|
||||
return this.mode === "attached" ? legacyNames : modernNames;
|
||||
return this.tools ?? (this.mode === "attached" ? legacyNames : modernNames);
|
||||
}
|
||||
serverInfo() {
|
||||
return { name: "upstream-fixture", version: "sdk-version-is-not-ida" };
|
||||
|
||||
Reference in New Issue
Block a user