fix(ida): report a procedure IDA cannot resolve as invalid input (#1785)
* test(ida): expect a missing procedure to be reported as invalid input * test(ida): name the invalid_request code and its category for a missing procedure * fix(ida): report a procedure IDA cannot resolve as invalid input * test(ida): pin the missing-procedure issue shape and the lookups that stay unreadable * fix(ida): treat a lookup answer for another query as a producer fault * test(ida): expect an argument-independent issue path and pin legacy error replies and escaping * fix(ida): report a missing procedure without naming an argument path * test(ida): expect provider-derived selectors to stay producer faults in composed workflows * fix(ida): keep provider-derived procedure selectors out of the caller's input errors * test(ida): cover session forwarding, parameter hygiene and structured derived-node errors * docs(ida): describe when procedure resolution blames the caller
This commit is contained in:
@@ -5,6 +5,9 @@ import {
|
||||
RecordingIdaMcp,
|
||||
} from "../../tests/fixtures/idaMcp.js";
|
||||
import type { JsonValue } from "../domain/jsonValue.js";
|
||||
import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
|
||||
import { AnalysisInputError } from "../domain/analysisErrorCore.js";
|
||||
import { decodeIdaToolResult } from "./IdaMcpConnection.js";
|
||||
import type { AnalysisOperation } from "../application/AnalysisProvider.js";
|
||||
import { IdaSessionClient } from "./IdaSessionClient.js";
|
||||
|
||||
@@ -96,3 +99,290 @@ describe("headless IDA operation failures", () => {
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
const lookup = (query: string, fn: JsonValue, error: string | null) => [
|
||||
{ query, fn, error },
|
||||
];
|
||||
const knownFunction = { addr: "0x1000", name: "main", size: "0x20" };
|
||||
|
||||
const missingProcedureFixture = async (
|
||||
answer: (query: string) => JsonValue | undefined,
|
||||
) => {
|
||||
const { root, target } = await createIdaTarget();
|
||||
roots.push(root);
|
||||
const producer = new RecordingIdaMcp(target, "headless");
|
||||
producer.overrideCall = (name, args) => {
|
||||
if (name !== "lookup_funcs") return undefined;
|
||||
const queries = args.queries;
|
||||
const query = Array.isArray(queries) ? String(queries[0]) : "";
|
||||
return answer(query);
|
||||
};
|
||||
const client = new IdaSessionClient(
|
||||
{
|
||||
command: "fixture",
|
||||
args: [],
|
||||
env: {},
|
||||
mode: "headless",
|
||||
timeoutMs: 1000,
|
||||
workspaceRoot: root,
|
||||
},
|
||||
target,
|
||||
producer,
|
||||
);
|
||||
return client;
|
||||
};
|
||||
|
||||
describe("headless IDA procedure lookup failures", () => {
|
||||
it.each([
|
||||
["Not found", "procedure_pseudo_code", "main"],
|
||||
["Not found", "procedure_address", "main"],
|
||||
["Not found", "analyze_function", "main"],
|
||||
["Not a function", "procedure_pseudo_code", "main"],
|
||||
["Not a function", "read_function_instructions", "main"],
|
||||
["Not a function", "procedure_pseudo_code", "0x1000"],
|
||||
["Not found", "procedure_assembly", "_missing"],
|
||||
["Not found", "procedure_pseudo_code", "main\nsecond line"],
|
||||
] as const)(
|
||||
"reports the upstream %s answer for %s of %s as an invalid request naming the procedure",
|
||||
async (upstream, operation, procedure) => {
|
||||
const client = await missingProcedureFixture((query) =>
|
||||
lookup(query, null, upstream),
|
||||
);
|
||||
try {
|
||||
const result = await client.execute(operation, { procedure });
|
||||
|
||||
if (result.ok) throw new Error("expected invalid input");
|
||||
expect(result.error._tag, "the caller's procedure is the fault").toBe(
|
||||
"AnalysisInputError",
|
||||
);
|
||||
const projected = projectAnalysisError(result.error);
|
||||
expect(projected.code, "the public code is invalid_request").toBe(
|
||||
"invalid_request",
|
||||
);
|
||||
expect(projected.category, "the category is invalid_input").toBe(
|
||||
"invalid_input",
|
||||
);
|
||||
expect(projected.retryable, "the caller can correct and retry").toBe(
|
||||
true,
|
||||
);
|
||||
expect(
|
||||
projected.details,
|
||||
"the operation and exactly one issue that names the procedure in its message",
|
||||
).toEqual({
|
||||
operation,
|
||||
issues: [
|
||||
{
|
||||
path: [],
|
||||
reason: "invalid_value",
|
||||
message: `IDA could not resolve procedure ${JSON.stringify(procedure)}: ${upstream}.`,
|
||||
},
|
||||
],
|
||||
});
|
||||
if (!(result.error instanceof AnalysisInputError))
|
||||
throw new Error("expected an AnalysisInputError");
|
||||
expect(
|
||||
result.error.issues.map(({ message }) => message ?? "").join(""),
|
||||
"a control character in the query is carried JSON-escaped, never raw",
|
||||
).not.toContain("\n");
|
||||
expect(
|
||||
(result.error.cause as Error | undefined)?.message,
|
||||
"the upstream text stays on the retained cause",
|
||||
).toContain(upstream);
|
||||
expect(
|
||||
JSON.stringify(projected).toLowerCase(),
|
||||
"no retry or doctor advice for a caller mistake",
|
||||
).not.toMatch(/doctor|retry once/u);
|
||||
} finally {
|
||||
expect((await client.close()).ok).toBe(true);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
it.each([
|
||||
["an upstream exception text", lookup("main", null, "Database is closed")],
|
||||
["a function with an error", lookup("main", knownFunction, "Not found")],
|
||||
["neither a function nor an error", lookup("main", null, null)],
|
||||
[
|
||||
"a near-miss spelling of the not-found text",
|
||||
lookup("main", null, "not found"),
|
||||
],
|
||||
[
|
||||
"an exception text that merely contains the not-found text",
|
||||
lookup("main", null, "Database lookup failed: Not found"),
|
||||
],
|
||||
["a padded not-found text", lookup("main", null, " Not found ")],
|
||||
["an empty error", lookup("main", null, "")],
|
||||
["a whitespace-only error", lookup("main", null, " ")],
|
||||
["an answer for a different query", lookup("helper", null, "Not found")],
|
||||
])("keeps %s as unreadable output", async (_label, answer) => {
|
||||
const client = await missingProcedureFixture(() => answer);
|
||||
try {
|
||||
const result = await client.execute("procedure_pseudo_code", {
|
||||
procedure: "main",
|
||||
});
|
||||
|
||||
if (result.ok) throw new Error("expected unreadable output");
|
||||
expect(result.error._tag, "a producer fault, not caller input").toBe(
|
||||
"AnalysisProtocolError",
|
||||
);
|
||||
expect(
|
||||
projectAnalysisError(result.error).code,
|
||||
"unknown beats a wrong fact about the caller's input",
|
||||
).toBe("unreadable_output");
|
||||
} finally {
|
||||
expect((await client.close()).ok).toBe(true);
|
||||
}
|
||||
});
|
||||
|
||||
it("keeps an unresolvable callee reported by IDA itself as unreadable output", async () => {
|
||||
const { root, target } = await createIdaTarget();
|
||||
roots.push(root);
|
||||
const producer = new RecordingIdaMcp(target, "headless");
|
||||
producer.overrideCall = (name, args) => {
|
||||
if (name === "callees")
|
||||
return [
|
||||
{
|
||||
callees: [{ addr: "0x2000", name: "sub_2000", type: "internal" }],
|
||||
more: false,
|
||||
},
|
||||
];
|
||||
if (name !== "lookup_funcs") return undefined;
|
||||
const queries = args.queries;
|
||||
const query = Array.isArray(queries) ? String(queries[0]) : "";
|
||||
return query === "0x2000"
|
||||
? lookup(query, null, "Not a function")
|
||||
: lookup(query, knownFunction, null);
|
||||
};
|
||||
const client = new IdaSessionClient(
|
||||
{
|
||||
command: "fixture",
|
||||
args: [],
|
||||
env: {},
|
||||
mode: "headless",
|
||||
timeoutMs: 1000,
|
||||
workspaceRoot: root,
|
||||
},
|
||||
target,
|
||||
producer,
|
||||
);
|
||||
try {
|
||||
const result = await client.execute("procedure_callees", {
|
||||
procedure: "main",
|
||||
});
|
||||
|
||||
if (result.ok) throw new Error("expected unreadable output");
|
||||
expect(
|
||||
projectAnalysisError(result.error).code,
|
||||
"an address IDA itself reported is not the caller's mistake",
|
||||
).toBe("unreadable_output");
|
||||
} finally {
|
||||
expect((await client.close()).ok).toBe(true);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("provider-derived procedure selectors", () => {
|
||||
it.each(["Not found", "Not a function"])(
|
||||
"reports a derived selector answered %s as a producer fault",
|
||||
async (upstream) => {
|
||||
const client = await missingProcedureFixture((query) =>
|
||||
lookup(query, null, upstream),
|
||||
);
|
||||
try {
|
||||
const result = await client.execute(
|
||||
"procedure_callees",
|
||||
{ procedure: "0x2000" },
|
||||
{ selectorOrigin: "provider" },
|
||||
);
|
||||
|
||||
if (result.ok) throw new Error("expected a producer fault");
|
||||
expect(result.error._tag, "not the caller's mistake").toBe(
|
||||
"AnalysisProtocolError",
|
||||
);
|
||||
expect(
|
||||
projectAnalysisError(result.error).code,
|
||||
"projected as on main",
|
||||
).toBe("unreadable_output");
|
||||
} finally {
|
||||
expect((await client.close()).ok).toBe(true);
|
||||
}
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe("attached IDA procedure lookup failures", () => {
|
||||
it("leaves a failed legacy lookup as unreadable output", async () => {
|
||||
const { root, target } = await createIdaTarget();
|
||||
roots.push(root);
|
||||
const producer = new RecordingIdaMcp(target, "attached");
|
||||
producer.overrideCall = (name) =>
|
||||
name === "get_function_by_name" ? null : undefined;
|
||||
const client = new IdaSessionClient(
|
||||
{
|
||||
command: "fixture",
|
||||
args: [],
|
||||
env: {},
|
||||
mode: "attached",
|
||||
timeoutMs: 1000,
|
||||
workspaceRoot: root,
|
||||
},
|
||||
target,
|
||||
producer,
|
||||
);
|
||||
try {
|
||||
const result = await client.execute("procedure_pseudo_code", {
|
||||
procedure: "main",
|
||||
});
|
||||
|
||||
if (result.ok) throw new Error("expected unreadable output");
|
||||
expect(
|
||||
projectAnalysisError(result.error).code,
|
||||
"the legacy profile keeps its pre-existing classification",
|
||||
).toBe("unreadable_output");
|
||||
} finally {
|
||||
expect((await client.close()).ok).toBe(true);
|
||||
}
|
||||
});
|
||||
|
||||
it("leaves an MCP error reply for an unknown legacy name as unreadable output", async () => {
|
||||
const { root, target } = await createIdaTarget();
|
||||
roots.push(root);
|
||||
const producer = new RecordingIdaMcp(target, "attached");
|
||||
producer.beforeCall = async (name) => {
|
||||
if (name === "get_function_by_name")
|
||||
decodeIdaToolResult({
|
||||
content: [{ type: "text", text: "Function not found: main" }],
|
||||
isError: true,
|
||||
});
|
||||
};
|
||||
const client = new IdaSessionClient(
|
||||
{
|
||||
command: "fixture",
|
||||
args: [],
|
||||
env: {},
|
||||
mode: "attached",
|
||||
timeoutMs: 1000,
|
||||
workspaceRoot: root,
|
||||
},
|
||||
target,
|
||||
producer,
|
||||
);
|
||||
try {
|
||||
const result = await client.execute("procedure_pseudo_code", {
|
||||
procedure: "main",
|
||||
});
|
||||
|
||||
if (result.ok) throw new Error("expected unreadable output");
|
||||
expect(
|
||||
result.error._tag,
|
||||
"a producer error reply, not caller input",
|
||||
).toBe("AnalysisProtocolError");
|
||||
expect(
|
||||
projectAnalysisError(result.error).code,
|
||||
"the legacy profile keeps its existing classification",
|
||||
).toBe("unreadable_output");
|
||||
} finally {
|
||||
expect((await client.close()).ok).toBe(true);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -51,6 +51,11 @@ const identity = (fn: IdaFunction) => ({
|
||||
classification: null,
|
||||
body: unknownBody,
|
||||
});
|
||||
/** The only lookup answers upstream reports for a name or address that is not a function. */
|
||||
const MISSING_PROCEDURE_ANSWERS: ReadonlySet<string> = new Set([
|
||||
"Not found",
|
||||
"Not a function",
|
||||
]);
|
||||
const lines = (asm: IdaDisassembly) =>
|
||||
asm.lines.map(({ address, instruction }) => `${address}: ${instruction}`);
|
||||
|
||||
@@ -64,6 +69,7 @@ export class IdaOperationRunner {
|
||||
private readonly protocol: "legacy" | "modern",
|
||||
private readonly database?: string,
|
||||
private readonly signal?: AbortSignal,
|
||||
private readonly selectorOrigin?: "provider",
|
||||
) {}
|
||||
|
||||
async call(
|
||||
@@ -106,11 +112,42 @@ export class IdaOperationRunner {
|
||||
return result;
|
||||
}
|
||||
|
||||
async resolve(query: string): Promise<IdaFunction> {
|
||||
/**
|
||||
* Resolves a procedure. When `requestedBy` names the caller's operation, an
|
||||
* upstream "not found" or "not a function" answer is reported as invalid
|
||||
* input; every other failed lookup stays a producer fault. `run()` passes
|
||||
* `requestedBy` only for caller-supplied selectors. Provider-derived
|
||||
* selectors, such as the addresses composed workflows revisit, and the
|
||||
* callee resolution inside `relationships()` pass none, because those
|
||||
* addresses were reported by IDA itself.
|
||||
*/
|
||||
async resolve(query: string, requestedBy?: string): Promise<IdaFunction> {
|
||||
if (this.protocol === "modern") {
|
||||
const item = modernLookupSchema.parse(
|
||||
await this.call("lookup_funcs", { queries: [query] }),
|
||||
)[0];
|
||||
if (
|
||||
requestedBy !== undefined &&
|
||||
item?.query === query &&
|
||||
item.fn === null &&
|
||||
item.error !== null &&
|
||||
MISSING_PROCEDURE_ANSWERS.has(item.error)
|
||||
)
|
||||
throw new AnalysisInputError(
|
||||
requestedBy,
|
||||
{
|
||||
cause: new AnalysisProtocolError(
|
||||
`IDA function lookup failed for ${query}: ${item.error}`,
|
||||
),
|
||||
},
|
||||
[
|
||||
{
|
||||
path: [],
|
||||
reason: "invalid_value",
|
||||
message: `IDA could not resolve procedure ${JSON.stringify(query)}: ${item.error}.`,
|
||||
},
|
||||
],
|
||||
);
|
||||
if (item?.fn == null || item.error !== null)
|
||||
throw new AnalysisProtocolError(
|
||||
`IDA function lookup failed for ${query}: ${item?.error ?? "missing function"}`,
|
||||
@@ -351,6 +388,7 @@ export class IdaOperationRunner {
|
||||
}
|
||||
const fn = await this.resolve(
|
||||
z.string().min(1).parse(parameters.procedure),
|
||||
this.selectorOrigin === "provider" ? undefined : operation,
|
||||
);
|
||||
this.locations.push({ kind: "address", address: fn.address });
|
||||
switch (operation) {
|
||||
|
||||
@@ -192,12 +192,7 @@ export class IdaSessionClient implements AnalysisClient {
|
||||
throw new AnalysisProtocolError(
|
||||
"IDA operation changed after validation.",
|
||||
);
|
||||
const runner = new IdaOperationRunner(
|
||||
this.connection,
|
||||
this.config.mode === "attached" ? "legacy" : "modern",
|
||||
this.#database,
|
||||
options?.signal,
|
||||
);
|
||||
const runner = this.#runner(options);
|
||||
const result = await runner.run(operation, parameters);
|
||||
if (wasCancelled(options))
|
||||
return err(new AnalysisCancelledError(operation));
|
||||
@@ -227,6 +222,16 @@ export class IdaSessionClient implements AnalysisClient {
|
||||
}
|
||||
}
|
||||
|
||||
#runner(options: ExecutionOptions | undefined): IdaOperationRunner {
|
||||
return new IdaOperationRunner(
|
||||
this.connection,
|
||||
this.config.mode === "attached" ? "legacy" : "modern",
|
||||
this.#database,
|
||||
options?.signal,
|
||||
options?.selectorOrigin,
|
||||
);
|
||||
}
|
||||
|
||||
#health(metadata: JsonValue) {
|
||||
return ok(
|
||||
createAnalysisExecution(
|
||||
|
||||
Reference in New Issue
Block a user