Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
15 changes: 11 additions & 4 deletions docs/apple-ips.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,22 @@ The parsed slice facts remain available even when no match can be derived.
slice's offset, arch, CPU type and subtype, UUID, and `__TEXT` vmaddr, plus
`image_matches`, per-frame results, and join-specific limitations. A joined
frame reports `static_address`, the slice `__TEXT` vmaddr plus the frame's
recorded `imageOffset`, in decimal and hexadecimal. An unjoined frame reports
recorded `imageOffset`, in decimal and hexadecimal, and `symbol`, the
containing symbol from the joined slice's `LC_SYMTAB` symbol table: the
table name, its runtime spelling, the symbol's static address, and the byte
offset of the frame inside it. The symbol is the nearest preceding section
symbol in the same segment as the static address. When the joined slice
carries no `LC_SYMTAB`, or its table is unusable, the frame reports
`symbol: null` and the join records why in its limitations. An unjoined frame reports
a reason instead; an unknown image UUID, an absent `imageIndex` row, and a
frame without `imageOffset` all stay unjoined rather than treated as aligned.

An artifact that is not a Mach-O image or FAT container reports
`join.status: "unreadable"` with a typed reason and leaves every report fact
intact. Symbol names printed inside the report stay recorded claims. Function
or symbol lookup at a joined static address, and matching dSYM symbol tables,
are not performed by this step.
intact. Symbol names printed inside the report stay recorded claims; a joined
frame's `symbol` is derived from the joined artifact's own symbol table
instead. DWARF debug-info lookup and matching dSYM symbol tables are not
performed by this step.

Symbol names printed inside the report are recorded claims and are not
verified. Linux ELF cores use the separate
Expand Down
2 changes: 1 addition & 1 deletion src/domain/native/appleIps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ it("rejects extra caller arguments and admits an empty crash inventory", () => {
unlisted_body_keys: [],
join: null,
limitations: [
"Frames join a caller-selected Mach-O or dSYM only by matching slice LC_UUID and architecture; function and symbol resolution at a joined static address stays unobserved.",
"Frames join a caller-selected Mach-O or dSYM only by matching slice LC_UUID and architecture; a joined frame reports its containing symbol-table symbol when the joined slice carries LC_SYMTAB, and no symbol otherwise.",
],
});
expect(report.metadata.bug_type).toBe("309");
Expand Down
34 changes: 33 additions & 1 deletion src/domain/native/appleIps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,30 @@ const frameJoinSchema = z.strictObject({
static_address: recordedIntegerSchema
.nullable()
.describe("Slice __TEXT vmaddr plus the frame's recorded imageOffset."),
symbol: z
.strictObject({
name: z
.string()
.min(1)
.describe("Symbol-table name of the containing symbol, e.g. _main."),
runtime_name: z
.string()
.min(1)
.nullable()
.describe(
"Runtime spelling without the one leading underscore Darwin compilers add; the table name when unprefixed.",
),
address: recordedIntegerSchema.describe(
"Static address of the containing symbol.",
),
offset: recordedIntegerSchema.describe(
"Byte offset of the frame's static address from the containing symbol.",
),
})
.nullable()
.describe(
"Containing LC_SYMTAB symbol for the joined static address, or null when the joined slice carries no usable symbol table.",
),
});

/** Slice identity resolution against one caller-selected Mach-O or dSYM. */
Expand Down Expand Up @@ -393,6 +417,14 @@ export const appleIpsIntegerTextBytes = (
): number =>
value === null ? 0 : 2 * (value.decimal.length + (value.hex?.length ?? 0));

/** Retained integer text of one joined frame, including its symbol. */
const appleIpsJoinFrameIntegerTextBytes = (
frame: Extract<AppleIpsJoin, { status: "selected" }>["frames"][number],
): number =>
appleIpsIntegerTextBytes(frame.static_address) +
appleIpsIntegerTextBytes(frame.symbol?.address ?? null) +
appleIpsIntegerTextBytes(frame.symbol?.offset ?? null);

