fix(ghidra): consume inspected configuration directory identity (#4077)
* fix(ghidra): consume inspected configuration directory identity Carry the parsed release coordinate through installation inspection and provider prerequisites into the launcher. Remove duplicate property reads and basename guesses, preserve unresolved identity, and canonicalize directory versions without changing observed provider versions. Keep verifier callers, lifecycle regressions, and installation guidance aligned. Extract the Ghidra-only follow-up from #2293 without changing CLI output or unrelated documentation. * docs(ghidra): clarify startup failure side-effect boundary Distinguish the launcher's directory and Java startup guard from the client's earlier private runtime allocation and target snapshot. Keep existing failure cleanup and runtime behavior unchanged. --------- Co-authored-by: Kaoru0822 <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
This commit is contained in:
16 files changed
+255
-57
No files matched your search
@@ -471,6 +471,85 @@ describe("Ghidra installation rejection diagnostics", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("Ghidra configuration directory identity", () => {
|
||||
it.each([
|
||||
[
|
||||
"application.version=12.1.4\napplication.release.name=PUBLIC\n",
|
||||
"ghidra_12.1.4_PUBLIC",
|
||||
],
|
||||
[
|
||||
"application.version=12.1.2-2.1\napplication.release.name=DEV\n",
|
||||
"ghidra_12.1.2-2.1_DEV",
|
||||
],
|
||||
[
|
||||
"application.version=12.1.4\napplication.release.name=pub\\\n lic\n",
|
||||
"ghidra_12.1.4_PUBLIC",
|
||||
],
|
||||
["application.version=12.1.4\n", null],
|
||||
["application.release.name=PUBLIC\n", null],
|
||||
["application.version=12.1.4\napplication.release.name=\n", null],
|
||||
["application.version=12.1.4\napplication.release.name=../outside\n", null],
|
||||
[
|
||||
"application.version=12.1.4\napplication.release.name=..\\\\outside\n",
|
||||
null,
|
||||
],
|
||||
["application.version=../12.1.4\napplication.release.name=PUBLIC\n", null],
|
||||
[
|
||||
"application.version=12.1.4+build\napplication.release.name=PUBLIC\n",
|
||||
null,
|
||||
],
|
||||
["application.version=12.1.4\napplication.release.name=\\uNOTHEX\n", null],
|
||||
])("derives a safe release coordinate from %j", (content, expected) => {
|
||||
expect(ghidraApplicationProperties(content)).toMatchObject({
|
||||
configurationDirectoryName: expected,
|
||||
});
|
||||
});
|
||||
|
||||
it.each([
|
||||
["12.1.0", "ghidra_12.1_PUBLIC"],
|
||||
["012.01.000", "ghidra_12.1_PUBLIC"],
|
||||
["12.1.0-BETA", "ghidra_12.1-BETA_PUBLIC"],
|
||||
["12.1.0-", "ghidra_12.1_PUBLIC"],
|
||||
])(
|
||||
"uses Ghidra's canonical configuration version while retaining %s",
|
||||
(raw, expected) => {
|
||||
expect(
|
||||
ghidraApplicationProperties(
|
||||
`application.version=${raw}\napplication.release.name=PUBLIC\n`,
|
||||
),
|
||||
).toMatchObject({
|
||||
providerVersion: raw,
|
||||
configurationDirectoryName: expected,
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
it.each([
|
||||
[
|
||||
"application.version=12.1.4\napplication.release.name=PUBLIC\napplication.java.min=21\n",
|
||||
"ghidra_12.1.4_PUBLIC",
|
||||
],
|
||||
["application.version=12.1.4\napplication.java.min=21\n", null],
|
||||
])(
|
||||
"preserves the inspected identity independently of the installation basename",
|
||||
(content, expected) => {
|
||||
const inspection = inspectGhidraInstallation(
|
||||
{
|
||||
environment: {},
|
||||
installDir: INSTALL,
|
||||
platform: "linux",
|
||||
architecture: "x64",
|
||||
},
|
||||
host({ readText: () => content }),
|
||||
);
|
||||
expect(inspection).toMatchObject({
|
||||
status: "available",
|
||||
configurationDirectoryName: expected,
|
||||
});
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe("Ghidra release line and JDK bounds", () => {
|
||||
it("uses Java Properties last-key, delimiter, escape, and continuation rules", () => {
|
||||
const properties = ghidraApplicationProperties(
|
||||
@@ -486,6 +565,7 @@ describe("Ghidra release line and JDK bounds", () => {
|
||||
);
|
||||
expect(properties).toEqual({
|
||||
providerVersion: "12.1.4",
|
||||
configurationDirectoryName: null,
|
||||
javaMinimum: { status: "major", major: 21 },
|
||||
javaMaximum: { status: "absent" },
|
||||
parseError: null,
|
||||
|
||||
@@ -202,6 +202,8 @@ interface GhidraInstallationObservation {
|
||||
readonly platform: NodeJS.Platform;
|
||||
readonly architecture: NodeJS.Architecture;
|
||||
readonly applicationPropertiesPath: string | null;
|
||||
/** Parsed release identity, never inferred from the installation directory name. */
|
||||
readonly configurationDirectoryName: string | null;
|
||||
readonly nativeDecompilerPath: string | null;
|
||||
readonly javaCommand: string;
|
||||
/** Observed java.vm.name; omitted when the probe cannot establish VM identity. */
|
||||
@@ -351,6 +353,8 @@ export const inspectGhidraInstallation = (
|
||||
platform,
|
||||
architecture,
|
||||
applicationPropertiesPath: coordinates.applicationPropertiesPath,
|
||||
configurationDirectoryName:
|
||||
coordinates.properties?.configurationDirectoryName ?? null,
|
||||
nativeDecompilerPath: coordinates.nativeDecompilerPath,
|
||||
javaCommand: coordinates.javaCommand,
|
||||
...(coordinates.java?.vmName === undefined
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { parseGhidraJavaProperties } from "./GhidraJavaProperties.js";
|
||||
import { ghidraConfigurationVersion } from "./GhidraReleasePolicy.js";
|
||||
|
||||
/** One Java major bound declared by `application.properties`. */
|
||||
type GhidraJavaBound =
|
||||
@@ -18,6 +19,8 @@ export type GhidraJavaRequirement =
|
||||
/** Version and Java bounds read from one `application.properties` file. */
|
||||
export interface GhidraApplicationProperties {
|
||||
readonly providerVersion: string | null;
|
||||
/** Release-owned private settings directory; null when its identity is unresolved. */
|
||||
readonly configurationDirectoryName: string | null;
|
||||
readonly javaMinimum: GhidraJavaBound;
|
||||
readonly javaMaximum: GhidraJavaBound;
|
||||
/** A malformed Java properties escape invalidates the entire file. */
|
||||
@@ -32,15 +35,36 @@ export const ghidraApplicationProperties = (
|
||||
if (!parsed.ok)
|
||||
return {
|
||||
providerVersion: null,
|
||||
configurationDirectoryName: null,
|
||||
javaMinimum: { status: "unresolved", raw: parsed.error },
|
||||
javaMaximum: { status: "unresolved", raw: parsed.error },
|
||||
parseError: parsed.error,
|
||||
};
|
||||
const providerVersion = parsed.value.get("application.version");
|
||||
// ApplicationIdentifier removes Java's ASCII whitespace and uppercases the
|
||||
// release name. Derive only a single safe component for official Ghidra builds.
|
||||
const releaseName = parsed.value
|
||||
.get("application.release.name")
|
||||
?.replaceAll(/[ \t\n\r\f\v]/gu, "")
|
||||
.toUpperCase();
|
||||
// providerVersion above remains the original evidence.
|
||||
const configurationVersion =
|
||||
providerVersion === undefined
|
||||
? undefined
|
||||
: ghidraConfigurationVersion(providerVersion);
|
||||
const configurationDirectoryName =
|
||||
providerVersion !== undefined &&
|
||||
configurationVersion !== undefined &&
|
||||
releaseName !== undefined &&
|
||||
/^[A-Za-z0-9][A-Za-z0-9._+-]*$/u.test(providerVersion) &&
|
||||
/^[A-Za-z0-9][A-Za-z0-9._-]*$/u.test(releaseName)
|
||||
? `ghidra_${configurationVersion}_${releaseName}`
|
||||
: null;
|
||||
const minimum = parsed.value.get("application.java.min");
|
||||
const maximum = parsed.value.get("application.java.max");
|
||||
return {
|
||||
providerVersion: providerVersion ?? null,
|
||||
configurationDirectoryName,
|
||||
javaMinimum:
|
||||
minimum === undefined ? { status: "absent" } : parseJavaBound(minimum),
|
||||
// Ghidra uses an explicitly empty maximum to mean no upper bound.
|
||||
|
||||
@@ -42,6 +42,7 @@ const probeLauncher = (javaHome: string, runtimeRoot: string) =>
|
||||
environment: {},
|
||||
platform: "darwin",
|
||||
analyzeHeadlessPath: join(runtimeRoot, "support", "analyzeHeadless"),
|
||||
configurationDirectoryName: "ghidra_test_PUBLIC",
|
||||
javaHome,
|
||||
bridgeScriptPath: join(runtimeRoot, "bridge", "ReaGhidraBridge.java"),
|
||||
});
|
||||
@@ -152,6 +153,7 @@ describe("Ghidra launch failure diagnostics", () => {
|
||||
environment: {},
|
||||
platform: "darwin",
|
||||
analyzeHeadlessPath: join(runtimeRoot, "support", "analyzeHeadless"),
|
||||
configurationDirectoryName: "ghidra_test_PUBLIC",
|
||||
bridgeScriptPath: join(runtimeRoot, "bridge", "ReaGhidraBridge.java"),
|
||||
});
|
||||
const launched = await launcher.launch(launchSession(runtimeRoot));
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import { parseGhidraJavaProperties } from "./GhidraJavaProperties.js";
|
||||
import { GHIDRA_BRIDGE_SOURCES } from "./GhidraBridgeSources.js";
|
||||
import {
|
||||
snapshotGhidraAnalysisSeeds,
|
||||
@@ -6,7 +5,7 @@ import {
|
||||
} from "./GhidraAnalysisSeeds.js";
|
||||
import type { GhidraLanguageOverride } from "../config/ghidraLanguageOverride.js";
|
||||
import { snapshotEnvironment } from "../process/snapshotEnvironment.js";
|
||||
import { mkdir, readFile, realpath } from "node:fs/promises";
|
||||
import { mkdir, realpath } from "node:fs/promises";
|
||||
import { basename, dirname, join, posix, win32 } from "node:path";
|
||||
|
||||
import writeFileAtomic from "write-file-atomic";
|
||||
@@ -149,6 +148,8 @@ const ghidraLaunchCommandOutput = (
|
||||
export interface GhidraHeadlessLauncherOptions {
|
||||
readonly environment: Readonly<NodeJS.ProcessEnv>;
|
||||
readonly analyzeHeadlessPath: string;
|
||||
/** Release coordinate owned by installation inspection; null must not be guessed. */
|
||||
readonly configurationDirectoryName: string | null;
|
||||
readonly javaHome?: string;
|
||||
readonly javaVmName?: string;
|
||||
readonly bridgeScriptPath: string;
|
||||
@@ -200,6 +201,10 @@ export class GhidraHeadlessLauncher implements GhidraLauncher {
|
||||
let configuredMaxHeap: string | undefined;
|
||||
const launchSupport: GhidraLaunchSupportDiagnostic[] = [];
|
||||
try {
|
||||
if (this.options.configurationDirectoryName === null)
|
||||
throw new GhidraLaunchError(
|
||||
"Ghidra configuration directory identity is unresolved; restore application.version and application.release.name in the selected installation's application.properties.",
|
||||
);
|
||||
const psp = this.options.pspExtension;
|
||||
if (psp !== undefined) {
|
||||
const installedRoot = await realpath(
|
||||
@@ -228,7 +233,7 @@ export class GhidraHeadlessLauncher implements GhidraLauncher {
|
||||
await createGhidraRuntimeDirectories(
|
||||
paths,
|
||||
platform,
|
||||
await ghidraConfigReleaseDirectory(this.options.analyzeHeadlessPath),
|
||||
this.options.configurationDirectoryName,
|
||||
);
|
||||
const seedPath =
|
||||
this.options.analysisSeeds === undefined
|
||||
@@ -754,32 +759,6 @@ const ghidraRuntimePaths = (runtimeRoot: string) => ({
|
||||
* that no supported JDK was found. Create the release directory while this
|
||||
* process still owns the runtime, leaving Ghidra only a file write inside it.
|
||||
*/
|
||||
const ghidraConfigReleaseDirectory = async (
|
||||
analyzeHeadlessPath: string,
|
||||
): Promise<string> => {
|
||||
const installRoot = dirname(dirname(analyzeHeadlessPath));
|
||||
try {
|
||||
const properties = await readFile(
|
||||
join(installRoot, "Ghidra", "application.properties"),
|
||||
"latin1",
|
||||
);
|
||||
const parsed = parseGhidraJavaProperties(properties);
|
||||
if (!parsed.ok) return basename(installRoot);
|
||||
const version = parsed.value.get("application.version");
|
||||
const release = parsed.value.get("application.release.name");
|
||||
return version === undefined ||
|
||||
release === undefined ||
|
||||
release.length === 0
|
||||
? basename(installRoot)
|
||||
: `ghidra_${version}_${release}`;
|
||||
} catch (cause: unknown) {
|
||||
// An unreadable properties file leaves the release coordinate unknown; the
|
||||
// conventional extraction directory carries the same name.
|
||||
void cause;
|
||||
return basename(installRoot);
|
||||
}
|
||||
};
|
||||
|
||||
const createGhidraRuntimeDirectories = async (
|
||||
paths: ReturnType<typeof ghidraRuntimePaths>,
|
||||
platform: NodeJS.Platform,
|
||||
|
||||
@@ -46,6 +46,7 @@ it("uses selected mixed-case Windows PATH and ComSpec through direct launch boun
|
||||
environment,
|
||||
platform: "win32",
|
||||
analyzeHeadlessPath: "C:\\ghidra\\support\\analyzeHeadless.bat",
|
||||
configurationDirectoryName: "ghidra_12.1.4_PUBLIC",
|
||||
bridgeScriptPath: "C:\\rea\\bridge.py",
|
||||
});
|
||||
environment.ComSpec = "C:\\changed\\cmd.exe";
|
||||
|
||||
@@ -149,6 +149,30 @@ it.each([
|
||||
},
|
||||
);
|
||||
|
||||
it("carries the inspected release identity into the production launcher", async () => {
|
||||
const launchers: GhidraHeadlessLauncher[] = [];
|
||||
const ghidra = provider(
|
||||
{
|
||||
...installationHost(),
|
||||
readText: () =>
|
||||
"application.version=12.1.4\napplication.release.name=PUBLIC\napplication.java.min=21\n",
|
||||
},
|
||||
(options) => {
|
||||
launchers.push(options.launcher as GhidraHeadlessLauncher);
|
||||
return {
|
||||
start: () => Promise.resolve(ok(sessionInfo())),
|
||||
callTool: () => Promise.resolve(ok(null)),
|
||||
close: () => Promise.resolve(ok(null)),
|
||||
};
|
||||
},
|
||||
);
|
||||
const client = await createElfClient(ghidra);
|
||||
expect(launchers[0]?.options).toMatchObject({
|
||||
configurationDirectoryName: "ghidra_12.1.4_PUBLIC",
|
||||
});
|
||||
await client.close();
|
||||
});
|
||||
|
||||
describe("Ghidra provider", () => {
|
||||
it("commits DOS loader and real-mode language while retaining the CPU family", async () => {
|
||||
const ghidra = provider();
|
||||
|
||||
@@ -580,6 +580,8 @@ export const createGhidraProviderClient = (input: {
|
||||
launcher: new GhidraHeadlessLauncher({
|
||||
environment: input.environment,
|
||||
analyzeHeadlessPath: prerequisites.value.analyzeHeadlessPath,
|
||||
configurationDirectoryName:
|
||||
prerequisites.value.configurationDirectoryName,
|
||||
javaHome: prerequisites.value.javaHome,
|
||||
...(installation.javaVmName === undefined
|
||||
? {}
|
||||
@@ -986,6 +988,7 @@ export const createGhidraProviderClient = (input: {
|
||||
|
||||
interface GhidraClientCoordinates {
|
||||
readonly analyzeHeadlessPath: string;
|
||||
readonly configurationDirectoryName: string | null;
|
||||
readonly javaHome: string;
|
||||
readonly providerVersion: string;
|
||||
readonly profile: AnalysisProfileCommitment;
|
||||
@@ -1026,6 +1029,7 @@ const ghidraClientPrerequisites = (
|
||||
return err(new ProviderAdapterError("ghidra", "health"));
|
||||
return ok({
|
||||
analyzeHeadlessPath: installation.analyzeHeadlessPath,
|
||||
configurationDirectoryName: installation.configurationDirectoryName,
|
||||
javaHome: installation.javaHome,
|
||||
providerVersion: installation.providerVersion,
|
||||
profile,
|
||||
|
||||
@@ -8,6 +8,7 @@ interface GhidraReleaseVersion {
|
||||
readonly major: number;
|
||||
readonly minor: number;
|
||||
readonly patch: number;
|
||||
readonly suffix: string;
|
||||
}
|
||||
|
||||
/** How an observed Ghidra release relates to this adapter's bridge. */
|
||||
@@ -108,7 +109,22 @@ const parseGhidraRelease = (raw: string): GhidraReleaseVersion | undefined => {
|
||||
if (match === null || major === undefined || minor === undefined)
|
||||
return undefined;
|
||||
const patch = match[3] === undefined ? 0 : integerGroup(match[3]);
|
||||
return patch === undefined ? undefined : { major, minor, patch };
|
||||
return patch === undefined
|
||||
? undefined
|
||||
: { major, minor, patch, suffix: match[4] ?? "" };
|
||||
};
|
||||
|
||||
/**
|
||||
* The version spelling Ghidra's `ApplicationVersion.toString` uses in its
|
||||
* settings directory: numeric components normalized, a zero patch and an
|
||||
* empty tag omitted. A build-metadata (`+`) suffix has no such spelling.
|
||||
*/
|
||||
export const ghidraConfigurationVersion = (raw: string): string | undefined => {
|
||||
const version = parseGhidraRelease(raw);
|
||||
if (version === undefined || version.suffix.startsWith("+")) return undefined;
|
||||
const patch = version.patch === 0 ? "" : `.${String(version.patch)}`;
|
||||
const suffix = version.suffix === "-" ? "" : version.suffix;
|
||||
return `${String(version.major)}.${String(version.minor)}${patch}${suffix}`;
|
||||
};
|
||||
|
||||
const integerGroup = (value: string | undefined): number | undefined => {
|
||||
|
||||
Reference in new issue
Block a user