Skip to content

Commit

Permalink
[benchmark] Fix potential use-after-free due to not following Rule of…
Browse files Browse the repository at this point in the history
… 3 (#1091)
  • Loading branch information
abdelrahim-hentabli authored Oct 22, 2024
1 parent 117ec5b commit dd632f9
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 0 deletions.
15 changes: 15 additions & 0 deletions tools/benchmarks/include/ops/c_api/base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@ class operation_base_t : public ops::operation_base_t<DerivedT> {
public:
operation_base_t() noexcept {}
~operation_base_t() noexcept {}
operation_base_t(const operation_base_t& other) {
cache_control_ = other.cache_control_;
numa_id_ = other.numa_id_;
if (other.job_) { init_lib_impl(); }
}

operation_base_t& operator=(const operation_base_t& other) {
if (this != &other) {
deinit_lib_impl();
cache_control_ = other.cache_control_;
numa_id_ = other.numa_id_;
if (other.job_) { init_lib_impl(); }
}
return *this;
}

void init_lib_impl() {
if (!cache_control_) throw std::runtime_error("manual cache control option is not supported in C API");
Expand Down
2 changes: 2 additions & 0 deletions tools/benchmarks/include/ops/c_api/crc64.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class crc64_t : public operation_base_t<crc64_t<path>> {
~crc64_t() noexcept(false) { deinit_lib_impl(); }

protected:
// WARNING: initializaiton of buffers must be re-done on copying or assigning object
void init_buffers_impl(const params_t& params) {
params_ = params;
if (params.original_size_)
Expand All @@ -45,6 +46,7 @@ class crc64_t : public operation_base_t<crc64_t<path>> {
data_.resize(params_.p_stream_->buffer.size() * 10);
}

// WARNING: initializaiton of lib params must be re-done on copying or assigning object
void init_lib_params_impl() noexcept {
job_->next_in_ptr = const_cast<std::uint8_t*>(params_.p_stream_->buffer.data());
job_->available_in = static_cast<std::uint32_t>(params_.p_stream_->buffer.size());
Expand Down
2 changes: 2 additions & 0 deletions tools/benchmarks/include/ops/c_api/deflate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ class deflate_t : public operation_base_t<deflate_t<path>> {
~deflate_t() noexcept(false) { deinit_lib_impl(); }

protected:
// WARNING: initializaiton of buffers must be re-done on copying or assigning object
void init_buffers_impl(const params_t& params) {
params_ = params;
stream_.resize(params_.p_source_data_->buffer.size() * 1.5);
}

// WARNING: initializaiton of lib params must be re-done on copying or assigning object
void init_lib_params_impl() {
job_->next_in_ptr = const_cast<std::uint8_t*>(params_.p_source_data_->buffer.data());
job_->available_in = static_cast<std::uint32_t>(params_.p_source_data_->buffer.size());
Expand Down
2 changes: 2 additions & 0 deletions tools/benchmarks/include/ops/c_api/inflate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class inflate_t : public operation_base_t<inflate_t<path>> {
~inflate_t() noexcept(false) { deinit_lib_impl(); }

protected:
// WARNING: initializaiton of buffers must be re-done on copying or assigning object
void init_buffers_impl(const params_t& params) {
params_ = params;
if (params.original_size_)
Expand All @@ -40,6 +41,7 @@ class inflate_t : public operation_base_t<inflate_t<path>> {
data_.resize(params_.p_stream_->buffer.size() * 10);
}

// WARNING: initializaiton of lib params must be re-done on copying or assigning object
void init_lib_params_impl() noexcept {
job_->next_in_ptr = const_cast<std::uint8_t*>(params_.p_stream_->buffer.data());
job_->available_in = static_cast<std::uint32_t>(params_.p_stream_->buffer.size());
Expand Down

0 comments on commit dd632f9

Please sign in to comment.