From 5acdeb79fb4d5bedbf83d9a63f324cf7599bd0aa Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Mon, 23 Nov 2026 00:39:55 -0500 Subject: [PATCH] refactor(elf): retire the obsolete unwind buffer parser (#14007) --- .../ElfUnwindRangesService.test.ts | 47 +- src/native/elf/ElfUnwindTables.fixture.ts | 9 + src/native/elf/ElfUnwindTables.test.ts | 486 +++++++++--------- src/native/elf/ElfUnwindTables.ts | 37 +- 4 files changed, 273 insertions(+), 306 deletions(-) diff --git a/src/application/binaryDiagnostics/ElfUnwindRangesService.test.ts b/src/application/binaryDiagnostics/ElfUnwindRangesService.test.ts index d106d29505..c04622833b 100644 --- a/src/application/binaryDiagnostics/ElfUnwindRangesService.test.ts +++ b/src/application/binaryDiagnostics/ElfUnwindRangesService.test.ts @@ -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> = {}, -): ElfUnwindRanges => { +): Promise => { 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, diff --git a/src/native/elf/ElfUnwindTables.fixture.ts b/src/native/elf/ElfUnwindTables.fixture.ts index d8d726ce13..76555d4dba 100644 --- a/src/native/elf/ElfUnwindTables.fixture.ts +++ b/src/native/elf/ElfUnwindTables.fixture.ts @@ -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; diff --git a/src/native/elf/ElfUnwindTables.test.ts b/src/native/elf/ElfUnwindTables.test.ts index 48ae6cbe51..a5802f811d 100644 --- a/src/native/elf/ElfUnwindTables.test.ts +++ b/src/native/elf/ElfUnwindTables.test.ts @@ -1,15 +1,12 @@ import { createHash } from "node:crypto"; import { expect, it, vi } from "vitest"; -import { ArtifactReaderFailure } from "../../resources/ArtifactReader.js"; +import type { NativeByteSource } from "../../domain/native/nativeSectionDirectory.js"; import { elfUnwindRangesSchema, inspectElfUnwindRangesInputSchema, } from "../../domain/native/elfUnwindRanges.js"; -import { - parseElfUnwindRanges, - parseElfUnwindRangesFromHandle, -} from "./ElfUnwindTables.js"; +import { parseElfUnwindRangesFromHandle } from "./ElfUnwindTables.js"; import { UNWIND_FN_A, UNWIND_FN_A_SIZE, @@ -20,6 +17,7 @@ import { UNWIND_PERSONALITY, UNWIND_SUFFIX_VA, elfUnwindAbsentFixture, + elfUnwindByteSource, elfUnwindFixture, elfUnwindSectionlessFixture, } from "./ElfUnwindTables.fixture.js"; @@ -55,8 +53,8 @@ const address = (value: number): string => `0x${value.toString(16)}`; it.each([true, false])( "retains valid ten-byte sign padding in CIE alignment (little %s)", - (little) => { - const report = parse( + async (little) => { + const report = await parse( elfUnwindFixture({ little, header: "match", @@ -71,8 +69,8 @@ it.each([true, false])( }, ); -it("rejects an overflowing ULEB pointer instead of wrapping it to a fabricated function address", () => { - const report = parse( +it("rejects an overflowing ULEB pointer instead of wrapping it to a fabricated function address", async () => { + const report = await parse( elfUnwindFixture({ header: "absent", leb128: { @@ -88,8 +86,8 @@ it("rejects an overflowing ULEB pointer instead of wrapping it to a fabricated f expect(report.coverage.status).toBe("partial"); }); -it("rejects a padded negative SLEB range instead of publishing a huge positive size", () => { - const report = parse( +it("rejects a padded negative SLEB range instead of publishing a huge positive size", async () => { + const report = await parse( elfUnwindFixture({ header: "absent", leb128: { @@ -123,8 +121,8 @@ it.each([ }, ])( "retains the representable LEB128 boundary $expected", - ({ signed, start, expected }) => { - const report = parse( + async ({ signed, start, expected }) => { + const report = await parse( elfUnwindFixture({ header: "absent", leb128: { pointers: { signed, start, size: [0x20] } }, @@ -143,8 +141,8 @@ it.each( ].map((start) => ({ start })), )( "omits an overflowing or unterminated signed operand and retains its sibling FDE (%j)", - ({ start }) => { - const report = parse( + async ({ start }) => { + const report = await parse( elfUnwindFixture({ header: "absent", leb128: { pointers: { signed: true, start, size: [0x20] } }, @@ -155,49 +153,36 @@ it.each( }, ); -const parse = (bytes: Buffer, raw: Readonly> = {}) => { +const parse = async ( + bytes: Buffer, + raw: Readonly> = {}, + readRange?: NativeByteSource["readRange"], + signal?: AbortSignal, +) => { const input = inspectElfUnwindRangesInputSchema.parse({ path: "/artifacts/unwind.elf", ...raw, }); + const source = + readRange === undefined + ? elfUnwindByteSource(bytes) + : { size: bytes.length, readRange }; return elfUnwindRangesSchema.parse( - parseElfUnwindRanges( - bytes, + await parseElfUnwindRangesFromHandle( + source, { path: input.path, sha256: createHash("sha256").update(bytes).digest("hex"), bytes: bytes.length, }, input, + signal, ), ); }; -const parseFromSource = ( - bytes: Buffer, - raw: Readonly> = {}, - readRange: (offset: number, length: number) => Promise = async ( - offset, - length, - ) => bytes.subarray(offset, offset + length), -) => { - const input = inspectElfUnwindRangesInputSchema.parse({ - path: "/artifacts/unwind.elf", - ...raw, - }); - return parseElfUnwindRangesFromHandle( - { size: bytes.length, readRange }, - { - path: input.path, - sha256: createHash("sha256").update(bytes).digest("hex"), - bytes: bytes.length, - }, - input, - ); -}; - -it("recovers two ELF64 ranges, an indirect personality, and a header mismatch", () => { - const report = parse(elfUnwindFixture()); +it("recovers two ELF64 ranges, an indirect personality, and a header mismatch", async () => { + const report = await parse(elfUnwindFixture()); expect(report.format).toBe("elf64"); expect(report.byte_order).toBe("little"); expect(report.machine).toBe(62); @@ -285,8 +270,8 @@ it("recovers two ELF64 ranges, an indirect personality, and a header mismatch", ); }); -it("agrees with a header whose search table names the same FDE starts", () => { - const report = parse(elfUnwindFixture({ header: "match" })); +it("agrees with a header whose search table names the same FDE starts", async () => { + const report = await parse(elfUnwindFixture({ header: "match" })); expect(report.coverage.eh_frame_hdr_agreement).toBe("agree"); expect(report.coverage.status).toBe("complete"); expect(report.eh_frame_hdr).toMatchObject({ @@ -306,8 +291,8 @@ it.each([ ["elf64", 64, true, 62], ] as const)( "decodes %s %s-endian unwind tables", - (_label, bits, little, machine) => { - const report = parse( + async (_label, bits, little, machine) => { + const report = await parse( elfUnwindFixture({ bits, little, machine, header: "match" }), ); expect(report.format).toBe(bits === 64 ? "elf64" : "elf32"); @@ -324,17 +309,19 @@ it.each([ }, ); -it("reads a version 3 return-address register and a dynsym name", () => { - const version = parse(elfUnwindFixture({ version3: true, header: "match" })); +it("reads a version 3 return-address register and a dynsym name", async () => { + const version = await parse( + elfUnwindFixture({ version3: true, header: "match" }), + ); expect(version.cies[0]).toMatchObject({ version: 3, return_address_register: 128, }); - const dynsym = parse( + const dynsym = await parse( elfUnwindFixture({ symbolSource: "dynsym", header: "match" }), ); expect(dynsym.ranges[0]?.symbol?.text).toBe("alpha"); - const unnamed = parse( + const unnamed = await parse( elfUnwindFixture({ symbolSource: "none", header: "match" }), ); expect(unnamed.coverage).toMatchObject({ @@ -344,8 +331,8 @@ it("reads a version 3 return-address register and a dynsym name", () => { }); }); -it("keeps earlier FDEs when a later record is truncated", () => { - const report = parse(elfUnwindFixture({ truncate: true })); +it("keeps earlier FDEs when a later record is truncated", async () => { + const report = await parse(elfUnwindFixture({ truncate: true })); expect(report.eh_frame.status).toBe("incomplete"); expect(report.coverage.status).toBe("partial"); expect(report.ranges.map((range) => range.start)).toEqual([ @@ -358,8 +345,8 @@ it("keeps earlier FDEs when a later record is truncated", () => { expect(report.eh_frame.status).not.toBe("absent"); }); -it("reports trailing bytes after the terminator as incomplete coverage", () => { - const report = parse( +it("reports trailing bytes after the terminator as incomplete coverage", async () => { + const report = await parse( elfUnwindFixture({ trailingByte: true, header: "match" }), ); expect(report.ranges).toHaveLength(2); @@ -374,7 +361,7 @@ it("rejects a short positional read of an admitted frame range", async () => { const { offset: frameOffset } = findSection(bytes, ".eh_frame"); let frameReads = 0; await expect( - parseFromSource(bytes, {}, async (offset, length) => { + parse(bytes, {}, async (offset, length) => { if (offset === frameOffset) { frameReads++; return Buffer.alloc(0); @@ -383,21 +370,21 @@ it("rejects a short positional read of an admitted frame range", async () => { }), ).rejects.toMatchObject({ reason: "format", - message: "ELF unwind range ended before its admitted file extent.", + message: expect.stringMatching(/unwind range.*admitted file extent/iu), }); expect(frameReads).toBe(1); }); -it("reports a missing header without dropping decoded ranges", () => { - const report = parse(elfUnwindFixture({ header: "absent" })); +it("reports a missing header without dropping decoded ranges", async () => { + const report = await parse(elfUnwindFixture({ header: "absent" })); expect(report.ranges).toHaveLength(2); expect(report.eh_frame_hdr.status).toBe("absent"); expect(report.coverage.eh_frame_hdr_agreement).toBe("unavailable"); expect(report.coverage.status).toBe("complete"); }); -it("reads one agreeing range from PT_GNU_EH_FRAME when section headers are absent", () => { - const report = parse(elfUnwindSectionlessFixture()); +it("reads one agreeing range from PT_GNU_EH_FRAME when section headers are absent", async () => { + const report = await parse(elfUnwindSectionlessFixture()); expect(report.eh_frame).toMatchObject({ status: "present", source: "program", @@ -420,9 +407,9 @@ it("reads one agreeing range from PT_GNU_EH_FRAME when section headers are absen expect(report.limitations).toContain(".debug_frame presence is unknown."); }); -it("bounds a header-located .eh_frame without a terminator by its search table", () => { +it("bounds a header-located .eh_frame without a terminator by its search table", async () => { const bytes = elfUnwindSectionlessFixture({ unterminated: "decodable" }); - const report = parse(bytes); + const report = await parse(bytes); expect(report.eh_frame).toMatchObject({ status: "present", source: "program", @@ -444,8 +431,8 @@ it("bounds a header-located .eh_frame without a terminator by its search table", ); }); -it("stops a header-located .eh_frame without a search table at its first undecodable record", () => { - const report = parse( +it("stops a header-located .eh_frame without a search table at its first undecodable record", async () => { + const report = await parse( elfUnwindSectionlessFixture({ unterminated: "undecodable", emptyTable: true, @@ -468,19 +455,19 @@ it("stops a header-located .eh_frame without a search table at its first undecod ); }); -it("rejects a program-header table outside the file before decoding unwind tables", () => { +it("rejects a program-header table outside the file before decoding unwind tables", async () => { const bytes = elfUnwindSectionlessFixture(); bytes.writeBigUInt64LE(BigInt(bytes.length), 32); - expect(() => parse(bytes)).toThrow( + await expect(parse(bytes)).rejects.toThrow( expect.objectContaining({ kind: "malformed" }), ); }); -it("reports .eh_frame as unknown when the header cannot locate it", () => { +it("reports .eh_frame as unknown when the header cannot locate it", async () => { const bytes = elfUnwindSectionlessFixture(); const headerOffset = Number(bytes.readBigUInt64LE(64 + 56 + 8)); bytes[headerOffset + 1] = 0x0f; - const report = parse(bytes); + const report = await parse(bytes); expect(report.eh_frame).toEqual({ status: "unknown", source: null, @@ -499,8 +486,8 @@ it("reports .eh_frame as unknown when the header cannot locate it", () => { ); }); -it("reports known absence when no unwind section or segment is declared", () => { - const report = parse(elfUnwindAbsentFixture()); +it("reports known absence when no unwind section or segment is declared", async () => { + const report = await parse(elfUnwindAbsentFixture()); expect(report.eh_frame.status).toBe("absent"); expect(report.debug_frame.status).toBe("absent"); expect(report.coverage).toMatchObject({ @@ -512,8 +499,8 @@ it("reports known absence when no unwind section or segment is declared", () => expect(report.ranges).toEqual([]); }); -it("pages ranges without changing coverage totals", () => { - const report = parse(elfUnwindFixture(), { offset: 1, limit: 1 }); +it("pages ranges without changing coverage totals", async () => { + const report = await parse(elfUnwindFixture(), { offset: 1, limit: 1 }); expect(report.ranges.map((range) => range.index)).toEqual([1]); expect(report.page).toEqual({ offset: 1, @@ -522,7 +509,7 @@ it("pages ranges without changing coverage totals", () => { total: 2, next_offset: null, }); - const past = parse(elfUnwindFixture(), { offset: 9, limit: 1 }); + const past = await parse(elfUnwindFixture(), { offset: 9, limit: 1 }); expect(past.ranges).toEqual([]); expect(past.page).toMatchObject({ offset: 9, @@ -532,8 +519,8 @@ it("pages ranges without changing coverage totals", () => { }); }); -it("returns only the CIEs a page's ranges name, however many the image has", () => { - const report = parse(elfUnwindFixture({ extraCies: 500 }), { +it("returns only the CIEs a page's ranges name, however many the image has", async () => { + const report = await parse(elfUnwindFixture({ extraCies: 500 }), { offset: 1, limit: 1, }); @@ -545,7 +532,7 @@ it("returns only the CIEs a page's ranges name, however many the image has", () total_cies: 502, examined_records: 504, }); - const first = parse(elfUnwindFixture({ extraCies: 500 }), { limit: 1 }); + const first = await parse(elfUnwindFixture({ extraCies: 500 }), { limit: 1 }); expect(first.cies.map((cie) => cie.index)).toEqual([0]); expect(first.coverage.total_cies).toBe(502); }); @@ -555,23 +542,9 @@ it.each([ { field: "FDE", maximum: 2, suffixNames: 1 }, ])( "fails closed when the $field budget is exhausted", - ({ field, maximum, suffixNames }) => { + async ({ field, maximum, suffixNames }) => { const bytes = elfUnwindFixture({ suffixNames }); - const input = inspectElfUnwindRangesInputSchema.parse({ - path: "/artifacts/unwind.elf", - max_ranges: maximum, - }); - expect(() => - parseElfUnwindRanges( - bytes, - { - path: input.path, - sha256: "a".repeat(64), - bytes: bytes.length, - }, - input, - ), - ).toThrow( + await expect(parse(bytes, { max_ranges: maximum })).rejects.toThrow( expect.objectContaining({ failure: expect.objectContaining({ kind: "limit", @@ -585,23 +558,16 @@ it.each([ }, ); -it("stops when the caller has already cancelled", () => { - const bytes = elfUnwindFixture(); - const input = inspectElfUnwindRangesInputSchema.parse({ - path: "/artifacts/unwind.elf", +it("preserves caller cancellation without reading a header", async () => { + const reason = Symbol("stop before unwind inspection"); + const read = vi.fn(async () => Buffer.alloc(0)); + await expect( + parse(elfUnwindFixture(), {}, read, AbortSignal.abort(reason)), + ).rejects.toMatchObject({ + reason: "cancelled", + cause: reason, }); - expect(() => - parseElfUnwindRanges( - bytes, - { - path: input.path, - sha256: "a".repeat(64), - bytes: bytes.length, - }, - input, - AbortSignal.abort(), - ), - ).toThrow(ArtifactReaderFailure); + expect(read).not.toHaveBeenCalled(); }); const findSection = (bytes: Buffer, name: string) => { @@ -624,9 +590,9 @@ const findSection = (bytes: Buffer, name: string) => { it.each(["compressed-symbols", "compressed-names", "non-string-names"])( "retains unwind ranges without interpreting %s as ordinary symbol storage", - (storage) => { + async (storage) => { const bytes = elfUnwindFixture({ header: "match" }); - expect(parse(bytes).ranges[0]?.symbol?.text).toBe("alpha"); + expect((await parse(bytes)).ranges[0]?.symbol?.text).toBe("alpha"); const section = findSection( bytes, storage === "compressed-symbols" ? ".symtab" : ".strtab", @@ -635,7 +601,7 @@ it.each(["compressed-symbols", "compressed-names", "non-string-names"])( bytes.writeUInt32LE(1, section.headerOffset + 4); else bytes.writeBigUInt64LE(0x800n, section.headerOffset + 8); - const report = parse(bytes); + const report = await parse(bytes); expect(report.ranges.map(({ start }) => start)).toEqual([ address(UNWIND_FN_A), address(UNWIND_FN_B), @@ -649,8 +615,8 @@ it.each(["compressed-symbols", "compressed-names", "non-string-names"])( }, ); -it("keeps .eh_frame ranges when .debug_frame is SHT_NOBITS", () => { - const report = parse( +it("keeps .eh_frame ranges when .debug_frame is SHT_NOBITS", async () => { + const report = await parse( elfUnwindFixture({ header: "match", backing: { section: ".debug_frame", kind: "nobits" }, @@ -671,8 +637,8 @@ it("keeps .eh_frame ranges when .debug_frame is SHT_NOBITS", () => { ); }); -it("reports a .debug_frame past the end of file without a file range", () => { - const report = parse( +it("reports a .debug_frame past the end of file without a file range", async () => { + const report = await parse( elfUnwindFixture({ header: "match", backing: { section: ".debug_frame", kind: "past-eof" }, @@ -683,12 +649,12 @@ it("reports a .debug_frame past the end of file without a file range", () => { expect(report.coverage.status).toBe("partial"); }); -it("decodes the in-file prefix of an .eh_frame that runs past the end of file", () => { +it("decodes the in-file prefix of an .eh_frame that runs past the end of file", async () => { const bytes = elfUnwindFixture({ header: "match", backing: { section: ".eh_frame", kind: "past-eof" }, }); - const report = parse(bytes); + const report = await parse(bytes); const { offset } = findSection(bytes, ".eh_frame"); expect(report.eh_frame).toEqual({ status: "incomplete", @@ -709,20 +675,29 @@ it("decodes the in-file prefix of an .eh_frame that runs past the end of file", it.each([ [".eh_frame", ".eh_frame is declared but not file-backed."], [".eh_frame_hdr", ".eh_frame_hdr is declared but not file-backed."], -] as const)("reports a SHT_NOBITS %s without a file range", (section, text) => { - const report = parse( - elfUnwindFixture({ header: "match", backing: { section, kind: "nobits" } }), - ); - const table = section === ".eh_frame" ? report.eh_frame : report.eh_frame_hdr; - expect(table.status).toBe("incomplete"); - expect(table.location).toBeNull(); - expect(report.coverage.status).toBe("partial"); - expect(report.limitations).toContain(text); - if (section === ".eh_frame_hdr") expect(report.ranges).toHaveLength(2); -}); +] as const)( + "reports a SHT_NOBITS %s without a file range", + async (section, text) => { + const report = await parse( + elfUnwindFixture({ + header: "match", + backing: { section, kind: "nobits" }, + }), + ); + const table = + section === ".eh_frame" ? report.eh_frame : report.eh_frame_hdr; + expect(table.status).toBe("incomplete"); + expect(table.location).toBeNull(); + expect(report.coverage.status).toBe("partial"); + expect(report.limitations).toContain(text); + if (section === ".eh_frame_hdr") expect(report.ranges).toHaveLength(2); + }, +); -it("resolves .eh_frame datarel pointers against the GOT, not .eh_frame_hdr", () => { - const report = parse(elfUnwindFixture({ header: "match", datarel: "got" })); +it("resolves .eh_frame datarel pointers against the GOT, not .eh_frame_hdr", async () => { + const report = await parse( + elfUnwindFixture({ header: "match", datarel: "got" }), + ); expect(report.cies[0]?.pointer_encoding).toEqual({ raw: 0x3b, name: "datarel|sdata4", @@ -741,18 +716,20 @@ it("resolves .eh_frame datarel pointers against the GOT, not .eh_frame_hdr", () expect(UNWIND_GOT_VA).not.toBe(UNWIND_HDR_VA); }); -it("does not use a non-allocated GOT section as the data-relative base", () => { +it("does not use a non-allocated GOT section as the data-relative base", async () => { const bytes = elfUnwindFixture({ header: "match", datarel: "got" }); const { headerOffset } = findSection(bytes, ".got.plt"); bytes.writeBigUInt64LE(1n, headerOffset + 8); bytes.writeBigUInt64LE(0x500000n, headerOffset + 16); - const report = parse(bytes); + const report = await parse(bytes); expect(report.coverage.status).toBe("partial"); expect(report.ranges.map((range) => range.start)).not.toContain("0x4fc100"); }); -it("leaves .eh_frame datarel pointers unresolved when no GOT is declared", () => { - const report = parse(elfUnwindFixture({ header: "match", datarel: "none" })); +it("leaves .eh_frame datarel pointers unresolved when no GOT is declared", async () => { + const report = await parse( + elfUnwindFixture({ header: "match", datarel: "none" }), + ); expect(report.ranges.map((range) => range.start)).toEqual([ address(UNWIND_FN_B), ]); @@ -766,8 +743,10 @@ it("leaves .eh_frame datarel pointers unresolved when no GOT is declared", () => ); }); -it("caps symbol lookup across .symtab and .dynsym together", () => { - const report = parse(elfUnwindFixture({ header: "match", symbolCap: true })); +it("caps symbol lookup across .symtab and .dynsym together", async () => { + const report = await parse( + elfUnwindFixture({ header: "match", symbolCap: true }), + ); expect(report.ranges).toHaveLength(2); expect(report.coverage).toMatchObject({ status: "partial", @@ -778,11 +757,11 @@ it("caps symbol lookup across .symtab and .dynsym together", () => { ); }); -it("keeps the symbol scan allowance for later tables when an earlier table has no string table", () => { +it("keeps the symbol scan allowance for later tables when an earlier table has no string table", async () => { const bytes = elfUnwindFixture({ header: "match", symbolCap: true }); const section = findSection(bytes, ".symtab"); bytes.writeUInt32LE(0, section.headerOffset + 40); - const report = parse(bytes); + const report = await parse(bytes); expect(report.ranges[0]?.symbol?.text).toBe("alpha"); expect(report.coverage.status).toBe("partial"); expect(report.limitations).not.toContain( @@ -792,8 +771,10 @@ it("keeps the symbol scan allowance for later tables when an earlier table has n it.each([".eh_frame", ".eh_frame_hdr"] as const)( "reports coverage incomplete when a duplicate %s is ignored", - (duplicate) => { - const report = parse(elfUnwindFixture({ header: "match", duplicate })); + async (duplicate) => { + const report = await parse( + elfUnwindFixture({ header: "match", duplicate }), + ); expect(report.ranges).toHaveLength(2); expect(report.coverage.status).toBe("partial"); expect(report.limitations).toContain( @@ -804,8 +785,8 @@ it.each([".eh_frame", ".eh_frame_hdr"] as const)( it.each(["duplicate-row", "shared-fde"] as const)( "reports header disagreement for a %s search table", - (header) => { - const report = parse(elfUnwindFixture({ header })); + async (header) => { + const report = await parse(elfUnwindFixture({ header })); expect(report.ranges).toHaveLength(2); expect(report.coverage.eh_frame_hdr_agreement).toBe("disagree"); expect(report.limitations).toContain( @@ -819,8 +800,8 @@ it.each([ ["elf32 big-endian", 32, false], ] as const)( "keeps a 4-byte CIE ID after a 64-bit .eh_frame length (%s)", - (_label, bits, little) => { - const report = parse( + async (_label, bits, little) => { + const report = await parse( elfUnwindFixture({ bits, little, @@ -849,8 +830,8 @@ it.each([ ["elf32 big-endian", 32, false], ] as const)( "resolves .eh_frame datarel pointers against DT_PLTGOT (%s)", - (_label, bits, little) => { - const report = parse( + async (_label, bits, little) => { + const report = await parse( elfUnwindFixture({ bits, little, header: "match", datarel: "dynamic" }), ); expect(report.ranges.map((range) => range.start)).toEqual([ @@ -872,8 +853,8 @@ it.each([ ["elf32 big-endian", 32, false], ] as const)( "reads DT_PLTGOT through the single PT_DYNAMIC header (%s)", - (_label, bits, little) => { - const report = parse( + async (_label, bits, little) => { + const report = await parse( elfUnwindFixture({ bits, little, @@ -899,8 +880,8 @@ it.each([ ["repeated SHT_DYNAMIC sections", { duplicate: ".dynamic" }], ] as const)( "keeps the GOT unknown when %s leave the dynamic table ambiguous", - (_label, options) => { - const report = parse( + async (_label, options) => { + const report = await parse( elfUnwindFixture({ header: "match", datarel: "dynamic", ...options }), ); expect(report.ranges.map((range) => range.start)).not.toContain( @@ -911,8 +892,8 @@ it.each([ }, ); -it("keeps the GOT unknown when repeated allocated .got.plt sections disagree", () => { - const report = parse( +it("keeps the GOT unknown when repeated allocated .got.plt sections disagree", async () => { + const report = await parse( elfUnwindFixture({ header: "match", datarel: "got", @@ -926,13 +907,13 @@ it("keeps the GOT unknown when repeated allocated .got.plt sections disagree", ( expect(report.limitations).toContain(GOT_UNKNOWN); }); -it("keeps the GOT unknown when compressed dynamic storage resembles decoded entries", () => { +it("keeps the GOT unknown when compressed dynamic storage resembles decoded entries", async () => { const bytes = elfUnwindFixture({ header: "match", datarel: "dynamic" }); const section = findSection(bytes, ".dynamic"); // The payload still resembles ordinary DT_PLTGOT/DT_NULL entries, but the // declaration makes it opaque compressed storage, not a decoded table. bytes.writeBigUInt64LE(0x800n, section.headerOffset + 8); - const report = parse(bytes); + const report = await parse(bytes); expect(report.ranges.map((range) => range.start)).not.toContain( address(UNWIND_FN_A), ); @@ -942,8 +923,8 @@ it("keeps the GOT unknown when compressed dynamic storage resembles decoded entr ); }); -it("caps .eh_frame_hdr rows at the max_ranges budget and marks the comparison incomplete", () => { - const report = parse(elfUnwindFixture({ header: "duplicate-row" }), { +it("caps .eh_frame_hdr rows at the max_ranges budget and marks the comparison incomplete", async () => { + const report = await parse(elfUnwindFixture({ header: "duplicate-row" }), { max_ranges: 2, }); expect(report.ranges).toHaveLength(2); @@ -961,8 +942,8 @@ it("caps .eh_frame_hdr rows at the max_ranges budget and marks the comparison in ); }); -it("bounds an overstated .eh_frame_hdr fde_count by the section bytes", () => { - const report = parse( +it("bounds an overstated .eh_frame_hdr fde_count by the section bytes", async () => { + const report = await parse( elfUnwindFixture({ header: "match", headerCount: 0x7fffffff }), { max_ranges: 65536 }, ); @@ -978,8 +959,8 @@ it("bounds an overstated .eh_frame_hdr fde_count by the section bytes", () => { ); }); -it("reports an .eh_frame_hdr fde_count above 32 bits as undecoded, keeping the FDEs", () => { - const report = parse( +it("reports an .eh_frame_hdr fde_count above 32 bits as undecoded, keeping the FDEs", async () => { + const report = await parse( elfUnwindFixture({ header: "match", headerCount: 0x1_0000_0002n }), ); expect(report.ranges).toHaveLength(2); @@ -994,24 +975,36 @@ it("reports an .eh_frame_hdr fde_count above 32 bits as undecoded, keeping the F ); }); -it("reports a header with an omitted search table as present without failing coverage", () => { - const report = parse( - elfUnwindFixture({ header: "match", headerCount: "omit" }), - ); - expect(report.ranges).toHaveLength(2); - expect(report.eh_frame_hdr).toMatchObject({ - status: "present", - fde_count: null, - table_entries: 0, - fde_count_encoding: { raw: 0xff }, - table_encoding: { raw: 0xff }, - }); - expect(report.coverage.eh_frame_hdr_agreement).toBe("unavailable"); - expect(report.coverage.status).toBe("complete"); +it("keeps a header with an omitted search table complete with or without a count", async () => { + for (const count of [null, 2]) { + const bytes = elfUnwindFixture({ + header: "match", + headerCount: count ?? "omit", + }); + bytes[findSection(bytes, ".eh_frame_hdr").offset + 3] = 0xff; + const report = await parse(bytes); + expect(report.ranges).toHaveLength(2); + expect(report.eh_frame_hdr).toMatchObject({ + status: "present", + fde_count: count, + table_entries: 0, + fde_count_encoding: { raw: count === null ? 0xff : 0x03 }, + table_encoding: { raw: 0xff }, + }); + expect(report.coverage).toMatchObject({ + status: "complete", + eh_frame_hdr_agreement: "unavailable", + }); + expect(report.limitations).not.toContainEqual( + expect.stringMatching(/search table.*indirect/iu), + ); + } }); -it("reports a raw zero LSDA pointer as no LSDA, not the field address", () => { - const report = parse(elfUnwindFixture({ header: "match", zeroLsda: true })); +it("reports a raw zero LSDA pointer as no LSDA, not the field address", async () => { + const report = await parse( + elfUnwindFixture({ header: "match", zeroLsda: true }), + ); expect(report.cies[1]?.lsda_encoding).toEqual({ raw: 0x1b, name: "pcrel|sdata4", @@ -1023,8 +1016,8 @@ it("reports a raw zero LSDA pointer as no LSDA, not the field address", () => { expect(report.coverage.status).toBe("complete"); }); -it("skips an FDE with a raw zero initial location as a discarded entry", () => { - const report = parse( +it("skips an FDE with a raw zero initial location as a discarded entry", async () => { + const report = await parse( elfUnwindFixture({ header: "match", discardedFde: true }), ); expect(report.ranges.map((range) => range.start)).toEqual([ @@ -1042,8 +1035,10 @@ it("skips an FDE with a raw zero initial location as a discarded entry", () => { ); }); -it("does not claim PT_GNU_EH_FRAME was ignored when it supplied the header", () => { - const report = parse(elfUnwindSectionlessFixture({ sections: ".eh_frame" })); +it("does not claim PT_GNU_EH_FRAME was ignored when it supplied the header", async () => { + const report = await parse( + elfUnwindSectionlessFixture({ sections: ".eh_frame" }), + ); expect(report.eh_frame).toMatchObject({ status: "present", source: "section", @@ -1058,8 +1053,8 @@ it("does not claim PT_GNU_EH_FRAME was ignored when it supplied the header", () ); }); -it("notes an ignored PT_GNU_EH_FRAME only when a .eh_frame_hdr section overrides different bytes", () => { - const report = parse( +it("notes an ignored PT_GNU_EH_FRAME only when a .eh_frame_hdr section overrides different bytes", async () => { + const report = await parse( elfUnwindSectionlessFixture({ sections: ".eh_frame_hdr" }), ); expect(report.eh_frame_hdr.status).toBe("present"); @@ -1069,20 +1064,20 @@ it("notes an ignored PT_GNU_EH_FRAME only when a .eh_frame_hdr section overrides ); }); -it("uses the load mapping for a non-allocated unwind header", () => { +it("uses the load mapping for a non-allocated unwind header", async () => { const bytes = elfUnwindSectionlessFixture({ sections: ".eh_frame_hdr" }); const { headerOffset } = findSection(bytes, ".eh_frame_hdr"); bytes.writeBigUInt64LE(0n, headerOffset + 8); bytes.writeBigUInt64LE(0n, headerOffset + 16); - const report = parse(bytes); + const report = await parse(bytes); expect(report.coverage).toMatchObject({ status: "complete", eh_frame_hdr_agreement: "agree", }); }); -it("translates and places addresses only through SHF_ALLOC sections", () => { - const report = parse( +it("translates and places addresses only through SHF_ALLOC sections", async () => { + const report = await parse( elfUnwindSectionlessFixture({ sections: ".eh_frame", nonAllocOverlay: true, @@ -1104,8 +1099,8 @@ it("translates and places addresses only through SHF_ALLOC sections", () => { }); }); -it("does not decode FDEs against a CIE with an unrecognized augmentation letter", () => { - const report = parse( +it("does not decode FDEs against a CIE with an unrecognized augmentation letter", async () => { + const report = await parse( elfUnwindFixture({ header: "match", unknownAugmentation: true }), ); expect(report.cies.map((cie) => cie.augmentation.text)).toEqual(["zPLRB"]); @@ -1123,8 +1118,8 @@ it("does not decode FDEs against a CIE with an unrecognized augmentation letter" it.each([32, 64] as const)( "wraps an unsigned pcrel ELF%i initial location at the address width", - (bits) => { - const report = parse( + async (bits) => { + const report = await parse( elfUnwindFixture({ bits, header: "match", unsignedPcrel: {} }), ); expect(report.ranges[0]).toMatchObject({ @@ -1135,8 +1130,8 @@ it.each([32, 64] as const)( }, ); -it("wraps an ELF32 FDE end that overflows the address space", () => { - const report = parse( +it("wraps an ELF32 FDE end that overflows the address space", async () => { + const report = await parse( elfUnwindFixture({ bits: 32, header: "match", @@ -1150,14 +1145,14 @@ it("wraps an ELF32 FDE end that overflows the address space", () => { }); }); -it("scans and decodes a symbol name shared by many symbols once, only for FDE starts", () => { +it("scans and decodes a symbol name shared by many symbols once, only for FDE starts", async () => { const bytes = elfUnwindFixture({ header: "match", sharedName: { symbols: 4096, bytes: 60000 }, }); stringTable.observed.length = 0; stringTable.decoded.length = 0; - const report = parse(bytes, { limit: 1 }); + const report = await parse(bytes, { limit: 1 }); expect(report.ranges).toHaveLength(1); expect(report.ranges[0]?.symbol?.text).toBe("n".repeat(60000)); expect(report.coverage).toMatchObject({ @@ -1171,8 +1166,8 @@ it("scans and decodes a symbol name shared by many symbols once, only for FDE st expect(shared(stringTable.decoded)).toBe(1); }); -it("skips an FDE whose initial location is indirect instead of reporting the pointer slot", () => { - const report = parse( +it("skips an FDE whose initial location is indirect instead of reporting the pointer slot", async () => { + const report = await parse( elfUnwindFixture({ header: "match", indirectStart: true }), ); expect(report.ranges.map((range) => range.start)).toEqual([ @@ -1187,10 +1182,10 @@ it("skips an FDE whose initial location is indirect instead of reporting the poi ); }); -it("does not locate .eh_frame through an indirect .eh_frame_hdr eh_frame_ptr", () => { +it("does not locate .eh_frame through an indirect .eh_frame_hdr eh_frame_ptr", async () => { const bytes = elfUnwindSectionlessFixture(); bytes[Number(bytes.readBigUInt64LE(64 + 56 + 8)) + 1] = 0x9b; - const report = parse(bytes); + const report = await parse(bytes); expect(report.eh_frame.status).toBe("unknown"); expect(report.ranges).toEqual([]); expect(report.eh_frame_hdr).toMatchObject({ @@ -1203,8 +1198,8 @@ it("does not locate .eh_frame through an indirect .eh_frame_hdr eh_frame_ptr", ( ); }); -it("stops a header-located .eh_frame without a search table at a CIE with an unrecognized augmentation", () => { - const report = parse( +it("stops a header-located .eh_frame without a search table at a CIE with an unrecognized augmentation", async () => { + const report = await parse( elfUnwindSectionlessFixture({ unterminated: "rejected-cie", emptyTable: true, @@ -1226,8 +1221,8 @@ it("stops a header-located .eh_frame without a search table at a CIE with an unr ); }); -it("reports coverage incomplete when a duplicate PT_GNU_EH_FRAME is ignored", () => { - const report = parse( +it("reports coverage incomplete when a duplicate PT_GNU_EH_FRAME is ignored", async () => { + const report = await parse( elfUnwindSectionlessFixture({ duplicateProgramHeader: true }), ); expect(report.ranges.map((range) => range.start)).toEqual([ @@ -1239,15 +1234,15 @@ it("reports coverage incomplete when a duplicate PT_GNU_EH_FRAME is ignored", () ); }); -it("bounds the symbol-name text decoded for one page by its own allowance", () => { +it("bounds the symbol-name text decoded for one page by its own allowance", async () => { const bytes = elfUnwindFixture({ header: "absent", suffixNames: 300 }); - const page = parse(bytes, { limit: 200 }); + const page = await parse(bytes, { limit: 200 }); expect(page.ranges).toHaveLength(200); expect(page.ranges[2]).toMatchObject({ start: address(UNWIND_SUFFIX_VA), symbol: { text: "s".repeat(65535) }, }); - expect(() => parse(bytes, { limit: 300 })).toThrow( + await expect(parse(bytes, { limit: 300 })).rejects.toThrow( expect.objectContaining({ failure: expect.objectContaining({ kind: "limit", @@ -1260,21 +1255,33 @@ it("bounds the symbol-name text decoded for one page by its own allowance", () = it("reads only the selected symbol name when adjacent ranges share string bytes", async () => { const bytes = elfUnwindFixture({ header: "absent", suffixNames: 2 }); const { offset } = findSection(bytes, ".strtab"); - const reads: number[] = []; + const reads: { offset: number; length: number }[] = []; const page = { offset: 2, limit: 1 }; - const report = await parseFromSource(bytes, page, async (at, length) => { - if (at === offset + 1 || at === offset + 2) reads.push(length); + const report = await parse(bytes, page, async (at, length) => { + if (at === offset + 1 || at === offset + 2) + reads.push({ offset: at, length }); return bytes.subarray(at, at + length); }); - expect(report).toEqual(parse(bytes, page)); - expect(report.ranges[0]?.symbol?.text).toHaveLength(65535); - expect(reads.filter((length) => length < 65536)).toEqual([65535]); + expect(report.ranges).toMatchObject([ + { + index: 2, + start: address(UNWIND_SUFFIX_VA), + size: "4", + symbol: { text: "s".repeat(65535), trust: "untrusted" }, + }, + ]); + expect(reads.filter(({ length }) => length < 65536)).toEqual([ + { offset: offset + 1, length: 65535 }, + ]); }); it("completes a CIE whose fields follow a NUL at the read window boundary", async () => { const bytes = elfUnwindFixture({ header: "match", longAugmentation: 65522 }); - const report = await parseFromSource(bytes); - expect(report).toEqual(parse(bytes)); + const report = await parse(bytes); + expect(report.ranges.map(({ start }) => start)).toEqual([ + address(UNWIND_FN_A), + address(UNWIND_FN_B), + ]); expect(report.coverage).toMatchObject({ status: "complete", total_ranges: 2, @@ -1287,7 +1294,7 @@ it("completes a CIE whose fields follow a NUL at the read window boundary", asyn }); }); -it("reads a long CIE augmentation as one byte slice instead of building it per character", () => { +it("reads a long CIE augmentation as one byte slice instead of building it per character", async () => { const letters = 1 << 20; const bytes = elfUnwindFixture({ header: "match", @@ -1295,7 +1302,7 @@ it("reads a long CIE augmentation as one byte slice instead of building it per c }); const fromCharCode = vi.spyOn(String, "fromCharCode"); try { - const report = parse(bytes, { limit: 1 }); + const report = await parse(bytes, { limit: 1 }); expect(report.cies[0]).toMatchObject({ augmentation: { text: `zRS${"S".repeat(letters)}` }, signal_frame: true, @@ -1307,10 +1314,10 @@ it("reads a long CIE augmentation as one byte slice instead of building it per c } }); -it("does not compare or bound by an .eh_frame_hdr search table with an indirect encoding", () => { +it("does not compare or bound by an .eh_frame_hdr search table with an indirect encoding", async () => { const bytes = elfUnwindFixture({ header: "match" }); bytes[findSection(bytes, ".eh_frame_hdr").offset + 3] = 0xbb; - const report = parse(bytes); + const report = await parse(bytes); expect(report.ranges.map((range) => range.start)).toEqual([ address(UNWIND_FN_A), address(UNWIND_FN_B), @@ -1332,17 +1339,17 @@ it("does not compare or bound by an .eh_frame_hdr search table with an indirect unterminated: "decodable", }); sectionless[Number(sectionless.readBigUInt64LE(64 + 56 + 8)) + 3] = 0xbb; - const unbounded = parse(sectionless); + const unbounded = await parse(sectionless); expect(unbounded.coverage.status).toBe("partial"); expect(unbounded.limitations).not.toContain( "The .eh_frame located only through the .eh_frame_hdr eh_frame_ptr has no section bounds and no terminator after the last FDE its search table names; bytes after that FDE were not decoded.", ); }); -it("leaves a non-zero indirect .eh_frame_hdr FDE count unresolved", () => { +it("leaves a non-zero indirect .eh_frame_hdr FDE count unresolved", async () => { const bytes = elfUnwindFixture({ header: "match" }); bytes[findSection(bytes, ".eh_frame_hdr").offset + 2] = 0x83; - const report = parse(bytes); + const report = await parse(bytes); expect(report.ranges).toHaveLength(2); expect(report.eh_frame_hdr).toMatchObject({ status: "incomplete", @@ -1358,27 +1365,10 @@ it("leaves a non-zero indirect .eh_frame_hdr FDE count unresolved", () => { ); }); -it("reports a header that declares a count but omits its search table as present", () => { - const bytes = elfUnwindFixture({ header: "match" }); - bytes[findSection(bytes, ".eh_frame_hdr").offset + 3] = 0xff; - const report = parse(bytes); - expect(report.eh_frame_hdr).toMatchObject({ - status: "present", - fde_count: 2, - table_entries: 0, - table_encoding: { raw: 0xff }, - }); - expect(report.coverage).toMatchObject({ - status: "complete", - eh_frame_hdr_agreement: "unavailable", - }); - expect(report.limitations).not.toContain( - "The .eh_frame_hdr search table uses an indirect encoding that was not dereferenced; it was not compared with the decoded FDEs.", +it("names a range from an STT_GNU_IFUNC resolver symbol at its exact start", async () => { + const report = await parse( + elfUnwindFixture({ header: "match", ifunc: true }), ); -}); - -it("names a range from an STT_GNU_IFUNC resolver symbol at its exact start", () => { - const report = parse(elfUnwindFixture({ header: "match", ifunc: true })); expect(report.ranges[0]).toMatchObject({ start: address(UNWIND_FN_A), symbol: { text: "alpha" }, @@ -1389,7 +1379,7 @@ it("names a range from an STT_GNU_IFUNC resolver symbol at its exact start", () ); }); -it("names an ARM Thumb range from a function symbol whose value sets bit 0", () => { +it("names an ARM Thumb range from a function symbol whose value sets bit 0", async () => { // AAELF32: a Thumb STT_FUNC value is the code address with bit 0 set, and // an IFUNC resolver in Thumb code is marked the same way. for (const variant of [ @@ -1397,7 +1387,7 @@ it("names an ARM Thumb range from a function symbol whose value sets bit 0", () { symbolSource: "dynsym" }, { ifunc: true }, ] as const) { - const report = parse( + const report = await parse( elfUnwindFixture({ bits: 32, machine: 40, @@ -1412,27 +1402,27 @@ it("names an ARM Thumb range from a function symbol whose value sets bit 0", () }); } // Another machine keeps the value as written, so bit 0 names no FDE start. - const x86 = parse( + const x86 = await parse( elfUnwindFixture({ bits: 32, machine: 3, header: "match", thumb: true }), ); expect(x86.ranges[0]?.symbol?.text).toBe("$t"); }); -it("excludes load mappings whose file size exceeds memory size", () => { +it("excludes load mappings whose file size exceeds memory size", async () => { const bytes = elfUnwindSectionlessFixture(); const phoff = Number(bytes.readBigUInt64LE(32)); bytes.writeBigUInt64LE(0n, phoff + 40); - const report = parse(bytes); + const report = await parse(bytes); expect(report.coverage.status).toBe("partial"); expect(report.limitations).toContain( "An invalid PT_LOAD mapping was excluded from unwind address resolution.", ); }); -it("compares the header pointer with a nonallocated frame's mapped address", () => { +it("compares the header pointer with a nonallocated frame's mapped address", async () => { const bytes = elfUnwindSectionlessFixture({ sections: ".eh_frame_hdr" }); const section = findSection(bytes, ".eh_frame"); bytes.writeBigUInt64LE(0n, section.headerOffset + 8); bytes.writeBigUInt64LE(0n, section.headerOffset + 16); - expect(parse(bytes).coverage.eh_frame_hdr_agreement).toBe("agree"); + expect((await parse(bytes)).coverage.eh_frame_hdr_agreement).toBe("agree"); }); diff --git a/src/native/elf/ElfUnwindTables.ts b/src/native/elf/ElfUnwindTables.ts index a555131d0e..874f44b5d7 100644 --- a/src/native/elf/ElfUnwindTables.ts +++ b/src/native/elf/ElfUnwindTables.ts @@ -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, + directory: ElfSectionDirectory, signal?: AbortSignal, ): Generator { assertArtifactReadActive(signal); @@ -1316,7 +1289,7 @@ const fileRange = (section: NativeSection): FileRange | null => { */ function* resolveGotBase( fileBytes: number, - directory: ReturnType, + 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, + directory: ElfSectionDirectory, wanted: ReadonlySet, signal: AbortSignal | undefined, report: {