Reverse engineer anything with agents, from app behavior down to native binaries.
TypeScript agent-skillsai-agentsbinary-analysisclaude-codeclicodexcordisdecompilerdeveloper-toolsdshdsh-pluginghidrahopperllmmcpmodel-context-protocolreareverse-engineeringstatic-analysistouhou
fix(apktool): own the version probe's process group and keep its timeout typed (#3808)
Kaoru0822089ef732e80e
diff --git a/src/apktool/ApktoolConfiguration.ts b/src/apktool/ApktoolConfiguration.tsindex 729c670e9..cacacf404 100644--- a/src/apktool/ApktoolConfiguration.ts+++ b/src/apktool/ApktoolConfiguration.ts@@ -8,10 +8,12 @@ import { snapshotEnvironment } from "../process/snapshotEnvironment.js";import { ABORTED, waitForAbortable } from "../process/AbortablePromise.js";import { ProviderStartupDeadline } from "../process/ProviderDeadline.js";+import { randomUUID } from "node:crypto";import {- execFileOutput,- execFileOutputFailure,-} from "../process/ExecFileOutput.js";+ OwnedCommandFailure,+ runOwnedCommand,+} from "../process/OwnedCommand.js";+import type { ProviderProcessSupervisor } from "../process/ProviderProcess.js";import type { ProviderAvailability } from "../application/AnalysisProvider.js";import type { ProviderRejectionCode } from "../contracts/providerSelection.js";import type { JsonValue } from "../domain/jsonValue.js";@@ -152,14 +154,23 @@ export type ApktoolConfigurationFailureCode =| "version_unresolved"| "unsupported_version";+/** Unresolved cleanup of a launched probe; its owner was retained separately. */+export interface ApktoolProbeCleanup {+ readonly reason: string;+ readonly resources: readonly string[];+}+export class ApktoolConfigurationFailure extends Error {+ readonly cleanup: ApktoolProbeCleanup | undefined;+constructor(readonly code: ApktoolConfigurationFailureCode,message: string,readonly diagnostics: Readonly<Record<string, JsonValue>> = {},- options?: ErrorOptions,+ options?: ErrorOptions & { readonly cleanup?: ApktoolProbeCleanup },) {super(message, options);+ this.cleanup = options?.cleanup;}}@@ -228,6 +239,7 @@ export const inspectApktoolClient = async (signal?: AbortSignal,environment: Readonly<Record<string, string | undefined>> = process.env,platform: NodeJS.Platform = process.platform,+ onCleanupOwner?: (owner: ProviderProcessSupervisor) => void,): Promise<ApktoolClientIdentity> => {const hostEnvironment = snapshotEnvironment(environment, platform);let invocation: ApktoolInvocation;@@ -254,23 +266,57 @@ export const inspectApktoolClient = async (let stdout: string;let stderr: string;try {- ({ stdout, stderr } = await execFileOutput(- invocation.command,- invocation.arguments,+ // The owned process group stops a shell launcher's children with it; a+ // child holding inherited output pipes cannot outlive the deadline.+ const output = await runOwnedCommand({+ command: invocation.command,+ arguments: invocation.arguments,windowsVerbatimArguments: invocation.windowsVerbatimArguments,- timeout: VERSION_TIMEOUT_MS,- stopSignal: "SIGTERM",- maxBuffer: VERSION_MAX_OUTPUT_BYTES,- env: hostEnvironment,- ...(signal === undefined ? {} : { signal }),+ // Apktool launchers are commonly interpreter scripts, so ownership is+ // proved by the run token rather than the launcher's argv prefix.+ expectedCommand: null,+ runId: randomUUID(),+ hostEnvironment,},- ));+ {+ timeoutMs: VERSION_TIMEOUT_MS,+ diagnosticBytes: VERSION_MAX_OUTPUT_BYTES,+ },+ signal === undefined ? {} : { signal },+ );+ stdout = output.stdout.text;+ stderr = output.stderr.text;} catch (cause) {- const failure = execFileOutputFailure(cause);- if (signal?.aborted || failure?.code === "ABORT_ERR")- throw new AnalysisCancelledError("inspect_apktool_client", { cause });- if (failure?.code === "ENOENT")+ if (+ cause instanceof OwnedCommandFailure &&+ cause.cleanupOwner !== undefined+ )+ onCleanupOwner?.(cause.cleanupOwner);+ const cleanup =+ cause instanceof OwnedCommandFailure && cause.cleanupFailure !== null+ ? { reason: cause.cleanupFailure, resources: cause.resources }+ : undefined;+ const cleanupDetails = cleanup === undefined ? {} : { cleanup };+ if (+ signal?.aborted === true ||+ (cause instanceof OwnedCommandFailure && cause.reason === "cancelled")+ )+ throw new AnalysisCancelledError("inspect_apktool_client", {+ cause,+ ...cleanupDetails,+ });+ if (cause instanceof OwnedCommandFailure && cause.reason === "timeout")+ throw new AnalysisTimeoutError(+ "inspect_apktool_client",+ VERSION_TIMEOUT_MS,+ { cause, ...cleanupDetails },+ );+ if (+ cause instanceof Error &&+ "code" in cause &&+ (cause.code === "ENOENT" || cause.code === "EACCES")+ )throw new ApktoolConfigurationFailure("command_missing",`The selected apktool launcher was not found: ${selection.command}`,@@ -281,19 +327,25 @@ export const inspectApktoolClient = async (},{ cause },);+ const snapshot =+ cause instanceof OwnedCommandFailure ? cause.snapshot : null;throw new ApktoolConfigurationFailure("version_unresolved","The selected apktool launcher did not report a usable version",{selected_command: selection.command,- ...(failure === undefined+ ...(snapshot === null? {}: {- exit_code: failure.code,- stderr_excerpt: failure.stderr.slice(0, 512),+ exit_code: snapshot.exitCode ?? null,+ stderr_excerpt: snapshot.stderr.text.slice(0, 512),}),+ ...(cause instanceof OwnedCommandFailure &&+ cause.reason === "output-limit"+ ? { max_output_bytes: VERSION_MAX_OUTPUT_BYTES }+ : {}),},- { cause },+ { cause, ...cleanupDetails },);}const identity = parseApktoolVersionOutput(stdout);diff --git a/src/apktool/ApktoolProvider.ts b/src/apktool/ApktoolProvider.tsindex eec08fe43..2fa61fe5f 100644--- a/src/apktool/ApktoolProvider.ts+++ b/src/apktool/ApktoolProvider.ts@@ -56,6 +56,7 @@ import { WindowsBatchCommandError } from "../process/WindowsBatchCommand.js";import {APKTOOL_COMMAND_ENV,ApktoolConfigurationFailure,+ type ApktoolProbeCleanup,apktoolConfigurationAvailability,apktoolInvocation,inspectApktoolClient,@@ -148,6 +149,19 @@ interface DecodeLimits {readonly maxOutputBytes: number;}+/** A probe whose launched processes could not be confirmed stopped. */+const probeCleanupFailure = (+ operation: string,+ failure: ApktoolConfigurationFailure,+ cleanup: ApktoolProbeCleanup,+): ProviderCleanupError =>+ new ProviderCleanupError(+ APKTOOL_PROVIDER_IDENTITY.id,+ cleanup.resources,+ { reason: cleanup.reason, probe_failure: failure.message },+ { operation, cause: failure },+ );+const runApktool = async (operation: string,selection: ApktoolCommandSelection,@@ -180,6 +194,9 @@ const runApktool = async (command: invocation.command,arguments: invocation.arguments,windowsVerbatimArguments: invocation.windowsVerbatimArguments,+ // Apktool launchers are commonly interpreter scripts, so ownership is+ // proved by the run token rather than the launcher's argv prefix.+ expectedCommand: null,runId: randomUUID(),hostEnvironment: environment,},@@ -328,6 +345,10 @@ export class ApktoolProvider {private readonly workspaces = new Set<string>();private readonly removeWorkspace: (path: string) => Promise<void>;readonly #pendingCommandOwners = new Map<ProviderProcessSupervisor, string>();+ /** A version probe whose cleanup is unresolved is retried on close. */+ readonly #retainProbeOwner = (owner: ProviderProcessSupervisor): void => {+ this.#pendingCommandOwners.set(owner, "apktool version probe");+ };readonly #shutdown = new AbortController();readonly #active = new Set<Promise<void>>();#closing = false;@@ -378,7 +399,13 @@ export class ApktoolProvider {process.platform,signal,);- await inspectApktoolClient(selection, signal, environment);+ await inspectApktoolClient(+ selection,+ signal,+ environment,+ process.platform,+ this.#retainProbeOwner,+ );throwIfCancelled("inspect_apktool_client", signal);return {status: "available",@@ -390,8 +417,15 @@ export class ApktoolProvider {},};} catch (cause) {- if (cause instanceof ApktoolConfigurationFailure)+ if (cause instanceof ApktoolConfigurationFailure) {+ if (cause.cleanup !== undefined)+ throw probeCleanupFailure(+ "inspect_apktool_client",+ cause,+ cause.cleanup,+ );return apktoolConfigurationAvailability(cause);+ }throw cause;}}@@ -437,7 +471,13 @@ export class ApktoolProvider {process.platform,signal,);- identity = await inspectApktoolClient(selection, signal, environment);+ identity = await inspectApktoolClient(+ selection,+ signal,+ environment,+ process.platform,+ this.#retainProbeOwner,+ );} catch (cause) {if (cause instanceof AnalysisCancelledError ||@@ -445,6 +485,10 @@ export class ApktoolProvider {)return err(cause);if (cause instanceof ApktoolConfigurationFailure) {+ if (cause.cleanup !== undefined)+ return err(+ probeCleanupFailure(request.operation, cause, cause.cleanup),+ );return err(providerConfigurationRefusal(APKTOOL_PROVIDER_IDENTITY.id,@@ -497,12 +541,14 @@ export class ApktoolProvider {async #drainAndClean(): Promise<void> {await Promise.all(this.#active);const failures: unknown[] = [];+ const retained: string[] = [];for (const [owner, root] of this.#pendingCommandOwners) {try {const stopped = await owner.stop();if (stopped.status === "incomplete") throw new Error(stopped.reason);this.#pendingCommandOwners.delete(owner);} catch (cause) {+ retained.push(root);failures.push(new Error(`apktool process cleanup remains incomplete for ${root}: ${cause instanceof Error ? cause.message : String(cause)}`,@@ -511,8 +557,23 @@ export class ApktoolProvider {);}}+ // Ownership stays retained for a later close; report it as typed cleanup.if (failures.length > 0)- throw new AggregateError(failures, "Apktool process cleanup failed");+ throw new ProviderCleanupError(+ APKTOOL_PROVIDER_IDENTITY.id,+ retained,+ {+ reason: failures+ .map((failure) =>+ failure instanceof Error ? failure.message : String(failure),+ )+ .join("; "),+ },+ {+ operation: "close",+ cause: new AggregateError(failures, "Apktool process cleanup failed"),+ },+ );for (const path of this.workspaces) {try {await this.removeWorkspace(path);diff --git a/src/apktool/ApktoolVersionProbe.lifecycle.test.ts b/src/apktool/ApktoolVersionProbe.lifecycle.test.tsnew file mode 100644index 000000000..6abe19649--- /dev/null+++ b/src/apktool/ApktoolVersionProbe.lifecycle.test.ts@@ -0,0 +1,107 @@+import { execFile } from "node:child_process";+import { chmod, readFile, writeFile } from "node:fs/promises";+import { join } from "node:path";+import { setTimeout as delay } from "node:timers/promises";+import { promisify } from "node:util";+import { expect, it } from "vitest";++import { AnalysisCancelledError } from "../domain/analysisErrorCore.js";+import { createTestTempDirectory } from "../../tests/fixtures/temporaryDirectory.js";+import { inspectApktoolClient } from "./ApktoolConfiguration.js";++/**+ * Only an exited process counts as stopped: it is gone, a zombie waiting to+ * be reaped, or absent from `ps`. A failed inspection is not proof of exit.+ */+const alive = async (pid: number): Promise<boolean> => {+ try {+ process.kill(pid, 0);+ } catch (cause: unknown) {+ if ((cause as NodeJS.ErrnoException).code === "ESRCH") return false;+ return true;+ }+ return promisify(execFile)("ps", ["-o", "stat=", "-p", `${pid}`]).then(+ ({ stdout }) => {+ const state = stdout.trim();+ return state !== "" && !state.startsWith("Z");+ },+ // `ps` exits 1 when no process matches; any other failure is unknown.+ (cause: unknown) => (cause as { code?: unknown }).code !== 1,+ );+};++const waitFor = async (check: () => Promise<boolean>, ms: number) => {+ const deadline = Date.now() + ms;+ while (Date.now() < deadline) {+ if (await check()) return true;+ await delay(25);+ }+ return false;+};++const readPids = async (path: string): Promise<readonly number[]> =>+ readFile(path, "utf8")+ .then((text) => JSON.parse(text) as number[])+ .catch(() => []);++// The interpreter is the test runtime itself, so its environment (and the+// ownership run token) is readable on both Linux and macOS.+it.skipIf(process.platform === "win32")(+ "stops an interpreter launcher and its pipe-holding child when the probe is cancelled",+ async () => {+ const root = await createTestTempDirectory("rea-apktool-probe-");+ const pids = join(root, "pids.json");+ const launcher = join(root, "apktool");+ await writeFile(+ launcher,+ [+ `#!${process.execPath}`,+ 'const { spawn } = require("node:child_process");',+ 'const child = spawn(process.execPath, ["-e", "setInterval(() => {}, 1000)"], { stdio: "inherit" });',+ `require("node:fs").writeFileSync(${JSON.stringify(pids)}, JSON.stringify([process.pid, child.pid]));`,+ "setInterval(() => {}, 1000);",+ ].join("\n"),+ );+ await chmod(launcher, 0o755);+ const controller = new AbortController();+ const started = performance.now();+ const probe = inspectApktoolClient(+ { command: launcher, commandSource: "environment" },+ controller.signal,+ process.env,+ ).catch((cause: unknown) => cause);+ try {+ expect(+ await waitFor(async () => (await readPids(pids)).length === 2, 10_000),+ ).toBe(true);+ const launched = await readPids(pids);+ controller.abort();+ // A regressed probe never settles; bound the wait so cleanup still runs.+ const outcome = await Promise.race([+ probe,+ delay(15_000).then(() => "still pending"),+ ]);+ expect(outcome).toBeInstanceOf(AnalysisCancelledError);+ expect(performance.now() - started).toBeLessThan(15_000);+ expect(+ await waitFor(+ async () =>+ (await Promise.all(launched.map(alive))).every((live) => !live),+ 5_000,+ ),+ ).toBe(true);+ } finally {+ // A failed assertion must not leave the fixture's processes running.+ controller.abort();+ await Promise.race([probe, delay(10_000)]);+ for (const pid of await readPids(pids))+ if (await alive(pid))+ try {+ process.kill(pid, "SIGKILL");+ } catch {+ // Already exited between the check and the signal.+ }+ }+ },+ 30_000,+);diff --git a/src/apktool/ApktoolVersionProbe.test.ts b/src/apktool/ApktoolVersionProbe.test.tsnew file mode 100644index 000000000..616157ba6--- /dev/null+++ b/src/apktool/ApktoolVersionProbe.test.ts@@ -0,0 +1,147 @@+import { afterEach, expect, it, vi } from "vitest";++import {+ AnalysisCancelledError,+ AnalysisTimeoutError,+} from "../domain/analysisErrorCore.js";+import { projectAnalysisError } from "../domain/analysisErrorProjection.js";+import {+ OwnedCommandFailure,+ runOwnedCommand,+} from "../process/OwnedCommand.js";+import type { ProviderProcessSupervisor } from "../process/ProviderProcess.js";+import { ProviderCleanupError } from "../domain/providerCleanupError.js";+import {+ ApktoolConfigurationFailure,+ inspectApktoolClient,+} from "./ApktoolConfiguration.js";+import { ApktoolProvider } from "./ApktoolProvider.js";++vi.mock("../process/OwnedCommand.js", async (importOriginal) => ({+ ...(await importOriginal<typeof import("../process/OwnedCommand.js")>()),+ runOwnedCommand: vi.fn(),+}));+afterEach(() => vi.mocked(runOwnedCommand).mockReset());++const selection = {+ command: "/opt/apktool/apktool",+ commandSource: "environment",+} as const;+const owner = {} as ProviderProcessSupervisor;++it("runs the version probe as an owned interpreter-script process group", async () => {+ vi.mocked(runOwnedCommand).mockResolvedValueOnce({+ stdout: { text: "2.9.3\n" },+ stderr: { text: "" },+ } as Awaited<ReturnType<typeof runOwnedCommand>>);+ await expect(+ inspectApktoolClient(selection, undefined, {}, "linux"),+ ).resolves.toEqual({ version: "2.9.3" });+ expect(vi.mocked(runOwnedCommand).mock.lastCall?.[0]).toMatchObject({+ command: "/opt/apktool/apktool",+ arguments: ["--version"],+ expectedCommand: null,+ });+ expect(vi.mocked(runOwnedCommand).mock.lastCall?.[1]).toEqual({+ timeoutMs: 20_000,+ diagnosticBytes: 64 * 1024,+ });+});++it("reports an expired version probe as a typed timeout", async () => {+ vi.mocked(runOwnedCommand).mockRejectedValueOnce(+ new OwnedCommandFailure("timeout", "Command deadline elapsed."),+ );+ const failure = await inspectApktoolClient(+ selection,+ undefined,+ {},+ "linux",+ ).catch((cause: unknown) => cause);+ expect(failure).toBeInstanceOf(AnalysisTimeoutError);+ expect(projectAnalysisError(failure as AnalysisTimeoutError)).toMatchObject({+ code: "provider_timeout",+ category: "timeout",+ details: { timeout_ms: 20_000 },+ });+});++it("retains an unresolved probe cleanup owner and reports the cleanup", async () => {+ vi.mocked(runOwnedCommand).mockRejectedValueOnce(+ new OwnedCommandFailure(+ "timeout",+ "Command deadline elapsed.",+ null,+ "cleanup incomplete",+ undefined,+ ["pid:1"],+ owner,+ ),+ );+ const retained: ProviderProcessSupervisor[] = [];+ const failure = await inspectApktoolClient(+ selection,+ undefined,+ {},+ "linux",+ (cleanupOwner) => retained.push(cleanupOwner),+ ).catch((cause: unknown) => cause);+ expect(failure).toBeInstanceOf(AnalysisTimeoutError);+ expect(projectAnalysisError(failure as AnalysisTimeoutError)).toMatchObject({+ code: "cleanup_incomplete",+ });+ expect(retained).toEqual([owner]);+});++it("reports a cancelled version probe as cancelled", async () => {+ vi.mocked(runOwnedCommand).mockRejectedValueOnce(+ new OwnedCommandFailure("cancelled", "Command cancelled."),+ );+ await expect(+ inspectApktoolClient(selection, undefined, {}, "linux"),+ ).rejects.toBeInstanceOf(AnalysisCancelledError);+});++it("keeps unresolved probe cleanup typed when the launcher overflows its output", async () => {+ const overflow = () =>+ new OwnedCommandFailure(+ "output-limit",+ "Command diagnostic output exceeded its complete-output budget.",+ null,+ "process group still running",+ undefined,+ ["pid:7", "process-group:7"],+ owner,+ );+ vi.mocked(runOwnedCommand).mockRejectedValueOnce(overflow());+ const failure = await inspectApktoolClient(+ selection,+ undefined,+ {},+ "linux",+ ).catch((cause: unknown) => cause);+ expect(failure).toBeInstanceOf(ApktoolConfigurationFailure);+ expect((failure as ApktoolConfigurationFailure).cleanup).toEqual({+ reason: "process group still running",+ resources: ["pid:7", "process-group:7"],+ });++ // Both provider entry points report the unresolved processes as cleanup.+ const provider = new ApktoolProvider({+ environment: { REA_APKTOOL_COMMAND: "/opt/apktool/apktool" },+ });+ vi.mocked(runOwnedCommand).mockRejectedValueOnce(overflow());+ await expect(provider.inspectAvailability()).rejects.toBeInstanceOf(+ ProviderCleanupError,+ );+ vi.mocked(runOwnedCommand).mockRejectedValueOnce(overflow());+ const result = await provider.execute({+ operation: "decode_android_resources",+ input: {},+ } as never);+ expect(result.ok ? null : result.error).toBeInstanceOf(ProviderCleanupError);+ expect(result.ok ? null : projectAnalysisError(result.error)).toMatchObject({+ code: "cleanup_incomplete",+ details: { resources: ["pid:7", "process-group:7"] },+ });+});diff --git a/tests/process-global/apktoolEnvironment.test.ts b/tests/process-global/apktoolEnvironment.test.tsindex 90f4d483c..876b05aa5 100644--- a/tests/process-global/apktoolEnvironment.test.ts+++ b/tests/process-global/apktoolEnvironment.test.ts@@ -1,18 +1,26 @@import { afterEach, expect, it, onTestFinished, vi } from "vitest";import { ApktoolProvider } from "../../src/apktool/ApktoolProvider.js";-import { execFileOutput } from "../../src/process/ExecFileOutput.js";+import { runOwnedCommand } from "../../src/process/OwnedCommand.js";+import type { ProviderProcessSnapshot } from "../../src/process/ProviderProcess.js";-vi.mock("../../src/process/ExecFileOutput.js", async (importOriginal) => ({+vi.mock("../../src/process/OwnedCommand.js", async (importOriginal) => ({...(await importOriginal<- typeof import("../../src/process/ExecFileOutput.js")+ typeof import("../../src/process/OwnedCommand.js")>()),- execFileOutput: vi.fn(async () => ({ stdout: "2.7.0-stub\n", stderr: "" })),+ runOwnedCommand: vi.fn(+ async () =>+ ({+ stdout: { text: "2.7.0-stub\n" },+ stderr: { text: "" },+ }) as unknown as ProviderProcessSnapshot,+ ),}));afterEach(() => {vi.unstubAllEnvs();- vi.mocked(execFileOutput).mockClear();+ vi.mocked(runOwnedCommand).mockClear();});+const lastSpawn = () => vi.mocked(runOwnedCommand).mock.lastCall?.[0];it.skipIf(process.platform === "win32")("keeps an explicit environment's PATH choice and samples ambient inheritance per operation",@@ -24,16 +32,15 @@ it.skipIf(process.platform === "win32")(vi.stubEnv("REA_APKTOOL_COMMAND", "/ambient/changed-apktool");vi.stubEnv("REA_APKTOOL_TEST_INHERITED_VALUE", "changed");await provider.inspectAvailability();- expect(vi.mocked(execFileOutput).mock.lastCall?.[0]).toBe("apktool");- expect(vi.mocked(execFileOutput).mock.lastCall?.[2]?.env).toMatchObject({+ expect(lastSpawn()?.command).toBe("apktool");+ expect(lastSpawn()?.hostEnvironment).toMatchObject({REA_APKTOOL_COMMAND: undefined,REA_APKTOOL_TEST_INHERITED_VALUE: "changed",});vi.stubEnv("REA_APKTOOL_TEST_INHERITED_VALUE", undefined);await provider.inspectAvailability();expect(- vi.mocked(execFileOutput).mock.lastCall?.[2]?.env- ?.REA_APKTOOL_TEST_INHERITED_VALUE,+ lastSpawn()?.hostEnvironment?.REA_APKTOOL_TEST_INHERITED_VALUE,).toBeUndefined();},);@@ -46,8 +53,6 @@ it.skipIf(process.platform === "win32")(onTestFinished(() => provider.close());vi.stubEnv("REA_APKTOOL_COMMAND", "/ambient/later-apktool");await provider.inspectAvailability();- expect(vi.mocked(execFileOutput).mock.lastCall?.[0]).toBe(- "/ambient/later-apktool",- );+ expect(lastSpawn()?.command).toBe("/ambient/later-apktool");},);