From f559766c551fd630966dfad0c3d908fcc1f209ce Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Tue, 1 Dec 2026 01:35:57 -0500 Subject: [PATCH] fix(ida): list each xref source address once (#15441) * fix(ida): list each xref source address once * test(ida): expect source addresses from the runner's xrefs read --- src/ida/IdaOperationRunner.test.ts | 10 +---- src/ida/IdaOperationRunner.ts | 16 +++---- .../ida/xrefsSourceAddresses.test.ts | 44 +++++++++++++++++++ 3 files changed, 51 insertions(+), 19 deletions(-) create mode 100644 tests/boundary/providers/ida/xrefsSourceAddresses.test.ts diff --git a/src/ida/IdaOperationRunner.test.ts b/src/ida/IdaOperationRunner.test.ts index 535069cc861..0b8ed30949d 100644 --- a/src/ida/IdaOperationRunner.test.ts +++ b/src/ida/IdaOperationRunner.test.ts @@ -59,7 +59,7 @@ it("rejects file coordinates before any symbol lookup", async () => { }); }); -it("projects the native xref source function and retains unused span evidence", async () => { +it("reads native xref rows as source addresses and retains the raw response", async () => { const producer = new RecordingIdaMcp(target, "attached"); const runner = new IdaOperationRunner(producer, "native"); const response = [ @@ -77,13 +77,7 @@ it("projects the native xref source function and retains unused span evidence", ]; producer.overrideCall = (tool) => tool === "xrefs_to" ? response : undefined; - expect(await runner.xrefs("0x1000")).toEqual([ - { - address: "0x1004", - type: "code", - function: { address: "0x1000", name: "main" }, - }, - ]); + expect(await runner.xrefs("0x1000")).toEqual(["0x1004"]); expect(runner.raw).toEqual([ { tool: "xrefs_to", diff --git a/src/ida/IdaOperationRunner.ts b/src/ida/IdaOperationRunner.ts index 9a5ddfb1a77..be0670e8b1c 100644 --- a/src/ida/IdaOperationRunner.ts +++ b/src/ida/IdaOperationRunner.ts @@ -447,11 +447,8 @@ export class IdaOperationRunner { return result.join("\n"); } - async xrefs( - address: string, - ): Promise< - { address: string; type: string; function: IdaFunction | null }[] - > { + /** Source addresses referencing `address`, each listed once. */ + async xrefs(address: string): Promise { const page = ( this.protocol === "native" ? nativeXrefsSchema : modernXrefsSchema ).parse(await this.call("xrefs_to", { addrs: [address], limit: 1000 }))[0]; @@ -464,11 +461,8 @@ export class IdaOperationRunner { 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, - })); + // Upstream rows are per (source, kind), so one source can appear as both code and data. + return [...new Set(page.xrefs.map(({ addr }) => addr))]; } async relationships( @@ -666,7 +660,7 @@ export class IdaOperationRunner { if (operation === "xrefs") { const inspectedAddress = idaAddressSchema.parse(parameters.address); this.locations.push({ kind: "address", address: inspectedAddress }); - return (await this.xrefs(inspectedAddress)).map(({ address }) => address); + return this.xrefs(inspectedAddress); } const fn = await this.resolve( z.string().min(1).parse(parameters.procedure), diff --git a/tests/boundary/providers/ida/xrefsSourceAddresses.test.ts b/tests/boundary/providers/ida/xrefsSourceAddresses.test.ts new file mode 100644 index 00000000000..780c78e337d --- /dev/null +++ b/tests/boundary/providers/ida/xrefsSourceAddresses.test.ts @@ -0,0 +1,44 @@ +import { afterEach, expect, it } from "vitest"; +import { EnhancedTools } from "../../../../src/application/EnhancedTools.js"; +import { + callGraphSession, + removeCallGraphSessions, +} from "../../../fixtures/idaCallGraph.js"; + +afterEach(removeCallGraphSessions); + +it("lists and counts each IDA xref source address once when it holds both code and data references", async () => { + const { client, producer } = await callGraphSession({ callees: [] }); + const fixtureCall = producer.overrideCall; + producer.overrideCall = (name, args) => + name === "xrefs_to" + ? [ + { + xrefs: [ + { addr: "0x2000", type: "code", fn: null }, + { addr: "0x2000", type: "data", fn: null }, + { addr: "0x3000", type: "code", fn: null }, + ], + more: false, + }, + ] + : fixtureCall?.(name, args); + try { + const direct = await client.execute("xrefs", { address: "0x1000" }); + if (!direct.ok) throw direct.error; + expect(direct.value.result).toEqual(["0x2000", "0x3000"]); + + const paged = await new EnhancedTools(client).executeValidated({ + name: "find_xrefs_to_name", + input: { name: "main", limit: 10 }, + }); + if (!paged.ok) throw paged.error; + expect(paged.value).toMatchObject({ + xrefs: ["0x2000", "0x3000"], + observed_xrefs: 2, + total_xrefs: 2, + }); + } finally { + await client.close(); + } +});