qa: Initialize lockstack to prevent null pointer deref

(cherry picked from commit bitcoin/bitcoin@fa9da85b7c)
This commit is contained in:
MarcoFalke 2018-05-21 21:35:47 -04:00 committed by Jack Grigg
parent 1c255a2fd6
commit 130871cb4f
1 changed files with 8 additions and 11 deletions

View File

@ -80,7 +80,7 @@ LockData& GetLockData() {
return lockdata; return lockdata;
} }
static thread_local std::unique_ptr<LockStack> lockstack; static thread_local LockStack g_lockstack;
static void potential_deadlock_detected(const std::pair<void*, void*>& mismatch, const LockStack& s1, const LockStack& s2) static void potential_deadlock_detected(const std::pair<void*, void*>& mismatch, const LockStack& s1, const LockStack& s2)
{ {
@ -110,22 +110,19 @@ static void potential_deadlock_detected(const std::pair<void*, void*>& mismatch,
static void push_lock(void* c, const CLockLocation& locklocation) static void push_lock(void* c, const CLockLocation& locklocation)
{ {
if (!lockstack)
lockstack.reset(new LockStack);
LockData& lockdata = GetLockData(); LockData& lockdata = GetLockData();
std::lock_guard<std::mutex> lock(lockdata.dd_mutex); std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
lockstack->push_back(std::make_pair(c, locklocation)); g_lockstack.push_back(std::make_pair(c, locklocation));
for (const std::pair<void*, CLockLocation> & i : (*lockstack)) { for (const std::pair<void*, CLockLocation>& i : g_lockstack) {
if (i.first == c) if (i.first == c)
break; break;
std::pair<void*, void*> p1 = std::make_pair(i.first, c); std::pair<void*, void*> p1 = std::make_pair(i.first, c);
if (lockdata.lockorders.count(p1)) if (lockdata.lockorders.count(p1))
continue; continue;
lockdata.lockorders[p1] = (*lockstack); lockdata.lockorders[p1] = g_lockstack;
std::pair<void*, void*> p2 = std::make_pair(c, i.first); std::pair<void*, void*> p2 = std::make_pair(c, i.first);
lockdata.invlockorders.insert(p2); lockdata.invlockorders.insert(p2);
@ -136,7 +133,7 @@ static void push_lock(void* c, const CLockLocation& locklocation)
static void pop_lock() static void pop_lock()
{ {
(*lockstack).pop_back(); g_lockstack.pop_back();
} }
void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry) void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry)
@ -152,14 +149,14 @@ void LeaveCritical()
std::string LocksHeld() std::string LocksHeld()
{ {
std::string result; std::string result;
for (const std::pair<void*, CLockLocation> & i : *lockstack) for (const std::pair<void*, CLockLocation>& i : g_lockstack)
result += i.second.ToString() + std::string("\n"); result += i.second.ToString() + std::string("\n");
return result; return result;
} }
void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs) void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs)
{ {
for (const std::pair<void*, CLockLocation> & i : *lockstack) for (const std::pair<void*, CLockLocation>& i : g_lockstack)
if (i.first == cs) if (i.first == cs)
return; return;
fprintf(stderr, "Assertion failed: lock %s not held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld().c_str()); fprintf(stderr, "Assertion failed: lock %s not held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld().c_str());
@ -168,7 +165,7 @@ void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine,
void AssertLockNotHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs) void AssertLockNotHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs)
{ {
for (const std::pair<void*, CLockLocation>& i : *lockstack) { for (const std::pair<void*, CLockLocation>& i : g_lockstack) {
if (i.first == cs) { if (i.first == cs) {
fprintf(stderr, "Assertion failed: lock %s held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld().c_str()); fprintf(stderr, "Assertion failed: lock %s held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld().c_str());
abort(); abort();