From 5bf09b6dff068e67c4b1d7818f4e538005b07935 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Thu, 26 Nov 2026 01:23:52 +0800 Subject: [PATCH] refactor(ghidra): reuse the verifier file digest for fixture checks (#14554) --- scripts/lib/real-ghidra-boundaries.mjs | 17 ++++--------- scripts/lib/real-ghidra-project-identity.mjs | 19 ++++----------- .../lib/real-ghidra-relocation-symbols.mjs | 12 ++++------ scripts/lib/real-ghidra-socket-boundary.mjs | 13 +++------- scripts/verify-real-ghidra-checkpoints.mjs | 11 ++++----- .../verify-real-ghidra-mips-relocatable.mjs | 6 ++--- scripts/verify-real-ghidra-mips.mjs | 24 +++++++------------ scripts/verify-real-ghidra-mips32-shared.mjs | 6 ++--- scripts/verify-real-ghidra-ppc.mjs | 6 ++--- scripts/verify-real-ghidra-ppc64.mjs | 11 ++++----- scripts/verify-real-ghidra-raw.mjs | 8 ++----- scripts/verify-real-ghidra-riscv.mjs | 22 ++++------------- 12 files changed, 44 insertions(+), 111 deletions(-) diff --git a/scripts/lib/real-ghidra-boundaries.mjs b/scripts/lib/real-ghidra-boundaries.mjs index 86a9e0b4549..f22df88ab1e 100644 --- a/scripts/lib/real-ghidra-boundaries.mjs +++ b/scripts/lib/real-ghidra-boundaries.mjs @@ -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, diff --git a/scripts/lib/real-ghidra-project-identity.mjs b/scripts/lib/real-ghidra-project-identity.mjs index 6bc4fe84a6e..3cc2a08c6c9 100644 --- a/scripts/lib/real-ghidra-project-identity.mjs +++ b/scripts/lib/real-ghidra-project-identity.mjs @@ -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)])); } diff --git a/scripts/lib/real-ghidra-relocation-symbols.mjs b/scripts/lib/real-ghidra-relocation-symbols.mjs index 616f608093f..7f98bbfa395 100644 --- a/scripts/lib/real-ghidra-relocation-symbols.mjs +++ b/scripts/lib/real-ghidra-relocation-symbols.mjs @@ -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, diff --git a/scripts/lib/real-ghidra-socket-boundary.mjs b/scripts/lib/real-ghidra-socket-boundary.mjs index b547b1183b8..39b079f8030 100644 --- a/scripts/lib/real-ghidra-socket-boundary.mjs +++ b/scripts/lib/real-ghidra-socket-boundary.mjs @@ -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, diff --git a/scripts/verify-real-ghidra-checkpoints.mjs b/scripts/verify-real-ghidra-checkpoints.mjs index 1d2547b9173..766f75cab3e 100644 --- a/scripts/verify-real-ghidra-checkpoints.mjs +++ b/scripts/verify-real-ghidra-checkpoints.mjs @@ -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]), diff --git a/scripts/verify-real-ghidra-mips-relocatable.mjs b/scripts/verify-real-ghidra-mips-relocatable.mjs index 15df1ef14c3..4881a403f25 100755 --- a/scripts/verify-real-ghidra-mips-relocatable.mjs +++ b/scripts/verify-real-ghidra-mips-relocatable.mjs @@ -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", ); diff --git a/scripts/verify-real-ghidra-mips.mjs b/scripts/verify-real-ghidra-mips.mjs index 05b9555a033..06a47d7aeac 100644 --- a/scripts/verify-real-ghidra-mips.mjs +++ b/scripts/verify-real-ghidra-mips.mjs @@ -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", ); diff --git a/scripts/verify-real-ghidra-mips32-shared.mjs b/scripts/verify-real-ghidra-mips32-shared.mjs index 5513299e42c..c1727437d1b 100755 --- a/scripts/verify-real-ghidra-mips32-shared.mjs +++ b/scripts/verify-real-ghidra-mips32-shared.mjs @@ -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", ); diff --git a/scripts/verify-real-ghidra-ppc.mjs b/scripts/verify-real-ghidra-ppc.mjs index 28598548f74..dce640d898b 100644 --- a/scripts/verify-real-ghidra-ppc.mjs +++ b/scripts/verify-real-ghidra-ppc.mjs @@ -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", ); diff --git a/scripts/verify-real-ghidra-ppc64.mjs b/scripts/verify-real-ghidra-ppc64.mjs index 64b41814016..9756b53b174 100644 --- a/scripts/verify-real-ghidra-ppc64.mjs +++ b/scripts/verify-real-ghidra-ppc64.mjs @@ -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", }); diff --git a/scripts/verify-real-ghidra-raw.mjs b/scripts/verify-real-ghidra-raw.mjs index 0bec49321ac..fb246f06b62 100644 --- a/scripts/verify-real-ghidra-raw.mjs +++ b/scripts/verify-real-ghidra-raw.mjs @@ -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}`); } }; diff --git a/scripts/verify-real-ghidra-riscv.mjs b/scripts/verify-real-ghidra-riscv.mjs index f768310ec28..5307b7ecdca 100644 --- a/scripts/verify-real-ghidra-riscv.mjs +++ b/scripts/verify-real-ghidra-riscv.mjs @@ -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.