diff --git a/src/application/binaryDiagnostics/ElfNotesService.test.ts b/src/application/binaryDiagnostics/ElfNotesService.test.ts index 93b568066d..cd22f20e40 100644 --- a/src/application/binaryDiagnostics/ElfNotesService.test.ts +++ b/src/application/binaryDiagnostics/ElfNotesService.test.ts @@ -4,8 +4,11 @@ import { ElfNotesService } from "./ElfNotesService.js"; import { ELF_NOTES_PROVIDER } from "../../domain/investigationProviders.js"; import { createHash } from "node:crypto"; import { inspectElfNotesInputSchema } from "../../domain/native/elfNotes.js"; -import { elfNoteFixture } from "../../native/elf/ElfNotes.fixture.js"; -import { parseElfNotes } from "../../native/elf/ElfNoteParser.js"; +import { + elfNoteByteSource, + elfNoteFixture, +} from "../../native/elf/ElfNotes.fixture.js"; +import { parseElfNotesFromHandle } from "../../native/elf/ElfNoteParser.js"; import { ok } from "../../domain/result.js"; import { AnalysisCancelledError, @@ -16,14 +19,15 @@ import { const identity = ELF_NOTES_PROVIDER; const path = "/artifacts/app"; const bytes = elfNoteFixture({ ownership: "none" }); -const report = parseElfNotes( - bytes, +const report = await parseElfNotesFromHandle( + elfNoteByteSource(bytes), { path, sha256: createHash("sha256").update(bytes).digest("hex"), bytes: bytes.length, }, inspectElfNotesInputSchema.parse({ path }), + async () => {}, ); it("rejects a relative path and extra arguments before reading", async () => { diff --git a/src/native/elf/ElfNoteCoreProfile.test.ts b/src/native/elf/ElfNoteCoreProfile.test.ts index 86d3cc45f5..2936a0ec8f 100644 --- a/src/native/elf/ElfNoteCoreProfile.test.ts +++ b/src/native/elf/ElfNoteCoreProfile.test.ts @@ -2,26 +2,28 @@ import { createHash } from "node:crypto"; import { expect, it } from "vitest"; import { inspectElfNotesInputSchema } from "../../domain/native/elfNotes.js"; -import { parseElfNotes } from "./ElfNoteParser.js"; +import { parseElfNotesFromHandle } from "./ElfNoteParser.js"; import { coreAuxvNote, coreFileNote, corePrpsinfoNote, coreSiginfoNote, + elfNoteByteSource, elfNoteFixture, gnuBuildIdNote, } from "./ElfNotes.fixture.js"; const input = inspectElfNotesInputSchema.parse({ path: "/fixture.core" }); const parse = (bytes: Buffer) => - parseElfNotes( - bytes, + parseElfNotesFromHandle( + elfNoteByteSource(bytes), { path: input.path, bytes: bytes.length, sha256: createHash("sha256").update(bytes).digest("hex"), }, input, + async () => {}, ); const fixture = () => @@ -44,12 +46,12 @@ it.each([ { name: "unrecognized Linux ABI version", osabi: 3, version: 1 }, ])( "keeps $name CORE payloads uninterpreted in both tables", - ({ osabi, version }) => { + async ({ osabi, version }) => { const bytes = fixture(); - const control = parse(bytes); + const control = await parse(bytes); bytes[7] = osabi; bytes[8] = version; - const report = parse(bytes); + const report = await parse(bytes); const core = report.entries.filter(({ owner }) => owner === "CORE"); expect(core).toHaveLength(8); for (const entry of core) { @@ -72,10 +74,10 @@ it.each([ it.each([0, 3])( "retains Linux CORE decoding for OSABI %i, ABI version zero", - (osabi) => { + async (osabi) => { const bytes = fixture(); bytes[7] = osabi; - const report = parse(bytes); + const report = await parse(bytes); const core = report.entries.filter(({ owner }) => owner === "CORE"); expect(core).toHaveLength(8); expect(core.every(({ core: value }) => value !== null)).toBe(true); @@ -88,10 +90,10 @@ it.each([0, 3])( }, ); -it("does not attach a CORE refusal to a foreign profile containing only GNU notes", () => { +it("does not attach a CORE refusal to a foreign profile containing only GNU notes", async () => { const bytes = elfNoteFixture({ notes: gnuBuildIdNote("0123456789abcdef") }); - const control = parse(bytes); + const control = await parse(bytes); bytes[7] = 6; - expect(parse(bytes).entries).toEqual(control.entries); - expect(parse(bytes).limitations).toEqual(control.limitations); + expect((await parse(bytes)).entries).toEqual(control.entries); + expect((await parse(bytes)).limitations).toEqual(control.limitations); }); diff --git a/src/native/elf/ElfNoteParser.test.ts b/src/native/elf/ElfNoteParser.test.ts index 46828ead85..3377a824a3 100644 --- a/src/native/elf/ElfNoteParser.test.ts +++ b/src/native/elf/ElfNoteParser.test.ts @@ -1,4 +1,3 @@ -import { ParseFailureError } from "../../domain/parseFailure.js"; import { createHash } from "node:crypto"; import { readFileSync } from "node:fs"; import { expect, it } from "vitest"; @@ -21,6 +20,7 @@ import { coreSiginfoNote, elf32ProcessIdentityFixture, elfNote, + elfNoteByteSource, elfNoteFixture, elfNoteTablePastEnd, elfExtendedSectionCountFixture, @@ -30,7 +30,6 @@ import { withElf64DirectSectionCount, } from "./ElfNotes.fixture.js"; import { - parseElfNotes, parseElfNotesFromHandle, readElfBuildIds, readElfBuildIdsFromHandle, @@ -41,16 +40,25 @@ const defaults = inspectElfNotesInputSchema.parse({ }); const parse = (bytes: Buffer, input: typeof defaults = defaults) => - parseElfNotes( - bytes, + parseElfNotesFromHandle( + elfNoteByteSource(bytes), { path: input.path, sha256: createHash("sha256").update(bytes).digest("hex"), bytes: bytes.length, }, input, + async () => {}, ); +const noteTextLimit = (maximum: number) => ({ + failure: { + kind: "limit", + format: "elf-notes", + limit: { name: "note_text_bytes", maximum }, + }, +}); + const elf64 = ( fields: { readonly phoff?: number; @@ -80,14 +88,16 @@ const elf64 = ( return tail === undefined ? header : Buffer.concat([header, tail]); }; -it("retains GNU build ID and ABI from program or section notes", () => { +it("retains GNU build ID and ABI from program or section notes", async () => { for (const [bits, little, ownership, machine] of [ [32, true, "program", 3], [32, false, "program", 20], [64, true, "program", 62], [64, true, "section", 62], ] as const) { - const report = parse(elfNoteFixture({ bits, little, ownership, machine })); + const report = await parse( + elfNoteFixture({ bits, little, ownership, machine }), + ); expect(report).toMatchObject({ format: bits === 64 ? "elf64" : "elf32", byte_order: little ? "little" : "big", @@ -129,11 +139,11 @@ it.each([ [64, false], ] as const)( "refuses compressed ELF%i %s-endian note storage without parsing its compression header", - (bits, little) => { + async (bits, little) => { for (const ownership of ["section", "both"] as const) { - expect(() => + await expect( parse(compressedElfNoteSectionFixture(bits, little, ownership)), - ).toThrow( + ).rejects.toThrow( expect.objectContaining({ kind: "unsupported", message: expect.stringMatching(/compressed.*note/iu), @@ -143,7 +153,7 @@ it.each([ }, ); -it("still checks an independent PT_NOTE beside an exact compressed-note alias", () => { +it("still checks an independent PT_NOTE beside an exact compressed-note alias", async () => { const original = compressedElfNoteSectionFixture(64, true, "both"); const alias = original.subarray(64, 120); const withDisjointNotes = (notes: Buffer): Buffer => { @@ -157,7 +167,7 @@ it("still checks an independent PT_NOTE beside an exact compressed-note alias", image.writeUInt16LE(2, 56); return image; }; - expect(() => parse(withDisjointNotes(Buffer.from([1])))).toThrow( + await expect(parse(withDisjointNotes(Buffer.from([1])))).rejects.toThrow( expect.objectContaining({ reason: "format", message: "ELF note table has trailing non-zero bytes.", @@ -166,38 +176,38 @@ it("still checks an independent PT_NOTE beside an exact compressed-note alias", const framed = withDisjointNotes( Buffer.concat([gnuBuildIdNote(), gnuAbiTagNote()]), ); - expect(() => parse(framed, { ...defaults, max_entries: 1 })).toThrow( + await expect(parse(framed, { ...defaults, max_entries: 1 })).rejects.toThrow( expect.objectContaining({ failure: expect.objectContaining({ limit: { name: "max_entries", maximum: 1, required: 2 }, }), }), ); - expect(() => parse(framed)).toThrow( + await expect(parse(framed)).rejects.toThrow( expect.objectContaining({ kind: "unsupported" }), ); const programs = Number(framed.readBigUInt64LE(32)); framed.writeBigUInt64LE(BigInt(framed.length), programs + 32); - expect(() => parse(framed)).toThrow(/leaves the artifact/u); + await expect(parse(framed)).rejects.toThrow(/leaves the artifact/u); }); -it("does not allocate the declared decoded note size, but still validates stored ranges", () => { +it("does not allocate the declared decoded note size, but still validates stored ranges", async () => { const bytes = compressedElfNoteSectionFixture(); const noteSection = 64 + 64; const storedOffset = Number(bytes.readBigUInt64LE(noteSection + 24)); bytes.writeBigUInt64LE(0xffffffffffffffffn, storedOffset + 8); - expect(() => parse(bytes)).toThrow( + await expect(parse(bytes)).rejects.toThrow( expect.objectContaining({ kind: "unsupported" }), ); bytes.writeBigUInt64LE(0n, noteSection + 32); - expect(() => parse(bytes)).toThrow( + await expect(parse(bytes)).rejects.toThrow( expect.objectContaining({ kind: "unsupported" }), ); bytes.writeBigUInt64LE(BigInt(bytes.length), noteSection + 32); - expect(() => parse(bytes)).toThrow(ArtifactReaderFailure); + await expect(parse(bytes)).rejects.toThrow(ArtifactReaderFailure); }); -it("does not decode an earlier raw section overlapping compressed storage", () => { +it("does not decode an earlier raw section overlapping compressed storage", async () => { const bytes = withElf64DirectSectionCount( compressedElfNoteSectionFixture(), 4, @@ -206,16 +216,16 @@ it("does not decode an earlier raw section overlapping compressed storage", () = bytes.copy(bytes, alias + 128, alias, alias + 64); bytes.writeBigUInt64LE(0n, alias + 8); bytes.writeBigUInt64LE(bytes.readBigUInt64LE(alias + 24) + 4n, alias + 24); - expect(() => parse(bytes)).toThrow( + await expect(parse(bytes)).rejects.toThrow( expect.objectContaining({ kind: "unsupported" }), ); }); -it("preserves unknown ELF64 section flag bits without treating them as file sizes", () => { +it("preserves unknown ELF64 section flag bits without treating them as file sizes", async () => { const bytes = elfNoteFixture({ ownership: "section" }); bytes.writeBigUInt64LE(0x8000000000000000n, 64 + 64 + 8); bytes.writeBigUInt64LE(0x8000000000000000n, 64 + 2 * 64 + 8); - expect(parse(bytes).entries[0]?.gnu?.build_id_hex).toBe( + expect((await parse(bytes)).entries[0]?.gnu?.build_id_hex).toBe( "0123456789abcdef0123456789abcdef01234567", ); }); @@ -231,8 +241,8 @@ it.each([ [64, false, 2], ] as const)( "decompresses an ELF%i little-endian=%s section-name table using algorithm %i", - (bits, little, compressionType) => { - const report = parse( + async (bits, little, compressionType) => { + const report = await parse( compressedElfNoteFixture(bits, little, compressionType), ); expect(report.tables).toEqual([ @@ -246,18 +256,18 @@ it.each([ it.each([1, 2])( "admits a decoded section name larger than algorithm %i storage", - (compressionType) => { + async (compressionType) => { const name = "n".repeat(512); const bytes = compressedElfNoteFixture(64, true, compressionType, name); expect(bytes.length).toBeLessThan(name.length); - const report = parse(bytes); + const report = await parse(bytes); expect(report.tables[0]?.name).toBe(name); }, ); it.each([1, 2])( "rejects malformed algorithm %i section-name storage", - (compressionType) => { + async (compressionType) => { for (const damage of ["corrupt", "truncated", "too-large", "too-small"]) { const bytes = compressedElfNoteFixture(64, true, compressionType); const sectionOffset = Number(bytes.readBigUInt64LE(40)); @@ -275,7 +285,7 @@ it.each([1, 2])( (damage === "too-large" ? 1n : -1n), namesOffset + 8, ); - expect(() => parse(bytes), damage).toThrow( + await expect(parse(bytes), damage).rejects.toThrow( /compressed section-name table/u, ); } @@ -302,8 +312,8 @@ it("refuses a Zstandard frame requiring more than the decoder window allowance", }); }); -it("keeps note facts but marks section names unavailable for unknown compression", () => { - const report = parse(compressedElfNoteFixture(64, true, 3)); +it("keeps note facts but marks section names unavailable for unknown compression", async () => { + const report = await parse(compressedElfNoteFixture(64, true, 3)); expect(report.tables[0]?.name).toBeNull(); expect(report.entries[0]?.gnu?.build_id_hex).toBe( "0123456789abcdef0123456789abcdef01234567", @@ -317,7 +327,7 @@ it("keeps note facts but marks section names unavailable for unknown compression it.each([1, 2])( "admits algorithm %i section names when max_file_bytes includes expansion", - (compressionType) => { + async (compressionType) => { const bytes = compressedElfNoteFixture(64, true, compressionType); const sectionOffset = Number(bytes.readBigUInt64LE(40)); const nameHeader = sectionOffset + bytes.readUInt16LE(62) * 64; @@ -330,7 +340,7 @@ it.each([1, 2])( }); let failure: unknown; try { - parse(bytes, input); + await parse(bytes, input); } catch (cause) { failure = cause; } @@ -339,7 +349,7 @@ it.each([1, 2])( selected: bytes.length + 1, }); - const admitted = parse( + const admitted = await parse( bytes, inspectElfNotesInputSchema.parse({ path: defaults.path, @@ -350,8 +360,8 @@ it.each([1, 2])( }, ); -it("reports independent PT_NOTE and SHT_NOTE declarations of the same bytes", () => { - const report = parse(elfNoteFixture({ ownership: "both" })); +it("reports independent PT_NOTE and SHT_NOTE declarations of the same bytes", async () => { + const report = await parse(elfNoteFixture({ ownership: "both" })); expect(report.tables.map((table) => table.kind).sort()).toEqual([ "program", "section", @@ -363,7 +373,7 @@ it("reports independent PT_NOTE and SHT_NOTE declarations of the same bytes", () it.each([true, false])( "decodes FDO package and dlopen notes (little endian: %s)", - (little) => { + async (little) => { const packaging = { type: "rpm", name: "café", @@ -377,7 +387,9 @@ it.each([true, false])( elfNote("FDO", 0x407c0c0a, dlopenBytes, little), elfNote("FDO", 99, Buffer.from("opaque"), little), ]); - const report = parse(elfNoteFixture({ ownership: "both", notes, little })); + const report = await parse( + elfNoteFixture({ ownership: "both", notes, little }), + ); const expected = [ ["fdo_packaging_metadata", { metadata: packaging, issue: null }], ["fdo_dlopen_metadata", { metadata: dlopen, issue: null }], @@ -396,56 +408,69 @@ it.each([true, false])( }, ); -it.each([ - ["missing terminator", 0xcafe1a7e, Buffer.from("{}"), /not NUL-terminated/u], - ["trailing data", 0xcafe1a7e, Buffer.from("{}\0x"), /non-NUL bytes/u], - ["invalid UTF-8", 0xcafe1a7e, Buffer.from([0xc3, 0]), /valid UTF-8/u], - ["invalid JSON", 0xcafe1a7e, Buffer.from("{\0"), /exact JSON/u], - [ - "inexact number", - 0xcafe1a7e, - Buffer.from('{"id":9007199254740993}\0'), - /exact JSON/u, - ], - [ - "duplicate name", - 0xcafe1a7e, - Buffer.from('{"name":"one","name":"two"}\0'), - /repeats/u, - ], - ["package array", 0xcafe1a7e, Buffer.from("[]\0"), /must be a JSON object/u], - ["dlopen scalar", 0x407c0c0a, Buffer.from("[1]\0"), /array of objects/u], - [ - "Evidence nesting", - 0xcafe1a7e, - Buffer.from('{"deep":' + "[".repeat(996) + "0" + "]".repeat(996) + "}\0"), - /nesting allowance/u, - ], -] as const)( - "retains descriptor evidence when FDO metadata has %s", - (_case, type, descriptor, issue) => { - const report = parse( - elfNoteFixture({ - ownership: "program", - notes: elfNote("FDO", type, descriptor), - }), - ); - expect(report.entries[0]).toMatchObject({ +it("retains raw descriptors across malformed FDO metadata in one note table", async () => { + const cases = [ + [ + "missing terminator", + 0xcafe1a7e, + Buffer.from("{}"), + /not NUL-terminated/u, + ], + ["trailing data", 0xcafe1a7e, Buffer.from("{}\0x"), /non-NUL bytes/u], + ["invalid UTF-8", 0xcafe1a7e, Buffer.from([0xc3, 0]), /valid UTF-8/u], + ["invalid JSON", 0xcafe1a7e, Buffer.from("{\0"), /exact JSON/u], + [ + "inexact number", + 0xcafe1a7e, + Buffer.from('{"id":9007199254740993}\0'), + /exact JSON/u, + ], + [ + "duplicate name", + 0xcafe1a7e, + Buffer.from('{"name":"one","name":"two"}\0'), + /repeats/u, + ], + [ + "package array", + 0xcafe1a7e, + Buffer.from("[]\0"), + /must be a JSON object/u, + ], + ["dlopen scalar", 0x407c0c0a, Buffer.from("[1]\0"), /array of objects/u], + [ + "Evidence nesting", + 0xcafe1a7e, + Buffer.from('{"deep":' + "[".repeat(996) + "0" + "]".repeat(996) + "}\0"), + /nesting allowance/u, + ], + ] as const; + const notes = Buffer.concat( + cases.map(([, type, descriptor]) => elfNote("FDO", type, descriptor)), + ); + const report = await parse(elfNoteFixture({ ownership: "program", notes })); + expect(report.coverage).toEqual({ + status: "complete", + examined_entries: cases.length, + examined_tables: 1, + }); + for (const [index, [name, , descriptor, issue]] of cases.entries()) + expect(report.entries[index], name).toMatchObject({ descriptor: { bytes: descriptor.length, sha256: createHash("sha256").update(descriptor).digest("hex"), }, fdo: { metadata: null, issue: expect.stringMatching(issue) }, }); - expect(report.coverage.status).toBe("complete"); - }, -); +}); -it("bounds distinct FDO decoding and each emitted copy independently", () => { +it("bounds distinct FDO decoding and each emitted copy independently", async () => { const metadata = { name: "x".repeat(33_000) }; const descriptor = Buffer.from(JSON.stringify(metadata) + "\0"); const note = elfNote("FDO", 0xcafe1a7e, descriptor); - const repeated = parse(elfNoteFixture({ ownership: "both", notes: note })); + const repeated = await parse( + elfNoteFixture({ ownership: "both", notes: note }), + ); expect(repeated.entries.map(({ fdo }) => fdo)).toEqual([ { metadata, issue: null }, { @@ -456,7 +481,7 @@ it("bounds distinct FDO decoding and each emitted copy independently", () => { expect(repeated.entries[0]?.descriptor).toEqual( repeated.entries[1]?.descriptor, ); - const distinct = parse( + const distinct = await parse( elfNoteFixture({ ownership: "program", notes: Buffer.concat([note, note, gnuBuildIdNote()]), @@ -470,18 +495,20 @@ it("bounds distinct FDO decoding and each emitted copy independently", () => { }, null, ]); - expect(distinct.entries[2]?.gnu?.build_id_hex).toBeTruthy(); + expect(distinct.entries[2]?.gnu?.build_id_hex).toBe( + "0123456789abcdef0123456789abcdef01234567", + ); expect(distinct.coverage.status).toBe("complete"); }); -it("reports missing note tables without claiming notes never existed", () => { - const report = parse(elfNoteFixture({ ownership: "none" })); +it("reports missing note tables without claiming notes never existed", async () => { + const report = await parse(elfNoteFixture({ ownership: "none" })); expect(report.tables).toEqual([]); expect(report.entries).toEqual([]); expect(report.limitations[0]).toMatch(/No PT_NOTE/u); }); -it("diagnoses incomplete note framing at the entry limit", () => { +it("diagnoses incomplete note framing at the entry limit", async () => { const note = elfNote("REA", 9, Buffer.from([1])); const truncatedName = Buffer.from(note); truncatedName.writeUInt32LE(16); @@ -498,7 +525,7 @@ it("diagnoses incomplete note framing at the entry limit", () => { ownership: "program", notes: Buffer.concat([note, truncated]), }); - expect(() => parse(image, input)).toThrow( + await expect(parse(image, input)).rejects.toThrow( expect.objectContaining({ reason: "format", }), @@ -506,10 +533,10 @@ it("diagnoses incomplete note framing at the entry limit", () => { } }); -it("keeps non-GNU owners as unknown types with descriptor hashes", () => { +it("keeps non-GNU owners as unknown types with descriptor hashes", async () => { const payload = Buffer.from("hello"); const notes = elfNote("CORE", 1, payload); - const report = parse(elfNoteFixture({ ownership: "program", notes })); + const report = await parse(elfNoteFixture({ ownership: "program", notes })); expect(report.entries[0]).toMatchObject({ owner: "CORE", type_name: "unknown", @@ -524,12 +551,12 @@ it("keeps non-GNU owners as unknown types with descriptor hashes", () => { it.each(["program", "section"] as const)( "keeps the 8-byte stride and physical descriptor for a GNU property note in a %s table", - (ownership) => { + async (ownership) => { const notes = Buffer.concat([ elfNote("GNU", 3, Buffer.alloc(20, 1), true, 8), gnuPropertyNote(), ]); - const report = parse( + const report = await parse( elfNoteFixture({ ownership, notes, @@ -566,8 +593,10 @@ it.each(["program", "section"] as const)( it.each([0, 1] as const)( "treats declared note alignment %i as 4-byte padding", - (alignment) => { - const report = parse(elfNoteFixture({ ownership: "program", alignment })); + async (alignment) => { + const report = await parse( + elfNoteFixture({ ownership: "program", alignment }), + ); expect(report.tables[0]?.alignment).toBe(4); expect( report.entries.some((entry) => entry.type_name === "gnu_build_id"), @@ -602,8 +631,8 @@ const hostElf = [ it.skipIf(hostElf === null)( "parses a host ELF without requiring it to contain notes", - () => { - const report = parse(hostElf!); + async () => { + const report = await parse(hostElf!); expect(report.coverage.status).toBe("complete"); const buildId = report.entries.find( (entry) => entry.type_name === "gnu_build_id", @@ -653,11 +682,11 @@ it.each([ { offset: 4, name: null, state: "unavailable" }, ])( "retains ELF section-name offset $offset consistently across consumers", - ({ offset, name, state }) => { + async ({ offset, name, state }) => { const bytes = unterminatedNoteSections(1, 4); bytes.set([0x41, 0x42, 0xe9, 0], bytes.length - 4); bytes.writeUInt32LE(offset, 64 + 2 * 64); - const notes = parse(bytes); + const notes = await parse(bytes); const directory = readElfDirectoryFromBuffer(bytes, "elf-directory"); expect(notes.tables[0]?.name).toBe(name); expect(directory.sections[2]).toMatchObject({ name, name_state: state }); @@ -675,11 +704,11 @@ it.each([ it.each([65535, 65536])( "preserves consumer-specific section-name windows for NUL at byte %i", - (terminatorOffset) => { + async (terminatorOffset) => { const bytes = unterminatedNoteSections(1, terminatorOffset + 1); bytes[bytes.length - 1] = 0; const name = "A".repeat(terminatorOffset); - const notes = parse(bytes); + const notes = await parse(bytes); const directory = readElfDirectoryFromBuffer(bytes, "elf-directory"); expect(notes.tables[0]?.name).toBe(name); expect(notes.coverage.status).toBe("complete"); @@ -695,8 +724,8 @@ it.each([65535, 65536])( }, ); -it("bounds repeated scans of an unterminated section name", () => { - const report = parse(unterminatedNoteSections(2, 64)); +it("bounds repeated scans of an unterminated section name", async () => { + const report = await parse(unterminatedNoteSections(2, 64)); expect(report.tables.map(({ name }) => name)).toEqual([null, null]); expect(report.coverage.status).toBe("complete"); const nameWarnings = report.limitations.filter((value) => @@ -706,27 +735,13 @@ it("bounds repeated scans of an unterminated section name", () => { expect(nameWarnings[0]).toMatch( /section names.*incomplete.*not NUL-terminated/iu, ); - let failure: unknown; - try { - parse(unterminatedNoteSections(3, 400)); - } catch (cause) { - failure = cause; - } - expect(failure).toBeInstanceOf(ParseFailureError); - expect(failure).toMatchObject({ - failure: { - kind: "limit", - format: "elf-notes", - limit: { name: "note_text_bytes" }, - }, - }); - expect(failure).toHaveProperty( - "message", - expect.stringMatching(/byte budget/u), + const crowded = unterminatedNoteSections(3, 400); + await expect(parse(crowded)).rejects.toMatchObject( + noteTextLimit(crowded.length), ); }); -it("rejects note text that repeats beyond the shared byte budget", () => { +it("rejects note text that repeats beyond the shared byte budget", async () => { const notes = elfNote("A".repeat(40), 1, Buffer.alloc(0)); const once = elfNoteFixture({ ownership: "program", notes }); const twice = elfNoteFixture({ ownership: "both", notes }); @@ -734,41 +749,40 @@ it("rejects note text that repeats beyond the shared byte budget", () => { path: defaults.path, max_file_bytes: 200, }); - expect(parse(once, input).entries).toHaveLength(1); - expect(() => parse(twice, input)).toThrow(ParseFailureError); - expect(() => parse(twice, input)).toThrow(/byte budget/u); + expect((await parse(once, input)).entries).toHaveLength(1); + await expect(parse(twice, input)).rejects.toMatchObject(noteTextLimit(200)); }); -it("rejects program and section header tables that do not fit", () => { - expect(() => +it("rejects program and section header tables that do not fit", async () => { + await expect( parse(elf64({ phoff: 64, phnum: 1 }, Buffer.from([1, 0, 0, 0]))), - ).toThrow(/leaves the artifact/u); - expect(() => + ).rejects.toThrow(/leaves the artifact/u); + await expect( parse( elf64({ shoff: 64, shnum: 1, shentsize: 64 }, Buffer.from([0, 0, 0, 0])), ), - ).toThrow(/leaves the artifact/u); + ).rejects.toThrow(/leaves the artifact/u); }); -it("rejects an extended section count stored in section zero", () => { - expect(() => parse(elf64({ shoff: 64, shnum: 0 }))).toThrow( +it("rejects an extended section count stored in section zero", async () => { + await expect(parse(elf64({ shoff: 64, shnum: 0 }))).rejects.toThrow( /extended numbering/u, ); }); -it("checks direct section count encoding in notes and the native directory", () => { +it("checks direct section count encoding in notes and the native directory", async () => { const bytes = elfExtendedSectionCountFixture(); const sectionZero = Number(bytes.readBigUInt64LE(40)); bytes.writeBigUInt64LE(0n, sectionZero + 32); bytes.writeUInt16LE(0xfeff, 60); - expect(parse(bytes).tables[0]?.name).toBe(".note.gnu.build-id"); + expect((await parse(bytes)).tables[0]?.name).toBe(".note.gnu.build-id"); expect( readElfDirectoryFromBuffer(bytes, "elf-directory").sections[1]?.name, ).toBe(".note.gnu.build-id"); bytes.writeUInt16LE(0xff00, 60); const reason = /e_shnum uses a reserved direct section count/u; - expect(() => parse(bytes)).toThrow(reason); + await expect(parse(bytes)).rejects.toThrow(reason); expect(() => readElfDirectoryFromBuffer(bytes, "elf-directory")).toThrow( reason, ); @@ -808,23 +822,39 @@ it("checks extended ELF header size using the whole file for note and build-ID r readRange: async (offset: number, length: number) => extended.subarray(offset, offset + length), }; - const report = parse(extended); - expect(report.entries[0]?.gnu?.build_id_hex).toBeDefined(); - expect( - await parseElfNotesFromHandle(source, artifact, defaults, async () => {}), - ).toEqual(report); - const ids = readElfBuildIds(extended, defaults, 64); - expect(ids.sources[0]?.hex).toEqual(report.entries[0]?.gnu?.build_id_hex); - expect( - await readElfBuildIdsFromHandle(source, defaults, 64, async (request) => - decodeElfSectionNameRequest(request), - ), - ).toEqual(ids); + const hex = "0123456789abcdef0123456789abcdef01234567"; + const note = { offset: 184, bytes: 36 }; + const descriptor = { offset: 200, bytes: 20 }; + const report = await parseElfNotesFromHandle( + source, + artifact, + defaults, + async () => {}, + ); + expect(report.entries).toMatchObject([ + { + location: note, + descriptor: { location: descriptor }, + gnu: { build_id_hex: hex }, + }, + ]); + const expected = [ + { hex, note, descriptor, table_kind: "program", table_index: 0 }, + ]; + expect(readElfBuildIds(extended, defaults, 64).sources).toEqual(expected); + const positional = await readElfBuildIdsFromHandle( + source, + defaults, + 64, + async (request) => decodeElfSectionNameRequest(request), + ); + expect(positional.sources).toEqual(expected); }); it("reads aliased note tables once and retains each program header's evidence", async () => { const count = 32; - const table = elfNote("", 0xffff, Buffer.alloc(1024 * 1024)); + const descriptor = Buffer.alloc(8192, 0xa5); + const table = elfNote("", 0xffff, descriptor); const bytes = repeatedNotes(count, table); const reads: Array<{ offset: number; length: number }> = []; const artifact = { @@ -845,21 +875,44 @@ it("reads aliased note tables once and retains each program header's evidence", defaults, async () => {}, ); - expect(report).toEqual(parse(bytes)); - expect(report.entries).toHaveLength(count); + const tableOffset = 64 + 56 * count; + const location = { offset: tableOffset, bytes: table.length }; + const sha256 = createHash("sha256").update(descriptor).digest("hex"); + const indices = Array.from({ length: count }, (_, index) => index); + expect(report.tables).toEqual( + indices.map((index) => ({ + kind: "program", + index, + name: null, + location, + alignment: 4, + })), + ); + expect(report.entries).toMatchObject( + indices.map((index) => ({ + index, + table_kind: "program", + table_index: index, + location, + descriptor: { + location: { offset: tableOffset + 16, bytes: descriptor.length }, + sha256, + }, + })), + ); expect(reads.filter(({ length }) => length === table.length)).toEqual([ - { offset: 64 + 56 * count, length: table.length }, + { offset: tableOffset, length: table.length }, ]); }); -it("reuses one descriptor hash and GNU NUL scan across note headers", () => { +it("reuses one descriptor hash and GNU NUL scan across note headers", async () => { const payload = Buffer.alloc(400); - const hashed = parse(repeatedNotes(6, elfNote("X", 1, payload))); + const hashed = await parse(repeatedNotes(6, elfNote("X", 1, payload))); expect(hashed.entries).toHaveLength(6); expect( new Set(hashed.entries.map((entry) => entry.descriptor.sha256)).size, ).toBe(1); - const gold = parse(repeatedNotes(6, elfNote("GNU", 4, payload))); + const gold = await parse(repeatedNotes(6, elfNote("GNU", 4, payload))); expect(gold.entries).toHaveLength(6); expect(gold.entries.every((entry) => entry.gnu?.gold_version === "")).toBe( true, @@ -868,7 +921,7 @@ it("reuses one descriptor hash and GNU NUL scan across note headers", () => { elfNote("X", 1, payload), elfNote("X", 1, Buffer.alloc(400, 1)), ]); - expect(() => + await expect( parse( elfNoteFixture({ ownership: "program", notes: distinct }), inspectElfNotesInputSchema.parse({ @@ -876,58 +929,47 @@ it("reuses one descriptor hash and GNU NUL scan across note headers", () => { max_file_bytes: payload.length + 160, }), ), - ).toThrow(/byte budget/u); + ).rejects.toThrow(/byte budget/u); }); -it("decodes a GNU gold version by scanning trailing NULs", () => { - const trailing = elfNote("GNU", 4, Buffer.from("gold\0\0x\0", "latin1")); - expect( - parse(elfNoteFixture({ ownership: "program", notes: trailing })).entries[0] - ?.gnu?.gold_version, - ).toBe("gold\0\0x"); - const descriptor = Buffer.concat([Buffer.alloc(131_072), Buffer.from("x")]); - const started = Date.now(); - const report = parse( - elfNoteFixture({ - ownership: "program", - notes: elfNote("GNU", 4, descriptor), - }), - ); - expect(Date.now() - started).toBeLessThan(1_000); - expect(report.entries[0]?.gnu?.gold_version).toBe(`${"\0".repeat(131_072)}x`); +it("preserves interior NULs in GNU gold alongside a bounded trailing scan", async () => { + const interior = `\0`.repeat(131_072) + "x"; + const notes = Buffer.concat([ + elfNote("GNU", 4, Buffer.from("gold\0\0x\0", "latin1")), + elfNote("GNU", 4, Buffer.from(interior, "latin1")), + ]); + const image = elfNoteFixture({ ownership: "program", notes }); + const started = process.cpuUsage(); + const report = await parse(image); + const { user, system } = process.cpuUsage(started); + // The nonzero tail exposes quadratic NUL trimming. Measure CPU to avoid + // charging scheduler delays against the parser on loaded runners. + expect(user + system).toBeLessThan(1_000_000); + expect(report.entries.map(({ gnu }) => gnu?.gold_version)).toEqual([ + "gold\0\0x", + interior, + ]); }); -it("rejects a declared note table that extends past the file", () => { +it("rejects a declared note table that extends past the file", async () => { const truncated = elfNoteTablePastEnd(); - expect(truncated.length).toBe(137); - expect(() => parse(truncated)).toThrow(ArtifactReaderFailure); - expect(() => parse(truncated)).toThrow(/leaves the artifact/u); - const present = Buffer.concat([truncated, Buffer.alloc(3)]); - expect(parse(present).entries).toHaveLength(1); - expect(parse(present).tables[0]?.location).toEqual({ - offset: 120, - bytes: 20, + await expect(parse(truncated)).rejects.toMatchObject({ + reason: "format", + message: expect.stringMatching(/leaves the artifact/u), }); + const report = await parse(Buffer.concat([truncated, Buffer.alloc(3)])); + expect(report.entries).toHaveLength(1); + expect(report.tables[0]?.location).toEqual({ offset: 120, bytes: 20 }); }); -it("fails when the entry budget is exhausted", () => { +it("fails when the entry budget is exhausted", async () => { const notes = Buffer.concat([ gnuBuildIdNote(), gnuAbiTagNote(), elfNote("GNU", 4, Buffer.from("gold")), ]); const bytes = elfNoteFixture({ ownership: "program", notes }); - expect(() => - parseElfNotes( - bytes, - { - path: defaults.path, - sha256: createHash("sha256").update(bytes).digest("hex"), - bytes: bytes.length, - }, - inspectElfNotesInputSchema.parse({ path: defaults.path, max_entries: 1 }), - ), - ).toThrow( + await expect(parse(bytes, { ...defaults, max_entries: 1 })).rejects.toThrow( expect.objectContaining({ failure: expect.objectContaining({ kind: "limit", @@ -935,20 +977,22 @@ it("fails when the entry budget is exhausted", () => { }), }), ); - expect(parse(bytes, { ...defaults, max_entries: 3 }).coverage).toEqual({ + expect( + (await parse(bytes, { ...defaults, max_entries: 3 })).coverage, + ).toEqual({ status: "complete", examined_tables: 1, examined_entries: 3, }); }); -it("reads a glibc-sized note table with the default entry budget", () => { +it("reads a glibc-sized note table with the default entry budget", async () => { // Ubuntu's libc.so.6 has 79 SystemTap probe notes beside its build-id, ABI // tag and GNU property notes; this table has the first two and the probes. const probes = Array.from({ length: 79 }, () => elfNote("stapsdt", 3, Buffer.alloc(40)), ); - const report = parse( + const report = await parse( elfNoteFixture({ ownership: "section", notes: Buffer.concat([gnuBuildIdNote(), gnuAbiTagNote(), ...probes]), @@ -957,31 +1001,31 @@ it("reads a glibc-sized note table with the default entry budget", () => { expect(report.entries).toHaveLength(81); }); -it("keeps unsafe table offsets ahead of unsupported extended numbering", () => { +it("keeps unsafe table offsets ahead of unsupported extended numbering", async () => { const bytes = elfExtendedSectionCountFixture(); bytes.writeBigUInt64LE(0xffffffffffffffffn, 32); - expect(() => parse(bytes)).toThrow( + await expect(parse(bytes)).rejects.toThrow( "ELF offset or size exceeds a safe integer.", ); }); -it("does not apply note precision admission to unrelated program records", () => { +it("does not apply note precision admission to unrelated program records", async () => { const bytes = elf64({ phoff: 64, phnum: 1 }, Buffer.alloc(56)); bytes.writeUInt32LE(0x70000001, 64); for (const relative of [8, 16, 24, 32, 40, 48]) bytes.writeBigUInt64LE(0xffffffffffffffffn, 64 + relative); - expect(parse(bytes).tables).toEqual([]); + expect((await parse(bytes)).tables).toEqual([]); bytes.writeUInt32LE(4, 64); - expect(() => parse(bytes)).toThrow( + await expect(parse(bytes)).rejects.toThrow( "ELF offset or size exceeds a safe integer.", ); }); -it("keeps compressed-note refusal ahead of alignment precision admission", () => { +it("keeps compressed-note refusal ahead of alignment precision admission", async () => { const bytes = compressedElfNoteSectionFixture(); const sectionTable = Number(bytes.readBigUInt64LE(40)); bytes.writeBigUInt64LE(0xffffffffffffffffn, sectionTable + 64 + 48); - expect(() => parse(bytes)).toThrow(/compressed.*note/iu); + await expect(parse(bytes)).rejects.toThrow(/compressed.*note/iu); }); it.each([ @@ -991,22 +1035,24 @@ it.each([ { bits: 64, little: false }, ] as const)( "rejects dangling section names with and without notes (ELF$bits $little)", - ({ bits, little }) => { + async ({ bits, little }) => { for (const ownership of ["program", "none"] as const) { const bytes = elfNoteFixture({ bits, little, ownership }); - const before = parse(bytes); + const before = await parse(bytes); const nameOffset = bits === 64 ? 62 : 50; if (little) bytes.writeUInt16LE(5, nameOffset); else bytes.writeUInt16BE(5, nameOffset); - expect(() => parse(bytes)).toThrow("ELF section table is missing"); + await expect(parse(bytes)).rejects.toThrow( + "ELF section table is missing", + ); if (little) bytes.writeUInt16LE(0, nameOffset); else bytes.writeUInt16BE(0, nameOffset); - expect(parse(bytes)).toEqual(before); + expect(await parse(bytes)).toEqual(before); } }, ); -it("decodes Linux core dump notes for 32-bit big-endian", () => { +it("decodes Linux core dump notes for 32-bit big-endian", async () => { const notes = Buffer.concat([ corePrpsinfoNote({ bits: 32, @@ -1053,7 +1099,7 @@ it("decodes Linux core dump notes for 32-bit big-endian", () => { notes, }); - const report = parse(bytes); + const report = await parse(bytes); expect(report.entries).toHaveLength(3); expect(report.entries[0]?.core?.prpsinfo).toMatchObject({ state: 1, @@ -1119,14 +1165,14 @@ const elf32ProcessLayouts: readonly [ it.each(elf32ProcessLayouts)( "decodes the Linux ELF32 process record for %s", - (_name, machine, little, idWidth, flags) => { + async (_name, machine, little, idWidth, flags) => { const { bytes, shortened, expected } = elf32ProcessIdentityFixture( machine, little, idWidth, flags, ); - const report = parse(bytes); + const report = await parse(bytes); expect(report).toMatchObject({ format: "elf32", byte_order: little ? "little" : "big", @@ -1151,8 +1197,8 @@ it.each([ { name: "x86 x32 compat", machine: 62, little: true }, ])( "decodes the 124-byte prpsinfo with 16-bit IDs for $name", - ({ machine, little }) => { - const report = parse( + async ({ machine, little }) => { + const report = await parse( elfNoteFixture({ bits: 32, machine, @@ -1200,9 +1246,9 @@ it.each([ machine: 0xffff, idWidth: 4 as const, }, -])("keeps raw prpsinfo evidence for $name", ({ machine, idWidth }) => { +])("keeps raw prpsinfo evidence for $name", async ({ machine, idWidth }) => { const descriptor = corePrpsinfoDescriptor({ bits: 32, idWidth }); - const report = parse( + const report = await parse( elfNoteFixture({ bits: 32, machine, @@ -1230,7 +1276,7 @@ it.each([ { bits: 64 as const, little: false, address: 0xf123456789abcdefn }, ])( "keeps unsigned ELF$bits file and auxiliary words exact", - ({ bits, little, address }) => { + async ({ bits, little, address }) => { const mapping = { start: address, end: address + 1n, @@ -1241,7 +1287,7 @@ it.each([ coreFileNote([mapping], 4096, bits, little), coreAuxvNote([[9, address]], bits, little), ]); - const { entries } = parse( + const { entries } = await parse( elfNoteFixture({ bits, little, type: 4, ownership: "program", notes }), ); const expected = `0x${address.toString(16)}`; @@ -1260,13 +1306,15 @@ it.each([ [29, "AT_HWCAP3"], [30, "AT_HWCAP4"], [51, "AT_MINSIGSTKSZ"], -])("names auxv type %i as %s", (type, name) => { +])("names auxv type %i as %s", async (type, name) => { const notes = coreAuxvNote([[type, 1]]); - const { entries } = parse(elfNoteFixture({ ownership: "program", notes })); + const { entries } = await parse( + elfNoteFixture({ ownership: "program", notes }), + ); expect(entries[0]?.core?.auxv?.[0]?.type_name).toBe(name); }); -it("tolerates malformed core notes by returning null without throwing", () => { +it("tolerates malformed core notes by returning null without throwing", async () => { const truncatedPrpsinfo = elfNote("CORE", 3, Buffer.alloc(20)); // requires 136 for 64-bit const truncatedFile = elfNote( "CORE", @@ -1294,7 +1342,7 @@ it("tolerates malformed core notes by returning null without throwing", () => { notes, }); - const report = parse(bytes); + const report = await parse(bytes); expect(report.entries).toHaveLength(5); expect(report.entries[0]?.core).toBeNull(); expect(report.entries[0]?.type_name).toBe("core_prpsinfo"); @@ -1306,7 +1354,7 @@ it("tolerates malformed core notes by returning null without throwing", () => { expect(report.entries[3]?.type_name).toBe("core_auxv"); expect(report.entries[4]?.core).toBeNull(); expect(report.entries[4]?.type_name).toBe("core_auxv"); - const valid = parse( + const valid = await parse( elfNoteFixture({ bits: 64, ownership: "program", @@ -1326,7 +1374,7 @@ it("tolerates malformed core notes by returning null without throwing", () => { }); }); -it("decodes siginfo using target ABI for MIPS machines", () => { +it("decodes siginfo using target ABI for MIPS machines", async () => { const desc = Buffer.alloc(128); desc.writeInt32LE(11, 0); // signo = SIGSEGV desc.writeInt32LE(1, 4); // code = SEGV_MAPERR (offset 4 on MIPS) @@ -1338,7 +1386,7 @@ it("decodes siginfo using target ABI for MIPS machines", () => { ownership: "program", notes: mipsSiginfo, }); - const report = parse(bytes); + const report = await parse(bytes); expect(report.entries[0]?.core?.siginfo).toEqual({ signo: 11, code: 1, @@ -1346,7 +1394,7 @@ it("decodes siginfo using target ABI for MIPS machines", () => { }); }); -it("preserves negative process IDs and signal numbers without rewriting to zero", () => { +it("preserves negative process IDs and signal numbers without rewriting to zero", async () => { const prps = corePrpsinfoNote({ pid: -1, ppid: -1, @@ -1359,7 +1407,7 @@ it("preserves negative process IDs and signal numbers without rewriting to zero" ownership: "program", notes: Buffer.concat([prps, sig]), }); - const report = parse(bytes); + const report = await parse(bytes); expect(report.entries[0]?.core?.prpsinfo).toMatchObject({ pid: -1, ppid: -1, @@ -1373,7 +1421,7 @@ it("preserves negative process IDs and signal numbers without rewriting to zero" }); }); -it("rejects 64-bit auxv tags and out-of-range page sizes with core: null without schema error", () => { +it("rejects 64-bit auxv tags and out-of-range page sizes with core: null without schema error", async () => { const validAuxv = coreAuxvNote([[6, 4096n]], 64, true); const wideTagAuxv = Buffer.from(validAuxv); wideTagAuxv.writeBigUInt64LE(0x100000000n, 20); @@ -1399,14 +1447,14 @@ it("rejects 64-bit auxv tags and out-of-range page sizes with core: null without ownership: "program", notes: Buffer.concat([wideTagAuxv, widePageSizeFile]), }); - const report = parse(bytes); + const report = await parse(bytes); expect(report.entries[0]?.core).toBeNull(); expect(report.entries[1]?.core).toBeNull(); }); it.each([32, 64] as const)( "leaves ELF%d NT_FILE mappings unknown when the descriptor contains a surplus pathname", - (bits) => { + async (bits) => { const little = bits === 64; const file = coreFileNote( [{ start: 0x1000, end: 0x2000, file_ofs: 2, path: "/bin/app" }], @@ -1432,8 +1480,8 @@ it.each([32, 64] as const)( }), ); }; - const exact = inspect(Buffer.alloc(0)); - const padded = inspect(Buffer.alloc(3)); + const exact = await inspect(Buffer.alloc(0)); + const padded = await inspect(Buffer.alloc(3)); expect(exact.entries).toHaveLength(2); expect(padded.entries.map(({ core }) => core)).toEqual( exact.entries.map(({ core }) => core), @@ -1447,7 +1495,7 @@ it.each([32, 64] as const)( }); const tail = Buffer.from("/lib/extra.so\0"); - const malformed = inspect(tail); + const malformed = await inspect(tail); expect(malformed.coverage).toEqual(exact.coverage); expect(malformed.entries).toHaveLength(2); const payload = Buffer.concat([descriptor, tail]); @@ -1461,7 +1509,7 @@ it.each([32, 64] as const)( }, ); -it("does not spend text budget on malformed or discarded NT_FILE paths", () => { +it("does not spend text budget on malformed or discarded NT_FILE paths", async () => { const header = Buffer.alloc(16); header.writeBigUInt64LE(32n, 0); header.writeBigUInt64LE(4096n, 8); // page_size = 4096 @@ -1486,7 +1534,7 @@ it("does not spend text budget on malformed or discarded NT_FILE paths", () => { const budget = 2 * 14 + 54 + path.length * 3; expect(image.length).toBeLessThanOrEqual(budget); const bytes = Buffer.concat([image, Buffer.alloc(budget - image.length)]); - const report = parse(bytes); + const report = await parse(bytes); expect(report.entries[0]).toMatchObject({ type_name: "core_file", core: null, @@ -1504,7 +1552,7 @@ it("does not spend text budget on malformed or discarded NT_FILE paths", () => { path_hex: Buffer.from(path).toString("hex"), }, ]); - expect(() => parse(bytes.subarray(0, bytes.length - 1))).toThrow( + await expect(parse(bytes.subarray(0, bytes.length - 1))).rejects.toThrow( expect.objectContaining({ failure: expect.objectContaining({ kind: "limit", @@ -1521,7 +1569,7 @@ it.each([ { bits: 64, little: false, machine: 21 }, ] as const)( "decodes Android .note.android.ident 132-byte, 152-byte, and legacy 4-byte descriptors (ELF$bits little=$little machine=$machine)", - ({ bits, little, machine }) => { + async ({ bits, little, machine }) => { const extendedTail = Buffer.alloc(20, 0xaa); const notes = Buffer.concat([ androidIdentNote({ @@ -1553,108 +1601,75 @@ it.each([ sectionName: ".note.android.ident", notes, }); - const report = parse(bytes); + const report = await parse(bytes); expect(report.tables[0]?.name).toBe(".note.android.ident"); - expect(report.entries).toHaveLength(4); - - expect(report.entries[0]).toMatchObject({ + const ident = { owner: "Android", type: 1, type_name: "android_ident", - descsz: 132, gnu: null, core: null, - android: { - android_api: 24, - ndk_version: "r27c", - ndk_build_number: "12297006", + }; + expect(report.entries).toMatchObject([ + { + ...ident, + descsz: 132, + android: { + android_api: 24, + ndk_version: "r27c", + ndk_build_number: "12297006", + }, }, - }); - expect(report.entries[1]).toMatchObject({ - owner: "Android", - type: 1, - type_name: "android_ident", - descsz: 152, - gnu: null, - core: null, - android: { - android_api: 35, - ndk_version: "r28", - ndk_build_number: "12479018", + { + ...ident, + descsz: 152, + android: { + android_api: 35, + ndk_version: "r28", + ndk_build_number: "12479018", + }, }, - }); - expect(report.entries[2]).toMatchObject({ - owner: "Android", - type: 1, - type_name: "android_ident", - descsz: 4, - gnu: null, - core: null, - descriptor: { - bytes: 4, - hex: little ? "15000000" : "00000015", + { + ...ident, + descsz: 4, + descriptor: { bytes: 4, hex: little ? "15000000" : "00000015" }, + android: { android_api: 21, ndk_version: null, ndk_build_number: null }, }, - android: { - android_api: 21, - ndk_version: null, - ndk_build_number: null, + { + owner: "Android", + type: 2, + type_name: "unknown", + gnu: null, + core: null, + android: null, + descriptor: { bytes: 4, hex: "01020304" }, }, - }); - expect(report.entries[3]).toMatchObject({ - owner: "Android", - type: 2, - type_name: "unknown", - gnu: null, - core: null, - android: null, - descriptor: { - bytes: 4, - hex: "01020304", - }, - }); + ]); }, ); -it("degrades truncated, unterminated, or non-printable Android ident notes to null while preserving descriptors", () => { - const truncated2 = Buffer.from([0x15, 0x00]); - const truncated68 = Buffer.alloc(68); - truncated68.writeUInt32LE(24, 0); - truncated68.write("r27c\0", 4, "latin1"); - - const unterminatedVersion = Buffer.alloc(132); - unterminatedVersion.writeUInt32LE(24, 0); - unterminatedVersion.fill(0x72, 4, 68); - unterminatedVersion.write("12297006\0", 68, "latin1"); - - const unterminatedBuild = Buffer.alloc(132); - unterminatedBuild.writeUInt32LE(24, 0); - unterminatedBuild.write("r27c\0", 4, "latin1"); - unterminatedBuild.fill(0x39, 68, 132); - - const controlByteSlot = Buffer.alloc(132); - controlByteSlot.writeUInt32LE(24, 0); - controlByteSlot.set([0x72, 0x07, 0x00], 4); - controlByteSlot.write("12297006\0", 68, "latin1"); - - const nonZeroPaddingSlot = Buffer.alloc(132); - nonZeroPaddingSlot.writeUInt32LE(24, 0); - nonZeroPaddingSlot.write("r27c\0", 4, "latin1"); - nonZeroPaddingSlot[20] = 0x58; - nonZeroPaddingSlot.write("12297006\0", 68, "latin1"); - +it("degrades truncated, unterminated, or non-printable Android ident notes to null while preserving descriptors", async () => { + const descriptor = Buffer.alloc(132); + descriptor.writeUInt32LE(24, 0); + descriptor.write("r27c\0", 4); + descriptor.write("12297006\0", 68); + const controlByte = Buffer.from(descriptor); + controlByte[5] = 0x07; + const nonZeroPadding = Buffer.from(descriptor); + nonZeroPadding[20] = 0x58; const descriptors = [ Buffer.alloc(0), - truncated2, - truncated68, - unterminatedVersion, - unterminatedBuild, - controlByteSlot, - nonZeroPaddingSlot, + Buffer.of(0x15, 0), + descriptor.subarray(0, 68), + Buffer.from(descriptor).fill(0x72, 4, 68), + Buffer.from(descriptor).fill(0x39, 68, 132), + controlByte, + nonZeroPadding, ]; const notes = Buffer.concat( descriptors.map((desc) => elfNote("Android", 1, desc)), ); - const report = parse(elfNoteFixture({ ownership: "program", notes })); + const report = await parse(elfNoteFixture({ ownership: "program", notes })); expect(report.entries).toHaveLength(descriptors.length); for (const [index, desc] of descriptors.entries()) { expect(report.entries[index]).toMatchObject({ @@ -1673,7 +1688,7 @@ it("degrades truncated, unterminated, or non-printable Android ident notes to nu } }); -it("charges TextBudget for scanned and retained Android ident strings", () => { +it("charges TextBudget for scanned and retained Android ident strings", async () => { // 6 aliased headers over a 552-byte file: 128 scanned once <= 552, and // 6 * (23 owner + 12 retained) = 210 <= 552 (un-cached 6 * 128 = 768 > 552). const typicalIdent = androidIdentNote({ @@ -1681,7 +1696,7 @@ it("charges TextBudget for scanned and retained Android ident strings", () => { ndkVersion: "r27c", ndkBuildNumber: "12297006", }); - const aliased6 = parse(repeatedNotes(6, typicalIdent)); + const aliased6 = await parse(repeatedNotes(6, typicalIdent)); expect(aliased6.entries).toHaveLength(6); expect( aliased6.entries.every( @@ -1697,21 +1712,10 @@ it("charges TextBudget for scanned and retained Android ident strings", () => { ndkVersion: "v".repeat(63), ndkBuildNumber: "b".repeat(63), }); - let retainedFailure: unknown; - try { - parse(repeatedNotes(6, maxStrings)); - } catch (cause) { - retainedFailure = cause; - } - expect(retainedFailure).toBeInstanceOf(ParseFailureError); - expect(retainedFailure).toMatchObject({ - failure: { - kind: "limit", - format: "elf-notes", - limit: { name: "note_text_bytes" }, - }, - message: expect.stringMatching(/byte budget/u), - }); + const retained = repeatedNotes(6, maxStrings); + await expect(parse(retained)).rejects.toMatchObject( + noteTextLimit(retained.length), + ); // Share one 132-byte descriptor range across a GNU gold-version header // (namesz=24) and a nested Android ident header (namesz=8 at +16) so @@ -1747,7 +1751,7 @@ it("charges TextBudget for scanned and retained Android ident strings", () => { sharedPayload, ]); - const withinScanBudget = parse( + const withinScanBudget = await parse( sharedScanImage, inspectElfNotesInputSchema.parse({ path: defaults.path, @@ -1760,25 +1764,7 @@ it("charges TextBudget for scanned and retained Android ident strings", () => { ndk_build_number: "1", }); - let scanFailure: unknown; - try { - parse( - sharedScanImage, - inspectElfNotesInputSchema.parse({ - path: defaults.path, - max_file_bytes: 259, - }), - ); - } catch (cause) { - scanFailure = cause; - } - expect(scanFailure).toBeInstanceOf(ParseFailureError); - expect(scanFailure).toMatchObject({ - failure: { - kind: "limit", - format: "elf-notes", - limit: { name: "note_text_bytes" }, - }, - message: expect.stringMatching(/259-byte budget/u), - }); + await expect( + parse(sharedScanImage, { ...defaults, max_file_bytes: 259 }), + ).rejects.toMatchObject(noteTextLimit(259)); }); diff --git a/src/native/elf/ElfNoteParser.ts b/src/native/elf/ElfNoteParser.ts index b578c3242c..969a75556f 100644 --- a/src/native/elf/ElfNoteParser.ts +++ b/src/native/elf/ElfNoteParser.ts @@ -1204,27 +1204,6 @@ const decodeElfNoteStepsFromSource = async ( return next.value; }; -/** Decode PT_NOTE and SHT_NOTE tables from one in-memory ELF image. */ -export const parseElfNotes = ( - bytes: Buffer, - artifact: ElfNotes["artifact"], - input: InspectElfNotesInput, -): ElfNotes => - decodeElfNoteSteps(bytes, projectElfNoteSteps(bytes.length, artifact, input)); - -/** Same note decode as parseElfNotes, with cooperative section-name decoding. */ -export const parseElfNotesCooperative = ( - bytes: Buffer, - artifact: ElfNotes["artifact"], - input: InspectElfNotesInput, - pause: ZstandardFramePause, -): Promise => - decodeElfNoteStepsCooperative( - bytes, - projectElfNoteSteps(bytes.length, artifact, input), - (request) => decodeElfSectionNameRequestCooperative(request, pause), - ); - /** Decode PT_NOTE and SHT_NOTE tables positionally over a bounded file handle. */ export const parseElfNotesFromHandle = ( source: ElfNoteByteSource, diff --git a/src/native/elf/ElfNotes.fixture.ts b/src/native/elf/ElfNotes.fixture.ts index f1e896b422..c66cb04ede 100644 --- a/src/native/elf/ElfNotes.fixture.ts +++ b/src/native/elf/ElfNotes.fixture.ts @@ -1,5 +1,13 @@ import { deflateSync, zstdCompressSync } from "node:zlib"; +import type { ElfNoteByteSource } from "./ElfNoteParser.js"; + +export const elfNoteByteSource = (bytes: Buffer): ElfNoteByteSource => ({ + size: bytes.length, + prefix: bytes.subarray(0, 4096), + readRange: async (offset, length) => bytes.subarray(offset, offset + length), +}); + const concat = (...parts: Buffer[]): Buffer => Buffer.concat(parts); const alignTo = (value: number, alignment: number): number =>