From d9db9bf187ce7be7e47826dfd40860384ce27a08 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Sun, 15 Nov 2026 01:53:10 -0500 Subject: [PATCH] refactor(ghidra): classify Ghidra failures through the shared provider classifier PSP extension inspection reports through classifyProviderFailure with a Ghidra dialect. The startup deadline decides at abort time whether the shared startup owner closed it, so Ghidra and Hopper no longer compare the abort reason with the owner's sentinel, and Ghidra's startup no longer compares a producer cancellation's reason with the deadline's. Three helpers that only construct one error kind now name that kind instead of AnalysisError. --- .../provider-failure-classifier-baseline.json | 4 - src/ghidra/GhidraClient.ts | 17 ++-- src/ghidra/GhidraJavaProbe.ts | 2 +- src/ghidra/GhidraLoadImageAttest.ts | 2 +- src/ghidra/GhidraNativeAotSnapshotOwner.ts | 3 +- src/ghidra/GhidraPspExtension.ts | 85 +++++++++---------- src/hopper/HopperClient.ts | 7 +- src/process/ProviderDeadline.test.ts | 34 ++++++++ src/process/ProviderDeadline.ts | 12 +++ 9 files changed, 96 insertions(+), 70 deletions(-) diff --git a/scripts/lint/rules/provider-failure-classifier-baseline.json b/scripts/lint/rules/provider-failure-classifier-baseline.json index 145ef2c6288..a66c92e3063 100644 --- a/scripts/lint/rules/provider-failure-classifier-baseline.json +++ b/scripts/lint/rules/provider-failure-classifier-baseline.json @@ -41,10 +41,6 @@ "src/frida/FridaInstrumentationManager.ts": 1, "src/ghidra/GhidraClient.ts": 1, "src/ghidra/GhidraFileArtifacts.ts": 1, - "src/ghidra/GhidraJavaProbe.ts": 1, - "src/ghidra/GhidraLoadImageAttest.ts": 1, - "src/ghidra/GhidraNativeAotSnapshotOwner.ts": 1, - "src/ghidra/GhidraPspExtension.ts": 1, "src/go/GoBinaryProvider.ts": 1, "src/hopper/HopperFileOffset.ts": 1, "src/il2cpp/Il2cppMetadataProvider.ts": 1, diff --git a/src/ghidra/GhidraClient.ts b/src/ghidra/GhidraClient.ts index efaf607616a..25655223bc5 100644 --- a/src/ghidra/GhidraClient.ts +++ b/src/ghidra/GhidraClient.ts @@ -34,10 +34,7 @@ import { Utf8NdjsonDecodingError, } from "../process/Utf8NdjsonLineBuffer.js"; import { ABORTED, waitForAbortable } from "../process/AbortablePromise.js"; -import { - SharedStartupAttempt, - STARTUP_OWNER_CLOSED, -} from "../process/SharedStartupAttempt.js"; +import { SharedStartupAttempt } from "../process/SharedStartupAttempt.js"; import { PrivateRuntimeRoot } from "../process/PrivateRuntimeRoot.js"; import { ProviderStartupDeadline } from "../process/ProviderDeadline.js"; import { ProviderRunLineage } from "../process/ProviderRunLineage.js"; @@ -1550,17 +1547,13 @@ export class GhidraClient { cause?: unknown, cancellation?: { readonly reason: unknown }, ): GhidraSessionError { - if ( - cancellation !== undefined && - !( - deadline.signal.aborted && - cancellation.reason === deadline.signal.reason - ) - ) + // A producer that cancelled while startup's deadline still ran reports + // its own cancellation; once the deadline aborted, the deadline reports. + if (cancellation !== undefined && !deadline.signal.aborted) return this.#failure("cancelled", "Ghidra startup was cancelled", cause, { cancellation, }); - if (deadline.signal.reason === STARTUP_OWNER_CLOSED) + if (deadline.ownerClosed) return this.#failure( "process", "Ghidra session closed during startup", diff --git a/src/ghidra/GhidraJavaProbe.ts b/src/ghidra/GhidraJavaProbe.ts index 72da6bf8378..49f1cf4bab7 100644 --- a/src/ghidra/GhidraJavaProbe.ts +++ b/src/ghidra/GhidraJavaProbe.ts @@ -336,7 +336,7 @@ const systemCode = (cause: unknown): string | undefined => const probeFailureError = ( failure: GhidraJavaProbeFailure, cause: unknown, -): AnalysisError => { +): ProviderAdapterError => { const { output, ...facts } = failure; return new ProviderAdapterError("ghidra", "ghidra:java", { cause, diff --git a/src/ghidra/GhidraLoadImageAttest.ts b/src/ghidra/GhidraLoadImageAttest.ts index 77c104c62a7..244169d8a34 100644 --- a/src/ghidra/GhidraLoadImageAttest.ts +++ b/src/ghidra/GhidraLoadImageAttest.ts @@ -67,7 +67,7 @@ const comparisonFailure = ( input: NativeLoadImageAttestationInput, reason: string, cause?: unknown, -): AnalysisError => +): ProviderAdapterError => new ProviderAdapterError("ghidra", input.operation, { diagnostics: { reason }, ...(cause === undefined ? {} : { cause }), diff --git a/src/ghidra/GhidraNativeAotSnapshotOwner.ts b/src/ghidra/GhidraNativeAotSnapshotOwner.ts index 3b62e0a3608..6857533ad08 100644 --- a/src/ghidra/GhidraNativeAotSnapshotOwner.ts +++ b/src/ghidra/GhidraNativeAotSnapshotOwner.ts @@ -6,6 +6,7 @@ import type { import { analysisErrorWithCleanupFailure } from "../domain/analysisErrorCleanup.js"; import { AnalysisArtifactChangedError, + type AnalysisCancelledError, cancelledBy, carriedReason, } from "../domain/analysisErrorCore.js"; @@ -358,7 +359,7 @@ export class GhidraNativeAotSnapshotOwner { reason: unknown, observations: JsonValue, partialObservation?: AnalysisPartialObservation, - ): AnalysisError { + ): AnalysisCancelledError { return cancelledBy(operation, carriedReason(reason), { diagnostics: { measured_observation: observations }, partialObservation, diff --git a/src/ghidra/GhidraPspExtension.ts b/src/ghidra/GhidraPspExtension.ts index ca3b471c31e..47cde6fe0a0 100644 --- a/src/ghidra/GhidraPspExtension.ts +++ b/src/ghidra/GhidraPspExtension.ts @@ -4,12 +4,12 @@ import { lstat, opendir, realpath } from "node:fs/promises"; import { isAbsolute, join } from "node:path"; import { z } from "zod"; import { sameRegularFileState } from "../filesystem/RegularFile.js"; -import { - AnalysisCapabilityUnavailableError, - cancelledBy, - carriedReason, -} from "../domain/analysisErrorCore.js"; +import { AnalysisCapabilityUnavailableError } from "../domain/analysisErrorCore.js"; import { AnalysisError } from "../domain/analysisErrorBase.js"; +import { + classifyProviderFailure, + type ProviderFailureDialect, +} from "../providers/classifyProviderFailure.js"; import { err, ok, type Result } from "../domain/result.js"; import type { GhidraInstallationInspection } from "./GhidraInstallation.js"; import { parseGhidraJavaProperties } from "./GhidraJavaProperties.js"; @@ -20,9 +20,7 @@ import { AnalysisCleanupLease } from "../artifacts/AnalysisCleanupLease.js"; import { OwnedDirectoryHandle } from "../filesystem/OwnedDirectoryHandle.js"; import { assertArtifactReadActive, - artifactFileCancellationCause, artifactFilePrimaryFacts, - bindArtifactFileCleanup, withArtifactRegularFile, } from "../artifacts/ArtifactFileOperation.js"; @@ -60,7 +58,7 @@ export const inspectGhidraPspExtension = async ( fingerprint(root, ghidraVersion, resources, signal), ); } catch (cause: unknown) { - throw ghidraPspInspectionError(cause); + throw classifyProviderFailure(cause, GHIDRA_PSP_INSPECTION_DIALECT); } }; @@ -227,46 +225,40 @@ export const resolveGhidraPspExtension = async ( ), ); } catch (cause: unknown) { - return err(ghidraPspInspectionError(cause)); + return err(classifyProviderFailure(cause, GHIDRA_PSP_INSPECTION_DIALECT)); } }; -/** Preserve the producer's primary failure and its live cleanup dependencies. */ -const ghidraPspInspectionError = (cause: unknown): AnalysisError => { - if (cause instanceof AnalysisError) return cause; - const cleanup = - cause instanceof ArtifactReaderFailure ? cause.cleanup : undefined; - const { cause: primary } = artifactFilePrimaryFacts(cause); - const admission = - primary instanceof GhidraAdmissionError ? primary : undefined; - const identity = - admission?.code ?? - (primary instanceof Error ? primary.message : String(primary)); - const reason = `PSP_EXTENSION_UNAVAILABLE: ${identity} Use a compatible caller-installed ghidra-allegrex under GHIDRA_INSTALL_DIR/Ghidra/Extensions; REA does not install it.`; - const diagnostics = - admission === undefined - ? undefined - : { code: admission.code, ...admission.detail }; - const error = - cause instanceof ArtifactReaderFailure && cause.reason === "cancelled" - ? cancelledBy( - "resolve_analysis_profile", - carriedReason(artifactFileCancellationCause(cause)), - { cleanup }, - ) - : new AnalysisCapabilityUnavailableError( - "ghidra", - "resolve_analysis_profile", - reason, - { - cause, - userMessage: reason, - cleanup, - ...(diagnostics === undefined ? {} : { diagnostics }), - }, - ); - bindArtifactFileCleanup(cause, error); - return error; +/** + * How a failed extension inspection is reported. The shared classifier keeps + * typed failures, a cancelled read and the read's live cleanup; anything else + * means no compatible caller-installed extension was established. + */ +const GHIDRA_PSP_INSPECTION_DIALECT: ProviderFailureDialect = { + providerId: "ghidra", + operation: "resolve_analysis_profile", + unclassified: (cause, details) => { + const { cause: primary } = artifactFilePrimaryFacts(cause); + const admission = + primary instanceof GhidraAdmissionError ? primary : undefined; + const identity = + admission?.code ?? + (primary instanceof Error ? primary.message : String(primary)); + const reason = `PSP_EXTENSION_UNAVAILABLE: ${identity} Use a compatible caller-installed ghidra-allegrex under GHIDRA_INSTALL_DIR/Ghidra/Extensions; REA does not install it.`; + return new AnalysisCapabilityUnavailableError( + "ghidra", + "resolve_analysis_profile", + reason, + { + ...details, + cause, + userMessage: reason, + ...(admission === undefined + ? {} + : { diagnostics: { code: admission.code, ...admission.detail } }), + }, + ); + }, }; /** Close before descending, retaining the exact directory owner on uncertainty. */ @@ -315,11 +307,12 @@ const readChildren = async ( { cause: signal.reason }, ) : attempt.cause; - const error = ghidraPspInspectionError( + const error = classifyProviderFailure( ArtifactReaderFailure.withCleanup( primary, ArtifactReaderFailure.cleanupObservation(attempt.cause, path), ), + GHIDRA_PSP_INSPECTION_DIALECT, ); lease.bindCleanupError(error); throw error; diff --git a/src/hopper/HopperClient.ts b/src/hopper/HopperClient.ts index bbfb65a762a..9717987dd4c 100644 --- a/src/hopper/HopperClient.ts +++ b/src/hopper/HopperClient.ts @@ -48,10 +48,7 @@ import { import { redactCapturedTransportCredential } from "../process/ProviderDiagnosticRedaction.js"; import { ProviderRunLineage } from "../process/ProviderRunLineage.js"; import { ABORTED, waitForAbortable } from "../process/AbortablePromise.js"; -import { - SharedStartupAttempt, - STARTUP_OWNER_CLOSED, -} from "../process/SharedStartupAttempt.js"; +import { SharedStartupAttempt } from "../process/SharedStartupAttempt.js"; import { type ProviderProcessDiagnostic, ProviderProcessSupervisor, @@ -975,7 +972,7 @@ export class HopperClient { async #startupInterruption( deadline: ProviderStartupDeadline, ): Promise { - if (deadline.signal.reason === STARTUP_OWNER_CLOSED) + if (deadline.ownerClosed) return new HopperProtocolError("Hopper session closed during startup"); if (deadline.interruption === "cancelled") return hopperCancelledBySignal(deadline.signal); diff --git a/src/process/ProviderDeadline.test.ts b/src/process/ProviderDeadline.test.ts index 1e3d7697bc2..3d71be44223 100644 --- a/src/process/ProviderDeadline.test.ts +++ b/src/process/ProviderDeadline.test.ts @@ -1,5 +1,6 @@ import { afterEach, beforeEach, expect, it, vi } from "vitest"; import { ProviderStartupDeadline } from "./ProviderDeadline.js"; +import { STARTUP_OWNER_CLOSED } from "./SharedStartupAttempt.js"; beforeEach(() => { vi.useFakeTimers({ @@ -323,3 +324,36 @@ it("finalizes an expired startup under its own budget and the caller's signal", deadline.dispose(); } }); + +it.each([ + { reason: STARTUP_OWNER_CLOSED, ownerClosed: true }, + { reason: new Error("caller stopped"), ownerClosed: false }, +])( + "reports whether the startup owner closed: $ownerClosed", + ({ reason, ownerClosed }) => { + const controller = new AbortController(); + const deadline = new ProviderStartupDeadline(1000, controller.signal); + try { + expect(deadline.ownerClosed).toBe(false); + controller.abort(reason); + expect(deadline.interruption).toBe("cancelled"); + expect(deadline.ownerClosed).toBe(ownerClosed); + expect(deadline.signal.reason).toBe(reason); + } finally { + deadline.dispose(); + } + }, +); + +it("does not report an owner close after the budget elapsed", async () => { + const controller = new AbortController(); + const deadline = new ProviderStartupDeadline(100, controller.signal); + try { + await vi.advanceTimersByTimeAsync(100); + controller.abort(STARTUP_OWNER_CLOSED); + expect(deadline.interruption).toBe("timeout"); + expect(deadline.ownerClosed).toBe(false); + } finally { + deadline.dispose(); + } +}); diff --git a/src/process/ProviderDeadline.ts b/src/process/ProviderDeadline.ts index 635a6b84356..7aeee499e98 100644 --- a/src/process/ProviderDeadline.ts +++ b/src/process/ProviderDeadline.ts @@ -1,4 +1,5 @@ import { Deadline } from "../domain/deadline.js"; +import { STARTUP_OWNER_CLOSED } from "./SharedStartupAttempt.js"; /** Result of an abort-aware interval wait. */ export type AbortableWaitResult = "elapsed" | "aborted"; @@ -46,6 +47,7 @@ export class ProviderStartupDeadline { readonly #onExternalAbort: () => void; #timer: NodeJS.Timeout | undefined; #interruption: "cancelled" | "timeout" | undefined; + #ownerClosed = false; constructor( readonly timeoutMs: number, @@ -66,6 +68,8 @@ export class ProviderStartupDeadline { return; } this.#interruption = "cancelled"; + // The startup owner closing is not a caller's cancellation. + this.#ownerClosed = signal?.reason === STARTUP_OWNER_CLOSED; this.#abort(signal?.reason); }; if (signal?.aborted === true) { @@ -88,6 +92,14 @@ export class ProviderStartupDeadline { ); } + /** + * True when the cancellation came from the shared startup owner closing, + * not from a caller. Decided when the caller's signal aborted. + */ + get ownerClosed(): boolean { + return this.#ownerClosed; + } + /** Milliseconds still available to the next startup phase. */ remainingMs(): number { // Node timers can fire a fraction of a millisecond before the monotonic