Skip to content

Commit 95975dd

Browse files
committed
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.
1 parent 4df6567 commit 95975dd

File tree

2 files changed

+92
-3
lines changed

2 files changed

+92
-3
lines changed

src/sync.cpp

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <set>
2121
#include <system_error>
2222
#include <thread>
23+
#include <type_traits>
2324
#include <unordered_map>
2425
#include <utility>
2526
#include <vector>
@@ -138,17 +139,50 @@ static void potential_deadlock_detected(const LockPair& mismatch, const LockStac
138139
throw std::logic_error(strprintf("potential deadlock detected: %s -> %s -> %s", mutex_b, mutex_a, mutex_b));
139140
}
140141

142+
static void double_lock_detected(const void* mutex, LockStack& lock_stack)
143+
{
144+
LogPrintf("DOUBLE LOCK DETECTED\n");
145+
LogPrintf("Lock order:\n");
146+
for (const LockStackItem& i : lock_stack) {
147+
if (i.first == mutex) {
148+
LogPrintf(" (*)"); /* Continued */
149+
}
150+
LogPrintf(" %s\n", i.second.ToString());
151+
}
152+
if (g_debug_lockorder_abort) {
153+
tfm::format(std::cerr, "Assertion failed: detected double lock at %s:%i, details in debug log.\n", __FILE__, __LINE__);
154+
abort();
155+
}
156+
throw std::logic_error("double lock detected");
157+
}
158+
141159
template <typename MutexType>
142160
static void push_lock(MutexType* c, const CLockLocation& locklocation)
143161
{
162+
constexpr bool is_recursive_mutex =
163+
std::is_base_of<RecursiveMutex, MutexType>::value ||
164+
std::is_base_of<std::recursive_mutex, MutexType>::value;
165+
144166
LockData& lockdata = GetLockData();
145167
std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
146168

147169
LockStack& lock_stack = lockdata.m_lock_stacks[std::this_thread::get_id()];
148170
lock_stack.emplace_back(c, locklocation);
149-
for (const LockStackItem& i : lock_stack) {
150-
if (i.first == c)
151-
break;
171+
for (size_t j = 0; j < lock_stack.size() - 1; ++j) {
172+
const LockStackItem& i = lock_stack[j];
173+
if (i.first == c) {
174+
if (is_recursive_mutex) {
175+
break;
176+
}
177+
// It is not a recursive mutex and it appears in the stack two times:
178+
// at position `j` and at the end (which we added just before this loop).
179+
// Can't allow locking the same (non-recursive) mutex two times from the
180+
// same thread as that results in an undefined behavior.
181+
auto lock_stack_copy = lock_stack;
182+
lock_stack.pop_back();
183+
double_lock_detected(c, lock_stack_copy);
184+
// double_lock_detected() does not return.
185+
}
152186

153187
const LockPair p1 = std::make_pair(i.first, c);
154188
if (lockdata.lockorders.count(p1))

src/test/sync_tests.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
#include <test/util/setup_common.h>
77

88
#include <boost/test/unit_test.hpp>
9+
#include <boost/thread/mutex.hpp>
10+
11+
#include <mutex>
912

1013
namespace {
1114
template <typename MutexType>
@@ -29,6 +32,38 @@ void TestPotentialDeadLockDetected(MutexType& mutex1, MutexType& mutex2)
2932
BOOST_CHECK(!error_thrown);
3033
#endif
3134
}
35+
36+
#ifdef DEBUG_LOCKORDER
37+
template <typename MutexType>
38+
void TestDoubleLock2(MutexType& m)
39+
{
40+
ENTER_CRITICAL_SECTION(m);
41+
LEAVE_CRITICAL_SECTION(m);
42+
}
43+
44+
template <typename MutexType>
45+
void TestDoubleLock(bool should_throw)
46+
{
47+
const bool prev = g_debug_lockorder_abort;
48+
g_debug_lockorder_abort = false;
49+
50+
MutexType m;
51+
ENTER_CRITICAL_SECTION(m);
52+
if (should_throw) {
53+
BOOST_CHECK_EXCEPTION(
54+
TestDoubleLock2(m), std::logic_error, [](const std::logic_error& e) {
55+
return strcmp(e.what(), "double lock detected") == 0;
56+
});
57+
} else {
58+
BOOST_CHECK_NO_THROW(TestDoubleLock2(m));
59+
}
60+
LEAVE_CRITICAL_SECTION(m);
61+
62+
BOOST_CHECK(LockStackEmpty());
63+
64+
g_debug_lockorder_abort = prev;
65+
}
66+
#endif /* DEBUG_LOCKORDER */
3267
} // namespace
3368

3469
BOOST_FIXTURE_TEST_SUITE(sync_tests, BasicTestingSetup)
@@ -55,4 +90,24 @@ BOOST_AUTO_TEST_CASE(potential_deadlock_detected)
5590
#endif
5691
}
5792

93+
/* Double lock would produce an undefined behavior. Thus, we only do that if
94+
* DEBUG_LOCKORDER is activated to detect it. We don't want non-DEBUG_LOCKORDER
95+
* build to produce tests that exhibit known undefined behavior. */
96+
#ifdef DEBUG_LOCKORDER
97+
BOOST_AUTO_TEST_CASE(double_lock_mutex)
98+
{
99+
TestDoubleLock<Mutex>(true /* should throw */);
100+
}
101+
102+
BOOST_AUTO_TEST_CASE(double_lock_boost_mutex)
103+
{
104+
TestDoubleLock<boost::mutex>(true /* should throw */);
105+
}
106+
107+
BOOST_AUTO_TEST_CASE(double_lock_recursive_mutex)
108+
{
109+
TestDoubleLock<RecursiveMutex>(false /* should not throw */);
110+
}
111+
#endif /* DEBUG_LOCKORDER */
112+
58113
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)