From 549effa056729e5f9239d9ed7864537a9c49e421 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Fri, 9 Oct 2026 03:54:02 +0800 Subject: [PATCH] fix(ghidra): resolve exact external function entries --- docs/native-investigation.md | 3 ++ docs/testing.md | 2 + scripts/lib/real-ghidra-boundaries.mjs | 47 +++++++++++++++++++ src/ghidra/GhidraInventoryValues.test.ts | 23 --------- src/ghidra/GhidraInventoryValues.ts | 4 ++ src/ghidra/GhidraProviderCapabilities.ts | 5 ++ tests/boundary/mcp/ghidraInventoryMcp.test.ts | 23 +++++++++ 7 files changed, 84 insertions(+), 23 deletions(-) diff --git a/docs/native-investigation.md b/docs/native-investigation.md index f394573f0..efa6e6253 100644 --- a/docs/native-investigation.md +++ b/docs/native-investigation.md @@ -82,6 +82,9 @@ by the inventory, including their namespaces and any platform symbol prefix. Overloads can share a fully qualified name; ambiguity errors return every matching entry address so the caller can select the intended function directly. +`resolve_containing_procedure` also resolves an exact external entry. Its empty +body remains explicit; nearby external addresses do not inherit that identity. + Ghidra function references cover the complete function-body AddressSet and its exact entry, including references into instruction interiors and references from embedded data. Addresses inside an enclosing span but outside the owned diff --git a/docs/testing.md b/docs/testing.md index 4c14a0c17..ebebdb4c1 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -280,6 +280,8 @@ xrefs, raw procedure references, and CLI/MCP dossiers must retain that data edge A valid legacy snapshot reconstructs the former omitted edge under its older profile; CLI and MCP must reject that binding with a mismatch reason and recovery advice. The rejected open must preserve the active live session. +Exact external entries must resolve while retaining an empty body; unknown +external addresses remain unresolved and external annotations are rejected. Malformed annotation readback, memory completeness, and inventory data remain separate SDK/provider integration cases; success from a real provider cannot establish rejection of a contradictory provider response. diff --git a/scripts/lib/real-ghidra-boundaries.mjs b/scripts/lib/real-ghidra-boundaries.mjs index c89e4533f..98d2f1085 100644 --- a/scripts/lib/real-ghidra-boundaries.mjs +++ b/scripts/lib/real-ghidra-boundaries.mjs @@ -95,6 +95,52 @@ export async function verifyGhidraBoundaries( assert.equal((await stat(socketRoot)).mode & 0o777, 0o700); const address = original.procedure.address; const name = original.procedure.name; + const procedures = await call("list_procedures"); + const external = procedures.find((item) => item.procedure.external); + assert.ok(external, "Source fixture lacks an external function"); + const externalDossier = await call("analyze_function", { + procedure: external.address, + }); + const externalEntry = await call("resolve_containing_procedure", { + address: external.address, + }); + assert.equal(externalEntry.found, true); + assert.equal(externalEntry.procedure.address, external.address); + assert.equal(externalEntry.procedure.classification.external, true); + assert.deepEqual( + externalEntry.procedure.body, + externalDossier.procedure.body, + ); + assert.equal(externalEntry.procedure.body.contains_entry, false); + assert.deepEqual(externalEntry.procedure.body.ranges, []); + const externalSpace = external.address.slice( + 0, + external.address.indexOf(":"), + ); + const lastExternalOffset = procedures + .filter((item) => item.procedure.external) + .map((item) => BigInt(item.address.slice(item.address.indexOf(":") + 1))) + .reduce((maximum, offset) => (offset > maximum ? offset : maximum), 0n); + const unknownExternal = `${externalSpace}:0x${(lastExternalOffset + 1n).toString(16)}`; + assert.deepEqual( + await call("resolve_containing_procedure", { address: unknownExternal }), + { + query_address: unknownExternal, + found: false, + procedure: null, + reason: "outside_segments", + }, + ); + assert.deepEqual( + (await cli("function", external.address)).procedure, + externalDossier.procedure, + "CLI and MCP must preserve external identity without inventing body bytes", + ); + await invalid( + "annotate_native_function", + { procedure: external.address, name: "rea_external_edit" }, + /Annotations require a local function entry/u, + ); const names = await call("list_names"); const leaf = names.find((item) => item.value.endsWith("rea_ghidra_inventory_leaf"), @@ -550,6 +596,7 @@ export async function verifyGhidraBoundaries( mutation_rollback: true, annotation_native_text_validation: true, lossless_unicode_transport: true, + exact_external_entry_resolution: true, complete_function_body_references: true, ambiguity_candidates_inline: true, legacy_reference_snapshot_rejected: true, diff --git a/src/ghidra/GhidraInventoryValues.test.ts b/src/ghidra/GhidraInventoryValues.test.ts index 8a882e27b..4d441ec39 100644 --- a/src/ghidra/GhidraInventoryValues.test.ts +++ b/src/ghidra/GhidraInventoryValues.test.ts @@ -31,18 +31,6 @@ describe("containing-procedure complete body evidence", () => { found: true, procedure: identity(), }); - it("accepts the Java producer identity including complete inclusive body ranges", () => { - const value = found(); - expect( - parseGhidraInventoryResult("resolve_containing_procedure", value), - ).toEqual({ ok: true, value }); - expect( - parseGhidraInventoryResult("resolve_containing_procedure", { - ...value, - query_address: "0x1021", - }).ok, - ).toBe(true); - }); it("rejects a missing body rather than treating a legacy identity as complete", () => { const { body: omitted, ...legacy } = identity(); expect(omitted.total_bytes).toBe(5); @@ -108,15 +96,4 @@ describe("containing-procedure complete body evidence", () => { }).ok, ).toBe(false); }); - it("retains explicit not-found outcomes without fabricating a body", () => { - const value = { - query_address: "0x1010", - found: false, - procedure: null, - reason: "not_in_procedure", - }; - expect( - parseGhidraInventoryResult("resolve_containing_procedure", value), - ).toEqual({ ok: true, value }); - }); }); diff --git a/src/ghidra/GhidraInventoryValues.ts b/src/ghidra/GhidraInventoryValues.ts index 1ed9d4809..ebb4c30cc 100644 --- a/src/ghidra/GhidraInventoryValues.ts +++ b/src/ghidra/GhidraInventoryValues.ts @@ -219,7 +219,11 @@ const containingProcedure = z.discriminatedUnion("found", [ .strict() .superRefine((value, context) => { const body = value.procedure.body; + const exactExternalEntry = + value.procedure.classification.external && + value.query_address === value.procedure.address; if ( + !exactExternalEntry && body.available && !functionBodyEntryAgrees( { ...body, contains_entry: true }, diff --git a/src/ghidra/GhidraProviderCapabilities.ts b/src/ghidra/GhidraProviderCapabilities.ts index a0f246d3a..28ddff247 100644 --- a/src/ghidra/GhidraProviderCapabilities.ts +++ b/src/ghidra/GhidraProviderCapabilities.ts @@ -85,6 +85,11 @@ export const limitationsFor = (operation: string): readonly string[] => { ...common, "Only Ghidra-defined string Data is observed; charset is reported, while a non-missing terminator cannot distinguish a present terminator from a fixed or Pascal layout.", ]; + case "resolve_containing_procedure": + return [ + ...common, + "An exact external entry resolves to its observed function identity even when its body is empty. This does not establish mapped executable bytes or containment of nearby external addresses.", + ]; case "list_segments": return [ ...common, diff --git a/tests/boundary/mcp/ghidraInventoryMcp.test.ts b/tests/boundary/mcp/ghidraInventoryMcp.test.ts index 0c6a660ec..3a484cae1 100644 --- a/tests/boundary/mcp/ghidraInventoryMcp.test.ts +++ b/tests/boundary/mcp/ghidraInventoryMcp.test.ts @@ -67,6 +67,29 @@ it("rejects contradictory provider output before emitting MCP Evidence", async ( limitations: ["Ghidra-specific instruction text."], }, }, + { + name: "resolve_containing_procedure", + arguments: { address: "EXTERNAL:0x2" }, + output: { + query_address: "EXTERNAL:0x2", + found: true, + procedure: { + ...ghidraFunctionIdentity(), + address: "EXTERNAL:0x1", + classification: { + ...ghidraFunctionIdentity().classification, + external: true, + }, + body: { + ...ghidraFunctionIdentity().body, + ranges: [], + total_bytes: 0, + span_bytes: 0, + contains_entry: false, + }, + }, + }, + }, ]; for (const probe of cases) { output = jsonValueSchema.parse(probe.output);