refactor(ghidra): reuse the verifier file digest for fixture checks (#14554)

This commit is contained in:
Vincent Koc authored and GitHub committed 2026-11-26 01:23:52 +08:00
1 parent b1501f2ba6
commit 5bf09b6dff
12 files changed
+44 -111

No files matched your search

+4 -13
View File
@@ -1,3 +1,4 @@
import { sha256File } from "./verifier-file-sha256.mjs";
import { canonicalOutputSchema } from "./canonical-output-schema.mjs";
import {
requireMcpToolError,
@@ -6,7 +7,7 @@ import {
} from "./mcp-verifier-results.mjs";
import assert from "node:assert/strict";
import { execFile } from "node:child_process";
import { createHash, randomUUID } from "node:crypto";
import { randomUUID } from "node:crypto";
import { access, readFile, readdir, stat } from "node:fs/promises";
import { dirname, join } from "node:path";
import { setTimeout as delay } from "node:timers/promises";
@@ -775,12 +776,7 @@ export async function verifyGhidraBoundaries(
await call("read_bytes", { address, length: baseline.length }),
bytes,
);
assert.equal(
createHash("sha256")
.update(await readFile(target.path))
.digest("hex"),
target.sha256,
);
assert.equal(await sha256File(target.path), target.sha256);
const cliUpdated = await cli("annotate-native-function", name, [
"--name",
changes.name,
@@ -885,12 +881,7 @@ export async function verifyGhidraBoundaries(
.comments,
original.comments,
);
assert.equal(
createHash("sha256")
.update(await readFile(target.path))
.digest("hex"),
target.sha256,
);
assert.equal(await sha256File(target.path), target.sha256);
await verifyStartupCancellation(client, call, target, env, options);
return {
mocked: false,
+4 -15
View File
@@ -1,11 +1,9 @@
import assert from "node:assert/strict";
import { execFile } from "node:child_process";
import { createHash } from "node:crypto";
import {
appendFile,
copyFile,
mkdtemp,
readFile,
readdir,
rm,
writeFile,
@@ -14,6 +12,7 @@ import { basename, join } from "node:path";
import { promisify } from "node:util";
import { Client } from "@modelcontextprotocol/client";
import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
import { sha256File } from "./verifier-file-sha256.mjs";
import { requireMcpToolError } from "./mcp-verifier-results.mjs";
import {
completeEvidenceCliArguments,
@@ -48,14 +47,11 @@ export async function verifyGhidraProjectIdentity({ target, entrypoint, env }) {
const runtimeRootsBefore = (await readdir(env.TMPDIR))
.filter((name) => name.startsWith("rea-ghidra-"))
.sort();
const sha = async (path) =>
createHash("sha256")
.update(await readFile(path))
.digest("hex");
try {
await copyFile(targetPath, different);
await appendFile(different, "\nREA_IDENTITY_VERIFIER\n");
assert.notEqual(await sha(different), await sha(targetPath));
assert.notEqual(await sha256File(different), await sha256File(targetPath));
await writeFile(
join(root, "ClearImportDigest.java"),
`import ghidra.app.script.GhidraScript;
@@ -246,12 +242,5 @@ async function projectState(paths) {
if (path.endsWith(".gpr")) await walk(`${path.slice(0, -4)}.rep`);
}
files.sort();
return Promise.all(
files.map(async (path) => [
path,
createHash("sha256")
.update(await readFile(path))
.digest("hex"),
]),
);
return Promise.all(files.map(async (path) => [path, await sha256File(path)]));
}
@@ -1,8 +1,7 @@
import assert from "node:assert/strict";
import { createHash } from "node:crypto";
import { readFile } from "node:fs/promises";
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import { sha256File } from "./verifier-file-sha256.mjs";
import { parseEvidence } from "../../dist/domain/evidence.js";
import { PrivateRuntimeRoot } from "../../dist/process/PrivateRuntimeRoot.js";
import { withVerifierCleanup } from "./verifier-cleanup.mjs";
@@ -47,11 +46,8 @@ export async function verifyGhidraRelocationSymbols({ clang, lld, env }) {
object,
]);
await run(lld, ["-shared", object, "-o", target]);
const hash = async () =>
createHash("sha256")
.update(await readFile(target))
.digest("hex");
const before = await hash();
const before = await sha256File(target);
const { stdout } = await run(process.execPath, [
"scripts/rea.mjs",
...(await completeEvidenceCliArguments([
@@ -76,7 +72,7 @@ export async function verifyGhidraRelocationSymbols({ clang, lld, env }) {
relocations.some(({ symbol }) => symbol === ""),
"The provider's empty symbol must remain distinct from an unrecorded field",
);
assert.equal(await hash(), before);
assert.equal(await sha256File(target), before);
return {
source,
target_sha256: before,
+3 -10
View File
@@ -1,6 +1,5 @@
import assert from "node:assert/strict";
import { execFile } from "node:child_process";
import { createHash } from "node:crypto";
import {
access,
mkdir,
@@ -15,6 +14,7 @@ import { basename, dirname, join } from "node:path";
import { promisify } from "node:util";
import { Client } from "@modelcontextprotocol/client";
import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
import { sha256File } from "./verifier-file-sha256.mjs";
import { withVerifierCleanup } from "./verifier-cleanup.mjs";
import {
completeEvidenceCliArguments,
@@ -36,9 +36,7 @@ export async function verifyGhidraSocketBoundary({
reason: "The Ghidra Unix-socket lane is not used on this host.",
};
const limit = process.platform === "darwin" ? 102 : 106;
const before = createHash("sha256")
.update(await readFile(target.path))
.digest("hex");
const before = await sha256File(target.path);
const root = await realpath(await mkdtemp("/tmp/rea-gsock-"));
const suffixBytes = Buffer.byteLength("/rea-ghidra-XXXXXX/bridge.sock");
let cleanupIncomplete = false;
@@ -189,12 +187,7 @@ export async function verifyGhidraSocketBoundary({
);
}
}
assert.equal(
createHash("sha256")
.update(await readFile(target.path))
.digest("hex"),
before,
);
assert.equal(await sha256File(target.path), before);
return {
status: "verified",
samples,
+4 -7
View File
@@ -1,9 +1,9 @@
#!/usr/bin/env node
import { sha256File } from "./lib/verifier-file-sha256.mjs";
import { createVerifierCommands } from "./lib/verify-commands.mjs";
import { createVerifierMcpOwner } from "./lib/verifier-mcp-owner.mjs";
import assert from "node:assert/strict";
import { createHash } from "node:crypto";
import {
copyFile,
mkdir,
@@ -74,10 +74,7 @@ const report = {
phases: [],
target: null,
};
const sha = async (path) =>
createHash("sha256")
.update(await readFile(path))
.digest("hex");
async function phase(name, operation) {
const start = performance.now();
try {
@@ -180,7 +177,7 @@ int main(void) { CheckpointRecord record = { 3, 11 }; return rea_checkpoint_leaf
],
{ timeout: 120_000, maxBuffer: 16 * 1024 * 1024 },
);
const targetDigest = await sha(target);
const targetDigest = await sha256File(target);
report.target = {
sha256: targetDigest,
source: `self-built C struct/function ${process.platform === "darwin" ? "object" : "executable"} fixture; never executed`,
@@ -472,7 +469,7 @@ int main(void) { CheckpointRecord record = { 3, 11 }; return rea_checkpoint_leaf
return {};
},
);
assert.equal(await sha(target), targetDigest);
assert.equal(await sha256File(target), targetDigest);
assert.deepEqual(await readdir(runtime), []);
const checkpointFiles = await readdir(
join(checkpointDirectory, (await readdir(checkpointDirectory))[0]),
@@ -1,11 +1,11 @@
#!/usr/bin/env node
import assert from "node:assert/strict";
import { createHash } from "node:crypto";
import { mkdir, readFile } from "node:fs/promises";
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import { Client } from "@modelcontextprotocol/client";
import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
import { sha256File } from "./lib/verifier-file-sha256.mjs";
import { parseBinaryTarget } from "../dist/application/BinaryTargetResolver.js";
import { parseEvidence } from "../dist/domain/evidence.js";
import { parseAnalysisSnapshot } from "../dist/domain/analysisSnapshot.js";
@@ -309,9 +309,7 @@ async function verifyFixture(byteOrder, kernelModule) {
dossier.procedure.address,
);
assert.equal(
createHash("sha256")
.update(await readFile(fixture.path))
.digest("hex"),
await sha256File(fixture.path),
fixture.sha256,
"Analysis modified the source object",
);
+9 -15
View File
@@ -13,6 +13,7 @@ import { basename, join, relative } from "node:path";
import { fileURLToPath } from "node:url";
import { Client } from "@modelcontextprotocol/client";
import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
import { sha256File } from "./lib/verifier-file-sha256.mjs";
import { parseBinaryTarget } from "../dist/application/BinaryTargetResolver.js";
import {
inspectMipsReadelf,
@@ -155,12 +156,12 @@ async function verifyExistingMipsProject(fixture, byteOrder) {
};
await walk(join(projectDir, "ExistingMips.rep"));
return Promise.all(
paths.sort().map(async (path) => [
relative(projectDir, path),
createHash("sha256")
.update(await readFile(path))
.digest("hex"),
]),
paths
.sort()
.map(async (path) => [
relative(projectDir, path),
await sha256File(path),
]),
);
};
const before = await digestTree();
@@ -219,12 +220,7 @@ async function verifyExistingMipsProject(fixture, byteOrder) {
before,
"REA changed the source project",
);
assert.equal(
createHash("sha256")
.update(await readFile(fixture.path))
.digest("hex"),
fixture.sha256,
);
assert.equal(await sha256File(fixture.path), fixture.sha256);
return {
byte_order: byteOrder,
project_unchanged: true,
@@ -797,9 +793,7 @@ try {
assert.equal(cliDossier.procedure.address, dossier.procedure.address);
assert.ok(cliDossier.pseudocode.trim().length > 0);
assert.equal(
createHash("sha256")
.update(await readFile(fixture.path))
.digest("hex"),
await sha256File(fixture.path),
fixture.sha256,
"Analysis modified the source executable",
);
+2 -4
View File
@@ -1,11 +1,11 @@
#!/usr/bin/env node
import assert from "node:assert/strict";
import { createHash } from "node:crypto";
import { mkdir, readFile } from "node:fs/promises";
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import { Client } from "@modelcontextprotocol/client";
import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
import { sha256File } from "./lib/verifier-file-sha256.mjs";
import { parseBinaryTarget } from "../dist/application/BinaryTargetResolver.js";
import { parseEvidence } from "../dist/domain/evidence.js";
import { parseAnalysisSnapshot } from "../dist/domain/analysisSnapshot.js";
@@ -316,9 +316,7 @@ async function verifyFixture(byteOrder, softFloat) {
dossier.procedure.address,
);
assert.equal(
createHash("sha256")
.update(await readFile(fixture.path))
.digest("hex"),
await sha256File(fixture.path),
fixture.sha256,
"Analysis modified the source library",
);
+2 -4
View File
@@ -1,12 +1,12 @@
#!/usr/bin/env node
import { createGhidraVerifierMcpOperations } from "./lib/ghidra-verifier-mcp.mjs";
import assert from "node:assert/strict";
import { createHash } from "node:crypto";
import { mkdir, readFile } from "node:fs/promises";
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import { Client } from "@modelcontextprotocol/client";
import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
import { sha256File } from "./lib/verifier-file-sha256.mjs";
import { parseBinaryTarget } from "../dist/application/BinaryTargetResolver.js";
import {
inspectPowerPcReadelf,
@@ -281,9 +281,7 @@ try {
assert.equal(cliDossier.procedure.address, dossier.procedure.address);
assert.ok(cliDossier.pseudocode.trim().length > 0);
assert.equal(
createHash("sha256")
.update(await readFile(fixture.path))
.digest("hex"),
await sha256File(fixture.path),
fixture.sha256,
"Analysis modified the source executable",
);
+4 -7
View File
@@ -1,12 +1,12 @@
#!/usr/bin/env node
import { createGhidraVerifierMcpOperations } from "./lib/ghidra-verifier-mcp.mjs";
import assert from "node:assert/strict";
import { createHash } from "node:crypto";
import { mkdir, readFile } from "node:fs/promises";
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import { Client } from "@modelcontextprotocol/client";
import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
import { sha256File } from "./lib/verifier-file-sha256.mjs";
import { parseBinaryTarget } from "../dist/application/BinaryTargetResolver.js";
import {
inspectPowerPc64Readelf,
@@ -82,10 +82,7 @@ const env = {
for (const setting of ["GHIDRA_HEADLESS_MAXMEM", "GHIDRA_MAXMEM"]) {
if (process.env[setting] !== undefined) env[setting] = process.env[setting];
}
const sha256 = async (path) =>
createHash("sha256")
.update(await readFile(path))
.digest("hex");
// Compiler, reader and CLI launches share one command owner. Its process group is what
// a timeout stops, including a linker that would otherwise outlive Clang.
const commands = await createVerifierCommands();
@@ -323,7 +320,7 @@ try {
);
assert.ok(cliDossier.pseudocode.trim().length > 0);
assert.equal(
await sha256(fixture.path),
await sha256File(fixture.path),
fixture.sha256,
"Analysis modified the source executable",
);
@@ -347,7 +344,7 @@ try {
}
reports.push({
path: shared,
sha256: await sha256(shared),
sha256: await sha256File(shared),
checks: ["ET_DYN refused before provider startup"],
status: "passed",
});
+2 -6
View File
@@ -1,3 +1,4 @@
import { sha256File } from "./lib/verifier-file-sha256.mjs";
import { withCanonicalOutputSchema } from "./lib/canonical-output-schema.mjs";
import assert from "node:assert/strict";
import { createHash } from "node:crypto";
@@ -926,12 +927,7 @@ async function fingerprintFiles(directory) {
const name = join(relative, entry.name);
if (entry.isDirectory()) await visit(name);
else if (entry.isFile())
result.push([
name,
createHash("sha256")
.update(await readFile(join(directory, name)))
.digest("hex"),
]);
result.push([name, await sha256File(join(directory, name))]);
else throw new Error(`Unexpected project entry: ${name}`);
}
};
+4 -18
View File
@@ -1,4 +1,5 @@
#!/usr/bin/env node
import { sha256File } from "./lib/verifier-file-sha256.mjs";
import { canonicalOutputSchema } from "./lib/canonical-output-schema.mjs";
import assert from "node:assert/strict";
import { createHash } from "node:crypto";
@@ -314,12 +315,7 @@ const verifyUnmodelledRefusal = async () => {
);
},
);
assert.equal(
createHash("sha256")
.update(await readFile(fixture.path))
.digest("hex"),
fixture.sha256,
);
assert.equal(await sha256File(fixture.path), fixture.sha256);
return {
path: fixture.path,
sha256: fixture.sha256,
@@ -828,12 +824,7 @@ try {
headers.push({ flags, outcome: "refused", reason: outcome });
}
}
assert.equal(
createHash("sha256")
.update(await readFile(fixture.path))
.digest("hex"),
fixture.sha256,
);
assert.equal(await sha256File(fixture.path), fixture.sha256);
reports.push({
path: fixture.path,
sha256: fixture.sha256,
@@ -1198,12 +1189,7 @@ try {
!`${headless.stdout}\n${headless.stderr}`.includes("ANALYZING"),
headless.stdout,
);
assert.equal(
createHash("sha256")
.update(await readFile(esp.path))
.digest("hex"),
esp.sha256,
);
assert.equal(await sha256File(esp.path), esp.sha256);
// A reservation named like the default space still moves into an overlay
// space, named as Ghidra's memory map names overlay blocks.