Skip to content

Conversation

@sensei-hacker
Copy link
Member

@sensei-hacker sensei-hacker commented Jan 23, 2026

User description

Summary

Implements automated validation of parameter group (PG) struct sizes during firmware builds to prevent settings corruption from struct layout changes without proper PG version increments.

This complements the existing GitHub Action PR check (#11278) with compile-time validation that catches issues during local development builds.

Changes

Core Validation System

  • check-pg-struct-sizes.sh: Build-time validation script that:

    • Extracts current struct sizes from compiled binary
    • Compares against database of known sizes + versions
    • Fails build if size changed but PG version wasn't incremented
    • Auto-updates database when version IS properly incremented
  • extract-pg-sizes-nm.sh: Extracts PG struct sizes using nm (no extra dependencies)

  • pg_struct_sizes.db: Database of 44 PG structs with current sizes and versions

Build Integration

  • Modified cmake/main.cmake to run validation as POST_BUILD step on all targets
  • Runs for SITL and all hardware targets
  • Uses cmake -E chdir to ensure correct working directory

Testing

Verified all scenarios work correctly:

  1. ✅ No changes: Validation passes, database unchanged
  2. ✅ Size changed WITHOUT version increment: Build fails with clear error message, database unchanged
  3. ✅ Size changed WITH version increment: Build succeeds, database auto-updated with new size/version

Test procedure:

  • Modified barometerConfig_t struct by adding a field
  • Built without version increment → failed as expected with clear error
  • Incremented PG version from 5 to 6
  • Rebuilt → succeeded and auto-updated database entry
  • Verified database file shows updated size (12B) and version (6)

Related

#11236

Limitations

  • Size only, not layout: Cannot detect field reordering with same total size

PROBLEM:
PR iNavFlight#11276 modified blackboxConfig_s struct in blackbox.h without
incrementing version in blackbox.c, and the workflow didn't catch it.

ROOT CAUSE:
Script processed files independently:
- Checked blackbox.c → found PG_REGISTER but struct not in this file
- Checked blackbox.h → found struct change but no PG_REGISTER
- Never connected the two pieces together

SOLUTION:
1. Expand file list to include companions that contain PG_REGISTER
   (if blackbox.h changed, also check blackbox.c for PG_REGISTER)

2. Search ALL changed files for struct definitions
   (when checking PG_REGISTER in blackbox.c, search all changed
   files including blackbox.h for the struct)

3. Detect when struct changed but PG_REGISTER wasn't touched
   (empty old_version and new_version → version not incremented)

This correctly catches the most common bug: developer modifies struct
in header file but forgets to increment version in source file.
Implement automated validation of parameter group struct sizes during
firmware builds to prevent settings corruption from struct changes
without version increments.

Features:
- Validates all PG struct sizes against database after each build
- Fails build if size changed without PG version increment
- Auto-updates database when version IS properly incremented
- No extra dependencies (uses standard nm tool)
- Works on SITL and all hardware targets

Implementation:
- check-pg-struct-sizes.sh: Build-time validation script
- extract-pg-sizes-nm.sh: Extract sizes from binaries using nm
- generate-pg-database.sh: Generate database with sizes + versions
- pg_struct_sizes.db: Database of 44 PG structs with current sizes/versions
- Integrated into cmake/main.cmake as POST_BUILD step

Testing verified:
- Passes when no changes made
- Fails when size changed without version increment
- Passes and auto-updates when size changed with version increment
- Database unchanged when validation fails

Complements existing GitHub Action PR check with compile-time validation.
@qodo-code-review
Copy link
Contributor

qodo-code-review bot commented Jan 23, 2026

PR Compliance Guide 🔍

All compliance sections have been disabled in the configurations.

1. Fix bash runtime error: Remove 'local' keyword from top-level variable
   - .github/scripts/check-pg-versions.sh:190
   - Variable 'companion' was declared with 'local' outside of a function

2. Add auto-addition of new PG structs to database
   - cmake/check-pg-struct-sizes.sh
   - New structs with PG_REGISTER are automatically added to database
   - Tracks additions in update report

3. Improve architecture detection robustness
   - cmake/extract-pg-sizes-nm.sh:26
   - Check for arm-none-eabi-nm existence before use
   - Use more robust file type checking

Note: Keeping automatic database updates when version IS incremented
as per original design requirement.
Removed files not needed in public release:
- cmake/PG-SIZE-EXTRACTION-NM.md (internal process documentation)
- cmake/PG-SIZE-EXTRACTION-PAHOLE.md (alternative approach documentation)
- cmake/PG-STRUCT-VALIDATION.md (internal design documentation)
- cmake/generate-pg-database.sh (replaced by auto-add feature in check script)
- cmake/compare-pg-sizes-nm.sh (developer utility, not needed for builds)

Remaining files provide core build-time validation:
- cmake/check-pg-struct-sizes.sh (main validation script)
- cmake/extract-pg-sizes-nm.sh (size extraction utility)
- cmake/pg_struct_sizes.db (struct size database)
Removed:
- .github/scripts/check-pg-versions.sh (GitHub Action script)
- .github/workflows/pg-version-check.yml (GitHub Action workflow)

The build-time validation in cmake/check-pg-struct-sizes.sh provides
superior coverage:
- Catches issues during every build (not just PRs)
- Works for local development builds
- Auto-updates database when versions are incremented
- Auto-adds new PG structs to database
- Provides immediate developer feedback

The GitHub Action approach is redundant with build-time validation.
@sensei-hacker sensei-hacker deleted the ci/pg-version-check-action branch January 23, 2026 23:17
@sensei-hacker
Copy link
Member Author

Superseded by #11283 with clean commit history.

The merge conflict was caused by commits that added GitHub Action files and then later removed them. The new PR contains only the build-time validation commits without the GitHub Action approach.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant