feat(native): inspect recorded crashes through pwntools and pwndbg (#1054)
* feat(native): inspect recorded crashes through pwntools and pwndbg * fix(native): bind core note semantics to raw owner bytes * fix(native): validate recorded core ABI and evidence scalars * fix(native): require complete recorded note interpretations * fix(native): bind physical core notes to original headers
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
type Range = { readonly offset: string; readonly bytes: string };
|
||||
type Segment = {
|
||||
readonly index: number;
|
||||
readonly type: string | number;
|
||||
readonly offset: string;
|
||||
readonly file_size: string;
|
||||
};
|
||||
type Note = {
|
||||
readonly segment_index: number;
|
||||
readonly location: Range;
|
||||
readonly owner_location: Range;
|
||||
readonly descriptor_location: Range;
|
||||
};
|
||||
type Padding = {
|
||||
readonly segment_index: number;
|
||||
readonly location: Range;
|
||||
readonly bytes_base64: string;
|
||||
};
|
||||
const align = (size: bigint) => ((size + 3n) / 4n) * 4n;
|
||||
const layoutMatches = (note: Note): boolean => {
|
||||
const start = BigInt(note.location.offset);
|
||||
const owner = BigInt(note.owner_location.bytes);
|
||||
const descriptor = BigInt(note.descriptor_location.bytes);
|
||||
return (
|
||||
BigInt(note.owner_location.offset) === start + 12n &&
|
||||
BigInt(note.descriptor_location.offset) === start + 12n + align(owner) &&
|
||||
BigInt(note.location.bytes) === 12n + align(owner) + align(descriptor)
|
||||
);
|
||||
};
|
||||
const paddingMatches = (
|
||||
item: Padding,
|
||||
segment: Segment | undefined,
|
||||
): boolean => {
|
||||
const bytes = BigInt(item.location.bytes);
|
||||
const decodedLength =
|
||||
(item.bytes_base64.length / 4) * 3 -
|
||||
(item.bytes_base64.endsWith("==")
|
||||
? 2
|
||||
: item.bytes_base64.endsWith("=")
|
||||
? 1
|
||||
: 0);
|
||||
return (
|
||||
bytes > 0n &&
|
||||
bytes < 12n &&
|
||||
BigInt(decodedLength) === bytes &&
|
||||
/^A*={0,2}$/.test(item.bytes_base64) &&
|
||||
segment !== undefined &&
|
||||
BigInt(item.location.offset) + bytes ===
|
||||
BigInt(segment.offset) + BigInt(segment.file_size)
|
||||
);
|
||||
};
|
||||
|
||||
/** Require exact aligned note layouts and one complete, nonoverlapping physical coverage. */
|
||||
export function recordedNoteCoverageIssues(
|
||||
segments: readonly Segment[],
|
||||
notes: readonly Note[],
|
||||
padding: readonly Padding[],
|
||||
): readonly string[] {
|
||||
const issues: string[] = [];
|
||||
const bySegment = new Map<number, Range[]>();
|
||||
const all: Range[] = [];
|
||||
const add = (segment: number, range: Range) => {
|
||||
const ranges = bySegment.get(segment) ?? [];
|
||||
ranges.push(range);
|
||||
bySegment.set(segment, ranges);
|
||||
all.push(range);
|
||||
};
|
||||
for (const note of notes) {
|
||||
if (!layoutMatches(note))
|
||||
issues.push(
|
||||
"Recorded note layout differs from its aligned header/owner/descriptor ranges.",
|
||||
);
|
||||
add(note.segment_index, note.location);
|
||||
}
|
||||
for (const item of padding) {
|
||||
if (!paddingMatches(item, segments[item.segment_index]))
|
||||
issues.push(
|
||||
"Recorded note padding must be a short zero-filled segment tail.",
|
||||
);
|
||||
add(item.segment_index, item.location);
|
||||
}
|
||||
const compare = (left: Range, right: Range) =>
|
||||
BigInt(left.offset) < BigInt(right.offset)
|
||||
? -1
|
||||
: BigInt(left.offset) > BigInt(right.offset)
|
||||
? 1
|
||||
: 0;
|
||||
let end = 0n;
|
||||
for (const range of all.sort(compare)) {
|
||||
const start = BigInt(range.offset);
|
||||
if (start < end)
|
||||
issues.push("Physical recorded note/padding ranges overlap or repeat.");
|
||||
if (start + BigInt(range.bytes) > end) end = start + BigInt(range.bytes);
|
||||
}
|
||||
for (const segment of segments) {
|
||||
if (segment.type !== "PT_NOTE") continue;
|
||||
let cursor = BigInt(segment.offset);
|
||||
for (const range of (bySegment.get(segment.index) ?? []).sort(compare)) {
|
||||
if (BigInt(range.offset) !== cursor)
|
||||
issues.push(
|
||||
"Recorded note/padding ranges leave a gap or overlap in their segment.",
|
||||
);
|
||||
cursor = BigInt(range.offset) + BigInt(range.bytes);
|
||||
}
|
||||
if (cursor !== BigInt(segment.offset) + BigInt(segment.file_size))
|
||||
issues.push(
|
||||
"Recorded note/padding ranges do not completely cover their segment.",
|
||||
);
|
||||
}
|
||||
return issues;
|
||||
}
|
||||
Reference in New Issue
Block a user