fix(ghidra): retain process ownership after failed launch rollback
This commit is contained in:
@@ -25,7 +25,7 @@ import type {
|
||||
import type { GhidraInventoryOperation } from "./GhidraInventoryValues.js";
|
||||
import type { GhidraFunctionOperation } from "./GhidraFunctionValues.js";
|
||||
import { createGhidraDiagnostics } from "./GhidraDiagnostics.js";
|
||||
import type { GhidraLaunch } from "./GhidraLauncher.js";
|
||||
import { GhidraLaunchError, type GhidraLaunch } from "./GhidraLauncher.js";
|
||||
import { GhidraResponseBuffer } from "./GhidraResponseBuffer.js";
|
||||
import { GhidraResponseRouter } from "./GhidraResponseRouter.js";
|
||||
import { GhidraRequestQueue } from "./GhidraRequestQueue.js";
|
||||
@@ -425,8 +425,18 @@ export class GhidraClient {
|
||||
err(this.#failure("start", "Ghidra launcher failed", cause)),
|
||||
);
|
||||
if (!launched.ok) {
|
||||
const partialLaunch =
|
||||
launched.error instanceof GhidraLaunchError
|
||||
? launched.error.partialLaunch
|
||||
: undefined;
|
||||
if (partialLaunch !== undefined) {
|
||||
this.#launch = partialLaunch;
|
||||
this.#process = new ProviderProcessSupervisor(partialLaunch, {
|
||||
onDiagnostic: (event) => this.#onProcessDiagnostic(event),
|
||||
});
|
||||
}
|
||||
const failure = deadline.signal.aborted
|
||||
? this.#interruptionFailure(deadline)
|
||||
? this.#interruptionFailure(deadline, launched.error)
|
||||
: launched.error instanceof GhidraSessionError
|
||||
? launched.error
|
||||
: this.#failure("start", launched.error.message, launched.error);
|
||||
@@ -699,10 +709,13 @@ export class GhidraClient {
|
||||
return this.#startupFailure(failure);
|
||||
}
|
||||
|
||||
#interruptionFailure(deadline: ProviderStartupDeadline): GhidraSessionError {
|
||||
#interruptionFailure(
|
||||
deadline: ProviderStartupDeadline,
|
||||
cause?: unknown,
|
||||
): GhidraSessionError {
|
||||
return deadline.interruption === "cancelled"
|
||||
? this.#failure("cancelled", "Ghidra startup was cancelled")
|
||||
: this.#failure("timeout", "Ghidra startup deadline elapsed", undefined, {
|
||||
? this.#failure("cancelled", "Ghidra startup was cancelled", cause)
|
||||
: this.#failure("timeout", "Ghidra startup deadline elapsed", cause, {
|
||||
timeoutMs: deadline.timeoutMs,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -56,9 +56,21 @@ export interface GhidraLauncher {
|
||||
|
||||
/** Local launch failure retained as the cause of a provider-neutral error. */
|
||||
export class GhidraLaunchError extends Error {
|
||||
constructor(message: string, options?: ErrorOptions) {
|
||||
super(message, options);
|
||||
readonly #partialLaunch: GhidraLaunch | undefined;
|
||||
|
||||
constructor(
|
||||
message: string,
|
||||
options?: ErrorOptions & { readonly partialLaunch?: GhidraLaunch },
|
||||
) {
|
||||
const { partialLaunch, ...errorOptions } = options ?? {};
|
||||
super(message, errorOptions);
|
||||
this.name = "GhidraLaunchError";
|
||||
this.#partialLaunch = partialLaunch;
|
||||
}
|
||||
|
||||
/** Process ownership retained for the client to retry an incomplete rollback. */
|
||||
get partialLaunch(): GhidraLaunch | undefined {
|
||||
return this.#partialLaunch;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,6 +86,8 @@ export interface GhidraHeadlessLauncherOptions {
|
||||
readonly dosMz?: true;
|
||||
readonly dosCom?: true;
|
||||
readonly analysisExtensions?: readonly GhidraExtension[];
|
||||
/** Spawn seam for provider-boundary lifecycle tests. */
|
||||
readonly spawnProcess?: typeof spawnOwnedProviderProcess;
|
||||
}
|
||||
|
||||
/** Launch Ghidra without copying scripts into or modifying its installation. */
|
||||
@@ -168,7 +182,7 @@ export class GhidraHeadlessLauncher implements GhidraLauncher {
|
||||
: { signal: options.signal }),
|
||||
})
|
||||
: { ...scriptCommand, environment };
|
||||
started = await spawnOwnedProviderProcess({
|
||||
started = await (this.options.spawnProcess ?? spawnOwnedProviderProcess)({
|
||||
command: command.command,
|
||||
arguments: command.arguments,
|
||||
runId: session.runId,
|
||||
@@ -181,6 +195,7 @@ export class GhidraHeadlessLauncher implements GhidraLauncher {
|
||||
hostEnvironment: {},
|
||||
...(options.signal === undefined ? {} : { signal: options.signal }),
|
||||
});
|
||||
const partialLaunch = ownedGhidraLaunch(started, paths, platform);
|
||||
await writeGhidraRuntimeFile(
|
||||
paths.ownershipPath,
|
||||
`${JSON.stringify({
|
||||
@@ -199,33 +214,60 @@ export class GhidraHeadlessLauncher implements GhidraLauncher {
|
||||
platform,
|
||||
);
|
||||
if (isAborted(options.signal)) {
|
||||
await cleanupStartedProcess(started, platform);
|
||||
return err(new AnalysisCancelledError("open_binary"));
|
||||
throw new AnalysisCancelledError("open_binary");
|
||||
}
|
||||
const spawned = started;
|
||||
return ok({
|
||||
...ownedGhidraProcess(spawned, platform),
|
||||
projectRoot: paths.projectRoot,
|
||||
ghidraLogPath: paths.ghidraLogPath,
|
||||
scriptLogPath: paths.scriptLogPath,
|
||||
});
|
||||
return ok(partialLaunch);
|
||||
} catch (cause: unknown) {
|
||||
if (started !== undefined)
|
||||
await cleanupStartedProcess(started, platform).catch(
|
||||
(cause: unknown) => {
|
||||
// best-effort cleanup: started-process cleanup must not mask the launch failure.
|
||||
void cause;
|
||||
},
|
||||
);
|
||||
return isAborted(options.signal)
|
||||
? err(new AnalysisCancelledError("open_binary"))
|
||||
: err(
|
||||
new GhidraLaunchError("Ghidra headless launch failed", { cause }),
|
||||
const primary = isAborted(options.signal)
|
||||
? new AnalysisCancelledError("open_binary", { cause })
|
||||
: new GhidraLaunchError("Ghidra headless launch failed", { cause });
|
||||
if (started === undefined) return err(primary);
|
||||
|
||||
let cleanupFailure: unknown;
|
||||
try {
|
||||
const cleanup = await cleanupStartedProcess(started, platform);
|
||||
if (!cleanup.cleaned)
|
||||
cleanupFailure = new Error(
|
||||
`Ghidra process cleanup was incomplete: ${cleanup.reason}`,
|
||||
);
|
||||
} catch (failure: unknown) {
|
||||
cleanupFailure = failure;
|
||||
}
|
||||
if (cleanupFailure !== undefined) {
|
||||
const partialLaunch = ownedGhidraLaunch(started, paths, platform);
|
||||
const primaryMessage = isAborted(options.signal)
|
||||
? primary.message
|
||||
: `Ghidra headless launch failed: ${cause instanceof Error ? cause.message : String(cause)}`;
|
||||
return err(
|
||||
new GhidraLaunchError(
|
||||
`${primaryMessage}; process cleanup remains incomplete`,
|
||||
{
|
||||
cause: new AggregateError(
|
||||
[primary, cleanupFailure],
|
||||
"Ghidra launch rollback did not release its process",
|
||||
{ cause: primary },
|
||||
),
|
||||
partialLaunch,
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
return err(primary);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const ownedGhidraLaunch = (
|
||||
spawned: SpawnedOwnedProviderProcess,
|
||||
paths: ReturnType<typeof ghidraRuntimePaths>,
|
||||
platform: NodeJS.Platform,
|
||||
): GhidraLaunch => ({
|
||||
...ownedGhidraProcess(spawned, platform),
|
||||
projectRoot: paths.projectRoot,
|
||||
ghidraLogPath: paths.ghidraLogPath,
|
||||
scriptLogPath: paths.scriptLogPath,
|
||||
});
|
||||
|
||||
const ownedGhidraProcess = (
|
||||
spawned: SpawnedOwnedProviderProcess,
|
||||
platform: NodeJS.Platform,
|
||||
|
||||
Reference in New Issue
Block a user