Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions docs/apple-application-analysis.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,19 @@ rea trace-dylib-resolution MyApp.app --root Contents/MacOS/MyApp --architecture

Load commands are parsed in TypeScript from Mach-O and FAT headers, including
the `dylib_use_command` encoding newer linkers emit for flags such as delayed
initialization. Nothing is executed and no Apple tool is required, so the tool
also works off macOS.
initialization, and `LC_LAZY_LOAD_DYLIB_INFO` (`0x3a`). That command stores its
install name in a linkedit payload rather than in the load command itself.
Its flag and chain-start image offsets must lie in the slice VM range, which
is dyld's one-page floor extended by every segment except `__LINKEDIT`,
measured from the `__TEXT` vmaddr. Payload strings must start after the 24-byte
header. The load-path NUL must occur before the payload's final byte, matching
dyld's scan. Distinct payload bytes and payload-range lookups stay within the 16 MiB
limit, and a file range already read is reused. String scanning and UTF-8
checks count every reference, including repeats and overlaps, and stop at that
same aggregate. A slice retains at most 1024 of these commands; further copies are
unsupported instead of becoming one traced edge each.
Nothing is executed and no Apple tool is required, so the tool also works off
macOS.

Resolution follows dyld:

Expand Down Expand Up @@ -222,8 +233,10 @@ cache rather than on disk, so REA does not check them against the host.
`findings` are derived, not observed:

