Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix(ghidra): declare observed function facets and call coverage
Fixes #2980
  • Loading branch information
RishTheFish21 committed Oct 13, 2026
commit 8e3a0975e8ba2bd9add2b6dcac20745bf16e5b2c
40 changes: 40 additions & 0 deletions bridge/ghidra/ReaGhidraBridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -1571,6 +1571,10 @@ private FunctionAnalysisObservation analyzeFunctionObservation(JsonObject params
!decompilation.getErrorMessage().isBlank()) {
limitations.add("Ghidra decompiler diagnostic: " + decompilation.getErrorMessage());
}
result.add(
"facet_availability",
functionFacetAvailability(decompiledText(decompilation) != null)
);
result.add("limitations", limitations);
return new FunctionAnalysisObservation(result, boundaryLimitations);
}
Expand Down Expand Up @@ -3545,6 +3549,42 @@ private JsonArray basicBlockValues(Function function) throws Exception {
return result;
}

/**
* Declare the dossier facets this bridge observed, so an empty facet means
* observed absence rather than an undeclared gap. Every facet except
* pseudocode is read from the complete function body (all instructions,
* comment types, reference-manager edges, call references and basic blocks).
*/
private static JsonObject functionFacetAvailability(boolean decompiled) {
JsonObject facets = new JsonObject();
JsonObject observed = new JsonObject();
observed.addProperty("available", true);
if (decompiled) {
facets.add("pseudocode", observed.deepCopy());
}
else {
JsonObject unavailable = new JsonObject();
unavailable.addProperty("available", false);
unavailable.addProperty(
"reason",
"Ghidra has no decompiled body for an external or bodyless procedure."
);
facets.add("pseudocode", unavailable);
}
for (String facet : List.of(
"assembly",
"comments",
"callers",
"callees",
"references",
"strings_names",
"cfg"
)) {
facets.add(facet, observed.deepCopy());
}
return facets;
}

