Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

fix(native): count Objective-C coverage by class object

morluto9dee989a7953
diff --git a/docs/native-investigation.md b/docs/native-investigation.md
index 22cd35468..f17f5bbc2 100644
--- a/docs/native-investigation.md
+++ b/docs/native-investigation.md
@@ -427,6 +427,8 @@ complete observation.
start after the conformance header. Names unavailable in metadata remain null.
The `swift_class_vtable_descriptors` coverage counters count examined vtable
slots and successfully decoded slots, rather than type-section entries.
+ The `objc_class_method_ivar_metadata` counters count admitted class and
+ metaclass objects and successfully decoded objects.
Pointers are decoded through the image's fixups:
- `LC_DYLD_CHAINED_FIXUPS` rebases and binds, including the authenticated
arm64e formats;
diff --git a/src/native/AppleObjcClassCoverage.test.ts b/src/native/AppleObjcClassCoverage.test.ts
new file mode 100644
index 000000000..eb48d1376
--- /dev/null
+++ b/src/native/AppleObjcClassCoverage.test.ts
@@ -0,0 +1,142 @@
+import { describe, expect, it } from "vitest";
+import { objcRecordBudgetMacho } from "../../tests/fixtures/objcRecordBudgetMacho.js";
+import { decodeAppleDispatchMetadata } from "./AppleDispatchMetadata.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("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"),
+ });
+ });
+});
diff --git a/src/native/AppleObjcDispatchFacets.ts b/src/native/AppleObjcDispatchFacets.ts
index 5f507336e..61397dbd9 100644
--- a/src/native/AppleObjcDispatchFacets.ts
+++ b/src/native/AppleObjcDispatchFacets.ts
@@ -97,7 +97,7 @@ export const decodeObjcDispatchFacets = (input: {
} = input;
const failures: string[] = [];
const categoryIssues: DecodeIssue[] = [];
- let examined = 0;
+ let classObjectsExamined = 0;
const methods = (
list: bigint,
className: string,
@@ -273,6 +273,9 @@ export const decodeObjcDispatchFacets = (input: {
const visited = new Set<string>();
const readClass = (address: bigint, meta = false) => {
if (address === 0n || visited.has(hex(address)) || !budget.admit()) return;
+ // Coverage counts admitted class objects, including recursively visited
+ // metaclasses, in the same units as the decoded objc_classes rows.
+ classObjectsExamined++;
visited.add(hex(address));
const { ro, name } = readLocalClass(read, address);
const root = (read.u32(ro) & 2) !== 0;
@@ -419,7 +422,6 @@ export const decodeObjcDispatchFacets = (input: {
}
const field = section.address + BigInt(index * 8);
const pointer = readClassListSlot(field);
- if (pointer.ok || pointer.error === "unmapped") examined++;
if (!pointer.ok) {
if (pointer.error === "exhausted") {
objcClassListScanTruncated = true;
@@ -454,7 +456,7 @@ export const decodeObjcDispatchFacets = (input: {
return {
failures,
categoryIssues,
- examined,
+ examined: classObjectsExamined,
categoriesExamined,
objcListScanTruncated,
objcClassListScanTruncated,