refactor(apple): share lazy dylib payload field reads (#12536)

This commit is contained in:
RRFZ authored and GitHub committed 2026-11-19 16:56:25 -05:00
1 parent 503379b953
commit 756f772430
4 files changed
+59 -17

No files matched your search

@@ -5,7 +5,7 @@
"src/domain/apple/machoCommandNames.ts": 24,
"src/domain/apple/machoContainer.ts": 13,
"src/domain/apple/machoEncryptionReader.ts": 1,
"src/domain/apple/machoLoadCommandReader.ts": 12,
"src/domain/apple/machoLoadCommandReader.ts": 3,
"src/domain/apple/plistScalars.ts": 6,
"src/domain/apple/propertyListDecodeBudget.ts": 2,
"src/domain/binaryTarget.ts": 10,
@@ -0,0 +1,37 @@
import { expect, it } from "vitest";
import { readMachoImage } from "../../domain/apple/machoLoadCommandReader.js";
import {
machoImageWithLazyLoadDylibInfo,
readerOf,
} from "./MachoImage.fixture.js";
it("resolves lazy dylib fields in an unaligned Uint8Array view", async () => {
const name = "@rpath/libExample.dylib";
const image = machoImageWithLazyLoadDylibInfo({
path: name,
symbols: ["_one", "_two"],
flags: 1,
});
const backing = new Uint8Array(image.length + 12).fill(0xff);
backing.set(image, 5);
const slice = backing.subarray(5, 5 + image.length);
const facts = await readMachoImage(readerOf(slice), slice.length);
expect(facts).toMatchObject({
status: "parsed",
slices: [
{
dependencies: [
{
command: "LC_LAZY_LOAD_DYLIB_INFO",
install_name: name,
weak: true,
},
],
},
],
});
const short = slice.subarray(0, -1);
expect(await readMachoImage(readerOf(short), short.length)).toMatchObject({
status: "malformed",
});
});
+12 -11
View File
@@ -12,6 +12,7 @@ import { readMachoSegmentHeader } from "./machoSegmentHeader.js";
import {
MachoLoadCommandCursor,
machoCommandTableIssue,
machoWordReader,
readMachoHeaderDeclarations,
readMachoCommandStringBytes,
readMachoLinkeditDataCommand,
@@ -798,9 +799,9 @@ const decodeLazyLoadDylibInfoCommand = (
"malformed",
`load command ${index} is too short for LC_LAZY_LOAD_DYLIB_INFO`,
);
const view = viewOf(body);
const dataOffset = view.getUint32(8, true);
const dataSize = view.getUint32(12, true);
const reader = machoWordReader(body, true);
const dataOffset = reader.u32(8);
const dataSize = reader.u32(12);
if (dataSize < LAZY_INFO_HEADER_BYTES)
throw machoIssue(
"malformed",
@@ -884,13 +885,13 @@ const decodeLazyLoadDylibInfoPayload = (
maxVmOffset: bigint,
work: LazyPayloadWork,
): MachoDependency => {
const view = viewOf(payload);
const pathOffset = view.getUint32(0, true);
const flagImageOffset = view.getUint32(4, true);
const flags = view.getUint16(8, true);
const chainStartImageOffset = view.getUint32(12, true);
const symbolCount = view.getUint32(16, true);
const symbolArrayOffset = view.getUint32(20, true);
const reader = machoWordReader(payload, true);
const pathOffset = reader.u32(0);
const flagImageOffset = reader.u32(4);
const flags = reader.u16(8);
const chainStartImageOffset = reader.u32(12);
const symbolCount = reader.u32(16);
const symbolArrayOffset = reader.u32(20);
if (BigInt(flagImageOffset) > maxVmOffset)
throw machoIssue(
"malformed",
@@ -931,7 +932,7 @@ const decodeLazyLoadDylibInfoPayload = (
);
work.symbolReferences = symbolReferences;
for (let symbol = 0; symbol < symbolCount; symbol += 1) {
const offset = view.getUint32(symbolArrayOffset + symbol * 4, true);
const offset = reader.u32(symbolArrayOffset + symbol * 4);
payloadString(
payload,
offset,
+9 -5
View File
@@ -8,7 +8,11 @@ import {
const MACHO_LOAD_COMMAND_HEADER_BYTES = 8;
const wordReader = (bytes: Uint8Array, littleEndian: boolean): BinaryReader =>
/** Read numeric fields from exactly the supplied Mach-O view. */
export const machoWordReader = (
bytes: Uint8Array,
littleEndian: boolean,
): BinaryReader =>
new BinaryReader(
Buffer.isBuffer(bytes)
? bytes
@@ -47,7 +51,7 @@ export const readMachoHeaderDeclarations = (
return err(
`truncated Mach-O header: a ${wide ? 64 : 32}-bit header needs ${headerSize} bytes; the file has ${fileSize}`,
);
const reader = wordReader(bytes, littleEndian);
const reader = machoWordReader(bytes, littleEndian);
const commandBytes = reader.u32(20);
return ok({
headerSize,
@@ -145,7 +149,7 @@ export class MachoLoadCommandCursor {
bytes.byteLength < MACHO_LOAD_COMMAND_HEADER_BYTES
)
return err(`load command ${this.#index} starts beyond sizeofcmds`);
const reader = wordReader(bytes, littleEndian);
const reader = machoWordReader(bytes, littleEndian);
const size = reader.u32(4);
if (
size < MACHO_LOAD_COMMAND_HEADER_BYTES ||
@@ -269,7 +273,7 @@ export const decodeMachoFixedWordFields = (
const names = FIXED_WORD_FIELDS.get(command);
if (names === undefined) return undefined;
if (fieldBytes.byteLength < names.length * 4) return null;
const reader = wordReader(fieldBytes, littleEndian);
const reader = machoWordReader(fieldBytes, littleEndian);
return Object.fromEntries(
names.map((name, index) => [name, reader.u32(index * 4)]),
);
@@ -367,7 +371,7 @@ export const readMachoCommandStringBytes = (
_tag: "InvalidMachoCommandString",
reason: "truncated-offset",
});
const reader = wordReader(body, littleEndian);
const reader = machoWordReader(body, littleEndian);
const offset = reader.u32(8);
if (offset < minimumOffset || offset >= body.byteLength)
return err({