-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeadlock_tests.cpp
More file actions
611 lines (536 loc) · 17.6 KB
/
Copy pathdeadlock_tests.cpp
File metadata and controls
611 lines (536 loc) · 17.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
// Copyright (c) 2019 Greg Griffith
// Copyright (c) 2019-2020 The Bitcoin Unlimited developers
// Copyright (c) 2021 The Bitcoin 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/setup_common.h>
#include <util/time.h>
#include <boost/test/unit_test.hpp>
#include <atomic>
#include <memory>
#include <optional>
#include <thread>
// The below code explicitly deadlocks in order to test the deadlock detector.
// This leads to false positives for thread sanitizers. So we disable this test
// if compiling with -fsanitize=thread.
#if defined(__SANITIZE_THREAD__)
# define SKIP_SANITIZER_NOT_SUPPORTED
#elif defined(__has_feature)
# if __has_feature(thread_sanitizer)
# define SKIP_SANITIZER_NOT_SUPPORTED
# endif
#endif
BOOST_FIXTURE_TEST_SUITE(deadlock_tests, BasicTestingSetupWithDeadlockExceptions)
#if defined(DEBUG_LOCKORDER) && !defined(SKIP_SANITIZER_NOT_SUPPORTED) // this ifdef covers the bulk of this file
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wthread-safety-analysis"
#endif
#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
// GCC warns excessively about shadowed variable names, which we use in lambdas in
// this test for clarity. So disable that warning.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wshadow"
#endif
// shared lock a shared mutex
// then try to exclusive lock the same shared mutex while holding shared lock,
// should self deadlock
BOOST_AUTO_TEST_CASE(test1) {
SharedMutex shared_mutex;
LOCK_SHARED(shared_mutex);
BOOST_CHECK_THROW(LOCK(shared_mutex), PotentialDeadlockError);
}
// RecursiveMutex version of test1 (self deadlock should not be tripped up here)
BOOST_AUTO_TEST_CASE(test1r) {
RecursiveMutex mutex;
LOCK(mutex);
BOOST_CHECK_NO_THROW(LOCK(mutex));
}
// exclusive lock a shared mutex
// then try to shared lock the same shared mutex while holding the exclusive
// lock, should self deadlock
BOOST_AUTO_TEST_CASE(test2) {
SharedMutex shared_mutex;
LOCK(shared_mutex);
BOOST_CHECK_THROW(LOCK_SHARED(shared_mutex), PotentialDeadlockError);
}
// shared lock a shared mutex
// then try to shared lock the same shared mutex while holding the original
// shared lock, should self deadlock, no recursion allowed in a shared mutex
BOOST_AUTO_TEST_CASE(test3) {
SharedMutex shared_mutex;
LOCK_SHARED(shared_mutex);
BOOST_CHECK_THROW(LOCK_SHARED(shared_mutex), PotentialDeadlockError);
}
// exclusive lock a shared mutex
// then try to exclusive likc the same shared mutex while holding the original
// exclusive lock, should self deadlock, no recursion allowed in a shared mutex
BOOST_AUTO_TEST_CASE(test4) {
SharedMutex shared_mutex;
LOCK(shared_mutex);
BOOST_CHECK_THROW(LOCK(shared_mutex), PotentialDeadlockError);
}
// 2 shared mutex lock themselves then try to
// shared lock each other
// this should deadlock and throw an exception
// We use a "global" (static) shared mutex here.
BOOST_AUTO_TEST_CASE(test5) {
static SharedMutex mutexA;
static SharedMutex mutexB;
struct Context {
std::atomic<bool> done{false};
std::atomic<int> lock_exceptions{0};
std::atomic<int> writelocks{0};
};
using SharedCtx = std::shared_ptr<Context>;
SharedCtx ctx = std::make_shared<Context>();
auto TestThread1 = [](SharedCtx ctx){
LOCK(mutexA);
++ctx->writelocks;
while (ctx->writelocks != 2) ;
try {
LOCK_SHARED(mutexB);
} catch (const PotentialDeadlockError&) {
++ctx->lock_exceptions;
}
while (!ctx->done) ;
};
auto TestThread2 = [](SharedCtx ctx){
LOCK(mutexB);
++ctx->writelocks;
while (ctx->writelocks != 2) ;
try {
LOCK_SHARED(mutexA);
} catch (const PotentialDeadlockError&) {
++ctx->lock_exceptions;
}
while (!ctx->done) ;
};
std::thread thread1(TestThread1, ctx);
std::thread thread2(TestThread2, ctx);
Tic elapsed;
while (!ctx->lock_exceptions && elapsed.secs() < 5) ; // wait for predicate or 5 seconds, whichever is sooner
ctx->done = true;
if (ctx->lock_exceptions != 1) {
// test failure -- detach threads in this case so process can proceed without hanging
thread1.detach();
thread2.detach();
} else {
thread1.join();
thread2.join();
}
BOOST_CHECK(ctx->lock_exceptions == 1);
}
// two shared mutex (A, B)
// thread1 lock_shared A,
// thread2 lock B
// thread1 lock_shared B
// thread2 lock A, should deadlock here
// because thread1 is holding a shared lock on A and is waiting for B
// while thread2 is holding an exclusive lock on B and is waiting for A
BOOST_AUTO_TEST_CASE(test6) {
struct Context {
SharedMutex mutexA;
SharedMutex mutexB;
std::atomic<bool> done{false};
std::atomic<int> lock_exceptions{0};
std::atomic<int> writelocks{0};
std::atomic<int> readlocks{0};
};
using SharedCtx = std::shared_ptr<Context>;
SharedCtx ctx = std::make_shared<Context>();
auto Thread1 = [](SharedCtx ctx){
LOCK_SHARED(ctx->mutexA);
++ctx->readlocks;
while (ctx->writelocks != 1) ;
try {
LOCK_SHARED(ctx->mutexB);
} catch (const PotentialDeadlockError&) {
++ctx->lock_exceptions;
}
while (!ctx->done) ;
};
auto Thread2 = [](SharedCtx ctx){
while (ctx->readlocks != 1) ;
LOCK(ctx->mutexB);
++ctx->writelocks;
try {
LOCK(ctx->mutexA);
} catch (const PotentialDeadlockError&) {
++ctx->lock_exceptions;
}
while (!ctx->done) ;
};
std::thread thread1(Thread1, ctx);
std::thread thread2(Thread2, ctx);
Tic elapsed;
while (!ctx->lock_exceptions && elapsed.secs() < 5) ; // wait for predicate or 5 seconds, whichever is sooner
ctx->done = true;
if (ctx->lock_exceptions != 1) {
// test failure -- detach threads in this case so process can proceed without hanging
thread1.detach();
thread2.detach();
} else {
thread1.join();
thread2.join();
}
BOOST_CHECK(ctx->lock_exceptions == 1);
}
// Threads 1 2 3 and shared mutex A B C
// Thread1 lock_shared A
// Thread2 lock_shared B
// Thread3 lock_shared C
// Thread1 lock B
// Thread2 lock C
// Thread3 lock A
// This tests locking race conditions as well as 3 way and higher lock ordering issues,
// the test is not specific on which thread will deadlock when trying to exclusively lock
// the above indicated shared mutex but one of them will.
BOOST_AUTO_TEST_CASE(test7) {
struct Context {
SharedMutex mutexA;
SharedMutex mutexB;
SharedMutex mutexC;
std::atomic<bool> done{false};
std::atomic<int> lock_exceptions{0};
std::atomic<int> readlocks{0};
};
using SharedCtx = std::shared_ptr<Context>;
SharedCtx ctx = std::make_shared<Context>();
auto Thread1 = [](SharedCtx ctx){
LOCK_SHARED(ctx->mutexA); // 1
++ctx->readlocks;
while (ctx->readlocks != 3) ;
try {
LOCK(ctx->mutexB);
} catch (const PotentialDeadlockError&) {
++ctx->lock_exceptions;
}
while (!ctx->done) ;
};
auto Thread2 = [](SharedCtx ctx) {
while (ctx->readlocks != 1) ;
LOCK_SHARED(ctx->mutexB); // 2
++ctx->readlocks;
while (ctx->readlocks != 3) ;
try {
LOCK(ctx->mutexC);
} catch (const PotentialDeadlockError&) {
++ctx->lock_exceptions;
}
while (!ctx->done) ;
};
auto Thread3 = [](SharedCtx ctx){
while (ctx->readlocks != 2) ;
LOCK_SHARED(ctx->mutexC); // 3
++ctx->readlocks;
while (ctx->readlocks != 3) ;
try {
LOCK(ctx->mutexA);
} catch (const PotentialDeadlockError&) {
++ctx->lock_exceptions;
}
while (!ctx->done) ;
};
std::thread thread1(Thread1, ctx);
std::thread thread2(Thread2, ctx);
std::thread thread3(Thread3, ctx);
Tic elapsed;
while (!ctx->lock_exceptions && elapsed.secs() < 5) ;
ctx->done = true;
if (ctx->lock_exceptions != 1) {
// test failure -- detach threads in this case so process can proceed without hanging
thread1.detach();
thread2.detach();
thread3.detach();
} else {
thread1.join();
thread2.join();
thread3.join();
}
BOOST_CHECK(ctx->lock_exceptions == 1);
}
// Threads 1 2 3 and shared mutex A B C
// Thread1 lock A
// Thread2 lock B
// Thread3 lock C
// Thread1 lock_shared B
// Thread2 lock_shared C
// Thread3 lock_shared A
// This tests locking race conditions as well as 3 way and higher lock ordering issues,
// the test is not specific on which thread will deadlock when trying to shared lock
// the above indicated shared mutex but one of them will.
BOOST_AUTO_TEST_CASE(test8) {
struct Context {
SharedMutex mutexA;
SharedMutex mutexB;
SharedMutex mutexC;
std::atomic<bool> done{false};
std::atomic<int> lock_exceptions{0};
std::atomic<int> writelocks{0};
};
using SharedCtx = std::shared_ptr<Context>;
SharedCtx ctx = std::make_shared<Context>();
auto Thread1 = [](SharedCtx ctx){
LOCK(ctx->mutexA); // 1
++ctx->writelocks;
while (ctx->writelocks != 3) ;
try {
LOCK_SHARED(ctx->mutexB);
} catch (const PotentialDeadlockError&) {
++ctx->lock_exceptions;
}
while (!ctx->done) ;
};
auto Thread2 = [](SharedCtx ctx){
while (ctx->writelocks != 1) ;
LOCK(ctx->mutexB); // 2
++ctx->writelocks;
while (ctx->writelocks != 3) ;
try {
LOCK_SHARED(ctx->mutexC);
} catch (const PotentialDeadlockError&) {
++ctx->lock_exceptions;
}
while (!ctx->done) ;
};
auto Thread3 = [](SharedCtx ctx){
while (ctx->writelocks != 2) ;
LOCK(ctx->mutexC);
++ctx->writelocks;
while (ctx->writelocks != 3) ;
try {
LOCK_SHARED(ctx->mutexA);
} catch (const PotentialDeadlockError&) {
++ctx->lock_exceptions;
}
while (!ctx->done) ;
};
std::thread thread1(Thread1, ctx);
std::thread thread2(Thread2, ctx);
std::thread thread3(Thread3, ctx);
while (!ctx->lock_exceptions) ;
ctx->done = true;
if (ctx->lock_exceptions != 1) {
// test failure -- detach threads in this case so process can proceed without hanging
thread1.detach();
thread2.detach();
thread3.detach();
} else {
thread1.join();
thread2.join();
thread3.join();
}
BOOST_CHECK(ctx->lock_exceptions == 1);
}
// Identical to test8, but uses a RecursiveMutex instead (deadlock should still be detected)
BOOST_AUTO_TEST_CASE(test8r) {
struct Context {
RecursiveMutex mutexA;
RecursiveMutex mutexB;
RecursiveMutex mutexC;
std::atomic<bool> done{false};
std::atomic<int> lock_exceptions{0};
std::atomic<int> writelocks{0};
};
using SharedCtx = std::shared_ptr<Context>;
SharedCtx ctx = std::make_shared<Context>();
auto Thread1 = [](SharedCtx ctx){
LOCK(ctx->mutexA); // 1
++ctx->writelocks;
while (ctx->writelocks != 3) ;
try {
LOCK(ctx->mutexB);
} catch (const PotentialDeadlockError&) {
++ctx->lock_exceptions;
}
while (!ctx->done) ;
};
auto Thread2 = [](SharedCtx ctx){
while (ctx->writelocks != 1) ;
LOCK(ctx->mutexB); // 2
++ctx->writelocks;
while (ctx->writelocks != 3) ;
try {
LOCK(ctx->mutexC);
} catch (const PotentialDeadlockError&) {
ctx->lock_exceptions++;
}
while (!ctx->done) ;
};
auto Thread3 = [](SharedCtx ctx){
while (ctx->writelocks != 2) ;
LOCK(ctx->mutexC);
++ctx->writelocks;
while (ctx->writelocks != 3) ;
try {
LOCK(ctx->mutexA);
} catch (const PotentialDeadlockError&) {
++ctx->lock_exceptions;
}
while (!ctx->done) ;
};
std::thread thread1(Thread1, ctx);
std::thread thread2(Thread2, ctx);
std::thread thread3(Thread3, ctx);
Tic elapsed;
while (!ctx->lock_exceptions && elapsed.secs() < 5) ;
ctx->done = true;
if (ctx->lock_exceptions != 1) {
// test failure -- detach threads in this case so process can proceed without hanging
thread1.detach();
thread2.detach();
thread3.detach();
} else {
thread1.join();
thread2.join();
thread3.join();
}
BOOST_CHECK(ctx->lock_exceptions == 1);
}
// 2 shared mutex lock themselves then try to
// shared lock each other
// this should deadlock and throw an exception
// this is the same as test 5 but we are using pointers to mutex
// instead of global mutex, this is an important difference
BOOST_AUTO_TEST_CASE(test9) {
struct Context {
SharedMutex mutex[2];
std::atomic<bool> done{false};
std::atomic<int> lock_exceptions{0};
std::atomic<int> writelocks{0};
};
using SharedCtx = std::shared_ptr<Context>;
SharedCtx ctx = std::make_shared<Context>();
auto TestThread = [](SharedCtx ctx, SharedMutex *mutexA, SharedMutex *mutexB) {
LOCK(*mutexA);
++ctx->writelocks;
while (ctx->writelocks != 2) ;
try {
LOCK_SHARED(*mutexB);
} catch (const PotentialDeadlockError&) {
++ctx->lock_exceptions;
}
while (!ctx->done) ;
};
std::thread thread1(TestThread, ctx, &ctx->mutex[0], &ctx->mutex[1]);
std::thread thread2(TestThread, ctx, &ctx->mutex[1], &ctx->mutex[0]);
Tic elapsed;
while (!ctx->lock_exceptions && elapsed.secs() < 5) ;
ctx->done = true;
if (ctx->lock_exceptions != 1) {
// test failure -- detach threads in this case so process can proceed without hanging
thread1.detach();
thread2.detach();
} else {
thread1.join();
thread2.join();
}
BOOST_CHECK(ctx->lock_exceptions == 1);
}
// a very basic test to test lock order history tracking
// this should error because a different lock ordering was previously seen
BOOST_AUTO_TEST_CASE(test10) {
SharedMutex mutexA;
SharedMutex mutexB;
auto Thread1 = [&]{
LOCK(mutexA);
LOCK(mutexB);
};
std::atomic_bool didThrow{false};
auto Thread2 = [&]{
LOCK(mutexB);
try {
LOCK(mutexA);
} catch (const PotentialDeadlockError &) {
didThrow = true;
}
};
std::thread thread1(Thread1);
thread1.join();
std::thread thread2(Thread2);
thread2.join();
BOOST_CHECK(didThrow);
}
// RecursiveMutex version of test10
// this should error because a different lock ordering was previously seen
BOOST_AUTO_TEST_CASE(test10r) {
RecursiveMutex mutexA;
RecursiveMutex mutexB;
auto Thread1 = [&]{
LOCK(mutexA);
LOCK(mutexB);
};
std::atomic_bool didThrow{false};
auto Thread2 = [&]{
LOCK(mutexB);
try {
LOCK(mutexA);
} catch (const PotentialDeadlockError &) {
didThrow = true;
}
};
std::thread thread1(Thread1);
thread1.join();
std::thread thread2(Thread2);
thread2.join();
BOOST_CHECK(didThrow);
}
// A test helper that checks that destroying a lock and creating another one
// in the same memory location should lead to a situation where there *is* no
// deadlock detected. This is because on lock destruction, lock histories
// should be cleared for that lock (since it's "going away"!).
template <typename MutexT>
void test11_generic() {
std::optional<MutexT> mutexA;
std::optional<MutexT> mutexB;
for (auto *lockPtr : {&mutexA, &mutexB}) {
// (Re)construct locks (wipe history for this run)
mutexA.emplace();
mutexB.emplace();
auto Thread1 = [&]{
LOCK(mutexA.value());
LOCK(mutexB.value());
};
std::atomic_bool didThrow{false};
auto Thread2 = [&]{
LOCK(mutexB.value());
try {
LOCK(mutexA.value());
} catch (const PotentialDeadlockError &) {
didThrow = true;
}
};
std::thread thread1(Thread1);
thread1.join();
// Now, delete and recreate one of the locks at the same memory
// location (lock history for it should be wiped).
lockPtr->reset();
lockPtr->emplace();
// Even though we are locking B then A in the below thread, one of
// them is "new" (was reconstructed above) and thus it has no
// lock order history, and so no deadlock should be detected.
std::thread thread2(Thread2);
thread2.join();
BOOST_CHECK(!didThrow);
}
}
BOOST_AUTO_TEST_CASE(test11) {
// Run the above test for all 3 lock types
test11_generic<RecursiveMutex>();
test11_generic<Mutex>();
test11_generic<SharedMutex>();
}
#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
#pragma GCC diagnostic pop
#endif
#ifdef __clang__
#pragma clang diagnostic pop
#endif
#else // !DEBUG_LOCKORDER || SKIP_SANITIZER_NOT_SUPPORTED
BOOST_AUTO_TEST_CASE(empty_deadlock_tests) {
BOOST_CHECK("Compile in Debug mode (without sanitize=thread), to enable the deadlock_tests");
}
#endif
BOOST_AUTO_TEST_SUITE_END()