From 24f37becf57dee71a46ea885e9398de591ab32d5 Mon Sep 17 00:00:00 2001 From: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com> Date: Thu, 3 Dec 2026 12:59:48 +0000 Subject: [PATCH] fix(firmware): group a page's MMIO accesses by region kind A 4 KiB page can now hold SVD-covered addresses classified svd_peripheral and uncovered ones classified by bus convention. Page groups were keyed by address alone and took their region kind and basis from the first access, so a region_kind group filter depended on access order and could hide matching accesses. Page identities now include the region kind, in both the grouping index and the literal-pool pre-allocation measurement, so each kind forms its own group with its own basis. Co-Authored-By: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com> --- .../firmware/firmwareLiteralPoolScanner.ts | 1 + .../firmware/firmwareLiteralPools.test.ts | 47 +++++++++++++++++++ src/domain/firmware/firmwareMmioGroups.ts | 37 +++++++++++---- 3 files changed, 75 insertions(+), 10 deletions(-) diff --git a/src/domain/firmware/firmwareLiteralPoolScanner.ts b/src/domain/firmware/firmwareLiteralPoolScanner.ts index 9f36702c75a..4fbe028f45a 100644 --- a/src/domain/firmware/firmwareLiteralPoolScanner.ts +++ b/src/domain/firmware/firmwareLiteralPoolScanner.ts @@ -245,6 +245,7 @@ const classificationGroup = ( ? null : BigInt(classified.mmioBaseAddress), architecturalBlock: classified.architecturalBlock, + regionKind: classified.regionKind, }); /** diff --git a/src/domain/firmware/firmwareLiteralPools.test.ts b/src/domain/firmware/firmwareLiteralPools.test.ts index db5bc879bf7..5a44216a859 100644 --- a/src/domain/firmware/firmwareLiteralPools.test.ts +++ b/src/domain/firmware/firmwareLiteralPools.test.ts @@ -1309,4 +1309,51 @@ describe("firmwareLiteralPools domain scanner", () => { ]); expect(ambiguous.svd_profile.matched_access_count).toBe(0); }); + + it("splits one page into a group per region kind when SVD covers only part of it", () => { + // Thumb `ldr r0, [pc, #4]` and `ldr r1, [pc, #8]` load 0x40000004 and 0x40000104. + const image = Buffer.alloc(16, 0x00); + image.writeUInt16LE(0x4801, 0); + image.writeUInt16LE(0x4902, 2); + image.writeUInt16LE(0xbf00, 4); + image.writeUInt16LE(0xbf00, 6); + image.writeUInt32LE(0x4000_0004, 8); + image.writeUInt32LE(0x4000_0104, 12); + const block = (name: string) => + `${name}0x400000000x00x100registers`; + const svd = parseSvdPeripheralSpans( + `${block("TIM2")}${block("TIM3")}`, + ); + if (!svd.ok) throw new Error("expected SVD"); + const res = scanFirmwareLiteralPools({ + artifact: { path: "/tmp/thumb.bin", sha256: "f".repeat(64), bytes: 16 }, + fileBytes: image, + architecture: "arm-thumb", + baseAddressHex: "0x08000000", + scanOffset: 0, + maxScanBytes: 16, + mmioOnly: true, + svdSupplied: true, + svdProfile: svd.value, + }); + if (!res.ok) throw new Error("expected scan"); + expect(res.value.accesses.map((row) => row.region_kind)).toEqual([ + "svd_peripheral", + "cortex_m_peripheral", + ]); + expect(res.value.peripheral_groups).toMatchObject([ + { + base_address: "0x40000000", + region_kind: "cortex_m_peripheral", + basis: "address_map_convention", + distinct_addresses: ["0x40000104"], + }, + { + base_address: "0x40000000", + region_kind: "svd_peripheral", + basis: "explicit_profile", + distinct_addresses: ["0x40000004"], + }, + ]); + }); }); diff --git a/src/domain/firmware/firmwareMmioGroups.ts b/src/domain/firmware/firmwareMmioGroups.ts index 45a96b5310e..d4e80032fd8 100644 --- a/src/domain/firmware/firmwareMmioGroups.ts +++ b/src/domain/firmware/firmwareMmioGroups.ts @@ -5,6 +5,7 @@ import { isMmioRegionKind, mmioAttributionBasis, type FirmwareMmioAccess, + type FirmwareMmioRegionKind, type FirmwareMmioPeripheralGroup, } from "./firmwareMmio.js"; @@ -30,20 +31,29 @@ interface GroupAccumulator { readonly samples: string[]; } -/** Grouping identity of one MMIO access: SVD peripheral, architectural block, or 4 KiB page. */ +/** + * Grouping identity of one MMIO access: SVD peripheral, architectural block, + * or 4 KiB page of one region kind, since a page can hold SVD-covered and + * convention-classified addresses. + */ export type MmioGroupIdentity = | { readonly kind: "svd" | "block"; readonly name: string; readonly base: bigint; } - | { readonly kind: "page"; readonly base: bigint }; + | { + readonly kind: "page"; + readonly base: bigint; + readonly regionKind: FirmwareMmioRegionKind; + }; export const mmioGroupIdentity = (access: { readonly address: bigint; readonly peripheralName: string | null; readonly peripheralBase: bigint | null; readonly architecturalBlock: string | null; + readonly regionKind: FirmwareMmioRegionKind; }): MmioGroupIdentity => { if (access.peripheralName !== null && access.peripheralBase !== null) { return { @@ -54,7 +64,7 @@ export const mmioGroupIdentity = (access: { } const page = access.address & PAGE_MASK; return access.architecturalBlock === null - ? { kind: "page", base: page } + ? { kind: "page", base: page, regionKind: access.regionKind } : { kind: "block", name: access.architecturalBlock, base: page }; }; @@ -64,24 +74,30 @@ export const mmioGroupIdentity = (access: { */ export class MmioGroupIndex { readonly #named = { svd: new Map(), block: new Map() }; - readonly #pages = new Map(); + readonly #pages = new Map>(); get(identity: MmioGroupIdentity): T | undefined { return identity.kind === "page" - ? this.#pages.get(identity.base) + ? this.#pages.get(identity.regionKind)?.get(identity.base) : this.#named[identity.kind].get(identity.name); } set(identity: MmioGroupIdentity, value: T): void { - if (identity.kind === "page") this.#pages.set(identity.base, value); - else this.#named[identity.kind].set(identity.name, value); + if (identity.kind === "page") { + let pages = this.#pages.get(identity.regionKind); + if (pages === undefined) { + pages = new Map(); + this.#pages.set(identity.regionKind, pages); + } + pages.set(identity.base, value); + } else this.#named[identity.kind].set(identity.name, value); } values(): T[] { return [ ...this.#named.svd.values(), ...this.#named.block.values(), - ...this.#pages.values(), + ...[...this.#pages.values()].flatMap((pages) => [...pages.values()]), ]; } } @@ -97,6 +113,7 @@ const groupAccess = (access: FirmwareMmioAccess) => peripheralBase: access.peripheral_base === null ? null : BigInt(access.peripheral_base), architecturalBlock: access.architectural_block, + regionKind: access.region_kind, }); const groupBytes = ( @@ -211,8 +228,8 @@ export const buildMmioPeripheralGroups = ( b.access_count - a.access_count || compareBigints(BigInt(a.base_address), BigInt(b.base_address)) || compareUnicodeCodePoints( - a.peripheral_name ?? a.architectural_block ?? "", - b.peripheral_name ?? b.architectural_block ?? "", + a.peripheral_name ?? a.architectural_block ?? a.region_kind, + b.peripheral_name ?? b.architectural_block ?? b.region_kind, ), ); return groups;