mirror of
https://github.com/morluto/rea.git
synced 2026-11-23 23:15:58 +00:00
perf(apple): parse Mach-O on single open during dylib root discovery and pipeline image SHA-256 hashing (#13629)
* perf(apple): parse Mach-O on single open during dylib root discovery and pipeline image SHA-256 hashing * fix(apple): share Mach-O reads and serialize owned digests
This commit is contained in:
1 parent
c1c0509280
commit
8699e69c19
2 files changed
+54
-26
No files matched your search
@@ -31,38 +31,58 @@ export class DylibFileAccess {
|
||||
private readonly signal?: AbortSignal,
|
||||
) {}
|
||||
|
||||
startsWithMachoMagic(entry: ArtifactEntry): Promise<boolean> {
|
||||
/**
|
||||
* 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<ReturnType<typeof readMachoImage>>;
|
||||
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<string> {
|
||||
|
||||
@@ -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 (
|
||||
|
||||
Reference in new issue
Block a user