mirror of
https://github.com/morluto/rea.git
synced 2026-10-26 17:35:11 +00:00
* feat(ghidra): bound one procedure's decompile without closing the session Fixes #5340 * fix(ghidra): report decompile limit as a request timeout Keeps BinarySession from marking every Ghidra operation unavailable after one procedure's decompile reaches the limit, and names the MCP tool-call deadline in the remediation. * fix(ghidra): budget the decompile limit and report cancellation-closed sessions Adds REA_GHIDRA_DECOMPILE_TIMEOUT_SECONDS to the derived Codex/Kimi tool deadline and forwards it through setup. Marks a remote failure that settles after caller cancellation so the timeout projection no longer promises session reuse. * feat(ghidra)!: default analyze_function to a retained summary analyze_function was the one analysis tool never wired into REA's detail: summary|complete convention, so it always returned the complete native dossier inline (0.2-10 MB, dominated by value-flow and references). Agents on tool-result-capped harnesses never reached the pseudocode. Add `detail` to analyze_function, defaulting to summary: return a compact dossier (procedure identity, pseudocode size, facet counts, value-flow availability) and retain the complete dossier Evidence in the session. Page pseudocode, references, assembly and value-flow through normalized_result.parent_evidence_id with inspect_analysis_view, which already projects every native facet. detail: complete returns the full dossier inline as before. Delivery detail never reaches the provider or Evidence identity. BREAKING CHANGE: analyze_function now defaults to the compact summary view instead of the complete dossier inline. Pass detail: "complete" for the previous behavior. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AoJZDPcrgmMsCqLG4LfiFb * feat(cli): expose analyze_function detail on the function command Wire `rea function` into the shared CLI detail-delivery convention (--detail, --evidence_output) so the CLI and MCP adapters offer the same summary | complete choice for analyze_function. Summary delivery writes the complete dossier Evidence to a bundle and returns the compact summary view; it fails before analysis when --evidence_output is missing, so no provider work is wasted. The CLI default stays complete because a one-shot command has no in-process Evidence retention. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AoJZDPcrgmMsCqLG4LfiFb * refactor(ghidra): harden analyze_function summary after independent review FlatlineRoundtable cross-model review (6 lanes) converged on the summary projection's availability semantics being underspecified. Document that facet_availability is authoritative over raw pseudocode length, that code_units are UTF-16 (the unit inspect_analysis_view pages by), and that value_flow.truncated is meaningful only when value-flow is available. Parenthesize the nullish-coalescing/comparison for clarity. No behavior change; declined claims (value-flow "truncated collapse", unresolved_calls dead code) were schema-impossible or already correct. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AoJZDPcrgmMsCqLG4LfiFb * test(ghidra): cover analyze_function detail wiring on both adapters Close the roundtable's top convergent finding: the summary path had only domain-projection coverage, not the server/CLI wiring where the breaking behavior lives. MCP server (registerEnhancedTools): the default delivers a retained summary whose complete parent is recorded for paging; delivery detail is stripped before the provider request; detail: complete returns the full dossier inline; a registration without retention rejects a summary request before any provider work. CLI (rea function): the default returns the complete dossier; --detail summary without --evidence_output fails before analysis; with the bundle sink it returns the compact summary and writes the complete Evidence bundle. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AoJZDPcrgmMsCqLG4LfiFb * fix(ghidra): keep analyze_function delivery detail out of provider inputs Address the five automated review findings on #5660. P1: `detail` lived in the shared enhancedInputSchemas.analyze_function, so inspect_native_api composed `{procedure, detail: "summary"}` and the strict Ghidra analyze_function schema rejected it. Delivery detail is now owned by the analyze_function MCP tool contract; the handler separates it before any provider call, and executeFunctionAnalysisEvidence parses the provider input only. The MCP regression test validates provider input exactly as the production Ghidra client does and exercises inspect_native_api. P2: the analyze_function summary Evidence now carries the parent's relationship_coverage like native facet views, so partial or unknown caller/callee discovery is not reported as complete. P2: the summary reports the dossier's declared facet_availability, nulls the counts of facets the provider declared unavailable, and lists each reason in unknowns, so Kuna's empty references, comments and basic blocks are not mistaken for observed absence. P2: inspect-analysis-view evidence-file sources accept the Evidence bundle that --evidence_output writes, selecting a record with source.evidence_id when the bundle holds several; the CLI test reads the saved bundle back through that route. Tests that assert the complete dossier over public analyze_function now pass detail complete, and the oversized-dossier test asserts the summary view it previously expected to be unsupported. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AoJZDPcrgmMsCqLG4LfiFb * fix(ghidra): keep omitted function facets unknown in the summary The analyze_function summary coerced an omitted unresolved_calls record to a count of 0 while the native facet view reports the same dossier as "not recorded", and it carried no counts at all for the pageable swift_string_literals facets. Count optional records as null when the dossier omits them, name them in unknowns, and add nullable counts for both Swift literal facets. The summary Evidence envelope also dropped the function address: createAnalysisViewEvidence now derives one function-view predicate for both the address location and relationship coverage. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AoJZDPcrgmMsCqLG4LfiFb * fix(scripts): request complete analyze_function dossiers in real-provider checks The real-provider check scripts call analyze_function over MCP and then consume the complete dossier: requireFunctionDossier, functionDossierSchema, .procedure/.pseudocode reads, operation assertions, compare_functions inputs and the oversized-dossier rejection path. The summary default broke them. Every such call site now passes detail: "complete". Provider-direct callers are unchanged because detail is an MCP contract field; the admission-error sites stay unchanged because provider failures return before the summary branch. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AoJZDPcrgmMsCqLG4LfiFb * fix(ghidra): keep unavailable value-flow counts unknown in the summary summarizeDossier coerced the five value_flow_* counts to zero when native_value_flow was null (IDA, Kuna) or declared unavailable (Ghidra), so the summary reported empty collections while value_flow.available said otherwise. The counts are now null unless value flow is available, with the reason already named in unknowns. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AoJZDPcrgmMsCqLG4LfiFb * fix(evidence): budget authentication headroom by file shape and serialized size The evidence-file headroom reserved two parsed copies, calibrated for one record, and charged strings at two bytes per character. Bundle authentication additionally holds the schema-parsed bundle, the re-parsed records and two JSON.stringify results, which expand control characters and lone surrogates sixfold: a bundle whose raw_result is a million NUL characters estimated at 2 MB parsed but needed 12 MB of serialized text. The budget now has a parsed term and a serialized term per file shape, owned by EvidenceAuthenticationHeadroom and shared by the analysis-view reader and the bundle importer, which had no guard. Two transient peaks the budget did not name are removed at their producers: the canonical digest hashes a large leaf directly instead of concatenating it into its buffer, and the JSON validator walks containers by cursor so a wide array no longer queues one entry per member. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AoJZDPcrgmMsCqLG4LfiFb * fix(mcp): route analyze_function summary replies to dossier consumers in docs and prompts The MCP guide, the version-comparison and crash-triage prompt contracts, the authored skill and the Kuna, IDA and function-matching docs still told agents to pass analyze_function replies straight to compare_functions or build_call_path. Under the summary default those consumers reject the reply. They now name the retained-evidence reference for compare_functions, the observations fields of match_native_functions, and detail: "complete" for build_call_path, which accepts only inline dossiers. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AoJZDPcrgmMsCqLG4LfiFb * test(evidence): pin validator member order and keep interrupted validation yielding Independent review of the cursor validator found the interrupted-validation fixture relied on the old last-first walk: its over-deep head was reached before the first cooperative yield. The padding now precedes the deep element so the walk yields first, which is the invariant the test guards. A new case pins first-defect-in-member-order reporting, skipped non-enumerable keys no longer count toward the yield cadence, and the import-headroom test loads the reader through a fresh module graph so it holds under the services project's shared workers. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AoJZDPcrgmMsCqLG4LfiFb * fix(ghidra): align deadline bounds and cancellation outcomes * refactor(ghidra): shorten deadline recovery guidance * fix(mcp): clarify the scope of inferred decompile deadlines * test(ghidra): verify deadline workflows and document measured costs Separate failure and registration cases by their distinct contracts. Record actual Linux Ghidra, CLI and MCP evidence with measured context, elapsed time, CPU and memory, and explicit remaining coverage limits. * docs(ghidra): pin integration and distinguish baseline format failures * fix(ghidra): own failure lifecycle wording in the provider * test(ghidra): budget timeout fixture generation before allocation * fix(analysis): preserve summary provenance and bound bundle guidance * fix(ghidra): align capability and traversal deadline guidance * style(config): align deadline guidance formatting * docs(ghidra): record capability and traversal verification * docs(analysis): use absolute paths for Evidence files * docs(analysis): record completed function summary measurements * docs(analysis): update main integration validation --------- Co-authored-by: Syed Rishad <rishadzzzz@gmail.com> Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com> Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>
766 lines
27 KiB
JavaScript
766 lines
27 KiB
JavaScript
#!/usr/bin/env node
|
|
import assert from "node:assert/strict";
|
|
import { execFile } from "node:child_process";
|
|
import { createHash } from "node:crypto";
|
|
import { mkdtemp, mkdir, readFile, rm, writeFile } from "node:fs/promises";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
import { promisify } from "node:util";
|
|
import { Client } from "@modelcontextprotocol/client";
|
|
import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
|
|
import { parseBinaryTarget } from "../dist/application/BinaryTargetResolver.js";
|
|
import {
|
|
inspectMipsReadelf,
|
|
expectedMipsElfProfile,
|
|
inspectLegacyMipsReadelf,
|
|
assertMipsLoadedImage,
|
|
assertMipsProbe,
|
|
assertMipsGlobal,
|
|
assertMipsSoftFloatStorage,
|
|
} from "../tests/conformance/ghidra/mips-checks.mjs";
|
|
import { parseEvidence } from "../dist/domain/evidence.js";
|
|
import { parseAnalysisSnapshot } from "../dist/domain/analysisSnapshot.js";
|
|
import { functionDossierSchema } from "../dist/domain/hopperValues.js";
|
|
import { inspectVerifierGhidraInstallation } from "./lib/ghidra-installation-inspection.mjs";
|
|
import { buildMipsFixture } from "../tests/conformance/ghidra/mips-fixture.mjs";
|
|
import {
|
|
buildPspFixture,
|
|
assertPspLoadedImage,
|
|
assertPspProbe,
|
|
} from "../tests/conformance/ghidra/psp-fixture.mjs";
|
|
import {
|
|
requireMcpToolError,
|
|
requireMcpOperationResult,
|
|
} from "./lib/mcp-verifier-results.mjs";
|
|
import { createVerifierRun, completeVerifierRun } from "./lib/verifier-run.mjs";
|
|
|
|
// Optional cross-target lane. No tool acquisition and no target execution.
|
|
const mode = process.argv[2];
|
|
const psp = mode === "--psp" || mode === "--psp-missing-extension";
|
|
const missingExtension = mode === "--psp-missing-extension";
|
|
if (process.argv.length > 3 || (mode !== undefined && !psp))
|
|
throw new Error(
|
|
"Usage: node scripts/verify-real-ghidra-mips.mjs [--psp|--psp-missing-extension]",
|
|
);
|
|
const prefix = psp ? "rea_psp" : "rea_mips";
|
|
if (!["linux", "darwin"].includes(process.platform))
|
|
throw new Error(
|
|
"MIPS verification requires a supported Linux/macOS Ghidra host",
|
|
);
|
|
if (process.env.GHIDRA_INSTALL_DIR === undefined)
|
|
throw new Error("Missing prerequisite: caller-supplied GHIDRA_INSTALL_DIR");
|
|
const installation = await inspectVerifierGhidraInstallation({
|
|
environment: process.env,
|
|
installDir: process.env.GHIDRA_INSTALL_DIR,
|
|
...(process.env.JAVA_HOME === undefined
|
|
? {}
|
|
: { javaHome: process.env.JAVA_HOME }),
|
|
});
|
|
if (installation.status !== "available")
|
|
throw new Error(
|
|
`Ghidra prerequisite unavailable: ${JSON.stringify(installation)}`,
|
|
);
|
|
const execute = promisify(execFile);
|
|
const entrypoint = fileURLToPath(new URL("./rea.mjs", import.meta.url));
|
|
const run = createVerifierRun();
|
|
const workspace = await mkdtemp(join(tmpdir(), "rea-mips-proof-"));
|
|
const runtime = join(workspace, "runtime");
|
|
await mkdir(runtime);
|
|
const env = {
|
|
PATH: process.env.PATH ?? "/usr/bin:/bin",
|
|
TMPDIR: runtime,
|
|
REA_LOG_LEVEL: "silent",
|
|
REA_ANALYSIS_PROVIDER: "ghidra",
|
|
REA_PROCESS_RUN_ID: run.run_id,
|
|
GHIDRA_INSTALL_DIR: installation.installDir,
|
|
JAVA_HOME: installation.javaHome,
|
|
HOPPER_LAUNCHER_PATH: "/rea-unconfigured-deep-provider/hopper",
|
|
};
|
|
for (const setting of ["GHIDRA_HEADLESS_MAXMEM", "GHIDRA_MAXMEM"]) {
|
|
if (process.env[setting] !== undefined) env[setting] = process.env[setting];
|
|
}
|
|
|
|
/**
|
|
* Typed float signatures must use soft-float o32 storage, and callers must see
|
|
* the source constants through it rather than FPU inputs.
|
|
*/
|
|
async function verifySoftFloat(call, symbols, byteOrder) {
|
|
const boundaries = {};
|
|
for (const name of [
|
|
"rea_mips_soft_pick",
|
|
"rea_mips_soft_wide",
|
|
"rea_mips_soft_mixed",
|
|
"rea_mips_soft_struct",
|
|
]) {
|
|
const api = await call("inspect_native_api", { procedure: symbols[name] });
|
|
boundaries[name] = api.boundary;
|
|
}
|
|
assertMipsSoftFloatStorage(boundaries, byteOrder);
|
|
const caller = functionDossierSchema.parse(
|
|
await call("analyze_function", {
|
|
procedure: symbols.rea_mips_soft_caller,
|
|
detail: "complete",
|
|
}),
|
|
);
|
|
// Includes the struct call and the indirect call through a typed pointer.
|
|
assert.doesNotMatch(caller.pseudocode, /\bin_f\d+\b/u);
|
|
for (const literal of [
|
|
"1.5",
|
|
"2.5",
|
|
"3.25",
|
|
"4.75",
|
|
"5.5",
|
|
"6.125",
|
|
"7.5",
|
|
"8.5",
|
|
"9.5",
|
|
])
|
|
assert.ok(
|
|
caller.pseudocode.includes(literal),
|
|
`Soft-float caller lost argument ${literal}`,
|
|
);
|
|
return { storage: boundaries };
|
|
}
|
|
function assertIncompleteMipsInspection(error) {
|
|
assert.equal(error.code, "capability_unavailable");
|
|
assert.equal(error.details.selection_reason, "target_unsupported");
|
|
const rejection = error.details.rejections.find(
|
|
(candidate) => candidate.provider_id === "ghidra",
|
|
);
|
|
assert.equal(rejection?.code, "architecture_unsupported");
|
|
assert.match(rejection.reason, /ABI declaration inspection is incomplete/u);
|
|
assert.match(rejection.reason, /section header table/u);
|
|
}
|
|
|
|
const reports = [];
|
|
let primaryFailure;
|
|
try {
|
|
if (!psp) {
|
|
// Recordless legacy admission: ABI field 0, no ABI flags record, pinned
|
|
// 32-bit language. Independent GNU readelf checks the header interpretation.
|
|
for (const byteOrder of ["little", "big"]) {
|
|
const fixture = await buildMipsFixture(workspace, byteOrder, "hard", {
|
|
legacy: true,
|
|
});
|
|
const legacy = await inspectLegacyMipsReadelf(fixture.path, byteOrder);
|
|
const resolved = await parseBinaryTarget(fixture.path);
|
|
if (!resolved.ok) throw resolved.error;
|
|
assert.equal(resolved.value.mips.flags, 0x50000001);
|
|
assert.equal(resolved.value.mips.abiFlags, null);
|
|
assert.deepEqual(
|
|
resolved.value.mips.limitations ?? [],
|
|
legacy.limitations,
|
|
"REA legacy ABI-record absence interpretation disagrees with GNU readelf",
|
|
);
|
|
assert.equal(resolved.value.sha256, fixture.sha256);
|
|
const snapshotPath = join(workspace, `${byteOrder}-legacy.snapshot.json`);
|
|
const transport = new StdioClientTransport({
|
|
command: process.execPath,
|
|
args: [entrypoint, "mcp"],
|
|
env,
|
|
stderr: "pipe",
|
|
});
|
|
const client = new Client({
|
|
name: "rea-real-mips-legacy-proof",
|
|
version: "1",
|
|
});
|
|
let stderr = "";
|
|
transport.stderr?.on("data", (chunk) => {
|
|
stderr = (stderr + chunk.toString()).slice(-65536);
|
|
});
|
|
const call = async (name, args = {}) => {
|
|
const reply = await client.callTool(
|
|
{ name, arguments: args },
|
|
undefined,
|
|
{ timeout: 180000 },
|
|
);
|
|
return requireMcpOperationResult(reply, name);
|
|
};
|
|
try {
|
|
await client.connect(transport);
|
|
const selected = await call("open_binary", {
|
|
path: fixture.path,
|
|
provider_id: "ghidra",
|
|
});
|
|
assert.equal(selected.architecture, "mips");
|
|
assert.equal(selected.sha256, fixture.sha256);
|
|
const image = await call("inspect_native_load_image");
|
|
assertMipsLoadedImage(image.observations, byteOrder, fixture.sha256);
|
|
// The pinned import language is the checked one, not an auto-selection.
|
|
assert.equal(
|
|
image.observations.language_id,
|
|
`MIPS:${byteOrder === "little" ? "LE" : "BE"}:32:default`,
|
|
);
|
|
const procedures = await call("list_procedures");
|
|
const entry = procedures.find((p) => p.value === "rea_mips_entry");
|
|
assert.ok(entry, "Entry function missing from legacy inventory");
|
|
assert.equal(
|
|
BigInt(entry.address),
|
|
BigInt(legacy.symbols.rea_mips_entry),
|
|
);
|
|
const probe = await call("inspect_native_instruction", {
|
|
address: legacy.symbols.rea_mips_probe,
|
|
});
|
|
const probeBranch = await call("inspect_native_instruction", {
|
|
address: `0x${(BigInt(legacy.symbols.rea_mips_probe) + 4n).toString(16)}`,
|
|
});
|
|
assertMipsProbe(
|
|
probe,
|
|
probeBranch,
|
|
byteOrder,
|
|
legacy.symbols.rea_mips_probe,
|
|
);
|
|
const dossier = functionDossierSchema.parse(
|
|
await call("analyze_function", {
|
|
procedure: entry.address,
|
|
detail: "complete",
|
|
}),
|
|
);
|
|
assert.ok(dossier.pseudocode.trim().length > 0);
|
|
const saved = await call("close_binary", {
|
|
snapshot_path: snapshotPath,
|
|
});
|
|
assert.ok(saved.evidence_records > 0);
|
|
const snapshot = parseAnalysisSnapshot(
|
|
JSON.parse(await readFile(snapshotPath, "utf8")),
|
|
);
|
|
assert.equal(snapshot.target.architecture, "mips");
|
|
const parameters = snapshot.binding.analysis_profile.parameters;
|
|
assert.equal(
|
|
parameters.language_id,
|
|
`MIPS:${byteOrder === "little" ? "LE" : "BE"}:32:default`,
|
|
);
|
|
assert.equal(parameters.language_selection, "provider-pinned-v1");
|
|
assert.equal(
|
|
parameters.mips_support_lane,
|
|
"elf32-exec-arch32-legacy-pinned-v1",
|
|
);
|
|
assert.equal(parameters.mips_abi_basis, "inferred-o32-assumption-v1");
|
|
assert.ok(
|
|
snapshot.evidence_bundle.records.some((record) =>
|
|
parseEvidence(record).limitations.some((item) =>
|
|
item.includes("analysis assumption"),
|
|
),
|
|
),
|
|
"Legacy o32 assumption limitation missing from retained Evidence",
|
|
);
|
|
assert.ok(
|
|
snapshot.evidence_bundle.records.some((record) =>
|
|
parseEvidence(record).limitations.some((item) =>
|
|
item.includes("Floating-point ABI semantics are unverified"),
|
|
),
|
|
),
|
|
"Legacy floating-point uncertainty missing from retained Evidence",
|
|
);
|
|
await call("open_binary", {
|
|
path: fixture.path,
|
|
provider_id: "ghidra",
|
|
snapshot_path: snapshotPath,
|
|
});
|
|
assert.deepEqual(await call("list_procedures"), procedures);
|
|
await call("close_binary");
|
|
} catch (cause) {
|
|
throw new Error(
|
|
`MIPS legacy ${byteOrder} verification failed; MCP stderr: ${stderr}`,
|
|
{ cause },
|
|
);
|
|
} finally {
|
|
try {
|
|
await client.close();
|
|
} finally {
|
|
await transport.close();
|
|
}
|
|
}
|
|
reports.push({
|
|
...fixture,
|
|
independent_reader: legacy,
|
|
variant: "recordless-legacy-pinned",
|
|
effective_language: `MIPS:${
|
|
byteOrder === "little" ? "LE" : "BE"
|
|
}:32:default`,
|
|
procedures: "verified-through-snapshot",
|
|
checks: [
|
|
"recordless header admitted with inferred o32 assumption",
|
|
"pinned 32-bit language checked after import",
|
|
"legacy limitations retained in Evidence",
|
|
"snapshot round-trip and profile parameters",
|
|
],
|
|
status: "passed",
|
|
});
|
|
}
|
|
}
|
|
for (const [byteOrder, floatAbi] of psp
|
|
? [["little", "hard"]]
|
|
: [
|
|
["little", "hard"],
|
|
["big", "hard"],
|
|
["little", "soft"],
|
|
["big", "soft"],
|
|
]) {
|
|
const fixture = psp
|
|
? await buildPspFixture(workspace)
|
|
: await buildMipsFixture(workspace, byteOrder, floatAbi);
|
|
const independent = psp
|
|
? fixture
|
|
: await inspectMipsReadelf(fixture.path, byteOrder, floatAbi);
|
|
const resolved = await parseBinaryTarget(fixture.path);
|
|
if (!resolved.ok) throw resolved.error;
|
|
if (psp) {
|
|
assert.equal(resolved.value.mips.flags, 0x10a23001);
|
|
assert.deepEqual(
|
|
resolved.value.mips,
|
|
independent.mips,
|
|
"REA PSP ELF/ABI interpretation disagrees with GNU readelf and raw ABI bytes",
|
|
);
|
|
} else {
|
|
assert.deepEqual(
|
|
resolved.value.mips,
|
|
independent.mips,
|
|
"REA ELF/ABI interpretation disagrees with GNU readelf",
|
|
);
|
|
}
|
|
assert.equal(resolved.value.sha256, fixture.sha256);
|
|
const snapshotPath = join(
|
|
workspace,
|
|
`${byteOrder}-${floatAbi}.snapshot.json`,
|
|
);
|
|
const transport = new StdioClientTransport({
|
|
command: process.execPath,
|
|
args: [entrypoint, "mcp"],
|
|
env,
|
|
stderr: "pipe",
|
|
});
|
|
const client = new Client({ name: "rea-real-mips-proof", version: "1" });
|
|
let stderr = "";
|
|
transport.stderr?.on("data", (chunk) => {
|
|
stderr = (stderr + chunk.toString()).slice(-65536);
|
|
});
|
|
const call = async (name, args = {}) => {
|
|
const reply = await client.callTool(
|
|
{ name, arguments: args },
|
|
{ timeout: 180000 },
|
|
);
|
|
return requireMcpOperationResult(reply, name);
|
|
};
|
|
try {
|
|
await client.connect(transport);
|
|
if (missingExtension) {
|
|
const rejected = await client.callTool(
|
|
{
|
|
name: "open_binary",
|
|
arguments: { path: fixture.path, provider_id: "ghidra" },
|
|
},
|
|
{ timeout: 60000 },
|
|
);
|
|
const error = requireMcpToolError(rejected);
|
|
assert.match(JSON.stringify(error), /PSP_EXTENSION_UNAVAILABLE/u);
|
|
let cliError;
|
|
try {
|
|
await execute(
|
|
process.execPath,
|
|
[
|
|
entrypoint,
|
|
"function",
|
|
fixture.path,
|
|
`${prefix}_entry`,
|
|
"--provider",
|
|
"ghidra",
|
|
"--json",
|
|
],
|
|
{ env, timeout: 60000, maxBuffer: 1024 * 1024 },
|
|
);
|
|
} catch (cause) {
|
|
cliError = cause;
|
|
}
|
|
assert.ok(
|
|
cliError && Number.isInteger(cliError.code) && cliError.code !== 0,
|
|
"Expected a typed public rejection, not a timeout or success",
|
|
);
|
|
assert.match(
|
|
`${cliError.stdout}\n${cliError.stderr}`,
|
|
/PSP_EXTENSION_UNAVAILABLE/u,
|
|
);
|
|
reports.push({
|
|
path: fixture.path,
|
|
status: "expected-missing-extension-refusal",
|
|
cli_exit: cliError.code,
|
|
mcp_error: error,
|
|
});
|
|
continue;
|
|
}
|
|
const target = await call("open_binary", {
|
|
path: fixture.path,
|
|
provider_id: "ghidra",
|
|
});
|
|
assert.equal(target.architecture, "mips");
|
|
assert.equal(target.sha256, fixture.sha256);
|
|
// ELF load-image verification is not the DOS comparison lane. Its retained
|
|
// observations still expose the actual Ghidra language and loaded bytes.
|
|
const image = await call("inspect_native_load_image");
|
|
if (psp) assertPspLoadedImage(image.observations, fixture.sha256);
|
|
else assertMipsLoadedImage(image.observations, byteOrder, fixture.sha256);
|
|
const procedures = await call("list_procedures");
|
|
const entry = procedures.find((p) => p.value === `${prefix}_entry`);
|
|
const leaf = procedures.find((p) => p.value === `${prefix}_leaf`);
|
|
assert.ok(entry, "Entry function missing from real Ghidra inventory");
|
|
assert.ok(leaf, "Leaf function missing from real Ghidra inventory");
|
|
assert.equal(
|
|
BigInt(entry.address),
|
|
BigInt(independent.symbols[`${prefix}_entry`]),
|
|
);
|
|
assert.equal(
|
|
BigInt(leaf.address),
|
|
BigInt(independent.symbols[`${prefix}_leaf`]),
|
|
);
|
|
const probe = independent.symbols[`${prefix}_probe`];
|
|
assert.ok(
|
|
procedures.some(
|
|
(p) =>
|
|
p.value === `${prefix}_probe` &&
|
|
BigInt(p.address) === BigInt(probe),
|
|
),
|
|
);
|
|
const move = await call("inspect_native_instruction", { address: probe });
|
|
const branch = await call("inspect_native_instruction", {
|
|
address: `0x${(BigInt(probe) + 4n).toString(16)}`,
|
|
});
|
|
if (psp) assertPspProbe(move, branch);
|
|
else assertMipsProbe(move, branch, byteOrder, probe);
|
|
const global = await call("read_bytes", {
|
|
address: independent.symbols[`${prefix}_global`],
|
|
length: 4,
|
|
});
|
|
assert.equal(global.returned_bytes, 4);
|
|
assertMipsGlobal(global.bytes_hex, byteOrder);
|
|
const marker = await call("read_bytes", {
|
|
address: independent.symbols[`${prefix}_marker`],
|
|
length: Buffer.byteLength(
|
|
psp
|
|
? "rea-psp-source-owned-fixture\0"
|
|
: "rea-mips-source-owned-fixture\0",
|
|
),
|
|
});
|
|
assert.equal(
|
|
marker.bytes_hex,
|
|
Buffer.from(
|
|
psp
|
|
? "rea-psp-source-owned-fixture\0"
|
|
: "rea-mips-source-owned-fixture\0",
|
|
).toString("hex"),
|
|
);
|
|
const callees = await call("procedure_callees", {
|
|
procedure: entry.address,
|
|
});
|
|
assert.ok(
|
|
callees.includes(leaf.address),
|
|
"Expected direct call not recovered",
|
|
);
|
|
const softFloat =
|
|
floatAbi === "soft"
|
|
? await verifySoftFloat(call, independent.symbols, byteOrder)
|
|
: undefined;
|
|
const assembly = await call("procedure_assembly", {
|
|
procedure: entry.address,
|
|
});
|
|
assert.ok(assembly.trim().length > 0);
|
|
const dossier = functionDossierSchema.parse(
|
|
await call("analyze_function", {
|
|
procedure: entry.address,
|
|
detail: "complete",
|
|
}),
|
|
);
|
|
assert.ok(dossier.pseudocode.trim().length > 0);
|
|
const mapping = await call("address_to_file_offset", {
|
|
address: entry.address,
|
|
});
|
|
const memory = await call("read_bytes", {
|
|
address: entry.address,
|
|
length: 16,
|
|
});
|
|
const sourceBytes = await readFile(fixture.path);
|
|
assert.equal(memory.returned_bytes, 16);
|
|
assert.equal(
|
|
memory.bytes_hex,
|
|
sourceBytes
|
|
.subarray(mapping.file_offset, mapping.file_offset + 16)
|
|
.toString("hex"),
|
|
);
|
|
const saved = await call("close_binary", {
|
|
snapshot_path: snapshotPath,
|
|
});
|
|
assert.ok(saved.evidence_records > 0);
|
|
const snapshot = parseAnalysisSnapshot(
|
|
JSON.parse(await readFile(snapshotPath, "utf8")),
|
|
);
|
|
assert.equal(snapshot.target.architecture, "mips");
|
|
assert.deepEqual(
|
|
snapshot.binding.analysis_profile.parameters.mips_elf,
|
|
expectedMipsElfProfile(independent.mips),
|
|
"Snapshot profile did not retain the independently inspected ELF/ABI declaration",
|
|
);
|
|
assert.equal(
|
|
snapshot.binding.analysis_profile.parameters.mips_soft_float_storage,
|
|
floatAbi === "soft" ? "o32-soft-float-prototype-v1" : undefined,
|
|
);
|
|
assert.equal(
|
|
snapshot.evidence_bundle.records.some((record) =>
|
|
/Soft-float prototype outcome: 4 functions and [1-9]\d* function-pointer definitions use __rea_soft_float\./u.test(
|
|
JSON.stringify(record),
|
|
),
|
|
),
|
|
floatAbi === "soft",
|
|
"Soft-float prototype outcome missing from, or unexpected in, retained Evidence",
|
|
);
|
|
assert.ok(snapshot.evidence_bundle.records.length > 0);
|
|
if (psp) {
|
|
assert.equal(
|
|
snapshot.binding.analysis_profile.parameters.language_id,
|
|
"Allegrex:LE:32:default",
|
|
);
|
|
assert.match(
|
|
snapshot.binding.analysis_profile.parameters.psp_extension.sha256,
|
|
/^[a-f0-9]{64}$/u,
|
|
);
|
|
assert.ok(
|
|
snapshot.evidence_bundle.records.some((record) =>
|
|
JSON.stringify(record).includes("VFPU prefix"),
|
|
),
|
|
"PSP limitations missing from retained Evidence",
|
|
);
|
|
}
|
|
for (const evidence of snapshot.evidence_bundle.records)
|
|
assert.equal(parseEvidence(evidence).subject?.architecture, "mips");
|
|
await call("open_binary", {
|
|
path: fixture.path,
|
|
provider_id: "ghidra",
|
|
snapshot_path: snapshotPath,
|
|
});
|
|
assert.deepEqual(await call("list_procedures"), procedures);
|
|
await call("close_binary");
|
|
|
|
if (psp) {
|
|
const alternate = join(workspace, "alternate");
|
|
await mkdir(alternate);
|
|
const other = await buildPspFixture(alternate, 11);
|
|
assert.notEqual(other.sha256, fixture.sha256);
|
|
assert.equal(
|
|
other.symbols.rea_psp_global,
|
|
fixture.symbols.rea_psp_global,
|
|
"Target-switch control must reuse the same global address",
|
|
);
|
|
for (const item of [fixture, other, fixture]) {
|
|
const selected = await call("open_binary", {
|
|
path: item.path,
|
|
provider_id: "ghidra",
|
|
});
|
|
assert.equal(selected.sha256, item.sha256);
|
|
const word = await call("read_bytes", {
|
|
address: item.symbols.rea_psp_global,
|
|
length: 4,
|
|
});
|
|
const expected = Buffer.alloc(4);
|
|
expected.writeUInt32LE(item.global_value);
|
|
assert.equal(word.bytes_hex, expected.toString("hex"));
|
|
await call("close_binary");
|
|
}
|
|
const wrongSnapshot = await client.callTool(
|
|
{
|
|
name: "open_binary",
|
|
arguments: {
|
|
path: other.path,
|
|
provider_id: "ghidra",
|
|
snapshot_path: snapshotPath,
|
|
},
|
|
},
|
|
{ timeout: 180000 },
|
|
);
|
|
requireMcpToolError(wrongSnapshot);
|
|
// A bad snapshot must not poison the next clean public session.
|
|
await call("open_binary", {
|
|
path: fixture.path,
|
|
provider_id: "ghidra",
|
|
});
|
|
await call("close_binary");
|
|
}
|
|
|
|
// Same public operation through CLI, not a private injected provider.
|
|
const cli = await execute(
|
|
process.execPath,
|
|
[
|
|
entrypoint,
|
|
"function",
|
|
fixture.path,
|
|
`${prefix}_entry`,
|
|
"--provider",
|
|
"ghidra",
|
|
"--json",
|
|
],
|
|
{ env, timeout: 240000, maxBuffer: 16 * 1024 * 1024 },
|
|
);
|
|
const evidence = parseEvidence(JSON.parse(cli.stdout));
|
|
assert.equal(evidence.subject?.architecture, "mips");
|
|
assert.equal(evidence.subject?.digest.sha256, fixture.sha256);
|
|
const cliDossier = functionDossierSchema.parse(
|
|
evidence.normalized_result,
|
|
);
|
|
assert.equal(cliDossier.procedure.address, dossier.procedure.address);
|
|
assert.ok(cliDossier.pseudocode.trim().length > 0);
|
|
assert.equal(
|
|
createHash("sha256")
|
|
.update(await readFile(fixture.path))
|
|
.digest("hex"),
|
|
fixture.sha256,
|
|
"Analysis modified the source executable",
|
|
);
|
|
// This upstream carved-firmware proof is specific to the generic MIPS fixture.
|
|
// PSP keeps its independent Allegrex/A-B-A checks; carved PSP is not verified.
|
|
let carved;
|
|
if (!psp) {
|
|
// A program-header ABI record survives carving, but GNU declarations in
|
|
// uncaptured section headers remain unknown: both adapters must refuse.
|
|
const read16 = (offset) =>
|
|
byteOrder === "little"
|
|
? sourceBytes.readUInt16LE(offset)
|
|
: sourceBytes.readUInt16BE(offset);
|
|
const read32 = (offset) =>
|
|
byteOrder === "little"
|
|
? sourceBytes.readUInt32LE(offset)
|
|
: sourceBytes.readUInt32BE(offset);
|
|
const sectionOffset = read32(32);
|
|
const cut = sectionOffset - 1;
|
|
const programOffset = read32(28);
|
|
const programCount = read16(44);
|
|
assert.equal(read16(42), 32);
|
|
assert.ok(cut >= programOffset + programCount * 32);
|
|
for (let i = 0; i < programCount; i += 1) {
|
|
const offset = programOffset + i * 32;
|
|
assert.ok(read32(offset + 4) + read32(offset + 16) <= cut);
|
|
}
|
|
const carvedBytes = sourceBytes.subarray(0, cut);
|
|
const carvedPath = join(
|
|
workspace,
|
|
`${byteOrder}-${floatAbi}-carved.elf`,
|
|
);
|
|
const carvedSha256 = createHash("sha256")
|
|
.update(carvedBytes)
|
|
.digest("hex");
|
|
await writeFile(carvedPath, carvedBytes);
|
|
const carvedResolved = await parseBinaryTarget(carvedPath);
|
|
if (!carvedResolved.ok) throw carvedResolved.error;
|
|
assert.deepEqual(
|
|
carvedResolved.value.mips.abiFlags,
|
|
independent.mips.abiFlags,
|
|
);
|
|
assert.match(
|
|
carvedResolved.value.mips.limitations.join(" "),
|
|
/section header table/u,
|
|
);
|
|
const carvedError = requireMcpToolError(
|
|
await client.callTool(
|
|
{
|
|
name: "open_binary",
|
|
arguments: { path: carvedPath, provider_id: "ghidra" },
|
|
},
|
|
{ timeout: 60000 },
|
|
),
|
|
);
|
|
assertIncompleteMipsInspection(carvedError);
|
|
let carvedCliError;
|
|
try {
|
|
await execute(
|
|
process.execPath,
|
|
[
|
|
entrypoint,
|
|
"function",
|
|
carvedPath,
|
|
independent.symbols.rea_mips_entry,
|
|
"--provider",
|
|
"ghidra",
|
|
"--json",
|
|
],
|
|
{ env, timeout: 60000, maxBuffer: 1024 * 1024 },
|
|
);
|
|
} catch (cause) {
|
|
carvedCliError = cause;
|
|
}
|
|
assert.ok(
|
|
carvedCliError &&
|
|
Number.isInteger(carvedCliError.code) &&
|
|
carvedCliError.code !== 0,
|
|
"Expected an incomplete-ABI refusal, not a timeout or success",
|
|
);
|
|
assertIncompleteMipsInspection(JSON.parse(carvedCliError.stdout));
|
|
assert.deepEqual(await readFile(carvedPath), carvedBytes);
|
|
carved = {
|
|
sha256: carvedSha256,
|
|
source_bytes: carvedBytes.length,
|
|
section_header_offset: sectionOffset,
|
|
status: "expected-incomplete-abi-refusal",
|
|
limitations: carvedResolved.value.mips.limitations,
|
|
cli_exit: carvedCliError.code,
|
|
mcp_error: carvedError,
|
|
};
|
|
}
|
|
reports.push({
|
|
...fixture,
|
|
independent_reader: independent,
|
|
effective_language: image.observations.language_id,
|
|
effective_compiler_spec: image.observations.compiler_spec_id,
|
|
checks: [
|
|
"independent ELF/ABI and symbols",
|
|
"loaded Ghidra identity",
|
|
"fixed instruction bytes and immediate",
|
|
psp
|
|
? "Allegrex BITREV decoding and A-B-A target identity"
|
|
: "conditional branch destination",
|
|
"global word and marker bytes",
|
|
"CLI/MCP and snapshot identity",
|
|
...(floatAbi === "soft"
|
|
? ["soft-float o32 parameter/return storage and caller constants"]
|
|
: []),
|
|
],
|
|
...(softFloat === undefined ? {} : { soft_float: softFloat }),
|
|
procedures: procedures.length,
|
|
carved: carved ?? {
|
|
status: "not_run",
|
|
reason:
|
|
"Carved-firmware verification covers generic MIPS fixtures; carved PSP coverage is not established.",
|
|
},
|
|
status: "passed",
|
|
});
|
|
} catch (cause) {
|
|
throw new Error(
|
|
`MIPS ${byteOrder} ${floatAbi}-float verification failed; MCP stderr: ${stderr}`,
|
|
{ cause },
|
|
);
|
|
} finally {
|
|
try {
|
|
await client.close();
|
|
} finally {
|
|
await transport.close();
|
|
}
|
|
}
|
|
}
|
|
} catch (cause) {
|
|
primaryFailure = cause;
|
|
}
|
|
const completed = await completeVerifierRun(run);
|
|
if (
|
|
completed.process_lineage.status !== "verified" ||
|
|
completed.process_lineage.descendants.length !== 0
|
|
)
|
|
throw new Error(
|
|
`Cleanup unverified; retained diagnostic workspace: ${workspace}`,
|
|
{ cause: primaryFailure },
|
|
);
|
|
if (primaryFailure !== undefined)
|
|
throw new Error(
|
|
`MIPS verification failed; retained workspace: ${workspace}`,
|
|
{
|
|
cause: primaryFailure,
|
|
},
|
|
);
|
|
await rm(workspace, { recursive: true, force: true });
|
|
console.log(JSON.stringify({ ...completed, fixtures: reports }, null, 2));
|