private static JsonArray comments(List<Instruction> instructions) {
TreeMap<String, JsonObject> observed = new TreeMap<>();
for (Instruction instruction : instructions) {
Expand Down
6 changes: 5 additions & 1 deletion docs/mcp-contracts.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ coverage declaration is also unknown. A producer that actually observed an
empty facet can declare it available so observed absence remains comparable.
Call comparison additionally retains the Evidence relationship coverage
checks. IDA declares the facets its selected adapter cannot provide; an empty
unsupported array does not prove observed absence.
unsupported array does not prove observed absence. Ghidra reads every facet
from the complete function body and declares each one available, except
pseudocode for an external or bodyless procedure. Its `analyze_function`
Evidence reports the same call relationship coverage as `procedure_callers`
and `procedure_callees`.
Evidence and bundle imports reject unsupported fields in their envelopes,
identities, manifests and other fixed metadata before authentication. Imports
leave retained session state unchanged when validation fails. Keep application
Expand Down
6 changes: 4 additions & 2 deletions src/ghidra/GhidraAnalysisProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ export const resolveGhidraAnalysisProfile = async (
load_image_observations: "source-mappings-entry-context-v2",
function_body_evidence: "complete-inclusive-ranges-v1",
function_references: "complete-body-and-entry-reference-manager-v2",
call_relationship_coverage: "resolved-call-reference-diagnostics-v1",
call_relationship_coverage: "resolved-call-reference-diagnostics-v2",
function_facet_availability: "declared-observed-facets-v1",
string_inventory_evidence: "defined-data-coverage-v1",
location_resolution: "explicit-address-exact-entry-symbol-first-v3",
procedure_file_offset_resolution:
Expand Down Expand Up @@ -213,7 +214,8 @@ export const resolveGhidraAnalysisProfile = async (
load_image_observations: "source-mappings-entry-context-v2",
function_body_evidence: "complete-inclusive-ranges-v1",
function_references: "complete-body-and-entry-reference-manager-v2",
call_relationship_coverage: "resolved-call-reference-diagnostics-v1",
call_relationship_coverage: "resolved-call-reference-diagnostics-v2",
function_facet_availability: "declared-observed-facets-v1",
string_inventory_evidence: "defined-data-coverage-v1",
location_resolution: "explicit-address-exact-entry-symbol-first-v3",
procedure_file_offset_resolution:
Expand Down
56 changes: 55 additions & 1 deletion src/ghidra/GhidraProvider.behavior.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { GHIDRA_SESSION_CAPABILITIES } from "./GhidraSessionValues.js";
import { err, ok } from "../domain/result.js";
import type { Result } from "../domain/result.js";
import { GhidraSessionError } from "./GhidraSessionError.js";
import { ghidraFunctionDossier } from "../domain/ghidraValues.fixture.js";
import { silentLogger } from "../logger.js";
import * as WindowsNativeLoader from "../windows/WindowsNativeLoader.js";
import { WINDOWS_NATIVE_AUTHORITY_UNAVAILABLE_REASON } from "../process/WindowsAuthority.js";
Expand Down Expand Up @@ -627,7 +628,8 @@ describe("Ghidra client projection", () => {
expect(resolved.value.profile.parameters).toMatchObject({
language_id: "auto-from-header",
compiler_spec_id: "auto-default",
call_relationship_coverage: "resolved-call-reference-diagnostics-v1",
call_relationship_coverage: "resolved-call-reference-diagnostics-v2",
function_facet_availability: "declared-observed-facets-v1",
});

const result = await ghidra
Expand Down Expand Up @@ -670,6 +672,58 @@ describe("Ghidra client projection", () => {
});
});

describe("Ghidra analyze_function coverage", () => {
it("reports call coverage and declared facets for analyze_function", async () => {
const dossier = ghidraFunctionDossier();
if (
typeof dossier !== "object" ||
dossier === null ||
Array.isArray(dossier)
)
throw new TypeError("Ghidra dossier fixture is invalid");
const facets = {
pseudocode: { available: true },
assembly: { available: true },
comments: { available: true },
callers: { available: true },
callees: { available: true },
references: { available: true },
strings_names: { available: true },
cfg: { available: true },
};
const ghidra = provider(installationHost(), () => ({
start: () => Promise.resolve(ok(sessionInfo())),
// analyze_function arrives in the bridge's observation envelope.
callTool: () =>
Promise.resolve(
ok({
value: { ...dossier, comments: [], facet_availability: facets },
limitations: [],
}),
),
close: () => Promise.resolve(ok(null)),
Comment on lines +698 to +704

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Bridge changes escape the test

The mock supplies facet_availability, so this test only checks that the provider preserves it. Removing the Java declaration would leave the test passing.

Add bridge-level coverage for an ordinary function and an external or bodyless function, including the unavailable pseudocode branch. That would catch regressions in the producer this PR changes.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/ghidra/GhidraProvider.behavior.test.ts
Line: 698-704

Comment:
**Bridge changes escape the test**

The mock supplies `facet_availability`, so this test only checks that the provider preserves it. Removing the Java declaration would leave the test passing.

Add bridge-level coverage for an ordinary function and an external or bodyless function, including the unavailable `pseudocode` branch. That would catch regressions in the producer this PR changes.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

}));
const client = await createElfClient(ghidra);
const execution = await client.execute("analyze_function", {
procedure: "fixture_main",
});
expect(execution.ok).toBe(true);
if (!execution.ok) return;
// Callers and callees come from the same call references as
// procedure_callers/procedure_callees, so they carry the same coverage.
expect(execution.value.relationshipCoverage).toEqual({
status: "complete",
diagnostics: [
expect.stringContaining("Only resolved Ghidra call references"),
],
});
expect(execution.value.result).toMatchObject({
comments: [],
facet_availability: facets,
});
});
});

describe("Ghidra result projection", () => {
it("rejects malformed inventory output before Evidence creation", async () => {
const ghidra = provider(installationHost(), () => ({
Expand Down
10 changes: 8 additions & 2 deletions src/ghidra/GhidraProviderCapabilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,17 @@ const searchLimitation =
const resolvedCallReferenceLimitation =
"Only resolved Ghidra call references are returned; unresolved computed or indirect calls remain unknown, while function classifications distinguish thunks and externals.";

/** Relationship-only caveats belong to each observed caller/callee result. */
/**
* Relationship-only caveats belong to each observed caller/callee result.
* analyze_function reads its callers and callees from the same Ghidra
* FunctionManager call references as procedure_callers/procedure_callees.
*/
export const relationshipCoverageFor = (
operation: string,
): RelationshipCoverage | undefined =>
operation === "procedure_callers" || operation === "procedure_callees"
operation === "procedure_callers" ||
operation === "procedure_callees" ||
operation === "analyze_function"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Unresolved calls look unchanged

With complete coverage for analyze_function, functions with unresolved indirect calls compare as unchanged in the calls dimension when their resolved callers and callees match. analyzeFunctionObservation never adds unresolved_calls, and functionCallCoverageLimitations ignores diagnostics for complete coverage.

Share the unresolved-call collector from procedureReferences with analyze_function and include those sites in the dossier. This keeps unresolved calls unknown while allowing fully observed functions to compare normally.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/ghidra/GhidraProviderCapabilities.ts
Line: 60

Comment:
**Unresolved calls look unchanged**

With complete coverage for `analyze_function`, functions with unresolved indirect calls compare as `unchanged` in the `calls` dimension when their resolved callers and callees match. `analyzeFunctionObservation` never adds `unresolved_calls`, and `functionCallCoverageLimitations` ignores diagnostics for complete coverage.

Share the unresolved-call collector from `procedureReferences` with `analyze_function` and include those sites in the dossier. This keeps unresolved calls `unknown` while allowing fully observed functions to compare normally.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

? {
status: "complete",
diagnostics: [resolvedCallReferenceLimitation],
Comment on lines +60 to 63

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Do not mark incomplete analyzed call sets complete

When the analyzed function contains a targetless indirect/computed call, this new branch emits status: "complete" even though analyzeFunctionObservation only serializes the resolved getCallingFunctions/getCalledFunctions sets and never fills unresolved_calls. Because functionCallCoverageLimitations ignores diagnostics on complete coverage, compare_functions can label that incomplete call set unchanged (and call-path consumers can treat it as exhaustive); emit the unresolved sites or downgrade this coverage instead.

AGENTS.md reference: AGENTS.md:L19-L19

Useful? React with 👍 / 👎.

Expand Down
Loading