Skip to content

Commit

Permalink
Fix qsbr_per_thread::on_next_epoch_deallocate crash safety
Browse files Browse the repository at this point in the history
Move current_interval_total_dealloc_size bump after emplacing the new element to
the deferred request list.
  • Loading branch information
laurynas-biveinis committed Mar 31, 2022
1 parent 3b02477 commit 83add10
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
4 changes: 2 additions & 2 deletions qsbr.hpp
Expand Up @@ -848,15 +848,15 @@ inline void qsbr_per_thread::on_next_epoch_deallocate(
return;
}

current_interval_total_dealloc_size += size;

current_interval_dealloc_requests.emplace_back(pointer
#ifndef NDEBUG
,
last_seen_epoch,
std::move(dealloc_callback)
#endif
);

current_interval_total_dealloc_size += size;
}

inline void qsbr_per_thread::advance_last_seen_epoch(
Expand Down
33 changes: 33 additions & 0 deletions test/test_qsbr_oom.cpp
Expand Up @@ -12,6 +12,7 @@
#include "heap.hpp"
#include "qsbr.hpp"
#include "qsbr_gtest_utils.hpp"
#include "thread_sync.hpp"

namespace {

Expand Down Expand Up @@ -73,6 +74,38 @@ TEST_F(QSBROOMTest, StartThread) {
[&second_thread] { join(second_thread); });
}

TEST_F(QSBROOMTest, DeferredDeallocation) {
auto *ptr = static_cast<char *>(allocate());
unodb::qsbr_thread second_thread;
const auto current_interval_total_dealloc_size_before =
unodb::this_thread().get_current_interval_total_dealloc_size();
oom_test(
2,
[&second_thread]() noexcept {
second_thread = unodb::qsbr_thread{[] {
unodb::detail::thread_syncs[0].notify();
unodb::detail::thread_syncs[1].wait();

quiescent();
}};

unodb::detail::thread_syncs[0].wait();
},
[ptr] { qsbr_deallocate(ptr); },
[ptr, current_interval_total_dealloc_size_before]() noexcept {
touch_memory(ptr);
const auto current_interval_total_dealloc_size_after =
unodb::this_thread().get_current_interval_total_dealloc_size();
UNODB_ASSERT_EQ(current_interval_total_dealloc_size_before,
current_interval_total_dealloc_size_after);
},
[ptr] { qsbr_deallocate(ptr); },
[&second_thread] {
unodb::detail::thread_syncs[1].notify();
join(second_thread);
});
}

UNODB_END_TESTS()

} // namespace
Expand Down

0 comments on commit 83add10

Please sign in to comment.