Repository navigation
mgr/PerfCounterInstance: guard get_latest_data() against empty buffer - #72352
stzuraski898 wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
The equivalent average-counter accessor remains unsafe on an empty buffer.
Review effort: Balanced
Findings: 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
PerfCounterInstanceunit 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.
| 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(); |
f6db6b8 to
e66490b
Compare
NitzanMordhai
left a comment
There was a problem hiding this comment.
@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.
| add_ceph_unittest(unittest_mgr_perfcounterinstance) | ||
| target_link_libraries(unittest_mgr_perfcounterinstance | ||
| ceph-common | ||
| Python3::Python |
There was a problem hiding this comment.
Do we really need Python3::Python ?
There was a problem hiding this comment.
We don't, will make a note to fix in #71237, since the CMakeLists entry will originate there.
e66490b to
d1be0a8
Compare
|
This pull request can no longer be automatically merged: a rebase is needed and changes have to be manually resolved |
|
@stzuraski898 The following error occurs while creating the Shaman build. Please investigate and resolve the issue, then add the |
d1be0a8 to
4b3b587
Compare
4b3b587 to
3d78c81
Compare
|
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
3d78c81 to
eed5022
Compare
|
@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...) |

Summary
PerfCounterInstance::get_latest_data()returnedbuffer.back()with noemptiness check.
bufferis aboost::circular_buffer, andback()on anempty buffer is undefined behaviour (it dereferences an invalid iterator).
Fix
Return a zero-valued
static const DataPointsentinel whenbuffer.empty(),otherwise
buffer.back(). The const-reference, zero-copy contract is preserved.Reachability / likelihood
Low.
DaemonPerfCounters::update()inserts a new instance and pushes its firstsample atomically under
daemon->lock, and readers(
ActivePyModules::with_perf_counters) take the same lock, so an instancepresent 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 genuineUB rather than a routine crash.
Note: the companion avg accessor
get_latest_data_avg()has the same latentissue 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.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
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.