fix(ghidra): keep JVM warnings out of LaunchSupport output (#1698)

* 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>
This commit is contained in:
Petru101
2026-10-13 02:46:00 +08:00
committed by GitHub
co-authored by Claude Opus 5.5 morluto
parent 0043d9b134
commit e20994a8ea
15 changed files with 646 additions and 84 deletions
+7
View File
@@ -61,6 +61,13 @@ export const createGhidraDiagnostics = (
ghidra_log_path: options.launch.ghidraLogPath,
script_log_path: options.launch.scriptLogPath,
process_id: options.launch.process.pid ?? null,
...(options.launch.launchSupport === undefined
? {}
: {
launch_support: options.launch.launchSupport.map((output) => ({
...output,
})),
}),
}),
...(options.snapshot === undefined
? {}
+9
View File
@@ -192,6 +192,8 @@ interface GhidraInstallationObservation {
readonly applicationPropertiesPath: string | null;
readonly nativeDecompilerPath: string | null;
readonly javaCommand: string;
/** Observed java.vm.name; omitted when the probe cannot establish VM identity. */
readonly javaVmName?: string;
readonly checks: readonly GhidraInstallationCheck[];
}
@@ -232,6 +234,7 @@ export interface GhidraJavaObservation {
readonly home: string;
readonly bits: number;
readonly runtime: "jdk" | "jre";
readonly vmName?: string;
}
/** A bounded executable-header probe: a small prefix plus the whole file size. */
@@ -309,6 +312,9 @@ export const inspectGhidraInstallation = (
applicationPropertiesPath: coordinates.applicationPropertiesPath,
nativeDecompilerPath: coordinates.nativeDecompilerPath,
javaCommand: coordinates.javaCommand,
...(coordinates.java?.vmName === undefined
? {}
: { javaVmName: coordinates.java.vmName }),
checks,
};
if (failed !== undefined)
@@ -539,6 +545,7 @@ export const ghidraInstallationDiagnostics = (
java_command: inspection.javaCommand,
java_home: inspection.javaHome,
java_version: inspection.javaVersion,
java_vm_name: inspection.javaVmName ?? null,
platform: inspection.platform,
architecture: inspection.architecture,
checks: inspection.checks.map((check) => ({
@@ -756,6 +763,7 @@ const systemGhidraInstallationHost = (): GhidraInstallationHost => ({
const output = `${observed.stdout ?? ""}\n${observed.stderr ?? ""}`;
const version = javaProperty(output, "java.version");
const home = javaProperty(output, "java.home");
const vmName = javaProperty(output, "java.vm.name");
const bits = Number.parseInt(
javaProperty(output, "sun.arch.data.model") ?? "0",
10,
@@ -765,6 +773,7 @@ const systemGhidraInstallationHost = (): GhidraInstallationHost => ({
version,
major: javaMajor(version),
home,
...(vmName === null ? {} : { vmName }),
bits,
runtime: executablePath(
(process.platform === "win32" ? win32 : posix).join(
+11
View File
@@ -7,11 +7,13 @@ 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[];
@@ -26,6 +28,13 @@ export const ghidraJavaLaunch = async (options: {
];
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",
@@ -42,6 +51,7 @@ export const ghidraJavaLaunch = async (options: {
...(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("=");
@@ -63,6 +73,7 @@ export const ghidraJavaLaunch = async (options: {
...(options.signal === undefined ? {} : { signal: options.signal }),
},
);
options.onStderr("-vmargs", configuredArguments.stderr);
const vmArguments = configuredArguments.stdout
.split(/\r?\n/u)
.filter((line) => line !== "");
+68 -8
View File
@@ -21,6 +21,8 @@ import { AnalysisCancelledError } from "../domain/analysisErrorCore.js";
import { err, ok, type Result } from "../domain/result.js";
import type { AnalysisCapturedOutput } from "../domain/analysisErrorBase.js";
import { execFileOutputFailure } from "../process/ExecFileOutput.js";
import { boundedUtf8Text } from "../process/boundedUtf8Text.js";
import { redactCapturedTransportCredential } from "../process/ProviderDiagnosticRedaction.js";
import {
cleanupOwnedProcessGroup,
cleanupWindowsProcessTree,
@@ -62,6 +64,18 @@ export type GhidraLaunch = ProviderProcessLaunch & {
readonly projectRoot: string;
readonly ghidraLogPath: string;
readonly scriptLogPath: string;
readonly launchSupport?: readonly GhidraLaunchSupportDiagnostic[];
};
/** Same per-stream projection budget for helper warnings and failed commands. */
export const GHIDRA_LAUNCH_COMMAND_OUTPUT_BYTES = 8 * 1024;
/** Successful helper stderr, retained independently of the analysis JVM output. */
export type GhidraLaunchSupportDiagnostic = {
readonly mode: "-envvars" | "-vmargs";
readonly stderr: string;
readonly stderr_bytes: number;
readonly stderr_truncated: boolean;
};
/** Provider-owned capability that starts one isolated headless analysis. */
@@ -76,20 +90,24 @@ export interface GhidraLauncher {
export class GhidraLaunchError extends Error {
readonly #partialLaunch: GhidraLaunch | undefined;
readonly #capturedOutput: AnalysisCapturedOutput | undefined;
readonly launchSupport: readonly GhidraLaunchSupportDiagnostic[];
constructor(
message: string,
options?: ErrorOptions & {
readonly partialLaunch?: GhidraLaunch;
readonly capturedOutput?: AnalysisCapturedOutput;
readonly launchSupport?: readonly GhidraLaunchSupportDiagnostic[];
},
) {
const { partialLaunch, capturedOutput, ...errorOptions } = options ?? {};
const { partialLaunch, capturedOutput, launchSupport, ...errorOptions } =
options ?? {};
super(message, errorOptions);
this.name = "GhidraLaunchError";
this.#partialLaunch = partialLaunch;
this.#capturedOutput =
capturedOutput === undefined ? undefined : { ...capturedOutput };
this.launchSupport = (launchSupport ?? []).map((output) => ({ ...output }));
}
/** Process ownership retained for the client to retry an incomplete rollback. */
@@ -129,6 +147,7 @@ export interface GhidraHeadlessLauncherOptions {
readonly environment: Readonly<NodeJS.ProcessEnv>;
readonly analyzeHeadlessPath: string;
readonly javaHome?: string;
readonly javaVmName?: string;
readonly bridgeScriptPath: string;
readonly platform?: NodeJS.Platform;
readonly comSpec?: string;
@@ -173,6 +192,7 @@ export class GhidraHeadlessLauncher implements GhidraLauncher {
const paths = ghidraRuntimePaths(session.runtimeRoot);
const platform = this.options.platform ?? process.platform;
let started: SpawnedOwnedProviderProcess | undefined;
const launchSupport: GhidraLaunchSupportDiagnostic[] = [];
try {
const psp = this.options.pspExtension;
if (psp !== undefined) {
@@ -284,10 +304,32 @@ export class GhidraHeadlessLauncher implements GhidraLauncher {
platform,
analyzeHeadlessPath: this.options.analyzeHeadlessPath,
javaHome: this.options.javaHome,
...(this.options.javaVmName === undefined
? {}
: { javaVmName: this.options.javaVmName }),
homeRoot: paths.homeRoot,
tempRoot: paths.tempRoot,
arguments: headlessArguments,
environment,
onStderr: (mode, stderr) => {
if (stderr === "") return;
const bytes = Buffer.byteLength(stderr);
const redacted = redactCapturedTransportCredential(
{ text: stderr, bytes, observedBytes: bytes },
session.token,
"[REDACTED]",
);
const bounded = boundedUtf8Text(
redacted,
GHIDRA_LAUNCH_COMMAND_OUTPUT_BYTES,
);
launchSupport.push({
mode,
stderr: bounded.text,
stderr_bytes: bytes,
stderr_truncated: bounded.truncated,
});
},
...(options.signal === undefined
? {}
: { signal: options.signal }),
@@ -306,7 +348,12 @@ export class GhidraHeadlessLauncher implements GhidraLauncher {
hostEnvironment: {},
...(options.signal === undefined ? {} : { signal: options.signal }),
});
const partialLaunch = ownedGhidraLaunch(started, paths, platform);
const partialLaunch = ownedGhidraLaunch(
started,
paths,
platform,
launchSupport,
);
await writeGhidraRuntimeFile(
paths.ownershipPath,
`${JSON.stringify({
@@ -330,12 +377,17 @@ export class GhidraHeadlessLauncher implements GhidraLauncher {
return ok(partialLaunch);
} catch (cause: unknown) {
const capturedOutput = ghidraLaunchCommandOutput(cause);
const launchFailure = new GhidraLaunchError(
"Ghidra headless launch failed",
{
cause,
launchSupport,
...(capturedOutput === undefined ? {} : { capturedOutput }),
},
);
const primary = isAborted(options.signal)
? new AnalysisCancelledError("open_binary", { cause })
: new GhidraLaunchError("Ghidra headless launch failed", {
cause,
...(capturedOutput === undefined ? {} : { capturedOutput }),
});
? new AnalysisCancelledError("open_binary", { cause: launchFailure })
: launchFailure;
if (started === undefined) return err(primary);
let cleanupFailure: unknown;
@@ -349,7 +401,12 @@ export class GhidraHeadlessLauncher implements GhidraLauncher {
cleanupFailure = failure;
}
if (cleanupFailure !== undefined) {
const partialLaunch = ownedGhidraLaunch(started, paths, platform);
const partialLaunch = ownedGhidraLaunch(
started,
paths,
platform,
launchSupport,
);
const primaryMessage = isAborted(options.signal)
? primary.message
: `Ghidra headless launch failed: ${cause instanceof Error ? cause.message : String(cause)}`;
@@ -363,6 +420,7 @@ export class GhidraHeadlessLauncher implements GhidraLauncher {
{ cause: primary },
),
partialLaunch,
launchSupport,
...(capturedOutput === undefined ? {} : { capturedOutput }),
},
),
@@ -377,11 +435,13 @@ const ownedGhidraLaunch = (
spawned: SpawnedOwnedProviderProcess,
paths: ReturnType<typeof ghidraRuntimePaths>,
platform: NodeJS.Platform,
launchSupport: readonly GhidraLaunchSupportDiagnostic[],
): GhidraLaunch => ({
...ownedGhidraProcess(spawned, platform),
projectRoot: paths.projectRoot,
ghidraLogPath: paths.ghidraLogPath,
scriptLogPath: paths.scriptLogPath,
...(launchSupport.length === 0 ? {} : { launchSupport: [...launchSupport] }),
});
const ownedGhidraProcess = (
+15 -1
View File
@@ -46,6 +46,7 @@ const installationHost = (): GhidraInstallationHost => ({
home: "/usr/lib/jvm/jdk-21",
bits: 64,
runtime: "jdk",
vmName: "OpenJDK 64-Bit Server VM",
}),
});
@@ -599,6 +600,11 @@ describe("Ghidra client projection", () => {
expect.objectContaining({
runId: "11111111-1111-4111-8111-111111111111",
platform: "linux",
launcher: expect.objectContaining({
options: expect.objectContaining({
javaVmName: "OpenJDK 64-Bit Server VM",
}),
}),
}),
]);
expect(toolCalls).toEqual([
@@ -756,6 +762,11 @@ describe("Ghidra result projection", () => {
ok: false,
error: { _tag: tag },
});
if (!result.ok)
expect(projectAnalysisError(result.error)).toMatchObject({
code: "cancelled",
details: { diagnostics: { remote_code: code } },
});
});
it("returns cancellation before profile work", async () => {
@@ -1691,7 +1702,10 @@ it("projects Ghidra startup failure and its incomplete cleanup together", async
diagnostics: {
primary_error: {
code: "provider_timeout",
details: { timeout_ms: 250 },
details: {
timeout_ms: 250,
diagnostics: { failure_kind: "timeout" },
},
},
cleanup_error: { code: "cleanup_incomplete" },
},
+17 -13
View File
@@ -541,6 +541,9 @@ export const createGhidraProviderClient = (input: {
environment: input.environment,
analyzeHeadlessPath: prerequisites.value.analyzeHeadlessPath,
javaHome: prerequisites.value.javaHome,
...(installation.javaVmName === undefined
? {}
: { javaVmName: installation.javaVmName }),
bridgeScriptPath: fileURLToPath(
new URL("../../bridge/ghidra/ReaGhidraBridge.java", import.meta.url),
),
@@ -980,37 +983,40 @@ const projectPrimarySessionError = (
failure: GhidraSessionError,
startupTimeoutMs: number,
): AnalysisError => {
const context = { cause: failure, diagnostics: failure.diagnostics };
if (failure.cause instanceof AnalysisAccessDeniedError)
return new AnalysisAccessDeniedError(
operation,
failure.cause.path,
failure.cause.systemCode,
{ cause: failure },
context,
);
if (failure.cause instanceof AnalysisArtifactChangedError)
return new AnalysisArtifactChangedError(
operation,
failure.cause.path,
failure.cause.reason,
{ cause: failure },
context,
);
if (
operation === "annotate_native_function" &&
failure.kind === "remote" &&
failure.remoteCode === "invalid_function_name"
)
return new AnalysisInputError(operation, { cause: failure }, [
return new AnalysisInputError(operation, context, [
{ path: ["name"], reason: "invalid_value", message: failure.message },
]);
if (failure.kind === "cancelled")
return new AnalysisCancelledError(operation);
if (
failure.kind === "cancelled" ||
(failure.kind === "remote" && failure.remoteCode === "decompile_cancelled")
)
return new AnalysisCancelledError(operation, context);
if (failure.kind === "timeout" || failure.kind === "analysis_timeout")
return new AnalysisTimeoutError(
operation,
failure.timeoutMs ?? startupTimeoutMs,
context,
);
if (failure.kind === "remote" && failure.remoteCode === "decompile_cancelled")
return new AnalysisCancelledError(operation);
if (
failure.kind === "remote" &&
failure.remoteCode === "regex_stack_exhausted"
@@ -1021,7 +1027,7 @@ const projectPrimarySessionError = (
failure.message,
null,
{
cause: failure,
...context,
remediationAction:
"Retry this search in literal mode or simplify the regex. The active analysis session and annotations remain available.",
},
@@ -1032,7 +1038,7 @@ const projectPrimarySessionError = (
failure.remoteCode ?? "",
)
)
return new AnalysisInputError(operation, { cause: failure }, [
return new AnalysisInputError(operation, context, [
{ path: [], reason: "invalid_value", message: failure.message },
]);
if (failure.kind === "remote" && failure.remoteCode === "method_unavailable")
@@ -1040,9 +1046,7 @@ const projectPrimarySessionError = (
"ghidra",
operation,
failure.message,
context,
);
return new ProviderAdapterError("ghidra", operation, {
cause: failure,
diagnostics: failure.diagnostics,
});
return new ProviderAdapterError("ghidra", operation, context);
};
+17 -5
View File
@@ -2,13 +2,15 @@ import type { AnalysisError } from "../domain/analysisErrorBase.js";
import type { JsonValue } from "../domain/jsonValue.js";
import { DarwinProcessOwnershipInspectionError } from "../process/DarwinProcessRunTokenReader.js";
import { boundedUtf8Text } from "../process/boundedUtf8Text.js";
import { GhidraLaunchError } from "./GhidraLauncher.js";
import {
GhidraLaunchError,
GHIDRA_LAUNCH_COMMAND_OUTPUT_BYTES,
} from "./GhidraLauncher.js";
const MAX_CAUSE_DEPTH = 3;
const MAX_CAUSE_MESSAGE_BYTES = 2 * 1024;
const MAX_CAUSE_FIELD_BYTES = 1024;
const MAX_COMPILER_STDERR_BYTES = 8 * 1024;
const MAX_LAUNCH_COMMAND_OUTPUT_BYTES = 8 * 1024;
/** Stable failure phase for one private Ghidra headless session. */
export type GhidraSessionFailureKind =
@@ -229,18 +231,28 @@ const causeLaunchOutcome = (
redact: (value: string) => string,
): Record<string, JsonValue> => {
if (!(cause instanceof GhidraLaunchError)) return {};
const launchSupport =
cause.launchSupport.length === 0
? {}
: {
launch_support: cause.launchSupport.map((output) => ({
...output,
stderr: redact(output.stderr),
})),
};
const output = cause.capturedOutput;
if (output === undefined) return {};
if (output === undefined) return launchSupport;
const stdout = boundedUtf8Text(
redact(output.stdout),
MAX_LAUNCH_COMMAND_OUTPUT_BYTES,
GHIDRA_LAUNCH_COMMAND_OUTPUT_BYTES,
);
const stderr = boundedUtf8Text(
redact(output.stderr),
MAX_LAUNCH_COMMAND_OUTPUT_BYTES,
GHIDRA_LAUNCH_COMMAND_OUTPUT_BYTES,
);
const truncated = output.truncated || stdout.truncated || stderr.truncated;
return {
...launchSupport,
launch_outcome: {
stdout: stdout.text,
stderr: stderr.text,