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