/** The named report fields own which observed and derived integers are retained. */
export const appleIpsReportIntegerTextBytes = (report: AppleIps): number => {
let bytes =
Expand Down Expand Up @@ -432,7 +464,7 @@ export const appleIpsReportIntegerTextBytes = (report: AppleIps): number => {
appleIpsIntegerTextBytes(match.text_vmaddr) +
appleIpsIntegerTextBytes(match.runtime_base);
for (const frame of report.join.frames)
bytes += appleIpsIntegerTextBytes(frame.static_address);
bytes += appleIpsJoinFrameIntegerTextBytes(frame);
}
return bytes;
};
Expand Down
125 changes: 105 additions & 20 deletions src/native/apple/AppleIpsMachOJoin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@ import { readMachoContainer } from "../../artifacts/apple/MachoLoadCommandReader
import { readUniversalMachoSlice } from "../../domain/apple/machoImageSelection.js";
import {
readMachoLoadCommandTableHeader,
type MachoLoadCommandTableHeader,
MachoLoadCommandCursor,
} from "../../domain/apple/machoLoadCommands.js";
import {
readMachoSegmentHeader,
admitMachoSegmentCommand,
} from "../../domain/apple/machoSegmentHeader.js";
import { FieldReader } from "./AppleIpsFieldReader.js";
import {
type AppleIpsJoinSegment,
type AppleIpsJoinSymbol,
appleIpsJoinFrameSymbol,
readAppleIpsJoinSymbols,
} from "./AppleIpsSymbolTable.js";
import {
machoArchitectureName,
machoKnownArm64Subtype,
Expand All @@ -23,6 +30,7 @@ import type { AppleIps, AppleIpsJoin } from "../../domain/native/appleIps.js";
*/

const LC_SEGMENT = 0x1;
const LC_SYMTAB = 0x2;
const LC_SEGMENT_64 = 0x19;
const LC_UUID = 0x1b;
const MAX_SLICES = 64;
Expand All @@ -43,6 +51,9 @@ interface SliceIdentity {
readonly cpuSubtype: number;
readonly uuid: string | null;
readonly textVmaddr: bigint | null;
readonly segments: readonly AppleIpsJoinSegment[];
readonly symbols: readonly AppleIpsJoinSymbol[];
readonly symbolIssue: string | null;
readonly unexamined: readonly string[];
}

Expand Down Expand Up @@ -104,6 +115,41 @@ const readSliceIdentities = async (bytes: Buffer): Promise<SliceParse> => {
return { kind: "slices", slices };
};

interface SliceSegment {
readonly address: bigint;
readonly size: bigint;
readonly isText: boolean;
}

/** Admit one segment command; null when malformed or 32/64-bit mismatched. */
const readSliceSegment = (
payload: Uint8Array,
command: number,
header: Pick<MachoLoadCommandTableHeader, "wide" | "littleEndian">,
imageBytes: number,
): SliceSegment | null => {
const wide = command === LC_SEGMENT_64;
if (wide !== header.wide) return null;
const segment = readMachoSegmentHeader(payload, wide, header.littleEndian);
if (
!segment.ok ||
!admitMachoSegmentCommand(
segment.value,
payload.length,
wide ? 64 : 32,
imageBytes,
).ok
)
return null;
return {
address: segment.value.address,
size: segment.value.memorySize,
isText:
segment.value.nameBytes.toString("utf8").replace(/\0.*$/su, "") ===
"__TEXT",
};
};

const sliceAt = (
bytes: Buffer,
offset: bigint,
Expand All @@ -116,6 +162,9 @@ const sliceAt = (
let uuid: string | null = null;
let uuidSeen = false;
let textVmaddr: bigint | null = null;
let symtabCommand: Uint8Array | null = null;
let symtabSeen = false;
const segments: AppleIpsJoinSegment[] = [];
for (;;) {
const next = cursor.nextOffset;
if (next === null) break;
Expand All @@ -128,37 +177,38 @@ const sliceAt = (
uuidSeen = true;
uuid = canonicalUuidBytes(payload.subarray(8, 24));
}
if (frame.value.command === LC_SYMTAB) {
if (symtabSeen) return null;
symtabSeen = true;
symtabCommand = payload;
}
if (
frame.value.command === LC_SEGMENT ||
frame.value.command === LC_SEGMENT_64
) {
const wide = frame.value.command === LC_SEGMENT_64;
if (wide !== header.wide) return null;
const segment = readMachoSegmentHeader(
const segment = readSliceSegment(
payload,
wide,
header.littleEndian,
frame.value.command,
header,
bytes.length,
);
if (
!segment.ok ||
!admitMachoSegmentCommand(
segment.value,
payload.length,
wide ? 64 : 32,
bytes.length,
).ok
)
return null;
const name = segment.value.nameBytes
.toString("utf8")
.replace(/\0.*$/su, "");
if (name === "__TEXT") {
if (segment === null) return null;
segments.push({ address: segment.address, size: segment.size });
if (segment.isText) {
if (textVmaddr !== null) return null;
textVmaddr = segment.value.address;
textVmaddr = segment.address;
}
}
}
if (!cursor.finish().ok) return null;
const symbolTable = readAppleIpsJoinSymbols({
bytes,
headerSize: header.headerSize,
wide: header.wide,
littleEndian: header.littleEndian,
symtabCommand,
segments,
});
const { cpuType, cpuSubtype } = header.identity;
return {
index,
Expand All @@ -168,6 +218,14 @@ const sliceAt = (
arch: machoArchitectureName(cpuType, cpuSubtype),
uuid,
textVmaddr,
segments,
symbols: symbolTable.kind === "symbols" ? symbolTable.symbols : [],
symbolIssue:
symbolTable.kind === "absent"
? `slice ${String(index)} carries no LC_SYMTAB, so joined frames report no symbol`
: symbolTable.kind === "unusable"
? `slice ${String(index)} symbol table is unusable: ${symbolTable.reason}; joined frames report no symbol`
: null,
unexamined: [],
};
};
Expand Down Expand Up @@ -311,6 +369,13 @@ export const resolveAppleIpsJoin = async (
matches.push(digest);
matchedImage.set(image.index, digest);
}
for (const slice of parsed.slices) {
if (
slice.symbolIssue !== null &&
matches.some((match) => match.slice_index === slice.index)
)
limitations.push(slice.symbolIssue);
}
const frames = report.threads.flatMap((thread) =>
thread.frames.map((frame) =>
frameJoin(frame, thread.index, {
Expand Down Expand Up @@ -417,12 +482,31 @@ const frameJoin = (
"frame records no imageOffset",
);
const staticAddress = context.read.add(digest.text_vmaddr, offset);
if (staticAddress === null)
return unjoined(
threadIndex,
frame.frame_index,
"frame static address could not be derived",
);
const slice = context.slices.find(
(candidate) => candidate.index === digest.slice_index,
);
const staticValue = BigInt(staticAddress.decimal);
return {
thread_index: threadIndex,
frame_index: frame.frame_index,
status: "joined",
reason: null,
static_address: staticAddress,
symbol:
slice === undefined
? null
: appleIpsJoinFrameSymbol(
slice.symbols,
slice.segments,
staticValue,
(value) => context.read.recordedUnsigned64(value),
),
};
};

Expand All @@ -436,4 +520,5 @@ const unjoined = (
status: "unjoined",
reason,
static_address: null,
symbol: null,
});
2 changes: 1 addition & 1 deletion src/native/apple/AppleIpsParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const recordedUuidPattern =
/^(?:[0-9a-fA-F]{32}|[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})$/u;
const IPS_LIMITATIONS = [
"Rows reproduce the selected .ips JSON; they do not establish that the process still exists or that a live kernel agrees with the recorded state.",
"Frames join a caller-selected Mach-O or dSYM only by matching slice LC_UUID and architecture; function and symbol resolution at a joined static address stays unobserved.",
"Frames join a caller-selected Mach-O or dSYM only by matching slice LC_UUID and architecture; a joined frame reports its containing symbol-table symbol when the joined slice carries LC_SYMTAB, and no symbol otherwise.",
"Symbol names printed inside the report are recorded claims and are not verified.",
];

Expand Down
Loading
Loading