refactor(verify): run Ghidra verifier commands through the supervised owner (2 of 2) (#12717)

* refactor(verify): run Ghidra verifier commands through the supervised owner (2 of 2)

Route the remaining Ghidra verifier compiler and CLI launches through
createVerifierCommands, close that owner before lineage observation, and
assert the raw arm64 alignment refusal from its structured error.

* fix(verify): stop detached Ghidra verifier commands on interrupt

SIGINT and SIGTERM abort the command owner and the scripts wait for its
cleanup, so a compiler started in its own process group cannot outlive
the verifier.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(verify): stop Ghidra verifier analysis when interrupted

Pass the verifier interruption signal into MCP and Ghidra waits, and stop
waiting for calls that cannot observe it, so SIGINT does not continue the
analysis before cleanup.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(verify): stop the switch evidence probe when the verifier is interrupted

Pass the verifier interruption signal into the detached Ghidra control
probe and end its wait so supervisor.stop() runs before a later SIGKILL.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(verify): observe work already started when a verifier interrupt wins

untilInterrupted now settles the caller's promise after the signal has
already aborted, so a connection started just before the check cannot
reject with nobody listening.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* refactor(verify): share verifier interrupt handling and keep it through release

Move the SIGINT/SIGTERM abort wiring into bindVerifierCommandInterruption.
Handlers stay installed until after fixture release, and untilInterrupted
still observes a promise that was already started.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(verify): keep diagnostic workspaces and stop fixture builds on interrupt

Check the interruption signal after finalization and before the fixture
root is removed. MIPS and RISC-V fixture compilers and readers now run
through the verifier command owner, so SIGTERM aborts them instead of
waiting out their execFile deadline.

Co-authored-by: morluto <morluto@users.noreply.github.com>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: morluto <morluto@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2026-11-19 23:29:09 -05:00
1 parent 385e5d228f
commit f25a0a0599
17 files changed
+1264 -539

No files matched your search

@@ -0,0 +1,79 @@
/**
* SIGINT and SIGTERM for one verifier command owner.
*
* Owned commands run in their own process group, so the terminal signal does
* not reach them. These handlers abort that work instead of letting Node exit.
* They stay installed until `finish`, which is after fixture release: removing
* them earlier restores Node's default termination while cleanup is still
* closing a detached process.
*
* `untilInterrupted` always observes the promise the caller already started.
* When the signal is already aborted, that interrupt is the result and a later
* settlement of the started promise is ignored.
*
* @param {{
* exec: (
* command: string,
* args: readonly string[],
* options?: object,
* ) => Promise<unknown>;
* }} commands
*/
export function bindVerifierCommandInterruption(commands) {
const controller = new AbortController();
const interrupt = (signal) =>
controller.abort(new Error(`Verifier interrupted by ${signal}`));
process.on("SIGINT", interrupt);
process.on("SIGTERM", interrupt);
const signal = controller.signal;
return {
signal,
exec: (command, args, options = {}) =>
commands.exec(command, args, { ...options, signal }),
throwIfInterrupted: () => {
if (signal.aborted) throw signal.reason;
},
/**
* The interrupt reason without removing the handlers. Fixture release still
* needs them so a late signal cannot skip cleanup.
*/
reason: () => (signal.aborted ? signal.reason : undefined),
/** Remove the handlers after fixture release has finished. */
finish: () => {
process.off("SIGINT", interrupt);
process.off("SIGTERM", interrupt);
return signal.aborted ? signal.reason : undefined;
},
/**
* Register this after every other release callback. It runs after those
* checks and before the fixture root is removed, so a signal during
* finalization keeps the diagnostic workspace.
*/
retainWorkspaceIfInterrupted: (fixture) => {
fixture.releaseAfter(() => {
if (signal.aborted) throw signal.reason;
});
},
untilInterrupted: (promise) =>
new Promise((resolve, reject) => {
let settled = false;
const finish = (settle, value) => {
if (settled) return;
settled = true;
signal.removeEventListener("abort", onAbort);
settle(value);
};
const onAbort = () => finish(reject, signal.reason);
// The caller starts `promise` before this check. Observe it even when
// the signal is already aborted so its later rejection cannot end the
// process. The interrupt wins.
if (signal.aborted) finish(reject, signal.reason);
else signal.addEventListener("abort", onAbort, { once: true });
Promise.resolve(promise).then(
(value) => finish(resolve, value),
(cause) => finish(reject, cause),
);
}),
};
}
+73 -14
View File
@@ -1,11 +1,9 @@
#!/usr/bin/env node
import assert from "node:assert/strict";
import { execFile } from "node:child_process";
import { createHash } from "node:crypto";
import { mkdir, readFile } from "node:fs/promises";
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";
@@ -19,7 +17,13 @@ import {
inspectMipsRelocatableReadelf,
} from "../tests/conformance/ghidra/mips-relocatable-checks.mjs";
import { requireMcpOperationResult } from "./lib/mcp-verifier-results.mjs";
import { createVerifierRun, completeVerifierRun } from "./lib/verifier-run.mjs";
import { createVerifierCommands } from "./lib/verify-commands.mjs";
import { bindVerifierCommandInterruption } from "./lib/verifier-command-interruption.mjs";
import {
assertVerifierCleanupComplete,
createVerifierRun,
completeVerifierRun,
} from "./lib/verifier-run.mjs";
import { VerifierFixture } from "./lib/verifier-fixture.mjs";
import { requireLaneToolchain } from "../tests/conformance/ghidra/fixture-toolchain.mjs";
@@ -46,7 +50,6 @@ 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();
// Refuse a host without the fixture toolchain before allocating anything.
@@ -77,6 +80,12 @@ const env = {
for (const setting of ["GHIDRA_HEADLESS_MAXMEM", "GHIDRA_MAXMEM"]) {
if (process.env[setting] !== undefined) env[setting] = process.env[setting];
}
// The public CLI launch uses this command owner. A timeout stops its process group.
const commands = await createVerifierCommands();
const commandInterruption = bindVerifierCommandInterruption(commands);
const execOwned = commandInterruption.exec;
const { untilInterrupted, throwIfInterrupted } = commandInterruption;
const finishCommandInterruption = () => commandInterruption.finish();
/** Load every relocated word through the public read_bytes operation. */
async function loadedWords(call, independent, segments, byteOrder) {
@@ -108,11 +117,13 @@ async function verifyFixture(byteOrder, kernelModule) {
workspace,
byteOrder,
kernelModule,
execOwned,
);
const independent = await inspectMipsRelocatableReadelf(
fixture.path,
byteOrder,
fixture.float_abi,
execOwned,
);
const resolved = await parseBinaryTarget(fixture.path);
if (!resolved.ok) throw resolved.error;
@@ -135,13 +146,20 @@ async function verifyFixture(byteOrder, kernelModule) {
transport.stderr?.on("data", (chunk) => {
stderr = (stderr + chunk.toString()).slice(-65536);
});
const call = async (name, args = {}) =>
requireMcpOperationResult(
await client.callTool({ name, arguments: args }, { timeout: 180000 }),
const call = async (name, args = {}) => {
throwIfInterrupted();
return requireMcpOperationResult(
await client.callTool(
{ name, arguments: args },
{ timeout: 180000, signal: commandInterruption.signal },
),
name,
);
};
try {
await client.connect(transport);
await untilInterrupted(
client.connect(transport, { signal: commandInterruption.signal }),
);
const target = await call("open_binary", {
path: fixture.path,
provider_id: "ghidra",
@@ -264,7 +282,7 @@ async function verifyFixture(byteOrder, kernelModule) {
assert.deepEqual(await call("list_procedures"), procedures);
await call("close_binary");
// Same public operation through CLI, not a private injected provider.
const cli = await execute(
const cli = await execOwned(
process.execPath,
[
entrypoint,
@@ -328,19 +346,60 @@ try {
} catch (cause) {
primaryFailure = cause;
}
// Close the command owner before the parent-tree check. A failed close stays
// registered for retry and the diagnostic workspace is retained.
let cleanupFailure;
try {
await workspaceFixture.closeEarly([() => commands.close()]);
} catch (cause) {
cleanupFailure = cause;
}
if (cleanupFailure !== undefined)
throw retainWorkspace(
`MIPS relocatable command cleanup failed; retained diagnostic workspace: ${workspace}`,
{
cause:
primaryFailure === undefined
? cleanupFailure
: new AggregateError(
[primaryFailure, cleanupFailure],
"MIPS relocatable verification and command cleanup both failed",
),
},
);
const completed = await completeVerifierRun(run);
if (
completed.process_lineage.status !== "verified" ||
completed.process_lineage.descendants.length !== 0
)
let lineageFailure;
try {
assertVerifierCleanupComplete(completed.process_lineage);
} catch (cause) {
lineageFailure = cause;
}
if (lineageFailure !== undefined)
throw retainWorkspace(
`Cleanup unverified; retained diagnostic workspace: ${workspace}`,
{ cause: primaryFailure },
{
cause:
primaryFailure === undefined
? lineageFailure
: new AggregateError(
[primaryFailure, lineageFailure],
"MIPS relocatable verification failed and cleanup was unverified",
),
},
);
if (primaryFailure !== undefined)
throw retainWorkspace(
`MIPS relocatable verification failed; retained workspace: ${workspace}`,
{ cause: primaryFailure },
);
const interrupted = commandInterruption.reason();
if (interrupted !== undefined)
throw retainWorkspace(
`MIPS relocatable verification interrupted; retained diagnostic workspace: ${workspace}`,
{ cause: interrupted },
);
commandInterruption.retainWorkspaceIfInterrupted(workspaceFixture);
await workspaceFixture.release();
const releaseInterrupted = finishCommandInterruption();
if (releaseInterrupted !== undefined) throw releaseInterrupted;
console.log(JSON.stringify({ ...completed, fixtures: reports }, null, 2));
+73 -15
View File
@@ -1,9 +1,7 @@
#!/usr/bin/env node
import assert from "node:assert/strict";
import { execFile } from "node:child_process";
import { mkdir, readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { promisify } from "node:util";
import { Client } from "@modelcontextprotocol/client";
import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
import { parseBinaryTarget } from "../dist/application/BinaryTargetResolver.js";
@@ -14,7 +12,13 @@ import {
requireMcpOperationResult,
requireMcpToolError,
} from "./lib/mcp-verifier-results.mjs";
import { createVerifierRun, completeVerifierRun } from "./lib/verifier-run.mjs";
import { createVerifierCommands } from "./lib/verify-commands.mjs";
import { bindVerifierCommandInterruption } from "./lib/verifier-command-interruption.mjs";
import {
assertVerifierCleanupComplete,
createVerifierRun,
completeVerifierRun,
} from "./lib/verifier-run.mjs";
import { VerifierFixture } from "./lib/verifier-fixture.mjs";
import { requireLaneToolchain } from "../tests/conformance/ghidra/fixture-toolchain.mjs";
@@ -25,7 +29,6 @@ if (
throw new Error(
"This optional lane requires BYO Ghidra on Linux/macOS and GNU MIPS cross-binutils.",
);
const execute = promisify(execFile);
const run = createVerifierRun();
// Refuse a host without the fixture toolchain before allocating anything.
await requireLaneToolchain("mips-shared");
@@ -56,6 +59,12 @@ const env = {
? {}
: { GHIDRA_HEADLESS_MAXMEM: process.env.GHIDRA_HEADLESS_MAXMEM }),
};
// The public CLI launch uses this command owner. A timeout stops its process group.
const commands = await createVerifierCommands();
const commandInterruption = bindVerifierCommandInterruption(commands);
const execOwned = commandInterruption.exec;
const { untilInterrupted, throwIfInterrupted } = commandInterruption;
const finishCommandInterruption = () => commandInterruption.finish();
const reports = [];
let failure;
try {
@@ -70,6 +79,7 @@ try {
workspace,
byteOrder,
variant,
execOwned,
);
const resolved = await parseBinaryTarget(fixture.path);
if (!resolved.ok) throw resolved.error;
@@ -115,9 +125,10 @@ try {
});
let calls = 0;
const reply = async (name, args = {}) => {
throwIfInterrupted();
const response = await client.callTool(
{ name, arguments: args },
{ timeout: 180000 },
{ timeout: 180000, signal: commandInterruption.signal },
);
await writeFile(
join(workspace, `${byteOrder}-${variant}-${++calls}-${name}.json`),
@@ -129,8 +140,12 @@ try {
requireMcpOperationResult(await reply(name, args), name);
const original = await readFile(fixture.path);
try {
await client.connect(transport);
await client.listTools();
await untilInterrupted(
client.connect(transport, { signal: commandInterruption.signal }),
);
await untilInterrupted(
client.listTools(undefined, { signal: commandInterruption.signal }),
);
if (
variant === "symbol-relative" ||
variant === "section-relocations"
@@ -149,7 +164,9 @@ try {
: /section relocation declarations/u,
);
assert.deepEqual(await readFile(fixture.path), original);
await client.ping();
await untilInterrupted(
client.ping({ signal: commandInterruption.signal }),
);
reports.push({ ...fixture, status: "refused", ping: true });
continue;
}
@@ -291,8 +308,10 @@ try {
firstInstruction,
);
await call("close_binary");
await client.ping();
const cli = await execute(
await untilInterrupted(
client.ping({ signal: commandInterruption.signal }),
);
const cli = await execOwned(
process.execPath,
[
"scripts/rea.mjs",
@@ -336,15 +355,45 @@ try {
} catch (cause) {
failure = cause;
}
// Close the command owner before the parent-tree check. A failed close stays
// registered for retry and the diagnostic workspace is retained.
let cleanupFailure;
try {
await workspaceFixture.closeEarly([() => commands.close()]);
} catch (cause) {
cleanupFailure = cause;
}
if (cleanupFailure !== undefined)
throw retainWorkspace(
`MIPS16 shared command cleanup failed; retained diagnostic workspace: ${workspace}`,
{
cause:
failure === undefined
? cleanupFailure
: new AggregateError(
[failure, cleanupFailure],
"MIPS16 shared verification and command cleanup both failed",
),
},
);
const completed = await completeVerifierRun(run);
if (
completed.process_lineage.status !== "verified" ||
completed.process_lineage.descendants.length !== 0
)
let lineageFailure;
try {
assertVerifierCleanupComplete(completed.process_lineage);
} catch (cause) {
lineageFailure = cause;
}
if (lineageFailure !== undefined)
throw retainWorkspace(
`Cleanup unverified; retained workspace: ${workspace}`,
{
cause: failure,
cause:
failure === undefined
? lineageFailure
: new AggregateError(
[failure, lineageFailure],
"MIPS16 shared verification failed and cleanup was unverified",
),
},
);
if (failure !== undefined)
@@ -352,5 +401,14 @@ if (failure !== undefined)
`MIPS16 shared verification failed; retained workspace: ${workspace}`,
{ cause: failure },
);
const interrupted = commandInterruption.reason();
if (interrupted !== undefined)
throw retainWorkspace(
`MIPS16 shared verification interrupted; retained diagnostic workspace: ${workspace}`,
{ cause: interrupted },
);
commandInterruption.retainWorkspaceIfInterrupted(workspaceFixture);
await workspaceFixture.release();
const releaseInterrupted = finishCommandInterruption();
if (releaseInterrupted !== undefined) throw releaseInterrupted;
console.log(JSON.stringify({ ...completed, fixtures: reports }, null, 2));
+78 -15
View File
@@ -1,11 +1,9 @@
#!/usr/bin/env node
import assert from "node:assert/strict";
import { execFile } from "node:child_process";
import { createHash } from "node:crypto";
import { mkdir, readFile } from "node:fs/promises";
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";
@@ -20,7 +18,13 @@ import {
mipsSharedPlacementDelta,
} from "../tests/conformance/ghidra/mips-shared-checks.mjs";
import { requireMcpOperationResult } from "./lib/mcp-verifier-results.mjs";
import { createVerifierRun, completeVerifierRun } from "./lib/verifier-run.mjs";
import { createVerifierCommands } from "./lib/verify-commands.mjs";
import { bindVerifierCommandInterruption } from "./lib/verifier-command-interruption.mjs";
import {
assertVerifierCleanupComplete,
createVerifierRun,
completeVerifierRun,
} from "./lib/verifier-run.mjs";
import { VerifierFixture } from "./lib/verifier-fixture.mjs";
import { requireLaneToolchain } from "../tests/conformance/ghidra/fixture-toolchain.mjs";
@@ -45,7 +49,6 @@ 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();
// Refuse a host without the fixture toolchain before allocating anything. The
@@ -77,6 +80,12 @@ const env = {
for (const setting of ["GHIDRA_HEADLESS_MAXMEM", "GHIDRA_MAXMEM"]) {
if (process.env[setting] !== undefined) env[setting] = process.env[setting];
}
// The public CLI launch uses this command owner. A timeout stops its process group.
const commands = await createVerifierCommands();
const commandInterruption = bindVerifierCommandInterruption(commands);
const execOwned = commandInterruption.exec;
const { untilInterrupted, throwIfInterrupted } = commandInterruption;
const finishCommandInterruption = () => commandInterruption.finish();
/** Read every word the oracle checks through the public read_bytes operation. */
async function loadedWords(call, independent, delta, byteOrder) {
@@ -108,11 +117,17 @@ async function loadedWords(call, independent, delta, byteOrder) {
}
async function verifyFixture(byteOrder, softFloat) {
const fixture = await buildMipsSharedFixture(workspace, byteOrder, softFloat);
const fixture = await buildMipsSharedFixture(
workspace,
byteOrder,
softFloat,
execOwned,
);
const independent = await inspectMipsSharedReadelf(
fixture.path,
byteOrder,
fixture.float_abi,
execOwned,
);
const resolved = await parseBinaryTarget(fixture.path);
if (!resolved.ok) throw resolved.error;
@@ -158,13 +173,20 @@ async function verifyFixture(byteOrder, softFloat) {
transport.stderr?.on("data", (chunk) => {
stderr = (stderr + chunk.toString()).slice(-65536);
});
const call = async (name, args = {}) =>
requireMcpOperationResult(
await client.callTool({ name, arguments: args }, { timeout: 180000 }),
const call = async (name, args = {}) => {
throwIfInterrupted();
return requireMcpOperationResult(
await client.callTool(
{ name, arguments: args },
{ timeout: 180000, signal: commandInterruption.signal },
),
name,
);
};
try {
await client.connect(transport);
await untilInterrupted(
client.connect(transport, { signal: commandInterruption.signal }),
);
const target = await call("open_binary", {
path: fixture.path,
provider_id: "ghidra",
@@ -270,7 +292,7 @@ async function verifyFixture(byteOrder, softFloat) {
assert.deepEqual(await call("list_procedures"), procedures);
await call("close_binary");
// Same public operation through CLI, not a private injected provider.
const cli = await execute(
const cli = await execOwned(
process.execPath,
[
entrypoint,
@@ -334,19 +356,60 @@ try {
} catch (cause) {
primaryFailure = cause;
}
// Close the command owner before the parent-tree check. A failed close stays
// registered for retry and the diagnostic workspace is retained.
let cleanupFailure;
try {
await workspaceFixture.closeEarly([() => commands.close()]);
} catch (cause) {
cleanupFailure = cause;
}
if (cleanupFailure !== undefined)
throw retainWorkspace(
`MIPS32 shared command cleanup failed; retained diagnostic workspace: ${workspace}`,
{
cause:
primaryFailure === undefined
? cleanupFailure
: new AggregateError(
[primaryFailure, cleanupFailure],
"MIPS32 shared verification and command cleanup both failed",
),
},
);
const completed = await completeVerifierRun(run);
if (
completed.process_lineage.status !== "verified" ||
completed.process_lineage.descendants.length !== 0
)
let lineageFailure;
try {
assertVerifierCleanupComplete(completed.process_lineage);
} catch (cause) {
lineageFailure = cause;
}
if (lineageFailure !== undefined)
throw retainWorkspace(
`Cleanup unverified; retained diagnostic workspace: ${workspace}`,
{ cause: primaryFailure },
{
cause:
primaryFailure === undefined
? lineageFailure
: new AggregateError(
[primaryFailure, lineageFailure],
"MIPS32 shared verification failed and cleanup was unverified",
),
},
);
if (primaryFailure !== undefined)
throw retainWorkspace(
`MIPS32 shared-library verification failed; retained workspace: ${workspace}`,
{ cause: primaryFailure },
);
const interrupted = commandInterruption.reason();
if (interrupted !== undefined)
throw retainWorkspace(
`MIPS32 shared-library verification interrupted; retained diagnostic workspace: ${workspace}`,
{ cause: interrupted },
);
commandInterruption.retainWorkspaceIfInterrupted(workspaceFixture);
await workspaceFixture.release();
const releaseInterrupted = finishCommandInterruption();
if (releaseInterrupted !== undefined) throw releaseInterrupted;
console.log(JSON.stringify({ ...completed, fixtures: reports }, null, 2));
+61 -13
View File
@@ -5,19 +5,23 @@ import {
requireMcpLifecycleResult,
requireMcpOutput,
} from "./lib/mcp-verifier-results.mjs";
import { execFile } from "node:child_process";
import { createHash } from "node:crypto";
import { mkdir, readFile, readdir, writeFile } from "node:fs/promises";
import { basename, isAbsolute, join, resolve } 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 { parseEvidence } from "../dist/domain/evidence.js";
import { nativeDataTypeSchema } from "../dist/domain/native/nativeDataType.js";
import { nativeLoadImageSchema } from "../dist/domain/native/nativeLoadImage.js";
import { createVerifierRun, completeVerifierRun } from "./lib/verifier-run.mjs";
import { createVerifierCommands } from "./lib/verify-commands.mjs";
import { bindVerifierCommandInterruption } from "./lib/verifier-command-interruption.mjs";
import {
assertVerifierCleanupComplete,
createVerifierRun,
completeVerifierRun,
} from "./lib/verifier-run.mjs";
import {
VerifierFixture,
withVerifierFixture,
@@ -87,6 +91,10 @@ const workspaceFixture = await VerifierFixture.open({
prefix: "rea-nativeaot-proof-",
});
const workspace = workspaceFixture.path;
const retainWorkspace = (message, options) => {
workspaceFixture.retain(message);
return new Error(message, options);
};
const runtime = join(workspace, "runtime");
await mkdir(runtime);
const packageRoot = process.env.REA_NATIVEAOT_PROOF_PACKAGE_ROOT;
@@ -113,6 +121,12 @@ if (mode === "default-native") delete env.REA_GHIDRA_NATIVEAOT_JAR;
// a simulated producer report. Its class/JAR exist only in the owned workspace.
if (mode === "loader-failure")
env.REA_GHIDRA_NATIVEAOT_JAR = join(workspace, "bad-extension.jar");
// javac, jar, and the optional CLI share one command owner. A timeout stops that process group.
const commands = await createVerifierCommands();
const commandInterruption = bindVerifierCommandInterruption(commands);
const execOwned = commandInterruption.exec;
const { untilInterrupted, throwIfInterrupted } = commandInterruption;
const finishCommandInterruption = () => commandInterruption.finish();
async function prepareLoaderFailure() {
const source = join(workspace, "NativeAotExtension.java");
const classes = join(workspace, "classes");
@@ -131,12 +145,12 @@ public final class NativeAotExtension {
process.env.JAVA_HOME === undefined
? name
: join(process.env.JAVA_HOME, "bin", name);
await promisify(execFile)(
await execOwned(
command("javac"),
["-J-Xmx512m", "-J-XX:ActiveProcessorCount=1", "-d", classes, source],
{ timeout: 60000 },
);
await promisify(execFile)(
await execOwned(
command("jar"),
["--create", "--file", env.REA_GHIDRA_NATIVEAOT_JAR, "-C", classes, "."],
{ timeout: 60000 },
@@ -171,20 +185,23 @@ workspaceFixture.releaseAfter(async () => {
"Owned runtime/project/extension files leaked after close",
);
verifierRun = await completeVerifierRun(run);
assert.equal(verifierRun.process_lineage.status, "verified");
assert.deepEqual(verifierRun.process_lineage.descendants, []);
assertVerifierCleanupComplete(verifierRun.process_lineage);
});
commandInterruption.retainWorkspaceIfInterrupted(workspaceFixture);
await withVerifierFixture(workspaceFixture, async () => {
let primaryFailure;
try {
if (mode === "loader-failure") await prepareLoaderFailure();
await client.connect(transport);
await untilInterrupted(
client.connect(transport, { signal: commandInterruption.signal }),
);
connected = true;
const response = await client.callTool(
{
name: "open_binary",
arguments: { path: target, provider_id: "ghidra" },
},
{ timeout: 360000 },
{ timeout: 360000, signal: commandInterruption.signal },
);
if (
["unsupported", "malformed", "ambiguous", "loader-failure"].includes(mode)
@@ -195,7 +212,7 @@ await withVerifierFixture(workspaceFixture, async () => {
opened = true;
failure = await client.callTool(
{ name: "inspect_native_load_image", arguments: {} },
{ timeout: 360000 },
{ timeout: 360000, signal: commandInterruption.signal },
);
}
assert.equal(failure.isError, true, JSON.stringify(failure));
@@ -358,7 +375,7 @@ await withVerifierFixture(workspaceFixture, async () => {
// One representative CLI boundary, selected explicitly to avoid repeated
// full NativeAOT auto-analysis during each focused iteration.
if (process.env.REA_NATIVEAOT_PROOF_CLI === "1") {
const { stdout } = await promisify(execFile)(
const { stdout } = await execOwned(
process.execPath,
[
entrypoint,
@@ -393,9 +410,39 @@ await withVerifierFixture(workspaceFixture, async () => {
}
} catch (error) {
console.error(stderr);
throw error;
primaryFailure = error;
}
// Close the command owner before release observes the parent tree.
let cleanupFailure;
try {
await workspaceFixture.closeEarly([() => commands.close()]);
} catch (cause) {
cleanupFailure = cause;
}
if (cleanupFailure !== undefined || primaryFailure !== undefined)
throw retainWorkspace(
`NativeAOT verification failed; retained diagnostic workspace: ${workspace}`,
{
cause:
cleanupFailure === undefined
? primaryFailure
: primaryFailure === undefined
? cleanupFailure
: new AggregateError(
[primaryFailure, cleanupFailure],
"NativeAOT verification and command cleanup both failed",
),
},
);
const interrupted = commandInterruption.reason();
if (interrupted !== undefined)
throw retainWorkspace(
`NativeAOT verification interrupted; retained diagnostic workspace: ${workspace}`,
{ cause: interrupted },
);
});
const releaseInterrupted = finishCommandInterruption();
if (releaseInterrupted !== undefined) throw releaseInterrupted;
const capture = process.env.REA_NATIVEAOT_PROOF_CAPTURE_DIR;
if (capture !== undefined) {
assert.ok(isAbsolute(capture));
@@ -418,9 +465,10 @@ function hash(bytes) {
return createHash("sha256").update(bytes).digest("hex");
}
async function call(name, args = {}) {
throwIfInterrupted();
const response = await client.callTool(
{ name, arguments: args },
{ timeout: 360000 },
{ timeout: 360000, signal: commandInterruption.signal },
);
if (name === "close_binary") return requireMcpLifecycleResult(response, name);
const evidence = parseEvidence(requireMcpOutput(response, name));
+230 -160
View File
@@ -1,10 +1,8 @@
import assert from "node:assert/strict";
import { execFile } from "node:child_process";
import { createHash } from "node:crypto";
import { mkdir, readFile } from "node:fs/promises";
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import { promisify } from "node:util";
import { parseBinaryTarget } from "../dist/application/BinaryTargetResolver.js";
import { parseConfig } from "../dist/config/parseConfig.js";
@@ -18,13 +16,18 @@ import {
ProviderProcessSupervisor,
spawnOwnedProviderProcess,
} from "../dist/process/ProviderProcess.js";
import { completeVerifierRun, createVerifierRun } from "./lib/verifier-run.mjs";
import { createVerifierCommands } from "./lib/verify-commands.mjs";
import { bindVerifierCommandInterruption } from "./lib/verifier-command-interruption.mjs";
import {
assertVerifierCleanupComplete,
completeVerifierRun,
createVerifierRun,
} from "./lib/verifier-run.mjs";
import {
VerifierFixture,
withVerifierFixture,
} from "./lib/verifier-fixture.mjs";
const exec = promisify(execFile);
const run = createVerifierRun();
const procedureName = (name) =>
process.platform === "darwin" ? `_${name}` : name;
@@ -53,187 +56,254 @@ const workspaceFixture = await VerifierFixture.open({
prefix: "rea-ghidra-noreturn-",
});
const workspace = workspaceFixture.path;
const retainWorkspace = (message, options) => {
workspaceFixture.retain(message);
return new Error(message, options);
};
let cleaned = true;
let report;
let verifierRun;
// cc uses this command owner. A timeout stops its process group.
const commands = await createVerifierCommands();
const commandInterruption = bindVerifierCommandInterruption(commands);
const execOwned = commandInterruption.exec;
const { untilInterrupted, throwIfInterrupted } = commandInterruption;
const finishCommandInterruption = () => commandInterruption.finish();
const digest = (bytes) => createHash("sha256").update(bytes).digest("hex");
// The workspace stays while an owned Ghidra process or server may still use
// it: this cleanup confirms only once their cleanup was verified.
workspaceFixture.releaseAfter(() => {
workspaceFixture.releaseAfter(async () => {
if (!cleaned)
throw new Error(
`Owned Ghidra cleanup is unverified; retaining ${workspace}.`,
);
verifierRun = await completeVerifierRun(run);
assertVerifierCleanupComplete(verifierRun.process_lineage);
});
commandInterruption.retainWorkspaceIfInterrupted(workspaceFixture);
await withVerifierFixture(workspaceFixture, async () => {
const targetPath = join(
workspace,
process.platform === "darwin" ? "noreturn.dylib" : "noreturn.so",
);
const targetOptions =
process.platform === "darwin"
? [
"-dynamiclib",
"-undefined",
"dynamic_lookup",
"-U_FORTIFY_SOURCE",
"-D_FORTIFY_SOURCE=0",
]
: ["-shared"];
await exec(process.env.REA_CC ?? "cc", [
...targetOptions,
"-fPIC",
"-O0",
"-fno-builtin",
join(fixtures, "no-return.c"),
"-o",
targetPath,
]);
const targetDigest = digest(await readFile(targetPath));
const sourceDigests = Object.fromEntries(
await Promise.all(
["ReaGhidraBridge.java", "ReaGhidraNoReturnFix.java"].map(
async (name) => [name, digest(await readFile(join(bridge, name)))],
),
),
);
const project = join(workspace, "project");
await mkdir(project);
const launch = await spawnOwnedProviderProcess({
command: installation.analyzeHeadlessPath,
arguments: [
project,
"NoReturn",
"-import",
let primaryFailure;
try {
const targetPath = join(
workspace,
process.platform === "darwin" ? "noreturn.dylib" : "noreturn.so",
);
const targetOptions =
process.platform === "darwin"
? [
"-dynamiclib",
"-undefined",
"dynamic_lookup",
"-U_FORTIFY_SOURCE",
"-D_FORTIFY_SOURCE=0",
]
: ["-shared"];
await execOwned(process.env.REA_CC ?? "cc", [
...targetOptions,
"-fPIC",
"-O0",
"-fno-builtin",
join(fixtures, "no-return.c"),
"-o",
targetPath,
"-readOnly",
"-deleteProject",
"-scriptPath",
`${fixtures};${bridge}`,
"-postScript",
join(fixtures, "ReaNoReturnProbe.java"),
bridge,
],
runId: run.run_id,
expectedCommand: null,
});
const supervisor = new ProviderProcessSupervisor({
...launch,
ownsProcessLifetime: true,
cleanup: () => cleanupOwnedProcessGroup(launch.ownership),
});
let probe;
try {
assert.ok(
await supervisor.waitForExit(120000),
"No-return probe timed out",
]);
const targetDigest = digest(await readFile(targetPath));
const sourceDigests = Object.fromEntries(
await Promise.all(
["ReaGhidraBridge.java", "ReaGhidraNoReturnFix.java"].map(
async (name) => [name, digest(await readFile(join(bridge, name)))],
),
),
);
assert.ok(
await supervisor.waitForOutputClose(1000),
"No-return probe output did not close",
);
const snapshot = supervisor.snapshot();
const output = `${snapshot.stdout.text}\n${snapshot.stderr.text}`;
assert.equal(snapshot.exitCode, 0, output);
const reports = [
...output.matchAll(/REA_NO_RETURN_PROBE_JSON (\{[^\r\n]*\})/gu),
];
assert.equal(reports.length, 1, output);
probe = JSON.parse(reports[0][1]);
assert.equal(probe.status, "passed");
assert.equal(probe.ghidra_version, SUPPORTED_GHIDRA_VERSION);
assert.equal(probe.target_sha256, targetDigest);
for (const [name, expected] of Object.entries(sourceDigests))
assert.equal(probe[name], expected);
assert.ok(probe.checks.includes("independent_entry_preserved"));
assert.ok(probe.checks.includes("propagated_flag_repaired_rea_tls_outer"));
assert.ok(probe.checks.includes("recovered_pseudocode"));
} finally {
const stopped = await supervisor.stop();
cleaned = stopped.status === "verified-cleanup";
assert.ok(
cleaned,
`Owned cleanup incomplete; retained ${workspace}: ${JSON.stringify(stopped)}`,
);
}
// Exercise the actual Java server, authenticated transport, request queue and Evidence
// projection. This read-only TCP lane also runs where AF_UNIX is unavailable; it does
// not claim Windows authority or validation of the default Linux Unix-socket transport.
const provider = new GhidraProvider(
config.value,
silentLogger,
process.env,
undefined,
(options) =>
new GhidraClient({ ...options, transport: "authenticated-loopback-tcp" }),
);
const target = await parseBinaryTarget(targetPath);
if (!target.ok) throw target.error;
const profile = await provider.resolveAnalysisProfile(target.value);
if (!profile.ok) throw profile.error;
assert.ok(profile.value.profile);
assert.equal(
profile.value.profile.parameters.no_return_repair,
"returning-imports-decoded-return",
);
const client = provider.createClient(target.value, profile.value.profile);
cleaned = false;
const observations = [];
const failures = [];
try {
for (const operation of [
"procedure_pseudo_code",
"procedure_assembly",
"procedure_info",
"read_function_instructions",
"analyze_function",
]) {
const result = await client.execute(operation, {
procedure: procedureName("rea_abort"),
});
if (!result.ok) throw result.error;
const warnings = result.value.limitations.filter((value) =>
/terminal call/u.test(value),
const project = join(workspace, "project");
await mkdir(project);
const launch = await spawnOwnedProviderProcess({
command: installation.analyzeHeadlessPath,
arguments: [
project,
"NoReturn",
"-import",
targetPath,
"-readOnly",
"-deleteProject",
"-scriptPath",
`${fixtures};${bridge}`,
"-postScript",
join(fixtures, "ReaNoReturnProbe.java"),
bridge,
],
runId: run.run_id,
expectedCommand: null,
signal: commandInterruption.signal,
});
const supervisor = new ProviderProcessSupervisor({
...launch,
ownsProcessLifetime: true,
cleanup: () => cleanupOwnedProcessGroup(launch.ownership),
});
let probe;
try {
assert.ok(
await untilInterrupted(supervisor.waitForExit(120000)),
"No-return probe timed out",
);
assert.ok(
warnings.some(
(value) => /abort/u.test(value) && /fallthrough/u.test(value),
),
operation,
await untilInterrupted(supervisor.waitForOutputClose(1000)),
"No-return probe output did not close",
);
const snapshot = supervisor.snapshot();
const output = `${snapshot.stdout.text}\n${snapshot.stderr.text}`;
assert.equal(snapshot.exitCode, 0, output);
const reports = [
...output.matchAll(/REA_NO_RETURN_PROBE_JSON (\{[^\r\n]*\})/gu),
];
assert.equal(reports.length, 1, output);
probe = JSON.parse(reports[0][1]);
assert.equal(probe.status, "passed");
assert.equal(probe.ghidra_version, SUPPORTED_GHIDRA_VERSION);
assert.equal(probe.target_sha256, targetDigest);
for (const [name, expected] of Object.entries(sourceDigests))
assert.equal(probe[name], expected);
assert.ok(probe.checks.includes("independent_entry_preserved"));
assert.ok(
probe.checks.includes("propagated_flag_repaired_rea_tls_outer"),
);
assert.ok(probe.checks.includes("recovered_pseudocode"));
} finally {
const stopped = await supervisor.stop();
cleaned = stopped.status === "verified-cleanup";
assert.ok(
cleaned,
`Owned cleanup incomplete; retained ${workspace}: ${JSON.stringify(stopped)}`,
);
observations.push({ operation, warnings });
}
const healthy = await client.execute("procedure_pseudo_code", {
procedure: procedureName("rea_tls"),
});
if (!healthy.ok) throw healthy.error;
assert.ok(
!healthy.value.limitations.some((value) => /terminal call/u.test(value)),
// Exercise the actual Java server, authenticated transport, request queue and Evidence
// projection. This read-only TCP lane also runs where AF_UNIX is unavailable; it does
// not claim Windows authority or validation of the default Linux Unix-socket transport.
const provider = new GhidraProvider(
config.value,
silentLogger,
process.env,
undefined,
(options) =>
new GhidraClient({
...options,
transport: "authenticated-loopback-tcp",
}),
);
const target = await parseBinaryTarget(targetPath);
if (!target.ok) throw target.error;
const profile = await provider.resolveAnalysisProfile(target.value);
if (!profile.ok) throw profile.error;
assert.ok(profile.value.profile);
assert.equal(
profile.value.profile.parameters.no_return_repair,
"returning-imports-decoded-return",
);
const client = provider.createClient(target.value, profile.value.profile);
cleaned = false;
const observations = [];
const failures = [];
try {
for (const operation of [
"procedure_pseudo_code",
"procedure_assembly",
"procedure_info",
"read_function_instructions",
"analyze_function",
]) {
throwIfInterrupted();
const result = await untilInterrupted(
client.execute(
operation,
{ procedure: procedureName("rea_abort") },
{ signal: commandInterruption.signal },
),
);
if (!result.ok) throw result.error;
const warnings = result.value.limitations.filter((value) =>
/terminal call/u.test(value),
);
assert.ok(
warnings.some(
(value) => /abort/u.test(value) && /fallthrough/u.test(value),
),
operation,
);
observations.push({ operation, warnings });
}
throwIfInterrupted();
const healthy = await untilInterrupted(
client.execute(
"procedure_pseudo_code",
{ procedure: procedureName("rea_tls") },
{ signal: commandInterruption.signal },
),
);
if (!healthy.ok) throw healthy.error;
assert.ok(
!healthy.value.limitations.some((value) =>
/terminal call/u.test(value),
),
);
} catch (cause) {
failures.push(cause);
}
const closed = await client.close();
cleaned = closed.ok;
if (!closed.ok) failures.push(closed.error);
if (failures.length > 0)
throw new AggregateError(
failures,
"No-return Evidence verification failed",
);
assert.equal(digest(await readFile(targetPath)), targetDigest);
report = {
status: "passed",
probe,
observations,
scope:
"Real Ghidra ELF or Mach-O analysis with injected flags; real read-only TCP Evidence projection. No target execution; exact Amethyst artifact and Windows are unverified.",
};
} catch (cause) {
failures.push(cause);
primaryFailure = cause;
}
const closed = await client.close();
cleaned = closed.ok;
if (!closed.ok) failures.push(closed.error);
if (failures.length > 0)
throw new AggregateError(
failures,
"No-return Evidence verification failed",
// Close the command owner before release observes the parent tree.
let cleanupFailure;
try {
await workspaceFixture.closeEarly([() => commands.close()]);
} catch (cause) {
cleanupFailure = cause;
}
if (cleanupFailure !== undefined || primaryFailure !== undefined)
throw retainWorkspace(
`No-return verification failed; retained diagnostic workspace: ${workspace}`,
{
cause:
cleanupFailure === undefined
? primaryFailure
: primaryFailure === undefined
? cleanupFailure
: new AggregateError(
[primaryFailure, cleanupFailure],
"No-return verification and command cleanup both failed",
),
},
);
const interrupted = commandInterruption.reason();
if (interrupted !== undefined)
throw retainWorkspace(
`No-return verification interrupted; retained diagnostic workspace: ${workspace}`,
{ cause: interrupted },
);
assert.equal(digest(await readFile(targetPath)), targetDigest);
report = {
status: "passed",
probe,
observations,
scope:
"Real Ghidra ELF or Mach-O analysis with injected flags; real read-only TCP Evidence projection. No target execution; exact Amethyst artifact and Windows are unverified.",
};
});
const releaseInterrupted = finishCommandInterruption();
if (releaseInterrupted !== undefined) throw releaseInterrupted;
console.log(
JSON.stringify({
...report,
cleanup: "complete",
verifier_run: await completeVerifierRun(run),
verifier_run: verifierRun,
}),
);
+116 -37
View File
@@ -1,11 +1,9 @@
import { withCanonicalOutputSchema } from "./lib/canonical-output-schema.mjs";
import assert from "node:assert/strict";
import { execFile } from "node:child_process";
import { createHash } from "node:crypto";
import { mkdir, readFile, readdir, writeFile } from "node:fs/promises";
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";
@@ -22,7 +20,13 @@ import {
requireMcpOutput,
requireMcpToolError,
} from "./lib/mcp-verifier-results.mjs";
import { createVerifierRun, completeVerifierRun } from "./lib/verifier-run.mjs";
import { createVerifierCommands } from "./lib/verify-commands.mjs";
import { bindVerifierCommandInterruption } from "./lib/verifier-command-interruption.mjs";
import {
assertVerifierCleanupComplete,
createVerifierRun,
completeVerifierRun,
} from "./lib/verifier-run.mjs";
import {
withVerifierCleanup,
closeVerifierResources,
@@ -56,6 +60,10 @@ const workspaceFixture = await VerifierFixture.open({
prefix: "rea-raw-proof-",
});
const workspace = workspaceFixture.path;
const retainWorkspace = (message, options) => {
workspaceFixture.retain(message);
return new Error(message, options);
};
let fixtureCleaned = true;
const runtime = join(workspace, "runtime");
await mkdir(runtime);
@@ -95,6 +103,12 @@ const env = {
process.env.REA_GHIDRA_DISABLED_ANALYZERS,
}),
};
// The public CLI launch uses this command owner. A timeout stops its process group.
const commands = await createVerifierCommands();
const commandInterruption = bindVerifierCommandInterruption(commands);
const execOwned = commandInterruption.exec;
const { untilInterrupted, throwIfInterrupted } = commandInterruption;
const finishCommandInterruption = () => commandInterruption.finish();
const transport = new StdioClientTransport({
command: process.execPath,
args: [entrypoint, "mcp"],
@@ -129,16 +143,21 @@ workspaceFixture.releaseAfter(async () => {
"Owned Java fixture cleanup remains pending",
);
verifierRun = await completeVerifierRun(run);
assert.equal(verifierRun.process_lineage.status, "verified");
assert.deepEqual(verifierRun.process_lineage.descendants, []);
assertVerifierCleanupComplete(verifierRun.process_lineage);
});
commandInterruption.retainWorkspaceIfInterrupted(workspaceFixture);
await withVerifierFixture(workspaceFixture, async () => {
let primaryFailure;
try {
await client.connect(transport);
connected = true;
const listedSearch = (await client.listTools()).tools.find(
(tool) => tool.name === "search_bytes",
await untilInterrupted(
client.connect(transport, { signal: commandInterruption.signal }),
);
connected = true;
const listedSearch = (
await untilInterrupted(
client.listTools(undefined, { signal: commandInterruption.signal }),
)
).tools.find((tool) => tool.name === "search_bytes");
assert.ok(listedSearch);
searchDefinition = withCanonicalOutputSchema(listedSearch);
const profiles = [];
@@ -221,23 +240,33 @@ await withVerifierFixture(workspaceFixture, async () => {
name: "retained_after_failed_open",
comment: "Owned session annotation",
});
const rejected = await client.callTool({
name: "open_binary",
arguments: {
throwIfInterrupted();
const rejected = await client.callTool(
{
name: "open_binary",
arguments: {
path,
format: "raw-binary",
architecture: "arm64",
entry_point: "0x8000001",
},
},
{ signal: commandInterruption.signal },
);
const rejection = requireMcpToolError(rejected);
// admitRawBinary refuses an arm64 entry that is not a multiple of 4.
// The completion facts are the session that the failed open must leave in place.
assert.partialDeepStrictEqual(rejection, {
code: "invalid_request",
category: "invalid_input",
details: {
path,
format: "raw-binary",
architecture: "arm64",
entry_point: "0x8000001",
reason: "raw-binary arm64 entry_point 0x8000001 must be 4-byte aligned",
constraint: "load_selection_rejected",
target_at_open_completion: { entry_point: "0x9000000" },
provider_id_at_open_completion: "ghidra",
},
});
assert.equal(rejected.isError, true);
assert.match(JSON.stringify(rejected.content), /must be 4-byte aligned/);
const rejection = JSON.parse(rejected.content[0].text).error;
assert.equal(
rejection.details.target_at_open_completion.entry_point,
"0x9000000",
);
assert.equal(rejection.details.provider_id_at_open_completion, "ghidra");
const unchanged = await call("binary_session");
assert.equal(unchanged.analysis_run.run_id, sessions[2]);
assert.equal(unchanged.entry_point, "0x9000000");
@@ -256,7 +285,7 @@ await withVerifierFixture(workspaceFixture, async () => {
);
await call("close_binary");
const { stdout } = await promisify(execFile)(
const { stdout } = await execOwned(
process.execPath,
[
entrypoint,
@@ -384,10 +413,17 @@ await withVerifierFixture(workspaceFixture, async () => {
overlapping.normalized_result.matches.map((match) => match.address),
["0x1002", "0x1003"],
);
const wrongCursor = await client.callTool({
name: "search_bytes",
arguments: { pattern: "c3", cursor: page.normalized_result.next_cursor },
});
throwIfInterrupted();
const wrongCursor = await client.callTool(
{
name: "search_bytes",
arguments: {
pattern: "c3",
cursor: page.normalized_result.next_cursor,
},
},
{ signal: commandInterruption.signal },
);
const cursorError = requireMcpToolError(wrongCursor);
assert.partialDeepStrictEqual(cursorError, {
code: "invalid_request",
@@ -625,12 +661,39 @@ await withVerifierFixture(workspaceFixture, async () => {
};
} catch (error) {
if (stderr.trim()) process.stderr.write(stderr);
throw error;
primaryFailure = error;
}
// Close the command owner before release observes the parent tree.
let cleanupFailure;
try {
await workspaceFixture.closeEarly([() => commands.close()]);
} catch (cause) {
cleanupFailure = cause;
}
if (cleanupFailure !== undefined || primaryFailure !== undefined)
throw retainWorkspace(
`Raw-binary verification failed; retained workspace: ${workspace}`,
{
cause:
cleanupFailure === undefined
? primaryFailure
: primaryFailure === undefined
? cleanupFailure
: new AggregateError(
[primaryFailure, cleanupFailure],
"Raw-binary verification and command cleanup both failed",
),
},
);
const interrupted = commandInterruption.reason();
if (interrupted !== undefined)
throw retainWorkspace(
`Raw-binary verification interrupted; retained diagnostic workspace: ${workspace}`,
{ cause: interrupted },
);
});
verifierRun = await completeVerifierRun(run);
assert.equal(verifierRun.process_lineage.status, "verified");
assert.deepEqual(verifierRun.process_lineage.descendants, []);
const releaseInterrupted = finishCommandInterruption();
if (releaseInterrupted !== undefined) throw releaseInterrupted;
process.stdout.write(
JSON.stringify({
verifier_run: verifierRun,
@@ -645,10 +708,12 @@ async function call(
targetSha256 = sha256,
targetFormat = "raw-binary",
) {
throwIfInterrupted();
const result = await client.callTool(
{ name, arguments: args },
{
timeout: 240000,
signal: commandInterruption.signal,
...(name === "search_bytes" ? { toolDefinition: searchDefinition } : {}),
},
);
@@ -713,6 +778,7 @@ async function verifyCrossBlockProject(target, targetSha256) {
env,
runId: run.run_id,
expectedCommand: null,
signal: commandInterruption.signal,
});
fixtureCleaned = false;
const supervisor = new ProviderProcessSupervisor({
@@ -721,9 +787,12 @@ async function verifyCrossBlockProject(target, targetSha256) {
cleanup: () => cleanupOwnedProcessGroup(launch.ownership),
});
await withVerifierCleanup(async () => {
assert.ok(await supervisor.waitForExit(180000), "Fixture setup timed out");
assert.ok(
await supervisor.waitForOutputClose(1000),
await untilInterrupted(supervisor.waitForExit(180000)),
"Fixture setup timed out",
);
assert.ok(
await untilInterrupted(supervisor.waitForOutputClose(1000)),
"Fixture output did not close",
);
const snapshot = supervisor.snapshot();
@@ -755,16 +824,26 @@ async function verifyCrossBlockProject(target, targetSha256) {
version: "1",
});
try {
await projectClient.connect(projectTransport);
const definition = (await projectClient.listTools()).tools.find(
(tool) => tool.name === "search_bytes",
await untilInterrupted(
projectClient.connect(projectTransport, {
signal: commandInterruption.signal,
}),
);
const definition = (
await untilInterrupted(
projectClient.listTools(undefined, {
signal: commandInterruption.signal,
}),
)
).tools.find((tool) => tool.name === "search_bytes");
assert.ok(definition);
const projectCall = async (name, args) => {
throwIfInterrupted();
const reply = await projectClient.callTool(
{ name, arguments: args },
{
timeout: 240000,
signal: commandInterruption.signal,
...(name === "search_bytes" ? { toolDefinition: definition } : {}),
},
);
+94 -30
View File
@@ -1,12 +1,10 @@
#!/usr/bin/env node
import { canonicalOutputSchema } from "./lib/canonical-output-schema.mjs";
import assert from "node:assert/strict";
import { execFile } from "node:child_process";
import { createHash } from "node:crypto";
import { mkdir, readFile, readdir, writeFile } from "node:fs/promises";
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 Ajv from "ajv/dist/2020.js";
@@ -34,7 +32,13 @@ import {
requireMcpOutput,
requireMcpToolError,
} from "./lib/mcp-verifier-results.mjs";
import { createVerifierRun, completeVerifierRun } from "./lib/verifier-run.mjs";
import { createVerifierCommands } from "./lib/verify-commands.mjs";
import { bindVerifierCommandInterruption } from "./lib/verifier-command-interruption.mjs";
import {
assertVerifierCleanupComplete,
createVerifierRun,
completeVerifierRun,
} from "./lib/verifier-run.mjs";
import { VerifierFixture } from "./lib/verifier-fixture.mjs";
import { requireLaneToolchain } from "../tests/conformance/ghidra/fixture-toolchain.mjs";
@@ -59,7 +63,6 @@ const installation = await inspectVerifierGhidraInstallation({
assert.equal(installation.status, "available", JSON.stringify(installation));
assert.equal(installation.providerVersion, SUPPORTED_GHIDRA_VERSION);
const execute = promisify(execFile);
const entrypoint = fileURLToPath(new URL("./rea.mjs", import.meta.url));
const run = createVerifierRun();
// Refuse a host without the fixture toolchain before allocating anything.
@@ -88,6 +91,12 @@ const env = {
};
for (const key of ["GHIDRA_HEADLESS_MAXMEM", "GHIDRA_MAXMEM"])
if (process.env[key] !== undefined) env[key] = process.env[key];
// Public CLI launches share one command owner. A timeout stops that process group.
const commands = await createVerifierCommands();
const commandInterruption = bindVerifierCommandInterruption(commands);
const execOwned = commandInterruption.exec;
const { untilInterrupted, throwIfInterrupted } = commandInterruption;
const finishCommandInterruption = () => commandInterruption.finish();
const transport = new StdioClientTransport({
command: process.execPath,
args: [entrypoint, "mcp"],
@@ -135,8 +144,12 @@ const OUTCOME =
const reports = [];
let failure;
try {
await client.connect(transport);
const catalog = await client.listTools();
await untilInterrupted(
client.connect(transport, { signal: commandInterruption.signal }),
);
const catalog = await untilInterrupted(
client.listTools(undefined, { signal: commandInterruption.signal }),
);
const ajv = new Ajv({ strict: false });
addFormats(ajv);
const validators = new Map();
@@ -155,16 +168,19 @@ try {
);
}
const typedSoft = await buildRiscvFixture(workspace, 32, "soft");
const callSites = await inspectRiscvCallSites(typedSoft.path);
const typedSoft = await buildRiscvFixture(workspace, 32, {
floatAbi: "soft",
execute: execOwned,
});
const callSites = await inspectRiscvCallSites(typedSoft.path, execOwned);
assertRiscvIlp32CallSites(callSites.calls);
const fixtures = [
await buildRiscvFixture(workspace, 32),
await buildRiscvFixture(workspace, 64),
await buildRiscvFixture(workspace, 32, { pie: true }),
await buildRiscvFixture(workspace, 64, { pie: true }),
await buildRiscvFixture(workspace, 32, { execute: execOwned }),
await buildRiscvFixture(workspace, 64, { execute: execOwned }),
await buildRiscvFixture(workspace, 32, { pie: true, execute: execOwned }),
await buildRiscvFixture(workspace, 64, { pie: true, execute: execOwned }),
typedSoft,
await stripRiscvFixture(workspace, typedSoft),
await stripRiscvFixture(workspace, typedSoft, execOwned),
];
for (const fixture of fixtures) {
const bits = fixture.riscv.elfClass;
@@ -208,9 +224,10 @@ try {
}
};
const call = async (name, args = {}) => {
throwIfInterrupted();
const reply = await client.callTool(
{ name, arguments: args },
{ timeout: 240000 },
{ timeout: 240000, signal: commandInterruption.signal },
);
const lifecycle = [
"open_binary",
@@ -382,7 +399,7 @@ try {
const snapshotPath = join(
workspace,
`${fixture.path.split(/[\/\\]/).at(-1)}.snapshot.json`,
`${fixture.path.split(/[/\\]/).at(-1)}.snapshot.json`,
);
const saved = await call("close_binary", { snapshot_path: snapshotPath });
assert.ok(saved.evidence_records > 0);
@@ -422,7 +439,7 @@ try {
[stripped ? addressFor("rea_riscv_entry") : "rea_riscv_entry"],
],
]) {
const cli = await execute(
const cli = await execOwned(
process.execPath,
[
entrypoint,
@@ -473,7 +490,7 @@ try {
name: "open_binary",
arguments: { path: sharedPath, provider_id: "ghidra" },
},
{ timeout: 60000 },
{ timeout: 60000, signal: commandInterruption.signal },
);
const error = requireMcpToolError(reply);
assert.equal(error.details?.selection_reason, "target_unsupported");
@@ -520,7 +537,7 @@ try {
name: "open_binary",
arguments: { path: mutatedPath, provider_id: "ghidra" },
},
{ timeout: 240000 },
{ timeout: 240000, signal: commandInterruption.signal },
);
const cliArguments = [
entrypoint,
@@ -546,7 +563,7 @@ try {
requireMcpLifecycleResult(
await client.callTool(
{ name: "close_binary", arguments: {} },
{ timeout: 240000 },
{ timeout: 240000, signal: commandInterruption.signal },
),
"close_binary",
);
@@ -561,7 +578,11 @@ try {
arch: fixture.riscv.isa.arch,
},
};
const cli = await execute(process.execPath, cliArguments, cliOptions);
const cli = await execOwned(
process.execPath,
cliArguments,
cliOptions,
);
assertProfile(
parseEvidence(JSON.parse(cli.stdout)).analysis_profile,
want,
@@ -575,11 +596,15 @@ try {
JSON.stringify(error),
);
await assert.rejects(
execute(process.execPath, cliArguments, cliOptions),
(cause) =>
Number.isInteger(cause.code) &&
cause.code !== 0 &&
`${cause.stdout}\n${cause.stderr}`.includes(outcome),
execOwned(process.execPath, cliArguments, cliOptions),
(cause) => {
const output = `${cause?.snapshot?.stdout?.text ?? ""}\n${cause?.snapshot?.stderr?.text ?? ""}`;
return (
Number.isInteger(cause?.code) &&
cause.code !== 0 &&
output.includes(outcome)
);
},
);
headers.push({ flags, outcome: "refused", reason: outcome });
}
@@ -636,15 +661,45 @@ for (const close of [() => client.close(), () => transport.close()]) {
);
}
}
// Close the command owner before the parent-tree check. A failed close stays
// registered for retry and the diagnostic workspace is retained.
let cleanupFailure;
try {
await workspaceFixture.closeEarly([() => commands.close()]);
} catch (cause) {
cleanupFailure = cause;
}
if (cleanupFailure !== undefined)
throw retainWorkspace(
`RISC-V command cleanup failed; retained diagnostic workspace: ${workspace}`,
{
cause:
failure === undefined
? cleanupFailure
: new AggregateError(
[failure, cleanupFailure],
"RISC-V verification and command cleanup both failed",
),
},
);
const completed = await completeVerifierRun(run);
if (
completed.process_lineage.status !== "verified" ||
completed.process_lineage.descendants.length !== 0
)
let lineageFailure;
try {
assertVerifierCleanupComplete(completed.process_lineage);
} catch (cause) {
lineageFailure = cause;
}
if (lineageFailure !== undefined)
throw retainWorkspace(
`Cleanup unverified; retained workspace: ${workspace}`,
{
cause: failure,
cause:
failure === undefined
? lineageFailure
: new AggregateError(
[failure, lineageFailure],
"RISC-V verification failed and cleanup was unverified",
),
},
);
if (failure !== undefined)
@@ -657,7 +712,16 @@ if (survivors.length > 0)
throw retainWorkspace(
`Runtime resources survived server shutdown (${survivors.join(", ")}); retained workspace: ${workspace}`,
);
const interrupted = commandInterruption.reason();
if (interrupted !== undefined)
throw retainWorkspace(
`RISC-V verification interrupted; retained diagnostic workspace: ${workspace}`,
{ cause: interrupted },
);
commandInterruption.retainWorkspaceIfInterrupted(workspaceFixture);
await workspaceFixture.release();
const releaseInterrupted = finishCommandInterruption();
if (releaseInterrupted !== undefined) throw releaseInterrupted;
console.log(
JSON.stringify(
{
+171 -111
View File
@@ -1,11 +1,9 @@
#!/usr/bin/env node
import { withVerifierCleanup } from "./lib/verifier-cleanup.mjs";
import assert from "node:assert/strict";
import { execFile } from "node:child_process";
import { copyFile, mkdir, readFile, readdir } from "node:fs/promises";
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 { inspectVerifierGhidraInstallation } from "./lib/ghidra-installation-inspection.mjs";
@@ -15,13 +13,18 @@ import {
spawnOwnedProviderProcess,
} from "../dist/process/ProviderProcess.js";
import { requireMcpOperationResult } from "./lib/mcp-verifier-results.mjs";
import { createVerifierRun, completeVerifierRun } from "./lib/verifier-run.mjs";
import { createVerifierCommands } from "./lib/verify-commands.mjs";
import { bindVerifierCommandInterruption } from "./lib/verifier-command-interruption.mjs";
import {
assertVerifierCleanupComplete,
createVerifierRun,
completeVerifierRun,
} from "./lib/verifier-run.mjs";
import {
VerifierFixture,
withVerifierFixture,
} from "./lib/verifier-fixture.mjs";
const execute = promisify(execFile);
const run = createVerifierRun();
const installation = await inspectVerifierGhidraInstallation({
environment: process.env,
@@ -32,9 +35,24 @@ if (process.platform !== "darwin" || installation.status !== "available")
throw new Error(
"verify:ghidra:swift requires macOS, Ghidra 12.2 with its native decompiler, and JDK 25; set GHIDRA_INSTALL_DIR and JAVA_HOME.",
);
// swiftc and clang share one command owner, including the pre-workspace probe.
const commands = await createVerifierCommands();
const commandInterruption = bindVerifierCommandInterruption(commands);
const execOwned = commandInterruption.exec;
const { untilInterrupted, throwIfInterrupted } = commandInterruption;
const finishCommandInterruption = () => commandInterruption.finish();
try {
await execute("swiftc", ["--version"], { timeout: 10_000 });
await execOwned("swiftc", ["--version"], { timeout: 10_000 });
} catch (cause) {
try {
await commands.close();
} catch (closeCause) {
throw new AggregateError(
[cause, closeCause],
"verify:ghidra:swift requires the host Swift compiler (swiftc), and closing its command owner failed.",
);
}
if (commandInterruption.signal.aborted) throw cause;
throw new Error(
"verify:ghidra:swift requires the host Swift compiler (swiftc).",
{ cause },
@@ -46,6 +64,10 @@ const rootFixture = await VerifierFixture.open({
private: false,
});
const root = rootFixture.path;
const retainWorkspace = (message, options) => {
rootFixture.retain(message);
return new Error(message, options);
};
const client = new Client({ name: "ghidra-swift-verifier", version: "1" });
const transport = new StdioClientTransport({
command: process.execPath,
@@ -86,124 +108,161 @@ rootFixture.releaseAfter(() => transport.close());
rootFixture.releaseAfter(async () => {
assert.equal(decoderCleaned, true, "Owned decoder cleanup remains pending");
verifierRun = await completeVerifierRun(run);
assert.equal(verifierRun.process_lineage.status, "verified");
assert.deepEqual(verifierRun.process_lineage.descendants, []);
assertVerifierCleanupComplete(verifierRun.process_lineage);
});
commandInterruption.retainWorkspaceIfInterrupted(rootFixture);
await withVerifierFixture(rootFixture, async () => {
await execute("swiftc", ["-O", "-g", source, "-o", binary], {
timeout: 60_000,
});
await mkdir(join(root, "copied"));
const copied = join(root, "copied", "swift-inventory");
await copyFile(binary, copied);
// Decoding covers AArch64 register pairs, so build that slice on any Mac host.
for (const { source: assembly, object } of literalObjects)
await execute(
"xcrun",
let primaryFailure;
try {
await execOwned("swiftc", ["-O", "-g", source, "-o", binary], {
timeout: 60_000,
});
await mkdir(join(root, "copied"));
const copied = join(root, "copied", "swift-inventory");
await copyFile(binary, copied);
// Decoding covers AArch64 register pairs, so build that slice on any Mac host.
for (const { source: assembly, object } of literalObjects)
await execOwned(
"xcrun",
[
"clang",
"-target",
"arm64-apple-macosx13.0",
"-c",
assembly,
"-o",
object,
],
{ timeout: 60_000 },
);
await execOwned(
"swiftc",
[
"clang",
"-O",
"-target",
"arm64-apple-macosx13.0",
"-c",
assembly,
literalSource,
...literalObjects.map(({ object }) => object),
"-o",
object,
literalBinary,
],
{ timeout: 60_000 },
);
await execute(
"swiftc",
[
"-O",
"-target",
"arm64-apple-macosx13.0",
literalSource,
...literalObjects.map(({ object }) => object),
"-o",
literalBinary,
],
{ timeout: 60_000 },
);
await verifyDecoderSafeguards();
await client.connect(transport);
connected = true;
for (const [variant, path] of [
["debug", binary],
["without-dsym", copied],
]) {
await call("open_binary", { path, provider_id: "ghidra" });
await withVerifierCleanup(async () => {
const procedures = await call("list_procedures", {});
const inventory = await call("analyze_swift_types", {});
assert(
inventory.total > 0,
"Swift procedures disappeared from the inventory",
);
assert(
inventory.categories.other.count > 0,
"Top-level Swift symbols were lost",
);
const structs = await call("analyze_swift_types", {
category: "structs",
});
assert(
structs.categories.structs.count > 0,
"Modern struct symbols were lost",
);
const metadata = await call("analyze_swift_types", {
category: "structs",
pattern: "VMa",
});
assert.equal(
metadata.total,
2,
"Pair and Scorer metadata accessors must both be classified",
);
const pair = await call("analyze_swift_types", {
category: "structs",
pattern: "Pair",
});
assert(pair.total > 0, "Literal name filtering lost Pair symbols");
const unresolved = await call("analyze_swift_types", {
category: "structs",
pattern: "swiftIndirect",
});
assert(
unresolved.unclassified.length > 0,
"Unsupported categories must remain explicit",
);
const entries = Object.values(inventory.categories).flatMap(
(category) => category.items,
);
assert.equal(
new Set(entries.map((entry) => entry.name)).size,
inventory.total,
);
assert(
!entries.some(
(entry) =>
entry.name === "entry" || entry.name === "_swift_allocObject",
),
);
for (const entry of entries)
await verifyDecoderSafeguards();
await untilInterrupted(
client.connect(transport, { signal: commandInterruption.signal }),
);
connected = true;
for (const [variant, path] of [
["debug", binary],
["without-dsym", copied],
]) {
await call("open_binary", { path, provider_id: "ghidra" });
await withVerifierCleanup(async () => {
const procedures = await call("list_procedures", {});
const inventory = await call("analyze_swift_types", {});
assert(
procedures.some(
(procedure) =>
procedure.address === entry.address &&
procedure.value === entry.name,
inventory.total > 0,
"Swift procedures disappeared from the inventory",
);
assert(
inventory.categories.other.count > 0,
"Top-level Swift symbols were lost",
);
const structs = await call("analyze_swift_types", {
category: "structs",
});
assert(
structs.categories.structs.count > 0,
"Modern struct symbols were lost",
);
const metadata = await call("analyze_swift_types", {
category: "structs",
pattern: "VMa",
});
assert.equal(
metadata.total,
2,
"Pair and Scorer metadata accessors must both be classified",
);
const pair = await call("analyze_swift_types", {
category: "structs",
pattern: "Pair",
});
assert(pair.total > 0, "Literal name filtering lost Pair symbols");
const unresolved = await call("analyze_swift_types", {
category: "structs",
pattern: "swiftIndirect",
});
assert(
unresolved.unclassified.length > 0,
"Unsupported categories must remain explicit",
);
const entries = Object.values(inventory.categories).flatMap(
(category) => category.items,
);
assert.equal(
new Set(entries.map((entry) => entry.name)).size,
inventory.total,
);
assert(
!entries.some(
(entry) =>
entry.name === "entry" || entry.name === "_swift_allocObject",
),
);
reports.push({
variant,
total: inventory.total,
structs: structs.total,
metadata_accessors: metadata.total,
unclassified: inventory.unclassified.length,
});
}, [() => call("close_binary", {})]);
for (const entry of entries)
assert(
procedures.some(
(procedure) =>
procedure.address === entry.address &&
procedure.value === entry.name,
),
);
reports.push({
variant,
total: inventory.total,
structs: structs.total,
metadata_accessors: metadata.total,
unclassified: inventory.unclassified.length,
});
}, [() => call("close_binary", {})]);
}
literalReport = await verifyStringLiterals();
} catch (cause) {
primaryFailure = cause;
}
literalReport = await verifyStringLiterals();
// Close the command owner before release observes the parent tree.
let cleanupFailure;
try {
await rootFixture.closeEarly([() => commands.close()]);
} catch (cause) {
cleanupFailure = cause;
}
if (cleanupFailure !== undefined || primaryFailure !== undefined)
throw retainWorkspace(
`Swift verification failed; retained diagnostic workspace: ${root}`,
{
cause:
cleanupFailure === undefined
? primaryFailure
: primaryFailure === undefined
? cleanupFailure
: new AggregateError(
[primaryFailure, cleanupFailure],
"Swift verification and command cleanup both failed",
),
},
);
const interrupted = commandInterruption.reason();
if (interrupted !== undefined)
throw retainWorkspace(
`Swift verification interrupted; retained diagnostic workspace: ${root}`,
{ cause: interrupted },
);
});
const releaseInterrupted = finishCommandInterruption();
if (releaseInterrupted !== undefined) throw releaseInterrupted;
console.log(
JSON.stringify(
{
@@ -219,9 +278,10 @@ console.log(
);
async function call(name, arguments_) {
throwIfInterrupted();
const response = await client.callTool(
{ name, arguments: arguments_ },
{ timeout: 180_000 },
{ timeout: 180_000, signal: commandInterruption.signal },
);
if (name === "analyze_swift_types")
assert.ok(response.structuredContent, `${name} omitted structured result`);
@@ -375,7 +435,7 @@ async function runDecoderProbe(scripts, report) {
: "Swift decoder probe timed out",
);
assert.ok(
await supervisor.waitForOutputClose(1_000),
await untilInterrupted(supervisor.waitForOutputClose(1_000)),
"Swift decoder probe output did not close",
);
const snapshot = supervisor.snapshot();
+26 -1
View File
@@ -46,6 +46,7 @@ export async function verifySwitchEvidenceControls({
target,
workspace,
runId,
signal,
}) {
const { spawnOwnedProviderProcess, ProviderProcessSupervisor } =
await import("../dist/process/ProviderProcess.js");
@@ -61,6 +62,7 @@ export async function verifySwitchEvidenceControls({
await mkdir(project);
const sourceDigest = digest(await readFile(bridge));
const targetDigest = digest(await readFile(target));
if (signal?.aborted) throw signal.reason;
const launch = await spawnOwnedProviderProcess({
command: installation.analyzeHeadlessPath,
arguments: [
@@ -79,6 +81,7 @@ export async function verifySwitchEvidenceControls({
],
runId,
expectedCommand: null,
...(signal === undefined ? {} : { signal }),
});
const supervisor = new ProviderProcessSupervisor({
...launch,
@@ -87,7 +90,7 @@ export async function verifySwitchEvidenceControls({
});
try {
assert.ok(
await supervisor.waitForExit(120000),
await waitForExitOrSignal(supervisor, signal),
"Production switch guard lane exceeded its 120-second headless deadline",
);
const snapshot = supervisor.snapshot();
@@ -127,6 +130,28 @@ export async function verifySwitchEvidenceControls({
}
}
// The caller may suppress SIGINT and SIGTERM. Stop this wait when that signal
// aborts so `finally` can reach supervisor.stop() before a later SIGKILL.
const waitForExitOrSignal = (supervisor, signal) =>
new Promise((resolve, reject) => {
if (signal?.aborted) {
reject(signal.reason);
return;
}
const onAbort = () => reject(signal.reason);
signal?.addEventListener("abort", onAbort, { once: true });
supervisor.waitForExit(120000).then(
(exited) => {
signal?.removeEventListener("abort", onAbort);
resolve(exited);
},
(cause) => {
signal?.removeEventListener("abort", onAbort);
reject(cause);
},
);
});
function digest(bytes) {
return createHash("sha256").update(bytes).digest("hex");
}
+196 -106
View File
@@ -5,7 +5,6 @@ import {
requireMcpOperationResult,
requireMcpOutput,
} from "./lib/mcp-verifier-results.mjs";
import { execFile } from "node:child_process";
import { createHash } from "node:crypto";
import {
copyFile,
@@ -16,7 +15,7 @@ import {
} from "node:fs/promises";
import { join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { parseArgs, promisify } from "node:util";
import { parseArgs } from "node:util";
import { Client } from "@modelcontextprotocol/client";
import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
import {
@@ -28,7 +27,13 @@ import {
assertSwitchBoundary,
assertSwitchOracleControls,
} from "./verify-real-ghidra-switch-assertions.mjs";
import { createVerifierRun, completeVerifierRun } from "./lib/verifier-run.mjs";
import { createVerifierCommands } from "./lib/verify-commands.mjs";
import { bindVerifierCommandInterruption } from "./lib/verifier-command-interruption.mjs";
import {
assertVerifierCleanupComplete,
createVerifierRun,
completeVerifierRun,
} from "./lib/verifier-run.mjs";
import { verifySwitchEvidenceControls } from "./verify-real-ghidra-switch-controls.mjs";
import {
VerifierFixture,
@@ -51,8 +56,14 @@ assert.ok(
process.platform === "linux" && process.arch === "x64",
"Ghidra switch compiler lane requires Linux x64; macOS/Windows and cross targets use their separate provider lanes.",
);
const exec = promisify(execFile);
const commands = {
// Compiler, reader and CLI launches share one command owner, including the
// pre-workspace version probes. A timeout stops that process group.
const commands = await createVerifierCommands();
const commandInterruption = bindVerifierCommandInterruption(commands);
const execOwned = commandInterruption.exec;
const { untilInterrupted, throwIfInterrupted } = commandInterruption;
const finishCommandInterruption = () => commandInterruption.finish();
const toolchain = {
gcc: process.env.REA_GCC ?? "gcc",
clang: process.env.REA_CLANG ?? "clang",
nm: process.env.REA_NM ?? "nm",
@@ -60,33 +71,62 @@ const commands = {
strip: process.env.REA_STRIP ?? "strip",
};
const versions = {};
for (const [role, command] of Object.entries(commands)) {
try {
for (const [role, command] of Object.entries(toolchain)) {
try {
const { stdout } = await execOwned(command, ["--version"]);
versions[role] = stdout.split("\n")[0];
} catch (cause) {
if (commandInterruption.signal.aborted) throw cause;
throw new Error(
`Ghidra switch compiler lane prerequisite missing: ${role} '${command}'`,
{ cause },
);
}
}
} catch (cause) {
try {
const { stdout } = await exec(command, ["--version"]);
versions[role] = stdout.split("\n")[0];
} catch (cause) {
throw new Error(
`Ghidra switch compiler lane prerequisite missing: ${role} '${command}'`,
{ cause },
await commands.close();
} catch (closeCause) {
throw new AggregateError(
[cause, closeCause],
"Switch toolchain probe failed and command cleanup failed",
);
}
throw cause;
}
let installation;
if (!compilerOnly) {
const { inspectVerifierGhidraInstallation } =
await import("./lib/ghidra-installation-inspection.mjs");
assert.ok(
process.env.GHIDRA_INSTALL_DIR,
"Ghidra switch lane prerequisite missing: GHIDRA_INSTALL_DIR (bring your own)",
);
installation = await inspectVerifierGhidraInstallation({
environment: process.env,
installDir: process.env.GHIDRA_INSTALL_DIR,
...(process.env.JAVA_HOME === undefined
? {}
: { javaHome: process.env.JAVA_HOME }),
});
assert.equal(installation.status, "available", JSON.stringify(installation));
try {
if (!compilerOnly) {
const { inspectVerifierGhidraInstallation } =
await import("./lib/ghidra-installation-inspection.mjs");
assert.ok(
process.env.GHIDRA_INSTALL_DIR,
"Ghidra switch lane prerequisite missing: GHIDRA_INSTALL_DIR (bring your own)",
);
installation = await inspectVerifierGhidraInstallation({
environment: process.env,
installDir: process.env.GHIDRA_INSTALL_DIR,
...(process.env.JAVA_HOME === undefined
? {}
: { javaHome: process.env.JAVA_HOME }),
});
assert.equal(
installation.status,
"available",
JSON.stringify(installation),
);
}
} catch (cause) {
try {
await commands.close();
} catch (closeCause) {
throw new AggregateError(
[cause, closeCause],
"Switch prerequisite check failed and command cleanup failed",
);
}
throw cause;
}
const run = createVerifierRun();
// Ghidra children use the workspace as TMPDIR; a plain root admits them on every host.
@@ -95,6 +135,10 @@ const workspaceFixture = await VerifierFixture.open({
private: false,
});
const workspace = workspaceFixture.path;
const retainWorkspace = (message, options) => {
workspaceFixture.retain(message);
return new Error(message, options);
};
const runtime = join(workspace, "runtime");
await mkdir(runtime);
const source = join(workspace, "switch.c");
@@ -103,94 +147,136 @@ const flags = ["-O2", "-g", "-fno-pie", "-no-pie"];
const results = [];
let productionControls = null;
// The workspace is removed only once no owned runtime or process survives.
let lifecycle;
workspaceFixture.releaseAfter(async () => {
assert.deepEqual(
(await readdir(runtime)).filter((name) => name.startsWith("rea-ghidra-")),
[],
"Owned Ghidra runtime survived close",
);
const cleanup = await completeVerifierRun(run);
assert.equal(cleanup.process_lineage.status, "verified");
assert.deepEqual(cleanup.process_lineage.descendants, []);
lifecycle = await completeVerifierRun(run);
assertVerifierCleanupComplete(lifecycle.process_lineage);
});
commandInterruption.retainWorkspaceIfInterrupted(workspaceFixture);
await withVerifierFixture(workspaceFixture, async () => {
for (const compiler of ["gcc", "clang"]) {
const original = join(workspace, `${compiler}-optimized`);
await exec(commands[compiler], [...flags, source, "-o", original]);
const bytes = await readFile(original);
const { stdout: symbols } = await exec(commands.nm, ["-n", original]);
const { stdout: disassembly } = await exec(commands.objdump, [
"-d",
original,
]);
const fixtures = assertCompiledSwitchOracle(
bytes,
switchSymbolOracle(symbols),
disassembly,
);
const oracleFaultControls = assertSwitchOracleControls(fixtures);
if (!compilerOnly && productionControls === null) {
productionControls = await verifySwitchEvidenceControls({
installation,
entrypoint,
target: original,
workspace,
runId: run.run_id,
});
assert.equal(
digest(await readFile(original)),
digest(bytes),
"Guard changed compiler-oracled input",
let primaryFailure;
try {
for (const compiler of ["gcc", "clang"]) {
const original = join(workspace, `${compiler}-optimized`);
await execOwned(toolchain[compiler], [...flags, source, "-o", original]);
const bytes = await readFile(original);
const { stdout: symbols } = await execOwned(toolchain.nm, [
"-n",
original,
]);
const { stdout: disassembly } = await execOwned(toolchain.objdump, [
"-d",
original,
]);
const fixtures = assertCompiledSwitchOracle(
bytes,
switchSymbolOracle(symbols),
disassembly,
);
}
const stripped = join(workspace, `${compiler}-stripped`);
await copyFile(original, stripped);
await exec(commands.strip, ["--strip-all", stripped]);
// Byte/physical target oracle survives removal of the names that REA could use.
assertCompiledSwitchOracle(await readFile(stripped), fixtures, disassembly);
for (const [variant, path] of [
["optimized", original],
["stripped", stripped],
]) {
const sha256 = digest(await readFile(path));
const observation = compilerOnly
? null
: await verifyTarget(path, sha256, fixtures, variant);
assert.equal(
digest(await readFile(path)),
sha256,
"REA analysis changed original compiled fixture",
const oracleFaultControls = assertSwitchOracleControls(fixtures);
if (!compilerOnly && productionControls === null) {
throwIfInterrupted();
productionControls = await verifySwitchEvidenceControls({
installation,
entrypoint,
target: original,
workspace,
runId: run.run_id,
signal: commandInterruption.signal,
});
assert.equal(
digest(await readFile(original)),
digest(bytes),
"Guard changed compiler-oracled input",
);
}
const stripped = join(workspace, `${compiler}-stripped`);
await copyFile(original, stripped);
await execOwned(toolchain.strip, ["--strip-all", stripped]);
// Byte/physical target oracle survives removal of the names that REA could use.
assertCompiledSwitchOracle(
await readFile(stripped),
fixtures,
disassembly,
);
results.push({
compiler,
variant,
oracle_fault_controls: oracleFaultControls,
sha256,
compiler_oracle: fixtures.map(
({
name,
address,
dispatch_address,
table_address,
slots,
default_address,
}) => ({
name,
address,
dispatch_address,
table_address,
slots,
default_address,
}),
),
observation,
});
for (const [variant, path] of [
["optimized", original],
["stripped", stripped],
]) {
const sha256 = digest(await readFile(path));
const observation = compilerOnly
? null
: await verifyTarget(path, sha256, fixtures, variant);
assert.equal(
digest(await readFile(path)),
sha256,
"REA analysis changed original compiled fixture",
);
results.push({
compiler,
variant,
oracle_fault_controls: oracleFaultControls,
sha256,
compiler_oracle: fixtures.map(
({
name,
address,
dispatch_address,
table_address,
slots,
default_address,
}) => ({
name,
address,
dispatch_address,
table_address,
slots,
default_address,
}),
),
observation,
});
}
}
} catch (cause) {
primaryFailure = cause;
}
// Close the command owner before release observes the parent tree.
let cleanupFailure;
try {
await workspaceFixture.closeEarly([() => commands.close()]);
} catch (cause) {
cleanupFailure = cause;
}
if (cleanupFailure !== undefined || primaryFailure !== undefined)
throw retainWorkspace(
`Switch verification failed; retained diagnostic workspace: ${workspace}`,
{
cause:
cleanupFailure === undefined
? primaryFailure
: primaryFailure === undefined
? cleanupFailure
: new AggregateError(
[primaryFailure, cleanupFailure],
"Switch verification and command cleanup both failed",
),
},
);
const interrupted = commandInterruption.reason();
if (interrupted !== undefined)
throw retainWorkspace(
`Switch verification interrupted; retained diagnostic workspace: ${workspace}`,
{ cause: interrupted },
);
});
const lifecycle = await completeVerifierRun(run);
if (lifecycle.process_lineage.status === "verified")
assert.deepEqual(lifecycle.process_lineage.descendants, []);
const releaseInterrupted = finishCommandInterruption();
if (releaseInterrupted !== undefined) throw releaseInterrupted;
process.stdout.write(
`${JSON.stringify({ lane: "linux-x64-compiler-switch", compiler_only: compilerOnly, entrypoint, versions, compiler_flags: flags, verifier_run: lifecycle, production_controls: productionControls, results, limitations: ["Exact compiler oracle admits absolute 8-byte native x86-64 ELF tables and fails explicitly when a compiler changes lowering.", "Compiled binaries are analyzed read-only; target programs are never executed.", "Comparison-only control proves absence of a fabricated table and preservation of provider limitations, not recovery of C source.", "Detached Ghidra object controls exercise actual bridge methods through reflection; they do not claim naturally compiled ambiguous dispatches.", "CLI/MCP comparison covers switch observations, excluding process-specific Evidence IDs."], cleanup: "complete" })}\n`,
);
@@ -221,7 +307,9 @@ async function verifyTarget(path, sha256, fixtures, variant) {
let stderr = "";
const observations = [];
return withVerifierCleanup(async () => {
await client.connect(transport);
await untilInterrupted(
client.connect(transport, { signal: commandInterruption.signal }),
);
transport.stderr?.on("data", (chunk) => {
stderr = (stderr + chunk.toString()).slice(-65536);
});
@@ -329,23 +417,25 @@ function expectedResidualUnknowns(boundary, fixture) {
return [...new Set(unknowns)];
}
async function toolResult(client, name, arguments_ = {}) {
throwIfInterrupted();
const result = await client.callTool(
{ name, arguments: arguments_ },
{ timeout: 240000 },
{ timeout: 240000, signal: commandInterruption.signal },
);
assert.ok(result.structuredContent, `${name} omitted structured result`);
return requireMcpOutput(result, name);
}
async function call(client, name, arguments_ = {}) {
throwIfInterrupted();
const result = await client.callTool(
{ name, arguments: arguments_ },
{ timeout: 240000 },
{ timeout: 240000, signal: commandInterruption.signal },
);
return requireMcpOperationResult(result, name);
}
async function cliSwitchEvidence(entrypoint, path, procedure, env) {
const { stdout } = await exec(
const { stdout } = await execOwned(
process.execPath,
[
entrypoint,
+14 -6
View File
@@ -155,6 +155,7 @@ export async function buildMipsRelocatableFixture(
directory,
byteOrder,
kernelModule = false,
run = execute,
) {
if (byteOrder !== "little" && byteOrder !== "big")
throw new Error("MIPS fixture byte order must be little or big");
@@ -180,7 +181,7 @@ export async function buildMipsRelocatableFixture(
path,
];
return {
...(await compileMipsFixture(path, byteOrder, 1, args)),
...(await compileMipsFixture(path, byteOrder, 1, args, {}, run)),
float_abi: kernelModule ? "soft" : "hard",
kernel_module_options: kernelModule,
source_sha256: createHash("sha256")
@@ -200,6 +201,7 @@ export async function buildMipsSharedFixture(
directory,
byteOrder,
softFloat = false,
run = execute,
) {
if (byteOrder !== "little" && byteOrder !== "big")
throw new Error("MIPS fixture byte order must be little or big");
@@ -224,9 +226,14 @@ export async function buildMipsSharedFixture(
path,
];
return {
...(await compileMipsFixture(path, byteOrder, 3, args, {
expectedFlags: 0x70001007,
})),
...(await compileMipsFixture(
path,
byteOrder,
3,
args,
{ expectedFlags: 0x70001007 },
run,
)),
float_abi: softFloat ? "soft" : "hard",
source_sha256: createHash("sha256")
.update(await readFile(source))
@@ -241,10 +248,11 @@ async function compileMipsFixture(
objectType,
args,
options = {},
run = execute,
) {
const compiler = fixtureCommand(FIXTURE_TOOLS.mipsClang);
const version = await execute(compiler, ["--version"], { timeout: 10000 });
await execute(compiler, args, { timeout: 60000 });
const version = await run(compiler, ["--version"], { timeout: 10000 });
await run(compiler, args, { timeout: 60000 });
const bytes = await readFile(path);
const read16 =
byteOrder === "little"
@@ -90,20 +90,25 @@ export function parseMipsRelocatableReadelf(text) {
}
/** Independent reader is a bounded test prerequisite, never an REA provider. */
export async function inspectMipsRelocatableReadelf(path, byteOrder, floatAbi) {
export async function inspectMipsRelocatableReadelf(
path,
byteOrder,
floatAbi,
run = execute,
) {
const command = fixtureCommand(FIXTURE_TOOLS.mipsReadelf);
const options = {
env: { ...process.env, LC_ALL: "C" },
timeout: 10000,
maxBuffer: 4 * 1024 * 1024,
};
const version = await execute(command, ["--version"], options);
const version = await run(command, ["--version"], options);
assert.match(
version.stdout,
/^GNU readelf\b/u,
"This lane requires GNU readelf",
);
const result = await execute(
const result = await run(
command,
["-h", "-A", "-S", "-s", "-r", "-W", path],
options,
@@ -93,20 +93,25 @@ export function parseMipsSharedReadelf(text) {
}
/** Independent reader is a bounded test prerequisite, never an REA provider. */
export async function inspectMipsSharedReadelf(path, byteOrder, floatAbi) {
export async function inspectMipsSharedReadelf(
path,
byteOrder,
floatAbi,
run = execute,
) {
const command = process.env.REA_MIPS_READELF ?? "readelf";
const options = {
env: { ...process.env, LC_ALL: "C" },
timeout: 10000,
maxBuffer: 16 * 1024 * 1024,
};
const version = await execute(command, ["--version"], options);
const version = await run(command, ["--version"], options);
assert.match(
version.stdout,
/^GNU readelf\b/u,
"This lane requires GNU readelf",
);
const result = await execute(
const result = await run(
command,
["-h", "-A", "-S", "-l", "-d", "-r", "--dyn-syms", "-W", path],
options,
@@ -15,7 +15,12 @@ const linkerScript = fileURLToPath(
const hash = (bytes) => createHash("sha256").update(bytes).digest("hex");
/** BYO GNU cross-binutils; generated controls are never executed or byte-patched. */
export async function buildMips16SharedFixture(directory, byteOrder, variant) {
export async function buildMips16SharedFixture(
directory,
byteOrder,
variant,
run = execute,
) {
assert(["little", "big"].includes(byteOrder));
assert(
[
@@ -32,17 +37,16 @@ export async function buildMips16SharedFixture(directory, byteOrder, variant) {
const object = `${prefix}.o`;
const path = `${prefix}.so`;
const assemblerVersion = (
await execute(assembler, ["--version"], { timeout: 10000 })
).stdout;
const linkerVersion = (
await execute(linker, ["--version"], { timeout: 10000 })
await run(assembler, ["--version"], { timeout: 10000 })
).stdout;
const linkerVersion = (await run(linker, ["--version"], { timeout: 10000 }))
.stdout;
assert(
assemblerVersion.includes("GNU assembler"),
"Use GNU's MIPS assembler: other assemblers have different compressed-instruction behavior.",
);
assert(linkerVersion.includes("GNU ld"));
await execute(
await run(
assembler,
[
"-32",
@@ -57,7 +61,7 @@ export async function buildMips16SharedFixture(directory, byteOrder, variant) {
],
{ timeout: 60000 },
);
await execute(
await run(
linker,
[
endian,
@@ -97,7 +101,7 @@ export async function buildMips16SharedFixture(directory, byteOrder, variant) {
assert.equal(mappings.length, 1);
return u32(mappings[0] + 4) + address - u32(mappings[0] + 8);
};
const readelf = await execute(
const readelf = await run(
fixtureCommand(FIXTURE_TOOLS.mipsReadelf),
["-W", "-h", "-l", "-s", "-r", path],
{ timeout: 10000 },
+3 -3
View File
@@ -328,15 +328,15 @@ export function parseRiscvCallSites(listing) {
}
/** Disassemble the typed fixture's caller with an independent LLVM tool. */
export async function inspectRiscvCallSites(path) {
export async function inspectRiscvCallSites(path, run = execute) {
const command = fixtureCommand(FIXTURE_TOOLS.riscvObjdump);
const options = {
env: { ...process.env, LC_ALL: "C" },
timeout: 10000,
maxBuffer: 1024 * 1024,
};
const version = await execute(command, ["--version"], options);
const { stdout, stderr } = await execute(
const version = await run(command, ["--version"], options);
const { stdout, stderr } = await run(
command,
[
"-d",
+22 -14
View File
@@ -37,8 +37,8 @@ const readerOptions = {
};
/** Independent reading of LLVM readelf's `-h -l -S -s -A` report. */
const readRiscvElf = async (reader, path, floatAbi) => {
const { stdout, stderr } = await execute(
const readRiscvElf = async (reader, path, floatAbi, run = execute) => {
const { stdout, stderr } = await run(
reader,
["-h", "-l", "-S", "-s", "-A", "-W", path],
readerOptions,
@@ -93,10 +93,13 @@ const sha256 = async (path) =>
/** Build and independently inspect a source-owned ELF; never execute it. */
export async function buildRiscvFixture(directory, bits, buildOptions = {}) {
const { floatAbi = "double", pie = false } =
typeof buildOptions === "string"
? { floatAbi: buildOptions }
: buildOptions;
const {
floatAbi = "double",
pie = false,
execute: run = execute,
} = typeof buildOptions === "string"
? { floatAbi: buildOptions }
: buildOptions;
assert.ok(bits === 32 || bits === 64);
const build = BUILDS[floatAbi];
assert.ok(build !== undefined, `No ${floatAbi}-float RISC-V fixture`);
@@ -133,11 +136,11 @@ export async function buildRiscvFixture(directory, bits, buildOptions = {}) {
path,
];
const options = { timeout: 10000, maxBuffer: 1024 * 1024 };
const compilerVersion = await execute(compiler, ["--version"], options);
const linkerVersion = await execute(linker, ["--version"], options);
const readerVersion = await execute(reader, ["--version"], options);
await execute(compiler, args, { ...options, timeout: 60000 });
const elf = await readRiscvElf(reader, path, floatAbi);
const compilerVersion = await run(compiler, ["--version"], options);
const linkerVersion = await run(linker, ["--version"], options);
const readerVersion = await run(reader, ["--version"], options);
await run(compiler, args, { ...options, timeout: 60000 });
const elf = await readRiscvElf(reader, path, floatAbi, run);
assert.equal(elf.class, `ELF${bits}`);
assert.match(elf.data, /little endian/u);
assert.match(elf.type, pie ? /^DYN\b/u : /^EXEC\b/u);
@@ -202,7 +205,7 @@ export async function buildRiscvFixture(directory, bits, buildOptions = {}) {
* section must keep its address and bytes, so the typed build's symbol
* addresses locate the same code in the stripped image.
*/
export async function stripRiscvFixture(directory, typed) {
export async function stripRiscvFixture(directory, typed, run = execute) {
const path = join(
directory,
typed.path.split("/").at(-1).replace(".elf", "-stripped.elf"),
@@ -213,11 +216,16 @@ export async function stripRiscvFixture(directory, typed) {
"-o",
path,
];
await execute(typed.toolchain.compiler.command, args, {
await run(typed.toolchain.compiler.command, args, {
timeout: 60000,
maxBuffer: 1024 * 1024,
});
const elf = await readRiscvElf(typed.toolchain.reader.command, path, "soft");
const elf = await readRiscvElf(
typed.toolchain.reader.command,
path,
"soft",
run,
);
assert.equal(elf.flags, typed.riscv.flags);
assert.deepEqual(elf.isa, typed.riscv.isa);
assert.ok(