2020-01-14 16:17:38 -03:00
|
|
|
// Copyright (c) 2012-2020 The Bitcoin Core developers
|
2017-11-08 17:28:35 -03:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#include <sync.h>
|
2019-11-05 17:18:59 -03:00
|
|
|
#include <test/util/setup_common.h>
|
2017-11-08 17:28:35 -03:00
|
|
|
|
|
|
|
#include <boost/test/unit_test.hpp>
|
2020-06-20 09:31:55 -04:00
|
|
|
|
|
|
|
#include <mutex>
|
2017-11-08 17:28:35 -03:00
|
|
|
|
2017-11-08 18:21:13 -03:00
|
|
|
namespace {
|
|
|
|
template <typename MutexType>
|
|
|
|
void TestPotentialDeadLockDetected(MutexType& mutex1, MutexType& mutex2)
|
2017-11-08 17:28:35 -03:00
|
|
|
{
|
|
|
|
{
|
|
|
|
LOCK2(mutex1, mutex2);
|
|
|
|
}
|
2020-06-20 16:00:52 -04:00
|
|
|
BOOST_CHECK(LockStackEmpty());
|
2017-11-08 17:28:35 -03:00
|
|
|
bool error_thrown = false;
|
|
|
|
try {
|
|
|
|
LOCK2(mutex2, mutex1);
|
|
|
|
} catch (const std::logic_error& e) {
|
2020-06-22 11:21:12 -04:00
|
|
|
BOOST_CHECK_EQUAL(e.what(), "potential deadlock detected: mutex1 -> mutex2 -> mutex1");
|
2017-11-08 17:28:35 -03:00
|
|
|
error_thrown = true;
|
|
|
|
}
|
2020-06-20 16:00:52 -04:00
|
|
|
BOOST_CHECK(LockStackEmpty());
|
2017-11-08 17:28:35 -03:00
|
|
|
#ifdef DEBUG_LOCKORDER
|
|
|
|
BOOST_CHECK(error_thrown);
|
|
|
|
#else
|
|
|
|
BOOST_CHECK(!error_thrown);
|
|
|
|
#endif
|
2017-11-08 18:21:13 -03:00
|
|
|
}
|
2020-06-20 09:31:55 -04:00
|
|
|
|
|
|
|
#ifdef DEBUG_LOCKORDER
|
|
|
|
template <typename MutexType>
|
|
|
|
void TestDoubleLock2(MutexType& m)
|
|
|
|
{
|
|
|
|
ENTER_CRITICAL_SECTION(m);
|
|
|
|
LEAVE_CRITICAL_SECTION(m);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename MutexType>
|
|
|
|
void TestDoubleLock(bool should_throw)
|
|
|
|
{
|
|
|
|
const bool prev = g_debug_lockorder_abort;
|
|
|
|
g_debug_lockorder_abort = false;
|
|
|
|
|
|
|
|
MutexType m;
|
|
|
|
ENTER_CRITICAL_SECTION(m);
|
|
|
|
if (should_throw) {
|
2020-11-26 10:39:12 -03:00
|
|
|
BOOST_CHECK_EXCEPTION(TestDoubleLock2(m), std::logic_error,
|
|
|
|
HasReason("double lock detected"));
|
2020-06-20 09:31:55 -04:00
|
|
|
} else {
|
|
|
|
BOOST_CHECK_NO_THROW(TestDoubleLock2(m));
|
|
|
|
}
|
|
|
|
LEAVE_CRITICAL_SECTION(m);
|
|
|
|
|
|
|
|
BOOST_CHECK(LockStackEmpty());
|
|
|
|
|
|
|
|
g_debug_lockorder_abort = prev;
|
|
|
|
}
|
|
|
|
#endif /* DEBUG_LOCKORDER */
|
2020-09-20 05:38:03 -03:00
|
|
|
|
|
|
|
template <typename MutexType>
|
|
|
|
void TestInconsistentLockOrderDetected(MutexType& mutex1, MutexType& mutex2) NO_THREAD_SAFETY_ANALYSIS
|
|
|
|
{
|
|
|
|
ENTER_CRITICAL_SECTION(mutex1);
|
|
|
|
ENTER_CRITICAL_SECTION(mutex2);
|
|
|
|
#ifdef DEBUG_LOCKORDER
|
|
|
|
BOOST_CHECK_EXCEPTION(LEAVE_CRITICAL_SECTION(mutex1), std::logic_error, HasReason("mutex1 was not most recent critical section locked"));
|
|
|
|
#endif // DEBUG_LOCKORDER
|
|
|
|
LEAVE_CRITICAL_SECTION(mutex2);
|
|
|
|
LEAVE_CRITICAL_SECTION(mutex1);
|
|
|
|
BOOST_CHECK(LockStackEmpty());
|
|
|
|
}
|
2017-11-08 18:21:13 -03:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
BOOST_FIXTURE_TEST_SUITE(sync_tests, BasicTestingSetup)
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(potential_deadlock_detected)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_LOCKORDER
|
|
|
|
bool prev = g_debug_lockorder_abort;
|
|
|
|
g_debug_lockorder_abort = false;
|
|
|
|
#endif
|
|
|
|
|
2020-01-07 13:14:15 -03:00
|
|
|
RecursiveMutex rmutex1, rmutex2;
|
2017-11-08 18:21:13 -03:00
|
|
|
TestPotentialDeadLockDetected(rmutex1, rmutex2);
|
2020-06-22 14:35:43 -04:00
|
|
|
// The second test ensures that lock tracking data have not been broken by exception.
|
|
|
|
TestPotentialDeadLockDetected(rmutex1, rmutex2);
|
2017-11-08 18:21:13 -03:00
|
|
|
|
2017-11-03 08:49:16 -03:00
|
|
|
Mutex mutex1, mutex2;
|
2017-11-08 18:21:13 -03:00
|
|
|
TestPotentialDeadLockDetected(mutex1, mutex2);
|
2020-06-22 14:35:43 -04:00
|
|
|
// The second test ensures that lock tracking data have not been broken by exception.
|
|
|
|
TestPotentialDeadLockDetected(mutex1, mutex2);
|
2017-11-08 17:28:35 -03:00
|
|
|
|
|
|
|
#ifdef DEBUG_LOCKORDER
|
|
|
|
g_debug_lockorder_abort = prev;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-06-20 09:31:55 -04:00
|
|
|
/* Double lock would produce an undefined behavior. Thus, we only do that if
|
|
|
|
* DEBUG_LOCKORDER is activated to detect it. We don't want non-DEBUG_LOCKORDER
|
|
|
|
* build to produce tests that exhibit known undefined behavior. */
|
|
|
|
#ifdef DEBUG_LOCKORDER
|
|
|
|
BOOST_AUTO_TEST_CASE(double_lock_mutex)
|
|
|
|
{
|
|
|
|
TestDoubleLock<Mutex>(true /* should throw */);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(double_lock_recursive_mutex)
|
|
|
|
{
|
|
|
|
TestDoubleLock<RecursiveMutex>(false /* should not throw */);
|
|
|
|
}
|
|
|
|
#endif /* DEBUG_LOCKORDER */
|
|
|
|
|
2020-09-20 05:38:03 -03:00
|
|
|
BOOST_AUTO_TEST_CASE(inconsistent_lock_order_detected)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_LOCKORDER
|
|
|
|
bool prev = g_debug_lockorder_abort;
|
|
|
|
g_debug_lockorder_abort = false;
|
|
|
|
#endif // DEBUG_LOCKORDER
|
|
|
|
|
|
|
|
RecursiveMutex rmutex1, rmutex2;
|
|
|
|
TestInconsistentLockOrderDetected(rmutex1, rmutex2);
|
|
|
|
// By checking lock order consistency (CheckLastCritical) before any unlocking (LeaveCritical)
|
|
|
|
// the lock tracking data must not have been broken by exception.
|
|
|
|
TestInconsistentLockOrderDetected(rmutex1, rmutex2);
|
|
|
|
|
|
|
|
Mutex mutex1, mutex2;
|
|
|
|
TestInconsistentLockOrderDetected(mutex1, mutex2);
|
|
|
|
// By checking lock order consistency (CheckLastCritical) before any unlocking (LeaveCritical)
|
|
|
|
// the lock tracking data must not have been broken by exception.
|
|
|
|
TestInconsistentLockOrderDetected(mutex1, mutex2);
|
|
|
|
|
|
|
|
#ifdef DEBUG_LOCKORDER
|
|
|
|
g_debug_lockorder_abort = prev;
|
|
|
|
#endif // DEBUG_LOCKORDER
|
|
|
|
}
|
|
|
|
|
2017-11-08 17:28:35 -03:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|