mirror of
https://github.com/morluto/rea.git
synced 2026-11-22 22:45:47 +00:00
refactor(artifacts): share cached prefix reader and ELF/MZ magic (#13663)
refactor(filesystem/artifacts): share createCachedPrefixReader and ELF/MZ magic helpers
This commit is contained in:
1 parent
b14693914c
commit
baed75fc2b
11 files changed
+76
-53
No files matched your search
@@ -8,8 +8,12 @@ import {
|
||||
artifactNameForClassification,
|
||||
namedArtifactFormat,
|
||||
} from "../domain/artifactNameSelection.js";
|
||||
import { readFileHandleRange } from "../filesystem/readFileHandleRange.js";
|
||||
import {
|
||||
createCachedPrefixReader,
|
||||
readFileHandleRange,
|
||||
} from "../filesystem/readFileHandleRange.js";
|
||||
import { readPeCoffHeader } from "../domain/peCoffHeader.js";
|
||||
import { hasDosMzMagic } from "../domain/dosMz.js";
|
||||
import { hasXarMagic } from "../domain/binaryContainer.js";
|
||||
import {
|
||||
regularFileCancellation,
|
||||
@@ -778,22 +782,12 @@ const detectArtifactFormat = async (
|
||||
"WebAssembly v1 artifacts are not supported by the native opening route",
|
||||
{ constraint: "wasm_requires_static_inspection" },
|
||||
);
|
||||
const readCachedRange = async (
|
||||
position: number,
|
||||
length: number,
|
||||
): Promise<Uint8Array> => {
|
||||
throwIfTargetResolutionCancelled(signal);
|
||||
if (position >= 0 && length >= 0) {
|
||||
if (position + length <= magic.length)
|
||||
return magic.subarray(position, position + length);
|
||||
if (magic.length >= initial.size)
|
||||
return magic.subarray(
|
||||
Math.min(position, initial.size),
|
||||
Math.min(position + length, initial.size),
|
||||
);
|
||||
}
|
||||
return readHandleRange(handle, position, length, signal);
|
||||
};
|
||||
const readCachedRange = createCachedPrefixReader(
|
||||
handle,
|
||||
magic,
|
||||
initial.size,
|
||||
() => throwIfTargetResolutionCancelled(signal),
|
||||
);
|
||||
// The shared recognizer table decides; this route reads the trailer or
|
||||
// locates an ESP flash layout only when a recognizer asks for it, and
|
||||
// refuses a file whose header names a format it cannot open.
|
||||
@@ -1018,7 +1012,7 @@ const readExecutableMetadata = async (
|
||||
// Header commitments such as Mach-O load commands and FAT slices are
|
||||
// checked against the whole file, not the probed prefix.
|
||||
const fileSize = initial.size;
|
||||
if (bytes.length >= 2 && bytes[0] === 0x4d && bytes[1] === 0x5a) {
|
||||
if (hasDosMzMagic(bytes)) {
|
||||
let evidence = parseMzHeaderEvidence(bytes, fileSize);
|
||||
let signatureEvidence:
|
||||
| { readonly offset: number; readonly bytes: Buffer }
|
||||
|
||||
@@ -12,6 +12,7 @@ import {
|
||||
type FatSliceHeaderEvidence,
|
||||
} from "../domain/apple/machoContainer.js";
|
||||
import {
|
||||
hasDosMzMagic,
|
||||
observeMzWindowsHeader,
|
||||
type MzWindowsHeaderRecordEvidence,
|
||||
type MzWindowsHeaderSignatureEvidence,
|
||||
@@ -299,7 +300,7 @@ export const createArtifactClassificationProbe = (
|
||||
if (!mzHeaderChecked && retainedBytes >= 64) {
|
||||
mzHeaderChecked = true;
|
||||
const prefix = retainedPrefix();
|
||||
if (prefix[0] === 0x4d && prefix[1] === 0x5a) {
|
||||
if (hasDosMzMagic(prefix)) {
|
||||
const windowsHeader = observeMzWindowsHeader(
|
||||
prefix,
|
||||
Number.MAX_SAFE_INTEGER,
|
||||
|
||||
@@ -5,7 +5,7 @@ import {
|
||||
declaredFileRange,
|
||||
} from "../../domain/binaryContainer.js";
|
||||
import { err, ok, type Result } from "../../domain/result.js";
|
||||
import { parseMzHeaderEvidence } from "../../domain/dosMz.js";
|
||||
import { hasDosMzMagic, parseMzHeaderEvidence } from "../../domain/dosMz.js";
|
||||
import {
|
||||
observePeCoffFrame,
|
||||
PE_COFF_FRAME_BYTES,
|
||||
@@ -730,7 +730,7 @@ const peClaims = async (
|
||||
): Promise<readonly SectionWindow[]> => {
|
||||
const { handle, fileSize, signal } = source;
|
||||
const dos = await tryRead(handle, 0, 64, fileSize, signal);
|
||||
if (dos === null || dos[0] !== 0x4d || dos[1] !== 0x5a) return [];
|
||||
if (dos === null || !hasDosMzMagic(dos)) return [];
|
||||
const probe = observePeCoffFrame(
|
||||
dos,
|
||||
fileSize,
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { BinaryReader } from "../../domain/binaryContainer.js";
|
||||
import { BinaryReader, hasElfMagic } from "../../domain/binaryContainer.js";
|
||||
import { hasDosMzMagic } from "../../domain/dosMz.js";
|
||||
import { LC } from "../../domain/apple/machoLoadCommandCodes.js";
|
||||
import {
|
||||
hashArtifactRange,
|
||||
@@ -181,9 +182,9 @@ const findDenoSection = async (
|
||||
const fat = readFatHeaderPrefix(prefix);
|
||||
if (thin !== null || fat.state === "incomplete" || fat.state === "parsed")
|
||||
return collapseRanges(carrier, "mach-o", await locateMachO(carrier));
|
||||
if (prefix.subarray(0, 4).equals(Buffer.from([0x7f, 0x45, 0x4c, 0x46])))
|
||||
if (hasElfMagic(prefix))
|
||||
return collapseRanges(carrier, "elf", await locateElfNotes(carrier));
|
||||
if (prefix[0] === 0x4d && prefix[1] === 0x5a)
|
||||
if (hasDosMzMagic(prefix))
|
||||
return collapseRanges(
|
||||
carrier,
|
||||
"pe",
|
||||
|
||||
@@ -33,7 +33,7 @@ import {
|
||||
zipPackageFormatForPath,
|
||||
type ZipPackageFormat,
|
||||
} from "../../domain/zipPackageFormat.js";
|
||||
import { parseMzHeaderEvidence } from "../../domain/dosMz.js";
|
||||
import { hasDosMzMagic, parseMzHeaderEvidence } from "../../domain/dosMz.js";
|
||||
import {
|
||||
classifyFatContainerEvidence,
|
||||
readFatContainerHeader,
|
||||
@@ -641,7 +641,7 @@ export const classifyArtifactBytes = (
|
||||
const native = readNativeHeaderFormat(prefix, fileSize);
|
||||
if (!native.ok) return "unknown";
|
||||
if (native.value !== null) return native.value;
|
||||
if (prefix.length >= 2 && prefix[0] === 0x4d && prefix[1] === 0x5a) {
|
||||
if (hasDosMzMagic(prefix)) {
|
||||
if (
|
||||
!Number.isSafeInteger(fileSize) ||
|
||||
fileSize < prefix.length ||
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import { probeUefiFlashImage } from "../../uefi/UefiFormatProbe.js";
|
||||
import { ParseFailureError } from "../../domain/parseFailure.js";
|
||||
import { readFileHandleRange } from "../../filesystem/readFileHandleRange.js";
|
||||
import {
|
||||
createCachedPrefixReader,
|
||||
readFileHandleRange,
|
||||
} from "../../filesystem/readFileHandleRange.js";
|
||||
import {
|
||||
artifactOperationCause,
|
||||
ArtifactReaderFailure,
|
||||
@@ -339,23 +342,12 @@ const classifyRootFormat = async (
|
||||
throw artifactOperationCause(cause);
|
||||
}
|
||||
// Fill each range: a short result is end of file, not a partial host read.
|
||||
const read = async (position: number, length: number): Promise<Buffer> => {
|
||||
abortIfNeeded(signal);
|
||||
if (position >= 0 && length >= 0) {
|
||||
if (position + length <= evidence.prefix.length)
|
||||
return evidence.prefix.subarray(position, position + length);
|
||||
if (evidence.prefix.length >= archiveBytes)
|
||||
return evidence.prefix.subarray(
|
||||
Math.min(position, archiveBytes),
|
||||
Math.min(position + length, archiveBytes),
|
||||
);
|
||||
}
|
||||
const bytes = await readFileHandleRange(handle, position, length, () =>
|
||||
abortIfNeeded(signal),
|
||||
);
|
||||
abortIfNeeded(signal);
|
||||
return bytes;
|
||||
};
|
||||
const read = createCachedPrefixReader(
|
||||
handle,
|
||||
evidence.prefix,
|
||||
archiveBytes,
|
||||
() => abortIfNeeded(signal),
|
||||
);
|
||||
const locateFlash = (selection: EspFlashSelection, explicit: boolean) =>
|
||||
locateEspFlashLayout(archiveBytes, read, selection, {
|
||||
explicit,
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { hasElfMagic } from "../../domain/binaryContainer.js";
|
||||
import { hasDosMzMagic } from "../../domain/dosMz.js";
|
||||
import { LC } from "../../domain/apple/machoLoadCommandCodes.js";
|
||||
import {
|
||||
hashArtifactRange,
|
||||
@@ -162,13 +164,13 @@ export const locateNodeSea = async (
|
||||
: await locateFatMachO(carrier, prefix);
|
||||
return collapseRanges(carrier, "mach-o", ranges);
|
||||
}
|
||||
if (prefix.subarray(0, 4).equals(Buffer.from([0x7f, 0x45, 0x4c, 0x46])))
|
||||
if (hasElfMagic(prefix))
|
||||
return collapseRanges(
|
||||
carrier,
|
||||
"elf",
|
||||
await locateElfNotes(carrier, prefix),
|
||||
);
|
||||
if (prefix[0] === 0x4d && prefix[1] === 0x5a)
|
||||
if (hasDosMzMagic(prefix))
|
||||
return collapseRanges(
|
||||
carrier,
|
||||
"pe",
|
||||
|
||||
@@ -27,6 +27,7 @@ import { readPeSectionHeader } from "./peSectionHeader.js";
|
||||
import { isStrictUtf8 } from "./strictUtf8.js";
|
||||
import { readElfFixedHeader } from "./elfFixedHeader.js";
|
||||
import {
|
||||
hasDosMzMagic,
|
||||
mzWindowsHeaderLayoutLimitation,
|
||||
parseMzHeaderEvidence,
|
||||
type MzHeaderEvidence,
|
||||
@@ -129,7 +130,7 @@ export const claimsExecutableHeader = (
|
||||
(bytes.length >= 4 &&
|
||||
(hasElfMagic(bytes) ||
|
||||
readMachoHeaderPrefixFacts(bytes).format !== null)) ||
|
||||
(bytes.length >= 2 && bytes[0] === 0x4d && bytes[1] === 0x5a) ||
|
||||
hasDosMzMagic(bytes) ||
|
||||
readFatContainerHeader(bytes, fileSize).state !== "not-fat";
|
||||
|
||||
/**
|
||||
@@ -145,7 +146,7 @@ export const parseExecutableHeader = (
|
||||
fatEvidence?: FatSliceHeaderEvidence,
|
||||
): Result<ExecutableMetadata, string> => {
|
||||
if (hasElfMagic(bytes)) return parseElf(bytes, fileSize);
|
||||
if (bytes.length >= 2 && bytes[0] === 0x4d && bytes[1] === 0x5a)
|
||||
if (hasDosMzMagic(bytes))
|
||||
return parseMzExecutableMetadata(
|
||||
bytes,
|
||||
parseMzHeaderEvidence(bytes, fileSize, mzWindowsHeaderSignature),
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { indexPeSectionRvas } from "../pe/peSectionRvaIndex.js";
|
||||
import { hasDosMzMagic } from "../dosMz.js";
|
||||
import { BinaryReader } from "../binaryContainer.js";
|
||||
import { observeMzWindowsHeader } from "../dosMz.js";
|
||||
import {
|
||||
@@ -458,7 +459,7 @@ const readCliDirectory = (
|
||||
/** Parse the file-backed PE and CLI layout without loading target code. */
|
||||
export const readManagedPeLayout = (bytes: Buffer): ManagedPeLayout => {
|
||||
requireRange(bytes, 0, 64, "pe.dos-header");
|
||||
if (bytes[0] !== 0x4d || bytes[1] !== 0x5a)
|
||||
if (!hasDosMzMagic(bytes))
|
||||
throw managedFailure(
|
||||
"invalid-directory",
|
||||
"pe.dos-header",
|
||||
|
||||
@@ -41,3 +41,35 @@ export const readFileHandleRange = async (
|
||||
}
|
||||
return bytes.subarray(0, filled);
|
||||
};
|
||||
|
||||
/**
|
||||
* Serve positional reads from an already-admitted prefix when covered, falling
|
||||
* back to `readFileHandleRange` for ranges beyond the prefix.
|
||||
*/
|
||||
export const createCachedPrefixReader =
|
||||
(
|
||||
handle: Pick<FileHandle, "read">,
|
||||
prefix: Buffer,
|
||||
fileSize: number,
|
||||
assertActive: (bytesRead: number) => void = () => {},
|
||||
): ((position: number, length: number) => Promise<Buffer>) =>
|
||||
async (position, length) => {
|
||||
assertActive(0);
|
||||
if (position >= 0 && length >= 0) {
|
||||
if (position + length <= prefix.length)
|
||||
return prefix.subarray(position, position + length);
|
||||
if (prefix.length >= fileSize)
|
||||
return prefix.subarray(
|
||||
Math.min(position, fileSize),
|
||||
Math.min(position + length, fileSize),
|
||||
);
|
||||
}
|
||||
const bytes = await readFileHandleRange(
|
||||
handle,
|
||||
position,
|
||||
length,
|
||||
assertActive,
|
||||
);
|
||||
assertActive(bytes.length);
|
||||
return bytes;
|
||||
};
|
||||
@@ -33,8 +33,10 @@ import {
|
||||
BinaryFormatFailure,
|
||||
BinaryReader,
|
||||
BinaryResourceFailure,
|
||||
hasElfMagic,
|
||||
isBinaryStringReadLimit,
|
||||
} from "../domain/binaryContainer.js";
|
||||
import { hasDosMzMagic } from "../domain/dosMz.js";
|
||||
import {
|
||||
readElfSectionDirectory,
|
||||
readPeSectionDirectory,
|
||||
@@ -194,10 +196,7 @@ export class GpuInventory {
|
||||
await this.#hip(source, depth);
|
||||
return "hip-bundle";
|
||||
}
|
||||
if (
|
||||
prefix.subarray(0, 4).equals(Buffer.from([0x7f, 0x45, 0x4c, 0x46])) ||
|
||||
(prefix[0] === 0x4d && prefix[1] === 0x5a)
|
||||
) {
|
||||
if (hasElfMagic(prefix) || hasDosMzMagic(prefix)) {
|
||||
const elf = prefix[0] === 0x7f;
|
||||
const directory = await readGpuNativeDirectory(
|
||||
source,
|
||||
|
||||
Reference in new issue
Block a user