fix(apple): traverse keyed archive hierarchy collections (#976)

This commit is contained in:
morluto
2026-10-07 23:00:43 +08:00
committed by GitHub
parent fb3749aff6
commit 71ac83f3dd
4 changed files with 302 additions and 64 deletions
+57 -44
View File
@@ -202,53 +202,66 @@ export const buildInterfaceBuilderAnalysis = (input: {
};
let hierarchyCount = 0;
let hierarchyTruncated = false;
const visitHierarchy = (value: unknown, parentId: string): void => {
if (hierarchyCount >= input.limits.max_objects) {
truncated = true;
hierarchyTruncated = true;
return;
}
if (Array.isArray(value)) {
for (const child of value) visitHierarchy(child, parentId);
return;
}
if (typeof value !== "object" || value === null) return;
const item = record(value);
const objectId = firstString(
item.objectID,
item["object-id"],
item.id,
typeof item.archiveUID === "number" ? String(item.archiveUID) : null,
);
let nextParent = parentId;
if (objectId !== null) {
const known = objectIds.has(objectId);
const childId = known
? objectNodeId(objectId)
: `${prefix}unknown:${objectId}`;
if (
!known &&
!addNode({
id: childId,
kind: "unknown",
name: firstString(item.label, item.name) ?? objectId,
location: null,
attributes: { interface_builder_object_id: objectId },
evidence: evidenceFor(`hierarchy item ${objectId}`),
})
)
const visitHierarchy = (
initial: unknown,
initialParentId: string,
): void => {
const pending: { readonly value: unknown; readonly parentId: string }[] =
[{ value: initial, parentId: initialParentId }];
while (pending.length > 0) {
if (hierarchyCount >= input.limits.max_objects) {
truncated = true;
hierarchyTruncated = true;
connect(
`${prefix}hierarchy:${parentId}:${childId}`,
parentId,
childId,
"contains",
`hierarchy ${objectId}`,
return;
}
const entry = pending.pop();
if (entry === undefined) break;
const { value, parentId } = entry;
if (Array.isArray(value)) {
for (let index = value.length - 1; index >= 0; index -= 1) {
const child = value[index];
if (child !== undefined) pending.push({ value: child, parentId });
}
continue;
}
if (typeof value !== "object" || value === null) continue;
const item = record(value);
const objectId = firstString(
item.objectID,
item["object-id"],
item.id,
typeof item.archiveUID === "number" ? String(item.archiveUID) : null,
);
nextParent = childId;
hierarchyCount += 1;
let nextParent = parentId;
if (objectId !== null) {
const known = objectIds.has(objectId);
const childId = known
? objectNodeId(objectId)
: `${prefix}unknown:${objectId}`;
if (
!known &&
!addNode({
id: childId,
kind: "unknown",
name: firstString(item.label, item.name) ?? objectId,
location: null,
attributes: { interface_builder_object_id: objectId },
evidence: evidenceFor(`hierarchy item ${objectId}`),
})
)
hierarchyTruncated = true;
connect(
`${prefix}hierarchy:${parentId}:${childId}`,
parentId,
childId,
"contains",
`hierarchy ${objectId}`,
);
nextParent = childId;
hierarchyCount += 1;
}
pending.push({ value: item.children, parentId: nextParent });
}
visitHierarchy(item.children, nextParent);
};
for (const hierarchy of parsed.hierarchy) visitHierarchy(hierarchy, rootId);
for (const [index, item] of parsed.connections
@@ -196,39 +196,85 @@ const keyedArchiveTable = (objectTable: readonly unknown[]) => {
};
const hierarchy = (start: unknown): unknown[] => {
const visited = new Set<number>();
const node = (
const root: unknown[] = [];
const pending: { readonly value: unknown; readonly output: unknown[] }[] = [
{ value: start, output: root },
];
const appendNode = (
item: Record<string, unknown>,
uid: number | undefined,
depth: number,
): unknown[] => {
output: unknown[],
): void => {
const objectId = firstString(
authoredId(item),
text(item.id),
uid === undefined ? null : String(uid),
);
const children = Object.entries(item)
.filter(([key]) => HIERARCHY_KEY.test(key))
.flatMap(([, child]) => visit(child, depth + 1));
return objectId === null ? children : [{ objectID: objectId, children }];
if (objectId === null) {
for (const [key, child] of Object.entries(item).reverse())
if (HIERARCHY_KEY.test(key)) pending.push({ value: child, output });
return;
}
const children: unknown[] = [];
output.push({ objectID: objectId, children });
for (const [key, child] of Object.entries(item).reverse())
if (HIERARCHY_KEY.test(key))
pending.push({ value: child, output: children });
};
const visit = (value: unknown, depth: number): unknown[] => {
if (depth > 32) return [];
while (pending.length > 0) {
const entry = pending.pop();
if (entry === undefined) break;
const { value, output } = entry;
const uid = archiveUid(value);
if (uid !== undefined) {
if (visited.has(uid)) return [];
if (visited.has(uid)) continue;
visited.add(uid);
const target = objectTable[uid];
if (Array.isArray(target)) return visit(target, depth + 1);
return typeof target === "object" && target !== null
? node(record(target), uid, depth)
: [];
if (Array.isArray(target)) {
for (let index = target.length - 1; index >= 0; index -= 1) {
const child = target[index];
if (child !== undefined) pending.push({ value: child, output });
}
continue;
}
if (typeof target !== "object" || target === null) continue;
const item = record(target);
if (archivedArrayClass(item.$class)) {
const members = item["NS.objects"];
if (Array.isArray(members))
for (let index = members.length - 1; index >= 0; index -= 1) {
const child = members[index];
if (child !== undefined) pending.push({ value: child, output });
}
continue;
}
appendNode(item, uid, output);
continue;
}
if (Array.isArray(value))
return value.flatMap((item) => visit(item, depth + 1));
if (typeof value !== "object" || value === null) return [];
return node(record(value), undefined, depth);
};
return visit(start, 0);
if (Array.isArray(value)) {
for (let index = value.length - 1; index >= 0; index -= 1) {
const child = value[index];
if (child !== undefined) pending.push({ value: child, output });
}
} else if (typeof value === "object" && value !== null) {
appendNode(record(value), undefined, output);
}
}
return root;
};
const archivedArrayClass = (classReference: unknown): boolean => {
const uid = archiveUid(classReference);
const descriptor = record(
uid === undefined ? classReference : objectTable[uid],
);
const classNames = Array.isArray(descriptor.$classes)
? descriptor.$classes.filter(
(name): name is string => typeof name === "string",
)
: [];
return (
classNames.includes("NSArray") || classNames.includes("NSMutableArray")
);
};
return { resolve, hierarchy };
};
@@ -13,6 +13,57 @@ import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
const compile = promisify(execFile);
type PlistValue =
| string
| number
| boolean
| Date
| Uint8Array
| PlistValue[]
| { [key: string]: PlistValue }
| null;
const keyedArchiveHierarchy = (
nodes: readonly {
readonly id: string;
readonly children: readonly number[];
}[],
) => {
const nodeClass = 1 + nodes.length * 2;
const arrayClass = nodeClass + 1;
const objects: PlistValue[] = Array.from(
{ length: arrayClass + 1 },
() => null,
);
objects[0] = "$null";
for (const [index, node] of nodes.entries()) {
const nodeUid = 1 + index * 2;
objects[nodeUid] = {
$class: { UID: nodeClass },
objectID: node.id,
children: { UID: nodeUid + 1 },
};
objects[nodeUid + 1] = {
$class: { UID: arrayClass },
"NS.objects": node.children.map((child) => ({ UID: 1 + child * 2 })),
};
}
objects[nodeClass] = {
$classname: "FixtureHierarchyNode",
$classes: ["FixtureHierarchyNode", "NSObject"],
};
objects[arrayClass] = {
$classname: "__NSArrayI",
$classes: ["__NSArrayI", "NSArray", "NSObject"],
};
return buildBinary({
$archiver: "NSKeyedArchiver",
$version: 100000,
$objects: objects,
$top: { root: { UID: 1 } },
});
};
describe("compiled Interface Builder bundle reader", () => {
it("reads nib plist archives and reports provenance", async () => {
const root = await createTestTempDirectory("rea-ib-test-");
@@ -149,7 +200,118 @@ describe("compiled Interface Builder bundle reader", () => {
},
]);
});
});
describe("keyed archive hierarchy coverage", () => {
it("projects a deep keyed-archive hierarchy through NSArray wrappers", async () => {
const root = await createTestTempDirectory("rea-ib-test-");
const bundle = join(root, "Example.app");
const resources = join(bundle, "Contents", "Resources");
await mkdir(resources, { recursive: true });
const nodes = Array.from({ length: 40 }, (_, index) => ({
id: `view-${index}`,
children: index === 39 ? [] : [index + 1],
}));
await writeFile(join(resources, "Deep.nib"), keyedArchiveHierarchy(nodes));
const analysis = await analyzeInterfaceBuilderBundle({
bundlePath: bundle,
targetSha256: "f".repeat(64),
});
expect(analysis.documents[0]?.hierarchy_complete).toBe(true);
expect(analysis.graph.truncated).toBe(false);
expect(analysis.graph.coverage).toContainEqual(
expect.objectContaining({
facet: "hierarchy:Contents/Resources/Deep.nib",
status: "complete",
omitted: 0,
}),
);
expect(
analysis.graph.edges.filter(
({ relation, id }) =>
relation === "contains" && id.includes(":hierarchy:"),
),
).toHaveLength(40);
});
it("preserves source order for children in an archived collection", async () => {
const root = await createTestTempDirectory("rea-ib-test-");
const bundle = join(root, "Example.app");
const resources = join(bundle, "Contents", "Resources");
await mkdir(resources, { recursive: true });
await writeFile(
join(resources, "Branching.nib"),
keyedArchiveHierarchy([
{ id: "root", children: [1, 2] },
{ id: "first", children: [] },
{ id: "second", children: [] },
]),
);
const analysis = await analyzeInterfaceBuilderBundle({
bundlePath: bundle,
targetSha256: "e".repeat(64),
});
const archiveIdsByNodeId = new Map(
analysis.graph.nodes.map(({ id, attributes }) => [
id,
attributes.interface_builder_object_id,
]),
);
const rootId = analysis.graph.nodes.find(
({ attributes }) => attributes.interface_builder_object_id === "root",
)?.id;
const childNames = analysis.graph.edges
.filter(
({ from, relation, id }) =>
from === rootId &&
relation === "contains" &&
id.includes(":hierarchy:"),
)
.map(({ to }) => (to === null ? null : archiveIdsByNodeId.get(to)));
expect(childNames).toEqual(["first", "second"]);
});
it.skipIf(process.platform !== "darwin")(
"projects a deep NSView archive produced by Foundation NSKeyedArchiver",
async () => {
const root = await createTestTempDirectory("rea-ib-test-");
const bundle = join(root, "Example.app");
const resources = join(bundle, "Contents", "Resources");
await mkdir(resources, { recursive: true });
const archive = join(resources, "Foundation.nib");
await compile("/usr/bin/xcrun", [
"swift",
"tests/conformance/native/keyed-archive-hierarchy.swift",
archive,
"40",
]);
const analysis = await analyzeInterfaceBuilderBundle({
bundlePath: bundle,
targetSha256: "d".repeat(64),
});
expect(analysis.documents[0]?.hierarchy_complete).toBe(true);
expect(analysis.graph.truncated).toBe(false);
expect(
analysis.graph.coverage.find(
({ facet }) =>
facet === "hierarchy:Contents/Resources/Foundation.nib",
),
).toMatchObject({ status: "complete", examined: 41, omitted: 0 });
expect(
analysis.graph.edges.filter(
({ relation, id }) =>
relation === "contains" && id.includes(":hierarchy:"),
),
).toHaveLength(41);
},
);
});
describe("compiled Interface Builder bundle reader cancellation", () => {
it("honors cancellation during directory traversal", async () => {
const root = await createTestTempDirectory("rea-ib-test-");
const controller = new AbortController();
@@ -0,0 +1,17 @@
import AppKit
let count = Int(CommandLine.arguments[2]) ?? 40
let root = NSView(frame: .zero)
root.identifier = NSUserInterfaceItemIdentifier("root")
var parent = root
for index in 0..<count {
let child = NSView(frame: .zero)
child.identifier = NSUserInterfaceItemIdentifier("view-\(index)")
parent.addSubview(child)
parent = child
}
let archive = try NSKeyedArchiver.archivedData(
withRootObject: root,
requiringSecureCoding: false,
)
try archive.write(to: URL(fileURLWithPath: CommandLine.arguments[1]))