Files
rea/src/domain/dyldPaths.ts
T
Kaoru0822andClaude Opus 5.5 11b7f918c2 feat(artifacts): trace Mach-O dylib resolution in bundles
Add trace_dylib_resolution (CLI trace-dylib-resolution) to the artifact
family. For the active Mach-O, or every executable in the active .app
(the main app, XPC services, extensions, login items, helpers), it
reports which file each dylib load reaches and how.

A pure TypeScript reader decodes FAT and Mach-O load commands:
LC_LOAD/WEAK/REEXPORT/LAZY/UPWARD_DYLIB, including the newer
dylib_use_command encoding with weak, re-export, upward and delayed-init
flags, plus LC_ID_DYLIB, LC_RPATH, LC_DYLD_ENVIRONMENT and code-signature
presence. Malformed tables are reported with the failing command instead
of being guessed.

Resolution follows dyld:
- @executable_path comes from each process root;
- @loader_path comes from the loading image, or from the owner of the
  LC_RPATH entry being expanded;
- @rpath searches the loader's rpaths first, then up the load chain;
- dependencies load dependents-first in load-command order, once per
  process, and a matching install name reuses an already loaded image;
- symlinks are followed inside the analyzed root, and escapes stop.

Every candidate path is reported with its outcome. Each edge resolves to
resolved, conditional (an outside-root candidate such as /usr/lib/swift
precedes), unresolved, or undetermined. Absolute and shared-cache system
paths are never checked against the host. Derived findings cover
unresolved required or weak loads, absent earlier @rpath candidates (with
library validation left unknown), and LC_DYLD_ENVIRONMENT.

verify:macos-bundle now also:
- checks every traced image against otool -l;
- asserts each resolution status on a clang-built fixture that adds a
  missing Overrides rpath, an absolute rpath, and a delay-init library;
- compares the predicted load order of two process roots with what dyld
  loads under DYLD_PRINT_LIBRARIES.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-07 10:31:17 +00:00

88 lines
2.9 KiB
TypeScript

import type { DylibTreeEntry, DylibTreeView } from "./dylibResolution.js";
const MAX_SYMLINK_HOPS = 32;
export type PathLookup =
| { readonly kind: "file"; readonly path: string }
| { readonly kind: "absent" | "escapes" | "not-file" };
/**
* Resolve a root-relative path one segment at a time, as `open` would:
* symlinks are followed inside the root, `..` applies to the real parent,
* and any escape above the root or through an absolute link stops.
*/
export const resolveTreePath = async (
view: DylibTreeView,
input: string,
): Promise<PathLookup> => {
const pending = segmentsOf(input);
const resolved: string[] = [];
let last: DylibTreeEntry | undefined;
let hops = 0;
while (pending.length > 0) {
const segment = pending.shift() ?? "";
if (segment === "..") {
if (resolved.length === 0) return { kind: "escapes" };
resolved.pop();
last = { kind: "directory" };
continue;
}
const path = [...resolved, segment].join("/");
const entry = await view.entry(path);
if (entry === undefined) return { kind: "absent" };
if (entry.kind === "symlink") {
hops += 1;
if (hops > MAX_SYMLINK_HOPS) return { kind: "absent" };
if (entry.target.startsWith("/")) return { kind: "escapes" };
pending.unshift(...segmentsOf(entry.target));
continue;
}
if (entry.kind === "file" && pending.length > 0) return { kind: "absent" };
resolved.push(segment);
last = entry;
}
return last?.kind === "file"
? { kind: "file", path: resolved.join("/") }
: { kind: "not-file" };
};
const segmentsOf = (path: string): string[] =>
path.split("/").filter((segment) => segment !== "" && segment !== ".");
export const directoryOf = (path: string): string => {
const index = path.lastIndexOf("/");
return index < 0 ? "" : path.slice(0, index);
};
export const joinPath = (directory: string, rest: string): string =>
directory === "" ? rest : rest === "" ? directory : `${directory}/${rest}`;
export type Expansion =
| { readonly scope: "target"; readonly path: string }
| { readonly scope: "outside"; readonly path: string }
| { readonly scope: "undetermined"; readonly path: string };
/** Expand one dyld path prefix relative to its owning image. */
export const expandPrefix = (
raw: string,
context: {
readonly ownerDirectory: string;
readonly executable: string | null;
},
): Expansion => {
const variable = /^@(executable_path|loader_path)(?:\/(.*))?$/u.exec(raw);
if (variable !== null) {
const rest = variable[2] ?? "";
if (variable[1] === "loader_path")
return { scope: "target", path: joinPath(context.ownerDirectory, rest) };
return context.executable === null
? { scope: "undetermined", path: raw }
: {
scope: "target",
path: joinPath(directoryOf(context.executable), rest),
};
}
if (raw.startsWith("/")) return { scope: "outside", path: raw };
return { scope: "undetermined", path: raw };
};