mirror of
https://github.com/morluto/rea.git
synced 2026-11-19 21:23:45 +00:00
* feat(elf): decode FDO packaging and dlopen note metadata Name FDO notes within their owner's namespace: type 0xcafe1a7e is fdo_packaging_metadata and 0x407c0c0a is fdo_dlopen_metadata. One elfNoteTypeName(owner, type) replaces the GNU-only gnuNoteTypeName. Each entry gains an fdo field next to gnu. metadata holds the descriptor's JSON object (packaging) or array (dlopen) when it is NUL-terminated UTF-8 JSON followed only by NUL padding; otherwise metadata is null and issue names the broken rule, without failing the inspection. Each descriptor range is decoded once and its retained JSON text is charged to the shared note text budget per repeated entry. A limitation marks the metadata as a producer declaration. Fixes #7235 * fix(elf): key FDO note decoding by note type The FDO decode cache was keyed by descriptor range only, but the note type selects the required JSON shape. Overlapping PT_NOTE and SHT_NOTE tables can give one descriptor range a packaging note and a dlopen note, and the second reused the first note's verdict. Include the type name in the key. Refs #7235 * fix(elf): bound FDO note decoding before parsing JSON The FDO decoder charged only descriptor source bytes before JSON.parse, but a dense JSON value such as [{},{},...] decodes to about 50 times its source size in heap before the service and Evidence copy it again. A 16 MiB descriptor took about 790 MiB of heap; one near the default 64 MiB file budget could exhaust the Node heap. Leave descriptors above 64 KiB undecoded with an issue naming the limit; real FDO notes are a few hundred bytes. Refs #7235 * fix(elf): share one FDO decode allowance across the report The 64 KiB FDO decode bound applied to each descriptor, so raising max_entries to 256 admitted 256 distinct dense 64 KiB notes: a 16 MiB file added about 700 MiB of heap before Evidence copied it again. Charge each distinct descriptor's size to one 64 KiB allowance per report; a range repeated by another table reuses its decode, and notes past the allowance keep their entry with an issue. Refs #7235 * test(desktop): exercise the shared JSON depth guard in the heap suite --------- Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
243 lines
8.3 KiB
TypeScript
243 lines
8.3 KiB
TypeScript
import { readFile, writeFile } from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
import { expect } from "vitest";
|
|
import { toolContract } from "../../src/contracts/toolContracts.js";
|
|
import {
|
|
compressedElfNoteFixture,
|
|
compressedElfNoteSectionFixture,
|
|
elfExtendedSectionCountFixture,
|
|
elfNote,
|
|
elfNoteFixture,
|
|
elfNoteWithUnreadableSectionNameFixture,
|
|
} from "../../src/native/elf/ElfNotes.fixture.js";
|
|
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
|
|
import { cliTest } from "../support/cli/cliFixture.js";
|
|
|
|
cliTest("reports a GNU build-id through the public CLI", async ({ cli }) => {
|
|
const root = await createTestTempDirectory("rea-elf-notes-cli-");
|
|
const path = join(root, "program");
|
|
await writeFile(path, elfNoteFixture({ ownership: "program" }));
|
|
const result = await cli.run({
|
|
arguments: ["inspect-elf-notes", path, "--json"],
|
|
});
|
|
expect(result.exitCode, result.stderr).toBe(0);
|
|
const evidence = toolContract("inspect_elf_notes").outputSchema.parse(
|
|
result.json,
|
|
);
|
|
expect(
|
|
evidence.normalized_result.entries.find(
|
|
(entry) => entry.type_name === "gnu_build_id",
|
|
)?.gnu?.build_id_hex,
|
|
).toBe("0123456789abcdef0123456789abcdef01234567");
|
|
});
|
|
|
|
cliTest(
|
|
"reports FDO producer assertions in public ELF note Evidence",
|
|
async ({ cli }) => {
|
|
const root = await createTestTempDirectory("rea-elf-notes-fdo-cli-");
|
|
const path = join(root, "program");
|
|
const metadata = [{ soname: ["libdemo.so.1"], feature: "examples" }];
|
|
const notes = elfNote(
|
|
"FDO",
|
|
0x407c0c0a,
|
|
Buffer.from(JSON.stringify(metadata) + "\0"),
|
|
);
|
|
await writeFile(
|
|
path,
|
|
elfNoteFixture({
|
|
ownership: "section",
|
|
notes,
|
|
sectionName: ".note.dlopen",
|
|
}),
|
|
);
|
|
const result = await cli.run({
|
|
arguments: ["inspect-elf-notes", path, "--json"],
|
|
});
|
|
expect(result.exitCode, result.stderr).toBe(0);
|
|
const evidence = toolContract("inspect_elf_notes").outputSchema.parse(
|
|
result.json,
|
|
);
|
|
expect(evidence.normalized_result.entries[0]).toMatchObject({
|
|
type_name: "fdo_dlopen_metadata",
|
|
fdo: { metadata, issue: null },
|
|
});
|
|
expect(evidence.limitations).toContain(
|
|
"FDO packaging and dlopen metadata are unverified producer declarations.",
|
|
);
|
|
},
|
|
);
|
|
|
|
cliTest(
|
|
"resolves section note names from zlib and Zstandard string tables through the CLI",
|
|
async ({ cli }) => {
|
|
const root = await createTestTempDirectory("rea-elf-notes-compressed-cli-");
|
|
for (const compressionType of [1, 2]) {
|
|
const path = join(root, `program-${String(compressionType)}`);
|
|
const bytes = compressedElfNoteFixture(64, true, compressionType);
|
|
await writeFile(path, bytes);
|
|
const result = await cli.run({
|
|
arguments: ["inspect-elf-notes", path, "--json"],
|
|
});
|
|
expect(result.exitCode, result.stderr).toBe(0);
|
|
const evidence = toolContract("inspect_elf_notes").outputSchema.parse(
|
|
result.json,
|
|
);
|
|
expect(evidence.normalized_result.tables[0]?.name).toBe(
|
|
".note.gnu.build-id",
|
|
);
|
|
expect(evidence.normalized_result.entries[0]?.gnu?.build_id_hex).toBe(
|
|
"0123456789abcdef0123456789abcdef01234567",
|
|
);
|
|
expect(Buffer.compare(await readFile(path), bytes)).toBe(0);
|
|
}
|
|
},
|
|
);
|
|
|
|
cliTest(
|
|
"keeps GNU notes when a section-name offset is outside its string table",
|
|
async ({ cli }) => {
|
|
const root = await createTestTempDirectory("rea-elf-notes-name-cli-");
|
|
const path = join(root, "program");
|
|
const bytes = elfNoteWithUnreadableSectionNameFixture("outside");
|
|
await writeFile(path, bytes);
|
|
const result = await cli.run({
|
|
arguments: ["inspect-elf-notes", path, "--json"],
|
|
});
|
|
expect(result.exitCode, result.stderr).toBe(0);
|
|
const evidence = toolContract("inspect_elf_notes").outputSchema.parse(
|
|
result.json,
|
|
);
|
|
expect(evidence.normalized_result.tables[0]?.name).toBeNull();
|
|
expect(evidence.normalized_result.limitations.join(" ")).toMatch(
|
|
/name offset.*string table/iu,
|
|
);
|
|
expect(evidence.normalized_result.entries[0]?.gnu?.build_id_hex).toBe(
|
|
"0123456789abcdef0123456789abcdef01234567",
|
|
);
|
|
expect(evidence.normalized_result.coverage).toMatchObject({
|
|
status: "complete",
|
|
examined_tables: 1,
|
|
examined_entries: 1,
|
|
});
|
|
expect(Buffer.compare(await readFile(path), bytes)).toBe(0);
|
|
},
|
|
);
|
|
|
|
cliTest(
|
|
"resolves a relative ELF path against the working directory",
|
|
async ({ cli }) => {
|
|
const root = await createTestTempDirectory("rea-elf-notes-cli-rel-");
|
|
await writeFile(
|
|
join(root, "unsigned"),
|
|
elfNoteFixture({ ownership: "none" }),
|
|
);
|
|
const result = await cli.run({
|
|
arguments: ["inspect-elf-notes", "unsigned", "--json"],
|
|
cwd: root,
|
|
});
|
|
expect(result.exitCode, result.stderr).toBe(0);
|
|
const evidence = toolContract("inspect_elf_notes").outputSchema.parse(
|
|
result.json,
|
|
);
|
|
expect(evidence.normalized_result.tables).toEqual([]);
|
|
expect(evidence.subject?.local_path).toBe(join(root, "unsigned"));
|
|
},
|
|
);
|
|
|
|
cliTest("reports a truncated ELF as invalid input", async ({ cli }) => {
|
|
const root = await createTestTempDirectory("rea-elf-notes-cli-bad-");
|
|
const path = join(root, "broken");
|
|
await writeFile(path, Buffer.from("\x7fELF"));
|
|
const result = await cli.run({
|
|
arguments: ["inspect-elf-notes", path, "--json"],
|
|
});
|
|
expect(result.exitCode).toBe(1);
|
|
expect(result.json).toMatchObject({
|
|
code: "invalid_request",
|
|
details: { operation: "inspect_elf_notes" },
|
|
});
|
|
});
|
|
|
|
cliTest(
|
|
"reports compressed note storage as unsupported without mutation",
|
|
async ({ cli }) => {
|
|
const root = await createTestTempDirectory("rea-elf-notes-storage-cli-");
|
|
const path = join(root, "debug-notes.o");
|
|
const bytes = compressedElfNoteSectionFixture();
|
|
await writeFile(path, bytes);
|
|
const result = await cli.run({
|
|
arguments: ["inspect-elf-notes", path, "--json"],
|
|
});
|
|
expect(result.exitCode).toBe(1);
|
|
expect(result.json).toMatchObject({
|
|
code: "unsupported_target",
|
|
message: expect.stringMatching(/compressed.*note/iu),
|
|
});
|
|
expect(Buffer.compare(await readFile(path), bytes)).toBe(0);
|
|
},
|
|
);
|
|
|
|
cliTest(
|
|
"reports valid extended numbering as unsupported without mutation",
|
|
async ({ cli }) => {
|
|
const root = await createTestTempDirectory("rea-elf-notes-extended-cli-");
|
|
const path = join(root, "extended-count.elf");
|
|
await writeFile(path, elfExtendedSectionCountFixture());
|
|
const before = await readFile(path);
|
|
const result = await cli.run({
|
|
arguments: ["inspect-elf-notes", path, "--json"],
|
|
});
|
|
expect(result.exitCode).toBe(1);
|
|
expect(result.json).toMatchObject({
|
|
code: "unsupported_target",
|
|
message: expect.stringMatching(/extended.*number/iu),
|
|
});
|
|
expect((await readFile(path)).equals(before)).toBe(true);
|
|
},
|
|
);
|
|
|
|
cliTest(
|
|
"rejects dangling section-name references and preserves sectionless notes",
|
|
async ({ cli }) => {
|
|
const root = await createTestTempDirectory("rea-elf-notes-presence-cli-");
|
|
const path = join(root, "program");
|
|
for (const ownership of ["program", "none"] as const) {
|
|
const bytes = elfNoteFixture({ ownership });
|
|
bytes.writeUInt16LE(5, 62);
|
|
await writeFile(path, bytes);
|
|
const result = await cli.run({
|
|
arguments: ["inspect-elf-notes", path, "--json"],
|
|
});
|
|
expect(result.exitCode).toBe(1);
|
|
expect(result.json).toMatchObject({
|
|
code: "invalid_request",
|
|
category: "invalid_input",
|
|
details: {
|
|
operation: "inspect_elf_notes",
|
|
issues: [
|
|
expect.objectContaining({
|
|
path: ["path"],
|
|
reason: "invalid_format",
|
|
message: "ELF section table is missing",
|
|
}),
|
|
],
|
|
},
|
|
});
|
|
expect((await readFile(path)).equals(bytes)).toBe(true);
|
|
bytes.writeUInt16LE(0, 62);
|
|
await writeFile(path, bytes);
|
|
const accepted = await cli.run({
|
|
arguments: ["inspect-elf-notes", path, "--json"],
|
|
});
|
|
expect(accepted.exitCode, accepted.stderr).toBe(0);
|
|
const evidence = toolContract("inspect_elf_notes").outputSchema.parse(
|
|
accepted.json,
|
|
);
|
|
expect(evidence.normalized_result.coverage.status).toBe("complete");
|
|
expect(evidence.normalized_result.tables).toHaveLength(
|
|
ownership === "none" ? 0 : 1,
|
|
);
|
|
}
|
|
},
|
|
);
|