Skip to content

fix(svdconv): guard bit masks and bound dimension expansion - #2659

Merged
thorstendb-ARM merged 32 commits into
mainfrom
fix/svdconv-input-bounds
Oct 21, 2026
Merged

thorstendb-ARM merged 32 commits into
mainfrom
fix/svdconv-input-bounds

Conversation

@thorstendb-ARM

@thorstendb-ARM thorstendb-ARM commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

SVDConv could overflow or sign-extend field masks, wrap dimension indices and addresses, and create excessive
model copies for nested dimensions. This change validates supported 32-bit register and field ranges before
generation, uses overflow-safe arithmetic, and bounds model expansion before output.

Related to #2647 and the security and code-quality tracking issue.

Changes

  • Validate raw model offsets, signed widths and their combined range with SvdUtils::GetBitRange32() before
    narrowing. Preserve existing 32-bit generator interfaces and formatting, raw getter contracts and unset sentinels.
    Existing internal 64-bit preparation remains; this PR does not add 64-bit input/output support.
  • Generate unsigned masks safely for bit 31 and full 32-bit fields. Check field ordering and register bounds before
    calculating reserved padding. Bound automatically generated enum lists with MAX_BITWIDTH_FOR_COMBO before shifting.
  • Use wide local arithmetic for dimension indices and strides, and validate the last offset before copying.
    Preserve the existing dimIncrement unit interpretation.
  • Limit generated indices and model-copy operations cumulatively to 1,000,000 per input, including nested dimensions
    and inherited content. M249 aborts before any generator runs; M250 reports invalid or overflowing dimension ranges.
    This bounds expansion work, not XML size or total process memory.
  • Validate SFD ranges and expression references before file creation. Reject an entire condition if an operand is
    invalid, handle missing inherited child containers, and propagate output-opening failures so SFR compilation
    cannot consume a stale SFD.
  • Add regression tests for boundary values, header output and padding, expansion failure propagation and partial
    models, and failed/stale SFD generation. Document the checked range boundary and its possible model-level cleanup
    in tools/svdconv/docs/todo.md.

Validation

For 35b0cac3:

  • A fresh macOS arm64 Release build passes 231 active unit tests and 49 integration tests.
    One existing unit test remains disabled. Header regressions cover overwide fields, overlaps and valid padding.
  • Expansion tests cover the one-million boundary, shared sibling and nested-copy budgets, repeated expansion,
    rejection before generation, and successful conversion of the next input. Test dimensions derive from the limit.
  • The complete Release conversion at 50a6cba7 processed 404 installed SVD paths, producing 404 nonempty headers
    and 403 nonempty SFD files
    , with no crashes or timeouts. Generated pack headers were not compiled, and SFD files
    were not loaded into a debugger.
  • Platform builds, tests and coverage: SVDConv CI.

ARM/V2M_MPS3_SSE_300_BSP/1.5.0/SVD/SSE300.svd previously generated an incorrect SFD: its 64-bit CNTCV
registers used unsigned int declarations and zero masks (& 0x0). The new check reports M226 and prevents that
incorrect SFD. The header already omitted these registers, replacing their address ranges with reserved space;
header generation still completes.

Checklist

  • Unit and integration tests cover the changes.
  • Release conversion completed; unsupported-input behavior documented.
  • Arithmetic reviewed for the supported 32-bit ranges and overflow-safe intermediate calculations.
  • Limits, diagnostics and the range-accessor follow-up documented.
  • No dependency changes.

@thorstendb-ARM thorstendb-ARM added bug Something isn't working svdconv Issues related to svdconv labels Oct 15, 2026
@github-actions

github-actions Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Test Results

  4 files   -    41   40 suites   - 121   40s ⏱️ - 18m 45s
281 tests  - 1 384  281 ✅  - 1 367  0 💤  - 17  0 ❌ ±0 
560 runs   - 3 453  560 ✅  - 3 384  0 💤  - 69  0 ❌ ±0 

Results for commit 43c3f60. ± Comparison against base commit 8f4936a.

This pull request removes 1453 and adds 69 tests. Note that renamed tests count towards both.
AuxCmdTests ‑ MkdirCmdTest
AuxCmdTests ‑ RmdirCmdTest
AuxCmdTests ‑ TouchCmdTest
BuildSystemGeneratorTests ‑ GenAuditFile
BuildSystemGeneratorTests ‑ GenAuditFile_WithOut_Existing_Audit_File
BuildSystemGeneratorTests ‑ GenAuditFile_With_Existing_Audit_File
BuildSystemGeneratorTests ‑ GetString
BuildSystemGeneratorTests ‑ StrConv
BuildSystemGeneratorTests ‑ StrNorm
CBuildGCCTests ‑ Asm
…
SvdBitWidthTest ‑ AccessMaskSkipsInvalidAndUncheckedFieldRanges
SvdBitWidthTest ‑ BitMasksUseWideIntermediatesWithoutAcceptingInvalidRanges
SvdBitWidthTest ‑ EnumValuesAboveTheFieldWidthRemainInvalid
SvdBitWidthTest ‑ ExpressionLookupRejectsPeripheralsWithoutRegisterContainers
SvdBitWidthTest ‑ ExpressionLookupRejectsRegistersWithoutFieldContainers
SvdBitWidthTest ‑ FieldExpressionsPreserve32BitBoundaryTextAndRejectWiderRanges
SvdBitWidthTest ‑ FieldValidationHandlesMissingRegisterAncestors
SvdBitWidthTest ‑ FieldsOutsideOffsetAndRegisterBoundsAreRejected
SvdBitWidthTest ‑ FullWidth32BitRegisterWithoutFieldsPreservesAllAccessBits
SvdBitWidthTest ‑ FullWidth32BitWriteMaskDoesNotSignExtend
…

