From 441aa170d37381fac61cb87cd1c2d7302e4d6e12 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Sun, 15 Nov 2026 12:33:55 +0800 Subject: [PATCH] fix(ghidra): name the failing fields when a bridge reply misses its contract (#10749) --- src/ghidra/GhidraFunctionValues.test.ts | 23 +++++++++++++++++++++++ src/ghidra/GhidraFunctionValues.ts | 16 ++++++++++++++-- src/ghidra/GhidraInventoryValues.test.ts | 20 ++++++++++++++++++++ src/ghidra/GhidraInventoryValues.ts | 10 ++++++---- 4 files changed, 63 insertions(+), 6 deletions(-) diff --git a/src/ghidra/GhidraFunctionValues.test.ts b/src/ghidra/GhidraFunctionValues.test.ts index fadb0f93a9..c45974d121 100644 --- a/src/ghidra/GhidraFunctionValues.test.ts +++ b/src/ghidra/GhidraFunctionValues.test.ts @@ -44,6 +44,29 @@ const parseFunctionValueFixture = ( return parsed.ok ? { ok: true as const, value: parsed.value.value } : parsed; }; +it("names the failing fields when a function reply or its envelope misses its contract", () => { + const dossier = ghidraFunctionDossier() as Record; + const envelope = parseGhidraFunctionResult("analyze_function", { + value: dossier, + limitations: "none", + }); + expect(envelope.ok ? undefined : envelope.error.diagnostics).toMatchObject({ + contract_issues: expect.arrayContaining([ + expect.objectContaining({ path: ["limitations"], code: "invalid_type" }), + ]), + }); + + const reply = parseGhidraFunctionResult("analyze_function", { + value: { ...dossier, pseudocode: 7 }, + limitations: [], + }); + expect(reply.ok ? undefined : reply.error.diagnostics).toMatchObject({ + contract_issues: expect.arrayContaining([ + expect.objectContaining({ path: ["pseudocode"], received: "number" }), + ]), + }); +}); + describe("Ghidra unresolved call locations", () => { it("rejects a noncanonical unresolved call site rather than retaining unverifiable location evidence", () => { expect( diff --git a/src/ghidra/GhidraFunctionValues.ts b/src/ghidra/GhidraFunctionValues.ts index f32fa8618a..6c0e350ac1 100644 --- a/src/ghidra/GhidraFunctionValues.ts +++ b/src/ghidra/GhidraFunctionValues.ts @@ -48,6 +48,7 @@ import { } from "../domain/relationshipCoverage.js"; import { err, ok, type Result } from "../domain/result.js"; import { analysisInputErrorFromIssues } from "../domain/inputIssueProjection.js"; +import { outputContractDiagnostics } from "../domain/native/nativeProviderOutput.js"; import { ghidraIdentifierSchema, ghidraInputAddressSchema, @@ -409,7 +410,12 @@ export const parseGhidraFunctionResult = ( new AnalysisOutputError( operation, "Ghidra bridge returned an invalid function observation envelope for this operation", - envelope.success ? undefined : { cause: envelope.error }, + envelope.success + ? undefined + : { + cause: envelope.error, + diagnostics: outputContractDiagnostics(envelope.error, value), + }, ), ); observation = envelope.data; @@ -452,7 +458,13 @@ export const parseGhidraFunctionResult = ( new AnalysisOutputError( operation, "Ghidra bridge output did not match the function-analysis contract", - { cause: parsed.error }, + { + cause: parsed.error, + diagnostics: outputContractDiagnostics( + parsed.error, + observation.value, + ), + }, ), ); }; diff --git a/src/ghidra/GhidraInventoryValues.test.ts b/src/ghidra/GhidraInventoryValues.test.ts index c994a0c7ba..59e80e3211 100644 --- a/src/ghidra/GhidraInventoryValues.test.ts +++ b/src/ghidra/GhidraInventoryValues.test.ts @@ -97,6 +97,26 @@ it.each(["list_procedures", "search_procedures"] as const)( }, ); +it("names the failing fields when an inventory reply misses its contract", () => { + const parsed = parseGhidraInventoryResult( + "list_procedures", + [{ address: 16, value: "secret-name" }], + null, + ); + + const error = parsed.ok ? undefined : parsed.error; + expect(error?.diagnostics).toMatchObject({ + contract_issues: expect.arrayContaining([ + expect.objectContaining({ + path: [0, "address"], + code: "invalid_type", + received: "number", + }), + ]), + }); + expect(JSON.stringify(error?.diagnostics)).not.toContain("secret-name"); +}); + describe("native load-image result projection", () => { it("omits raw bridge output when the observation keeps every reported fact", () => { const observations = JSON.parse( diff --git a/src/ghidra/GhidraInventoryValues.ts b/src/ghidra/GhidraInventoryValues.ts index 4d4eb20cfa..bce4778a00 100644 --- a/src/ghidra/GhidraInventoryValues.ts +++ b/src/ghidra/GhidraInventoryValues.ts @@ -26,6 +26,7 @@ import { AnalysisOutputError, } from "../domain/analysisErrorCore.js"; import { projectBinaryOverview } from "../domain/native/nativeSegments.js"; +import { outputContractDiagnostics } from "../domain/native/nativeProviderOutput.js"; import { availableFunctionBodySchema, functionBodyEntryAgrees, @@ -391,7 +392,7 @@ export function parseGhidraInventoryResult( const parsed = resultSchemas.inspect_native_load_image.safeParse(value); return parsed.success ? ok({ value: normalizeNativeLoadImageObservation(parsed.data) }) - : err(inventoryOutputFailure(operation, parsed.error)); + : err(inventoryOutputFailure(operation, parsed.error, value)); } if (operation === "set_address_name" || operation === "set_addresses_names") { return parseGhidraAddressNameResult(operation, value, namingRequest); @@ -406,15 +407,16 @@ export function parseGhidraInventoryResult( ? { rawResult: value } : {}), }) - : err(inventoryOutputFailure(operation, parsed.error)); + : err(inventoryOutputFailure(operation, parsed.error, value)); } const inventoryOutputFailure = ( operation: GhidraInventoryOperation, - cause: unknown, + cause: z.ZodError, + value: JsonValue, ): AnalysisOutputError => new AnalysisOutputError( operation, "Ghidra bridge output did not match the inventory contract", - { cause }, + { cause, diagnostics: outputContractDiagnostics(cause, value) }, );