diff --git a/docs/il2cpp-metadata.md b/docs/il2cpp-metadata.md index 1210ee1914..e21224b5c3 100644 --- a/docs/il2cpp-metadata.md +++ b/docs/il2cpp-metadata.md @@ -489,10 +489,13 @@ Each category says which selected file is at fault: table entry, is reported the same way with `details.format` `macho`. - **conflict**: both files parsed but do not agree. They may come from different builds, either file may be damaged, or the binary's registration - layout may be outside coverage; nothing establishes which. The refusal is an - `invalid_request` with an `invalid_value` issue for each of `path` and - `binary_path` (`--binary` in the CLI) and no `inspect_input_contract` - recovery. Neither file is called malformed. + layout may be outside coverage; nothing establishes which. The refusal has + code `input_conflict`, `details.failure_kind` `conflict`, `retry` + `after_change` and no `recovery`. `details.inputs` lists both files as + `{ field, path }`: `path` for the metadata and `binary_path` for the binary + (`binary` in the CLI, after its `--binary` option). `details.path` names + the metadata. Neither file is called malformed; select a pair from the same + build. See [target failures](reference/target-failures.md). A token that is not a MethodDef row, or a row reused within one image, is the metadata's own fault, so linking refuses it as malformed metadata with diff --git a/src/cli/projectCliAnalysisResult.ts b/src/cli/projectCliAnalysisResult.ts index 7126e29948..01867a5e62 100644 --- a/src/cli/projectCliAnalysisResult.ts +++ b/src/cli/projectCliAnalysisResult.ts @@ -80,19 +80,26 @@ export const projectCliInputFields = ( ? [{ name: binding.operation, flags: binding.flags }] : orderedOperations(binding.operations, error.details.operation); const issues = renamedIssues(error, error.details, binding); + const inputs = renamedTargetInputs(error.details, binding); const limit = flaggedLimit(error.details.limit, operations); const relatedFlags = binding.operations === undefined && error.details.operation !== binding.operation ? undefined : relatedCliFlagsFromOperations(error.details, operations); - if (issues === undefined && limit === undefined && relatedFlags === undefined) + if ( + issues === undefined && + inputs === undefined && + limit === undefined && + relatedFlags === undefined + ) return output; return { ...error, details: { ...error.details, ...(issues === undefined ? {} : { issues }), + ...(inputs === undefined ? {} : { inputs }), ...(limit === undefined ? {} : { limit }), ...(relatedFlags === undefined ? {} @@ -260,6 +267,29 @@ const renamedIssues = ( return changed ? issues : undefined; }; +/** + * A target conflict's inputs, each named by the request field that selected + * it, take this command's CLI spelling of that field. + */ +const renamedTargetInputs = ( + details: Record, + binding: CliInputFieldBinding, +): JsonValue[] | undefined => { + if (details.operation !== binding.operation || !Array.isArray(details.inputs)) + return undefined; + let changed = false; + const inputs = details.inputs.map((input) => { + if (!isObject(input) || typeof input.field !== "string") return input; + const alias = Object.hasOwn(binding.fields, input.field) + ? binding.fields[input.field] + : undefined; + if (alias === undefined || alias === input.field) return input; + changed = true; + return { ...input, field: alias }; + }); + return changed ? inputs : undefined; +}; + /** * The request parameter a limit names, when a caller selects it per call: a * parameter control, or a registered setting a request argument can also diff --git a/src/domain/managed/managedReaderFailure.test.ts b/src/domain/managed/managedReaderFailure.test.ts index ba5ded54d6..8033f80089 100644 --- a/src/domain/managed/managedReaderFailure.test.ts +++ b/src/domain/managed/managedReaderFailure.test.ts @@ -1,9 +1,6 @@ import { describe, expect, it } from "vitest"; -import { - AnalysisTargetFailureError, - AnalysisUnsupportedTargetError, -} from "../analysisErrorCore.js"; +import { AnalysisTargetFailureError } from "../analysisErrorCore.js"; import { projectAnalysisError } from "../analysisErrorProjection.js"; import type { ManagedParseIssue } from "./managedArtifact.js"; import { @@ -65,8 +62,16 @@ describe("managedInspectionTargetFailure", () => { PATH, managedFailure(code, "metadata.tables", "a newer layout"), ); - expect(failure).toBeInstanceOf(AnalysisUnsupportedTargetError); - expect(failure).toMatchObject({ path: PATH, reason: "a newer layout" }); + expect(failure).toBeInstanceOf(AnalysisTargetFailureError); + expect(failure).toMatchObject({ + path: PATH, + failure: { + kind: "unsupported", + format: "pe-cli", + field: "metadata.tables", + detail: "a newer layout", + }, + }); }, ); @@ -88,7 +93,7 @@ describe("managedInspectionTargetFailure", () => { managedFailure(code, "metadata.tables", "detail"), ); // Only a broken rule is malformed in both the inventory and a refusal. - expect(refusal instanceof AnalysisTargetFailureError).toBe( + expect(refusal?.failure.kind === "malformed").toBe( status === "malformed", ); }, diff --git a/src/domain/managed/managedReaderFailure.ts b/src/domain/managed/managedReaderFailure.ts index 4237d7ead0..60ee6197b9 100644 --- a/src/domain/managed/managedReaderFailure.ts +++ b/src/domain/managed/managedReaderFailure.ts @@ -1,7 +1,5 @@ -import { - AnalysisTargetFailureError, - AnalysisUnsupportedTargetError, -} from "../analysisErrorCore.js"; +import { AnalysisTargetFailureError } from "../analysisErrorCore.js"; +import type { TargetFailureOf } from "../targetFailure.js"; import type { ManagedParseIssue } from "./managedArtifact.js"; /** Header facts admitted before an unsupported table layout was identified. */ @@ -36,20 +34,47 @@ export class ManagedHeapAmbiguityFailure extends ManagedReaderFailure {} export const isManagedCoverageGap = (code: ManagedParseIssue["code"]) => code === "unsupported-metadata" || code === "unsupported-signature"; +/** The ParseFailure format of a managed PE/CLI image and its ECMA-335 metadata. */ +export const MANAGED_PE_CLI_FORMAT = "pe-cli"; + /** - * Metadata status after a reader stopped. A budget or a construct outside - * coverage leaves metadata unknown (`partial`); only a broken rule makes it - * `malformed`. + * The target failure a managed reader's issue establishes: the one place + * that interprets an issue code. A construct outside decoder coverage is + * `unsupported`, so a newer valid file is never called corrupt; every other + * code except `resource-limit` names a rule the bytes broke. A resource limit + * names a budget only its producer knows, so it returns `undefined`. + */ +export const managedIssueTargetFailure = ({ + code, + scope, + offset, + detail, +}: ManagedParseIssue): + | TargetFailureOf<"malformed" | "unsupported"> + | undefined => { + if (code === "resource-limit") return undefined; + const facts = { + format: MANAGED_PE_CLI_FORMAT, + detail, + field: scope, + ...(offset === null ? {} : { offset }), + }; + return isManagedCoverageGap(code) + ? { kind: "unsupported", ...facts, undecodedContent: true } + : { kind: "malformed", ...facts }; +}; + +/** + * Metadata status after a reader stopped, derived from the issue's target + * failure: only bytes that break a rule make metadata `malformed`; a budget + * or a construct outside coverage leaves it unknown (`partial`). */ export const managedMetadataFailureStatus = ( issue: ManagedParseIssue, ): "partial" | "malformed" => - issue.code === "resource-limit" || isManagedCoverageGap(issue.code) - ? "partial" - : "malformed"; - -/** The ParseFailure format of a managed PE/CLI image and its ECMA-335 metadata. */ -export const MANAGED_PE_CLI_FORMAT = "pe-cli"; + managedIssueTargetFailure(issue)?.kind === "malformed" + ? "malformed" + : "partial"; /** * The selected file's refusal when a managed reader stopped on its bytes. @@ -64,26 +89,12 @@ export const managedInspectionTargetFailure = ( operation: string, path: string, cause: unknown, -): AnalysisTargetFailureError | AnalysisUnsupportedTargetError | undefined => { +): AnalysisTargetFailureError | undefined => { if (!(cause instanceof ManagedReaderFailure)) return undefined; - const { code, scope, offset, detail } = cause.issue; - if (code === "resource-limit") return undefined; - if (isManagedCoverageGap(code)) - return new AnalysisUnsupportedTargetError(operation, path, detail, { - cause, - }); - return new AnalysisTargetFailureError( - operation, - path, - { - kind: "malformed", - format: MANAGED_PE_CLI_FORMAT, - detail, - field: scope, - ...(offset === null ? {} : { offset }), - }, - { cause }, - ); + const failure = managedIssueTargetFailure(cause.issue); + return failure === undefined + ? undefined + : new AnalysisTargetFailureError(operation, path, failure, { cause }); }; /** Construct a typed managed parse failure at an exact byte location. */ diff --git a/src/il2cpp/Il2cppMetadataProvider.ts b/src/il2cpp/Il2cppMetadataProvider.ts index ff723f445a..8f26d9436d 100644 --- a/src/il2cpp/Il2cppMetadataProvider.ts +++ b/src/il2cpp/Il2cppMetadataProvider.ts @@ -10,7 +10,10 @@ import { parseFailureAnalysisError, parseLimitConstraint, } from "../domain/parseFailureAnalysisError.js"; -import { Il2cppMetadataFormatFailure } from "./Il2cppMetadataFormat.js"; +import { + IL2CPP_METADATA_FORMAT, + Il2cppMetadataFormatFailure, +} from "./Il2cppMetadataFormat.js"; import { inspectIl2cppMetadataV108 } from "./Il2cppMetadataV108.js"; import type { AnalysisPartialObservation } from "../domain/analysisErrorBase.js"; import { jsonValueSchema } from "../domain/jsonValue.js"; @@ -42,10 +45,9 @@ import { import { jsonValueByteLength } from "../domain/jsonValueByteLength.js"; import { AnalysisCapabilityUnavailableError, - AnalysisInputError, + AnalysisTargetFailureError, AnalysisOutputError, AnalysisResourceConstraintError, - AnalysisUnsupportedTargetError, cancelledBy, } from "../domain/analysisErrorCore.js"; import { @@ -89,27 +91,20 @@ const linkConflict = ( binaryPath: string, metadataPath: string, cause: Il2cppBinaryFormatFailure, -): AnalysisInputError => - new AnalysisInputError( +): AnalysisTargetFailureError => + new AnalysisTargetFailureError( OPERATION, + metadataPath, { - cause, - conformsToInputContract: true, - remediationAction: - "Select the native binary and global-metadata.dat from the same build. If they are from the same build, either file may be damaged or the binary's registration layout may be outside coverage. Without the binary, the metadata alone can still be read.", + kind: "conflict", + format: IL2CPP_BINARY_FORMAT, + detail: `Selected metadata does not link to the selected binary: ${cause.message}`, + inputs: [ + { field: "path", path: metadataPath }, + { field: "binary_path", path: binaryPath }, + ], }, - [ - { - path: ["path"], - reason: "invalid_value", - message: `Selected metadata ${metadataPath} does not link to the selected binary: ${cause.message}`, - }, - { - path: ["binary_path"], - reason: "invalid_value", - message: `Selected binary ${binaryPath} does not link to the metadata: ${cause.message}`, - }, - ], + { cause }, ); const failureForPrimary = ( @@ -522,10 +517,17 @@ export class Il2cppMetadataProvider implements Il2cppMetadataPort { ); assertArtifactReadActive(options?.signal); if (v108 !== null) { - const failure = new AnalysisUnsupportedTargetError( + const failure = new AnalysisTargetFailureError( OPERATION, input.path, - "unsupported-version: metadata version 108 has a partial header and string observation; type, method and binary layouts are not decoded", + { + kind: "unsupported", + format: IL2CPP_METADATA_FORMAT, + detail: + "unsupported-version: metadata version 108 has a partial header and string observation; type, method and binary layouts are not decoded", + field: "version", + undecodedContent: true, + }, ); failure.partialObservation = metadataSourceEvidence( artifact, diff --git a/tests/acceptance/il2cppMetadataCli.test.ts b/tests/acceptance/il2cppMetadataCli.test.ts index 9a368d81f4..cb0152c6a5 100644 --- a/tests/acceptance/il2cppMetadataCli.test.ts +++ b/tests/acceptance/il2cppMetadataCli.test.ts @@ -1104,23 +1104,15 @@ cliTest( const pairConflict = (reason: string) => (path: string, binaryPath: string, binaryField: string) => ({ - code: "invalid_request", + code: "input_conflict", details: { - issues: [ - { - path: ["path"], - reason: "invalid_value", - message: expect.stringContaining( - `Selected metadata ${path} does not link to the selected binary: ${reason}:`, - ), - }, - { - path: [binaryField], - reason: "invalid_value", - message: expect.stringContaining( - `Selected binary ${binaryPath} does not link to the metadata: ${reason}:`, - ), - }, + failure_kind: "conflict", + path, + reason: expect.stringContaining(`${reason}:`), + // Both inputs, each by this adapter's spelling of its field. + inputs: [ + { field: "path", path }, + { field: binaryField, path: binaryPath }, ], }, }); diff --git a/tests/boundary/il2cpp/il2cppMetadataProvider.test.ts b/tests/boundary/il2cpp/il2cppMetadataProvider.test.ts index a1bf904550..00a10c06e8 100644 --- a/tests/boundary/il2cpp/il2cppMetadataProvider.test.ts +++ b/tests/boundary/il2cpp/il2cppMetadataProvider.test.ts @@ -144,7 +144,10 @@ it("accepts padding after the assumed v108 header", async () => { const result = await inspectMetadata(path); expect(result.ok).toBe(false); if (result.ok) return; - expect(result.error).toBeInstanceOf(AnalysisUnsupportedTargetError); + expect(result.error).toMatchObject({ + _tag: "AnalysisTargetFailureError", + failure: { kind: "unsupported", format: "il2cpp-metadata" }, + }); expect(result.error.partialObservation).toMatchObject({ normalized_result: { version_108_partial_inventory: { @@ -970,32 +973,21 @@ it("retains both observed file identities and the selected architecture when nat if (result.ok) throw new Error("Unresolved registration accepted"); // Both files parse; neither is called malformed. The conflict lists each // selecting field and keeps each file's identity. - expect(result.error).toMatchObject({ - _tag: "AnalysisInputError", - conformsToInputContract: true, - issues: [ - { - path: ["path"], - reason: "invalid_value", - message: expect.stringContaining( - `Selected metadata ${path} does not link to the selected binary: code-registration-not-found:`, - ), - }, - { - path: ["binary_path"], - reason: "invalid_value", - message: expect.stringContaining( - `Selected binary ${binary_path} does not link to the metadata: code-registration-not-found:`, - ), - }, - ], - }); expect(projectAnalysisError(result.error)).toMatchObject({ - code: "invalid_request", + code: "input_conflict", retryable: false, remediation: { action: expect.stringContaining("from the same build"), }, + details: { + failure_kind: "conflict", + path, + reason: expect.stringContaining("code-registration-not-found:"), + inputs: [ + { field: "path", path }, + { field: "binary_path", path: binary_path }, + ], + }, }); expect(projectAnalysisError(result.error)).not.toHaveProperty("recovery"); expect(result.error.partialObservation).toMatchObject({