fix(ghidra): expose terminal-call boundary assumptions
Preserve observed terminal-call sites and callee flags across function views, raw bridge responses, Evidence, and snapshot replay. Version analysis profiles so older unqualified results cannot be reused. Qualify Ghidra function extents without changing external no-return flags or claiming recovery of excluded code. Validation: 76 focused tests, check:fast, docs:check, formatting, and the full Ghidra 12.1.4 host lane pass. Real CLI and all five stdio MCP function views retain the terminal-call evidence; returning puts calls remain unflagged. The reported stripped Rust ELF and Windows host remain unverified. Fixes #1658
This commit is contained in:
@@ -4,8 +4,98 @@ import { EnhancedTools } from "../../../src/application/EnhancedTools.js";
|
||||
import { connectGhidraMcp, sessionEvidence } from "./ghidraMcpHarness.js";
|
||||
import { functionDossierSchema } from "../../../src/domain/hopperValues.js";
|
||||
import { ghidraFunctionDossier } from "../../../src/domain/ghidraValues.fixture.js";
|
||||
import { jsonValueSchema } from "../../../src/domain/jsonValue.js";
|
||||
import {
|
||||
jsonValueSchema,
|
||||
type JsonValue,
|
||||
} from "../../../src/domain/jsonValue.js";
|
||||
import { ok } from "../../../src/domain/result.js";
|
||||
import {
|
||||
isGhidraFunctionOperation,
|
||||
type GhidraFunctionOperation,
|
||||
} from "../../../src/ghidra/GhidraFunctionValues.js";
|
||||
|
||||
it("retains terminal-call qualifications across function views and snapshot replay", async () => {
|
||||
const limitation =
|
||||
"Ghidra Listing reports a terminal call at 0x401000 to EXTERNAL:0x20 __tls_get_addr (external=true, thunk=false, hasNoReturn=true); fallthrough is excluded by Ghidra's flow model. This records Ghidra's FunctionManager flag, not an independent verification that the callee cannot return.";
|
||||
const dossier = functionDossierSchema.parse(ghidraFunctionDossier());
|
||||
dossier.limitations.push(limitation);
|
||||
const { signature, locals, ...identity } = dossier.procedure;
|
||||
if (!identity.body.available)
|
||||
throw new Error("The function fixture must include its analyzed body");
|
||||
const values = new Map<GhidraFunctionOperation, JsonValue>([
|
||||
["procedure_pseudo_code", dossier.pseudocode],
|
||||
["procedure_assembly", dossier.assembly.join("\n")],
|
||||
[
|
||||
"procedure_info",
|
||||
{
|
||||
name: identity.name,
|
||||
entrypoint: identity.address,
|
||||
signature,
|
||||
locals,
|
||||
basicblock_count: dossier.basic_blocks.length,
|
||||
length: identity.body.total_bytes,
|
||||
classification: identity.classification,
|
||||
body: identity.body,
|
||||
},
|
||||
],
|
||||
[
|
||||
"read_function_instructions",
|
||||
{
|
||||
procedure: identity,
|
||||
instructions: dossier.assembly,
|
||||
limitations: [limitation],
|
||||
},
|
||||
],
|
||||
["analyze_function", jsonValueSchema.parse(dossier)],
|
||||
]);
|
||||
const harness = await connectGhidraMcp(
|
||||
"ghidra-terminal-call-evidence",
|
||||
(operation) => {
|
||||
if (!isGhidraFunctionOperation(operation))
|
||||
throw new Error(`Unexpected operation: ${operation}`);
|
||||
const value = values.get(operation);
|
||||
if (value === undefined)
|
||||
throw new Error(`Unexpected operation: ${operation}`);
|
||||
return Promise.resolve(ok({ value, limitations: [limitation] }));
|
||||
},
|
||||
);
|
||||
try {
|
||||
for (const [name, value] of values) {
|
||||
const request = {
|
||||
name,
|
||||
arguments:
|
||||
name === "analyze_function"
|
||||
? { procedure: identity.address }
|
||||
: { procedure: identity.address, document: "fixture" },
|
||||
};
|
||||
const reply = await harness.mcp.callTool(request);
|
||||
expect(reply.isError, name).not.toBe(true);
|
||||
const evidence = sessionEvidence(
|
||||
harness.session,
|
||||
reply.structuredContent,
|
||||
);
|
||||
expect(evidence.normalized_result, name).toEqual(value);
|
||||
expect(evidence.raw_result, name).toEqual({
|
||||
value,
|
||||
limitations: [limitation],
|
||||
});
|
||||
expect(evidence.limitations, name).toContain(limitation);
|
||||
expect(evidence.analysis_profile?.parameters).toMatchObject({
|
||||
function_boundary_observations: "ghidra-terminal-call-limitations-v1",
|
||||
});
|
||||
const replay = await harness.session.execute(name, evidence.parameters);
|
||||
if (!replay.ok) throw replay.error;
|
||||
expect(replay.value.result, name).toEqual(value);
|
||||
expect(replay.value.limitations, name).toContain(limitation);
|
||||
expect(replay.value.limitations, name).toContainEqual(
|
||||
expect.stringContaining("Loaded from a local REA analysis snapshot"),
|
||||
);
|
||||
}
|
||||
expect(harness.calls).toEqual([...values.keys()]);
|
||||
} finally {
|
||||
await harness.close();
|
||||
}
|
||||
});
|
||||
|
||||
it("rejects contradictory annotation readback across the provider and MCP boundaries", async () => {
|
||||
const dossier = functionDossierSchema.parse(ghidraFunctionDossier());
|
||||
|
||||
@@ -206,15 +206,19 @@ const resultBuilders = new Map<GhidraOperation, GhidraResultBuilder>([
|
||||
["procedure_callers", () => []],
|
||||
[
|
||||
"procedure_info",
|
||||
() => ({
|
||||
name: "fixture_main",
|
||||
entrypoint: "0x401000",
|
||||
basicblock_count: 1,
|
||||
length: 6,
|
||||
signature: "int fixture_main(void)",
|
||||
locals: [],
|
||||
classification: ghidraFunctionClassification(),
|
||||
}),
|
||||
() => {
|
||||
const identity = ghidraFunctionIdentity();
|
||||
return {
|
||||
name: "fixture_main",
|
||||
entrypoint: "0x401000",
|
||||
basicblock_count: 1,
|
||||
length: 6,
|
||||
signature: "int fixture_main(void)",
|
||||
locals: [],
|
||||
classification: ghidraFunctionClassification(),
|
||||
body: identity.body,
|
||||
};
|
||||
},
|
||||
],
|
||||
["procedure_pseudo_code", () => "int fixture_main(void) { return 42; }"],
|
||||
[
|
||||
@@ -247,9 +251,23 @@ const resultFor = (
|
||||
const builder = resultBuilders.get(operation);
|
||||
if (builder === undefined)
|
||||
throw new TypeError(`Unexpected Ghidra operation: ${operation}`);
|
||||
return builder(input, typeof input.limit === "number" ? input.limit : 100);
|
||||
const value = builder(
|
||||
input,
|
||||
typeof input.limit === "number" ? input.limit : 100,
|
||||
);
|
||||
return functionObservationOperations.has(operation)
|
||||
? { value, limitations: [] }
|
||||
: value;
|
||||
};
|
||||
|
||||
const functionObservationOperations: ReadonlySet<GhidraOperation> = new Set([
|
||||
"analyze_function",
|
||||
"procedure_assembly",
|
||||
"procedure_info",
|
||||
"procedure_pseudo_code",
|
||||
"read_function_instructions",
|
||||
]);
|
||||
|
||||
const stringItem = (address: string, value: string): JsonValue => ({
|
||||
address,
|
||||
value,
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
volatile int rea_ghidra_inventory_global = 7;
|
||||
|
||||
@@ -43,6 +44,15 @@ __attribute__((noinline, used)) int rea_ghidra_inventory_leaf(int value) {
|
||||
return value + rea_ghidra_inventory_global;
|
||||
}
|
||||
|
||||
// Ghidra's no-return analysis recognizes abort; this function provides a real
|
||||
// terminal-call boundary while retaining a returning path.
|
||||
__attribute__((noinline, used)) int rea_ghidra_inventory_terminal_call(
|
||||
int should_abort) {
|
||||
if (should_abort) abort();
|
||||
puts("REA_GHIDRA_TERMINAL_CALL_RETURNED");
|
||||
return should_abort;
|
||||
}
|
||||
|
||||
// A reference into the function body can land inside an instruction.
|
||||
__attribute__((used)) const unsigned char *volatile rea_ghidra_interior_pointer =
|
||||
(const unsigned char *)(void *)rea_ghidra_inventory_leaf + 1;
|
||||
|
||||
Reference in New Issue
Block a user