Skip to content

armgit / rea

Public Imported from GitHub
0

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

fix(native): keep property flags unknown for unfinished quoted types (#3825)

Kaoru0822b7dcf782aac1
diff --git a/docs/native-investigation.md b/docs/native-investigation.md
index ecda41a46..3df66a93f 100644
--- a/docs/native-investigation.md
+++ b/docs/native-investigation.md
@@ -378,7 +378,9 @@ complete observation.
property: `N` means `nonatomic`; a complete typed attribute string without
`N` means the Objective-C default `atomic`;
missing or incomplete attribute metadata leaves `atomicity` and any
- unestablished read-only value null. `is_meta_class`, `is_root_class`, and
+ unestablished read-only value null. A type attribute whose quoted class name
+ never closes is incomplete and marks `objc_properties_categories` partial
+ with `property_attributes_incomplete`. `is_meta_class`, `is_root_class`, and
`ivar_count` are also nullable when the selected provider exposes only symbol
names. Class implementation methods have null `is_required` and `is_optional`
fields because those facts apply to protocol declarations; decoded protocol
diff --git a/src/native/AppleMachoFixups.test.ts b/src/native/AppleMachoFixups.test.ts
index 318fdbf7c..f3f84a76c 100644
--- a/src/native/AppleMachoFixups.test.ts
+++ b/src/native/AppleMachoFixups.test.ts
@@ -983,4 +983,17 @@ describe("Objective-C property attributes", () => {
expect(parsePropertyAttributes("count", "").atomicity).toBeNull();
expect(parsePropertyAttributes("count", "R").atomicity).toBeNull();
});
+
+ it("establishes no defaults from an unfinished quoted class name", () => {
+ // `,N,R` here lie inside the unterminated quote, so they are not flags.
+ expect(parsePropertyAttributes("title", 'T@"NSString,N,R')).toMatchObject({
+ type_encoding: '@"NSString,N,R',
+ atomicity: null,
+ is_readonly: null,
+ });
+ expect(parsePropertyAttributes("title", 'N,R,T@"NSString')).toMatchObject({
+ atomicity: "nonatomic",
+ is_readonly: true,
+ });
+ });
});
diff --git a/src/native/AppleObjcProperties.ts b/src/native/AppleObjcProperties.ts
index cd9c0774d..5b78b5ab9 100644
--- a/src/native/AppleObjcProperties.ts
+++ b/src/native/AppleObjcProperties.ts
@@ -19,8 +19,21 @@ export interface ObjcPropertyReaders {
export const parsePropertyAttributes = (
name: string,
attributes: string,
-): ObjcProperty => {
- const tokens = attributes.length === 0 ? [] : splitAttributes(attributes);
+): ObjcProperty => decodePropertyAttributes(name, attributes).property;
+
+/**
+ * Decode one attribute string. A quoted class name that never closes leaves
+ * the string incomplete: later attributes may be inside the unfinished type,
+ * so a missing `N` or `R` does not establish the Objective-C defaults.
+ */
+const decodePropertyAttributes = (
+ name: string,
+ attributes: string,
+): { readonly property: ObjcProperty; readonly complete: boolean } => {
+ const { tokens, complete } =
+ attributes.length === 0
+ ? { tokens: [], complete: true }
+ : splitAttributes(attributes);
const typeAttributes = tokens.filter((token) => token[0] === "T");
const typeAttribute =
typeAttributes.length === 1 ? typeAttributes[0] : undefined;
@@ -30,7 +43,8 @@ export const parsePropertyAttributes = (
: null;
const value = (code: string): string | null =>
tokens.find((token) => token[0] === code)?.slice(1) ?? null;
- return {
+ const defaultsEstablished = typeEncoding !== null && complete;
+ const property: ObjcProperty = {
name,
type_encoding: typeEncoding,
attributes: tokens.map((token) => ({
@@ -39,21 +53,27 @@ export const parsePropertyAttributes = (
})),
atomicity: tokens.includes("N")
? "nonatomic"
- : typeEncoding === null
- ? null
- : "atomic",
+ : defaultsEstablished
+ ? "atomic"
+ : null,
is_readonly: tokens.includes("R")
? true
- : typeEncoding === null
- ? null
- : false,
+ : defaultsEstablished
+ ? false
+ : null,
getter: value("G"),
setter: value("S"),
};
+ return { property, complete };
};
-/** Split on commas that are outside a quoted class name in the type encoding. */
-const splitAttributes = (attributes: string): string[] => {
+/**
+ * Split on commas that are outside a quoted class name in the type encoding,
+ * and report whether every quote closed.
+ */
+const splitAttributes = (
+ attributes: string,
+): { readonly tokens: string[]; readonly complete: boolean } => {
const tokens: string[] = [];
let current = "";
let quoted = false;
@@ -65,7 +85,10 @@ const splitAttributes = (attributes: string): string[] => {
} else current += character;
}
tokens.push(current);
- return tokens.filter((token) => token.length > 0);
+ return {
+ tokens: tokens.filter((token) => token.length > 0),
+ complete: !quoted,
+ };
};
/** Decode `property_list_t { entsize_and_flags, count, property_t[] }`. */
@@ -73,8 +96,13 @@ export const readObjcPropertyList = (
read: ObjcPropertyReaders,
list: bigint,
admit: () => boolean,
-): ObjcProperty[] => {
- if (list === 0n) return [];
+): {
+ readonly properties: ObjcProperty[];
+ /** Names of properties whose attribute string is incomplete. */
+ readonly incomplete: string[];
+} => {
+ const incomplete: string[] = [];
+ if (list === 0n) return { properties: [], incomplete };
const entrySize = read.u32(list) & ~3;
const count = read.u32(list + 4n);
if (entrySize !== 16)
@@ -85,14 +113,14 @@ export const readObjcPropertyList = (
for (let index = 0; index < count; index++) {
if (!admit()) break;
const entry = list + 8n + BigInt(index * entrySize);
- properties.push(
- parsePropertyAttributes(
- read.string(read.pointer(entry)),
- read.string(read.pointer(entry + 8n)),
- ),
+ const decoded = decodePropertyAttributes(
+ read.string(read.pointer(entry)),
+ read.string(read.pointer(entry + 8n)),
);
+ properties.push(decoded.property);
+ if (!decoded.complete) incomplete.push(decoded.property.name);
}
- return properties;
+ return { properties, incomplete };
};
/** Decode a property list, recording a failure for `owner` instead of throwing. */
@@ -107,7 +135,21 @@ export const readObjcPropertiesOf = (
},
): ObjcProperty[] => {
try {
- return readObjcPropertyList(read, list, context.admit);
+ const { properties, incomplete } = readObjcPropertyList(
+ read,
+ list,
+ context.admit,
+ );
+ for (const name of incomplete)
+ context.issues.push(
+ issue(
+ "objc_properties_categories",
+ "property_attributes_incomplete",
+ `${context.owner}.${name}: the type attribute has an unterminated quoted class name, so atomicity and unestablished read-only state are unknown`,
+ context.location,
+ ),
+ );
+ return properties;
} catch (cause: unknown) {
context.issues.push(
issue(
diff --git a/tests/boundary/cli/nativeDispatchPropertyAttributes.test.ts b/tests/boundary/cli/nativeDispatchPropertyAttributes.test.ts
new file mode 100644
index 000000000..51ab9dd4b
--- /dev/null
+++ b/tests/boundary/cli/nativeDispatchPropertyAttributes.test.ts
@@ -0,0 +1,110 @@
+import { writeFile } from "node:fs/promises";
+import { join } from "node:path";
+
+import { describe, expect } from "vitest";
+
+import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
+import { cliTest } from "../../support/cli/cliFixture.js";
+
+/** arm64 Mach-O whose two class properties share one attribute string. */
+const machoWithPropertyAttributes = (attributes: string): Buffer => {
+ const bytes = Buffer.alloc(4096);
+ const base = 0x100000000n;
+ const u32 = (at: number, value: number) => bytes.writeUInt32LE(value, at);
+ const pointer = (at: number, offset: number) =>
+ bytes.writeBigUInt64LE(base + BigInt(offset), at);
+ u32(0, 0xfeedfacf);
+ u32(4, 0x0100000c);
+ u32(12, 2);
+ u32(16, 1);
+ u32(20, 152);
+ u32(32, 0x19);
+ u32(36, 152);
+ bytes.write("__DATA", 40);
+ pointer(56, 0);
+ bytes.writeBigUInt64LE(4096n, 64);
+ bytes.writeBigUInt64LE(4096n, 80);
+ u32(88, 5);
+ u32(92, 5);
+ u32(96, 1);
+ bytes.write("__objc_classlist", 104);
+ bytes.write("__DATA", 120);
+ pointer(136, 0x180);
+ bytes.writeBigUInt64LE(8n, 144);
+ u32(152, 0x180);
+ pointer(0x180, 0x200);
+ pointer(0x200 + 32, 0x300);
+ u32(0x300, 2);
+ u32(0x308, 16);
+ pointer(0x300 + 24, 0x400);
+ bytes.write("Fixture\0", 0x400);
+ pointer(0x340, 0x500);
+ u32(0x500, 16);
+ u32(0x504, 2);
+ pointer(0x508, 0x600);
+ pointer(0x510, 0x620);
+ pointer(0x518, 0x640);
+ pointer(0x520, 0x620);
+ bytes.write("first\0", 0x600);
+ bytes.write(`${attributes}\0`, 0x620);
+ bytes.write("second\0", 0x640);
+ return bytes;
+};
+
+describe.skipIf(process.platform !== "darwin")(
+ "native dispatch property attributes",
+ () => {
+ cliTest.for([
+ ["complete nonatomic", "Tq,N", "nonatomic", false, "complete"],
+ ["complete default", 'T@"NSString",C', "atomic", false, "complete"],
+ ["unfinished quote", 'T@"NSString', null, null, "partial"],
+ ["unfinished quote after R", 'R,T@"NSString', null, true, "partial"],
+ ] as const)(
+ "reports established property flags only for $0 attributes",
+ async ([, attributes, atomicity, readonly, facet], { cli }) => {
+ const directory = await createTestTempDirectory("rea-objc-property-");
+ const path = join(directory, "fixture.bin");
+ await writeFile(path, machoWithPropertyAttributes(attributes));
+ const output = await cli.run({
+ arguments: ["inspect-native-dispatch-metadata", path, "--json"],
+ environment: { REA_LOG_LEVEL: "silent" },
+ });
+ expect(output.exitCode).toBe(0);
+ const property = {
+ atomicity,
+ is_readonly: readonly,
+ attributes: expect.arrayContaining([
+ { name: "T", value: expect.any(String) },
+ ]),
+ };
+ expect(output.json).toMatchObject({
+ normalized_result: {
+ result: {
+ objc_classes: [
+ {
+ name: "Fixture",
+ properties: [
+ { name: "first", ...property },
+ { name: "second", ...property },
+ ],
+ },
+ ],
+ coverage: expect.arrayContaining([
+ expect.objectContaining({
+ facet: "objc_properties_categories",
+ status: facet,
+ reason:
+ facet === "partial"
+ ? expect.stringContaining(
+ "property_attributes_incomplete",
+ )
+ : null,
+ }),
+ ]),
+ },
+ },
+ });
+ },
+ );
+ },
+);