mirror of
https://github.com/morluto/rea.git
synced 2026-12-04 14:01:00 +00:00
* feat(firmware): decode ARM M-profile and RISC-V mtvec interrupt vector tables * fix(firmware): name vector-table exceptions per ARM variant and report unbound SVD interrupts The architecture field now takes the ARM M-profile variant names REA uses for raw images (armv6-m through armv8.1-m.main) plus riscv. ARMv8-M Baseline gets ARMv6-M's exceptions; only Mainline names MemManage, BusFault, UsageFault, SecureFault and DebugMonitor. The old armv8-m value named Mainline faults on Cortex-M23 tables. vtor_aligned checks the 128-byte ARMv7-M/ARMv8-M minimum and is null on ARMv6-M, whose VTOR is optional and implementation-defined. Slots past the end of the 32-bit address space are no longer decoded with wrapped addresses, and an initial stack pointer that is not word-aligned is flagged. CMSIS-SVD interrupts without a name or with a non-integer value are reported as anomalies instead of skipped silently, and the expanded-name budget is charged once while parsing instead of twice with two formulas. likely_default_stub is removed: every shared-handler group set it to true. RISC-V limitations now cover vectored exceptions on slot 0, CLIC address tables and the mcause numbering assumed for SVD bindings. * fix(firmware): bound SVD anomaly diagnostics, RISC-V pair addresses, and shared budget recovery * fix(firmware): index SVD duplicates per peripheral and admit one-slot RISC-V tables Duplicate interrupt declarations were found by rebuilding a key that repeats the full peripheral name for every <interrupt>, so a long name repeated across many declarations cost name length times declaration count. Bound names are now indexed per peripheral name, looked up once per <peripheral>. The 8-byte minimum held for every architecture, but only an ARM table must start with InitialSP and Reset; a RISC-V Direct-mode table can be one instruction. max_entries and max_file_bytes now admit one slot. * refactor(firmware): hoist the SVD IRQ value patterns to module scope * fix(firmware): record RISC-V pair byte extent and attribute caller input errors * style(firmware): format the vector-table provider * fix(firmware): add summary delivery and analysis views for vector tables * test(firmware): check vector-table recoveries and budgets through their owners Recovery assertions compare the delivered action with the shared refusal formatter applied to the reported limit, instead of pinning sentences. The summary-delivery test set REA_AGENT_RESULT_MAX_BYTES, which nothing reads, and used a result large enough to exceed the default budget too; it now sets REA_AGENT_RESULT_BUDGET_BYTES, asserts the reported budget, and uses a result between the minimum and default budgets, checking that the default delivers it complete. The view counts active ARM entries and recognized RISC-V slots with a loop instead of filtered copies. * perf(firmware): count active vector slots once in the report producer The analysis view recounted active ARM entries and recognized RISC-V slots on every summary and page request. The producer already collects exactly those slots as handler targets, so the report now stores active_arm_entry_count and recognized_riscv_slot_count from them, and the view reads the stored fields. The service checks the stored counts against the slots once when it validates the provider's report. --------- Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
199 lines
6.5 KiB
TypeScript
199 lines
6.5 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import { AnalysisCancelledError } from "../../domain/analysisErrorCore.js";
|
|
import { FirmwareVectorTableService } from "./FirmwareVectorTableService.js";
|
|
import { FIRMWARE_VECTOR_TABLE_PROVIDER } from "../../domain/investigationProviders.js";
|
|
import { decodeLimit } from "../../domain/parseFailure.js";
|
|
import { parseLimitConstraint } from "../../domain/parseFailureAnalysisError.js";
|
|
import {
|
|
firmwareVectorBudgetLimit,
|
|
type FirmwareVectorTable,
|
|
} from "../../domain/firmware/firmwareVectorTable.js";
|
|
import { err, ok } from "../../domain/result.js";
|
|
import type { FirmwareVectorTablePort } from "../../ports/binaryDiagnostics/FirmwareVectorTablePort.js";
|
|
|
|
const sampleArmReport = (
|
|
path: string,
|
|
vectorTableOffset: number,
|
|
): FirmwareVectorTable => ({
|
|
artifact: {
|
|
path,
|
|
sha256: "e".repeat(64),
|
|
bytes: 256,
|
|
},
|
|
architecture: "armv7-m",
|
|
base_address: "0x8000000",
|
|
vector_table_offset: vectorTableOffset,
|
|
vector_table_address: `0x${(0x08000000 + vectorTableOffset).toString(16)}`,
|
|
vtor_aligned: vectorTableOffset % 128 === 0,
|
|
examined_entries: 4,
|
|
table_file_range: { start: vectorTableOffset, end: vectorTableOffset + 16 },
|
|
coverage: {
|
|
status: "partial",
|
|
examined_bytes: 16,
|
|
available_from_offset_bytes: 256 - vectorTableOffset,
|
|
},
|
|
initial_stack_pointer: "0x20010000",
|
|
reset_code_address: "0x8000100",
|
|
trap_entry_address: null,
|
|
riscv_table_shape: null,
|
|
arm_entries: [],
|
|
active_arm_entry_count: 0,
|
|
riscv_slots: [],
|
|
recognized_riscv_slot_count: 0,
|
|
shared_handler_groups: [],
|
|
svd_interrupts: {
|
|
supplied: true,
|
|
mapped_irq_count: 0,
|
|
ambiguous_irq_count: 0,
|
|
unmapped_active_irq_count: 0,
|
|
},
|
|
anomalies: [],
|
|
limitations: ["Decoded 4 slots up to max_entries."],
|
|
});
|
|
|
|
describe("FirmwareVectorTableService", () => {
|
|
it("refuses a provider report whose stored slot counts disagree with its slots", async () => {
|
|
const mockPort: FirmwareVectorTablePort = {
|
|
identity: FIRMWARE_VECTOR_TABLE_PROVIDER,
|
|
inspect: async (input) =>
|
|
ok({
|
|
...sampleArmReport(input.path, input.vector_table_offset),
|
|
active_arm_entry_count: 1,
|
|
}),
|
|
};
|
|
const res = await new FirmwareVectorTableService(mockPort).inspect({
|
|
path: "/tmp/stm32.bin",
|
|
architecture: "armv7-m",
|
|
base_address: "0x08000000",
|
|
});
|
|
expect(res.ok).toBe(false);
|
|
if (res.ok) return;
|
|
expect(res.error.message).toContain("malformed output");
|
|
});
|
|
|
|
it("records sha256-bound Evidence, file-offset-range location, hashed svd_xml provenance, and omits defaulted controls", async () => {
|
|
const mockPort: FirmwareVectorTablePort = {
|
|
identity: FIRMWARE_VECTOR_TABLE_PROVIDER,
|
|
inspect: async (input) =>
|
|
ok(sampleArmReport(input.path, input.vector_table_offset)),
|
|
};
|
|
|
|
const service = new FirmwareVectorTableService(mockPort);
|
|
const res = await service.inspect({
|
|
path: "/tmp/stm32.bin",
|
|
architecture: "armv7-m",
|
|
base_address: "0x08000000",
|
|
svd_xml: "<device><peripherals/></device>",
|
|
});
|
|
|
|
expect(res.ok).toBe(true);
|
|
if (!res.ok) return;
|
|
expect(res.value.operation).toBe("inspect_firmware_vector_table");
|
|
expect(res.value.subject.identity).toMatchObject({
|
|
kind: "file",
|
|
sha256: "e".repeat(64),
|
|
});
|
|
expect(res.value.parameters).toEqual({
|
|
path: "/tmp/stm32.bin",
|
|
architecture: "armv7-m",
|
|
base_address: "0x08000000",
|
|
svd_xml_sha256: expect.stringMatching(/^[0-9a-f]{64}$/u),
|
|
});
|
|
expect(res.value.locations).toEqual([
|
|
{ kind: "artifact-path", path: "/tmp/stm32.bin" },
|
|
{ kind: "file-offset-range", start: 0, end: 16 },
|
|
]);
|
|
expect(res.value.limitations).toEqual([
|
|
"Decoded 4 slots up to max_entries.",
|
|
"Inline svd_xml UTF-8 text is not retained in Evidence and is unavailable from this record; parameters.svd_xml_sha256 records the SHA-256 digest of the caller-supplied svd_xml.",
|
|
]);
|
|
|
|
const withoutSvd = await service.inspect({
|
|
path: "/tmp/stm32.bin",
|
|
architecture: "armv7-m",
|
|
base_address: "0x08000000",
|
|
});
|
|
expect(withoutSvd.ok).toBe(true);
|
|
if (!withoutSvd.ok) return;
|
|
expect(withoutSvd.value.limitations).toEqual([
|
|
"Decoded 4 slots up to max_entries.",
|
|
]);
|
|
});
|
|
|
|
it("retains completed Evidence and caller selections on late cancellation", async () => {
|
|
const controller = new AbortController();
|
|
const reason = { source: "vector table completion" };
|
|
const report = sampleArmReport("/tmp/stm32.bin", 0);
|
|
const service = new FirmwareVectorTableService({
|
|
identity: FIRMWARE_VECTOR_TABLE_PROVIDER,
|
|
inspect: async () => {
|
|
controller.abort(reason);
|
|
return ok(report);
|
|
},
|
|
});
|
|
|
|
const result = await service.inspect(
|
|
{
|
|
path: "/tmp/stm32.bin",
|
|
architecture: "armv7-m",
|
|
base_address: "0x08000000",
|
|
},
|
|
{ signal: controller.signal },
|
|
);
|
|
if (result.ok) throw new Error("Expected late cancellation");
|
|
expect(result.error).toBeInstanceOf(AnalysisCancelledError);
|
|
expect(result.error.cause).toBe(reason);
|
|
expect(result.error.partialObservation).toMatchObject({
|
|
operation: "inspect_firmware_vector_table",
|
|
parameters: {
|
|
path: "/tmp/stm32.bin",
|
|
architecture: "armv7-m",
|
|
base_address: "0x08000000",
|
|
},
|
|
normalized_result: report,
|
|
});
|
|
});
|
|
|
|
it("propagates parameter limit refusals with control_kind: parameter", async () => {
|
|
const mockPort: FirmwareVectorTablePort = {
|
|
identity: FIRMWARE_VECTOR_TABLE_PROVIDER,
|
|
inspect: async (input) =>
|
|
err(
|
|
parseLimitConstraint(
|
|
"inspect_firmware_vector_table",
|
|
"file-size",
|
|
decodeLimit(
|
|
"firmware-vector-table",
|
|
"File exceeds max_file_bytes",
|
|
{
|
|
name: "max_file_bytes",
|
|
maximum: input.max_file_bytes,
|
|
},
|
|
),
|
|
{
|
|
retryable: false,
|
|
limit: firmwareVectorBudgetLimit(
|
|
"max_file_bytes",
|
|
input.max_file_bytes,
|
|
),
|
|
},
|
|
),
|
|
),
|
|
};
|
|
const service = new FirmwareVectorTableService(mockPort);
|
|
const res = await service.inspect({
|
|
path: "/tmp/huge.bin",
|
|
architecture: "armv7-m",
|
|
base_address: "0x08000000",
|
|
max_file_bytes: 16,
|
|
});
|
|
expect(res.ok).toBe(false);
|
|
if (res.ok) return;
|
|
expect(res.error.limit).toMatchObject({
|
|
control_kind: "parameter",
|
|
parameter: "max_file_bytes",
|
|
effective_value: 16,
|
|
});
|
|
});
|
|
});
|