Repository navigation
mgr/PerfCounterInstance: guard get_latest_data_avg() against empty/zero-capacity avg_buffer - #72353
Open
stzuraski898 wants to merge 3 commits into
Conversation
Contributor
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
The guard safely removes the undefined behavior and both empty-buffer cases are covered by focused tests.
Review effort: Balanced
Findings: None
What changed in this PR
Prevents undefined behavior when requesting the latest average from an empty performance-counter buffer.
Changes:
- Returns a zero-valued sentinel for empty average buffers.
- Adds regression and general
PerfCounterInstanceunit tests. - Registers the new test target.
| File | Description |
|---|---|
src/mgr/PerfCounterInstance.h |
Adds the empty-buffer guard. |
src/test/mgr/test_perfcounterinstance.cc |
Tests buffer behavior and the regression. |
src/test/mgr/CMakeLists.txt |
Builds and registers the unit tests. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Introduce src/test/mgr/test_perfcounterinstance.cc and its unittest_mgr_perfcounterinstance CMake target. This scaffolding does not exist on ceph-origin/main; it is cherry-picked from ceph#71237 as a temporary measure until that PR merges, and should be reconciled when the PR lands. Only the PerfCounterInstance test file and its target are taken; no other test files from that PR are imported. The CMake target is adapted to the base branch's conventions (compiling src/mgr/PerfCounterInstance.cc directly), mirroring the existing unittest_mgr_daemonstate target. Coordinated with #81019, which cherry-picks the same file; if both land, the scaffolding is added once. Fixes: https://tracker.ceph.com/issues/81020 Signed-off-by: Steven Zuraski <[email protected]> Assisted-by: Claude Code (Opus 4.8)
…ro-capacity avg_buffer Wrong behaviour: get_latest_data_avg() returned avg_buffer.back() with no emptiness guard. boost::circular_buffer::back() on an empty buffer is undefined behaviour. Root cause: the accessor was introduced without a precondition check in b421142 ("mgr: add get_latest_counter() to C++ -> Python interface") and extracted verbatim into this translation unit by 35f6dd0. The constructor sizes exactly one of the two circular buffers based on the PERFCOUNTER_LONGRUNAVG bit: for a scalar (non-avg) instance, avg_buffer is given capacity 0 and is therefore *always* empty. While callers currently gate on PERFCOUNTER_LONGRUNAVG, this is a latent defect: avg_buffer has capacity 0 for scalar instances and is not populated before a read in edge cases; this guard prevents undefined behaviour if a future code path reaches get_latest_data_avg() without the PERFCOUNTER_LONGRUNAVG gate, and also provides defense-in-depth for an avg instance prior to receiving any samples. Fix: return a defined zero AvgDataPoint sentinel when avg_buffer is empty, otherwise return the latest sample. This preserves the const-reference signature so callers are unchanged, and gives the accessor defined "no data" behaviour consistent with the documented intent. Fixes: https://tracker.ceph.com/issues/81020 Signed-off-by: Steven Zuraski <[email protected]> Assisted-by: Claude Code (Opus 4.8)
…) guard Add regression tests verifying get_latest_data_avg() returns a defined zero sentinel when avg_buffer is empty (both for scalar instances where avg_buffer has capacity 0 and is always empty, and for avg instances prior to receiving any samples), and returns the pushed sample after push_avg(). Fixes: https://tracker.ceph.com/issues/81020 Signed-off-by: Steven Zuraski <[email protected]> Assisted-by: Claude Code (Opus 4.8)
stzuraski898
force-pushed
the
wip-sz-81020-perfcounterinstance-get-latest-data-avg-guard
branch
from
October 26, 2026 20:11
481ddcb to
5dffeee
Compare
4 of 14 tasks
stzuraski898
added a commit
to stzuraski898/ceph
that referenced
this pull request
Nov 23, 2026
get_latest_data() returned buffer.back() with no emptiness check. buffer is a boost::circular_buffer, and back() on an empty buffer is undefined behaviour (it dereferences an invalid iterator). The accessor was introduced without a precondition check in b421142 ("mgr: add get_latest_counter() to C++ -> Python interface") and extracted verbatim into this header by 35f6dd0. Return a zero-valued static const DataPoint sentinel when buffer.empty(), otherwise buffer.back(). The const-reference, zero-copy contract is preserved, so callers are unchanged. Reachability is low: DaemonPerfCounters::update() inserts a new instance and pushes its first sample atomically under daemon->lock, and readers take the same lock. The empty state is only reachable if report decoding throws after the insert but before the push (a corrupt or truncated MMgrReport), or if the scalar accessor is called on an avg-type instance, whose scalar buffer has capacity 0. This is defensive hardening against genuine UB rather than a routine crash. Tests: add test_perfcounterinstance.cc and the unittest_mgr_perfcounterinstance target, cherry-picked from ceph#71237, plus regression tests for the empty-buffer path. ceph#72353 adds the same scaffolding; whichever PR lands second must be rebased to add only its test cases. Fixes: https://tracker.ceph.com/issues/81019 Assisted-by: IBM Bob Signed-off-by: Steven Zuraski <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
PerfCounterInstance::get_latest_data_avg()returnedavg_buffer.back()withno emptiness check.
avg_bufferis aboost::circular_buffer, andback()onan empty (or zero-capacity) buffer is undefined behaviour.
Fix
Return a zero-valued
static const AvgDataPointsentinel whenavg_buffer.empty(), otherwiseavg_buffer.back().empty()also covers thezero-capacity case. The const-reference contract is preserved.
Reachability / likelihood
Not reachable on current code; this is a latent defect / defense-in-depth.
All callers gate on
PERFCOUNTER_LONGRUNAVG, so a scalar instance (the onlycase where
avg_bufferis zero-capacity) is never passed to this accessor. Foravg instances,
DaemonPerfCounters::update()inserts and callspush_avg()atomically under
daemon->lock, and readers take the same lock, so an avginstance is never observable with an empty
avg_buffer. The guard prevents UBif a future code path reaches this accessor without the gate.
Tests
Adds regression tests for the empty/zero-capacity path. The test file
(
test_perfcounterinstance.cc) and CMake target are cherry-picked from #71237.Fixes: https://tracker.ceph.com/issues/81020
Contribution Guidelines
To sign and title your commits, please refer to Submitting Patches to Ceph.
If you are submitting a fix for a stable branch (e.g. "quincy"), please refer to Submitting Patches to Ceph - Backports for the proper workflow.
When filling out the below checklist, you may click boxes directly in the GitHub web UI. When entering or editing the entire PR message in the GitHub web UI editor, you may also select a checklist item by adding an
xbetween the brackets:[x]. Spaces and capitalization matter when checking off items this way.Checklist
Show available Jenkins commands
jenkins test classic perfJenkins Job | Jenkins Job Definitionjenkins test crimson perfJenkins Job | Jenkins Job Definitionjenkins test signedJenkins Job | Jenkins Job Definitionjenkins test make checkJenkins Job | Jenkins Job Definitionjenkins test make check arm64Jenkins Job | Jenkins Job Definitionjenkins test submodulesJenkins Job | Jenkins Job Definitionjenkins test dashboardJenkins Job | Jenkins Job Definitionjenkins test dashboard cephadmJenkins Job | Jenkins Job Definitionjenkins test apiJenkins Job | Jenkins Job Definitionjenkins test docsReadTheDocs | Github Workflow Definitionjenkins test ceph-volume allJenkins Jobs | Jenkins Jobs Definitionjenkins test windowsJenkins Job | Jenkins Job Definitionjenkins test rook e2eJenkins Job | Jenkins Job DefinitionYou must only issue one Jenkins command per-comment. Jenkins does not understand
comments with more than one command.