From a752cf99c295e0cc79ae2ba5bd56150c62ea8075 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Thu, 19 Nov 2026 07:10:40 -0500 Subject: [PATCH] refactor(android): carry mapping limits from the parser (#12302) --- src/android/AndroidMappingFile.ts | 12 +++-- src/domain/android/proguardMapping.test.ts | 45 ++++++++----------- src/domain/android/proguardMapping.ts | 20 ++++++--- .../android/androidMappingFile.test.ts | 16 ++++++- 4 files changed, 55 insertions(+), 38 deletions(-) diff --git a/src/android/AndroidMappingFile.ts b/src/android/AndroidMappingFile.ts index 18911dd8a0..753da72373 100644 --- a/src/android/AndroidMappingFile.ts +++ b/src/android/AndroidMappingFile.ts @@ -196,14 +196,17 @@ export const readAndroidMapping = async ( } const mapping = parseProguardMapping(text, ANDROID_MAPPING_LIMITS); if (!mapping.ok) { - if (mapping.error.kind === "capacity") + if (mapping.error.kind === "capacity") { + const { cause, line } = mapping.error; + const { detail, limit } = cause.failure; return err( new AnalysisResourceConstraintError( operation, - mapping.error.resource === "index" ? "memory" : "transport", - `The mapping exceeds its ${mapping.error.resource} admission budget.`, - { limit_bytes: mapping.error.limit_bytes, line: mapping.error.line }, + limit.name === "projection" ? "transport" : "memory", + detail, + { limit_bytes: limit.maximum, line }, { + cause, retryable: false, diagnostics: { mapping_path: path, mapping_sha256: file.sha256 }, remediationAction: @@ -211,6 +214,7 @@ export const readAndroidMapping = async ( }, ), ); + } return malformed(`Mapping file has no R8/ProGuard class records: ${path}`); } return ok({ file, mapping: mapping.value }); diff --git a/src/domain/android/proguardMapping.test.ts b/src/domain/android/proguardMapping.test.ts index c249232712..01f2df4ac9 100644 --- a/src/domain/android/proguardMapping.test.ts +++ b/src/domain/android/proguardMapping.test.ts @@ -707,6 +707,21 @@ it.each([ }); }); +const capacityRefusal = (name: string, maximum: number, line: number) => ({ + ok: false, + error: { + kind: "capacity", + line, + cause: { + failure: { + kind: "limit", + format: "proguard-mapping", + limit: { name, maximum }, + }, + }, + }, +}); + it("refuses class-index growth at its source line with an explicit retained budget", () => { const parsed = parseProguardMapping( Array.from({ length: 8 }, (_, index) => `Owner${index} -> a${index}:`).join( @@ -714,10 +729,7 @@ it("refuses class-index growth at its source line with an explicit retained budg ), { indexBytes: 4096 }, ); - expect(parsed).toEqual({ - ok: false, - error: { kind: "capacity", resource: "index", limit_bytes: 4096, line: 4 }, - }); + expect(parsed).toMatchObject(capacityRefusal("index", 4096, 4)); }); it("admits a method's parameter representation before allocating its split array", () => { @@ -733,10 +745,7 @@ it("admits a method's parameter representation before allocating its split array } finally { split.mockRestore(); } - expect(parsed).toMatchObject({ - ok: false, - error: { kind: "capacity", resource: "index", line: 2 }, - }); + expect(parsed).toMatchObject(capacityRefusal("index", 4096, 2)); expect(splits).toBe(0); }); @@ -751,15 +760,7 @@ it("refuses oversized JSON metadata before JSON.parse materializes its tree", () } finally { json.mockRestore(); } - expect(parsed).toEqual({ - ok: false, - error: { - kind: "capacity", - resource: "projection", - limit_bytes: 1024, - line: 2, - }, - }); + expect(parsed).toMatchObject(capacityRefusal("projection", 1024, 2)); expect(calls).toBe(0); }); @@ -885,15 +886,7 @@ it("bounds repeated shared class names in expanded per-class mapping claims", () projectionBytes: 1024, }, ); - expect(parsed).toEqual({ - ok: false, - error: { - kind: "capacity", - resource: "projection", - limit_bytes: 1024, - line: 3, - }, - }); + expect(parsed).toMatchObject(capacityRefusal("projection", 1024, 3)); }); it("does not cumulatively charge deduplicated line-range splits", () => { diff --git a/src/domain/android/proguardMapping.ts b/src/domain/android/proguardMapping.ts index 09ff73f13f..46c1f6371b 100644 --- a/src/domain/android/proguardMapping.ts +++ b/src/domain/android/proguardMapping.ts @@ -2,6 +2,7 @@ import { err, ok, type Result } from "../result.js"; import { dexDescriptorSchema, dexTypeSchema } from "./dexTypes.js"; import { parseJsonWithDuplicatePolicy } from "../jsonDuplicateMember.js"; import { jsonStringByteLength } from "../jsonStringByteLength.js"; +import { decodeLimit, type ParseFailureError } from "../parseFailure.js"; /** Retained representation and per-residual-class claim budgets, not file-size limits. */ export const PROGUARD_MAPPING_LIMITS = Object.freeze({ @@ -17,8 +18,7 @@ export type ProguardMappingFailure = | { readonly kind: "no-class-records" } | { readonly kind: "capacity"; - readonly resource: "index" | "projection"; - readonly limit_bytes: number; + readonly cause: ParseFailureError<"limit">; readonly line: number; }; @@ -234,12 +234,18 @@ const capacity = ( ): false => { state.failure ??= { kind: "capacity", - resource, line, - limit_bytes: - resource === "index" - ? state.limits.indexBytes - : state.limits.projectionBytes, + cause: decodeLimit( + "proguard-mapping", + `The mapping exceeds its ${resource} admission budget.`, + { + name: resource, + maximum: + resource === "index" + ? state.limits.indexBytes + : state.limits.projectionBytes, + }, + ), }; return false; }; diff --git a/tests/boundary/android/androidMappingFile.test.ts b/tests/boundary/android/androidMappingFile.test.ts index 82748c9406..3e4acaae63 100644 --- a/tests/boundary/android/androidMappingFile.test.ts +++ b/tests/boundary/android/androidMappingFile.test.ts @@ -199,7 +199,17 @@ it("refuses expanded shared-name claims from a small mapping with auditable nonr retryable: false, reportedLimits: { limit_bytes: ANDROID_MAPPING_LIMITS.projectionBytes, - line: expect.any(Number), + line: 8, + }, + cause: { + failure: { + kind: "limit", + format: "proguard-mapping", + limit: { + name: "projection", + maximum: ANDROID_MAPPING_LIMITS.projectionBytes, + }, + }, }, diagnostics: { mapping_path: path, mapping_sha256: digest(text) }, cleanupIncomplete: false, @@ -209,6 +219,10 @@ it("refuses expanded shared-name claims from a small mapping with auditable nonr retryable: false, details: { resource: "transport", + reported_limits: { + limit_bytes: ANDROID_MAPPING_LIMITS.projectionBytes, + line: 8, + }, diagnostics: { mapping_path: path, mapping_sha256: digest(text) }, }, });