feat(apple): inspect dyld shared caches and resolve system paths

macOS 11 and later ship system libraries only inside the dyld shared
cache. trace_dylib_resolution therefore left every /usr/lib and /System
load undetermined, and REA had no way to see what a cache contains.

inspect_dyld_shared_cache (CLI inspect-dyld-shared-cache) reads a cache
file set selected by path, without an open target. Its pure TypeScript
reader parses modern and legacy dyld_cache_header layouts and reports:
- UUID, architecture, platform, OS version, Mac Catalyst alternate,
  cache type, shared region, max slide;
- every VM mapping;
- each subcache by file suffix, checked by UUID. A missing or
  mismatched subcache makes coverage partial;
- the complete image list.
For each requested install path it reads that image's load commands
through the cache mappings with the existing Mach-O reader.

trace_dylib_resolution accepts an optional shared_cache. Absolute
candidates found in a cache that serves the process's CPU family resolve
with the new outcome and status `shared-cache`. That status also takes
precedence over later in-bundle fallbacks such as a back-deployed
libswiftCore. The result names the cache UUID and OS version it used.

verify:dyld-shared-cache checks every image in the host cache (3,649 on
macOS 26.6), with dependencies and their re-export, weak, upward and
delay-init attributes, against `dyld_info -all_dyld_cache
-linked_dylibs`. It also checks CLI/MCP parity, and that each
shared-cache edge in the fixture trace is a library dyld actually loads.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kaoru0822
2026-10-07 11:43:15 +00:00
co-authored by Claude Opus 5.5
parent a0ec3e2016
commit ade11fa02d
39 changed files with 1911 additions and 176 deletions
+39 -1
View File
@@ -1,7 +1,7 @@
import { createHash } from "node:crypto";
import { constants } from "node:fs";
import { lstat, open, readlink, realpath } from "node:fs/promises";
import { join, relative, sep } from "node:path";
import { join, relative, resolve, sep } from "node:path";
import {
dylibResolutionInputSchema,
@@ -14,6 +14,7 @@ import {
} from "../domain/dylibResolution.js";
import { ArtifactReaderFailure } from "./ArtifactReader.js";
import { DirectoryArtifactReader } from "./DirectoryArtifactReader.js";
import { DyldSharedCache } from "./DyldSharedCacheReader.js";
import { hasMachoMagic, readMachoImage } from "./MachoLoadCommandReader.js";
const HASH_CHUNK_BYTES = 1024 * 1024;
@@ -80,7 +81,9 @@ export const traceDylibResolution = async (options: {
.split(sep)
.join("/");
const view = new FilesystemTreeView(root, options.signal);
let cache: DyldSharedCache | undefined;
try {
cache = await openSharedCache(parsed.data.shared_cache, options.signal);
const { roots, unclassified } =
parsed.data.roots !== undefined
? { roots: parsed.data.roots, unclassified: [] }
@@ -94,6 +97,7 @@ export const traceDylibResolution = async (options: {
...(parsed.data.architecture === undefined
? {}
: { architecture: parsed.data.architecture }),
...(cache === undefined ? {} : { sharedCache: sharedCacheView(cache) }),
...(options.signal === undefined ? {} : { signal: options.signal }),
});
const digests = new Map<string, string>();
@@ -111,6 +115,15 @@ export const traceDylibResolution = async (options: {
...trace,
root_path: options.rootPath,
target_sha256: options.targetSha256,
shared_cache:
cache === undefined || parsed.data.shared_cache === undefined
? null
: {
path: resolve(parsed.data.shared_cache),
uuid: cache.header.uuid,
architecture: cache.header.architecture,
os_version: cache.header.os_version,
},
images: trace.images.map((image) => ({
...image,
sha256: digests.get(image.path),
@@ -124,9 +137,34 @@ export const traceDylibResolution = async (options: {
{ cause },
);
throw cause;
} finally {
await cache?.close();
}
};
const openSharedCache = async (
path: string | undefined,
signal?: AbortSignal,
): Promise<DyldSharedCache | undefined> => {
if (path === undefined) return undefined;
try {
return await DyldSharedCache.open(resolve(path), signal);
} catch (cause: unknown) {
if (missing(cause))
throw new ArtifactReaderFailure(
"path",
"shared_cache does not name an existing dyld shared cache file",
{ cause },
);
throw cause;
}
};
const sharedCacheView = (cache: DyldSharedCache) => ({
architecture: cache.header.architecture,
has: (path: string): boolean => cache.find(path) !== undefined,
});
/**
* Every Mach-O in the bundle with an executable slice is its own process root.
* Mach-O files that do not parse are returned separately: whether they are