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

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

Route the first Ghidra verifier batch's compilers, linkers, and CLI through
createVerifierCommands, and close that owner before the lineage check.
Replace MCP isError prose checks with the structured refusal each tool builds.

* fix(verify): resolve Windows 6502 toolchain names through the command owner

runOwnedCommand passes a bare ca65 or ld65 name to the Windows process
owner, which accepts only a drive-absolute path. The verifier command
owner resolves those PATH names before launch.

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

* fix(verify): release the Ghidra fixture when a toolchain probe fails

retain() only registered a cleanup that nothing ran, so a failed compiler
probe left the empty fixture directory behind. Release it after the command
owner closes, and name the retained path when that close does not confirm.

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

* fix(verify): close the m68k command owner when session shutdown fails

Client, transport, and command cleanup are independent. A failed client or
transport close, or a failed runtime read, no longer skips the command owner.

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:04:57 -05:00
1 parent fa918a35f5
commit d5818ad0da
10 files changed
+732 -393

No files matched your search

+107 -40
View File
@@ -1,6 +1,5 @@
import { closeVerifierResources } from "./lib/verifier-cleanup.mjs";
import assert from "node:assert/strict";
import { execFile } from "node:child_process";
import { createHash } from "node:crypto";
import {
mkdir,
@@ -12,7 +11,6 @@ import {
} 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";
@@ -20,8 +18,14 @@ import { parseEvidence } from "../dist/domain/evidence.js";
import {
requireMcpOperationResult,
requireMcpOutput,
requireMcpToolError,
} from "./lib/mcp-verifier-results.mjs";
import { createVerifierRun, completeVerifierRun } from "./lib/verifier-run.mjs";
import {
assertVerifierCleanupComplete,
createVerifierRun,
completeVerifierRun,
} from "./lib/verifier-run.mjs";
import { createVerifierCommands } from "./lib/verify-commands.mjs";
import {
VerifierFixture,
withVerifierFixture,
@@ -33,25 +37,52 @@ if (process.env.GHIDRA_INSTALL_DIR === undefined)
throw new Error(
"Set GHIDRA_INSTALL_DIR to a bring-your-own Ghidra installation.",
);
const exec = promisify(execFile);
const run = createVerifierRun();
const root = fileURLToPath(new URL("..", import.meta.url));
const ca65 = process.env.REA_CA65 ?? "ca65";
const ld65 = process.env.REA_LD65 ?? "ld65";
const toolchain = {};
for (const [name, command] of Object.entries({ ca65, ld65 })) {
const version = await exec(command, ["--version"], { timeout: 10000 });
toolchain[name] = (version.stdout + version.stderr).trim();
let workspaceFixture;
// The same owner serves the pre-workspace probes. Its Windows launcher resolves
// bare ca65/ld65 names; the shared owned-command runner requires a drive path.
const commands = await createVerifierCommands();
try {
for (const [name, command] of Object.entries({ ca65, ld65 })) {
const version = await commands.exec(command, ["--version"], {
timeout: 10000,
});
toolchain[name] = (version.stdout + version.stderr).trim();
}
await mkdir(join(root, "build"), { recursive: true });
workspaceFixture = await VerifierFixture.open({
prefix: "rea-6502-proof-",
parent: join(root, "build"),
private: false,
});
await mkdir(join(workspaceFixture.path, "runtime"));
} catch (cause) {
const failures = [cause];
try {
await commands.close();
} catch (closeCause) {
failures.push(closeCause);
}
if (workspaceFixture !== undefined) {
try {
await workspaceFixture.release();
} catch (releaseCause) {
failures.push(releaseCause);
}
}
if (failures.length > 1)
throw new AggregateError(
failures,
"6502 toolchain probe and command cleanup both failed",
);
throw cause;
}
await mkdir(join(root, "build"), { recursive: true });
const workspaceFixture = await VerifierFixture.open({
prefix: "rea-6502-proof-",
parent: join(root, "build"),
private: false,
});
const workspace = workspaceFixture.path;
const runtime = join(workspace, "runtime");
await mkdir(runtime);
const path = join(workspace, "image.bin");
const object = join(workspace, "image.o");
const entrypoint = join(root, "scripts", "rea.mjs");
@@ -92,19 +123,19 @@ workspaceFixture.releaseAfter(async () => {
"Owned Ghidra runtime survived close",
);
verifierRun = await completeVerifierRun(run);
if (process.platform !== "win32") {
assert.equal(verifierRun.process_lineage.status, "verified");
assert.deepEqual(verifierRun.process_lineage.descendants, []);
}
// Windows has no retained Job Object lease, so lineage observation is unavailable.
if (process.platform !== "win32")
assertVerifierCleanupComplete(verifierRun.process_lineage);
});
await withVerifierFixture(workspaceFixture, async () => {
let failure;
try {
await exec(
await commands.exec(
ca65,
[join(root, "tests/conformance/ghidra/6502.s"), "-o", object],
{ timeout: 10000 },
);
await exec(
await commands.exec(
ld65,
[
"-C",
@@ -191,14 +222,23 @@ await withVerifierFixture(workspaceFixture, async () => {
entry_point: "0xffff",
},
});
assert.equal(rejected.isError, true);
assert.match(JSON.stringify(rejected.content), /6502 address width/u);
// admitRawBinary rejects an image that does not fit the 6502 address width.
const rejectedError = requireMcpToolError(rejected);
assert.partialDeepStrictEqual(rejectedError, {
code: "invalid_request",
details: {
path,
reason:
"raw-binary mapping from 0xffff must fit in the 6502 address width",
constraint: "load_selection_rejected",
},
});
const preserved = await call("binary_session");
assert.equal(preserved.analysis_run.run_id, status.analysis_run.run_id);
assert.equal(preserved.entry_point, "0x8001");
await call("close_binary");
await client.ping();
const { stdout } = await exec(
const { stdout } = await commands.exec(
process.execPath,
[
entrypoint,
@@ -220,7 +260,7 @@ await withVerifierFixture(workspaceFixture, async () => {
assert.deepEqual(cli.normalized_result, image.normalized_result);
assert.equal(cli.analysis_profile.digest, image.analysis_profile.digest);
assert.equal(cli.subject.identity.sha256, sha256);
const fileCli = await exec(
const fileCli = await commands.exec(
process.execPath,
[
entrypoint,
@@ -256,7 +296,7 @@ await withVerifierFixture(workspaceFixture, async () => {
[0, true],
]) {
const lowPath = join(workspace, `low-${entry}.bin`);
await exec(
await commands.exec(
ld65,
[
"-C",
@@ -333,7 +373,7 @@ await withVerifierFixture(workspaceFixture, async () => {
),
);
await call("close_binary");
const lowCli = await exec(
const lowCli = await commands.exec(
process.execPath,
[
entrypoint,
@@ -400,10 +440,23 @@ await withVerifierFixture(workspaceFixture, async () => {
{ name: "inspect_native_load_image", arguments: {} },
{ timeout: 240000 },
);
assert.equal(linkedImage.isError, true);
// Native Windows admission refuses the junction before Ghidra imports it.
const linkedError = requireMcpToolError(linkedImage);
assert.partialDeepStrictEqual(linkedError, {
code: "execution_failure",
details: {
provider_id: "ghidra",
operation: "inspect_native_load_image",
diagnostics: {
failure_kind: "start",
failure_message: "Ghidra target snapshot failed admission",
failure_cause: { code: "ERR_REA_WINDOWS_NATIVE" },
},
},
});
assert.match(
JSON.stringify(linkedImage),
/Reparse path component is unsupported/u,
linkedError.details.diagnostics.failure_cause.message,
/^Reparse path component is unsupported \(tag \d+\) /u,
);
await call("close_binary");
const snapshotPath = join(workspace, "direct-snapshot.json");
@@ -423,7 +476,7 @@ await withVerifierFixture(workspaceFixture, async () => {
snapshotPath,
"--json",
];
const saved = await exec(process.execPath, cliArgs(path), {
const saved = await commands.exec(process.execPath, cliArgs(path), {
env,
timeout: 240000,
maxBuffer: 16 * 1024 * 1024,
@@ -441,16 +494,14 @@ await withVerifierFixture(workspaceFixture, async () => {
assert.deepEqual(await readdir(runtime), []);
// Replaying a matching snapshot must still reach native path admission.
await assert.rejects(
exec(process.execPath, cliArgs(linkedPath), {
commands.exec(process.execPath, cliArgs(linkedPath), {
env,
timeout: 240000,
maxBuffer: 16 * 1024 * 1024,
}),
(cause) => {
assert.match(
cause.stdout + cause.stderr,
/Reparse path component is unsupported/u,
);
const output = `${cause.snapshot?.stdout?.text ?? ""}${cause.snapshot?.stderr?.text ?? ""}`;
assert.match(output, /Reparse path component is unsupported/u);
return true;
},
);
@@ -486,14 +537,30 @@ await withVerifierFixture(workspaceFixture, async () => {
};
} catch (cause) {
if (stderr) process.stderr.write(stderr);
throw cause;
failure = cause;
} finally {
try {
await workspaceFixture.closeEarly([() => commands.close()]);
} catch (cause) {
failure =
failure === undefined
? cause
: new AggregateError(
[failure, cause],
"6502 verification and command cleanup both failed",
);
}
}
if (failure !== undefined) {
workspaceFixture.retain(
`6502 verification failed; retained workspace: ${workspace}`,
);
throw failure;
}
});
verifierRun = await completeVerifierRun(run);
if (process.platform !== "win32") {
assert.equal(verifierRun.process_lineage.status, "verified");
assert.deepEqual(verifierRun.process_lineage.descendants, []);
}
if (process.platform !== "win32")
assertVerifierCleanupComplete(verifierRun.process_lineage);
// Keep the workspace for diagnosis unless verification and cleanup both pass.
process.stdout.write(
JSON.stringify({ verifier_run: verifierRun, ...report }) + "\n",
+45 -19
View File
@@ -1,25 +1,28 @@
import assert from "node:assert/strict";
import { execFile } from "node:child_process";
import { createHash } from "node:crypto";
import { 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 { parseEvidence } from "../dist/domain/evidence.js";
import {
requireMcpOperationResult,
requireMcpOutput,
requireMcpToolError,
} from "./lib/mcp-verifier-results.mjs";
import { createVerifierRun, completeVerifierRun } from "./lib/verifier-run.mjs";
import {
assertVerifierCleanupComplete,
createVerifierRun,
completeVerifierRun,
} from "./lib/verifier-run.mjs";
import { createVerifierCommands } from "./lib/verify-commands.mjs";
import { VerifierFixture } from "./lib/verifier-fixture.mjs";
if (process.argv.length !== 2 || process.platform !== "linux")
throw new Error("Run this ELF verifier on Linux without arguments.");
if (!process.env.GHIDRA_INSTALL_DIR)
throw new Error("Set GHIDRA_INSTALL_DIR to a caller-installed Ghidra.");
const exec = promisify(execFile);
const run = createVerifierRun();
const root = fileURLToPath(new URL("..", import.meta.url));
// Ghidra children use the workspace as TMPDIR; a plain root admits them on every host.
@@ -38,6 +41,8 @@ const env = {
REA_ANALYSIS_PROVIDER: "ghidra",
REA_LOG_LEVEL: "silent",
};
// Compiler and CLI share one command owner, created before either starts.
const commands = await createVerifierCommands();
const transport = new StdioClientTransport({
command: process.execPath,
args: [entrypoint, "mcp"],
@@ -55,8 +60,10 @@ let primaryFailure;
let verifierRun;
try {
const compiler = process.env.CC ?? "gcc";
const version = await exec(compiler, ["--version"], { timeout: 10000 });
await exec(
const version = await commands.exec(compiler, ["--version"], {
timeout: 10000,
});
await commands.exec(
compiler,
[
"-O0",
@@ -203,14 +210,24 @@ try {
{ name: "procedure_pseudo_code", arguments: { procedure: missingAddress } },
{ timeout: 240000 },
);
assert.equal(rejected.isError, true);
assert.ok(
JSON.stringify(rejected.content).includes(`Ghidra image base: ${base}`),
);
assert.ok(JSON.stringify(rejected.content).includes("list_procedures"));
assert.ok(
JSON.stringify(rejected.content).includes("inspect_native_load_image"),
);
// procedure_not_found keeps the caller-selected procedure and the inventory tools.
const rejectedError = requireMcpToolError(rejected);
assert.partialDeepStrictEqual(rejectedError, {
code: "invalid_request",
details: {
operation: "procedure_pseudo_code",
related_operations: ["list_procedures", "search_procedures"],
issues: [
{
path: ["procedure"],
reason: "invalid_value",
observed: { procedure: missingAddress },
},
],
diagnostics: { remote_code: "procedure_not_found" },
},
});
assert.equal(rejectedError.recovery, undefined);
const pseudo = parseEvidence(
await call("procedure_pseudo_code", { procedure: addition.address }),
);
@@ -219,7 +236,7 @@ try {
assert.equal(retained.analysis_run.run_id, active.analysis_run.run_id);
await call("close_binary");
const cli = async (args) =>
exec(
commands.exec(
process.execPath,
[entrypoint, ...args, "--provider", "ghidra", "--json"],
{ env, timeout: 240000, maxBuffer: 16 * 1024 * 1024 },
@@ -234,8 +251,9 @@ try {
} catch (cause) {
cliFailure = cause;
}
assert.ok(cliFailure && typeof cliFailure.stdout === "string");
const error = JSON.parse(cliFailure.stdout);
const cliStdout = cliFailure?.snapshot?.stdout?.text;
assert.equal(typeof cliStdout, "string");
const error = JSON.parse(cliStdout);
assert.equal(error.code, "invalid_request");
assert.ok(JSON.stringify(error).includes(`Ghidra image base: ${base}`));
for (const command of [
@@ -314,9 +332,17 @@ workspaceFixture.releaseAfter(() => transport.close());
workspaceFixture.releaseAfter(async () => {
assert.deepEqual(await readdir(runtime), []);
verifierRun = await completeVerifierRun(run);
assert.equal(verifierRun.process_lineage.status, "verified");
assert.deepEqual(verifierRun.process_lineage.descendants, []);
assertVerifierCleanupComplete(verifierRun.process_lineage);
});
if (primaryFailure !== undefined)
workspaceFixture.retain(
`Address-context verification failed; retained workspace: ${workspace}`,
);
try {
await workspaceFixture.closeEarly([() => commands.close()]);
} catch (cause) {
cleanupFailures.push(cause);
}
try {
await workspaceFixture.release();
} catch (cause) {
+29 -21
View File
@@ -3,7 +3,6 @@ import { createVerifierCommands } from "./lib/verify-commands.mjs";
import { createVerifierMcpOwner } from "./lib/verifier-mcp-owner.mjs";
import assert from "node:assert/strict";
import { execFile } from "node:child_process";
import { createHash } from "node:crypto";
import {
copyFile,
@@ -14,7 +13,6 @@ import {
} 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 {
@@ -36,8 +34,7 @@ if (process.platform === "win32")
throw new Error(
"Owned Ghidra checkpoints currently support Linux/macOS only.",
);
const run = createVerifierRun(),
exec = promisify(execFile);
const run = createVerifierRun();
// Ghidra children use the root as HOME and TMPDIR; a plain root admits them on every host.
const workspace = await VerifierFixture.open({
prefix: "rea-ghidra-checkpoints-",
@@ -100,21 +97,30 @@ async function phase(name, operation) {
}
}
async function cli(procedure, flags = [], path = target) {
const { stdout } = await exec(
process.execPath,
[
entrypoint,
"decompile",
path,
procedure,
"--provider",
"ghidra",
"--json",
...flags,
],
{ env, timeout: 240000, maxBuffer: 16 * 1024 * 1024 },
);
return JSON.parse(stdout);
try {
const { stdout } = await compilerCommands.exec(
process.execPath,
[
entrypoint,
"decompile",
path,
procedure,
"--provider",
"ghidra",
"--json",
...flags,
],
{ env, timeout: 240000, maxBuffer: 16 * 1024 * 1024 },
);
return JSON.parse(stdout);
} catch (cause) {
// The checkpoint refusal helper reads the execFile-shaped stdout and stderr.
if (cause instanceof Error && cause.snapshot?.stdout?.text !== undefined) {
cause.stdout = cause.snapshot.stdout.text;
cause.stderr = cause.snapshot.stderr.text;
}
throw cause;
}
}
async function connect(environment = {}) {
const owner = createVerifierMcpOwner();
@@ -479,7 +485,7 @@ int main(void) { CheckpointRecord record = { 3, 11 }; return rea_checkpoint_leaf
} finally {
let cleanupVerified = false;
try {
await compilerCommands.close();
await workspace.closeEarly([() => compilerCommands.close()]);
report.run = await completeVerifierRun(run);
assertVerifierCleanupComplete(report.run.process_lineage);
cleanupVerified = true;
@@ -488,7 +494,7 @@ int main(void) { CheckpointRecord record = { 3, 11 }; return rea_checkpoint_leaf
report.retained_workspace = root;
process.exitCode = 1;
}
if (cleanupVerified) {
if (cleanupVerified && report.status === "verified") {
try {
await workspace.release();
} catch (cause) {
@@ -496,6 +502,8 @@ int main(void) { CheckpointRecord record = { 3, 11 }; return rea_checkpoint_leaf
report.retained_workspace = root;
process.exitCode = 1;
}
} else if (report.retained_workspace === undefined) {
report.retained_workspace = root;
}
process.stdout.write(JSON.stringify(report, null, 2) + "\n");
}
+30 -9
View File
@@ -7,12 +7,10 @@ import {
requireMcpOutput,
requireMcpToolError,
} 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 { isAbsolute, 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 { Ajv2020 } from "ajv/dist/2020.js";
@@ -24,7 +22,12 @@ import {
buildDosComFixture,
buildRejectedDosComFixture,
} from "../tests/conformance/ghidra/dos-com-fixture.mjs";
import { createVerifierRun, completeVerifierRun } from "./lib/verifier-run.mjs";
import {
assertVerifierCleanupComplete,
createVerifierRun,
completeVerifierRun,
} from "./lib/verifier-run.mjs";
import { createVerifierCommands } from "./lib/verify-commands.mjs";
import { verifyGhidraAddressBoundaries } from "./lib/real-ghidra-address-boundaries.mjs";
import {
VerifierFixture,
@@ -96,6 +99,8 @@ const env = {
: { JAVA_HOME: process.env.JAVA_HOME }),
};
// The CLI shares one command owner, created before the lane starts a tool.
const commands = await createVerifierCommands();
const transport = new StdioClientTransport({
command: process.execPath,
args: [entrypoint, "mcp"],
@@ -124,10 +129,10 @@ workspaceFixture.releaseAfter(async () => {
"Owned Ghidra runtime survived close",
);
verifierRun = await completeVerifierRun(run);
assert.equal(verifierRun.process_lineage.status, "verified");
assert.deepEqual(verifierRun.process_lineage.descendants, []);
assertVerifierCleanupComplete(verifierRun.process_lineage);
});
await withVerifierFixture(workspaceFixture, async () => {
let failure;
try {
await client.connect(transport);
connected = true;
@@ -524,13 +529,29 @@ await withVerifierFixture(workspaceFixture, async () => {
};
} catch (error) {
if (stderr.trim()) process.stderr.write(stderr);
throw error;
failure = error;
} finally {
try {
await workspaceFixture.closeEarly([() => commands.close()]);
} catch (cause) {
failure =
failure === undefined
? cause
: new AggregateError(
[failure, cause],
"COM verification and command cleanup both failed",
);
}
}
if (failure !== undefined) {
workspaceFixture.retain(
`COM verification failed; retained workspace: ${workspace}`,
);
throw failure;
}
});
assert.ok(report);
verifierRun = await completeVerifierRun(run);
assert.equal(verifierRun.process_lineage.status, "verified");
assert.deepEqual(verifierRun.process_lineage.descendants, []);
process.stdout.write(
JSON.stringify({
verifier_run: verifierRun,
@@ -628,7 +649,7 @@ async function call(name, args = {}) {
return evidence.normalized_result;
}
async function cli(command, address, options = []) {
const { stdout } = await promisify(execFile)(
const { stdout } = await commands.exec(
process.execPath,
[
entrypoint,
+5 -4
View File
@@ -1,10 +1,8 @@
#!/usr/bin/env node
import assert from "node:assert/strict";
import { execFile } from "node:child_process";
import { readFile, mkdir } 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";
@@ -33,6 +31,7 @@ import {
assertVerifierCleanupComplete,
} from "./lib/verifier-run.mjs";
import { withVerifierCleanup } from "./lib/verifier-cleanup.mjs";
import { createVerifierCommands } from "./lib/verify-commands.mjs";
import { VerifierFixture } from "./lib/verifier-fixture.mjs";
import { requireLaneToolchain } from "../tests/conformance/ghidra/fixture-toolchain.mjs";
@@ -57,7 +56,6 @@ if (installation.status !== "available")
`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.
@@ -89,6 +87,8 @@ for (const setting of ["GHIDRA_HEADLESS_MAXMEM", "GHIDRA_MAXMEM"]) {
if (process.env[setting] !== undefined)
baseEnv[setting] = process.env[setting];
}
// The CLI shares one command owner, created before the lane starts a tool.
const commands = await createVerifierCommands();
/**
* True when GhidraProvider would admit the language id as an override: the same
@@ -230,7 +230,7 @@ async function runPass({ fixture, languageOverride, label, layout }) {
}
if (languageOverride === undefined) {
// CLI path through the same stock lane, not a private injected provider.
const cli = await execute(
const cli = await commands.exec(
process.execPath,
[
entrypoint,
@@ -317,6 +317,7 @@ try {
}
}, [
async () => {
await workspaceFixture.closeEarly([() => commands.close()]);
completed = await completeVerifierRun(run);
assertVerifierCleanupComplete(completed.process_lineage);
},
+45 -11
View File
@@ -4,13 +4,12 @@ import assert from "node:assert/strict";
import {
requireMcpOperationResult,
requireMcpOutput,
requireMcpToolError,
} 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 { isAbsolute, 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 { parseEvidence } from "../dist/domain/evidence.js";
@@ -24,6 +23,7 @@ import {
completeVerifierRun,
} from "./lib/verifier-run.mjs";
import { verifyGhidraAddressBoundaries } from "./lib/real-ghidra-address-boundaries.mjs";
import { createVerifierCommands } from "./lib/verify-commands.mjs";
import {
VerifierFixture,
withVerifierFixture,
@@ -93,6 +93,8 @@ const expected = {
near: address(fixture.near_offset),
far: address(fixture.far_offset),
};
// The CLI shares one command owner, created before the lane starts a tool.
const commands = await createVerifierCommands();
const transport = new StdioClientTransport({
command: process.execPath,
args: [entrypoint, "mcp"],
@@ -123,12 +125,11 @@ workspaceFixture.releaseAfter(async () => {
"Owned Ghidra runtime survived close",
);
verifierRun = await completeVerifierRun(run);
if (process.platform !== "win32") {
assert.equal(verifierRun.process_lineage.status, "verified");
assert.deepEqual(verifierRun.process_lineage.descendants, []);
}
// Windows has no retained Job Object lease, so lineage observation is unavailable.
if (!windows) assertVerifierCleanupComplete(verifierRun.process_lineage);
});
await withVerifierFixture(workspaceFixture, async () => {
let failure;
try {
await client.connect(transport);
connected = true;
@@ -227,7 +228,23 @@ await withVerifierFixture(workspaceFixture, async () => {
name: "address_to_file_offset",
arguments: { address: "0x80000" },
});
assert.equal(noSource.isError, true);
// address_unmapped is a well-formed address with no memory block.
const noSourceError = requireMcpToolError(noSource);
assert.partialDeepStrictEqual(noSourceError, {
code: "invalid_request",
details: {
operation: "address_to_file_offset",
related_operations: ["inspect_native_load_image"],
issues: [
{
path: ["address"],
reason: "invalid_value",
observed: { address: "0x80000", file_mapping: "unmapped" },
},
],
diagnostics: { remote_code: "address_unmapped" },
},
});
const procedures = await call("list_procedures");
assert.ok(
Array.isArray(procedures),
@@ -527,13 +544,30 @@ await withVerifierFixture(workspaceFixture, async () => {
} catch (error) {
if (stderr.trim())
process.stderr.write(`DOS verifier MCP diagnostics:\n${stderr}\n`);
throw error;
failure = error;
} finally {
try {
await workspaceFixture.closeEarly([() => commands.close()]);
} catch (cause) {
failure =
failure === undefined
? cause
: new AggregateError(
[failure, cause],
"DOS verification and command cleanup both failed",
);
}
}
if (failure !== undefined) {
workspaceFixture.retain(
`DOS verification failed; retained workspace: ${workspace}`,
);
throw failure;
}
});
assert.ok(report, "No DOS proof report was produced");
verifierRun = await completeVerifierRun(run);
// Windows provider jobs are verified by their native owners, as in the 6502 lane.
if (!windows) assertVerifierCleanupComplete(verifierRun.process_lineage);
// The release already required a verified lineage. This later observation is the report.
process.stdout.write(
`${JSON.stringify({ verifier_run: verifierRun, ...report, cleanup: "complete" })}\n`,
);
@@ -610,7 +644,7 @@ async function evidenceCall(name, arguments_) {
return evidence;
}
async function cliEvidence(command, procedure, options = []) {
const { stdout } = await promisify(execFile)(
const { stdout } = await commands.exec(
process.execPath,
[
entrypoint,
+78 -26
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, 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";
@@ -12,8 +10,15 @@ import { parseEvidence } from "../dist/domain/evidence.js";
import {
requireMcpOperationResult,
requireMcpOutput,
requireMcpToolError,
} from "./lib/mcp-verifier-results.mjs";
import { createVerifierRun, completeVerifierRun } from "./lib/verifier-run.mjs";
import {
assertVerifierCleanupComplete,
createVerifierRun,
completeVerifierRun,
} from "./lib/verifier-run.mjs";
import { closeVerifierResources } from "./lib/verifier-cleanup.mjs";
import { createVerifierCommands } from "./lib/verify-commands.mjs";
import { VerifierFixture } from "./lib/verifier-fixture.mjs";
if (process.argv.length !== 2)
@@ -27,7 +32,6 @@ if (process.env.GHIDRA_INSTALL_DIR === undefined)
throw new Error(
"Set GHIDRA_INSTALL_DIR to a bring-your-own Ghidra installation.",
);
const exec = promisify(execFile);
const run = createVerifierRun();
const root = fileURLToPath(new URL("..", import.meta.url));
// Source-owned big-endian 68000 image, loaded at 0x1000:
@@ -77,6 +81,8 @@ const env = {
REA_ANALYSIS_PROVIDER: "ghidra",
REA_PROCESS_RUN_ID: run.run_id,
};
// Compiler and CLI launches share one command owner, created before either starts.
const commands = await createVerifierCommands();
const transport = new StdioClientTransport({
command: process.execPath,
args: [entrypoint, "mcp"],
@@ -89,6 +95,7 @@ transport.stderr?.on("data", (chunk) => {
stderr = (stderr + chunk.toString()).slice(-65536);
});
let report;
let primaryFailure;
try {
await client.connect(transport);
const opened = await call("open_binary", {
@@ -165,10 +172,10 @@ try {
);
// Rejected selections must not replace the live session.
for (const [entryPoint, reason] of [
["0x1001", /raw-binary m68k entry_point 0x1001 must be 2-byte aligned/u],
["0x1001", "raw-binary m68k entry_point 0x1001 must be 2-byte aligned"],
[
"0xfffffff0",
/raw-binary mapping from 0xfffffff0 must fit in the m68k address width/u,
"raw-binary mapping from 0xfffffff0 must fit in the m68k address width",
],
]) {
const rejected = await client.callTool({
@@ -180,15 +187,23 @@ try {
entry_point: entryPoint,
},
});
assert.equal(rejected.isError, true);
assert.match(JSON.stringify(rejected.content), reason);
// admitRawBinary rejects the selection before Ghidra starts.
const rejectedError = requireMcpToolError(rejected);
assert.partialDeepStrictEqual(rejectedError, {
code: "invalid_request",
details: {
path,
reason,
constraint: "load_selection_rejected",
},
});
const preserved = await call("binary_session");
assert.equal(preserved.analysis_run.run_id, status.analysis_run.run_id);
assert.equal(preserved.entry_point, "0x1000");
}
await call("close_binary");
await client.ping();
const { stdout } = await exec(
const { stdout } = await commands.exec(
process.execPath,
[
entrypoint,
@@ -209,7 +224,7 @@ try {
const cli = evidence(JSON.parse(stdout));
assert.deepEqual(cli.normalized_result, image.normalized_result);
assert.equal(cli.analysis_profile.digest, image.analysis_profile.digest);
const fileCli = await exec(
const fileCli = await commands.exec(
process.execPath,
[
entrypoint,
@@ -262,29 +277,66 @@ try {
};
} catch (cause) {
if (stderr) process.stderr.write(stderr);
workspaceFixture.retain(
`m68k verification failed; retained workspace: ${workspace}`,
);
throw cause;
primaryFailure = cause;
} finally {
// Client, transport, and command cleanup are independent. A failure in one
// must not skip the others.
const shutdownFailures = [];
try {
await client.close();
} finally {
await transport.close();
await closeVerifierResources([
() => client.close(),
() => transport.close(),
]);
} catch (cause) {
shutdownFailures.push(
...(cause instanceof AggregateError ? cause.errors : [cause]),
);
}
try {
await workspaceFixture.closeEarly([() => commands.close()]);
} catch (cause) {
shutdownFailures.push(
...(cause instanceof AggregateError ? cause.errors : [cause]),
);
}
if (shutdownFailures.length > 0) {
const shutdown =
shutdownFailures.length === 1
? shutdownFailures[0]
: new AggregateError(shutdownFailures, "m68k shutdown failed");
primaryFailure =
primaryFailure === undefined
? shutdown
: new AggregateError(
[primaryFailure, shutdown],
"m68k verification and shutdown both failed",
);
}
}
if (primaryFailure === undefined) {
try {
const survivors = await readdir(runtime);
if (survivors.length > 0)
primaryFailure = retainWorkspace(
`Runtime resources survived server shutdown (${survivors.join(", ")}); retained workspace: ${workspace}`,
);
} catch (cause) {
primaryFailure = cause;
}
}
const survivors = await readdir(runtime);
if (survivors.length > 0)
throw retainWorkspace(
`Runtime resources survived server shutdown (${survivors.join(", ")}); retained workspace: ${workspace}`,
);
const verifierRun = await completeVerifierRun(run);
if (
verifierRun.process_lineage.status !== "verified" ||
verifierRun.process_lineage.descendants.length !== 0
)
try {
assertVerifierCleanupComplete(verifierRun.process_lineage);
} catch (cause) {
throw retainWorkspace(
`Cleanup unverified; retained diagnostic workspace: ${workspace}`,
{ cause: primaryFailure ?? cause },
);
}
if (primaryFailure !== undefined)
throw retainWorkspace(
`m68k verification failed; retained workspace: ${workspace}`,
{ cause: primaryFailure },
);
await workspaceFixture.release();
process.stdout.write(
+57 -20
View File
@@ -1,6 +1,5 @@
#!/usr/bin/env node
import assert from "node:assert/strict";
import { execFile } from "node:child_process";
import { createHash } from "node:crypto";
import {
mkdir,
@@ -11,7 +10,6 @@ import {
} from "node:fs/promises";
import { basename, join, relative } 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";
@@ -43,7 +41,12 @@ import {
requireMcpOperationResult,
requireMcpOutput,
} from "./lib/mcp-verifier-results.mjs";
import { createVerifierRun, completeVerifierRun } from "./lib/verifier-run.mjs";
import {
assertVerifierCleanupComplete,
createVerifierRun,
completeVerifierRun,
} from "./lib/verifier-run.mjs";
import { createVerifierCommands } from "./lib/verify-commands.mjs";
import { runOwnedCommand } from "../dist/process/OwnedCommand.js";
import { VerifierFixture } from "./lib/verifier-fixture.mjs";
import { requireLaneToolchain } from "../tests/conformance/ghidra/fixture-toolchain.mjs";
@@ -74,7 +77,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.
@@ -105,6 +107,20 @@ const env = {
for (const setting of ["GHIDRA_HEADLESS_MAXMEM", "GHIDRA_MAXMEM"]) {
if (process.env[setting] !== undefined) env[setting] = process.env[setting];
}
// CLI launches share one command owner, created before the lane starts a tool.
const commands = await createVerifierCommands();
/** stdout and stderr of an owned command that exited nonzero. */
const commandFailureOutput = (cause) => {
const snapshot = cause?.snapshot;
if (typeof snapshot?.stdout?.text === "string")
return {
code: cause.code,
stdout: snapshot.stdout.text,
stderr: snapshot.stderr.text,
};
return cause;
};
/** Observe an existing project, and verify REA never alters the caller's copy. */
async function verifyExistingMipsProject(fixture, byteOrder) {
@@ -483,7 +499,7 @@ try {
assert.match(JSON.stringify(error), /PSP_EXTENSION_UNAVAILABLE/u);
let cliError;
try {
await execute(
await commands.exec(
process.execPath,
[
entrypoint,
@@ -499,18 +515,19 @@ try {
} catch (cause) {
cliError = cause;
}
const cliOutput = commandFailureOutput(cliError);
assert.ok(
cliError && Number.isInteger(cliError.code) && cliError.code !== 0,
cliOutput && Number.isInteger(cliOutput.code) && cliOutput.code !== 0,
"Expected a typed public rejection, not a timeout or success",
);
assert.match(
`${cliError.stdout}\n${cliError.stderr}`,
`${cliOutput.stdout}\n${cliOutput.stderr}`,
/PSP_EXTENSION_UNAVAILABLE/u,
);
reports.push({
path: fixture.path,
status: "expected-missing-extension-refusal",
cli_exit: cliError.code,
cli_exit: cliOutput.code,
mcp_error: error,
});
continue;
@@ -755,7 +772,7 @@ try {
}
// Same public operation through CLI, not a private injected provider.
const cli = await execute(
const cli = await commands.exec(
process.execPath,
[
entrypoint,
@@ -813,7 +830,7 @@ try {
assertMipsCarvedTargetRefusal(carvedError, openedPath, carve.reason);
let carvedCliError;
try {
await execute(
await commands.exec(
process.execPath,
[
entrypoint,
@@ -829,14 +846,15 @@ try {
} catch (cause) {
carvedCliError = cause;
}
const carvedOutput = commandFailureOutput(carvedCliError);
assert.ok(
carvedCliError &&
Number.isInteger(carvedCliError.code) &&
carvedCliError.code !== 0,
carvedOutput &&
Number.isInteger(carvedOutput.code) &&
carvedOutput.code !== 0,
"Expected a target-opening refusal, not a timeout or success",
);
assertMipsCarvedTargetRefusal(
JSON.parse(carvedCliError.stdout),
JSON.parse(carvedOutput.stdout),
openedPath,
carve.reason,
);
@@ -847,7 +865,7 @@ try {
section_header_offset: carve.sectionOffset,
status: "expected-target-refusal",
reason: carve.reason,
cli_exit: carvedCliError.code,
cli_exit: carvedOutput.code,
mcp_error: carvedError,
};
}
@@ -901,15 +919,34 @@ try {
} catch (cause) {
primaryFailure = cause;
}
let cleanupFailure;
try {
await workspaceFixture.closeEarly([() => commands.close()]);
} catch (cause) {
cleanupFailure = cause;
}
if (cleanupFailure !== undefined)
throw retainWorkspace(
`MIPS command cleanup failed; retained diagnostic workspace: ${workspace}`,
{
cause:
primaryFailure === undefined
? cleanupFailure
: new AggregateError(
[primaryFailure, cleanupFailure],
"MIPS verification and command cleanup both failed",
),
},
);
const completed = await completeVerifierRun(run);
if (
completed.process_lineage.status !== "verified" ||
completed.process_lineage.descendants.length !== 0
)
try {
assertVerifierCleanupComplete(completed.process_lineage);
} catch (cause) {
throw retainWorkspace(
`Cleanup unverified; retained diagnostic workspace: ${workspace}`,
{ cause: primaryFailure },
{ cause: primaryFailure ?? cause },
);
}
if (primaryFailure !== undefined)
throw retainWorkspace(
`MIPS verification failed; retained workspace: ${workspace}`,
+15 -3
View File
@@ -19,7 +19,10 @@ import { parseAnalysisSnapshot } from "../dist/domain/analysisSnapshot.js";
import { functionDossierSchema } from "../dist/domain/native/functionDossier.js";
import { inspectVerifierGhidraInstallation } from "./lib/ghidra-installation-inspection.mjs";
import { buildPowerPc64Fixture } from "../tests/conformance/ghidra/ppc-fixture.mjs";
import { requireMcpOperationResult } from "./lib/mcp-verifier-results.mjs";
import {
requireMcpOperationResult,
requireMcpToolError,
} from "./lib/mcp-verifier-results.mjs";
import { createVerifierCommands } from "./lib/verify-commands.mjs";
import { createVerifierRun, completeVerifierRun } from "./lib/verifier-run.mjs";
import { VerifierFixture } from "./lib/verifier-fixture.mjs";
@@ -143,8 +146,17 @@ try {
name: "open_binary",
arguments: { path: shared, provider_id: "ghidra" },
});
assert.equal(rejected.isError, true);
assert.match(JSON.stringify(rejected.content), /ET_EXEC only/u);
// powerPc64UnsupportedReason refuses ET_DYN before Ghidra starts.
const rejectedError = requireMcpToolError(rejected);
assert.partialDeepStrictEqual(rejectedError, {
code: "unsupported_target",
details: {
operation: "resolve_analysis_profile",
path: shared,
reason:
"This Ghidra PPC64 lane supports ET_EXEC only; relocatable objects, shared objects and position-independent executables need separate load verification.",
},
});
for (const byteOrder of ["little", "big"]) {
const fixture = await buildPowerPc64Fixture(
+321 -240
View File
@@ -2,11 +2,9 @@
import { verifyProjectCoverage } from "./verify-real-ghidra-project-coverage.mjs";
import { execFile } from "node:child_process";
import { writeFile } from "node:fs/promises";
import { isAbsolute, join } from "node:path";
import { fileURLToPath } from "node:url";
import { promisify } from "node:util";
import { parseBinaryTarget } from "../dist/application/BinaryTargetResolver.js";
import { resolveGhidraAnalysisProfile } from "../dist/ghidra/GhidraAnalysisProfile.js";
@@ -31,7 +29,12 @@ import {
call,
verifyInventoryOperations,
} from "./verify-real-ghidra-inventory.mjs";
import { completeVerifierRun, createVerifierRun } from "./lib/verifier-run.mjs";
import {
assertVerifierCleanupComplete,
completeVerifierRun,
createVerifierRun,
} from "./lib/verifier-run.mjs";
import { createVerifierCommands } from "./lib/verify-commands.mjs";
import {
VerifierFixture,
onceConfirmed,
@@ -49,9 +52,9 @@ import {
verifyRelativeSignedSwitch,
verifyNativeValueTrace,
} from "./verify-real-ghidra-function.mjs";
const exec = promisify(execFile);
const verifierRun = createVerifierRun();
let report;
let verifierCompleted;
const installDir = process.env.GHIDRA_INSTALL_DIR;
if (installDir === undefined || !isAbsolute(installDir))
throw new Error(
@@ -123,256 +126,334 @@ const crossTarget = crossFormat
: aarch64JumpTableOnly
? "aarch64-linux-gnu"
: `${expectedNativeTarget.format}/${expectedNativeTarget.architecture}`;
for (const [command, role] of toolchain) {
// Compilers, linkers and the CLI share one command owner, created before they start.
const commands = await createVerifierCommands();
try {
for (const [command, role] of toolchain) {
try {
await commands.exec(command, [command === lldLink ? "/?" : "--version"]);
} catch (cause) {
throw new Error(
`Ghidra ${lane} verification cannot run for target ${crossTarget}: required ${role} '${command}' is unavailable or failed its preflight.`,
{ cause },
);
}
}
} catch (cause) {
let failure = cause;
try {
await exec(command, [command === lldLink ? "/?" : "--version"]);
} catch (cause) {
throw new Error(
`Ghidra ${lane} verification cannot run for target ${crossTarget}: required ${role} '${command}' is unavailable or failed its preflight.`,
{ cause },
await fixtures.closeEarly([() => commands.close()]);
} catch (closeCause) {
failure = new AggregateError(
[cause, closeCause],
"Ghidra verification and command cleanup both failed",
);
}
// The preflight never used the fixture. Release it when the command owner
// closed. A failed close stays registered, so release keeps the root, names
// it, and leaves the fixture as a cleanup lease.
try {
await fixtures.release();
} catch (releaseCause) {
failure = new AggregateError(
[failure, releaseCause],
`Ghidra preflight failed; retained workspace: ${fixtureRoot}`,
);
if (!fixtures.released) failure.cleanupLeases = [fixtures];
}
throw failure;
}
await withVerifierFixture(fixtures, async () => {
const common = ["-O0", "-g", "-fno-inline"];
if (expectedNativeTarget.format === "elf") common.push("-fno-pie", "-no-pie");
await exec(compiler, [...common, sourcePath, "-o", debugPath]);
await exec(compiler, [
"-O0",
"-g",
"-gdwarf-4",
"-c",
sourcePath,
"-o",
layoutPath,
]);
const strippedFlags = [...common, "-s"];
if (expectedNativeTarget.format === "mach-o")
strippedFlags.push("-fvisibility=hidden");
await exec(compiler, [...strippedFlags, sourcePath, "-o", strippedPath]);
// The dense jump-table oracle needs the switch lowered to a table: GCC 16 and
// later expand dense switches as compare chains at -O0, so the CLI oracle
// runs against an optimized build while the debug fixture keeps its -O0
// stack-local and DWARF shape.
const jumpTableFlags = ["-O2", "-g", "-fno-inline"];
if (expectedNativeTarget.format === "elf")
jumpTableFlags.push("-fno-pie", "-no-pie");
await exec(compiler, [...jumpTableFlags, sourcePath, "-o", jumpTablePath]);
const crossTargets = [];
if (aarch64JumpTableOnly) {
const relativeSource = fileURLToPath(
new URL("../tests/conformance/ghidra/relative-switch.S", import.meta.url),
);
for (const entrySize of [1, 2]) {
for (const format of [
"elf",
...(expectedNativeTarget.architecture === "arm64" ? ["mach-o"] : []),
]) {
const targetPath = join(
fixtureRoot,
`relative-${entrySize}-${format}.o`,
);
await exec(clang, [
...(format === "elf" ? ["--target=aarch64-linux-gnu"] : []),
`-DREA_ENTRY_BYTES=${entrySize}`,
"-c",
relativeSource,
"-o",
targetPath,
]);
crossTargets.push([
targetPath,
`relative-${entrySize}`,
{ format, architecture: "arm64" },
]);
let workFailure;
try {
const common = ["-O0", "-g", "-fno-inline"];
if (expectedNativeTarget.format === "elf")
common.push("-fno-pie", "-no-pie");
await commands.exec(compiler, [...common, sourcePath, "-o", debugPath]);
await commands.exec(compiler, [
"-O0",
"-g",
"-gdwarf-4",
"-c",
sourcePath,
"-o",
layoutPath,
]);
const strippedFlags = [...common, "-s"];
if (expectedNativeTarget.format === "mach-o")
strippedFlags.push("-fvisibility=hidden");
await commands.exec(compiler, [
...strippedFlags,
sourcePath,
"-o",
strippedPath,
]);
// The dense jump-table oracle needs the switch lowered to a table: GCC 16 and
// later expand dense switches as compare chains at -O0, so the CLI oracle
// runs against an optimized build while the debug fixture keeps its -O0
// stack-local and DWARF shape.
const jumpTableFlags = ["-O2", "-g", "-fno-inline"];
if (expectedNativeTarget.format === "elf")
jumpTableFlags.push("-fno-pie", "-no-pie");
await commands.exec(compiler, [
...jumpTableFlags,
sourcePath,
"-o",
jumpTablePath,
]);
const crossTargets = [];
if (aarch64JumpTableOnly) {
const relativeSource = fileURLToPath(
new URL(
"../tests/conformance/ghidra/relative-switch.S",
import.meta.url,
),
);
for (const entrySize of [1, 2]) {
for (const format of [
"elf",
...(expectedNativeTarget.architecture === "arm64" ? ["mach-o"] : []),
]) {
const targetPath = join(
fixtureRoot,
`relative-${entrySize}-${format}.o`,
);
await commands.exec(clang, [
...(format === "elf" ? ["--target=aarch64-linux-gnu"] : []),
`-DREA_ENTRY_BYTES=${entrySize}`,
"-c",
relativeSource,
"-o",
targetPath,
]);
crossTargets.push([
targetPath,
`relative-${entrySize}`,
{ format, architecture: "arm64" },
]);
}
}
}
// Compile the assembly fixtures through the same operator-selected Clang.
const switchFixtures = [
...[0, 2, 10].map((selectorBias) => ({
source: "../tests/conformance/ghidra/relative-switch-signed.S",
variant: `relative-signed-${selectorBias}`,
flags: [`-DREA_SELECTOR_BIAS=${selectorBias}`],
})),
{
source: "../tests/conformance/ghidra/masked-byte-switch.S",
variant: "masked-byte-switch",
flags: [],
},
];
for (const { source, variant, flags } of switchFixtures) {
const sourceFile = fileURLToPath(new URL(source, import.meta.url));
for (const format of [
"elf",
...(expectedNativeTarget.architecture === "arm64" ? ["mach-o"] : []),
]) {
const targetPath = join(fixtureRoot, `${variant}-${format}.o`);
await exec(clang, [
...(format === "elf" ? ["--target=aarch64-linux-gnu"] : []),
...flags,
"-c",
sourceFile,
"-o",
targetPath,
]);
crossTargets.push([
targetPath,
variant,
{ format, architecture: "arm64" },
]);
// Compile the assembly fixtures through the same operator-selected Clang.
const switchFixtures = [
...[0, 2, 10].map((selectorBias) => ({
source: "../tests/conformance/ghidra/relative-switch-signed.S",
variant: `relative-signed-${selectorBias}`,
flags: [`-DREA_SELECTOR_BIAS=${selectorBias}`],
})),
{
source: "../tests/conformance/ghidra/masked-byte-switch.S",
variant: "masked-byte-switch",
flags: [],
},
];
for (const { source, variant, flags } of switchFixtures) {
const sourceFile = fileURLToPath(new URL(source, import.meta.url));
for (const format of [
"elf",
...(expectedNativeTarget.architecture === "arm64" ? ["mach-o"] : []),
]) {
const targetPath = join(fixtureRoot, `${variant}-${format}.o`);
await commands.exec(clang, [
...(format === "elf" ? ["--target=aarch64-linux-gnu"] : []),
...flags,
"-c",
sourceFile,
"-o",
targetPath,
]);
crossTargets.push([
targetPath,
variant,
{ format, architecture: "arm64" },
]);
}
}
}
await exec(clang, [
"--target=aarch64-linux-gnu",
"-O2",
"-g",
"-fno-inline",
"-fno-pie",
"-c",
crossFormatSourcePath,
"-o",
arm64ElfPath,
]);
crossTargets.push([
arm64ElfPath,
"aarch64-jump-table",
{ format: "elf", architecture: "arm64" },
]);
} else if (crossFormat) {
await exec(clang, [
"--target=aarch64-linux-gnu",
"-O2",
"-g",
"-fno-inline",
"-fno-pie",
"-nostdlib",
"-static",
"-fuse-ld=lld",
crossFormatSourcePath,
"-Wl,-e,rea_cross_start",
"-o",
arm64ElfPath,
]);
await exec(clang, [
"--target=x86_64-pc-windows-msvc",
"-O0",
"-gcodeview",
"-fno-inline",
"-c",
crossFormatSourcePath,
"-o",
peObjectPath,
]);
await exec(lldLink, [
"/entry:rea_cross_start",
"/subsystem:console",
"/nodefaultlib",
"/export:rea_cross_entry",
`/implib:${peImportLibraryPath}`,
`/out:${pePath}`,
peObjectPath,
]);
await exec(clang, [
"--target=x86_64-apple-darwin",
"-O0",
"-g",
"-fno-inline",
"-c",
crossFormatSourcePath,
"-o",
machObjectPath,
]);
crossTargets.push(
[
await commands.exec(clang, [
"--target=aarch64-linux-gnu",
"-O2",
"-g",
"-fno-inline",
"-fno-pie",
"-c",
crossFormatSourcePath,
"-o",
arm64ElfPath,
"cross-arm64-elf",
]);
crossTargets.push([
arm64ElfPath,
"aarch64-jump-table",
{ format: "elf", architecture: "arm64" },
],
[pePath, "cross-x86_64-pe", { format: "pe", architecture: "x86_64" }],
[
]);
} else if (crossFormat) {
await commands.exec(clang, [
"--target=aarch64-linux-gnu",
"-O2",
"-g",
"-fno-inline",
"-fno-pie",
"-nostdlib",
"-static",
"-fuse-ld=lld",
crossFormatSourcePath,
"-Wl,-e,rea_cross_start",
"-o",
arm64ElfPath,
]);
await commands.exec(clang, [
"--target=x86_64-pc-windows-msvc",
"-O0",
"-gcodeview",
"-fno-inline",
"-c",
crossFormatSourcePath,
"-o",
peObjectPath,
]);
await commands.exec(lldLink, [
"/entry:rea_cross_start",
"/subsystem:console",
"/nodefaultlib",
"/export:rea_cross_entry",
`/implib:${peImportLibraryPath}`,
`/out:${pePath}`,
peObjectPath,
]);
await commands.exec(clang, [
"--target=x86_64-apple-darwin",
"-O0",
"-g",
"-fno-inline",
"-c",
crossFormatSourcePath,
"-o",
machObjectPath,
"cross-x86_64-mach-o",
{ format: "mach-o", architecture: "x86_64" },
],
);
}
await writeFile(malformedPath, Buffer.from("not-a-binary\n", "utf8"));
]);
crossTargets.push(
[
arm64ElfPath,
"cross-arm64-elf",
{ format: "elf", architecture: "arm64" },
],
[pePath, "cross-x86_64-pe", { format: "pe", architecture: "x86_64" }],
[
machObjectPath,
"cross-x86_64-mach-o",
{ format: "mach-o", architecture: "x86_64" },
],
);
}
await writeFile(malformedPath, Buffer.from("not-a-binary\n", "utf8"));
const relocationSymbols = crossFormat
? await verifyGhidraRelocationSymbols({
clang,
lld,
env: process.env,
})
: null;
const debug = await verifyTarget(debugPath, "debug", expectedNativeTarget);
assertDebugFixture(debug);
const layout = await verifyTarget(
layoutPath,
"type-layout",
expectedNativeTarget,
);
const stripped = await verifyTarget(
strippedPath,
"stripped",
expectedNativeTarget,
);
assertStrippedFixture(stripped);
const jumpTable = await verifyTarget(
jumpTablePath,
"jump-table",
expectedNativeTarget,
);
const crossResults = [];
for (const [targetPath, variant, expectedTarget] of crossTargets) {
const result = await verifyTarget(targetPath, variant, expectedTarget);
if (
variant !== "aarch64-jump-table" &&
variant !== "masked-byte-switch" &&
!variant.startsWith("relative-")
)
assertCrossFixture(result);
crossResults.push(result);
}
await assertMalformedFixture(malformedPath);
const customPath = process.env.GHIDRA_TARGET_PATH;
const custom =
customPath === undefined ? null : await verifyTarget(customPath, "custom");
const projectCoverage =
!crossFormat && !aarch64JumpTableOnly && process.platform !== "win32"
? await verifyProjectCoverage(verifierRun)
const relocationSymbols = crossFormat
? await verifyGhidraRelocationSymbols({
clang,
lld,
env: process.env,
})
: null;
report = {
ok: true,
provider: { id: "ghidra", version: SUPPORTED_GHIDRA_VERSION },
verification_lane: aarch64JumpTableOnly
? "aarch64-jump-table"
: crossFormat
? "cross-format"
: "host-native",
fixture_sources:
crossFormat || aarch64JumpTableOnly
? [sourcePath, crossFormatSourcePath]
: [sourcePath],
fixtures: [debug, stripped, layout, jumpTable, ...crossResults].map(
summary,
),
malformed_target: "rejected-before-provider-start",
native_api_cli: aarch64JumpTableOnly
? (crossResults[0]?.native_api_cli ?? null)
: jumpTable.native_api_cli,
native_value_e2e: debug.native_values,
selected_project_coverage: projectCoverage,
relocation_symbols: relocationSymbols,
custom_target: custom === null ? null : summary(custom),
cleanup: "complete",
};
const debug = await verifyTarget(debugPath, "debug", expectedNativeTarget);
assertDebugFixture(debug);
const layout = await verifyTarget(
layoutPath,
"type-layout",
expectedNativeTarget,
);
const stripped = await verifyTarget(
strippedPath,
"stripped",
expectedNativeTarget,
);
assertStrippedFixture(stripped);
const jumpTable = await verifyTarget(
jumpTablePath,
"jump-table",
expectedNativeTarget,
);
const crossResults = [];
for (const [targetPath, variant, expectedTarget] of crossTargets) {
const result = await verifyTarget(targetPath, variant, expectedTarget);
if (
variant !== "aarch64-jump-table" &&
variant !== "masked-byte-switch" &&
!variant.startsWith("relative-")
)
assertCrossFixture(result);
crossResults.push(result);
}
await assertMalformedFixture(malformedPath);
const customPath = process.env.GHIDRA_TARGET_PATH;
const custom =
customPath === undefined
? null
: await verifyTarget(customPath, "custom");
const projectCoverage =
!crossFormat && !aarch64JumpTableOnly && process.platform !== "win32"
? await verifyProjectCoverage(verifierRun)
: null;
report = {
ok: true,
provider: { id: "ghidra", version: SUPPORTED_GHIDRA_VERSION },
verification_lane: aarch64JumpTableOnly
? "aarch64-jump-table"
: crossFormat
? "cross-format"
: "host-native",
fixture_sources:
crossFormat || aarch64JumpTableOnly
? [sourcePath, crossFormatSourcePath]
: [sourcePath],
fixtures: [debug, stripped, layout, jumpTable, ...crossResults].map(
summary,
),
malformed_target: "rejected-before-provider-start",
native_api_cli: aarch64JumpTableOnly
? (crossResults[0]?.native_api_cli ?? null)
: jumpTable.native_api_cli,
native_value_e2e: debug.native_values,
selected_project_coverage: projectCoverage,
relocation_symbols: relocationSymbols,
custom_target: custom === null ? null : summary(custom),
cleanup: "complete",
};
} catch (cause) {
workFailure = cause;
}
try {
await fixtures.closeEarly([() => commands.close()]);
} catch (cause) {
workFailure =
workFailure === undefined
? cause
: new AggregateError(
[workFailure, cause],
"Ghidra verification and command cleanup both failed",
);
}
let completed;
try {
completed = await completeVerifierRun(verifierRun);
// Windows has no retained Job Object lease, so lineage observation is unavailable.
if (process.platform !== "win32")
assertVerifierCleanupComplete(completed.process_lineage);
} catch (cause) {
workFailure =
workFailure === undefined
? cause
: new AggregateError([workFailure, cause], "Ghidra cleanup unverified");
}
if (workFailure !== undefined) {
fixtures.retain(
`Ghidra verification failed; retained workspace: ${fixtureRoot}`,
);
throw workFailure;
}
verifierCompleted = completed;
});
process.stdout.write(
`${JSON.stringify({ verifier_run: await completeVerifierRun(verifierRun), ...report })}\n`,
`${JSON.stringify({ verifier_run: verifierCompleted, ...report })}\n`,
);
function nativeFixtureTarget(platform, architecture) {
@@ -400,7 +481,7 @@ async function verifyNativeApiCli(
);
if (denseSwitch === undefined)
throw new Error("The Ghidra inventory omitted the dense switch fixture");
const { stdout } = await exec(
const { stdout } = await commands.exec(
process.execPath,
[
"scripts/rea.mjs",