Files
reverse-engineering-anything/src/native/AppleObjcClassCoverage.test.ts
T
morluto 89640a446a fix(parsers): stop reading every RangeError as a format failure (#10602)
* refactor(parsers): report format failures through one ParseFailure algebra

Add ParseFailure (malformed, truncated, unsupported, limit, with format,
offset and field) and its one carrier, ParseFailureError, in the domain.
BinaryReader now names truncation and the field it read, and its native
format and resource failures are ParseFailureError subclasses.

Providers projected a format failure with their own instanceof ladder,
and some dropped the selected path from the invalid_format message.
parseFailureAnalysisError now owns that projection for the native,
Go, devicetree, V8, Windows Shortcut and Electron fuse providers.
The V8 code cache, Windows Shortcut and Electron fuse format classes
are removed in favour of the algebra.

The architecture guard counts raw Buffer and DataView reads, plain
Error throws in parser folders and plain JSON.parse of parser input
against baselines that can only shrink.

* fix(parsers): stop reading every RangeError as a format failure

Mach-O, Swift and Objective-C metadata parsers threw RangeError for a
truncated or malformed record, so their catch sites treated any
RangeError as a partial decode: a call stack overflow or a host string
limit became an "unresolved" metadata reason, and the IL2CPP Mach-O
path reported it as a malformed container.

Those parsers and the CIL method body reader now throw ParseFailureError.
hostRangeErrorKind is the one interpreter of a host RangeError (stack
overflow, host capacity, raw out-of-bounds read), and
parseFailureFromCause admits only a parse failure or a raw
out-of-bounds read as evidence about the input. Fixup stream catch-alls
rethrow host limits. The architecture guard counts the remaining direct
instanceof RangeError checks.

* fix(parsers): preserve Apple metadata failure ownership

* test(architecture): shrink parser failure baseline

* fix: preserve uncertainty for host range errors
2026-11-15 10:03:26 +08:00

164 lines
5.0 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { objcRecordBudgetMacho } from "../../tests/fixtures/objcRecordBudgetMacho.js";
import { decodeAppleDispatchMetadata } from "./AppleDispatchMetadata.fixture.js";
const base = 0x100000000n;
const provenance = {
path: "/fixtures/class-coverage.macho",
sha256: "a".repeat(64),
};
const classWithMetaclass = (options: { duplicateRoot?: boolean } = {}) => {
const bytes = objcRecordBudgetMacho("class");
const pointer = (offset: number, target: number) =>
bytes.writeBigUInt64LE(base + BigInt(target), offset);
// Link the valid root class to a separate, equally valid metaclass object.
pointer(0x200, 0xa00);
pointer(0xa20, 0xa40);
bytes.writeUInt32LE(2, 0xa40);
pointer(0xa58, 0xb00);
bytes.write("FixtureClass\0", 0xb00);
if (options.duplicateRoot) {
bytes.writeBigUInt64LE(16n, 144);
pointer(0x188, 0x200);
}
return bytes;
};
describe("Objective-C class-object coverage units", () => {
it("propagates a host stack limit while decoding a deep protocol chain", () => {
const count = 5_000;
const bytes = Buffer.alloc(0x1000 + count * 128);
objcRecordBudgetMacho("protocol").copy(bytes);
bytes.writeBigUInt64LE(BigInt(bytes.length), 64);
bytes.writeBigUInt64LE(BigInt(bytes.length), 80);
bytes.writeBigUInt64LE(base + 0x1000n, 0x6c0);
for (let index = 0; index < count; index++) {
const at = 0x1000 + index * 128;
bytes.writeBigUInt64LE(base + 0x980n, at + 8);
bytes.writeUInt32LE(72, at + 64);
if (index + 1 < count) {
bytes.writeBigUInt64LE(base + BigInt(at + 96), at + 16);
bytes.writeBigUInt64LE(1n, at + 96);
bytes.writeBigUInt64LE(base + BigInt(at + 128), at + 104);
}
}
expect(() =>
decodeAppleDispatchMetadata(bytes, count * 3, provenance),
).toThrow(RangeError);
});
it("counts both admitted class and metaclass records", () => {
const result = decodeAppleDispatchMetadata(
classWithMetaclass(),
100,
provenance,
);
expect(result.objc_classes).toMatchObject([
{
name: "FixtureClass",
is_meta_class: false,
decode: { status: "decoded" },
},
{
name: "FixtureClass",
is_meta_class: true,
decode: { status: "decoded" },
},
]);
expect(
result.coverage.find(
({ facet }) => facet === "objc_class_method_ivar_metadata",
),
).toMatchObject({ status: "complete", examined: 2, decoded: 2 });
});
it("counts a duplicate class-list identity once while retaining the two class objects", () => {
const result = decodeAppleDispatchMetadata(
classWithMetaclass({ duplicateRoot: true }),
100,
provenance,
);
expect(result.objc_classes).toHaveLength(2);
expect(
result.coverage.find(
({ facet }) => facet === "objc_class_method_ivar_metadata",
),
).toMatchObject({ status: "complete", examined: 2, decoded: 2 });
});
it("keeps an admitted malformed class in examined and preserves its failure", () => {
const bytes = classWithMetaclass();
bytes.writeUInt8(0, 0x900);
const result = decodeAppleDispatchMetadata(bytes, 100, provenance);
expect(result.objc_classes).toEqual([]);
expect(
result.coverage.find(
({ facet }) => facet === "objc_class_method_ivar_metadata",
),
).toMatchObject({
status: "partial",
examined: 1,
decoded: 0,
reason: expect.stringContaining("empty name"),
});
});
it("preserves unknown-root partial facts for both admitted objects", () => {
const bytes = classWithMetaclass();
bytes.writeUInt32LE(0, 0x300);
bytes.writeUInt32LE(0, 0xa40);
const result = decodeAppleDispatchMetadata(bytes, 100, provenance);
expect(result.objc_classes).toMatchObject([
{
name: "FixtureClass",
is_meta_class: false,
super_class: null,
decode: { status: "partial", reason: "superclass_binding_unresolved" },
},
{
name: "FixtureClass",
is_meta_class: true,
super_class: null,
decode: { status: "partial", reason: "superclass_binding_unresolved" },
},
]);
expect(
result.coverage.find(
({ facet }) => facet === "objc_class_method_ivar_metadata",
),
).toMatchObject({ status: "partial", examined: 2, decoded: 0 });
});
it("keeps object counts and truncation separate when the metaclass exceeds the budget", () => {
const result = decodeAppleDispatchMetadata(
classWithMetaclass(),
2,
provenance,
);
expect(result.objc_classes).toMatchObject([
{
name: "FixtureClass",
is_meta_class: false,
decode: { status: "decoded" },
},
]);
expect(
result.coverage.find(
({ facet }) => facet === "objc_class_method_ivar_metadata",
),
).toMatchObject({
status: "partial",
examined: 1,
decoded: 1,
reason: expect.stringContaining("max_records_reached"),
});
});
});