fix(ghidra): recover evidenced switch cases and default targets (#579)

Correlate typed labels with recovered block entries and one indirect dispatch.
Preserve ambiguous and nonexact integer associations as unknown with raw diagnostics.
Expose defaults separately, collect backing load metadata, and invalidate old profiles.
Add independent compiler-byte CLI/MCP acceptance and actual Ghidra model controls.

Assisted-by: Kogasa (gensokyo-skills:kogasa-surprise-testing)
Assisted-by: Suika (gensokyo-skills:suika-scatter-gather-planning)
This commit is contained in:
N0zoM1z0
2026-10-05 22:54:17 +08:00
committed by GitHub
parent bb91abd3df
commit b225b0f64b
26 changed files with 1955 additions and 55 deletions
@@ -5,6 +5,71 @@ import { ghidraFunctionDossier } from "../domain/hopperValues.fixture.js";
import { nativeApiBoundarySchema } from "../domain/nativeApiBoundary.js";
import { projectNativeApiInspection } from "./NativeApiInspection.js";
describe("native API switch uncertainty", () => {
it("reports an unevidenced default path independently of known numeric cases", () => {
const dossier = functionDossierSchema.parse(ghidraFunctionDossier());
const boundary = dossier.native_api;
if (boundary?.available !== true)
throw new TypeError("Ghidra native API fixture is unavailable");
const table = boundary.jump_tables[0];
if (table === undefined)
throw new TypeError("Ghidra jump-table fixture is unavailable");
const incomplete = functionDossierSchema.parse({
...dossier,
native_api: {
...boundary,
jump_tables: [{ ...table, default_targets: [] }],
},
});
expect(projectNativeApiInspection(incomplete).residual_unknowns).toContain(
"Which default or out-of-range path, if any, belongs to the dispatch at 0x401010?",
);
});
it("does not report evidenced defaults as unknown cases and retains genuinely unresolved mappings", () => {
const dossier = functionDossierSchema.parse(ghidraFunctionDossier());
const boundary = dossier.native_api;
if (boundary?.available !== true)
throw new TypeError("Ghidra native API fixture is unavailable");
const table = boundary.jump_tables[0];
const mapping = table?.mappings[0];
if (table === undefined || mapping === undefined)
throw new TypeError("Ghidra jump-table fixture is unavailable");
const defaultTarget = {
target_address: mapping.target_address,
confidence: "high",
evidence: mapping.evidence,
};
const complete = functionDossierSchema.parse({
...dossier,
native_api: {
...boundary,
jump_tables: [{ ...table, default_targets: [defaultTarget] }],
},
});
expect(projectNativeApiInspection(complete).residual_unknowns).toEqual([]);
const unresolved = functionDossierSchema.parse({
...complete,
native_api: {
...boundary,
jump_tables: [
{
...table,
default_targets: [defaultTarget],
mappings: [
mapping,
{ ...mapping, case_value: null, target_address: "0x401030" },
],
},
],
},
});
expect(projectNativeApiInspection(unresolved).residual_unknowns).toContain(
"Which source-level case values correspond to every target dispatched at 0x401010?",
);
});
});
describe("native API inspection", () => {
it("preserves structured type and jump-table boundary evidence", () => {
const dossier = functionDossierSchema.parse(ghidraFunctionDossier());
+4
View File
@@ -72,6 +72,10 @@ const availableResidualUnknowns = (
): readonly string[] => {
const unknowns: string[] = [];
for (const table of boundary.jump_tables) {
if (table.default_targets.length === 0)
unknowns.push(
`Which default or out-of-range path, if any, belongs to the dispatch at ${table.dispatch_address}?`,
);
if (table.mappings.length === 0)
unknowns.push(
`Which case values and targets belong to the dispatch at ${table.dispatch_address}?`,
@@ -40,6 +40,6 @@ export const FUNCTION_WORKFLOW_TOOL_CONTRACTS = [
),
functionWorkflow(
"inspect_native_api",
"Analyze a native API boundary in one function identified by symbol or provider-returned address. Returns confidence, evidence, jump-table dispatch/data/target mappings, unsupported branches, and residual unknowns.",
"Analyze a native API boundary in one function identified by symbol or provider-returned address. Returns confidence, evidence, jump-table dispatch/data/case mappings, separately evidenced default targets, unsupported branches, and residual unknowns. Null case values mean unresolved cases, never a known default.",
),
] as const satisfies readonly ToolContract[];
+14
View File
@@ -114,6 +114,20 @@ export const ghidraNativeApiBoundary = () => ({
],
},
],
default_targets: [
{
target_address: "0x401030",
confidence: "high" as const,
evidence: [
{
kind: "jump-table" as const,
source: "ghidra-clang-case-token",
detail:
"Typed default label belongs to the block at 0x401030 with unique indirect dispatch predecessor 0x401010.",
},
],
},
],
limitations: [],
},
],
+7
View File
@@ -71,11 +71,18 @@ const jumpTableMappingSchema = z
})
.strict();
const jumpTableDefaultTargetSchema = z.strictObject({
target_address: z.string().min(1),
confidence: inferenceConfidenceSchema,
evidence: z.array(inferenceEvidenceSchema).min(1),
});
const inferredJumpTableSchema = z
.object({
dispatch_address: z.string().min(1),
data_sources: z.array(jumpTableDataSourceSchema),
mappings: z.array(jumpTableMappingSchema),
default_targets: z.array(jumpTableDefaultTargetSchema).default([]),
limitations: z.array(z.string()),
})
.strict();
File diff suppressed because one or more lines are too long
+2
View File
@@ -40,6 +40,8 @@ export const resolveGhidraAnalysisProfile = (
].sort(),
import_mode: "ephemeral-read-only",
function_body_evidence: "complete-inclusive-ranges-v1",
jump_table_evidence: "typed-case-default-blocks-v1",
decompiler_jump_loads: true,
loader: dosMz ? "MzLoader" : "auto-from-header",
language_id: dosMz ? "x86:LE:16:Real Mode" : "auto-from-header",
compiler_spec_id: dosMz ? "default" : "auto-default",
+107
View File
@@ -193,6 +193,113 @@ describe("Ghidra function-analysis result values", () => {
});
describe("Ghidra jump-table mapping contract", () => {
it("preserves evidenced defaults separately from cases and unresolved targets", () => {
const dossier = functionDossierSchema.parse(ghidraFunctionDossier());
const boundary = dossier.native_api;
if (boundary?.available !== true)
throw new TypeError("Ghidra native API fixture is unavailable");
const table = boundary.jump_tables[0];
const mapping = table?.mappings[0];
if (table === undefined || mapping === undefined)
throw new TypeError("Ghidra jump-table fixture is unavailable");
const defaults = [
{
target_address: mapping.target_address,
confidence: "high",
evidence: [
{
kind: "jump-table",
source: "ghidra-clang-case-token",
detail: "Default label reaches this recovered destination.",
},
],
},
];
const parsed = parseGhidraFunctionResult("analyze_function", {
...dossier,
native_api: {
...boundary,
jump_tables: [
{
...table,
default_targets: defaults,
mappings: [
mapping,
{ ...mapping, case_value: null, target_address: "0x401030" },
],
},
],
},
});
expect(parsed.ok).toBe(true);
if (!parsed.ok) throw parsed.error;
const output = functionDossierSchema.parse(parsed.value).native_api;
if (output?.available !== true)
throw new TypeError("Ghidra native API fixture is unavailable");
expect(output.jump_tables[0]).toMatchObject({
default_targets: defaults,
mappings: [
{ case_value: 0, target_address: "0x401020" },
{ case_value: null, target_address: "0x401030" },
],
});
});
it("normalizes legacy missing defaults without promoting unknown cases", () => {
const dossier = functionDossierSchema.parse(ghidraFunctionDossier());
const boundary = dossier.native_api;
if (boundary?.available !== true)
throw new TypeError("Ghidra native API fixture is unavailable");
const table = boundary.jump_tables[0];
const mapping = table?.mappings[0];
if (table === undefined || mapping === undefined)
throw new TypeError("Ghidra jump-table fixture is unavailable");
const { default_targets: _legacyDefaults, ...legacy } = table;
const parsed = parseGhidraFunctionResult("analyze_function", {
...dossier,
native_api: {
...boundary,
jump_tables: [
{ ...legacy, mappings: [{ ...mapping, case_value: null }] },
],
},
});
expect(parsed.ok).toBe(true);
if (!parsed.ok) throw parsed.error;
expect(functionDossierSchema.parse(parsed.value).native_api).toMatchObject({
jump_tables: [{ default_targets: [], mappings: [{ case_value: null }] }],
});
});
it("rejects non-canonical default destinations and defaults without evidence", () => {
const dossier = functionDossierSchema.parse(ghidraFunctionDossier());
const boundary = dossier.native_api;
if (boundary?.available !== true)
throw new TypeError("Ghidra native API fixture is unavailable");
const table = boundary.jump_tables[0];
const mapping = table?.mappings[0];
if (table === undefined || mapping === undefined)
throw new TypeError("Ghidra jump-table fixture is unavailable");
for (const destination of [
{
target_address: "0X401020",
confidence: "high",
evidence: mapping.evidence,
},
{ target_address: "0x401020", confidence: "high", evidence: [] },
]) {
expect(
parseGhidraFunctionResult("analyze_function", {
...dossier,
native_api: {
...boundary,
jump_tables: [{ ...table, default_targets: [destination] }],
},
}),
).toMatchObject({ ok: false, error: { _tag: "AnalysisOutputError" } });
}
});
it("keeps jump-table sources separate from case-to-target mappings", () => {
const parsed = parseGhidraFunctionResult(
"analyze_function",
+1
View File
@@ -197,6 +197,7 @@ const ghidraNativeApiBoundary = nativeApiBoundarySchema.superRefine(
table.dispatch_address,
...table.data_sources.map(({ address }) => address),
...table.mappings.map(({ target_address }) => target_address),
...table.default_targets.map(({ target_address }) => target_address),
]);
if (
addresses.some(
@@ -39,6 +39,31 @@ const provider = (
return new GhidraProvider(config.value, silentLogger, host, clientFactory);
};
describe("Ghidra jump-table profile", () => {
it("separates typed case/default evidence and jump-load metadata from legacy cache profiles", async () => {
const resolved = await provider().resolveAnalysisProfile(
executableTarget("elf", "x86_64"),
);
if (!resolved.ok || resolved.value.profile === null)
throw new Error("expected a committed Ghidra profile");
const profile = resolved.value.profile;
const legacy = createAnalysisProfile(
profile.provider,
Object.fromEntries(
Object.entries(profile.parameters).filter(
([key]) =>
key !== "jump_table_evidence" && key !== "decompiler_jump_loads",
),
),
);
expect(profile.digest).not.toBe(legacy.digest);
expect(profile.parameters.jump_table_evidence).toBe(
"typed-case-default-blocks-v1",
);
expect(profile.parameters.decompiler_jump_loads).toBe(true);
});
});
describe("Ghidra provider", () => {
it("separates complete body evidence from legacy length-only cache profiles", async () => {
const resolved = await provider().resolveAnalysisProfile(