♻️ This comment has been updated with latest results.

@thorstendb-ARM
thorstendb-ARM marked this pull request as ready for review October 15, 2026 14:23
Comment thread tools/svdconv/test/unittests/src/SvdBitWidthTest.cpp Fixed

This comment was marked as outdated.

Propagate SFD file creation failures and validate actual expression operands before opening output. Discard the entire condition when an operand cannot be generated.

Add regressions for invalid field and register operands, failed output creation, and stale SFD preservation. Release and UBSan pass 210 active unit tests and 48 integration tests.
Retain checked wide intermediate arithmetic while restoring 32-bit header interfaces and field expressions. Remove the added 64-bit default-mask extension and document the Cortex-M scope.

Cover 32-bit output boundaries, rejected wider ranges and full-width write-mask sign extension. Release and UBSan each pass 211 active unit tests and 48 integration tests.
Reject references through absent register or field containers before accessing the first child. Invalid inherited fields can leave these containers empty and trigger an end-iterator assertion on MSVC.

Add regression coverage for both register and peripheral lookup paths.
Check raw model offsets and signed widths against the 32-bit header range before converting them. Insert fields without replacing existing map entries, and cover negative widths, large aliasing offsets, and overlapping field groups.
Comment thread tools/svdconv/test/unittests/src/SvdBitWidthTest.cpp Fixed
thorstendb-ARM and others added 5 commits October 20, 2026 13:31
…s check'

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Keep field positions and masks wide across grouping, padding, generator interfaces and formatting, while preserving the 32-bit output range. Accept signed widths in the mask helper without narrowing and guard unsigned padding calculations.

Remove redundant enum validation and successful 64-bit input assumptions from tests. Restore the already-correct reset-value mask calculation and retain all required expansion and output-failure checks.
Comment thread tools/svdconv/SVDGenerator/src/HeaderData_Field.cpp Fixed
@thorstendb-ARM

Copy link
Copy Markdown
Contributor Author

The Copilot review from 20 October applies to commit 93c33b3. Both findings were addressed in 2ddda56e and remain addressed at the current head, 35b0cac3:

  • Output creation failure: return failure before generating output, preventing SFR compilation from consuming a stale SFD.
  • Invalid field references: validate referenced operands before opening the output and reject the entire invalid condition.

Both threads are resolved and have regression-test details in the linked replies. The old overview still says "2 open issues", so it has been collapsed as outdated; its original text remains available. This is a status clarification, not a new Copilot assessment. A new Copilot review is currently unavailable because GitHub reports "Monthly limit reached".

@thorstendb-ARM
thorstendb-ARM merged commit b7c824c into main Oct 21, 2026
18 checks passed
@thorstendb-ARM
thorstendb-ARM deleted the fix/svdconv-input-bounds branch October 21, 2026 12:38
@codecov

codecov Bot commented Oct 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.45%. Comparing base (48a66a5) to head (6e032f4).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2659      +/-   ##
==========================================
+ Coverage   86.30%   87.45%   +1.15%     
==========================================
  Files         207      207              
  Lines       33322    33533     +211     
==========================================
+ Hits        28757    29325     +568     
+ Misses       4565     4208     -357     
Flag Coverage Δ
svdconv-cov 74.62% <100.00%> (+3.86%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
tools/svdconv/SVDConv/src/SVDConv.cpp 68.11% <100.00%> (+2.27%) ⬆️
...ools/svdconv/SVDGenerator/src/HeaderData_Field.cpp 89.87% <100.00%> (+89.87%) ⬆️
...ls/svdconv/SVDGenerator/src/HeaderData_PosMask.cpp 70.27% <100.00%> (ø)
tools/svdconv/SVDGenerator/src/SfdData.cpp 97.10% <100.00%> (+9.60%) ⬆️
...s/svdconv/SVDGenerator/src/SfdData_SingleItems.cpp 64.44% <100.00%> (+1.08%) ⬆️
tools/svdconv/SVDGenerator/src/SvdGenerator.cpp 95.33% <100.00%> (ø)
tools/svdconv/SVDModel/include/SvdDimension.h 100.00% <ø> (ø)
tools/svdconv/SVDModel/include/SvdItem.h 80.24% <100.00%> (+1.76%) ⬆️
tools/svdconv/SVDModel/include/SvdRegister.h 100.00% <ø> (ø)
tools/svdconv/SVDModel/include/SvdUtils.h 100.00% <ø> (ø)
... and 11 more

... and 12 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working svdconv Issues related to svdconv

Projects

Status: In review

Development

Successfully merging this pull request may close these issues.

4 participants