mirror of
https://github.com/morluto/rea.git
synced 2026-11-23 15:05:51 +00:00
fix(native): share ELF, PE, and Mach-O framing across artifact readers (#12156)
* fix(elf): share dynamic table singleton validation * fix(elf): share note storage and framing across readers Refs #12130 * fix(elf): retain zero-filled load mapping ambiguity Refs #12136 * fix(pe): share header extent admission Refs #12143 * fix(macho): share embedded section backing checks Refs #12142 * fix(pe): share header facts and validate resource directory order * fix(macho): require complete carrier load command framing * fix(pe): probe declared resource headers by file offset * fix(elf): preserve zero-fill observations across mapping gaps * refactor(pe): share positional COFF header observation (#12173) * fix(elf): check load-map agreement across section extents (#12175) * fix(elf): check load-map agreement across section extents * fix(elf): preserve agreed section links across unreadable tables
This commit is contained in:
1 parent
089fce8614
commit
0b19b06bf6
47 files changed
+1900
-913
No files matched your search
@@ -8,6 +8,11 @@ import {
|
||||
readMachoLoadCommandTableHeader,
|
||||
MachoLoadCommandCursor,
|
||||
} from "../../domain/apple/machoLoadCommands.js";
|
||||
import {
|
||||
machoSectionBacking,
|
||||
readMachoSectionHeader,
|
||||
} from "../../domain/apple/machoSectionHeader.js";
|
||||
import { readMachoSegmentHeader } from "../../domain/apple/machoSegmentHeader.js";
|
||||
import type { FileHandle } from "node:fs/promises";
|
||||
|
||||
import {
|
||||
@@ -33,8 +38,6 @@ export const BUN_RECORD_BYTES_1_3_3 = 36;
|
||||
export const BUN_RECORD_BYTES_1_3_9 = 52;
|
||||
|
||||
const MAX_PE_SECTIONS = 96;
|
||||
const SEGMENT_64_HEADER_BYTES = 72;
|
||||
const SEGMENT_32_HEADER_BYTES = 56;
|
||||
const SECTION_64_BYTES = 80;
|
||||
const SECTION_32_BYTES = 68;
|
||||
|
||||
@@ -116,12 +119,15 @@ interface BunProbeSource {
|
||||
interface SectionWindow {
|
||||
readonly carrier: Exclude<BunCarrier, "appended-trailer">;
|
||||
readonly fileOffset: number;
|
||||
readonly declaredSize: number;
|
||||
readonly available: number;
|
||||
/** The declared section extent ends past EOF. */
|
||||
readonly truncated: boolean;
|
||||
/** The Mach-O table could not be fully framed after this section was located. */
|
||||
readonly loadCommandsFailure?: string;
|
||||
}
|
||||
|
||||
type SectionClaim = SectionWindow | { readonly failure: ArtifactReaderFailure };
|
||||
|
||||
/**
|
||||
* Locate at most one graph claim with bounded reads of headers, length
|
||||
* prefixes, and suffixes. Payload data is not read. Multiple real claims are
|
||||
@@ -135,7 +141,7 @@ export const probeBunStandaloneGraph = async (
|
||||
): Promise<BunStandaloneProbe> => {
|
||||
abortIfNeeded(signal);
|
||||
const source = { handle, fileSize, signal, limits };
|
||||
let claims: readonly SectionWindow[];
|
||||
let claims: readonly SectionClaim[];
|
||||
try {
|
||||
claims = [
|
||||
...(await machOClaims(source, 0, fileSize)),
|
||||
@@ -150,7 +156,10 @@ export const probeBunStandaloneGraph = async (
|
||||
const real: BunStandaloneProbe[] = [];
|
||||
for (const claim of claims) {
|
||||
abortIfNeeded(signal);
|
||||
const evaluated = await evaluateSection(source, claim);
|
||||
const evaluated =
|
||||
"failure" in claim
|
||||
? presentFailure(claim.failure)
|
||||
: await evaluateSection(source, claim);
|
||||
if (evaluated.status === "present") {
|
||||
if (real.length > 0)
|
||||
return presentFailure(
|
||||
@@ -380,6 +389,10 @@ const evaluateSection = async (
|
||||
);
|
||||
const failure = suffixFailure(suffix, length, limits);
|
||||
if (failure !== undefined) return presentFailure(failure);
|
||||
if (claim.loadCommandsFailure !== undefined)
|
||||
return presentFailure(
|
||||
bunFormatFailure(`Mach-O load commands: ${claim.loadCommandsFailure}`),
|
||||
);
|
||||
return {
|
||||
status: "present",
|
||||
claim: { carrier: claim.carrier, payloadOffset, payloadLength: length },
|
||||
@@ -473,7 +486,7 @@ const machOClaims = async (
|
||||
source: BunProbeSource,
|
||||
imageOffset: number,
|
||||
imageLimit: number,
|
||||
): Promise<readonly SectionWindow[]> => {
|
||||
): Promise<readonly SectionClaim[]> => {
|
||||
const { handle, fileSize, signal, limits } = source;
|
||||
if (imageOffset < 0 || imageOffset >= fileSize) return [];
|
||||
const header = await tryRead(handle, imageOffset, 12, fileSize, signal);
|
||||
@@ -512,12 +525,16 @@ const machOClaims = async (
|
||||
signal,
|
||||
);
|
||||
if (commands === null || commands.length !== sizeofcmds) return [];
|
||||
const claims: SectionWindow[] = [];
|
||||
const claims: SectionClaim[] = [];
|
||||
const cursor = new MachoLoadCommandCursor(table);
|
||||
let framingFailure: string | undefined;
|
||||
while (cursor.nextOffset !== null) {
|
||||
abortIfNeeded(signal);
|
||||
const read = cursor.read(commands.subarray(cursor.nextOffset));
|
||||
if (!read.ok) break;
|
||||
if (!read.ok) {
|
||||
framingFailure = read.error;
|
||||
break;
|
||||
}
|
||||
const { command: cmd, offset, size } = read.value;
|
||||
if (cmd === LC.SEGMENT_64 || cmd === LC.SEGMENT)
|
||||
for (const claim of segmentClaims(
|
||||
@@ -529,7 +546,15 @@ const machOClaims = async (
|
||||
))
|
||||
claims.push(claim);
|
||||
}
|
||||
return claims;
|
||||
const complete = cursor.finish();
|
||||
if (!complete.ok) framingFailure ??= complete.error;
|
||||
return framingFailure === undefined
|
||||
? claims
|
||||
: claims.map((claim) =>
|
||||
"failure" in claim
|
||||
? claim
|
||||
: { ...claim, loadCommandsFailure: framingFailure },
|
||||
);
|
||||
};
|
||||
|
||||
const segmentClaims = (
|
||||
@@ -538,34 +563,44 @@ const segmentClaims = (
|
||||
wide: boolean,
|
||||
imageOffset: number,
|
||||
fileSize: number,
|
||||
): readonly SectionWindow[] => {
|
||||
const headerBytes = wide ? SEGMENT_64_HEADER_BYTES : SEGMENT_32_HEADER_BYTES;
|
||||
): readonly SectionClaim[] => {
|
||||
const parsedSegment = readMachoSegmentHeader(command, wide, littleEndian);
|
||||
if (!parsedSegment.ok) return [];
|
||||
const segment = parsedSegment.value;
|
||||
const { headerSize, sectionCount } = segment;
|
||||
const sectionBytes = wide ? SECTION_64_BYTES : SECTION_32_BYTES;
|
||||
if (command.length < headerBytes) return [];
|
||||
const nsects = readU32(command, wide ? 64 : 48, littleEndian);
|
||||
if (headerBytes + nsects * sectionBytes > command.length) return [];
|
||||
const claims: SectionWindow[] = [];
|
||||
for (let index = 0; index < nsects; index += 1) {
|
||||
const start = headerBytes + index * sectionBytes;
|
||||
const section = command.subarray(start, start + sectionBytes);
|
||||
if (!exactName(section.subarray(0, 16), "__bun")) continue;
|
||||
if (!exactName(section.subarray(16, 32), "__BUN")) continue;
|
||||
const size = wide
|
||||
? readBoundedU64(section, 40, littleEndian)
|
||||
: readU32(section, 36, littleEndian);
|
||||
const offset = readU32(section, wide ? 48 : 40, littleEndian);
|
||||
if (size === undefined) {
|
||||
if (headerSize + sectionCount * sectionBytes > command.length) return [];
|
||||
const claims: SectionClaim[] = [];
|
||||
for (let index = 0; index < sectionCount; index += 1) {
|
||||
const start = headerSize + index * sectionBytes;
|
||||
const bytes = command.subarray(start, start + sectionBytes);
|
||||
if (!exactName(bytes.subarray(0, 16), "__bun")) continue;
|
||||
if (!exactName(bytes.subarray(16, 32), "__BUN")) continue;
|
||||
const parsedSection = readMachoSectionHeader(bytes, wide, littleEndian);
|
||||
if (!parsedSection.ok) continue;
|
||||
const section = parsedSection.value;
|
||||
const { issue } = machoSectionBacking(section, segment);
|
||||
if (issue !== null) {
|
||||
claims.push({
|
||||
carrier: "mach-o-section",
|
||||
fileOffset: imageOffset + offset,
|
||||
declaredSize: Number.MAX_SAFE_INTEGER,
|
||||
available: 0,
|
||||
truncated: false,
|
||||
failure: bunFormatFailure(`Mach-O __bun section ${issue}`),
|
||||
});
|
||||
continue;
|
||||
}
|
||||
if (section.size > BigInt(Number.MAX_SAFE_INTEGER)) {
|
||||
claims.push({
|
||||
failure: bunFormatFailure(
|
||||
"Mach-O __bun section size is not a safe integer",
|
||||
),
|
||||
});
|
||||
continue;
|
||||
}
|
||||
claims.push(
|
||||
windowFor("mach-o-section", imageOffset + offset, size, fileSize),
|
||||
windowFor(
|
||||
"mach-o-section",
|
||||
imageOffset + section.offset,
|
||||
Number(section.size),
|
||||
fileSize,
|
||||
),
|
||||
);
|
||||
}
|
||||
return claims;
|
||||
@@ -574,7 +609,7 @@ const segmentClaims = (
|
||||
const fatClaims = async (
|
||||
source: BunProbeSource,
|
||||
prefix: Buffer,
|
||||
): Promise<readonly SectionWindow[]> => {
|
||||
): Promise<readonly SectionClaim[]> => {
|
||||
const { handle, fileSize, signal } = source;
|
||||
let parsed = readFatContainerHeader(prefix, fileSize);
|
||||
if (parsed.state === "needs-data") {
|
||||
@@ -589,7 +624,7 @@ const fatClaims = async (
|
||||
parsed = readFatContainerHeader(table, fileSize);
|
||||
}
|
||||
if (parsed.state !== "parsed") return [];
|
||||
const claims: SectionWindow[] = [];
|
||||
const claims: SectionClaim[] = [];
|
||||
for (const slice of parsed.slices) {
|
||||
abortIfNeeded(signal);
|
||||
if (parsed.rangeIssues[slice.index] !== null) continue;
|
||||
@@ -766,7 +801,6 @@ const windowFor = (
|
||||
return {
|
||||
carrier,
|
||||
fileOffset,
|
||||
declaredSize,
|
||||
available: 0,
|
||||
truncated: false,
|
||||
};
|
||||
@@ -774,7 +808,6 @@ const windowFor = (
|
||||
return {
|
||||
carrier,
|
||||
fileOffset,
|
||||
declaredSize,
|
||||
available: Math.min(declaredSize, room),
|
||||
truncated: declaredSize > room,
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user