Skip to content

crush: use std::vector instead of variable length arrays#56428

Merged
yuriw merged 1 commit intoceph:mainfrom
tchaikov:wip-crush-no-vla
Jun 5, 2024
Merged

crush: use std::vector instead of variable length arrays#56428
yuriw merged 1 commit intoceph:mainfrom
tchaikov:wip-crush-no-vla

Conversation

@tchaikov
Copy link
Contributor

@tchaikov tchaikov commented Mar 24, 2024

despite that variable length arrays (VLA for short) has been around for a long time, it is an extension supported by GCC and Clang, and is not a part of C++ standard, its implementation allocates the dynamically sized array on stack, hence is a source of potential stack overflow.

when compiling with Clang, it complains. so in this change, we switch to std::vector<>, which is defined by the C++ standard, and it allocates the storage on heap, so it is immune to the possible stack overflow problem.

/home/kefu/dev/ceph/src/crush/CrushWrapper.h:1613:16: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension]
 1613 |     int rawout[maxout];
      |                ^~~~~~
/home/kefu/dev/ceph/src/crush/CrushWrapper.h:1613:16: note: function parameter 'maxout' with unknown value cannot be used in a constant expression
/home/kefu/dev/ceph/src/crush/CrushWrapper.h:1610:60: note: declared here
 1610 |   void do_rule(int rule, int x, std::vector<int>& out, int maxout,
      |                                                            ^
/home/kefu/dev/ceph/src/crush/CrushWrapper.h:1614:15: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension]
 1614 |     char work[crush_work_size(crush, maxout)];
      |               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/kefu/dev/ceph/src/crush/CrushWrapper.h:1614:31: note: implicit use of 'this' pointer is only allowed within the evaluation of a call to a 'constexpr' member function
 1614 |     char work[crush_work_size(crush, maxout)];
      |                               ^
2 warnings generated.

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
  • jenkins retest this please
  • jenkins test classic perf
  • jenkins test crimson perf
  • jenkins test signed
  • jenkins test make check
  • jenkins test make check arm64
  • jenkins test submodules
  • jenkins test dashboard
  • jenkins test dashboard cephadm
  • jenkins test api
  • jenkins test docs
  • jenkins render docs
  • jenkins test ceph-volume all
  • jenkins test ceph-volume tox
  • jenkins test windows
  • jenkins test rook e2e

despite that variable length arrays (VLA for short) has been around
for a long time, it is an extension supported by GCC and Clang, and is
not a part of C++ standard, its implementation allocates the dynamically
sized array on stack, hence is a source of potential stack overflow.

when compiling with Clang, it complains. so in this change, we switch
to std::vector<>, which is defined by the C++ standard, and it allocates
the storage on heap, so it is immune to the possible stack overflow problem.

```
/home/kefu/dev/ceph/src/crush/CrushWrapper.h:1613:16: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension]
 1613 |     int rawout[maxout];
      |                ^~~~~~
/home/kefu/dev/ceph/src/crush/CrushWrapper.h:1613:16: note: function parameter 'maxout' with unknown value cannot be used in a constant expression
/home/kefu/dev/ceph/src/crush/CrushWrapper.h:1610:60: note: declared here
 1610 |   void do_rule(int rule, int x, std::vector<int>& out, int maxout,
      |                                                            ^
/home/kefu/dev/ceph/src/crush/CrushWrapper.h:1614:15: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension]
 1614 |     char work[crush_work_size(crush, maxout)];
      |               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/kefu/dev/ceph/src/crush/CrushWrapper.h:1614:31: note: implicit use of 'this' pointer is only allowed within the evaluation of a call to a 'constexpr' member function
 1614 |     char work[crush_work_size(crush, maxout)];
      |                               ^
2 warnings generated.
```

Signed-off-by: Kefu Chai <tchaikov@gmail.com>
@tchaikov tchaikov requested a review from a team as a code owner March 24, 2024 10:50
@github-actions github-actions bot added the core label Mar 24, 2024
@tchaikov
Copy link
Contributor Author

@rzarzynski hi Radek, could you help review this change?

Copy link
Contributor

@rzarzynski rzarzynski left a comment

Choose a reason for hiding this comment

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

IMHO this looks sane!

@yuriw
Copy link
Contributor

yuriw commented May 3, 2024

This PR is under test in https://tracker.ceph.com/issues/65797.

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