Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/native-investigation.md
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,13 @@ complete observation.
section lengths and unsupported non-PowerPC big-endian CPU declarations return
`unsupported_target` with the selected path and precise reason. Recovery
selects a well-formed or supported artifact and reopens that target.
An unreadable adopted-protocol sublist leaves its class, category or
`objc_protocol_records` entry available with partial decode status and
`protocol_list_unreadable`. Independently decoded metadata and any readable
protocol-name prefix remain available; an empty partial list does not
establish that its owner adopts no protocols. Existing list and record
limits remain in effect. Reader v3 uses a new analysis profile; snapshots
bound to reader v2 are incompatible and must be regenerated for this recovery.
The byte reader holds the whole selected file in memory, so it admits files up
to 64 MiB / 67108864 bytes by default. Select a larger budget with
`max_input_bytes` (CLI `--max-input-bytes`), at most 1 GiB / 1073741824
Expand Down
141 changes: 141 additions & 0 deletions src/native/AppleDispatchMetadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2478,3 +2478,144 @@ it.each([false, true])(
).toBe(unreadParent ? "partial" : "complete");
},
);

describe("Objective-C adopted protocol-list count limit", () => {
// Root class and metaclass that adopt the fixture protocol through the
// class_ro_t base protocol list at 0x700.
const rootClass = (protocolCount: bigint, category?: "partial-method") => {
const bytes = fixture(false, category);
bytes.writeUInt32LE(2, 12);
bytes.writeUInt32LE(5, 88);
bytes.writeUInt32LE(2, 0x300);
bytes.writeUInt32LE(3, 0x380);
bytes.writeBigUInt64LE(protocolCount, 0x700);
return bytes;
};
const classCoverage = (
result: ReturnType<typeof decodeAppleDispatchMetadata>,
) =>
result.coverage.find(
({ facet }) => facet === "objc_class_method_ivar_metadata",
);

it("retains the class and metaclass rows when the list exceeds its count limit", () => {
const result = decodeAppleDispatchMetadata(
rootClass(20001n),
100,
provenance,
);
expect(
result.objc_classes.map(({ name, is_meta_class, protocols, decode }) => ({
name,
is_meta_class,
protocols,
decode,
})),
).toEqual([
{
name: "Fixture",
is_meta_class: false,
protocols: [],
decode: { status: "partial", reason: "protocol_list_unreadable" },
},
// The metaclass has no adopted-protocol list of its own.
{
name: "Fixture",
is_meta_class: true,
protocols: [],
decode: { status: "decoded", reason: null },
},
]);
expect(result.objc_classes[0]?.properties).toHaveLength(3);
expect(result.objc_ivars).toHaveLength(1);
expect(result.objc_dispatch_implementations).toMatchObject([
{ class_name: "Fixture", selector: "performAction:" },
]);
expect(classCoverage(result)).toMatchObject({
status: "partial",
// The class row is retained as partial, so it is examined but not
// counted as fully decoded; the metaclass is fully decoded.
examined: 2,
decoded: 1,
});
});

it("retains the readable protocol prefix when a later list slot is unmapped", () => {
const bytes = rootClass(2n);
bytes.writeBigUInt64LE(base + 0x5000n, 0x710);
const result = decodeAppleDispatchMetadata(bytes, 100, provenance);
expect(result.objc_classes).toMatchObject([
{
name: "Fixture",
protocols: ["FixtureProtocol"],
decode: { status: "partial", reason: "protocol_list_unreadable" },
},
{ name: "Fixture", is_meta_class: true },
]);
expect(result.objc_protocol_records).toMatchObject([
{ name: "FixtureProtocol", decode: { status: "decoded" } },
]);
});

it("retains a category's own methods and properties after a protocol-list refusal", () => {
const bytes = rootClass(0n, "partial-method");
bytes.writeBigUInt64LE(base + 0x700n, 0xb98);
bytes.writeBigUInt64LE(base + 0x600n, 0xb68);
bytes.writeBigUInt64LE(base + 0xa00n, 0xb60);
bytes.writeBigUInt64LE(20001n, 0xa00);
const result = decodeAppleDispatchMetadata(bytes, 100, provenance);
expect(result.objc_categories).toMatchObject([
{
name: "FixtureCategory",
class_name: "Fixture",
instance_methods: ["performAction:"],
protocols: [],
decode: { status: "partial", reason: "protocol_list_unreadable" },
},
]);
expect(result.objc_categories[0]?.properties).toHaveLength(3);
expect(classCoverage(result)?.status).toBe("complete");
expect(
result.coverage.find(
({ facet }) => facet === "objc_properties_categories",
),
).toMatchObject({ status: "partial" });
});

it("retains an adopting protocol's own method declarations after a list refusal", () => {
const bytes = rootClass(1n);
bytes.writeBigUInt64LE(base + 0xa00n, 0x750);
bytes.writeBigUInt64LE(20001n, 0xa00);
const result = decodeAppleDispatchMetadata(bytes, 100, provenance);
expect(result.objc_protocol_records).toMatchObject([
{
name: "FixtureProtocol",
adopted_protocols: [],
methods: [{ selector: "requiredAction:" }],
optional_methods: [{ selector: "optionalAction:" }],
decode: { status: "partial", reason: "protocol_list_unreadable" },
},
]);
expect(result.objc_protocols).toMatchObject([
{ name: "FixtureProtocol", methods: [{ selector: "requiredAction:" }] },
]);
expect(result.objc_classes).toHaveLength(2);
expect(classCoverage(result)?.status).toBe("partial");
});

it.each([0n, 1n])(
"keeps complete class coverage for an in-limit count of %s",
(count) => {
const bytes = rootClass(count);
const result = decodeAppleDispatchMetadata(bytes, 100, provenance);
const reason = classCoverage(result)?.reason ?? "";
expect(reason).not.toContain("Protocol list of Fixture");
expect(result.objc_classes).toHaveLength(2);
for (const row of result.objc_classes)
expect(row.decode?.reason ?? "").not.toContain(
"protocol_list_unreadable",
);
expect(classCoverage(result)?.status).toBe("complete");
},
);
});
24 changes: 18 additions & 6 deletions src/native/AppleObjcCategories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { ObjcDispatchReaders } from "./AppleObjcDispatchFacets.js";
import { readObjcPropertiesOf } from "./AppleObjcProperties.js";
import { issue, type DecodeIssue } from "./AppleDispatchDecodeFacts.js";
import { BinaryFormatFailure } from "./metadata/BinaryContainer.js";
import type { ObjcProtocolListResult } from "./AppleObjcProtocols.js";

