fix(apktool): advise the JVM limit that governs the reported memory area

This commit is contained in:
morluto committed 2026-11-14 20:49:27 -05:00
1 parent 869acefeda
commit d2d2cfe8db
2 files changed
+42 -3

No files matched your search

+24 -3
View File
@@ -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<RuntimeExhaustionArea | "unknown", string>
> = {
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) => {
@@ -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(