mirror of
https://github.com/morluto/rea.git
synced 2026-11-20 05:32:44 +00:00
refactor(native): share mapped ELF and FAT scalar readers (#12631)
This commit is contained in:
1 parent
bd887a96be
commit
2c7c858c5a
4 files changed
+34
-28
No files matched your search
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user