mirror of
https://github.com/morluto/rea.git
synced 2026-12-04 05:51:06 +00:00
fix(firmware): keep literal-pool constants that competing SVD peripherals cover
When two equal-width CMSIS-SVD peripherals covered a loaded constant, the literal-pool classifier treated it as outside the profile. For arm-a32 it became `other`, and the default mmio_only filter dropped the access, even though the supplied SVD declares that address as peripheral space. findSvdMatch now reports ambiguous coverage separately from no coverage. Such a constant is classified as svd_peripheral, or as the Cortex-M PPB on arm-thumb, with no peripheral name. Each such address adds one anomaly, charged in the representation measurement, matching how split-immediate architectures report competing peripherals. Co-Authored-By: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>
This commit is contained in:
1 parent
3d41ed79ac
commit
505fc0cdb7
3 files changed
+106
-4
No files matched your search
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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<true>();
|
||||
const ambiguous = new Set<number>();
|
||||
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<number>();
|
||||
const distinctSlots = new Set<number>();
|
||||
// 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,
|
||||
|
||||
@@ -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) =>
|
||||
`<peripheral><name>${name}</name><baseAddress>0x1c200000</baseAddress><addressBlock><offset>0x0</offset><size>0x100</size><usage>registers</usage></addressBlock></peripheral>`;
|
||||
const scan = (peripherals: string) => {
|
||||
const svd = parseSvdPeripheralSpans(
|
||||
`<device><peripherals>${peripherals}</peripherals></device>`,
|
||||
);
|
||||
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);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user