fix(ghidra): expose NativeAOT opt-out recovery (#12923)

This commit is contained in:
morluto authored and GitHub committed 2026-11-20 21:12:13 +00:00
1 parent 9c3ab4a0df
commit 753b7e908c
3 files changed
+98 -116

No files matched your search

+82 -76
View File
@@ -97,6 +97,7 @@ import type { BinaryTarget } from "../domain/binaryTargetTypes.js";
import { parseExecutableHeader } from "../domain/binaryTarget.js";
import { elf, thinMach } from "../domain/binaryTarget.fixture.js";
import { GhidraProvider } from "./GhidraProvider.js";
import type { GhidraExtensionResult } from "./extensions/GhidraExtensions.js";
import { GhidraClient, type GhidraTargetSnapshot } from "./GhidraClient.js";
import {
ghidraMissingProgramGuidance,
@@ -4322,97 +4323,102 @@ const peTarget = (
});
describe("Ghidra extension failures", () => {
it.each(["unsupported", "failed", "malformed"] as const)(
it.each([
"unsupported",
"failed",
"wrong artifact",
"loader failure",
] as const)(
"preserves %s recovery diagnostics and closes without restarting",
async (status) => {
async (scenario) => {
const directory = await createTestTempDirectory(
"rea-provider-extension-",
);
const jar = join(directory, "addon.jar");
await writeFile(jar, Buffer.from([0x50, 0x4b, 3, 4]));
const config = parseConfig({
GHIDRA_INSTALL_DIR: INSTALL,
REA_GHIDRA_NATIVEAOT_JAR: jar,
});
if (!config.ok) throw config.error;
let starts = 0,
calls = 0,
closes = 0;
const ghidra = new GhidraProvider(
config.value,
silentLogger,
{},
const bytes = Buffer.from([0x50, 0x4b, 3, 4]);
await writeFile(jar, bytes);
const status =
scenario === "unsupported" || scenario === "wrong artifact"
? "unsupported"
: "failed";
const reason =
scenario === "loader failure"
? "Extension loading/analysis failed: ClassNotFoundException"
: "directory-discovery: Unsupported layout at 0x401000";
const report: GhidraExtensionResult = {
id: "nativeaot",
sha256:
scenario === "wrong artifact"
? "0".repeat(64)
: createHash("sha256").update(bytes).digest("hex"),
status,
reason,
result:
scenario === "loader failure"
? { loader_failure: reason }
: {
id: "nativeaot",
integration_api: 1,
source_revision: "effeb734fc570c32650f88b159608979dc7b423e",
source_revision_authority: "build-reported-unattested",
status,
reason,
method_tables: 0,
diagnostics: ["exact producer diagnostic"],
},
};
const counts = { starts: 0, calls: 0, closes: 0 };
const ghidra = provider(
installationHost(),
() => ({
start: () => {
starts++;
return Promise.resolve(
ok({
...sessionInfo(),
analysis_extensions: [
{
id: "nativeaot",
sha256:
status === "malformed"
? "0".repeat(64)
: createHash("sha256")
.update(Buffer.from([0x50, 0x4b, 3, 4]))
.digest("hex"),
status: status === "malformed" ? "failed" : status,
reason:
"directory-discovery: Unsupported layout at 0x401000",
result: {
id: "nativeaot",
integration_api: 1,
source_revision:
"effeb734fc570c32650f88b159608979dc7b423e",
source_revision_authority: "build-reported-unattested",
status: status === "malformed" ? "failed" : status,
reason:
"directory-discovery: Unsupported layout at 0x401000",
method_tables: 0,
diagnostics: ["exact producer diagnostic"],
},
},
],
}),
);
start: async () => {
counts.starts++;
return ok({ ...sessionInfo(), analysis_extensions: [report] });
},
callTool: () => {
calls++;
return Promise.resolve(ok(null));
callTool: async () => {
counts.calls++;
return ok(null);
},
close: () => {
closes++;
return Promise.resolve(ok(null));
close: async () => {
counts.closes++;
return ok(null);
},
}),
{ REA_GHIDRA_NATIVEAOT_JAR: jar },
);
const target = executableTarget("elf", "x86_64");
const resolved = await ghidra.resolveAnalysisProfile(target);
if (!resolved.ok || resolved.value.profile === null)
throw new Error("expected extension profile");
const client = ghidra.createClient(target, resolved.value.profile);
onTestFinished(async () => {
await ghidra.closeOwnedResources();
});
const client = await createElfClient(ghidra);
onTestFinished(async () => {
await client.close();
});
const failed = await client.execute("health", {});
expect(failed.ok).toBe(false);
if (!failed.ok) {
expect(failed.error._tag).toBe(
status === "unsupported"
? "AnalysisCapabilityUnavailableError"
: "ProviderAdapterError",
);
expect(JSON.stringify(failed.error)).toContain(
status === "malformed"
? "identity mismatch"
: "Unsupported layout at 0x401000",
);
if (failed.ok) throw new Error("Expected extension handshake refusal");
if (scenario === "unsupported") {
expect(projectAnalysisError(failed.error)).toMatchObject({
code: "capability_unavailable",
retryable: false,
details: { reason: expect.stringContaining(reason) },
remediation: {
action: expect.stringContaining("REA_GHIDRA_NATIVEAOT_JAR"),
},
});
} else {
expect(failed.error).toMatchObject({
_tag: "ProviderAdapterError",
diagnostics: {
reason:
scenario === "wrong artifact"
? expect.stringContaining("identity mismatch")
: reason,
analysis_extensions: [report],
},
});
}
expect(await client.execute("list_procedures", {})).toEqual(failed);
expect({ starts, calls, closes }).toEqual({
starts: 1,
calls: 0,
closes: 1,
});
expect(counts).toEqual({ starts: 1, calls: 0, closes: 1 });
},
);
});
@@ -14,9 +14,8 @@ export const ghidraExtensionFailure = (
results: readonly GhidraExtensionResult[],
operation: string,
): AnalysisError | undefined => {
const reports = results;
const invalid = validateGhidraExtensionResults(extensions, reports);
const failed = reports.find(
const invalid = validateGhidraExtensionResults(extensions, results);
const failed = results.find(
(value) => value.status === "unsupported" || value.status === "failed",
);
if (invalid === null && failed === undefined) return undefined;
@@ -24,12 +23,16 @@ export const ghidraExtensionFailure = (
? new AnalysisCapabilityUnavailableError(
"ghidra",
operation,
`${failed.id}: ${failed.reason}. Omit REA_GHIDRA_NATIVEAOT_JAR to continue ordinary native analysis.`,
`${failed.id}: ${failed.reason}`,
{
remediationAction:
"Omit REA_GHIDRA_NATIVEAOT_JAR to continue ordinary native analysis.",
},
)
: new ProviderAdapterError("ghidra", operation, {
diagnostics: {
reason: invalid ?? failed?.reason ?? "Ghidra extension failed",
analysis_extensions: jsonValueSchema.parse(reports),
analysis_extensions: jsonValueSchema.parse(results),
},
});
};
+8 -35
View File
@@ -3,7 +3,7 @@ import { execFile } from "node:child_process";
import { constants } from "node:fs";
import { promisify } from "node:util";
import { createHash } from "node:crypto";
import { open, readFile, writeFile } from "node:fs/promises";
import { open, readFile, readdir, realpath, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import { createTestTempDirectory } from "../../../tests/fixtures/temporaryDirectory.js";
@@ -24,10 +24,7 @@ afterEach(async () => {
await resources.close();
resources = new ArtifactResourceScope();
});
const root = async () => {
const path = await createTestTempDirectory("rea-extension-test-");
return path;
};
const root = () => createTestTempDirectory("rea-extension-test-");
const target: BinaryTarget = {
path: "/tmp/native",
kind: "executable",
@@ -138,30 +135,23 @@ describe("optional Ghidra extension boundary", () => {
false,
);
});
it("commits the actual regular artifact bytes and caller path", async () => {
it("commits the configured artifact and snapshots its exact bytes exclusively", async () => {
const { extension, bytes, path } = await artifact();
expect(extension).toMatchObject({
configured_path: path,
path,
path: await realpath(path),
sha256: createHash("sha256").update(bytes).digest("hex"),
integration_api: 1,
});
});
it("snapshots identical bytes exclusively into the private runtime", async () => {
const { extension, bytes } = await artifact();
const runtime = await root();
const snapshots = await snapshotGhidraExtensions(
[extension],
runtime,
resources,
);
expect(snapshots[0]?.path).toBe(join(runtime, "extensions/nativeaot.jar"));
expect(
Buffer.compare(
await readFile(join(runtime, "extensions/nativeaot.jar")),
bytes,
),
).toBe(0);
const staged = join(runtime, "extensions", "nativeaot.jar");
expect(snapshots).toEqual([{ ...extension, path: staged }]);
expect((await readFile(staged)).equals(bytes)).toBe(true);
await expect(
snapshotGhidraExtensions([extension], runtime, resources),
).rejects.toMatchObject({
@@ -394,6 +384,7 @@ describe("Ghidra extension profile and producer validation", () => {
reason: "unregistered",
},
});
expect(await readdir(runtime)).toEqual([]);
});
});
@@ -467,24 +458,6 @@ describe("Ghidra extension recovery coverage", () => {
),
).toContain("producer report");
});
it("preserves real loader failures without requiring a successful analyzer report", async () => {
const { extension } = await artifact();
const reason = "Extension loading/analysis failed: ClassNotFoundException";
expect(
validateGhidraExtensionResults(
[extension],
[
{
id: "nativeaot",
sha256: extension.sha256,
status: "failed",
reason,
result: { loader_failure: reason },
},
],
),
).toBeNull();
});
it("does not accept complete recovery without format and byte identities", async () => {
const { extension } = await artifact();
const incomplete = result(extension.sha256);