Skip to content

mgr/PerfCounterInstance: guard get_latest_data_avg() against empty/zero-capacity avg_buffer - #72353

Open
stzuraski898 wants to merge 3 commits into
ceph:mainfrom
stzuraski898:wip-sz-81020-perfcounterinstance-get-latest-data-avg-guard
Open

stzuraski898 wants to merge 3 commits into
ceph:mainfrom
stzuraski898:wip-sz-81020-perfcounterinstance-get-latest-data-avg-guard

Conversation

@stzuraski898

@stzuraski898 stzuraski898 commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

PerfCounterInstance::get_latest_data_avg() returned avg_buffer.back() with
no emptiness check. avg_buffer is a boost::circular_buffer, and back() on
an empty (or zero-capacity) buffer is undefined behaviour.

Fix

Return a zero-valued static const AvgDataPoint sentinel when
avg_buffer.empty(), otherwise avg_buffer.back(). empty() also covers the
zero-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 only
case where avg_buffer is zero-capacity) is never passed to this accessor. For
avg instances, DaemonPerfCounters::update() inserts and calls push_avg()
atomically under daemon->lock, and readers take the same lock, so an avg
instance is never observable with an empty avg_buffer. The guard prevents UB
if 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.

Coordination: #72352 adds the same test file and
unittest_mgr_perfcounterinstance CMake target. To avoid a duplicate-file /
duplicate-target build break, the scaffolding will be owned by a single PR and
the other rebased to add only its test cases.

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 x between the brackets: [x]. Spaces and capitalization matter when checking off items this way.

Checklist

  • Tracker (select at least one)
    • References tracker ticket
    • Very recent bug; references commit where it was introduced
    • New feature (ticket optional)
    • Doc update (no ticket needed)
    • Code cleanup (no ticket needed)
  • Component impact
    • Affects Dashboard, opened tracker ticket
    • Affects Orchestrator, opened tracker ticket
    • No impact that needs to be tracked
  • Documentation (select at least one)
    • Updates relevant documentation
    • No doc update is appropriate
  • Tests (select at least one)
Show available Jenkins commands

You must only issue one Jenkins command per-comment. Jenkins does not understand
comments with more than one command.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 PerfCounterInstance unit 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
stzuraski898 force-pushed the wip-sz-81020-perfcounterinstance-get-latest-data-avg-guard branch from 481ddcb to 5dffeee Compare October 26, 2026 20:11
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants