From bc8b3c4cfaf3904f47ae86b15e4bb4df4c9d05c7 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Tue, 17 Nov 2026 03:58:30 +0800 Subject: [PATCH] fix(process): classify macOS ownership helper capacity exhaustion as resource constraint (#11771) * fix(process): classify macOS ownership helper capacity exhaustion as resource constraint Refs #11679 Refs #11714 * fix(process): exclude cancelled preparation from host capacity detection --- src/android/JadxJavaProbe.test.ts | 41 ++++++++++ src/android/JadxJavaProbe.ts | 11 ++- src/apktool/ApktoolConfiguration.test.ts | 41 ++++++++++ src/apktool/ApktoolConfiguration.ts | 61 +++++++-------- .../reverse/ReverseEngineeringService.ts | 13 ++-- .../GhidraJavaProbe.preparation.test.ts | 78 +++++++++++++++++++ src/ghidra/GhidraJavaProbe.ts | 8 +- src/process/OwnedCommand.ts | 24 ++++++ .../ownedProcessAvailability.test.ts | 17 ++++ src/providers/ownedProcessAvailability.ts | 6 +- .../reverseProviderLaunchErrors.test.ts | 34 ++++++++ 11 files changed, 293 insertions(+), 41 deletions(-) diff --git a/src/android/JadxJavaProbe.test.ts b/src/android/JadxJavaProbe.test.ts index 31fda3503f..33d290b5d0 100644 --- a/src/android/JadxJavaProbe.test.ts +++ b/src/android/JadxJavaProbe.test.ts @@ -1,4 +1,5 @@ import { validateJadxConfiguration } from "./JadxConfiguration.js"; +import { DarwinProcessOwnershipInspectionError } from "../process/DarwinProcessRunTokenReader.js"; import { ProviderCleanupError } from "../domain/providerCleanupError.js"; import { ArtifactResourceRetryFailure } from "../resources/ArtifactResourceScope.js"; import { expect, it, vi } from "vitest"; @@ -287,3 +288,43 @@ it.each([ } }, ); + +it("reports EMFILE during macOS ownership helper preparation as a resource constraint", async () => { + const resources = createInspectionResources(); + const emfile = spawnFailure("EMFILE", "/usr/bin/swiftc"); + const prepare = vi + .spyOn(systemProcessOwnershipHost, "prepare") + .mockRejectedValue( + new DarwinProcessOwnershipInspectionError("helper capacity exhausted", { + cause: emfile, + }), + ); + try { + const result: unknown = await inspectJadxJava( + "/selected/java", + {}, + { + resourceOwner: resources.owner, + launcher: async (input) => { + await prepareProcessOwnershipInspection(input.signal); + throw new Error("Java must not start"); + }, + }, + ).catch((cause: unknown) => cause); + expect(result).toBeInstanceOf(AnalysisError); + if (!(result instanceof AnalysisError)) throw result; + expect(projectAnalysisError(result)).toMatchObject({ + code: "resource_constraint", + retryable: true, + details: { + diagnostics: { + system_code: "EMFILE", + java_executable: "/selected/java", + }, + }, + }); + } finally { + prepare.mockRestore(); + await resources.close(); + } +}); diff --git a/src/android/JadxJavaProbe.ts b/src/android/JadxJavaProbe.ts index adb611b59d..20205c39dd 100644 --- a/src/android/JadxJavaProbe.ts +++ b/src/android/JadxJavaProbe.ts @@ -24,6 +24,7 @@ import { jsonValueSchema, type JsonValue } from "../domain/jsonValue.js"; import { err, ok, type Result } from "../domain/result.js"; import { prepareOwnedCommand, + preparationHostCapacityFailure, runOwnedCommand, OwnedCommandFailure, } from "../process/OwnedCommand.js"; @@ -221,7 +222,11 @@ export const inspectJadxJava = async ( }, }, }); - } else if (preparation !== undefined && command?.reason !== "timeout") { + } else if ( + preparation !== undefined && + command?.reason !== "timeout" && + hostConstraint === undefined + ) { primary = new ProviderAdapterError("jadx", operation, { cause, ...(output === undefined ? {} : { capturedOutput: output }), @@ -295,7 +300,9 @@ const hostLaunchConstraint = ( command: Pick | undefined, cause: unknown, ) => { - const launch = launcherFailureOf(command?.processError ?? cause); + const launch = + preparationHostCapacityFailure(cause) ?? + launcherFailureOf(command?.processError ?? cause); return launch?.kind === "host-exhausted" || launch?.kind === "argument-list" ? launch : undefined; diff --git a/src/apktool/ApktoolConfiguration.test.ts b/src/apktool/ApktoolConfiguration.test.ts index 573519351c..845db4a5e1 100644 --- a/src/apktool/ApktoolConfiguration.test.ts +++ b/src/apktool/ApktoolConfiguration.test.ts @@ -1,6 +1,7 @@ import * as commandOwner from "../process/OwnedCommand.js"; import { failedInspectionDescriptor } from "../../tests/fixtures/processInspectionCleanup.js"; import { awaitAcknowledgement } from "../../tests/fixtures/lifecycleAcknowledgement.js"; +import { spawnFailure } from "../../tests/support/process/spawnFailure.js"; import { AnalysisError, primaryAnalysisError, @@ -9,6 +10,7 @@ import { ProcessOwnershipIdentityBatchFailure, ProcessOwnershipInspectionCancelled, } from "../process/ProcessInspectionCancellation.js"; +import { DarwinProcessOwnershipInspectionError } from "../process/DarwinProcessRunTokenReader.js"; import { systemProcessOwnershipHost } from "../process/ProcessOwnershipObservation.js"; import { ApktoolCommandCleanup } from "./ApktoolCommandCleanup.js"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; @@ -663,3 +665,42 @@ it("keeps a recorded command timeout primary with its actual inspection stop fac prepare.mockRestore(); } }); + +it("reports EMFILE during macOS ownership helper preparation as a resource constraint", async () => { + const emfile = spawnFailure("EMFILE", "/usr/bin/swiftc"); + const darwin = new DarwinProcessOwnershipInspectionError( + "helper capacity exhausted", + { cause: emfile }, + ); + const failure = new commandOwner.OwnedCommandFailure( + "process", + "Ownership preparation failed", + null, + null, + { preparationFailure: darwin }, + ); + const prepare = vi + .spyOn(systemProcessOwnershipHost, "prepare") + .mockResolvedValue(undefined); + const command = vi + .spyOn(commandOwner, "runOwnedCommand") + .mockRejectedValue(failure); + try { + const result: unknown = await inspectApktoolClient( + { command: "/selected/apktool", commandSource: "environment" }, + commands, + { platform: "linux" }, + ).catch((cause: unknown) => cause); + expect(result).toMatchObject({ + _tag: "AnalysisResourceConstraintError", + retryable: true, + diagnostics: { + system_code: "EMFILE", + selected_command: "/selected/apktool", + }, + }); + } finally { + command.mockRestore(); + prepare.mockRestore(); + } +}); diff --git a/src/apktool/ApktoolConfiguration.ts b/src/apktool/ApktoolConfiguration.ts index a4e35015c9..c5652f43cd 100644 --- a/src/apktool/ApktoolConfiguration.ts +++ b/src/apktool/ApktoolConfiguration.ts @@ -26,6 +26,7 @@ import { ProviderStartupDeadline } from "../process/ProviderDeadline.js"; import { randomUUID } from "node:crypto"; import { OwnedCommandFailure, + preparationHostCapacityFailure, runOwnedCommand, prepareOwnedCommand, } from "../process/OwnedCommand.js"; @@ -488,10 +489,35 @@ const versionProbeFailure = ( ) ); if (inspectionCancellation !== undefined) return inspectionCancellation; - if ( - preparation !== undefined && - !(cause instanceof OwnedCommandFailure && cause.reason === "timeout") - ) + if (cause instanceof OwnedCommandFailure && cause.reason === "timeout") + return new AnalysisTimeoutError( + "inspect_apktool_client", + VERSION_TIMEOUT_MS, + { + cause, + ...(output === undefined ? {} : { capturedOutput: output }), + ...apktoolVersionProbeTimeoutGuidance(), + }, + ); + const launch = + preparationHostCapacityFailure(cause) ?? launcherFailureOf(cause); + // The host, not the selected launcher, refused this launch. + if (launch?.kind === "host-exhausted" || launch?.kind === "argument-list") + return classifyLaunchFailure(launch, { + providerId: "apktool", + operation: "inspect_apktool_client", + command: selection.command, + fixedArguments: true, + selection: APKTOOL_COMMAND_ENV, + details: { + cause, + diagnostics: { + selected_command: selection.command, + command_source: selection.commandSource, + }, + }, + }); + if (preparation !== undefined) return new ProviderAdapterError("apktool", "inspect_apktool_client", { cause, diagnostics: { @@ -510,33 +536,6 @@ const versionProbeFailure = ( ? {} : { capturedOutput: preparation.capturedOutput }), }); - if (cause instanceof OwnedCommandFailure && cause.reason === "timeout") - return new AnalysisTimeoutError( - "inspect_apktool_client", - VERSION_TIMEOUT_MS, - { - cause, - ...(output === undefined ? {} : { capturedOutput: output }), - ...apktoolVersionProbeTimeoutGuidance(), - }, - ); - const launch = launcherFailureOf(cause); - // The host, not the selected launcher, refused this launch. - if (launch?.kind === "host-exhausted" || launch?.kind === "argument-list") - return classifyLaunchFailure(launch, { - providerId: "apktool", - operation: "inspect_apktool_client", - command: selection.command, - fixedArguments: true, - selection: APKTOOL_COMMAND_ENV, - details: { - cause, - diagnostics: { - selected_command: selection.command, - command_source: selection.commandSource, - }, - }, - }); if (launch?.kind === "missing" || launch?.kind === "not_executable") { const execDenied = launch.kind === "not_executable"; return new ApktoolConfigurationFailure( diff --git a/src/application/reverse/ReverseEngineeringService.ts b/src/application/reverse/ReverseEngineeringService.ts index a33b3128d9..d5028e67d6 100644 --- a/src/application/reverse/ReverseEngineeringService.ts +++ b/src/application/reverse/ReverseEngineeringService.ts @@ -71,6 +71,7 @@ import { import { classifyLaunchFailure } from "../../process/classifyProviderOutcome.js"; import { OwnedCommandFailure, + preparationHostCapacityFailure, runOwnedCommand, } from "../../process/OwnedCommand.js"; import { DarwinProcessOwnershipInspectionError } from "../../process/DarwinProcessRunTokenReader.js"; @@ -780,11 +781,13 @@ export class ReverseEngineeringService { outputLimitReached = true; // Host capacity and an oversized argument list are classified by the // shared launch owner; a refusal keeps this provider's repair below. - const launch = launcherFailureOf( - cause instanceof LinuxOwnedProcessPrerequisiteError - ? cause.cause - : cause, - ); + const launch = + preparationHostCapacityFailure(ownedCause) ?? + launcherFailureOf( + cause instanceof LinuxOwnedProcessPrerequisiteError + ? cause.cause + : cause, + ); if ( earlyFailure === undefined && (launch?.kind === "host-exhausted" || launch?.kind === "argument-list") diff --git a/src/ghidra/GhidraJavaProbe.preparation.test.ts b/src/ghidra/GhidraJavaProbe.preparation.test.ts index da8b1b5214..05069373a3 100644 --- a/src/ghidra/GhidraJavaProbe.preparation.test.ts +++ b/src/ghidra/GhidraJavaProbe.preparation.test.ts @@ -5,6 +5,7 @@ import { primaryAnalysisError, } from "../domain/analysisErrorBase.js"; import { ArtifactResourceRetryFailure } from "../resources/ArtifactResourceScope.js"; +import { DarwinProcessOwnershipInspectionError } from "../process/DarwinProcessRunTokenReader.js"; import { ProcessOwnershipIdentityBatchFailure, ProcessOwnershipInspectionCancelled, @@ -16,6 +17,7 @@ import { import type { OwnedProviderProcessSpawnOptions } from "../process/ProviderProcess.js"; import { failedInspectionDescriptor } from "../../tests/fixtures/processInspectionCleanup.js"; import { createInspectionResources } from "../../tests/fixtures/inspectionResources.js"; +import { spawnFailure } from "../../tests/support/process/spawnFailure.js"; it.each([false, true])( "retains physical preparation ownership before Java launch (batch: %s)", @@ -141,3 +143,79 @@ it("keeps the owned command deadline primary when launcher inspection is stopped await resources.close(); } }); + +it("reports EMFILE during macOS ownership helper preparation as a resource constraint", async () => { + const resources = createInspectionResources(); + const emfile = spawnFailure("EMFILE", "/usr/bin/swiftc"); + const prepare = vi + .spyOn(systemProcessOwnershipHost, "prepare") + .mockRejectedValue( + new DarwinProcessOwnershipInspectionError("helper capacity exhausted", { + cause: emfile, + }), + ); + try { + const result = await probeGhidraJava( + "/selected/java", + {}, + { + resourceOwner: resources.owner, + launcher: async (input) => { + await prepareProcessOwnershipInspection(input.signal); + throw new Error("Java must not start"); + }, + }, + ); + expect(result.ok).toBe(false); + if (result.ok) throw new Error("Expected resource constraint failure"); + expect(result.error).toMatchObject({ + _tag: "AnalysisResourceConstraintError", + retryable: true, + diagnostics: { + system_code: "EMFILE", + java_executable: "/selected/java", + }, + }); + } finally { + prepare.mockRestore(); + await resources.close(); + } +}); + +it("keeps caller cancellation primary when macOS helper preparation is cancelled with a spawn EMFILE reason", async () => { + const resources = createInspectionResources(); + const reason = spawnFailure("EMFILE", "/usr/bin/swiftc"); + const prepare = vi + .spyOn(systemProcessOwnershipHost, "prepare") + .mockRejectedValue( + new DarwinProcessOwnershipInspectionError( + "helper compilation cancelled", + { + cause: reason, + cancellation: { reason }, + }, + ), + ); + try { + const result = await probeGhidraJava( + "/selected/java", + {}, + { + resourceOwner: resources.owner, + launcher: async (input) => { + await prepareProcessOwnershipInspection(input.signal); + throw new Error("Java must not start"); + }, + }, + ); + expect(result.ok).toBe(false); + if (result.ok) throw new Error("Expected cancelled probe failure"); + expect(result.error).toMatchObject({ + _tag: "AnalysisCancelledError", + cause: reason, + }); + } finally { + prepare.mockRestore(); + await resources.close(); + } +}); diff --git a/src/ghidra/GhidraJavaProbe.ts b/src/ghidra/GhidraJavaProbe.ts index 3706472cef..47319344c2 100644 --- a/src/ghidra/GhidraJavaProbe.ts +++ b/src/ghidra/GhidraJavaProbe.ts @@ -20,6 +20,7 @@ import type { RetainedResourceScope } from "../ports/AnalysisProvider.js"; import { DarwinProcessOwnershipInspectionError } from "../process/DarwinProcessRunTokenReader.js"; import { OwnedCommandFailure, + preparationHostCapacityFailure, runOwnedCommand, prepareOwnedCommand, } from "../process/OwnedCommand.js"; @@ -272,7 +273,8 @@ const observeGhidraJava = async ( }); } else if ( preparation !== undefined && - commandFailure?.reason !== "timeout" + commandFailure?.reason !== "timeout" && + preparationHostCapacityFailure(cause) === undefined ) { primary = new ProviderAdapterError("ghidra", "ghidra:java", { cause, @@ -292,7 +294,9 @@ const observeGhidraJava = async ( } else if (cause instanceof AnalysisError) { primary = cause; } - const launch = launcherFailureOf(commandFailure?.processError ?? cause); + const launch = + preparationHostCapacityFailure(cause) ?? + launcherFailureOf(commandFailure?.processError ?? cause); if ( primary === undefined && (launch?.kind === "host-exhausted" || launch?.kind === "argument-list") diff --git a/src/process/OwnedCommand.ts b/src/process/OwnedCommand.ts index 73e40a67d0..802c093a61 100644 --- a/src/process/OwnedCommand.ts +++ b/src/process/OwnedCommand.ts @@ -40,6 +40,7 @@ import { waitForAbortableDelay, } from "./ProviderDeadline.js"; import { errorMessage } from "../kernel/errorMessage.js"; +import { launcherFailureOf, type LauncherFailure } from "./launchFailure.js"; /** A short-lived command failed its lifecycle or complete-output contract. */ export class OwnedCommandFailure extends Error { @@ -198,6 +199,29 @@ function throwCommandPreparationFailure( throw cause; } +/** + * Ownership preparation could not start a process it needed, such as the + * macOS ownership helper's compiler, because the host lacked capacity. The + * command cannot start for the same reason, so callers report it as their own + * host-capacity launch failure. Other preparation failures, including a + * missing helper compiler, say nothing about the command's executable. + */ +export const preparationHostCapacityFailure = ( + cause: unknown, +): LauncherFailure | undefined => { + if (cause instanceof OwnedCommandFailure && cause.cancellation !== undefined) + return undefined; + const preparation = + cause instanceof OwnedCommandFailure ? cause.preparationFailure : cause; + if ( + !(preparation instanceof DarwinProcessOwnershipInspectionError) || + preparation.cancellation !== undefined + ) + return undefined; + const launch = launcherFailureOf(preparation.cause); + return launch?.kind === "host-exhausted" ? launch : undefined; +}; + /** Execute a single owned process with independent cleanup and bounded diagnostic retention. */ export const runOwnedCommand = async ( spawn: OwnedProviderProcessSpawnOptions, diff --git a/src/providers/ownedProcessAvailability.test.ts b/src/providers/ownedProcessAvailability.test.ts index 9004bdbc1c..fe14912626 100644 --- a/src/providers/ownedProcessAvailability.test.ts +++ b/src/providers/ownedProcessAvailability.test.ts @@ -15,6 +15,7 @@ import { processOwnershipAdmissionRecovery, } from "../process/ProcessOwnershipObservation.js"; import { ownedProcessPrerequisiteAvailability } from "./ownedProcessAvailability.js"; +import { spawnFailure } from "../../tests/support/process/spawnFailure.js"; describe("Linux process table prerequisite discovery", () => { it.each(["ENOENT", "EACCES", "EIO"])( @@ -259,4 +260,20 @@ describe("Linux ownership prerequisite discovery", () => { }, }); }); + + it("does not report macOS ownership helper capacity exhaustion as missing Command Line Tools", () => { + const emfile = spawnFailure("EMFILE", "/usr/bin/swiftc"); + const darwin = new DarwinProcessOwnershipInspectionError( + "helper compiler capacity exhausted", + { cause: emfile }, + ); + expect(ownedProcessPrerequisiteAvailability(darwin)).toBeUndefined(); + expect( + ownedProcessPrerequisiteAvailability( + new OwnedCommandFailure("process", "preparation failed", null, null, { + preparationFailure: darwin, + }), + ), + ).toBeUndefined(); + }); }); diff --git a/src/providers/ownedProcessAvailability.ts b/src/providers/ownedProcessAvailability.ts index 3c53b21a48..48181620c1 100644 --- a/src/providers/ownedProcessAvailability.ts +++ b/src/providers/ownedProcessAvailability.ts @@ -2,7 +2,10 @@ import { AnalysisCapabilityUnavailableError } from "../domain/analysisErrorCore. import { ProviderAdapterError } from "../domain/providerAdapterError.js"; import { jsonValueSchema } from "../domain/jsonValue.js"; import { DarwinProcessOwnershipInspectionError } from "../process/DarwinProcessRunTokenReader.js"; -import { OwnedCommandFailure } from "../process/OwnedCommand.js"; +import { + OwnedCommandFailure, + preparationHostCapacityFailure, +} from "../process/OwnedCommand.js"; import { DARWIN_PROCESS_OWNERSHIP_REMEDIATION, LINUX_PROCESS_OWNERSHIP_REMEDIATION, @@ -54,6 +57,7 @@ const darwinPrerequisiteRefusal = ( current = current.preparationFailure; } return current instanceof DarwinProcessOwnershipInspectionError && + preparationHostCapacityFailure(current) === undefined && current.cleanupFailure === undefined && current.cleanupOwner === undefined && current.cancellation === undefined diff --git a/tests/composition/reverseProviderLaunchErrors.test.ts b/tests/composition/reverseProviderLaunchErrors.test.ts index 69582488ed..f1919b8f55 100644 --- a/tests/composition/reverseProviderLaunchErrors.test.ts +++ b/tests/composition/reverseProviderLaunchErrors.test.ts @@ -530,6 +530,40 @@ it("preserves command deadline timeout when macOS helper preparation aborts with } }); +it("keeps caller cancellation primary when macOS helper preparation is cancelled with a spawn EMFILE reason", async () => { + const { directory, path } = await createArtifact(); + const command = resolve(directory, "configured-objdump"); + const reason = spawnFailure("EMFILE", "/usr/bin/swiftc"); + const darwinError = new DarwinProcessOwnershipInspectionError( + "Darwin process ownership helper preparation was cancelled", + { + cause: reason, + cancellation: { reason }, + }, + ); + const service = new TestReverseEngineeringService({ + environment: { REA_OBJDUMP_COMMAND: command }, + runOwned: async () => { + throw darwinError; + }, + }); + try { + const result = await service.inspectWithObjdump({ + path, + operation: "file_headers", + }); + expect(result.ok).toBe(false); + if (!result.ok) { + expect(result.error._tag).toBe("AnalysisCancelledError"); + expect(result.error.cause).toBe(reason); + } + expect(service.observedLaunchFailures()).toEqual({}); + } finally { + await service.close(); + await rm(directory, { recursive: true, force: true }); + } +}); + it("uses platform-aware recovery without /proc when Darwin baseline capture rejects identity_snapshot_unavailable", async () => { const { directory } = await createArtifact(); const command = resolve(directory, "configured-objdump");