const CLASS_SYMBOL = /^_OBJC_(?:META)?CLASS_\$_(.+)$/u;

Expand Down Expand Up @@ -50,7 +51,11 @@ export const decodeObjcCategories = (input: {
category: string,
onTruncated?: () => void,
) => string[];
readonly protocols: (list: bigint, onTruncated?: () => void) => string[];
/** Read the protocol-list pointer field, retaining known names on failure. */
readonly protocolList: (
field: bigint,
onTruncated?: () => void,
) => ObjcProtocolListResult;
/** Name of a class defined in this image, from its class_ro_t. */
readonly localClassName: (address: bigint) => string;
}): number => {
Expand Down Expand Up @@ -119,10 +124,16 @@ export const decodeObjcCategories = (input: {
name,
onTruncated,
);
const protocols = input.protocols(
read.pointer(category + 32n),
onTruncated,
);
const protocols = input.protocolList(category + 32n, onTruncated);
if (!protocols.ok)
input.issues.push(
issue(
"objc_properties_categories",
"protocol_list_unreadable",
`Category ${name} adopted protocol list: ${protocols.error.reason}`,
`0x${(category + 32n).toString(16)}`,
),
);
const properties = readObjcPropertiesOf(
read,
read.pointer(category + 40n),
Expand All @@ -136,6 +147,7 @@ export const decodeObjcCategories = (input: {
);
const decodeReasons = [
...(className === null ? ["category_class_unresolved"] : []),
...(protocols.ok ? [] : ["protocol_list_unreadable"]),
...(budgetTruncated ? ["max_records_reached"] : []),
];
result.objc_categories.push({
Expand All @@ -149,7 +161,7 @@ export const decodeObjcCategories = (input: {
: "local",
instance_methods: instanceMethods,
class_methods: classMethods,
protocols,
protocols: protocols.ok ? protocols.value : protocols.error.names,
properties,
location: read.location(category),
decode:
Expand Down
12 changes: 10 additions & 2 deletions src/native/AppleObjcDispatchFacets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,12 +361,20 @@ export const decodeObjcDispatchFacets = (input: {
location: hex(read.pointer(ro + 64n)),
onTruncated,
});
const protocols = protocolReader.list(read.pointer(ro + 40n), onTruncated);
const adoptedProtocols = protocolReader.readList(ro + 40n, onTruncated);
if (!adoptedProtocols.ok)
failures.push(
`Protocol list of ${name}: ${adoptedProtocols.error.reason}`,
);
const protocols = adoptedProtocols.ok
? adoptedProtocols.value
: adoptedProtocols.error.names;
const classDecodeReasons = [
...(superclass.name === null && !root
? ["superclass_binding_unresolved"]
: []),
...(classListTruncated ? ["max_records_reached"] : []),
...(adoptedProtocols.ok ? [] : ["protocol_list_unreadable"]),
];
result.objc_classes.push({
name,
Expand Down Expand Up @@ -449,7 +457,7 @@ export const decodeObjcDispatchFacets = (input: {
issues: categoryIssues,
admit: () => budget.admit(),
methods,
protocols: (list, onTruncated) => protocolReader.list(list, onTruncated),
protocolList: protocolReader.readList,
localClassName: (address) => readLocalClass(read, address).name,
});
return {
Expand Down
63 changes: 46 additions & 17 deletions src/native/AppleObjcProtocols.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import { readObjcPropertyList } from "./AppleObjcProperties.js";
import type { ObjcSwiftMetadata } from "../domain/native/objcSwiftMetadata.js";
import type { SwiftNominalReaders } from "./AppleSwiftNominalTypes.js";
import { err, ok, type Result } from "../domain/result.js";

/** A failed list still owns the names read before its unreadable suffix. */
export type ObjcProtocolListResult = Result<
string[],
{
readonly _tag: "ProtocolListUnreadable";
readonly names: string[];
readonly reason: string;
}
>;

/** Read protocol lists and absolute protocol method declarations with cycle-safe identities. */
export const createObjcProtocolReader = (input: {
Expand All @@ -19,23 +30,36 @@ export const createObjcProtocolReader = (input: {
const { readers: read, result, failures } = input;
const visited = new Set<bigint>();
const name = (address: bigint) => read.string(read.pointer(address + 8n));
const list = (address: bigint, onTruncated?: () => void): string[] => {
if (address === 0n) return [];
// `count` is plain uintptr_t data, not a pointer fixup.
const count = read.u64(address);
if (count > 20000n)
throw new RangeError("Protocol list exceeds 20000 entries");
/** Own the list pointer field as well as the list's count and entries. */
const readList = (
field: bigint,
onTruncated?: () => void,
): ObjcProtocolListResult => {
const names: string[] = [];
for (let index = 0n; index < count; index++) {
if (!read.admit()) {
onTruncated?.();
break;
try {
const address = read.pointer(field);
if (address === 0n) return ok(names);
// `count` is plain uintptr_t data, not a pointer fixup.
const count = read.u64(address);
if (count > 20000n)
throw new RangeError("Protocol list exceeds 20000 entries");
for (let index = 0n; index < count; index++) {
if (!read.admit()) {
onTruncated?.();
break;
}
const protocol = read.pointer(address + 8n + index * 8n);
names.push(name(protocol));
record(protocol);
}
const protocol = read.pointer(address + 8n + index * 8n);
names.push(name(protocol));
record(protocol);
return ok(names);
} catch (cause: unknown) {
return err({
_tag: "ProtocolListUnreadable",
names,
reason: cause instanceof Error ? cause.message : String(cause),
});
}
return names;
};
const methods = (
address: bigint,
Expand Down Expand Up @@ -97,7 +121,11 @@ export const createObjcProtocolReader = (input: {
const onTruncated = () => {
budgetTruncated = true;
};
const adopted = list(read.pointer(address + 16n), onTruncated);
const adopted = readList(address + 16n, onTruncated);
if (!adopted.ok)
failures.push(
`Protocol 0x${address.toString(16)} adopted protocol list: ${adopted.error.reason}`,
);
if (protocolName.length === 0)
throw new TypeError("Objective-C protocol has an empty name");
const skipped: string[] = [];
Expand Down Expand Up @@ -165,6 +193,7 @@ export const createObjcProtocolReader = (input: {
);
const decodeReasons = [
...propertyReasons,
...(adopted.ok ? [] : ["protocol_list_unreadable"]),
...(skipped.length === 0 ? [] : ["protocol_method_selector_empty"]),
...(budgetTruncated ? ["max_records_reached"] : []),
];
Expand All @@ -176,7 +205,7 @@ export const createObjcProtocolReader = (input: {
});
result.objc_protocol_records.push({
name: protocolName,
adopted_protocols: adopted,
adopted_protocols: adopted.ok ? adopted.value : adopted.error.names,
methods: required,
optional_methods: optional,
location: read.location(address),
Expand All @@ -195,5 +224,5 @@ export const createObjcProtocolReader = (input: {
);
}
};
return { list, record };
return { readList, record };
};
4 changes: 2 additions & 2 deletions src/native/NativeMacOSProviderMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ export const APPLE_DISPATCH_METADATA_ANALYSIS_PROFILE = createAnalysisProfile(
{
id: NATIVE_MACOS_PROVIDER_IDENTITY.id,
name: NATIVE_MACOS_PROVIDER_IDENTITY.name,
version: "apple-metadata-reader-2",
version: "apple-metadata-reader-3",
},
{
operation: "inspect_native_dispatch_metadata",
reader: "apple-dispatch-metadata",
revision: 2,
revision: 3,
},
);
/** Operations whose target process can show UI, use the network and write files. */
Expand Down
Loading
Loading