From 2c7c858c5a45a8174a46673ce1bed5ed780aaa3a Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Thu, 19 Nov 2026 20:59:55 -0500 Subject: [PATCH] refactor(native): share mapped ELF and FAT scalar readers (#12631) --- scripts/verify/baselines/raw-buffer-read.json | 2 -- src/domain/native/elfPointerRelocations.ts | 24 +++------------- src/domain/native/readBinaryContainer.ts | 8 ++---- src/native/metadata/BinaryContainer.test.ts | 28 +++++++++++++++++++ 4 files changed, 34 insertions(+), 28 deletions(-) diff --git a/scripts/verify/baselines/raw-buffer-read.json b/scripts/verify/baselines/raw-buffer-read.json index e02c78266d..0be587a8d0 100644 --- a/scripts/verify/baselines/raw-buffer-read.json +++ b/scripts/verify/baselines/raw-buffer-read.json @@ -2,8 +2,6 @@ "src/domain/apple/machoLoadCommandReader.ts": 2, "src/domain/desktop/openPgpKeys.ts": 2, "src/domain/managed/managedMetadataInventoryRows.ts": 2, - "src/domain/native/elfPointerRelocations.ts": 6, - "src/domain/native/readBinaryContainer.ts": 1, "src/native/AppleDispatchMetadata.ts": 4, "src/native/AppleObjcDispatchFacets.ts": 1, "src/native/AppleSwiftDispatchFacets.ts": 1, diff --git a/src/domain/native/elfPointerRelocations.ts b/src/domain/native/elfPointerRelocations.ts index 19d940845b..eb8f89ce83 100644 --- a/src/domain/native/elfPointerRelocations.ts +++ b/src/domain/native/elfPointerRelocations.ts @@ -596,6 +596,7 @@ export function* readElfRelocationWritesSteps( input, ); const word = input.bits === 64 ? 8 : 4; + const reader = new BinaryReader(bytes, input.little, "elf"); const index = createFileMappingIndex(input.mappings, input.zeroFills); const fileOffset = (address: bigint, width: number): number | null => { try { @@ -606,16 +607,7 @@ export function* readElfRelocationWritesSteps( throw cause; } }; - const rawWord = (offset: number): bigint => - input.bits === 64 - ? input.little - ? bytes.readBigUInt64LE(offset) - : bytes.readBigUInt64BE(offset) - : BigInt( - input.little - ? bytes.readUInt32LE(offset) - : bytes.readUInt32BE(offset), - ); + const rawWord = (offset: number): bigint => reader.word(offset, input.bits); const target = (address: bigint, width: number): number | null => { const at = fileOffset(address, width); if ( @@ -679,14 +671,13 @@ export function* readElfRelocationWritesSteps( ); return parsed.ok ? parsed.value : undefined; }; - const hashReader = new BinaryReader(bytes, input.little, "elf"); const hashWords = (address: bigint): ElfHashWordReader => (offset) => { try { // Hash walks skip bloom/bucket words; their whole prefix must be mapped. const at = index.offset(address, offset + 4) + offset; - return ok(hashReader.u32(at)); + return ok(reader.u32(at)); } catch (cause) { if (cause instanceof BinaryMappingFailure) return err(cause.message); throw cause; @@ -1015,14 +1006,7 @@ export function* readElfRelocationWritesSteps( return at === null ? undefined : { - value: - width === word - ? rawWord(at) - : BigInt( - input.little - ? bytes.readUInt32LE(at) - : bytes.readUInt32BE(at), - ), + value: reader.word(at, width === 4 ? 32 : input.bits), file_offset: at, relocation: null, }; diff --git a/src/domain/native/readBinaryContainer.ts b/src/domain/native/readBinaryContainer.ts index 246dfc1925..c1fb1eed6d 100644 --- a/src/domain/native/readBinaryContainer.ts +++ b/src/domain/native/readBinaryContainer.ts @@ -3,7 +3,7 @@ import { type BinaryContainer, } from "../binaryContainer.js"; import { - classifyFatContainerIdentity, + classifyFatContainerEvidence, readFatContainerHeader, readFatHeaderPrefix, readMachoHeaderPrefixFacts, @@ -46,11 +46,7 @@ const readNonElfContainer = (bytes: Buffer): BinaryContainer => { throw new BinaryFormatFailure("malformed", fatHeader.reason); const identity = fatHeader.state === "parsed" - ? classifyFatContainerIdentity(fatHeader, (offset) => { - if (offset >= bytes.length) return undefined; - if (offset + 4 > bytes.length) return null; - return bytes.readUInt32BE(offset); - }) + ? classifyFatContainerEvidence(fatHeader, bytes) : undefined; // An invalid FAT-shaped table is not proof of a Mach-O container: JVM // constant-pool bytes can share both the magic and a known CPU value. diff --git a/src/native/metadata/BinaryContainer.test.ts b/src/native/metadata/BinaryContainer.test.ts index e720bcfcb6..7a0bcf3772 100644 --- a/src/native/metadata/BinaryContainer.test.ts +++ b/src/native/metadata/BinaryContainer.test.ts @@ -3,6 +3,11 @@ import type { ZeroFillMapping, } from "../../domain/virtualFileMapping.js"; import { readBinaryContainer } from "../../domain/native/readBinaryContainer.js"; +import { + CPU, + fatImage, + machoImage, +} from "../../artifacts/apple/MachoImage.fixture.js"; import { expect, it } from "vitest"; import { BinaryFormatFailure, @@ -353,6 +358,29 @@ it("keeps a JVM-sized FAT count unconfirmed while rejecting malformed zero-count ); }); +it("requires an observed thin slice before identifying a FAT image", () => { + const cpu = CPU.arm64; + const thin = machoImage({ cpu, wide: true }); + const read = (bytes: Uint8Array) => + readBinaryContainer( + Buffer.from(fatImage([{ cpu, bytes }])), + "elf-directory", + ); + + expect(() => read(thin)).toThrow( + new BinaryFormatFailure( + "unsupported", + "Universal/fat Mach-O containers require selecting a thin slice", + ), + ); + expect(() => read(Buffer.alloc(thin.length))).toThrow( + new BinaryFormatFailure( + "unsupported", + "Only ELF, PE, and thin Mach-O native images are supported", + ), + ); +}); + /** An x86-64 ELF executable with one PT_LOAD declaring `fileSize` bytes. */ const elfWithLoadSegment = (fileSize: bigint): Buffer => { const bytes = Buffer.alloc(64 + 56);