diff --git a/src/artifacts/apple/DylibFileAccess.ts b/src/artifacts/apple/DylibFileAccess.ts index 76caa90001..029184cf35 100644 --- a/src/artifacts/apple/DylibFileAccess.ts +++ b/src/artifacts/apple/DylibFileAccess.ts @@ -31,38 +31,58 @@ export class DylibFileAccess { private readonly signal?: AbortSignal, ) {} - startsWithMachoMagic(entry: ArtifactEntry): Promise { + /** + * Check the 4-byte Mach-O magic and, on a match, parse the image headers on + * the same open descriptor so root discovery avoids a second open/stat cycle. + */ + readMachoIfMagic(entry: ArtifactEntry): Promise< + | { + readonly facts: Awaited>; + readonly identity: BigIntStats; + } + | undefined + > { return this.#read(entry.adapterKey, async (file, initial) => { if (!sameArtifactSourceIdentity(entry.sourceIdentity, initial)) throw changed(entry.adapterKey); - const bytes = await readFileHandleRange(file, 0, 4, () => + const magic = await readFileHandleRange(file, 0, 4, () => assertArtifactReadActive(this.signal), ); assertArtifactReadActive(this.signal); - return hasMachoMagic(bytes); + if (!hasMachoMagic(magic)) return undefined; + return this.#image(entry.adapterKey, file, initial, magic); }); } image(path: string) { - return this.#read(path, async (file, identity) => { - const size = Number(identity.size); - if (!Number.isSafeInteger(size)) - throw new ArtifactReaderFailure( - "limit", - `Mach-O file extent is not safely addressable: ${path}`, - ); - const facts = await readMachoImage(async (offset, length) => { - const bytes = await readFileHandleRange( - file, - offset, - Math.max(0, Math.min(length, size - offset)), - () => assertArtifactReadActive(this.signal), - ); - assertArtifactReadActive(this.signal); - return bytes; - }, size); - return { facts, identity }; - }); + return this.#read(path, (file, identity) => + this.#image(path, file, identity), + ); + } + + async #image( + path: string, + file: FileHandle, + identity: BigIntStats, + prefix?: Buffer, + ) { + const size = Number(identity.size); + if (!Number.isSafeInteger(size)) + throw new ArtifactReaderFailure( + "limit", + `Mach-O file extent is not safely addressable: ${path}`, + ); + const facts = await readMachoImage(async (offset, length) => { + const clamped = Math.max(0, Math.min(length, size - offset)); + if (prefix !== undefined && offset === 0 && clamped <= prefix.length) + return prefix.subarray(0, clamped); + const bytes = await readFileHandleRange(file, offset, clamped, () => + assertArtifactReadActive(this.signal), + ); + assertArtifactReadActive(this.signal); + return bytes; + }, size); + return { facts, identity }; } sha256(path: string, expected: BigIntStats | undefined): Promise { diff --git a/src/artifacts/apple/DylibResolutionReader.ts b/src/artifacts/apple/DylibResolutionReader.ts index fdbcf1864b..bfbb94e545 100644 --- a/src/artifacts/apple/DylibResolutionReader.ts +++ b/src/artifacts/apple/DylibResolutionReader.ts @@ -81,6 +81,13 @@ class FilesystemTreeView implements DylibTreeView { return pending; } + /** Seed an already-verified Mach-O parse so follow-up resolution skips re-opening the file. */ + seedImage(path: string, facts: MachoImageFacts, identity: BigIntStats): void { + if (this.#images.has(path)) return; + this.#images.set(path, Promise.resolve(facts)); + this.#identities.set(path, identity); + } + /** Identity of the file whose header bytes `image()` parsed. */ identity(path: string): BigIntStats | undefined { return this.#identities.get(path); @@ -272,12 +279,13 @@ const executableRoots = async ( firstSymlink ??= entry.path; } return entry.kind === "file" - ? files.startsWithMachoMagic(entry) + ? files.readMachoIfMagic(entry) : undefined; }, - async (entry, macho) => { - if (!macho) return; - const facts = await view.image(entry.path); + async (entry, observed) => { + if (observed === undefined) return; + view.seedImage(entry.path, observed.facts, observed.identity); + const { facts } = observed; if (facts.status === "malformed" || facts.status === "unsupported") unclassified.push(entry.path); else if (