feat(ghidra): configurable import language and analysis seeds (#1667)

* feat(ghidra): configurable import language and analysis seeds

REA_GHIDRA_LANGUAGE_ID and REA_GHIDRA_COMPILER_SPEC_ID set the import
language when the header cannot identify the compiler (for example,
Borland C++ PE images).

REA_GHIDRA_SEED_FILE seeds functions, code and labels before
auto-analysis, for code reached only through computed jumps or data.

Both are validated before launch, recorded in the analysis profile and
verified in the bridge handshake.

* fix(ghidra): parse language XML and preserve seed ordering

* test(ghidra): use explicit platform paths for seed launch checks

* fix(javascript): retain cleanup failure reason in error causes

---------

Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
This commit is contained in:
sqmagellan
2026-10-11 09:57:38 +08:00
committed by GitHub
co-authored by morluto
parent c1efa6467d
commit af88184eee
27 changed files with 1827 additions and 20 deletions
@@ -123,7 +123,11 @@ it("preserves a failed owned cleanup retry when the next caller cancels before a
const file = source("app.mjs", "export const observed = 7;");
const first = await worker.analyze(file, 100);
if (!first.ok) throw first.error.error;
await expect(worker.close()).rejects.toBeInstanceOf(ProviderCleanupError);
const closing = worker.close();
await expect(closing).rejects.toBeInstanceOf(ProviderCleanupError);
await expect(closing).rejects.toMatchObject({
cause: { message: "Fixture owned cleanup is unavailable" },
});
const interrupted = await next.analyze(file, 100, controller.signal);
if (interrupted.ok)
throw new Error("Unreleased ownership must block subsequent analysis");
@@ -1,5 +1,5 @@
import { access, chmod, readFile, stat } from "node:fs/promises";
import { join } from "node:path";
import { join, posix, win32 } from "node:path";
import { fileURLToPath } from "node:url";
import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
@@ -160,6 +160,88 @@ describe("Ghidra headless launcher", () => {
});
});
describe("Ghidra configured import language and seeds", () => {
const base = {
platform: "darwin" as const,
projectRoot: "/tmp/project",
targetPath: "/tmp/target",
bridgeScriptPath: "/package/bridge/ReaGhidraBridge.java",
descriptorPath: "/tmp/session.json",
ghidraLogPath: "/tmp/ghidra.log",
scriptLogPath: "/tmp/script.log",
};
it("passes a configured language and compiler spec to the import", () => {
const arguments_ = ghidraHeadlessArguments({
...base,
languageOverride: {
languageId: "x86:LE:32:default",
compilerSpecId: "borlandcpp",
},
});
expectOptions(arguments_, [
["-processor", "x86:LE:32:default"],
["-cspec", "borlandcpp"],
]);
expect(arguments_).not.toContain("-loader");
expect(arguments_.indexOf("-processor")).toBeGreaterThan(
arguments_.indexOf("-import"),
);
});
it("lets Ghidra choose the language default compiler spec", () => {
const arguments_ = ghidraHeadlessArguments({
...base,
languageOverride: { languageId: "x86:LE:32:default" },
});
expectOptions(arguments_, [["-processor", "x86:LE:32:default"]]);
expect(arguments_).not.toContain("-cspec");
});
it("never combines a configured language with a DOS import", () => {
expect(() =>
ghidraHeadlessArguments({
...base,
dosMz: true,
languageOverride: { languageId: "x86:LE:32:default" },
}),
).toThrow(/cannot be combined with a DOS import/u);
});
it.each(["darwin", "win32"] as const)(
"applies seeds after loader preparation and before analysis scripts on %s",
(platform) => {
const path = platform === "win32" ? win32 : posix;
const bridgeDirectory = path.join("/package", "bridge");
const seedPath = path.join("/tmp/runtime/seeds", "seeds.tsv");
const arguments_ = ghidraHeadlessArguments({
...base,
platform,
bridgeScriptPath: path.join(bridgeDirectory, "ReaGhidraBridge.java"),
dosCom: true,
seedPath,
});
const prepare = arguments_.indexOf(
path.join(bridgeDirectory, "ReaGhidraPrepareCom.java"),
);
const seeds = arguments_.indexOf(
path.join(bridgeDirectory, "ReaGhidraApplySeeds.java"),
);
expect(prepare).toBeGreaterThanOrEqual(0);
expect(arguments_[seeds - 1]).toBe("-preScript");
expect(arguments_[seeds + 1]).toBe(seedPath);
expect(prepare).toBeLessThan(seeds);
expect(seeds).toBeLessThan(arguments_.indexOf("-postScript"));
},
);
it("adds no seed script without a seed snapshot", () => {
expect(ghidraHeadlessArguments(base)).not.toContain(
posix.join("/package/bridge", "ReaGhidraApplySeeds.java"),
);
});
});
describe("Ghidra Windows launcher", () => {
it("keeps analysis repair outside Windows P0 authority", () => {
const args = ghidraHeadlessArguments({
@@ -0,0 +1,38 @@
//@category REA Verification
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import ghidra.app.script.GhidraScript;
import ghidra.program.model.listing.Program;
/** Run the production seed script against a minimal 32-bit import. */
public final class ReaSeedRegressionProbe extends GhidraScript {
@Override
public void run() throws Exception {
currentProgram.getMemory().createUninitializedBlock(
".bss", toAddr(0x1000), 0x20, false
);
runScript("ReaGhidraApplySeeds.java", new String[] { getScriptArgs()[0] });
JsonObject report = JsonParser.parseString(
currentProgram.getOptions(Program.PROGRAM_INFO)
.getString("REA analysis seeds", null)
).getAsJsonObject();
require(report.get("entries").getAsInt() == 5, "all seeds accounted for");
require(report.get("function_created").getAsInt() == 2,
"out-of-range seed must not disturb descending function order");
require(report.get("function_failed").getAsInt() == 0, "no swallowed function entry");
require(getFunctionAt(toAddr(0)).getName().equals("seed_low"), "lower function name");
require(getFunctionAt(toAddr(1)).getName().equals("seed_high"), "higher function name");
require(report.get("label_applied").getAsInt() == 1, "mapped BSS label applied");
require(currentProgram.getSymbolTable().getPrimarySymbol(toAddr(0x1000))
.getName().equals("seed_global"), "BSS label name");
require(report.get("unmapped").getAsInt() == 2,
"only out-of-range function and uninitialized code seed are skipped");
require(report.get("code_decoded").getAsInt() == 0, "BSS is not decoded");
println("REA_SEED_REGRESSION_OK " + report);
}
private void require(boolean condition, String message) {
if (!condition) throw new AssertionError(message);
}
}