aboutsummaryrefslogtreecommitdiff
path: root/src/sync.cpp
diff options
context:
space:
mode:
authorVasil Dimov <vd@FreeBSD.org>2020-06-20 15:31:55 +0200
committerVasil Dimov <vd@FreeBSD.org>2020-08-10 18:43:08 +0200
commit95975dd08d8fdaaeaf28e0d06b861ce2748c17b6 (patch)
treec2fb3525064d0a722cad4c27cccdef67f41e4647 /src/sync.cpp
parent4df6567e4cbb4677e8048de2f8008612e1b860b9 (diff)
downloadbitcoin-95975dd08d8fdaaeaf28e0d06b861ce2748c17b6.tar.xz
sync: detect double lock from the same thread
Double lock of the same (non-recursive) mutex from the same thread is producing an undefined behavior. Detect this from DEBUG_LOCKORDER and react similarly to the deadlock detection.
Diffstat (limited to 'src/sync.cpp')
-rw-r--r--src/sync.cpp40
1 files changed, 37 insertions, 3 deletions
diff --git a/src/sync.cpp b/src/sync.cpp
index 7de8439d6f..d020b4e334 100644
--- a/src/sync.cpp
+++ b/src/sync.cpp
@@ -20,6 +20,7 @@
#include <set>
#include <system_error>
#include <thread>
+#include <type_traits>
#include <unordered_map>
#include <utility>
#include <vector>
@@ -138,17 +139,50 @@ static void potential_deadlock_detected(const LockPair& mismatch, const LockStac
throw std::logic_error(strprintf("potential deadlock detected: %s -> %s -> %s", mutex_b, mutex_a, mutex_b));
}
+static void double_lock_detected(const void* mutex, LockStack& lock_stack)
+{
+ LogPrintf("DOUBLE LOCK DETECTED\n");
+ LogPrintf("Lock order:\n");
+ for (const LockStackItem& i : lock_stack) {
+ if (i.first == mutex) {
+ LogPrintf(" (*)"); /* Continued */
+ }
+ LogPrintf(" %s\n", i.second.ToString());
+ }
+ if (g_debug_lockorder_abort) {
+ tfm::format(std::cerr, "Assertion failed: detected double lock at %s:%i, details in debug log.\n", __FILE__, __LINE__);
+ abort();
+ }
+ throw std::logic_error("double lock detected");
+}
+
template <typename MutexType>
static void push_lock(MutexType* c, const CLockLocation& locklocation)
{
+ constexpr bool is_recursive_mutex =
+ std::is_base_of<RecursiveMutex, MutexType>::value ||
+ std::is_base_of<std::recursive_mutex, MutexType>::value;
+
LockData& lockdata = GetLockData();
std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
LockStack& lock_stack = lockdata.m_lock_stacks[std::this_thread::get_id()];
lock_stack.emplace_back(c, locklocation);
- for (const LockStackItem& i : lock_stack) {
- if (i.first == c)
- break;
+ for (size_t j = 0; j < lock_stack.size() - 1; ++j) {
+ const LockStackItem& i = lock_stack[j];
+ if (i.first == c) {
+ if (is_recursive_mutex) {
+ break;
+ }
+ // It is not a recursive mutex and it appears in the stack two times:
+ // at position `j` and at the end (which we added just before this loop).
+ // Can't allow locking the same (non-recursive) mutex two times from the
+ // same thread as that results in an undefined behavior.
+ auto lock_stack_copy = lock_stack;
+ lock_stack.pop_back();
+ double_lock_detected(c, lock_stack_copy);
+ // double_lock_detected() does not return.
+ }
const LockPair p1 = std::make_pair(i.first, c);
if (lockdata.lockorders.count(p1))