fix(apple): admit only compatible dyld subcache mappings (#1090)
UUID, architecture, and cache VM offset are one admission decision, so a matching UUID cannot publish contradictory mappings. A shared-cache trace hit also requires the mapped bytes to parse as a Mach-O the process can load. Co-authored-by: Cursor Agent <cursoragent@cursor.com>
This commit is contained in:
@@ -540,6 +540,7 @@ const cache = (
|
||||
].includes(path)
|
||||
? "mapped"
|
||||
: "absent",
|
||||
image: () => Promise.resolve(parsed(slice())),
|
||||
...overrides,
|
||||
});
|
||||
|
||||
@@ -597,6 +598,38 @@ describe("shared cache resolution", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("does not treat mapped bytes as a load until they parse as a compatible Mach-O", async () => {
|
||||
const root = memoryView({
|
||||
[MAIN]: executable({
|
||||
dependencies: [dependency("/usr/lib/libSystem.B.dylib")],
|
||||
}),
|
||||
});
|
||||
const notMachO = await traceDylibLoading(root, {
|
||||
roots: [MAIN],
|
||||
sharedCache: cache({
|
||||
image: () => Promise.resolve({ status: "not-mach-o" }),
|
||||
}),
|
||||
});
|
||||
expect(notMachO.edges[0]?.resolution).toEqual({
|
||||
status: "undetermined",
|
||||
image: null,
|
||||
});
|
||||
expect(notMachO.coverage).toMatchObject({
|
||||
status: "partial",
|
||||
unverified_shared_cache_images: ["/usr/lib/libSystem.B.dylib"],
|
||||
});
|
||||
expect(notMachO.images.map(({ path }) => path)).toEqual([MAIN]);
|
||||
const wrongArchitecture = await traceDylibLoading(root, {
|
||||
roots: [MAIN],
|
||||
sharedCache: cache({
|
||||
image: () => Promise.resolve(parsed(slice({ architecture: "x86_64" }))),
|
||||
}),
|
||||
});
|
||||
expect(wrongArchitecture.edges[0]?.candidates[0]?.outcome).toBe(
|
||||
"undetermined",
|
||||
);
|
||||
});
|
||||
|
||||
it("keeps a cache hit conditional after an earlier undeterminable candidate", async () => {
|
||||
const trace = await traceDylibLoading(
|
||||
memoryView({
|
||||
|
||||
@@ -127,14 +127,24 @@ const evaluateCandidate = async (
|
||||
rpath_owner: template.rpathOwner,
|
||||
};
|
||||
if (template.expansion.scope === "outside") {
|
||||
const cached =
|
||||
context.sharedCache?.lookup(template.expansion.path) ?? "absent";
|
||||
if (cached === "mapped")
|
||||
return {
|
||||
...base,
|
||||
outcome: "shared-cache",
|
||||
resolved_path: template.expansion.path,
|
||||
};
|
||||
const sharedCache = context.sharedCache;
|
||||
const cached = sharedCache?.lookup(template.expansion.path) ?? "absent";
|
||||
if (cached === "mapped" && sharedCache !== undefined) {
|
||||
// Address coverage is not a load. Confirm a compatible Mach-O without
|
||||
// retaining its commands, so cache images stay leaves.
|
||||
const facts = await sharedCache.image(template.expansion.path);
|
||||
if (
|
||||
facts?.status === "parsed" &&
|
||||
compatibleSlice(facts.slices, context.architecture) !== undefined
|
||||
)
|
||||
return {
|
||||
...base,
|
||||
outcome: "shared-cache",
|
||||
resolved_path: template.expansion.path,
|
||||
};
|
||||
context.unverifiedCacheImages.add(template.expansion.path);
|
||||
return { ...base, outcome: "undetermined", resolved_path: null };
|
||||
}
|
||||
// Listed in the cache, but its subcache is unavailable: dyld might load it.
|
||||
if (cached === "unverified") {
|
||||
context.unverifiedCacheImages.add(template.expansion.path);
|
||||
|
||||
@@ -80,10 +80,18 @@ export interface DylibSharedCacheView {
|
||||
/** Subcaches whose bytes are missing or do not match, such as `.03 (missing)`. */
|
||||
readonly unavailableSubcaches: readonly string[];
|
||||
/**
|
||||
* `mapped`: listed and inside a verified mapping; `unverified`: listed, but
|
||||
* the subcache holding it is unavailable; `absent`: not listed.
|
||||
* `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
|
||||
* unavailable. `absent`: not listed.
|
||||
*/
|
||||
lookup(path: string): "mapped" | "unverified" | "absent";
|
||||
/**
|
||||
* Load commands at a mapped install path. Undefined when `lookup` is not
|
||||
* `mapped`. Tracing uses this only to confirm the hit; cached load commands
|
||||
* are not traversed.
|
||||
*/
|
||||
image(path: string): Promise<MachoImageFacts | undefined>;
|
||||
}
|
||||
|
||||
const candidateSchema = z.strictObject({
|
||||
|
||||
Reference in New Issue
Block a user