diff --git a/scripts/verify/baselines/local-limit-error-class.json b/scripts/verify/baselines/local-limit-error-class.json deleted file mode 100644 index 7ccf3bdfa0..0000000000 --- a/scripts/verify/baselines/local-limit-error-class.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "src/browser/WebSourceMapFetcher.ts": 1, - "src/domain/firmware/devicetreeParser.ts": 1, - "src/v8/V8CodeCache.errors.ts": 1 -} diff --git a/scripts/verify/baselines/range-error-interpretation.json b/scripts/verify/baselines/range-error-interpretation.json deleted file mode 100644 index 17c3dd7fff..0000000000 --- a/scripts/verify/baselines/range-error-interpretation.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "src/application/JsonInputFile.ts": 1, - "src/artifacts/nodeSea/NodeSeaBlob.ts": 1, - "src/browser/WebBundleAnalysisWorkerServer.ts": 1, - "src/cli/cliResultOutput.ts": 1, - "src/domain/apiContract/GraphqlDocuments.ts": 1, - "src/domain/apiContract/parseGraphqlContract.ts": 1, - "src/javascript/analysis/JavaScriptAnalysisWorkerServer.ts": 2, - "src/wasm/WabtFailures.ts": 1 -} diff --git a/src/application/JsonInputFile.ts b/src/application/JsonInputFile.ts index 2262ef398e..e074499901 100644 --- a/src/application/JsonInputFile.ts +++ b/src/application/JsonInputFile.ts @@ -41,6 +41,7 @@ import { AnalysisInputError, AnalysisResourceConstraintError, } from "../domain/analysisErrorCore.js"; +import { hostRangeErrorKind } from "../domain/parseFailure.js"; import { err, ok, type Result } from "../domain/result.js"; type JsonInputFileFailure = @@ -527,9 +528,9 @@ const jsonReadFailure = ( ) return cause; if ( + hostRangeErrorKind(cause) === "host-capacity" && cause instanceof Error && - ((cause instanceof RangeError && - cause.message === "Invalid string length") || + (cause.message === "Invalid string length" || ("code" in cause && cause.code === "ERR_STRING_TOO_LONG")) ) return new AnalysisResourceConstraintError( diff --git a/src/artifacts/fit/FitStructure.ts b/src/artifacts/fit/FitStructure.ts index 309aa309e2..977b353a7e 100644 --- a/src/artifacts/fit/FitStructure.ts +++ b/src/artifacts/fit/FitStructure.ts @@ -16,7 +16,7 @@ import { type Devicetree, } from "../../domain/firmware/devicetree.js"; import { - DevicetreeLimitFailure, + devicetreeLimitOf, parseDevicetree, } from "../../domain/firmware/devicetreeParser.js"; import { BinaryFormatFailure } from "../../domain/binaryContainer.js"; @@ -569,12 +569,13 @@ const parseFit = (fdt: Buffer, path: string): Devicetree => { } catch (cause) { if (cause instanceof BinaryFormatFailure) throw formatFailure(cause.message); - if (cause instanceof DevicetreeLimitFailure) + const limit = devicetreeLimitOf(cause); + if (limit !== undefined) throw new ArtifactReaderFailure( "limit", - `FIT devicetree exceeds the fixed ${cause.limit} allowance`, + `FIT devicetree exceeds the fixed ${limit.limit} allowance`, { - boundary: cause.limit === "decoded_bytes" ? "memory" : "capacity", + boundary: limit.limit === "decoded_bytes" ? "memory" : "capacity", cause, }, ); diff --git a/src/artifacts/nodeSea/NodeSeaBlob.ts b/src/artifacts/nodeSea/NodeSeaBlob.ts index 4eef979b8b..0d8aba28cb 100644 --- a/src/artifacts/nodeSea/NodeSeaBlob.ts +++ b/src/artifacts/nodeSea/NodeSeaBlob.ts @@ -9,6 +9,7 @@ import { DEFAULT_NODE_SEA_READER_LIMITS, type NodeSeaReaderLimits, } from "../../domain/artifactReaderLimits.js"; +import { hostRangeErrorKind } from "../../domain/parseFailure.js"; import { strictUtf8Decoder } from "../../domain/strictUtf8.js"; /** Node `SeaBlob::kMagic` from `src/node_sea.h` (v24). */ @@ -236,7 +237,7 @@ class BlobCursor { try { text = strictUtf8Decoder("preserve").decode(raw); } catch (cause: unknown) { - if (cause instanceof RangeError) + if (hostRangeErrorKind(cause) === "host-capacity") throw new ArtifactReaderFailure( "limit", "Node SEA UTF-8 text exceeds this runtime's string or memory capacity. Admission budgets do not raise runtime representation limits or reserve host memory.", diff --git a/src/browser/WebBundleAnalysisWorkerServer.ts b/src/browser/WebBundleAnalysisWorkerServer.ts index 170f9afd93..08c586ccf8 100644 --- a/src/browser/WebBundleAnalysisWorkerServer.ts +++ b/src/browser/WebBundleAnalysisWorkerServer.ts @@ -34,6 +34,8 @@ import { } from "./WebBundleAnalysisWorkerProtocol.js"; import { decodeStrictUtf8 } from "../domain/strictUtf8.js"; import { AnalysisResourceConstraintError } from "../domain/analysisErrorCore.js"; +import { decodeLimit, isHostLimitFailure } from "../domain/parseFailure.js"; +import { isParseLimit } from "../domain/parseFailureAnalysisError.js"; import { errorMessage } from "../kernel/errorMessage.js"; export interface WebBundleScriptAnalyzer { @@ -46,6 +48,7 @@ export interface WebBundleScriptAnalyzer { class WebBundleWorkerInputError extends Error {} class WebBundleWorkerOutputError extends Error {} +const WEB_BUNDLE_RESULT_FORMAT = "web-bundle-worker-result"; const send = async (input: WebBundleWorkerMessage): Promise => { const value = webBundleWorkerMessageSchema.parse(input); @@ -192,8 +195,14 @@ const analyzeJob = async ( const chunk = Buffer.from(part); bytes += chunk.length; if (bytes > request.result_budget_bytes) - throw new RangeError( + throw decodeLimit( + WEB_BUNDLE_RESULT_FORMAT, "Web bundle worker result exceeds its admitted byte budget", + { + name: "result_budget_bytes", + maximum: request.result_budget_bytes, + required: bytes, + }, ); hash.update(chunk); let offset = 0; @@ -221,7 +230,10 @@ const analyzeJob = async ( } catch (cause: unknown) { const refusal = cause instanceof AnalysisResourceConstraintError ? cause : undefined; - const resource = cause instanceof RangeError || refusal !== undefined; + const resource = + isHostLimitFailure(cause) || + isParseLimit(cause, WEB_BUNDLE_RESULT_FORMAT) || + refusal !== undefined; const input = cause instanceof WebBundleWorkerInputError; const fatalCleanupFailure = cause instanceof ArtifactReaderFailure && diff --git a/src/browser/WebSourceMapFetcher.test.ts b/src/browser/WebSourceMapFetcher.test.ts index d2fb42303a..cdf06fbf4f 100644 --- a/src/browser/WebSourceMapFetcher.test.ts +++ b/src/browser/WebSourceMapFetcher.test.ts @@ -425,7 +425,12 @@ it.each([ if (trigger === "caller-abort") expect(result.error.cause).toBe(reason); else { expect(result.error.cause).toMatchObject({ - name: "SourceMapSizeLimitError", + name: "ParseFailureError", + failure: { + kind: "limit", + format: "source-map", + limit: { name: "fetch_bytes", maximum: 1 }, + }, }); expect( result.error.secondaryFailures.some( diff --git a/src/browser/WebSourceMapFetcher.ts b/src/browser/WebSourceMapFetcher.ts index 60b6fae663..2fb0d53a1b 100644 --- a/src/browser/WebSourceMapFetcher.ts +++ b/src/browser/WebSourceMapFetcher.ts @@ -321,14 +321,19 @@ class SourceMapCallerAbort extends Error { const sourceMapCallerReason = (value: unknown): unknown => value instanceof SourceMapCallerAbort ? value.reason : value; -class SourceMapSizeLimitError extends Error { - constructor(maxBytes: number) { - super( - `Source-map response exceeded its retained budget. ${sourceMapLimitRecovery("fetch_bytes", maxBytes)}`, - ); - this.name = "SourceMapSizeLimitError"; - } -} +const sourceMapSizeLimitError = ( + maxBytes: number, +): ParseFailureError<"limit"> => + sourceMapLimitFailure( + "Source-map response exceeded its retained budget.", + "fetch_bytes", + maxBytes, + ); + +const isSourceMapSizeLimitError = ( + cause: unknown, +): cause is ParseFailureError<"limit"> => + isSourceMapLimit(cause) && cause.failure.limit.name === "fetch_bytes"; class SourceMapRedirectLocationError extends Error { constructor(url: string, location: string) { @@ -718,7 +723,8 @@ const fetchOne = async ( const cause = caught instanceof SourceMapResponseCleanup ? caught.cause : caught; if (cause instanceof AnalysisError) throw cause; - if (isSourceMapLimit(cause)) throw cause; + if (isSourceMapLimit(cause) && !isSourceMapSizeLimitError(cause)) + throw cause; if ( signal?.aborted === true && !(signal.reason instanceof SourceMapDeadlineError) @@ -728,7 +734,7 @@ const fetchOne = async ( request, "fetch_failed", cause instanceof SourceMapRedirectLocationError || - cause instanceof SourceMapSizeLimitError || + isSourceMapSizeLimitError(cause) || cause instanceof SourceMapDeadlineError ? cause.message : "Source-map fetch or validation failed.", @@ -847,7 +853,7 @@ const readBoundedText = async ( if (done) break; const nextBytes = value?.byteLength ?? 0; if (budget.retainedBytes + bytes + nextBytes > maxBytes) { - throw new SourceMapSizeLimitError(maxBytes); + throw sourceMapSizeLimitError(maxBytes); } if (value !== undefined) chunks.push(value); bytes += nextBytes; @@ -879,7 +885,7 @@ const readBoundedText = async ( signal, sourceMapCallerReason(cause), ); - if (cause instanceof SourceMapSizeLimitError) { + if (isSourceMapSizeLimitError(cause)) { try { // Preserve the size-limit item's existing finalization boundary. const completion = cleanup.completion; diff --git a/src/cli/cliResultOutput.ts b/src/cli/cliResultOutput.ts index 7f4f3c3c8a..5a5e45478b 100644 --- a/src/cli/cliResultOutput.ts +++ b/src/cli/cliResultOutput.ts @@ -13,6 +13,7 @@ import { AnalysisInputError, AnalysisResourceConstraintError, } from "../domain/analysisErrorCore.js"; +import { hostRangeErrorKind } from "../domain/parseFailure.js"; import { err, ok, type Result } from "../domain/result.js"; import { sliceTextByTokenWindow } from "./tokenWindow.js"; import { @@ -119,7 +120,8 @@ export const createCliResultOutput = ( if ( completed === undefined || handled || - !(cause instanceof RangeError) || + hostRangeErrorKind(cause) !== "host-capacity" || + !(cause instanceof Error) || cause.message !== "Invalid string length" ) return false; diff --git a/src/domain/apiContract/GraphqlDocuments.ts b/src/domain/apiContract/GraphqlDocuments.ts index 7aef7c6d57..02edb173f2 100644 --- a/src/domain/apiContract/GraphqlDocuments.ts +++ b/src/domain/apiContract/GraphqlDocuments.ts @@ -1,6 +1,7 @@ import { GraphQLError, Source, parse, visit, type DocumentNode } from "graphql"; import { AnalysisResourceConstraintError } from "../analysisErrorCore.js"; import { fixedAllowanceOptions } from "../fixedAllowanceRemediation.js"; +import { isHostLimitFailure } from "../parseFailure.js"; import { API_CONTRACT_OPERATION, API_CONTRACT_MAX_FILE_BYTES, @@ -77,7 +78,7 @@ export const parseGraphqlDocument = ( } catch (cause) { if (cause instanceof GraphQLError) throw new GraphqlDocumentRefusal(cause, document, role); - if (cause instanceof RangeError) + if (isHostLimitFailure(cause)) throw new AnalysisResourceConstraintError( API_CONTRACT_OPERATION, "memory", diff --git a/src/domain/apiContract/parseGraphqlContract.ts b/src/domain/apiContract/parseGraphqlContract.ts index 3e6af663ff..66cb34701e 100644 --- a/src/domain/apiContract/parseGraphqlContract.ts +++ b/src/domain/apiContract/parseGraphqlContract.ts @@ -4,6 +4,7 @@ import { AnalysisInputError, AnalysisResourceConstraintError, } from "../analysisErrorCore.js"; +import { isHostLimitFailure } from "../parseFailure.js"; import { err, ok, type Result } from "../result.js"; import type { GraphqlContract } from "./graphqlContract.js"; import { API_CONTRACT_OPERATION } from "./apiContract.js"; @@ -41,7 +42,7 @@ export function* parseGraphqlContractSteps( inventory.roots = roots; model = constructed; } catch (cause) { - if (cause instanceof RangeError) + if (isHostLimitFailure(cause)) throw new AnalysisResourceConstraintError( API_CONTRACT_OPERATION, "memory", diff --git a/src/domain/firmware/devicetreeParser.test.ts b/src/domain/firmware/devicetreeParser.test.ts index 3b1f205dee..ae90d349b1 100644 --- a/src/domain/firmware/devicetreeParser.test.ts +++ b/src/domain/firmware/devicetreeParser.test.ts @@ -4,7 +4,11 @@ import { devicetreeSchema, inspectDevicetreeInputSchema, } from "./devicetree.js"; -import { DevicetreeLimitFailure, parseDevicetree } from "./devicetreeParser.js"; +import { + DEVICETREE_DECODED_BYTES, + devicetreeLimitFailure, + parseDevicetree, +} from "./devicetreeParser.js"; import { cells, devicetreeFixture, @@ -305,7 +309,7 @@ it.each(["compatible", "status"])( properties: { [name]: Buffer.alloc(6 * 1024 * 1024) }, }); expect(() => parse(bytes, { max_file_bytes: 16777216 })).toThrow( - new DevicetreeLimitFailure("decoded_bytes"), + devicetreeLimitFailure("decoded_bytes", DEVICETREE_DECODED_BYTES), ); }, ); @@ -323,7 +327,7 @@ it("accounts for string values serialized again in the summary", () => { parse(devicetreeFixture({ ...child, name: "" }), { max_file_bytes: 16777216, }), - ).toThrow(new DevicetreeLimitFailure("decoded_bytes")); + ).toThrow(devicetreeLimitFailure("decoded_bytes", DEVICETREE_DECODED_BYTES)); }); it("retains undecodable alias identities and labels the summary derived", () => { const report = parse( diff --git a/src/domain/firmware/devicetreeParser.ts b/src/domain/firmware/devicetreeParser.ts index b759172b01..4feedd224f 100644 --- a/src/domain/firmware/devicetreeParser.ts +++ b/src/domain/firmware/devicetreeParser.ts @@ -1,19 +1,48 @@ import type { Devicetree, InspectDevicetreeInput } from "./devicetree.js"; import { BinaryFormatFailure } from "../binaryContainer.js"; +import { decodeLimit, ParseFailureError } from "../parseFailure.js"; -export class DevicetreeLimitFailure extends Error { - constructor( - readonly limit: - | keyof Omit - | "decoded_bytes", - ) { - super( - limit === "decoded_bytes" - ? "Devicetree exceeds its fixed 32 MiB decoded representation allowance; select a smaller blob. This allowance has no runtime override." - : `Devicetree exceeds ${limit}; raise that caller-selected limit within its schema ceiling or select a smaller blob.`, - ); - } -} +/** The ParseFailure format of flattened devicetree blobs. */ +export const DEVICETREE_FORMAT = "devicetree"; + +export const DEVICETREE_DECODED_BYTES = 33554432; + +export type DevicetreeLimit = + | keyof Omit + | "decoded_bytes"; + +const DEVICETREE_LIMITS = new Set([ + "max_file_bytes", + "max_nodes", + "max_properties", + "max_depth", + "decoded_bytes", +]); + +export const devicetreeLimitFailure = ( + limit: DevicetreeLimit, + maximum: number, +): ParseFailureError<"limit"> => + decodeLimit( + DEVICETREE_FORMAT, + limit === "decoded_bytes" + ? "Devicetree exceeds its fixed 32 MiB decoded representation allowance; select a smaller blob. This allowance has no runtime override." + : `Devicetree exceeds ${limit}; raise that caller-selected limit within its schema ceiling or select a smaller blob.`, + { name: limit, maximum }, + ); + +export const devicetreeLimitOf = ( + cause: unknown, +): { readonly limit: DevicetreeLimit; readonly maximum: number } | undefined => + cause instanceof ParseFailureError && + cause.failure.kind === "limit" && + cause.failure.format === DEVICETREE_FORMAT && + DEVICETREE_LIMITS.has(cause.failure.limit.name as DevicetreeLimit) + ? { + limit: cause.failure.limit.name as DevicetreeLimit, + maximum: cause.failure.limit.maximum, + } + : undefined; const malformed = (message: string): never => { throw new BinaryFormatFailure("malformed", message); }; @@ -27,7 +56,7 @@ export const parseDevicetree = ( input: InspectDevicetreeInput, ): Devicetree => { if (bytes.length > input.max_file_bytes) - throw new DevicetreeLimitFailure("max_file_bytes"); + throw devicetreeLimitFailure("max_file_bytes", input.max_file_bytes); if (bytes.length < 28) malformed("Truncated FDT header"); const u32 = (offset: number) => bytes.readUInt32BE(offset); if (u32(0) !== 0xd00dfeed) @@ -66,8 +95,8 @@ export const parseDevicetree = ( let decodedBytes = 0; const admit = (size: number) => { decodedBytes += size; - if (decodedBytes > 33554432) - throw new DevicetreeLimitFailure("decoded_bytes"); + if (decodedBytes > DEVICETREE_DECODED_BYTES) + throw devicetreeLimitFailure("decoded_bytes", DEVICETREE_DECODED_BYTES); }; const ranges: { start: number; end: number }[] = [ { start: 0, end: headerBytes }, @@ -116,7 +145,7 @@ export const parseDevicetree = ( cursor += 16; if (address === 0n && size === 0n) break; if (reservations.length >= input.max_properties) - throw new DevicetreeLimitFailure("max_properties"); + throw devicetreeLimitFailure("max_properties", input.max_properties); admit(128); reservations.push({ offset: cursor - 16, @@ -158,9 +187,9 @@ export const parseDevicetree = ( cursor += 4; if (token === 1) { if (stack.length >= input.max_depth) - throw new DevicetreeLimitFailure("max_depth"); + throw devicetreeLimitFailure("max_depth", input.max_depth); if (nodes.length >= input.max_nodes) - throw new DevicetreeLimitFailure("max_nodes"); + throw devicetreeLimitFailure("max_nodes", input.max_nodes); const name = text(cursor, structEnd); cursor = align(name.next); need(0); @@ -199,7 +228,7 @@ export const parseDevicetree = ( cursor += 8; need(length); if (++properties > input.max_properties) - throw new DevicetreeLimitFailure("max_properties"); + throw devicetreeLimitFailure("max_properties", input.max_properties); if (nameoff >= header.size_dt_strings) malformed("FDT property string offset out of range"); let name = names.get(nameoff); diff --git a/src/firmware/devicetree/DevicetreeProvider.ts b/src/firmware/devicetree/DevicetreeProvider.ts index 96342b8179..9323561549 100644 --- a/src/firmware/devicetree/DevicetreeProvider.ts +++ b/src/firmware/devicetree/DevicetreeProvider.ts @@ -23,7 +23,7 @@ import { import { err, ok } from "../../domain/result.js"; import { DEVICETREE_PROVIDER } from "../../domain/investigationProviders.js"; import { - DevicetreeLimitFailure, + devicetreeLimitOf, parseDevicetree, } from "../../domain/firmware/devicetreeParser.js"; @@ -80,15 +80,15 @@ const devicetreeFailurePrimary = ( input: InspectDevicetreeInput, ) => { const { cause, origin } = facts; - if (cause instanceof DevicetreeLimitFailure) + const limit = devicetreeLimitOf(cause); + if (limit !== undefined && cause instanceof Error) return new AnalysisResourceConstraintError( OPERATION, - cause.limit === "decoded_bytes" ? "memory" : "capacity", + limit.limit === "decoded_bytes" ? "memory" : "capacity", cause.message, { - limit: cause.limit, - selected: - cause.limit === "decoded_bytes" ? 33554432 : input[cause.limit], + limit: limit.limit, + selected: limit.maximum, }, { cause, retryable: false, remediationAction: cause.message }, ); diff --git a/src/javascript/analysis/JavaScriptAnalysisWorkerServer.ts b/src/javascript/analysis/JavaScriptAnalysisWorkerServer.ts index d4a364f4b0..6475f17588 100644 --- a/src/javascript/analysis/JavaScriptAnalysisWorkerServer.ts +++ b/src/javascript/analysis/JavaScriptAnalysisWorkerServer.ts @@ -18,6 +18,7 @@ import { type BoundedControlLine, } from "../../process/BoundedControlLines.js"; import { AnalysisResourceConstraintError } from "../../domain/analysisErrorCore.js"; +import { isHostLimitFailure } from "../../domain/parseFailure.js"; import { javaScriptRuntime } from "../../domain/runtimeVersion.js"; import type { JavaScriptArtifactFile } from "../../domain/javascript/javascriptArtifactFiles.js"; import type { JavaScriptPackageType } from "../../domain/javascript/javascriptSourceParser.js"; @@ -214,12 +215,12 @@ const analyzeJob = async ( ); await send({ kind: "complete", id: request.id, descriptor }); } catch (cause: unknown) { + const hostLimit = isHostLimitFailure(cause); await send({ kind: "failure", id: request.id, reason: - cause instanceof AnalysisResourceConstraintError || - cause instanceof RangeError + cause instanceof AnalysisResourceConstraintError || hostLimit ? "resource" : cause instanceof TypeError ? "output" @@ -227,7 +228,7 @@ const analyzeJob = async ( resource: cause instanceof AnalysisResourceConstraintError ? cause.resource - : cause instanceof RangeError + : hostLimit ? "memory" : null, limits: diff --git a/src/v8/V8CodeCache.errors.ts b/src/v8/V8CodeCache.errors.ts index 207443cf9e..762651c5dd 100644 --- a/src/v8/V8CodeCache.errors.ts +++ b/src/v8/V8CodeCache.errors.ts @@ -1,13 +1,45 @@ +import { decodeLimit, ParseFailureError } from "../domain/parseFailure.js"; import type { V8CodeCacheInputBudget } from "../domain/v8/v8CodeCacheBudget.js"; -/** A caller-selected budget was exceeded while scanning the payload. */ -export class V8CodeCacheBudgetFailure extends Error { - constructor( - readonly boundary: keyof V8CodeCacheInputBudget, - readonly limit: number, - message: string, - ) { - super(message); - this.name = "V8CodeCacheBudgetFailure"; - } +/** The ParseFailure format of V8 code cache containers. */ +export const V8_CODE_CACHE_FORMAT = "v8-code-cache"; + +export type V8CodeCacheBudgetBoundary = keyof V8CodeCacheInputBudget; + +export interface V8CodeCacheBudgetLimit { + readonly boundary: V8CodeCacheBudgetBoundary; + readonly limit: number; + readonly message: string; } + +const V8_CODE_CACHE_BUDGET_BOUNDARIES = new Set([ + "max_strings", + "min_string_bytes", +]); + +/** A caller-selected budget was exceeded while scanning the payload. */ +export const v8CodeCacheBudgetFailure = ( + boundary: V8CodeCacheBudgetBoundary, + limit: number, + message: string, +): ParseFailureError<"limit"> => + decodeLimit(V8_CODE_CACHE_FORMAT, message, { + name: boundary, + maximum: limit, + }); + +export const v8CodeCacheBudgetLimitOf = ( + cause: unknown, +): V8CodeCacheBudgetLimit | undefined => + cause instanceof ParseFailureError && + cause.failure.kind === "limit" && + cause.failure.format === V8_CODE_CACHE_FORMAT && + V8_CODE_CACHE_BUDGET_BOUNDARIES.has( + cause.failure.limit.name as V8CodeCacheBudgetBoundary, + ) + ? { + boundary: cause.failure.limit.name as V8CodeCacheBudgetBoundary, + limit: cause.failure.limit.maximum, + message: cause.failure.detail, + } + : undefined; diff --git a/src/v8/V8CodeCacheParser.test.ts b/src/v8/V8CodeCacheParser.test.ts index 0430882b7f..46df14a90c 100644 --- a/src/v8/V8CodeCacheParser.test.ts +++ b/src/v8/V8CodeCacheParser.test.ts @@ -238,7 +238,12 @@ it("enforces the caller-selected observation budget", () => { }), ).toThrow( expect.objectContaining({ - name: "V8CodeCacheBudgetFailure", + name: "ParseFailureError", + failure: expect.objectContaining({ + kind: "limit", + format: "v8-code-cache", + limit: { name: "max_strings", maximum: 1 }, + }), message: expect.stringContaining("max_strings"), }), ); diff --git a/src/v8/V8CodeCacheParser.ts b/src/v8/V8CodeCacheParser.ts index ae353b4539..3f18ffddf8 100644 --- a/src/v8/V8CodeCacheParser.ts +++ b/src/v8/V8CodeCacheParser.ts @@ -9,15 +9,18 @@ import { type V8CodeCache, } from "../domain/v8/v8CodeCache.js"; import type { V8CodeCacheInputBudget } from "../domain/v8/v8CodeCacheBudget.js"; -import { V8CodeCacheBudgetFailure } from "../v8/V8CodeCache.errors.js"; +import { + V8_CODE_CACHE_FORMAT, + v8CodeCacheBudgetFailure, +} from "../v8/V8CodeCache.errors.js"; import { malformedInput, unsupportedInput } from "../domain/parseFailure.js"; import { sha256Hex } from "../kernel/sha256Hex.js"; const unsupported = (message: string): never => { - throw unsupportedInput("v8-code-cache", message); + throw unsupportedInput(V8_CODE_CACHE_FORMAT, message); }; const malformed = (message: string): never => { - throw malformedInput("v8-code-cache", message); + throw malformedInput(V8_CODE_CACHE_FORMAT, message); }; /** @@ -120,7 +123,7 @@ export const parseV8CodeCache = ( if (observations.length >= input.max_strings) // The message states the fact. Recovery advice belongs to the provider, which // can see whether either budget still has headroom before recommending a retry. - throw new V8CodeCacheBudgetFailure( + throw v8CodeCacheBudgetFailure( "max_strings", input.max_strings, `Payload contains more than ${String(input.max_strings)} printable runs of at least ${String(input.min_string_bytes)} bytes, so the max_strings observation budget was exhausted before the scan finished.`, diff --git a/src/v8/V8CodeCacheProvider.test.ts b/src/v8/V8CodeCacheProvider.test.ts index 3a19297ac6..965ac4d1de 100644 --- a/src/v8/V8CodeCacheProvider.test.ts +++ b/src/v8/V8CodeCacheProvider.test.ts @@ -6,7 +6,10 @@ import { MIN_INSPECT_V8_CODE_CACHE_STRING_BYTES, inspectV8CodeCacheInputSchema, } from "../domain/v8/v8CodeCache.js"; -import { V8CodeCacheBudgetFailure } from "./V8CodeCache.errors.js"; +import { + v8CodeCacheBudgetFailure, + v8CodeCacheBudgetLimitOf, +} from "./V8CodeCache.errors.js"; import { observationBudgetRecovery, observationBudgetRemediation, @@ -28,8 +31,14 @@ const input = (overrides: Record = {}) => ...overrides, }); -const exhausted = (limit: number) => - new V8CodeCacheBudgetFailure("max_strings", limit, "budget exhausted"); +const exhausted = (limit: number) => { + const parsed = v8CodeCacheBudgetLimitOf( + v8CodeCacheBudgetFailure("max_strings", limit, "budget exhausted"), + ); + if (parsed === undefined) + throw new Error("Expected V8 code cache budget limit"); + return parsed; +}; it("leaves both retries open while both budgets have headroom", () => { // The default `min_string_bytes` is the schema minimum, so lowering it is not an @@ -90,13 +99,14 @@ it("drops only the count retry when max_strings is at its ceiling", () => { }); it("requires a smaller artifact only when the run length cannot drop", () => { + const limit = v8CodeCacheBudgetLimitOf( + v8CodeCacheBudgetFailure("min_string_bytes", 4096, "budget exhausted"), + ); + if (limit === undefined) + throw new Error("Expected V8 code cache budget limit"); expect( observationBudgetRecovery( - new V8CodeCacheBudgetFailure( - "min_string_bytes", - 4096, - "budget exhausted", - ), + limit, input({ min_string_bytes: MIN_INSPECT_V8_CODE_CACHE_STRING_BYTES }), ), ).toMatchObject({ diff --git a/src/v8/V8CodeCacheProvider.ts b/src/v8/V8CodeCacheProvider.ts index d06b97dad4..a54a8a748e 100644 --- a/src/v8/V8CodeCacheProvider.ts +++ b/src/v8/V8CodeCacheProvider.ts @@ -28,7 +28,11 @@ import { type InspectV8CodeCacheInput, } from "../domain/v8/v8CodeCache.js"; import { err, ok } from "../domain/result.js"; -import { V8CodeCacheBudgetFailure } from "./V8CodeCache.errors.js"; +import { + v8CodeCacheBudgetLimitOf, + type V8CodeCacheBudgetBoundary, + type V8CodeCacheBudgetLimit, +} from "./V8CodeCache.errors.js"; import { V8_CODE_CACHE_PROVIDER_IDENTITY } from "./V8CodeCacheProviderIdentity.js"; import { parseV8CodeCache } from "./V8CodeCacheParser.js"; @@ -101,7 +105,7 @@ const inputBudgetRemediation = ( * can read the choice rather than the wording. */ export interface ObservationBudgetRecovery { - readonly boundary: V8CodeCacheBudgetFailure["boundary"]; + readonly boundary: V8CodeCacheBudgetBoundary; /** Whether a higher `max_strings` is still within the schema's admitted range. */ readonly canRaiseMaxStrings: boolean; /** Whether a higher `min_string_bytes` is still within the schema's admitted range. */ @@ -113,7 +117,7 @@ export interface ObservationBudgetRecovery { } export const observationBudgetRecovery = ( - cause: V8CodeCacheBudgetFailure, + cause: Pick, input: InspectV8CodeCacheInput, ): ObservationBudgetRecovery => { const canRaiseMaxStrings = @@ -141,7 +145,7 @@ export const observationBudgetRecovery = ( /** Render the recovery choice for one exhausted observation budget. */ export const observationBudgetRemediation = ( - cause: V8CodeCacheBudgetFailure, + cause: Pick, input: InspectV8CodeCacheInput, ): string => { const recovery = observationBudgetRecovery(cause, input); @@ -161,11 +165,12 @@ const v8CodeCacheFailurePrimary = ( input: InspectV8CodeCacheInput, ) => { const { cause, origin } = facts; - if (cause instanceof V8CodeCacheBudgetFailure) + const budgetLimit = v8CodeCacheBudgetLimitOf(cause); + if (budgetLimit !== undefined) return new AnalysisResourceConstraintError( OPERATION, "capacity", - cause.message, + budgetLimit.message, { max_strings: input.max_strings, min_string_bytes: input.min_string_bytes, @@ -173,7 +178,7 @@ const v8CodeCacheFailurePrimary = ( { cause, retryable: false, - remediationAction: observationBudgetRemediation(cause, input), + remediationAction: observationBudgetRemediation(budgetLimit, input), }, ); const formatFailure = parseFailureAnalysisError(OPERATION, input.path, cause); diff --git a/src/wasm/WabtFailures.ts b/src/wasm/WabtFailures.ts index 7d6b84c4ce..9151f353bb 100644 --- a/src/wasm/WabtFailures.ts +++ b/src/wasm/WabtFailures.ts @@ -17,6 +17,7 @@ import { AnalysisResourceConstraintError, AnalysisCapabilityUnavailableError, } from "../domain/analysisErrorCore.js"; +import { hostRangeErrorKind } from "../domain/parseFailure.js"; import type { ProviderOutcome } from "../process/ProviderOutcome.js"; import type { ProviderFailureDialect } from "../providers/classifyProviderFailure.js"; import { @@ -177,7 +178,7 @@ const allocationRefused = (cause: unknown): boolean => { "ERR_MEMORY_ALLOCATION_FAILED", "ERR_BUFFER_TOO_LARGE", ].includes(String(physical.code))) || - (physical instanceof RangeError && + (hostRangeErrorKind(physical) === "host-capacity" && physical.message === "Array buffer allocation failed")) ); };