fix(ghidra): preserve function references and snapshot boundaries
Collect references from the complete owned function body and exact entry, including instruction-interior destinations and embedded-data sources. Bind the corrected semantics to the analysis profile so legacy snapshots cannot replay incomplete results. Preserve snapshot mismatch reasons and recovery advice through CLI and MCP without disturbing live sessions. Return exact entry candidates for ambiguous function names. Expand the real-provider regression workflow and replace redundant parser assertions with malformed-output coverage through the pinned MCP client. Verified with Ghidra 12.1.4 on macOS arm64: full host-native lane, real CLI/MCP boundary probes, 30 focused tests, and typecheck/lint.
This commit is contained in:
@@ -0,0 +1,184 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { execFile } from "node:child_process";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { writeFile, rm } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { promisify } from "node:util";
|
||||
import { parseBinaryTarget } from "../../dist/application/BinaryTargetResolver.js";
|
||||
import { createAnalysisProfile } from "../../dist/domain/analysisProfile.js";
|
||||
import {
|
||||
createAnalysisSnapshotEntry,
|
||||
parseAnalysisSnapshot,
|
||||
serializeAnalysisSnapshot,
|
||||
snapshotBinding,
|
||||
snapshotTarget,
|
||||
} from "../../dist/domain/analysisSnapshot.js";
|
||||
import { createEvidence } from "../../dist/domain/evidence.js";
|
||||
import { createEvidenceBundle } from "../../dist/domain/evidenceBundle.js";
|
||||
import { requireMcpResult } from "./mcp-verifier-results.mjs";
|
||||
|
||||
/** Reject a valid older snapshot whose instruction-only collector omitted a real edge. */
|
||||
export async function verifyLegacyGhidraReferenceSnapshot(
|
||||
client,
|
||||
{ target, procedure, omittedEdge, entrypoint, env },
|
||||
) {
|
||||
const bundle = requireMcpResult(
|
||||
await client.callTool({
|
||||
name: "get_evidence_bundle",
|
||||
arguments: {},
|
||||
}),
|
||||
"get_evidence_bundle",
|
||||
);
|
||||
const observed = bundle.records.find(
|
||||
(record) =>
|
||||
record.operation === "analyze_function" &&
|
||||
record.parameters.procedure === procedure,
|
||||
);
|
||||
assert.ok(observed);
|
||||
const { function_references: coverage, ...legacyParameters } =
|
||||
observed.analysis_profile.parameters;
|
||||
assert.equal(
|
||||
typeof coverage,
|
||||
"string",
|
||||
"Reference semantics must participate in snapshot identity",
|
||||
);
|
||||
const legacyProfile = createAnalysisProfile(
|
||||
observed.provider,
|
||||
legacyParameters,
|
||||
);
|
||||
assert.notEqual(legacyProfile.digest, observed.analysis_profile.digest);
|
||||
const parsed = await parseBinaryTarget(
|
||||
target.path,
|
||||
process.cwd(),
|
||||
process.arch,
|
||||
);
|
||||
if (!parsed.ok) throw parsed.error;
|
||||
const nativeTarget = parsed.value;
|
||||
const result = structuredClone(observed.normalized_result);
|
||||
// Reconstruct the observed pre-fix omission; all other data
|
||||
// comes from the real provider and the file has a fully valid Evidence binding.
|
||||
result.incoming_references = result.incoming_references.filter(
|
||||
(edge) =>
|
||||
edge.source_address !== omittedEdge.source_address ||
|
||||
edge.target_address !== omittedEdge.target_address,
|
||||
);
|
||||
assert.equal(
|
||||
result.incoming_references.length,
|
||||
observed.normalized_result.incoming_references.length - 1,
|
||||
);
|
||||
const evidence = createEvidence(nativeTarget, observed.provider, {
|
||||
operation: observed.operation,
|
||||
parameters: observed.parameters,
|
||||
result,
|
||||
rawResult: result,
|
||||
analysisProfile: legacyProfile,
|
||||
limitations: observed.limitations,
|
||||
locations: observed.locations,
|
||||
});
|
||||
const binding = snapshotBinding(legacyProfile);
|
||||
const identity = snapshotTarget(nativeTarget);
|
||||
const snapshot = parseAnalysisSnapshot({
|
||||
target: identity,
|
||||
binding,
|
||||
entries: [
|
||||
createAnalysisSnapshotEntry({
|
||||
target: identity,
|
||||
binding,
|
||||
operation: observed.operation,
|
||||
parameters: observed.parameters,
|
||||
execution: {
|
||||
result,
|
||||
rawResult: result,
|
||||
provider: observed.provider,
|
||||
analysisProfile: legacyProfile,
|
||||
limitations: observed.limitations,
|
||||
locations: observed.locations,
|
||||
subject: nativeTarget,
|
||||
},
|
||||
}),
|
||||
],
|
||||
evidence_bundle: createEvidenceBundle([evidence]),
|
||||
});
|
||||
assert.equal(
|
||||
snapshot.entries.length,
|
||||
1,
|
||||
"The legacy query must remain valid and replayable under its own profile",
|
||||
);
|
||||
const path = join(env.TMPDIR, `legacy-reference-${randomUUID()}.json`);
|
||||
await writeFile(path, serializeAnalysisSnapshot(snapshot), { flag: "wx" });
|
||||
try {
|
||||
const session = requireMcpResult(
|
||||
await client.callTool({ name: "binary_session", arguments: {} }),
|
||||
"binary_session",
|
||||
);
|
||||
const reply = await client.callTool({
|
||||
name: "open_binary",
|
||||
arguments: {
|
||||
path: target.path,
|
||||
provider_id: "ghidra",
|
||||
snapshot_path: path,
|
||||
},
|
||||
});
|
||||
assert.equal(reply.isError, true);
|
||||
const mcpRejection = reply.structuredContent.error;
|
||||
assert.equal(mcpRejection.code, "evidence_integrity_mismatch");
|
||||
assert.match(mcpRejection.details.reason, /profile_mismatch/u);
|
||||
assert.match(
|
||||
mcpRejection.remediation.action,
|
||||
/without this snapshot.*fresh snapshot/u,
|
||||
);
|
||||
assert.deepEqual(
|
||||
requireMcpResult(
|
||||
await client.callTool({ name: "binary_session", arguments: {} }),
|
||||
"binary_session",
|
||||
),
|
||||
session,
|
||||
"A rejected snapshot must preserve the selected session",
|
||||
);
|
||||
const retained = requireMcpResult(
|
||||
await client.callTool({
|
||||
name: "analyze_function",
|
||||
arguments: { procedure },
|
||||
}),
|
||||
"analyze_function",
|
||||
);
|
||||
assert.ok(
|
||||
retained.incoming_references.some(
|
||||
(edge) =>
|
||||
edge.source_address === omittedEdge.source_address &&
|
||||
edge.target_address === omittedEdge.target_address,
|
||||
),
|
||||
"A rejected legacy snapshot must not replace complete live analysis",
|
||||
);
|
||||
await assert.rejects(
|
||||
promisify(execFile)(
|
||||
process.execPath,
|
||||
[
|
||||
entrypoint,
|
||||
"function",
|
||||
target.path,
|
||||
procedure,
|
||||
"--provider",
|
||||
"ghidra",
|
||||
"--snapshot",
|
||||
path,
|
||||
"--json",
|
||||
],
|
||||
{ env, timeout: 240000, maxBuffer: 72 * 1024 * 1024 },
|
||||
),
|
||||
(error) => {
|
||||
assert.equal(error.code, 1);
|
||||
const rejection = JSON.parse(error.stdout);
|
||||
assert.equal(rejection.code, "evidence_integrity_mismatch");
|
||||
assert.equal(rejection.details.reason, mcpRejection.details.reason);
|
||||
assert.equal(
|
||||
rejection.remediation.action,
|
||||
mcpRejection.remediation.action,
|
||||
);
|
||||
return true;
|
||||
},
|
||||
);
|
||||
} finally {
|
||||
await rm(path);
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import { promisify } from "node:util";
|
||||
import Ajv from "ajv/dist/2020.js";
|
||||
import addFormats from "ajv-formats";
|
||||
import { mcpTextValue, requireMcpResult } from "./mcp-verifier-results.mjs";
|
||||
import { verifyLegacyGhidraReferenceSnapshot } from "./ghidra-reference-snapshot-e2e.mjs";
|
||||
|
||||
/** Probe real Ghidra location, annotation and error contracts through public adapters. */
|
||||
export async function verifyGhidraBoundaries(
|
||||
@@ -56,6 +57,7 @@ export async function verifyGhidraBoundaries(
|
||||
assert.match(JSON.stringify(error.details.issues), diagnostic);
|
||||
}
|
||||
rejectedCalls++;
|
||||
return reply.structuredContent?.error;
|
||||
};
|
||||
const cli = async (command, value, flags = []) => {
|
||||
const { stdout } = await promisify(execFile)(
|
||||
@@ -93,6 +95,61 @@ export async function verifyGhidraBoundaries(
|
||||
assert.equal((await stat(socketRoot)).mode & 0o777, 0o700);
|
||||
const address = original.procedure.address;
|
||||
const name = original.procedure.name;
|
||||
const names = await call("list_names");
|
||||
const leaf = names.find((item) =>
|
||||
item.value.endsWith("rea_ghidra_inventory_leaf"),
|
||||
);
|
||||
const pointer = names.find((item) =>
|
||||
item.value.endsWith("rea_ghidra_interior_pointer"),
|
||||
);
|
||||
assert.ok(
|
||||
leaf && pointer,
|
||||
"Source fixture lacks the interior function pointer",
|
||||
);
|
||||
const interiorTarget = `0x${(BigInt(leaf.address) + 1n).toString(16)}`;
|
||||
assert.ok(
|
||||
(await call("xrefs", { address: interiorTarget })).includes(
|
||||
pointer.address,
|
||||
),
|
||||
);
|
||||
const incoming = await call("procedure_references", {
|
||||
procedure: leaf.address,
|
||||
direction: "incoming",
|
||||
});
|
||||
const interiorEdge = incoming.references.find(
|
||||
(edge) =>
|
||||
edge.source_address === pointer.address &&
|
||||
edge.target_address === interiorTarget,
|
||||
);
|
||||
assert.ok(
|
||||
interiorEdge,
|
||||
"Procedure references omitted an observed interior-body edge",
|
||||
);
|
||||
assert.equal(interiorEdge.target_procedure?.address, leaf.address);
|
||||
assert.equal(interiorEdge.source_procedure, null);
|
||||
assert.equal(interiorEdge.kind.data, true);
|
||||
const leafDossier = await call("analyze_function", {
|
||||
procedure: leaf.address,
|
||||
});
|
||||
assert.ok(
|
||||
leafDossier.incoming_references.some(
|
||||
(edge) =>
|
||||
edge.source_address === pointer.address &&
|
||||
edge.target_address === interiorTarget,
|
||||
),
|
||||
);
|
||||
assert.deepEqual(
|
||||
(await cli("function", leaf.value)).incoming_references,
|
||||
leafDossier.incoming_references,
|
||||
"CLI and MCP must retain the same interior-body references",
|
||||
);
|
||||
await verifyLegacyGhidraReferenceSnapshot(client, {
|
||||
target,
|
||||
procedure: leaf.address,
|
||||
omittedEdge: interiorEdge,
|
||||
entrypoint,
|
||||
env,
|
||||
});
|
||||
const baseline = await call("inspect_native_instruction", { address });
|
||||
assert.equal(baseline.status, "decoded");
|
||||
const bytes = await call("read_bytes", { address, length: baseline.length });
|
||||
@@ -442,12 +499,37 @@ export async function verifyGhidraBoundaries(
|
||||
);
|
||||
assert.equal(await call("address_name", { address }), renamed);
|
||||
}
|
||||
for (const selected of [address, indirectProcedure.address]) {
|
||||
await call("annotate_native_function", {
|
||||
procedure: selected,
|
||||
name: "rea_ambiguous",
|
||||
});
|
||||
}
|
||||
const ambiguous = await invalid(
|
||||
"procedure_address",
|
||||
{ procedure: "rea_ambiguous" },
|
||||
/ambiguous.*select an exact entry address/u,
|
||||
);
|
||||
for (const selected of [address, indirectProcedure.address]) {
|
||||
assert.ok(
|
||||
JSON.stringify(ambiguous.details.issues).includes(selected),
|
||||
"Ambiguity diagnostics must retain every matching entry address",
|
||||
);
|
||||
assert.equal(
|
||||
await call("procedure_address", { procedure: selected }),
|
||||
selected,
|
||||
);
|
||||
}
|
||||
await call("close_binary");
|
||||
await assert.rejects(access(socketRoot), { code: "ENOENT" });
|
||||
await assert.rejects(access(runtimeRoot), { code: "ENOENT" });
|
||||
await call("open_binary", { path: target.path, provider_id: "ghidra" });
|
||||
assert.equal(await call("procedure_address", { procedure: name }), address);
|
||||
assert.equal(await call("address_name", { address }), name);
|
||||
assert.equal(
|
||||
await call("procedure_address", { procedure: indirectProcedure.value }),
|
||||
indirectProcedure.address,
|
||||
);
|
||||
assert.deepEqual(
|
||||
(await call("analyze_function", { procedure: address })).comments,
|
||||
original.comments,
|
||||
@@ -468,6 +550,9 @@ export async function verifyGhidraBoundaries(
|
||||
mutation_rollback: true,
|
||||
annotation_native_text_validation: true,
|
||||
lossless_unicode_transport: true,
|
||||
complete_function_body_references: true,
|
||||
ambiguity_candidates_inline: true,
|
||||
legacy_reference_snapshot_rejected: true,
|
||||
source_immutable: true,
|
||||
reopen_discards_edits: true,
|
||||
long_tmpdir_private_socket_cleanup: true,
|
||||
|
||||
Reference in New Issue
Block a user