Skip to content

Commit 14a4b49

Browse files
committed
Merge #13300: qa: Initialize lockstack to prevent null pointer deref
fa9da85 qa: Initialize lockstack to prevent null pointer deref (MarcoFalke) Pull request description: It is currently impossible to call debug methods such as `AssertLock(Not)Held` on a thread without running into undefined behavior, unless a lock was pushed on the stack in this thread. Initializing the global `lockstack` seems to fix both issues. Tree-SHA512: 8cb76b22cb31887ddf15742fdc790f01e8f04ed837367d0fd4996535748d124342e8bfde68952b903847b96ad33406c64907a53ebab9646f78d97fa4365c3061
2 parents fb77310 + fa9da85 commit 14a4b49

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

src/sync.cpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ struct LockData {
7575
std::mutex dd_mutex;
7676
} static lockdata;
7777

78-
static thread_local std::unique_ptr<LockStack> lockstack;
78+
static thread_local LockStack g_lockstack;
7979

8080
static void potential_deadlock_detected(const std::pair<void*, void*>& mismatch, const LockStack& s1, const LockStack& s2)
8181
{
@@ -105,21 +105,18 @@ static void potential_deadlock_detected(const std::pair<void*, void*>& mismatch,
105105

106106
static void push_lock(void* c, const CLockLocation& locklocation)
107107
{
108-
if (!lockstack)
109-
lockstack.reset(new LockStack);
110-
111108
std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
112109

113-
lockstack->push_back(std::make_pair(c, locklocation));
110+
g_lockstack.push_back(std::make_pair(c, locklocation));
114111

115-
for (const std::pair<void*, CLockLocation> & i : (*lockstack)) {
112+
for (const std::pair<void*, CLockLocation>& i : g_lockstack) {
116113
if (i.first == c)
117114
break;
118115

119116
std::pair<void*, void*> p1 = std::make_pair(i.first, c);
120117
if (lockdata.lockorders.count(p1))
121118
continue;
122-
lockdata.lockorders[p1] = (*lockstack);
119+
lockdata.lockorders[p1] = g_lockstack;
123120

124121
std::pair<void*, void*> p2 = std::make_pair(c, i.first);
125122
lockdata.invlockorders.insert(p2);
@@ -130,7 +127,7 @@ static void push_lock(void* c, const CLockLocation& locklocation)
130127

131128
static void pop_lock()
132129
{
133-
(*lockstack).pop_back();
130+
g_lockstack.pop_back();
134131
}
135132

136133
void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry)
@@ -146,14 +143,14 @@ void LeaveCritical()
146143
std::string LocksHeld()
147144
{
148145
std::string result;
149-
for (const std::pair<void*, CLockLocation> & i : *lockstack)
146+
for (const std::pair<void*, CLockLocation>& i : g_lockstack)
150147
result += i.second.ToString() + std::string("\n");
151148
return result;
152149
}
153150

154151
void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs)
155152
{
156-
for (const std::pair<void*, CLockLocation> & i : *lockstack)
153+
for (const std::pair<void*, CLockLocation>& i : g_lockstack)
157154
if (i.first == cs)
158155
return;
159156
fprintf(stderr, "Assertion failed: lock %s not held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld().c_str());
@@ -162,7 +159,7 @@ void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine,
162159

163160
void AssertLockNotHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs)
164161
{
165-
for (const std::pair<void*, CLockLocation>& i : *lockstack) {
162+
for (const std::pair<void*, CLockLocation>& i : g_lockstack) {
166163
if (i.first == cs) {
167164
fprintf(stderr, "Assertion failed: lock %s held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld().c_str());
168165
abort();

0 commit comments

Comments
 (0)