Skip to content

fix(watchdog/avr): mark .init3 hook __attribute__((naked)) so it falls through (Closes #2798) - #2800

Merged
zackees merged 2 commits into
masterfrom
fix/avr-watchdog-init3-naked
Jun 5, 2026
Merged

zackees merged 2 commits into
masterfrom
fix/avr-watchdog-init3-naked

Conversation

@zackees

@zackees zackees commented Jun 5, 2026 •

Copy link
Copy Markdown
Member

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:

  • `.init3` must be `naked` — code placed there falls through to `.init4`, it does not return.
  • The three statements the hook performs (`MCUSR` capture, `MCUSR = 0`, `wdt_disable()`) compile to plain `STS` / inline-asm sequences with no stack temps, so the general "naked is fragile across toolchains" concern doesn't apply here.

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

  • Refactor
    • Improved watchdog initialization on AVR platforms to use a startup-safe low-level form, enhancing boot reliability while preserving existing behavior and public APIs.
  • Documentation
    • Updated internal docs to reflect the new startup implementation.

…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]>
@coderabbitai

coderabbitai Bot commented Jun 5, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 438a6073-4d4b-4394-8200-12cf8facf7ab

📥 Commits

Reviewing files that changed from the base of the PR and between 1e5fd52 and fe36bcd.

📒 Files selected for processing (1)
  • src/platforms/avr/watchdog_avr.impl.hpp
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/platforms/avr/watchdog_avr.impl.hpp

📝 Walkthrough

Walkthrough

The AVR watchdog .init3 initialization hook is corrected by adding the __attribute__((naked)) attribute to the fastled_watchdog_init3() function. The change prevents the compiler from emitting function prologue, epilogue, and return instructions that would cause execution to jump to uninitialized memory instead of falling through to the next initialization stage.

Changes

Watchdog initialization chain fix

Layer / File(s) Summary
Watchdog init3 naked attribute and register preservation
src/platforms/avr/watchdog_avr.impl.hpp
The fastled_watchdog_init3() function receives __attribute__((naked, used, section(".init3"))). The body captures MCUSR into sAvrCapturedMcusr, clears MCUSR, calls wdt_disable(), and intentionally omits an explicit return so execution falls through to the next init stage.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • FastLED/FastLED#2780: Modifies the same src/platforms/avr/watchdog_avr.impl.hpp area and relates to whether the watchdog init-chain hook is compiled.

Poem

🐰 A tiny hook that saved the day,
No stray ret to run away.
Now naked, quiet, in its place,
MCUSR kept, watchdog off — no race. 🥕

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: marking the .init3 hook with attribute((naked)) to fix the fallthrough issue. It is specific, concise, and clearly conveys the primary fix.
Linked Issues check ✅ Passed The PR fully addresses the objectives from #2798: it marks fastled_watchdog_init3() with attribute((naked)), removes inline, preserves the three core operations (MCUSR capture/clear, wdt_disable), and updates documentation to explain the naked requirement.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing the .init3 watchdog hook in src/platforms/avr/watchdog_avr.impl.hpp. No unrelated changes to other platforms or unrelated code paths are present.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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/avr-watchdog-init3-naked

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.

❤️ Share

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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

📥 Commits

Reviewing files that changed from the base of the PR and between 787de45 and 1e5fd52.

📒 Files selected for processing (1)
  • src/platforms/avr/watchdog_avr.impl.hpp

Comment thread src/platforms/avr/watchdog_avr.impl.hpp Resolved
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]>
@zackees
zackees merged commit 86fd901 into master Jun 5, 2026
9 checks passed
@fastled-project-sync fastled-project-sync Bot moved this from Triage to Done in FastLED Tracker Jun 5, 2026
@zackees
zackees deleted the fix/avr-watchdog-init3-naked branch August 21, 2026 19:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

broken build: uno AVR8JS Test — watchdog .init3 hook missing __attribute__((naked)), ret jumps to garbage

1 participant