refactor(elf): retire obsolete note buffer parsers (#13995)

This commit is contained in:
morluto authored and GitHub committed 2026-11-23 00:23:44 -05:00
1 parent 27eef47122
commit a8fa445c41
5 files changed
+367 -388

No files matched your search

@@ -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 () => {
+14 -12
View File
@@ -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);
});
File diff suppressed because it is too large. Load diff
-21
View File
@@ -1204,27 +1204,6 @@ const decodeElfNoteStepsFromSource = async <T>(
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<ElfNotes> =>
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,
+8
View File
@@ -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 =>