feat(recorded-crash): decode AArch64 core register notes (#2505)

Squash merge of https://github.com/morluto/rea/pull/2505.

Co-authored-by: NIU-123370 <191000457+NIU-123370@users.noreply.github.com>
This commit is contained in:
The one -Niu
2026-10-13 23:48:24 +08:00
committed by GitHub
co-authored by NIU-123370
parent 2b98fc5f51
commit 522b9927c8
13 changed files with 532 additions and 90 deletions
@@ -0,0 +1,135 @@
import { describe, expect, it } from "vitest";
import {
RECORDED_LINUX_CORE_ARCHITECTURES,
RECORDED_LINUX_CORE_PROFILES,
} from "../../domain/native/recordedCrash.js";
import {
recordedCrashFixture,
recordedCrashFixtureBytes,
} from "../../../tests/fixtures/binaryDiagnostics/recordedCrash.js";
import { validateRecordedCrashSources } from "./RecordedCrashSourceBindings.js";
const diagnostics = { stdout: "", stderr: "", truncated: false } as const;
const bind = (
architecture?: "x86_64-little-endian" | "aarch64-little-endian",
) => {
const report = recordedCrashFixture("/selected.core", architecture);
return { report, bytes: recordedCrashFixtureBytes(report) };
};
describe("recorded core source bindings", () => {
it.each(RECORDED_LINUX_CORE_ARCHITECTURES)(
"binds each %s register to its own source bytes",
(architecture) => {
const { report, bytes } = bind(architecture);
expect(() =>
validateRecordedCrashSources(report, bytes, diagnostics),
).not.toThrow();
},
);
it("measures the AArch64 register window one slot wide per name", () => {
const profile = RECORDED_LINUX_CORE_PROFILES["aarch64-little-endian"];
expect(profile.registers.length).toBe(34);
expect(profile.prstatusBytes).toBe(392);
expect(profile.machine).toBe(183);
// The fixtures and the bindings both read these numbers, so they are pinned
// here against the ABI record rather than only used consistently.
expect(profile.registerOffset).toBe(112);
expect(profile.registerBytes).toBe(8);
const amd64 = RECORDED_LINUX_CORE_PROFILES["x86_64-little-endian"];
expect(amd64.registers.length).toBe(27);
expect(amd64.prstatusBytes).toBe(336);
expect(amd64.machine).toBe(62);
expect(amd64.registerOffset).toBe(112);
expect(amd64.registerBytes).toBe(8);
});
it("stamps each crafted core with the machine its profile declares", () => {
for (const architecture of RECORDED_LINUX_CORE_ARCHITECTURES) {
const bytes = bind(architecture).bytes;
expect(bytes.readUInt16LE(18)).toBe(
RECORDED_LINUX_CORE_PROFILES[architecture].machine,
);
}
});
it("rejects an AArch64 register bound to another slot", () => {
const { report, bytes } = bind("aarch64-little-endian");
const shifted = {
...report,
threads: report.threads.map((thread) => ({
...thread,
registers: thread.registers.map((register, index) =>
index === 3
? {
...register,
location: {
...register.location,
offset: `0x${(BigInt(register.location.offset) + 8n).toString(
16,
)}`,
},
}
: register,
),
})),
};
expect(() =>
validateRecordedCrashSources(shifted, bytes, diagnostics),
).toThrow(/aarch64-little-endian register identity/);
});
it("rejects an AArch64 note shorter than the profile descriptor", () => {
const { report, bytes } = bind("aarch64-little-endian");
const shortened = {
...report,
notes: report.notes.map((note, index) =>
index === 0
? {
...note,
descriptor_location: {
...note.descriptor_location,
bytes: "0x150",
},
}
: note,
),
};
expect(() =>
validateRecordedCrashSources(shortened, bytes, diagnostics),
).toThrow(/aarch64-little-endian source bytes/);
});
it("rejects an amd64 register name reported for AArch64", () => {
const { report, bytes } = bind("aarch64-little-endian");
const renamed = {
...report,
threads: report.threads.map((thread) => ({
...thread,
registers: thread.registers.map((register, index) =>
index === 0 ? { ...register, name: "rax" } : register,
),
})),
};
expect(() =>
validateRecordedCrashSources(renamed, bytes, diagnostics),
).toThrow(/aarch64-little-endian register identity/);
});
it("rejects a reported PID that differs from the recorded source bytes", () => {
const { report, bytes } = bind("aarch64-little-endian");
const wrongPid = {
...report,
threads: report.threads.map((thread) => ({
...thread,
historical_pid: thread.historical_pid + 1,
})),
};
expect(() =>
validateRecordedCrashSources(wrongPid, bytes, diagnostics),
).toThrow(/aarch64-little-endian source bytes/);
});
});
@@ -1,23 +1,26 @@
import { AnalysisOutputError } from "../../domain/analysisErrorCore.js";
import {
RECORDED_LINUX_AMD64_REGISTERS,
RECORDED_LINUX_CORE_PROFILES,
type RecordedCrash,
} from "../../domain/native/recordedCrash.js";
const registerOffsets = new Map<string, number>(
RECORDED_LINUX_AMD64_REGISTERS.map((name, index) => [name, 112 + index * 8]),
);
/** Bind interpreted values and register identities to Linux amd64 source bytes. */
/** Bind interpreted values and register identities to recorded source bytes. */
export function validateRecordedCrashSources(
report: RecordedCrash,
snapshot: Buffer,
diagnostics: RecordedCrash["decoder_diagnostics"],
): void {
const profile = RECORDED_LINUX_CORE_PROFILES[report.architecture];
const registerOffsets = new Map<string, number>(
profile.registers.map((name, index) => [
name,
profile.registerOffset + index * profile.registerBytes,
]),
);
const fail = () => {
throw new AnalysisOutputError(
"inspect_recorded_crash",
"Reported PID or signal value differs from its recorded Linux amd64 source bytes.",
`Reported PID or signal value differs from its recorded ${report.architecture} source bytes.`,
{ capturedOutput: diagnostics },
);
};
@@ -29,10 +32,10 @@ export function validateRecordedCrashSources(
if (length < minimumBytes) return fail();
return snapshot.subarray(start, start + length);
};
// Fixed offsets independently checked against unchanged pwntools 4.15.0
// elf_prstatus_amd64 / elf_siginfo_64; this is the sole supported note ABI.
// Offsets come from unchanged pwntools 4.15.0 elf_prstatus_amd64 /
// elf_prstatus_aarch64 and elf_siginfo_64 through the shared domain profile.
for (const thread of report.threads) {
const bytes = descriptor(thread.note_index, 336);
const bytes = descriptor(thread.note_index, profile.prstatusBytes);
if (
thread.historical_pid !== bytes.readInt32LE(32) ||
thread.recorded_current_signal !== bytes.readInt16LE(12)
@@ -49,7 +52,7 @@ export function validateRecordedCrashSources(
)
throw new AnalysisOutputError(
"inspect_recorded_crash",
"Reported register source does not match its Linux amd64 register identity.",
`Reported register source does not match its ${report.architecture} register identity.`,
{ capturedOutput: diagnostics },
);
}
@@ -1,5 +1,8 @@
import { AnalysisOutputError } from "../../domain/analysisErrorCore.js";
import type { RecordedCrash } from "../../domain/native/recordedCrash.js";
import {
RECORDED_LINUX_CORE_PROFILES,
type RecordedCrash,
} from "../../domain/native/recordedCrash.js";
// Exact amd64/core enum projections in the pinned pyelftools 0.33 profile.
const segmentKinds = new Map([
@@ -81,6 +84,8 @@ export function validateRecordedCrashStructure(
return snapshot.subarray(Number(offset), Number(offset) + bytes);
};
const header = bytesAt(0n, 64);
const expectedMachine =
RECORDED_LINUX_CORE_PROFILES[report.architecture].machine;
if (
!header
.subarray(0, 6)
@@ -90,7 +95,7 @@ export function validateRecordedCrashStructure(
![0, 3].includes(header[7] ?? -1) ||
header[8] !== 0 ||
header.readUInt16LE(16) !== 4 ||
header.readUInt16LE(18) !== 62
header.readUInt16LE(18) !== expectedMachine
)
fail();
const table = header.readBigUInt64LE(32);