mirror of
https://github.com/meshtastic/firmware.git
synced 2026-09-12 22:29:00 -04:00
* Add native test coverage for the UptimeClock monotonic seam
src/UptimeClock.{h,cpp} shipped without a dedicated test suite. Port the six
tests from the monotonic-time branch (test/test_time), retargeted to the
renamed header.
The wrap test crosses 0xFFFFFFFF via advanceTestMillis() rather than a second
setTestMillis(): setTestMillis() sets clockSourceChanged, which makes
getMillis64() rebase its accumulator and swallow the wrap.
* NextHopRouter: fix 49.7-day millis() rollover in retransmission timing
Resolves the "FIXME, handle 51 day rolloever here!!!" in
NextHopRouter::doRetransmissions() by switching the retransmission-due
comparison from plain unsigned <= to a signed-difference cast.
The previous p.nextTxMsec <= now comparison silently breaks across the
~49.7 day millis() wraparound: pending retransmissions either stall
for the remainder of the wrap window, or all fire simultaneously at
the rollover boundary. Long-running router/infrastructure nodes do hit
this in practice.
The replacement (int32_t)(p.nextTxMsec - now) <= 0 is the standard
Arduino/embedded idiom for rollover-safe deadline checks and behaves
identically to the original for any non-wrap timing.
* Address Copilot review: use unsigned half-range for rollover-safe retransmit check
Review feedback from @Copilot on PR #10227: casting a uint32_t
subtraction to int32_t is implementation-defined in C++ when the
unsigned value exceeds INT32_MAX (even though it works on typical
two's-complement targets).
Switch to the fully well-defined unsigned half-range form:
nextTxMsec is in the past-or-equal iff (now - nextTxMsec) has not
wrapped past 2^31 ms. Future offsets < 2^31 ms wrap into the top
half and read as 'not yet'.
Same semantics as the signed-cast version on every two's-complement
platform we care about, but portable to any conforming C++ impl.
* Use monotonic time for airtime windows
* Document monotonic airtime windows
* Fix test_packet_signing sentinel that #10227's rollover fix inverts
test_C3_invalid_repeated_packet_cannot_ack_or_change_retry_state parked a
pending packet at nextTxMsec = UINT32_MAX to mean "never retransmit", then
asserted that a rejected repeated packet leaves the retry state untouched.
NextHopRouter::doRetransmissions() now tests whether a retransmit is due with
an unsigned half-range compare, (uint32_t)(now - nextTxMsec) < 0x80000000u,
so that retransmission timing survives the ~49.7 day millis() wrap. Under it
now - 0xFFFFFFFF == now + 1, a small positive delta, so UINT32_MAX reads as
~1ms in the past: the retransmit fires and rewrites nextTxMsec, and the test
failed with "Expected 4294967295 Was 6247".
Use a representable future time instead. Production is unaffected either way -
nextTxMsec is only ever written as millis() + d, and UINT32_MAX came from the
test harness alone - so the sentinel is what needs to go, not the comparison.
Special-casing UINT32_MAX in the retransmit path would keep a value that reads
as "expired" under any wrap-correct compare.
The value is held in a local because millis() advances across
runPipelineIngress(), so recomputing it at the assertion would compare against
a different number.
Reported upstream on meshtastic/firmware#10227, whose branch predates this test.
* Make Throttle time-injectable and add hasElapsed()
Throttle backs ~94 call sites, which makes it the highest-leverage place in
the tree to put the clock seam: reading Time::getMillis() instead of millis()
in its three call sites turns all of them into time-injectable code at once,
without touching any of them. The 32-bit millis() wrap is not otherwise
reachable from a native test.
The read is behaviour-preserving - Time::getMillis() returns millis() unless a
test injects a clock - and the full native suite passes with it live.
Also add hasElapsed(), the complement of isWithinTimespanMs(), because 51 of
the 94 call sites are spelled !isWithinTimespanMs and read poorly. Its
boundary is inclusive (>=) since isWithinTimespanMs uses <; both are
documented. It deliberately does not treat lastExecutionMs == 0 as "never
run": call sites pair that test with the interval check themselves, and
absorbing a sentinel into the one helper every module depends on is exactly
the value-overloading hazard being removed elsewhere.
Migrating the existing !isWithinTimespanMs sites is cosmetic and deliberately
left out of this commit.
test/test_throttle/ covers window semantics, both boundaries, the complement
identity, execute()'s first-run and throttled paths, and - the point of the
exercise - a window opened before the wrap closing correctly after it,
including at the 24h interval that is the longest in the tree.
* Stop disarmed deadline sentinels reaching the comparison
Two deadline variables encoded "inactive" as a magic value that only reads as
"never" because the comparison against it is a naive millis() compare. Under
any rollover-correct comparison both invert to "expired ~49 days ago", so they
have to be untangled before those comparisons can be fixed.
Power::reboot() set rebootAtMsec = -1 on platforms with no reboot
implementation, intending "never fire". Every reader already treats 0 as the
disarm value - powerCommandsCheck() tests `if (rebootAtMsec && ...)`, and
AdminModule writes 0 to cancel - so -1 was both wrong and unnecessary. Use 0.
Left as UINT32_MAX it would reboot-loop the moment the comparison is corrected.
ExternalNotificationModule's nag window compared against nagCycleCutoff, which
holds UINT32_MAX once stopped and 1 at boot. isNagging is the real armed flag,
so test it first and short-circuit: a disarmed cutoff can no longer reach the
arithmetic, while an idle module still takes the same sleep path that the
boot-time value of 1 was relying on.
Note this fixes the sentinel only. The comparison itself is still a naive
`nagCycleCutoff < millis()` and remains on the list to convert.
* Fix millis() rollover in every deadline and interval comparison
Roughly 20 sites compared against millis() directly - `millis() > deadline`,
`deadline < millis()`, `last + interval < millis()`. All of them break for
about 24 days after the 32-bit millis() wrap: depending on which side of the
wrap each value sits, the action either stalls for weeks or fires immediately
and repeatedly. The longest affected interval is the 12 hour NTP renewal, a
~50x margin against the wrap, so none of these needed the range - only the
correct comparison.
Add Throttle::deadlinePassed(deadlineMs) for sites that store an absolute
deadline they cannot re-express as "interval since an event". It uses the same
unsigned half-range test as NextHopRouter::doRetransmissions() rather than
introducing a competing signed-cast idiom, and unlike the signed cast it is
defined for every input. Sites that do store an event use the existing
isWithinTimespanMs / hasElapsed. Nothing gained new state.
Because both helpers read Time::getMillis(), every converted site is now
reachable from a native test that drives the clock across the wrap; the
comparison itself is covered directly in test/test_throttle/.
Sentinel handling is the reason this could not be a mechanical rewrite. The
disarm convention is not uniform: 0 means "inactive" for rebootAtMsec,
shutdownAtMsec, alertBannerUntil, fixHoldEnds, suppressUntilMs and
touchResumeBlockUntilMs; 0 means "due now" for ntp_renew, which is forced to 0
at link-up; UINT32_MAX means "inactive" for nagCycleCutoff; and
alertBannerUntil == 0 in isOverlayBannerShowing() means "show indefinitely".
Every inactive marker is arithmetically far in the past, so a correct
comparison fires on it - each site tests its sentinel before the arithmetic,
and keeps the meaning it had.
Two sites carried a second bug found on the way:
BME680Sensor tested (stateUpdateCounter * STATE_SAVE_PERIOD) < millis(). With
a 6 hour period and a uint16_t counter that product overflows uint32_t after
about 198 saves, independently of the millis() wrap. It now measures the
interval since the last save.
EInkDynamicDisplay had `if (previousRunMs > millis()) return;` as a millis()
overflow guard, which skipped rate limiting entirely for the whole post-wrap
period - the bug it meant to prevent. Every check below it already goes
through Throttle, so the guard is removed rather than fixed.
MotionSensor's calibration countdown is converted to a signed delta rather
than deadlinePassed, because it needs the remaining magnitude and not a
boolean; that matches the already-correct check in the same file.
* Remove getMillis64() and use Throttle for the NodeInfo reply window
getMillis64() had exactly one caller and no callers in tests. It also carried
obligations that made it the wrong shape for this firmware: a wrap accumulator
in mutable statics, which is not ISR-safe, and which must be polled at least
once every ~49.7 days or it silently misses a wrap and returns a time ~49 days
short.
Its one caller only wanted to know whether a 12 hour suppression window had
elapsed - which Throttle answers correctly across the wrap without any
accumulator. NodeInfoModule now stores Time::getMillis() in lastNodeInfoSeen
and tests the window with Throttle::isWithinTimespanMs, so the map holds
milliseconds rather than seconds derived from a 64-bit read.
USERPREFS_NODEINFO_REPLY_SUPPRESS_SECS is user-overridable and now feeds a
multiply by 1000, so a static_assert rejects any value too large to express in
milliseconds instead of letting it wrap.
clockSourceChanged goes too. It existed solely to rebase getMillis64()'s
accumulator when a test swapped clock sources, and it made the wrap untestable
through the injection API: setTestMillis() set the flag, so a wrap crossed by
two setTestMillis() calls was swallowed. With the accumulator gone the flag has
nothing to rebase, and the injection API is a plain settable clock.
The three getMillis64 tests are dropped as they no longer describe anything.
One test replaces them, pinning that advanceTestMillis() wraps past
0xFFFFFFFF rather than saturating, since the Throttle wrap tests rely on it.
Also fix eviction in pruneLastNodeInfoCache(): it picked the entry with the
smallest stored stamp, which is the wrong victim once some stamps sit on the
far side of the wrap. It now evicts the largest elapsed time.
* Add CI guard and docs rule against naive millis() comparisons
Fixing the existing sites does not stop the next one being added. The
millis-deadline-check job rejects millis() placed directly next to a comparison
operator, in either order, anywhere in src/. It lives in test_native.yml
alongside suite-count-check, which sets the precedent for a repo-hygiene guard
that CI enforces and bin/run-tests.sh does not.
The correct idioms all subtract before comparing, so none of them match the
pattern. Line comments are stripped first, so documentation is free to name the
broken form - as the guard's own comment and the coding conventions both do.
Writing the check before finishing the sweep turned out to be worth it: it
found roughly 14 sites that a by-hand audit of deadline variables had missed,
including two extra nagCycleCutoff compares, both boot-screen timeouts, and a
6 hour sensor save interval that was also overflowing a uint32_t multiply.
.github/millis-deadline-allowlist.txt covers the cases that are genuinely not
deadline tests. Both current entries are uptime thresholds - "has the device
been up N ms" - with no stored deadline and no event to measure from: a 30s
button holdoff against phantom shutdown from floating pins, and a 10s window
for the OEM boot logo. Each re-crosses its threshold once per wrap, which is
harmless for boot-holdoff logic and not worth new state to avoid. Entries are
keyed on file plus exact source text, without line numbers, so an edit above an
entry does not silently invalidate it.
Locally the guard reports 19 matches before the sweep and 2 after, both
allowlisted.
The Throttle bullet in the coding conventions is rewritten from "prefer
Throttle for rate limiting" to "never compare against millis() directly", lists
all four helpers with when to use which, names the CI guard, and documents the
sentinel hazard with the rebootAtMsec = -1 case that would have become a reboot
loop. Mirrored into AGENTS.md; CLAUDE.md gets a pointer row.
* Trim rollover comments to what the code needs
The comments added with the millis() rollover fixes carried too much of the
investigation that produced them: how many sites were found, which document
recorded them, what the old code used to do. That belongs in the commit history,
not in the source, and some of it was already stale - Power::reboot() still
described the check it disarms as "a naive millis() > deadline" when that
comparison had been fixed in the same series.
What stays is the non-obvious part at each site: which sentinel value the
variable overloads and what it means there, since that differs between call
sites and is what a correct comparison gets wrong. 0 means "not scheduled" for
rebootAtMsec, "renew now" for ntp_renew, and "show indefinitely" in
isOverlayBannerShowing().
Exposition is kept where it earns its place: the Throttle helpers, the uptime
clock's note on why there is no 64-bit variant, and the tests. The Throttle
docs lose only the site count and the "longest interval in the firmware"
statistic, both of which would age badly; the range trade-off between the two
forms is what a caller actually needs.
Comments only - no code changed, verified by diff.
* possible fixes
* Address review feedback on the rollover fixes
- BME680Sensor: checkpoint lastStateSaveMs after a successful write instead of
at the interval test. The first save (IAQ accuracy >= 2) left it at 0, timing
the next save from boot, and stamping before the write deferred the retry a
full period when the write failed. Reads Time::getMillis(), the same clock
Throttle compares against.
- Throttle: add deadlinePassedAt(now, deadline) for loops that snapshot the
clock once and test many deadlines; deadlinePassed() now delegates to it.
NextHopRouter::doRetransmissions() uses it, replacing the inline half-range
compare adopted from #10227 (nightjoker7) - same arithmetic, credited at the
call site - and takes its snapshot from Time::getMillis() so setNextTx()
deadlines and the due test cannot diverge under an injected test clock.
- test_native.yml: set -euo pipefail in the millis-deadline guard, matching the
sibling suite-count job. Without -e a partially failed scan could report "no
violations" from truncated output.
- test_packet_signing: build the not-due deadline from Time::getMillis() rather
than millis(), so the test and the router read one clock.
- test_throttle: cover deadlinePassedAt(), and correct a wrapped-value comment
(0xFFFFFF00 + 400 is 0x00000090, not 0x00000094).
Two review comments were declined: the AirTime mutex (every airTime-> caller
runs in the single cooperative loop, WebServerThread included) and the
MotionSensor 0-sentinel countdown (the calibration frame is only installed
while a window is open).
clod helped out here
* Correct the described failure window of a naive millis() compare
The comments and agent docs said a bare `millis() > deadline` "breaks for ~24
days after the wrap". That figure belongs to the fix, not the bug: it is the
half-range limit of deadlinePassed(), which reads deadlines more than 2^31 ms
ahead as already passed, and the range over which a UINT32_MAX sentinel reads
as passed.
The naive compare's actual failure is an inversion lasting only while the
deadline sits on the far side of the wrap, so it is bounded by the interval:
the action fires immediately and loses its wait, or blocks for about the wait
it should have performed - days for the nRF52 flash-corruption backoff,
one skipped cycle for a seconds-long retransmit timer.
Comments and docs only; the ~24.8 day statements that correctly describe
deadlinePassed()'s own range are left as they were.
clod helped out here
* Restore a monotonic uptime clock and consolidate the wrap counters
Time::getMillisMonotonic() is the getMillis64() shape - a 32-bit wrap
counter carried across reads - promoted to the shared timebase, with
Time::getUptimeSecs() as the derived whole-seconds view. This deliberately
reverses the earlier removal of getMillis64(), and the distinction matters:
removal was right for a lazily-read accumulator with one rare caller, where
a 49.7-day gap between reads silently swallowed a wrap. Here every read is
the poll and AirTime::runOnce() guarantees one per second; the missed-wrap
contract is pinned by a test rather than left as a footnote.
Three private wrap counters collapse into it:
- AirTime::syncNow() takes its seconds from Time::getUptimeSecs() and drops
its lastSyncMsec checkpoint; window rotation is unchanged.
- DeviceTelemetryModule loses refreshUptime()/uptimeWrapCount/uptimeLastMs;
uptime_seconds comes from Time::getUptimeSecs(), which also removes the
0.296s-per-wrap truncation of (0xFFFFFFFF / 1000) * wraps. Its two
interval checks move to Throttle::hasElapsed().
- HostMetricsModule's copies of those members were never read (its uptime
comes from /proc/uptime) - deleted.
Not ISR-safe (unguarded mutable carry): ISRs keep using getMillis(), which
stays a pure read. Audited: no interrupt-context file reads getTime(),
getValidTime(), or the new accessors.
test/native-suite-count 44 -> 45: the bump for test_uptime_clock was lost
in a branch history rewrite, leaving every later value off by one -
run-tests.sh reports AMBER and CI's suite-count-check fails on the current
push until this correction.
* Anchor the wall clock in monotonic milliseconds
getTime() computed elapsed-since-time-set as a 32-bit millis() delta, so a
node that took time once and stayed up past 49.7 days reported a wall clock
one full cycle in the past - and last_heard, rx_time, message and position
stamps all inherited it. The anchor is now the 64-bit monotonic count
(timeStartMsec -> timeStartMs64) and the elapsed term is computed in 64-bit,
so the wall clock is exact at any uptime.
All six anchor writers follow: the five hardware-RTC read branches and
perhapsSetRTC(), which keeps a truncated 32-bit copy of the same instant for
its Throttle-checked rate-limit stamps. The test seams anchor the same way.
Two native regression tests drive getTime() across the wrap through the
Time seam - one anchored before the wrap and read after it, one anchored
after a counted wrap - with the test epoch derived from BUILD_EPOCH so the
plausibility window cannot rot as the build date advances.
* Stamp the rx_time placeholder in monotonic uptime seconds
computeRxTimeStamp() stamped Time::getMillis() when the clock was untrusted,
and reconcilePendingRxTimes() back-calculated with a 32-bit millis() delta -
correct within one wrap, but a placeholder older than 49.7 days aliased to a
small elapsed value and reconciled to a plausible-but-wrong recent epoch:
the exact failure has_rx_time exists to prevent, reachable by an ordinary
unattended router whose phone connects two months in.
The placeholder is now Time::getUptimeSecs(). Both stamps come off the
monotonic counter, so the elapsed term is exact at any age and the aliasing
window is gone outright rather than widened. If elapsed somehow exceeds the
epoch itself, the packet stays un-dated (absent, never wrong) instead of
clamping to a pre-1970 value. Defence in depth: a placeholder that leaks
needs ~50 years of uptime to cross MIN_PLAUSIBLE_EPOCH, where milliseconds
took 18.3 days.
The stream-API reconciliation tests keep their scenarios with the placeholder
unit switched, and ScopedTimeFixture resets the monotonic carry so uptime
seconds are deterministic per case.
* Date nodes heard before the clock arrives, without polluting last_heard
A node first heard while the wall clock was untrusted got no last_heard at
all, and nothing backfilled it once time arrived - the phone showed "Last
heard: unknown" for a node it had just announced. The arrival instant now
waits in a RAM-only sidecar (NodeNum -> uptime seconds, 32 slots,
reuse-oldest - the RouteHealth shape) and is converted to a real epoch on
the clock-becoming-trusted transition, beside the existing rx_time
reconciliation. last_heard itself never holds anything but a real epoch or
0: it persists to flash and the warm tier, where an uptime-relative value
would be meaningless after reboot.
The sidecar's write sites are updateFrom()'s no-trusted-clock path (the
rx_time placeholder already carries the arrival instant, so this is a store,
not a second clock read) and addFromContact's anti-eviction stamps, which
previously wrote a bare getTime() - boot-relative seconds on a clockless
node, the exact value lastHeardIsWallClock() exists to catch. Eviction
ranking honours the stamps: heard-this-boot outranks every stored epoch,
ordered among themselves, so a stamped contact is not the first victim.
PhoneAPI re-reads last_heard at nodeinfo send time: a record prefetched
before the clock became trusted can carry 0 while the store has since been
backfilled, and re-reading at the pop makes handshake ordering (time-set vs
node-list download) irrelevant. Backfill never moves last_heard backwards
and skips the pathological elapsed-exceeds-epoch case. A node evicted to
the warm tier before time arrives is still absorbed with last_heard 0 -
same as before, bounded to the untrusted window.
* Update the agent docs for the monotonic timebase
The conventions bullet asserted there is deliberately no 64-bit millis; the
monotonic uptime clock restored for timestamps changes that contract. State
the split explicitly: Throttle for deadlines and intervals (no carry state),
Time::getMillisMonotonic()/getUptimeSecs() for timestamps, polled by
construction and not ISR-safe.
* Publish the monotonic wrap carry from a single writer
getMillisMonotonic() was a read-modify-write on two unguarded statics, and it
is reached off the main loop: the nRF52 Bluefruit task via
onFromRadioAuthorize() -> PhoneAPI::getFromRadio -> getValidTime(), and the
portduino civetweb workers via the same path. Two readers interleaving inside
the wrap window could each increment the carry, putting every uptime and
wall-clock reading 2^32 ms ahead for the rest of the boot - a permanent ~49.7
day jump in rx_time, last_heard and ClientNotification.time.
Readers no longer write. serviceMonotonic() publishes a snapshot behind a
seqlock and is the only writer; a reader adds its own unsigned elapsed time to
that snapshot, which is exact across the wrap, so it never inspects the
boundary and cannot miscount it. The main loop publishes every iteration, so
the once-per-49.7-days obligation now has the whole window of margin instead of
resting on an instruction-wide race.
AirTime was the guaranteed poller and is now a pure reader, so the two airtime
wrap tests step the clock the way loop() does. The test clock itself is atomic
so a suite can drive it from one thread while others read.
* Re-arm the GPS ephemeris hold when none is in force
The rollover sweep guarded the hold re-arm with `fixHoldEnds != 0 &&`, which
reads like the sentinel rule but inverts this site. The comparison it replaced,
`(fixHoldEnds + GPS_THREAD_INTERVAL) < millis()`, was always true when nothing
was armed - that was the point, since 0 means "not holding" and so is a reason
to arm. With the guard, a publish that cleared the hold without sleeping (the
`shouldPublish && !tooLong && !holdExpired` path, which does not call down())
left hasValidLocation set and prev_fixQual non-zero, so no disjunct held:
nothing re-armed, nothing published, and the receiver stayed powered at the
200ms poll until searchedTooLong() fired.
State the question positively instead. fixHoldInForce() is the only place the
sentinel is interpreted, and both of runOnce()'s decisions derive from it - the
asymmetry is now visible rather than implied, since arming does not require a
prior hold but expiring does. Its `!= 0` test is not redundant with the
arithmetic: deadlinePassed() is an unsigned half-range test, so past 2^31 ms of
uptime the sentinel reads as a deadline ~24.9 days in the future.
Kept beside its caller rather than in a header; the native test build compiles
GPS.cpp, so the suite declares the prototypes.
Also converts the getACK() wait to isWithinTimespanMs(start, interval): it has
both the start instant and the interval in hand, which gives the full 49.7-day
range instead of 24.8 days ahead, and takes its anchor from Time::getMillis()
so the wait is injectable.
* Date the NodeInfo reply window in uptime seconds
The 12h reply-suppression stamp regressed from wrap-immune 64-bit seconds to
raw 32-bit milliseconds, and pruneLastNodeInfoCache() evicts only by node count
and DB membership - never by age. A stable mesh under the node cap therefore
keeps every stamp indefinitely, and once uptime passes 49.7 days an old one
aliases back into the window: `now - stamp` computes as ~0 and a legitimate
NodeInfo request goes unanswered for up to 12h. It self-heals and repeats once
per wrap cycle.
Store Time::getUptimeSecs() instead, which does not wrap for 136 years, and
drop the millisecond conversion the previous shape needed. Entries past the
window are now evicted too: they can only ever decide "don't suppress".
N8-N11 cover the window from both sides, and N10 pins the regression - it needs
a full 2^32 ms of uptime to elapse, not merely a crossing of the boundary,
because that is when a millisecond stamp reads as "answered this instant".
tearDown() now restores the injected clock and C14's region and TX bucket. A
failing assertion aborts the test body, so restoring at the end of it leaked
that state into every later case.
* Update the agent docs for the single-writer clock and sentinel direction
Two rules the preceding three commits changed.
The monotonic clock is no longer maintained by whoever happens to read it:
serviceMonotonic() is the only writer, readers are pure, and calling it from
anywhere but the main loop reintroduces the double-count.
The sentinel guidance gained the half it was missing. It named UINT32_MAX as a
sentinel while prescribing an idiom that only covers 0, and it assumed the
sentinel always means "suppress" - at the GPS fix-hold site it meant "fire",
which is how that regression passed review looking like the rule.
* Name the fix-hold expiry predicate and arm it from the injected clock
holdJustExpired() gives the second reading of the fixHoldEnds sentinel a
name beside the first, so both are pinned by test/test_gps_fix_hold/ and
neither can be respelled at the call site. The old inline form could not
be tested: written as a literal, its guard folds at compile time and the
assertion asserts nothing.
The arm site used bare millis() while the evaluation reads the Throttle
clock; same value in production, but it kept that write out of reach of
Time::setTestMillis(). Remap a deadline that lands on 0, which would
otherwise read as no hold at all.
* Share the extend formula between the clock's reader and writer
getMillisMonotonic() and serviceMonotonic() carried byte-identical wrap
arithmetic. A one-sided edit to either would drift the published carry
from what readers report, so keep one copy.
* Trim the NodeInfo dedup comment to the house limit
* todo note for potential future imrpovments
* fix some simple deadlines
* Trim the hold-expiry test comment to the house limit
* Fix non-blocking uptime publication and pre-clock recency edges (#29)
* fix(time): avoid blocking monotonic readers
* test(time): make paused-publisher check deterministic
* fix(time): address review portability gaps
* Init the eviction sentinel to the newest possible recency
EvictionRecency{} is {0, false}, which evictionRecencyOlder() ranks as older than
every candidate: without the oldestIndex/oldestBoringIndex guards nothing would
ever be selected and a full node DB would stop evicting entirely.
Init to the genuine maximum instead, so the sentinel is correct on its own. The
index guards stay: two independent reasons the scan is right beats one.
* Keep the deadline-guard check name branch protection matches
The guard was widened to cover Time::getMillis() and unqualified getMillis(),
and renamed to suit. Upstream branch protection matches required checks by name,
so a rename means the old name never reports and merges block on a check that
will never arrive.
Widen the guard, keep the name; the descriptive text carries the broader scope.
* Correct native-suite-count to 47 after the develop merge
Upstream #11293 added test_nmea_wpl and took develop's count to 43; this branch
had independently reached 46. Merging develop resolved the counter textually,
keeping 46, while the directory set became the union of both sides at 47.
The suite-count CI gate fails on the mismatch, and it gates the native test jobs,
so the tests themselves were being skipped.
* test(uptime): make the wrap fall where the comment says it does
The concurrent-reader case started at 0xFFFFF000, leaving 0x1000 to the wrap, so
the 0x800 advance annotated "cross the wrap" fell short and the wrap actually
happened during the following 60s advance.
Start at 0xFFFFF800 instead, so the first advance lands exactly on the wrap while
the readers are running and the second is the ordinary time after it - the shape
both comments already described. Total elapsed is unchanged, so the closing
assertion still holds.
* Respond to human comments
* Did I ever tell you about the time I went to Shelbyville? I wore an onion on my belt, which was the style at the time.
* Convert the I2S nag deadline develop dragged in
The HAS_I2S_SPEAKER_NRF52 RTTTL block arrived from develop with a raw
nagCycleCutoff >= millis(), which the deadline guard rejects. Use the same
Throttle::deadlinePassed() form as the two sibling paths in this function.
* Arm the LittleFS format guard with a flag, not a zero timestamp
preFSBegin() runs in the first millisecond of boot, so millis() can legitimately
return 0 there. Both readers of last_format_ms treated 0 as "nothing formatted
this boot", which would skip the repeat-corruption escalation and let a dead
flash reformat-loop instead of reporting FLASH_CORRUPTION_UNRECOVERABLE.
* Note the single-thread contract on AirTime
* Note the AirTime locking TODO, and tighten the thread note
The two constant getters are not constrained, and getSilentMinutes() reads the
buckets without rotating them, so "the accessors mutate" was not accurate.
* trunk: ignore trufflehog false positives on millis-wrap test constants
test_throttle and test_uptime_clock pin dense clusters of hex boundary
constants (0xFFFFFF00u and neighbors) to exercise 32-bit millis()
rollover. trufflehog's Lob detector stitches nearby hex literals into
one candidate string, and the result happens to match a Lob API key
shape - not a secret, just test fixtures.
Same pattern already used for the gitleaks/nodedb-fixture false
positive in this file.
---------
Co-authored-by: nightjoker7 <mattdeering7@gmail.com>
Co-authored-by: Clive Blackledge <clive@ansible.org>
Co-authored-by: Benjamin Faershtein <119711889+RCGV1@users.noreply.github.com>
Co-authored-by: Thomas Göttgens <tgoettgens@gmail.com>
Co-authored-by: Ben Meadors <benmmeadors@gmail.com>
357 lines
13 KiB
C++
357 lines
13 KiB
C++
// Unit tests for src/UptimeClock.{h,cpp} - the monotonic uptime seam.
|
|
// Covers: test-clock injection, stepping the injected clock, the real-clock fallback, and the
|
|
// single-writer wrap carry (readers derive, serviceMonotonic() publishes). getMillis() itself is a
|
|
// plain 32-bit read with no wrap handling of its own - its consumers' wrap arithmetic is tested in
|
|
// test_throttle/.
|
|
#include "Arduino.h"
|
|
#include "TestUtil.h"
|
|
#include "UptimeClock.h"
|
|
#include "gps/RTC.h"
|
|
#include <atomic>
|
|
#include <chrono>
|
|
#include <cstdint>
|
|
#include <sys/time.h>
|
|
#include <thread>
|
|
#include <unity.h>
|
|
#include <vector>
|
|
|
|
namespace
|
|
{
|
|
std::atomic<bool> publishPaused{false};
|
|
std::atomic<bool> releasePublish{false};
|
|
|
|
void pauseMonotonicPublish()
|
|
{
|
|
publishPaused.store(true, std::memory_order_release);
|
|
while (!releasePublish.load(std::memory_order_acquire))
|
|
std::this_thread::yield();
|
|
}
|
|
} // namespace
|
|
|
|
void setUp(void)
|
|
{
|
|
Time::resetMonotonicForTests(); // absolute uptime assertions must not depend on case order
|
|
}
|
|
void tearDown(void)
|
|
{
|
|
Time::useRealClock(); // don't leak the fake clock into other suites
|
|
resetRTCStateForTests();
|
|
}
|
|
|
|
// Step the injected clock the way the firmware does: the main loop calls serviceMonotonic() every
|
|
// iteration, so any advance is followed by a publish.
|
|
static void advanceAndService(uint32_t deltaMs)
|
|
{
|
|
Time::advanceTestMillis(deltaMs);
|
|
Time::serviceMonotonic();
|
|
}
|
|
|
|
// --- injection ---
|
|
|
|
void test_getMillis_returns_injected_value()
|
|
{
|
|
Time::setTestMillis(123456);
|
|
TEST_ASSERT_EQUAL_UINT32(123456, Time::getMillis());
|
|
}
|
|
|
|
void test_advanceTestMillis_steps_clock()
|
|
{
|
|
Time::setTestMillis(1000);
|
|
Time::advanceTestMillis(500);
|
|
TEST_ASSERT_EQUAL_UINT32(1500, Time::getMillis());
|
|
}
|
|
|
|
// Advancing past 0xFFFFFFFF wraps like millis() does, rather than saturating. This is the property
|
|
// the Throttle wrap tests are built on, so it is worth pinning here too.
|
|
void test_advanceTestMillis_wraps_like_millis()
|
|
{
|
|
Time::setTestMillis(0xFFFFFF00u);
|
|
Time::advanceTestMillis(0x200u);
|
|
TEST_ASSERT_EQUAL_UINT32(0x00000100u, Time::getMillis());
|
|
}
|
|
|
|
// --- getMillisMonotonic(): the published wrap carry ---
|
|
|
|
void test_monotonic_matches_millis_before_any_wrap()
|
|
{
|
|
Time::setTestMillis(123456);
|
|
TEST_ASSERT_EQUAL_UINT64(123456u, Time::getMillisMonotonic());
|
|
}
|
|
|
|
void test_monotonic_counts_a_wrap()
|
|
{
|
|
Time::setTestMillis(0xFFFFFF00u);
|
|
Time::serviceMonotonic();
|
|
TEST_ASSERT_EQUAL_UINT64(0xFFFFFF00u, Time::getMillisMonotonic());
|
|
|
|
advanceAndService(0x200u); // crosses the 32-bit wrap; low word is now 0x00000100
|
|
TEST_ASSERT_EQUAL_UINT64(0x100000100ull, Time::getMillisMonotonic());
|
|
}
|
|
|
|
// The property that lets readers stay pure: a reader adds its own unsigned elapsed time to the
|
|
// published snapshot, so it is exact across a wrap that no publish has observed yet. Nothing here
|
|
// needs to detect the boundary, which is why concurrent readers cannot double-count it.
|
|
void test_monotonic_reader_crosses_the_wrap_without_a_publish()
|
|
{
|
|
Time::setTestMillis(0xFFFFFF00u);
|
|
Time::serviceMonotonic(); // last publish before the wrap
|
|
|
|
Time::advanceTestMillis(0x200u); // cross the wrap with no publish at all
|
|
TEST_ASSERT_EQUAL_UINT64(0x100000100ull, Time::getMillisMonotonic());
|
|
}
|
|
|
|
// Reads must not advance the carry. Under the old read-modify-write accessor each reader bumped
|
|
// the wrap counter itself, which is what made two of them able to count one wrap twice.
|
|
void test_monotonic_reads_do_not_advance_the_carry()
|
|
{
|
|
Time::setTestMillis(0xFFFFFF00u);
|
|
Time::serviceMonotonic();
|
|
|
|
Time::advanceTestMillis(0x200u);
|
|
for (int i = 0; i < 8; i++)
|
|
TEST_ASSERT_EQUAL_UINT64(0x100000100ull, Time::getMillisMonotonic());
|
|
|
|
Time::serviceMonotonic(); // the eight reads must not have left eight wraps behind
|
|
TEST_ASSERT_EQUAL_UINT64(0x100000100ull, Time::getMillisMonotonic());
|
|
}
|
|
|
|
void test_monotonic_counts_every_wrap_when_serviced_each_window()
|
|
{
|
|
Time::setTestMillis(0x80000000u);
|
|
Time::serviceMonotonic();
|
|
TEST_ASSERT_EQUAL_UINT64(0x80000000ull, Time::getMillisMonotonic());
|
|
|
|
// Three full 2^32 cycles, published once per half-cycle - well inside the required
|
|
// one-publish-per-49.7-days window.
|
|
for (int wrap = 1; wrap <= 3; wrap++) {
|
|
advanceAndService(0x80000000u); // crosses the wrap; low word back to 0
|
|
advanceAndService(0x80000000u); // completes the cycle; low word back to 0x80000000
|
|
TEST_ASSERT_EQUAL_UINT64(0x80000000ull + ((uint64_t)wrap << 32), Time::getMillisMonotonic());
|
|
}
|
|
}
|
|
|
|
// The documented contract, pinned: a full 2^32 ms elapsing between two publishes is
|
|
// indistinguishable from no time passing, so the wrap is lost. This is why the main loop's
|
|
// per-iteration serviceMonotonic() matters - and it is now the only obligation, where before every
|
|
// reader had to participate.
|
|
void test_monotonic_misses_a_wrap_not_serviced_within_the_window()
|
|
{
|
|
Time::setTestMillis(1000);
|
|
Time::serviceMonotonic();
|
|
TEST_ASSERT_EQUAL_UINT64(1000u, Time::getMillisMonotonic());
|
|
|
|
Time::advanceTestMillis(0x80000000u);
|
|
advanceAndService(0x80000000u); // full cycle with no publish in between: low word is 1000 again
|
|
|
|
TEST_ASSERT_EQUAL_UINT64(1000u, Time::getMillisMonotonic()); // the elapsed 2^32 ms is lost
|
|
}
|
|
|
|
void test_getUptimeSecs_stays_exact_across_the_wrap()
|
|
{
|
|
Time::setTestMillis(4294967000u); // 4294967 whole seconds, 296ms short of the wrap
|
|
Time::serviceMonotonic();
|
|
TEST_ASSERT_EQUAL_UINT32(4294967u, Time::getUptimeSecs());
|
|
|
|
advanceAndService(1000); // crosses the wrap
|
|
TEST_ASSERT_EQUAL_UINT32(4294968u, Time::getUptimeSecs());
|
|
}
|
|
|
|
// --- concurrent readers ---
|
|
|
|
// Readers run flat out while the clock is stepped across several wraps. Under the old accessor two
|
|
// readers interleaving inside the wrap window could each bump the counter, jumping every later
|
|
// reading 2^32 ms forward; here they only ever read, so the final value has to be exact.
|
|
//
|
|
// A one-instruction race is not something a test can hit on demand, so this is corroboration
|
|
// rather than the guarantee - the guarantee is structural, and test_monotonic_reads_do_not_advance
|
|
// _the_carry pins it. What this case does catch is any future change that puts a write back on the
|
|
// read path.
|
|
void test_monotonic_exact_with_concurrent_readers()
|
|
{
|
|
constexpr int kReaders = 4;
|
|
constexpr int kWraps = 3;
|
|
constexpr uint32_t kStep = 0x40000000u; // quarter of a cycle, so each wrap is crossed mid-step
|
|
|
|
Time::setTestMillis(0xFFFFF000u);
|
|
Time::serviceMonotonic();
|
|
|
|
std::atomic<bool> stop{false};
|
|
std::atomic<bool> wentBackwards{false};
|
|
std::vector<std::thread> readers;
|
|
for (int i = 0; i < kReaders; i++) {
|
|
readers.emplace_back([&stop, &wentBackwards]() {
|
|
uint64_t previous = 0;
|
|
while (!stop.load(std::memory_order_relaxed)) {
|
|
const uint64_t now = Time::getMillisMonotonic();
|
|
if (now < previous)
|
|
wentBackwards.store(true, std::memory_order_relaxed);
|
|
previous = now;
|
|
}
|
|
});
|
|
}
|
|
|
|
uint64_t expected = 0xFFFFF000ull;
|
|
for (int i = 0; i < kWraps * 4; i++) {
|
|
advanceAndService(kStep);
|
|
expected += kStep;
|
|
}
|
|
|
|
stop.store(true, std::memory_order_relaxed);
|
|
for (auto &reader : readers)
|
|
reader.join();
|
|
|
|
TEST_ASSERT_FALSE_MESSAGE(wentBackwards.load(std::memory_order_relaxed), "monotonic clock retreated for a reader");
|
|
TEST_ASSERT_EQUAL_UINT64(expected, Time::getMillisMonotonic());
|
|
}
|
|
|
|
// nRF BLE callbacks run above the main loop. A reader that preempts publication must be able to
|
|
// consume the previous complete snapshot without waiting for the suspended writer.
|
|
void test_monotonic_reader_completes_while_publish_is_paused()
|
|
{
|
|
Time::setTestMillis(100);
|
|
Time::serviceMonotonic();
|
|
Time::advanceTestMillis(1);
|
|
|
|
publishPaused.store(false, std::memory_order_relaxed);
|
|
releasePublish.store(false, std::memory_order_relaxed);
|
|
Time::setMonotonicPublishHookForTests(pauseMonotonicPublish);
|
|
|
|
std::thread writer([]() { Time::serviceMonotonic(); });
|
|
while (!publishPaused.load(std::memory_order_acquire))
|
|
std::this_thread::yield();
|
|
|
|
std::atomic<bool> readerStarted{false};
|
|
std::atomic<bool> readerDone{false};
|
|
uint64_t readerValue = 0;
|
|
std::thread reader([&readerStarted, &readerDone, &readerValue]() {
|
|
readerStarted.store(true, std::memory_order_release);
|
|
readerValue = Time::getMillisMonotonic();
|
|
readerDone.store(true, std::memory_order_release);
|
|
});
|
|
while (!readerStarted.load(std::memory_order_acquire))
|
|
std::this_thread::yield();
|
|
const auto deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(100);
|
|
while (!readerDone.load(std::memory_order_acquire) && std::chrono::steady_clock::now() < deadline)
|
|
std::this_thread::yield();
|
|
const bool completedWhilePaused = readerDone.load(std::memory_order_acquire);
|
|
|
|
releasePublish.store(true, std::memory_order_release);
|
|
writer.join();
|
|
reader.join();
|
|
Time::setMonotonicPublishHookForTests(nullptr);
|
|
|
|
TEST_ASSERT_TRUE_MESSAGE(completedWhilePaused, "reader waited for a lower-priority publisher");
|
|
TEST_ASSERT_EQUAL_UINT64(101u, readerValue);
|
|
}
|
|
|
|
// --- getTime(): the wall clock must not retreat at the millis() wrap ---
|
|
|
|
// Epoch used by the wall-clock cases; must sit between BUILD_EPOCH (stamped at build time) and
|
|
// BUILD_EPOCH + 40 years or perhapsSetRTC() rejects it as implausible - so derive it.
|
|
#ifdef BUILD_EPOCH
|
|
static constexpr uint32_t kTestEpoch = (uint32_t)BUILD_EPOCH + 3600;
|
|
#else
|
|
static constexpr uint32_t kTestEpoch = 1800000000u;
|
|
#endif
|
|
|
|
void test_getTime_stays_exact_across_the_wrap()
|
|
{
|
|
resetRTCStateForTests();
|
|
Time::setTestMillis(0xFFFFFF00u); // 256ms short of the wrap
|
|
Time::serviceMonotonic();
|
|
|
|
struct timeval tv = {};
|
|
tv.tv_sec = kTestEpoch;
|
|
TEST_ASSERT_EQUAL_INT(RTCSetResultSuccess, perhapsSetRTC(RTCQualityFromNet, &tv));
|
|
TEST_ASSERT_EQUAL_UINT32(kTestEpoch, getTime(false));
|
|
|
|
advanceAndService(400u * 1000u); // crosses the wrap partway through
|
|
// With a 32-bit anchor this read came back 49.7 days in the past.
|
|
TEST_ASSERT_EQUAL_UINT32(kTestEpoch + 400, getTime(false));
|
|
}
|
|
|
|
// The anchor must also be correct when the time-set itself happens after a counted wrap, i.e.
|
|
// when the monotonic clock is already past 32-bit range.
|
|
void test_getTime_anchored_after_a_wrap_is_exact()
|
|
{
|
|
resetRTCStateForTests();
|
|
Time::setTestMillis(0xFFFFFF00u);
|
|
Time::serviceMonotonic(); // latch the pre-wrap value
|
|
advanceAndService(0x200u); // cross the wrap; monotonic is now > 2^32
|
|
|
|
struct timeval tv = {};
|
|
tv.tv_sec = kTestEpoch;
|
|
TEST_ASSERT_EQUAL_INT(RTCSetResultSuccess, perhapsSetRTC(RTCQualityFromNet, &tv));
|
|
|
|
advanceAndService(100u * 1000u);
|
|
TEST_ASSERT_EQUAL_UINT32(kTestEpoch + 100, getTime(false));
|
|
}
|
|
|
|
// A reader on another thread must not be able to perturb the wall clock. This is the user-visible
|
|
// shape of the race: getTime() is reached from the nRF52 BLE task and the portduino web server
|
|
// threads, and a double-counted wrap put every rx_time and last_heard ~49.7 days in the future.
|
|
void test_getTime_unaffected_by_concurrent_readers_across_the_wrap()
|
|
{
|
|
resetRTCStateForTests();
|
|
Time::setTestMillis(0xFFFFF800u); // exactly 0x800 short of the wrap, so the first advance lands on it
|
|
Time::serviceMonotonic();
|
|
|
|
struct timeval tv = {};
|
|
tv.tv_sec = kTestEpoch;
|
|
TEST_ASSERT_EQUAL_INT(RTCSetResultSuccess, perhapsSetRTC(RTCQualityFromNet, &tv));
|
|
|
|
std::atomic<bool> stop{false};
|
|
std::vector<std::thread> readers;
|
|
for (int i = 0; i < 4; i++) {
|
|
readers.emplace_back([&stop]() {
|
|
while (!stop.load(std::memory_order_relaxed))
|
|
(void)getTime(false); // what the BLE / web-server threads actually call
|
|
});
|
|
}
|
|
|
|
advanceAndService(0x800u); // cross the wrap while the readers are running
|
|
advanceAndService(60u * 1000u); // and some ordinary time after it
|
|
|
|
stop.store(true, std::memory_order_relaxed);
|
|
for (auto &reader : readers)
|
|
reader.join();
|
|
|
|
TEST_ASSERT_EQUAL_UINT32(kTestEpoch + 62, getTime(false)); // 0x800ms + 60s, rounded down
|
|
}
|
|
|
|
// --- real clock fallback ---
|
|
|
|
void test_real_clock_advances_when_not_injected()
|
|
{
|
|
Time::useRealClock();
|
|
uint32_t t0 = Time::getMillis();
|
|
testDelay(5);
|
|
uint32_t t1 = Time::getMillis();
|
|
TEST_ASSERT_TRUE(t1 >= t0); // real millis() is monotonic over a short delay
|
|
}
|
|
|
|
void setup()
|
|
{
|
|
initializeTestEnvironment();
|
|
UNITY_BEGIN();
|
|
RUN_TEST(test_getMillis_returns_injected_value);
|
|
RUN_TEST(test_advanceTestMillis_steps_clock);
|
|
RUN_TEST(test_advanceTestMillis_wraps_like_millis);
|
|
RUN_TEST(test_monotonic_matches_millis_before_any_wrap);
|
|
RUN_TEST(test_monotonic_counts_a_wrap);
|
|
RUN_TEST(test_monotonic_reader_crosses_the_wrap_without_a_publish);
|
|
RUN_TEST(test_monotonic_reads_do_not_advance_the_carry);
|
|
RUN_TEST(test_monotonic_counts_every_wrap_when_serviced_each_window);
|
|
RUN_TEST(test_monotonic_misses_a_wrap_not_serviced_within_the_window);
|
|
RUN_TEST(test_getUptimeSecs_stays_exact_across_the_wrap);
|
|
RUN_TEST(test_monotonic_exact_with_concurrent_readers);
|
|
RUN_TEST(test_monotonic_reader_completes_while_publish_is_paused);
|
|
RUN_TEST(test_getTime_stays_exact_across_the_wrap);
|
|
RUN_TEST(test_getTime_anchored_after_a_wrap_is_exact);
|
|
RUN_TEST(test_getTime_unaffected_by_concurrent_readers_across_the_wrap);
|
|
RUN_TEST(test_real_clock_advances_when_not_injected);
|
|
exit(UNITY_END());
|
|
}
|
|
|
|
void loop() {}
|