Skip to content

armgit / rea

Public Imported from GitHub
0

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

fix(objc): isolate empty protocol selectors and category class names (#4549)

Oke Iyanuoluwa Enochd5be402fb3db
diff --git a/src/native/AppleDispatchMetadata.test.ts b/src/native/AppleDispatchMetadata.test.ts
index 5e532e8d6..b267b8963 100644
--- a/src/native/AppleDispatchMetadata.test.ts
+++ b/src/native/AppleDispatchMetadata.test.ts
@@ -604,10 +604,19 @@ describe("Objective-C malformed identity recovery", () => {
]);
expect(result.objc_protocols.map(({ name }) => name)).toEqual([
"AdoptedProtocol",
+ "FixtureProtocol",
]);
expect(result.objc_protocol_records.map(({ name }) => name)).toEqual([
"AdoptedProtocol",
+ "FixtureProtocol",
]);
+ expect(result.objc_protocol_records[1]).toMatchObject({
+ adopted_protocols: ["AdoptedProtocol"],
+ methods: selector === 0x920 ? [] : [{ selector: "requiredAction:" }],
+ optional_methods:
+ selector === 0x940 ? [] : [{ selector: "optionalAction:" }],
+ decode: { status: "partial", reason: "protocol_method_selector_empty" },
+ });
expect(
result.coverage.find(
({ facet }) => facet === "objc_class_method_ivar_metadata",
@@ -1142,3 +1151,45 @@ describe("Apple dispatch list scan budget", () => {
});
});
});
+
+describe("malformed Objective-C protocol method lists", () => {
+ it("reports an empty protocol method selector without discarding the protocol or its other methods", () => {
+ const bytes = fixture();
+ bytes.writeUInt32LE(2, 0x804);
+ // The first required method names an empty string; the second is valid.
+ bytes.writeBigUInt64LE(base + 0x930n, 0x808);
+ bytes.writeBigUInt64LE(base + 0x920n, 0x820);
+ const result = decodeAppleDispatchMetadata(bytes, 100, provenance);
+ expect(result.objc_protocols).toMatchObject([
+ {
+ name: "FixtureProtocol",
+ methods: [{ selector: "requiredAction:", is_required: true }],
+ optional_methods: [{ selector: "optionalAction:", is_optional: true }],
+ },
+ ]);
+ expect(result.objc_protocol_records).toMatchObject([
+ {
+ name: "FixtureProtocol",
+ methods: [{ selector: "requiredAction:" }],
+ decode: { status: "partial", reason: "protocol_method_selector_empty" },
+ },
+ ]);
+ expect(result.objc_classes.map(({ name }) => name)).toEqual([
+ "Fixture",
+ "Fixture",
+ ]);
+ expect(result.objc_dispatch_implementations).toMatchObject([
+ { selector: "performAction:" },
+ ]);
+ expect(
+ result.coverage.find(
+ ({ facet }) => facet === "objc_class_method_ivar_metadata",
+ ),
+ ).toMatchObject({
+ status: "partial",
+ reason: expect.stringContaining(
+ "Protocol 0x100000740: Objective-C protocol FixtureProtocol required instance method 0 at 0x100000808 has an empty selector",
+ ),
+ });
+ });
+});
diff --git a/src/native/AppleObjcProtocols.ts b/src/native/AppleObjcProtocols.ts
index 5e41e3c38..1d130b868 100644
--- a/src/native/AppleObjcProtocols.ts
+++ b/src/native/AppleObjcProtocols.ts
@@ -33,7 +33,13 @@ export const createObjcProtocolReader = (input: {
}
return names;
};
- const methods = (address: bigint, required: boolean, instance: boolean) => {
+ const methods = (
+ address: bigint,
+ required: boolean,
+ instance: boolean,
+ /** Entries skipped for an empty selector, reported by the declaring protocol. */
+ emptySelectors: string[],
+ ) => {
const output: ObjcSwiftMetadata["objc_protocols"][number]["methods"] = [];
if (address === 0n) return output;
const flags = read.u32(address),
@@ -50,11 +56,15 @@ export const createObjcProtocolReader = (input: {
: read.pointer(entry);
if (relative && (flags & 0x40000000) === 0)
selector = read.pointer(selector);
+ // One empty selector is reported on its own, so the protocol and its
+ // other method declarations keep their decoded records.
const selectorName = read.string(selector);
- if (selectorName.length === 0)
- throw new TypeError(
- "Objective-C protocol method has an empty selector",
+ if (selectorName.length === 0) {
+ emptySelectors.push(
+ `${required ? "required" : "optional"} ${instance ? "instance" : "class"} method ${String(index)} at 0x${entry.toString(16)} has an empty selector`,
);
+ continue;
+ }
output.push({
selector: selectorName,
method_type: instance ? "instance" : "class",
@@ -77,14 +87,19 @@ export const createObjcProtocolReader = (input: {
const adopted = list(read.pointer(address + 16n));
if (protocolName.length === 0)
throw new TypeError("Objective-C protocol has an empty name");
+ const skipped: string[] = [];
const required = [
- ...methods(read.pointer(address + 24n), true, true),
- ...methods(read.pointer(address + 32n), true, false),
+ ...methods(read.pointer(address + 24n), true, true, skipped),
+ ...methods(read.pointer(address + 32n), true, false, skipped),
];
const optional = [
- ...methods(read.pointer(address + 40n), false, true),
- ...methods(read.pointer(address + 48n), false, false),
+ ...methods(read.pointer(address + 40n), false, true, skipped),
+ ...methods(read.pointer(address + 48n), false, false, skipped),
];
+ for (const message of skipped)
+ failures.push(
+ `Protocol 0x${address.toString(16)}: Objective-C protocol ${protocolName} ${message}`,
+ );
result.objc_protocols.push({
name: protocolName,
methods: required,
@@ -97,7 +112,10 @@ export const createObjcProtocolReader = (input: {
methods: required,
optional_methods: optional,
location: read.location(address),
- decode: { status: "decoded", reason: null },
+ decode:
+ skipped.length === 0
+ ? { status: "decoded", reason: null }
+ : { status: "partial", reason: "protocol_method_selector_empty" },
evidence: read.evidence(
address,
"Objective-C protocol declaration and encoded method lists; IMP fields are not handler targets",