mirror of
https://github.com/morluto/rea.git
synced 2026-12-01 11:01:11 +00:00
* fix(native): report non-native bytes as unrecognized, not malformed
A file that is not the format a reader owns was reported as a broken file
of that format. `inspect_native_initializers` called a PE file or random
bytes a malformed target, and the thin Mach-O reader called any bytes
without Mach-O magic a malformed Mach-O.
Format owners can now say "these bytes are not my format": parse failures
and format target failures gain the `unrecognized` kind that target
failures already define ("no reader for this operation recognizes the
bytes"). The owners report it once:
- the thin Mach-O reader, for bytes without Mach-O magic;
- the shared native container reader, for bytes that are not ELF, PE or
Mach-O (previously `unsupported`);
- the initializers provider, for a file with neither ELF nor Mach-O magic.
Function starts no longer pre-checks the header itself; the shared reader
owns that decision. IL2CPP keeps mapping unknown native bytes to its
unsupported-container reason. A recognized header that is cut off or
broken stays truncated or malformed.
* fix(resources): keep every format failure kind's facts through artifact wrappers
`ArtifactReaderFailure` kept the owner's facts only for its own list of
three format kinds, and `artifactOperationReason` had a second list. With
`unrecognized` added, `artifactFormatFailure` and `artifactParseFailure`
dropped its facts, so `translateArtifactFailure` raised an internal
invariant error instead of the target failure.
The format kinds are now one list, `FORMAT_TARGET_FAILURE_KINDS`, and
`isFormatTargetFailureKind` is the one guard. `FormatTargetFailure`,
`ParseFailureKind` and `BinaryFormatFailure` derive from that list, and
the reader-failure constructor and the operation-reason check both use the
guard, so a new kind cannot be carried by one wrapper and dropped by another.
* test(conformance): expect unrecognized for foreign bytes on native readers
The Go, Rust and native-initializers readers now report bytes that are not
ELF, PE or Mach-O as unrecognized, so their foreign-bytes conformance cases
expect that kind. The case names follow the kind.
925 lines
31 KiB
TypeScript
925 lines
31 KiB
TypeScript
import type { FixedCeilingFacts } from "./fixedAllowanceRemediation.js";
|
|
import {
|
|
lastAtOrBelow,
|
|
resolveMappedFileOffset,
|
|
spansOverlap,
|
|
type FileMapping,
|
|
type ZeroFillMapping,
|
|
} from "./virtualFileMapping.js";
|
|
import type { PeDataDirectory } from "./peOptionalHeader.js";
|
|
import { decodeLimit, ParseFailureError } from "./parseFailure.js";
|
|
import type {
|
|
FormatTargetFailureKind,
|
|
TruncatedExtent,
|
|
} from "./targetFailure.js";
|
|
|
|
/** Return whether the leading bytes begin with the 4-byte ELF magic (`\x7fELF`). */
|
|
export const hasElfMagic = (bytes: Uint8Array): boolean =>
|
|
bytes.length >= 4 &&
|
|
bytes[0] === 0x7f &&
|
|
bytes[1] === 0x45 &&
|
|
bytes[2] === 0x4c &&
|
|
bytes[3] === 0x46;
|
|
|
|
/** Return whether the leading bytes begin with the 4-byte XAR archive magic (`xar!`). */
|
|
export const hasXarMagic = (bytes: Uint8Array): boolean =>
|
|
bytes.length >= 4 &&
|
|
bytes[0] === 0x78 &&
|
|
bytes[1] === 0x61 &&
|
|
bytes[2] === 0x72 &&
|
|
bytes[3] === 0x21;
|
|
|
|
/** Native container facts a format failure can name. */
|
|
interface BinaryFormatFailureFacts {
|
|
/** The image holds the metadata, in a form this reader cannot decode. */
|
|
readonly undecodedContent?: boolean;
|
|
/** The native format, when the reader knows it. */
|
|
readonly format?: string;
|
|
readonly offset?: number;
|
|
readonly field?: string;
|
|
/** The measured end and needed length of a truncated structure. */
|
|
readonly extent?: TruncatedExtent;
|
|
}
|
|
|
|
/** Distinguish corrupt recognized binaries from containers outside parser coverage. */
|
|
export class BinaryFormatFailure extends ParseFailureError<FormatTargetFailureKind> {
|
|
constructor(
|
|
kind: FormatTargetFailureKind,
|
|
message: string,
|
|
facts: BinaryFormatFailureFacts = {},
|
|
) {
|
|
const {
|
|
undecodedContent = false,
|
|
format = "native-binary",
|
|
extent,
|
|
...at
|
|
} = facts;
|
|
super(
|
|
kind === "unsupported"
|
|
? { kind, format, detail: message, undecodedContent, ...at }
|
|
: kind === "truncated" && extent !== undefined
|
|
? { kind, format, detail: message, ...at, extent }
|
|
: { kind, format, detail: message, ...at },
|
|
);
|
|
this.name = "BinaryFormatFailure";
|
|
}
|
|
|
|
/** The image holds the metadata, in a form this reader cannot decode. */
|
|
get undecodedContent(): boolean {
|
|
return this.failure.kind === "unsupported" && this.failure.undecodedContent;
|
|
}
|
|
}
|
|
|
|
/** A missing file span is a candidate miss; conflicting owners are malformed. */
|
|
export class BinaryMappingFailure extends BinaryFormatFailure {
|
|
constructor(
|
|
readonly reason: "unmapped" | "ambiguous",
|
|
message: string,
|
|
) {
|
|
super("malformed", message);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Resource boundary exceeded while decoding metadata or native container
|
|
* structure. `allowance` names the exhausted allowance in the failure's
|
|
* `LimitRef`; it defaults to the boundary when a decoder has only one.
|
|
*/
|
|
export class BinaryResourceFailure extends ParseFailureError<"limit"> {
|
|
/**
|
|
* @param maximum_bytes The exceeded maximum, in `unit`. Despite its name,
|
|
* some producers count entries or values; `unit` says which, and is
|
|
* undefined when the producer has not named it.
|
|
*/
|
|
constructor(
|
|
readonly boundary: "build-info" | "structure" | "search",
|
|
readonly maximum_bytes: number,
|
|
message: string,
|
|
allowance: string = boundary,
|
|
readonly unit?: "bytes" | "entries",
|
|
required?: number | bigint,
|
|
) {
|
|
super({
|
|
kind: "limit",
|
|
format: "native-binary",
|
|
detail: message,
|
|
limit: {
|
|
name: allowance,
|
|
maximum: maximum_bytes,
|
|
...(required === undefined ? {} : { required }),
|
|
},
|
|
});
|
|
this.name = "BinaryResourceFailure";
|
|
}
|
|
}
|
|
|
|
/**
|
|
* `max_structure_bytes` when omitted: the bytes of one native directory or
|
|
* load-command table read, and of the section names decoded from it.
|
|
*/
|
|
export const DEFAULT_NATIVE_STRUCTURE_BYTES = 16 * 1024 * 1024;
|
|
|
|
/** Limit name a `BinaryResourceFailure` carries for `max_structure_bytes`. */
|
|
export const NATIVE_STRUCTURE_LIMIT = "max_structure_bytes";
|
|
|
|
/** A native table read or decoded section-name set above `max_structure_bytes`. */
|
|
export const nativeStructureFailure = (
|
|
what: string,
|
|
requiredBytes: bigint | number,
|
|
maximumBytes: number,
|
|
): BinaryResourceFailure =>
|
|
new BinaryResourceFailure(
|
|
"structure",
|
|
maximumBytes,
|
|
`${what} requires ${String(requiredBytes)} bytes, above max_structure_bytes (${String(maximumBytes)})`,
|
|
NATIVE_STRUCTURE_LIMIT,
|
|
"bytes",
|
|
requiredBytes,
|
|
);
|
|
|
|
/** Whether a resource failure stopped at `max_structure_bytes`. */
|
|
export const isNativeStructureFailure = (
|
|
cause: unknown,
|
|
): cause is BinaryResourceFailure =>
|
|
cause instanceof BinaryResourceFailure &&
|
|
cause.failure.limit.name === NATIVE_STRUCTURE_LIMIT;
|
|
|
|
/** The exceeded maximum as fixed-ceiling facts; the unit only when its producer named it. */
|
|
export const binaryResourceCeiling = (
|
|
failure: BinaryResourceFailure,
|
|
): FixedCeilingFacts => ({
|
|
limit: failure.maximum_bytes,
|
|
...(failure.unit === undefined ? {} : { unit: failure.unit }),
|
|
});
|
|
|
|
/** The failure's allowance when it is one of `allowances`, else `null`. */
|
|
export const binaryResourceAllowance = <Allowance extends string>(
|
|
failure: BinaryResourceFailure,
|
|
allowances: readonly Allowance[],
|
|
): Allowance | null =>
|
|
allowances.find((allowance) => allowance === failure.failure.limit.name) ??
|
|
null;
|
|
|
|
/** Limit name of the fixed per-string NUL-termination read allowance. */
|
|
const STRING_READ_LIMIT = "string-read-bytes";
|
|
|
|
/** A string read stopped at its allowance; this does not prove invalid text. */
|
|
export type BinaryStringReadLimit = ParseFailureError<"limit">;
|
|
|
|
/** A bounded read could not establish termination within `maximumBytes`. */
|
|
export const binaryStringReadLimit = (
|
|
maximumBytes: number,
|
|
): BinaryStringReadLimit =>
|
|
decodeLimit(
|
|
"native-binary",
|
|
`String termination was not established within its ${String(maximumBytes)}-byte read allowance`,
|
|
{ name: STRING_READ_LIMIT, maximum: maximumBytes },
|
|
);
|
|
|
|
/** Whether a cause is a string read that stopped at its allowance. */
|
|
export const isBinaryStringReadLimit = (
|
|
cause: unknown,
|
|
): cause is BinaryStringReadLimit =>
|
|
cause instanceof ParseFailureError &&
|
|
cause.failure.kind === "limit" &&
|
|
cause.failure.limit.name === STRING_READ_LIMIT;
|
|
|
|
/** Recognized format failures and bounded string-read failures. */
|
|
export const isBinaryFormatOrStringLimitFailure = (
|
|
cause: unknown,
|
|
): cause is BinaryFormatFailure | BinaryStringReadLimit =>
|
|
cause instanceof BinaryFormatFailure || isBinaryStringReadLimit(cause);
|
|
|
|
/** Native loader region facts; language-specific fallback selection belongs to consumers. */
|
|
export interface LoadRegion {
|
|
readonly mapping: FileMapping;
|
|
readonly memory_size: bigint;
|
|
readonly read: boolean;
|
|
readonly write: boolean;
|
|
readonly execute: boolean;
|
|
/** Raw Mach-O VM_PROT masks preserve bits not represented by booleans. */
|
|
readonly protection_masks?: {
|
|
readonly current: number;
|
|
readonly maximum: number;
|
|
};
|
|
}
|
|
|
|
/** Parsed section facts; consumer metadata validity is separate from native structure. */
|
|
export type ImageSection = {
|
|
readonly offset: number;
|
|
readonly size: number;
|
|
readonly file_backed: boolean;
|
|
readonly read_only: boolean;
|
|
readonly compressed: boolean;
|
|
} & (
|
|
| {
|
|
readonly format: "elf";
|
|
/** Null when the ELF name table is unavailable or this name is unreadable. */
|
|
readonly name: string | null;
|
|
readonly type: number;
|
|
readonly flags: bigint;
|
|
readonly address: bigint;
|
|
readonly declared_range: {
|
|
readonly offset: bigint;
|
|
readonly size: bigint;
|
|
};
|
|
}
|
|
| {
|
|
readonly format: "pe";
|
|
readonly name: string;
|
|
readonly virtual_address: number;
|
|
readonly virtual_size: number;
|
|
readonly raw_size: number;
|
|
readonly file_alignment: number;
|
|
readonly characteristics: number;
|
|
}
|
|
| {
|
|
readonly format: "macho";
|
|
readonly name: string;
|
|
readonly section_name: string;
|
|
readonly type: number;
|
|
readonly flags: number;
|
|
readonly address: bigint;
|
|
readonly declared_range: {
|
|
readonly offset: bigint;
|
|
readonly size: bigint;
|
|
};
|
|
readonly segment_mapping: FileMapping | null;
|
|
readonly segment_unmapped: boolean;
|
|
/** Non-zero-fill bytes within the file and the segment's file range. */
|
|
readonly on_disk: boolean;
|
|
/** Declared `reloff` and `nreloc`, which only object files populate. */
|
|
readonly relocations: { readonly offset: number; readonly count: number };
|
|
}
|
|
);
|
|
|
|
/** Declared PT_DYNAMIC location, before consumer-specific admission. */
|
|
export interface ElfDynamicSegment {
|
|
readonly offset: bigint;
|
|
readonly size: bigint;
|
|
readonly virtual_address: bigint;
|
|
}
|
|
|
|
/** Native container facts, without language metadata search policy. */
|
|
interface BinaryContainerFacts {
|
|
readonly architecture: string;
|
|
readonly bits: 32 | 64;
|
|
readonly byte_order: "little" | "big";
|
|
/** Declared PE preferred base, when available. */
|
|
readonly image_base?: bigint;
|
|
/**
|
|
* File-backed PE headers (`SizeOfHeaders`, clipped to the file), which the
|
|
* loader maps at the image base. Kept apart from section `mappings`.
|
|
*/
|
|
readonly pe_header_mapping?: FileMapping;
|
|
/** PE optional-header data directories, indexed as declared. */
|
|
readonly pe_data_directories?: readonly PeDataDirectory[];
|
|
/** Mach-O load-command observation, when available. */
|
|
readonly macho_chained_fixups?: boolean;
|
|
/** Section-name lookup was incomplete; absence by name is not established. */
|
|
readonly section_names_limitation?: string;
|
|
readonly mappings: readonly FileMapping[];
|
|
readonly load_regions?: readonly LoadRegion[];
|
|
/** File-backed ranges the native loader marks executable. */
|
|
readonly executable_mappings?: readonly FileMapping[];
|
|
readonly zero_fills: readonly ZeroFillMapping[];
|
|
readonly sections: readonly ImageSection[];
|
|
}
|
|
|
|
/** ELF identity and dynamic declarations are present only for an ELF container. */
|
|
export type BinaryContainer = BinaryContainerFacts &
|
|
(
|
|
| {
|
|
readonly format: "elf";
|
|
readonly elf: {
|
|
readonly machine: number;
|
|
readonly type: number;
|
|
readonly dynamicSegments: readonly ElfDynamicSegment[];
|
|
};
|
|
}
|
|
| { readonly format: "pe" }
|
|
| {
|
|
readonly format: "macho";
|
|
readonly macho_cpu_type: number;
|
|
/** `mach_header.filetype`, such as MH_OBJECT (1) or MH_EXECUTE (2). */
|
|
readonly macho_file_type: number;
|
|
/**
|
|
* Every LC_SEGMENT/LC_SEGMENT_64 in declaration order, including
|
|
* segments with no file bytes. `load_regions` holds only the
|
|
* file-backed ones, so whether a segment was declared is read here.
|
|
*/
|
|
readonly macho_segments: readonly MachoSegmentDeclaration[];
|
|
}
|
|
);
|
|
|
|
/** One declared Mach-O segment command, whether or not it has file bytes. */
|
|
export interface MachoSegmentDeclaration {
|
|
readonly name: string;
|
|
readonly address: bigint;
|
|
readonly memory_size: bigint;
|
|
readonly file_offset: bigint;
|
|
readonly file_size: bigint;
|
|
/** Initial VM protection (`initprot`). */
|
|
readonly protection: number;
|
|
}
|
|
|
|
/** All declared file-backed image memory, including PE headers at the image base. */
|
|
export const imageFileMappings = (
|
|
container: BinaryContainer,
|
|
): readonly FileMapping[] =>
|
|
container.pe_header_mapping === undefined
|
|
? container.mappings
|
|
: [container.pe_header_mapping, ...container.mappings];
|
|
|
|
/** Decode an unsigned 16-bit integer from a caller-validated `[offset, offset + 2)` span. */
|
|
export const readUint16At = (
|
|
bytes: Uint8Array,
|
|
offset: number,
|
|
little: boolean,
|
|
): number =>
|
|
little
|
|
? bytes[offset]! | (bytes[offset + 1]! << 8)
|
|
: (bytes[offset]! << 8) | bytes[offset + 1]!;
|
|
|
|
/** Decode a signed 32-bit integer from a caller-validated `[offset, offset + 4)` span. */
|
|
export const readInt32At = (
|
|
bytes: Uint8Array,
|
|
offset: number,
|
|
little: boolean,
|
|
): number =>
|
|
little
|
|
? bytes[offset]! |
|
|
(bytes[offset + 1]! << 8) |
|
|
(bytes[offset + 2]! << 16) |
|
|
(bytes[offset + 3]! << 24)
|
|
: (bytes[offset]! << 24) |
|
|
(bytes[offset + 1]! << 16) |
|
|
(bytes[offset + 2]! << 8) |
|
|
bytes[offset + 3]!;
|
|
|
|
/** Decode an unsigned 32-bit integer from a caller-validated `[offset, offset + 4)` span. */
|
|
export const readUint32At = (
|
|
bytes: Uint8Array,
|
|
offset: number,
|
|
little: boolean,
|
|
): number => readInt32At(bytes, offset, little) >>> 0;
|
|
|
|
/** Decode an unsigned 64-bit integer from a caller-validated `[offset, offset + 8)` span. */
|
|
export const readUint64At = (
|
|
bytes: Uint8Array,
|
|
offset: number,
|
|
little: boolean,
|
|
): bigint => {
|
|
const lo = readUint32At(bytes, little ? offset : offset + 4, little);
|
|
const hi = readUint32At(bytes, little ? offset + 4 : offset, little);
|
|
return (BigInt(hi) << 32n) | BigInt(lo);
|
|
};
|
|
|
|
/** Decode a signed 64-bit integer from a caller-validated `[offset, offset + 8)` span. */
|
|
export const readInt64At = (
|
|
bytes: Uint8Array,
|
|
offset: number,
|
|
little: boolean,
|
|
): bigint => {
|
|
const lo = readUint32At(bytes, little ? offset : offset + 4, little);
|
|
const hi = readInt32At(bytes, little ? offset + 4 : offset, little);
|
|
return (BigInt(hi) << 32n) | BigInt(lo);
|
|
};
|
|
|
|
/** Reader that checks every structural range before decoding numeric fields. */
|
|
export class BinaryReader {
|
|
constructor(
|
|
readonly bytes: Buffer,
|
|
readonly little: boolean,
|
|
/** The native format these bytes hold, named in every failure. */
|
|
readonly format: string,
|
|
) {}
|
|
|
|
#checkRange(offset: number, size: number, label: string): void {
|
|
if (
|
|
!Number.isSafeInteger(offset) ||
|
|
!Number.isSafeInteger(size) ||
|
|
offset < 0 ||
|
|
size < 0 ||
|
|
offset > this.bytes.length
|
|
)
|
|
throw new BinaryFormatFailure(
|
|
"malformed",
|
|
`${label} range is truncated or outside the file`,
|
|
{
|
|
format: this.format,
|
|
field: label,
|
|
...(Number.isSafeInteger(offset) ? { offset } : {}),
|
|
},
|
|
);
|
|
if (size > this.bytes.length - offset)
|
|
throw new BinaryFormatFailure(
|
|
"truncated",
|
|
`${label} range is truncated or outside the file`,
|
|
{ format: this.format, offset, field: label },
|
|
);
|
|
}
|
|
|
|
range(offset: number, size: number, label: string): Buffer {
|
|
this.#checkRange(offset, size, label);
|
|
return this.bytes.subarray(offset, offset + size);
|
|
}
|
|
|
|
u8(offset: number): number {
|
|
this.#checkRange(offset, 1, "8-bit field");
|
|
return this.bytes.readUInt8(offset);
|
|
}
|
|
|
|
i8(offset: number): number {
|
|
this.#checkRange(offset, 1, "8-bit field");
|
|
return this.bytes.readInt8(offset);
|
|
}
|
|
|
|
u16(offset: number): number {
|
|
this.#checkRange(offset, 2, "16-bit field");
|
|
return this.little
|
|
? this.bytes.readUInt16LE(offset)
|
|
: this.bytes.readUInt16BE(offset);
|
|
}
|
|
|
|
i16(offset: number): number {
|
|
this.#checkRange(offset, 2, "16-bit field");
|
|
return this.little
|
|
? this.bytes.readInt16LE(offset)
|
|
: this.bytes.readInt16BE(offset);
|
|
}
|
|
|
|
u32(offset: number): number {
|
|
this.#checkRange(offset, 4, "32-bit field");
|
|
return this.little
|
|
? this.bytes.readUInt32LE(offset)
|
|
: this.bytes.readUInt32BE(offset);
|
|
}
|
|
|
|
i32(offset: number): number {
|
|
this.#checkRange(offset, 4, "32-bit field");
|
|
return this.little
|
|
? this.bytes.readInt32LE(offset)
|
|
: this.bytes.readInt32BE(offset);
|
|
}
|
|
|
|
i64(offset: number): bigint {
|
|
this.#checkRange(offset, 8, "64-bit field");
|
|
return this.little
|
|
? this.bytes.readBigInt64LE(offset)
|
|
: this.bytes.readBigInt64BE(offset);
|
|
}
|
|
|
|
f32(offset: number): number {
|
|
this.#checkRange(offset, 4, "32-bit field");
|
|
return this.little
|
|
? this.bytes.readFloatLE(offset)
|
|
: this.bytes.readFloatBE(offset);
|
|
}
|
|
|
|
f64(offset: number): number {
|
|
this.#checkRange(offset, 8, "64-bit field");
|
|
return this.little
|
|
? this.bytes.readDoubleLE(offset)
|
|
: this.bytes.readDoubleBE(offset);
|
|
}
|
|
|
|
word(offset: number, bits: 32 | 64): bigint {
|
|
if (bits === 32) return BigInt(this.u32(offset));
|
|
this.#checkRange(offset, 8, "64-bit field");
|
|
return this.little
|
|
? this.bytes.readBigUInt64LE(offset)
|
|
: this.bytes.readBigUInt64BE(offset);
|
|
}
|
|
|
|
fileRange(offset: bigint, size: bigint, label: string): FileMapping {
|
|
const length = BigInt(this.bytes.length);
|
|
if (offset < 0n || offset > length)
|
|
throw new BinaryFormatFailure(
|
|
"malformed",
|
|
`${label} range is outside the file`,
|
|
{
|
|
format: this.format,
|
|
field: label,
|
|
...(offset >= BigInt(Number.MIN_SAFE_INTEGER) &&
|
|
offset <= BigInt(Number.MAX_SAFE_INTEGER)
|
|
? { offset: Number(offset) }
|
|
: {}),
|
|
},
|
|
);
|
|
if (size > length - offset)
|
|
throw new BinaryFormatFailure(
|
|
"truncated",
|
|
`${label} range is outside the file`,
|
|
{ format: this.format, offset: Number(offset), field: label },
|
|
);
|
|
const result = { address: 0n, offset: Number(offset), size: Number(size) };
|
|
this.range(result.offset, result.size, label);
|
|
return result;
|
|
}
|
|
|
|
table(
|
|
offset: bigint,
|
|
count: number,
|
|
width: number,
|
|
label: string,
|
|
structureBytes: number = DEFAULT_NATIVE_STRUCTURE_BYTES,
|
|
): number {
|
|
const size = BigInt(count) * BigInt(width);
|
|
const range = this.fileRange(offset, size, label);
|
|
requireBinaryTableBudget(size, label, structureBytes);
|
|
return range.offset;
|
|
}
|
|
}
|
|
|
|
/** Reuse a reader for the same byte view, byte order, and diagnostic format. */
|
|
export const createBinaryReaderCache = (little: boolean, format: string) => {
|
|
const readers = new WeakMap<Uint8Array, BinaryReader>();
|
|
return (bytes: Uint8Array): BinaryReader => {
|
|
let reader = readers.get(bytes);
|
|
if (reader === undefined) {
|
|
const buffer = Buffer.isBuffer(bytes)
|
|
? bytes
|
|
: Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
reader = new BinaryReader(buffer, little, format);
|
|
readers.set(bytes, reader);
|
|
}
|
|
return reader;
|
|
};
|
|
};
|
|
|
|
/** Reject a structural table above `max_structure_bytes` before reading its bytes. */
|
|
export const requireBinaryTableBudget = (
|
|
size: bigint,
|
|
label: string,
|
|
structureBytes: number = DEFAULT_NATIVE_STRUCTURE_BYTES,
|
|
): void => {
|
|
if (size > BigInt(structureBytes))
|
|
throw nativeStructureFailure(label, size, structureBytes);
|
|
};
|
|
|
|
/** Admit a declared file span, or mark it unbacked when it leaves the file. */
|
|
export const declaredFileRange = (
|
|
fileBytes: number,
|
|
offset: bigint,
|
|
size: bigint,
|
|
): {
|
|
readonly offset: number;
|
|
readonly size: number;
|
|
readonly file_backed: boolean;
|
|
} => {
|
|
if (
|
|
offset < 0n ||
|
|
size < 0n ||
|
|
offset > BigInt(Number.MAX_SAFE_INTEGER) ||
|
|
size > BigInt(Number.MAX_SAFE_INTEGER) ||
|
|
offset > BigInt(fileBytes) ||
|
|
size > BigInt(fileBytes) - offset
|
|
)
|
|
return { offset: 0, size: 0, file_backed: false };
|
|
return {
|
|
offset: Number(offset),
|
|
size: Number(size),
|
|
file_backed: size > 0n,
|
|
};
|
|
};
|
|
|
|
/** Translate the domain mapping result into native parser failure classes. */
|
|
export const mappedFileOffset = (
|
|
mappings: readonly FileMapping[],
|
|
address: bigint,
|
|
size: number,
|
|
zeroFills: readonly ZeroFillMapping[] = [],
|
|
): number => {
|
|
const mapped = resolveMappedFileOffset(mappings, address, size, zeroFills);
|
|
if (mapped.ok) return mapped.value;
|
|
const { reason, message } = mapped.error;
|
|
if (reason === "invalid-size")
|
|
throw new BinaryFormatFailure("malformed", message);
|
|
throw new BinaryMappingFailure(reason, message);
|
|
};
|
|
|
|
/** Sorted starts with the furthest end reached by any span starting at or before each. */
|
|
const prefixReach = (
|
|
spans: readonly { readonly start: bigint; readonly end: bigint }[],
|
|
): { readonly starts: bigint[]; readonly reach: bigint[] } => {
|
|
const sorted = [...spans].sort((left, right) =>
|
|
left.start < right.start ? -1 : left.start > right.start ? 1 : 0,
|
|
);
|
|
const starts: bigint[] = [];
|
|
const reach: bigint[] = [];
|
|
let furthest = -1n;
|
|
for (const span of sorted) {
|
|
if (span.end > furthest) furthest = span.end;
|
|
starts.push(span.start);
|
|
reach.push(furthest);
|
|
}
|
|
return { starts, reach };
|
|
};
|
|
|
|
/**
|
|
* Indexed `mappedFileOffset` for parsers that translate many spans in one
|
|
* image. Construction is O(n log n) over mappings; each lookup is O(log n), so
|
|
* hostile section counts cannot multiply the cost of every table entry. It
|
|
* accepts, rejects and translates exactly the spans `mappedFileOffset` does;
|
|
* a span that is both a conflicting file alias and a zero-fill overlap may be
|
|
* refused under either ambiguity message.
|
|
*/
|
|
export const createFileMappingIndex = (
|
|
mappings: readonly FileMapping[],
|
|
zeroFills: readonly ZeroFillMapping[] = [],
|
|
): FileMappingIndex => {
|
|
const backed = mappings
|
|
.filter((mapping) => mapping.size > 0)
|
|
.map((mapping) => ({
|
|
start: mapping.address,
|
|
end: mapping.address + BigInt(mapping.size),
|
|
translation: BigInt(mapping.offset) - mapping.address,
|
|
}));
|
|
const zeroFilled = prefixReach(
|
|
zeroFills
|
|
.filter((zeroFill) => zeroFill.size > 0n)
|
|
.map((zeroFill) => ({
|
|
start: zeroFill.address,
|
|
end: zeroFill.address + zeroFill.size,
|
|
})),
|
|
);
|
|
// Elementary intervals between every mapping boundary, each with the single
|
|
// file translation shared by all mappings covering it (or none/ambiguous).
|
|
const events = new Map<bigint, { add: bigint[]; remove: bigint[] }>();
|
|
const event = (at: bigint) => {
|
|
let entry = events.get(at);
|
|
if (entry === undefined) {
|
|
entry = { add: [], remove: [] };
|
|
events.set(at, entry);
|
|
}
|
|
return entry;
|
|
};
|
|
for (const span of backed) {
|
|
event(span.start).add.push(span.translation);
|
|
event(span.end).remove.push(span.translation);
|
|
}
|
|
const boundaries = [...events.keys()].sort((left, right) =>
|
|
left < right ? -1 : left > right ? 1 : 0,
|
|
);
|
|
const active = new Map<bigint, number>();
|
|
// Undefined is a gap; null is an overlap with conflicting translations.
|
|
const translations: (bigint | null | undefined)[] = [];
|
|
for (const boundary of boundaries) {
|
|
const entry = events.get(boundary);
|
|
for (const translation of entry?.remove ?? []) {
|
|
const count = (active.get(translation) ?? 0) - 1;
|
|
if (count === 0) active.delete(translation);
|
|
else active.set(translation, count);
|
|
}
|
|
for (const translation of entry?.add ?? [])
|
|
active.set(translation, (active.get(translation) ?? 0) + 1);
|
|
translations.push(
|
|
active.size === 0
|
|
? undefined
|
|
: active.size === 1
|
|
? (active.keys().next().value as bigint)
|
|
: null,
|
|
);
|
|
}
|
|
// runEnd[i]: end of the maximal run of intervals sharing interval i's translation.
|
|
const runEnd: bigint[] = boundaries.map(() => 0n);
|
|
for (let index = boundaries.length - 1; index >= 0; index--) {
|
|
const next = boundaries[index + 1];
|
|
const translation = translations[index];
|
|
runEnd[index] =
|
|
next === undefined
|
|
? (boundaries[index] as bigint)
|
|
: translation !== null &&
|
|
translation !== undefined &&
|
|
translations[index + 1] === translation
|
|
? (runEnd[index + 1] as bigint)
|
|
: next;
|
|
}
|
|
const ambiguousPrefix = [0];
|
|
const previousMapped: number[] = [];
|
|
const nextMapped: number[] = [];
|
|
const translationRuns: number[] = [];
|
|
let previous = -1;
|
|
let run = 0;
|
|
let previousTranslation: bigint | undefined;
|
|
for (const [index, translation] of translations.entries()) {
|
|
ambiguousPrefix.push(
|
|
(ambiguousPrefix[index] ?? 0) + (translation === null ? 1 : 0),
|
|
);
|
|
if (typeof translation === "bigint") {
|
|
if (translation !== previousTranslation) run += 1;
|
|
previousTranslation = translation;
|
|
previous = index;
|
|
}
|
|
previousMapped.push(previous);
|
|
translationRuns.push(run);
|
|
}
|
|
let next = translations.length;
|
|
for (let index = translations.length - 1; index >= 0; index--) {
|
|
if (typeof translations[index] === "bigint") next = index;
|
|
nextMapped[index] = next;
|
|
}
|
|
const tryOffset = (address: bigint, size: number): number | undefined => {
|
|
if (!Number.isSafeInteger(size) || size <= 0)
|
|
return mappedFileOffset(mappings, address, size, zeroFills);
|
|
const end = address + BigInt(size);
|
|
const interval = lastAtOrBelow(boundaries, address);
|
|
const lower = Math.max(0, interval);
|
|
const upper = lastAtOrBelow(boundaries, end - 1n);
|
|
const first = nextMapped[lower] ?? translations.length;
|
|
const last = previousMapped[upper] ?? -1;
|
|
// Like the scan, conflicts anywhere in the requested span win over gaps.
|
|
if (
|
|
(ambiguousPrefix[upper + 1] ?? 0) - (ambiguousPrefix[lower] ?? 0) > 0 ||
|
|
(first <= upper &&
|
|
last >= lower &&
|
|
translationRuns[first] !== translationRuns[last])
|
|
)
|
|
throw new BinaryMappingFailure(
|
|
"ambiguous",
|
|
"Virtual span has ambiguous or noncontiguous file mappings",
|
|
);
|
|
const translation = translations[interval];
|
|
if (typeof translation !== "bigint" || (runEnd[interval] as bigint) < end)
|
|
return undefined;
|
|
const zeroFill = lastAtOrBelow(zeroFilled.starts, end - 1n);
|
|
if (zeroFill >= 0 && (zeroFilled.reach[zeroFill] as bigint) > address)
|
|
throw new BinaryMappingFailure(
|
|
"ambiguous",
|
|
"Virtual address has ambiguous file and zero-fill mappings",
|
|
);
|
|
return Number(address + translation);
|
|
};
|
|
const offset = (address: bigint, size: number): number => {
|
|
const resolved = tryOffset(address, size);
|
|
if (resolved === undefined)
|
|
throw new BinaryMappingFailure(
|
|
"unmapped",
|
|
"Virtual address or string range is not file mapped",
|
|
);
|
|
return resolved;
|
|
};
|
|
const prefix = (
|
|
address: bigint,
|
|
maximumBytes: number,
|
|
): FileMapping | null => {
|
|
if (!Number.isSafeInteger(maximumBytes) || maximumBytes < 1) return null;
|
|
const interval = lastAtOrBelow(boundaries, address);
|
|
const translation = translations[interval];
|
|
if (typeof translation !== "bigint") return null;
|
|
const zeroFill = lastAtOrBelow(zeroFilled.starts, address);
|
|
if (zeroFill >= 0 && (zeroFilled.reach[zeroFill] as bigint) > address)
|
|
return null;
|
|
const mappingEnd = runEnd[interval] as bigint;
|
|
const zeroFillStart = zeroFilled.starts[zeroFill + 1];
|
|
const backingEnd =
|
|
zeroFillStart !== undefined && zeroFillStart < mappingEnd
|
|
? zeroFillStart
|
|
: mappingEnd;
|
|
const limit = address + BigInt(maximumBytes);
|
|
const end = backingEnd < limit ? backingEnd : limit;
|
|
return {
|
|
address,
|
|
offset: Number(address + translation),
|
|
size: Number(end - address),
|
|
};
|
|
};
|
|
const access: MappedStringAccess = {
|
|
offset,
|
|
rangeAt: (address, maximum) => {
|
|
const start = offset(address, 1);
|
|
const interval = lastAtOrBelow(boundaries, address);
|
|
return {
|
|
offset: start,
|
|
size: Math.min(maximum, Number((runEnd[interval] as bigint) - address)),
|
|
};
|
|
},
|
|
};
|
|
return {
|
|
offset,
|
|
tryOffset,
|
|
prefix,
|
|
readCString: (bytes, address, maximumBytes) =>
|
|
readMappedString(bytes, access, address, maximumBytes),
|
|
readCStringFromReader: (readPrefix, address, maximumBytes) =>
|
|
readMappedStringFromReader(readPrefix, access, address, maximumBytes),
|
|
};
|
|
};
|
|
|
|
/** One immutable image's indexed mappings and bounded string access. */
|
|
export interface FileMappingIndex {
|
|
readonly offset: (address: bigint, size: number) => number;
|
|
/** Like `offset`, returning `undefined` on unmapped spans while still throwing on ambiguity. */
|
|
readonly tryOffset: (address: bigint, size: number) => number | undefined;
|
|
/** Longest file-backed prefix up to the requested length with one translation. */
|
|
readonly prefix: (
|
|
address: bigint,
|
|
maximumBytes: number,
|
|
) => FileMapping | null;
|
|
readonly readCString: (
|
|
bytes: Buffer,
|
|
address: bigint,
|
|
maximumBytes: number,
|
|
) => Buffer;
|
|
/** Supply a nonempty prefix within each requested file range. */
|
|
readonly readCStringFromReader: (
|
|
readPrefix: (offset: number, maximum: number) => Promise<Buffer>,
|
|
address: bigint,
|
|
maximumBytes: number,
|
|
) => Promise<Buffer>;
|
|
}
|
|
|
|
interface MappedStringAccess {
|
|
readonly rangeAt: (
|
|
address: bigint,
|
|
maximum: number,
|
|
) => Pick<FileMapping, "offset" | "size">;
|
|
readonly offset: (address: bigint, size: number) => number;
|
|
}
|
|
|
|
const readMappedString = (
|
|
bytes: Buffer,
|
|
access: MappedStringAccess,
|
|
address: bigint,
|
|
maximumBytes: number,
|
|
): Buffer => {
|
|
const chunks: Buffer[] = [];
|
|
let length = 0;
|
|
while (length <= maximumBytes) {
|
|
const cursor = address + BigInt(length);
|
|
const { offset, size } = access.rangeAt(cursor, maximumBytes + 1 - length);
|
|
const chunk = bytes.subarray(offset, offset + size);
|
|
const terminator = chunk.indexOf(0);
|
|
const consumed = terminator < 0 ? size : terminator + 1;
|
|
access.offset(cursor, consumed);
|
|
if (terminator >= 0) {
|
|
const tail = chunk.subarray(0, terminator);
|
|
if (chunks.length === 0) return tail;
|
|
chunks.push(tail);
|
|
return Buffer.concat(chunks, length + terminator);
|
|
}
|
|
chunks.push(chunk);
|
|
length += size;
|
|
}
|
|
throw binaryStringReadLimit(maximumBytes);
|
|
};
|
|
|
|
const readMappedStringFromReader = async (
|
|
readPrefix: (offset: number, maximum: number) => Promise<Buffer>,
|
|
access: MappedStringAccess,
|
|
address: bigint,
|
|
maximumBytes: number,
|
|
): Promise<Buffer> => {
|
|
const chunks: Buffer[] = [];
|
|
let length = 0;
|
|
while (length <= maximumBytes) {
|
|
const cursor = address + BigInt(length);
|
|
const { offset, size } = access.rangeAt(cursor, maximumBytes + 1 - length);
|
|
const chunk = await readPrefix(offset, size);
|
|
if (chunk.length === 0 || chunk.length > size)
|
|
throw new BinaryFormatFailure(
|
|
"truncated",
|
|
"Mapped string reader did not supply an admitted byte prefix",
|
|
);
|
|
const terminator = chunk.indexOf(0);
|
|
const consumed = terminator < 0 ? chunk.length : terminator + 1;
|
|
access.offset(cursor, consumed);
|
|
if (terminator >= 0) {
|
|
const tail = chunk.subarray(0, terminator);
|
|
if (chunks.length === 0) return tail;
|
|
chunks.push(tail);
|
|
return Buffer.concat(chunks, length + terminator);
|
|
}
|
|
chunks.push(chunk);
|
|
length += chunk.length;
|
|
}
|
|
throw binaryStringReadLimit(maximumBytes);
|
|
};
|
|
|
|
/** Recognize a wholly zero-filled span while refusing even partial file aliases. */
|
|
export const isZeroFilledSpan = (
|
|
mappings: readonly FileMapping[],
|
|
zeroFills: readonly ZeroFillMapping[],
|
|
address: bigint,
|
|
size: number,
|
|
): boolean => {
|
|
const bytes = BigInt(size);
|
|
if (
|
|
!zeroFills.some(
|
|
(mapping) =>
|
|
address >= mapping.address &&
|
|
address + bytes <= mapping.address + mapping.size,
|
|
)
|
|
)
|
|
return false;
|
|
for (const mapping of mappings) {
|
|
if (spansOverlap(address, bytes, mapping.address, BigInt(mapping.size)))
|
|
throw new BinaryMappingFailure(
|
|
"ambiguous",
|
|
"Virtual address has ambiguous file and zero-fill mappings",
|
|
);
|
|
}
|
|
return true;
|
|
};
|