fix(apple): account for repeated archive projections

This commit is contained in:
morluto
2026-10-09 06:32:10 +08:00
parent d575a7340b
commit e1df5e8555
7 changed files with 391 additions and 70 deletions
+67 -38
View File
@@ -278,6 +278,14 @@ const projectNibArchive = (
maxHierarchyNodes: number,
) => {
const byId = new Map(archive.objects.map((object) => [object.id, object]));
const normalizedClassNames = new Map<string, string>();
const normalizeClassName = (value: string): string => {
const known = normalizedClassNames.get(value);
if (known !== undefined) return known;
const normalized = value.replace(/\0+$/u, "");
normalizedClassNames.set(value, normalized);
return normalized;
};
const decodedStrings = new Map<number, string | null>();
const decodedString = (
objectId: number,
@@ -303,14 +311,6 @@ const projectNibArchive = (
decodedStrings.set(objectId, value);
return value;
};
const reserveStringOccurrence = (value: string | null): string | null => {
if (value !== null)
budget.reserve(
64 + value.length * 6,
"NIBArchive repeated string projection exceeds the aggregate Interface Builder decode budget",
);
return value;
};
let projectedFields = 0;
for (const object of archive.objects)
projectedFields += Object.keys(object.values).length;
@@ -325,13 +325,21 @@ const projectNibArchive = (
const dereference = (value: JsonValue | undefined): JsonValue | undefined => {
if (typeof value !== "object" || value === null || Array.isArray(value))
return value;
const encodedData = value.$nib_data_base64;
if (typeof encodedData === "string") {
budget.reserveBase64JsonStringOccurrence(encodedData);
return value;
}
const ref = value.$nib_object_ref;
if (typeof ref !== "number") return value;
const target = byId.get(ref);
if (target === undefined) return undefined;
if (target.class_name.replace(/\0+$/u, "") === "NSString") {
if (normalizeClassName(target.class_name) === "NSString") {
const string = decodedString(ref, target);
if (string !== null) return reserveStringOccurrence(string);
if (string !== null) {
budget.reserveJsonStringOccurrence(string);
return string;
}
}
return { objectID: String(ref) };
};
@@ -341,18 +349,31 @@ const projectNibArchive = (
const ref = value.$nib_object_ref;
if (typeof ref !== "number") return null;
const target = byId.get(ref);
if (target?.class_name.replace(/\0+$/u, "") !== "NSString") return null;
return reserveStringOccurrence(decodedString(ref, target));
if (
target === undefined ||
normalizeClassName(target.class_name) !== "NSString"
)
return null;
const string = decodedString(ref, target);
if (string === null) return null;
budget.reserveJsonStringOccurrence(string);
return string;
};
const runtimeClasses = new Map<number, string | null>();
const runtimeClass = (objectId: number): string | null => {
if (runtimeClasses.has(objectId))
return runtimeClasses.get(objectId) ?? null;
const object = byId.get(objectId);
if (object === undefined) return null;
const storedClass = object.class_name.replace(/\0+$/u, "");
if (storedClass === "NSClassSwapper")
return referencedString(object.values.NSClassName) ?? storedClass;
return storedClass === "UIClassSwapper"
? (referencedString(object.values.UIClassName) ?? storedClass)
: storedClass;
const storedClass = normalizeClassName(object.class_name);
const resolvedClass =
storedClass === "NSClassSwapper"
? (referencedString(object.values.NSClassName) ?? storedClass)
: storedClass === "UIClassSwapper"
? (referencedString(object.values.UIClassName) ?? storedClass)
: storedClass;
runtimeClasses.set(objectId, resolvedClass);
return resolvedClass;
};
const objects = Object.fromEntries(
archive.objects
@@ -364,19 +385,22 @@ const projectNibArchive = (
})
.map((object) => {
const className =
runtimeClass(object.id) ?? object.class_name.replace(/\0+$/u, "");
const strings = Object.entries(object.values).flatMap(
([key, value]) => {
const resolved = dereference(value);
return typeof resolved === "string" ? [[key, resolved]] : [];
},
);
const name =
strings.find(([key]) =>
/(?:title|label|identifier|accessibility|name|contents)/iu.test(
key ?? "",
),
)?.[1] ?? className;
runtimeClass(object.id) ?? normalizeClassName(object.class_name);
budget.reserveJsonStringOccurrence(className);
let name = className;
for (const [key, value] of Object.entries(object.values)) {
if (
!/(?:title|label|identifier|accessibility|name|contents)/iu.test(
key,
)
)
continue;
const resolved = dereference(value);
if (typeof resolved !== "string") continue;
name = resolved;
break;
}
budget.reserveJsonStringOccurrence(name);
return [
String(object.id),
{
@@ -384,10 +408,10 @@ const projectNibArchive = (
label: name,
objectID: String(object.id),
nibValues: Object.fromEntries(
Object.entries(object.values).map(([key, value]) => [
key,
dereference(value) ?? null,
]),
Object.entries(object.values).map(([key, value]) => {
budget.reserveJsonPropertyNameOccurrence(key);
return [key, dereference(value) ?? null];
}),
),
},
];
@@ -403,7 +427,7 @@ const projectNibArchive = (
: null;
};
for (const object of archive.objects) {
const className = object.class_name.replace(/\0+$/u, "");
const className = normalizeClassName(object.class_name);
// AppKit connectors use NS-prefixed keys; UIKit's runtime outlet and
// event connections use UI-prefixed keys with the same roles.
const uiKit = /^UIRuntime\w*Connection$/u.test(className);
@@ -420,6 +444,7 @@ const projectNibArchive = (
: className.includes("Control") || className.includes("Event")
? "action"
: className;
budget.reserveJsonStringOccurrence(type);
const eventMask = values.UIEventMask;
// The source is the outlet owner or the sending control; the destination
// is the outlet value or the action target, and a nil target is the
@@ -485,7 +510,7 @@ const projectNibArchive = (
return { objectID: String(objectId), children };
};
const ibData = archive.objects.find(
({ class_name }) => class_name.replace(/\0+$/u, "") === "NSIBObjectData",
({ class_name }) => normalizeClassName(class_name) === "NSIBObjectData",
);
const rootId = reference(ibData?.values.NSRoot);
const hierarchyRoots = rootId === null ? [] : [rootId];
@@ -512,7 +537,11 @@ const projectNibArchive = (
"com.apple.ibtool.document.connections": connections,
"com.apple.ibtool.document.hierarchy": merged.hierarchy,
"com.apple.ibtool.document.classes": Object.fromEntries(
archive.classes.map((name) => [name.replace(/\0+$/u, ""), {}]),
archive.classes.map((name) => {
const className = normalizeClassName(name);
budget.reserveJsonPropertyNameOccurrence(className);
return [className, {}];
}),
),
}),
};
@@ -48,6 +48,33 @@ export class InterfaceBuilderDecodeBudget {
this.#remainingBytes -= bytes;
}
/** Reserve an upper bound for one JSON-serialized string occurrence. */
reserveJsonStringOccurrence(value: string): void {
// JSON escaping uses at most six ASCII bytes per UTF-16 code unit, plus
// quotes. Count occurrences even when the source string is shared.
this.reserve(
value.length * 6 + 2,
"projected JSON strings exceed the aggregate Interface Builder decode budget",
);
}
/** Reserve the exact JSON string length for a base64 value from Buffer. */
reserveBase64JsonStringOccurrence(value: string): void {
// Buffer's base64 alphabet contains no JSON-escaped characters.
this.reserve(
value.length + 2,
"projected NIB base64 values exceed the aggregate Interface Builder decode budget",
);
}
/** Reserve an upper bound for one JSON object property name occurrence. */
reserveJsonPropertyNameOccurrence(value: string): void {
this.reserve(
value.length * 6 + 3,
"projected JSON property names exceed the aggregate Interface Builder decode budget",
);
}
/** Reserve bytes for an optional projection without throwing on exhaustion. */
tryReserve(bytes: number): boolean {
if (
@@ -214,7 +241,7 @@ const estimateBinaryPlistExpansion = (
const byteLength = type === 6 ? size * 2 : size;
if (cursor + byteLength > offsetTable)
throw new TypeError("binary plist scalar object is truncated");
estimate += type === 4 ? Math.ceil((size * 4) / 3) : byteLength * 2;
estimate += type === 4 ? size + 4 * Math.ceil(size / 3) : byteLength * 2;
if (estimate > maxBytes)
throw new InterfaceBuilderDecodeBudgetExceeded(
"aggregate_decode_budget_exhausted",
+1 -1
View File
@@ -395,7 +395,7 @@ const readValue = (
`value ${index} data`,
);
budget.reserve(
32 + Math.ceil((length * 4) / 3),
32 + 4 * Math.ceil(length / 3),
`NIBArchive data value ${index} exceeds the aggregate Interface Builder decode budget`,
);
const encoded = bytes.subarray(cursor.offset, next).toString("base64");