Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion req/include/req_compactor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,23 @@ class req_compactor {
uint32_t capacity_; // 0 while items_ is nullptr
T* items_; // nullptr until first use, see reserve()

bool ensure_enough_sections();
// the section parameters, which change as the state advances
struct sections {
float size_raw;
uint32_t size;
uint8_t num;
uint32_t nom_capacity() const { return req_constants::MULTIPLIER * num * size; }
};
sections get_sections() const;
void set_sections(const sections& s);
// one step of the section schedule for the given state: halves the section size (by sqrt(2)) and doubles
// the number of sections once the state reaches 2^(num - 1); returns false if the sections stay as they are
static bool next_sections(uint64_t state, sections& s);
// destroys the items and frees the storage
void destroy_items();
// allocates storage and copies the items of other, for a constructor; releases both if a copy throws
template<typename O>
void copy_items(const O& other);
std::pair<uint32_t, uint32_t> compute_compaction_range(uint32_t secs_to_compact) const;
void grow(uint32_t new_capacity);
void ensure_space(uint32_t num);
Expand Down
133 changes: 95 additions & 38 deletions req/include/req_compactor_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand All @@ -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>
Expand Down Expand Up @@ -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");
}
Comment on lines +179 to 182

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If comparator_ itself throws inside std::is_sorted, destroy_items() is not reached and the allocated buffer and copied items leak. Wrapping the post-copy_items check in try { ... } catch (...) { destroy_items(); throw; } avoids that.

}

Expand Down Expand Up @@ -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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Since other's items are now copied into [from, from + num) before sort(), self-merging (sketch.merge(sketch)) when level 0 is unsorted sets this->sorted_ = true (aliasing other.sorted_), skips std::sort(from, from + num, comparator_), and passes an unsorted range to std::inplace_merge (sketch.merge(std::move(sketch)) also moves out of *this before merging):

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>
Expand All @@ -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);
Expand All @@ -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))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

For k = 1024, 4 doublings reach s.num = 48 (s.size = 256), which passes check_header (num_sections <= 64). If state >= (1ULL << 47) (for example on a deserialized sketch), next_sections doubles s.num to 96 (ne = 182 >= MIN_K), and the next iteration of while (next_sections(...)) in merge() evaluates 1ULL << 95 (UB, and serialize() writes num_sections = 96 which check_header rejects). Stopping once s.num > 32 keeps s.num <= 48 consistent with check_header.

&& 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;
Expand Down Expand Up @@ -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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Symmetric case on the read side: deserialize_items (L518, L575) still calls alloc.allocate(0) and serde.deserialize(..., 0) when num == 0, leaving items_ != nullptr while capacity_ == 0 (and passing (nullptr, 0) to memcpy if an allocator returns nullptr for zero-sized allocations).

}

template<typename T, typename C, typename A>
Expand All @@ -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);
}

Expand Down
7 changes: 7 additions & 0 deletions req/include/req_sketch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@ class req_sketch {
* If cross-language portability is required, callers should ensure that
* the input string uses a compatible encoding (valid UTF-8).
* The first update allocates storage for items, unless reserve() or a merge already did.
* If an allocation or a copy of the item throws before the item is added, the sketch is unchanged.
* If a compaction that follows fails to allocate, the item stays added, the sketch stays valid,
* and later updates compact it.
* If the comparator throws while items are sorted or merged, or does not impose a consistent
* total order, the state of the sketch is undefined.
* @param item from a stream of items
*/
template<typename FwdT>
Expand All @@ -208,6 +213,8 @@ class req_sketch {
* both sketches were built using compatible string encodings.
* The first merge of a non-empty sketch allocates storage for items,
* unless reserve() or an update already did.
* If an allocation or a copy of an item throws, this sketch stays valid and usable, but may hold part of other.
* If the comparator throws while items are sorted or merged, the state of this sketch is undefined.
* @param other sketch to merge into this one
*/
template<typename FwdSk>
Expand Down
35 changes: 20 additions & 15 deletions req/include/req_sketch_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If item aliases an element in this and compactors_[0] is at num_items_ == capacity_ (such as right after deserialize(), or after a failed compression filled the buffer), append() calls grow() and frees the backing array before constructing from item (heap-use-after-free under ASan, same pattern as #573):

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 T before grow() also avoids reallocating compactors_[0] when T's copy constructor throws.

if (is_new_min) min_item_ = std::move(new_min);
if (is_new_max) max_item_ = std::move(new_max);
++num_retained_;
++n_;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should update() and merge() also cap n_ at 2^63 - 1 as in #585? Without an overflow check, ~60 repeated merges wrap n_ to 0 (is_empty() becomes true while items are retained) and reach 1ULL << 64 UB at level 64.

reset_sorted_view();
if (num_retained_ >= max_nom_size_) compress();
}

Expand All @@ -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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Committing min_item_ and max_item_ before merging any compactor leaves them modified even when compactors_[0].merge() throws on allocation or on the first item copy: an empty target stays is_empty() == true while holding live min_item_ / max_item_, and a 1-item target {10} failing to merge {1, 999} ends up with n == 1, sole item 10, min == 1, max == 999. In addition, if num_levels == 1 after remove_empty_levels_above(num_levels) in catch, exact-mode serialize() omits min_item_ and max_item_, so get_min_item() / get_max_item() disagree with a serialization round-trip.

// 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]));
Expand Down
Loading
Loading