Skip to content

fix: guard FreeRtosSysUpTime against tick configs that wrap early#634

Merged
DavidCozens merged 3 commits into
mainfrom
fix/freertos-sysuptime-tick-wrap-guard
Jul 20, 2026
Merged

fix: guard FreeRtosSysUpTime against tick configs that wrap early#634
DavidCozens merged 3 commits into
mainfrom
fix/freertos-sysuptime-tick-wrap-guard

Conversation

@DavidCozens

@DavidCozens DavidCozens commented Jul 20, 2026

Copy link
Copy Markdown
Collaborator

Closes #609 (S24.22).

The gap

SolidSyslogFreeRtosSysUpTime_Get is (uint64) xTaskGetTickCount() * 100 / configTICK_RATE_HZ cast to uint32. xTaskGetTickCount() returns the raw FreeRTOS tick counter, which wraps at TickType_t width. Below a 64-bit tick type — or a 32-bit one above 100 Hz — the scaled uptime wraps well before the RFC 3418 2³²-hundredths (~497-day) period and jumps backwards early (seconds-to-minutes under configUSE_16_BIT_TICKS).

The fix (option B — guard the faithful envelope)

A compile-time _Static_assert that refuses to build outside the envelope where the result is a faithful RFC 3418 modulo-2³² counter:

  • 64-bit TickType_t at any tick rate, or
  • 32-bit TickType_t with configTICK_RATE_HZ <= 100 (at 100 Hz the tick wrap lands exactly on the hundredths wrap).

A 16-bit tick counter, or 32-bit above 100 Hz, fails to build. The guard is width-agnostic (sizeof(TickType_t)), so it accepts a 64-bit integrator without depending on the newer configTICK_TYPE_WIDTH_IN_BITS macro. An integrator outside the envelope widens the tick type, lowers the rate, or supplies their own SolidSyslogSysUpTimeFunction.

The _Get doc-comment is updated from "not a full TimeTicks counter" to state the new RFC 3418 contract.

Why a guard, not a wrap-tracking extension

Discussed on the issue: the overflow-extension (a wrap counter) adds state to a currently-stateless getter for a payoff only 16-bit/high-rate integrators need. The guard turns a silent, subtly-wrong uptime into an informed build-time choice, and a faithful counter is an easy function for those integrators to provide.

Scope note

All repo FreeRTOS configs (test fake + both BDD targets) run 32-bit ticks at 100 Hz, so they sit inside the envelope and compile unchanged.

Verification

  • Red proven: temporarily flipping the fake config to 1000 Hz fails the build with the guard message; reverted.
  • Green under the supported config; SolidSyslogFreeRtosSysUpTimeTest 4/4 pass.
  • clang-format applied; cppcheck-MISRA clean (no new findings, no suppression line-shifts).

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved system uptime (TimeTicks) reporting to more faithfully follow RFC 3418 modulo-2^32 behaviour, including more accurate hundredths-of-seconds across tick wrap scenarios.
    • Adjusted the scaling calculation to reduce rounding issues across supported tick-rate configurations.
  • Documentation
    • Updated the uptime API documentation to clarify TimeTicks semantics and scaling constraints.
  • Chores
    • Added build-time validation to reject unsupported tick counter widths and invalid tick-rate settings.
  • Tests
    • Updated test build configuration include paths for the uptime tests.

xTaskGetTickCount() wraps at TickType_t width; below 64-bit ticks (or a
32-bit tick counter above 100 Hz) the scaled uptime wraps well before the
RFC 3418 2^32-hundredths period and jumps backwards. Add a _Static_assert
that refuses to build outside the faithful envelope — 64-bit ticks at any
rate, or 32-bit ticks with configTICK_RATE_HZ <= 100 — and update the
doc-comment to state the RFC 3418 contract. Integrators outside the
envelope widen the tick type, lower the rate, or supply their own
SolidSyslogSysUpTimeFunction.

Closes #609

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 8437d024-c440-49da-b4fb-b3f9b1277d77

📥 Commits

Reviewing files that changed from the base of the PR and between 6865759 and 5449452.

📒 Files selected for processing (3)
  • Platform/FreeRtos/Source/SolidSyslogFreeRtosSysUpTime.c
  • Tests/FreeRtos/CMakeLists.txt
  • misra_suppressions.txt

Walkthrough

FreeRTOS sysUpTime documentation now defines RFC 3418 modulo-2^32 TimeTicks behaviour. A compile-time guard rejects unsupported tick configurations, and runtime calculation separates whole-second and sub-second scaling.

Changes

FreeRTOS uptime compliance

