refactor(elf): retire the obsolete unwind buffer parser (#14007)

This commit is contained in:
morluto authored and GitHub committed 2026-11-23 00:39:55 -05:00
1 parent 880a2d5d51
commit 5acdeb79fb
4 files changed
+273 -306

No files matched your search

@@ -7,9 +7,10 @@ import {
inspectElfUnwindRangesInputSchema,
type ElfUnwindRanges,
} from "../../domain/native/elfUnwindRanges.js";
import { parseElfUnwindRanges } from "../../native/elf/ElfUnwindTables.js";
import { parseElfUnwindRangesFromHandle } from "../../native/elf/ElfUnwindTables.js";
import {
elfUnwindAbsentFixture,
elfUnwindByteSource,
elfUnwindFixture,
elfUnwindSectionlessFixture,
} from "../../native/elf/ElfUnwindTables.fixture.js";
@@ -30,13 +31,13 @@ const undecodableHeaderPointer = (): Buffer => {
const parsed = (
bytes: Buffer,
raw: Readonly<Record<string, unknown>> = {},
): ElfUnwindRanges => {
): Promise<ElfUnwindRanges> => {
const input = inspectElfUnwindRangesInputSchema.parse({
path: "/artifacts/unwind.elf",
...raw,
});
return parseElfUnwindRanges(
bytes,
return parseElfUnwindRangesFromHandle(
elfUnwindByteSource(bytes),
{
path: input.path,
sha256: "a".repeat(64),
@@ -46,7 +47,7 @@ const parsed = (
);
};
const report = parsed(elfUnwindAbsentFixture());
const report = await parsed(elfUnwindAbsentFixture());
const accept = async (
value: ElfUnwindRanges,
@@ -85,7 +86,6 @@ it("rejects a relative path and extra arguments before reading", async () => {
it("records an absent unwind table for the selected ELF", async () => {
const result = await accept(report);
expect(result.ok).toBe(true);
if (!result.ok) throw result.error;
expect(result.value).toMatchObject({
confidence: "observed",
@@ -160,17 +160,15 @@ it.each([
elfUnwindFixture({ header: "match", headerCount: 0x7fffffff }),
],
] as const)("accepts parser output for %s", async (_label, bytes) => {
const value = parsed(bytes);
const value = await parsed(bytes);
const result = await accept(value);
expect(result.ok, _label).toBe(true);
if (!result.ok) throw result.error;
expect(result.value.normalized_result).toEqual(value);
});
it("accepts a paged parser report without changing coverage", async () => {
const value = parsed(elfUnwindFixture(), { offset: 1, limit: 1 });
const value = await parsed(elfUnwindFixture(), { offset: 1, limit: 1 });
const result = await accept(value, { offset: 1, limit: 1 });
expect(result.ok).toBe(true);
if (!result.ok) throw result.error;
expect(result.value.normalized_result).toMatchObject({
page: { offset: 1, limit: 1, returned: 1, total: 2, next_offset: null },
@@ -179,13 +177,12 @@ it("accepts a paged parser report without changing coverage", async () => {
});
it("bounds CIE Evidence locations by the page, not by the CIE count", async () => {
const value = parsed(elfUnwindFixture({ extraCies: 500 }), {
const value = await parsed(elfUnwindFixture({ extraCies: 500 }), {
offset: 1,
limit: 1,
});
expect(value.coverage.total_cies).toBe(502);
const result = await accept(value, { offset: 1, limit: 1 });
expect(result.ok).toBe(true);
if (!result.ok) throw result.error;
expect(result.value.normalized_result).toMatchObject({
cies: [{ index: 1 }],
@@ -195,8 +192,8 @@ it("bounds CIE Evidence locations by the page, not by the CIE count", async () =
});
it("rejects a page that returns a CIE none of its ranges name", async () => {
const full = parsed(elfUnwindFixture({ extraCies: 2 }));
const page = parsed(elfUnwindFixture({ extraCies: 2 }), {
const full = await parsed(elfUnwindFixture({ extraCies: 2 }));
const page = await parsed(elfUnwindFixture({ extraCies: 2 }), {
offset: 1,
limit: 1,
});
@@ -211,11 +208,10 @@ it("rejects a page that returns a CIE none of its ranges name", async () => {
});
it("accepts an FDE with a zero LSDA under a CIE that declares an LSDA encoding", async () => {
const value = parsed(elfUnwindFixture({ zeroLsda: true }));
const value = await parsed(elfUnwindFixture({ zeroLsda: true }));
expect(value.cies[1]?.lsda_encoding?.raw).toBe(0x1b);
expect(value.ranges[1]?.lsda).toBeNull();
const result = await accept(value);
expect(result.ok).toBe(true);
if (!result.ok) throw result.error;
expect(result.value.normalized_result).toMatchObject({
ranges: [{ lsda: null }, { lsda: null }],
@@ -238,14 +234,13 @@ it("accepts a header that declares a count but omits its search table", async ()
)
bytes[Number(bytes.readBigUInt64LE(at + 24)) + 3] = 0xff;
}
const value = parsed(bytes);
const value = await parsed(bytes);
expect(value.eh_frame_hdr).toMatchObject({
status: "present",
fde_count: 2,
table_encoding: { raw: 0xff },
});
const result = await accept(value);
expect(result.ok).toBe(true);
if (!result.ok) throw result.error;
expect(result.value.normalized_result).toMatchObject({
coverage: { status: "complete", eh_frame_hdr_agreement: "unavailable" },
@@ -253,14 +248,13 @@ it("accepts a header that declares a count but omits its search table", async ()
});
it("accepts a null personality stored under an indirect encoding", async () => {
const value = parsed(elfUnwindFixture({ nullPersonality: true }));
const value = await parsed(elfUnwindFixture({ nullPersonality: true }));
expect(value.cies[1]?.personality).toMatchObject({
address: "0x0",
indirect: false,
encoding: { raw: 0x9b },
});
const result = await accept(value);
expect(result.ok).toBe(true);
if (!result.ok) throw result.error;
expect(result.value.normalized_result).toMatchObject({
ranges: [{ personality: null }, { personality: { address: "0x0" } }],
@@ -268,7 +262,7 @@ it("accepts a null personality stored under an indirect encoding", async () => {
});
it("rejects a direct personality reported as indirect", async () => {
const value = parsed(elfUnwindFixture({ nullPersonality: true }));
const value = await parsed(elfUnwindFixture({ nullPersonality: true }));
const cie = value.cies[1];
if (cie?.personality == null) throw new Error("fixture has no personality");
cie.personality.encoding = { raw: 0x1b, name: "pcrel|sdata4" };
@@ -308,7 +302,7 @@ it.each([
},
],
] as const)("rejects parser output with %s", async (_label, mutate) => {
const value = structuredClone(parsed(elfUnwindFixture()));
const value = structuredClone(await parsed(elfUnwindFixture()));
mutate(value);
const result = await accept(value);
expect(result.ok).toBe(false);
@@ -316,11 +310,10 @@ it.each([
});
it("reports a header capped by max_ranges as an incomplete Result", async () => {
const value = parsed(elfUnwindFixture({ header: "duplicate-row" }), {
const value = await parsed(elfUnwindFixture({ header: "duplicate-row" }), {
max_ranges: 2,
});
const result = await accept(value, { max_ranges: 2 });
expect(result.ok).toBe(true);
if (!result.ok) throw result.error;
expect(result.value.normalized_result).toMatchObject({
eh_frame_hdr: { status: "incomplete", fde_count: 3, table_entries: 2 },
@@ -330,7 +323,9 @@ it("reports a header capped by max_ranges as an incomplete Result", async () =>
it("rejects header table entries beyond the max_ranges budget", async () => {
const value = structuredClone(
parsed(elfUnwindFixture({ header: "duplicate-row" }), { max_ranges: 2 }),
await parsed(elfUnwindFixture({ header: "duplicate-row" }), {
max_ranges: 2,
}),
);
value.eh_frame_hdr.table_entries = 3;
const result = await accept(value, { max_ranges: 2 });
@@ -339,7 +334,7 @@ it("rejects header table entries beyond the max_ranges budget", async () => {
});
it("rejects a report whose artifact path is not the selected file", async () => {
const value = structuredClone(parsed(elfUnwindFixture()));
const value = structuredClone(await parsed(elfUnwindFixture()));
value.artifact.path = "/artifacts/other";
const result = await new ElfUnwindRangesService({
identity,
@@ -1,3 +1,12 @@
import type { NativeByteSource } from "../../domain/native/nativeSectionDirectory.js";
/** In-memory carrier with the same retained prefix as the physical provider. */
export const elfUnwindByteSource = (bytes: Buffer): NativeByteSource => ({
size: bytes.length,
prefix: bytes.subarray(0, 4096),
readRange: async (offset, length) => bytes.subarray(offset, offset + length),
});
/** Hand-assembled ELF images with .eh_frame CIE/FDE records and .eh_frame_hdr. */
export const UNWIND_FN_A = 0x400100;
File diff suppressed because it is too large. Load diff
+5 -32
View File
@@ -35,9 +35,9 @@ import {
import { buildIntervalIndex } from "./ElfIntervalIndex.js";
import {
ELF_PT_GNU_EH_FRAME,
readElfDirectoryFromBuffer,
readElfDirectoryFromReader,
type ElfProgramDeclaration,
type ElfSectionDirectory,
type NativeByteSource,
type NativeSection,
} from "../../domain/native/nativeSectionDirectory.js";
@@ -264,34 +264,7 @@ function* readSlice(
return yield { offset, length };
}
/** Read .eh_frame/.eh_frame_hdr function ranges. Truncation stays incomplete. */
export const parseElfUnwindRanges = (
bytes: Buffer,
artifact: ElfUnwindRanges["artifact"],
input: InspectElfUnwindRangesInput,
signal?: AbortSignal,
): ElfUnwindRanges => {
assertArtifactReadActive(signal);
const directory = readElfDirectoryFromBuffer(bytes, OPERATION);
const steps = elfUnwindRangesSteps(
bytes.length,
artifact,
input,
directory,
signal,
);
let next = steps.next();
while (!next.done) {
const { offset, length } = next.value;
const end = Math.min(bytes.length, offset + length);
next = steps.next(
offset >= bytes.length ? Buffer.alloc(0) : bytes.subarray(offset, end),
);
}
return next.value;
};
/** Read the same unwind facts over a bounded FileHandle without buffering the full image. */
/** Read unwind ranges over a bounded file handle. Truncation stays incomplete. */
export const parseElfUnwindRangesFromHandle = async (
source: NativeByteSource,
artifact: ElfUnwindRanges["artifact"],
@@ -347,7 +320,7 @@ function* elfUnwindRangesSteps(
fileBytes: number,
artifact: ElfUnwindRanges["artifact"],
input: InspectElfUnwindRangesInput,
directory: ReturnType<typeof readElfDirectoryFromBuffer>,
directory: ElfSectionDirectory,
signal?: AbortSignal,
): Generator<ReadStep, ElfUnwindRanges, Buffer> {
assertArtifactReadActive(signal);
@@ -1316,7 +1289,7 @@ const fileRange = (section: NativeSection): FileRange | null => {
*/
function* resolveGotBase(
fileBytes: number,
directory: ReturnType<typeof readElfDirectoryFromBuffer>,
directory: ElfSectionDirectory,
): Generator<
ReadStep,
{ readonly address: bigint; readonly source: string } | null,
@@ -2013,7 +1986,7 @@ interface MatchedSymbol {
* and each name retains only its location and length until page selection.
*/
function* scanSymbols(
directory: ReturnType<typeof readElfDirectoryFromBuffer>,
directory: ElfSectionDirectory,
wanted: ReadonlySet<bigint>,
signal: AbortSignal | undefined,
report: {