Reverse engineer anything with agents, from app behavior down to native binaries.
TypeScript agent-skillsai-agentsbinary-analysisclaude-codeclicodexcordisdecompilerdeveloper-toolsdshdsh-pluginghidrahopperllmmcpmodel-context-protocolreareverse-engineeringstatic-analysistouhou
fix(test): honor Ghidra startup budgets in Windows verification (#3417)
Ysopwnabled9cd239c5ed7
diff --git a/scripts/lib/ghidra-verifier-timeouts.d.mts b/scripts/lib/ghidra-verifier-timeouts.d.mtsnew file mode 100644index 000000000..2cedd680f--- /dev/null+++ b/scripts/lib/ghidra-verifier-timeouts.d.mts@@ -0,0 +1,33 @@+export interface GhidraVerifierTimeouts {+ readonly clientOptions: {+ readonly startupTimeoutMs: number;+ };+ readonly requestOptions: {+ readonly timeout: number;+ };+}++/** Resolve one verifier-owned timeout policy before target I/O or allocation. */+export function resolveGhidraVerifierTimeouts(+ environment: Readonly<Record<string, string | undefined>>,+): GhidraVerifierTimeouts;++export interface GhidraVerifierMcpClient {+ connect(+ transport: unknown,+ options: GhidraVerifierTimeouts["requestOptions"],+ ): Promise<void>;+ callTool(+ request: unknown,+ options: GhidraVerifierTimeouts["requestOptions"],+ ): Promise<unknown>;+}++/** Bind every verifier MCP operation to the same owned request budget. */+export function createGhidraVerifierMcpOperations(+ client: GhidraVerifierMcpClient,+ requestOptions: GhidraVerifierTimeouts["requestOptions"],+): {+ connect(transport: unknown): Promise<void>;+ callTool(request: unknown): Promise<unknown>;+};diff --git a/scripts/lib/ghidra-verifier-timeouts.mjs b/scripts/lib/ghidra-verifier-timeouts.mjsnew file mode 100644index 000000000..7f0240e26--- /dev/null+++ b/scripts/lib/ghidra-verifier-timeouts.mjs@@ -0,0 +1,31 @@+import { parseConfig } from "../../dist/config/parseConfig.js";++const FILE_OFFSET_OPERATION_TIMEOUT_MS = 240_000;+const MAX_TIMER_DELAY_MS = 2_147_483_647;++/** Resolve one verifier-owned timeout policy before target I/O or allocation. */+export function resolveGhidraVerifierTimeouts(environment) {+ const configuration = parseConfig(environment);+ if (!configuration.ok) throw configuration.error;+ const startupTimeoutMs = configuration.value.ghidraStartupTimeoutMs;+ // A cold public query includes provider startup before its analysis request.+ // Preserve the operation budget after the caller-selected startup deadline.+ const publicRequestTimeoutMs =+ startupTimeoutMs + FILE_OFFSET_OPERATION_TIMEOUT_MS;+ if (publicRequestTimeoutMs > MAX_TIMER_DELAY_MS)+ throw new RangeError(+ "The Ghidra startup and verification operation budgets exceed the Node.js timer limit.",+ );+ return {+ clientOptions: { startupTimeoutMs },+ requestOptions: { timeout: publicRequestTimeoutMs },+ };+}++/** Bind every verifier MCP operation to the same owned request budget. */+export function createGhidraVerifierMcpOperations(client, requestOptions) {+ return {+ connect: (transport) => client.connect(transport, requestOptions),+ callTool: (request) => client.callTool(request, requestOptions),+ };+}diff --git a/scripts/lib/real-ghidra-file-offsets.d.mts b/scripts/lib/real-ghidra-file-offsets.d.mtsnew file mode 100644index 000000000..5ddd76108--- /dev/null+++ b/scripts/lib/real-ghidra-file-offsets.d.mts@@ -0,0 +1,16 @@+export interface GhidraFileOffsetVerification {+ readonly cli_mcp_parity: true;+ readonly explicit_and_numeric_offsets: true;+ readonly invalid_and_unmapped_offsets: true;+ readonly mapped_offset_without_procedure: true;+ readonly ambiguous_mappings: "verified" | "unavailable-single-section";+ readonly source_unchanged: true;+ readonly cleanup: "complete";+}++/** Exercise file coordinates through public CLI/MCP using a source-owned PE fixture. */+export function verifyGhidraFileOffsets(+ targetPath: string,+ entrypoint: string,+ procedure: string,+): Promise<GhidraFileOffsetVerification>;diff --git a/scripts/lib/real-ghidra-file-offsets.mjs b/scripts/lib/real-ghidra-file-offsets.mjsindex ea881dd04..99faeda05 100644--- a/scripts/lib/real-ghidra-file-offsets.mjs+++ b/scripts/lib/real-ghidra-file-offsets.mjs@@ -14,6 +14,10 @@ import { promisify } from "node:util";import { Client } from "@modelcontextprotocol/client";import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";import { parseEvidence } from "../../dist/domain/evidence.js";+import {+ createGhidraVerifierMcpOperations,+ resolveGhidraVerifierTimeouts,+} from "./ghidra-verifier-timeouts.mjs";import {requireMcpOperationResult,requireMcpToolError,@@ -26,17 +30,22 @@ export async function verifyGhidraFileOffsets(entrypoint,procedure,) {+ const environment = {+ ...process.env,+ REA_ANALYSIS_PROVIDER: "ghidra",+ REA_LOG_LEVEL: "silent",+ };+ const { requestOptions } = resolveGhidraVerifierTimeouts(environment);+ const timeoutMs = requestOptions.timeout;const original = await readFile(targetPath);const workspace = await mkdtemp(join(tmpdir(), "rea-file-offsets-"));const runtime = join(workspace, "runtime");await mkdir(runtime);const env = {- ...process.env,+ ...environment,TEMP: runtime,TMP: runtime,TMPDIR: runtime,- REA_ANALYSIS_PROVIDER: "ghidra",- REA_LOG_LEVEL: "silent",};const client = new Client({ name: "rea-file-offsets", version: "1" });const transport = new StdioClientTransport({@@ -45,21 +54,19 @@ export async function verifyGhidraFileOffsets(env,stderr: "pipe",});+ const mcp = createGhidraVerifierMcpOperations(client, requestOptions);let connected = false;let complete = false;let primaryFailure;const call = async (name, arguments_ = {}) => {- const result = await client.callTool(- { name, arguments: arguments_ },- { timeout: 240_000 },- );+ const result = await mcp.callTool({ name, arguments: arguments_ });const projected = requireMcpOperationResult(result, name);return ["open_binary", "binary_session", "close_binary"].includes(name)? projected: (result.structuredContent ?? JSON.parse(mcpTextValue(result)));};try {- await client.connect(transport);+ await mcp.connect(transport);connected = true;await call("open_binary", { path: targetPath, provider_id: "ghidra" });const session = await call("binary_session");@@ -90,12 +97,12 @@ export async function verifyGhidraFileOffsets(),);}- const invalid = await client.callTool({+ const invalid = await mcp.callTool({name: "procedure_pseudo_code",arguments: { procedure: "file-offset:0x20000000000000" },});assert.equal(requireMcpToolError(invalid).code, "invalid_request");- const unmapped = await client.callTool({+ const unmapped = await mcp.callTool({name: "procedure_pseudo_code",arguments: { procedure: "file-offset:0x100000" },});@@ -122,7 +129,7 @@ export async function verifyGhidraFileOffsets(assert.equal(headerOffset.normalized_result.file_offset, 0);for (const selector of ["file-offset:0x0", "0x0"]) {const missing = requireMcpToolError(- await client.callTool({+ await mcp.callTool({name: "procedure_pseudo_code",arguments: { procedure: selector },}),@@ -147,7 +154,7 @@ export async function verifyGhidraFileOffsets("ghidra","--json",],- { env, timeout: 240_000, maxBuffer: 16 * 1024 * 1024 },+ { env, timeout: timeoutMs, maxBuffer: 16 * 1024 * 1024 },)).stdout,);@@ -164,7 +171,7 @@ export async function verifyGhidraFileOffsets("ghidra","--json",],- { env, timeout: 240_000, maxBuffer: 16 * 1024 * 1024 },+ { env, timeout: timeoutMs, maxBuffer: 16 * 1024 * 1024 },).then(() => {throw new Error(@@ -183,13 +190,10 @@ export async function verifyGhidraFileOffsets(const ambiguousPath = join(workspace, "ambiguous.exe");await writeFile(ambiguousPath, alias);await call("open_binary", { path: ambiguousPath, provider_id: "ghidra" });- const ambiguous = await client.callTool(- {- name: "procedure_pseudo_code",- arguments: { procedure: `file-offset:${coordinate}` },- },- { timeout: 240_000 },- );+ const ambiguous = await mcp.callTool({+ name: "procedure_pseudo_code",+ arguments: { procedure: `file-offset:${coordinate}` },+ });assert.ok(JSON.stringify(requireMcpToolError(ambiguous)).includes("multiple loaded mappings",diff --git a/scripts/verify-real-ghidra-windows.mjs b/scripts/verify-real-ghidra-windows.mjsindex 2e6e57d22..7b3fbd63f 100644--- a/scripts/verify-real-ghidra-windows.mjs+++ b/scripts/verify-real-ghidra-windows.mjs@@ -27,6 +27,7 @@ import {} from "../dist/ghidra/GhidraSessionValues.js";import { parseBinaryTarget } from "../dist/application/BinaryTargetResolver.js";import { completeVerifierRun, createVerifierRun } from "./lib/verifier-run.mjs";+import { resolveGhidraVerifierTimeouts } from "./lib/ghidra-verifier-timeouts.mjs";import { verifyGhidraCompactReferences } from "./lib/real-ghidra-compact-references.mjs";import { verifyGhidraFileOffsets } from "./lib/real-ghidra-file-offsets.mjs";import { verifyWindowsGhidraByteSearch } from "./lib/ghidra-windows-byte-search.mjs";@@ -47,6 +48,7 @@ if ()throw new Error("Windows Ghidra must not advertise database mutation");const installDir = process.env.GHIDRA_INSTALL_DIR;+const { clientOptions } = resolveGhidraVerifierTimeouts(process.env);if (installDir === undefined || !isAbsolute(installDir))throw new Error("Set GHIDRA_INSTALL_DIR to the absolute root of Ghidra 12.1.4.",@@ -116,6 +118,7 @@ const client = new GhidraClient({transport: "authenticated-loopback-tcp",providerVersion: SUPPORTED_GHIDRA_VERSION,profileDigest: profile.value.profile.digest,+ ...clientOptions,});let coordinates;diff --git a/tests/boundary/providers/ghidra/ghidraVerifierTimeouts.test.ts b/tests/boundary/providers/ghidra/ghidraVerifierTimeouts.test.tsnew file mode 100644index 000000000..51722d25d--- /dev/null+++ b/tests/boundary/providers/ghidra/ghidraVerifierTimeouts.test.ts@@ -0,0 +1,74 @@+import { randomUUID } from "node:crypto";+import { tmpdir } from "node:os";+import { join } from "node:path";++import { afterEach, describe, expect, it, vi } from "vitest";++import {+ createGhidraVerifierMcpOperations,+ resolveGhidraVerifierTimeouts,+} from "../../../../scripts/lib/ghidra-verifier-timeouts.mjs";+import { verifyGhidraFileOffsets } from "../../../../scripts/lib/real-ghidra-file-offsets.mjs";++const MAX_TIMER_DELAY_MS = 2_147_483_647;+const FILE_OFFSET_OPERATION_TIMEOUT_MS = 240_000;++afterEach(() => vi.unstubAllEnvs());++describe("Ghidra verifier timeout ownership", () => {+ it("preserves the selected startup budget for the direct client and public requests", () => {+ expect(+ resolveGhidraVerifierTimeouts({+ REA_GHIDRA_STARTUP_TIMEOUT_MS: "600000",+ }),+ ).toEqual({+ clientOptions: { startupTimeoutMs: 600_000 },+ requestOptions: { timeout: 840_000 },+ });+ });++ it("accepts the exact combined Node timer limit", () => {+ const startupTimeoutMs =+ MAX_TIMER_DELAY_MS - FILE_OFFSET_OPERATION_TIMEOUT_MS;+ expect(+ resolveGhidraVerifierTimeouts({+ REA_GHIDRA_STARTUP_TIMEOUT_MS: String(startupTimeoutMs),+ }),+ ).toEqual({+ clientOptions: { startupTimeoutMs },+ requestOptions: { timeout: MAX_TIMER_DELAY_MS },+ });+ });++ it("uses the public request budget for initialization and every tool call", async () => {+ const transport = { name: "transport" };+ const request = { name: "procedure_pseudo_code" };+ const response = { content: [] };+ const connect = vi.fn(async () => undefined);+ const callTool = vi.fn(async () => response);+ const requestOptions = { timeout: 840_000 };+ const operations = createGhidraVerifierMcpOperations(+ { connect, callTool },+ requestOptions,+ );++ await operations.connect(transport);+ await expect(operations.callTool(request)).resolves.toBe(response);++ expect(connect).toHaveBeenCalledWith(transport, requestOptions);+ expect(callTool).toHaveBeenCalledWith(request, requestOptions);+ });++ it("rejects a combined timer overflow before reading the target", async () => {+ vi.stubEnv(+ "REA_GHIDRA_STARTUP_TIMEOUT_MS",+ String(MAX_TIMER_DELAY_MS - FILE_OFFSET_OPERATION_TIMEOUT_MS + 1),+ );+ const absentTarget = join(tmpdir(), `rea-absent-${randomUUID()}`);+ await expect(+ verifyGhidraFileOffsets(absentTarget, "absent", "0x0"),+ ).rejects.toThrow(+ "The Ghidra startup and verification operation budgets exceed the Node.js timer limit.",+ );+ });+});