Layer / File(s) Summary
Uptime contract, configuration guard, and scaling
Platform/FreeRtos/Interface/SolidSyslogFreeRtosSysUpTime.h, Platform/FreeRtos/Source/SolidSyslogFreeRtosSysUpTime.c, Tests/FreeRtos/CMakeLists.txt, misra_suppressions.txt
Documents supported TickType_t and tick-rate combinations, adds a SOLIDSYSLOG_STATIC_ASSERT for invalid configurations, replaces single-step scaling with divided-first hundredths calculation, and updates test include wiring and the MISRA suppression line.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarises the main change: adding a guard for FreeRTOS tick configs that would wrap too early.
Description check ✅ Passed The PR description covers purpose, change, testing, and affected areas, matching the repo template well enough.
Linked Issues check ✅ Passed [#609] The guard and doc update satisfy the issue's required compile-time rejection of unsupported tick-width/rate combinations.
Out of Scope Changes check ✅ Passed The include-path and MISRA suppression edits support the uptime change and do not appear unrelated.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/freertos-sysuptime-tick-wrap-guard

Comment @coderabbitai help to get the list of available commands.

@github-actions

Copy link
Copy Markdown
Contributor

☀️   Quality Summary

   🚦   build-linux-gcc: 100% successful (✔️ 1524 passed)
   🚦   build-freertos-host-tdd-plustcp: 100% successful (✔️ 1876 passed)
   🚦   build-linux-clang: 100% successful (✔️ 1455 passed)
   🚦   sanitize-linux-gcc: 100% successful (✔️ 1455 passed)
   🚦   integration-linux-openssl: 100% successful (✔️ 16 passed)
   🚦   integration-linux-mbedtls: 100% successful (✔️ 14 passed)
   🚦   integration-windows-openssl: 100% successful (✔️ 16 passed)
   🚦   bdd-linux-syslog-ng: 94% successful (✔️ 49 passed, 🙈 3 skipped)
   🚦   bdd-windows-otel: 88% successful (✔️ 46 passed, 🙈 6 skipped)
   🚦   bdd-freertos-qemu-plustcp: 87% successful (✔️ 45 passed, 🙈 7 skipped)
   🚦   bdd-freertos-qemu-lwip: 87% successful (✔️ 45 passed, 🙈 7 skipped)
   🚦   build-windows-msvc: 100% successful (✔️ 1300 passed)
   🚦   build-linux-tunable-override: 100% successful (✔️ 1455 passed)
   ⚠️   Clang-Tidy: No warnings
   ⚠️   CPPCheck: No warnings


Created by Quality Monitor v1.14.0 (#f3859fd). More details are shown in the GitHub Checks Result.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@Platform/FreeRtos/Source/SolidSyslogFreeRtosSysUpTime.c`:
- Around line 21-26: Add compile-only configuration cases covering the
_Static_assert in SolidSyslogFreeRtosSysUpTime: verify 16-bit TickType_t and
unsupported 32-bit tick-rate configurations fail, while supported 32-bit and
64-bit configurations pass. Ensure these cases exercise every assertion branch
and preserve the existing 100% line and branch coverage requirement.
- Around line 11-26: Narrow the uptime contract to tick rates that divide
HUNDREDTHS_PER_SECOND and prevent overflow in the 64-bit tick-to-hundredths
conversion. In SolidSyslogFreeRtosSysUpTime.c, update the static assertion and
conversion logic accordingly, using division-before-multiplication or wider
arithmetic; in Platform/FreeRtos/Interface/SolidSyslogFreeRtosSysUpTime.h lines
14-21, update the corresponding supported-configuration documentation or
declaration contract.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 41eb40ee-b141-4344-bf33-013cc916b6cb

📥 Commits

Reviewing files that changed from the base of the PR and between 5c13d4a and 1ed22c4.

📒 Files selected for processing (2)
  • Platform/FreeRtos/Interface/SolidSyslogFreeRtosSysUpTime.h
  • Platform/FreeRtos/Source/SolidSyslogFreeRtosSysUpTime.c

Comment thread Platform/FreeRtos/Source/SolidSyslogFreeRtosSysUpTime.c
Comment thread Platform/FreeRtos/Source/SolidSyslogFreeRtosSysUpTime.c Outdated
… scale overflow

Address CodeRabbit review on #634:

- The 32-bit faithful envelope is a tick rate that *divides* 100, not merely
  <= 100. A non-divisor rate (e.g. 40 Hz) injects a phase discontinuity at the
  tick-counter wrap (offset = 2^32*100/RATE mod 2^32 != 0), a reachable
  backwards jump. Guard now requires (100 % configTICK_RATE_HZ) == 0 on the
  32-bit path; 64-bit ticks stay any-rate (wrap is millions of years out).
- Divide the tick count before scaling by 100 (exact whole/remainder split) so
  the intermediate cannot overflow a 64-bit TickType_t.

Doc-comment updated to the divisor-of-100 contract.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Comment thread Platform/FreeRtos/Source/SolidSyslogFreeRtosSysUpTime.c Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@Platform/FreeRtos/Source/SolidSyslogFreeRtosSysUpTime.c`:
- Around line 12-21: Update the header comment around the TickType_t wrap
behavior to qualify the 64-bit guarantee: state that its 2^64-tick wrap is
beyond any realistic uptime rather than claiming every wrap is phase-continuous
or that the offset is always zero. Retain the exact phase-continuity and
zero-offset claims only for 32-bit TickType_t rates that divide 100, and
preserve the existing guidance for unsupported widths or rates.
- Around line 22-28: The compile-time check in SolidSyslogFreeRtosSysUpTime must
support the documented C99 baseline. Replace the direct _Static_assert in
SolidSyslogFreeRtosSysUpTime with SOLIDSYSLOG_STATIC_ASSERT from
SolidSyslogMacros.h, preserving the existing condition and diagnostic message.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 6d338801-a206-4388-8910-63503744f25c

📥 Commits

Reviewing files that changed from the base of the PR and between 1ed22c4 and 6865759.

📒 Files selected for processing (2)
  • Platform/FreeRtos/Interface/SolidSyslogFreeRtosSysUpTime.h
  • Platform/FreeRtos/Source/SolidSyslogFreeRtosSysUpTime.c

Comment thread Platform/FreeRtos/Source/SolidSyslogFreeRtosSysUpTime.c
Comment thread Platform/FreeRtos/Source/SolidSyslogFreeRtosSysUpTime.c Outdated
@github-actions

Copy link
Copy Markdown
Contributor

☀️   Quality Summary

   🚦   build-linux-gcc: 100% successful (✔️ 1524 passed)
   🚦   build-freertos-host-tdd-plustcp: 100% successful (✔️ 1876 passed)
   🚦   build-linux-clang: 100% successful (✔️ 1455 passed)
   🚦   sanitize-linux-gcc: 100% successful (✔️ 1455 passed)
   🚦   integration-linux-openssl: 100% successful (✔️ 16 passed)
   🚦   integration-linux-mbedtls: 100% successful (✔️ 14 passed)
   🚦   integration-windows-openssl: 100% successful (✔️ 16 passed)
   🚦   bdd-linux-syslog-ng: 94% successful (✔️ 49 passed, 🙈 3 skipped)
   🚦   bdd-windows-otel: 88% successful (✔️ 46 passed, 🙈 6 skipped)
   🚦   bdd-freertos-qemu-plustcp: 87% successful (✔️ 45 passed, 🙈 7 skipped)
   🚦   bdd-freertos-qemu-lwip: 87% successful (✔️ 45 passed, 🙈 7 skipped)
   🚦   build-windows-msvc: 100% successful (✔️ 1300 passed)
   🚦   build-linux-tunable-override: 100% successful (✔️ 1455 passed)
   ⚠️   Clang-Tidy: No warnings
   ⚠️   CPPCheck: No warnings


Created by Quality Monitor v1.14.0 (#f3859fd). More details are shown in the GitHub Checks Result.

Address CodeRabbit re-review on #634: the raw _Static_assert is a C11
primitive, so it breaks the documented C99 portability target (the c99
preset). Switch to SOLIDSYSLOG_STATIC_ASSERT from SolidSyslogMacros.h,
which tiers to static_assert (C++), _Static_assert (C11), or a negative-
array-size fallback (C99) — matching every other static-assert site
(SolidSyslogFormatter.c, SolidSyslogMbedTlsAesGcmPolicy.c). Condition and
message are unchanged.

Add Core/Source to the SysUpTime host-test include path (the FreeRtos
production pack already exposes it); bump the file's misra-5.7 suppression
line for the added include.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown
Contributor

☀️   Quality Summary

   🚦   build-linux-gcc: 100% successful (✔️ 1524 passed)
   🚦   build-freertos-host-tdd-plustcp: 100% successful (✔️ 1876 passed)
   🚦   build-linux-clang: 100% successful (✔️ 1455 passed)
   🚦   sanitize-linux-gcc: 100% successful (✔️ 1455 passed)
   🚦   integration-linux-openssl: 100% successful (✔️ 16 passed)
   🚦   integration-linux-mbedtls: 100% successful (✔️ 14 passed)
   🚦   integration-windows-openssl: 100% successful (✔️ 16 passed)
   🚦   bdd-linux-syslog-ng: 94% successful (✔️ 49 passed, 🙈 3 skipped)
   🚦   bdd-windows-otel: 88% successful (✔️ 46 passed, 🙈 6 skipped)
   🚦   bdd-freertos-qemu-plustcp: 87% successful (✔️ 45 passed, 🙈 7 skipped)
   🚦   bdd-freertos-qemu-lwip: 87% successful (✔️ 45 passed, 🙈 7 skipped)
   🚦   build-windows-msvc: 100% successful (✔️ 1300 passed)
   🚦   build-linux-tunable-override: 100% successful (✔️ 1455 passed)
   ⚠️   Clang-Tidy: No warnings
   ⚠️   CPPCheck: No warnings


Created by Quality Monitor v1.14.0 (#f3859fd). More details are shown in the GitHub Checks Result.

@DavidCozens
DavidCozens merged commit 09372e7 into main Jul 20, 2026
30 checks passed
@DavidCozens
DavidCozens deleted the fix/freertos-sysuptime-tick-wrap-guard branch July 20, 2026 10:21
@DavidCozens DavidCozens mentioned this pull request Jul 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

S24.22: FreeRtosSysUpTime does not account for FreeRTOS tick-counter wrap

1 participant