mirror of
https://github.com/morluto/rea.git
synced 2026-11-27 14:49:15 +00:00
* fix(errors): refuse malformed target bytes as a target, not invalid input parseFailureAnalysisError is the shared owner that turns a byte-format parser's ParseFailure into a public refusal. It mapped malformed and truncated bytes to an AnalysisInputError on the path field. The envelope then reported invalid_request, with inspect_input_contract recovery and "correct the listed fields". The arguments were valid; the target bytes were the problem. Readers that wrap the ParseFailureError in an ArtifactReaderFailure, and ArtifactOperationError reason=malformed, were classified the same way. Add AnalysisMalformedTargetError. It keeps the parser's kind, format, detail, offset and field, and projects as unsupported_target with details.format_failure. Its recovery is to choose another operation or a well-formed copy of the target. Route both parser entry points through it and reclassify ArtifactOperationError reason=malformed. Argument and selection errors (missing files, directories, symlinks, JSON arguments) stay invalid_request. Fixes #14170 * docs(maven): describe duplicate variant names as malformed metadata Duplicate Gradle variant names go through malformedInput, which now reports unsupported_target with details.format_failure: malformed. Point the earlier statement at that section instead of the old invalid_format code. * test(errors): expect malformed-target refusals from CLI byte-format readers Truncated IPS, ELF, PE and shortcut bytes now project as unsupported_target with format_failure=malformed. Update the CLI acceptance cases that still expected invalid_request. * fix(errors): keep a cancelled read a cancellation whatever its abort reason projectOwnedArtifactInputFailure read the reader failure's cause without checking the reader's own reason. A cancelled read carries the caller's opaque abort reason as that cause, so aborting with a ParseFailureError turned the cancellation into a non-retryable malformed-target refusal before the shared cancellation projection ran. Only a reader whose own reason is malformed or truncated now yields a malformed target. The ELF dependency checkpoint cancellation test now also aborts with a parser failure as the reason, through the public provider read. * test(pe): expect malformed-target refusals from the PE filesystem boundaries Short non-PE bytes now refuse the target rather than the request field. The missing-file, directory, symlink and child-path controls keep their caller-input expectations. * fix(elf): refuse malformed ELF note bytes as a target, not invalid input inspect_elf_notes shared this PR's root cause: its parser reported structural violations as a reader "format" failure, which the shared stable-artifact projection treats as a request-field error, so truncated or non-ELF bytes still returned invalid_request. The parser now raises malformed or truncated ParseFailureErrors, which the shared projection reports as a malformed target with format_failure and format facts. Consumers that interpreted the old reader reason now use the parser's facts: debug-link build-id observation keys on ParseFailureError kinds, and binary hardening drops its rewrap of note-parser format failures into BinaryFormatFailure, which the parser's own failure supersedes. * test(errors): own the byte readers' malformed-target projection in one CLI table The PR repeated the same malformed -> unsupported_target mapping in a dedicated CLI test per command and again in each provider's filesystem boundary test. The stable-artifact parser-failure CLI table now owns it: every byte reader it covers (now including ELF dependencies, debug link, unwind ranges and native initializers) refuses a truncated ELF header as a malformed target with operation, path, format_failure, retained file identity and no argument recovery. Removed as redundant with that table: the truncated or non-ELF/non-PE CLI tests for ELF debug link, dependencies, unwind ranges and notes, native initializers, and PE imports and certificates; and the malformed-file blocks in thirteen filesystem boundary tests, which keep their directory, symlink, missing-file and child-path controls. Format specific refusals (a truncated IPS body, a wrong shortcut CLSID, DWARF units) keep their own tests, as do the owner tests in parseFailureAnalysisError and ArtifactOperationFailure. * test(errors): expect malformed target bytes as unsupported_target in acceptance tests The first full acceptance run of this PR found five expectations still asserting invalid_request for target-byte failures: the timing test's truncated ELF, a truncated DTB, duplicate Rust dependency JSON, and PE32 and PE64 load-config images without an MZ header. Assert the unsupported_target code, with format_failure malformed where the non-MZ PE path matches the already-updated PE imports test.
293 lines
9.3 KiB
TypeScript
293 lines
9.3 KiB
TypeScript
import { readFile, truncate, writeFile } from "node:fs/promises";
|
|
import { createHash } from "node:crypto";
|
|
import { join } from "node:path";
|
|
import { expect } from "vitest";
|
|
import { toolContract } from "../../src/contracts/toolContracts.js";
|
|
import {
|
|
elfRelocatableCtors,
|
|
elf64AliasedInitializers,
|
|
elf64PieInitializers,
|
|
elfUnmappedInitializerTargets,
|
|
machoArm64Initializers,
|
|
machoIndirectInitializerGolden,
|
|
} from "../../src/native/initializers/NativeInitializers.fixture.js";
|
|
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
|
|
import { cliTest } from "../support/cli/cliFixture.js";
|
|
import { aps2InitializerImage } from "../fixtures/nativeAps2Initializers.js";
|
|
|
|
const contract = toolContract("inspect_native_initializers");
|
|
|
|
cliTest(
|
|
"reads APS2 RELA alongside RELR through the public CLI without changing the image",
|
|
async ({ cli }) => {
|
|
const root = await createTestTempDirectory("rea-aps2-initializers-cli-");
|
|
const path = join(root, "library.so");
|
|
const bytes = aps2InitializerImage();
|
|
await writeFile(path, bytes);
|
|
const result = await cli.run({
|
|
arguments: ["inspect-native-initializers", path, "--json"],
|
|
});
|
|
expect(result.exitCode, result.stderr).toBe(0);
|
|
const report = contract.outputSchema.parse(result.json).normalized_result;
|
|
expect(report.coverage).toMatchObject({
|
|
status: "complete",
|
|
total_entries: 7,
|
|
});
|
|
expect(
|
|
report.entries
|
|
.filter((entry) => entry.kind === "init_array")
|
|
.map((entry) => [entry.value_va, entry.resolution]),
|
|
).toEqual([
|
|
["0x100", "relative_addend"],
|
|
["0x110", "relative_addend"],
|
|
[null, "unresolved_import"],
|
|
]);
|
|
expect(
|
|
report.entries.find((entry) => entry.kind === "fini_array"),
|
|
).toMatchObject({ value_va: "0x120", resolution: "relr" });
|
|
expect((await readFile(path)).equals(bytes)).toBe(true);
|
|
},
|
|
);
|
|
|
|
cliTest(
|
|
"does not name an initializer from an LLVM indirect-symbol string index",
|
|
async ({ cli }) => {
|
|
const root = await createTestTempDirectory("rea-native-init-indirect-cli-");
|
|
const path = join(root, "program.o");
|
|
const bytes = machoIndirectInitializerGolden();
|
|
await writeFile(path, bytes);
|
|
const result = await cli.run({
|
|
arguments: ["inspect-native-initializers", path, "--json"],
|
|
});
|
|
expect(result.exitCode, result.stderr).toBe(0);
|
|
const report = contract.outputSchema.parse(result.json).normalized_result;
|
|
expect(report.entries).toMatchObject([
|
|
{
|
|
value_va: "0x8",
|
|
value_file_offset: 376,
|
|
symbol: null,
|
|
resolution: "raw",
|
|
},
|
|
]);
|
|
expect(report.artifact.sha256).toBe(
|
|
"dc67f456f3b98a7e2279971a5049b089668fadbbd75f0c4e630383d413f713bf",
|
|
);
|
|
expect((await readFile(path)).equals(bytes)).toBe(true);
|
|
},
|
|
);
|
|
|
|
cliTest(
|
|
"keeps an unrelocated ELF alias's value and source identity through the CLI",
|
|
async ({ cli }) => {
|
|
const root = await createTestTempDirectory("rea-native-init-alias-cli-");
|
|
const path = join(root, "program");
|
|
const bytes = elf64AliasedInitializers();
|
|
await writeFile(path, bytes);
|
|
const result = await cli.run({
|
|
arguments: ["inspect-native-initializers", path, "--json"],
|
|
});
|
|
expect(result.exitCode, result.stderr).toBe(0);
|
|
const report = contract.outputSchema.parse(result.json).normalized_result;
|
|
expect(report.status).toBe("parsed");
|
|
expect(report.artifact).toMatchObject({
|
|
path,
|
|
sha256: createHash("sha256").update(bytes).digest("hex"),
|
|
bytes: bytes.length,
|
|
});
|
|
expect(
|
|
report.entries.find((entry) => entry.kind === "preinit_array"),
|
|
).toMatchObject({
|
|
slot_va: "0x200",
|
|
slot_file_offset: 0x200,
|
|
value_va: "0x100",
|
|
resolution: "relative_addend",
|
|
});
|
|
expect(
|
|
report.entries.find((entry) => entry.kind === "init_array"),
|
|
).toMatchObject({
|
|
slot_va: "0x1200",
|
|
slot_file_offset: 0x200,
|
|
value_va: "0x0",
|
|
symbol: null,
|
|
resolution: "raw",
|
|
});
|
|
expect((await readFile(path)).equals(bytes)).toBe(true);
|
|
},
|
|
);
|
|
|
|
cliTest(
|
|
"lists Mach-O initializers and finalizers through the public CLI",
|
|
async ({ cli }) => {
|
|
const root = await createTestTempDirectory("rea-native-init-cli-");
|
|
const path = join(root, "program");
|
|
await writeFile(path, machoArm64Initializers());
|
|
const result = await cli.run({
|
|
arguments: ["inspect-native-initializers", path, "--json"],
|
|
});
|
|
expect(result.exitCode, result.stderr).toBe(0);
|
|
const evidence = contract.outputSchema.parse(result.json);
|
|
expect(
|
|
evidence.normalized_result.entries.map((entry) => [
|
|
entry.kind,
|
|
entry.value_va,
|
|
entry.symbol?.name,
|
|
]),
|
|
).toEqual([
|
|
["mod_init", "0x100001000", "_init"],
|
|
["init_offsets", "0x100001000", "_init"],
|
|
["mod_term", "0x100001100", "_term"],
|
|
]);
|
|
},
|
|
);
|
|
|
|
cliTest(
|
|
"pages relocatable-object constructors with decimal options",
|
|
async ({ cli }) => {
|
|
const root = await createTestTempDirectory("rea-native-init-cli-page-");
|
|
const path = join(root, "object.o");
|
|
await writeFile(path, elfRelocatableCtors());
|
|
const result = await cli.run({
|
|
arguments: [
|
|
"inspect-native-initializers",
|
|
path,
|
|
"--offset",
|
|
"1",
|
|
"--limit",
|
|
"1",
|
|
"--json",
|
|
],
|
|
});
|
|
expect(result.exitCode, result.stderr).toBe(0);
|
|
const evidence = contract.outputSchema.parse(result.json);
|
|
expect(evidence.normalized_result.coverage).toMatchObject({
|
|
// Every slot read counts, including the two omitted .ctors sentinels.
|
|
examined_entries: 4,
|
|
total_entries: 2,
|
|
returned_entries: 1,
|
|
offset: 1,
|
|
});
|
|
expect(evidence.normalized_result.entries[0]).toMatchObject({
|
|
index: 1,
|
|
kind: "ctors",
|
|
symbol: { name: "ctor" },
|
|
resolution: "symbol_addend",
|
|
});
|
|
},
|
|
);
|
|
|
|
const dynamicTableCeiling = 16 * 1024 * 1024;
|
|
|
|
cliTest.for([
|
|
{
|
|
name: "short partial row",
|
|
size: 33,
|
|
backed: true,
|
|
code: "unsupported_target",
|
|
},
|
|
{
|
|
name: "oversized unbacked declaration",
|
|
size: dynamicTableCeiling + 16,
|
|
backed: false,
|
|
code: "unsupported_target",
|
|
},
|
|
{
|
|
name: "oversized backed partial row",
|
|
size: dynamicTableCeiling + 1,
|
|
backed: true,
|
|
code: "unsupported_target",
|
|
},
|
|
{
|
|
name: "oversized backed whole rows",
|
|
size: dynamicTableCeiling + 16,
|
|
backed: true,
|
|
code: "resource_constraint",
|
|
},
|
|
])(
|
|
"adjudicates PT_DYNAMIC admission for $name",
|
|
async ({ size, backed, code }, { cli }) => {
|
|
const root = await createTestTempDirectory("rea-native-init-dynamic-cli-");
|
|
const path = join(root, "library.so");
|
|
const bytes = elf64PieInitializers();
|
|
// The second ELF64 program header declares PT_DYNAMIC at file offset 0x300.
|
|
bytes.writeBigUInt64LE(BigInt(size), 120 + 32);
|
|
bytes.writeBigUInt64LE(BigInt(size), 120 + 40);
|
|
await writeFile(path, bytes);
|
|
if (backed && 0x300 + size > bytes.length)
|
|
await truncate(path, 0x300 + size);
|
|
const result = await cli.run({
|
|
arguments: ["inspect-native-initializers", path, "--json"],
|
|
});
|
|
expect(result.json).toMatchObject({
|
|
code,
|
|
details: {
|
|
operation: "inspect_native_initializers",
|
|
...(code === "unsupported_target"
|
|
? { path, format_failure: "malformed" }
|
|
: {
|
|
reported_limits: {
|
|
boundary: "structure",
|
|
maximum: dynamicTableCeiling,
|
|
},
|
|
}),
|
|
},
|
|
});
|
|
},
|
|
);
|
|
|
|
cliTest(
|
|
"keeps unmapped ELF target locations null in public CLI Evidence",
|
|
async ({ cli }) => {
|
|
const root = await createTestTempDirectory("rea-native-init-unmapped-cli-");
|
|
const path = join(root, "program");
|
|
await writeFile(path, elfUnmappedInitializerTargets());
|
|
const result = await cli.run({
|
|
arguments: ["inspect-native-initializers", path, "--json"],
|
|
});
|
|
expect(result.exitCode, result.stderr).toBe(0);
|
|
const evidence = contract.outputSchema.parse(result.json);
|
|
expect(
|
|
evidence.normalized_result.entries.map((entry) => [
|
|
entry.value_va,
|
|
entry.value_file_offset,
|
|
]),
|
|
).toEqual([
|
|
["0x0", null],
|
|
["0x10", null],
|
|
["0x401000", 0x100],
|
|
]);
|
|
expect(evidence.normalized_result.coverage).toMatchObject({
|
|
examined_entries: 3,
|
|
total_entries: 3,
|
|
});
|
|
},
|
|
);
|
|
|
|
cliTest(
|
|
"resolves Android RELR initializer slots through the public CLI without changing source bytes",
|
|
async ({ cli }) => {
|
|
const root = await createTestTempDirectory("rea-android-relr-cli-");
|
|
const path = join(root, "library.so");
|
|
const bytes = elf64PieInitializers(0, "android");
|
|
await writeFile(path, bytes);
|
|
const result = await cli.run({
|
|
arguments: ["inspect-native-initializers", path, "--json"],
|
|
});
|
|
expect(result.exitCode, result.stderr).toBe(0);
|
|
const report = contract.outputSchema.parse(result.json).normalized_result;
|
|
expect(report.coverage).toMatchObject({
|
|
status: "complete",
|
|
total_entries: 7,
|
|
});
|
|
expect(
|
|
report.entries.find((entry) => entry.kind === "fini_array"),
|
|
).toMatchObject({
|
|
slot_va: "0x220",
|
|
slot_file_offset: 0x220,
|
|
value_va: "0x120",
|
|
resolution: "relr",
|
|
symbol: { name: "fini_fn" },
|
|
});
|
|
expect((await readFile(path)).equals(bytes)).toBe(true);
|
|
},
|
|
);
|