diff --git a/src/domain/firmware/firmwareLiteralPoolRegions.ts b/src/domain/firmware/firmwareLiteralPoolRegions.ts index b7e5022fa0f..1b5faf2071c 100644 --- a/src/domain/firmware/firmwareLiteralPoolRegions.ts +++ b/src/domain/firmware/firmwareLiteralPoolRegions.ts @@ -56,6 +56,8 @@ export type SvdPeripheralParseError = export interface ConstantClassification { readonly regionKind: FirmwareMmioRegionKind; + /** Competing equal-width CMSIS-SVD peripherals cover the constant, so none is named. */ + readonly svdAmbiguous: boolean; readonly architecturalBlock: string | null; readonly svdPeripheral: string | null; readonly svdRegisterOffset: string | null; @@ -2085,10 +2087,15 @@ const classifyCortexMPpbBlock = (value: number): string | null => { return null; }; +/** A unique CMSIS-SVD peripheral at an address, or coverage by competing equal-width peripherals. */ +export type LiteralPoolSvdMatch = + | { readonly name: string; readonly baseAddress: number } + | "ambiguous"; + export const findSvdMatch = ( value: number, svdProfile: SvdPeripheralProfile, -): { readonly name: string; readonly baseAddress: number } | undefined => { +): LiteralPoolSvdMatch | undefined => { const segments = svdProfile.segments; let low = 0; let high = segments.length - 1; @@ -2100,7 +2107,7 @@ export const findSvdMatch = ( } else if (value >= seg.endAddress) { low = mid + 1; } else { - return seg.match ?? undefined; + return seg.match ?? "ambiguous"; } } return undefined; @@ -2110,6 +2117,7 @@ const nonMmioClassification = ( regionKind: FirmwareMmioRegionKind, ): ConstantClassification => ({ regionKind, + svdAmbiguous: false, architecturalBlock: null, svdPeripheral: null, svdRegisterOffset: null, @@ -2125,6 +2133,7 @@ const classifyExplicitSvdMatch = ( if (isa === "arm-thumb" && u32 >= 0xe000_0000 && u32 <= 0xe00f_ffff) { return { regionKind: "cortex_m_ppb_scs", + svdAmbiguous: false, architecturalBlock: classifyCortexMPpbBlock(u32), svdPeripheral: svdMatch.name, svdRegisterOffset, @@ -2139,6 +2148,7 @@ const classifyExplicitSvdMatch = ( : "svd_peripheral"; return { regionKind, + svdAmbiguous: false, architecturalBlock: null, svdPeripheral: svdMatch.name, svdRegisterOffset, @@ -2153,6 +2163,7 @@ const classifyThumbArchitecturalFallback = ( if (u32 >= 0xe000_0000 && u32 <= 0xe00f_ffff) { return { regionKind: "cortex_m_ppb_scs", + svdAmbiguous: false, architecturalBlock: classifyCortexMPpbBlock(u32), svdPeripheral: null, svdRegisterOffset: null, @@ -2162,6 +2173,7 @@ const classifyThumbArchitecturalFallback = ( if (u32 >= 0x4000_0000 && u32 <= 0x5fff_ffff) { return { regionKind: "cortex_m_peripheral", + svdAmbiguous: false, architecturalBlock: null, svdPeripheral: null, svdRegisterOffset: null, @@ -2183,6 +2195,7 @@ const classifyXtensaArchitecturalFallback = ( if (isEspPeriphBus) { return { regionKind: "esp_peripheral", + svdAmbiguous: false, architecturalBlock: null, svdPeripheral: null, svdRegisterOffset: null, @@ -2218,6 +2231,23 @@ export const classifyLoadedConstant = (options: { }): ConstantClassification => { const u32 = options.value >>> 0; const svdMatch = findSvdMatch(u32, options.svdProfile); + if (svdMatch === "ambiguous") { + // The SVD establishes peripheral space even when it cannot name one owner. + const ppb = + options.isa === "arm-thumb" + ? classifyThumbArchitecturalFallback(u32) + : null; + return ppb?.regionKind === "cortex_m_ppb_scs" + ? { ...ppb, svdAmbiguous: true } + : { + regionKind: "svd_peripheral", + svdAmbiguous: true, + architecturalBlock: null, + svdPeripheral: null, + svdRegisterOffset: null, + mmioBaseAddress: (u32 & 0xffff_f000) >>> 0, + }; + } if (svdMatch !== undefined) { return classifyExplicitSvdMatch(u32, options.isa, svdMatch); } diff --git a/src/domain/firmware/firmwareLiteralPoolScanner.ts b/src/domain/firmware/firmwareLiteralPoolScanner.ts index 5857b46925a..9f36702c75a 100644 --- a/src/domain/firmware/firmwareLiteralPoolScanner.ts +++ b/src/domain/firmware/firmwareLiteralPoolScanner.ts @@ -230,6 +230,9 @@ interface CandidateScanContext { readonly mmioOnly: boolean; } +const ambiguousSvdAnomaly = (address: number): string => + `Loaded constant ${formatHexAddress(address)} lies in more than one equal-width CMSIS-SVD peripheral; peripheral_name remains null.`; + const classificationGroup = ( value: number, classified: ConstantClassification, @@ -245,7 +248,8 @@ const classificationGroup = ( }); /** - * Measure the UTF-8 bytes of the access rows a collection pass would emit, plus an upper bound for each peripheral group they form. Name + * Measure the UTF-8 bytes of the access rows and anomalies a collection pass + * would emit, plus an upper bound for each peripheral group they form. Name * byte lengths are computed once per name, not once per access. */ function* measureRepresentationBytesSteps( @@ -268,6 +272,7 @@ function* measureRepresentationBytesSteps( return bytes; }; const groups = new MmioGroupIndex(); + const ambiguous = new Set(); let totalBytes = BASE_REPORT_JSON_BYTES; // not-an-allowance: cooperative byte accumulator for yielding during representation measurement let bytesSinceYield = 0; @@ -291,6 +296,10 @@ function* measureRepresentationBytesSteps( BASE_ACCESS_JSON_BYTES + utf8Length(classified.svdPeripheral) + utf8Length(classified.architecturalBlock); + if (classified.svdAmbiguous && !ambiguous.has(loadedValue)) { + ambiguous.add(loadedValue); + rowBytes += Buffer.byteLength(ambiguousSvdAnomaly(loadedValue)) + 8; + } if (isMmio) { const identity = classificationGroup(loadedValue, classified); if (groups.get(identity) === undefined) { @@ -314,6 +323,8 @@ function* collectAccessesSteps(context: CandidateScanContext) { svdProfile: context.svdProfile, }); const accesses: FirmwareMmioAccess[] = []; + const anomalies: string[] = []; + const ambiguous = new Set(); const distinctSlots = new Set(); // not-an-allowance: tally of literal-pool loads classified as MMIO let mmioCount = 0; @@ -347,6 +358,10 @@ function* collectAccessesSteps(context: CandidateScanContext) { const isMmio = isMmioRegionKind(classified.regionKind); if (isMmio) mmioCount += 1; if (context.mmioOnly && !isMmio) continue; + if (classified.svdAmbiguous && !ambiguous.has(cand.loadedValue)) { + ambiguous.add(cand.loadedValue); + anomalies.push(ambiguousSvdAnomaly(cand.loadedValue)); + } bytesSinceYield += BASE_ACCESS_JSON_BYTES + (classified.svdPeripheral?.length ?? 0) + @@ -380,6 +395,7 @@ function* collectAccessesSteps(context: CandidateScanContext) { return { accesses, + anomalies, distinctSlotCount: distinctSlots.size, mmioCount, svdMatchedCount, @@ -518,7 +534,7 @@ export function* scanFirmwareLiteralPoolsSteps( }, accesses: collected.accesses, peripheral_groups: peripheralGroups, - anomalies: [], + anomalies: collected.anomalies, limitations: buildScanLimitations({ architecture: options.architecture, scanTruncated, diff --git a/src/domain/firmware/firmwareLiteralPools.test.ts b/src/domain/firmware/firmwareLiteralPools.test.ts index bdb9218f9dc..db5bc879bf7 100644 --- a/src/domain/firmware/firmwareLiteralPools.test.ts +++ b/src/domain/firmware/firmwareLiteralPools.test.ts @@ -363,6 +363,7 @@ describe("firmwareLiteralPools domain scanner", () => { }); expect(profiled).toEqual({ regionKind: "svd_peripheral", + svdAmbiguous: false, architecturalBlock: null, svdPeripheral: "SYSCTL", svdRegisterOffset: "0x4", @@ -1253,4 +1254,59 @@ describe("firmwareLiteralPools domain scanner", () => { }); } }); + + it("keeps literal-pool constants that competing equal-width SVD peripherals cover, unnamed with an anomaly", () => { + // A32 `ldr r0, [pc, #0]` at 0 loads the word at 8 (0x1c200004). + const image = Buffer.alloc(12, 0x00); + image.writeUInt32LE(0xe59f_0000, 0); + image.writeUInt32LE(0xe1a0_0000, 4); + image.writeUInt32LE(0x1c20_0004, 8); + const block = (name: string) => + `${name}0x1c2000000x00x100registers`; + const scan = (peripherals: string) => { + const svd = parseSvdPeripheralSpans( + `${peripherals}`, + ); + if (!svd.ok) throw new Error("expected SVD"); + const res = scanFirmwareLiteralPools({ + artifact: { path: "/tmp/a32.bin", sha256: "c".repeat(64), bytes: 12 }, + fileBytes: image, + architecture: "arm-a32", + baseAddressHex: "0x00000000", + scanOffset: 0, + maxScanBytes: 12, + mmioOnly: true, + svdSupplied: true, + svdProfile: svd.value, + }); + if (!res.ok) throw new Error("expected scan"); + return res.value; + }; + const named = scan(block("TIMER0")); + expect(named.accesses).toMatchObject([ + { address: "0x1c200004", peripheral_name: "TIMER0" }, + ]); + expect(named.anomalies).toEqual([]); + + const ambiguous = scan(block("TIMER0") + block("TIMER1")); + expect(ambiguous.accesses).toMatchObject([ + { + address: "0x1c200004", + region_kind: "svd_peripheral", + peripheral_name: null, + register_offset: null, + }, + ]); + expect(ambiguous.anomalies).toEqual([ + "Loaded constant 0x1c200004 lies in more than one equal-width CMSIS-SVD peripheral; peripheral_name remains null.", + ]); + expect(ambiguous.peripheral_groups).toMatchObject([ + { + base_address: "0x1c200000", + region_kind: "svd_peripheral", + basis: "explicit_profile", + }, + ]); + expect(ambiguous.svd_profile.matched_access_count).toBe(0); + }); });