- `required-load-unresolved`, `weak-load-unresolved`, and `lazy-load-unresolved`.
`LC_LAZY_LOAD_DYLIB` dependencies are resolved but not traversed, because dyld
loads them on first use; an unresolved one is not a launch failure.
`LC_LAZY_LOAD_DYLIB` and `LC_LAZY_LOAD_DYLIB_INFO` dependencies are resolved
but not traversed, because dyld loads them on first use; an unresolved one is
not a launch failure. `LC_LAZY_LOAD_DYLIB_INFO` has no compatibility version,
so those fields are null. Its may-be-missing flag is reported as `weak`.
- `earlier-rpath-candidate-absent`: a compatible Mach-O placed at an earlier
modeled search path could take precedence, subject to unmodeled search inputs
and code-signing library validation. These findings do not establish which
Expand Down
6 changes: 5 additions & 1 deletion scripts/verify/apple/macos-dylib-e2e.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,12 @@ const verifyParserConformance = async (app, trace) => {
join(app, image.path),
slice.architecture,
);
// otool -l prints LC_LAZY_LOAD_DYLIB_INFO without its linkedit install
// name, so this comparison cannot expect that edge. The structural
// reader tests own the payload.
const edges = trace.edges.filter(
({ loader, architecture, root }) =>
({ loader, architecture, root, command }) =>
command !== "LC_LAZY_LOAD_DYLIB_INFO" &&
loader === image.path &&
architecture === slice.architecture &&
root ===
Expand Down
124 changes: 124 additions & 0 deletions src/artifacts/apple/MachoImage.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,130 @@ export const machoImage = (options: {
return bytes;
};

/**
* Little-endian `LC_LAZY_LOAD_DYLIB_INFO` (0x3a) payload.
* The 24-byte header is load-path offset, flag image offset, flags,
* pointer format, chain-start image offset, symbol count, and the
* symbol-offset array offset. Strings are NUL-terminated and the payload
* is padded to 8 bytes, matching a one-symbol `@rpath/libl.dylib` payload
* of 56 bytes.
*/
export const lazyLoadDylibInfoPayload = (options: {
readonly path: string;
readonly symbols?: readonly string[];
readonly flags?: number;
readonly flagImageOffset?: number;
readonly chainStartImageOffset?: number;
}): Uint8Array => {
const symbols = options.symbols ?? [];
const headerSize = 24;
const pathBytes = encoder.encode(options.path);
const symbolBytes = symbols.map((symbol) => encoder.encode(symbol));
const symbolArrayOffset = headerSize;
const pathOffset = symbolArrayOffset + symbols.length * 4;
let cursor = pathOffset + pathBytes.length + 1;
const symbolOffsets = symbolBytes.map((bytes) => {
const offset = cursor;
cursor += bytes.length + 1;
return offset;
});
const payload = new Uint8Array(Math.ceil(cursor / 8) * 8);
const view = new DataView(payload.buffer);
view.setUint32(0, pathOffset, true);
view.setUint32(4, options.flagImageOffset ?? 0, true);
view.setUint16(8, options.flags ?? 0, true);
view.setUint32(12, options.chainStartImageOffset ?? 0, true);
view.setUint32(16, symbols.length, true);
view.setUint32(20, symbolArrayOffset, true);
symbolOffsets.forEach((offset, index) =>
view.setUint32(symbolArrayOffset + index * 4, offset, true),
);
payload.set(pathBytes, pathOffset);
symbolBytes.forEach((bytes, index) => {
const offset = symbolOffsets[index];
if (offset !== undefined) payload.set(bytes, offset);
});
return payload;
};

/**
* Place one `LC_LAZY_LOAD_DYLIB_INFO` command among the other commands and
* append its payload after the image. `dataoff` is relative to this slice.
*/
/** `LC_SEGMENT` or `LC_SEGMENT_64` with a name and VM range, and no sections. */
export const segmentCommand = (options: {
readonly name: string;
readonly vmaddr: bigint;
readonly vmsize: bigint;
readonly wide?: boolean;
}): Uint8Array => {
const wide = options.wide ?? true;
const bytes = new Uint8Array(wide ? 72 : 56);
const view = new DataView(bytes.buffer);
view.setUint32(0, wide ? 0x19 : 0x1, true);
view.setUint32(4, bytes.length, true);
bytes.set(encoder.encode(options.name).subarray(0, 16), 8);
if (wide) {
view.setBigUint64(24, options.vmaddr, true);
view.setBigUint64(32, options.vmsize, true);
} else {
view.setUint32(24, Number(options.vmaddr), true);
view.setUint32(28, Number(options.vmsize), true);
}
return bytes;
};

/** 16-byte `LC_LAZY_LOAD_DYLIB_INFO` pointing at a slice-relative payload. */
export const lazyLoadDylibInfoCommand = (
dataOffset: number,
dataSize: number,
): Uint8Array => {
const bytes = new Uint8Array(16);
const view = new DataView(bytes.buffer);
view.setUint32(0, 0x3a, true);
view.setUint32(4, bytes.length, true);
view.setUint32(8, dataOffset, true);
view.setUint32(12, dataSize, true);
return bytes;
};

export const machoImageWithLazyLoadDylibInfo = (options: {
readonly path: string;
readonly symbols?: readonly string[];
readonly flags?: number;
readonly flagImageOffset?: number;
readonly chainStartImageOffset?: number;
readonly cpu?: { readonly type: number; readonly subtype: number };
readonly fileType?: number;
readonly wide?: boolean;
readonly commandsBefore?: readonly Uint8Array[];
readonly commandsAfter?: readonly Uint8Array[];
}): Uint8Array => {
const payload = lazyLoadDylibInfoPayload(options);
const command = new Uint8Array(16);
const commandView = new DataView(command.buffer);
commandView.setUint32(0, 0x3a, true);
commandView.setUint32(4, command.length, true);
const wide = options.wide ?? true;
const commandsBefore = options.commandsBefore ?? [];
const image = machoImage({
...(options.cpu === undefined ? {} : { cpu: options.cpu }),
...(options.fileType === undefined ? {} : { fileType: options.fileType }),
wide,
commands: [...commandsBefore, command, ...(options.commandsAfter ?? [])],
});
const out = new Uint8Array(image.length + payload.length);
out.set(image);
out.set(payload, image.length);
const headerSize = wide ? 32 : 28;
const commandAt =
headerSize + commandsBefore.reduce((total, item) => total + item.length, 0);
const view = new DataView(out.buffer);
view.setUint32(commandAt + 8, image.length, true);
view.setUint32(commandAt + 12, payload.length, true);
return out;
};

/** Thin Mach-O with one unknown command, for width and command-size regressions. */
export const machoImageWithUnknownCommand = (
wide: boolean,
Expand Down
Loading
Loading