mirror of
https://github.com/morluto/rea.git
synced 2026-11-16 03:33:25 +00:00
* refactor(macho): name load-command codes through the shared LC table * refactor(macho): name version-min and string commands through the shared tables * refactor(parsers): report XML, web artifact and Mach-O failures through ParseFailure (#10760) * refactor(parsers): report XML, web artifact and Mach-O failures through ParseFailure * refactor(xml): type XML parse failures as malformed or unsupported only * refactor(parsers): catch per-format document and source failure classes (#10794) * refactor(parsers): report apktool strings and XML plist source failures through ParseFailure * refactor(apktool): drop the unused ParseFailureError import * refactor(native): select ELF sections through the keyed owner * test(native): pin returned exports and versions for repeated version sections * refactor(nativeaot): read neither of repeated frozen object regions (#10790) * refactor(nativeaot): read neither of repeated frozen object regions through the keyed owner * refactor(parsers): retire every first-match section lookup (#10793) * fix(nativeaot): report an ambiguous frozen object region as partial (#10812) * refactor(native): select program headers and load commands through the keyed owner (#10827) * refactor(native): select the GOT base and dependency segments through the keyed owner (#10847) * refactor(native): select the GOT base and dependency segments through the keyed owner resolveGotBase selects the SHT_DYNAMIC section and the allocated .got.plt/.got sections with uniqueKeyed, so a repeat leaves the GOT base unknown instead of taking the first match; elfDependencies reads PT_LOAD segments through the same keyed program table as PT_DYNAMIC. A new keyed-first-match guard recognizes a find/findLast or filter-then-[0] whose predicate compares a key field, whatever the table is called; existing matches are baselined. The unwind fixture can declare PT_DYNAMIC headers and repeated .dynamic/.got.plt sections. * test(native): pin the PT_DYNAMIC path and destructured keyed lookups The keyed-first-match guard moves to scripts/verify/keyed-first-match.mjs with focused tests, and now recognizes a destructured key in any position and a destructured filter predicate. The single PT_DYNAMIC unwind test declares .dynamic as SHT_PROGBITS so only the program header supplies DT_PLTGOT, and asserts the resolved ranges. * fix(macho): refuse repeated chained-fixup and dyld-info tables (#10850) parseMachoLayout kept the last LC_DYLD_CHAINED_FIXUPS range and merged the rebase/bind streams of every LC_DYLD_INFO(_ONLY) command. A repeat now refuses the slice as malformed. NativeMachoInspection's chained import-name reader already declined repeated LC_DYLD_CHAINED_FIXUPS; that rule is now covered by a test. * refactor(parsers): retire the Mach-O, source-map, NDJSON and managed-graph failure classes (#10818) * refactor(parsers): retire the Mach-O, source-map, NDJSON and managed-graph failure classes * refactor(plist): report property-list and keyed-archive failures through ParseFailure (#10822) * refactor(plist): report property-list and keyed-archive failures through ParseFailure * refactor(kuna): report Kuna document failures through ParseFailure (#10824) * refactor(kuna): report Kuna document failures through ParseFailure * test(kuna): pin the limit and malformed facts of Kuna document failures * refactor(plist): report plist decode budgets through ParseFailure (#10842) * refactor(plist): report plist decode budgets through ParseFailure The plist preflight owner throws a ParseFailure limit (plist-decode-bytes) for its byte budget, the shared PlistNestingLimitError for nesting depth, and a malformed failure for a reference cycle, instead of a per-format PropertyListDecodeBudgetFailure whose meaning each caller re-mapped through a fail callback. The fail option, PropertyListDecodeBudgetFailure and interfaceBuilderDecodeBudgetFailure are removed; the parser-format-failure-class baseline reaches zero. * refactor(plist): report keyed-archive allowances as typed limit failures (#10844) keyedArchive.ts and decodeKeyedArchiveBytes throw decodeLimit parse failures (keyed-archive-bytes, -object-table-entries, -values-per-object, -reference-edges) and malformed parse failures instead of plain RangeError/TypeError. KeyedArchiveReader no longer classifies every RangeError as a limit: only a limit parse failure or a host stack/capacity limit is one. The unreachable 128-level scan nesting check is removed; projectPlistValue already refuses the archive first. * test(plist): expect the nesting limit as its ParseFailure carrier * fix(verify): count a filter index only on the filter's own result * refactor(macho): select slices through one owner with typed refusals * refactor(keyed): select FIT properties and certificate extensions through uniqueKeyed * refactor(keyed): key each FIT node once, pin repeated FIT properties and subjectKeyIdentifiers, and type the unknown Mach-O header as unsupported * refactor(plist): report plist and NIBArchive input faults through ParseFailure and defects through invariantViolation (#10895) * refactor(plist): report plist and NIBArchive input faults through ParseFailure and defects through invariantViolation * fix(plist): report a cut-short NIB header or plist length record as truncated * fix(native): project the selected Mach-O's parse failures through the shared owner nativeMachoSourcePrimary mapped every non-limit ParseFailureError to AnalysisOutputError, so a malformed or truncated selected Mach-O was reported as unreadable tool output and an unsupported one lost its category. It now uses parseFailureAnalysisError like the ELF, Go and other byte-format providers: malformed and truncated bytes are an invalid path, unsupported bytes an unsupported target, and a limit still falls through to its producer's budget.
564 lines
19 KiB
TypeScript
564 lines
19 KiB
TypeScript
import {
|
|
malformedInput,
|
|
parseFailureFromCause,
|
|
} from "../domain/parseFailure.js";
|
|
import { setImmediate as immediate } from "node:timers/promises";
|
|
import { createHash } from "node:crypto";
|
|
import { isSelectedRegularFileError } from "../filesystem/RegularFile.js";
|
|
import {
|
|
DEFAULT_DISPATCH_METADATA_INPUT_BYTES,
|
|
MAXIMUM_DISPATCH_METADATA_INPUT_BYTES,
|
|
objcSwiftMetadataSchema,
|
|
nativeDispatchMetadataResultSchema,
|
|
type DispatchMetadataInputAdmission,
|
|
type ObjcSwiftMetadata,
|
|
} from "../domain/native/objcSwiftMetadata.js";
|
|
import {
|
|
isMachOFormat,
|
|
type BinaryTarget,
|
|
} from "../domain/binaryTargetTypes.js";
|
|
import {
|
|
AnalysisArtifactChangedError,
|
|
AnalysisAccessDeniedError,
|
|
AnalysisCapabilityUnavailableError,
|
|
AnalysisResourceConstraintError,
|
|
AnalysisUnsupportedTargetError,
|
|
cancelledBy,
|
|
carriedReason,
|
|
} from "../domain/analysisErrorCore.js";
|
|
import {
|
|
ArtifactFileCleanupFailure,
|
|
artifactFileCancellationCause,
|
|
artifactFilePrimaryFacts,
|
|
type ArtifactFilePrimaryFacts,
|
|
bindArtifactFileCleanup,
|
|
withArtifactRegularFile,
|
|
} from "../artifacts/ArtifactFileOperation.js";
|
|
import { ArtifactReaderFailure } from "../artifacts/ArtifactReader.js";
|
|
import type { ArtifactResourceScope } from "../artifacts/ArtifactResourceScope.js";
|
|
import { AnalysisError } from "../domain/analysisErrorBase.js";
|
|
import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
|
|
import { ProviderAdapterError } from "../domain/providerAdapterError.js";
|
|
import { ProviderCleanupError } from "../domain/providerCleanupError.js";
|
|
import { APPLE_DISPATCH_METADATA_ANALYSIS_PROFILE } from "./NativeMacOSProviderMetadata.js";
|
|
import { NATIVE_MACOS_PROVIDER_IDENTITY as IDENTITY } from "./NativeMacosProviderIdentity.js";
|
|
import { readBoundedFileBytes } from "../process/BoundedFileBytes.js";
|
|
import { EvidenceIntegrityError } from "../domain/evidenceErrors.js";
|
|
import { pushDispatchCoverage } from "./AppleDispatchCoverage.js";
|
|
import { parsePointerFixupsSteps } from "./AppleMachoFixups.js";
|
|
import { BinaryFormatFailure } from "./metadata/BinaryContainer.js";
|
|
import { parseMachoLayout } from "./AppleMachoSelection.js";
|
|
import {
|
|
selectMachoSlice,
|
|
type AdmittedMachoSlice,
|
|
} from "./metadata/MachoSliceSelection.js";
|
|
import { decodeObjcDispatchFacets } from "./AppleObjcDispatchFacets.js";
|
|
import {
|
|
createSwiftRelativeReader,
|
|
decodeSwiftDispatchFacets,
|
|
type DispatchRecordBudget,
|
|
type SwiftRelativeTargetLocation,
|
|
} from "./AppleSwiftDispatchFacets.js";
|
|
import { createSwiftClassVtableDecoder } from "./AppleSwiftVtables.js";
|
|
import {
|
|
decodeSwiftNominalTypes,
|
|
createSwiftContextReader,
|
|
} from "./AppleSwiftNominalTypes.js";
|
|
import { inspectObjcClassImportBindings } from "./AppleObjcClassImportBindings.js";
|
|
|
|
/** Decode validated little-endian 64-bit Apple Objective-C metadata without loading target code. */
|
|
export function* decodeAppleDispatchMetadataSteps(
|
|
bytes: Buffer,
|
|
maxRecords: number,
|
|
provenance: { path: string; sha256: string },
|
|
architecture = "arm64",
|
|
objcClassImportAddresses?: readonly string[],
|
|
objcClassImportAddressesComplete = true,
|
|
admittedSlice?: AdmittedMachoSlice,
|
|
): Generator<void, ObjcSwiftMetadata, void> {
|
|
const selected = selectMachoSlice(bytes, {
|
|
mode: "admitted",
|
|
architecture,
|
|
slice: admittedSlice,
|
|
});
|
|
if (!selected.ok) {
|
|
if (selected.error.kind === "malformed")
|
|
throw malformedInput("macho", selected.error.message);
|
|
// Admission bound this architecture and slice to these exact bytes, so a
|
|
// refusal is an inconsistency inside REA, not the caller's selection.
|
|
throw new ProviderAdapterError(IDENTITY.id, OPERATION, {
|
|
diagnostics: {
|
|
target_path: provenance.path,
|
|
refusal: selected.error.kind,
|
|
reason: selected.error.message,
|
|
},
|
|
});
|
|
}
|
|
const layout = parseMachoLayout(bytes, selected.value);
|
|
const { segments, sections, offset } = layout;
|
|
const fixups = yield* parsePointerFixupsSteps(
|
|
bytes,
|
|
layout.fixups,
|
|
segments,
|
|
layout.baseAddress,
|
|
);
|
|
const objcClassImports = inspectObjcClassImportBindings({
|
|
bytes,
|
|
layout,
|
|
fixups,
|
|
addresses: objcClassImportAddresses,
|
|
addressesComplete: objcClassImportAddressesComplete,
|
|
maxRecords,
|
|
});
|
|
const u32 = (address: bigint) => bytes.readUInt32LE(offset(address, 4));
|
|
const decodePointer = (address: bigint) =>
|
|
fixups.decode(address, bytes.readBigUInt64LE(offset(address, 8)));
|
|
/** In-image target of a raw or rebased pointer; a bind is not an address. */
|
|
const pointer = (address: bigint) => {
|
|
const decodedPointer = decodePointer(address);
|
|
if (decodedPointer.kind === "rebase" || decodedPointer.kind === "raw")
|
|
return decodedPointer.target;
|
|
if (decodedPointer.kind === "bind") return 0n;
|
|
// Preserve the on-disk observation for structural Objective-C metadata.
|
|
// `raw` is not promoted to an authoritative fixup result: the shared
|
|
// pointer-fixup coverage remains partial, and consumers that need to
|
|
// distinguish null from an unresolved bind use resolvePointer directly.
|
|
if (decodedPointer.kind === "unknown") return decodedPointer.raw;
|
|
throw malformedInput("macho", decodedPointer.reason);
|
|
};
|
|
const bound = (address: bigint) => {
|
|
const decodedPointer = decodePointer(address);
|
|
return decodedPointer.kind === "bind" ? decodedPointer : undefined;
|
|
};
|
|
const location = (address: bigint) => ({
|
|
address: `0x${address.toString(16)}`,
|
|
file_offset: offset(address),
|
|
});
|
|
const targetLocation = (address: bigint): SwiftRelativeTargetLocation => {
|
|
let fileOffset: number | null;
|
|
try {
|
|
fileOffset = offset(address);
|
|
} catch (cause) {
|
|
if (parseFailureFromCause(cause) === undefined) throw cause;
|
|
fileOffset = null;
|
|
}
|
|
return { address: `0x${address.toString(16)}`, file_offset: fileOffset };
|
|
};
|
|
const evidence = (address: bigint, description: string) => [
|
|
{
|
|
kind: "binary_metadata" as const,
|
|
description,
|
|
location: location(address),
|
|
artifact_path: provenance.path,
|
|
artifact_sha256: provenance.sha256,
|
|
},
|
|
];
|
|
const string = (address: bigint) => {
|
|
const start = offset(address);
|
|
let end = start;
|
|
while (end < bytes.length && end - start < 4096 && bytes[end] !== 0) end++;
|
|
if (end === bytes.length || end - start >= 4096)
|
|
throw malformedInput(
|
|
"macho",
|
|
"Unterminated or oversized metadata string",
|
|
);
|
|
offset(address, end - start + 1);
|
|
// A metadata C string owns every code point, including an initial U+FEFF.
|
|
return new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(
|
|
bytes.subarray(start, end),
|
|
);
|
|
};
|
|
const result = objcSwiftMetadataSchema.parse({
|
|
db_save_result: null,
|
|
target_selection: {
|
|
kind: "macho-selected-image-v1",
|
|
container: layout.selectedImage.container,
|
|
offset: layout.selectedImage.offset,
|
|
byte_length: layout.selectedImage.byteLength,
|
|
cpu_type: layout.selectedImage.cpuType,
|
|
cpu_subtype: layout.selectedImage.cpuSubtype,
|
|
container_table_sha256: layout.selectedImage.containerTableSha256,
|
|
},
|
|
...(objcClassImportAddresses === undefined
|
|
? {}
|
|
: {
|
|
objc_class_import_bindings: objcClassImports.bindings,
|
|
objc_class_import_binding_coverage: {
|
|
facet: "objc_class_import_bindings",
|
|
...objcClassImports.coverage,
|
|
},
|
|
}),
|
|
});
|
|
let records = 0;
|
|
const budget: DispatchRecordBudget = {
|
|
truncated: false,
|
|
isFull: () => records >= maxRecords,
|
|
markTruncated: () => {
|
|
budget.truncated = true;
|
|
},
|
|
admit: () => {
|
|
if (records >= maxRecords) {
|
|
budget.truncated = true;
|
|
return false;
|
|
}
|
|
records++;
|
|
return true;
|
|
},
|
|
};
|
|
const readers = {
|
|
u32,
|
|
i32: (address: bigint) => bytes.readInt32LE(offset(address, 4)),
|
|
u64: (address: bigint) => bytes.readBigUInt64LE(offset(address, 8)),
|
|
pointer,
|
|
resolvePointer: decodePointer,
|
|
bound,
|
|
string,
|
|
location,
|
|
targetLocation,
|
|
evidence,
|
|
offset,
|
|
};
|
|
const {
|
|
failures,
|
|
categoryIssues,
|
|
examined,
|
|
categoriesExamined,
|
|
objcListScanTruncated,
|
|
objcClassListScanTruncated,
|
|
} = decodeObjcDispatchFacets({
|
|
bytes,
|
|
sections,
|
|
segments,
|
|
readers,
|
|
budget,
|
|
maxListSlots: maxRecords,
|
|
result,
|
|
});
|
|
const swiftRelative = createSwiftRelativeReader({
|
|
bytes,
|
|
readers,
|
|
budget,
|
|
result,
|
|
});
|
|
const contextReader = createSwiftContextReader({
|
|
u32,
|
|
string,
|
|
relative: swiftRelative,
|
|
});
|
|
const swift = decodeSwiftDispatchFacets({
|
|
contextReader,
|
|
sections,
|
|
segments,
|
|
readers,
|
|
relative: swiftRelative,
|
|
budget,
|
|
result,
|
|
});
|
|
const typeEntries: bigint[] = [];
|
|
for (const section of sections.filter(
|
|
({ name }) => name === "__swift5_types",
|
|
)) {
|
|
if (section.size % 4 !== 0)
|
|
throw new BinaryFormatFailure(
|
|
"malformed",
|
|
"Misaligned Swift type section",
|
|
);
|
|
for (let index = 0; index < section.size / 4 && index < maxRecords; index++)
|
|
typeEntries.push(section.address + BigInt(index * 4));
|
|
if (section.size / 4 > maxRecords) budget.truncated = true;
|
|
}
|
|
const admit = () => budget.admit();
|
|
const executable = (address: bigint) =>
|
|
segments.some(
|
|
(segment) =>
|
|
segment.executable &&
|
|
address >= segment.address &&
|
|
address < segment.address + segment.size,
|
|
);
|
|
const vtables = createSwiftClassVtableDecoder({
|
|
result,
|
|
readers: {
|
|
u32,
|
|
relative: swiftRelative,
|
|
location,
|
|
evidence,
|
|
admit,
|
|
executable,
|
|
},
|
|
});
|
|
const declarations = decodeSwiftNominalTypes({
|
|
contextReader,
|
|
entries: typeEntries,
|
|
result,
|
|
visitor: vtables.visitor,
|
|
readers: {
|
|
u32,
|
|
relative: swiftRelative,
|
|
string,
|
|
location,
|
|
evidence,
|
|
executable,
|
|
admit,
|
|
},
|
|
});
|
|
pushDispatchCoverage({
|
|
result,
|
|
failures,
|
|
categoryIssues,
|
|
examined,
|
|
categoriesExamined,
|
|
objcListScanTruncated,
|
|
objcClassListScanTruncated,
|
|
truncated: budget.truncated,
|
|
fixups,
|
|
declarations,
|
|
vtables: vtables.facts(),
|
|
swift,
|
|
});
|
|
return objcSwiftMetadataSchema.parse(result);
|
|
}
|
|
|
|
const OPERATION = "inspect_native_dispatch_metadata";
|
|
const MEBIBYTE = 1024 * 1024;
|
|
const bytesWithUnits = (bytes: number): string =>
|
|
Number.isInteger(bytes / (MEBIBYTE * 1024))
|
|
? `${String(bytes)} bytes (${String(bytes / (MEBIBYTE * 1024))} GiB)`
|
|
: Number.isInteger(bytes / MEBIBYTE)
|
|
? `${String(bytes)} bytes (${String(bytes / MEBIBYTE)} MiB)`
|
|
: `${String(bytes)} bytes`;
|
|
|
|
/** Refuse a file larger than the effective budget, naming how to raise it if possible. */
|
|
const inputBudgetExceeded = (
|
|
admission: Omit<DispatchMetadataInputAdmission, "retained_bytes">,
|
|
observedBytes: number | null,
|
|
): AnalysisResourceConstraintError => {
|
|
const budget = `${admission.source === "caller" ? "the selected max_input_bytes" : "the default input budget"} of ${bytesWithUnits(admission.maximum_bytes)}`;
|
|
const observed =
|
|
observedBytes === null ? "" : ` (${String(observedBytes)} bytes)`;
|
|
const remediationAction =
|
|
observedBytes === null
|
|
? "The file grew while it was read. Retry once the file is stable."
|
|
: observedBytes <= MAXIMUM_DISPATCH_METADATA_INPUT_BYTES
|
|
? `Retry with max_input_bytes (CLI --max-input-bytes) of at least ${String(observedBytes)}, at most ${bytesWithUnits(MAXIMUM_DISPATCH_METADATA_INPUT_BYTES)}; REA holds that many bytes in memory for the read.`
|
|
: `The file exceeds the largest selectable budget of ${bytesWithUnits(MAXIMUM_DISPATCH_METADATA_INPUT_BYTES)}; use a deep analysis provider for its Objective-C and Swift metadata.`;
|
|
return new AnalysisResourceConstraintError(
|
|
OPERATION,
|
|
"memory",
|
|
`Apple static metadata input${observed} exceeds ${budget}.`,
|
|
{
|
|
input_bytes: admission.maximum_bytes,
|
|
input_bytes_source: admission.source,
|
|
maximum_selectable_input_bytes: MAXIMUM_DISPATCH_METADATA_INPUT_BYTES,
|
|
...(observedBytes === null
|
|
? {}
|
|
: { observed_input_bytes: observedBytes }),
|
|
},
|
|
{ retryable: false, remediationAction },
|
|
);
|
|
};
|
|
|
|
/** Optional per-read selections beyond the record budget. */
|
|
export interface AppleDispatchMetadataOptions {
|
|
readonly signal?: AbortSignal | undefined;
|
|
readonly objcClassImportAddresses?: readonly string[] | undefined;
|
|
readonly objcClassImportAddressesComplete?: boolean | undefined;
|
|
/** Caller-selected input budget; the default applies when omitted. */
|
|
readonly maxInputBytes?: number | undefined;
|
|
}
|
|
|
|
/**
|
|
* Read digest-verified target bytes under the effective input budget and
|
|
* decode their static metadata. The caller's scope retains the target
|
|
* descriptor when its final close fails.
|
|
*/
|
|
export const inspectAppleDispatchMetadata = async (
|
|
target: BinaryTarget,
|
|
maxRecords: number,
|
|
resources: ArtifactResourceScope,
|
|
options: AppleDispatchMetadataOptions = {},
|
|
) => {
|
|
const { signal } = options;
|
|
if (target.kind !== "executable" || !isMachOFormat(target.format))
|
|
throw new TypeError("Apple dispatch metadata requires a Mach-O target");
|
|
const admission =
|
|
options.maxInputBytes === undefined
|
|
? {
|
|
maximum_bytes: DEFAULT_DISPATCH_METADATA_INPUT_BYTES,
|
|
source: "default" as const,
|
|
}
|
|
: { maximum_bytes: options.maxInputBytes, source: "caller" as const };
|
|
const maximumBytes = admission.maximum_bytes;
|
|
// Called only after the caller's signal aborted.
|
|
const cancelled = () => cancelledBy(OPERATION, carriedReason(signal?.reason));
|
|
const { bytes, digest } = await withArtifactRegularFile(
|
|
target.path,
|
|
resources,
|
|
signal,
|
|
async ({ handle, initial: before }) => {
|
|
if (before.size > maximumBytes)
|
|
throw inputBudgetExceeded(admission, before.size);
|
|
if (signal?.aborted) throw cancelled();
|
|
const read = await readBoundedFileBytes(handle, maximumBytes, {
|
|
signal,
|
|
cancellationFailure: () => cancelled(),
|
|
});
|
|
if (read === undefined) throw inputBudgetExceeded(admission, null);
|
|
const sha256 = createHash("sha256").update(read).digest("hex");
|
|
if (sha256 !== target.sha256)
|
|
throw new EvidenceIntegrityError(
|
|
"Apple metadata target digest changed after session binding",
|
|
);
|
|
return { bytes: read, digest: sha256 };
|
|
},
|
|
"reject",
|
|
);
|
|
const steps = decodeAppleDispatchMetadataSteps(
|
|
bytes,
|
|
maxRecords,
|
|
{ path: target.path, sha256: digest },
|
|
target.architecture,
|
|
options.objcClassImportAddresses,
|
|
options.objcClassImportAddressesComplete ?? true,
|
|
target.format === "mach-o-universal" ? target.universalSlice : undefined,
|
|
);
|
|
let next = steps.next();
|
|
while (!next.done) {
|
|
await immediate();
|
|
if (signal?.aborted) throw cancelled();
|
|
next = steps.next();
|
|
}
|
|
return nativeDispatchMetadataResultSchema.parse({
|
|
target_sha256: target.sha256,
|
|
target_architecture: target.architecture,
|
|
provider: APPLE_DISPATCH_METADATA_ANALYSIS_PROFILE.provider,
|
|
analysis_profile_digest: APPLE_DISPATCH_METADATA_ANALYSIS_PROFILE.digest,
|
|
input_admission: { ...admission, retained_bytes: bytes.length },
|
|
result: next.value,
|
|
});
|
|
};
|
|
|
|
/** Translate the owned target read, keeping a failed close beside its physical outcome. */
|
|
export const appleDispatchMetadataFailure = (
|
|
cause: unknown,
|
|
path: string,
|
|
): AnalysisError => {
|
|
if (cause instanceof ArtifactReaderFailure && cause.cleanup !== undefined) {
|
|
const physical =
|
|
cause instanceof ArtifactFileCleanupFailure
|
|
? cause.fileCleanupFailure
|
|
: undefined;
|
|
const previous =
|
|
physical?.outcome.kind === "failed"
|
|
? appleDispatchMetadataPrimaryFailure(
|
|
artifactFilePrimaryFacts(cause),
|
|
path,
|
|
)
|
|
: physical?.completionCancellation === undefined
|
|
? undefined
|
|
: cancelledBy(
|
|
OPERATION,
|
|
carriedReason(physical.completionCancellation.reason),
|
|
);
|
|
const failure = new ProviderCleanupError(
|
|
IDENTITY.id,
|
|
cause.cleanup.resources,
|
|
{
|
|
reason: cause.cleanup.reason,
|
|
previous_error:
|
|
previous === undefined ? null : projectAnalysisError(previous),
|
|
},
|
|
{ operation: OPERATION, cause, cleanupPrimary: previous },
|
|
);
|
|
bindArtifactFileCleanup(cause, failure);
|
|
return failure;
|
|
}
|
|
return appleDispatchMetadataPrimaryFailure(
|
|
artifactFilePrimaryFacts(cause),
|
|
path,
|
|
);
|
|
};
|
|
|
|
const appleDispatchMetadataPrimaryFailure = (
|
|
{ cause, origin }: ArtifactFilePrimaryFacts,
|
|
path: string,
|
|
): AnalysisError => {
|
|
if (origin === "owned-or-classified") {
|
|
if (cause instanceof AnalysisError) return cause;
|
|
// A recognized Mach-O class this reader cannot decode still holds metadata
|
|
// another reader may recover.
|
|
if (cause instanceof BinaryFormatFailure && cause.undecodedContent)
|
|
return new AnalysisCapabilityUnavailableError(
|
|
IDENTITY.id,
|
|
OPERATION,
|
|
cause.message,
|
|
{ cause },
|
|
);
|
|
if (cause instanceof BinaryFormatFailure)
|
|
return new AnalysisUnsupportedTargetError(
|
|
OPERATION,
|
|
path,
|
|
cause.message,
|
|
{
|
|
cause,
|
|
remediationAction:
|
|
cause.kind !== "unsupported"
|
|
? "Select a Mach-O artifact with well-formed metadata sections, reopen the target, then retry."
|
|
: "Select a supported little-endian 64-bit Mach-O artifact, reopen the target, then retry.",
|
|
},
|
|
);
|
|
if (cause instanceof ArtifactReaderFailure && cause.reason === "cancelled")
|
|
return cancelledBy(
|
|
OPERATION,
|
|
carriedReason(artifactFileCancellationCause(cause)),
|
|
);
|
|
if (
|
|
cause instanceof ArtifactReaderFailure &&
|
|
cause.reason === "unavailable"
|
|
)
|
|
return new AnalysisCapabilityUnavailableError(
|
|
IDENTITY.id,
|
|
OPERATION,
|
|
cause.message,
|
|
{ cause },
|
|
);
|
|
if (cause instanceof ArtifactReaderFailure && cause.reason === "integrity")
|
|
return new AnalysisArtifactChangedError(OPERATION, path, cause.message, {
|
|
cause,
|
|
});
|
|
if (cause instanceof ArtifactReaderFailure && cause.reason === "path")
|
|
return new AnalysisArtifactChangedError(OPERATION, path, cause.message, {
|
|
cause,
|
|
});
|
|
}
|
|
const readCause =
|
|
origin === "owned-or-classified" &&
|
|
cause instanceof ArtifactReaderFailure &&
|
|
cause.reason === "io"
|
|
? cause.cause
|
|
: cause;
|
|
if (
|
|
readCause instanceof Error &&
|
|
"code" in readCause &&
|
|
(readCause.code === "EACCES" || readCause.code === "EPERM")
|
|
)
|
|
return new AnalysisAccessDeniedError(OPERATION, path, readCause.code, {
|
|
cause,
|
|
});
|
|
// This path already belongs to the bound target; loss of readability is a
|
|
// change to that artifact, rather than a new caller input-selection error.
|
|
if (isSelectedRegularFileError(readCause))
|
|
return new AnalysisArtifactChangedError(
|
|
OPERATION,
|
|
path,
|
|
`Bound metadata target could not be read (${readCause.code}): ${path}`,
|
|
{ cause },
|
|
);
|
|
return new ProviderAdapterError(IDENTITY.id, OPERATION, {
|
|
cause,
|
|
diagnostics: {
|
|
target_path: path,
|
|
reason: cause instanceof Error ? cause.message : String(cause),
|
|
},
|
|
});
|
|
};
|