From d2d2cfe8dbb39750f3a12ce402304e8ab365e8cc Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Sat, 14 Nov 2026 20:49:27 -0500 Subject: [PATCH] fix(apktool): advise the JVM limit that governs the reported memory area --- src/apktool/ApktoolProvider.ts | 27 ++++++++++++++++--- .../apktoolRuntimeExhaustion.test.ts | 18 +++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/apktool/ApktoolProvider.ts b/src/apktool/ApktoolProvider.ts index 553046c4e04..5314530fd37 100644 --- a/src/apktool/ApktoolProvider.ts +++ b/src/apktool/ApktoolProvider.ts @@ -62,6 +62,7 @@ import { OwnedCommandFailure, runOwnedCommand, } from "../process/OwnedCommand.js"; +import type { RuntimeExhaustionArea } from "../process/RuntimeExhaustion.js"; import { snapshotEnvironment } from "../process/snapshotEnvironment.js"; import { WindowsBatchCommandError } from "../process/WindowsBatchCommand.js"; import { ownedProcessPrerequisiteAvailability } from "../application/ownedProcessAvailability.js"; @@ -82,6 +83,27 @@ import { const DECODE_MAX_OUTPUT_BYTES = 4 * 1024 * 1024; const MANIFEST_MAX_BYTES = 512 * 1024; const MAX_DECODED_ENTRIES_VISITED = 100_000; +/** + * Recovery for the JVM memory area Apktool's decoder reported exhausted. REA + * does not set the decoder JVM's limits, so each names the launcher setting + * that governs that area. + */ +const APKTOOL_EXHAUSTION_REMEDIATION: Readonly< + Record +> = { + heap: "REA does not set Apktool's Java heap. Raise the maximum heap (-Xmx) of the selected Apktool launcher's JVM, or decode a smaller APK.", + "gc-overhead": + "REA does not set Apktool's Java heap. The decoder JVM spent nearly all its time collecting garbage; raise the maximum heap (-Xmx) of the selected Apktool launcher's JVM, or decode a smaller APK.", + metaspace: + "REA does not set Apktool's class metadata limit. Raise -XX:MaxMetaspaceSize of the selected Apktool launcher's JVM; a larger heap (-Xmx) does not raise it.", + "array-size": + "The decoder requested an array larger than the JVM allows; a larger heap (-Xmx) does not lift this limit. Decode a smaller APK.", + "native-thread": + "The decoder JVM could not create a native thread. Check host thread/process limits and available native memory for the selected Apktool launcher; a larger heap (-Xmx) does not help.", + unknown: + "REA does not set the memory limits of Apktool's JVM. Review the reported out-of-memory line and the selected Apktool launcher's JVM memory settings, or decode a smaller APK.", +}; + const STDERR_EXCERPT_BYTES = 512; const apktoolDecodeTimeoutGuidance = ( @@ -219,12 +241,11 @@ const apktoolCommandFailure = ( "capture", ), }, - runtimeExhaustion: () => ({ + runtimeExhaustion: ({ area }) => ({ limits: null, recovery: { retryable: false, - remediationAction: - "REA does not set Apktool's Java heap. Raise the maximum heap (-Xmx) of the selected Apktool launcher's JVM, or decode a smaller APK. Repeating the unchanged request is expected to fail the same way.", + remediationAction: `${APKTOOL_EXHAUSTION_REMEDIATION[area ?? "unknown"]} Repeating the unchanged request is expected to fail the same way.`, }, }), dialect: (exit, details) => { diff --git a/tests/boundary/providers/apktoolRuntimeExhaustion.test.ts b/tests/boundary/providers/apktoolRuntimeExhaustion.test.ts index 20d9fe29873..a7e69998191 100644 --- a/tests/boundary/providers/apktoolRuntimeExhaustion.test.ts +++ b/tests/boundary/providers/apktoolRuntimeExhaustion.test.ts @@ -49,6 +49,24 @@ it("reports the decoder JVM's heap exhaustion as memory, not a target refusal", expect(projected.remediation.action).toContain("-Xmx"); }); +it.each([ + ["Metaspace", "MaxMetaspaceSize"], + ["Requested array size exceeds VM limit", "smaller APK"], +])( + "advises the limit that governs a %s report rather than the heap", + async (detail, advice) => { + const projected = await failingDecode( + `printf 'Exception in thread "main" java.lang.OutOfMemoryError: ${detail}\\n' >&2`, + ); + expect(projected).toMatchObject({ + code: "resource_constraint", + details: { resource: "memory" }, + }); + expect(projected.remediation.action).toContain(advice); + expect(projected.remediation.action).not.toMatch(/Raise the maximum heap/u); + }, +); + it("observes the heap report while output streams, even past the capture allowance", async () => { // A stack trace longer than the 4 MiB capture allowance follows the report. const projected = await failingDecode(