Debian Codegraph @

clang/test/SemaCXX/warn-thread-safety-analysis.cpp

raw
1 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -Wthread-safety -Wthread-safety-pointer -Wthread-safety-beta -Wno-thread-safety-negative -fcxx-exceptions -DUSE_CAPABILITY=0 %s
2 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -Wthread-safety -Wthread-safety-pointer -Wthread-safety-beta -Wno-thread-safety-negative -fcxx-exceptions -DUSE_CAPABILITY=1 %s
3 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++17 -Wthread-safety -Wthread-safety-pointer -Wthread-safety-beta -Wno-thread-safety-negative -fcxx-exceptions -DUSE_CAPABILITY=0 %s
4 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++17 -Wthread-safety -Wthread-safety-pointer -Wthread-safety-beta -Wno-thread-safety-negative -fcxx-exceptions -DUSE_CAPABILITY=1 %s
5
6 // FIXME: should also run %clang_cc1 -fsyntax-only -verify -Wthread-safety -std=c++11 -Wc++98-compat %s
7 // FIXME: should also run %clang_cc1 -fsyntax-only -verify -Wthread-safety %s
8
9 #include "thread-safety-annotations.h"
10
11 class LOCKABLE Mutex {
12 public:
13 void Lock() EXCLUSIVE_LOCK_FUNCTION();
14 void ReaderLock() SHARED_LOCK_FUNCTION();
15 void Unlock() UNLOCK_FUNCTION();
16 void ExclusiveUnlock() EXCLUSIVE_UNLOCK_FUNCTION();
17 void ReaderUnlock() SHARED_UNLOCK_FUNCTION();
18 bool TryLock() EXCLUSIVE_TRYLOCK_FUNCTION(true);
19 bool ReaderTryLock() SHARED_TRYLOCK_FUNCTION(true);
20 void LockWhen(const int &cond) EXCLUSIVE_LOCK_FUNCTION();
21
22 void PromoteShared() SHARED_UNLOCK_FUNCTION() EXCLUSIVE_LOCK_FUNCTION();
23 void DemoteExclusive() EXCLUSIVE_UNLOCK_FUNCTION() SHARED_LOCK_FUNCTION();
24
25 // for negative capabilities
26 const Mutex& operator!() const { return *this; }
27
28 void AssertHeld() ASSERT_EXCLUSIVE_LOCK();
29 void AssertReaderHeld() ASSERT_SHARED_LOCK();
30 };
31
32 class SCOPED_LOCKABLE MutexLock {
33 public:
34 MutexLock(Mutex *mu) EXCLUSIVE_LOCK_FUNCTION(mu);
35 MutexLock(Mutex *mu, bool adopt) EXCLUSIVE_LOCKS_REQUIRED(mu);
36 ~MutexLock() UNLOCK_FUNCTION();
37 };
38
39 class SCOPED_LOCKABLE ReaderMutexLock {
40 public:
41 ReaderMutexLock(Mutex *mu) SHARED_LOCK_FUNCTION(mu);
42 ReaderMutexLock(Mutex *mu, bool adopt) SHARED_LOCKS_REQUIRED(mu);
43 ~ReaderMutexLock() UNLOCK_FUNCTION();
44 };
45
46 class SCOPED_LOCKABLE ReleasableMutexLock {
47 public:
48 ReleasableMutexLock(Mutex *mu) EXCLUSIVE_LOCK_FUNCTION(mu);
49 ~ReleasableMutexLock() UNLOCK_FUNCTION();
50
51 void Release() UNLOCK_FUNCTION();
52 };
53
54 class SCOPED_LOCKABLE DoubleMutexLock {
55 public:
56 DoubleMutexLock(Mutex *mu1, Mutex *mu2) EXCLUSIVE_LOCK_FUNCTION(mu1, mu2);
57 ~DoubleMutexLock() UNLOCK_FUNCTION();
58 };
59
60 template<typename Mu>
61 class SCOPED_LOCKABLE TemplateMutexLock {
62 public:
63 TemplateMutexLock(Mu *mu) EXCLUSIVE_LOCK_FUNCTION(mu);
64 ~TemplateMutexLock() UNLOCK_FUNCTION();
65 };
66
67 template<typename... Mus>
68 class SCOPED_LOCKABLE VariadicMutexLock {
69 public:
70 VariadicMutexLock(Mus *...mus) EXCLUSIVE_LOCK_FUNCTION(mus...);
71 ~VariadicMutexLock() UNLOCK_FUNCTION();
72 };
73
74 // The universal lock, written "*", allows checking to be selectively turned
75 // off for a particular piece of code.
76 void beginNoWarnOnReads() SHARED_LOCK_FUNCTION("*");
77 void endNoWarnOnReads() UNLOCK_FUNCTION("*");
78 void beginNoWarnOnWrites() EXCLUSIVE_LOCK_FUNCTION("*");
79 void endNoWarnOnWrites() UNLOCK_FUNCTION("*");
80
81
82 // For testing handling of smart pointers.
83 template<class T>
84 class SmartPtr {
85 public:
86 SmartPtr(T* p) : ptr_(p) { }
87 SmartPtr(const SmartPtr<T>& p) : ptr_(p.ptr_) { }
88 ~SmartPtr();
89
90 T* get() const { return ptr_; }
91 T* operator->() const { return ptr_; }
92 T& operator*() const { return *ptr_; }
93 T& operator[](int i) const { return ptr_[i]; }
94
95 private:
96 T* ptr_;
97 };
98
99 template<typename T, typename U>
100 U& operator->*(const SmartPtr<T>& ptr, U T::*p) { return ptr->*p; }
101
102
103 // For testing destructor calls and cleanup.
104 class MyString {
105 public:
106 MyString(const char* s);
107 ~MyString();
108 };
109
110
111 // For testing operator overloading
112 template <class K, class T>
113 class MyMap {
114 public:
115 T& operator[](const K& k);
116 };
117
118
119 // For testing handling of containers.
120 template <class T>
121 class MyContainer {
122 public:
123 MyContainer();
124
125 typedef T* iterator;
126 typedef const T* const_iterator;
127
128 T* begin();
129 T* end();
130
131 const T* cbegin();
132 const T* cend();
133
134 T& operator[](int i);
135 const T& operator[](int i) const;
136
137 private:
138 T* ptr_;
139 };
140
141
142
143 Mutex sls_mu;
144
145 Mutex sls_mu2 __attribute__((acquired_after(sls_mu)));
146 int sls_guard_var __attribute__((guarded_var)) = 0;
147 int sls_guardby_var __attribute__((guarded_by(sls_mu))) = 0;
148
149 bool getBool();
150
151 class MutexWrapper {
152 public:
153 Mutex mu;
154 int x __attribute__((guarded_by(mu)));
155 void MyLock() EXCLUSIVE_LOCK_FUNCTION(mu);
156 };
157
158 struct TestingMoreComplexAttributes {
159 Mutex lock;
160 struct { Mutex lock; } strct;
161 union {
162 bool a __attribute__((guarded_by(lock)));
163 bool b __attribute__((guarded_by(strct.lock)));
164 bool *ptr_a __attribute__((pt_guarded_by(lock)));
165 bool *ptr_b __attribute__((pt_guarded_by(strct.lock)));
166 Mutex lock1 __attribute__((acquired_before(lock))) __attribute__((acquired_before(strct.lock)));
167 Mutex lock2 __attribute__((acquired_after(lock))) __attribute__((acquired_after(strct.lock)));
168 };
169 } more_complex_atttributes;
170
171 void more_complex_attributes() {
172 more_complex_atttributes.a = true; // expected-warning{{writing variable 'a' requires holding mutex 'lock' exclusively}}
173 more_complex_atttributes.b = true; // expected-warning{{writing variable 'b' requires holding mutex 'strct.lock' exclusively}}
174 *more_complex_atttributes.ptr_a = true; // expected-warning{{writing the value pointed to by 'ptr_a' requires holding mutex 'lock' exclusively}}
175 *more_complex_atttributes.ptr_b = true; // expected-warning{{writing the value pointed to by 'ptr_b' requires holding mutex 'strct.lock' exclusively}}
176
177 more_complex_atttributes.lock.Lock();
178 more_complex_atttributes.lock1.Lock(); // expected-warning{{mutex 'lock1' must be acquired before 'lock'}}
179 more_complex_atttributes.lock1.Unlock();
180 more_complex_atttributes.lock.Unlock();
181
182 more_complex_atttributes.lock2.Lock();
183 more_complex_atttributes.lock.Lock(); // expected-warning{{mutex 'lock' must be acquired before 'lock2'}}
184 more_complex_atttributes.lock.Unlock();
185 more_complex_atttributes.lock2.Unlock();
186 }
187
188 MutexWrapper sls_mw;
189
190 void sls_fun_0() {
191 sls_mw.mu.Lock();
192 sls_mw.x = 5;
193 sls_mw.mu.Unlock();
194 }
195
196 void sls_fun_2() {
197 sls_mu.Lock();
198 int x = sls_guard_var;
199 sls_mu.Unlock();
200 }
201
202 void sls_fun_3() {
203 sls_mu.Lock();
204 sls_guard_var = 2;
205 sls_mu.Unlock();
206 }
207
208 void sls_fun_4() {
209 sls_mu2.Lock();
210 sls_guard_var = 2;
211 sls_mu2.Unlock();
212 }
213
214 void sls_fun_5() {
215 sls_mu.Lock();
216 int x = sls_guardby_var;
217 sls_mu.Unlock();
218 }
219
220 void sls_fun_6() {
221 sls_mu.Lock();
222 sls_guardby_var = 2;
223 sls_mu.Unlock();
224 }
225
226 void sls_fun_7() {
227 sls_mu.Lock();
228 sls_mu2.Lock();
229 sls_mu2.Unlock();
230 sls_mu.Unlock();
231 }
232
233 void sls_fun_8() {
234 sls_mu.Lock();
235 if (getBool())
236 sls_mu.Unlock();
237 else
238 sls_mu.Unlock();
239 }
240
241 void sls_fun_9() {
242 if (getBool())
243 sls_mu.Lock();
244 else
245 sls_mu.Lock();
246 sls_mu.Unlock();
247 }
248
249 void sls_fun_good_6() {
250 if (getBool()) {
251 sls_mu.Lock();
252 } else {
253 if (getBool()) {
254 getBool(); // EMPTY
255 } else {
256 getBool(); // EMPTY
257 }
258 sls_mu.Lock();
259 }
260 sls_mu.Unlock();
261 }
262
263 void sls_fun_good_7() {
264 sls_mu.Lock();
265 while (getBool()) {
266 sls_mu.Unlock();
267 if (getBool()) {
268 if (getBool()) {
269 sls_mu.Lock();
270 continue;
271 }
272 }
273 sls_mu.Lock();
274 }
275 sls_mu.Unlock();
276 }
277
278 void sls_fun_good_8() {
279 sls_mw.MyLock();
280 sls_mw.mu.Unlock();
281 }
282
283 void sls_fun_bad_1() {
284 sls_mu.Unlock(); // \
285 // expected-warning{{releasing mutex 'sls_mu' that was not held}}
286 }
287
288 void sls_fun_bad_2() {
289 sls_mu.Lock(); // expected-note{{mutex acquired here}}
290 sls_mu.Lock(); // \
291 // expected-warning{{acquiring mutex 'sls_mu' that is already held}}
292 sls_mu.Unlock();
293 }
294
295 void sls_fun_bad_3() {
296 sls_mu.Lock(); // expected-note {{mutex acquired here}}
297 } // expected-warning{{mutex 'sls_mu' is still held at the end of function}}
298
299 void sls_fun_bad_4() {
300 if (getBool())
301 sls_mu.Lock(); // expected-note{{mutex acquired here}}
302 else
303 sls_mu2.Lock(); // expected-note{{mutex acquired here}}
304 } // expected-warning{{mutex 'sls_mu' is not held on every path through here}} \
305 // expected-warning{{mutex 'sls_mu2' is not held on every path through here}}
306
307 void sls_fun_bad_5() {
308 sls_mu.Lock(); // expected-note {{mutex acquired here}}
309 if (getBool())
310 sls_mu.Unlock();
311 } // expected-warning{{mutex 'sls_mu' is not held on every path through here}}
312
313 void sls_fun_bad_6() {
314 if (getBool()) {
315 sls_mu.Lock(); // expected-note {{mutex acquired here}}
316 } else {
317 if (getBool()) {
318 getBool(); // EMPTY
319 } else {
320 getBool(); // EMPTY
321 }
322 }
323 sls_mu.Unlock(); // \
324 expected-warning{{mutex 'sls_mu' is not held on every path through here}}\
325 expected-warning{{releasing mutex 'sls_mu' that was not held}}
326 }
327
328 void sls_fun_bad_7() {
329 sls_mu.Lock();
330 while (getBool()) { // \
331 expected-warning{{expecting mutex 'sls_mu' to be held at start of each loop}}
332 sls_mu.Unlock();
333 if (getBool()) {
334 if (getBool()) {
335 continue;
336 }
337 }
338 sls_mu.Lock(); // expected-note {{mutex acquired here}}
339 }
340 sls_mu.Unlock();
341 }
342
343 void sls_fun_bad_8() {
344 sls_mu.Lock(); // expected-note{{mutex acquired here}}
345
346 do {
347 sls_mu.Unlock(); // expected-warning{{expecting mutex 'sls_mu' to be held at start of each loop}}
348 } while (getBool());
349 }
350
351 void sls_fun_bad_9() {
352 do {
353 sls_mu.Lock(); // \
354 // expected-warning{{expecting mutex 'sls_mu' to be held at start of each loop}} \
355 // expected-note{{mutex acquired here}}
356 } while (getBool());
357 sls_mu.Unlock();
358 }
359
360 void sls_fun_bad_10() {
361 sls_mu.Lock(); // expected-note 2{{mutex acquired here}}
362 while(getBool()) { // expected-warning{{expecting mutex 'sls_mu' to be held at start of each loop}}
363 sls_mu.Unlock();
364 }
365 } // expected-warning{{mutex 'sls_mu' is still held at the end of function}}
366
367 void sls_fun_bad_11() {
368 while (getBool()) { // \
369 expected-warning{{expecting mutex 'sls_mu' to be held at start of each loop}}
370 sls_mu.Lock(); // expected-note {{mutex acquired here}}
371 }
372 sls_mu.Unlock(); // \
373 // expected-warning{{releasing mutex 'sls_mu' that was not held}}
374 }
375
376 void sls_fun_bad_12() {
377 sls_mu.Lock(); // expected-note {{mutex acquired here}}
378 while (getBool()) {
379 sls_mu.Unlock();
380 if (getBool()) {
381 if (getBool()) {
382 break;
383 }
384 }
385 sls_mu.Lock();
386 }
387 sls_mu.Unlock(); // \
388 expected-warning{{mutex 'sls_mu' is not held on every path through here}} \
389 expected-warning{{releasing mutex 'sls_mu' that was not held}}
390 }
391
392 //-----------------------------------------//
393 // Handling lock expressions in attribute args
394 // -------------------------------------------//
395
396 Mutex aa_mu;
397
398 class GlobalLocker {
399 public:
400 void globalLock() EXCLUSIVE_LOCK_FUNCTION(aa_mu);
401 void globalUnlock() UNLOCK_FUNCTION(aa_mu);
402 };
403
404 GlobalLocker glock;
405
406 void aa_fun_1() {
407 glock.globalLock();
408 glock.globalUnlock();
409 }
410
411 void aa_fun_bad_1() {
412 glock.globalUnlock(); // \
413 // expected-warning{{releasing mutex 'aa_mu' that was not held}}
414 }
415
416 void aa_fun_bad_2() {
417 glock.globalLock(); // expected-note{{mutex acquired here}}
418 glock.globalLock(); // \
419 // expected-warning{{acquiring mutex 'aa_mu' that is already held}}
420 glock.globalUnlock();
421 }
422
423 void aa_fun_bad_3() {
424 glock.globalLock(); // expected-note{{mutex acquired here}}
425 } // expected-warning{{mutex 'aa_mu' is still held at the end of function}}
426
427 //--------------------------------------------------//
428 // Regression tests for unusual method names
429 //--------------------------------------------------//
430
431 Mutex wmu;
432
433 // Test diagnostics for other method names.
434 class WeirdMethods {
435 // FIXME: can't currently check inside constructors and destructors.
436 WeirdMethods() {
437 wmu.Lock(); // EXPECTED-NOTE {{mutex acquired here}}
438 } // EXPECTED-WARNING {{mutex 'wmu' is still held at the end of function}}
439 ~WeirdMethods() {
440 wmu.Lock(); // EXPECTED-NOTE {{mutex acquired here}}
441 } // EXPECTED-WARNING {{mutex 'wmu' is still held at the end of function}}
442 void operator++() {
443 wmu.Lock(); // expected-note {{mutex acquired here}}
444 } // expected-warning {{mutex 'wmu' is still held at the end of function}}
445 operator int*() {
446 wmu.Lock(); // expected-note {{mutex acquired here}}
447 return 0;
448 } // expected-warning {{mutex 'wmu' is still held at the end of function}}
449 };
450
451 //-----------------------------------------------//
452 // Errors for guarded by or guarded var variables
453 // ----------------------------------------------//
454
455 int *pgb_gvar __attribute__((pt_guarded_var));
456 int *pgb_var __attribute__((pt_guarded_by(sls_mu)));
457
458 class PGBFoo {
459 public:
460 int x;
461 int *pgb_field __attribute__((guarded_by(sls_mu2)))
462 __attribute__((pt_guarded_by(sls_mu)));
463 void testFoo() {
464 pgb_field = &x; // \
465 // expected-warning {{writing variable 'pgb_field' requires holding mutex 'sls_mu2' exclusively}}
466 *pgb_field = x; // expected-warning {{reading variable 'pgb_field' requires holding mutex 'sls_mu2'}} \
467 // expected-warning {{writing the value pointed to by 'pgb_field' requires holding mutex 'sls_mu' exclusively}}
468 x = *pgb_field; // expected-warning {{reading variable 'pgb_field' requires holding mutex 'sls_mu2'}} \
469 // expected-warning {{reading the value pointed to by 'pgb_field' requires holding mutex 'sls_mu'}}
470 (*pgb_field)++; // expected-warning {{reading variable 'pgb_field' requires holding mutex 'sls_mu2'}} \
471 // expected-warning {{writing the value pointed to by 'pgb_field' requires holding mutex 'sls_mu' exclusively}}
472 }
473 };
474
475 class GBFoo {
476 public:
477 int gb_field __attribute__((guarded_by(sls_mu)));
478
479 void testFoo() {
480 gb_field = 0; // \
481 // expected-warning {{writing variable 'gb_field' requires holding mutex 'sls_mu' exclusively}}
482 }
483
484 void testNoAnal() NO_THREAD_SAFETY_ANALYSIS {
485 gb_field = 0;
486 }
487 };
488
489 GBFoo GlobalGBFoo __attribute__((guarded_by(sls_mu)));
490
491 void gb_fun_0() {
492 sls_mu.Lock();
493 int x = *pgb_var;
494 sls_mu.Unlock();
495 }
496
497 void gb_fun_1() {
498 sls_mu.Lock();
499 *pgb_var = 2;
500 sls_mu.Unlock();
501 }
502
503 void gb_fun_2() {
504 int x;
505 pgb_var = &x;
506 }
507
508 void gb_fun_3() {
509 int *x = pgb_var;
510 }
511
512 void gb_bad_0() {
513 sls_guard_var = 1; // \
514 // expected-warning{{writing variable 'sls_guard_var' requires holding any mutex exclusively}}
515 }
516
517 void gb_bad_1() {
518 int x = sls_guard_var; // \
519 // expected-warning{{reading variable 'sls_guard_var' requires holding any mutex}}
520 }
521
522 void gb_bad_2() {
523 sls_guardby_var = 1; // \
524 // expected-warning {{writing variable 'sls_guardby_var' requires holding mutex 'sls_mu' exclusively}}
525 }
526
527 void gb_bad_3() {
528 int x = sls_guardby_var; // \
529 // expected-warning {{reading variable 'sls_guardby_var' requires holding mutex 'sls_mu'}}
530 }
531
532 void gb_bad_4() {
533 *pgb_gvar = 1; // \
534 // expected-warning {{writing the value pointed to by 'pgb_gvar' requires holding any mutex exclusively}}
535 }
536
537 void gb_bad_5() {
538 int x = *pgb_gvar; // \
539 // expected-warning {{reading the value pointed to by 'pgb_gvar' requires holding any mutex}}
540 }
541
542 void gb_bad_6() {
543 *pgb_var = 1; // \
544 // expected-warning {{writing the value pointed to by 'pgb_var' requires holding mutex 'sls_mu' exclusively}}
545 }
546
547 void gb_bad_7() {
548 int x = *pgb_var; // \
549 // expected-warning {{reading the value pointed to by 'pgb_var' requires holding mutex 'sls_mu'}}
550 }
551
552 void gb_bad_8() {
553 GBFoo G;
554 G.gb_field = 0; // \
555 // expected-warning {{writing variable 'gb_field' requires holding mutex 'sls_mu'}}
556 }
557
558 void gb_bad_9() {
559 sls_guard_var++; // \
560 // expected-warning{{writing variable 'sls_guard_var' requires holding any mutex exclusively}}
561 sls_guard_var--; // \
562 // expected-warning{{writing variable 'sls_guard_var' requires holding any mutex exclusively}}
563 ++sls_guard_var; // \
564 // expected-warning{{writing variable 'sls_guard_var' requires holding any mutex exclusively}}
565 --sls_guard_var;// \
566 // expected-warning{{writing variable 'sls_guard_var' requires holding any mutex exclusively}}
567 }
568
569 //-----------------------------------------------//
570 // Warnings on variables with late parsed attributes
571 // ----------------------------------------------//
572
573 class LateFoo {
574 public:
575 int a __attribute__((guarded_by(mu)));
576 int b;
577
578 void foo() EXCLUSIVE_LOCKS_REQUIRED(mu) { }
579
580 void test() {
581 a = 0; // \
582 // expected-warning{{writing variable 'a' requires holding mutex 'mu' exclusively}}
583 b = a; // \
584 // expected-warning {{reading variable 'a' requires holding mutex 'mu'}}
585 c = 0; // \
586 // expected-warning {{writing variable 'c' requires holding mutex 'mu' exclusively}}
587 }
588
589 int c __attribute__((guarded_by(mu)));
590
591 Mutex mu;
592 };
593
594 class LateBar {
595 public:
596 int a_ __attribute__((guarded_by(mu1_)));
597 int b_;
598 int *q __attribute__((pt_guarded_by(mu)));
599 Mutex mu1_;
600 Mutex mu;
601 LateFoo Foo;
602 LateFoo Foo2;
603 LateFoo *FooPointer;
604 };
605
606 LateBar b1, *b3;
607
608 void late_0() {
609 LateFoo FooA;
610 LateFoo FooB;
611 FooA.mu.Lock();
612 FooA.a = 5;
613 FooA.mu.Unlock();
614 }
615
616 void late_1() {
617 LateBar BarA;
618 BarA.FooPointer->mu.Lock();
619 BarA.FooPointer->a = 2;
620 BarA.FooPointer->mu.Unlock();
621 }
622
623 void late_bad_0() {
624 LateFoo fooA;
625 LateFoo fooB;
626 fooA.mu.Lock();
627 fooB.a = 5; // \
628 // expected-warning{{writing variable 'a' requires holding mutex 'fooB.mu' exclusively}} \
629 // expected-note{{found near match 'fooA.mu'}}
630 fooA.mu.Unlock();
631 }
632
633 void late_bad_1() {
634 Mutex mu;
635 mu.Lock();
636 b1.mu1_.Lock();
637 int res = b1.a_ + b3->b_;
638 b3->b_ = *b1.q; // \
639 // expected-warning{{reading the value pointed to by 'q' requires holding mutex 'b1.mu'}}
640 b1.mu1_.Unlock();
641 b1.b_ = res;
642 mu.Unlock();
643 }
644
645 void late_bad_2() {
646 LateBar BarA;
647 BarA.FooPointer->mu.Lock();
648 BarA.Foo.a = 2; // \
649 // expected-warning{{writing variable 'a' requires holding mutex 'BarA.Foo.mu' exclusively}} \
650 // expected-note{{found near match 'BarA.FooPointer->mu'}}
651 BarA.FooPointer->mu.Unlock();
652 }
653
654 void late_bad_3() {
655 LateBar BarA;
656 BarA.Foo.mu.Lock();
657 BarA.FooPointer->a = 2; // \
658 // expected-warning{{writing variable 'a' requires holding mutex 'BarA.FooPointer->mu' exclusively}} \
659 // expected-note{{found near match 'BarA.Foo.mu'}}
660 BarA.Foo.mu.Unlock();
661 }
662
663 void late_bad_4() {
664 LateBar BarA;
665 BarA.Foo.mu.Lock();
666 BarA.Foo2.a = 2; // \
667 // expected-warning{{writing variable 'a' requires holding mutex 'BarA.Foo2.mu' exclusively}} \
668 // expected-note{{found near match 'BarA.Foo.mu'}}
669 BarA.Foo.mu.Unlock();
670 }
671
672 //-----------------------------------------------//
673 // Extra warnings for shared vs. exclusive locks
674 // ----------------------------------------------//
675
676 void shared_fun_0() {
677 sls_mu.Lock();
678 do {
679 sls_mu.Unlock();
680 sls_mu.Lock();
681 } while (getBool());
682 sls_mu.Unlock();
683 }
684
685 void shared_fun_1() {
686 sls_mu.ReaderLock(); // \
687 // expected-note {{the other acquisition of mutex 'sls_mu' is here}}
688 do {
689 sls_mu.Unlock();
690 sls_mu.Lock(); // \
691 // expected-warning {{mutex 'sls_mu' is acquired exclusively and shared in the same scope}}
692 } while (getBool());
693 sls_mu.Unlock();
694 }
695
696 void shared_fun_3() {
697 if (getBool())
698 sls_mu.Lock();
699 else
700 sls_mu.Lock();
701 *pgb_var = 1;
702 sls_mu.Unlock();
703 }
704
705 void shared_fun_4() {
706 if (getBool())
707 sls_mu.ReaderLock();
708 else
709 sls_mu.ReaderLock();
710 int x = sls_guardby_var;
711 sls_mu.Unlock();
712 }
713
714 void shared_fun_8() {
715 if (getBool())
716 sls_mu.Lock(); // \
717 // expected-warning {{mutex 'sls_mu' is acquired exclusively and shared in the same scope}}
718 else
719 sls_mu.ReaderLock(); // \
720 // expected-note {{the other acquisition of mutex 'sls_mu' is here}}
721 sls_mu.Unlock();
722 }
723
724 void shared_fun_9() {
725 sls_mu.Lock();
726 sls_mu.ExclusiveUnlock();
727
728 sls_mu.ReaderLock();
729 sls_mu.ReaderUnlock();
730 }
731
732 void shared_fun_10() {
733 sls_mu.Lock();
734 sls_mu.DemoteExclusive();
735 sls_mu.ReaderUnlock();
736 }
737
738 void shared_fun_11() {
739 sls_mu.ReaderLock();
740 sls_mu.PromoteShared();
741 sls_mu.Unlock();
742 }
743
744 void shared_bad_0() {
745 sls_mu.Lock(); // \
746 // expected-note {{the other acquisition of mutex 'sls_mu' is here}}
747 do {
748 sls_mu.Unlock();
749 sls_mu.ReaderLock(); // \
750 // expected-warning {{mutex 'sls_mu' is acquired exclusively and shared in the same scope}}
751 } while (getBool());
752 sls_mu.Unlock();
753 }
754
755 void shared_bad_1() {
756 if (getBool())
757 sls_mu.Lock(); // \
758 // expected-warning {{mutex 'sls_mu' is acquired exclusively and shared in the same scope}}
759 else
760 sls_mu.ReaderLock(); // \
761 // expected-note {{the other acquisition of mutex 'sls_mu' is here}}
762 *pgb_var = 1;
763 sls_mu.Unlock();
764 }
765
766 void shared_bad_2() {
767 if (getBool())
768 sls_mu.ReaderLock(); // \
769 // expected-warning {{mutex 'sls_mu' is acquired exclusively and shared in the same scope}}
770 else
771 sls_mu.Lock(); // \
772 // expected-note {{the other acquisition of mutex 'sls_mu' is here}}
773 *pgb_var = 1;
774 sls_mu.Unlock();
775 }
776
777 void shared_bad_3() {
778 sls_mu.Lock(); // expected-note {{mutex acquired here}}
779 sls_mu.ReaderUnlock(); // \
780 // expected-warning {{releasing mutex 'sls_mu' using shared access, expected exclusive access}}
781 }
782
783 void shared_bad_4() {
784 sls_mu.ReaderLock(); // expected-note {{mutex acquired here}}
785 sls_mu.ExclusiveUnlock(); // \
786 // expected-warning {{releasing mutex 'sls_mu' using exclusive access, expected shared access}}
787 }
788
789 void shared_bad_5() {
790 sls_mu.Lock(); // expected-note {{mutex acquired here}}
791 sls_mu.PromoteShared(); // \
792 // expected-warning {{releasing mutex 'sls_mu' using shared access, expected exclusive access}}
793 sls_mu.ExclusiveUnlock();
794 }
795
796 void shared_bad_6() {
797 sls_mu.ReaderLock(); // expected-note {{mutex acquired here}}
798 sls_mu.DemoteExclusive(); // \
799 // expected-warning {{releasing mutex 'sls_mu' using exclusive access, expected shared access}}
800 sls_mu.ReaderUnlock();
801 }
802
803 // FIXME: Add support for functions (not only methods)
804 class LRBar {
805 public:
806 void aa_elr_fun() EXCLUSIVE_LOCKS_REQUIRED(aa_mu);
807 void aa_elr_fun_s() SHARED_LOCKS_REQUIRED(aa_mu);
808 void le_fun() __attribute__((locks_excluded(sls_mu)));
809 };
810
811 class LRFoo {
812 public:
813 void test() EXCLUSIVE_LOCKS_REQUIRED(sls_mu);
814 void testShared() SHARED_LOCKS_REQUIRED(sls_mu2);
815 };
816
817 void elr_fun() EXCLUSIVE_LOCKS_REQUIRED(sls_mu);
818 void elr_fun() {}
819
820 LRFoo MyLRFoo;
821 LRBar Bar;
822
823 void es_fun_0() {
824 aa_mu.Lock();
825 Bar.aa_elr_fun();
826 aa_mu.Unlock();
827 }
828
829 void es_fun_1() {
830 aa_mu.Lock();
831 Bar.aa_elr_fun_s();
832 aa_mu.Unlock();
833 }
834
835 void es_fun_2() {
836 aa_mu.ReaderLock();
837 Bar.aa_elr_fun_s();
838 aa_mu.Unlock();
839 }
840
841 void es_fun_3() {
842 sls_mu.Lock();
843 MyLRFoo.test();
844 sls_mu.Unlock();
845 }
846
847 void es_fun_4() {
848 sls_mu2.Lock();
849 MyLRFoo.testShared();
850 sls_mu2.Unlock();
851 }
852
853 void es_fun_5() {
854 sls_mu2.ReaderLock();
855 MyLRFoo.testShared();
856 sls_mu2.Unlock();
857 }
858
859 void es_fun_6() {
860 Bar.le_fun();
861 }
862
863 void es_fun_7() {
864 sls_mu.Lock();
865 elr_fun();
866 sls_mu.Unlock();
867 }
868
869 void es_fun_8() NO_THREAD_SAFETY_ANALYSIS;
870
871 void es_fun_8() {
872 Bar.aa_elr_fun_s();
873 }
874
875 void es_fun_9() SHARED_LOCKS_REQUIRED(aa_mu);
876 void es_fun_9() {
877 Bar.aa_elr_fun_s();
878 }
879
880 void es_fun_10() EXCLUSIVE_LOCKS_REQUIRED(aa_mu);
881 void es_fun_10() {
882 Bar.aa_elr_fun_s();
883 }
884
885 void es_bad_0() {
886 Bar.aa_elr_fun(); // \
887 // expected-warning {{calling function 'aa_elr_fun' requires holding mutex 'aa_mu' exclusively}}
888 }
889
890 void es_bad_1() {
891 aa_mu.ReaderLock();
892 Bar.aa_elr_fun(); // \
893 // expected-warning {{calling function 'aa_elr_fun' requires holding mutex 'aa_mu' exclusively}}
894 aa_mu.Unlock();
895 }
896
897 void es_bad_2() {
898 Bar.aa_elr_fun_s(); // \
899 // expected-warning {{calling function 'aa_elr_fun_s' requires holding mutex 'aa_mu'}}
900 }
901
902 void es_bad_3() {
903 MyLRFoo.test(); // \
904 // expected-warning {{calling function 'test' requires holding mutex 'sls_mu' exclusively}}
905 }
906
907 void es_bad_4() {
908 MyLRFoo.testShared(); // \
909 // expected-warning {{calling function 'testShared' requires holding mutex 'sls_mu2'}}
910 }
911
912 void es_bad_5() {
913 sls_mu.ReaderLock();
914 MyLRFoo.test(); // \
915 // expected-warning {{calling function 'test' requires holding mutex 'sls_mu' exclusively}}
916 sls_mu.Unlock();
917 }
918
919 void es_bad_6() {
920 sls_mu.Lock();
921 Bar.le_fun(); // \
922 // expected-warning {{cannot call function 'le_fun' while mutex 'sls_mu' is held}}
923 sls_mu.Unlock();
924 }
925
926 void es_bad_7() {
927 sls_mu.ReaderLock();
928 Bar.le_fun(); // \
929 // expected-warning {{cannot call function 'le_fun' while mutex 'sls_mu' is held}}
930 sls_mu.Unlock();
931 }
932
933
934 //-----------------------------------------------//
935 // Unparseable lock expressions
936 // ----------------------------------------------//
937
938 // FIXME -- derive new tests for unhandled expressions
939
940
941 //----------------------------------------------------------------------------//
942 // The following test cases are ported from the gcc thread safety implementation
943 // They are each wrapped inside a namespace with the test number of the gcc test
944 //
945 // FIXME: add all the gcc tests, once this analysis passes them.
946 //----------------------------------------------------------------------------//
947
948 //-----------------------------------------//
949 // Good testcases (no errors)
950 //-----------------------------------------//
951
952 namespace thread_annot_lock_20 {
953 class Bar {
954 public:
955 static int func1() EXCLUSIVE_LOCKS_REQUIRED(mu1_);
956 static int b_ GUARDED_BY(mu1_);
957 static Mutex mu1_;
958 static int a_ GUARDED_BY(mu1_);
959 };
960
961 Bar b1;
962
963 int Bar::func1()
964 {
965 int res = 5;
966
967 if (a_ == 4)
968 res = b_;
969 return res;
970 }
971 } // end namespace thread_annot_lock_20
972
973 namespace thread_annot_lock_22 {
974 // Test various usage of GUARDED_BY and PT_GUARDED_BY annotations, especially
975 // uses in class definitions.
976 Mutex mu;
977
978 class Bar {
979 public:
980 int a_ GUARDED_BY(mu1_);
981 int b_;
982 int *q PT_GUARDED_BY(mu);
983 Mutex mu1_ ACQUIRED_AFTER(mu);
984 };
985
986 Bar b1, *b3;
987 int *p GUARDED_BY(mu) PT_GUARDED_BY(mu);
988 int res GUARDED_BY(mu) = 5;
989
990 int func(int i)
991 {
992 int x;
993 mu.Lock();
994 b1.mu1_.Lock();
995 res = b1.a_ + b3->b_;
996 *p = i;
997 b1.a_ = res + b3->b_;
998 b3->b_ = *b1.q;
999 b1.mu1_.Unlock();
1000 b1.b_ = res;
1001 x = res;
1002 mu.Unlock();
1003 return x;
1004 }
1005 } // end namespace thread_annot_lock_22
1006
1007 namespace thread_annot_lock_27_modified {
1008 // test lock annotations applied to function definitions
1009 // Modified: applied annotations only to function declarations
1010 Mutex mu1;
1011 Mutex mu2 ACQUIRED_AFTER(mu1);
1012
1013 class Foo {
1014 public:
1015 int method1(int i) SHARED_LOCKS_REQUIRED(mu2) EXCLUSIVE_LOCKS_REQUIRED(mu1);
1016 };
1017
1018 int Foo::method1(int i) {
1019 return i;
1020 }
1021
1022
1023 int foo(int i) EXCLUSIVE_LOCKS_REQUIRED(mu2) SHARED_LOCKS_REQUIRED(mu1);
1024 int foo(int i) {
1025 return i;
1026 }
1027
1028 static int bar(int i) EXCLUSIVE_LOCKS_REQUIRED(mu1);
1029 static int bar(int i) {
1030 return i;
1031 }
1032
1033 void main() {
1034 Foo a;
1035
1036 mu1.Lock();
1037 mu2.Lock();
1038 a.method1(1);
1039 foo(2);
1040 mu2.Unlock();
1041 bar(3);
1042 mu1.Unlock();
1043 }
1044 } // end namespace thread_annot_lock_27_modified
1045
1046
1047 namespace thread_annot_lock_38 {
1048 // Test the case where a template member function is annotated with lock
1049 // attributes in a non-template class.
1050 class Foo {
1051 public:
1052 void func1(int y) LOCKS_EXCLUDED(mu_);
1053 template <typename T> void func2(T x) LOCKS_EXCLUDED(mu_);
1054 private:
1055 Mutex mu_;
1056 };
1057
1058 Foo *foo;
1059
1060 void main()
1061 {
1062 foo->func1(5);
1063 foo->func2(5);
1064 }
1065 } // end namespace thread_annot_lock_38
1066
1067 namespace thread_annot_lock_43 {
1068 // Tests lock canonicalization
1069 class Foo {
1070 public:
1071 Mutex *mu_;
1072 };
1073
1074 class FooBar {
1075 public:
1076 Foo *foo_;
1077 int GetA() EXCLUSIVE_LOCKS_REQUIRED(foo_->mu_) { return a_; }
1078 int a_ GUARDED_BY(foo_->mu_);
1079 };
1080
1081 FooBar *fb;
1082
1083 void main()
1084 {
1085 int x;
1086 fb->foo_->mu_->Lock();
1087 x = fb->GetA();
1088 fb->foo_->mu_->Unlock();
1089 }
1090 } // end namespace thread_annot_lock_43
1091
1092 namespace thread_annot_lock_49 {
1093 // Test the support for use of lock expression in the annotations
1094 class Foo {
1095 public:
1096 Mutex foo_mu_;
1097 };
1098
1099 class Bar {
1100 private:
1101 Foo *foo;
1102 Mutex bar_mu_ ACQUIRED_AFTER(foo->foo_mu_);
1103
1104 public:
1105 void Test1() {
1106 foo->foo_mu_.Lock();
1107 bar_mu_.Lock();
1108 bar_mu_.Unlock();
1109 foo->foo_mu_.Unlock();
1110 }
1111 };
1112
1113 void main() {
1114 Bar bar;
1115 bar.Test1();
1116 }
1117 } // end namespace thread_annot_lock_49
1118
1119 namespace thread_annot_lock_61_modified {
1120 // Modified to fix the compiler errors
1121 // Test the fix for a bug introduced by the support of pass-by-reference
1122 // parameters.
1123 struct Foo { Foo &operator<< (bool) {return *this;} };
1124 Foo &getFoo();
1125 struct Bar { Foo &func () {return getFoo();} };
1126 struct Bas { void operator& (Foo &) {} };
1127 void mumble()
1128 {
1129 Bas() & Bar().func() << "" << "";
1130 Bas() & Bar().func() << "";
1131 }
1132 } // end namespace thread_annot_lock_61_modified
1133
1134
1135 namespace thread_annot_lock_65 {
1136 // Test the fix for a bug in the support of allowing reader locks for
1137 // non-const, non-modifying overload functions. (We didn't handle the builtin
1138 // properly.)
1139 enum MyFlags {
1140 Zero,
1141 One,
1142 Two,
1143 Three,
1144 Four,
1145 Five,
1146 Six,
1147 Seven,
1148 Eight,
1149 Nine
1150 };
1151
1152 inline MyFlags
1153 operator|(MyFlags a, MyFlags b)
1154 {
1155 return MyFlags(static_cast<int>(a) | static_cast<int>(b));
1156 }
1157
1158 inline MyFlags&
1159 operator|=(MyFlags& a, MyFlags b)
1160 {
1161 return a = a | b;
1162 }
1163 } // end namespace thread_annot_lock_65
1164
1165 namespace thread_annot_lock_66_modified {
1166 // Modified: Moved annotation to function defn
1167 // Test annotations on out-of-line definitions of member functions where the
1168 // annotations refer to locks that are also data members in the class.
1169 Mutex mu;
1170
1171 class Foo {
1172 public:
1173 int method1(int i) SHARED_LOCKS_REQUIRED(mu1, mu, mu2);
1174 int data GUARDED_BY(mu1);
1175 Mutex *mu1;
1176 Mutex *mu2;
1177 };
1178
1179 int Foo::method1(int i)
1180 {
1181 return data + i;
1182 }
1183
1184 void main()
1185 {
1186 Foo a;
1187
1188 a.mu2->Lock();
1189 a.mu1->Lock();
1190 mu.Lock();
1191 a.method1(1);
1192 mu.Unlock();
1193 a.mu1->Unlock();
1194 a.mu2->Unlock();
1195 }
1196 } // end namespace thread_annot_lock_66_modified
1197
1198 namespace thread_annot_lock_68_modified {
1199 // Test a fix to a bug in the delayed name binding with nested template
1200 // instantiation. We use a stack to make sure a name is not resolved to an
1201 // inner context.
1202 template <typename T>
1203 class Bar {
1204 Mutex mu_;
1205 };
1206
1207 template <typename T>
1208 class Foo {
1209 public:
1210 void func(T x) {
1211 mu_.Lock();
1212 count_ = x;
1213 mu_.Unlock();
1214 }
1215
1216 private:
1217 T count_ GUARDED_BY(mu_);
1218 Bar<T> bar_;
1219 Mutex mu_;
1220 };
1221
1222 void main()
1223 {
1224 Foo<int> *foo;
1225 foo->func(5);
1226 }
1227 } // end namespace thread_annot_lock_68_modified
1228
1229 namespace thread_annot_lock_30_modified {
1230 // Test delay parsing of lock attribute arguments with nested classes.
1231 // Modified: trylocks replaced with exclusive_lock_fun
1232 int a = 0;
1233
1234 class Bar {
1235 struct Foo;
1236
1237 public:
1238 void MyLock() EXCLUSIVE_LOCK_FUNCTION(mu);
1239
1240 int func() {
1241 MyLock();
1242 // if (foo == 0) {
1243 // return 0;
1244 // }
1245 a = 5;
1246 mu.Unlock();
1247 return 1;
1248 }
1249
1250 class FooBar {
1251 int x;
1252 int y;
1253 };
1254
1255 private:
1256 Mutex mu;
1257 };
1258
1259 Bar *bar;
1260
1261 void main()
1262 {
1263 bar->func();
1264 }
1265 } // end namespace thread_annot_lock_30_modified
1266
1267 namespace thread_annot_lock_47 {
1268 // Test the support for annotations on virtual functions.
1269 // This is a good test case. (i.e. There should be no warning emitted by the
1270 // compiler.)
1271 class Base {
1272 public:
1273 virtual void func1() EXCLUSIVE_LOCKS_REQUIRED(mu_);
1274 virtual void func2() LOCKS_EXCLUDED(mu_);
1275 Mutex mu_;
1276 };
1277
1278 class Child : public Base {
1279 public:
1280 virtual void func1() EXCLUSIVE_LOCKS_REQUIRED(mu_);
1281 virtual void func2() LOCKS_EXCLUDED(mu_);
1282 };
1283
1284 void main() {
1285 Child *c;
1286 Base *b = c;
1287
1288 b->mu_.Lock();
1289 b->func1();
1290 b->mu_.Unlock();
1291 b->func2();
1292
1293 c->mu_.Lock();
1294 c->func1();
1295 c->mu_.Unlock();
1296 c->func2();
1297 }
1298 } // end namespace thread_annot_lock_47
1299
1300 //-----------------------------------------//
1301 // Tests which produce errors
1302 //-----------------------------------------//
1303
1304 namespace thread_annot_lock_13 {
1305 Mutex mu1;
1306 Mutex mu2;
1307
1308 int g GUARDED_BY(mu1);
1309 int w GUARDED_BY(mu2);
1310
1311 class Foo {
1312 public:
1313 void bar() LOCKS_EXCLUDED(mu_, mu1);
1314 int foo() SHARED_LOCKS_REQUIRED(mu_) EXCLUSIVE_LOCKS_REQUIRED(mu2);
1315
1316 private:
1317 int a_ GUARDED_BY(mu_);
1318 public:
1319 Mutex mu_ ACQUIRED_AFTER(mu1);
1320 };
1321
1322 int Foo::foo()
1323 {
1324 int res;
1325 w = 5;
1326 res = a_ + 5;
1327 return res;
1328 }
1329
1330 void Foo::bar()
1331 {
1332 int x;
1333 mu_.Lock();
1334 x = foo(); // expected-warning {{calling function 'foo' requires holding mutex 'mu2' exclusively}}
1335 a_ = x + 1;
1336 mu_.Unlock();
1337 if (x > 5) {
1338 mu1.Lock();
1339 g = 2;
1340 mu1.Unlock();
1341 }
1342 }
1343
1344 void main()
1345 {
1346 Foo f1, *f2;
1347 f1.mu_.Lock();
1348 f1.bar(); // expected-warning {{cannot call function 'bar' while mutex 'f1.mu_' is held}}
1349 mu2.Lock();
1350 f1.foo();
1351 mu2.Unlock();
1352 f1.mu_.Unlock();
1353 f2->mu_.Lock();
1354 f2->bar(); // expected-warning {{cannot call function 'bar' while mutex 'f2->mu_' is held}}
1355 f2->mu_.Unlock();
1356 mu2.Lock();
1357 w = 2;
1358 mu2.Unlock();
1359 }
1360 } // end namespace thread_annot_lock_13
1361
1362 namespace thread_annot_lock_18_modified {
1363 // Modified: Trylocks removed
1364 // Test the ability to distnguish between the same lock field of
1365 // different objects of a class.
1366 class Bar {
1367 public:
1368 bool MyLock() EXCLUSIVE_LOCK_FUNCTION(mu1_);
1369 void MyUnlock() UNLOCK_FUNCTION(mu1_);
1370 int a_ GUARDED_BY(mu1_);
1371
1372 private:
1373 Mutex mu1_;
1374 };
1375
1376 Bar *b1, *b2;
1377
1378 void func()
1379 {
1380 b1->MyLock();
1381 b1->a_ = 5;
1382 b2->a_ = 3; // \
1383 // expected-warning {{writing variable 'a_' requires holding mutex 'b2->mu1_' exclusively}} \
1384 // expected-note {{found near match 'b1->mu1_'}}
1385 b2->MyLock();
1386 b2->MyUnlock();
1387 b1->MyUnlock();
1388 }
1389 } // end namespace thread_annot_lock_18_modified
1390
1391 namespace thread_annot_lock_21 {
1392 // Test various usage of GUARDED_BY and PT_GUARDED_BY annotations, especially
1393 // uses in class definitions.
1394 Mutex mu;
1395
1396 class Bar {
1397 public:
1398 int a_ GUARDED_BY(mu1_);
1399 int b_;
1400 int *q PT_GUARDED_BY(mu);
1401 Mutex mu1_ ACQUIRED_AFTER(mu);
1402 };
1403
1404 Bar b1, *b3;
1405 int *p GUARDED_BY(mu) PT_GUARDED_BY(mu);
1406
1407 int res GUARDED_BY(mu) = 5;
1408
1409 int func(int i)
1410 {
1411 int x;
1412 b3->mu1_.Lock();
1413 res = b1.a_ + b3->b_; // expected-warning {{reading variable 'a_' requires holding mutex 'b1.mu1_'}} \
1414 // expected-warning {{writing variable 'res' requires holding mutex 'mu' exclusively}} \
1415 // expected-note {{found near match 'b3->mu1_'}}
1416 *p = i; // expected-warning {{reading variable 'p' requires holding mutex 'mu'}} \
1417 // expected-warning {{writing the value pointed to by 'p' requires holding mutex 'mu' exclusively}}
1418 b1.a_ = res + b3->b_; // expected-warning {{reading variable 'res' requires holding mutex 'mu'}} \
1419 // expected-warning {{writing variable 'a_' requires holding mutex 'b1.mu1_' exclusively}} \
1420 // expected-note {{found near match 'b3->mu1_'}}
1421 b3->b_ = *b1.q; // expected-warning {{reading the value pointed to by 'q' requires holding mutex 'mu'}}
1422 b3->mu1_.Unlock();
1423 b1.b_ = res; // expected-warning {{reading variable 'res' requires holding mutex 'mu'}}
1424 x = res; // expected-warning {{reading variable 'res' requires holding mutex 'mu'}}
1425 return x;
1426 }
1427 } // end namespace thread_annot_lock_21
1428
1429 namespace thread_annot_lock_35_modified {
1430 // Test the analyzer's ability to distinguish the lock field of different
1431 // objects.
1432 class Foo {
1433 private:
1434 Mutex lock_;
1435 int a_ GUARDED_BY(lock_);
1436
1437 public:
1438 void Func(Foo* child) LOCKS_EXCLUDED(lock_) {
1439 Foo *new_foo = new Foo;
1440
1441 lock_.Lock();
1442
1443 child->Func(new_foo); // There shouldn't be any warning here as the
1444 // acquired lock is not in child.
1445 child->bar(7); // \
1446 // expected-warning {{calling function 'bar' requires holding mutex 'child->lock_' exclusively}} \
1447 // expected-note {{found near match 'lock_'}}
1448 child->a_ = 5; // \
1449 // expected-warning {{writing variable 'a_' requires holding mutex 'child->lock_' exclusively}} \
1450 // expected-note {{found near match 'lock_'}}
1451 lock_.Unlock();
1452 }
1453
1454 void bar(int y) EXCLUSIVE_LOCKS_REQUIRED(lock_) {
1455 a_ = y;
1456 }
1457 };
1458
1459 Foo *x;
1460
1461 void main() {
1462 Foo *child = new Foo;
1463 x->Func(child);
1464 }
1465 } // end namespace thread_annot_lock_35_modified
1466
1467 namespace thread_annot_lock_36_modified {
1468 // Modified to move the annotations to function defns.
1469 // Test the analyzer's ability to distinguish the lock field of different
1470 // objects
1471 class Foo {
1472 private:
1473 Mutex lock_;
1474 int a_ GUARDED_BY(lock_);
1475
1476 public:
1477 void Func(Foo* child) LOCKS_EXCLUDED(lock_);
1478 void bar(int y) EXCLUSIVE_LOCKS_REQUIRED(lock_);
1479 };
1480
1481 void Foo::Func(Foo* child) {
1482 Foo *new_foo = new Foo;
1483
1484 lock_.Lock();
1485
1486 child->lock_.Lock();
1487 child->Func(new_foo); // expected-warning {{cannot call function 'Func' while mutex 'child->lock_' is held}}
1488 child->bar(7);
1489 child->a_ = 5;
1490 child->lock_.Unlock();
1491
1492 lock_.Unlock();
1493 }
1494
1495 void Foo::bar(int y) {
1496 a_ = y;
1497 }
1498
1499
1500 Foo *x;
1501
1502 void main() {
1503 Foo *child = new Foo;
1504 x->Func(child);
1505 }
1506 } // end namespace thread_annot_lock_36_modified
1507
1508
1509 namespace thread_annot_lock_42 {
1510 // Test support of multiple lock attributes of the same kind on a decl.
1511 class Foo {
1512 private:
1513 Mutex mu1, mu2, mu3;
1514 int x GUARDED_BY(mu1) GUARDED_BY(mu2);
1515 int y GUARDED_BY(mu2);
1516
1517 void f2() LOCKS_EXCLUDED(mu1) LOCKS_EXCLUDED(mu2) LOCKS_EXCLUDED(mu3) {
1518 mu2.Lock();
1519 y = 2;
1520 mu2.Unlock();
1521 }
1522
1523 public:
1524 void f1() EXCLUSIVE_LOCKS_REQUIRED(mu2) EXCLUSIVE_LOCKS_REQUIRED(mu1) {
1525 x = 5;
1526 f2(); // expected-warning {{cannot call function 'f2' while mutex 'mu1' is held}} \
1527 // expected-warning {{cannot call function 'f2' while mutex 'mu2' is held}}
1528 }
1529 };
1530
1531 Foo *foo;
1532
1533 void func()
1534 {
1535 foo->f1(); // expected-warning {{calling function 'f1' requires holding mutex 'foo->mu2' exclusively}} \
1536 // expected-warning {{calling function 'f1' requires holding mutex 'foo->mu1' exclusively}}
1537 }
1538 } // end namespace thread_annot_lock_42
1539
1540 namespace thread_annot_lock_46 {
1541 // Test the support for annotations on virtual functions.
1542 class Base {
1543 public:
1544 virtual void func1() EXCLUSIVE_LOCKS_REQUIRED(mu_);
1545 virtual void func2() LOCKS_EXCLUDED(mu_);
1546 Mutex mu_;
1547 };
1548
1549 class Child : public Base {
1550 public:
1551 virtual void func1() EXCLUSIVE_LOCKS_REQUIRED(mu_);
1552 virtual void func2() LOCKS_EXCLUDED(mu_);
1553 };
1554
1555 void main() {
1556 Child *c;
1557 Base *b = c;
1558
1559 b->func1(); // expected-warning {{calling function 'func1' requires holding mutex 'c->mu_' exclusively}}
1560 b->mu_.Lock();
1561 b->func2(); // expected-warning {{cannot call function 'func2' while mutex 'c->mu_' is held}}
1562 b->mu_.Unlock();
1563
1564 c->func1(); // expected-warning {{calling function 'func1' requires holding mutex 'c->mu_' exclusively}}
1565 c->mu_.Lock();
1566 c->func2(); // expected-warning {{cannot call function 'func2' while mutex 'c->mu_' is held}}
1567 c->mu_.Unlock();
1568 }
1569 } // end namespace thread_annot_lock_46
1570
1571 namespace thread_annot_lock_67_modified {
1572 // Modified: attributes on definitions moved to declarations
1573 // Test annotations on out-of-line definitions of member functions where the
1574 // annotations refer to locks that are also data members in the class.
1575 Mutex mu;
1576 Mutex mu3;
1577
1578 class Foo {
1579 public:
1580 int method1(int i) SHARED_LOCKS_REQUIRED(mu1, mu, mu2, mu3);
1581 int data GUARDED_BY(mu1);
1582 Mutex *mu1;
1583 Mutex *mu2;
1584 };
1585
1586 int Foo::method1(int i) {
1587 return data + i;
1588 }
1589
1590 void main()
1591 {
1592 Foo a;
1593 a.method1(1); // expected-warning {{calling function 'method1' requires holding mutex 'a.mu1'}} \
1594 // expected-warning {{calling function 'method1' requires holding mutex 'mu'}} \
1595 // expected-warning {{calling function 'method1' requires holding mutex 'a.mu2'}} \
1596 // expected-warning {{calling function 'method1' requires holding mutex 'mu3'}}
1597 }
1598 } // end namespace thread_annot_lock_67_modified
1599
1600
1601 namespace substitution_test {
1602 class MyData {
1603 public:
1604 Mutex mu;
1605
1606 void lockData() EXCLUSIVE_LOCK_FUNCTION(mu);
1607 void unlockData() UNLOCK_FUNCTION(mu);
1608
1609 void doSomething() EXCLUSIVE_LOCKS_REQUIRED(mu) { }
1610 void operator()() EXCLUSIVE_LOCKS_REQUIRED(mu) { }
1611
1612 MyData operator+(const MyData& other) const SHARED_LOCKS_REQUIRED(mu, other.mu);
1613 };
1614
1615 MyData operator-(const MyData& a, const MyData& b) SHARED_LOCKS_REQUIRED(a.mu, b.mu);
1616
1617 class DataLocker {
1618 public:
1619 void lockData (MyData *d) EXCLUSIVE_LOCK_FUNCTION(d->mu);
1620 void unlockData(MyData *d) UNLOCK_FUNCTION(d->mu);
1621 };
1622
1623
1624 class Foo {
1625 public:
1626 void foo(MyData* d) EXCLUSIVE_LOCKS_REQUIRED(d->mu) { }
1627
1628 void subst(MyData& d) {
1629 d.doSomething(); // expected-warning {{calling function 'doSomething' requires holding mutex 'd.mu' exclusively}}
1630 d(); // expected-warning {{calling function 'operator()' requires holding mutex 'd.mu' exclusively}}
1631 d.operator()(); // expected-warning {{calling function 'operator()' requires holding mutex 'd.mu' exclusively}}
1632
1633 d.lockData();
1634 d.doSomething();
1635 d();
1636 d.operator()();
1637 d.unlockData();
1638 }
1639
1640 void binop(MyData& a, MyData& b) EXCLUSIVE_LOCKS_REQUIRED(a.mu) {
1641 a + b; // expected-warning {{calling function 'operator+' requires holding mutex 'b.mu'}}
1642 // expected-note@-1 {{found near match 'a.mu'}}
1643 b + a; // expected-warning {{calling function 'operator+' requires holding mutex 'b.mu'}}
1644 // expected-note@-1 {{found near match 'a.mu'}}
1645 a - b; // expected-warning {{calling function 'operator-' requires holding mutex 'b.mu'}}
1646 // expected-note@-1 {{found near match 'a.mu'}}
1647 }
1648
1649 void bar1(MyData* d) {
1650 d->lockData();
1651 foo(d);
1652 d->unlockData();
1653 }
1654
1655 void bar2(MyData* d) {
1656 DataLocker dlr;
1657 dlr.lockData(d);
1658 foo(d);
1659 dlr.unlockData(d);
1660 }
1661
1662 void bar3(MyData* d1, MyData* d2) {
1663 DataLocker dlr;
1664 dlr.lockData(d1); // expected-note {{mutex acquired here}}
1665 dlr.unlockData(d2); // \
1666 // expected-warning {{releasing mutex 'd2->mu' that was not held}}
1667 } // expected-warning {{mutex 'd1->mu' is still held at the end of function}}
1668
1669 void bar4(MyData* d1, MyData* d2) {
1670 DataLocker dlr;
1671 dlr.lockData(d1);
1672 foo(d2); // \
1673 // expected-warning {{calling function 'foo' requires holding mutex 'd2->mu' exclusively}} \
1674 // expected-note {{found near match 'd1->mu'}}
1675 dlr.unlockData(d1);
1676 }
1677 };
1678
1679 // Automatic object destructor calls don't appear as expressions in the CFG,
1680 // so we have to handle them separately whenever substitutions are required.
1681 struct DestructorRequires {
1682 Mutex mu;
1683 ~DestructorRequires() EXCLUSIVE_LOCKS_REQUIRED(mu);
1684 };
1685
1686 void destructorRequires() {
1687 DestructorRequires rd;
1688 rd.mu.AssertHeld();
1689 }
1690
1691 struct DestructorExcludes {
1692 Mutex mu;
1693 ~DestructorExcludes() LOCKS_EXCLUDED(mu);
1694 };
1695
1696 void destructorExcludes() {
1697 DestructorExcludes ed;
1698 ed.mu.Lock(); // expected-note {{mutex acquired here}}
1699 } // expected-warning {{cannot call function '~DestructorExcludes' while mutex 'ed.mu' is held}}
1700 // expected-warning@-1 {{mutex 'ed.mu' is still held at the end of function}}
1701
1702 } // end namespace substituation_test
1703
1704
1705
1706 namespace constructor_destructor_tests {
1707 Mutex fooMu;
1708 int myVar GUARDED_BY(fooMu);
1709
1710 class Foo {
1711 public:
1712 Foo() EXCLUSIVE_LOCK_FUNCTION(fooMu) { }
1713 ~Foo() UNLOCK_FUNCTION(fooMu) { }
1714 };
1715
1716 void fooTest() {
1717 Foo foo;
1718 myVar = 0;
1719 }
1720 }
1721
1722
1723 namespace template_member_test {
1724
1725 struct S { int n; };
1726 struct T {
1727 Mutex m;
1728 S *s GUARDED_BY(this->m);
1729 };
1730 Mutex m;
1731 struct U {
1732 union {
1733 int n;
1734 };
1735 } *u GUARDED_BY(m);
1736
1737 template<typename U>
1738 struct IndirectLock {
1739 int DoNaughtyThings(T *t) {
1740 u->n = 0; // expected-warning {{reading variable 'u' requires holding mutex 'm'}}
1741 return t->s->n; // expected-warning {{reading variable 's' requires holding mutex 't->m'}}
1742 }
1743 };
1744
1745 template struct IndirectLock<int>; // expected-note {{here}}
1746
1747 struct V {
1748 void f(int);
1749 void f(double);
1750
1751 Mutex m;
1752 V *p GUARDED_BY(this->m);
1753 };
1754 template<typename U> struct W {
1755 V v;
1756 void f(U u) {
1757 v.p->f(u); // expected-warning {{reading variable 'p' requires holding mutex 'v.m'}}
1758 }
1759 };
1760 template struct W<int>; // expected-note {{here}}
1761
1762 }
1763
1764 namespace test_scoped_lockable {
1765
1766 struct TestScopedLockable {
1767 Mutex mu1;
1768 Mutex mu2;
1769 int a __attribute__((guarded_by(mu1)));
1770 int b __attribute__((guarded_by(mu2)));
1771
1772 bool getBool();
1773
1774 bool lock2Bool(MutexLock);
1775
1776 void foo1() {
1777 MutexLock mulock(&mu1);
1778 a = 5;
1779 }
1780
1781 #ifdef __cpp_guaranteed_copy_elision
1782 void const_lock() {
1783 const MutexLock mulock = MutexLock(&mu1);
1784 a = 5;
1785 }
1786 #endif
1787
1788 void temporary() {
1789 MutexLock{&mu1}, a = 5;
1790 }
1791
1792 void temporary_cfg(int x) {
1793 // test the case where a pair of temporary Ctor and Dtor is in different CFG blocks
1794 lock2Bool(MutexLock{&mu1}) || x;
1795 MutexLock{&mu1}; // no-warn
1796 }
1797
1798 void lifetime_extension() {
1799 const MutexLock &mulock = MutexLock(&mu1);
1800 a = 5;
1801 }
1802
1803 void foo2() {
1804 ReaderMutexLock mulock1(&mu1);
1805 if (getBool()) {
1806 MutexLock mulock2a(&mu2);
1807 b = a + 1;
1808 }
1809 else {
1810 MutexLock mulock2b(&mu2);
1811 b = a + 2;
1812 }
1813 }
1814
1815 void foo3() {
1816 MutexLock mulock_a(&mu1); // expected-note{{mutex acquired here}}
1817 MutexLock mulock_b(&mu1); // \
1818 // expected-warning {{acquiring mutex 'mu1' that is already held}}
1819 }
1820
1821 void temporary_double_lock() {
1822 MutexLock mulock_a(&mu1); // expected-note{{mutex acquired here}}
1823 MutexLock{&mu1}; // \
1824 // expected-warning {{acquiring mutex 'mu1' that is already held}}
1825 }
1826
1827 void foo4() {
1828 MutexLock mulock1(&mu1), mulock2(&mu2);
1829 a = b+1;
1830 b = a+1;
1831 }
1832
1833 void foo5() {
1834 DoubleMutexLock mulock(&mu1, &mu2);
1835 a = b + 1;
1836 b = a + 1;
1837 }
1838
1839 void foo6() {
1840 TemplateMutexLock<Mutex> mulock1(&mu1), mulock2(&mu2);
1841 a = b + 1;
1842 b = a + 1;
1843 }
1844
1845 void foo7() {
1846 VariadicMutexLock<Mutex, Mutex> mulock(&mu1, &mu2);
1847 a = b + 1;
1848 b = a + 1;
1849 }
1850 };
1851
1852 namespace test_function_param_lock_unlock {
1853 class A {
1854 public:
1855 A() EXCLUSIVE_LOCK_FUNCTION(mu_) { mu_.Lock(); }
1856 ~A() UNLOCK_FUNCTION(mu_) { mu_.Unlock(); }
1857 private:
1858 Mutex mu_;
1859 };
1860 int do_something(A a) { return 0; }
1861
1862 // Unlock in dtor without lock in ctor.
1863 // FIXME: We cannot detect that we are releasing a lock that was never held!
1864 class B {
1865 public:
1866 B() {}
1867 B(int) {}
1868 ~B() UNLOCK_FUNCTION(mu_) { mu_.Unlock(); }
1869 private:
1870 Mutex mu_;
1871 };
1872 int do_something(B b) { return 0; }
1873
1874 class SCOPED_LOCKABLE MutexWrapper {
1875 public:
1876 MutexWrapper(Mutex *mu) : mu_(mu) {}
1877 ~MutexWrapper() UNLOCK_FUNCTION(mu_) { mu_->Unlock(); }
1878 void Lock() EXCLUSIVE_LOCK_FUNCTION(mu_) { mu_->Lock(); }
1879
1880 Mutex *mu_;
1881 };
1882 // FIXME: This is a false-positive as the lock is released by the dtor.
1883 void do_something(MutexWrapper mw) {
1884 mw.Lock(); // expected-note {{mutex acquired here}}
1885 } // expected-warning {{mutex 'mw.mu_' is still held at the end of function}}
1886
1887 } // namespace test_function_param_lock_unlock
1888
1889 } // end namespace test_scoped_lockable
1890
1891
1892 namespace FunctionAttrTest {
1893
1894 class Foo {
1895 public:
1896 Mutex mu_;
1897 int a GUARDED_BY(mu_);
1898 };
1899
1900 Foo fooObj;
1901
1902 void foo() EXCLUSIVE_LOCKS_REQUIRED(fooObj.mu_);
1903
1904 void bar() {
1905 foo(); // expected-warning {{calling function 'foo' requires holding mutex 'fooObj.mu_' exclusively}}
1906 fooObj.mu_.Lock();
1907 foo();
1908 fooObj.mu_.Unlock();
1909 }
1910
1911 }; // end namespace FunctionAttrTest
1912
1913
1914 namespace TryLockTest {
1915
1916 struct TestTryLock {
1917 Mutex mu;
1918 int a GUARDED_BY(mu);
1919 bool cond;
1920
1921 void foo1() {
1922 if (mu.TryLock()) {
1923 a = 1;
1924 mu.Unlock();
1925 }
1926 }
1927
1928 void foo2() {
1929 if (!mu.TryLock()) return;
1930 a = 2;
1931 mu.Unlock();
1932 }
1933
1934 void foo2_builtin_expect() {
1935 if (__builtin_expect(!mu.TryLock(), false))
1936 return;
1937 a = 2;
1938 mu.Unlock();
1939 }
1940
1941 void foo3() {
1942 bool b = mu.TryLock();
1943 if (b) {
1944 a = 3;
1945 mu.Unlock();
1946 }
1947 }
1948
1949 void foo3_builtin_expect() {
1950 bool b = mu.TryLock();
1951 if (__builtin_expect(b, true)) {
1952 a = 3;
1953 mu.Unlock();
1954 }
1955 }
1956
1957 void foo4() {
1958 bool b = mu.TryLock();
1959 if (!b) return;
1960 a = 4;
1961 mu.Unlock();
1962 }
1963
1964 void foo5() {
1965 while (mu.TryLock()) {
1966 a = a + 1;
1967 mu.Unlock();
1968 }
1969 }
1970
1971 void foo6() {
1972 bool b = mu.TryLock();
1973 b = !b;
1974 if (b) return;
1975 a = 6;
1976 mu.Unlock();
1977 }
1978
1979 void foo7() {
1980 bool b1 = mu.TryLock();
1981 bool b2 = !b1;
1982 bool b3 = !b2;
1983 if (b3) {
1984 a = 7;
1985 mu.Unlock();
1986 }
1987 }
1988
1989 // Test use-def chains: join points
1990 void foo8() {
1991 bool b = mu.TryLock();
1992 bool b2 = b;
1993 if (cond)
1994 b = true;
1995 if (b) { // b should be unknown at this point, because of the join point
1996 a = 8; // expected-warning {{writing variable 'a' requires holding mutex 'mu' exclusively}}
1997 }
1998 if (b2) { // b2 should be known at this point.
1999 a = 8;
2000 mu.Unlock();
2001 }
2002 }
2003
2004 // Test use-def-chains: back edges
2005 void foo9() {
2006 bool b = mu.TryLock();
2007
2008 for (int i = 0; i < 10; ++i);
2009
2010 if (b) { // b is still known, because the loop doesn't alter it
2011 a = 9;
2012 mu.Unlock();
2013 }
2014 }
2015
2016 // Test use-def chains: back edges
2017 void foo10() {
2018 bool b = mu.TryLock();
2019
2020 while (cond) {
2021 if (b) { // b should be unknown at this point b/c of the loop
2022 a = 10; // expected-warning {{writing variable 'a' requires holding mutex 'mu' exclusively}}
2023 }
2024 b = !b;
2025 }
2026 }
2027
2028 // Test merge of exclusive trylock
2029 void foo11() {
2030 if (cond) {
2031 if (!mu.TryLock())
2032 return;
2033 }
2034 else {
2035 mu.Lock();
2036 }
2037 a = 10;
2038 mu.Unlock();
2039 }
2040
2041 // Test merge of shared trylock
2042 void foo12() {
2043 if (cond) {
2044 if (!mu.ReaderTryLock())
2045 return;
2046 }
2047 else {
2048 mu.ReaderLock();
2049 }
2050 int i = a;
2051 mu.Unlock();
2052 }
2053
2054 // Test with conditional operator
2055 void foo13() {
2056 if (mu.TryLock() ? 1 : 0)
2057 mu.Unlock();
2058 }
2059
2060 void foo14() {
2061 if (mu.TryLock() ? 0 : 1)
2062 return;
2063 mu.Unlock();
2064 }
2065
2066 void foo15() {
2067 if (mu.TryLock() ? 0 : 1) // expected-note{{mutex acquired here}}
2068 mu.Unlock(); // expected-warning{{releasing mutex 'mu' that was not held}}
2069 } // expected-warning{{mutex 'mu' is not held on every path through here}}
2070 }; // end TestTrylock
2071
2072 } // end namespace TrylockTest
2073
2074
2075 namespace TestTemplateAttributeInstantiation {
2076
2077 class Foo1 {
2078 public:
2079 Mutex mu_;
2080 int a GUARDED_BY(mu_);
2081 };
2082
2083 class Foo2 {
2084 public:
2085 int a GUARDED_BY(mu_);
2086 Mutex mu_;
2087 };
2088
2089
2090 class Bar {
2091 public:
2092 // Test non-dependent expressions in attributes on template functions
2093 template <class T>
2094 void barND(Foo1 *foo, T *fooT) EXCLUSIVE_LOCKS_REQUIRED(foo->mu_) {
2095 foo->a = 0;
2096 }
2097
2098 // Test dependent expressions in attributes on template functions
2099 template <class T>
2100 void barD(Foo1 *foo, T *fooT) EXCLUSIVE_LOCKS_REQUIRED(fooT->mu_) {
2101 fooT->a = 0;
2102 }
2103 };
2104
2105
2106 template <class T>
2107 class BarT {
2108 public:
2109 Foo1 fooBase;
2110 T fooBaseT;
2111
2112 // Test non-dependent expression in ordinary method on template class
2113 void barND() EXCLUSIVE_LOCKS_REQUIRED(fooBase.mu_) {
2114 fooBase.a = 0;
2115 }
2116
2117 // Test dependent expressions in ordinary methods on template class
2118 void barD() EXCLUSIVE_LOCKS_REQUIRED(fooBaseT.mu_) {
2119 fooBaseT.a = 0;
2120 }
2121
2122 // Test dependent expressions in template method in template class
2123 template <class T2>
2124 void barTD(T2 *fooT) EXCLUSIVE_LOCKS_REQUIRED(fooBaseT.mu_, fooT->mu_) {
2125 fooBaseT.a = 0;
2126 fooT->a = 0;
2127 }
2128 };
2129
2130 template <class T>
2131 class Cell {
2132 public:
2133 Mutex mu_;
2134 // Test dependent guarded_by
2135 T data GUARDED_BY(mu_);
2136
2137 void fooEx() EXCLUSIVE_LOCKS_REQUIRED(mu_) {
2138 data = 0;
2139 }
2140
2141 void foo() {
2142 mu_.Lock();
2143 data = 0;
2144 mu_.Unlock();
2145 }
2146 };
2147
2148 void test() {
2149 Bar b;
2150 BarT<Foo2> bt;
2151 Foo1 f1;
2152 Foo2 f2;
2153
2154 f1.mu_.Lock();
2155 f2.mu_.Lock();
2156 bt.fooBase.mu_.Lock();
2157 bt.fooBaseT.mu_.Lock();
2158
2159 b.barND(&f1, &f2);
2160 b.barD(&f1, &f2);
2161 bt.barND();
2162 bt.barD();
2163 bt.barTD(&f2);
2164
2165 f1.mu_.Unlock();
2166 bt.barTD(&f1); // \
2167 // expected-warning {{calling function 'barTD<TestTemplateAttributeInstantiation::Foo1>' requires holding mutex 'f1.mu_' exclusively}} \
2168 // expected-note {{found near match 'bt.fooBase.mu_'}}
2169
2170 bt.fooBase.mu_.Unlock();
2171 bt.fooBaseT.mu_.Unlock();
2172 f2.mu_.Unlock();
2173
2174 Cell<int> cell;
2175 cell.data = 0; // \
2176 // expected-warning {{writing variable 'data' requires holding mutex 'cell.mu_' exclusively}}
2177 cell.foo();
2178 cell.mu_.Lock();
2179 cell.fooEx();
2180 cell.mu_.Unlock();
2181 }
2182
2183
2184 template <class T>
2185 class CellDelayed {
2186 public:
2187 // Test dependent guarded_by
2188 T data GUARDED_BY(mu_);
2189 static T static_data GUARDED_BY(static_mu_);
2190
2191 void fooEx(CellDelayed<T> *other) EXCLUSIVE_LOCKS_REQUIRED(mu_, other->mu_) {
2192 this->data = other->data;
2193 }
2194
2195 template <class T2>
2196 void fooExT(CellDelayed<T2> *otherT) EXCLUSIVE_LOCKS_REQUIRED(mu_, otherT->mu_) {
2197 this->data = otherT->data;
2198 }
2199
2200 void foo() {
2201 mu_.Lock();
2202 data = 0;
2203 mu_.Unlock();
2204 }
2205
2206 Mutex mu_;
2207 static Mutex static_mu_;
2208 };
2209
2210 void testDelayed() {
2211 CellDelayed<int> celld;
2212 CellDelayed<int> celld2;
2213 celld.foo();
2214 celld.mu_.Lock();
2215 celld2.mu_.Lock();
2216
2217 celld.fooEx(&celld2);
2218 celld.fooExT(&celld2);
2219
2220 celld2.mu_.Unlock();
2221 celld.mu_.Unlock();
2222 }
2223
2224 }; // end namespace TestTemplateAttributeInstantiation
2225
2226
2227 namespace FunctionDeclDefTest {
2228
2229 class Foo {
2230 public:
2231 Mutex mu_;
2232 int a GUARDED_BY(mu_);
2233
2234 virtual void foo1(Foo *f_declared) EXCLUSIVE_LOCKS_REQUIRED(f_declared->mu_);
2235 };
2236
2237 // EXCLUSIVE_LOCKS_REQUIRED should be applied, and rewritten to f_defined->mu_
2238 void Foo::foo1(Foo *f_defined) {
2239 f_defined->a = 0;
2240 };
2241
2242 void test() {
2243 Foo myfoo;
2244 myfoo.foo1(&myfoo); // \
2245 // expected-warning {{calling function 'foo1' requires holding mutex 'myfoo.mu_' exclusively}}
2246 myfoo.mu_.Lock();
2247 myfoo.foo1(&myfoo);
2248 myfoo.mu_.Unlock();
2249 }
2250
2251 };
2252
2253 namespace GoingNative {
2254
2255 struct LOCKABLE mutex {
2256 void lock() EXCLUSIVE_LOCK_FUNCTION();
2257 void unlock() UNLOCK_FUNCTION();
2258 // ...
2259 };
2260 bool foo();
2261 bool bar();
2262 mutex m;
2263 void test() {
2264 m.lock();
2265 while (foo()) { // expected-warning {{expecting mutex 'm' to be held at start of each loop}}
2266 m.unlock();
2267 // ...
2268 if (bar()) {
2269 // ...
2270 if (foo())
2271 continue;
2272 //...
2273 }
2274 // ...
2275 m.lock(); // expected-note {{mutex acquired here}}
2276 }
2277 m.unlock();
2278 }
2279
2280 }
2281
2282
2283
2284 namespace FunctionDefinitionTest {
2285
2286 class Foo {
2287 public:
2288 void foo1();
2289 void foo2();
2290 void foo3(Foo *other);
2291
2292 template<class T>
2293 void fooT1(const T& dummy1);
2294
2295 template<class T>
2296 void fooT2(const T& dummy2) EXCLUSIVE_LOCKS_REQUIRED(mu_);
2297
2298 Mutex mu_;
2299 int a GUARDED_BY(mu_);
2300 };
2301
2302 template<class T>
2303 class FooT {
2304 public:
2305 void foo();
2306
2307 Mutex mu_;
2308 T a GUARDED_BY(mu_);
2309 };
2310
2311
2312 void Foo::foo1() NO_THREAD_SAFETY_ANALYSIS {
2313 a = 1;
2314 }
2315
2316 void Foo::foo2() EXCLUSIVE_LOCKS_REQUIRED(mu_) {
2317 a = 2;
2318 }
2319
2320 void Foo::foo3(Foo *other) EXCLUSIVE_LOCKS_REQUIRED(other->mu_) {
2321 other->a = 3;
2322 }
2323
2324 template<class T>
2325 void Foo::fooT1(const T& dummy1) EXCLUSIVE_LOCKS_REQUIRED(mu_) {
2326 a = dummy1;
2327 }
2328
2329 /* TODO -- uncomment with template instantiation of attributes.
2330 template<class T>
2331 void Foo::fooT2(const T& dummy2) {
2332 a = dummy2;
2333 }
2334 */
2335
2336 void fooF1(Foo *f) EXCLUSIVE_LOCKS_REQUIRED(f->mu_) {
2337 f->a = 1;
2338 }
2339
2340 void fooF2(Foo *f);
2341 void fooF2(Foo *f) EXCLUSIVE_LOCKS_REQUIRED(f->mu_) {
2342 f->a = 2;
2343 }
2344
2345 void fooF3(Foo *f) EXCLUSIVE_LOCKS_REQUIRED(f->mu_);
2346 void fooF3(Foo *f) {
2347 f->a = 3;
2348 }
2349
2350 template<class T>
2351 void FooT<T>::foo() EXCLUSIVE_LOCKS_REQUIRED(mu_) {
2352 a = 0;
2353 }
2354
2355 void test() {
2356 int dummy = 0;
2357 Foo myFoo;
2358
2359 myFoo.foo2(); // \
2360 // expected-warning {{calling function 'foo2' requires holding mutex 'myFoo.mu_' exclusively}}
2361 myFoo.foo3(&myFoo); // \
2362 // expected-warning {{calling function 'foo3' requires holding mutex 'myFoo.mu_' exclusively}}
2363 myFoo.fooT1(dummy); // \
2364 // expected-warning {{calling function 'fooT1<int>' requires holding mutex 'myFoo.mu_' exclusively}}
2365
2366 myFoo.fooT2(dummy); // \
2367 // expected-warning {{calling function 'fooT2<int>' requires holding mutex 'myFoo.mu_' exclusively}}
2368
2369 fooF1(&myFoo); // \
2370 // expected-warning {{calling function 'fooF1' requires holding mutex 'myFoo.mu_' exclusively}}
2371 fooF2(&myFoo); // \
2372 // expected-warning {{calling function 'fooF2' requires holding mutex 'myFoo.mu_' exclusively}}
2373 fooF3(&myFoo); // \
2374 // expected-warning {{calling function 'fooF3' requires holding mutex 'myFoo.mu_' exclusively}}
2375
2376 myFoo.mu_.Lock();
2377 myFoo.foo2();
2378 myFoo.foo3(&myFoo);
2379 myFoo.fooT1(dummy);
2380
2381 myFoo.fooT2(dummy);
2382
2383 fooF1(&myFoo);
2384 fooF2(&myFoo);
2385 fooF3(&myFoo);
2386 myFoo.mu_.Unlock();
2387
2388 FooT<int> myFooT;
2389 myFooT.foo(); // \
2390 // expected-warning {{calling function 'foo' requires holding mutex 'myFooT.mu_' exclusively}}
2391 }
2392
2393 } // end namespace FunctionDefinitionTest
2394
2395
2396 namespace SelfLockingTest {
2397
2398 class LOCKABLE MyLock {
2399 public:
2400 int foo GUARDED_BY(this);
2401
2402 void lock() EXCLUSIVE_LOCK_FUNCTION();
2403 void unlock() UNLOCK_FUNCTION();
2404
2405 void doSomething() {
2406 this->lock(); // allow 'this' as a lock expression
2407 foo = 0;
2408 doSomethingElse();
2409 this->unlock();
2410 }
2411
2412 void doSomethingElse() EXCLUSIVE_LOCKS_REQUIRED(this) {
2413 foo = 1;
2414 };
2415
2416 void test() {
2417 foo = 2; // \
2418 // expected-warning {{writing variable 'foo' requires holding mutex 'this' exclusively}}
2419 }
2420 };
2421
2422
2423 class LOCKABLE MyLock2 {
2424 public:
2425 Mutex mu_;
2426 int foo GUARDED_BY(this);
2427
2428 // don't check inside lock and unlock functions
2429 void lock() EXCLUSIVE_LOCK_FUNCTION() { mu_.Lock(); }
2430 void unlock() UNLOCK_FUNCTION() { mu_.Unlock(); }
2431
2432 // don't check inside constructors and destructors
2433 MyLock2() { foo = 1; }
2434 ~MyLock2() { foo = 0; }
2435 };
2436
2437
2438 } // end namespace SelfLockingTest
2439
2440
2441 namespace InvalidNonstatic {
2442
2443 // Forward decl here causes bogus "invalid use of non-static data member"
2444 // on reference to mutex_ in guarded_by attribute.
2445 class Foo;
2446
2447 class Foo {
2448 Mutex* mutex_;
2449
2450 int foo __attribute__((guarded_by(mutex_)));
2451 };
2452
2453 } // end namespace InvalidNonStatic
2454
2455
2456 namespace NoReturnTest {
2457
2458 bool condition();
2459 void fatal() __attribute__((noreturn));
2460
2461 Mutex mu_;
2462
2463 void test1() {
2464 MutexLock lock(&mu_);
2465 if (condition()) {
2466 fatal();
2467 return;
2468 }
2469 }
2470
2471 } // end namespace NoReturnTest
2472
2473
2474 namespace TestMultiDecl {
2475
2476 class Foo {
2477 public:
2478 int GUARDED_BY(mu_) a;
2479 int GUARDED_BY(mu_) b, c;
2480
2481 void foo() {
2482 a = 0; // \
2483 // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}}
2484 b = 0; // \
2485 // expected-warning {{writing variable 'b' requires holding mutex 'mu_' exclusively}}
2486 c = 0; // \
2487 // expected-warning {{writing variable 'c' requires holding mutex 'mu_' exclusively}}
2488 }
2489
2490 private:
2491 Mutex mu_;
2492 };
2493
2494 } // end namespace TestMultiDecl
2495
2496
2497 namespace WarnNoDecl {
2498
2499 class Foo {
2500 void foo(int a); __attribute__(( // \
2501 // expected-warning {{declaration does not declare anything}}
2502 exclusive_locks_required(a))); // \
2503 // expected-warning {{attribute exclusive_locks_required ignored}}
2504
2505 };
2506
2507 } // end namespace WarnNoDecl
2508
2509
2510
2511 namespace MoreLockExpressions {
2512
2513 class Foo {
2514 public:
2515 Mutex mu_;
2516 int a GUARDED_BY(mu_);
2517 };
2518
2519 class Bar {
2520 public:
2521 int b;
2522 Foo* f;
2523
2524 Foo& getFoo() { return *f; }
2525 Foo& getFoo2(int c) { return *f; }
2526 Foo& getFoo3(int c, int d) { return *f; }
2527
2528 Foo& getFooey() { return *f; }
2529 };
2530
2531 Foo& getBarFoo(Bar &bar, int c) { return bar.getFoo2(c); }
2532
2533 void test() {
2534 Foo foo;
2535 Foo *fooArray;
2536 Foo &(*fooFuncPtr)();
2537 Bar bar;
2538 int a;
2539 int b;
2540 int c;
2541
2542 bar.getFoo().mu_.Lock();
2543 bar.getFoo().a = 0;
2544 bar.getFoo().mu_.Unlock();
2545
2546 (bar.getFoo().mu_).Lock(); // test parenthesis
2547 bar.getFoo().a = 0;
2548 (bar.getFoo().mu_).Unlock();
2549
2550 bar.getFoo2(a).mu_.Lock();
2551 bar.getFoo2(a).a = 0;
2552 bar.getFoo2(a).mu_.Unlock();
2553
2554 bar.getFoo3(a, b).mu_.Lock();
2555 bar.getFoo3(a, b).a = 0;
2556 bar.getFoo3(a, b).mu_.Unlock();
2557
2558 getBarFoo(bar, a).mu_.Lock();
2559 getBarFoo(bar, a).a = 0;
2560 getBarFoo(bar, a).mu_.Unlock();
2561
2562 bar.getFoo2(10).mu_.Lock();
2563 bar.getFoo2(10).a = 0;
2564 bar.getFoo2(10).mu_.Unlock();
2565
2566 bar.getFoo2(a + 1).mu_.Lock();
2567 bar.getFoo2(a + 1).a = 0;
2568 bar.getFoo2(a + 1).mu_.Unlock();
2569
2570 (a > 0 ? fooArray[1] : fooArray[b]).mu_.Lock();
2571 (a > 0 ? fooArray[1] : fooArray[b]).a = 0;
2572 (a > 0 ? fooArray[1] : fooArray[b]).mu_.Unlock();
2573
2574 fooFuncPtr().mu_.Lock();
2575 fooFuncPtr().a = 0;
2576 fooFuncPtr().mu_.Unlock();
2577 }
2578
2579
2580 void test2() {
2581 Foo *fooArray;
2582 Bar bar;
2583 int a;
2584 int b;
2585 int c;
2586
2587 bar.getFoo().mu_.Lock();
2588 bar.getFooey().a = 0; // \
2589 // expected-warning {{writing variable 'a' requires holding mutex 'bar.getFooey().mu_' exclusively}} \
2590 // expected-note {{found near match 'bar.getFoo().mu_'}}
2591 bar.getFoo().mu_.Unlock();
2592
2593 bar.getFoo2(a).mu_.Lock();
2594 bar.getFoo2(b).a = 0; // \
2595 // expected-warning {{writing variable 'a' requires holding mutex 'bar.getFoo2(b).mu_' exclusively}} \
2596 // expected-note {{found near match 'bar.getFoo2(a).mu_'}}
2597 bar.getFoo2(a).mu_.Unlock();
2598
2599 bar.getFoo3(a, b).mu_.Lock();
2600 bar.getFoo3(a, c).a = 0; // \
2601 // expected-warning {{writing variable 'a' requires holding mutex 'bar.getFoo3(a, c).mu_' exclusively}} \
2602 // expected-note {{found near match 'bar.getFoo3(a, b).mu_'}}
2603 bar.getFoo3(a, b).mu_.Unlock();
2604
2605 getBarFoo(bar, a).mu_.Lock();
2606 getBarFoo(bar, b).a = 0; // \
2607 // expected-warning {{writing variable 'a' requires holding mutex 'getBarFoo(bar, b).mu_' exclusively}} \
2608 // expected-note {{found near match 'getBarFoo(bar, a).mu_'}}
2609 getBarFoo(bar, a).mu_.Unlock();
2610
2611 (a > 0 ? fooArray[1] : fooArray[b]).mu_.Lock();
2612 (a > 0 ? fooArray[b] : fooArray[c]).a = 0; // \
2613 // expected-warning {{writing variable 'a' requires holding mutex '((0 < a) ? fooArray[b] : fooArray[c]).mu_' exclusively}} \
2614 // expected-note {{found near match '((0 < a) ? fooArray[1] : fooArray[b]).mu_'}}
2615 (a > 0 ? fooArray[1] : fooArray[b]).mu_.Unlock();
2616 }
2617
2618
2619 } // end namespace MoreLockExpressions
2620
2621
2622 namespace TrylockJoinPoint {
2623
2624 class Foo {
2625 Mutex mu;
2626 bool c;
2627
2628 void foo() {
2629 if (c) {
2630 if (!mu.TryLock())
2631 return;
2632 } else {
2633 mu.Lock();
2634 }
2635 mu.Unlock();
2636 }
2637 };
2638
2639 } // end namespace TrylockJoinPoint
2640
2641
2642 namespace LockReturned {
2643
2644 class Foo {
2645 public:
2646 int a GUARDED_BY(mu_);
2647 void foo() EXCLUSIVE_LOCKS_REQUIRED(mu_);
2648 void foo2(Foo* f) EXCLUSIVE_LOCKS_REQUIRED(mu_, f->mu_);
2649
2650 static void sfoo(Foo* f) EXCLUSIVE_LOCKS_REQUIRED(f->mu_);
2651
2652 Mutex* getMu() LOCK_RETURNED(mu_);
2653
2654 Mutex mu_;
2655
2656 static Mutex* getMu(Foo* f) LOCK_RETURNED(f->mu_);
2657 };
2658
2659
2660 // Calls getMu() directly to lock and unlock
2661 void test1(Foo* f1, Foo* f2) {
2662 f1->a = 0; // expected-warning {{writing variable 'a' requires holding mutex 'f1->mu_' exclusively}}
2663 f1->foo(); // expected-warning {{calling function 'foo' requires holding mutex 'f1->mu_' exclusively}}
2664
2665 f1->foo2(f2); // expected-warning {{calling function 'foo2' requires holding mutex 'f1->mu_' exclusively}} \
2666 // expected-warning {{calling function 'foo2' requires holding mutex 'f2->mu_' exclusively}}
2667 Foo::sfoo(f1); // expected-warning {{calling function 'sfoo' requires holding mutex 'f1->mu_' exclusively}}
2668
2669 f1->getMu()->Lock();
2670
2671 f1->a = 0;
2672 f1->foo();
2673 f1->foo2(f2); // \
2674 // expected-warning {{calling function 'foo2' requires holding mutex 'f2->mu_' exclusively}} \
2675 // expected-note {{found near match 'f1->mu_'}}
2676
2677 Foo::getMu(f2)->Lock();
2678 f1->foo2(f2);
2679 Foo::getMu(f2)->Unlock();
2680
2681 Foo::sfoo(f1);
2682
2683 f1->getMu()->Unlock();
2684 }
2685
2686
2687 Mutex* getFooMu(Foo* f) LOCK_RETURNED(Foo::getMu(f));
2688
2689 class Bar : public Foo {
2690 public:
2691 int b GUARDED_BY(getMu());
2692 void bar() EXCLUSIVE_LOCKS_REQUIRED(getMu());
2693 void bar2(Bar* g) EXCLUSIVE_LOCKS_REQUIRED(getMu(this), g->getMu());
2694
2695 static void sbar(Bar* g) EXCLUSIVE_LOCKS_REQUIRED(g->getMu());
2696 static void sbar2(Bar* g) EXCLUSIVE_LOCKS_REQUIRED(getFooMu(g));
2697 };
2698
2699
2700
2701 // Use getMu() within other attributes.
2702 // This requires at lest levels of substitution, more in the case of
2703 void test2(Bar* b1, Bar* b2) {
2704 b1->b = 0; // expected-warning {{writing variable 'b' requires holding mutex 'b1->mu_' exclusively}}
2705 b1->bar(); // expected-warning {{calling function 'bar' requires holding mutex 'b1->mu_' exclusively}}
2706 b1->bar2(b2); // expected-warning {{calling function 'bar2' requires holding mutex 'b1->mu_' exclusively}} \
2707 // expected-warning {{calling function 'bar2' requires holding mutex 'b2->mu_' exclusively}}
2708 Bar::sbar(b1); // expected-warning {{calling function 'sbar' requires holding mutex 'b1->mu_' exclusively}}
2709 Bar::sbar2(b1); // expected-warning {{calling function 'sbar2' requires holding mutex 'b1->mu_' exclusively}}
2710
2711 b1->getMu()->Lock();
2712
2713 b1->b = 0;
2714 b1->bar();
2715 b1->bar2(b2); // \
2716 // expected-warning {{calling function 'bar2' requires holding mutex 'b2->mu_' exclusively}} \
2717 // // expected-note {{found near match 'b1->mu_'}}
2718
2719 b2->getMu()->Lock();
2720 b1->bar2(b2);
2721
2722 b2->getMu()->Unlock();
2723
2724 Bar::sbar(b1);
2725 Bar::sbar2(b1);
2726
2727 b1->getMu()->Unlock();
2728 }
2729
2730
2731 // Lock the mutex directly, but use attributes that call getMu()
2732 // Also lock the mutex using getFooMu, which calls a lock_returned function.
2733 void test3(Bar* b1, Bar* b2) {
2734 b1->mu_.Lock();
2735 b1->b = 0;
2736 b1->bar();
2737
2738 getFooMu(b2)->Lock();
2739 b1->bar2(b2);
2740 getFooMu(b2)->Unlock();
2741
2742 Bar::sbar(b1);
2743 Bar::sbar2(b1);
2744
2745 b1->mu_.Unlock();
2746 }
2747
2748 } // end namespace LockReturned
2749
2750
2751 namespace ReleasableScopedLock {
2752
2753 class Foo {
2754 Mutex mu_;
2755 bool c;
2756 int a GUARDED_BY(mu_);
2757
2758 void test1();
2759 void test2();
2760 void test3();
2761 void test4();
2762 void test5();
2763 void test6();
2764 };
2765
2766
2767 void Foo::test1() {
2768 ReleasableMutexLock rlock(&mu_);
2769 rlock.Release();
2770 }
2771
2772 void Foo::test2() {
2773 ReleasableMutexLock rlock(&mu_);
2774 if (c) { // test join point -- held/not held during release
2775 rlock.Release();
2776 }
2777 // No warning on join point because the lock will be released by the scope object anyway.
2778 }
2779
2780 void Foo::test3() {
2781 ReleasableMutexLock rlock(&mu_);
2782 a = 0;
2783 rlock.Release();
2784 a = 1; // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}}
2785 }
2786
2787 void Foo::test4() {
2788 ReleasableMutexLock rlock(&mu_);
2789 rlock.Release(); // expected-note{{mutex released here}}
2790 rlock.Release(); // expected-warning {{releasing mutex 'mu_' that was not held}}
2791 }
2792
2793 void Foo::test5() {
2794 ReleasableMutexLock rlock(&mu_);
2795 if (c) {
2796 rlock.Release();
2797 }
2798 // No warning on join point because the lock will be released by the scope object anyway.
2799 rlock.Release(); // expected-warning {{releasing mutex 'mu_' that was not held}}
2800 }
2801
2802 void Foo::test6() {
2803 ReleasableMutexLock rlock(&mu_);
2804 do {
2805 if (c) {
2806 rlock.Release();
2807 break;
2808 }
2809 } while (c);
2810 // No warning on join point because the lock will be released by the scope object anyway
2811 a = 1; // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}}
2812 }
2813
2814
2815 } // end namespace ReleasableScopedLock
2816
2817
2818 namespace RelockableScopedLock {
2819
2820 class DeferTraits {};
2821
2822 class SCOPED_LOCKABLE RelockableExclusiveMutexLock {
2823 public:
2824 RelockableExclusiveMutexLock(Mutex *mu) EXCLUSIVE_LOCK_FUNCTION(mu);
2825 RelockableExclusiveMutexLock(Mutex *mu, DeferTraits) LOCKS_EXCLUDED(mu);
2826 ~RelockableExclusiveMutexLock() EXCLUSIVE_UNLOCK_FUNCTION();
2827
2828 void Lock() EXCLUSIVE_LOCK_FUNCTION();
2829 void Unlock() UNLOCK_FUNCTION();
2830 };
2831
2832 struct SharedTraits {};
2833 struct ExclusiveTraits {};
2834
2835 class SCOPED_LOCKABLE RelockableMutexLock {
2836 public:
2837 RelockableMutexLock(Mutex *mu, DeferTraits) LOCKS_EXCLUDED(mu);
2838 RelockableMutexLock(Mutex *mu, SharedTraits) SHARED_LOCK_FUNCTION(mu);
2839 RelockableMutexLock(Mutex *mu, ExclusiveTraits) EXCLUSIVE_LOCK_FUNCTION(mu);
2840 ~RelockableMutexLock() UNLOCK_FUNCTION();
2841
2842 void Lock() EXCLUSIVE_LOCK_FUNCTION();
2843 void Unlock() UNLOCK_FUNCTION();
2844
2845 void ReaderLock() SHARED_LOCK_FUNCTION();
2846 void ReaderUnlock() UNLOCK_FUNCTION();
2847
2848 void PromoteShared() UNLOCK_FUNCTION() EXCLUSIVE_LOCK_FUNCTION();
2849 void DemoteExclusive() UNLOCK_FUNCTION() SHARED_LOCK_FUNCTION();
2850 };
2851
2852 Mutex mu;
2853 int x GUARDED_BY(mu);
2854 bool b;
2855
2856 void print(int);
2857
2858 void relock() {
2859 RelockableExclusiveMutexLock scope(&mu);
2860 x = 2;
2861 scope.Unlock();
2862
2863 x = 3; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}}
2864
2865 scope.Lock();
2866 x = 4;
2867 }
2868
2869 void deferLock() {
2870 RelockableExclusiveMutexLock scope(&mu, DeferTraits{});
2871 x = 2; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}}
2872 scope.Lock();
2873 x = 3;
2874 }
2875
2876 void relockExclusive() {
2877 RelockableMutexLock scope(&mu, SharedTraits{});
2878 print(x);
2879 x = 2; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}}
2880 scope.ReaderUnlock();
2881
2882 print(x); // expected-warning {{reading variable 'x' requires holding mutex 'mu'}}
2883
2884 scope.Lock();
2885 print(x);
2886 x = 4;
2887
2888 scope.DemoteExclusive();
2889 print(x);
2890 x = 5; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}}
2891 }
2892
2893 void relockShared() {
2894 RelockableMutexLock scope(&mu, ExclusiveTraits{});
2895 print(x);
2896 x = 2;
2897 scope.Unlock();
2898
2899 print(x); // expected-warning {{reading variable 'x' requires holding mutex 'mu'}}
2900
2901 scope.ReaderLock();
2902 print(x);
2903 x = 4; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}}
2904
2905 scope.PromoteShared();
2906 print(x);
2907 x = 5;
2908 }
2909
2910 void deferLockShared() {
2911 RelockableMutexLock scope(&mu, DeferTraits{});
2912 print(x); // expected-warning {{reading variable 'x' requires holding mutex 'mu'}}
2913 scope.ReaderLock();
2914 print(x);
2915 x = 2; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}}
2916 }
2917
2918 void doubleUnlock() {
2919 RelockableExclusiveMutexLock scope(&mu);
2920 scope.Unlock(); // expected-note{{mutex released here}}
2921 scope.Unlock(); // expected-warning {{releasing mutex 'mu' that was not held}}
2922 }
2923
2924 void doubleLock1() {
2925 RelockableExclusiveMutexLock scope(&mu); // expected-note{{mutex acquired here}}
2926 scope.Lock(); // expected-warning {{acquiring mutex 'mu' that is already held}}
2927 }
2928
2929 void doubleLock2() {
2930 RelockableExclusiveMutexLock scope(&mu);
2931 scope.Unlock();
2932 scope.Lock(); // expected-note{{mutex acquired here}}
2933 scope.Lock(); // expected-warning {{acquiring mutex 'mu' that is already held}}
2934 }
2935
2936 void lockJoin() {
2937 RelockableMutexLock scope(&mu, DeferTraits{});
2938 if (b)
2939 scope.Lock();
2940 // No warning on join point because the lock will be released by the scope object anyway.
2941 x = 2; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}}
2942 }
2943
2944 void unlockJoin() {
2945 RelockableMutexLock scope(&mu, DeferTraits{});
2946 scope.Lock();
2947 if (b)
2948 scope.Unlock();
2949 // No warning on join point because the lock will be released by the scope object anyway.
2950 x = 2; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}}
2951 }
2952
2953 void loopAcquire() {
2954 RelockableMutexLock scope(&mu, DeferTraits{});
2955 for (unsigned i = 1; i < 10; ++i)
2956 scope.Lock(); // We could catch this double lock with negative capabilities.
2957 }
2958
2959 void loopRelease() {
2960 RelockableMutexLock scope(&mu, ExclusiveTraits{}); // expected-note {{mutex acquired here}}
2961 // We have to warn on this join point despite the lock being managed ...
2962 for (unsigned i = 1; i < 10; ++i) { // expected-warning {{expecting mutex 'mu' to be held at start of each loop}}
2963 x = 1; // ... because we might miss that this doesn't always happen under lock.
2964 if (i == 5)
2965 scope.Unlock();
2966 }
2967 }
2968
2969 void loopPromote() {
2970 RelockableMutexLock scope(&mu, SharedTraits{});
2971 for (unsigned i = 1; i < 10; ++i) {
2972 x = 1; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}}
2973 if (i == 5)
2974 scope.PromoteShared();
2975 }
2976 }
2977
2978 void loopDemote() {
2979 RelockableMutexLock scope(&mu, ExclusiveTraits{}); // expected-note {{the other acquisition of mutex 'mu' is here}}
2980 // We have to warn on this join point despite the lock being managed ...
2981 for (unsigned i = 1; i < 10; ++i) {
2982 x = 1; // ... because we might miss that this doesn't always happen under exclusive lock.
2983 if (i == 5)
2984 scope.DemoteExclusive(); // expected-warning {{mutex 'mu' is acquired exclusively and shared in the same scope}}
2985 }
2986 }
2987
2988 void loopAcquireContinue() {
2989 RelockableMutexLock scope(&mu, DeferTraits{});
2990 for (unsigned i = 1; i < 10; ++i) {
2991 x = 1; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}}
2992 if (i == 5) {
2993 scope.Lock();
2994 continue;
2995 }
2996 }
2997 }
2998
2999 void loopReleaseContinue() {
3000 RelockableMutexLock scope(&mu, ExclusiveTraits{}); // expected-note {{mutex acquired here}}
3001 // We have to warn on this join point despite the lock being managed ...
3002 for (unsigned i = 1; i < 10; ++i) { // expected-warning {{expecting mutex 'mu' to be held at start of each loop}}
3003 x = 1; // ... because we might miss that this doesn't always happen under lock.
3004 if (i == 5) {
3005 scope.Unlock();
3006 continue;
3007 }
3008 }
3009 }
3010
3011 void loopPromoteContinue() {
3012 RelockableMutexLock scope(&mu, SharedTraits{});
3013 for (unsigned i = 1; i < 10; ++i) {
3014 x = 1; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}}
3015 if (i == 5) {
3016 scope.PromoteShared();
3017 continue;
3018 }
3019 }
3020 }
3021
3022 void loopDemoteContinue() {
3023 RelockableMutexLock scope(&mu, ExclusiveTraits{}); // expected-note {{the other acquisition of mutex 'mu' is here}}
3024 // We have to warn on this join point despite the lock being managed ...
3025 for (unsigned i = 1; i < 10; ++i) {
3026 x = 1; // ... because we might miss that this doesn't always happen under exclusive lock.
3027 if (i == 5) {
3028 scope.DemoteExclusive(); // expected-warning {{mutex 'mu' is acquired exclusively and shared in the same scope}}
3029 continue;
3030 }
3031 }
3032 }
3033
3034 void exclusiveSharedJoin() {
3035 RelockableMutexLock scope(&mu, DeferTraits{});
3036 if (b)
3037 scope.Lock();
3038 else
3039 scope.ReaderLock();
3040 // No warning on join point because the lock will be released by the scope object anyway.
3041 print(x);
3042 x = 2; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}}
3043 }
3044
3045 void sharedExclusiveJoin() {
3046 RelockableMutexLock scope(&mu, DeferTraits{});
3047 if (b)
3048 scope.ReaderLock();
3049 else
3050 scope.Lock();
3051 // No warning on join point because the lock will be released by the scope object anyway.
3052 print(x);
3053 x = 2; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}}
3054 }
3055
3056 void assertJoin() {
3057 RelockableMutexLock scope(&mu, DeferTraits{});
3058 if (b)
3059 scope.Lock();
3060 else
3061 mu.AssertHeld();
3062 x = 2;
3063 }
3064
3065 void assertSharedJoin() {
3066 RelockableMutexLock scope(&mu, DeferTraits{});
3067 if (b)
3068 scope.ReaderLock();
3069 else
3070 mu.AssertReaderHeld();
3071 print(x);
3072 x = 2; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}}
3073 }
3074
3075 void assertStrongerJoin() {
3076 RelockableMutexLock scope(&mu, DeferTraits{});
3077 if (b)
3078 scope.ReaderLock();
3079 else
3080 mu.AssertHeld();
3081 print(x);
3082 x = 2; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}}
3083 }
3084
3085 void assertWeakerJoin() {
3086 RelockableMutexLock scope(&mu, DeferTraits{});
3087 if (b)
3088 scope.Lock();
3089 else
3090 mu.AssertReaderHeld();
3091 print(x);
3092 x = 2; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}}
3093 }
3094
3095 void directUnlock() {
3096 RelockableExclusiveMutexLock scope(&mu);
3097 mu.Unlock();
3098 // Debatable that there is no warning. Currently we don't track in the scoped
3099 // object whether it is active, but just check if the contained locks can be
3100 // reacquired. Here they can, because mu has been unlocked manually.
3101 scope.Lock();
3102 }
3103
3104 void directRelock() {
3105 RelockableExclusiveMutexLock scope(&mu);
3106 scope.Unlock();
3107 mu.Lock();
3108 // Similarly debatable that there is no warning.
3109 scope.Unlock();
3110 }
3111
3112 // Doesn't make a lot of sense, just making sure there is no crash.
3113 void destructLock() {
3114 RelockableExclusiveMutexLock scope(&mu);
3115 scope.~RelockableExclusiveMutexLock();
3116 scope.Lock(); // Should be UB, so we don't really care.
3117 }
3118
3119 class SCOPED_LOCKABLE MemberLock {
3120 public:
3121 MemberLock() EXCLUSIVE_LOCK_FUNCTION(mutex);
3122 ~MemberLock() UNLOCK_FUNCTION(mutex);
3123 void Lock() EXCLUSIVE_LOCK_FUNCTION(mutex);
3124 Mutex mutex;
3125 };
3126
3127 void relockShared2() {
3128 MemberLock lock; // expected-note{{mutex acquired here}}
3129 lock.Lock(); // expected-warning {{acquiring mutex 'lock.mutex' that is already held}}
3130 }
3131
3132 class SCOPED_LOCKABLE WeirdScope {
3133 private:
3134 Mutex *other;
3135
3136 public:
3137 WeirdScope(Mutex *mutex) EXCLUSIVE_LOCK_FUNCTION(mutex);
3138 void unlock() EXCLUSIVE_UNLOCK_FUNCTION() EXCLUSIVE_UNLOCK_FUNCTION(other);
3139 void lock() EXCLUSIVE_LOCK_FUNCTION() EXCLUSIVE_LOCK_FUNCTION(other);
3140 ~WeirdScope() EXCLUSIVE_UNLOCK_FUNCTION();
3141
3142 void requireOther() EXCLUSIVE_LOCKS_REQUIRED(other);
3143 };
3144
3145 void relockWeird() {
3146 WeirdScope scope(&mu);
3147 x = 1;
3148 scope.unlock(); // expected-warning {{releasing mutex 'scope.other' that was not held}}
3149 x = 2; // \
3150 // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}}
3151 scope.requireOther(); // \
3152 // expected-warning {{calling function 'requireOther' requires holding mutex 'scope.other' exclusively}}
3153 scope.lock(); // expected-note {{mutex acquired here}}
3154 x = 3;
3155 scope.requireOther();
3156 } // expected-warning {{mutex 'scope.other' is still held at the end of function}}
3157
3158 } // end namespace RelockableScopedLock
3159
3160
3161 namespace ScopedUnlock {
3162
3163 class SCOPED_LOCKABLE MutexUnlock {
3164 public:
3165 MutexUnlock(Mutex *mu) EXCLUSIVE_UNLOCK_FUNCTION(mu);
3166 ~MutexUnlock() EXCLUSIVE_UNLOCK_FUNCTION();
3167
3168 void Lock() EXCLUSIVE_UNLOCK_FUNCTION();
3169 void Unlock() EXCLUSIVE_LOCK_FUNCTION();
3170 };
3171
3172 class SCOPED_LOCKABLE ReaderMutexUnlock {
3173 public:
3174 ReaderMutexUnlock(Mutex *mu) SHARED_UNLOCK_FUNCTION(mu);
3175 ~ReaderMutexUnlock() EXCLUSIVE_UNLOCK_FUNCTION();
3176
3177 void Lock() EXCLUSIVE_UNLOCK_FUNCTION();
3178 void Unlock() EXCLUSIVE_LOCK_FUNCTION();
3179 };
3180
3181 template<typename... Mus>
3182 class SCOPED_LOCKABLE VariadicMutexUnlock {
3183 public:
3184 VariadicMutexUnlock(Mus *...mus) EXCLUSIVE_UNLOCK_FUNCTION(mus...);
3185 ~VariadicMutexUnlock() EXCLUSIVE_UNLOCK_FUNCTION();
3186
3187 void Lock() EXCLUSIVE_UNLOCK_FUNCTION();
3188 void Unlock() EXCLUSIVE_LOCK_FUNCTION();
3189 };
3190
3191 Mutex mu;
3192 int x GUARDED_BY(mu);
3193 bool c;
3194 void print(int);
3195
3196 void simple() EXCLUSIVE_LOCKS_REQUIRED(mu) {
3197 x = 1;
3198 MutexUnlock scope(&mu);
3199 x = 2; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}}
3200 }
3201
3202 void simpleShared() SHARED_LOCKS_REQUIRED(mu) {
3203 print(x);
3204 ReaderMutexUnlock scope(&mu);
3205 print(x); // expected-warning {{reading variable 'x' requires holding mutex 'mu'}}
3206 }
3207
3208 void innerUnlock() {
3209 MutexLock outer(&mu);
3210 if (x == 0) {
3211 MutexUnlock inner(&mu);
3212 x = 1; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}}
3213 }
3214 x = 2;
3215 }
3216
3217 void innerUnlockShared() {
3218 ReaderMutexLock outer(&mu);
3219 if (x == 0) {
3220 ReaderMutexUnlock inner(&mu);
3221 print(x); // expected-warning {{reading variable 'x' requires holding mutex 'mu'}}
3222 }
3223 print(x);
3224 }
3225
3226 void manual() EXCLUSIVE_LOCKS_REQUIRED(mu) {
3227 MutexUnlock scope(&mu);
3228 scope.Lock();
3229 x = 2;
3230 scope.Unlock();
3231 x = 3; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}}
3232 }
3233
3234 void join() EXCLUSIVE_LOCKS_REQUIRED(mu) {
3235 MutexUnlock scope(&mu);
3236 if (c)
3237 scope.Lock();
3238 // No warning on join point because the lock will be released by the scope object anyway.
3239 scope.Lock();
3240 }
3241
3242 void doubleLock() EXCLUSIVE_LOCKS_REQUIRED(mu) {
3243 MutexUnlock scope(&mu);
3244 scope.Lock(); // expected-note{{mutex acquired here}}
3245 scope.Lock(); // expected-warning {{acquiring mutex 'mu' that is already held}}
3246 }
3247
3248 void doubleUnlock() EXCLUSIVE_LOCKS_REQUIRED(mu) {
3249 MutexUnlock scope(&mu); // expected-note{{mutex released here}}
3250 scope.Unlock(); // expected-warning {{releasing mutex 'mu' that was not held}}
3251 }
3252
3253 Mutex mu2;
3254 int y GUARDED_BY(mu2);
3255
3256 void variadic() EXCLUSIVE_LOCKS_REQUIRED(mu, mu2) {
3257 VariadicMutexUnlock<Mutex, Mutex> scope(&mu, &mu2);
3258 x = 2; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}}
3259 y = 3; // expected-warning {{writing variable 'y' requires holding mutex 'mu2' exclusively}}
3260 scope.Lock();
3261 x = y = 4;
3262 }
3263
3264 class SCOPED_LOCKABLE MutexLockUnlock {
3265 public:
3266 MutexLockUnlock(Mutex *mu1, Mutex *mu2) EXCLUSIVE_UNLOCK_FUNCTION(mu1) EXCLUSIVE_LOCK_FUNCTION(mu2);
3267 ~MutexLockUnlock() EXCLUSIVE_UNLOCK_FUNCTION();
3268
3269 void Release() EXCLUSIVE_UNLOCK_FUNCTION();
3270 void Acquire() EXCLUSIVE_LOCK_FUNCTION();
3271 };
3272
3273 Mutex other;
3274 void fn() EXCLUSIVE_LOCKS_REQUIRED(other);
3275
3276 void lockUnlock() EXCLUSIVE_LOCKS_REQUIRED(mu) {
3277 MutexLockUnlock scope(&mu, &other);
3278 fn();
3279 x = 1; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}}
3280 }
3281
3282 } // end namespace ScopedUnlock
3283
3284 namespace PassingScope {
3285
3286 class SCOPED_LOCKABLE RelockableScope {
3287 public:
3288 RelockableScope(Mutex *mu) EXCLUSIVE_LOCK_FUNCTION(mu);
3289 void Release() UNLOCK_FUNCTION();
3290 void Acquire() EXCLUSIVE_LOCK_FUNCTION();
3291 ~RelockableScope() EXCLUSIVE_UNLOCK_FUNCTION();
3292 };
3293
3294 class SCOPED_LOCKABLE ReaderRelockableScope {
3295 public:
3296 ReaderRelockableScope(Mutex *mu) SHARED_LOCK_FUNCTION(mu);
3297 void Release() UNLOCK_FUNCTION();
3298 void Acquire() SHARED_LOCK_FUNCTION();
3299 ~ReaderRelockableScope() UNLOCK_FUNCTION();
3300 };
3301
3302 Mutex mu;
3303 Mutex mu1;
3304 Mutex mu2;
3305 int x GUARDED_BY(mu);
3306 int y GUARDED_BY(mu) GUARDED_BY(mu2);
3307 void print(int);
3308
3309 // Analysis inside the function with annotated parameters
3310
3311 void sharedRequired(ReleasableMutexLock& scope SHARED_LOCKS_REQUIRED(mu)) {
3312 print(x); // OK.
3313 }
3314
3315 void sharedAcquire(ReaderRelockableScope& scope SHARED_LOCK_FUNCTION(mu)) {
3316 scope.Acquire();
3317 print(x);
3318 }
3319
3320 void sharedRelease(RelockableScope& scope SHARED_UNLOCK_FUNCTION(mu)) {
3321 print(x);
3322 scope.Release();
3323 print(x); // expected-warning{{reading variable 'x' requires holding mutex 'mu'}}
3324 }
3325
3326 void requiredLock(ReleasableMutexLock& scope EXCLUSIVE_LOCKS_REQUIRED(mu)) {
3327 x = 1; // OK.
3328 }
3329
3330 void reacquireRequiredLock(RelockableScope& scope EXCLUSIVE_LOCKS_REQUIRED(mu)) {
3331 scope.Release();
3332 scope.Acquire();
3333 x = 2; // OK.
3334 }
3335
3336 void releaseSingleMutex(ReleasableMutexLock& scope EXCLUSIVE_UNLOCK_FUNCTION(mu)) {
3337 x = 1; // OK.
3338 scope.Release();
3339 x = 2; // expected-warning{{writing variable 'x' requires holding mutex 'mu' exclusively}}
3340 }
3341
3342 void releaseMultipleMutexes(ReleasableMutexLock& scope EXCLUSIVE_UNLOCK_FUNCTION(mu, mu2)) {
3343 y = 1; // OK.
3344 scope.Release();
3345 y = 2; // expected-warning{{writing variable 'y' requires holding mutex 'mu' exclusively}}
3346 // expected-warning@-1{{writing variable 'y' requires holding mutex 'mu2' exclusively}}
3347 }
3348
3349 void acquireLock(RelockableScope& scope EXCLUSIVE_LOCK_FUNCTION(mu)) {
3350 x = 1; // expected-warning{{writing variable 'x' requires holding mutex 'mu' exclusively}}
3351 scope.Acquire();
3352 x = 2; // OK.
3353 }
3354
3355 void acquireMultipleLocks(RelockableScope& scope EXCLUSIVE_LOCK_FUNCTION(mu, mu2)) {
3356 y = 1; // expected-warning{{writing variable 'y' requires holding mutex 'mu' exclusively}}
3357 // expected-warning@-1{{writing variable 'y' requires holding mutex 'mu2' exclusively}}
3358 scope.Acquire();
3359 y = 2; // OK.
3360 }
3361
3362 void excludedLock(ReleasableMutexLock& scope LOCKS_EXCLUDED(mu)) {
3363 x = 1; // expected-warning{{writing variable 'x' requires holding mutex 'mu' exclusively}}
3364 }
3365
3366 void acquireAndReleaseExcludedLocks(RelockableScope& scope LOCKS_EXCLUDED(mu)) {
3367 scope.Acquire();
3368 scope.Release();
3369 }
3370
3371 // expected-note@+1{{mutex acquired here}}
3372 void unreleasedMutex(ReleasableMutexLock& scope EXCLUSIVE_UNLOCK_FUNCTION(mu)) {
3373 x = 1; // OK.
3374 } // expected-warning{{mutex 'mu' is still held at the end of function}}
3375
3376 // expected-note@+1{{mutex acquired here}}
3377 void acquireAlreadyHeldMutex(RelockableScope& scope EXCLUSIVE_UNLOCK_FUNCTION(mu)) {
3378 scope.Acquire(); // expected-warning{{acquiring mutex 'mu' that is already held}}
3379 scope.Release();
3380 }
3381
3382 void reacquireMutex(RelockableScope& scope EXCLUSIVE_UNLOCK_FUNCTION(mu)) {
3383 scope.Release();
3384 scope.Acquire(); // expected-note{{mutex acquired here}}
3385 x = 2; // OK.
3386 } // expected-warning{{mutex 'mu' is still held at the end of function}}
3387
3388 // expected-note@+1{{mutex acquired here}}
3389 void requireSingleMutex(ReleasableMutexLock& scope EXCLUSIVE_LOCKS_REQUIRED(mu)) {
3390 x = 1; // OK.
3391 scope.Release();
3392 x = 2; // expected-warning{{writing variable 'x' requires holding mutex 'mu' exclusively}}
3393 } // expected-warning{{expecting mutex 'mu' to be held at the end of function}}
3394
3395 // expected-note@+1 2{{mutex acquired here}}
3396 void requireMultipleMutexes(ReleasableMutexLock& scope EXCLUSIVE_LOCKS_REQUIRED(mu, mu2)) {
3397 y = 1; // OK.
3398 scope.Release();
3399 y = 2; // expected-warning{{writing variable 'y' requires holding mutex 'mu' exclusively}}
3400 // expected-warning@-1{{writing variable 'y' requires holding mutex 'mu2' exclusively}}
3401 } // expected-warning{{expecting mutex 'mu' to be held at the end of function}}
3402 // expected-warning@-1{{expecting mutex 'mu2' to be held at the end of function}}
3403
3404 // expected-note@+1{{mutex acquired here}}
3405 void acquireAlreadyHeldLock(RelockableScope& scope EXCLUSIVE_LOCKS_REQUIRED(mu)) {
3406 scope.Acquire(); // expected-warning{{acquiring mutex 'mu' that is already held}}
3407 }
3408
3409 // expected-note@+1 {{mutex acquired here}}
3410 void releaseWithoutHoldingLock(ReleasableMutexLock& scope EXCLUSIVE_LOCK_FUNCTION(mu)) {
3411 scope.Release(); // expected-warning{{releasing mutex 'mu' that was not held}}
3412 } // expected-warning{{expecting mutex 'mu' to be held at the end of function}}
3413
3414 // expected-note@+1 {{mutex acquired here}}
3415 void endWithReleasedMutex(RelockableScope& scope EXCLUSIVE_LOCK_FUNCTION(mu)) {
3416 scope.Acquire();
3417 scope.Release();
3418 } // expected-warning{{expecting mutex 'mu' to be held at the end of function}}
3419
3420 void acquireExcludedLock(RelockableScope& scope LOCKS_EXCLUDED(mu)) {
3421 x = 1; // expected-warning{{writing variable 'x' requires holding mutex 'mu' exclusively}}
3422 scope.Acquire(); // expected-note {{mutex acquired here}}
3423 x = 2; // OK.
3424 } // expected-warning{{mutex 'mu' is still held at the end of function}}
3425
3426 void acquireMultipleExcludedLocks(RelockableScope& scope LOCKS_EXCLUDED(mu, mu2)) {
3427 y = 1; // expected-warning{{writing variable 'y' requires holding mutex 'mu' exclusively}}
3428 // expected-warning@-1{{writing variable 'y' requires holding mutex 'mu2' exclusively}}
3429 scope.Acquire(); // expected-note 2{{mutex acquired here}}
3430 y = 2; // OK.
3431 } // expected-warning{{mutex 'mu' is still held at the end of function}}
3432 // expected-warning@-1{{mutex 'mu2' is still held at the end of function}}
3433
3434 void reacquireExcludedLocks(RelockableScope& scope LOCKS_EXCLUDED(mu)) {
3435 scope.Release(); // expected-warning{{releasing mutex 'mu' that was not held}}
3436 scope.Acquire(); // expected-note {{mutex acquired here}}
3437 x = 2; // OK.
3438 } // expected-warning{{mutex 'mu' is still held at the end of function}}
3439
3440 // expected-note@+1{{mutex acquired here}}
3441 void sharedRequired2(ReleasableMutexLock& scope SHARED_LOCKS_REQUIRED(mu)) {
3442 print(x); // OK.
3443 scope.Release();
3444 print(x); // expected-warning{{reading variable 'x' requires holding mutex 'mu'}}
3445 } // expected-warning{{expecting mutex 'mu' to be held at the end of function}}
3446
3447 // expected-note@+1{{mutex acquired here}}
3448 void sharedAcquire2(RelockableScope& scope SHARED_LOCK_FUNCTION(mu)) {
3449 print(x); // expected-warning{{reading variable 'x' requires holding mutex 'mu'}}
3450 scope.Release(); // expected-warning{{releasing mutex 'mu' that was not held}}
3451 } // expected-warning{{expecting mutex 'mu' to be held at the end of function}}
3452
3453 // expected-note@+1 2{{mutex acquired here}}
3454 void sharedRelease2(RelockableScope& scope SHARED_UNLOCK_FUNCTION(mu)) {
3455 scope.Acquire(); //expected-warning{{acquiring mutex 'mu' that is already held}}
3456 } //expected-warning{{mutex 'mu' is still held at the end of function}}
3457
3458 // Handling the call of the function with annotated parameters
3459
3460 // expected-note@+1{{see attribute on parameter here}}
3461 void release(ReleasableMutexLock& scope EXCLUSIVE_UNLOCK_FUNCTION(mu));
3462 // expected-note@+1{{see attribute on parameter here}}
3463 void release_DoubleMutexLock(DoubleMutexLock& scope EXCLUSIVE_UNLOCK_FUNCTION(mu));
3464 // expected-note@+1 2{{see attribute on parameter here}}
3465 void release_two(ReleasableMutexLock& scope EXCLUSIVE_UNLOCK_FUNCTION(mu, mu2));
3466 // expected-note@+1{{see attribute on parameter here}}
3467 void release_double(DoubleMutexLock& scope EXCLUSIVE_UNLOCK_FUNCTION(mu, mu2));
3468 void require(ReleasableMutexLock& scope EXCLUSIVE_LOCKS_REQUIRED(mu));
3469 void acquire(RelockableScope& scope EXCLUSIVE_LOCK_FUNCTION(mu));
3470 void exclude(RelockableScope& scope LOCKS_EXCLUDED(mu));
3471
3472 void release_shared(ReaderRelockableScope& scope SHARED_UNLOCK_FUNCTION(mu));
3473 void require_shared(ReleasableMutexLock& scope SHARED_LOCKS_REQUIRED(mu));
3474 void acquire_shared(ReaderRelockableScope& scope SHARED_LOCK_FUNCTION(mu));
3475
3476 void unlockCall() {
3477 ReleasableMutexLock scope(&mu);
3478 x = 1; // OK.
3479 release(scope);
3480 x = 2; // expected-warning{{writing variable 'x' requires holding mutex 'mu' exclusively}}
3481 }
3482
3483 void unlockSharedCall() {
3484 ReaderRelockableScope scope(&mu);
3485 print(x); // OK.
3486 release_shared(scope);
3487 print(x); // expected-warning{{reading variable 'x' requires holding mutex 'mu'}}
3488 }
3489
3490 void requireCall() {
3491 ReleasableMutexLock scope(&mu);
3492 x = 1; // OK.
3493 require(scope);
3494 x = 2; // Ok.
3495 }
3496
3497 void requireSharedCall() {
3498 ReleasableMutexLock scope(&mu);
3499 print(x); // OK.
3500 require_shared(scope);
3501 print(x); // Ok.
3502 }
3503
3504 void acquireCall() {
3505 RelockableScope scope(&mu);
3506 scope.Release();
3507 acquire(scope);
3508 x = 2; // Ok.
3509 }
3510
3511 void acquireSharedCall() {
3512 ReaderRelockableScope scope(&mu);
3513 scope.Release();
3514 acquire_shared(scope);
3515 print(x); // Ok.
3516 }
3517
3518 void writeAfterExcludeCall() {
3519 RelockableScope scope(&mu);
3520 scope.Release();
3521 exclude(scope);
3522 x = 2; // expected-warning{{writing variable 'x' requires holding mutex 'mu' exclusively}}
3523 }
3524
3525 void unlockCallAfterExplicitRelease() {
3526 ReleasableMutexLock scope(&mu);
3527 x = 1; // OK.
3528 scope.Release(); // expected-note{{mutex released here}}
3529 release(scope); // expected-warning{{releasing mutex 'mu' that was not held}}
3530 x = 2; // expected-warning{{writing variable 'x' requires holding mutex 'mu' exclusively}}
3531 }
3532
3533 void unmatchedMutexes() {
3534 ReleasableMutexLock scope(&mu2);
3535 release(scope); // expected-warning{{mutex managed by 'scope' is 'mu2' instead of 'mu'}}
3536 // expected-warning@-1{{releasing mutex 'mu' that was not held}}
3537 }
3538
3539 void wrongOrder() {
3540 DoubleMutexLock scope(&mu2, &mu);
3541 release_double(scope); // expected-warning{{mutex managed by 'scope' is 'mu2' instead of 'mu'}}
3542 }
3543
3544 void differentNumberOfMutexes() {
3545 ReleasableMutexLock scope(&mu);
3546 release_two(scope); // expected-warning{{mutex 'mu2' not managed by 'scope'}}
3547 // expected-warning@-1{{releasing mutex 'mu2' that was not held}}
3548 }
3549
3550 void differentNumberOfMutexes2() {
3551 ReleasableMutexLock scope(&mu2);
3552 release_two(scope); // expected-warning{{mutex managed by 'scope' is 'mu2' instead of 'mu'}}
3553 // expected-warning@-1{{releasing mutex 'mu' that was not held}}
3554 }
3555
3556 void differentNumberOfMutexes3() {
3557 DoubleMutexLock scope(&mu, &mu2);
3558 release_DoubleMutexLock(scope); // expected-warning{{did not expect mutex 'mu2' to be managed by 'scope'}}
3559 }
3560
3561 void releaseDefault(ReleasableMutexLock& scope EXCLUSIVE_UNLOCK_FUNCTION(mu), int = 0);
3562
3563 void unlockFunctionDefault() {
3564 ReleasableMutexLock scope(&mu);
3565 x = 1; // OK.
3566 releaseDefault(scope);
3567 x = 2; // expected-warning{{writing variable 'x' requires holding mutex 'mu' exclusively}}
3568 }
3569
3570 void requireCallWithReleasedLock() {
3571 ReleasableMutexLock scope(&mu);
3572 scope.Release();
3573 require(scope); // expected-warning{{calling function 'require' requires holding mutex 'mu' exclusively}}
3574 }
3575
3576 void acquireCallWithAlreadyHeldLock() {
3577 RelockableScope scope(&mu); // expected-note{{mutex acquired here}}
3578 acquire(scope); // expected-warning{{acquiring mutex 'mu' that is already held}}
3579 x = 1;
3580 }
3581
3582 void excludeCallWithAlreadyHeldLock() {
3583 RelockableScope scope(&mu);
3584 exclude(scope); // expected-warning{{cannot call function 'exclude' while mutex 'mu' is held}}
3585 x = 2; // OK.
3586 }
3587
3588 void requireConst(const ReleasableMutexLock& scope EXCLUSIVE_LOCKS_REQUIRED(mu));
3589 void requireConstCall() {
3590 requireConst(ReleasableMutexLock(&mu));
3591 }
3592
3593 void passScopeUndeclared(ReleasableMutexLock &scope) {
3594 release(scope); // expected-warning{{calling function 'release' requires holding mutex 'scope' exclusively}}
3595 // expected-warning@-1{{releasing mutex 'mu' that was not held}}
3596 }
3597
3598 class SCOPED_LOCKABLE ScopedWithoutLock {
3599 public:
3600 ScopedWithoutLock();
3601
3602 ~ScopedWithoutLock() EXCLUSIVE_UNLOCK_FUNCTION();
3603 };
3604
3605 void require(ScopedWithoutLock &scope EXCLUSIVE_LOCKS_REQUIRED(mu));
3606
3607 void constructWithoutLock() {
3608 ScopedWithoutLock scope;
3609 require(scope); // expected-warning{{calling function 'require' requires holding mutex 'mu' exclusively}}
3610 // expected-warning@-1{{calling function 'require' requires holding mutex 'scope' exclusively}}
3611 } // expected-warning {{releasing mutex 'scope' that was not held}}
3612
3613 void requireConst(const ScopedWithoutLock& scope EXCLUSIVE_LOCKS_REQUIRED(mu));
3614
3615 void requireCallWithReleasedLock2() {
3616 requireConst(ScopedWithoutLock());
3617 // expected-warning@-1{{calling function 'requireConst' requires holding mutex '#undefined' exclusively}}
3618 // expected-warning@-2{{calling function 'requireConst' requires holding mutex 'mu' exclusively}}
3619 }
3620
3621 void requireDecl(RelockableScope &scope EXCLUSIVE_LOCKS_REQUIRED(mu));
3622 void requireDecl(RelockableScope &scope) {
3623 scope.Release();
3624 scope.Acquire();
3625 }
3626
3627 struct foo
3628 {
3629 Mutex mu;
3630 // expected-note@+1{{see attribute on parameter here}}
3631 void require(RelockableScope &scope EXCLUSIVE_LOCKS_REQUIRED(mu));
3632 void callRequire(){
3633 RelockableScope scope(&mu);
3634 // TODO: False positive due to incorrect parsing of parameter attribute arguments.
3635 require(scope);
3636 // expected-warning@-1{{calling function 'require' requires holding mutex 'mu' exclusively}}
3637 // expected-warning@-2{{mutex managed by 'scope' is 'mu' instead of 'mu'}}
3638 }
3639 };
3640
3641 struct ObjectWithMutex {
3642 Mutex mu;
3643 int x GUARDED_BY(mu);
3644 };
3645 void releaseMember(ObjectWithMutex& object, ReleasableMutexLock& scope EXCLUSIVE_UNLOCK_FUNCTION(object.mu)) {
3646 object.x = 1;
3647 scope.Release();
3648 }
3649 void releaseMemberCall() {
3650 ObjectWithMutex obj;
3651 ReleasableMutexLock lock(&obj.mu);
3652 releaseMember(obj, lock);
3653 }
3654
3655 } // end namespace PassingScope
3656
3657 namespace TrylockFunctionTest {
3658
3659 class Foo {
3660 public:
3661 Mutex mu1_;
3662 Mutex mu2_;
3663 bool c;
3664
3665 bool lockBoth() EXCLUSIVE_TRYLOCK_FUNCTION(true, mu1_, mu2_);
3666 };
3667
3668 bool Foo::lockBoth() {
3669 if (!mu1_.TryLock())
3670 return false;
3671
3672 mu2_.Lock();
3673 if (!c) {
3674 mu1_.Unlock();
3675 mu2_.Unlock();
3676 return false;
3677 }
3678
3679 return true;
3680 }
3681
3682
3683 } // end namespace TrylockFunctionTest
3684
3685
3686
3687 namespace DoubleLockBug {
3688
3689 class Foo {
3690 public:
3691 Mutex mu_;
3692 int a GUARDED_BY(mu_);
3693
3694 void foo1() EXCLUSIVE_LOCKS_REQUIRED(mu_);
3695 int foo2() SHARED_LOCKS_REQUIRED(mu_);
3696 };
3697
3698
3699 void Foo::foo1() EXCLUSIVE_LOCKS_REQUIRED(mu_) {
3700 a = 0;
3701 }
3702
3703 int Foo::foo2() SHARED_LOCKS_REQUIRED(mu_) {
3704 return a;
3705 }
3706
3707 }
3708
3709
3710
3711 namespace UnlockBug {
3712
3713 class Foo {
3714 public:
3715 Mutex mutex_;
3716
3717 void foo1() EXCLUSIVE_LOCKS_REQUIRED(mutex_) { // expected-note {{mutex acquired here}}
3718 mutex_.Unlock();
3719 } // expected-warning {{expecting mutex 'mutex_' to be held at the end of function}}
3720
3721
3722 void foo2() SHARED_LOCKS_REQUIRED(mutex_) { // expected-note {{mutex acquired here}}
3723 mutex_.Unlock();
3724 } // expected-warning {{expecting mutex 'mutex_' to be held at the end of function}}
3725 };
3726
3727 } // end namespace UnlockBug
3728
3729
3730
3731 namespace FoolishScopedLockableBug {
3732
3733 class SCOPED_LOCKABLE WTF_ScopedLockable {
3734 public:
3735 WTF_ScopedLockable(Mutex* mu) EXCLUSIVE_LOCK_FUNCTION(mu);
3736
3737 // have to call release() manually;
3738 ~WTF_ScopedLockable();
3739
3740 void release() UNLOCK_FUNCTION();
3741 };
3742
3743
3744 class Foo {
3745 Mutex mu_;
3746 int a GUARDED_BY(mu_);
3747 bool c;
3748
3749 void doSomething();
3750
3751 void test1() {
3752 WTF_ScopedLockable wtf(&mu_);
3753 wtf.release();
3754 }
3755
3756 void test2() {
3757 WTF_ScopedLockable wtf(&mu_); // expected-note {{mutex acquired here}}
3758 } // expected-warning {{mutex 'mu_' is still held at the end of function}}
3759
3760 void test3() {
3761 if (c) {
3762 WTF_ScopedLockable wtf(&mu_);
3763 wtf.release();
3764 }
3765 }
3766
3767 void test4() {
3768 if (c) {
3769 doSomething();
3770 }
3771 else {
3772 WTF_ScopedLockable wtf(&mu_);
3773 wtf.release();
3774 }
3775 }
3776
3777 void test5() {
3778 if (c) {
3779 WTF_ScopedLockable wtf(&mu_); // expected-note {{mutex acquired here}}
3780 }
3781 } // expected-warning {{mutex 'mu_' is not held on every path through here}}
3782
3783 void test6() {
3784 if (c) {
3785 doSomething();
3786 }
3787 else {
3788 WTF_ScopedLockable wtf(&mu_); // expected-note {{mutex acquired here}}
3789 }
3790 } // expected-warning {{mutex 'mu_' is not held on every path through here}}
3791 };
3792
3793
3794 } // end namespace FoolishScopedLockableBug
3795
3796
3797
3798 namespace TemporaryCleanupExpr {
3799
3800 class Foo {
3801 int a GUARDED_BY(getMutexPtr().get());
3802
3803 SmartPtr<Mutex> getMutexPtr();
3804
3805 void test();
3806 };
3807
3808
3809 void Foo::test() {
3810 {
3811 ReaderMutexLock lock(getMutexPtr().get());
3812 int b = a;
3813 }
3814 int b = a; // expected-warning {{reading variable 'a' requires holding mutex 'getMutexPtr()'}}
3815 }
3816
3817 #ifdef __cpp_guaranteed_copy_elision
3818
3819 void guaranteed_copy_elision() {
3820 MutexLock lock = MutexLock{&sls_mu};
3821 sls_guard_var = 0;
3822 }
3823
3824 void guaranteed_copy_elision_const() {
3825 const MutexLock lock = MutexLock{&sls_mu};
3826 sls_guard_var = 0;
3827 }
3828
3829 #endif
3830
3831 } // end namespace TemporaryCleanupExpr
3832
3833
3834
3835 namespace SmartPointerTests {
3836
3837 class Foo {
3838 public:
3839 SmartPtr<Mutex> mu_;
3840 int a GUARDED_BY(mu_);
3841 int b GUARDED_BY(mu_.get());
3842 int c GUARDED_BY(*mu_);
3843
3844 void Lock() EXCLUSIVE_LOCK_FUNCTION(mu_);
3845 void Unlock() UNLOCK_FUNCTION(mu_);
3846
3847 void test0();
3848 void test1();
3849 void test2();
3850 void test3();
3851 void test4();
3852 void test5();
3853 void test6();
3854 void test7();
3855 void test8();
3856 };
3857
3858 void Foo::test0() {
3859 a = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}}
3860 b = 0; // expected-warning {{writing variable 'b' requires holding mutex 'mu_' exclusively}}
3861 c = 0; // expected-warning {{writing variable 'c' requires holding mutex 'mu_' exclusively}}
3862 }
3863
3864 void Foo::test1() {
3865 mu_->Lock();
3866 a = 0;
3867 b = 0;
3868 c = 0;
3869 mu_->Unlock();
3870 }
3871
3872 void Foo::test2() {
3873 (*mu_).Lock();
3874 a = 0;
3875 b = 0;
3876 c = 0;
3877 (*mu_).Unlock();
3878 }
3879
3880
3881 void Foo::test3() {
3882 mu_.get()->Lock();
3883 a = 0;
3884 b = 0;
3885 c = 0;
3886 mu_.get()->Unlock();
3887 }
3888
3889
3890 void Foo::test4() {
3891 MutexLock lock(mu_.get());
3892 a = 0;
3893 b = 0;
3894 c = 0;
3895 }
3896
3897
3898 void Foo::test5() {
3899 MutexLock lock(&(*mu_));
3900 a = 0;
3901 b = 0;
3902 c = 0;
3903 }
3904
3905
3906 void Foo::test6() {
3907 Lock();
3908 a = 0;
3909 b = 0;
3910 c = 0;
3911 Unlock();
3912 }
3913
3914
3915 void Foo::test7() {
3916 {
3917 Lock();
3918 mu_->Unlock();
3919 }
3920 {
3921 mu_->Lock();
3922 Unlock();
3923 }
3924 {
3925 mu_.get()->Lock();
3926 mu_->Unlock();
3927 }
3928 {
3929 mu_->Lock();
3930 mu_.get()->Unlock();
3931 }
3932 {
3933 mu_.get()->Lock();
3934 (*mu_).Unlock();
3935 }
3936 {
3937 (*mu_).Lock();
3938 mu_->Unlock();
3939 }
3940 }
3941
3942
3943 void Foo::test8() {
3944 mu_->Lock(); // expected-note 2 {{mutex acquired here}}
3945 mu_.get()->Lock(); // expected-warning {{acquiring mutex 'mu_' that is already held}}
3946 (*mu_).Lock(); // expected-warning {{acquiring mutex 'mu_' that is already held}}
3947 mu_.get()->Unlock(); // expected-note {{mutex released here}}
3948 Unlock(); // expected-warning {{releasing mutex 'mu_' that was not held}}
3949 }
3950
3951
3952 class Bar {
3953 SmartPtr<Foo> foo;
3954
3955 void test0();
3956 void test1();
3957 void test2();
3958 void test3();
3959 };
3960
3961
3962 void Bar::test0() {
3963 foo->a = 0; // expected-warning {{writing variable 'a' requires holding mutex 'foo->mu_' exclusively}}
3964 (*foo).b = 0; // expected-warning {{writing variable 'b' requires holding mutex 'foo->mu_' exclusively}}
3965 foo.get()->c = 0; // expected-warning {{writing variable 'c' requires holding mutex 'foo->mu_' exclusively}}
3966 }
3967
3968
3969 void Bar::test1() {
3970 foo->mu_->Lock();
3971 foo->a = 0;
3972 (*foo).b = 0;
3973 foo.get()->c = 0;
3974 foo->mu_->Unlock();
3975 }
3976
3977
3978 void Bar::test2() {
3979 (*foo).mu_->Lock();
3980 foo->a = 0;
3981 (*foo).b = 0;
3982 foo.get()->c = 0;
3983 foo.get()->mu_->Unlock();
3984 }
3985
3986
3987 void Bar::test3() {
3988 MutexLock lock(foo->mu_.get());
3989 foo->a = 0;
3990 (*foo).b = 0;
3991 foo.get()->c = 0;
3992 }
3993
3994 } // end namespace SmartPointerTests
3995
3996
3997
3998 namespace DuplicateAttributeTest {
3999
4000 class LOCKABLE Foo {
4001 public:
4002 Mutex mu1_;
4003 Mutex mu2_;
4004 Mutex mu3_;
4005 int a GUARDED_BY(mu1_);
4006 int b GUARDED_BY(mu2_);
4007 int c GUARDED_BY(mu3_);
4008
4009 void lock() EXCLUSIVE_LOCK_FUNCTION();
4010 void unlock() UNLOCK_FUNCTION();
4011
4012 void lock1() EXCLUSIVE_LOCK_FUNCTION(mu1_);
4013 void slock1() SHARED_LOCK_FUNCTION(mu1_);
4014 void lock3() EXCLUSIVE_LOCK_FUNCTION(mu1_, mu2_, mu3_);
4015 void locklots()
4016 EXCLUSIVE_LOCK_FUNCTION(mu1_)
4017 EXCLUSIVE_LOCK_FUNCTION(mu2_)
4018 EXCLUSIVE_LOCK_FUNCTION(mu1_, mu2_, mu3_);
4019
4020 void unlock1() UNLOCK_FUNCTION(mu1_);
4021 void unlock3() UNLOCK_FUNCTION(mu1_, mu2_, mu3_);
4022 void unlocklots()
4023 UNLOCK_FUNCTION(mu1_)
4024 UNLOCK_FUNCTION(mu2_)
4025 UNLOCK_FUNCTION(mu1_, mu2_, mu3_);
4026 };
4027
4028
4029 void Foo::lock() EXCLUSIVE_LOCK_FUNCTION() { }
4030 void Foo::unlock() UNLOCK_FUNCTION() { }
4031
4032 void Foo::lock1() EXCLUSIVE_LOCK_FUNCTION(mu1_) {
4033 mu1_.Lock();
4034 }
4035
4036 void Foo::slock1() SHARED_LOCK_FUNCTION(mu1_) {
4037 mu1_.ReaderLock();
4038 }
4039
4040 void Foo::lock3() EXCLUSIVE_LOCK_FUNCTION(mu1_, mu2_, mu3_) {
4041 mu1_.Lock();
4042 mu2_.Lock();
4043 mu3_.Lock();
4044 }
4045
4046 void Foo::locklots()
4047 EXCLUSIVE_LOCK_FUNCTION(mu1_, mu2_)
4048 EXCLUSIVE_LOCK_FUNCTION(mu2_, mu3_) {
4049 mu1_.Lock();
4050 mu2_.Lock();
4051 mu3_.Lock();
4052 }
4053
4054 void Foo::unlock1() UNLOCK_FUNCTION(mu1_) {
4055 mu1_.Unlock();
4056 }
4057
4058 void Foo::unlock3() UNLOCK_FUNCTION(mu1_, mu2_, mu3_) {
4059 mu1_.Unlock();
4060 mu2_.Unlock();
4061 mu3_.Unlock();
4062 }
4063
4064 void Foo::unlocklots()
4065 UNLOCK_FUNCTION(mu1_, mu2_)
4066 UNLOCK_FUNCTION(mu2_, mu3_) {
4067 mu1_.Unlock();
4068 mu2_.Unlock();
4069 mu3_.Unlock();
4070 }
4071
4072
4073 void test0() {
4074 Foo foo;
4075 foo.lock();
4076 foo.unlock();
4077
4078 foo.lock(); // expected-note{{mutex acquired here}}
4079 foo.lock(); // expected-warning {{acquiring mutex 'foo' that is already held}}
4080 foo.unlock(); // expected-note{{mutex released here}}
4081 foo.unlock(); // expected-warning {{releasing mutex 'foo' that was not held}}
4082 }
4083
4084
4085 void test1() {
4086 Foo foo;
4087 foo.lock1();
4088 foo.a = 0;
4089 foo.unlock1();
4090
4091 foo.lock1(); // expected-note{{mutex acquired here}}
4092 foo.lock1(); // expected-warning {{acquiring mutex 'foo.mu1_' that is already held}}
4093 foo.a = 0;
4094 foo.unlock1(); // expected-note{{mutex released here}}
4095 foo.unlock1(); // expected-warning {{releasing mutex 'foo.mu1_' that was not held}}
4096 }
4097
4098
4099 int test2() {
4100 Foo foo;
4101 foo.slock1();
4102 int d1 = foo.a;
4103 foo.unlock1();
4104
4105 foo.slock1(); // expected-note{{mutex acquired here}}
4106 foo.slock1(); // expected-warning {{acquiring mutex 'foo.mu1_' that is already held}}
4107 int d2 = foo.a;
4108 foo.unlock1(); // expected-note{{mutex released here}}
4109 foo.unlock1(); // expected-warning {{releasing mutex 'foo.mu1_' that was not held}}
4110 return d1 + d2;
4111 }
4112
4113
4114 void test3() {
4115 Foo foo;
4116 foo.lock3();
4117 foo.a = 0;
4118 foo.b = 0;
4119 foo.c = 0;
4120 foo.unlock3();
4121
4122 foo.lock3(); // expected-note 3 {{mutex acquired here}}
4123 foo.lock3(); // \
4124 // expected-warning {{acquiring mutex 'foo.mu1_' that is already held}} \
4125 // expected-warning {{acquiring mutex 'foo.mu2_' that is already held}} \
4126 // expected-warning {{acquiring mutex 'foo.mu3_' that is already held}}
4127 foo.a = 0;
4128 foo.b = 0;
4129 foo.c = 0;
4130 foo.unlock3(); // expected-note 3 {{mutex released here}}
4131 foo.unlock3(); // \
4132 // expected-warning {{releasing mutex 'foo.mu1_' that was not held}} \
4133 // expected-warning {{releasing mutex 'foo.mu2_' that was not held}} \
4134 // expected-warning {{releasing mutex 'foo.mu3_' that was not held}}
4135 }
4136
4137
4138 void testlots() {
4139 Foo foo;
4140 foo.locklots();
4141 foo.a = 0;
4142 foo.b = 0;
4143 foo.c = 0;
4144 foo.unlocklots();
4145
4146 foo.locklots(); // expected-note 3 {{mutex acquired here}}
4147 foo.locklots(); // \
4148 // expected-warning {{acquiring mutex 'foo.mu1_' that is already held}} \
4149 // expected-warning {{acquiring mutex 'foo.mu2_' that is already held}} \
4150 // expected-warning {{acquiring mutex 'foo.mu3_' that is already held}}
4151 foo.a = 0;
4152 foo.b = 0;
4153 foo.c = 0;
4154 foo.unlocklots(); // expected-note 3 {{mutex released here}}
4155 foo.unlocklots(); // \
4156 // expected-warning {{releasing mutex 'foo.mu1_' that was not held}} \
4157 // expected-warning {{releasing mutex 'foo.mu2_' that was not held}} \
4158 // expected-warning {{releasing mutex 'foo.mu3_' that was not held}}
4159 }
4160
4161 } // end namespace DuplicateAttributeTest
4162
4163
4164
4165 namespace TryLockEqTest {
4166
4167 class Foo {
4168 Mutex mu_;
4169 int a GUARDED_BY(mu_);
4170 bool c;
4171
4172 int tryLockMutexI() EXCLUSIVE_TRYLOCK_FUNCTION(1, mu_);
4173 Mutex* tryLockMutexP() EXCLUSIVE_TRYLOCK_FUNCTION(1, mu_);
4174 void unlock() UNLOCK_FUNCTION(mu_);
4175
4176 void test1();
4177 void test2();
4178 };
4179
4180
4181 void Foo::test1() {
4182 if (tryLockMutexP() == 0) {
4183 a = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}}
4184 return;
4185 }
4186 a = 0;
4187 unlock();
4188
4189 if (tryLockMutexP() != 0) {
4190 a = 0;
4191 unlock();
4192 }
4193
4194 if (0 != tryLockMutexP()) {
4195 a = 0;
4196 unlock();
4197 }
4198
4199 if (!(tryLockMutexP() == 0)) {
4200 a = 0;
4201 unlock();
4202 }
4203
4204 if (tryLockMutexI() == 0) {
4205 a = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}}
4206 return;
4207 }
4208 a = 0;
4209 unlock();
4210
4211 if (0 == tryLockMutexI()) {
4212 a = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}}
4213 return;
4214 }
4215 a = 0;
4216 unlock();
4217
4218 if (tryLockMutexI() == 1) {
4219 a = 0;
4220 unlock();
4221 }
4222
4223 if (mu_.TryLock() == false) {
4224 a = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}}
4225 return;
4226 }
4227 a = 0;
4228 unlock();
4229
4230 if (mu_.TryLock() == true) {
4231 a = 0;
4232 unlock();
4233 }
4234 else {
4235 a = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}}
4236 }
4237
4238 #if __has_feature(cxx_nullptr)
4239 if (tryLockMutexP() == nullptr) {
4240 a = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}}
4241 return;
4242 }
4243 a = 0;
4244 unlock();
4245 #endif
4246 }
4247
4248 } // end namespace TryLockEqTest
4249
4250
4251 namespace ExistentialPatternMatching {
4252
4253 class Graph {
4254 public:
4255 Mutex mu_;
4256 };
4257
4258 void LockAllGraphs() EXCLUSIVE_LOCK_FUNCTION(&Graph::mu_);
4259 void UnlockAllGraphs() UNLOCK_FUNCTION(&Graph::mu_);
4260
4261 class Node {
4262 public:
4263 int a GUARDED_BY(&Graph::mu_);
4264
4265 void foo() EXCLUSIVE_LOCKS_REQUIRED(&Graph::mu_) {
4266 a = 0;
4267 }
4268 void foo2() LOCKS_EXCLUDED(&Graph::mu_);
4269 };
4270
4271 void test() {
4272 Graph g1;
4273 Graph g2;
4274 Node n1;
4275
4276 n1.a = 0; // expected-warning {{writing variable 'a' requires holding mutex '&ExistentialPatternMatching::Graph::mu_' exclusively}}
4277 n1.foo(); // expected-warning {{calling function 'foo' requires holding mutex '&ExistentialPatternMatching::Graph::mu_' exclusively}}
4278 n1.foo2();
4279
4280 g1.mu_.Lock();
4281 n1.a = 0;
4282 n1.foo();
4283 n1.foo2(); // expected-warning {{cannot call function 'foo2' while mutex '&ExistentialPatternMatching::Graph::mu_' is held}}
4284 g1.mu_.Unlock();
4285
4286 g2.mu_.Lock();
4287 n1.a = 0;
4288 n1.foo();
4289 n1.foo2(); // expected-warning {{cannot call function 'foo2' while mutex '&ExistentialPatternMatching::Graph::mu_' is held}}
4290 g2.mu_.Unlock();
4291
4292 LockAllGraphs();
4293 n1.a = 0;
4294 n1.foo();
4295 n1.foo2(); // expected-warning {{cannot call function 'foo2' while mutex '&ExistentialPatternMatching::Graph::mu_' is held}}
4296 UnlockAllGraphs();
4297
4298 LockAllGraphs();
4299 g1.mu_.Unlock();
4300
4301 LockAllGraphs();
4302 g2.mu_.Unlock();
4303
4304 LockAllGraphs(); // expected-note{{mutex acquired here}}
4305 g1.mu_.Lock(); // expected-warning {{acquiring mutex 'g1.mu_' that is already held}}
4306 g1.mu_.Unlock();
4307 }
4308
4309 } // end namespace ExistentialPatternMatching
4310
4311
4312 namespace StringIgnoreTest {
4313
4314 class Foo {
4315 public:
4316 Mutex mu_;
4317 void lock() EXCLUSIVE_LOCK_FUNCTION("");
4318 void unlock() UNLOCK_FUNCTION("");
4319 void goober() EXCLUSIVE_LOCKS_REQUIRED("");
4320 void roober() SHARED_LOCKS_REQUIRED("");
4321 };
4322
4323
4324 class Bar : public Foo {
4325 public:
4326 void bar(Foo* f) {
4327 f->unlock();
4328 f->goober();
4329 f->roober();
4330 f->lock();
4331 };
4332 };
4333
4334 } // end namespace StringIgnoreTest
4335
4336
4337 namespace LockReturnedScopeFix {
4338
4339 class Base {
4340 protected:
4341 struct Inner;
4342 bool c;
4343
4344 const Mutex& getLock(const Inner* i);
4345
4346 void lockInner (Inner* i) EXCLUSIVE_LOCK_FUNCTION(getLock(i));
4347 void unlockInner(Inner* i) UNLOCK_FUNCTION(getLock(i));
4348 void foo(Inner* i) EXCLUSIVE_LOCKS_REQUIRED(getLock(i));
4349
4350 void bar(Inner* i);
4351 };
4352
4353
4354 struct Base::Inner {
4355 Mutex lock_;
4356 void doSomething() EXCLUSIVE_LOCKS_REQUIRED(lock_);
4357 };
4358
4359
4360 const Mutex& Base::getLock(const Inner* i) LOCK_RETURNED(i->lock_) {
4361 return i->lock_;
4362 }
4363
4364
4365 void Base::foo(Inner* i) {
4366 i->doSomething();
4367 }
4368
4369 void Base::bar(Inner* i) {
4370 if (c) {
4371 i->lock_.Lock();
4372 unlockInner(i);
4373 }
4374 else {
4375 lockInner(i);
4376 i->lock_.Unlock();
4377 }
4378 }
4379
4380 } // end namespace LockReturnedScopeFix
4381
4382
4383 namespace TrylockWithCleanups {
4384
4385 struct Foo {
4386 Mutex mu_;
4387 int a GUARDED_BY(mu_);
4388 };
4389
4390 Foo* GetAndLockFoo(const MyString& s)
4391 EXCLUSIVE_TRYLOCK_FUNCTION(true, &Foo::mu_);
4392
4393 static void test() {
4394 Foo* lt = GetAndLockFoo("foo");
4395 if (!lt) return;
4396 int a = lt->a;
4397 lt->mu_.Unlock();
4398 }
4399
4400 } // end namespace TrylockWithCleanups
4401
4402
4403 namespace UniversalLock {
4404
4405 class Foo {
4406 Mutex mu_;
4407 bool c;
4408
4409 int a GUARDED_BY(mu_);
4410 void r_foo() SHARED_LOCKS_REQUIRED(mu_);
4411 void w_foo() EXCLUSIVE_LOCKS_REQUIRED(mu_);
4412
4413 void test1() {
4414 int b;
4415
4416 beginNoWarnOnReads();
4417 b = a;
4418 r_foo();
4419 endNoWarnOnReads();
4420
4421 beginNoWarnOnWrites();
4422 a = 0;
4423 w_foo();
4424 endNoWarnOnWrites();
4425 }
4426
4427 // don't warn on joins with universal lock
4428 void test2() {
4429 if (c) {
4430 beginNoWarnOnWrites();
4431 }
4432 a = 0; // \
4433 // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}}
4434 endNoWarnOnWrites(); // \
4435 // expected-warning {{releasing wildcard '*' that was not held}}
4436 }
4437
4438
4439 // make sure the universal lock joins properly
4440 void test3() {
4441 if (c) {
4442 mu_.Lock();
4443 beginNoWarnOnWrites();
4444 }
4445 else {
4446 beginNoWarnOnWrites();
4447 mu_.Lock();
4448 }
4449 a = 0;
4450 endNoWarnOnWrites();
4451 mu_.Unlock();
4452 }
4453
4454
4455 // combine universal lock with other locks
4456 void test4() {
4457 beginNoWarnOnWrites();
4458 mu_.Lock();
4459 mu_.Unlock();
4460 endNoWarnOnWrites();
4461
4462 mu_.Lock();
4463 beginNoWarnOnWrites();
4464 endNoWarnOnWrites();
4465 mu_.Unlock();
4466
4467 mu_.Lock();
4468 beginNoWarnOnWrites();
4469 mu_.Unlock();
4470 endNoWarnOnWrites();
4471 }
4472 };
4473
4474 } // end namespace UniversalLock
4475
4476
4477 namespace TemplateLockReturned {
4478
4479 template<class T>
4480 class BaseT {
4481 public:
4482 virtual void baseMethod() = 0;
4483 Mutex* get_mutex() LOCK_RETURNED(mutex_) { return &mutex_; }
4484
4485 Mutex mutex_;
4486 int a GUARDED_BY(mutex_);
4487 };
4488
4489
4490 class Derived : public BaseT<int> {
4491 public:
4492 void baseMethod() EXCLUSIVE_LOCKS_REQUIRED(get_mutex()) {
4493 a = 0;
4494 }
4495 };
4496
4497 } // end namespace TemplateLockReturned
4498
4499
4500 namespace ExprMatchingBugFix {
4501
4502 class Foo {
4503 public:
4504 Mutex mu_;
4505 };
4506
4507
4508 class Bar {
4509 public:
4510 bool c;
4511 Foo* foo;
4512 Bar(Foo* f) : foo(f) { }
4513
4514 struct Nested {
4515 Foo* foo;
4516 Nested(Foo* f) : foo(f) { }
4517
4518 void unlockFoo() UNLOCK_FUNCTION(&Foo::mu_);
4519 };
4520
4521 void test();
4522 };
4523
4524
4525 void Bar::test() {
4526 foo->mu_.Lock();
4527 if (c) {
4528 Nested *n = new Nested(foo);
4529 n->unlockFoo();
4530 }
4531 else {
4532 foo->mu_.Unlock();
4533 }
4534 }
4535
4536 }; // end namespace ExprMatchingBugfix
4537
4538
4539 namespace ComplexNameTest {
4540
4541 class Foo {
4542 public:
4543 static Mutex mu_;
4544
4545 Foo() EXCLUSIVE_LOCKS_REQUIRED(mu_) { }
4546 ~Foo() EXCLUSIVE_LOCKS_REQUIRED(mu_) { }
4547
4548 int operator[](int i) EXCLUSIVE_LOCKS_REQUIRED(mu_) { return 0; }
4549 };
4550
4551 class Bar {
4552 public:
4553 static Mutex mu_;
4554
4555 Bar() LOCKS_EXCLUDED(mu_) { }
4556 ~Bar() LOCKS_EXCLUDED(mu_) { }
4557
4558 int operator[](int i) LOCKS_EXCLUDED(mu_) { return 0; }
4559 };
4560
4561
4562 void test1() {
4563 Foo f; // expected-warning {{calling function 'Foo' requires holding mutex 'mu_' exclusively}}
4564 int a = f[0]; // expected-warning {{calling function 'operator[]' requires holding mutex 'mu_' exclusively}}
4565 } // expected-warning {{calling function '~Foo' requires holding mutex 'mu_' exclusively}}
4566
4567
4568 void test2() {
4569 Bar::mu_.Lock();
4570 {
4571 Bar b; // expected-warning {{cannot call function 'Bar' while mutex 'mu_' is held}}
4572 int a = b[0]; // expected-warning {{cannot call function 'operator[]' while mutex 'mu_' is held}}
4573 } // expected-warning {{cannot call function '~Bar' while mutex 'mu_' is held}}
4574 Bar::mu_.Unlock();
4575 }
4576
4577 }; // end namespace ComplexNameTest
4578
4579
4580 namespace UnreachableExitTest {
4581
4582 class FemmeFatale {
4583 public:
4584 FemmeFatale();
4585 ~FemmeFatale() __attribute__((noreturn));
4586 };
4587
4588 void exitNow() __attribute__((noreturn));
4589 void exitDestruct(const MyString& ms) __attribute__((noreturn));
4590
4591 Mutex fatalmu_;
4592
4593 void test1() EXCLUSIVE_LOCKS_REQUIRED(fatalmu_) {
4594 exitNow();
4595 }
4596
4597 void test2() EXCLUSIVE_LOCKS_REQUIRED(fatalmu_) {
4598 FemmeFatale femme;
4599 }
4600
4601 bool c;
4602
4603 void test3() EXCLUSIVE_LOCKS_REQUIRED(fatalmu_) {
4604 if (c) {
4605 exitNow();
4606 }
4607 else {
4608 FemmeFatale femme;
4609 }
4610 }
4611
4612 void test4() EXCLUSIVE_LOCKS_REQUIRED(fatalmu_) {
4613 exitDestruct("foo");
4614 }
4615
4616 } // end namespace UnreachableExitTest
4617
4618
4619 namespace VirtualMethodCanonicalizationTest {
4620
4621 class Base {
4622 public:
4623 virtual Mutex* getMutex() = 0;
4624 };
4625
4626 class Base2 : public Base {
4627 public:
4628 Mutex* getMutex();
4629 };
4630
4631 class Base3 : public Base2 {
4632 public:
4633 Mutex* getMutex();
4634 };
4635
4636 class Derived : public Base3 {
4637 public:
4638 Mutex* getMutex(); // overrides Base::getMutex()
4639 };
4640
4641 void baseFun(Base *b) EXCLUSIVE_LOCKS_REQUIRED(b->getMutex()) { }
4642
4643 void derivedFun(Derived *d) EXCLUSIVE_LOCKS_REQUIRED(d->getMutex()) {
4644 baseFun(d);
4645 }
4646
4647 } // end namespace VirtualMethodCanonicalizationTest
4648
4649
4650 namespace TemplateFunctionParamRemapTest {
4651
4652 template <class T>
4653 struct Cell {
4654 T dummy_;
4655 Mutex* mu_;
4656 };
4657
4658 class Foo {
4659 public:
4660 template <class T>
4661 void elr(Cell<T>* c) EXCLUSIVE_LOCKS_REQUIRED(c->mu_);
4662
4663 void test();
4664 };
4665
4666 template<class T>
4667 void Foo::elr(Cell<T>* c1) { }
4668
4669 void Foo::test() {
4670 Cell<int> cell;
4671 elr(&cell); // \
4672 // expected-warning {{calling function 'elr<int>' requires holding mutex 'cell.mu_' exclusively}}
4673 }
4674
4675
4676 template<class T>
4677 void globalELR(Cell<T>* c) EXCLUSIVE_LOCKS_REQUIRED(c->mu_);
4678
4679 template<class T>
4680 void globalELR(Cell<T>* c1) { }
4681
4682 void globalTest() {
4683 Cell<int> cell;
4684 globalELR(&cell); // \
4685 // expected-warning {{calling function 'globalELR<int>' requires holding mutex 'cell.mu_' exclusively}}
4686 }
4687
4688
4689 template<class T>
4690 void globalELR2(Cell<T>* c) EXCLUSIVE_LOCKS_REQUIRED(c->mu_);
4691
4692 // second declaration
4693 template<class T>
4694 void globalELR2(Cell<T>* c2);
4695
4696 template<class T>
4697 void globalELR2(Cell<T>* c3) { }
4698
4699 // re-declaration after definition
4700 template<class T>
4701 void globalELR2(Cell<T>* c4);
4702
4703 void globalTest2() {
4704 Cell<int> cell;
4705 globalELR2(&cell); // \
4706 // expected-warning {{calling function 'globalELR2<int>' requires holding mutex 'cell.mu_' exclusively}}
4707 }
4708
4709
4710 template<class T>
4711 class FooT {
4712 public:
4713 void elr(Cell<T>* c) EXCLUSIVE_LOCKS_REQUIRED(c->mu_);
4714 };
4715
4716 template<class T>
4717 void FooT<T>::elr(Cell<T>* c1) { }
4718
4719 void testFooT() {
4720 Cell<int> cell;
4721 FooT<int> foo;
4722 foo.elr(&cell); // \
4723 // expected-warning {{calling function 'elr' requires holding mutex 'cell.mu_' exclusively}}
4724 }
4725
4726 } // end namespace TemplateFunctionParamRemapTest
4727
4728
4729 namespace SelfConstructorTest {
4730
4731 class SelfLock {
4732 public:
4733 SelfLock() EXCLUSIVE_LOCK_FUNCTION(mu_);
4734 ~SelfLock() UNLOCK_FUNCTION(mu_);
4735
4736 void foo() EXCLUSIVE_LOCKS_REQUIRED(mu_);
4737
4738 Mutex mu_;
4739 };
4740
4741 class LOCKABLE SelfLock2 {
4742 public:
4743 SelfLock2() EXCLUSIVE_LOCK_FUNCTION();
4744 ~SelfLock2() UNLOCK_FUNCTION();
4745
4746 void foo() EXCLUSIVE_LOCKS_REQUIRED(this);
4747 };
4748
4749 class SelfLockDeferred {
4750 public:
4751 SelfLockDeferred() LOCKS_EXCLUDED(mu_);
4752 ~SelfLockDeferred() UNLOCK_FUNCTION(mu_);
4753
4754 Mutex mu_;
4755 };
4756
4757 class LOCKABLE SelfLockDeferred2 {
4758 public:
4759 SelfLockDeferred2() LOCKS_EXCLUDED(this);
4760 ~SelfLockDeferred2() UNLOCK_FUNCTION();
4761 };
4762
4763
4764 void test() {
4765 SelfLock s;
4766 s.foo();
4767 }
4768
4769 void test2() {
4770 SelfLock2 s2;
4771 s2.foo();
4772 }
4773
4774 void testDeferredTemporary() {
4775 SelfLockDeferred(); // expected-warning {{releasing mutex '<temporary>.mu_' that was not held}}
4776 }
4777
4778 void testDeferredTemporary2() {
4779 SelfLockDeferred2(); // expected-warning {{releasing mutex '<temporary>' that was not held}}
4780 }
4781
4782 } // end namespace SelfConstructorTest
4783
4784
4785 namespace MultipleAttributeTest {
4786
4787 class Foo {
4788 Mutex mu1_;
4789 Mutex mu2_;
4790 int a GUARDED_BY(mu1_);
4791 int b GUARDED_BY(mu2_);
4792 int c GUARDED_BY(mu1_) GUARDED_BY(mu2_);
4793 int* d PT_GUARDED_BY(mu1_) PT_GUARDED_BY(mu2_);
4794
4795 void foo1() EXCLUSIVE_LOCKS_REQUIRED(mu1_)
4796 EXCLUSIVE_LOCKS_REQUIRED(mu2_);
4797 void foo2() SHARED_LOCKS_REQUIRED(mu1_)
4798 SHARED_LOCKS_REQUIRED(mu2_);
4799 void foo3() LOCKS_EXCLUDED(mu1_)
4800 LOCKS_EXCLUDED(mu2_);
4801 void lock() EXCLUSIVE_LOCK_FUNCTION(mu1_)
4802 EXCLUSIVE_LOCK_FUNCTION(mu2_);
4803 void readerlock() SHARED_LOCK_FUNCTION(mu1_)
4804 SHARED_LOCK_FUNCTION(mu2_);
4805 void unlock() UNLOCK_FUNCTION(mu1_)
4806 UNLOCK_FUNCTION(mu2_);
4807 bool trylock() EXCLUSIVE_TRYLOCK_FUNCTION(true, mu1_)
4808 EXCLUSIVE_TRYLOCK_FUNCTION(true, mu2_);
4809 bool readertrylock() SHARED_TRYLOCK_FUNCTION(true, mu1_)
4810 SHARED_TRYLOCK_FUNCTION(true, mu2_);
4811 void assertBoth() ASSERT_EXCLUSIVE_LOCK(mu1_)
4812 ASSERT_EXCLUSIVE_LOCK(mu2_);
4813
4814 void alsoAssertBoth() ASSERT_EXCLUSIVE_LOCK(mu1_, mu2_);
4815
4816 void assertShared() ASSERT_SHARED_LOCK(mu1_)
4817 ASSERT_SHARED_LOCK(mu2_);
4818
4819 void alsoAssertShared() ASSERT_SHARED_LOCK(mu1_, mu2_);
4820
4821 void test();
4822 void testAssert();
4823 void testAssertShared();
4824 };
4825
4826
4827 void Foo::foo1() {
4828 a = 1;
4829 b = 2;
4830 }
4831
4832 void Foo::foo2() {
4833 int result = a + b;
4834 }
4835
4836 void Foo::foo3() { }
4837 void Foo::lock() { mu1_.Lock(); mu2_.Lock(); }
4838 void Foo::readerlock() { mu1_.ReaderLock(); mu2_.ReaderLock(); }
4839 void Foo::unlock() { mu1_.Unlock(); mu2_.Unlock(); }
4840 bool Foo::trylock() { return true; }
4841 bool Foo::readertrylock() { return true; }
4842
4843
4844 void Foo::test() {
4845 mu1_.Lock();
4846 foo1(); // expected-warning {{}}
4847 c = 0; // expected-warning {{}}
4848 *d = 0; // expected-warning {{}}
4849 mu1_.Unlock();
4850
4851 mu1_.ReaderLock();
4852 foo2(); // expected-warning {{}}
4853 int x = c; // expected-warning {{}}
4854 int y = *d; // expected-warning {{}}
4855 mu1_.Unlock();
4856
4857 mu2_.Lock();
4858 foo3(); // expected-warning {{}}
4859 mu2_.Unlock();
4860
4861 lock();
4862 a = 0;
4863 b = 0;
4864 unlock();
4865
4866 readerlock();
4867 int z = a + b;
4868 unlock();
4869
4870 if (trylock()) {
4871 a = 0;
4872 b = 0;
4873 unlock();
4874 }
4875
4876 if (readertrylock()) {
4877 int zz = a + b;
4878 unlock();
4879 }
4880 }
4881
4882 // Force duplication of attributes
4883 void Foo::assertBoth() { }
4884 void Foo::alsoAssertBoth() { }
4885 void Foo::assertShared() { }
4886 void Foo::alsoAssertShared() { }
4887
4888 void Foo::testAssert() {
4889 {
4890 assertBoth();
4891 a = 0;
4892 b = 0;
4893 }
4894 {
4895 alsoAssertBoth();
4896 a = 0;
4897 b = 0;
4898 }
4899 }
4900
4901 void Foo::testAssertShared() {
4902 {
4903 assertShared();
4904 int zz = a + b;
4905 }
4906
4907 {
4908 alsoAssertShared();
4909 int zz = a + b;
4910 }
4911 }
4912
4913
4914 } // end namespace MultipleAttributeTest
4915
4916
4917 namespace GuardedNonPrimitiveTypeTest {
4918
4919
4920 class Data {
4921 public:
4922 Data(int i) : dat(i) { }
4923
4924 int getValue() const { return dat; }
4925 void setValue(int i) { dat = i; }
4926
4927 int operator[](int i) const { return dat; }
4928 int& operator[](int i) { return dat; }
4929
4930 void operator()() { }
4931
4932 Data& operator+=(int);
4933 Data& operator-=(int);
4934 Data& operator*=(int);
4935 Data& operator/=(int);
4936 Data& operator%=(int);
4937 Data& operator^=(int);
4938 Data& operator&=(int);
4939 Data& operator|=(int);
4940 Data& operator<<=(int);
4941 Data& operator>>=(int);
4942 Data& operator++();
4943 Data& operator++(int);
4944 Data& operator--();
4945 Data& operator--(int);
4946
4947 private:
4948 int dat;
4949 };
4950
4951 class DataWithAddrOf : public Data {
4952 public:
4953 const Data* operator&();
4954 const Data* operator&() const;
4955 };
4956
4957 class DataCell {
4958 public:
4959 DataCell(const Data& d) : dat(d) { }
4960
4961 private:
4962 Data dat;
4963 };
4964
4965
4966 void showData(const Data* d);
4967 void showDataCell(const DataCell& dc);
4968
4969
4970 class Foo {
4971 public:
4972 // method call tests
4973 void test() {
4974 data_.setValue(0); // FIXME -- should be writing \
4975 // expected-warning {{reading variable 'data_' requires holding mutex 'mu_'}}
4976 int a = data_.getValue(); // \
4977 // expected-warning {{reading variable 'data_' requires holding mutex 'mu_'}}
4978
4979 datap1_->setValue(0); // FIXME -- should be writing \
4980 // expected-warning {{reading variable 'datap1_' requires holding mutex 'mu_'}}
4981 a = datap1_->getValue(); // \
4982 // expected-warning {{reading variable 'datap1_' requires holding mutex 'mu_'}}
4983
4984 datap2_->setValue(0); // FIXME -- should be writing \
4985 // expected-warning {{reading the value pointed to by 'datap2_' requires holding mutex 'mu_'}}
4986 a = datap2_->getValue(); // \
4987 // expected-warning {{reading the value pointed to by 'datap2_' requires holding mutex 'mu_'}}
4988
4989 (*datap2_).setValue(0); // FIXME -- should be writing \
4990 // expected-warning {{reading the value pointed to by 'datap2_' requires holding mutex 'mu_'}}
4991 a = (*datap2_).getValue(); // \
4992 // expected-warning {{reading the value pointed to by 'datap2_' requires holding mutex 'mu_'}}
4993
4994 mu_.Lock();
4995 data_.setValue(1);
4996 datap1_->setValue(1);
4997 datap2_->setValue(1);
4998 mu_.Unlock();
4999
5000 mu_.ReaderLock();
5001 a = data_.getValue();
5002 datap1_->setValue(0); // reads datap1_, writes *datap1_
5003 a = datap1_->getValue();
5004 a = datap2_->getValue();
5005 mu_.Unlock();
5006 }
5007
5008 // operator tests
5009 void test2() {
5010 data_ = Data(1); // expected-warning {{writing variable 'data_' requires holding mutex 'mu_' exclusively}}
5011 *datap1_ = data_; // expected-warning {{reading variable 'datap1_' requires holding mutex 'mu_'}} \
5012 // expected-warning {{reading variable 'data_' requires holding mutex 'mu_'}}
5013 *datap2_ = data_; // expected-warning {{writing the value pointed to by 'datap2_' requires holding mutex 'mu_' exclusively}} \
5014 // expected-warning {{reading variable 'data_' requires holding mutex 'mu_'}}
5015 data_ = *datap1_; // expected-warning {{writing variable 'data_' requires holding mutex 'mu_' exclusively}} \
5016 // expected-warning {{reading variable 'datap1_' requires holding mutex 'mu_'}}
5017 data_ = *datap2_; // expected-warning {{writing variable 'data_' requires holding mutex 'mu_' exclusively}} \
5018 // expected-warning {{reading the value pointed to by 'datap2_' requires holding mutex 'mu_'}}
5019 data_ += 1; // expected-warning {{writing variable 'data_' requires holding mutex 'mu_' exclusively}}
5020 data_ -= 1; // expected-warning {{writing variable 'data_' requires holding mutex 'mu_' exclusively}}
5021 data_ *= 1; // expected-warning {{writing variable 'data_' requires holding mutex 'mu_' exclusively}}
5022 data_ /= 1; // expected-warning {{writing variable 'data_' requires holding mutex 'mu_' exclusively}}
5023 data_ %= 1; // expected-warning {{writing variable 'data_' requires holding mutex 'mu_' exclusively}}
5024 data_ ^= 1; // expected-warning {{writing variable 'data_' requires holding mutex 'mu_' exclusively}}
5025 data_ &= 1; // expected-warning {{writing variable 'data_' requires holding mutex 'mu_' exclusively}}
5026 data_ |= 1; // expected-warning {{writing variable 'data_' requires holding mutex 'mu_' exclusively}}
5027 data_ <<= 1; // expected-warning {{writing variable 'data_' requires holding mutex 'mu_' exclusively}}
5028 data_ >>= 1; // expected-warning {{writing variable 'data_' requires holding mutex 'mu_' exclusively}}
5029 ++data_; // expected-warning {{writing variable 'data_' requires holding mutex 'mu_' exclusively}}
5030 data_++; // expected-warning {{writing variable 'data_' requires holding mutex 'mu_' exclusively}}
5031 --data_; // expected-warning {{writing variable 'data_' requires holding mutex 'mu_' exclusively}}
5032 data_--; // expected-warning {{writing variable 'data_' requires holding mutex 'mu_' exclusively}}
5033
5034 data_[0] = 0; // expected-warning {{reading variable 'data_' requires holding mutex 'mu_'}}
5035 (*datap2_)[0] = 0; // expected-warning {{reading the value pointed to by 'datap2_' requires holding mutex 'mu_'}}
5036
5037 data_(); // expected-warning {{reading variable 'data_' requires holding mutex 'mu_'}}
5038
5039 // Calls operator&, and does not take the address.
5040 (void)&data_ao_; // expected-warning {{reading variable 'data_ao_' requires holding mutex 'mu_'}}
5041 (void)__builtin_addressof(data_ao_); // expected-warning {{passing variable 'data_ao_' by reference requires holding mutex 'mu_'}}
5042 showData(&data_ao_); // expected-warning {{reading variable 'data_ao_' requires holding mutex 'mu_'}}
5043 }
5044
5045 // const operator tests
5046 void test3() const {
5047 Data mydat(data_); // expected-warning {{reading variable 'data_' requires holding mutex 'mu_'}}
5048
5049 //FIXME
5050 //showDataCell(data_); // xpected-warning {{reading variable 'data_' requires holding mutex 'mu_'}}
5051 //showDataCell(*datap2_); // xpected-warning {{reading the value pointed to by 'datap2_' requires holding mutex 'mu_'}}
5052
5053 int a = data_[0]; // expected-warning {{reading variable 'data_' requires holding mutex 'mu_'}}
5054
5055 (void)&data_ao_; // expected-warning {{reading variable 'data_ao_' requires holding mutex 'mu_'}}
5056 showData(&data_ao_); // expected-warning {{reading variable 'data_ao_' requires holding mutex 'mu_'}}
5057 }
5058
5059 private:
5060 Mutex mu_;
5061 Data data_ GUARDED_BY(mu_);
5062 Data* datap1_ GUARDED_BY(mu_);
5063 Data* datap2_ PT_GUARDED_BY(mu_);
5064 DataWithAddrOf data_ao_ GUARDED_BY(mu_);
5065 };
5066
5067 } // end namespace GuardedNonPrimitiveTypeTest
5068
5069
5070 namespace GuardedNonPrimitive_MemberAccess {
5071
5072 class Cell {
5073 public:
5074 Cell(int i);
5075
5076 void cellMethod();
5077
5078 int a;
5079 };
5080
5081
5082 class Foo {
5083 public:
5084 int a;
5085 Cell c GUARDED_BY(cell_mu_);
5086 Cell* cp PT_GUARDED_BY(cell_mu_);
5087
5088 void myMethod();
5089
5090 Mutex cell_mu_;
5091 };
5092
5093
5094 class Bar {
5095 private:
5096 Mutex mu_;
5097 Foo foo GUARDED_BY(mu_);
5098 Foo* foop PT_GUARDED_BY(mu_);
5099
5100 void test() {
5101 foo.myMethod(); // expected-warning {{reading variable 'foo' requires holding mutex 'mu_'}}
5102
5103 int fa = foo.a; // expected-warning {{reading variable 'foo' requires holding mutex 'mu_'}}
5104 foo.a = fa; // expected-warning {{writing variable 'foo' requires holding mutex 'mu_' exclusively}}
5105
5106 fa = foop->a; // expected-warning {{reading the value pointed to by 'foop' requires holding mutex 'mu_'}}
5107 foop->a = fa; // expected-warning {{writing the value pointed to by 'foop' requires holding mutex 'mu_' exclusively}}
5108
5109 fa = (*foop).a; // expected-warning {{reading the value pointed to by 'foop' requires holding mutex 'mu_'}}
5110 (*foop).a = fa; // expected-warning {{writing the value pointed to by 'foop' requires holding mutex 'mu_' exclusively}}
5111
5112 foo.c = Cell(0); // expected-warning {{writing variable 'foo' requires holding mutex 'mu_'}} \
5113 // expected-warning {{writing variable 'c' requires holding mutex 'foo.cell_mu_' exclusively}}
5114 foo.c.cellMethod(); // expected-warning {{reading variable 'foo' requires holding mutex 'mu_'}} \
5115 // expected-warning {{reading variable 'c' requires holding mutex 'foo.cell_mu_'}}
5116
5117 foop->c = Cell(0); // expected-warning {{writing the value pointed to by 'foop' requires holding mutex 'mu_'}} \
5118 // expected-warning {{writing variable 'c' requires holding mutex 'foop->cell_mu_' exclusively}}
5119 foop->c.cellMethod(); // expected-warning {{reading the value pointed to by 'foop' requires holding mutex 'mu_'}} \
5120 // expected-warning {{reading variable 'c' requires holding mutex 'foop->cell_mu_'}}
5121
5122 (*foop).c = Cell(0); // expected-warning {{writing the value pointed to by 'foop' requires holding mutex 'mu_'}} \
5123 // expected-warning {{writing variable 'c' requires holding mutex 'foop->cell_mu_' exclusively}}
5124 (*foop).c.cellMethod(); // expected-warning {{reading the value pointed to by 'foop' requires holding mutex 'mu_'}} \
5125 // expected-warning {{reading variable 'c' requires holding mutex 'foop->cell_mu_'}}
5126 };
5127 };
5128
5129 } // namespace GuardedNonPrimitive_MemberAccess
5130
5131
5132 namespace TestThrowExpr {
5133
5134 class Foo {
5135 Mutex mu_;
5136
5137 bool hasError();
5138
5139 void test() {
5140 mu_.Lock();
5141 if (hasError()) {
5142 throw "ugly";
5143 }
5144 mu_.Unlock();
5145 }
5146 };
5147
5148 } // end namespace TestThrowExpr
5149
5150
5151 namespace UnevaluatedContextTest {
5152
5153 // parse attribute expressions in an unevaluated context.
5154
5155 static inline Mutex* getMutex1();
5156 static inline Mutex* getMutex2();
5157
5158 void bar() EXCLUSIVE_LOCKS_REQUIRED(getMutex1());
5159
5160 void bar2() EXCLUSIVE_LOCKS_REQUIRED(getMutex1(), getMutex2());
5161
5162 } // end namespace UnevaluatedContextTest
5163
5164
5165 namespace LockUnlockFunctionTest {
5166
5167 // Check built-in lock functions
5168 class LOCKABLE MyLockable {
5169 public:
5170 void lock() EXCLUSIVE_LOCK_FUNCTION() { mu_.Lock(); }
5171 void readerLock() SHARED_LOCK_FUNCTION() { mu_.ReaderLock(); }
5172 void unlock() UNLOCK_FUNCTION() { mu_.Unlock(); }
5173
5174 private:
5175 Mutex mu_;
5176 };
5177
5178
5179 class Foo {
5180 public:
5181 // Correct lock/unlock functions
5182 void lock() EXCLUSIVE_LOCK_FUNCTION(mu_) {
5183 mu_.Lock();
5184 }
5185
5186 void readerLock() SHARED_LOCK_FUNCTION(mu_) {
5187 mu_.ReaderLock();
5188 }
5189
5190 void unlock() UNLOCK_FUNCTION(mu_) {
5191 mu_.Unlock();
5192 }
5193
5194 void unlockExclusive() EXCLUSIVE_UNLOCK_FUNCTION(mu_) {
5195 mu_.Unlock();
5196 }
5197
5198 void unlockShared() SHARED_UNLOCK_FUNCTION(mu_) {
5199 mu_.ReaderUnlock();
5200 }
5201
5202 // Check failure to lock.
5203 void lockBad() EXCLUSIVE_LOCK_FUNCTION(mu_) { // expected-note {{mutex acquired here}}
5204 mu2_.Lock();
5205 mu2_.Unlock();
5206 } // expected-warning {{expecting mutex 'mu_' to be held at the end of function}}
5207
5208 void readerLockBad() SHARED_LOCK_FUNCTION(mu_) { // expected-note {{mutex acquired here}}
5209 mu2_.Lock();
5210 mu2_.Unlock();
5211 } // expected-warning {{expecting mutex 'mu_' to be held at the end of function}}
5212
5213 void unlockBad() UNLOCK_FUNCTION(mu_) { // expected-note {{mutex acquired here}}
5214 mu2_.Lock();
5215 mu2_.Unlock();
5216 } // expected-warning {{mutex 'mu_' is still held at the end of function}}
5217
5218 // Check locking the wrong thing.
5219 void lockBad2() EXCLUSIVE_LOCK_FUNCTION(mu_) { // expected-note {{mutex acquired here}}
5220 mu2_.Lock(); // expected-note {{mutex acquired here}}
5221 } // expected-warning {{expecting mutex 'mu_' to be held at the end of function}} \
5222 // expected-warning {{mutex 'mu2_' is still held at the end of function}}
5223
5224
5225 void readerLockBad2() SHARED_LOCK_FUNCTION(mu_) { // expected-note {{mutex acquired here}}
5226 mu2_.ReaderLock(); // expected-note {{mutex acquired here}}
5227 } // expected-warning {{expecting mutex 'mu_' to be held at the end of function}} \
5228 // expected-warning {{mutex 'mu2_' is still held at the end of function}}
5229
5230
5231 void unlockBad2() UNLOCK_FUNCTION(mu_) { // expected-note {{mutex acquired here}}
5232 mu2_.Unlock(); // expected-warning {{releasing mutex 'mu2_' that was not held}}
5233 } // expected-warning {{mutex 'mu_' is still held at the end of function}}
5234
5235 private:
5236 Mutex mu_;
5237 Mutex mu2_;
5238 };
5239
5240 } // end namespace LockUnlockFunctionTest
5241
5242
5243 namespace AssertHeldTest {
5244
5245 class Foo {
5246 public:
5247 int c;
5248 int a GUARDED_BY(mu_);
5249 Mutex mu_;
5250
5251 void test1() {
5252 mu_.AssertHeld();
5253 int b = a;
5254 a = 0;
5255 }
5256
5257 void test2() {
5258 mu_.AssertReaderHeld();
5259 int b = a;
5260 a = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}}
5261 }
5262
5263 void test3() {
5264 if (c) {
5265 mu_.AssertHeld();
5266 }
5267 else {
5268 mu_.AssertHeld();
5269 }
5270 int b = a;
5271 a = 0;
5272 }
5273
5274 void test4() EXCLUSIVE_LOCKS_REQUIRED(mu_) {
5275 mu_.AssertHeld();
5276 int b = a;
5277 a = 0;
5278 }
5279
5280 void test5() UNLOCK_FUNCTION(mu_) {
5281 mu_.AssertHeld();
5282 mu_.Unlock();
5283 }
5284
5285 void test6() {
5286 mu_.AssertHeld();
5287 mu_.Unlock(); // should this be a warning?
5288 }
5289
5290 void test7() {
5291 if (c) {
5292 mu_.AssertHeld();
5293 }
5294 else {
5295 mu_.Lock();
5296 }
5297 int b = a;
5298 a = 0;
5299 mu_.Unlock();
5300 }
5301
5302 void test8() {
5303 if (c) {
5304 mu_.Lock();
5305 }
5306 else {
5307 mu_.AssertHeld();
5308 }
5309 // FIXME: should warn, because it's unclear whether we need to release or not.
5310 int b = a;
5311 a = 0;
5312 mu_.Unlock(); // should this be a warning?
5313 }
5314
5315 void test9() {
5316 if (c) {
5317 mu_.AssertHeld();
5318 }
5319 else {
5320 mu_.Lock(); // expected-note {{mutex acquired here}}
5321 }
5322 } // expected-warning {{mutex 'mu_' is still held at the end of function}}
5323
5324 void test10() {
5325 if (c) {
5326 mu_.Lock(); // expected-note {{mutex acquired here}}
5327 }
5328 else {
5329 mu_.AssertHeld();
5330 }
5331 } // expected-warning {{mutex 'mu_' is still held at the end of function}}
5332
5333 void assertMu() ASSERT_EXCLUSIVE_LOCK(mu_);
5334
5335 void test11() {
5336 assertMu();
5337 int b = a;
5338 a = 0;
5339 }
5340
5341 void test12() {
5342 if (c)
5343 mu_.ReaderLock(); // expected-warning {{mutex 'mu_' is acquired exclusively and shared in the same scope}}
5344 else
5345 mu_.AssertHeld(); // expected-note {{the other acquisition of mutex 'mu_' is here}}
5346 // FIXME: should instead warn because it's unclear whether we need to release or not.
5347 int b = a;
5348 a = 0;
5349 mu_.Unlock();
5350 }
5351
5352 void test13() {
5353 if (c)
5354 mu_.Lock(); // expected-warning {{mutex 'mu_' is acquired exclusively and shared in the same scope}}
5355 else
5356 mu_.AssertReaderHeld(); // expected-note {{the other acquisition of mutex 'mu_' is here}}
5357 // FIXME: should instead warn because it's unclear whether we need to release or not.
5358 int b = a;
5359 a = 0;
5360 mu_.Unlock();
5361 }
5362 };
5363
5364 } // end namespace AssertHeldTest
5365
5366
5367 namespace LogicalConditionalTryLock {
5368
5369 class Foo {
5370 public:
5371 Mutex mu;
5372 int a GUARDED_BY(mu);
5373 bool c;
5374
5375 bool newc();
5376
5377 void test1() {
5378 if (c && mu.TryLock()) {
5379 a = 0;
5380 mu.Unlock();
5381 }
5382 }
5383
5384 void test2() {
5385 bool b = mu.TryLock();
5386 if (c && b) {
5387 a = 0;
5388 mu.Unlock();
5389 }
5390 }
5391
5392 void test3() {
5393 if (c || !mu.TryLock())
5394 return;
5395 a = 0;
5396 mu.Unlock();
5397 }
5398
5399 void test4() {
5400 while (c && mu.TryLock()) {
5401 a = 0;
5402 c = newc();
5403 mu.Unlock();
5404 }
5405 }
5406
5407 void test5() {
5408 while (c) {
5409 if (newc() || !mu.TryLock())
5410 break;
5411 a = 0;
5412 mu.Unlock();
5413 }
5414 }
5415
5416 void test6() {
5417 mu.Lock();
5418 do {
5419 a = 0;
5420 mu.Unlock();
5421 } while (newc() && mu.TryLock());
5422 }
5423
5424 void test7() {
5425 for (bool b = mu.TryLock(); c && b;) {
5426 a = 0;
5427 mu.Unlock();
5428 }
5429 }
5430
5431 void test8() {
5432 if (c && newc() && mu.TryLock()) {
5433 a = 0;
5434 mu.Unlock();
5435 }
5436 }
5437
5438 void test9() {
5439 if (!(c && newc() && mu.TryLock()))
5440 return;
5441 a = 0;
5442 mu.Unlock();
5443 }
5444
5445 void test10() {
5446 if (!(c || !mu.TryLock())) {
5447 a = 0;
5448 mu.Unlock();
5449 }
5450 }
5451 };
5452
5453 } // end namespace LogicalConditionalTryLock
5454
5455
5456
5457 namespace PtGuardedByTest {
5458
5459 void doSomething();
5460
5461 class Cell {
5462 public:
5463 int a;
5464 };
5465
5466
5467 // This mainly duplicates earlier tests, but just to make sure...
5468 class PtGuardedByCorrectnessTest {
5469 Mutex mu1;
5470 Mutex mu2;
5471 int* a GUARDED_BY(mu1) PT_GUARDED_BY(mu2);
5472 Cell* c GUARDED_BY(mu1) PT_GUARDED_BY(mu2);
5473 int sa[10] GUARDED_BY(mu1);
5474 Cell sc[10] GUARDED_BY(mu1);
5475
5476 static constexpr int Cell::*pa = &Cell::a;
5477
5478 void test1() {
5479 mu1.Lock();
5480 if (a == 0) doSomething(); // OK, we don't dereference.
5481 a = 0;
5482 c = 0;
5483 if (sa[0] == 42) doSomething();
5484 sa[0] = 57;
5485 if (sc[0].a == 42) doSomething();
5486 sc[0].a = 57;
5487 mu1.Unlock();
5488 }
5489
5490 void test2() {
5491 mu1.ReaderLock();
5492 if (*a == 0) doSomething(); // expected-warning {{reading the value pointed to by 'a' requires holding mutex 'mu2'}}
5493 *a = 0; // expected-warning {{writing the value pointed to by 'a' requires holding mutex 'mu2' exclusively}}
5494
5495 if (c->a == 0) doSomething(); // expected-warning {{reading the value pointed to by 'c' requires holding mutex 'mu2'}}
5496 c->a = 0; // expected-warning {{writing the value pointed to by 'c' requires holding mutex 'mu2' exclusively}}
5497 c->*pa = 0; // expected-warning {{writing the value pointed to by 'c' requires holding mutex 'mu2' exclusively}}
5498
5499 if ((*c).a == 0) doSomething(); // expected-warning {{reading the value pointed to by 'c' requires holding mutex 'mu2'}}
5500 (*c).a = 0; // expected-warning {{writing the value pointed to by 'c' requires holding mutex 'mu2' exclusively}}
5501 (*c).*pa = 0; // expected-warning {{writing the value pointed to by 'c' requires holding mutex 'mu2' exclusively}}
5502
5503 if (a[0] == 42) doSomething(); // expected-warning {{reading the value pointed to by 'a' requires holding mutex 'mu2'}}
5504 a[0] = 57; // expected-warning {{writing the value pointed to by 'a' requires holding mutex 'mu2' exclusively}}
5505 if (c[0].a == 42) doSomething(); // expected-warning {{reading the value pointed to by 'c' requires holding mutex 'mu2'}}
5506 c[0].a = 57; // expected-warning {{writing the value pointed to by 'c' requires holding mutex 'mu2' exclusively}}
5507 mu1.Unlock();
5508 }
5509
5510 void test3() {
5511 mu2.Lock();
5512 if (*a == 0) doSomething(); // expected-warning {{reading variable 'a' requires holding mutex 'mu1'}}
5513 *a = 0; // expected-warning {{reading variable 'a' requires holding mutex 'mu1'}}
5514
5515 if (c->a == 0) doSomething(); // expected-warning {{reading variable 'c' requires holding mutex 'mu1'}}
5516 c->a = 0; // expected-warning {{reading variable 'c' requires holding mutex 'mu1'}}
5517
5518 if ((*c).a == 0) doSomething(); // expected-warning {{reading variable 'c' requires holding mutex 'mu1'}}
5519 (*c).a = 0; // expected-warning {{reading variable 'c' requires holding mutex 'mu1'}}
5520
5521 if (a[0] == 42) doSomething(); // expected-warning {{reading variable 'a' requires holding mutex 'mu1'}}
5522 a[0] = 57; // expected-warning {{reading variable 'a' requires holding mutex 'mu1'}}
5523 if (c[0].a == 42) doSomething(); // expected-warning {{reading variable 'c' requires holding mutex 'mu1'}}
5524 c[0].a = 57; // expected-warning {{reading variable 'c' requires holding mutex 'mu1'}}
5525 mu2.Unlock();
5526 }
5527
5528 void test4() { // Literal arrays
5529 if (sa[0] == 42) doSomething(); // expected-warning {{reading variable 'sa' requires holding mutex 'mu1'}}
5530 sa[0] = 57; // expected-warning {{writing variable 'sa' requires holding mutex 'mu1' exclusively}}
5531 if (sc[0].a == 42) doSomething(); // expected-warning {{reading variable 'sc' requires holding mutex 'mu1'}}
5532 sc[0].a = 57; // expected-warning {{writing variable 'sc' requires holding mutex 'mu1' exclusively}}
5533 sc[0].*pa = 57; // expected-warning {{writing variable 'sc' requires holding mutex 'mu1' exclusively}}
5534
5535 if (*sa == 42) doSomething(); // expected-warning {{reading variable 'sa' requires holding mutex 'mu1'}}
5536 *sa = 57; // expected-warning {{writing variable 'sa' requires holding mutex 'mu1' exclusively}}
5537 if ((*sc).a == 42) doSomething(); // expected-warning {{reading variable 'sc' requires holding mutex 'mu1'}}
5538 (*sc).a = 57; // expected-warning {{writing variable 'sc' requires holding mutex 'mu1' exclusively}}
5539 if (sc->a == 42) doSomething(); // expected-warning {{reading variable 'sc' requires holding mutex 'mu1'}}
5540 sc->a = 57; // expected-warning {{writing variable 'sc' requires holding mutex 'mu1' exclusively}}
5541 }
5542
5543 void test5() {
5544 mu1.ReaderLock(); // OK -- correct use.
5545 mu2.Lock();
5546 if (*a == 0) doSomething();
5547 *a = 0;
5548
5549 if (c->a == 0) doSomething();
5550 c->a = 0;
5551
5552 if ((*c).a == 0) doSomething();
5553 (*c).a = 0;
5554 mu2.Unlock();
5555 mu1.Unlock();
5556 }
5557 };
5558
5559
5560 class SmartPtr_PtGuardedBy_Test {
5561 Mutex mu1;
5562 Mutex mu2;
5563 SmartPtr<int> sp GUARDED_BY(mu1) PT_GUARDED_BY(mu2);
5564 SmartPtr<Cell> sq GUARDED_BY(mu1) PT_GUARDED_BY(mu2);
5565
5566 static constexpr int Cell::*pa = &Cell::a;
5567
5568 void test1() {
5569 mu1.ReaderLock();
5570 mu2.Lock();
5571
5572 sp.get();
5573 if (*sp == 0) doSomething();
5574 *sp = 0;
5575 sq->a = 0;
5576 sq->*pa = 0;
5577
5578 if (sp[0] == 0) doSomething();
5579 sp[0] = 0;
5580
5581 mu2.Unlock();
5582 mu1.Unlock();
5583 }
5584
5585 void test2() {
5586 mu2.Lock();
5587
5588 sp.get(); // expected-warning {{reading variable 'sp' requires holding mutex 'mu1'}}
5589 if (*sp == 0) doSomething(); // expected-warning {{reading variable 'sp' requires holding mutex 'mu1'}}
5590 *sp = 0; // expected-warning {{reading variable 'sp' requires holding mutex 'mu1'}}
5591 sq->a = 0; // expected-warning {{reading variable 'sq' requires holding mutex 'mu1'}}
5592 sq->*pa = 0; // expected-warning {{reading variable 'sq' requires holding mutex 'mu1'}}
5593
5594 if (sp[0] == 0) doSomething(); // expected-warning {{reading variable 'sp' requires holding mutex 'mu1'}}
5595 sp[0] = 0; // expected-warning {{reading variable 'sp' requires holding mutex 'mu1'}}
5596 if (sq[0].a == 0) doSomething(); // expected-warning {{reading variable 'sq' requires holding mutex 'mu1'}}
5597 sq[0].a = 0; // expected-warning {{reading variable 'sq' requires holding mutex 'mu1'}}
5598
5599 mu2.Unlock();
5600 }
5601
5602 void test3() {
5603 mu1.Lock();
5604
5605 sp.get();
5606 if (*sp == 0) doSomething(); // expected-warning {{reading the value pointed to by 'sp' requires holding mutex 'mu2'}}
5607 *sp = 0; // expected-warning {{reading the value pointed to by 'sp' requires holding mutex 'mu2'}}
5608 sq->a = 0; // expected-warning {{reading the value pointed to by 'sq' requires holding mutex 'mu2'}}
5609 sq->*pa = 0; // expected-warning {{reading the value pointed to by 'sq' requires holding mutex 'mu2'}}
5610
5611 if (sp[0] == 0) doSomething(); // expected-warning {{reading the value pointed to by 'sp' requires holding mutex 'mu2'}}
5612 sp[0] = 0; // expected-warning {{reading the value pointed to by 'sp' requires holding mutex 'mu2'}}
5613 if (sq[0].a == 0) doSomething(); // expected-warning {{reading the value pointed to by 'sq' requires holding mutex 'mu2'}}
5614 sq[0].a = 0; // expected-warning {{reading the value pointed to by 'sq' requires holding mutex 'mu2'}}
5615
5616 mu1.Unlock();
5617 }
5618 };
5619
5620 } // end namespace PtGuardedByTest
5621
5622
5623 namespace NonMemberCalleeICETest {
5624
5625 class A {
5626 void Run() {
5627 (RunHelper)(); // expected-warning {{calling function 'RunHelper' requires holding mutex 'M' exclusively}}
5628 }
5629
5630 void RunHelper() EXCLUSIVE_LOCKS_REQUIRED(M);
5631 Mutex M;
5632 };
5633
5634 } // end namespace NonMemberCalleeICETest
5635
5636
5637 namespace pt_guard_attribute_type {
5638 int i PT_GUARDED_BY(sls_mu); // expected-warning {{'pt_guarded_by' only applies to pointer types; type here is 'int'}}
5639 int j PT_GUARDED_VAR; // expected-warning {{'pt_guarded_var' only applies to pointer types; type here is 'int'}}
5640
5641 void test() {
5642 int i PT_GUARDED_BY(sls_mu); // expected-warning {{'pt_guarded_by' attribute only applies to non-static data members and global variables}}
5643 int j PT_GUARDED_VAR; // expected-warning {{'pt_guarded_var' attribute only applies to non-static data members and global variables}}
5644
5645 typedef int PT_GUARDED_BY(sls_mu) bad1; // expected-warning {{'pt_guarded_by' attribute only applies to}}
5646 typedef int PT_GUARDED_VAR bad2; // expected-warning {{'pt_guarded_var' attribute only applies to}}
5647 }
5648 } // end namespace pt_guard_attribute_type
5649
5650
5651 namespace ThreadAttributesOnLambdas {
5652
5653 class Foo {
5654 Mutex mu_;
5655
5656 void LockedFunction() EXCLUSIVE_LOCKS_REQUIRED(mu_);
5657
5658 void test() {
5659 auto func1 = [this]() EXCLUSIVE_LOCKS_REQUIRED(mu_) {
5660 LockedFunction();
5661 };
5662
5663 auto func2 = [this]() NO_THREAD_SAFETY_ANALYSIS {
5664 LockedFunction();
5665 };
5666
5667 auto func3 = [this]() EXCLUSIVE_LOCK_FUNCTION(mu_) {
5668 mu_.Lock();
5669 };
5670
5671 func1(); // expected-warning {{calling function 'operator()' requires holding mutex 'mu_' exclusively}}
5672 func1.operator()(); // expected-warning {{calling function 'operator()' requires holding mutex 'mu_' exclusively}}
5673 func2();
5674 func2.operator()();
5675 func3();
5676 mu_.Unlock();
5677 func3.operator()();
5678 mu_.Unlock();
5679 }
5680 };
5681
5682 } // end namespace ThreadAttributesOnLambdas
5683
5684
5685
5686 namespace AttributeExpressionCornerCases {
5687
5688 class Foo {
5689 int a GUARDED_BY(getMu());
5690
5691 Mutex* getMu() LOCK_RETURNED("");
5692 Mutex* getUniv() LOCK_RETURNED("*");
5693
5694 void test1() {
5695 a = 0;
5696 }
5697
5698 void test2() EXCLUSIVE_LOCKS_REQUIRED(getUniv()) {
5699 a = 0;
5700 }
5701
5702 void foo(Mutex* mu) EXCLUSIVE_LOCKS_REQUIRED(mu);
5703
5704 void test3() {
5705 foo(nullptr);
5706 }
5707 };
5708
5709
5710 class MapTest {
5711 struct MuCell { Mutex* mu; };
5712
5713 MyMap<MyString, Mutex*> map;
5714 MyMap<MyString, MuCell> mapCell;
5715
5716 int a GUARDED_BY(map["foo"]);
5717 int b GUARDED_BY(mapCell["foo"].mu);
5718
5719 void test() {
5720 map["foo"]->Lock();
5721 a = 0;
5722 map["foo"]->Unlock();
5723 }
5724
5725 void test2() {
5726 mapCell["foo"].mu->Lock();
5727 b = 0;
5728 mapCell["foo"].mu->Unlock();
5729 }
5730 };
5731
5732
5733 class PreciseSmartPtr {
5734 SmartPtr<Mutex> mu;
5735 int val GUARDED_BY(mu);
5736
5737 static bool compare(PreciseSmartPtr& a, PreciseSmartPtr &b) {
5738 a.mu->Lock();
5739 bool result = (a.val == b.val); // expected-warning {{reading variable 'val' requires holding mutex 'b.mu'}} \
5740 // expected-note {{found near match 'a.mu'}}
5741 a.mu->Unlock();
5742 return result;
5743 }
5744 };
5745
5746
5747 class SmartRedeclare {
5748 SmartPtr<Mutex> mu;
5749 int val GUARDED_BY(mu);
5750
5751 void test() EXCLUSIVE_LOCKS_REQUIRED(mu);
5752 void test2() EXCLUSIVE_LOCKS_REQUIRED(mu.get());
5753 void test3() EXCLUSIVE_LOCKS_REQUIRED(mu.get());
5754 };
5755
5756
5757 void SmartRedeclare::test() EXCLUSIVE_LOCKS_REQUIRED(mu.get()) {
5758 val = 0;
5759 }
5760
5761 void SmartRedeclare::test2() EXCLUSIVE_LOCKS_REQUIRED(mu) {
5762 val = 0;
5763 }
5764
5765 void SmartRedeclare::test3() {
5766 val = 0;
5767 }
5768
5769
5770 namespace CustomMutex {
5771
5772
5773 class LOCKABLE BaseMutex { };
5774 class DerivedMutex : public BaseMutex { };
5775
5776 void customLock(const BaseMutex *m) EXCLUSIVE_LOCK_FUNCTION(m);
5777 void customUnlock(const BaseMutex *m) UNLOCK_FUNCTION(m);
5778
5779 static struct DerivedMutex custMu;
5780
5781 static void doSomethingRequiringLock() EXCLUSIVE_LOCKS_REQUIRED(custMu) { }
5782
5783 void customTest() {
5784 customLock(reinterpret_cast<BaseMutex*>(&custMu)); // ignore casts
5785 doSomethingRequiringLock();
5786 customUnlock(reinterpret_cast<BaseMutex*>(&custMu));
5787 }
5788
5789 } // end namespace CustomMutex
5790
5791 } // end AttributeExpressionCornerCases
5792
5793
5794 namespace ScopedLockReturnedInvalid {
5795
5796 class Opaque;
5797
5798 Mutex* getMutex(Opaque* o) LOCK_RETURNED("");
5799
5800 void test(Opaque* o) {
5801 MutexLock lock(getMutex(o));
5802 }
5803
5804 } // end namespace ScopedLockReturnedInvalid
5805
5806
5807 namespace NegativeRequirements {
5808
5809 class Bar {
5810 Mutex mu;
5811 int a GUARDED_BY(mu);
5812
5813 public:
5814 void baz() EXCLUSIVE_LOCKS_REQUIRED(!mu) {
5815 mu.Lock();
5816 a = 0;
5817 mu.Unlock();
5818 }
5819 };
5820
5821
5822 class Foo {
5823 Mutex mu;
5824 int a GUARDED_BY(mu);
5825
5826 public:
5827 void foo() {
5828 mu.Lock(); // warning? needs !mu?
5829 baz(); // expected-warning {{cannot call function 'baz' while mutex 'mu' is held}}
5830 bar();
5831 mu.Unlock();
5832 }
5833
5834 void bar() {
5835 bar2(); // expected-warning {{calling function 'bar2' requires negative capability '!mu'}}
5836 }
5837
5838 void bar2() EXCLUSIVE_LOCKS_REQUIRED(!mu) {
5839 baz();
5840 }
5841
5842 void baz() EXCLUSIVE_LOCKS_REQUIRED(!mu) {
5843 mu.Lock();
5844 a = 0;
5845 mu.Unlock();
5846 }
5847
5848 void test() {
5849 Bar b;
5850 b.baz(); // no warning -- in different class.
5851 }
5852 };
5853
5854 } // end namespace NegativeRequirements
5855
5856
5857 namespace NegativeThreadRoles {
5858
5859 typedef int __attribute__((capability("role"))) ThreadRole;
5860
5861 void acquire(ThreadRole R) EXCLUSIVE_LOCK_FUNCTION(R) NO_THREAD_SAFETY_ANALYSIS {}
5862 void release(ThreadRole R) UNLOCK_FUNCTION(R) NO_THREAD_SAFETY_ANALYSIS {}
5863
5864 ThreadRole FlightControl, Logger;
5865
5866 extern void enque_log_msg(const char *msg);
5867 void log_msg(const char *msg) {
5868 enque_log_msg(msg);
5869 }
5870
5871 void dispatch_log(const char *msg) __attribute__((requires_capability(!FlightControl))) {}
5872 void dispatch_log2(const char *msg) __attribute__((requires_capability(Logger))) {}
5873
5874 void flight_control_entry(void) __attribute__((requires_capability(FlightControl))) {
5875 dispatch_log("wrong"); /* expected-warning {{cannot call function 'dispatch_log' while role 'FlightControl' is held}} */
5876 dispatch_log2("also wrong"); /* expected-warning {{calling function 'dispatch_log2' requires holding role 'Logger' exclusively}} */
5877 }
5878
5879 void spawn_fake_flight_control_thread(void) {
5880 acquire(FlightControl);
5881 flight_control_entry();
5882 release(FlightControl);
5883 }
5884
5885 extern const char *deque_log_msg(void) __attribute__((requires_capability(Logger)));
5886 void logger_entry(void) __attribute__((requires_capability(Logger)))
5887 __attribute__((requires_capability(!FlightControl))) {
5888 const char *msg;
5889
5890 while ((msg = deque_log_msg())) {
5891 dispatch_log(msg);
5892 }
5893 }
5894
5895 void spawn_fake_logger_thread(void) __attribute__((requires_capability(!FlightControl))) {
5896 acquire(Logger);
5897 logger_entry();
5898 release(Logger);
5899 }
5900
5901 int main(void) __attribute__((requires_capability(!FlightControl))) {
5902 spawn_fake_flight_control_thread();
5903 spawn_fake_logger_thread();
5904
5905 for (;;)
5906 ; /* Pretend to dispatch things. */
5907
5908 return 0;
5909 }
5910
5911 } // end namespace NegativeThreadRoles
5912
5913
5914 namespace AssertSharedExclusive {
5915
5916 void doSomething();
5917
5918 class Foo {
5919 Mutex mu;
5920 int a GUARDED_BY(mu);
5921
5922 void test() SHARED_LOCKS_REQUIRED(mu) {
5923 mu.AssertHeld();
5924 if (a > 0)
5925 doSomething();
5926 }
5927 };
5928
5929 } // end namespace AssertSharedExclusive
5930
5931
5932 namespace RangeBasedForAndReferences {
5933
5934 class Foo {
5935 struct MyStruct {
5936 int a;
5937 };
5938
5939 Mutex mu;
5940 int a GUARDED_BY(mu);
5941 MyContainer<int> cntr GUARDED_BY(mu);
5942 MyStruct s GUARDED_BY(mu);
5943 int arr[10] GUARDED_BY(mu);
5944
5945 void nonref_test() {
5946 int b = a; // expected-warning {{reading variable 'a' requires holding mutex 'mu'}}
5947 b = 0; // no warning
5948 }
5949
5950 void auto_test() {
5951 auto b = a; // expected-warning {{reading variable 'a' requires holding mutex 'mu'}}
5952 b = 0; // no warning
5953 auto &c = a; // no warning
5954 c = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu' exclusively}}
5955 }
5956
5957 void ref_test() {
5958 int &b = a;
5959 int &c = b;
5960 int &d = c;
5961 b = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu' exclusively}}
5962 c = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu' exclusively}}
5963 d = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu' exclusively}}
5964
5965 MyStruct &rs = s;
5966 rs.a = 0; // expected-warning {{writing variable 's' requires holding mutex 'mu' exclusively}}
5967
5968 int (&rarr)[10] = arr;
5969 rarr[2] = 0; // expected-warning {{writing variable 'arr' requires holding mutex 'mu' exclusively}}
5970 }
5971
5972 void ptr_test() {
5973 int *b = &a;
5974 *b = 0; // no expected warning yet
5975 }
5976
5977 void for_test() {
5978 int total = 0;
5979 for (int i : cntr) { // expected-warning2 {{reading variable 'cntr' requires holding mutex 'mu'}}
5980 total += i;
5981 }
5982 }
5983 };
5984
5985
5986 } // end namespace RangeBasedForAndReferences
5987
5988
5989
5990 namespace PassByRefTest {
5991
5992 class Foo {
5993 public:
5994 Foo() : a(0), b(0) { }
5995
5996 int a;
5997 int b;
5998
5999 void operator+(const Foo& f);
6000
6001 void operator[](const Foo& g);
6002
6003 void operator()();
6004 };
6005
6006 template<class T>
6007 T&& mymove(T& f);
6008
6009
6010 // test top-level functions
6011 void copy(Foo f);
6012 void write1(Foo& f);
6013 void write2(int a, Foo& f);
6014 void write3(Foo* f);
6015 void write4(Foo** f);
6016 void read1(const Foo& f);
6017 void read2(int a, const Foo& f);
6018 void read3(const Foo* f);
6019 void read4(Foo* const* f);
6020 void destroy(Foo&& f);
6021
6022 void operator/(const Foo& f, const Foo& g);
6023 void operator*(const Foo& f, const Foo& g);
6024
6025 // Test constructors.
6026 struct FooRead {
6027 FooRead(const Foo &);
6028 };
6029 struct FooWrite {
6030 FooWrite(Foo &);
6031 };
6032
6033 // Test variadic functions
6034 template<typename... T>
6035 void copyVariadic(T...) {}
6036 template<typename... T>
6037 void writeVariadic(T&...) {}
6038 template<typename... T>
6039 void readVariadic(const T&...) {}
6040
6041 void copyVariadicC(int, ...);
6042
6043 class Bar {
6044 public:
6045 Mutex mu;
6046 Foo foo GUARDED_BY(mu);
6047 Foo foo2 GUARDED_BY(mu);
6048 Foo* foop PT_GUARDED_BY(mu);
6049 Foo* foop2 GUARDED_BY(mu);
6050 SmartPtr<Foo> foosp PT_GUARDED_BY(mu);
6051
6052 // test methods.
6053 void mwrite1(Foo& f);
6054 void mwrite2(int a, Foo& f);
6055 void mwrite3(Foo* f);
6056 void mread1(const Foo& f);
6057 void mread2(int a, const Foo& f);
6058 void mread3(const Foo* f);
6059
6060 // static methods
6061 static void smwrite1(Foo& f);
6062 static void smwrite2(int a, Foo& f);
6063 static void smwrite3(Foo* f);
6064 static void smread1(const Foo& f);
6065 static void smread2(int a, const Foo& f);
6066 static void smread3(const Foo* f);
6067
6068 void operator<<(const Foo& f);
6069
6070 void test1() {
6071 copy(foo); // expected-warning {{reading variable 'foo' requires holding mutex 'mu'}}
6072 write1(foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}}
6073 write2(10, foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}}
6074 read1(foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}}
6075 read2(10, foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}}
6076 destroy(mymove(foo)); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}}
6077
6078 copyVariadic(foo); // expected-warning {{reading variable 'foo' requires holding mutex 'mu'}}
6079 readVariadic(foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}}
6080 writeVariadic(foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}}
6081 copyVariadicC(1, foo); // expected-warning {{reading variable 'foo' requires holding mutex 'mu'}}
6082
6083 FooRead reader(foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}}
6084 FooWrite writer(foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}}
6085
6086 mwrite1(foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}}
6087 mwrite2(10, foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}}
6088 mread1(foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}}
6089 mread2(10, foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}}
6090
6091 smwrite1(foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}}
6092 smwrite2(10, foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}}
6093 smread1(foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}}
6094 smread2(10, foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}}
6095
6096 foo + foo2; // expected-warning {{reading variable 'foo' requires holding mutex 'mu'}} \
6097 // expected-warning {{passing variable 'foo2' by reference requires holding mutex 'mu'}}
6098 foo / foo2; // expected-warning {{reading variable 'foo' requires holding mutex 'mu'}} \
6099 // expected-warning {{passing variable 'foo2' by reference requires holding mutex 'mu'}}
6100 foo * foo2; // expected-warning {{reading variable 'foo' requires holding mutex 'mu'}} \
6101 // expected-warning {{passing variable 'foo2' by reference requires holding mutex 'mu'}}
6102 foo[foo2]; // expected-warning {{reading variable 'foo' requires holding mutex 'mu'}} \
6103 // expected-warning {{passing variable 'foo2' by reference requires holding mutex 'mu'}}
6104 foo(); // expected-warning {{reading variable 'foo' requires holding mutex 'mu'}}
6105 (*this) << foo; // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}}
6106
6107 copy(*foop); // expected-warning {{reading the value pointed to by 'foop' requires holding mutex 'mu'}}
6108 write1(*foop); // expected-warning {{passing the value that 'foop' points to by reference requires holding mutex 'mu'}}
6109 write2(10, *foop); // expected-warning {{passing the value that 'foop' points to by reference requires holding mutex 'mu'}}
6110 read1(*foop); // expected-warning {{passing the value that 'foop' points to by reference requires holding mutex 'mu'}}
6111 read2(10, *foop); // expected-warning {{passing the value that 'foop' points to by reference requires holding mutex 'mu'}}
6112 destroy(mymove(*foop)); // expected-warning {{passing the value that 'foop' points to by reference requires holding mutex 'mu'}}
6113
6114 copy(*foosp); // expected-warning {{reading the value pointed to by 'foosp' requires holding mutex 'mu'}}
6115 write1(*foosp); // expected-warning {{reading the value pointed to by 'foosp' requires holding mutex 'mu'}}
6116 write2(10, *foosp); // expected-warning {{reading the value pointed to by 'foosp' requires holding mutex 'mu'}}
6117 read1(*foosp); // expected-warning {{reading the value pointed to by 'foosp' requires holding mutex 'mu'}}
6118 read2(10, *foosp); // expected-warning {{reading the value pointed to by 'foosp' requires holding mutex 'mu'}}
6119 destroy(mymove(*foosp)); // expected-warning {{reading the value pointed to by 'foosp' requires holding mutex 'mu'}}
6120
6121 // TODO -- these require better smart pointer handling.
6122 copy(*foosp.get());
6123 write1(*foosp.get());
6124 write2(10, *foosp.get());
6125 read1(*foosp.get());
6126 read2(10, *foosp.get());
6127 destroy(mymove(*foosp.get()));
6128 }
6129
6130 void test_pass_pointer() {
6131 (void)&foo; // no warning
6132 (void)foop; // no warning
6133
6134 write3(&foo); // expected-warning {{passing pointer to variable 'foo' requires holding mutex 'mu'}}
6135 write3(foop); // expected-warning {{passing pointer 'foop' requires holding mutex 'mu'}}
6136 write3(&*foop); // expected-warning {{passing pointer 'foop' requires holding mutex 'mu'}}
6137 write4((Foo **)&foo); // expected-warning {{passing pointer to variable 'foo' requires holding mutex 'mu'}}
6138 write4(&foop); // no warning
6139 read3(&foo); // expected-warning {{passing pointer to variable 'foo' requires holding mutex 'mu'}}
6140 read3(foop); // expected-warning {{passing pointer 'foop' requires holding mutex 'mu'}}
6141 read3(&*foop); // expected-warning {{passing pointer 'foop' requires holding mutex 'mu'}}
6142 read4((Foo **)&foo); // expected-warning {{passing pointer to variable 'foo' requires holding mutex 'mu'}}
6143 read4(&foop); // no warning
6144 mwrite3(&foo); // expected-warning {{passing pointer to variable 'foo' requires holding mutex 'mu'}}
6145 mwrite3(foop); // expected-warning {{passing pointer 'foop' requires holding mutex 'mu'}}
6146 mwrite3(&*foop); // expected-warning {{passing pointer 'foop' requires holding mutex 'mu'}}
6147 mread3(&foo); // expected-warning {{passing pointer to variable 'foo' requires holding mutex 'mu'}}
6148 mread3(foop); // expected-warning {{passing pointer 'foop' requires holding mutex 'mu'}}
6149 mread3(&*foop); // expected-warning {{passing pointer 'foop' requires holding mutex 'mu'}}
6150 smwrite3(&foo); // expected-warning {{passing pointer to variable 'foo' requires holding mutex 'mu'}}
6151 smwrite3(foop); // expected-warning {{passing pointer 'foop' requires holding mutex 'mu'}}
6152 smwrite3(&*foop); // expected-warning {{passing pointer 'foop' requires holding mutex 'mu'}}
6153 smread3(&foo); // expected-warning {{passing pointer to variable 'foo' requires holding mutex 'mu'}}
6154 smread3(foop); // expected-warning {{passing pointer 'foop' requires holding mutex 'mu'}}
6155 smread3(&*foop); // expected-warning {{passing pointer 'foop' requires holding mutex 'mu'}}
6156
6157 write3(foop2); // expected-warning {{reading variable 'foop2' requires holding mutex 'mu'}}
6158 write3(&*foop2); // expected-warning {{reading variable 'foop2' requires holding mutex 'mu'}}
6159 write4(&foop2); // expected-warning {{passing pointer to variable 'foop2' requires holding mutex 'mu'}}
6160 read3(foop2); // expected-warning {{reading variable 'foop2' requires holding mutex 'mu'}}
6161 read3(&*foop2); // expected-warning {{reading variable 'foop2' requires holding mutex 'mu'}}
6162 read4(&foop2); // expected-warning {{passing pointer to variable 'foop2' requires holding mutex 'mu'}}
6163 mwrite3(foop2); // expected-warning {{reading variable 'foop2' requires holding mutex 'mu'}}
6164 mwrite3(&*foop2); // expected-warning {{reading variable 'foop2' requires holding mutex 'mu'}}
6165 mread3(foop2); // expected-warning {{reading variable 'foop2' requires holding mutex 'mu'}}
6166 mread3(&*foop2); // expected-warning {{reading variable 'foop2' requires holding mutex 'mu'}}
6167 smwrite3(foop2); // expected-warning {{reading variable 'foop2' requires holding mutex 'mu'}}
6168 smwrite3(&*foop2); // expected-warning {{reading variable 'foop2' requires holding mutex 'mu'}}
6169 smread3(foop2); // expected-warning {{reading variable 'foop2' requires holding mutex 'mu'}}
6170 smread3(&*foop2); // expected-warning {{reading variable 'foop2' requires holding mutex 'mu'}}
6171
6172 mu.Lock();
6173 write3(&foo);
6174 write3(foop);
6175 write3(&*foop);
6176 write3(foop2);
6177 write4(&foop2);
6178 read3(&foo);
6179 read3(foop);
6180 read3(&*foop);
6181 read3(foop2);
6182 read4(&foop2);
6183 mwrite3(&foo);
6184 mwrite3(foop);
6185 mwrite3(&*foop);
6186 mwrite3(foop2);
6187 mread3(&foo);
6188 mread3(foop);
6189 mread3(&*foop);
6190 mread3(foop2);
6191 smwrite3(&foo);
6192 smwrite3(foop);
6193 smwrite3(&*foop);
6194 smwrite3(foop2);
6195 smread3(&foo);
6196 smread3(foop);
6197 smread3(&*foop);
6198 smread3(foop2);
6199 mu.Unlock();
6200
6201 mu.ReaderLock();
6202 write3(&foo);
6203 write3(foop);
6204 write3(&*foop);
6205 write3(foop2);
6206 write4(&foop2);
6207 read3(&foo);
6208 read3(foop);
6209 read3(&*foop);
6210 read3(foop2);
6211 read4(&foop2);
6212 mwrite3(&foo);
6213 mwrite3(foop);
6214 mwrite3(&*foop);
6215 mwrite3(foop2);
6216 mread3(&foo);
6217 mread3(foop);
6218 mread3(&*foop);
6219 mread3(foop2);
6220 smwrite3(&foo);
6221 smwrite3(foop);
6222 smwrite3(&*foop);
6223 smwrite3(foop2);
6224 smread3(&foo);
6225 smread3(foop);
6226 smread3(&*foop);
6227 smread3(foop2);
6228 mu.ReaderUnlock();
6229 }
6230 };
6231
6232 class Return {
6233 Mutex mu;
6234 Foo foo GUARDED_BY(mu);
6235 Foo* foo_ptr PT_GUARDED_BY(mu);
6236 Foo foo_depr GUARDED_VAR; // test deprecated attribute
6237 Foo* foo_ptr_depr PT_GUARDED_VAR; // test deprecated attribute
6238
6239 Foo returns_value_locked() {
6240 MutexLock lock(&mu);
6241 return foo;
6242 }
6243
6244 Foo returns_value_locks_required() EXCLUSIVE_LOCKS_REQUIRED(mu) {
6245 return foo;
6246 }
6247
6248 Foo returns_value_releases_lock_after_return() UNLOCK_FUNCTION(mu) {
6249 MutexLock lock(&mu, true);
6250 return foo;
6251 }
6252
6253 Foo returns_value_aquires_lock() EXCLUSIVE_LOCK_FUNCTION(mu) {
6254 mu.Lock();
6255 return foo;
6256 }
6257
6258 Foo returns_value_not_locked() {
6259 return foo; // expected-warning {{reading variable 'foo' requires holding mutex 'mu'}}
6260 }
6261
6262 Foo returns_value_releases_lock_before_return() UNLOCK_FUNCTION(mu) {
6263 mu.Unlock();
6264 return foo; // expected-warning {{reading variable 'foo' requires holding mutex 'mu'}}
6265 }
6266
6267 Foo &returns_ref_not_locked() {
6268 return foo; // expected-warning {{returning variable 'foo' by reference requires holding mutex 'mu'}}
6269 }
6270
6271 Foo &returns_ref_locked() {
6272 MutexLock lock(&mu);
6273 return foo; // expected-warning {{returning variable 'foo' by reference requires holding mutex 'mu'}}
6274 }
6275
6276 Foo &returns_ref_shared_locks_required() SHARED_LOCKS_REQUIRED(mu) {
6277 return foo; // expected-warning {{returning variable 'foo' by reference requires holding mutex 'mu' exclusively}}
6278 }
6279
6280 Foo &returns_ref_exclusive_locks_required() EXCLUSIVE_LOCKS_REQUIRED(mu) {
6281 return foo;
6282 }
6283
6284 Foo &returns_ref_releases_lock_after_return() UNLOCK_FUNCTION(mu) {
6285 MutexLock lock(&mu, true);
6286 return foo; // expected-warning {{returning variable 'foo' by reference requires holding mutex 'mu' exclusively}}
6287 }
6288
6289 Foo& returns_ref_releases_lock_before_return() UNLOCK_FUNCTION(mu) {
6290 mu.Unlock();
6291 return foo; // // expected-warning {{returning variable 'foo' by reference requires holding mutex 'mu' exclusively}}
6292 }
6293
6294 Foo &returns_ref_aquires_lock() EXCLUSIVE_LOCK_FUNCTION(mu) {
6295 mu.Lock();
6296 return foo;
6297 }
6298
6299 const Foo &returns_constref_shared_locks_required() SHARED_LOCKS_REQUIRED(mu) {
6300 return foo;
6301 }
6302
6303 Foo *returns_ptr_exclusive_locks_required() EXCLUSIVE_LOCKS_REQUIRED(mu) {
6304 return &foo;
6305 }
6306
6307 Foo *returns_pt_ptr_exclusive_locks_required() EXCLUSIVE_LOCKS_REQUIRED(mu) {
6308 return foo_ptr;
6309 }
6310
6311 Foo *returns_ptr_shared_locks_required() SHARED_LOCKS_REQUIRED(mu) {
6312 return &foo; // expected-warning {{returning pointer to variable 'foo' requires holding mutex 'mu' exclusively}}
6313 }
6314
6315 Foo *returns_pt_ptr_shared_locks_required() SHARED_LOCKS_REQUIRED(mu) {
6316 return foo_ptr; // expected-warning {{returning pointer 'foo_ptr' requires holding mutex 'mu' exclusively}}
6317 }
6318
6319 const Foo *returns_constptr_shared_locks_required() SHARED_LOCKS_REQUIRED(mu) {
6320 return &foo;
6321 }
6322
6323 const Foo *returns_pt_constptr_shared_locks_required() SHARED_LOCKS_REQUIRED(mu) {
6324 return foo_ptr;
6325 }
6326
6327 Foo *returns_ptr() {
6328 return &foo; // expected-warning {{returning pointer to variable 'foo' requires holding mutex 'mu' exclusively}}
6329 }
6330
6331 Foo *returns_pt_ptr() {
6332 return foo_ptr; // expected-warning {{returning pointer 'foo_ptr' requires holding mutex 'mu' exclusively}}
6333 }
6334
6335 Foo &returns_ref2() {
6336 return *foo_ptr; // expected-warning {{returning the value that 'foo_ptr' points to by reference requires holding mutex 'mu' exclusively}}
6337 }
6338
6339 Foo *returns_ptr_deprecated() {
6340 return &foo_depr; // expected-warning {{writing variable 'foo_depr' requires holding any mutex exclusively}}
6341 }
6342
6343 Foo *returns_pt_ptr_deprecated() {
6344 return foo_ptr_depr; // expected-warning {{writing the value pointed to by 'foo_ptr_depr' requires holding any mutex exclusively}}
6345 }
6346
6347 Foo &returns_ref_deprecated() {
6348 return *foo_ptr_depr; // expected-warning {{writing the value pointed to by 'foo_ptr_depr' requires holding any mutex exclusively}}
6349 }
6350
6351 // FIXME: Basic alias analysis would help catch cases like below.
6352 Foo *returns_ptr_alias() {
6353 mu.Lock();
6354 Foo *ret = &foo;
6355 mu.Unlock();
6356 return ret; // no warning (false negative)
6357 }
6358
6359 Foo *returns_pt_ptr_alias() {
6360 mu.Lock();
6361 Foo *ret = foo_ptr;
6362 mu.Unlock();
6363 return ret; // no warning (false negative)
6364 }
6365
6366 Foo &returns_ref2_alias() {
6367 mu.Lock();
6368 Foo *ret = foo_ptr;
6369 mu.Unlock();
6370 return *ret; // no warning (false negative)
6371 }
6372 };
6373
6374
6375 } // end namespace PassByRefTest
6376
6377
6378 namespace AcquiredBeforeAfterText {
6379
6380 class Foo {
6381 Mutex mu1 ACQUIRED_BEFORE(mu2, mu3);
6382 Mutex mu2;
6383 Mutex mu3;
6384
6385 void test1() {
6386 mu1.Lock();
6387 mu2.Lock();
6388 mu3.Lock();
6389
6390 mu3.Unlock();
6391 mu2.Unlock();
6392 mu1.Unlock();
6393 }
6394
6395 void test2() {
6396 mu2.Lock();
6397 mu1.Lock(); // expected-warning {{mutex 'mu1' must be acquired before 'mu2'}}
6398 mu1.Unlock();
6399 mu2.Unlock();
6400 }
6401
6402 void test3() {
6403 mu3.Lock();
6404 mu1.Lock(); // expected-warning {{mutex 'mu1' must be acquired before 'mu3'}}
6405 mu1.Unlock();
6406 mu3.Unlock();
6407 }
6408
6409 void test4() EXCLUSIVE_LOCKS_REQUIRED(mu1) {
6410 mu2.Lock();
6411 mu2.Unlock();
6412 }
6413
6414 void test5() EXCLUSIVE_LOCKS_REQUIRED(mu2) {
6415 mu1.Lock(); // expected-warning {{mutex 'mu1' must be acquired before 'mu2'}}
6416 mu1.Unlock();
6417 }
6418
6419 void test6() EXCLUSIVE_LOCKS_REQUIRED(mu2) {
6420 mu1.AssertHeld();
6421 }
6422
6423 void test7() EXCLUSIVE_LOCKS_REQUIRED(mu1, mu2, mu3) { }
6424
6425 void test8() EXCLUSIVE_LOCKS_REQUIRED(mu3, mu2, mu1) { }
6426 };
6427
6428
6429 class Foo2 {
6430 Mutex mu1;
6431 Mutex mu2 ACQUIRED_AFTER(mu1);
6432 Mutex mu3 ACQUIRED_AFTER(mu1);
6433
6434 void test1() {
6435 mu1.Lock();
6436 mu2.Lock();
6437 mu3.Lock();
6438
6439 mu3.Unlock();
6440 mu2.Unlock();
6441 mu1.Unlock();
6442 }
6443
6444 void test2() {
6445 mu2.Lock();
6446 mu1.Lock(); // expected-warning {{mutex 'mu1' must be acquired before 'mu2'}}
6447 mu1.Unlock();
6448 mu2.Unlock();
6449 }
6450
6451 void test3() {
6452 mu3.Lock();
6453 mu1.Lock(); // expected-warning {{mutex 'mu1' must be acquired before 'mu3'}}
6454 mu1.Unlock();
6455 mu3.Unlock();
6456 }
6457 };
6458
6459
6460 class Foo3 {
6461 Mutex mu1 ACQUIRED_BEFORE(mu2);
6462 Mutex mu2;
6463 Mutex mu3 ACQUIRED_AFTER(mu2) ACQUIRED_BEFORE(mu4);
6464 Mutex mu4;
6465
6466 void test1() {
6467 mu1.Lock();
6468 mu2.Lock();
6469 mu3.Lock();
6470 mu4.Lock();
6471
6472 mu4.Unlock();
6473 mu3.Unlock();
6474 mu2.Unlock();
6475 mu1.Unlock();
6476 }
6477
6478 void test2() {
6479 mu4.Lock();
6480 mu2.Lock(); // expected-warning {{mutex 'mu2' must be acquired before 'mu4'}}
6481
6482 mu2.Unlock();
6483 mu4.Unlock();
6484 }
6485
6486 void test3() {
6487 mu4.Lock();
6488 mu1.Lock(); // expected-warning {{mutex 'mu1' must be acquired before 'mu4'}}
6489
6490 mu1.Unlock();
6491 mu4.Unlock();
6492 }
6493
6494 void test4() {
6495 mu3.Lock();
6496 mu1.Lock(); // expected-warning {{mutex 'mu1' must be acquired before 'mu3'}}
6497
6498 mu1.Unlock();
6499 mu3.Unlock();
6500 }
6501 };
6502
6503
6504 // Test transitive DAG traversal with AFTER
6505 class Foo4 {
6506 Mutex mu1;
6507 Mutex mu2 ACQUIRED_AFTER(mu1);
6508 Mutex mu3 ACQUIRED_AFTER(mu1);
6509 Mutex mu4 ACQUIRED_AFTER(mu2, mu3);
6510 Mutex mu5 ACQUIRED_AFTER(mu4);
6511 Mutex mu6 ACQUIRED_AFTER(mu4);
6512 Mutex mu7 ACQUIRED_AFTER(mu5, mu6);
6513 Mutex mu8 ACQUIRED_AFTER(mu7);
6514
6515 void test() {
6516 mu8.Lock();
6517 mu1.Lock(); // expected-warning {{mutex 'mu1' must be acquired before 'mu8'}}
6518 mu1.Unlock();
6519 mu8.Unlock();
6520 }
6521 };
6522
6523
6524 // Test transitive DAG traversal with BEFORE
6525 class Foo5 {
6526 Mutex mu1 ACQUIRED_BEFORE(mu2, mu3);
6527 Mutex mu2 ACQUIRED_BEFORE(mu4);
6528 Mutex mu3 ACQUIRED_BEFORE(mu4);
6529 Mutex mu4 ACQUIRED_BEFORE(mu5, mu6);
6530 Mutex mu5 ACQUIRED_BEFORE(mu7);
6531 Mutex mu6 ACQUIRED_BEFORE(mu7);
6532 Mutex mu7 ACQUIRED_BEFORE(mu8);
6533 Mutex mu8;
6534
6535 void test() {
6536 mu8.Lock();
6537 mu1.Lock(); // expected-warning {{mutex 'mu1' must be acquired before 'mu8'}}
6538 mu1.Unlock();
6539 mu8.Unlock();
6540 }
6541 };
6542
6543
6544 class Foo6 {
6545 Mutex mu1 ACQUIRED_AFTER(mu3); // expected-warning {{cycle in acquired_before/after dependencies, starting with 'mu1'}}
6546 Mutex mu2 ACQUIRED_AFTER(mu1); // expected-warning {{cycle in acquired_before/after dependencies, starting with 'mu2'}}
6547 Mutex mu3 ACQUIRED_AFTER(mu2); // expected-warning {{cycle in acquired_before/after dependencies, starting with 'mu3'}}
6548
6549 Mutex mu_b ACQUIRED_BEFORE(mu_b); // expected-warning {{cycle in acquired_before/after dependencies, starting with 'mu_b'}}
6550 Mutex mu_a ACQUIRED_AFTER(mu_a); // expected-warning {{cycle in acquired_before/after dependencies, starting with 'mu_a'}}
6551
6552 void test0() {
6553 mu_a.Lock();
6554 mu_b.Lock();
6555 mu_b.Unlock();
6556 mu_a.Unlock();
6557 }
6558
6559 void test1a() {
6560 mu1.Lock();
6561 mu1.Unlock();
6562 }
6563
6564 void test1b() {
6565 mu1.Lock();
6566 mu_a.Lock();
6567 mu_b.Lock();
6568 mu_b.Unlock();
6569 mu_a.Unlock();
6570 mu1.Unlock();
6571 }
6572
6573 void test() {
6574 mu2.Lock();
6575 mu2.Unlock();
6576 }
6577
6578 void test3() {
6579 mu3.Lock();
6580 mu3.Unlock();
6581 }
6582 };
6583
6584 } // end namespace AcquiredBeforeAfterTest
6585
6586
6587 namespace ScopedAdoptTest {
6588
6589 class Foo {
6590 Mutex mu;
6591 int a GUARDED_BY(mu);
6592 int b;
6593
6594 void test1() EXCLUSIVE_UNLOCK_FUNCTION(mu) {
6595 MutexLock slock(&mu, true);
6596 a = 0;
6597 }
6598
6599 void test2() SHARED_UNLOCK_FUNCTION(mu) {
6600 ReaderMutexLock slock(&mu, true);
6601 b = a;
6602 }
6603
6604 void test3() EXCLUSIVE_LOCKS_REQUIRED(mu) { // expected-note {{mutex acquired here}}
6605 MutexLock slock(&mu, true);
6606 a = 0;
6607 } // expected-warning {{expecting mutex 'mu' to be held at the end of function}}
6608
6609 void test4() SHARED_LOCKS_REQUIRED(mu) { // expected-note {{mutex acquired here}}
6610 ReaderMutexLock slock(&mu, true);
6611 b = a;
6612 } // expected-warning {{expecting mutex 'mu' to be held at the end of function}}
6613
6614 };
6615
6616 } // end namespace ScopedAdoptTest
6617
6618
6619 namespace TestReferenceNoThreadSafetyAnalysis {
6620
6621 #define TS_UNCHECKED_READ(x) ts_unchecked_read(x)
6622
6623 // Takes a reference to a guarded data member, and returns an unguarded
6624 // reference.
6625 template <class T>
6626 inline const T& ts_unchecked_read(const T& v) NO_THREAD_SAFETY_ANALYSIS {
6627 return v;
6628 }
6629
6630 template <class T>
6631 inline T& ts_unchecked_read(T& v) NO_THREAD_SAFETY_ANALYSIS {
6632 return v;
6633 }
6634
6635
6636 class Foo {
6637 public:
6638 Foo(): a(0) { }
6639
6640 int a;
6641 };
6642
6643
6644 class Bar {
6645 public:
6646 Bar() : a(0) { }
6647
6648 Mutex mu;
6649 int a GUARDED_BY(mu);
6650 Foo foo GUARDED_BY(mu);
6651 };
6652
6653
6654 void test() {
6655 Bar bar;
6656 const Bar cbar;
6657
6658 int a = TS_UNCHECKED_READ(bar.a); // nowarn
6659 TS_UNCHECKED_READ(bar.a) = 1; // nowarn
6660
6661 int b = TS_UNCHECKED_READ(bar.foo).a; // nowarn
6662 TS_UNCHECKED_READ(bar.foo).a = 1; // nowarn
6663
6664 int c = TS_UNCHECKED_READ(cbar.a); // nowarn
6665 }
6666
6667 #undef TS_UNCHECKED_READ
6668
6669 } // end namespace TestReferenceNoThreadSafetyAnalysis
6670
6671
6672 namespace GlobalAcquiredBeforeAfterTest {
6673
6674 Mutex mu1;
6675 Mutex mu2 ACQUIRED_AFTER(mu1);
6676
6677 void test3() {
6678 mu2.Lock();
6679 mu1.Lock(); // expected-warning {{mutex 'mu1' must be acquired before 'mu2'}}
6680 mu1.Unlock();
6681 mu2.Unlock();
6682 }
6683
6684 } // end namespace GlobalAcquiredBeforeAfterTest
6685
6686
6687 namespace LifetimeExtensionText {
6688
6689 struct Holder {
6690 virtual ~Holder() throw() {}
6691 int i = 0;
6692 };
6693
6694 void test() {
6695 // Should not crash.
6696 const auto &value = Holder().i;
6697 }
6698
6699 } // end namespace LifetimeExtensionTest
6700
6701
6702 namespace LockableUnions {
6703
6704 union LOCKABLE MutexUnion {
6705 int a;
6706 char* b;
6707
6708 void Lock() EXCLUSIVE_LOCK_FUNCTION();
6709 void Unlock() UNLOCK_FUNCTION();
6710 };
6711
6712 MutexUnion muun2;
6713 MutexUnion muun1 ACQUIRED_BEFORE(muun2);
6714
6715 void test() {
6716 muun2.Lock();
6717 muun1.Lock(); // expected-warning {{mutex 'muun1' must be acquired before 'muun2'}}
6718 muun1.Unlock();
6719 muun2.Unlock();
6720 }
6721
6722 } // end namespace LockableUnions
6723
6724 // This used to crash.
6725 class acquired_before_empty_str {
6726 void WaitUntilSpaceAvailable() {
6727 lock_.ReaderLock(); // expected-note {{acquired here}}
6728 } // expected-warning {{mutex 'lock_' is still held at the end of function}}
6729 Mutex lock_ ACQUIRED_BEFORE("");
6730 };
6731
6732 namespace PR34800 {
6733 struct A {
6734 operator int() const;
6735 };
6736 struct B {
6737 bool g() __attribute__((locks_excluded(h))); // expected-warning {{'locks_excluded' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'int'}}
6738 int h;
6739 };
6740 struct C {
6741 B *operator[](int);
6742 };
6743 C c;
6744 void f() { c[A()]->g(); }
6745 } // namespace PR34800
6746
6747 #ifdef __cpp_guaranteed_copy_elision
6748
6749 namespace ReturnScopedLockable {
6750
6751 class Object {
6752 public:
6753 MutexLock lock() EXCLUSIVE_LOCK_FUNCTION(mutex) {
6754 return MutexLock(&mutex);
6755 }
6756
6757 ReaderMutexLock lockShared() SHARED_LOCK_FUNCTION(mutex) {
6758 return ReaderMutexLock(&mutex);
6759 }
6760
6761 MutexLock adopt() EXCLUSIVE_LOCKS_REQUIRED(mutex) {
6762 return MutexLock(&mutex, true);
6763 }
6764
6765 ReaderMutexLock adoptShared() SHARED_LOCKS_REQUIRED(mutex) {
6766 return ReaderMutexLock(&mutex, true);
6767 }
6768
6769 int x GUARDED_BY(mutex);
6770 void needsLock() EXCLUSIVE_LOCKS_REQUIRED(mutex);
6771
6772 void testInside() {
6773 MutexLock scope = lock();
6774 x = 1;
6775 needsLock();
6776 }
6777
6778 Mutex mutex;
6779 };
6780
6781 Object obj;
6782
6783 void testLock() {
6784 MutexLock scope = obj.lock();
6785 obj.x = 1;
6786 obj.needsLock();
6787 }
6788
6789 int testSharedLock() {
6790 ReaderMutexLock scope = obj.lockShared();
6791 obj.x = 1; // expected-warning {{writing variable 'x' requires holding mutex 'obj.mutex' exclusively}}
6792 return obj.x;
6793 }
6794
6795 void testAdopt() {
6796 obj.mutex.Lock();
6797 MutexLock scope = obj.adopt();
6798 obj.x = 1;
6799 }
6800
6801 int testAdoptShared() {
6802 obj.mutex.Lock();
6803 ReaderMutexLock scope = obj.adoptShared();
6804 obj.x = 1;
6805 return obj.x;
6806 }
6807
6808 } // namespace ReturnScopedLockable
6809
6810 #endif // __cpp_guaranteed_copy_elision
6811
6812 namespace PR38640 {
6813 void f() {
6814 // Self-referencing assignment previously caused an infinite loop when thread
6815 // safety analysis was enabled.
6816 int &i = i; // expected-warning {{reference 'i' is not yet bound to a value when used within its own initialization}}
6817 }
6818 } // namespace PR38640
6819
6820 namespace Derived_Smart_Pointer {
6821 template <class T>
6822 class SmartPtr_Derived : public SmartPtr<T> {};
6823
6824 class Foo {
6825 public:
6826 SmartPtr_Derived<Mutex> mu_;
6827 int a GUARDED_BY(mu_);
6828 int b GUARDED_BY(mu_.get());
6829 int c GUARDED_BY(*mu_);
6830
6831 void Lock() EXCLUSIVE_LOCK_FUNCTION(mu_);
6832 void Unlock() UNLOCK_FUNCTION(mu_);
6833
6834 void test0() {
6835 a = 1; // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}}
6836 b = 1; // expected-warning {{writing variable 'b' requires holding mutex 'mu_' exclusively}}
6837 c = 1; // expected-warning {{writing variable 'c' requires holding mutex 'mu_' exclusively}}
6838 }
6839
6840 void test1() {
6841 Lock();
6842 a = 1;
6843 b = 1;
6844 c = 1;
6845 Unlock();
6846 }
6847 };
6848
6849 class Bar {
6850 SmartPtr_Derived<Foo> foo;
6851
6852 void test0() {
6853 foo->a = 1; // expected-warning {{writing variable 'a' requires holding mutex 'foo->mu_' exclusively}}
6854 (*foo).b = 1; // expected-warning {{writing variable 'b' requires holding mutex 'foo->mu_' exclusively}}
6855 foo.get()->c = 1; // expected-warning {{writing variable 'c' requires holding mutex 'foo->mu_' exclusively}}
6856 }
6857
6858 void test1() {
6859 foo->Lock();
6860 foo->a = 1;
6861 foo->Unlock();
6862
6863 foo->mu_->Lock();
6864 foo->b = 1;
6865 foo->mu_->Unlock();
6866
6867 MutexLock lock(foo->mu_.get());
6868 foo->c = 1;
6869 }
6870 };
6871
6872 class PointerGuard {
6873 Mutex mu1;
6874 Mutex mu2;
6875 SmartPtr_Derived<int> i GUARDED_BY(mu1) PT_GUARDED_BY(mu2);
6876
6877 void test0() {
6878 i.get(); // expected-warning {{reading variable 'i' requires holding mutex 'mu1'}}
6879 *i = 2; // expected-warning {{reading variable 'i' requires holding mutex 'mu1'}} \
6880 // expected-warning {{reading the value pointed to by 'i' requires holding mutex 'mu2'}}
6881
6882 }
6883
6884 void test1() {
6885 mu1.Lock();
6886
6887 i.get();
6888 *i = 2; // expected-warning {{reading the value pointed to by 'i' requires holding mutex 'mu2'}}
6889
6890 mu1.Unlock();
6891 }
6892
6893 void test2() {
6894 mu2.Lock();
6895
6896 i.get(); // expected-warning {{reading variable 'i' requires holding mutex 'mu1'}}
6897 *i = 2; // expected-warning {{reading variable 'i' requires holding mutex 'mu1'}}
6898
6899 mu2.Unlock();
6900 }
6901
6902 void test3() {
6903 mu1.Lock();
6904 mu2.Lock();
6905
6906 i.get();
6907 *i = 2;
6908
6909 mu2.Unlock();
6910 mu1.Unlock();
6911 }
6912 };
6913 } // namespace Derived_Smart_Pointer
6914
6915 // Test for capabilities that are heap-allocated and stored in static variables.
6916 namespace FunctionStaticVariable {
6917 struct Data {
6918 Mutex mu;
6919 int x GUARDED_BY(mu);
6920 };
6921
6922 void testStaticVariable() {
6923 }
6924
6925 void testHeapAllocation() {
6926 static Data *d = new Data;
6927 d->mu.Lock();
6928 d->x = 5;
6929 d->mu.Unlock();
6930 }
6931
6932 void testHeapAllocationBug() {
6933 static auto *d = new Data;
6934 d->x = 10; // expected-warning{{writing variable 'x' requires holding mutex 'd->mu' exclusively}}
6935 }
6936
6937 void testHeapAllocationScopedLock() {
6938 static Mutex *mu = new Mutex;
6939 MutexLock lock(mu);
6940 }
6941 } // namespace FunctionStaticVariable
6942
6943 namespace Reentrancy {
6944
6945 class LOCKABLE REENTRANT_CAPABILITY ReentrantMutex {
6946 public:
6947 void Lock() EXCLUSIVE_LOCK_FUNCTION();
6948 void ReaderLock() SHARED_LOCK_FUNCTION();
6949 void Unlock() UNLOCK_FUNCTION();
6950 void ExclusiveUnlock() EXCLUSIVE_UNLOCK_FUNCTION();
6951 void ReaderUnlock() SHARED_UNLOCK_FUNCTION();
6952 bool TryLock() EXCLUSIVE_TRYLOCK_FUNCTION(true);
6953 bool ReaderTryLock() SHARED_TRYLOCK_FUNCTION(true);
6954
6955 // for negative capabilities
6956 const ReentrantMutex& operator!() const { return *this; }
6957
6958 void AssertHeld() ASSERT_EXCLUSIVE_LOCK();
6959 void AssertReaderHeld() ASSERT_SHARED_LOCK();
6960 };
6961
6962 class SCOPED_LOCKABLE ReentrantMutexLock {
6963 public:
6964 ReentrantMutexLock(ReentrantMutex *mu) EXCLUSIVE_LOCK_FUNCTION(mu);
6965 ~ReentrantMutexLock() UNLOCK_FUNCTION();
6966 };
6967
6968 class SCOPED_LOCKABLE ReentrantReaderMutexLock {
6969 public:
6970 ReentrantReaderMutexLock(ReentrantMutex *mu) SHARED_LOCK_FUNCTION(mu);
6971 ~ReentrantReaderMutexLock() UNLOCK_FUNCTION();
6972 };
6973
6974 class SCOPED_LOCKABLE RelockableReentrantMutexLock {
6975 public:
6976 RelockableReentrantMutexLock(ReentrantMutex *mu) EXCLUSIVE_LOCK_FUNCTION(mu);
6977 ~RelockableReentrantMutexLock() EXCLUSIVE_UNLOCK_FUNCTION();
6978
6979 void Lock() EXCLUSIVE_LOCK_FUNCTION();
6980 void Unlock() UNLOCK_FUNCTION();
6981 };
6982
6983 ReentrantMutex rmu;
6984 int guard_var __attribute__((guarded_var)) = 0;
6985 int guardby_var __attribute__((guarded_by(rmu))) = 0;
6986
6987 void testReentrantMany() {
6988 rmu.Lock();
6989 rmu.Lock();
6990 rmu.Lock();
6991 rmu.Lock();
6992 rmu.Lock();
6993 rmu.Lock();
6994 rmu.Lock();
6995 rmu.Lock();
6996 rmu.Unlock();
6997 rmu.Unlock();
6998 rmu.Unlock();
6999 rmu.Unlock();
7000 rmu.Unlock();
7001 rmu.Unlock();
7002 rmu.Unlock();
7003 rmu.Unlock();
7004 }
7005
7006 void testReentrantManyReader() {
7007 rmu.ReaderLock();
7008 rmu.ReaderLock();
7009 rmu.ReaderLock();
7010 rmu.ReaderLock();
7011 rmu.ReaderLock();
7012 rmu.ReaderLock();
7013 rmu.ReaderLock();
7014 rmu.ReaderLock();
7015 rmu.ReaderUnlock();
7016 rmu.ReaderUnlock();
7017 rmu.ReaderUnlock();
7018 rmu.ReaderUnlock();
7019 rmu.ReaderUnlock();
7020 rmu.ReaderUnlock();
7021 rmu.ReaderUnlock();
7022 rmu.ReaderUnlock();
7023 }
7024
7025 void testReentrantLock1() {
7026 rmu.Lock();
7027 guard_var = 2;
7028 rmu.Lock();
7029 guard_var = 2;
7030 rmu.Unlock();
7031 guard_var = 2;
7032 rmu.Unlock();
7033 guard_var = 2; // expected-warning{{writing variable 'guard_var' requires holding any mutex exclusively}}
7034 }
7035
7036 void testReentrantReaderLock1() {
7037 rmu.ReaderLock();
7038 int x = guard_var;
7039 rmu.ReaderLock();
7040 int y = guard_var;
7041 rmu.ReaderUnlock();
7042 int z = guard_var;
7043 rmu.ReaderUnlock();
7044 int a = guard_var; // expected-warning{{reading variable 'guard_var' requires holding any mutex}}
7045 }
7046
7047 void testReentrantLock2() {
7048 rmu.Lock();
7049 guardby_var = 2;
7050 rmu.Lock();
7051 guardby_var = 2;
7052 rmu.Unlock();
7053 guardby_var = 2;
7054 rmu.Unlock();
7055 guardby_var = 2; // expected-warning{{writing variable 'guardby_var' requires holding mutex 'rmu' exclusively}}
7056 }
7057
7058 void testReentrantReaderLock2() {
7059 rmu.ReaderLock();
7060 int x = guardby_var;
7061 rmu.ReaderLock();
7062 int y = guardby_var;
7063 rmu.ReaderUnlock();
7064 int z = guardby_var;
7065 rmu.ReaderUnlock();
7066 int a = guardby_var; // expected-warning{{reading variable 'guardby_var' requires holding mutex 'rmu'}}
7067 }
7068
7069 void testReentrantTryLock1() {
7070 if (rmu.TryLock()) {
7071 guardby_var = 1;
7072 if (rmu.TryLock()) {
7073 guardby_var = 1;
7074 rmu.Unlock();
7075 }
7076 guardby_var = 1;
7077 rmu.Unlock();
7078 }
7079 guardby_var = 1; // expected-warning{{writing variable 'guardby_var' requires holding mutex 'rmu' exclusively}}
7080 }
7081
7082 void testReentrantTryLock2() {
7083 rmu.Lock();
7084 guardby_var = 1;
7085 if (rmu.TryLock()) {
7086 guardby_var = 1;
7087 rmu.Unlock();
7088 }
7089 guardby_var = 1;
7090 rmu.Unlock();
7091 guardby_var = 1; // expected-warning{{writing variable 'guardby_var' requires holding mutex 'rmu' exclusively}}
7092 }
7093
7094 void testReentrantNotHeld() {
7095 rmu.Unlock(); // \
7096 // expected-warning{{releasing mutex 'rmu' that was not held}}
7097 }
7098
7099 void testReentrantMissingUnlock() {
7100 rmu.Lock(); // expected-note{{mutex acquired here}}
7101 rmu.Lock(); // reenter
7102 rmu.Unlock();
7103 } // expected-warning{{mutex 'rmu' is still held at the end of function}}
7104
7105 // Acquiring the same capability with different access privileges is not
7106 // considered reentrant.
7107 void testMixedSharedExclusive() {
7108 rmu.ReaderLock(); // expected-note{{mutex acquired here}}
7109 rmu.Lock(); // expected-warning{{acquiring mutex 'rmu' that is already held}}
7110 rmu.Unlock(); // expected-note{{mutex released here}}
7111 rmu.ReaderUnlock(); // expected-warning{{releasing mutex 'rmu' that was not held}}
7112 }
7113
7114 void testReentrantIntersection() {
7115 rmu.Lock();
7116 if (guardby_var) {
7117 rmu.Lock();
7118 rmu.Lock();
7119 rmu.Unlock();
7120 } else {
7121 rmu.Lock();
7122 guardby_var = 1;
7123 }
7124 guardby_var = 1;
7125 rmu.Unlock();
7126 guardby_var = 1;
7127 rmu.Unlock();
7128 guardby_var = 1; // expected-warning{{writing variable 'guardby_var' requires holding mutex 'rmu' exclusively}}
7129 }
7130
7131 void testReentrantIntersectionBad() {
7132 rmu.Lock(); // expected-note{{mutex acquired here}}
7133 if (guardby_var) {
7134 rmu.Lock();
7135 rmu.Lock();
7136 } else {
7137 rmu.Lock();
7138 guardby_var = 1;
7139 }
7140 guardby_var = 1; // expected-warning{{mutex 'rmu' is not held on every path through here with equal reentrancy depth}}
7141 rmu.Unlock();
7142 guardby_var = 1;
7143 rmu.Unlock();
7144 guardby_var = 1;
7145 rmu.Unlock();
7146 }
7147
7148 void testReentrantLoopBad() {
7149 rmu.Lock(); // expected-note{{mutex acquired here}}
7150 while (guardby_var) { // expected-warning{{expecting mutex 'rmu' to be held at start of each loop with equal reentrancy depth}}
7151 rmu.Lock();
7152 }
7153 rmu.Unlock();
7154 }
7155
7156 void testLocksRequiredReentrant() EXCLUSIVE_LOCKS_REQUIRED(rmu) {
7157 guardby_var = 1;
7158 rmu.Lock();
7159 rmu.Lock();
7160 guardby_var = 1;
7161 rmu.Unlock();
7162 rmu.Unlock();
7163 guardby_var = 1;
7164 }
7165
7166 void testAssertReentrant() {
7167 rmu.AssertHeld();
7168 guardby_var = 1;
7169 rmu.Lock();
7170 guardby_var = 1;
7171 rmu.Unlock();
7172 guardby_var = 1;
7173 }
7174
7175 void testAssertReaderReentrant() {
7176 rmu.AssertReaderHeld();
7177 int x = guardby_var;
7178 rmu.ReaderLock();
7179 int y = guardby_var;
7180 rmu.ReaderUnlock();
7181 int z = guardby_var;
7182 }
7183
7184 struct TestScopedReentrantLockable {
7185 ReentrantMutex mu1;
7186 ReentrantMutex mu2;
7187 int a __attribute__((guarded_by(mu1)));
7188 int b __attribute__((guarded_by(mu2)));
7189
7190 bool getBool();
7191
7192 void foo1() {
7193 ReentrantMutexLock mulock1(&mu1);
7194 a = 5;
7195 ReentrantMutexLock mulock2(&mu1);
7196 a = 5;
7197 }
7198
7199 void foo2() {
7200 ReentrantMutexLock mulock1(&mu1);
7201 a = 5;
7202 mu1.Lock();
7203 a = 5;
7204 mu1.Unlock();
7205 a = 5;
7206 }
7207
7208 #ifdef __cpp_guaranteed_copy_elision
7209 void const_lock() {
7210 const ReentrantMutexLock mulock1 = ReentrantMutexLock(&mu1);
7211 a = 5;
7212 const ReentrantMutexLock mulock2 = ReentrantMutexLock(&mu1);
7213 a = 3;
7214 }
7215 #endif
7216
7217 void temporary() {
7218 ReentrantMutexLock{&mu1}, a = 1, ReentrantMutexLock{&mu1}, a = 5;
7219 }
7220
7221 void lifetime_extension() {
7222 const ReentrantMutexLock &mulock1 = ReentrantMutexLock(&mu1);
7223 a = 5;
7224 const ReentrantMutexLock &mulock2 = ReentrantMutexLock(&mu1);
7225 a = 5;
7226 }
7227
7228 void foo3() {
7229 ReentrantReaderMutexLock mulock1(&mu1);
7230 if (getBool()) {
7231 ReentrantMutexLock mulock2a(&mu2);
7232 b = a + 1;
7233 }
7234 else {
7235 ReentrantMutexLock mulock2b(&mu2);
7236 b = a + 2;
7237 }
7238 }
7239
7240 void foo4() {
7241 ReentrantMutexLock mulock_a(&mu1);
7242 ReentrantMutexLock mulock_b(&mu1);
7243 }
7244
7245 void temporary_double_lock() {
7246 ReentrantMutexLock mulock_a(&mu1);
7247 ReentrantMutexLock{&mu1};
7248 }
7249
7250 void foo5() {
7251 ReentrantMutexLock mulock1(&mu1), mulock2(&mu2);
7252 {
7253 ReentrantMutexLock mulock3(&mu1), mulock4(&mu2);
7254 a = b+1;
7255 }
7256 b = a+1;
7257 }
7258 };
7259
7260 void scopedDoubleUnlock() {
7261 RelockableReentrantMutexLock scope(&rmu);
7262 scope.Unlock(); // expected-note{{mutex released here}}
7263 scope.Unlock(); // expected-warning {{releasing mutex 'rmu' that was not held}}
7264 }
7265
7266 void scopedDoubleLock1() {
7267 RelockableReentrantMutexLock scope(&rmu);
7268 scope.Lock();
7269 scope.Unlock();
7270 }
7271
7272 void scopedDoubleLock2() {
7273 RelockableReentrantMutexLock scope(&rmu);
7274 scope.Unlock();
7275 scope.Lock();
7276 scope.Lock();
7277 scope.Unlock();
7278 }
7279
7280 typedef int __attribute__((capability("bitlock"))) REENTRANT_CAPABILITY *bitlock_t;
7281 void bit_lock(bitlock_t l) EXCLUSIVE_LOCK_FUNCTION(l);
7282 void bit_unlock(bitlock_t l) UNLOCK_FUNCTION(l);
7283 bitlock_t bl;
7284 void testReentrantTypedef() {
7285 bit_lock(bl);
7286 bit_lock(bl);
7287 bit_unlock(bl);
7288 bit_unlock(bl);
7289 }
7290
7291 class TestNegativeWithReentrantMutex {
7292 ReentrantMutex rmu;
7293 int a GUARDED_BY(rmu);
7294
7295 public:
7296 void baz() EXCLUSIVE_LOCKS_REQUIRED(!rmu) {
7297 rmu.Lock();
7298 rmu.Lock();
7299 a = 0;
7300 rmu.Unlock();
7301 rmu.Unlock();
7302 }
7303 };
7304
7305 } // namespace Reentrancy
7306
7307 // Tests for tracking aliases of capabilities.
7308 namespace CapabilityAliases {
7309 struct Foo {
7310 Mutex mu;
7311 int data GUARDED_BY(mu);
7312 };
7313
7314 Foo *returnsFoo();
7315 Foo *returnsFoo(Foo *foo);
7316 void locksRequired(Foo *foo) EXCLUSIVE_LOCKS_REQUIRED(foo->mu);
7317 void escapeAlias(int a, Foo *&ptr);
7318 void escapeAlias(int b, Foo **ptr);
7319 void passByConstRef(Foo* const& ptr);
7320
7321 void testBasicPointerAlias(Foo *f) {
7322 Foo *ptr = f;
7323 ptr->mu.Lock(); // lock through alias
7324 f->data = 42; // access through original
7325 ptr->mu.Unlock(); // unlock through alias
7326 }
7327
7328 void testCastPointerAlias(Foo *f) {
7329 // Cast to void* from unsigned long to test non-pointer cast indirection.
7330 void *priv = (void *)(__UINTPTR_TYPE__)(&f->mu);
7331 f->mu.Lock();
7332 f->data = 42;
7333 auto *mu = (Mutex *)priv;
7334 mu->Unlock();
7335 }
7336
7337 void testBasicPointerAliasNoInit(Foo *f) {
7338 Foo *ptr;
7339
7340 ptr = nullptr;
7341 ptr = f;
7342 ptr->mu.Lock();
7343 f->data = 42;
7344 ptr->mu.Unlock();
7345 ptr = nullptr;
7346 }
7347
7348 void testBasicPointerAliasLoop() {
7349 for (;;) {
7350 Foo *f = returnsFoo();
7351 Foo *ptr = f;
7352 if (!ptr)
7353 break;
7354 ptr->mu.Lock();
7355 f->data = 42;
7356 ptr->mu.Unlock();
7357 }
7358 }
7359
7360 void testPointerAliasNoEscape1(Foo *f) {
7361 Foo *ptr = f;
7362 testBasicPointerAlias(ptr); // pass alias by value
7363
7364 ptr->mu.Lock();
7365 f->data = 42;
7366 ptr->mu.Unlock();
7367 }
7368
7369 void testPointerAliasNoEscape2(Foo *f) {
7370 Foo *ptr = f;
7371 passByConstRef(ptr); // pass alias by const ref
7372
7373 ptr->mu.Lock();
7374 f->data = 42;
7375 ptr->mu.Unlock();
7376 }
7377
7378 void testPointerAliasNoEscape3() {
7379 Foo *ptr = returnsFoo();
7380 ptr->mu.Lock();
7381 locksRequired(ptr);
7382 ptr->mu.Unlock();
7383 }
7384
7385 void testPointerAliasEscape1(Foo *f) {
7386 Foo *ptr = f;
7387 escapeAlias(0, ptr);
7388
7389 ptr->mu.Lock();
7390 f->data = 42; // expected-warning{{writing variable 'data' requires holding mutex 'f->mu' exclusively}} \
7391 // expected-note{{found near match 'ptr->mu'}}
7392 ptr->mu.Unlock();
7393 }
7394
7395 void testPointerAliasEscape2(Foo *f) {
7396 Foo *ptr = f;
7397 escapeAlias(0, &ptr);
7398
7399 ptr->mu.Lock();
7400 f->data = 42; // expected-warning{{writing variable 'data' requires holding mutex 'f->mu' exclusively}} \
7401 // expected-note{{found near match 'ptr->mu'}}
7402 ptr->mu.Unlock();
7403 }
7404
7405 void testPointerAliasEscape3(Foo *f) {
7406 Foo *ptr;
7407
7408 ptr = f;
7409 escapeAlias(0, &ptr);
7410
7411 ptr->mu.Lock();
7412 f->data = 42; // expected-warning{{writing variable 'data' requires holding mutex 'f->mu' exclusively}} \
7413 // expected-note{{found near match 'ptr->mu'}}
7414 ptr->mu.Unlock();
7415 }
7416
7417 void testPointerAliasEscapeAndReset(Foo *f) {
7418 Foo *ptr;
7419
7420 ptr = f;
7421 escapeAlias(0, &ptr);
7422 ptr = f;
7423
7424 ptr->mu.Lock();
7425 f->data = 42;
7426 ptr->mu.Unlock();
7427 }
7428
7429 void testPointerAliasTryLock1() {
7430 Foo *ptr = returnsFoo();
7431 if (ptr->mu.TryLock()) {
7432 locksRequired(ptr);
7433 ptr->mu.Unlock();
7434 }
7435 }
7436
7437 void testPointerAliasTryLock2() {
7438 Foo *ptr;
7439 ptr = returnsFoo();
7440 Foo *ptr2 = ptr;
7441 if (ptr->mu.TryLock()) {
7442 locksRequired(ptr);
7443 ptr2->mu.Unlock();
7444 }
7445 }
7446
7447 // FIXME: This test demonstrates a dubious pattern that the analysis correctly
7448 // flags as unsafe, though the user might perceive it as a false positive. The
7449 // pattern combines a TryLock() failure path with a conditional reassignment of
7450 // the pointer being locked:
7451 //
7452 // 1. The conditional reassignment `ptr = returnsFoo(ptr);` forces `ptr` to
7453 // become a phi node in the CFG at the subsequent merge point. The alias
7454 // analysis correctly tracks that `ptr` could refer to one of two distinct
7455 // objects.
7456 //
7457 // 2. The lock acquired on the `!TryLock()` path should be (conceptually)
7458 // `phi(P1, P2)->mu`, while the lock on the successful path is on the
7459 // original `P1->mu`. When the paths merge the analysis currently discards
7460 // the alias as it cannot prove a single alias on that path.
7461 //
7462 // While this pattern is stylistically fragile and difficult to reason about, a
7463 // robust solution would require a more advanced symbolic representation of
7464 // capabilities within the analyzer. For now, we warn on such ambiguity.
7465 void testPointerAliasTryLockDubious(int x) {
7466 Foo *ptr = returnsFoo();
7467 if (!ptr->mu.TryLock()) { // expected-note{{mutex acquired here}}
7468 if (x)
7469 ptr = returnsFoo(ptr); // <-- this breaks the pattern
7470 ptr->mu.Lock(); // expected-note{{mutex acquired here}}
7471 }
7472 ptr->data = 42; // expected-warning{{writing variable 'data' requires holding mutex 'ptr->mu' exclusively}} \
7473 // expected-warning{{mutex 'ptr->mu' is not held on every path through here}} \
7474 // expected-warning{{mutex 'returnsFoo().mu' is not held on every path through here}}
7475 ptr->mu.Unlock(); // expected-warning{{releasing mutex 'ptr->mu' that was not held}}
7476 }
7477
7478 void testReassignment() {
7479 Foo f1, f2;
7480 Foo *ptr = &f1;
7481 ptr->mu.Lock();
7482 f1.data = 42;
7483 ptr->mu.Unlock();
7484
7485 ptr = &f2;
7486 ptr->mu.Lock();
7487 f2.data = 42;
7488 f1.data = 42; // expected-warning{{writing variable 'data' requires holding mutex 'f1.mu'}} \
7489 // expected-note{{found near match 'f2.mu'}}
7490 ptr->mu.Unlock();
7491 }
7492
7493 // Nested field access through pointer
7494 struct Container {
7495 Foo foo;
7496 };
7497
7498 void testNestedAccess(Container *c) {
7499 Foo *ptr = &c->foo; // pointer to nested field
7500 ptr->mu.Lock();
7501 c->foo.data = 42;
7502 ptr->mu.Unlock();
7503 }
7504
7505 void testNestedAcquire(Container *c) EXCLUSIVE_LOCK_FUNCTION(&c->foo.mu) {
7506 Foo *buf = &c->foo;
7507 buf->mu.Lock();
7508 }
7509
7510 struct ContainerOfPtr {
7511 Foo *foo_ptr;
7512 ContainerOfPtr *next;
7513 };
7514
7515 void testIndirectAccess(ContainerOfPtr *fc) {
7516 Foo *ptr = fc->foo_ptr; // get pointer
7517 ptr->mu.Lock();
7518 fc->foo_ptr->data = 42; // access via original
7519 ptr->mu.Unlock();
7520 }
7521
7522 void testAliasChainUnrelatedReassignment1(ContainerOfPtr *list) {
7523 Foo *eb = list->foo_ptr;
7524 eb->mu.Lock();
7525 list = list->next;
7526 eb->data = 42;
7527 eb->mu.Unlock();
7528 }
7529
7530 void testAliasChainUnrelatedReassignment2(ContainerOfPtr *list) {
7531 ContainerOfPtr *busyp = list;
7532 Foo *eb = busyp->foo_ptr;
7533 eb->mu.Lock();
7534 busyp = busyp->next;
7535 eb->data = 42;
7536 eb->mu.Unlock();
7537 }
7538
7539 void testControlFlowDoWhile(Foo *f, int x) {
7540 Foo *ptr = f;
7541
7542 f->mu.Lock();
7543 if (x) {
7544 // complex merge
7545 do { } while (x--);
7546 }
7547 ptr->data = 42;
7548 ptr->mu.Unlock();
7549 }
7550
7551 // FIXME: No alias tracking through complex control flow.
7552 void testComplexControlFlow(Foo *f1, Foo *f2, bool cond) {
7553 Foo *ptr;
7554 if (cond) {
7555 ptr = f1;
7556 } else {
7557 ptr = f2;
7558 }
7559 ptr->mu.Lock();
7560 if (cond) {
7561 f1->data = 42; // expected-warning{{writing variable 'data' requires holding mutex 'f1->mu' exclusively}} \
7562 // expected-note{{found near match 'ptr->mu'}}
7563 } else {
7564 f2->data = 42; // expected-warning{{writing variable 'data' requires holding mutex 'f2->mu' exclusively}} \
7565 // expected-note{{found near match 'ptr->mu'}}
7566 }
7567 ptr->mu.Unlock();
7568 }
7569
7570 void testLockFunction(Foo *f) EXCLUSIVE_LOCK_FUNCTION(&f->mu) {
7571 Mutex *mu = &f->mu;
7572 mu->Lock();
7573 }
7574
7575 void testUnlockFunction(Foo *f) UNLOCK_FUNCTION(&f->mu) {
7576 Mutex *mu = &f->mu;
7577 mu->Unlock();
7578 }
7579
7580 // This is an idiom to deal with "pointer to returned object has a lock held,
7581 // but you must unlock it later" where the statement expression would be hidden
7582 // behind a macro.
7583 void lockWithinStatementExpr() {
7584 Foo *f = ({ auto x = returnsFoo(); x->mu.Lock(); x; });
7585 f->data = 42;
7586 f->mu.Unlock();
7587 }
7588
7589 // Semantically UB, but let's not crash the compiler with this (should be
7590 // handled by -Wuninitialized).
7591 void testSelfInit() {
7592 Mutex *mu = mu; // don't do this at home
7593 mu->Lock();
7594 mu->Unlock();
7595 }
7596
7597 void testSelfAssign() {
7598 Foo *f = returnsFoo();
7599 f = f;
7600 f->mu.Lock();
7601 f->data = 42;
7602 f->mu.Unlock();
7603 }
7604
7605 void testRecursiveAssign() {
7606 Foo *f = returnsFoo();
7607 f = returnsFoo(f);
7608 f->mu.Lock();
7609 f->data = 42;
7610 f->mu.Unlock();
7611 }
7612
7613 void testNew(Mutex *&out, int &x) {
7614 Mutex *mu = new Mutex;
7615 __atomic_store_n(&out, mu, __ATOMIC_RELEASE);
7616 mu->Lock();
7617 x = 42; // ... perhaps guarded by mu
7618 mu->Unlock();
7619 }
7620
7621 void testNestedLoopInvariant(Container *c, int n) {
7622 Foo *ptr = &c->foo;
7623 ptr->mu.Lock();
7624
7625 for (int i = 0; i < n; ++i) {
7626 for (int j = 0; j < n; ++j) {
7627 }
7628 }
7629
7630 c->foo.data = 42; // ok: alias still valid
7631 ptr->mu.Unlock();
7632 }
7633
7634 void testLoopWithBreak(Foo *f, bool cond) {
7635 Foo *ptr = f;
7636 ptr->mu.Lock();
7637 for (int i = 0; i < 10; ++i) {
7638 if (cond) {
7639 break; // merge point is after the loop
7640 }
7641 }
7642 f->data = 42; // ok
7643 ptr->mu.Unlock();
7644 }
7645
7646 void testLoopWithContinue(Foo *f, bool cond) {
7647 Foo *ptr = f;
7648 ptr->mu.Lock();
7649 for (int i = 0; i < 10; ++i) {
7650 if (cond) {
7651 continue; // tests merge at the top of loop.
7652 }
7653 }
7654 f->data = 42; // ok
7655 ptr->mu.Unlock();
7656 }
7657
7658 void testLoopConditionalReassignment(Foo *f1, Foo *f2, bool cond) {
7659 Foo *ptr = f1;
7660 ptr->mu.Lock(); // expected-note{{mutex acquired here}}
7661
7662 for (int i = 0; i < 10; ++i) {
7663 if (cond) {
7664 ptr = f2; // alias is reassigned on some path inside the loop.
7665 }
7666 }
7667 f1->data = 42;
7668 ptr->mu.Unlock(); // expected-warning{{releasing mutex 'ptr->mu' that was not held}}
7669 } // expected-warning{{mutex 'f1->mu' is still held at the end of function}}
7670 } // namespace CapabilityAliases