fix(apple): distinguish cache loadability and propagate search uncertainty

This commit is contained in:
morluto
2026-10-08 23:16:28 +08:00
129 changed files with 3977 additions and 1588 deletions
+100 -4
View File
@@ -146,7 +146,7 @@ describe("dyld path expansion", () => {
}),
[plugin]: parsed(
slice({
file_type: "bundle",
file_type: "dylib",
dependencies: [dependency("@rpath/libshared.dylib")],
}),
),
@@ -302,10 +302,13 @@ describe("dyld resolution outcomes", () => {
trace.findings.map(({ kind, edge_index: index }) => [kind, index]),
).toEqual([
["earlier-rpath-candidate-absent", 0],
["weak-load-unresolved", 1],
["required-load-unresolved", 2],
["dyld-environment-present", null],
]);
expect(trace.edges.map(({ resolution }) => resolution.status)).toEqual([
"conditional",
"undetermined",
"undetermined",
]);
expect(trace.findings[0]?.explanation).toContain(
"Contents/MacOS/../Overrides/libfound.dylib",
);
@@ -540,7 +543,7 @@ const cache = (
].includes(path)
? "mapped"
: "absent",
image: () => Promise.resolve(parsed(slice())),
image: () => Promise.resolve(parsed(slice({ architecture: "arm64e" }))),
...overrides,
});
@@ -830,3 +833,96 @@ describe("conditional loads", () => {
).toContain(`${VENDOR} loads only conditionally; if it loads,`);
});
});
describe("dependency loadability", () => {
it.each(["execute", "bundle", "other"] as const)(
"does not load cached or in-root %s images as dependencies",
async (fileType) => {
for (const useCache of [true, false]) {
const path = useCache
? "/usr/lib/libSystem.B.dylib"
: "@executable_path/libinvalid.dylib";
const trace = await traceDylibLoading(
memoryView({
[MAIN]: executable({ dependencies: [dependency(path)] }),
"Contents/MacOS/libinvalid.dylib": parsed(
slice({ file_type: fileType }),
),
}),
{
roots: [MAIN],
...(useCache
? {
sharedCache: cache({
image: async () =>
parsed(
slice({ architecture: "arm64e", file_type: fileType }),
),
}),
}
: {}),
},
);
expect(trace.edges[0]?.resolution).toEqual({
status: "undetermined",
image: null,
});
}
},
);
it.each([{ platforms: [] }, { platforms: [{ id: 2, name: "ios" }] }])(
"keeps cached unknown or incompatible platforms uncertain (%j)",
async ({ platforms }) => {
const trace = await traceDylibLoading(
memoryView({
[MAIN]: executable({
dependencies: [dependency("/usr/lib/libSystem.B.dylib")],
}),
}),
{
roots: [MAIN],
sharedCache: cache({
image: async () =>
parsed(slice({ architecture: "arm64e", platforms })),
}),
},
);
expect(trace.edges[0]?.resolution.status).toBe("undetermined");
expect(trace.coverage.status).toBe("partial");
},
);
});
it("propagates embedded search-path uncertainty through found, missing, lazy and descendant edges", async () => {
const child = "Contents/MacOS/child.dylib";
const trace = await traceDylibLoading(
memoryView({
[MAIN]: executable({
dyld_environment: ["DYLD_LIBRARY_PATH=/tmp"],
dependencies: [
dependency("@executable_path/child.dylib"),
dependency("@executable_path/missing.dylib"),
dependency("@executable_path/lazy.dylib", {
command: "LC_LAZY_LOAD_DYLIB",
}),
dependency("/usr/lib/libSystem.B.dylib"),
],
}),
[child]: parsed(
slice({ dependencies: [dependency("@loader_path/gone.dylib")] }),
),
}),
{ roots: [MAIN], sharedCache: cache() },
);
expect(trace.edges.map(({ resolution }) => resolution.status)).toEqual([
"conditional",
"undetermined",
"undetermined",
"conditional",
"undetermined",
]);
expect(trace.edges.at(-1)?.loader_conditional).toBe(true);
expect(trace.findings.map(({ kind }) => kind)).toEqual([
"dyld-environment-present",
]);
});
+39 -10
View File
@@ -1,5 +1,6 @@
import {
compatibleSlice,
cacheServes,
directoryOf,
expandPrefix,
joinPath,
@@ -55,13 +56,15 @@ interface ProcessContext {
readonly view: DylibTreeView;
readonly root: string;
readonly architecture: string;
readonly platforms: MachoSlice["platforms"];
readonly searchPathsUnknown: boolean;
readonly executable: string | null;
readonly loaded: Map<string, LoadedImage>;
readonly byInstallName: Map<string, string>;
readonly images: Map<string, MachoImageFacts>;
/** The supplied cache, when it serves this process's architecture and platform. */
readonly sharedCache: DylibSharedCacheView | undefined;
/** Cached install paths whose subcache bytes were unavailable, across processes. */
/** Cached install paths whose bytes or loadability could not be verified. */
readonly unverifiedCacheImages: Set<string>;
}
@@ -135,7 +138,14 @@ const evaluateCandidate = async (
const facts = await sharedCache.image(template.expansion.path);
if (
facts?.status === "parsed" &&
compatibleSlice(facts.slices, context.architecture) !== undefined
facts.slices.some(
(slice) =>
cacheServes(slice.architecture, context.architecture) &&
slice.file_type === "dylib" &&
slice.platforms.some(({ id }) =>
context.platforms.some((platform) => platform.id === id),
),
)
)
return {
...base,
@@ -160,13 +170,24 @@ const evaluateCandidate = async (
if (lookup.kind !== "file")
return { ...base, outcome: "absent", resolved_path: null };
const facts = await imageFacts(context, lookup.path);
const outcome: Candidate["outcome"] =
facts.status !== "parsed"
? facts.status
: compatibleSlice(facts.slices, context.architecture) === undefined
? "architecture-missing"
: "resolved";
return { ...base, outcome, resolved_path: lookup.path };
if (facts.status !== "parsed")
return { ...base, outcome: facts.status, resolved_path: lookup.path };
const slice = compatibleSlice(facts.slices, context.architecture);
if (slice === undefined)
return {
...base,
outcome: "architecture-missing",
resolved_path: lookup.path,
};
if (slice.file_type !== "dylib")
return { ...base, outcome: "unsupported", resolved_path: lookup.path };
if (
!slice.platforms.some(({ id }) =>
context.platforms.some((platform) => platform.id === id),
)
)
return { ...base, outcome: "undetermined", resolved_path: lookup.path };
return { ...base, outcome: "resolved", resolved_path: lookup.path };
};
const imageFacts = async (
@@ -234,7 +255,13 @@ const resolveDependency = async (
)
break;
}
const searched = resolution(candidates);
const modeled = resolution(candidates);
const searched: Edge["resolution"] = context.searchPathsUnknown
? {
...modeled,
status: modeled.image === null ? "undetermined" : "conditional",
}
: modeled;
// Reusing an image that itself loads only conditionally is conditional too.
const resolved: Edge["resolution"] =
loaded !== undefined &&
@@ -368,6 +395,8 @@ export const traceDylibLoading = async (
view,
root,
architecture: slice.architecture,
platforms: slice.platforms,
searchPathsUnknown: slice.dyld_environment.length > 0,
executable: slice.file_type === "execute" ? root : null,
loaded: new Map(),
byInstallName: new Map(),
+2 -2
View File
@@ -82,7 +82,7 @@ export interface DylibSharedCacheView {
/**
* `mapped`: listed and inside an admitted mapping. That is address coverage,
* not a load: a shared-cache hit also requires `image` to parse a slice this
* process can load. `unverified`: listed, but the subcache holding it is
* process can load as a dylib on its platform. `unverified`: listed, but the subcache holding it is
* unavailable. `absent`: not listed.
*/
lookup(path: string): "mapped" | "unverified" | "absent";
@@ -191,7 +191,7 @@ export const dylibResolutionResultSchema = z.strictObject({
status: z.enum(["complete", "partial"]),
unparsed_images: z.array(z.string()),
roots_without_architecture: z.array(z.string()),
/** Install paths the supplied cache lists in subcaches that were unavailable. */
/** Listed cache paths whose bytes or process-compatible dylib representation could not be verified. */
unverified_shared_cache_images: z.array(z.string()),
}),
limitations: z.array(z.string().min(1)),
+9 -1
View File
@@ -1,4 +1,5 @@
import { z } from "zod";
import { AnalysisInputError } from "../analysisErrorCore.js";
import { jsonValueSchema, type JsonValue } from "../jsonValue.js";
/** Select one archive within the active bundle and optionally one named root. */
@@ -85,7 +86,14 @@ export const projectKeyedArchive = (
if (top === undefined || Object.keys(top).length === 0)
throw new TypeError("Keyed archive has no $top roots");
if (selection.root !== undefined && !Object.hasOwn(top, selection.root))
throw new TypeError(`Archive root does not exist: ${selection.root}`);
throw new AnalysisInputError("inspect_keyed_archive", undefined, [
{
path: ["root"],
reason: "invalid_value",
message: `Archive root does not exist: ${selection.root}. Select one of the archive's $top roots or omit root.`,
expected: Object.keys(top),
},
]);
const roots =
selection.root === undefined
? top