* fix(ghidra): keep JVM warnings out of LaunchSupport output Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(ghidra): retain LaunchSupport warnings in session diagnostics * fix(ghidra): select helper logging flags by observed JVM identity * fix(ghidra): preserve diagnostics through typed failure projection --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
106 lines
3.6 KiB
TypeScript
106 lines
3.6 KiB
TypeScript
import { snapshotEnvironment } from "../process/snapshotEnvironment.js";
|
|
import { dirname, join } from "node:path";
|
|
import { execFileOutput } from "../process/ExecFileOutput.js";
|
|
|
|
/** Launch coordinates obtained from Ghidra's own release configuration helper. */
|
|
export const ghidraJavaLaunch = async (options: {
|
|
readonly platform: NodeJS.Platform;
|
|
readonly analyzeHeadlessPath: string;
|
|
readonly javaHome: string;
|
|
readonly javaVmName?: string;
|
|
readonly homeRoot: string;
|
|
readonly tempRoot: string;
|
|
readonly arguments: readonly string[];
|
|
readonly environment: NodeJS.ProcessEnv;
|
|
readonly signal?: AbortSignal;
|
|
readonly onStderr: (mode: "-envvars" | "-vmargs", stderr: string) => void;
|
|
}): Promise<{
|
|
readonly command: string;
|
|
readonly arguments: readonly string[];
|
|
readonly environment: NodeJS.ProcessEnv;
|
|
}> => {
|
|
const supportRoot = dirname(options.analyzeHeadlessPath);
|
|
const installRoot = dirname(supportRoot);
|
|
const command = join(options.javaHome, "bin", "java");
|
|
const isolatedProperties = [
|
|
`-Duser.home=${options.homeRoot}`,
|
|
`-Djava.io.tmpdir=${options.tempRoot}`,
|
|
];
|
|
const supportArguments = [
|
|
...isolatedProperties,
|
|
// Only identified HotSpot VMs have these unified-logging semantics. OpenJ9
|
|
// uses different -Xlog behavior; missing VM identity must stay unknown.
|
|
...(/^(?:OpenJDK|Java HotSpot\(TM\)) (?:64-Bit )?(?:Server|Client|Zero) VM$/u.test(
|
|
options.javaVmName ?? "",
|
|
)
|
|
? ["-Xlog:disable", "-Xlog:all=warning:stderr"]
|
|
: []),
|
|
"-cp",
|
|
join(supportRoot, "LaunchSupport.jar"),
|
|
"LaunchSupport",
|
|
installRoot,
|
|
];
|
|
const environment = {
|
|
...snapshotEnvironment(options.environment, options.platform),
|
|
};
|
|
const configuredEnvironment = await execFileOutput(
|
|
command,
|
|
[...supportArguments, "-envvars"],
|
|
{
|
|
env: environment,
|
|
...(options.signal === undefined ? {} : { signal: options.signal }),
|
|
},
|
|
);
|
|
options.onStderr("-envvars", configuredEnvironment.stderr);
|
|
for (const line of configuredEnvironment.stdout.split(/\r?\n/u)) {
|
|
if (line === "") continue;
|
|
const separator = line.indexOf("=");
|
|
const sourceKey = line.slice(0, separator);
|
|
const key =
|
|
options.platform === "win32" ? sourceKey.toUpperCase() : sourceKey;
|
|
if (separator < 1 || !/^[A-Za-z_][A-Za-z0-9_]*$/u.test(key))
|
|
throw new Error(
|
|
`Invalid Ghidra LaunchSupport environment entry: ${line}`,
|
|
);
|
|
if ((environment[key] ?? "") === "")
|
|
environment[key] = line.slice(separator + 1);
|
|
}
|
|
const configuredArguments = await execFileOutput(
|
|
command,
|
|
[...supportArguments, "-vmargs"],
|
|
{
|
|
env: environment,
|
|
...(options.signal === undefined ? {} : { signal: options.signal }),
|
|
},
|
|
);
|
|
options.onStderr("-vmargs", configuredArguments.stderr);
|
|
const vmArguments = configuredArguments.stdout
|
|
.split(/\r?\n/u)
|
|
.filter((line) => line !== "");
|
|
if (
|
|
vmArguments.some(
|
|
(argument) => !argument.startsWith("-") || argument.includes("\0"),
|
|
)
|
|
)
|
|
throw new Error("Invalid Ghidra LaunchSupport JVM argument");
|
|
return {
|
|
command,
|
|
environment,
|
|
arguments: [
|
|
...vmArguments,
|
|
...(options.platform === "darwin" ? ["-Xdock:name=Ghidra-Headless"] : []),
|
|
`-Xmx${environment.GHIDRA_HEADLESS_MAXMEM || environment.GHIDRA_MAXMEM || "2G"}`,
|
|
"-XX:ParallelGCThreads=2",
|
|
"-XX:CICompilerCount=2",
|
|
"-Djava.awt.headless=true",
|
|
...isolatedProperties,
|
|
"-showversion",
|
|
"-cp",
|
|
join(installRoot, "Ghidra", "Framework", "Utility", "lib", "Utility.jar"),
|
|
"ghidra.Ghidra",
|
|
"ghidra.app.util.headless.AnalyzeHeadless",
|
|
...options.arguments,
|
|
],
|
|
};
|
|
};
|