Skip to content

mgr/PerfCounterInstance: guard get_latest_data() against empty buffer - #72352

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

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

Conversation

@stzuraski898

@stzuraski898 stzuraski898 commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

PerfCounterInstance::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).

Fix

Return a zero-valued static const DataPoint sentinel when buffer.empty(),
otherwise buffer.back(). The const-reference, zero-copy contract is preserved.

Reachability / likelihood

Low. DaemonPerfCounters::update() inserts a new instance and pushes its first
sample atomically under daemon->lock, and readers
(ActivePyModules::with_perf_counters) take the same lock, so an instance
present in the map has always received at least one sample by the time it is
queried. Dispatch also routes scalar vs. avg counters to the matching buffer.
The empty-buffer state is therefore not observable on the normal path; it is
only reachable if report decoding throws after the insert but before the push
(a corrupt/truncated MMgrReport). This is defensive hardening against genuine
UB rather than a routine crash.

Note: the companion avg accessor get_latest_data_avg() has the same latent
issue and is addressed in #72353 (see coordination note below).

Tests

Adds a regression test for the empty-buffer path. The test file
(test_perfcounterinstance.cc) and CMake target are cherry-picked from #71237.

Coordination: #72353 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/81019

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.

@stzuraski898
stzuraski898 requested a review from a team as a code owner October 2, 2026 21:49
@stzuraski898
stzuraski898 requested a balanced review from Copilot and removed request for a team October 2, 2026 21:50

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

🟡 Changes recommended

The equivalent average-counter accessor remains unsafe on an empty buffer.

Review effort: Balanced
Findings: 1 High severity

Open (1)
What changed in this PR

Prevents undefined behavior when querying an unsampled scalar manager performance counter.

Changes:

  • Returns a zero-valued sentinel for empty scalar buffers.
  • Adds PerfCounterInstance unit coverage and its CMake target.
File Description
src/​mgr/​PerfCounterInstance.h Guards empty scalar buffers.
src/​test/​mgr/​test_perfcounterinstance.cc Adds unit tests, including the regression case.
src/​test/​mgr/​CMakeLists.txt Registers the new test executable.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/mgr/PerfCounterInstance.h Outdated
return buffer.back();
// back() is UB on an empty circular_buffer; return a zero sentinel.
static const DataPoint empty{utime_t(), 0};
return buffer.empty() ? empty : buffer.back();

@NitzanMordhai NitzanMordhai 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.

@stzuraski898 i don't think that it can't happen but it not harm to have that code in place. please make sure that the commit message is correct as well and address the comment i added here.

Comment thread src/test/mgr/CMakeLists.txt Outdated
add_ceph_unittest(unittest_mgr_perfcounterinstance)
target_link_libraries(unittest_mgr_perfcounterinstance
ceph-common
Python3::Python

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.

Do we really need Python3::Python ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We don't, will make a note to fix in #71237, since the CMakeLists entry will originate there.

@stzuraski898
stzuraski898 force-pushed the wip-sz-81019-perfcounterinstance-get-latest-data-guard branch from e66490b to d1be0a8 Compare October 28, 2026 19:39
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown

This pull request can no longer be automatically merged: a rebase is needed and changes have to be manually resolved

@SrinivasaBharath

Copy link
Copy Markdown
Contributor

@stzuraski898 The following error occurs while creating the Shaman build. Please investigate and resolve the issue, then add the needs-qa label.
PR #72352 has merge conflicts with previously merged changes. Please resolve conflicts manually or rebase the PR.

@stzuraski898
stzuraski898 force-pushed the wip-sz-81019-perfcounterinstance-get-latest-data-guard branch from d1be0a8 to 4b3b587 Compare November 9, 2026 21:54
@stzuraski898
stzuraski898 force-pushed the wip-sz-81019-perfcounterinstance-get-latest-data-guard branch from 4b3b587 to 3d78c81 Compare November 23, 2026 23:46
@stzuraski898

Copy link
Copy Markdown
Contributor Author

Rebased, may want to re-review @NitzanMordhai

Introduce src/test/mgr/test_perfcounterinstance.cc and its
unittest_mgr_perfcounterinstance CMake target.

This commit is temporary scaffolding cherry-picked from
ceph#71237 (branch wip-sz-77389-wideMgrScaffolding)
and should be taken out once PR 71237 is merged.

Only the PerfCounterInstance test file and its target are taken; no other
test files from that PR are imported. The CMake target compiles
src/mgr/PerfCounterInstance.cc directly, mirroring the existing
unittest_mgr_daemonstate target.

Signed-off-by: Steven Zuraski <[email protected]>
Assisted-by: IBM Bob
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.

Fixes: https://tracker.ceph.com/issues/81019
Signed-off-by: Steven Zuraski <[email protected]>
Assisted-by: IBM Bob
Add regression unit tests verifying that get_latest_data() returns a defined
zero sentinel when buffer is empty (both for avg instances where buffer has
capacity 0 and is always empty, and for scalar instances prior to receiving
any samples), and returns the pushed sample after push().

Fixes: https://tracker.ceph.com/issues/81019
Signed-off-by: Steven Zuraski <[email protected]>
Assisted-by: IBM Bob
@stzuraski898
stzuraski898 force-pushed the wip-sz-81019-perfcounterinstance-get-latest-data-guard branch from 3d78c81 to eed5022 Compare December 1, 2026 17:23
@NitzanMordhai

Copy link
Copy Markdown
Contributor

@stzuraski898 i think you are still have extra commits in that pr. Please remove the one that isn't needed (also Python3::Python still there...)

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.

4 participants