Skip to content

Commit

Permalink
Add unit test for DEBUG_LOCKORDER code
Browse files Browse the repository at this point in the history
Cherry-picked from: 41b88e9
  • Loading branch information
ryanofsky authored and patricklodder committed Apr 17, 2024
1 parent 2df8b17 commit 7f665a9
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Makefile.test.include
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ BITCOIN_TESTS =\
test/sigopcount_tests.cpp \
test/skiplist_tests.cpp \
test/streams_tests.cpp \
test/sync_tests.cpp \
test/test_bitcoin.cpp \
test/test_bitcoin.h \
test/test_random.h \
Expand Down
8 changes: 7 additions & 1 deletion src/sync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ static void potential_deadlock_detected(const LockPair& mismatch, const LockStac
}
LogPrintf(" %s\n", i.second.ToString());
}
assert(false);
if (g_debug_lockorder_abort) {
fprintf(stderr, "Assertion failed: detected inconsistent lock order at %s:%i, details in debug log.\n", __FILE__, __LINE__);
abort();
}
throw std::logic_error("potential deadlock detected");
}

static void push_lock(void* c, const CLockLocation& locklocation, bool fTry)
Expand Down Expand Up @@ -206,4 +210,6 @@ void DeleteLock(void* cs)
}
}

bool g_debug_lockorder_abort = true;

#endif /* DEBUG_LOCKORDER */
7 changes: 7 additions & 0 deletions src/sync.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ void LeaveCritical();
std::string LocksHeld();
void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs);
void DeleteLock(void* cs);

/**
* Call abort() if a potential lock order deadlock bug is detected, instead of
* just logging information and throwing a logic_error. Defaults to true, and
* set to false in DEBUG_LOCKORDER unit tests.
*/
extern bool g_debug_lockorder_abort;
#else
void static inline EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false) {}
void static inline LeaveCritical() {}
Expand Down
41 changes: 41 additions & 0 deletions src/test/sync_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) 2012-2017 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <sync.h>
#include <test/test_bitcoin.h>

#include <boost/test/unit_test.hpp>

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

CCriticalSection mutex1, mutex2;
{
LOCK2(mutex1, mutex2);
}
bool error_thrown = false;
try {
LOCK2(mutex2, mutex1);
} catch (const std::logic_error& e) {
BOOST_CHECK_EQUAL(e.what(), "potential deadlock detected");
error_thrown = true;
}
#ifdef DEBUG_LOCKORDER
BOOST_CHECK(error_thrown);
#else
BOOST_CHECK(!error_thrown);
#endif

#ifdef DEBUG_LOCKORDER
g_debug_lockorder_abort = prev;
#endif
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 7f665a9

Please sign in to comment.