Bitcoin Core  22.99.0
P2P Digital Currency
sync.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2020 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #if defined(HAVE_CONFIG_H)
7 #endif
8 
9 #include <sync.h>
10 
11 #include <logging.h>
12 #include <tinyformat.h>
13 #include <util/strencodings.h>
14 #include <util/threadnames.h>
15 
16 #include <map>
17 #include <mutex>
18 #include <set>
19 #include <system_error>
20 #include <thread>
21 #include <type_traits>
22 #include <unordered_map>
23 #include <utility>
24 #include <vector>
25 
26 #ifdef DEBUG_LOCKORDER
27 //
28 // Early deadlock detection.
29 // Problem being solved:
30 // Thread 1 locks A, then B, then C
31 // Thread 2 locks D, then C, then A
32 // --> may result in deadlock between the two threads, depending on when they run.
33 // Solution implemented here:
34 // Keep track of pairs of locks: (A before B), (A before C), etc.
35 // Complain if any thread tries to lock in a different order.
36 //
37 
38 struct CLockLocation {
39  CLockLocation(
40  const char* pszName,
41  const char* pszFile,
42  int nLine,
43  bool fTryIn,
44  const std::string& thread_name)
45  : fTry(fTryIn),
46  mutexName(pszName),
47  sourceFile(pszFile),
48  m_thread_name(thread_name),
49  sourceLine(nLine) {}
50 
51  std::string ToString() const
52  {
53  return strprintf(
54  "'%s' in %s:%s%s (in thread '%s')",
55  mutexName, sourceFile, sourceLine, (fTry ? " (TRY)" : ""), m_thread_name);
56  }
57 
58  std::string Name() const
59  {
60  return mutexName;
61  }
62 
63 private:
64  bool fTry;
65  std::string mutexName;
66  std::string sourceFile;
67  const std::string& m_thread_name;
68  int sourceLine;
69 };
70 
71 using LockStackItem = std::pair<void*, CLockLocation>;
72 using LockStack = std::vector<LockStackItem>;
73 using LockStacks = std::unordered_map<std::thread::id, LockStack>;
74 
75 using LockPair = std::pair<void*, void*>;
76 using LockOrders = std::map<LockPair, LockStack>;
77 using InvLockOrders = std::set<LockPair>;
78 
79 struct LockData {
80  LockStacks m_lock_stacks;
81  LockOrders lockorders;
82  InvLockOrders invlockorders;
83  std::mutex dd_mutex;
84 };
85 
86 LockData& GetLockData() {
87  // This approach guarantees that the object is not destroyed until after its last use.
88  // The operating system automatically reclaims all the memory in a program's heap when that program exits.
89  // Since the ~LockData() destructor is never called, the LockData class and all
90  // its subclasses must have implicitly-defined destructors.
91  static LockData& lock_data = *new LockData();
92  return lock_data;
93 }
94 
95 static void potential_deadlock_detected(const LockPair& mismatch, const LockStack& s1, const LockStack& s2)
96 {
97  LogPrintf("POTENTIAL DEADLOCK DETECTED\n");
98  LogPrintf("Previous lock order was:\n");
99  for (const LockStackItem& i : s1) {
100  if (i.first == mismatch.first) {
101  LogPrintf(" (1)"); /* Continued */
102  }
103  if (i.first == mismatch.second) {
104  LogPrintf(" (2)"); /* Continued */
105  }
106  LogPrintf(" %s\n", i.second.ToString());
107  }
108 
109  std::string mutex_a, mutex_b;
110  LogPrintf("Current lock order is:\n");
111  for (const LockStackItem& i : s2) {
112  if (i.first == mismatch.first) {
113  LogPrintf(" (1)"); /* Continued */
114  mutex_a = i.second.Name();
115  }
116  if (i.first == mismatch.second) {
117  LogPrintf(" (2)"); /* Continued */
118  mutex_b = i.second.Name();
119  }
120  LogPrintf(" %s\n", i.second.ToString());
121  }
122  if (g_debug_lockorder_abort) {
123  tfm::format(std::cerr, "Assertion failed: detected inconsistent lock order for %s, details in debug log.\n", s2.back().second.ToString());
124  abort();
125  }
126  throw std::logic_error(strprintf("potential deadlock detected: %s -> %s -> %s", mutex_b, mutex_a, mutex_b));
127 }
128 
129 static void double_lock_detected(const void* mutex, const LockStack& lock_stack)
130 {
131  LogPrintf("DOUBLE LOCK DETECTED\n");
132  LogPrintf("Lock order:\n");
133  for (const LockStackItem& i : lock_stack) {
134  if (i.first == mutex) {
135  LogPrintf(" (*)"); /* Continued */
136  }
137  LogPrintf(" %s\n", i.second.ToString());
138  }
139  if (g_debug_lockorder_abort) {
140  tfm::format(std::cerr,
141  "Assertion failed: detected double lock for %s, details in debug log.\n",
142  lock_stack.back().second.ToString());
143  abort();
144  }
145  throw std::logic_error("double lock detected");
146 }
147 
148 template <typename MutexType>
149 static void push_lock(MutexType* c, const CLockLocation& locklocation)
150 {
151  constexpr bool is_recursive_mutex =
152  std::is_base_of<RecursiveMutex, MutexType>::value ||
153  std::is_base_of<std::recursive_mutex, MutexType>::value;
154 
155  LockData& lockdata = GetLockData();
156  std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
157 
158  LockStack& lock_stack = lockdata.m_lock_stacks[std::this_thread::get_id()];
159  lock_stack.emplace_back(c, locklocation);
160  for (size_t j = 0; j < lock_stack.size() - 1; ++j) {
161  const LockStackItem& i = lock_stack[j];
162  if (i.first == c) {
163  if (is_recursive_mutex) {
164  break;
165  }
166  // It is not a recursive mutex and it appears in the stack two times:
167  // at position `j` and at the end (which we added just before this loop).
168  // Can't allow locking the same (non-recursive) mutex two times from the
169  // same thread as that results in an undefined behavior.
170  auto lock_stack_copy = lock_stack;
171  lock_stack.pop_back();
172  double_lock_detected(c, lock_stack_copy);
173  // double_lock_detected() does not return.
174  }
175 
176  const LockPair p1 = std::make_pair(i.first, c);
177  if (lockdata.lockorders.count(p1))
178  continue;
179 
180  const LockPair p2 = std::make_pair(c, i.first);
181  if (lockdata.lockorders.count(p2)) {
182  auto lock_stack_copy = lock_stack;
183  lock_stack.pop_back();
184  potential_deadlock_detected(p1, lockdata.lockorders[p2], lock_stack_copy);
185  // potential_deadlock_detected() does not return.
186  }
187 
188  lockdata.lockorders.emplace(p1, lock_stack);
189  lockdata.invlockorders.insert(p2);
190  }
191 }
192 
193 static void pop_lock()
194 {
195  LockData& lockdata = GetLockData();
196  std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
197 
198  LockStack& lock_stack = lockdata.m_lock_stacks[std::this_thread::get_id()];
199  lock_stack.pop_back();
200  if (lock_stack.empty()) {
201  lockdata.m_lock_stacks.erase(std::this_thread::get_id());
202  }
203 }
204 
205 template <typename MutexType>
206 void EnterCritical(const char* pszName, const char* pszFile, int nLine, MutexType* cs, bool fTry)
207 {
208  push_lock(cs, CLockLocation(pszName, pszFile, nLine, fTry, util::ThreadGetInternalName()));
209 }
210 template void EnterCritical(const char*, const char*, int, Mutex*, bool);
211 template void EnterCritical(const char*, const char*, int, RecursiveMutex*, bool);
212 template void EnterCritical(const char*, const char*, int, std::mutex*, bool);
213 template void EnterCritical(const char*, const char*, int, std::recursive_mutex*, bool);
214 
215 void CheckLastCritical(void* cs, std::string& lockname, const char* guardname, const char* file, int line)
216 {
217  LockData& lockdata = GetLockData();
218  std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
219 
220  const LockStack& lock_stack = lockdata.m_lock_stacks[std::this_thread::get_id()];
221  if (!lock_stack.empty()) {
222  const auto& lastlock = lock_stack.back();
223  if (lastlock.first == cs) {
224  lockname = lastlock.second.Name();
225  return;
226  }
227  }
228 
229  LogPrintf("INCONSISTENT LOCK ORDER DETECTED\n");
230  LogPrintf("Current lock order (least recent first) is:\n");
231  for (const LockStackItem& i : lock_stack) {
232  LogPrintf(" %s\n", i.second.ToString());
233  }
234  if (g_debug_lockorder_abort) {
235  tfm::format(std::cerr, "%s:%s %s was not most recent critical section locked, details in debug log.\n", file, line, guardname);
236  abort();
237  }
238  throw std::logic_error(strprintf("%s was not most recent critical section locked", guardname));
239 }
240 
241 void LeaveCritical()
242 {
243  pop_lock();
244 }
245 
246 std::string LocksHeld()
247 {
248  LockData& lockdata = GetLockData();
249  std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
250 
251  const LockStack& lock_stack = lockdata.m_lock_stacks[std::this_thread::get_id()];
252  std::string result;
253  for (const LockStackItem& i : lock_stack)
254  result += i.second.ToString() + std::string("\n");
255  return result;
256 }
257 
258 static bool LockHeld(void* mutex)
259 {
260  LockData& lockdata = GetLockData();
261  std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
262 
263  const LockStack& lock_stack = lockdata.m_lock_stacks[std::this_thread::get_id()];
264  for (const LockStackItem& i : lock_stack) {
265  if (i.first == mutex) return true;
266  }
267 
268  return false;
269 }
270 
271 template <typename MutexType>
272 void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, MutexType* cs)
273 {
274  if (LockHeld(cs)) return;
275  tfm::format(std::cerr, "Assertion failed: lock %s not held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld());
276  abort();
277 }
278 template void AssertLockHeldInternal(const char*, const char*, int, Mutex*);
279 template void AssertLockHeldInternal(const char*, const char*, int, RecursiveMutex*);
280 
281 template <typename MutexType>
282 void AssertLockNotHeldInternal(const char* pszName, const char* pszFile, int nLine, MutexType* cs)
283 {
284  if (!LockHeld(cs)) return;
285  tfm::format(std::cerr, "Assertion failed: lock %s held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld());
286  abort();
287 }
288 template void AssertLockNotHeldInternal(const char*, const char*, int, Mutex*);
289 template void AssertLockNotHeldInternal(const char*, const char*, int, RecursiveMutex*);
290 
291 void DeleteLock(void* cs)
292 {
293  LockData& lockdata = GetLockData();
294  std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
295  const LockPair item = std::make_pair(cs, nullptr);
296  LockOrders::iterator it = lockdata.lockorders.lower_bound(item);
297  while (it != lockdata.lockorders.end() && it->first.first == cs) {
298  const LockPair invitem = std::make_pair(it->first.second, it->first.first);
299  lockdata.invlockorders.erase(invitem);
300  lockdata.lockorders.erase(it++);
301  }
302  InvLockOrders::iterator invit = lockdata.invlockorders.lower_bound(item);
303  while (invit != lockdata.invlockorders.end() && invit->first == cs) {
304  const LockPair invinvitem = std::make_pair(invit->second, invit->first);
305  lockdata.lockorders.erase(invinvitem);
306  lockdata.invlockorders.erase(invit++);
307  }
308 }
309 
310 bool LockStackEmpty()
311 {
312  LockData& lockdata = GetLockData();
313  std::lock_guard<std::mutex> lock(lockdata.dd_mutex);
314  const auto it = lockdata.m_lock_stacks.find(std::this_thread::get_id());
315  if (it == lockdata.m_lock_stacks.end()) {
316  return true;
317  }
318  return it->second.empty();
319 }
320 
321 bool g_debug_lockorder_abort = true;
322 
323 #endif /* DEBUG_LOCKORDER */
ToString
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:87
tinyformat::format
void format(std::ostream &out, const char *fmt, const Args &... args)
Format list of arguments to the stream according to given format string.
Definition: tinyformat.h:1062
sync.h
AnnotatedMixin< std::mutex >
CheckLastCritical
void CheckLastCritical(void *cs, std::string &lockname, const char *guardname, const char *file, int line)
Definition: sync.h:75
DeleteLock
void DeleteLock(void *cs)
Definition: sync.h:80
bitcoin-config.h
LeaveCritical
void LeaveCritical()
Definition: sync.h:74
cs
static void pool cs
Definition: mempool_eviction.cpp:12
tinyformat.h
strencodings.h
AssertLockHeldInternal
void AssertLockHeldInternal(const char *pszName, const char *pszFile, int nLine, MutexType *cs) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: sync.h:77
util::ThreadGetInternalName
const std::string & ThreadGetInternalName()
Get the thread's internal (in-memory) name; used e.g.
Definition: threadnames.cpp:53
LogPrintf
#define LogPrintf(...)
Definition: logging.h:185
LockStackEmpty
bool LockStackEmpty()
Definition: sync.h:81
strprintf
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164
EnterCritical
void EnterCritical(const char *pszName, const char *pszFile, int nLine, MutexType *cs, bool fTry=false)
Definition: sync.h:73
logging.h
threadnames.h
AssertLockNotHeldInternal
void AssertLockNotHeldInternal(const char *pszName, const char *pszFile, int nLine, MutexType *cs) LOCKS_EXCLUDED(cs)
Definition: sync.h:79