diff --git a/docs/guides/native/native-investigation.md b/docs/guides/native/native-investigation.md index 0dd3163850..a08c407ac1 100644 --- a/docs/guides/native/native-investigation.md +++ b/docs/guides/native/native-investigation.md @@ -720,12 +720,12 @@ bound. The XML estimate charges every element and every text, comment or CDATA node of the decoder's DOM at a measured upper bound, so a plist made of many small elements reaches it at a few MiB of source. JSON decode memory has not been measured and has no equivalent decode estimate. A refused input reports non-retryable `resource_constraint` with -`reported_limits.maximum_input_bytes` or `maximum_decoded_bytes` as applicable. +`reported_limits.maximum_input_bytes` for the input ceiling or `reported_limits: { boundary: "plist-decode-bytes", maximum: 67108864 }` for the decode budget (with `details.refusal.unit: "bytes"`). Ordinary direct-source decoding has a ceiling of 997 source nesting levels, derived from the existing 1000-level JSON result capacity. Three levels are reserved for typed scalar fields, the report value and its Evidence envelope. Deeper input reports `resource_constraint` with -`reported_limits.maximum_nesting_depth: 997` and names the fixed nesting allowance. +`reported_limits: { boundary: "plist-nesting-depth", maximum: 997 }` and `details.refusal` naming the fixed nesting allowance and its `unit` (`"levels"`). XML admission also requires native validation, which can refuse input below this decoding ceiling. Compatibility conversions can likewise encounter an earlier native utility refusal; both retain native diff --git a/docs/il2cpp-metadata.md b/docs/il2cpp-metadata.md index 091e01b376..1f241da6ea 100644 --- a/docs/il2cpp-metadata.md +++ b/docs/il2cpp-metadata.md @@ -459,7 +459,7 @@ The builds checked are listed after the module search rules above. | `generic-method-registration-not-found` | input error | No CodeRegistration layout bounds every genericMethodTable row, or the generic tables are unreadable. | | `ambiguous-generic-method-registration` | input error | More than one CodeRegistration layout bounds every genericMethodTable row. | -Format failures name `binary_path` and slice-selection failures name `binary_architecture`; capacity refusals remain typed resource errors whose reported limits name the exceeded budget (`boundary`, the capacity reason) and its maximum in the unit the budget counts (`maximum_entries`, `maximum_bytes` or `maximum_frames`). Failed linking preserves the metadata source identity, validated counts, selected binary path/architecture and its SHA-256 when observed, plus prior producer observations. It never publishes an incomplete native table as complete. The binary input is bounded to 1 GiB. The virtual-pointer index has the same 1 GiB estimated representation allowance, reserving 2 KiB per distinct virtual destination before insertion for retained Maps and finish-time lookup storage. Repeated declarations of one destination share that reservation. This conservative allocation accounting is separate from actual process RSS and the report allowance; exhaustion returns a typed `native-index-capacity` refusal before another destination is retained. +Format failures name `binary_path` and slice-selection failures name `binary_architecture`; capacity refusals remain typed resource errors whose `reported_limits` name the exceeded budget (`boundary`, the capacity reason) and its `maximum`, with `details.refusal.unit` naming the unit the budget counts (`"entries"`, `"bytes"` or `"frames"`). Failed linking preserves the metadata source identity, validated counts, selected binary path/architecture and its SHA-256 when observed, plus prior producer observations. It never publishes an incomplete native table as complete. The binary input is bounded to 1 GiB. The virtual-pointer index has the same 1 GiB estimated representation allowance, reserving 2 KiB per distinct virtual destination before insertion for retained Maps and finish-time lookup storage. Repeated declarations of one destination share that reservation. This conservative allocation accounting is separate from actual process RSS and the report allowance; exhaustion returns a typed `native-index-capacity` refusal before another destination is retained. Scans yield cooperatively in bounded batches. Name-address retention and reverse-trie construction are each limited to 65,536 entries/nodes and fallback module-array candidates to 64 (`native-index-capacity`); exceeding capacity does not prove absence. Rendered type text has a 64 MiB UTF-8 cache-workspace guard, separate from the 64 MiB encoded-report admission, and 64 nested rendering frames (`native-type-text-capacity` / `type-nesting-capacity`). Genuine cycles are malformed; unsupported type kinds are explicit coverage failures. Native row/text admission runs before allocation or retention. Unused generic parameter names are read on demand from the same stable metadata snapshot. There is no cross-inspection file cache; validated immutable Evidence is reused for session views. diff --git a/scripts/verify/baselines/resource-constraint-constructor.json b/scripts/verify/baselines/resource-constraint-constructor.json index e8c9c1e88c..666ba26dca 100644 --- a/scripts/verify/baselines/resource-constraint-constructor.json +++ b/scripts/verify/baselines/resource-constraint-constructor.json @@ -83,13 +83,13 @@ "src/ghidra/GhidraFileArtifacts.ts": 1, "src/ghidra/GhidraInstallationProperties.ts": 1, "src/ghidra/GhidraProviderClient.ts": 4, - "src/go/GoBinaryProvider.ts": 3, + "src/go/GoBinaryProvider.ts": 2, "src/gpu/GpuCodeObjectsProvider.ts": 3, "src/hopper/HopperCallerReconciliation.ts": 1, "src/ida/IdaMcpConnection.ts": 1, "src/il2cpp/Il2cppGhidraSeedWriter.ts": 1, "src/il2cpp/Il2cppGhidraSeeds.ts": 1, - "src/il2cpp/Il2cppMetadataProvider.ts": 4, + "src/il2cpp/Il2cppMetadataProvider.ts": 3, "src/javascript/analysis/JavaScriptAnalysisTransfer.ts": 1, "src/javascript/analysis/JavaScriptAnalysisWorker.ts": 10, "src/javascript/analysis/JavaScriptAnalysisWorkerServer.ts": 1, @@ -111,16 +111,15 @@ "src/native/NativePlistSource.ts": 2, "src/native/SignatureTargetBinding.ts": 1, "src/native/apple/AppleIpsProvider.ts": 2, - "src/native/elf/ElfNotesProvider.ts": 2, - "src/native/elf/ElfUnwindRangesProvider.ts": 3, + "src/native/elf/ElfNotesProvider.ts": 1, + "src/native/elf/ElfUnwindRangesProvider.ts": 2, "src/native/elf/KernelBuildConfigProvider.ts": 1, - "src/native/elf/KernelModuleProvider.ts": 2, - "src/native/exports/NativeExportsProvider.ts": 2, - "src/native/hardening/BinaryHardeningProvider.ts": 2, - "src/native/initializers/NativeInitializersProvider.ts": 3, + "src/native/elf/KernelModuleProvider.ts": 1, + "src/native/exports/NativeExportsProvider.ts": 1, + "src/native/hardening/BinaryHardeningProvider.ts": 1, + "src/native/initializers/NativeInitializersProvider.ts": 1, "src/native/parsers/lipo.ts": 1, - "src/native/parsers/plist.ts": 2, - "src/native/pe/PeBinaryFailure.ts": 2, + "src/native/pe/PeBinaryFailure.ts": 1, "src/native/pe/PeCertificatesProvider.ts": 2, "src/native/pe/PeResourcesProvider.ts": 1, "src/native/pwntools/PwntoolsDecoder.ts": 1, diff --git a/src/domain/parseFailureAnalysisError.test.ts b/src/domain/parseFailureAnalysisError.test.ts index 8c0bc2da1f..4d1b4d6702 100644 --- a/src/domain/parseFailureAnalysisError.test.ts +++ b/src/domain/parseFailureAnalysisError.test.ts @@ -2,14 +2,21 @@ import { expect, it } from "vitest"; import { AnalysisInputError, + AnalysisResourceConstraintError, AnalysisUnsupportedTargetError, } from "./analysisErrorCore.js"; +import { fixedAllowanceOptions } from "./fixedAllowanceRemediation.js"; import { + decodeLimit, malformedInput, ParseFailureError, unsupportedInput, } from "./parseFailure.js"; -import { parseFailureAnalysisError } from "./parseFailureAnalysisError.js"; +import { + isParseLimit, + parseFailureAnalysisError, + parseLimitConstraint, +} from "./parseFailureAnalysisError.js"; const project = (cause: unknown) => parseFailureAnalysisError("inspect_example", "/tmp/sample.bin", cause); @@ -90,3 +97,74 @@ it("leaves a limit and any other cause to the producer", () => { undefined, ); }); + +it("recognizes only a parse limit of the named format", () => { + const limit = decodeLimit("elf", "section table exceeds budget", { + name: "structure", + maximum: 16, + }); + expect(isParseLimit(limit, "elf")).toBe(true); + expect(isParseLimit(limit, "pe")).toBe(false); + expect(isParseLimit(malformedInput("elf", "bad header"), "elf")).toBe(false); + expect(isParseLimit(new Error("other"), "elf")).toBe(false); +}); + +it("builds a structured limit refusal with optional path context and required size", () => { + const plain = decodeLimit("sigrok-decoder-catalog", "line budget exceeded", { + name: "line_budget", + maximum: 65536, + }); + const plainError = parseLimitConstraint( + "inspect_signal_decoders", + "memory", + plain, + fixedAllowanceOptions("The decoder line allowance", "capture"), + ); + expect(plainError).toBeInstanceOf(AnalysisResourceConstraintError); + expect(plainError).toMatchObject({ + operation: "inspect_signal_decoders", + resource: "memory", + reason: "line budget exceeded", + reportedLimits: { boundary: "line_budget", maximum: 65536 }, + cause: plain, + }); + + const numericRequired = decodeLimit("elf", "section names exceed budget", { + name: "max_file_bytes", + maximum: 1024, + required: 2048, + }); + expect( + parseLimitConstraint("inspect_elf_notes", "memory", numericRequired, { + path: "/tmp/sample.elf", + retryable: false, + remediationAction: "Raise max_file_bytes above 1024.", + }), + ).toMatchObject({ + reason: "Selected artifact /tmp/sample.elf: section names exceed budget", + reportedLimits: { + boundary: "max_file_bytes", + maximum: 1024, + required: 2048, + }, + retryable: false, + }); + + const bigintRequired = decodeLimit("elf", "section names exceed safe range", { + name: "max_file_bytes", + maximum: 1024, + required: 9007199254740993n, + }); + expect( + parseLimitConstraint("inspect_elf_notes", "memory", bigintRequired, { + retryable: false, + }), + ).toMatchObject({ + reason: "section names exceed safe range", + reportedLimits: { + boundary: "max_file_bytes", + maximum: 1024, + required: "9007199254740993", + }, + }); +}); diff --git a/src/domain/parseFailureAnalysisError.ts b/src/domain/parseFailureAnalysisError.ts index 8dbc13692c..68a9b208dc 100644 --- a/src/domain/parseFailureAnalysisError.ts +++ b/src/domain/parseFailureAnalysisError.ts @@ -17,26 +17,41 @@ export const isParseLimit = ( cause.failure.kind === "limit" && cause.failure.format === format; +const reportedRequired = (required: number | bigint): number | string => + typeof required === "bigint" ? String(required) : required; + /** * The public refusal for a parse that stopped at a named limit. The reason is - * the failure's detail and the reported limit is the failure's `LimitRef` - * (`{ [name]: maximum }`), so neither is spelled again at the catch site. The - * producer still supplies the recovery, which names a budget only it knows. + * the failure's detail (prefixed with `Selected artifact :` when `path` + * names one) and `reportedLimits` comes from the failure's `LimitRef` + * (`{ boundary: name, maximum, required? }`), so neither is spelled again at + * the catch site. The producer still supplies the recovery, which names a + * budget only it knows. */ export const parseLimitConstraint = ( operation: string, resource: (typeof ANALYSIS_RESOURCE_KINDS)[number], cause: ParseFailureError<"limit">, options: Omit & - ResourceConstraintRecovery & { readonly requestSent?: boolean }, + ResourceConstraintRecovery & { + readonly path?: string | undefined; + readonly requestSent?: boolean; + }, ): AnalysisResourceConstraintError => { const { detail, limit } = cause.failure; + const { path, ...errorOptions } = options; return new AnalysisResourceConstraintError( operation, resource, - detail, - { [limit.name]: limit.maximum }, - { ...options, cause }, + path === undefined ? detail : `Selected artifact ${path}: ${detail}`, + { + boundary: limit.name, + maximum: limit.maximum, + ...(limit.required === undefined + ? {} + : { required: reportedRequired(limit.required) }), + }, + { ...errorOptions, cause }, ); }; diff --git a/src/go/GoBinaryProvider.ts b/src/go/GoBinaryProvider.ts index 6d5fe68946..5045232fa6 100644 --- a/src/go/GoBinaryProvider.ts +++ b/src/go/GoBinaryProvider.ts @@ -1,5 +1,8 @@ import type { AnalysisControlLimit } from "../domain/analysisSettings.js"; -import { parseFailureAnalysisError } from "../domain/parseFailureAnalysisError.js"; +import { + parseFailureAnalysisError, + parseLimitConstraint, +} from "../domain/parseFailureAnalysisError.js"; import { jsonValueByteLength } from "../domain/jsonValueByteLength.js"; import { GO_BINARY_PROVIDER_IDENTITY } from "./GoBinaryProviderIdentity.js"; import { @@ -95,17 +98,12 @@ const failureForPrimary = ( : cause.boundary === "build-info" ? "max_build_info_bytes" : undefined; - return new AnalysisResourceConstraintError( + return parseLimitConstraint( OPERATION, cause.boundary === "search" ? "cpu" : "memory", - `Selected artifact ${path}: ${cause.message}`, + cause, { - boundary: cause.boundary, - maximum_bytes: cause.maximum_bytes, - ...(parameter === undefined ? {} : { parameter, unit: "bytes" }), - }, - { - cause, + path, ...(parameter === undefined ? fixedAllowanceOptions( "The Go structural decoding allowance", diff --git a/src/il2cpp/Il2cppMetadataProvider.ts b/src/il2cpp/Il2cppMetadataProvider.ts index 484148a724..e078fc0d0b 100644 --- a/src/il2cpp/Il2cppMetadataProvider.ts +++ b/src/il2cpp/Il2cppMetadataProvider.ts @@ -2,7 +2,12 @@ import { IL2CPP_METADATA_PROVIDER_IDENTITY } from "./Il2CppMetadataProviderIdent import { Il2cppBinaryFormatFailure, IL2CPP_BINARY_BYTES, + IL2CPP_BINARY_FORMAT, } from "./Il2cppBinaryFormat.js"; +import { + isParseLimit, + parseLimitConstraint, +} from "../domain/parseFailureAnalysisError.js"; import { Il2cppMetadataFormatFailure } from "./Il2cppMetadataFormat.js"; import { inspectIl2cppMetadataV108 } from "./Il2cppMetadataV108.js"; import type { AnalysisPartialObservation } from "../domain/analysisErrorBase.js"; @@ -82,30 +87,20 @@ const failureForPrimary = ( return cause; if ( cause instanceof Il2cppBinaryFormatFailure && - cause.failure.kind === "limit" + isParseLimit(cause, IL2CPP_BINARY_FORMAT) ) - return new AnalysisResourceConstraintError( + return parseLimitConstraint( OPERATION, cause.reason === "native-type-text-capacity" ? "memory" : "capacity", - cause.message, - // The producing site states what its maximum counts. - { - boundary: cause.reason, - ...(cause.unit === undefined - ? {} - : { [`maximum_${cause.unit}`]: cause.failure.limit.maximum }), - }, - { - cause, - ...fixedAllowanceOptions( - "The IL2CPP native-link allowance", - cause.reason === "native-type-text-capacity" ? "parser" : "count", - { - limit: cause.failure.limit.maximum, - ...(cause.unit === undefined ? {} : { unit: cause.unit }), - }, - ), - }, + cause, + fixedAllowanceOptions( + "The IL2CPP native-link allowance", + cause.reason === "native-type-text-capacity" ? "parser" : "count", + { + limit: cause.failure.limit.maximum, + ...(cause.unit === undefined ? {} : { unit: cause.unit }), + }, + ), ); if ( cause instanceof Il2cppBinaryFormatFailure || diff --git a/src/native/elf/ElfNotesProvider.test.ts b/src/native/elf/ElfNotesProvider.test.ts index 0d7a459e72..b0f72d8379 100644 --- a/src/native/elf/ElfNotesProvider.test.ts +++ b/src/native/elf/ElfNotesProvider.test.ts @@ -149,8 +149,9 @@ describe("ELF note provider failures", () => { expect(refused.error).toMatchObject({ resource: "memory", reportedLimits: { - max_file_bytes: bytes.length + 1, - minimum_required_bytes: String(minimumMaxFileBytes), + boundary: "max_file_bytes", + maximum: bytes.length + 1, + required: String(minimumMaxFileBytes), }, retryable: false, }); diff --git a/src/native/elf/ElfNotesProvider.ts b/src/native/elf/ElfNotesProvider.ts index 053cacaeb3..25de1d055d 100644 --- a/src/native/elf/ElfNotesProvider.ts +++ b/src/native/elf/ElfNotesProvider.ts @@ -1,4 +1,8 @@ -import { parseFailureAnalysisError } from "../../domain/parseFailureAnalysisError.js"; +import { + isParseLimit, + parseFailureAnalysisError, + parseLimitConstraint, +} from "../../domain/parseFailureAnalysisError.js"; import { projectOwnedArtifactIdentityFailure, projectSelectedArtifactFileFailure, @@ -97,23 +101,13 @@ const elfNotesFailurePrimary = ( ) => { const { cause, origin } = facts; const sectionNameBudget = elfNoteSectionNameBudget(cause); - if (sectionNameBudget !== undefined) - return new AnalysisResourceConstraintError( - OPERATION, - "memory", - sectionNameBudget.detail, - { - max_file_bytes: sectionNameBudget.selected, - minimum_required_bytes: sectionNameBudget.required.toString(), - }, - { - cause, - retryable: false, - remediationAction: sectionNameBudgetRemediation( - sectionNameBudget.required, - ), - }, - ); + if (isParseLimit(cause, "elf") && sectionNameBudget !== undefined) + return parseLimitConstraint(OPERATION, "memory", cause, { + retryable: false, + remediationAction: sectionNameBudgetRemediation( + sectionNameBudget.required, + ), + }); const formatFailure = parseFailureAnalysisError(OPERATION, input.path, cause); if (formatFailure !== undefined) return formatFailure; const identityFailure = projectOwnedArtifactIdentityFailure( diff --git a/src/native/elf/ElfUnwindRangesProvider.ts b/src/native/elf/ElfUnwindRangesProvider.ts index 0b7ac82929..8a474f0d11 100644 --- a/src/native/elf/ElfUnwindRangesProvider.ts +++ b/src/native/elf/ElfUnwindRangesProvider.ts @@ -1,4 +1,7 @@ -import { parseFailureAnalysisError } from "../../domain/parseFailureAnalysisError.js"; +import { + parseFailureAnalysisError, + parseLimitConstraint, +} from "../../domain/parseFailureAnalysisError.js"; import { projectOwnedArtifactIdentityFailure, projectSelectedArtifactFileFailure, @@ -114,19 +117,15 @@ const elfUnwindFailurePrimary = ( }, ); if (cause instanceof BinaryResourceFailure) - return new AnalysisResourceConstraintError( + return parseLimitConstraint( OPERATION, "memory", - cause.message, - { max_file_bytes: input.max_file_bytes }, - { - cause, - ...fixedAllowanceOptions( - "The ELF structural decoding allowance", - "parser", - binaryResourceCeiling(cause), - ), - }, + cause, + fixedAllowanceOptions( + "The ELF structural decoding allowance", + "parser", + binaryResourceCeiling(cause), + ), ); const formatFailure = parseFailureAnalysisError(OPERATION, input.path, cause); if (formatFailure !== undefined) return formatFailure; diff --git a/src/native/elf/KernelModuleProvider.ts b/src/native/elf/KernelModuleProvider.ts index 208769e7cf..7b4c51c316 100644 --- a/src/native/elf/KernelModuleProvider.ts +++ b/src/native/elf/KernelModuleProvider.ts @@ -1,4 +1,7 @@ -import { parseFailureAnalysisError } from "../../domain/parseFailureAnalysisError.js"; +import { + parseFailureAnalysisError, + parseLimitConstraint, +} from "../../domain/parseFailureAnalysisError.js"; import { projectOwnedArtifactIdentityFailure, projectSelectedArtifactFileFailure, @@ -96,19 +99,15 @@ const kernelModuleFailurePrimary = ( ) => { const { cause, origin } = facts; if (cause instanceof BinaryResourceFailure) - return new AnalysisResourceConstraintError( + return parseLimitConstraint( OPERATION, "memory", - cause.message, - { boundary: cause.boundary, maximum_bytes: cause.maximum_bytes }, - { - cause, - ...fixedAllowanceOptions( - "The ELF structural decoding allowance", - "parser", - binaryResourceCeiling(cause), - ), - }, + cause, + fixedAllowanceOptions( + "The ELF structural decoding allowance", + "parser", + binaryResourceCeiling(cause), + ), ); const formatFailure = parseFailureAnalysisError(OPERATION, input.path, cause); if (formatFailure !== undefined) return formatFailure; diff --git a/src/native/exports/NativeExportsProvider.ts b/src/native/exports/NativeExportsProvider.ts index e3121771f6..666d34c5eb 100644 --- a/src/native/exports/NativeExportsProvider.ts +++ b/src/native/exports/NativeExportsProvider.ts @@ -1,4 +1,7 @@ -import { parseFailureAnalysisError } from "../../domain/parseFailureAnalysisError.js"; +import { + parseFailureAnalysisError, + parseLimitConstraint, +} from "../../domain/parseFailureAnalysisError.js"; import { imageFileSizeRemediation, projectSelectedArtifactFileFailure, @@ -126,19 +129,13 @@ const nativeExportsFailurePrimary = ( ); if (identityFailure !== undefined) return identityFailure; if (cause instanceof BinaryResourceFailure) - return new AnalysisResourceConstraintError( - OPERATION, - "capacity", - `Selected artifact ${input.path}: ${cause.message}`, - { boundary: cause.boundary, maximum: cause.maximum_bytes }, - { + return parseLimitConstraint(OPERATION, "capacity", cause, { + path: input.path, + ...limitRecovery( + binaryResourceAllowance(cause, NATIVE_EXPORT_LIMITS), cause, - ...limitRecovery( - binaryResourceAllowance(cause, NATIVE_EXPORT_LIMITS), - cause, - ), - }, - ); + ), + }); const formatFailure = parseFailureAnalysisError(OPERATION, input.path, cause); if (formatFailure !== undefined) return formatFailure; if ( diff --git a/src/native/hardening/BinaryHardeningProvider.ts b/src/native/hardening/BinaryHardeningProvider.ts index 2bc3967de6..ac1bdb3a36 100644 --- a/src/native/hardening/BinaryHardeningProvider.ts +++ b/src/native/hardening/BinaryHardeningProvider.ts @@ -1,4 +1,7 @@ -import { parseFailureAnalysisError } from "../../domain/parseFailureAnalysisError.js"; +import { + parseFailureAnalysisError, + parseLimitConstraint, +} from "../../domain/parseFailureAnalysisError.js"; import { imageFileSizeRemediation, projectOwnedArtifactIdentityFailure, @@ -138,20 +141,14 @@ const binaryHardeningFailurePrimary = ( ); if (identityFailure !== undefined) return identityFailure; if (cause instanceof BinaryResourceFailure) - return new AnalysisResourceConstraintError( - OPERATION, - "capacity", - `Selected artifact ${input.path}: ${cause.message}`, - { boundary: cause.boundary, maximum: cause.maximum_bytes }, - { - cause, - ...fixedAllowanceOptions( - "The binary hardening structural allowance", - "parser", - binaryResourceCeiling(cause), - ), - }, - ); + return parseLimitConstraint(OPERATION, "capacity", cause, { + path: input.path, + ...fixedAllowanceOptions( + "The binary hardening structural allowance", + "parser", + binaryResourceCeiling(cause), + ), + }); const formatFailure = parseFailureAnalysisError(OPERATION, input.path, cause); if (formatFailure !== undefined) return formatFailure; if ( diff --git a/src/native/initializers/NativeInitializersProvider.ts b/src/native/initializers/NativeInitializersProvider.ts index 90becd3aaf..1aac28296c 100644 --- a/src/native/initializers/NativeInitializersProvider.ts +++ b/src/native/initializers/NativeInitializersProvider.ts @@ -1,4 +1,8 @@ -import { parseFailureAnalysisError } from "../../domain/parseFailureAnalysisError.js"; +import { + isParseLimit, + parseFailureAnalysisError, + parseLimitConstraint, +} from "../../domain/parseFailureAnalysisError.js"; import { imageFileSizeRemediation, projectSelectedArtifactFileFailure, @@ -33,7 +37,10 @@ import { BinaryResourceFailure, binaryResourceCeiling, } from "../../domain/binaryContainer.js"; -import { ElfPointerRelocationFailure } from "../../domain/native/elfPointerRelocations.js"; +import { + ELF_POINTER_RELOCATION_FORMAT, + ElfPointerRelocationFailure, +} from "../../domain/native/elfPointerRelocations.js"; import { requireSelectedNativeImage } from "../../domain/apple/machoSliceSelection.js"; import { readElfInitializers } from "./ElfInitializers.js"; import { readMachoInitializers } from "./MachoInitializers.js"; @@ -199,40 +206,28 @@ const failurePrimary = ( if (identityFailure !== undefined) return identityFailure; if ( cause instanceof ElfPointerRelocationFailure && - cause.failure.kind === "limit" + isParseLimit(cause, ELF_POINTER_RELOCATION_FORMAT) ) - return new AnalysisResourceConstraintError( - OPERATION, - "capacity", - `Selected artifact ${input.path}: ${cause.message}`, - { boundary: "structure", maximum: cause.failure.limit.maximum }, - { - cause, - ...fixedAllowanceOptions( - "The native initializer structural allowance", - "parser", - { - limit: cause.failure.limit.maximum, - ...(cause.unit === undefined ? {} : { unit: cause.unit }), - }, - ), - }, - ); + return parseLimitConstraint(OPERATION, "capacity", cause, { + path: input.path, + ...fixedAllowanceOptions( + "The native initializer structural allowance", + "parser", + { + limit: cause.failure.limit.maximum, + ...(cause.unit === undefined ? {} : { unit: cause.unit }), + }, + ), + }); if (cause instanceof BinaryResourceFailure) - return new AnalysisResourceConstraintError( - OPERATION, - "capacity", - `Selected artifact ${input.path}: ${cause.message}`, - { boundary: cause.boundary, maximum: cause.maximum_bytes }, - { - cause, - ...fixedAllowanceOptions( - "The native initializer structural allowance", - "parser", - binaryResourceCeiling(cause), - ), - }, - ); + return parseLimitConstraint(OPERATION, "capacity", cause, { + path: input.path, + ...fixedAllowanceOptions( + "The native initializer structural allowance", + "parser", + binaryResourceCeiling(cause), + ), + }); const formatFailure = parseFailureAnalysisError(OPERATION, input.path, cause); if (formatFailure !== undefined) return formatFailure; if ( diff --git a/src/native/parsers/plist.test.ts b/src/native/parsers/plist.test.ts index 8330bcbbf3..3779e2ca67 100644 --- a/src/native/parsers/plist.test.ts +++ b/src/native/parsers/plist.test.ts @@ -93,7 +93,7 @@ it.each([ code: "resource_constraint", retryable: false, details: { - reported_limits: { maximum_nesting_depth: maximum }, + reported_limits: { boundary: "plist-nesting-depth", maximum }, refusal: { kind: "fixed_ceiling", allowance: "The native plist nesting allowance", diff --git a/src/native/parsers/plist.ts b/src/native/parsers/plist.ts index 0d344a8e0b..2126d0fa53 100644 --- a/src/native/parsers/plist.ts +++ b/src/native/parsers/plist.ts @@ -6,8 +6,9 @@ import { import { AnalysisOutputError, - AnalysisResourceConstraintError, + type AnalysisResourceConstraintError, } from "../../domain/analysisErrorCore.js"; +import { parseLimitConstraint } from "../../domain/parseFailureAnalysisError.js"; import { fixedAllowanceOptions } from "../../domain/fixedAllowanceRemediation.js"; import type { JsonValue } from "../../domain/jsonValue.js"; import { @@ -23,7 +24,10 @@ import { PlistNestingLimitError, } from "../../domain/apple/plistValue.js"; import { parseXmlPropertyList } from "../../domain/propertyListKeys.js"; -import { malformedInput } from "../../domain/parseFailure.js"; +import { + malformedInput, + type ParseFailureError, +} from "../../domain/parseFailure.js"; import { InternalInvariantError } from "../../domain/internalInvariant.js"; import { err, ok, type Result } from "../../domain/result.js"; @@ -100,38 +104,32 @@ export interface PlistJsonParseAttempt { type PlistParseError = AnalysisOutputError | AnalysisResourceConstraintError; -const plistNestingError = (cause: unknown): AnalysisResourceConstraintError => - new AnalysisResourceConstraintError( +const plistNestingError = ( + cause: PlistNestingLimitError, +): AnalysisResourceConstraintError => + parseLimitConstraint( "inspect_plist", "memory", - errorMessage(cause), - { maximum_nesting_depth: MAX_NATIVE_PLIST_NESTING_DEPTH }, - { - cause, - ...fixedAllowanceOptions("The native plist nesting allowance", "parser", { - limit: MAX_NATIVE_PLIST_NESTING_DEPTH, - unit: "levels", - }), - }, + cause, + fixedAllowanceOptions("The native plist nesting allowance", "parser", { + limit: MAX_NATIVE_PLIST_NESTING_DEPTH, + unit: "levels", + }), ); const plistDecodeBudgetError = ( - cause: Error, + cause: ParseFailureError<"limit">, encoding: "XML" | "binary", ): AnalysisResourceConstraintError => - new AnalysisResourceConstraintError( + parseLimitConstraint( "inspect_plist", "memory", - cause.message, - { maximum_decoded_bytes: MAX_NATIVE_PLIST_DECODE_BYTES }, - { - cause, - ...fixedAllowanceOptions( - `The native ${encoding} plist decode allowance`, - "parser", - { limit: MAX_NATIVE_PLIST_DECODE_BYTES, unit: "bytes" }, - ), - }, + cause, + fixedAllowanceOptions( + `The native ${encoding} plist decode allowance`, + "parser", + { limit: MAX_NATIVE_PLIST_DECODE_BYTES, unit: "bytes" }, + ), ); /** diff --git a/src/native/pe/PeBinaryFailure.ts b/src/native/pe/PeBinaryFailure.ts index bae3f8597c..ae63ad0b04 100644 --- a/src/native/pe/PeBinaryFailure.ts +++ b/src/native/pe/PeBinaryFailure.ts @@ -1,5 +1,8 @@ import type { FixedCeilingFacts } from "../../domain/fixedAllowanceRemediation.js"; -import { parseFailureAnalysisError } from "../../domain/parseFailureAnalysisError.js"; +import { + parseFailureAnalysisError, + parseLimitConstraint, +} from "../../domain/parseFailureAnalysisError.js"; import { projectSelectedArtifactFileFailure, stableArtifactInputFailure, @@ -69,13 +72,10 @@ const peBinaryFailurePrimary = ( ); if (cause instanceof BinaryResourceFailure) { const limit = binaryResourceAllowance(cause, options.limits); - return new AnalysisResourceConstraintError( - operation, - "capacity", - `Selected artifact ${input.path}: ${cause.message}`, - { boundary: cause.boundary, maximum: cause.maximum_bytes, limit }, - { cause, ...options.limitRecovery(limit, binaryResourceCeiling(cause)) }, - ); + return parseLimitConstraint(operation, "capacity", cause, { + path: input.path, + ...options.limitRecovery(limit, binaryResourceCeiling(cause)), + }); } const formatFailure = parseFailureAnalysisError(operation, input.path, cause); if (formatFailure !== undefined) return formatFailure; diff --git a/tests/acceptance/applications/nativePlistNesting.test.ts b/tests/acceptance/applications/nativePlistNesting.test.ts index ef206a525f..9683f02067 100644 --- a/tests/acceptance/applications/nativePlistNesting.test.ts +++ b/tests/acceptance/applications/nativePlistNesting.test.ts @@ -98,7 +98,10 @@ describe.skipIf(process.platform !== "darwin")( retryable: false, details: { resource: "memory", - reported_limits: { maximum_nesting_depth: 997 }, + reported_limits: { + boundary: "plist-nesting-depth", + maximum: 997, + }, }, remediation: { action: expect.stringMatching(/\S/u), diff --git a/tests/acceptance/goBinaryCli.test.ts b/tests/acceptance/goBinaryCli.test.ts index 732c0b0e89..ec8b08833e 100644 --- a/tests/acceptance/goBinaryCli.test.ts +++ b/tests/acceptance/goBinaryCli.test.ts @@ -166,7 +166,7 @@ cliTest( resource: "memory", reported_limits: { boundary: "build-info", - maximum_bytes: maximum, + maximum, }, } : { issues: [{ path: ["path"], reason: "invalid_format" }] }), @@ -181,7 +181,7 @@ cliTest( resource: "memory", reported_limits: { boundary: "build-info", - maximum_bytes: maximum, + maximum, }, } : { issues: [{ path: ["path"], reason: "invalid_format" }] }), @@ -268,7 +268,7 @@ cliTest( resource: "cpu", reported_limits: { boundary: "search", - maximum_bytes: 256 * 1024 * 1024, + maximum: 256 * 1024 * 1024, }, }, }; diff --git a/tests/acceptance/peLoadConfigCli.test.ts b/tests/acceptance/peLoadConfigCli.test.ts index 7f90f16bc6..6bc402d984 100644 --- a/tests/acceptance/peLoadConfigCli.test.ts +++ b/tests/acceptance/peLoadConfigCli.test.ts @@ -134,7 +134,7 @@ cliTest("fails closed at the callback limit", async ({ cli }) => { expect(result.json).toMatchObject({ code: "resource_constraint", retryable: false, - details: { reported_limits: { limit: "callbacks", maximum: 1 } }, + details: { reported_limits: { boundary: "callbacks", maximum: 1 } }, remediation: { action: expect.stringContaining("Raise max_callbacks above 1"), }, diff --git a/tests/boundary/captures/fieldDiscovery.test.ts b/tests/boundary/captures/fieldDiscovery.test.ts index 3bc664b76c..7156ef982d 100644 --- a/tests/boundary/captures/fieldDiscovery.test.ts +++ b/tests/boundary/captures/fieldDiscovery.test.ts @@ -1,6 +1,15 @@ import { expect, it } from "vitest"; +import { fixedAllowanceOptions } from "../../../src/domain/fixedAllowanceRemediation.js"; import { inspectPacketFieldsInputSchema } from "../../../src/domain/captures/packetFields.js"; -import { parseFieldDiscovery } from "../../../src/captures/parseFieldDiscovery.js"; +import { + isParseLimit, + parseLimitConstraint, +} from "../../../src/domain/parseFailureAnalysisError.js"; +import { + FIELD_GLOSSARY_FORMAT, + FIELD_RECORD_BUDGET, + parseFieldDiscovery, +} from "../../../src/captures/parseFieldDiscovery.js"; it("keeps duplicate registrations and source ordinals instead of overwriting abbreviations", () => { const text = @@ -53,12 +62,35 @@ it("rejects damaged records even outside the selected page", () => { it("bounds explicit full-result expansion while allowing a page from the same large glossary", () => { const text = "P\t" + "Long name ".repeat(30) + "\tabbr\n"; const glossary = text.repeat(6000); - expect(() => + let thrown: unknown; + try { parseFieldDiscovery( glossary, inspectPacketFieldsInputSchema.parse({ limit: null }), + ); + } catch (cause) { + thrown = cause; + } + expect(isParseLimit(thrown, FIELD_GLOSSARY_FORMAT)).toBe(true); + if (!isParseLimit(thrown, FIELD_GLOSSARY_FORMAT)) + throw new Error("Expected glossary parse limit"); + expect( + parseLimitConstraint( + "inspect_packet_fields", + "memory", + thrown, + fixedAllowanceOptions( + "The packet-field discovery record allowance", + "capture", + ), ), - ).toThrow("representation budget"); + ).toMatchObject({ + reason: expect.stringContaining("representation budget"), + reportedLimits: { + boundary: "record_budget_bytes", + maximum: FIELD_RECORD_BUDGET, + }, + }); const result = parseFieldDiscovery( glossary, inspectPacketFieldsInputSchema.parse({ offset: 5999, limit: 1 }), diff --git a/tests/boundary/cli/nativePlistSelectionFailureCli.test.ts b/tests/boundary/cli/nativePlistSelectionFailureCli.test.ts index 27ed3cf2cf..8c206a148a 100644 --- a/tests/boundary/cli/nativePlistSelectionFailureCli.test.ts +++ b/tests/boundary/cli/nativePlistSelectionFailureCli.test.ts @@ -433,7 +433,10 @@ describe.skipIf(process.platform !== "darwin")( details: { operation: "inspect_plist", resource: "memory", - reported_limits: { maximum_decoded_bytes: 64 * 1024 * 1024 }, + reported_limits: { + boundary: "plist-decode-bytes", + maximum: 64 * 1024 * 1024, + }, refusal: { kind: "fixed_ceiling", allowance: "The native binary plist decode allowance", diff --git a/tests/boundary/filesystem/nativeExports.test.ts b/tests/boundary/filesystem/nativeExports.test.ts index ad283cba70..409eebddcc 100644 --- a/tests/boundary/filesystem/nativeExports.test.ts +++ b/tests/boundary/filesystem/nativeExports.test.ts @@ -80,7 +80,7 @@ describe("native export stable filesystem boundary", () => { expect(result.error).toMatchObject({ _tag: "AnalysisResourceConstraintError", resource: "capacity", - reportedLimits: { boundary: "structure", maximum: 16777216 }, + reportedLimits: { boundary: "retained_text", maximum: 16777216 }, retryable: false, fixedAllowance: { name: "The native export retained-text allowance", @@ -295,7 +295,7 @@ describe("native export entry allowance", () => { expect(result.error).toMatchObject({ _tag: "AnalysisResourceConstraintError", resource: "capacity", - reportedLimits: { boundary: "structure", maximum: 262144 }, + reportedLimits: { boundary: "rows", maximum: 262144 }, retryable: false, fixedAllowance: { name: "The native export inventory allowance", diff --git a/tests/boundary/filesystem/peImports.test.ts b/tests/boundary/filesystem/peImports.test.ts index d42f8a3289..cc8612971a 100644 --- a/tests/boundary/filesystem/peImports.test.ts +++ b/tests/boundary/filesystem/peImports.test.ts @@ -110,7 +110,7 @@ describe("PE import stable filesystem boundary", () => { if (dlls.ok) throw new Error("Expected DLL budget failure"); expect(dlls.error).toMatchObject({ _tag: "AnalysisResourceConstraintError", - reportedLimits: { limit: "dlls", maximum: 1 }, + reportedLimits: { boundary: "dlls", maximum: 1 }, remediationAction: expect.stringContaining("Raise max_dlls above 1"), retryable: false, }); @@ -121,7 +121,7 @@ describe("PE import stable filesystem boundary", () => { if (text.ok) throw new Error("Expected retained-text failure"); expect(text.error).toMatchObject({ _tag: "AnalysisResourceConstraintError", - reportedLimits: { limit: "retained_text", maximum: 16777216 }, + reportedLimits: { boundary: "retained_text", maximum: 16777216 }, remediationAction: undefined, fixedAllowance: { name: expect.stringContaining("PE import retained-text allowance"), diff --git a/tests/boundary/go/goBinaryProvider.test.ts b/tests/boundary/go/goBinaryProvider.test.ts index d6696fde2d..cb3b5bf0e3 100644 --- a/tests/boundary/go/goBinaryProvider.test.ts +++ b/tests/boundary/go/goBinaryProvider.test.ts @@ -70,11 +70,14 @@ it("preserves the actual build-info byte guard as a resource failure", async () expect(result.error.reason).toContain(path); expect(result.error.reportedLimits).toEqual({ boundary: "build-info", - maximum_bytes: 1024 * 1024, + maximum: 1024 * 1024, + }); + const projected = projectAnalysisError(result.error); + expect(projected.details?.limit).toMatchObject({ + control_kind: "parameter", parameter: "max_build_info_bytes", unit: "bytes", }); - const projected = projectAnalysisError(result.error); expect(projected.retryable).toBe(false); expect(projected.remediation.action).toContain( "Go build-info string allowance", @@ -95,7 +98,7 @@ it("keeps the header-search budget distinct from a worker CPU limit", async () = resource: "cpu", reported_limits: { boundary: "search", - maximum_bytes: 256 * 1024 * 1024, + maximum: 256 * 1024 * 1024, }, }, }); @@ -260,13 +263,23 @@ it("adjusts string_literals file offsets inside a universal Mach-O slice and rec }); it.each([ - ["max_input_bytes", 4095, 8192], - ["max_report_bytes", 1, 16384], - ["max_build_info_bytes", 1, 4096], - ["max_header_search_bytes", 1, 4096], + [ + "max_input_bytes", + 4095, + 8192, + { maximum_input_bytes: 4095, parameter: "max_input_bytes", unit: "bytes" }, + ], + [ + "max_report_bytes", + 1, + 16384, + { maximum_output_bytes: 1, parameter: "max_report_bytes", unit: "bytes" }, + ], + ["max_build_info_bytes", 1, 4096, { boundary: "build-info", maximum: 1 }], + ["max_header_search_bytes", 1, 4096, { boundary: "search", maximum: 1 }], ] as const)( "recovers the same Go artifact after increasing %s", - async (parameter, low, raised) => { + async (parameter, low, raised, reportedLimits) => { const root = await createTestTempDirectory("rea-go-budget-recovery-"); const path = join(root, "application.elf"); await writeFile(path, createGoBinaryFixture().bytes); @@ -279,7 +292,14 @@ it.each([ ); expect(projectAnalysisError(refused.error)).toMatchObject({ code: "resource_constraint", - details: { reported_limits: { parameter, unit: "bytes" } }, + details: { + reported_limits: reportedLimits, + limit: { + control_kind: "parameter", + parameter, + unit: "bytes", + }, + }, remediation: { action: expect.stringContaining(parameter) }, }); const recovered = await service.inspect({ path, [parameter]: raised }); diff --git a/tests/boundary/il2cpp/il2cppMetadataProvider.test.ts b/tests/boundary/il2cpp/il2cppMetadataProvider.test.ts index 6ea96aa101..f4c8046b5a 100644 --- a/tests/boundary/il2cpp/il2cppMetadataProvider.test.ts +++ b/tests/boundary/il2cpp/il2cppMetadataProvider.test.ts @@ -1067,7 +1067,7 @@ it("reports the exceeded native-link maximum in the public refusal", async () => resource: "capacity", reportedLimits: { boundary: "native-index-capacity", - maximum_entries: 64, + maximum: 64, }, fixedAllowance: { limit: 64, unit: "entries" }, }); diff --git a/tests/boundary/mcp/binaryDiagnosticsMcp.test.ts b/tests/boundary/mcp/binaryDiagnosticsMcp.test.ts index 144bc10785..9f4316b635 100644 --- a/tests/boundary/mcp/binaryDiagnosticsMcp.test.ts +++ b/tests/boundary/mcp/binaryDiagnosticsMcp.test.ts @@ -59,6 +59,7 @@ it("reports the exhausted native export allowance through MCP", async () => { { filename: "byte-limit.dll", bytes: peRepeatedExportNamesImage(0x3000), + boundary: "retained_text", maximum: 16777216, expected: "fixed byte allowance", excluded: "fixed count", @@ -66,6 +67,7 @@ it("reports the exhausted native export allowance through MCP", async () => { { filename: "count-limit.dll", bytes: peStoredExportTableImage(0x40001), + boundary: "rows", maximum: 262144, expected: "fixed count", excluded: "fixed byte allowance", @@ -83,7 +85,10 @@ it("reports the exhausted native export allowance through MCP", async () => { code: "resource_constraint", retryable: false, details: { - reported_limits: { boundary: "structure", maximum: fixture.maximum }, + reported_limits: { + boundary: fixture.boundary, + maximum: fixture.maximum, + }, }, }); expect(error.remediation.action).toContain(fixture.expected); diff --git a/tests/boundary/providers/native/nativePlistBinarySource.test.ts b/tests/boundary/providers/native/nativePlistBinarySource.test.ts index d9f54775d2..3d36844745 100644 --- a/tests/boundary/providers/native/nativePlistBinarySource.test.ts +++ b/tests/boundary/providers/native/nativePlistBinarySource.test.ts @@ -108,7 +108,7 @@ it.each([ // Three times the source length exceeds the 64 MiB decode estimate while // the source stays below the separate 64 MiB input ceiling. value: { data: Buffer.alloc(22 * 1024 * 1024, 0x78) }, - limits: { maximum_decoded_bytes: 64 * 1024 * 1024 }, + limits: { boundary: "plist-decode-bytes", maximum: 64 * 1024 * 1024 }, refusal: { allowance: "The native binary plist decode allowance", limit: 64 * 1024 * 1024, @@ -118,7 +118,7 @@ it.each([ { budget: "nesting", value: nested(MAX_JSON_DEPTH - 1), - limits: { maximum_nesting_depth: MAX_JSON_DEPTH - 3 }, + limits: { boundary: "plist-nesting-depth", maximum: MAX_JSON_DEPTH - 3 }, refusal: { allowance: "The native plist nesting allowance", limit: MAX_JSON_DEPTH - 3, diff --git a/tests/boundary/signals/decoderDiscovery.test.ts b/tests/boundary/signals/decoderDiscovery.test.ts index 6b69d679f8..e10af322f4 100644 --- a/tests/boundary/signals/decoderDiscovery.test.ts +++ b/tests/boundary/signals/decoderDiscovery.test.ts @@ -1,6 +1,14 @@ import { expect, it } from "vitest"; +import { fixedAllowanceOptions } from "../../../src/domain/fixedAllowanceRemediation.js"; +import { + isParseLimit, + parseLimitConstraint, +} from "../../../src/domain/parseFailureAnalysisError.js"; import { inspectSignalDecodersInputSchema } from "../../../src/domain/signals/signalDecoders.js"; -import { parseDecoderDiscovery } from "../../../src/signals/parseDecoderDiscovery.js"; +import { + DECODER_CATALOG_FORMAT, + parseDecoderDiscovery, +} from "../../../src/signals/parseDecoderDiscovery.js"; it("rejects malformed catalogs and the wrong decoder identity", () => { for (const text of [ @@ -39,10 +47,30 @@ it("keeps help line ordinals, blank lines and literal special characters through }); it("bounds line expansion before constructing help records", () => { - expect(() => + let thrown: unknown; + try { parseDecoderDiscovery( "ID: uart\n" + "\n".repeat(65536), inspectSignalDecodersInputSchema.parse({ decoder: "uart" }), + ); + } catch (cause) { + thrown = cause; + } + expect(isParseLimit(thrown, DECODER_CATALOG_FORMAT)).toBe(true); + if (!isParseLimit(thrown, DECODER_CATALOG_FORMAT)) + throw new Error("Expected decoder catalog parse limit"); + expect( + parseLimitConstraint( + "inspect_signal_decoders", + "memory", + thrown, + fixedAllowanceOptions( + "The sigrok decoder-discovery line allowance", + "capture", + ), ), - ).toThrow("representation budget"); + ).toMatchObject({ + reason: expect.stringContaining("representation budget"), + reportedLimits: { boundary: "line_budget", maximum: 65536 }, + }); }); diff --git a/tests/boundary/signals/recordingProvider.test.ts b/tests/boundary/signals/recordingProvider.test.ts index 520e897a42..fc43efeb77 100644 --- a/tests/boundary/signals/recordingProvider.test.ts +++ b/tests/boundary/signals/recordingProvider.test.ts @@ -1380,7 +1380,7 @@ it.each([ _tag: "AnalysisResourceConstraintError", resource: "memory", reason: "Decoder discovery exceeds the 65,536-line representation budget", - reportedLimits: { line_budget: 65536 }, + reportedLimits: { boundary: "line_budget", maximum: 65536 }, fixedAllowance: { kind: "capture" }, }); expect(result.error.capturedOutput?.stdout).toBe(