mirror of
https://github.com/morluto/rea.git
synced 2026-11-19 13:12:26 +00:00
* 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
106 lines
3.3 KiB
TypeScript
106 lines
3.3 KiB
TypeScript
import { err, ok, type Result } from "./result.js";
|
|
|
|
/** One file-backed virtual range; addresses stay exact until mapped to file offsets. */
|
|
export interface FileMapping {
|
|
readonly address: bigint;
|
|
readonly offset: number;
|
|
readonly size: number;
|
|
}
|
|
|
|
/** Declared zero-initialized virtual memory with no corresponding file bytes. */
|
|
export interface ZeroFillMapping {
|
|
readonly address: bigint;
|
|
readonly size: bigint;
|
|
}
|
|
|
|
/** Why a declared virtual span does not establish one contiguous file span. */
|
|
export type VirtualFileMappingFailure =
|
|
| {
|
|
readonly reason: "invalid-size" | "ambiguous";
|
|
readonly message: string;
|
|
}
|
|
| {
|
|
readonly reason: "unmapped";
|
|
readonly message: string;
|
|
/** A gap does not erase the zero-initialized ranges within the request. */
|
|
readonly overlapsZeroFill: boolean;
|
|
};
|
|
|
|
/** Map a physically contiguous virtual span across consistent adjacent ranges. */
|
|
export const resolveMappedFileOffset = (
|
|
mappings: readonly FileMapping[],
|
|
address: bigint,
|
|
size: number,
|
|
zeroFills: readonly ZeroFillMapping[] = [],
|
|
): Result<number, VirtualFileMappingFailure> => {
|
|
if (!Number.isSafeInteger(size) || size < 0)
|
|
return err({
|
|
reason: "invalid-size",
|
|
message: "Virtual span size is invalid",
|
|
});
|
|
const spanBytes = BigInt(size);
|
|
const end = address + spanBytes;
|
|
const overlapsZeroFill = () =>
|
|
zeroFills.some(({ address: start, size: bytes }) =>
|
|
spansOverlap(address, spanBytes, start, bytes),
|
|
);
|
|
const unmapped = () =>
|
|
err({
|
|
reason: "unmapped" as const,
|
|
message: "Virtual address or string range is not file mapped",
|
|
overlapsZeroFill: overlapsZeroFill(),
|
|
});
|
|
let found: number | undefined;
|
|
const covered: { start: bigint; end: bigint }[] = [];
|
|
let whollyCovered = size === 0;
|
|
for (const mapping of mappings) {
|
|
const mappingEnd = mapping.address + BigInt(mapping.size);
|
|
const start = address > mapping.address ? address : mapping.address;
|
|
const overlapEnd = end < mappingEnd ? end : mappingEnd;
|
|
if (
|
|
size === 0
|
|
? address < mapping.address || address > mappingEnd
|
|
: start >= overlapEnd
|
|
)
|
|
continue;
|
|
const origin = mapping.offset + Number(address - mapping.address);
|
|
if (found !== undefined && found !== origin)
|
|
return err({
|
|
reason: "ambiguous",
|
|
message: "Virtual span has ambiguous or noncontiguous file mappings",
|
|
});
|
|
found = origin;
|
|
if (start === address && overlapEnd === end) whollyCovered = true;
|
|
else if (!whollyCovered) covered.push({ start, end: overlapEnd });
|
|
}
|
|
if (found === undefined) return unmapped();
|
|
if (!whollyCovered) {
|
|
covered.sort((left, right) =>
|
|
left.start < right.start ? -1 : left.start > right.start ? 1 : 0,
|
|
);
|
|
let cursor = address;
|
|
for (const span of covered) {
|
|
if (span.start > cursor) return unmapped();
|
|
if (span.end > cursor) cursor = span.end;
|
|
}
|
|
if (cursor < end) return unmapped();
|
|
}
|
|
if (overlapsZeroFill())
|
|
return err({
|
|
reason: "ambiguous",
|
|
message: "Virtual address has ambiguous file and zero-fill mappings",
|
|
});
|
|
return ok(found);
|
|
};
|
|
|
|
const spansOverlap = (
|
|
address: bigint,
|
|
size: bigint,
|
|
otherAddress: bigint,
|
|
otherSize: bigint,
|
|
): boolean =>
|
|
size > 0n &&
|
|
otherSize > 0n &&
|
|
address < otherAddress + otherSize &&
|
|
otherAddress < address + size;
|