Skip to content

fix(mp3): make the decode stack watermark reproducible (#4106) - #4114

Merged
zackees merged 1 commit into
masterfrom
fix/4106-watermark-reproducible
Sep 1, 2026
Merged

zackees merged 1 commit into
masterfrom
fix/4106-watermark-reproducible

Conversation

@zackees

@zackees zackees commented Sep 1, 2026 •

Copy link
Copy Markdown
Member

The decode stack watermark gave exact-but-different answers on different CI runners while the decoder was byte-identical — 1736 and 3336 for the retired Helix backend, each exact, one reproduced on a re-run. #4105 demoted it to informational as a stopgap. The issue asked for the mechanism to be instrumented rather than reasoned about, so that is what this does.

It was glibc

ci/codec_memory/watermark.cpp forwarded the decoder's memcpy/memmove/memset to libc. The metric is "the deepest byte anything disturbed", so glibc's own block-move frames sat inside the measured window — and those are large and differ between glibc builds and CPU dispatch paths.

How I got there, including the wrong turn

  1. Dumped the disturbed-span map. Dense and contiguous to depth 2696, locally deterministic at 2952 across three runs. So: not noise, something specific is down there.
  2. Tested the obvious hypothesis first, and it was wrong. I expected glibc IFUNC picking different memcpy variants per CPU — that shape explains "exact but host-dependent" perfectly. Forcing variants with GLIBC_TUNABLES (-AVX2_Usable, -AVX2_Usable,-AVX_Usable,-SSE4_1,-SSSE3) moved the number not at all. Hypothesis dead.
  3. Instrumented the decoder with -finstrument-functions, recording the minimum stack pointer at every function entry. Deepest decoder frame: mp3d_DCT_II at depth 1896 — 800 bytes shallower than the paint was reporting. So the decoder was being charged for something below itself.
  4. Confirmed by removal. Swapped the libc forwarders for plain loops: 2952 → 2056, and every disturbed span below ~1800 vanished. That is the ~900 bytes, and it is glibc's.

The fix

The harness uses its own loops. The pointers are volatile so loop-idiom recognition cannot turn them back into a memcpy call and silently restore the variance — a guarantee from the standard rather than a build flag someone can drop later. I verified the only remaining libc mem* call in the linked binary is the paint itself in startWatermark, which runs before the measured window and above it.

Measuring glibc was also measuring the wrong thing: the 2 KiB budget this audit enforces is an MCU budget, and no MCU links a vectorised memcpy — newlib's is a word loop much like these.

Why I trust the new number

The two independent instruments now corroborate each other:

instrument before after
compiler-derived callgraph (the actual gate) 2032 2032
live watermark 2696 2056
disagreement 664 24

24 bytes is the audit call boundary. That 664-byte disagreement was the bug visible in the ledger the whole time, and nobody had chased it.

Validation

  • Five consecutive runs: 2056 every time.
  • Unchanged under every GLIBC_TUNABLES setting tried.
  • LEDGER:PASS end to end, all other metrics byte-identical.
  • 1,111 Python tests.

INFORMATIONAL_METRICS is now empty and the metric is gated again like every other one — the stopgap is no longer needed. The set is kept (empty, documented) rather than deleted, so re-demoting a metric later doesn't require reinventing the mechanism.

Closes #4106

Summary by CodeRabbit

  • Bug Fixes

    • Improved stack-watermark measurements by removing host-dependent variation.
    • Preserved correct behavior for overlapping memory operations in measurement routines.
  • Documentation

    • Updated recorded stack-watermark results and confirmed the metric is reproducible and regression-gated.
    • Documented agreement between static callgraph estimates and live measurements.

The metric landed on exact-but-different values on different CI runners while
the decoder was byte-identical -- 1736 and 3336 for the retired Helix backend,
each exact, one reproduced on a re-run. #4105 demoted it to informational as a
stopgap. The issue asked for the mechanism to be instrumented rather than
reasoned about, so that is what this does.

**It was glibc.** `watermark.cpp` forwarded the decoder's `memcpy`/`memmove`/
`memset` to libc, so glibc's own frames sat inside the measured window. The
metric is the deepest byte *anything* disturbed, and glibc's block-move
implementations are large and differ between builds and CPU dispatch paths.

Measured, not inferred, in this order:

- Dumped every disturbed span. Dense and contiguous to depth 2696, locally
  deterministic at 2952 across runs -- so not noise.
- Tested the obvious suspect first and it was wrong: forcing different `mem*`
  variants via `GLIBC_TUNABLES` (`-AVX2_Usable`, `-SSE4_1,-SSSE3`) moved the
  number not at all.
- Instrumented the decoder with `-finstrument-functions` and recorded the
  minimum stack pointer per function entry. Deepest decoder frame is
  `mp3d_DCT_II` at 1896 -- 800 bytes shallower than the paint reported, so
  something below the decoder was being counted.
- Replaced the libc forwarders with plain loops: 2952 -> 2056, and every span
  below ~1800 vanished. That is the ~900 bytes, and it is glibc's.

The harness now uses its own loops. The pointers are `volatile` so loop-idiom
recognition cannot call `memcpy` right back and silently restore the variance
-- a guarantee from the standard rather than a build flag someone can drop.
Verified the only remaining libc `mem*` call in the binary is the paint itself
in `startWatermark`, which runs before the measured window and above it.

Measuring glibc was also measuring the wrong thing: the 2 KiB budget is an MCU
budget, and no MCU links a vectorised `memcpy`.

Two independent instruments now corroborate each other. The compiler-derived
callgraph says 2032 and the live watermark says 2056 -- a 24-byte gap that is
the audit call boundary. Before the fix they disagreed by 664 bytes.

So the metric is gated again: `INFORMATIONAL_METRICS` is now empty and the
ledger row is 2056. Five consecutive runs identical, unchanged under every
`GLIBC_TUNABLES` setting tried, and `LEDGER:PASS` end to end.

Closes #4106

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01ByD5ZKrnFJuchjRmPiQdd5
@coderabbitai

coderabbitai Bot commented Sep 1, 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: Team

Run ID: 4e3d5410-2e86-4391-9afc-54aa0b66890b

📥 Commits

Reviewing files that changed from the base of the PR and between 6990cdc and 921ead7.

📒 Files selected for processing (3)
  • ci/codec_memory/audit.py
  • ci/codec_memory/watermark.cpp
  • codec_memory_ledger.md

Included review availability: Your plan provides up to 8 included reviews per hour; 7 remain after this review.


📝 Walkthrough

Walkthrough

The watermark harness replaces libc memory calls with volatile byte loops. The audit now regression-gates stack-watermark-observed, and the ledger records updated reproducible measurements.

Changes

Codec memory watermark

Layer / File(s) Summary
Volatile watermark memory primitives
ci/codec_memory/watermark.cpp
fl::memcpy, fl::memmove, and fl::memset use volatile byte loops. memmove preserves overlap-safe copy direction.
Watermark audit and ledger baseline
ci/codec_memory/audit.py, codec_memory_ledger.md
stack-watermark-observed is removed from informational metrics and is regression-gated. The ledger records the updated minimp3-float measurement and loop-based measurement details.

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

Merge Risk: ⚪ Minimal · up to 921ea

This PR makes the CI memory measurement deterministic and restores regression gating without changing production behavior or public interfaces. No actionable merge-blocking risk remains beyond normal checks and review.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 2 files. (1 skipped: 1 … Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the primary change: making the MP3 decode stack watermark reproducible.
Linked Issues check ✅ Passed The changes satisfy issue #4106. They remove libc-related measurement variability, produce a reproducible watermark, and restore regression gating for the metric.
Out of Scope Changes check ✅ Passed All changes are directly related to reproducing the MP3 stack watermark and restoring its audit enforcement. No unrelated changes are present.
Full details: Docstring Coverage

Explanation

Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 2 files. (1 skipped: 1 unsupported.)

  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/4106-watermark-reproducible

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.

@zackees
zackees merged commit a530181 into master Sep 1, 2026
32 checks passed
@fastled-project-sync fastled-project-sync Bot moved this from Triage to Done in FastLED Tracker Sep 1, 2026
@zackees
zackees deleted the fix/4106-watermark-reproducible branch September 1, 2026 12:57
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.

test(mp3): helix stack-watermark-observed is flaky and can fail the memory audit spuriously

1 participant