Repository navigation
REQ: keep the sketch valid when an allocation or an item copy throws #587
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,9 +50,34 @@ items_(nullptr) // allocated on first use, see reserve() | |
|
|
||
| template<typename T, typename C, typename A> | ||
| req_compactor<T, C, A>::~req_compactor() { | ||
| destroy_items(); | ||
| } | ||
|
|
||
| template<typename T, typename C, typename A> | ||
| void req_compactor<T, C, A>::destroy_items() { | ||
| if (items_ != nullptr) { | ||
| for (auto it = begin(); it != end(); ++it) (*it).~T(); | ||
| allocator_.deallocate(items_, capacity_); | ||
| items_ = nullptr; | ||
| } | ||
| } | ||
|
|
||
| template<typename T, typename C, typename A> | ||
| template<typename O> | ||
| void req_compactor<T, C, A>::copy_items(const O& other) { | ||
| if (other.items_ == nullptr) return; | ||
| items_ = allocator_.allocate(capacity_); | ||
| const uint32_t from = hra_ ? capacity_ - num_items_ : 0; | ||
| const uint32_t to = hra_ ? capacity_ : num_items_; | ||
| uint32_t i = from; | ||
| try { | ||
| for (; i < to; ++i) new (items_ + i) T(other.items_[i]); | ||
| } catch (...) { | ||
| // the destructor doesn't run if a constructor throws, so release the items here | ||
| for (uint32_t j = from; j < i; ++j) items_[j].~T(); | ||
| allocator_.deallocate(items_, capacity_); | ||
| items_ = nullptr; | ||
| throw; | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -72,12 +97,7 @@ num_items_(other.num_items_), | |
| capacity_(other.capacity_), | ||
| items_(nullptr) | ||
| { | ||
| if (other.items_ != nullptr) { | ||
| items_ = allocator_.allocate(capacity_); | ||
| const uint32_t from = hra_ ? capacity_ - num_items_ : 0; | ||
| const uint32_t to = hra_ ? capacity_ : num_items_; | ||
| for (uint32_t i = from; i < to; ++i) new (items_ + i) T(other.items_[i]); | ||
| } | ||
| copy_items(other); | ||
| } | ||
|
|
||
| template<typename T, typename C, typename A> | ||
|
|
@@ -155,14 +175,10 @@ num_items_(other.num_items_), | |
| capacity_(other.capacity_), | ||
| items_(nullptr) | ||
| { | ||
| if (other.items_ != nullptr) { | ||
| items_ = allocator_.allocate(capacity_); | ||
| const uint32_t from = hra_ ? capacity_ - num_items_ : 0; | ||
| const uint32_t to = hra_ ? capacity_ : num_items_; | ||
| for (uint32_t i = from; i < to; ++i) new (items_ + i) T(other.items_[i]); | ||
| if (sorted_ && !std::is_sorted(items_ + from, items_ + to, comparator_)) { | ||
| throw std::logic_error("items must be sorted"); | ||
| } | ||
| copy_items(other); | ||
| if (sorted_ && !std::is_sorted(begin(), end(), comparator_)) { | ||
| destroy_items(); // the destructor doesn't run if a constructor throws | ||
| throw std::logic_error("items must be sorted"); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -256,18 +272,42 @@ template<typename FwdC> | |
| void req_compactor<T, C, A>::merge(FwdC&& other) { | ||
| // TODO: swap if other is larger? | ||
| if (lg_weight_ != other.lg_weight_) throw std::logic_error("weight mismatch"); | ||
| state_ |= other.state_; | ||
| while (ensure_enough_sections()) {} | ||
| ensure_space(other.get_num_items()); | ||
| const uint32_t num = other.get_num_items(); | ||
| // the sections once the states are combined | ||
| const uint64_t new_state = state_ | other.state_; | ||
| sections new_sections = get_sections(); | ||
| bool more_sections = false; | ||
| while (next_sections(new_state, new_sections)) more_sections = true; | ||
|
|
||
| // allocate first, so that a failure changes nothing: | ||
| // the capacity the sections and the items need, grown the same way as before this was done up front | ||
| uint32_t new_capacity = capacity_; | ||
| if (more_sections && new_capacity < 2 * new_sections.nom_capacity()) new_capacity = 2 * new_sections.nom_capacity(); | ||
| if (num > 0) { | ||
| if (new_capacity == 0) new_capacity = 2 * new_sections.nom_capacity(); // as reserve() would | ||
| if (num_items_ + num > new_capacity) new_capacity = num_items_ + num + new_sections.nom_capacity(); | ||
| } | ||
| if (new_capacity != capacity_) grow(new_capacity); | ||
|
|
||
| // copy (or move) the items of other into the free space; if a copy throws, destroy the copies | ||
| T* from = hra_ ? begin() - num : end(); | ||
| uint32_t i = 0; | ||
| try { | ||
| auto other_it = other.begin(); | ||
| for (; i < num; ++i, ++other_it) new (from + i) T(conditional_forward<FwdC>(*other_it)); | ||
| } catch (...) { | ||
| for (uint32_t j = 0; j < i; ++j) from[j].~T(); | ||
| throw; | ||
| } | ||
|
|
||
| // nothing below allocates or copies | ||
| state_ = new_state; | ||
| if (more_sections) set_sections(new_sections); | ||
| sort(); | ||
| auto offset = hra_ ? capacity_ - num_items_ : num_items_; | ||
| auto from = hra_ ? begin() - other.get_num_items() : end(); | ||
| auto to = from + other.get_num_items(); | ||
| auto other_it = other.begin(); | ||
| for (auto it = from; it != to; ++it, ++other_it) new (it) T(conditional_forward<FwdC>(*other_it)); | ||
| if (!other.sorted_) std::sort(from, to, comparator_); | ||
| if (num_items_ > 0) std::inplace_merge(hra_ ? from : begin(), items_ + offset, hra_ ? end() : to, C()); | ||
| num_items_ += other.get_num_items(); | ||
| if (!other.sorted_) std::sort(from, from + num, comparator_); | ||
|
Comment on lines
306
to
+307
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since req_sketch<int> s(12, true);
s.update(1);
s.update(5);
s.merge(s); // leaves [1, 5, 1, 5] with sorted_ == true |
||
| const auto offset = hra_ ? capacity_ - num_items_ : num_items_; | ||
| if (num_items_ > 0) std::inplace_merge(hra_ ? from : begin(), items_ + offset, hra_ ? end() : from + num, comparator_); | ||
| num_items_ += num; | ||
| } | ||
|
|
||
| template<typename T, typename C, typename A> | ||
|
|
@@ -285,12 +325,18 @@ std::pair<uint32_t, uint32_t> req_compactor<T, C, A>::compact(req_compactor& nex | |
| const uint32_t secs_to_compact = std::min<uint32_t>(count_trailing_zeros_in_u64(~state_) + 1, num_sections_); | ||
| auto compaction_range = compute_compaction_range(secs_to_compact); | ||
| if (compaction_range.second - compaction_range.first < 2) throw std::logic_error("compaction range error"); | ||
| const auto num = (compaction_range.second - compaction_range.first) / 2; | ||
|
|
||
| // allocate first, so that a failure changes nothing: space in the next level, | ||
| // and the growth that more sections need once the state advances | ||
| next.ensure_space(num); | ||
| sections new_sections = get_sections(); | ||
| const bool more_sections = next_sections(state_ + 1, new_sections); | ||
| if (more_sections && capacity_ < 2 * new_sections.nom_capacity()) grow(2 * new_sections.nom_capacity()); | ||
|
|
||
| if ((state_ & 1) == 1) { coin_ = !coin_; } // for odd flip coin; | ||
| else { coin_ = random_utils::random_bit(); } // random coin flip | ||
|
|
||
| const auto num = (compaction_range.second - compaction_range.first) / 2; | ||
| next.ensure_space(num); | ||
| auto next_middle = hra_ ? next.begin() : next.end(); | ||
| auto next_empty = hra_ ? next.begin() - num : next.end(); | ||
| promote_evens_or_odds(begin() + compaction_range.first, begin() + compaction_range.second, coin_, next_empty); | ||
|
|
@@ -300,24 +346,35 @@ std::pair<uint32_t, uint32_t> req_compactor<T, C, A>::compact(req_compactor& nex | |
| num_items_ -= compaction_range.second - compaction_range.first; | ||
|
|
||
| ++state_; | ||
| ensure_enough_sections(); | ||
| if (more_sections) set_sections(new_sections); | ||
| return std::pair<uint32_t, uint32_t>( | ||
| num, | ||
| get_nom_capacity() - starting_nom_capacity | ||
| ); | ||
| } | ||
|
|
||
| template<typename T, typename C, typename A> | ||
| bool req_compactor<T, C, A>::ensure_enough_sections() { | ||
| const float ssr = section_size_raw_ / sqrtf(2); | ||
| auto req_compactor<T, C, A>::get_sections() const -> sections { | ||
| return sections{section_size_raw_, section_size_, num_sections_}; | ||
| } | ||
|
|
||
| template<typename T, typename C, typename A> | ||
| void req_compactor<T, C, A>::set_sections(const sections& s) { | ||
| section_size_raw_ = s.size_raw; | ||
| section_size_ = s.size; | ||
| num_sections_ = s.num; | ||
| } | ||
|
|
||
| template<typename T, typename C, typename A> | ||
| bool req_compactor<T, C, A>::next_sections(uint64_t state, sections& s) { | ||
| const float ssr = s.size_raw / sqrtf(2); | ||
| const uint32_t ne = nearest_even(ssr); | ||
| if (state_ >= static_cast<uint64_t>(1ULL << (num_sections_ - 1)) | ||
| && section_size_ > req_constants::MIN_K | ||
| if (state >= static_cast<uint64_t>(1ULL << (s.num - 1)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For |
||
| && s.size > req_constants::MIN_K | ||
| && ne >= req_constants::MIN_K) { | ||
| section_size_raw_ = ssr; | ||
| section_size_ = ne; | ||
| num_sections_ <<= 1; | ||
| if (capacity_ < 2 * get_nom_capacity()) grow(2 * get_nom_capacity()); | ||
| s.size_raw = ssr; | ||
| s.size = ne; | ||
| s.num <<= 1; | ||
| return true; | ||
| } | ||
| return false; | ||
|
|
@@ -408,7 +465,7 @@ void req_compactor<T, C, A>::serialize(std::ostream& os, const S& serde) const { | |
| const uint16_t padding = 0; | ||
| write(os, padding); | ||
| write(os, num_items_); | ||
| serde.serialize(os, begin(), num_items_); | ||
| if (num_items_ > 0) serde.serialize(os, begin(), num_items_); // an empty compactor may have no storage | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Symmetric case on the read side: |
||
| } | ||
|
|
||
| template<typename T, typename C, typename A> | ||
|
|
@@ -423,7 +480,7 @@ size_t req_compactor<T, C, A>::serialize(void* dst, size_t capacity, const S& se | |
| const uint16_t padding = 0; | ||
| ptr += copy_to_mem(padding, ptr); | ||
| ptr += copy_to_mem(num_items_, ptr); | ||
| ptr += serde.serialize(ptr, end_ptr - ptr, begin(), num_items_); | ||
| if (num_items_ > 0) ptr += serde.serialize(ptr, end_ptr - ptr, begin(), num_items_); // an empty compactor may have no storage | ||
| return ptr - static_cast<uint8_t*>(dst); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -174,17 +174,20 @@ template<typename FwdT> | |
| void req_sketch<T, C, A>::update(FwdT&& item) { | ||
| if (!check_update_item(item)) { return; } | ||
| compactors_[0].reserve(); // first, so that a failure leaves the sketch unchanged | ||
| if (is_empty()) { | ||
| min_item_.emplace(item); | ||
| max_item_.emplace(item); | ||
| } else { | ||
| if (comparator_(item, *min_item_)) *min_item_ = item; | ||
| if (comparator_(*max_item_, item)) *max_item_ = item; | ||
| } | ||
| // compare and copy first, but change nothing until the item is in level zero, | ||
| // so that an exception leaves the sketch as it was | ||
| const bool is_new_min = is_empty() || comparator_(item, *min_item_); | ||
| const bool is_new_max = is_empty() || comparator_(*max_item_, item); | ||
| optional<T> new_min; | ||
| optional<T> new_max; | ||
| if (is_new_min) new_min = optional<T>(static_cast<const T&>(item)); | ||
| if (is_new_max) new_max = optional<T>(static_cast<const T&>(item)); | ||
| reset_sorted_view(); | ||
| compactors_[0].append(std::forward<FwdT>(item)); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If req_sketch<std::string> s0(12);
s0.update(std::string(64, 'a'));
auto b = s0.serialize();
auto s = req_sketch<std::string>::deserialize(b.data(), b.size());
s.update(s.get_quantile(0.5)); // heap-use-after-free in append()Materializing |
||
| if (is_new_min) min_item_ = std::move(new_min); | ||
| if (is_new_max) max_item_ = std::move(new_max); | ||
| ++num_retained_; | ||
| ++n_; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should |
||
| reset_sorted_view(); | ||
| if (num_retained_ >= max_nom_size_) compress(); | ||
| } | ||
|
|
||
|
|
@@ -198,13 +201,15 @@ void req_sketch<T, C, A>::merge(FwdSk&& other) { | |
| try { | ||
| // grow until this has at least as many compactors as other | ||
| while (get_num_levels() < other.get_num_levels()) grow(); | ||
| if (is_empty()) { | ||
| min_item_.emplace(conditional_forward<FwdSk>(*other.min_item_)); | ||
| max_item_.emplace(conditional_forward<FwdSk>(*other.max_item_)); | ||
| } else { | ||
| if (comparator_(*other.min_item_, *min_item_)) *min_item_ = conditional_forward<FwdSk>(*other.min_item_); | ||
| if (comparator_(*max_item_, *other.max_item_)) *max_item_ = conditional_forward<FwdSk>(*other.max_item_); | ||
| } | ||
| // compare and copy first, so that a failed copy leaves min and max as they were | ||
| const bool is_new_min = is_empty() || comparator_(*other.min_item_, *min_item_); | ||
| const bool is_new_max = is_empty() || comparator_(*max_item_, *other.max_item_); | ||
| optional<T> new_min; | ||
| optional<T> new_max; | ||
| if (is_new_min) new_min = optional<T>(conditional_forward<FwdSk>(*other.min_item_)); | ||
| if (is_new_max) new_max = optional<T>(conditional_forward<FwdSk>(*other.max_item_)); | ||
| if (is_new_min) min_item_ = std::move(new_min); | ||
| if (is_new_max) max_item_ = std::move(new_max); | ||
|
Comment on lines
+211
to
+212
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Committing |
||
| // merge the items in all height compactors | ||
| for (size_t i = 0; i < other.get_num_levels(); ++i) { | ||
| compactors_[i].merge(conditional_forward<FwdSk>(other.compactors_[i])); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
comparator_itself throws insidestd::is_sorted,destroy_items()is not reached and the allocated buffer and copied items leak. Wrapping the post-copy_itemscheck intry { ... } catch (...) { destroy_items(); throw; }avoids that.