Repository navigation
fix(watchdog/avr): mark .init3 hook __attribute__((naked)) so it falls through (Closes #2798) - #2800
Conversation
…s through `fastled_watchdog_init3()` (added in #2745) was attributed `section(".init3"), used` but *not* `naked`. GCC therefore emitted a standard prologue/epilogue ending in `ret`. But `.init3` is not a function call site — the linker concatenates it inline into the boot init chain between `.init2` (BSS clear) and `.init4` (data copy), and there is no return address on the stack when it runs. The `ret` pops two bytes of uninitialized SRAM and jumps there, hanging the chip before `setup()` can run. Symptom on CI: `uno AVR8JS Test` fails because the emulated firmware never reaches `Serial.println("Test setup starting")` — the avr8js grep for `"Test loop"` times out at 30 s on a nearly-empty serial log. Green at `c2faf92e` (before #2745), red at every commit after. Fix: add `__attribute__((naked))` (the avr-libc-canonical pattern for clearing `MCUSR` + `wdt_disable()` in `.init3`) and drop the `inline` keyword (which combines awkwardly with `naked`). The three statements the hook performs compile to plain `STS`/inline-asm sequences with no stack temps, so `naked` is safe — and is in fact the only correct choice for code placed in `.initN`. The trailing comment is updated to explain why no explicit `ret` is needed (execution falls through to `.init4`). The prior in-source justification for *not* using `naked` ("fragile across optimization/toolchain versions") was a general concern that doesn't apply to this specific body — it's the standard recipe for `.init3` WDT/MCUSR clearing across AVR codebases. Regression-guarded locally with `bash compile`: PASS uno (classic-AVR — uses the .init3 hook) PASS attiny85 (classic-AVR — uses the .init3 hook) PASS attiny88 (classic-AVR — uses the .init3 hook) PASS attiny4313 (classic-AVR — uses the .init3 hook) PASS atmega8a (megaAVR / no classic symbols — noop fallback) PASS nano_every (megaAVR-0 — noop fallback) PASS ATtiny1604 (tinyAVR-1 — noop fallback) PASS ATtiny1616 (tinyAVR-1 — noop fallback) `bash lint` clean. Runtime verification (avr8js emulator) is only available on CI; PR CI is the authoritative check. Closes #2798 Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughThe AVR watchdog ChangesWatchdog initialization chain fix
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 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 `@src/platforms/avr/watchdog_avr.impl.hpp`:
- Around line 111-113: Update the inaccurate lifecycle comment for the `.init3`
hook to state that it runs before avr-libc's `.init4` BSS/data initialization
(not after), and explicitly note that because `.init3` executes prior to
`.init4` the sAvrCapturedMcusr variable must remain in the `.noinit` section;
locate the comment near the `.init3` hook in watchdog_avr.impl.hpp and replace
the existing wording so it clearly documents ordering relative to `.init4`, the
purpose of capturing/clearing MCUSR, and the rationale for keeping
sAvrCapturedMcusr in `.noinit`.
🪄 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: CHILL
Plan: Pro
Run ID: 28381c93-4149-415e-84f9-f9955fa06c45
📒 Files selected for processing (1)
src/platforms/avr/watchdog_avr.impl.hpp
Fix the `.init3` lifecycle comment in `watchdog_avr.impl.hpp`. The previous
wording ("runs after the C runtime has zeroed BSS") contradicted the
`sAvrCapturedMcusr` block 50 lines above it, which (correctly) explains
that `.init4` BSS/data init runs AFTER our `.init3` hook — that being the
whole reason the captured-MCUSR variable lives in `.noinit`.
Reword to state the actual ordering and cross-link the rationale, so a
future reader can't accidentally "fix" the .noinit attribute back to .bss.
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Summary
Fixes the `uno AVR8JS Test` workflow that has been red on master at every commit since #2745 ("watchdog Phase 2") merged. Closes #2798.
Continues the README badge sweep: #2780 (AVR family) / #2785 (STM32 GIGA) / #2788 (Apollo3) / #2793 (RP2040).
What's broken
`fastled_watchdog_init3()` in `src/platforms/avr/watchdog_avr.impl.hpp` was attributed `section(".init3"), used` but not `naked`. GCC therefore emits a standard function prologue/epilogue ending in `ret`.
But `.init3` is not a function call site — the linker concatenates this code inline into the AVR boot init chain between `.init2` (BSS clear) and `.init4` (data copy), and there is no return address on the stack when it runs. The `ret` pops two bytes of uninitialized SRAM and jumps there, hanging the chip before `setup()` ever executes.
Symptom: avr8js's 30 s emulator run for the `Test` sketch never produces `Test loop` (or even `Test setup starting`), because the firmware is dead before the first `Serial.println`.
Bisect: Green at `c2faf92e` (before #2745). Red at every commit since.
What this PR changes
Add `attribute((naked))` to the `.init3` hook (the avr-libc-canonical pattern for clearing `MCUSR` + `wdt_disable()` at boot), drop the now-incompatible `inline` keyword, and rewrite the comment block to explain that:
The prior comment justifying not using `naked` was a general worry — for this specific body, naked is the only correct choice.
Test plan
Local regression guard with `bash compile` on every classic + modern AVR board in the README sweep:
```
PASS uno (classic-AVR — uses the .init3 hook now)
PASS attiny85 (classic-AVR — uses the .init3 hook now)
PASS attiny88 (classic-AVR — uses the .init3 hook now)
PASS attiny4313 (classic-AVR — uses the .init3 hook now)
PASS atmega8a (megaAVR / no classic symbols — noop fallback)
PASS nano_every (megaAVR-0 — noop fallback)
PASS ATtiny1604 (tinyAVR-1 — noop fallback)
PASS ATtiny1616 (tinyAVR-1 — noop fallback)
```
Runtime verification of the actual fix requires the avr8js emulator, which only runs on CI. PR CI on `uno AVR8JS Test` is the authoritative check.
`bash lint` clean.
Scope
🤖 Generated with Claude Code
Summary by CodeRabbit