1 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -Wthread-safety -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-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-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-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 // The universal lock, written "*", allows checking to be selectively turned 61 // off for a particular piece of code. 62 void beginNoWarnOnReads() SHARED_LOCK_FUNCTION("*"); 63 void endNoWarnOnReads() UNLOCK_FUNCTION("*"); 64 void beginNoWarnOnWrites() EXCLUSIVE_LOCK_FUNCTION("*"); 65 void endNoWarnOnWrites() UNLOCK_FUNCTION("*"); 66 67 68 // For testing handling of smart pointers. 69 template<class T> 70 class SmartPtr { 71 public: 72 SmartPtr(T* p) : ptr_(p) { } 73 SmartPtr(const SmartPtr<T>& p) : ptr_(p.ptr_) { } 74 ~SmartPtr(); 75 76 T* get() const { return ptr_; } 77 T* operator->() const { return ptr_; } 78 T& operator*() const { return *ptr_; } 79 T& operator[](int i) const { return ptr_[i]; } 80 81 private: 82 T* ptr_; 83 }; 84 85 86 // For testing destructor calls and cleanup. 87 class MyString { 88 public: 89 MyString(const char* s); 90 ~MyString(); 91 }; 92 93 94 // For testing operator overloading 95 template <class K, class T> 96 class MyMap { 97 public: 98 T& operator[](const K& k); 99 }; 100 101 102 // For testing handling of containers. 103 template <class T> 104 class MyContainer { 105 public: 106 MyContainer(); 107 108 typedef T* iterator; 109 typedef const T* const_iterator; 110 111 T* begin(); 112 T* end(); 113 114 const T* cbegin(); 115 const T* cend(); 116 117 T& operator[](int i); 118 const T& operator[](int i) const; 119 120 private: 121 T* ptr_; 122 }; 123 124 125 126 Mutex sls_mu; 127 128 Mutex sls_mu2 __attribute__((acquired_after(sls_mu))); 129 int sls_guard_var __attribute__((guarded_var)) = 0; 130 int sls_guardby_var __attribute__((guarded_by(sls_mu))) = 0; 131 132 bool getBool(); 133 134 class MutexWrapper { 135 public: 136 Mutex mu; 137 int x __attribute__((guarded_by(mu))); 138 void MyLock() EXCLUSIVE_LOCK_FUNCTION(mu); 139 }; 140 141 MutexWrapper sls_mw; 142 143 void sls_fun_0() { 144 sls_mw.mu.Lock(); 145 sls_mw.x = 5; 146 sls_mw.mu.Unlock(); 147 } 148 149 void sls_fun_2() { 150 sls_mu.Lock(); 151 int x = sls_guard_var; 152 sls_mu.Unlock(); 153 } 154 155 void sls_fun_3() { 156 sls_mu.Lock(); 157 sls_guard_var = 2; 158 sls_mu.Unlock(); 159 } 160 161 void sls_fun_4() { 162 sls_mu2.Lock(); 163 sls_guard_var = 2; 164 sls_mu2.Unlock(); 165 } 166 167 void sls_fun_5() { 168 sls_mu.Lock(); 169 int x = sls_guardby_var; 170 sls_mu.Unlock(); 171 } 172 173 void sls_fun_6() { 174 sls_mu.Lock(); 175 sls_guardby_var = 2; 176 sls_mu.Unlock(); 177 } 178 179 void sls_fun_7() { 180 sls_mu.Lock(); 181 sls_mu2.Lock(); 182 sls_mu2.Unlock(); 183 sls_mu.Unlock(); 184 } 185 186 void sls_fun_8() { 187 sls_mu.Lock(); 188 if (getBool()) 189 sls_mu.Unlock(); 190 else 191 sls_mu.Unlock(); 192 } 193 194 void sls_fun_9() { 195 if (getBool()) 196 sls_mu.Lock(); 197 else 198 sls_mu.Lock(); 199 sls_mu.Unlock(); 200 } 201 202 void sls_fun_good_6() { 203 if (getBool()) { 204 sls_mu.Lock(); 205 } else { 206 if (getBool()) { 207 getBool(); // EMPTY 208 } else { 209 getBool(); // EMPTY 210 } 211 sls_mu.Lock(); 212 } 213 sls_mu.Unlock(); 214 } 215 216 void sls_fun_good_7() { 217 sls_mu.Lock(); 218 while (getBool()) { 219 sls_mu.Unlock(); 220 if (getBool()) { 221 if (getBool()) { 222 sls_mu.Lock(); 223 continue; 224 } 225 } 226 sls_mu.Lock(); 227 } 228 sls_mu.Unlock(); 229 } 230 231 void sls_fun_good_8() { 232 sls_mw.MyLock(); 233 sls_mw.mu.Unlock(); 234 } 235 236 void sls_fun_bad_1() { 237 sls_mu.Unlock(); // \ 238 // expected-warning{{releasing mutex 'sls_mu' that was not held}} 239 } 240 241 void sls_fun_bad_2() { 242 sls_mu.Lock(); // expected-note{{mutex acquired here}} 243 sls_mu.Lock(); // \ 244 // expected-warning{{acquiring mutex 'sls_mu' that is already held}} 245 sls_mu.Unlock(); 246 } 247 248 void sls_fun_bad_3() { 249 sls_mu.Lock(); // expected-note {{mutex acquired here}} 250 } // expected-warning{{mutex 'sls_mu' is still held at the end of function}} 251 252 void sls_fun_bad_4() { 253 if (getBool()) 254 sls_mu.Lock(); // expected-note{{mutex acquired here}} 255 else 256 sls_mu2.Lock(); // expected-note{{mutex acquired here}} 257 } // expected-warning{{mutex 'sls_mu' is not held on every path through here}} \ 258 // expected-warning{{mutex 'sls_mu2' is not held on every path through here}} 259 260 void sls_fun_bad_5() { 261 sls_mu.Lock(); // expected-note {{mutex acquired here}} 262 if (getBool()) 263 sls_mu.Unlock(); 264 } // expected-warning{{mutex 'sls_mu' is not held on every path through here}} 265 266 void sls_fun_bad_6() { 267 if (getBool()) { 268 sls_mu.Lock(); // expected-note {{mutex acquired here}} 269 } else { 270 if (getBool()) { 271 getBool(); // EMPTY 272 } else { 273 getBool(); // EMPTY 274 } 275 } 276 sls_mu.Unlock(); // \ 277 expected-warning{{mutex 'sls_mu' is not held on every path through here}}\ 278 expected-warning{{releasing mutex 'sls_mu' that was not held}} 279 } 280 281 void sls_fun_bad_7() { 282 sls_mu.Lock(); 283 while (getBool()) { 284 sls_mu.Unlock(); 285 if (getBool()) { 286 if (getBool()) { 287 continue; // \ 288 expected-warning{{expecting mutex 'sls_mu' to be held at start of each loop}} 289 } 290 } 291 sls_mu.Lock(); // expected-note {{mutex acquired here}} 292 } 293 sls_mu.Unlock(); 294 } 295 296 void sls_fun_bad_8() { 297 sls_mu.Lock(); // expected-note{{mutex acquired here}} 298 299 do { 300 sls_mu.Unlock(); // expected-warning{{expecting mutex 'sls_mu' to be held at start of each loop}} 301 } while (getBool()); 302 } 303 304 void sls_fun_bad_9() { 305 do { 306 sls_mu.Lock(); // \ 307 // expected-warning{{expecting mutex 'sls_mu' to be held at start of each loop}} \ 308 // expected-note{{mutex acquired here}} 309 } while (getBool()); 310 sls_mu.Unlock(); 311 } 312 313 void sls_fun_bad_10() { 314 sls_mu.Lock(); // expected-note 2{{mutex acquired here}} 315 while(getBool()) { // expected-warning{{expecting mutex 'sls_mu' to be held at start of each loop}} 316 sls_mu.Unlock(); 317 } 318 } // expected-warning{{mutex 'sls_mu' is still held at the end of function}} 319 320 void sls_fun_bad_11() { 321 while (getBool()) { // \ 322 expected-warning{{expecting mutex 'sls_mu' to be held at start of each loop}} 323 sls_mu.Lock(); // expected-note {{mutex acquired here}} 324 } 325 sls_mu.Unlock(); // \ 326 // expected-warning{{releasing mutex 'sls_mu' that was not held}} 327 } 328 329 void sls_fun_bad_12() { 330 sls_mu.Lock(); // expected-note {{mutex acquired here}} 331 while (getBool()) { 332 sls_mu.Unlock(); 333 if (getBool()) { 334 if (getBool()) { 335 break; // expected-warning{{mutex 'sls_mu' is not held on every path through here}} 336 } 337 } 338 sls_mu.Lock(); 339 } 340 sls_mu.Unlock(); // \ 341 // expected-warning{{releasing mutex 'sls_mu' that was not held}} 342 } 343 344 //-----------------------------------------// 345 // Handling lock expressions in attribute args 346 // -------------------------------------------// 347 348 Mutex aa_mu; 349 350 class GlobalLocker { 351 public: 352 void globalLock() EXCLUSIVE_LOCK_FUNCTION(aa_mu); 353 void globalUnlock() UNLOCK_FUNCTION(aa_mu); 354 }; 355 356 GlobalLocker glock; 357 358 void aa_fun_1() { 359 glock.globalLock(); 360 glock.globalUnlock(); 361 } 362 363 void aa_fun_bad_1() { 364 glock.globalUnlock(); // \ 365 // expected-warning{{releasing mutex 'aa_mu' that was not held}} 366 } 367 368 void aa_fun_bad_2() { 369 glock.globalLock(); // expected-note{{mutex acquired here}} 370 glock.globalLock(); // \ 371 // expected-warning{{acquiring mutex 'aa_mu' that is already held}} 372 glock.globalUnlock(); 373 } 374 375 void aa_fun_bad_3() { 376 glock.globalLock(); // expected-note{{mutex acquired here}} 377 } // expected-warning{{mutex 'aa_mu' is still held at the end of function}} 378 379 //--------------------------------------------------// 380 // Regression tests for unusual method names 381 //--------------------------------------------------// 382 383 Mutex wmu; 384 385 // Test diagnostics for other method names. 386 class WeirdMethods { 387 // FIXME: can't currently check inside constructors and destructors. 388 WeirdMethods() { 389 wmu.Lock(); // EXPECTED-NOTE {{mutex acquired here}} 390 } // EXPECTED-WARNING {{mutex 'wmu' is still held at the end of function}} 391 ~WeirdMethods() { 392 wmu.Lock(); // EXPECTED-NOTE {{mutex acquired here}} 393 } // EXPECTED-WARNING {{mutex 'wmu' is still held at the end of function}} 394 void operator++() { 395 wmu.Lock(); // expected-note {{mutex acquired here}} 396 } // expected-warning {{mutex 'wmu' is still held at the end of function}} 397 operator int*() { 398 wmu.Lock(); // expected-note {{mutex acquired here}} 399 return 0; 400 } // expected-warning {{mutex 'wmu' is still held at the end of function}} 401 }; 402 403 //-----------------------------------------------// 404 // Errors for guarded by or guarded var variables 405 // ----------------------------------------------// 406 407 int *pgb_gvar __attribute__((pt_guarded_var)); 408 int *pgb_var __attribute__((pt_guarded_by(sls_mu))); 409 410 class PGBFoo { 411 public: 412 int x; 413 int *pgb_field __attribute__((guarded_by(sls_mu2))) 414 __attribute__((pt_guarded_by(sls_mu))); 415 void testFoo() { 416 pgb_field = &x; // \ 417 // expected-warning {{writing variable 'pgb_field' requires holding mutex 'sls_mu2' exclusively}} 418 *pgb_field = x; // expected-warning {{reading variable 'pgb_field' requires holding mutex 'sls_mu2'}} \ 419 // expected-warning {{writing the value pointed to by 'pgb_field' requires holding mutex 'sls_mu' exclusively}} 420 x = *pgb_field; // expected-warning {{reading variable 'pgb_field' requires holding mutex 'sls_mu2'}} \ 421 // expected-warning {{reading the value pointed to by 'pgb_field' requires holding mutex 'sls_mu'}} 422 (*pgb_field)++; // expected-warning {{reading variable 'pgb_field' requires holding mutex 'sls_mu2'}} \ 423 // expected-warning {{writing the value pointed to by 'pgb_field' requires holding mutex 'sls_mu' exclusively}} 424 } 425 }; 426 427 class GBFoo { 428 public: 429 int gb_field __attribute__((guarded_by(sls_mu))); 430 431 void testFoo() { 432 gb_field = 0; // \ 433 // expected-warning {{writing variable 'gb_field' requires holding mutex 'sls_mu' exclusively}} 434 } 435 436 void testNoAnal() NO_THREAD_SAFETY_ANALYSIS { 437 gb_field = 0; 438 } 439 }; 440 441 GBFoo GlobalGBFoo __attribute__((guarded_by(sls_mu))); 442 443 void gb_fun_0() { 444 sls_mu.Lock(); 445 int x = *pgb_var; 446 sls_mu.Unlock(); 447 } 448 449 void gb_fun_1() { 450 sls_mu.Lock(); 451 *pgb_var = 2; 452 sls_mu.Unlock(); 453 } 454 455 void gb_fun_2() { 456 int x; 457 pgb_var = &x; 458 } 459 460 void gb_fun_3() { 461 int *x = pgb_var; 462 } 463 464 void gb_bad_0() { 465 sls_guard_var = 1; // \ 466 // expected-warning{{writing variable 'sls_guard_var' requires holding any mutex exclusively}} 467 } 468 469 void gb_bad_1() { 470 int x = sls_guard_var; // \ 471 // expected-warning{{reading variable 'sls_guard_var' requires holding any mutex}} 472 } 473 474 void gb_bad_2() { 475 sls_guardby_var = 1; // \ 476 // expected-warning {{writing variable 'sls_guardby_var' requires holding mutex 'sls_mu' exclusively}} 477 } 478 479 void gb_bad_3() { 480 int x = sls_guardby_var; // \ 481 // expected-warning {{reading variable 'sls_guardby_var' requires holding mutex 'sls_mu'}} 482 } 483 484 void gb_bad_4() { 485 *pgb_gvar = 1; // \ 486 // expected-warning {{writing the value pointed to by 'pgb_gvar' requires holding any mutex exclusively}} 487 } 488 489 void gb_bad_5() { 490 int x = *pgb_gvar; // \ 491 // expected-warning {{reading the value pointed to by 'pgb_gvar' requires holding any mutex}} 492 } 493 494 void gb_bad_6() { 495 *pgb_var = 1; // \ 496 // expected-warning {{writing the value pointed to by 'pgb_var' requires holding mutex 'sls_mu' exclusively}} 497 } 498 499 void gb_bad_7() { 500 int x = *pgb_var; // \ 501 // expected-warning {{reading the value pointed to by 'pgb_var' requires holding mutex 'sls_mu'}} 502 } 503 504 void gb_bad_8() { 505 GBFoo G; 506 G.gb_field = 0; // \ 507 // expected-warning {{writing variable 'gb_field' requires holding mutex 'sls_mu'}} 508 } 509 510 void gb_bad_9() { 511 sls_guard_var++; // \ 512 // expected-warning{{writing variable 'sls_guard_var' requires holding any mutex exclusively}} 513 sls_guard_var--; // \ 514 // expected-warning{{writing variable 'sls_guard_var' requires holding any mutex exclusively}} 515 ++sls_guard_var; // \ 516 // expected-warning{{writing variable 'sls_guard_var' requires holding any mutex exclusively}} 517 --sls_guard_var;// \ 518 // expected-warning{{writing variable 'sls_guard_var' requires holding any mutex exclusively}} 519 } 520 521 //-----------------------------------------------// 522 // Warnings on variables with late parsed attributes 523 // ----------------------------------------------// 524 525 class LateFoo { 526 public: 527 int a __attribute__((guarded_by(mu))); 528 int b; 529 530 void foo() EXCLUSIVE_LOCKS_REQUIRED(mu) { } 531 532 void test() { 533 a = 0; // \ 534 // expected-warning{{writing variable 'a' requires holding mutex 'mu' exclusively}} 535 b = a; // \ 536 // expected-warning {{reading variable 'a' requires holding mutex 'mu'}} 537 c = 0; // \ 538 // expected-warning {{writing variable 'c' requires holding mutex 'mu' exclusively}} 539 } 540 541 int c __attribute__((guarded_by(mu))); 542 543 Mutex mu; 544 }; 545 546 class LateBar { 547 public: 548 int a_ __attribute__((guarded_by(mu1_))); 549 int b_; 550 int *q __attribute__((pt_guarded_by(mu))); 551 Mutex mu1_; 552 Mutex mu; 553 LateFoo Foo; 554 LateFoo Foo2; 555 LateFoo *FooPointer; 556 }; 557 558 LateBar b1, *b3; 559 560 void late_0() { 561 LateFoo FooA; 562 LateFoo FooB; 563 FooA.mu.Lock(); 564 FooA.a = 5; 565 FooA.mu.Unlock(); 566 } 567 568 void late_1() { 569 LateBar BarA; 570 BarA.FooPointer->mu.Lock(); 571 BarA.FooPointer->a = 2; 572 BarA.FooPointer->mu.Unlock(); 573 } 574 575 void late_bad_0() { 576 LateFoo fooA; 577 LateFoo fooB; 578 fooA.mu.Lock(); 579 fooB.a = 5; // \ 580 // expected-warning{{writing variable 'a' requires holding mutex 'fooB.mu' exclusively}} \ 581 // expected-note{{found near match 'fooA.mu'}} 582 fooA.mu.Unlock(); 583 } 584 585 void late_bad_1() { 586 Mutex mu; 587 mu.Lock(); 588 b1.mu1_.Lock(); 589 int res = b1.a_ + b3->b_; 590 b3->b_ = *b1.q; // \ 591 // expected-warning{{reading the value pointed to by 'q' requires holding mutex 'b1.mu'}} 592 b1.mu1_.Unlock(); 593 b1.b_ = res; 594 mu.Unlock(); 595 } 596 597 void late_bad_2() { 598 LateBar BarA; 599 BarA.FooPointer->mu.Lock(); 600 BarA.Foo.a = 2; // \ 601 // expected-warning{{writing variable 'a' requires holding mutex 'BarA.Foo.mu' exclusively}} \ 602 // expected-note{{found near match 'BarA.FooPointer->mu'}} 603 BarA.FooPointer->mu.Unlock(); 604 } 605 606 void late_bad_3() { 607 LateBar BarA; 608 BarA.Foo.mu.Lock(); 609 BarA.FooPointer->a = 2; // \ 610 // expected-warning{{writing variable 'a' requires holding mutex 'BarA.FooPointer->mu' exclusively}} \ 611 // expected-note{{found near match 'BarA.Foo.mu'}} 612 BarA.Foo.mu.Unlock(); 613 } 614 615 void late_bad_4() { 616 LateBar BarA; 617 BarA.Foo.mu.Lock(); 618 BarA.Foo2.a = 2; // \ 619 // expected-warning{{writing variable 'a' requires holding mutex 'BarA.Foo2.mu' exclusively}} \ 620 // expected-note{{found near match 'BarA.Foo.mu'}} 621 BarA.Foo.mu.Unlock(); 622 } 623 624 //-----------------------------------------------// 625 // Extra warnings for shared vs. exclusive locks 626 // ----------------------------------------------// 627 628 void shared_fun_0() { 629 sls_mu.Lock(); 630 do { 631 sls_mu.Unlock(); 632 sls_mu.Lock(); 633 } while (getBool()); 634 sls_mu.Unlock(); 635 } 636 637 void shared_fun_1() { 638 sls_mu.ReaderLock(); // \ 639 // expected-warning {{mutex 'sls_mu' is acquired exclusively and shared in the same scope}} 640 do { 641 sls_mu.Unlock(); 642 sls_mu.Lock(); // \ 643 // expected-note {{the other acquisition of mutex 'sls_mu' is here}} 644 } while (getBool()); 645 sls_mu.Unlock(); 646 } 647 648 void shared_fun_3() { 649 if (getBool()) 650 sls_mu.Lock(); 651 else 652 sls_mu.Lock(); 653 *pgb_var = 1; 654 sls_mu.Unlock(); 655 } 656 657 void shared_fun_4() { 658 if (getBool()) 659 sls_mu.ReaderLock(); 660 else 661 sls_mu.ReaderLock(); 662 int x = sls_guardby_var; 663 sls_mu.Unlock(); 664 } 665 666 void shared_fun_8() { 667 if (getBool()) 668 sls_mu.Lock(); // \ 669 // expected-warning {{mutex 'sls_mu' is acquired exclusively and shared in the same scope}} 670 else 671 sls_mu.ReaderLock(); // \ 672 // expected-note {{the other acquisition of mutex 'sls_mu' is here}} 673 sls_mu.Unlock(); 674 } 675 676 void shared_fun_9() { 677 sls_mu.Lock(); 678 sls_mu.ExclusiveUnlock(); 679 680 sls_mu.ReaderLock(); 681 sls_mu.ReaderUnlock(); 682 } 683 684 void shared_fun_10() { 685 sls_mu.Lock(); 686 sls_mu.DemoteExclusive(); 687 sls_mu.ReaderUnlock(); 688 } 689 690 void shared_fun_11() { 691 sls_mu.ReaderLock(); 692 sls_mu.PromoteShared(); 693 sls_mu.Unlock(); 694 } 695 696 void shared_bad_0() { 697 sls_mu.Lock(); // \ 698 // expected-warning {{mutex 'sls_mu' is acquired exclusively and shared in the same scope}} 699 do { 700 sls_mu.Unlock(); 701 sls_mu.ReaderLock(); // \ 702 // expected-note {{the other acquisition of mutex 'sls_mu' is here}} 703 } while (getBool()); 704 sls_mu.Unlock(); 705 } 706 707 void shared_bad_1() { 708 if (getBool()) 709 sls_mu.Lock(); // \ 710 // expected-warning {{mutex 'sls_mu' is acquired exclusively and shared in the same scope}} 711 else 712 sls_mu.ReaderLock(); // \ 713 // expected-note {{the other acquisition of mutex 'sls_mu' is here}} 714 *pgb_var = 1; 715 sls_mu.Unlock(); 716 } 717 718 void shared_bad_2() { 719 if (getBool()) 720 sls_mu.ReaderLock(); // \ 721 // expected-warning {{mutex 'sls_mu' is acquired exclusively and shared in the same scope}} 722 else 723 sls_mu.Lock(); // \ 724 // expected-note {{the other acquisition of mutex 'sls_mu' is here}} 725 *pgb_var = 1; 726 sls_mu.Unlock(); 727 } 728 729 void shared_bad_3() { 730 sls_mu.Lock(); // expected-note {{mutex acquired here}} 731 sls_mu.ReaderUnlock(); // \ 732 // expected-warning {{releasing mutex 'sls_mu' using shared access, expected exclusive access}} 733 } 734 735 void shared_bad_4() { 736 sls_mu.ReaderLock(); // expected-note {{mutex acquired here}} 737 sls_mu.ExclusiveUnlock(); // \ 738 // expected-warning {{releasing mutex 'sls_mu' using exclusive access, expected shared access}} 739 } 740 741 void shared_bad_5() { 742 sls_mu.Lock(); // expected-note {{mutex acquired here}} 743 sls_mu.PromoteShared(); // \ 744 // expected-warning {{releasing mutex 'sls_mu' using shared access, expected exclusive access}} 745 sls_mu.ExclusiveUnlock(); 746 } 747 748 void shared_bad_6() { 749 sls_mu.ReaderLock(); // expected-note {{mutex acquired here}} 750 sls_mu.DemoteExclusive(); // \ 751 // expected-warning {{releasing mutex 'sls_mu' using exclusive access, expected shared access}} 752 sls_mu.ReaderUnlock(); 753 } 754 755 // FIXME: Add support for functions (not only methods) 756 class LRBar { 757 public: 758 void aa_elr_fun() EXCLUSIVE_LOCKS_REQUIRED(aa_mu); 759 void aa_elr_fun_s() SHARED_LOCKS_REQUIRED(aa_mu); 760 void le_fun() __attribute__((locks_excluded(sls_mu))); 761 }; 762 763 class LRFoo { 764 public: 765 void test() EXCLUSIVE_LOCKS_REQUIRED(sls_mu); 766 void testShared() SHARED_LOCKS_REQUIRED(sls_mu2); 767 }; 768 769 void elr_fun() EXCLUSIVE_LOCKS_REQUIRED(sls_mu); 770 void elr_fun() {} 771 772 LRFoo MyLRFoo; 773 LRBar Bar; 774 775 void es_fun_0() { 776 aa_mu.Lock(); 777 Bar.aa_elr_fun(); 778 aa_mu.Unlock(); 779 } 780 781 void es_fun_1() { 782 aa_mu.Lock(); 783 Bar.aa_elr_fun_s(); 784 aa_mu.Unlock(); 785 } 786 787 void es_fun_2() { 788 aa_mu.ReaderLock(); 789 Bar.aa_elr_fun_s(); 790 aa_mu.Unlock(); 791 } 792 793 void es_fun_3() { 794 sls_mu.Lock(); 795 MyLRFoo.test(); 796 sls_mu.Unlock(); 797 } 798 799 void es_fun_4() { 800 sls_mu2.Lock(); 801 MyLRFoo.testShared(); 802 sls_mu2.Unlock(); 803 } 804 805 void es_fun_5() { 806 sls_mu2.ReaderLock(); 807 MyLRFoo.testShared(); 808 sls_mu2.Unlock(); 809 } 810 811 void es_fun_6() { 812 Bar.le_fun(); 813 } 814 815 void es_fun_7() { 816 sls_mu.Lock(); 817 elr_fun(); 818 sls_mu.Unlock(); 819 } 820 821 void es_fun_8() NO_THREAD_SAFETY_ANALYSIS; 822 823 void es_fun_8() { 824 Bar.aa_elr_fun_s(); 825 } 826 827 void es_fun_9() SHARED_LOCKS_REQUIRED(aa_mu); 828 void es_fun_9() { 829 Bar.aa_elr_fun_s(); 830 } 831 832 void es_fun_10() EXCLUSIVE_LOCKS_REQUIRED(aa_mu); 833 void es_fun_10() { 834 Bar.aa_elr_fun_s(); 835 } 836 837 void es_bad_0() { 838 Bar.aa_elr_fun(); // \ 839 // expected-warning {{calling function 'aa_elr_fun' requires holding mutex 'aa_mu' exclusively}} 840 } 841 842 void es_bad_1() { 843 aa_mu.ReaderLock(); 844 Bar.aa_elr_fun(); // \ 845 // expected-warning {{calling function 'aa_elr_fun' requires holding mutex 'aa_mu' exclusively}} 846 aa_mu.Unlock(); 847 } 848 849 void es_bad_2() { 850 Bar.aa_elr_fun_s(); // \ 851 // expected-warning {{calling function 'aa_elr_fun_s' requires holding mutex 'aa_mu'}} 852 } 853 854 void es_bad_3() { 855 MyLRFoo.test(); // \ 856 // expected-warning {{calling function 'test' requires holding mutex 'sls_mu' exclusively}} 857 } 858 859 void es_bad_4() { 860 MyLRFoo.testShared(); // \ 861 // expected-warning {{calling function 'testShared' requires holding mutex 'sls_mu2'}} 862 } 863 864 void es_bad_5() { 865 sls_mu.ReaderLock(); 866 MyLRFoo.test(); // \ 867 // expected-warning {{calling function 'test' requires holding mutex 'sls_mu' exclusively}} 868 sls_mu.Unlock(); 869 } 870 871 void es_bad_6() { 872 sls_mu.Lock(); 873 Bar.le_fun(); // \ 874 // expected-warning {{cannot call function 'le_fun' while mutex 'sls_mu' is held}} 875 sls_mu.Unlock(); 876 } 877 878 void es_bad_7() { 879 sls_mu.ReaderLock(); 880 Bar.le_fun(); // \ 881 // expected-warning {{cannot call function 'le_fun' while mutex 'sls_mu' is held}} 882 sls_mu.Unlock(); 883 } 884 885 886 //-----------------------------------------------// 887 // Unparseable lock expressions 888 // ----------------------------------------------// 889 890 // FIXME -- derive new tests for unhandled expressions 891 892 893 //----------------------------------------------------------------------------// 894 // The following test cases are ported from the gcc thread safety implementation 895 // They are each wrapped inside a namespace with the test number of the gcc test 896 // 897 // FIXME: add all the gcc tests, once this analysis passes them. 898 //----------------------------------------------------------------------------// 899 900 //-----------------------------------------// 901 // Good testcases (no errors) 902 //-----------------------------------------// 903 904 namespace thread_annot_lock_20 { 905 class Bar { 906 public: 907 static int func1() EXCLUSIVE_LOCKS_REQUIRED(mu1_); 908 static int b_ GUARDED_BY(mu1_); 909 static Mutex mu1_; 910 static int a_ GUARDED_BY(mu1_); 911 }; 912 913 Bar b1; 914 915 int Bar::func1() 916 { 917 int res = 5; 918 919 if (a_ == 4) 920 res = b_; 921 return res; 922 } 923 } // end namespace thread_annot_lock_20 924 925 namespace thread_annot_lock_22 { 926 // Test various usage of GUARDED_BY and PT_GUARDED_BY annotations, especially 927 // uses in class definitions. 928 Mutex mu; 929 930 class Bar { 931 public: 932 int a_ GUARDED_BY(mu1_); 933 int b_; 934 int *q PT_GUARDED_BY(mu); 935 Mutex mu1_ ACQUIRED_AFTER(mu); 936 }; 937 938 Bar b1, *b3; 939 int *p GUARDED_BY(mu) PT_GUARDED_BY(mu); 940 int res GUARDED_BY(mu) = 5; 941 942 int func(int i) 943 { 944 int x; 945 mu.Lock(); 946 b1.mu1_.Lock(); 947 res = b1.a_ + b3->b_; 948 *p = i; 949 b1.a_ = res + b3->b_; 950 b3->b_ = *b1.q; 951 b1.mu1_.Unlock(); 952 b1.b_ = res; 953 x = res; 954 mu.Unlock(); 955 return x; 956 } 957 } // end namespace thread_annot_lock_22 958 959 namespace thread_annot_lock_27_modified { 960 // test lock annotations applied to function definitions 961 // Modified: applied annotations only to function declarations 962 Mutex mu1; 963 Mutex mu2 ACQUIRED_AFTER(mu1); 964 965 class Foo { 966 public: 967 int method1(int i) SHARED_LOCKS_REQUIRED(mu2) EXCLUSIVE_LOCKS_REQUIRED(mu1); 968 }; 969 970 int Foo::method1(int i) { 971 return i; 972 } 973 974 975 int foo(int i) EXCLUSIVE_LOCKS_REQUIRED(mu2) SHARED_LOCKS_REQUIRED(mu1); 976 int foo(int i) { 977 return i; 978 } 979 980 static int bar(int i) EXCLUSIVE_LOCKS_REQUIRED(mu1); 981 static int bar(int i) { 982 return i; 983 } 984 985 void main() { 986 Foo a; 987 988 mu1.Lock(); 989 mu2.Lock(); 990 a.method1(1); 991 foo(2); 992 mu2.Unlock(); 993 bar(3); 994 mu1.Unlock(); 995 } 996 } // end namespace thread_annot_lock_27_modified 997 998 999 namespace thread_annot_lock_38 { 1000 // Test the case where a template member function is annotated with lock 1001 // attributes in a non-template class. 1002 class Foo { 1003 public: 1004 void func1(int y) LOCKS_EXCLUDED(mu_); 1005 template <typename T> void func2(T x) LOCKS_EXCLUDED(mu_); 1006 private: 1007 Mutex mu_; 1008 }; 1009 1010 Foo *foo; 1011 1012 void main() 1013 { 1014 foo->func1(5); 1015 foo->func2(5); 1016 } 1017 } // end namespace thread_annot_lock_38 1018 1019 namespace thread_annot_lock_43 { 1020 // Tests lock canonicalization 1021 class Foo { 1022 public: 1023 Mutex *mu_; 1024 }; 1025 1026 class FooBar { 1027 public: 1028 Foo *foo_; 1029 int GetA() EXCLUSIVE_LOCKS_REQUIRED(foo_->mu_) { return a_; } 1030 int a_ GUARDED_BY(foo_->mu_); 1031 }; 1032 1033 FooBar *fb; 1034 1035 void main() 1036 { 1037 int x; 1038 fb->foo_->mu_->Lock(); 1039 x = fb->GetA(); 1040 fb->foo_->mu_->Unlock(); 1041 } 1042 } // end namespace thread_annot_lock_43 1043 1044 namespace thread_annot_lock_49 { 1045 // Test the support for use of lock expression in the annotations 1046 class Foo { 1047 public: 1048 Mutex foo_mu_; 1049 }; 1050 1051 class Bar { 1052 private: 1053 Foo *foo; 1054 Mutex bar_mu_ ACQUIRED_AFTER(foo->foo_mu_); 1055 1056 public: 1057 void Test1() { 1058 foo->foo_mu_.Lock(); 1059 bar_mu_.Lock(); 1060 bar_mu_.Unlock(); 1061 foo->foo_mu_.Unlock(); 1062 } 1063 }; 1064 1065 void main() { 1066 Bar bar; 1067 bar.Test1(); 1068 } 1069 } // end namespace thread_annot_lock_49 1070 1071 namespace thread_annot_lock_61_modified { 1072 // Modified to fix the compiler errors 1073 // Test the fix for a bug introduced by the support of pass-by-reference 1074 // parameters. 1075 struct Foo { Foo &operator<< (bool) {return *this;} }; 1076 Foo &getFoo(); 1077 struct Bar { Foo &func () {return getFoo();} }; 1078 struct Bas { void operator& (Foo &) {} }; 1079 void mumble() 1080 { 1081 Bas() & Bar().func() << "" << ""; 1082 Bas() & Bar().func() << ""; 1083 } 1084 } // end namespace thread_annot_lock_61_modified 1085 1086 1087 namespace thread_annot_lock_65 { 1088 // Test the fix for a bug in the support of allowing reader locks for 1089 // non-const, non-modifying overload functions. (We didn't handle the builtin 1090 // properly.) 1091 enum MyFlags { 1092 Zero, 1093 One, 1094 Two, 1095 Three, 1096 Four, 1097 Five, 1098 Six, 1099 Seven, 1100 Eight, 1101 Nine 1102 }; 1103 1104 inline MyFlags 1105 operator|(MyFlags a, MyFlags b) 1106 { 1107 return MyFlags(static_cast<int>(a) | static_cast<int>(b)); 1108 } 1109 1110 inline MyFlags& 1111 operator|=(MyFlags& a, MyFlags b) 1112 { 1113 return a = a | b; 1114 } 1115 } // end namespace thread_annot_lock_65 1116 1117 namespace thread_annot_lock_66_modified { 1118 // Modified: Moved annotation to function defn 1119 // Test annotations on out-of-line definitions of member functions where the 1120 // annotations refer to locks that are also data members in the class. 1121 Mutex mu; 1122 1123 class Foo { 1124 public: 1125 int method1(int i) SHARED_LOCKS_REQUIRED(mu1, mu, mu2); 1126 int data GUARDED_BY(mu1); 1127 Mutex *mu1; 1128 Mutex *mu2; 1129 }; 1130 1131 int Foo::method1(int i) 1132 { 1133 return data + i; 1134 } 1135 1136 void main() 1137 { 1138 Foo a; 1139 1140 a.mu2->Lock(); 1141 a.mu1->Lock(); 1142 mu.Lock(); 1143 a.method1(1); 1144 mu.Unlock(); 1145 a.mu1->Unlock(); 1146 a.mu2->Unlock(); 1147 } 1148 } // end namespace thread_annot_lock_66_modified 1149 1150 namespace thread_annot_lock_68_modified { 1151 // Test a fix to a bug in the delayed name binding with nested template 1152 // instantiation. We use a stack to make sure a name is not resolved to an 1153 // inner context. 1154 template <typename T> 1155 class Bar { 1156 Mutex mu_; 1157 }; 1158 1159 template <typename T> 1160 class Foo { 1161 public: 1162 void func(T x) { 1163 mu_.Lock(); 1164 count_ = x; 1165 mu_.Unlock(); 1166 } 1167 1168 private: 1169 T count_ GUARDED_BY(mu_); 1170 Bar<T> bar_; 1171 Mutex mu_; 1172 }; 1173 1174 void main() 1175 { 1176 Foo<int> *foo; 1177 foo->func(5); 1178 } 1179 } // end namespace thread_annot_lock_68_modified 1180 1181 namespace thread_annot_lock_30_modified { 1182 // Test delay parsing of lock attribute arguments with nested classes. 1183 // Modified: trylocks replaced with exclusive_lock_fun 1184 int a = 0; 1185 1186 class Bar { 1187 struct Foo; 1188 1189 public: 1190 void MyLock() EXCLUSIVE_LOCK_FUNCTION(mu); 1191 1192 int func() { 1193 MyLock(); 1194 // if (foo == 0) { 1195 // return 0; 1196 // } 1197 a = 5; 1198 mu.Unlock(); 1199 return 1; 1200 } 1201 1202 class FooBar { 1203 int x; 1204 int y; 1205 }; 1206 1207 private: 1208 Mutex mu; 1209 }; 1210 1211 Bar *bar; 1212 1213 void main() 1214 { 1215 bar->func(); 1216 } 1217 } // end namespace thread_annot_lock_30_modified 1218 1219 namespace thread_annot_lock_47 { 1220 // Test the support for annotations on virtual functions. 1221 // This is a good test case. (i.e. There should be no warning emitted by the 1222 // compiler.) 1223 class Base { 1224 public: 1225 virtual void func1() EXCLUSIVE_LOCKS_REQUIRED(mu_); 1226 virtual void func2() LOCKS_EXCLUDED(mu_); 1227 Mutex mu_; 1228 }; 1229 1230 class Child : public Base { 1231 public: 1232 virtual void func1() EXCLUSIVE_LOCKS_REQUIRED(mu_); 1233 virtual void func2() LOCKS_EXCLUDED(mu_); 1234 }; 1235 1236 void main() { 1237 Child *c; 1238 Base *b = c; 1239 1240 b->mu_.Lock(); 1241 b->func1(); 1242 b->mu_.Unlock(); 1243 b->func2(); 1244 1245 c->mu_.Lock(); 1246 c->func1(); 1247 c->mu_.Unlock(); 1248 c->func2(); 1249 } 1250 } // end namespace thread_annot_lock_47 1251 1252 //-----------------------------------------// 1253 // Tests which produce errors 1254 //-----------------------------------------// 1255 1256 namespace thread_annot_lock_13 { 1257 Mutex mu1; 1258 Mutex mu2; 1259 1260 int g GUARDED_BY(mu1); 1261 int w GUARDED_BY(mu2); 1262 1263 class Foo { 1264 public: 1265 void bar() LOCKS_EXCLUDED(mu_, mu1); 1266 int foo() SHARED_LOCKS_REQUIRED(mu_) EXCLUSIVE_LOCKS_REQUIRED(mu2); 1267 1268 private: 1269 int a_ GUARDED_BY(mu_); 1270 public: 1271 Mutex mu_ ACQUIRED_AFTER(mu1); 1272 }; 1273 1274 int Foo::foo() 1275 { 1276 int res; 1277 w = 5; 1278 res = a_ + 5; 1279 return res; 1280 } 1281 1282 void Foo::bar() 1283 { 1284 int x; 1285 mu_.Lock(); 1286 x = foo(); // expected-warning {{calling function 'foo' requires holding mutex 'mu2' exclusively}} 1287 a_ = x + 1; 1288 mu_.Unlock(); 1289 if (x > 5) { 1290 mu1.Lock(); 1291 g = 2; 1292 mu1.Unlock(); 1293 } 1294 } 1295 1296 void main() 1297 { 1298 Foo f1, *f2; 1299 f1.mu_.Lock(); 1300 f1.bar(); // expected-warning {{cannot call function 'bar' while mutex 'f1.mu_' is held}} 1301 mu2.Lock(); 1302 f1.foo(); 1303 mu2.Unlock(); 1304 f1.mu_.Unlock(); 1305 f2->mu_.Lock(); 1306 f2->bar(); // expected-warning {{cannot call function 'bar' while mutex 'f2->mu_' is held}} 1307 f2->mu_.Unlock(); 1308 mu2.Lock(); 1309 w = 2; 1310 mu2.Unlock(); 1311 } 1312 } // end namespace thread_annot_lock_13 1313 1314 namespace thread_annot_lock_18_modified { 1315 // Modified: Trylocks removed 1316 // Test the ability to distnguish between the same lock field of 1317 // different objects of a class. 1318 class Bar { 1319 public: 1320 bool MyLock() EXCLUSIVE_LOCK_FUNCTION(mu1_); 1321 void MyUnlock() UNLOCK_FUNCTION(mu1_); 1322 int a_ GUARDED_BY(mu1_); 1323 1324 private: 1325 Mutex mu1_; 1326 }; 1327 1328 Bar *b1, *b2; 1329 1330 void func() 1331 { 1332 b1->MyLock(); 1333 b1->a_ = 5; 1334 b2->a_ = 3; // \ 1335 // expected-warning {{writing variable 'a_' requires holding mutex 'b2->mu1_' exclusively}} \ 1336 // expected-note {{found near match 'b1->mu1_'}} 1337 b2->MyLock(); 1338 b2->MyUnlock(); 1339 b1->MyUnlock(); 1340 } 1341 } // end namespace thread_annot_lock_18_modified 1342 1343 namespace thread_annot_lock_21 { 1344 // Test various usage of GUARDED_BY and PT_GUARDED_BY annotations, especially 1345 // uses in class definitions. 1346 Mutex mu; 1347 1348 class Bar { 1349 public: 1350 int a_ GUARDED_BY(mu1_); 1351 int b_; 1352 int *q PT_GUARDED_BY(mu); 1353 Mutex mu1_ ACQUIRED_AFTER(mu); 1354 }; 1355 1356 Bar b1, *b3; 1357 int *p GUARDED_BY(mu) PT_GUARDED_BY(mu); 1358 1359 int res GUARDED_BY(mu) = 5; 1360 1361 int func(int i) 1362 { 1363 int x; 1364 b3->mu1_.Lock(); 1365 res = b1.a_ + b3->b_; // expected-warning {{reading variable 'a_' requires holding mutex 'b1.mu1_'}} \ 1366 // expected-warning {{writing variable 'res' requires holding mutex 'mu' exclusively}} \ 1367 // expected-note {{found near match 'b3->mu1_'}} 1368 *p = i; // expected-warning {{reading variable 'p' requires holding mutex 'mu'}} \ 1369 // expected-warning {{writing the value pointed to by 'p' requires holding mutex 'mu' exclusively}} 1370 b1.a_ = res + b3->b_; // expected-warning {{reading variable 'res' requires holding mutex 'mu'}} \ 1371 // expected-warning {{writing variable 'a_' requires holding mutex 'b1.mu1_' exclusively}} \ 1372 // expected-note {{found near match 'b3->mu1_'}} 1373 b3->b_ = *b1.q; // expected-warning {{reading the value pointed to by 'q' requires holding mutex 'mu'}} 1374 b3->mu1_.Unlock(); 1375 b1.b_ = res; // expected-warning {{reading variable 'res' requires holding mutex 'mu'}} 1376 x = res; // expected-warning {{reading variable 'res' requires holding mutex 'mu'}} 1377 return x; 1378 } 1379 } // end namespace thread_annot_lock_21 1380 1381 namespace thread_annot_lock_35_modified { 1382 // Test the analyzer's ability to distinguish the lock field of different 1383 // objects. 1384 class Foo { 1385 private: 1386 Mutex lock_; 1387 int a_ GUARDED_BY(lock_); 1388 1389 public: 1390 void Func(Foo* child) LOCKS_EXCLUDED(lock_) { 1391 Foo *new_foo = new Foo; 1392 1393 lock_.Lock(); 1394 1395 child->Func(new_foo); // There shouldn't be any warning here as the 1396 // acquired lock is not in child. 1397 child->bar(7); // \ 1398 // expected-warning {{calling function 'bar' requires holding mutex 'child->lock_' exclusively}} \ 1399 // expected-note {{found near match 'lock_'}} 1400 child->a_ = 5; // \ 1401 // expected-warning {{writing variable 'a_' requires holding mutex 'child->lock_' exclusively}} \ 1402 // expected-note {{found near match 'lock_'}} 1403 lock_.Unlock(); 1404 } 1405 1406 void bar(int y) EXCLUSIVE_LOCKS_REQUIRED(lock_) { 1407 a_ = y; 1408 } 1409 }; 1410 1411 Foo *x; 1412 1413 void main() { 1414 Foo *child = new Foo; 1415 x->Func(child); 1416 } 1417 } // end namespace thread_annot_lock_35_modified 1418 1419 namespace thread_annot_lock_36_modified { 1420 // Modified to move the annotations to function defns. 1421 // Test the analyzer's ability to distinguish the lock field of different 1422 // objects 1423 class Foo { 1424 private: 1425 Mutex lock_; 1426 int a_ GUARDED_BY(lock_); 1427 1428 public: 1429 void Func(Foo* child) LOCKS_EXCLUDED(lock_); 1430 void bar(int y) EXCLUSIVE_LOCKS_REQUIRED(lock_); 1431 }; 1432 1433 void Foo::Func(Foo* child) { 1434 Foo *new_foo = new Foo; 1435 1436 lock_.Lock(); 1437 1438 child->lock_.Lock(); 1439 child->Func(new_foo); // expected-warning {{cannot call function 'Func' while mutex 'child->lock_' is held}} 1440 child->bar(7); 1441 child->a_ = 5; 1442 child->lock_.Unlock(); 1443 1444 lock_.Unlock(); 1445 } 1446 1447 void Foo::bar(int y) { 1448 a_ = y; 1449 } 1450 1451 1452 Foo *x; 1453 1454 void main() { 1455 Foo *child = new Foo; 1456 x->Func(child); 1457 } 1458 } // end namespace thread_annot_lock_36_modified 1459 1460 1461 namespace thread_annot_lock_42 { 1462 // Test support of multiple lock attributes of the same kind on a decl. 1463 class Foo { 1464 private: 1465 Mutex mu1, mu2, mu3; 1466 int x GUARDED_BY(mu1) GUARDED_BY(mu2); 1467 int y GUARDED_BY(mu2); 1468 1469 void f2() LOCKS_EXCLUDED(mu1) LOCKS_EXCLUDED(mu2) LOCKS_EXCLUDED(mu3) { 1470 mu2.Lock(); 1471 y = 2; 1472 mu2.Unlock(); 1473 } 1474 1475 public: 1476 void f1() EXCLUSIVE_LOCKS_REQUIRED(mu2) EXCLUSIVE_LOCKS_REQUIRED(mu1) { 1477 x = 5; 1478 f2(); // expected-warning {{cannot call function 'f2' while mutex 'mu1' is held}} \ 1479 // expected-warning {{cannot call function 'f2' while mutex 'mu2' is held}} 1480 } 1481 }; 1482 1483 Foo *foo; 1484 1485 void func() 1486 { 1487 foo->f1(); // expected-warning {{calling function 'f1' requires holding mutex 'foo->mu2' exclusively}} \ 1488 // expected-warning {{calling function 'f1' requires holding mutex 'foo->mu1' exclusively}} 1489 } 1490 } // end namespace thread_annot_lock_42 1491 1492 namespace thread_annot_lock_46 { 1493 // Test the support for annotations on virtual functions. 1494 class Base { 1495 public: 1496 virtual void func1() EXCLUSIVE_LOCKS_REQUIRED(mu_); 1497 virtual void func2() LOCKS_EXCLUDED(mu_); 1498 Mutex mu_; 1499 }; 1500 1501 class Child : public Base { 1502 public: 1503 virtual void func1() EXCLUSIVE_LOCKS_REQUIRED(mu_); 1504 virtual void func2() LOCKS_EXCLUDED(mu_); 1505 }; 1506 1507 void main() { 1508 Child *c; 1509 Base *b = c; 1510 1511 b->func1(); // expected-warning {{calling function 'func1' requires holding mutex 'b->mu_' exclusively}} 1512 b->mu_.Lock(); 1513 b->func2(); // expected-warning {{cannot call function 'func2' while mutex 'b->mu_' is held}} 1514 b->mu_.Unlock(); 1515 1516 c->func1(); // expected-warning {{calling function 'func1' requires holding mutex 'c->mu_' exclusively}} 1517 c->mu_.Lock(); 1518 c->func2(); // expected-warning {{cannot call function 'func2' while mutex 'c->mu_' is held}} 1519 c->mu_.Unlock(); 1520 } 1521 } // end namespace thread_annot_lock_46 1522 1523 namespace thread_annot_lock_67_modified { 1524 // Modified: attributes on definitions moved to declarations 1525 // Test annotations on out-of-line definitions of member functions where the 1526 // annotations refer to locks that are also data members in the class. 1527 Mutex mu; 1528 Mutex mu3; 1529 1530 class Foo { 1531 public: 1532 int method1(int i) SHARED_LOCKS_REQUIRED(mu1, mu, mu2, mu3); 1533 int data GUARDED_BY(mu1); 1534 Mutex *mu1; 1535 Mutex *mu2; 1536 }; 1537 1538 int Foo::method1(int i) { 1539 return data + i; 1540 } 1541 1542 void main() 1543 { 1544 Foo a; 1545 a.method1(1); // expected-warning {{calling function 'method1' requires holding mutex 'a.mu1'}} \ 1546 // expected-warning {{calling function 'method1' requires holding mutex 'mu'}} \ 1547 // expected-warning {{calling function 'method1' requires holding mutex 'a.mu2'}} \ 1548 // expected-warning {{calling function 'method1' requires holding mutex 'mu3'}} 1549 } 1550 } // end namespace thread_annot_lock_67_modified 1551 1552 1553 namespace substitution_test { 1554 class MyData { 1555 public: 1556 Mutex mu; 1557 1558 void lockData() EXCLUSIVE_LOCK_FUNCTION(mu); 1559 void unlockData() UNLOCK_FUNCTION(mu); 1560 1561 void doSomething() EXCLUSIVE_LOCKS_REQUIRED(mu) { } 1562 }; 1563 1564 1565 class DataLocker { 1566 public: 1567 void lockData (MyData *d) EXCLUSIVE_LOCK_FUNCTION(d->mu); 1568 void unlockData(MyData *d) UNLOCK_FUNCTION(d->mu); 1569 }; 1570 1571 1572 class Foo { 1573 public: 1574 void foo(MyData* d) EXCLUSIVE_LOCKS_REQUIRED(d->mu) { } 1575 1576 void bar1(MyData* d) { 1577 d->lockData(); 1578 foo(d); 1579 d->unlockData(); 1580 } 1581 1582 void bar2(MyData* d) { 1583 DataLocker dlr; 1584 dlr.lockData(d); 1585 foo(d); 1586 dlr.unlockData(d); 1587 } 1588 1589 void bar3(MyData* d1, MyData* d2) { 1590 DataLocker dlr; 1591 dlr.lockData(d1); // expected-note {{mutex acquired here}} 1592 dlr.unlockData(d2); // \ 1593 // expected-warning {{releasing mutex 'd2->mu' that was not held}} 1594 } // expected-warning {{mutex 'd1->mu' is still held at the end of function}} 1595 1596 void bar4(MyData* d1, MyData* d2) { 1597 DataLocker dlr; 1598 dlr.lockData(d1); 1599 foo(d2); // \ 1600 // expected-warning {{calling function 'foo' requires holding mutex 'd2->mu' exclusively}} \ 1601 // expected-note {{found near match 'd1->mu'}} 1602 dlr.unlockData(d1); 1603 } 1604 }; 1605 } // end namespace substituation_test 1606 1607 1608 1609 namespace constructor_destructor_tests { 1610 Mutex fooMu; 1611 int myVar GUARDED_BY(fooMu); 1612 1613 class Foo { 1614 public: 1615 Foo() EXCLUSIVE_LOCK_FUNCTION(fooMu) { } 1616 ~Foo() UNLOCK_FUNCTION(fooMu) { } 1617 }; 1618 1619 void fooTest() { 1620 Foo foo; 1621 myVar = 0; 1622 } 1623 } 1624 1625 1626 namespace template_member_test { 1627 1628 struct S { int n; }; 1629 struct T { 1630 Mutex m; 1631 S *s GUARDED_BY(this->m); 1632 }; 1633 Mutex m; 1634 struct U { 1635 union { 1636 int n; 1637 }; 1638 } *u GUARDED_BY(m); 1639 1640 template<typename U> 1641 struct IndirectLock { 1642 int DoNaughtyThings(T *t) { 1643 u->n = 0; // expected-warning {{reading variable 'u' requires holding mutex 'm'}} 1644 return t->s->n; // expected-warning {{reading variable 's' requires holding mutex 't->m'}} 1645 } 1646 }; 1647 1648 template struct IndirectLock<int>; // expected-note {{here}} 1649 1650 struct V { 1651 void f(int); 1652 void f(double); 1653 1654 Mutex m; 1655 V *p GUARDED_BY(this->m); 1656 }; 1657 template<typename U> struct W { 1658 V v; 1659 void f(U u) { 1660 v.p->f(u); // expected-warning {{reading variable 'p' requires holding mutex 'v.m'}} 1661 } 1662 }; 1663 template struct W<int>; // expected-note {{here}} 1664 1665 } 1666 1667 namespace test_scoped_lockable { 1668 1669 struct TestScopedLockable { 1670 Mutex mu1; 1671 Mutex mu2; 1672 int a __attribute__((guarded_by(mu1))); 1673 int b __attribute__((guarded_by(mu2))); 1674 1675 bool getBool(); 1676 1677 void foo1() { 1678 MutexLock mulock(&mu1); 1679 a = 5; 1680 } 1681 1682 void foo2() { 1683 ReaderMutexLock mulock1(&mu1); 1684 if (getBool()) { 1685 MutexLock mulock2a(&mu2); 1686 b = a + 1; 1687 } 1688 else { 1689 MutexLock mulock2b(&mu2); 1690 b = a + 2; 1691 } 1692 } 1693 1694 void foo3() { 1695 MutexLock mulock_a(&mu1); // expected-note{{mutex acquired here}} 1696 MutexLock mulock_b(&mu1); // \ 1697 // expected-warning {{acquiring mutex 'mu1' that is already held}} 1698 } 1699 1700 void foo4() { 1701 MutexLock mulock1(&mu1), mulock2(&mu2); 1702 a = b+1; 1703 b = a+1; 1704 } 1705 1706 void foo5() { 1707 DoubleMutexLock mulock(&mu1, &mu2); 1708 a = b + 1; 1709 b = a + 1; 1710 } 1711 }; 1712 1713 } // end namespace test_scoped_lockable 1714 1715 1716 namespace FunctionAttrTest { 1717 1718 class Foo { 1719 public: 1720 Mutex mu_; 1721 int a GUARDED_BY(mu_); 1722 }; 1723 1724 Foo fooObj; 1725 1726 void foo() EXCLUSIVE_LOCKS_REQUIRED(fooObj.mu_); 1727 1728 void bar() { 1729 foo(); // expected-warning {{calling function 'foo' requires holding mutex 'fooObj.mu_' exclusively}} 1730 fooObj.mu_.Lock(); 1731 foo(); 1732 fooObj.mu_.Unlock(); 1733 } 1734 1735 }; // end namespace FunctionAttrTest 1736 1737 1738 namespace TryLockTest { 1739 1740 struct TestTryLock { 1741 Mutex mu; 1742 int a GUARDED_BY(mu); 1743 bool cond; 1744 1745 void foo1() { 1746 if (mu.TryLock()) { 1747 a = 1; 1748 mu.Unlock(); 1749 } 1750 } 1751 1752 void foo2() { 1753 if (!mu.TryLock()) return; 1754 a = 2; 1755 mu.Unlock(); 1756 } 1757 1758 void foo2_builtin_expect() { 1759 if (__builtin_expect(!mu.TryLock(), false)) 1760 return; 1761 a = 2; 1762 mu.Unlock(); 1763 } 1764 1765 void foo3() { 1766 bool b = mu.TryLock(); 1767 if (b) { 1768 a = 3; 1769 mu.Unlock(); 1770 } 1771 } 1772 1773 void foo3_builtin_expect() { 1774 bool b = mu.TryLock(); 1775 if (__builtin_expect(b, true)) { 1776 a = 3; 1777 mu.Unlock(); 1778 } 1779 } 1780 1781 void foo4() { 1782 bool b = mu.TryLock(); 1783 if (!b) return; 1784 a = 4; 1785 mu.Unlock(); 1786 } 1787 1788 void foo5() { 1789 while (mu.TryLock()) { 1790 a = a + 1; 1791 mu.Unlock(); 1792 } 1793 } 1794 1795 void foo6() { 1796 bool b = mu.TryLock(); 1797 b = !b; 1798 if (b) return; 1799 a = 6; 1800 mu.Unlock(); 1801 } 1802 1803 void foo7() { 1804 bool b1 = mu.TryLock(); 1805 bool b2 = !b1; 1806 bool b3 = !b2; 1807 if (b3) { 1808 a = 7; 1809 mu.Unlock(); 1810 } 1811 } 1812 1813 // Test use-def chains: join points 1814 void foo8() { 1815 bool b = mu.TryLock(); 1816 bool b2 = b; 1817 if (cond) 1818 b = true; 1819 if (b) { // b should be unknown at this point, because of the join point 1820 a = 8; // expected-warning {{writing variable 'a' requires holding mutex 'mu' exclusively}} 1821 } 1822 if (b2) { // b2 should be known at this point. 1823 a = 8; 1824 mu.Unlock(); 1825 } 1826 } 1827 1828 // Test use-def-chains: back edges 1829 void foo9() { 1830 bool b = mu.TryLock(); 1831 1832 for (int i = 0; i < 10; ++i); 1833 1834 if (b) { // b is still known, because the loop doesn't alter it 1835 a = 9; 1836 mu.Unlock(); 1837 } 1838 } 1839 1840 // Test use-def chains: back edges 1841 void foo10() { 1842 bool b = mu.TryLock(); 1843 1844 while (cond) { 1845 if (b) { // b should be unknown at this point b/c of the loop 1846 a = 10; // expected-warning {{writing variable 'a' requires holding mutex 'mu' exclusively}} 1847 } 1848 b = !b; 1849 } 1850 } 1851 1852 // Test merge of exclusive trylock 1853 void foo11() { 1854 if (cond) { 1855 if (!mu.TryLock()) 1856 return; 1857 } 1858 else { 1859 mu.Lock(); 1860 } 1861 a = 10; 1862 mu.Unlock(); 1863 } 1864 1865 // Test merge of shared trylock 1866 void foo12() { 1867 if (cond) { 1868 if (!mu.ReaderTryLock()) 1869 return; 1870 } 1871 else { 1872 mu.ReaderLock(); 1873 } 1874 int i = a; 1875 mu.Unlock(); 1876 } 1877 1878 // Test with conditional operator 1879 void foo13() { 1880 if (mu.TryLock() ? 1 : 0) 1881 mu.Unlock(); 1882 } 1883 1884 void foo14() { 1885 if (mu.TryLock() ? 0 : 1) 1886 return; 1887 mu.Unlock(); 1888 } 1889 1890 void foo15() { 1891 if (mu.TryLock() ? 0 : 1) // expected-note{{mutex acquired here}} 1892 mu.Unlock(); // expected-warning{{releasing mutex 'mu' that was not held}} 1893 } // expected-warning{{mutex 'mu' is not held on every path through here}} 1894 }; // end TestTrylock 1895 1896 } // end namespace TrylockTest 1897 1898 1899 namespace TestTemplateAttributeInstantiation { 1900 1901 class Foo1 { 1902 public: 1903 Mutex mu_; 1904 int a GUARDED_BY(mu_); 1905 }; 1906 1907 class Foo2 { 1908 public: 1909 int a GUARDED_BY(mu_); 1910 Mutex mu_; 1911 }; 1912 1913 1914 class Bar { 1915 public: 1916 // Test non-dependent expressions in attributes on template functions 1917 template <class T> 1918 void barND(Foo1 *foo, T *fooT) EXCLUSIVE_LOCKS_REQUIRED(foo->mu_) { 1919 foo->a = 0; 1920 } 1921 1922 // Test dependent expressions in attributes on template functions 1923 template <class T> 1924 void barD(Foo1 *foo, T *fooT) EXCLUSIVE_LOCKS_REQUIRED(fooT->mu_) { 1925 fooT->a = 0; 1926 } 1927 }; 1928 1929 1930 template <class T> 1931 class BarT { 1932 public: 1933 Foo1 fooBase; 1934 T fooBaseT; 1935 1936 // Test non-dependent expression in ordinary method on template class 1937 void barND() EXCLUSIVE_LOCKS_REQUIRED(fooBase.mu_) { 1938 fooBase.a = 0; 1939 } 1940 1941 // Test dependent expressions in ordinary methods on template class 1942 void barD() EXCLUSIVE_LOCKS_REQUIRED(fooBaseT.mu_) { 1943 fooBaseT.a = 0; 1944 } 1945 1946 // Test dependent expressions in template method in template class 1947 template <class T2> 1948 void barTD(T2 *fooT) EXCLUSIVE_LOCKS_REQUIRED(fooBaseT.mu_, fooT->mu_) { 1949 fooBaseT.a = 0; 1950 fooT->a = 0; 1951 } 1952 }; 1953 1954 template <class T> 1955 class Cell { 1956 public: 1957 Mutex mu_; 1958 // Test dependent guarded_by 1959 T data GUARDED_BY(mu_); 1960 1961 void fooEx() EXCLUSIVE_LOCKS_REQUIRED(mu_) { 1962 data = 0; 1963 } 1964 1965 void foo() { 1966 mu_.Lock(); 1967 data = 0; 1968 mu_.Unlock(); 1969 } 1970 }; 1971 1972 void test() { 1973 Bar b; 1974 BarT<Foo2> bt; 1975 Foo1 f1; 1976 Foo2 f2; 1977 1978 f1.mu_.Lock(); 1979 f2.mu_.Lock(); 1980 bt.fooBase.mu_.Lock(); 1981 bt.fooBaseT.mu_.Lock(); 1982 1983 b.barND(&f1, &f2); 1984 b.barD(&f1, &f2); 1985 bt.barND(); 1986 bt.barD(); 1987 bt.barTD(&f2); 1988 1989 f1.mu_.Unlock(); 1990 bt.barTD(&f1); // \ 1991 // expected-warning {{calling function 'barTD<TestTemplateAttributeInstantiation::Foo1>' requires holding mutex 'f1.mu_' exclusively}} \ 1992 // expected-note {{found near match 'bt.fooBase.mu_'}} 1993 1994 bt.fooBase.mu_.Unlock(); 1995 bt.fooBaseT.mu_.Unlock(); 1996 f2.mu_.Unlock(); 1997 1998 Cell<int> cell; 1999 cell.data = 0; // \ 2000 // expected-warning {{writing variable 'data' requires holding mutex 'cell.mu_' exclusively}} 2001 cell.foo(); 2002 cell.mu_.Lock(); 2003 cell.fooEx(); 2004 cell.mu_.Unlock(); 2005 } 2006 2007 2008 template <class T> 2009 class CellDelayed { 2010 public: 2011 // Test dependent guarded_by 2012 T data GUARDED_BY(mu_); 2013 static T static_data GUARDED_BY(static_mu_); 2014 2015 void fooEx(CellDelayed<T> *other) EXCLUSIVE_LOCKS_REQUIRED(mu_, other->mu_) { 2016 this->data = other->data; 2017 } 2018 2019 template <class T2> 2020 void fooExT(CellDelayed<T2> *otherT) EXCLUSIVE_LOCKS_REQUIRED(mu_, otherT->mu_) { 2021 this->data = otherT->data; 2022 } 2023 2024 void foo() { 2025 mu_.Lock(); 2026 data = 0; 2027 mu_.Unlock(); 2028 } 2029 2030 Mutex mu_; 2031 static Mutex static_mu_; 2032 }; 2033 2034 void testDelayed() { 2035 CellDelayed<int> celld; 2036 CellDelayed<int> celld2; 2037 celld.foo(); 2038 celld.mu_.Lock(); 2039 celld2.mu_.Lock(); 2040 2041 celld.fooEx(&celld2); 2042 celld.fooExT(&celld2); 2043 2044 celld2.mu_.Unlock(); 2045 celld.mu_.Unlock(); 2046 } 2047 2048 }; // end namespace TestTemplateAttributeInstantiation 2049 2050 2051 namespace FunctionDeclDefTest { 2052 2053 class Foo { 2054 public: 2055 Mutex mu_; 2056 int a GUARDED_BY(mu_); 2057 2058 virtual void foo1(Foo *f_declared) EXCLUSIVE_LOCKS_REQUIRED(f_declared->mu_); 2059 }; 2060 2061 // EXCLUSIVE_LOCKS_REQUIRED should be applied, and rewritten to f_defined->mu_ 2062 void Foo::foo1(Foo *f_defined) { 2063 f_defined->a = 0; 2064 }; 2065 2066 void test() { 2067 Foo myfoo; 2068 myfoo.foo1(&myfoo); // \ 2069 // expected-warning {{calling function 'foo1' requires holding mutex 'myfoo.mu_' exclusively}} 2070 myfoo.mu_.Lock(); 2071 myfoo.foo1(&myfoo); 2072 myfoo.mu_.Unlock(); 2073 } 2074 2075 }; 2076 2077 namespace GoingNative { 2078 2079 struct LOCKABLE mutex { 2080 void lock() EXCLUSIVE_LOCK_FUNCTION(); 2081 void unlock() UNLOCK_FUNCTION(); 2082 // ... 2083 }; 2084 bool foo(); 2085 bool bar(); 2086 mutex m; 2087 void test() { 2088 m.lock(); 2089 while (foo()) { 2090 m.unlock(); 2091 // ... 2092 if (bar()) { 2093 // ... 2094 if (foo()) 2095 continue; // expected-warning {{expecting mutex 'm' to be held at start of each loop}} 2096 //... 2097 } 2098 // ... 2099 m.lock(); // expected-note {{mutex acquired here}} 2100 } 2101 m.unlock(); 2102 } 2103 2104 } 2105 2106 2107 2108 namespace FunctionDefinitionTest { 2109 2110 class Foo { 2111 public: 2112 void foo1(); 2113 void foo2(); 2114 void foo3(Foo *other); 2115 2116 template<class T> 2117 void fooT1(const T& dummy1); 2118 2119 template<class T> 2120 void fooT2(const T& dummy2) EXCLUSIVE_LOCKS_REQUIRED(mu_); 2121 2122 Mutex mu_; 2123 int a GUARDED_BY(mu_); 2124 }; 2125 2126 template<class T> 2127 class FooT { 2128 public: 2129 void foo(); 2130 2131 Mutex mu_; 2132 T a GUARDED_BY(mu_); 2133 }; 2134 2135 2136 void Foo::foo1() NO_THREAD_SAFETY_ANALYSIS { 2137 a = 1; 2138 } 2139 2140 void Foo::foo2() EXCLUSIVE_LOCKS_REQUIRED(mu_) { 2141 a = 2; 2142 } 2143 2144 void Foo::foo3(Foo *other) EXCLUSIVE_LOCKS_REQUIRED(other->mu_) { 2145 other->a = 3; 2146 } 2147 2148 template<class T> 2149 void Foo::fooT1(const T& dummy1) EXCLUSIVE_LOCKS_REQUIRED(mu_) { 2150 a = dummy1; 2151 } 2152 2153 /* TODO -- uncomment with template instantiation of attributes. 2154 template<class T> 2155 void Foo::fooT2(const T& dummy2) { 2156 a = dummy2; 2157 } 2158 */ 2159 2160 void fooF1(Foo *f) EXCLUSIVE_LOCKS_REQUIRED(f->mu_) { 2161 f->a = 1; 2162 } 2163 2164 void fooF2(Foo *f); 2165 void fooF2(Foo *f) EXCLUSIVE_LOCKS_REQUIRED(f->mu_) { 2166 f->a = 2; 2167 } 2168 2169 void fooF3(Foo *f) EXCLUSIVE_LOCKS_REQUIRED(f->mu_); 2170 void fooF3(Foo *f) { 2171 f->a = 3; 2172 } 2173 2174 template<class T> 2175 void FooT<T>::foo() EXCLUSIVE_LOCKS_REQUIRED(mu_) { 2176 a = 0; 2177 } 2178 2179 void test() { 2180 int dummy = 0; 2181 Foo myFoo; 2182 2183 myFoo.foo2(); // \ 2184 // expected-warning {{calling function 'foo2' requires holding mutex 'myFoo.mu_' exclusively}} 2185 myFoo.foo3(&myFoo); // \ 2186 // expected-warning {{calling function 'foo3' requires holding mutex 'myFoo.mu_' exclusively}} 2187 myFoo.fooT1(dummy); // \ 2188 // expected-warning {{calling function 'fooT1<int>' requires holding mutex 'myFoo.mu_' exclusively}} 2189 2190 myFoo.fooT2(dummy); // \ 2191 // expected-warning {{calling function 'fooT2<int>' requires holding mutex 'myFoo.mu_' exclusively}} 2192 2193 fooF1(&myFoo); // \ 2194 // expected-warning {{calling function 'fooF1' requires holding mutex 'myFoo.mu_' exclusively}} 2195 fooF2(&myFoo); // \ 2196 // expected-warning {{calling function 'fooF2' requires holding mutex 'myFoo.mu_' exclusively}} 2197 fooF3(&myFoo); // \ 2198 // expected-warning {{calling function 'fooF3' requires holding mutex 'myFoo.mu_' exclusively}} 2199 2200 myFoo.mu_.Lock(); 2201 myFoo.foo2(); 2202 myFoo.foo3(&myFoo); 2203 myFoo.fooT1(dummy); 2204 2205 myFoo.fooT2(dummy); 2206 2207 fooF1(&myFoo); 2208 fooF2(&myFoo); 2209 fooF3(&myFoo); 2210 myFoo.mu_.Unlock(); 2211 2212 FooT<int> myFooT; 2213 myFooT.foo(); // \ 2214 // expected-warning {{calling function 'foo' requires holding mutex 'myFooT.mu_' exclusively}} 2215 } 2216 2217 } // end namespace FunctionDefinitionTest 2218 2219 2220 namespace SelfLockingTest { 2221 2222 class LOCKABLE MyLock { 2223 public: 2224 int foo GUARDED_BY(this); 2225 2226 void lock() EXCLUSIVE_LOCK_FUNCTION(); 2227 void unlock() UNLOCK_FUNCTION(); 2228 2229 void doSomething() { 2230 this->lock(); // allow 'this' as a lock expression 2231 foo = 0; 2232 doSomethingElse(); 2233 this->unlock(); 2234 } 2235 2236 void doSomethingElse() EXCLUSIVE_LOCKS_REQUIRED(this) { 2237 foo = 1; 2238 }; 2239 2240 void test() { 2241 foo = 2; // \ 2242 // expected-warning {{writing variable 'foo' requires holding mutex 'this' exclusively}} 2243 } 2244 }; 2245 2246 2247 class LOCKABLE MyLock2 { 2248 public: 2249 Mutex mu_; 2250 int foo GUARDED_BY(this); 2251 2252 // don't check inside lock and unlock functions 2253 void lock() EXCLUSIVE_LOCK_FUNCTION() { mu_.Lock(); } 2254 void unlock() UNLOCK_FUNCTION() { mu_.Unlock(); } 2255 2256 // don't check inside constructors and destructors 2257 MyLock2() { foo = 1; } 2258 ~MyLock2() { foo = 0; } 2259 }; 2260 2261 2262 } // end namespace SelfLockingTest 2263 2264 2265 namespace InvalidNonstatic { 2266 2267 // Forward decl here causes bogus "invalid use of non-static data member" 2268 // on reference to mutex_ in guarded_by attribute. 2269 class Foo; 2270 2271 class Foo { 2272 Mutex* mutex_; 2273 2274 int foo __attribute__((guarded_by(mutex_))); 2275 }; 2276 2277 } // end namespace InvalidNonStatic 2278 2279 2280 namespace NoReturnTest { 2281 2282 bool condition(); 2283 void fatal() __attribute__((noreturn)); 2284 2285 Mutex mu_; 2286 2287 void test1() { 2288 MutexLock lock(&mu_); 2289 if (condition()) { 2290 fatal(); 2291 return; 2292 } 2293 } 2294 2295 } // end namespace NoReturnTest 2296 2297 2298 namespace TestMultiDecl { 2299 2300 class Foo { 2301 public: 2302 int GUARDED_BY(mu_) a; 2303 int GUARDED_BY(mu_) b, c; 2304 2305 void foo() { 2306 a = 0; // \ 2307 // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}} 2308 b = 0; // \ 2309 // expected-warning {{writing variable 'b' requires holding mutex 'mu_' exclusively}} 2310 c = 0; // \ 2311 // expected-warning {{writing variable 'c' requires holding mutex 'mu_' exclusively}} 2312 } 2313 2314 private: 2315 Mutex mu_; 2316 }; 2317 2318 } // end namespace TestMultiDecl 2319 2320 2321 namespace WarnNoDecl { 2322 2323 class Foo { 2324 void foo(int a); __attribute__(( // \ 2325 // expected-warning {{declaration does not declare anything}} 2326 exclusive_locks_required(a))); // \ 2327 // expected-warning {{attribute exclusive_locks_required ignored}} 2328 }; 2329 2330 } // end namespace WarnNoDecl 2331 2332 2333 2334 namespace MoreLockExpressions { 2335 2336 class Foo { 2337 public: 2338 Mutex mu_; 2339 int a GUARDED_BY(mu_); 2340 }; 2341 2342 class Bar { 2343 public: 2344 int b; 2345 Foo* f; 2346 2347 Foo& getFoo() { return *f; } 2348 Foo& getFoo2(int c) { return *f; } 2349 Foo& getFoo3(int c, int d) { return *f; } 2350 2351 Foo& getFooey() { return *f; } 2352 }; 2353 2354 Foo& getBarFoo(Bar &bar, int c) { return bar.getFoo2(c); } 2355 2356 void test() { 2357 Foo foo; 2358 Foo *fooArray; 2359 Foo &(*fooFuncPtr)(); 2360 Bar bar; 2361 int a; 2362 int b; 2363 int c; 2364 2365 bar.getFoo().mu_.Lock(); 2366 bar.getFoo().a = 0; 2367 bar.getFoo().mu_.Unlock(); 2368 2369 (bar.getFoo().mu_).Lock(); // test parenthesis 2370 bar.getFoo().a = 0; 2371 (bar.getFoo().mu_).Unlock(); 2372 2373 bar.getFoo2(a).mu_.Lock(); 2374 bar.getFoo2(a).a = 0; 2375 bar.getFoo2(a).mu_.Unlock(); 2376 2377 bar.getFoo3(a, b).mu_.Lock(); 2378 bar.getFoo3(a, b).a = 0; 2379 bar.getFoo3(a, b).mu_.Unlock(); 2380 2381 getBarFoo(bar, a).mu_.Lock(); 2382 getBarFoo(bar, a).a = 0; 2383 getBarFoo(bar, a).mu_.Unlock(); 2384 2385 bar.getFoo2(10).mu_.Lock(); 2386 bar.getFoo2(10).a = 0; 2387 bar.getFoo2(10).mu_.Unlock(); 2388 2389 bar.getFoo2(a + 1).mu_.Lock(); 2390 bar.getFoo2(a + 1).a = 0; 2391 bar.getFoo2(a + 1).mu_.Unlock(); 2392 2393 (a > 0 ? fooArray[1] : fooArray[b]).mu_.Lock(); 2394 (a > 0 ? fooArray[1] : fooArray[b]).a = 0; 2395 (a > 0 ? fooArray[1] : fooArray[b]).mu_.Unlock(); 2396 2397 fooFuncPtr().mu_.Lock(); 2398 fooFuncPtr().a = 0; 2399 fooFuncPtr().mu_.Unlock(); 2400 } 2401 2402 2403 void test2() { 2404 Foo *fooArray; 2405 Bar bar; 2406 int a; 2407 int b; 2408 int c; 2409 2410 bar.getFoo().mu_.Lock(); 2411 bar.getFooey().a = 0; // \ 2412 // expected-warning {{writing variable 'a' requires holding mutex 'bar.getFooey().mu_' exclusively}} \ 2413 // expected-note {{found near match 'bar.getFoo().mu_'}} 2414 bar.getFoo().mu_.Unlock(); 2415 2416 bar.getFoo2(a).mu_.Lock(); 2417 bar.getFoo2(b).a = 0; // \ 2418 // expected-warning {{writing variable 'a' requires holding mutex 'bar.getFoo2(b).mu_' exclusively}} \ 2419 // expected-note {{found near match 'bar.getFoo2(a).mu_'}} 2420 bar.getFoo2(a).mu_.Unlock(); 2421 2422 bar.getFoo3(a, b).mu_.Lock(); 2423 bar.getFoo3(a, c).a = 0; // \ 2424 // expected-warning {{writing variable 'a' requires holding mutex 'bar.getFoo3(a, c).mu_' exclusively}} \ 2425 // expected-note {{found near match 'bar.getFoo3(a, b).mu_'}} 2426 bar.getFoo3(a, b).mu_.Unlock(); 2427 2428 getBarFoo(bar, a).mu_.Lock(); 2429 getBarFoo(bar, b).a = 0; // \ 2430 // expected-warning {{writing variable 'a' requires holding mutex 'getBarFoo(bar, b).mu_' exclusively}} \ 2431 // expected-note {{found near match 'getBarFoo(bar, a).mu_'}} 2432 getBarFoo(bar, a).mu_.Unlock(); 2433 2434 (a > 0 ? fooArray[1] : fooArray[b]).mu_.Lock(); 2435 (a > 0 ? fooArray[b] : fooArray[c]).a = 0; // \ 2436 // expected-warning {{writing variable 'a' requires holding mutex '((0 < a) ? fooArray[b] : fooArray[c]).mu_' exclusively}} \ 2437 // expected-note {{found near match '((0 < a) ? fooArray[1] : fooArray[b]).mu_'}} 2438 (a > 0 ? fooArray[1] : fooArray[b]).mu_.Unlock(); 2439 } 2440 2441 2442 } // end namespace MoreLockExpressions 2443 2444 2445 namespace TrylockJoinPoint { 2446 2447 class Foo { 2448 Mutex mu; 2449 bool c; 2450 2451 void foo() { 2452 if (c) { 2453 if (!mu.TryLock()) 2454 return; 2455 } else { 2456 mu.Lock(); 2457 } 2458 mu.Unlock(); 2459 } 2460 }; 2461 2462 } // end namespace TrylockJoinPoint 2463 2464 2465 namespace LockReturned { 2466 2467 class Foo { 2468 public: 2469 int a GUARDED_BY(mu_); 2470 void foo() EXCLUSIVE_LOCKS_REQUIRED(mu_); 2471 void foo2(Foo* f) EXCLUSIVE_LOCKS_REQUIRED(mu_, f->mu_); 2472 2473 static void sfoo(Foo* f) EXCLUSIVE_LOCKS_REQUIRED(f->mu_); 2474 2475 Mutex* getMu() LOCK_RETURNED(mu_); 2476 2477 Mutex mu_; 2478 2479 static Mutex* getMu(Foo* f) LOCK_RETURNED(f->mu_); 2480 }; 2481 2482 2483 // Calls getMu() directly to lock and unlock 2484 void test1(Foo* f1, Foo* f2) { 2485 f1->a = 0; // expected-warning {{writing variable 'a' requires holding mutex 'f1->mu_' exclusively}} 2486 f1->foo(); // expected-warning {{calling function 'foo' requires holding mutex 'f1->mu_' exclusively}} 2487 2488 f1->foo2(f2); // expected-warning {{calling function 'foo2' requires holding mutex 'f1->mu_' exclusively}} \ 2489 // expected-warning {{calling function 'foo2' requires holding mutex 'f2->mu_' exclusively}} 2490 Foo::sfoo(f1); // expected-warning {{calling function 'sfoo' requires holding mutex 'f1->mu_' exclusively}} 2491 2492 f1->getMu()->Lock(); 2493 2494 f1->a = 0; 2495 f1->foo(); 2496 f1->foo2(f2); // \ 2497 // expected-warning {{calling function 'foo2' requires holding mutex 'f2->mu_' exclusively}} \ 2498 // expected-note {{found near match 'f1->mu_'}} 2499 2500 Foo::getMu(f2)->Lock(); 2501 f1->foo2(f2); 2502 Foo::getMu(f2)->Unlock(); 2503 2504 Foo::sfoo(f1); 2505 2506 f1->getMu()->Unlock(); 2507 } 2508 2509 2510 Mutex* getFooMu(Foo* f) LOCK_RETURNED(Foo::getMu(f)); 2511 2512 class Bar : public Foo { 2513 public: 2514 int b GUARDED_BY(getMu()); 2515 void bar() EXCLUSIVE_LOCKS_REQUIRED(getMu()); 2516 void bar2(Bar* g) EXCLUSIVE_LOCKS_REQUIRED(getMu(this), g->getMu()); 2517 2518 static void sbar(Bar* g) EXCLUSIVE_LOCKS_REQUIRED(g->getMu()); 2519 static void sbar2(Bar* g) EXCLUSIVE_LOCKS_REQUIRED(getFooMu(g)); 2520 }; 2521 2522 2523 2524 // Use getMu() within other attributes. 2525 // This requires at lest levels of substitution, more in the case of 2526 void test2(Bar* b1, Bar* b2) { 2527 b1->b = 0; // expected-warning {{writing variable 'b' requires holding mutex 'b1->mu_' exclusively}} 2528 b1->bar(); // expected-warning {{calling function 'bar' requires holding mutex 'b1->mu_' exclusively}} 2529 b1->bar2(b2); // expected-warning {{calling function 'bar2' requires holding mutex 'b1->mu_' exclusively}} \ 2530 // expected-warning {{calling function 'bar2' requires holding mutex 'b2->mu_' exclusively}} 2531 Bar::sbar(b1); // expected-warning {{calling function 'sbar' requires holding mutex 'b1->mu_' exclusively}} 2532 Bar::sbar2(b1); // expected-warning {{calling function 'sbar2' requires holding mutex 'b1->mu_' exclusively}} 2533 2534 b1->getMu()->Lock(); 2535 2536 b1->b = 0; 2537 b1->bar(); 2538 b1->bar2(b2); // \ 2539 // expected-warning {{calling function 'bar2' requires holding mutex 'b2->mu_' exclusively}} \ 2540 // // expected-note {{found near match 'b1->mu_'}} 2541 2542 b2->getMu()->Lock(); 2543 b1->bar2(b2); 2544 2545 b2->getMu()->Unlock(); 2546 2547 Bar::sbar(b1); 2548 Bar::sbar2(b1); 2549 2550 b1->getMu()->Unlock(); 2551 } 2552 2553 2554 // Sanity check -- lock the mutex directly, but use attributes that call getMu() 2555 // Also lock the mutex using getFooMu, which calls a lock_returned function. 2556 void test3(Bar* b1, Bar* b2) { 2557 b1->mu_.Lock(); 2558 b1->b = 0; 2559 b1->bar(); 2560 2561 getFooMu(b2)->Lock(); 2562 b1->bar2(b2); 2563 getFooMu(b2)->Unlock(); 2564 2565 Bar::sbar(b1); 2566 Bar::sbar2(b1); 2567 2568 b1->mu_.Unlock(); 2569 } 2570 2571 } // end namespace LockReturned 2572 2573 2574 namespace ReleasableScopedLock { 2575 2576 class Foo { 2577 Mutex mu_; 2578 bool c; 2579 int a GUARDED_BY(mu_); 2580 2581 void test1(); 2582 void test2(); 2583 void test3(); 2584 void test4(); 2585 void test5(); 2586 void test6(); 2587 }; 2588 2589 2590 void Foo::test1() { 2591 ReleasableMutexLock rlock(&mu_); 2592 rlock.Release(); 2593 } 2594 2595 void Foo::test2() { 2596 ReleasableMutexLock rlock(&mu_); 2597 if (c) { // test join point -- held/not held during release 2598 rlock.Release(); 2599 } 2600 // No warning on join point because the lock will be released by the scope object anyway. 2601 } 2602 2603 void Foo::test3() { 2604 ReleasableMutexLock rlock(&mu_); 2605 a = 0; 2606 rlock.Release(); 2607 a = 1; // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}} 2608 } 2609 2610 void Foo::test4() { 2611 ReleasableMutexLock rlock(&mu_); 2612 rlock.Release(); // expected-note{{mutex released here}} 2613 rlock.Release(); // expected-warning {{releasing mutex 'mu_' that was not held}} 2614 } 2615 2616 void Foo::test5() { 2617 ReleasableMutexLock rlock(&mu_); 2618 if (c) { 2619 rlock.Release(); 2620 } 2621 // No warning on join point because the lock will be released by the scope object anyway. 2622 rlock.Release(); // expected-warning {{releasing mutex 'mu_' that was not held}} 2623 } 2624 2625 void Foo::test6() { 2626 ReleasableMutexLock rlock(&mu_); 2627 do { 2628 if (c) { 2629 rlock.Release(); 2630 break; 2631 } 2632 } while (c); 2633 // No warning on join point because the lock will be released by the scope object anyway 2634 a = 1; // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}} 2635 } 2636 2637 2638 } // end namespace ReleasableScopedLock 2639 2640 2641 namespace RelockableScopedLock { 2642 2643 class DeferTraits {}; 2644 2645 class SCOPED_LOCKABLE RelockableExclusiveMutexLock { 2646 public: 2647 RelockableExclusiveMutexLock(Mutex *mu) EXCLUSIVE_LOCK_FUNCTION(mu); 2648 RelockableExclusiveMutexLock(Mutex *mu, DeferTraits) LOCKS_EXCLUDED(mu); 2649 ~RelockableExclusiveMutexLock() EXCLUSIVE_UNLOCK_FUNCTION(); 2650 2651 void Lock() EXCLUSIVE_LOCK_FUNCTION(); 2652 void Unlock() UNLOCK_FUNCTION(); 2653 }; 2654 2655 struct SharedTraits {}; 2656 struct ExclusiveTraits {}; 2657 2658 class SCOPED_LOCKABLE RelockableMutexLock { 2659 public: 2660 RelockableMutexLock(Mutex *mu, DeferTraits) LOCKS_EXCLUDED(mu); 2661 RelockableMutexLock(Mutex *mu, SharedTraits) SHARED_LOCK_FUNCTION(mu); 2662 RelockableMutexLock(Mutex *mu, ExclusiveTraits) EXCLUSIVE_LOCK_FUNCTION(mu); 2663 ~RelockableMutexLock() UNLOCK_FUNCTION(); 2664 2665 void Lock() EXCLUSIVE_LOCK_FUNCTION(); 2666 void Unlock() UNLOCK_FUNCTION(); 2667 2668 void ReaderLock() SHARED_LOCK_FUNCTION(); 2669 void ReaderUnlock() UNLOCK_FUNCTION(); 2670 2671 void PromoteShared() UNLOCK_FUNCTION() EXCLUSIVE_LOCK_FUNCTION(); 2672 void DemoteExclusive() UNLOCK_FUNCTION() SHARED_LOCK_FUNCTION(); 2673 }; 2674 2675 Mutex mu; 2676 int x GUARDED_BY(mu); 2677 bool b; 2678 2679 void print(int); 2680 2681 void relock() { 2682 RelockableExclusiveMutexLock scope(&mu); 2683 x = 2; 2684 scope.Unlock(); 2685 2686 x = 3; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}} 2687 2688 scope.Lock(); 2689 x = 4; 2690 } 2691 2692 void deferLock() { 2693 RelockableExclusiveMutexLock scope(&mu, DeferTraits{}); 2694 x = 2; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}} 2695 scope.Lock(); 2696 x = 3; 2697 } 2698 2699 void relockExclusive() { 2700 RelockableMutexLock scope(&mu, SharedTraits{}); 2701 print(x); 2702 x = 2; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}} 2703 scope.ReaderUnlock(); 2704 2705 print(x); // expected-warning {{reading variable 'x' requires holding mutex 'mu'}} 2706 2707 scope.Lock(); 2708 print(x); 2709 x = 4; 2710 2711 scope.DemoteExclusive(); 2712 print(x); 2713 x = 5; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}} 2714 } 2715 2716 void relockShared() { 2717 RelockableMutexLock scope(&mu, ExclusiveTraits{}); 2718 print(x); 2719 x = 2; 2720 scope.Unlock(); 2721 2722 print(x); // expected-warning {{reading variable 'x' requires holding mutex 'mu'}} 2723 2724 scope.ReaderLock(); 2725 print(x); 2726 x = 4; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}} 2727 2728 scope.PromoteShared(); 2729 print(x); 2730 x = 5; 2731 } 2732 2733 void deferLockShared() { 2734 RelockableMutexLock scope(&mu, DeferTraits{}); 2735 print(x); // expected-warning {{reading variable 'x' requires holding mutex 'mu'}} 2736 scope.ReaderLock(); 2737 print(x); 2738 x = 2; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}} 2739 } 2740 2741 void doubleUnlock() { 2742 RelockableExclusiveMutexLock scope(&mu); 2743 scope.Unlock(); // expected-note{{mutex released here}} 2744 scope.Unlock(); // expected-warning {{releasing mutex 'mu' that was not held}} 2745 } 2746 2747 void doubleLock1() { 2748 RelockableExclusiveMutexLock scope(&mu); // expected-note{{mutex acquired here}} 2749 scope.Lock(); // expected-warning {{acquiring mutex 'mu' that is already held}} 2750 } 2751 2752 void doubleLock2() { 2753 RelockableExclusiveMutexLock scope(&mu); 2754 scope.Unlock(); 2755 scope.Lock(); // expected-note{{mutex acquired here}} 2756 scope.Lock(); // expected-warning {{acquiring mutex 'mu' that is already held}} 2757 } 2758 2759 void lockJoin() { 2760 RelockableMutexLock scope(&mu, DeferTraits{}); 2761 if (b) 2762 scope.Lock(); 2763 // No warning on join point because the lock will be released by the scope object anyway. 2764 x = 2; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}} 2765 } 2766 2767 void unlockJoin() { 2768 RelockableMutexLock scope(&mu, DeferTraits{}); 2769 scope.Lock(); 2770 if (b) 2771 scope.Unlock(); 2772 // No warning on join point because the lock will be released by the scope object anyway. 2773 x = 2; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}} 2774 } 2775 2776 void directUnlock() { 2777 RelockableExclusiveMutexLock scope(&mu); 2778 mu.Unlock(); 2779 // Debatable that there is no warning. Currently we don't track in the scoped 2780 // object whether it is active, but just check if the contained locks can be 2781 // reacquired. Here they can, because mu has been unlocked manually. 2782 scope.Lock(); 2783 } 2784 2785 void directRelock() { 2786 RelockableExclusiveMutexLock scope(&mu); 2787 scope.Unlock(); 2788 mu.Lock(); 2789 // Similarly debatable that there is no warning. 2790 scope.Unlock(); 2791 } 2792 2793 // Doesn't make a lot of sense, just making sure there is no crash. 2794 void destructLock() { 2795 RelockableExclusiveMutexLock scope(&mu); 2796 scope.~RelockableExclusiveMutexLock(); 2797 scope.Lock(); // Should be UB, so we don't really care. 2798 } 2799 2800 class SCOPED_LOCKABLE MemberLock { 2801 public: 2802 MemberLock() EXCLUSIVE_LOCK_FUNCTION(mutex); 2803 ~MemberLock() UNLOCK_FUNCTION(mutex); 2804 void Lock() EXCLUSIVE_LOCK_FUNCTION(mutex); 2805 Mutex mutex; 2806 }; 2807 2808 void relockShared2() { 2809 MemberLock lock; // expected-note{{mutex acquired here}} 2810 lock.Lock(); // expected-warning {{acquiring mutex 'lock.mutex' that is already held}} 2811 } 2812 2813 class SCOPED_LOCKABLE WeirdScope { 2814 private: 2815 Mutex *other; 2816 2817 public: 2818 WeirdScope(Mutex *mutex) EXCLUSIVE_LOCK_FUNCTION(mutex); 2819 void unlock() EXCLUSIVE_UNLOCK_FUNCTION() EXCLUSIVE_UNLOCK_FUNCTION(other); 2820 void lock() EXCLUSIVE_LOCK_FUNCTION() EXCLUSIVE_LOCK_FUNCTION(other); 2821 ~WeirdScope() EXCLUSIVE_UNLOCK_FUNCTION(); 2822 2823 void requireOther() EXCLUSIVE_LOCKS_REQUIRED(other); 2824 }; 2825 2826 void relockWeird() { 2827 WeirdScope scope(&mu); 2828 x = 1; 2829 scope.unlock(); // expected-warning {{releasing mutex 'scope.other' that was not held}} 2830 x = 2; // \ 2831 // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}} 2832 scope.requireOther(); // \ 2833 // expected-warning {{calling function 'requireOther' requires holding mutex 'scope.other' exclusively}} 2834 scope.lock(); // expected-note {{mutex acquired here}} 2835 x = 3; 2836 scope.requireOther(); 2837 } // expected-warning {{mutex 'scope.other' is still held at the end of function}} 2838 2839 } // end namespace RelockableScopedLock 2840 2841 2842 namespace ScopedUnlock { 2843 2844 class SCOPED_LOCKABLE MutexUnlock { 2845 public: 2846 MutexUnlock(Mutex *mu) EXCLUSIVE_UNLOCK_FUNCTION(mu); 2847 ~MutexUnlock() EXCLUSIVE_UNLOCK_FUNCTION(); 2848 2849 void Lock() EXCLUSIVE_UNLOCK_FUNCTION(); 2850 void Unlock() EXCLUSIVE_LOCK_FUNCTION(); 2851 }; 2852 2853 class SCOPED_LOCKABLE ReaderMutexUnlock { 2854 public: 2855 ReaderMutexUnlock(Mutex *mu) SHARED_UNLOCK_FUNCTION(mu); 2856 ~ReaderMutexUnlock() EXCLUSIVE_UNLOCK_FUNCTION(); 2857 2858 void Lock() EXCLUSIVE_UNLOCK_FUNCTION(); 2859 void Unlock() EXCLUSIVE_LOCK_FUNCTION(); 2860 }; 2861 2862 Mutex mu; 2863 int x GUARDED_BY(mu); 2864 bool c; 2865 void print(int); 2866 2867 void simple() EXCLUSIVE_LOCKS_REQUIRED(mu) { 2868 x = 1; 2869 MutexUnlock scope(&mu); 2870 x = 2; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}} 2871 } 2872 2873 void simpleShared() SHARED_LOCKS_REQUIRED(mu) { 2874 print(x); 2875 ReaderMutexUnlock scope(&mu); 2876 print(x); // expected-warning {{reading variable 'x' requires holding mutex 'mu'}} 2877 } 2878 2879 void innerUnlock() { 2880 MutexLock outer(&mu); 2881 if (x == 0) { 2882 MutexUnlock inner(&mu); 2883 x = 1; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}} 2884 } 2885 x = 2; 2886 } 2887 2888 void innerUnlockShared() { 2889 ReaderMutexLock outer(&mu); 2890 if (x == 0) { 2891 ReaderMutexUnlock inner(&mu); 2892 print(x); // expected-warning {{reading variable 'x' requires holding mutex 'mu'}} 2893 } 2894 print(x); 2895 } 2896 2897 void manual() EXCLUSIVE_LOCKS_REQUIRED(mu) { 2898 MutexUnlock scope(&mu); 2899 scope.Lock(); 2900 x = 2; 2901 scope.Unlock(); 2902 x = 3; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}} 2903 } 2904 2905 void join() EXCLUSIVE_LOCKS_REQUIRED(mu) { 2906 MutexUnlock scope(&mu); 2907 if (c) 2908 scope.Lock(); 2909 // No warning on join point because the lock will be released by the scope object anyway. 2910 scope.Lock(); 2911 } 2912 2913 void doubleLock() EXCLUSIVE_LOCKS_REQUIRED(mu) { 2914 MutexUnlock scope(&mu); 2915 scope.Lock(); // expected-note{{mutex acquired here}} 2916 scope.Lock(); // expected-warning {{acquiring mutex 'mu' that is already held}} 2917 } 2918 2919 void doubleUnlock() EXCLUSIVE_LOCKS_REQUIRED(mu) { 2920 MutexUnlock scope(&mu); // expected-note{{mutex released here}} 2921 scope.Unlock(); // expected-warning {{releasing mutex 'mu' that was not held}} 2922 } 2923 2924 class SCOPED_LOCKABLE MutexLockUnlock { 2925 public: 2926 MutexLockUnlock(Mutex *mu1, Mutex *mu2) EXCLUSIVE_UNLOCK_FUNCTION(mu1) EXCLUSIVE_LOCK_FUNCTION(mu2); 2927 ~MutexLockUnlock() EXCLUSIVE_UNLOCK_FUNCTION(); 2928 2929 void Release() EXCLUSIVE_UNLOCK_FUNCTION(); 2930 void Acquire() EXCLUSIVE_LOCK_FUNCTION(); 2931 }; 2932 2933 Mutex other; 2934 void fn() EXCLUSIVE_LOCKS_REQUIRED(other); 2935 2936 void lockUnlock() EXCLUSIVE_LOCKS_REQUIRED(mu) { 2937 MutexLockUnlock scope(&mu, &other); 2938 fn(); 2939 x = 1; // expected-warning {{writing variable 'x' requires holding mutex 'mu' exclusively}} 2940 } 2941 2942 } // end namespace ScopedUnlock 2943 2944 2945 namespace TrylockFunctionTest { 2946 2947 class Foo { 2948 public: 2949 Mutex mu1_; 2950 Mutex mu2_; 2951 bool c; 2952 2953 bool lockBoth() EXCLUSIVE_TRYLOCK_FUNCTION(true, mu1_, mu2_); 2954 }; 2955 2956 bool Foo::lockBoth() { 2957 if (!mu1_.TryLock()) 2958 return false; 2959 2960 mu2_.Lock(); 2961 if (!c) { 2962 mu1_.Unlock(); 2963 mu2_.Unlock(); 2964 return false; 2965 } 2966 2967 return true; 2968 } 2969 2970 2971 } // end namespace TrylockFunctionTest 2972 2973 2974 2975 namespace DoubleLockBug { 2976 2977 class Foo { 2978 public: 2979 Mutex mu_; 2980 int a GUARDED_BY(mu_); 2981 2982 void foo1() EXCLUSIVE_LOCKS_REQUIRED(mu_); 2983 int foo2() SHARED_LOCKS_REQUIRED(mu_); 2984 }; 2985 2986 2987 void Foo::foo1() EXCLUSIVE_LOCKS_REQUIRED(mu_) { 2988 a = 0; 2989 } 2990 2991 int Foo::foo2() SHARED_LOCKS_REQUIRED(mu_) { 2992 return a; 2993 } 2994 2995 } 2996 2997 2998 2999 namespace UnlockBug { 3000 3001 class Foo { 3002 public: 3003 Mutex mutex_; 3004 3005 void foo1() EXCLUSIVE_LOCKS_REQUIRED(mutex_) { // expected-note {{mutex acquired here}} 3006 mutex_.Unlock(); 3007 } // expected-warning {{expecting mutex 'mutex_' to be held at the end of function}} 3008 3009 3010 void foo2() SHARED_LOCKS_REQUIRED(mutex_) { // expected-note {{mutex acquired here}} 3011 mutex_.Unlock(); 3012 } // expected-warning {{expecting mutex 'mutex_' to be held at the end of function}} 3013 }; 3014 3015 } // end namespace UnlockBug 3016 3017 3018 3019 namespace FoolishScopedLockableBug { 3020 3021 class SCOPED_LOCKABLE WTF_ScopedLockable { 3022 public: 3023 WTF_ScopedLockable(Mutex* mu) EXCLUSIVE_LOCK_FUNCTION(mu); 3024 3025 // have to call release() manually; 3026 ~WTF_ScopedLockable(); 3027 3028 void release() UNLOCK_FUNCTION(); 3029 }; 3030 3031 3032 class Foo { 3033 Mutex mu_; 3034 int a GUARDED_BY(mu_); 3035 bool c; 3036 3037 void doSomething(); 3038 3039 void test1() { 3040 WTF_ScopedLockable wtf(&mu_); 3041 wtf.release(); 3042 } 3043 3044 void test2() { 3045 WTF_ScopedLockable wtf(&mu_); // expected-note {{mutex acquired here}} 3046 } // expected-warning {{mutex 'mu_' is still held at the end of function}} 3047 3048 void test3() { 3049 if (c) { 3050 WTF_ScopedLockable wtf(&mu_); 3051 wtf.release(); 3052 } 3053 } 3054 3055 void test4() { 3056 if (c) { 3057 doSomething(); 3058 } 3059 else { 3060 WTF_ScopedLockable wtf(&mu_); 3061 wtf.release(); 3062 } 3063 } 3064 3065 void test5() { 3066 if (c) { 3067 WTF_ScopedLockable wtf(&mu_); // expected-note {{mutex acquired here}} 3068 } 3069 } // expected-warning {{mutex 'mu_' is not held on every path through here}} 3070 3071 void test6() { 3072 if (c) { 3073 doSomething(); 3074 } 3075 else { 3076 WTF_ScopedLockable wtf(&mu_); // expected-note {{mutex acquired here}} 3077 } 3078 } // expected-warning {{mutex 'mu_' is not held on every path through here}} 3079 }; 3080 3081 3082 } // end namespace FoolishScopedLockableBug 3083 3084 3085 3086 namespace TemporaryCleanupExpr { 3087 3088 class Foo { 3089 int a GUARDED_BY(getMutexPtr().get()); 3090 3091 SmartPtr<Mutex> getMutexPtr(); 3092 3093 void test(); 3094 }; 3095 3096 3097 void Foo::test() { 3098 { 3099 ReaderMutexLock lock(getMutexPtr().get()); 3100 int b = a; 3101 } 3102 int b = a; // expected-warning {{reading variable 'a' requires holding mutex 'getMutexPtr()'}} 3103 } 3104 3105 #ifdef __cpp_guaranteed_copy_elision 3106 3107 void guaranteed_copy_elision() { 3108 MutexLock lock = MutexLock{&sls_mu}; 3109 sls_guard_var = 0; 3110 } 3111 3112 void guaranteed_copy_elision_const() { 3113 const MutexLock lock = MutexLock{&sls_mu}; 3114 sls_guard_var = 0; 3115 } 3116 3117 #endif 3118 3119 } // end namespace TemporaryCleanupExpr 3120 3121 3122 3123 namespace SmartPointerTests { 3124 3125 class Foo { 3126 public: 3127 SmartPtr<Mutex> mu_; 3128 int a GUARDED_BY(mu_); 3129 int b GUARDED_BY(mu_.get()); 3130 int c GUARDED_BY(*mu_); 3131 3132 void Lock() EXCLUSIVE_LOCK_FUNCTION(mu_); 3133 void Unlock() UNLOCK_FUNCTION(mu_); 3134 3135 void test0(); 3136 void test1(); 3137 void test2(); 3138 void test3(); 3139 void test4(); 3140 void test5(); 3141 void test6(); 3142 void test7(); 3143 void test8(); 3144 }; 3145 3146 void Foo::test0() { 3147 a = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}} 3148 b = 0; // expected-warning {{writing variable 'b' requires holding mutex 'mu_' exclusively}} 3149 c = 0; // expected-warning {{writing variable 'c' requires holding mutex 'mu_' exclusively}} 3150 } 3151 3152 void Foo::test1() { 3153 mu_->Lock(); 3154 a = 0; 3155 b = 0; 3156 c = 0; 3157 mu_->Unlock(); 3158 } 3159 3160 void Foo::test2() { 3161 (*mu_).Lock(); 3162 a = 0; 3163 b = 0; 3164 c = 0; 3165 (*mu_).Unlock(); 3166 } 3167 3168 3169 void Foo::test3() { 3170 mu_.get()->Lock(); 3171 a = 0; 3172 b = 0; 3173 c = 0; 3174 mu_.get()->Unlock(); 3175 } 3176 3177 3178 void Foo::test4() { 3179 MutexLock lock(mu_.get()); 3180 a = 0; 3181 b = 0; 3182 c = 0; 3183 } 3184 3185 3186 void Foo::test5() { 3187 MutexLock lock(&(*mu_)); 3188 a = 0; 3189 b = 0; 3190 c = 0; 3191 } 3192 3193 3194 void Foo::test6() { 3195 Lock(); 3196 a = 0; 3197 b = 0; 3198 c = 0; 3199 Unlock(); 3200 } 3201 3202 3203 void Foo::test7() { 3204 { 3205 Lock(); 3206 mu_->Unlock(); 3207 } 3208 { 3209 mu_->Lock(); 3210 Unlock(); 3211 } 3212 { 3213 mu_.get()->Lock(); 3214 mu_->Unlock(); 3215 } 3216 { 3217 mu_->Lock(); 3218 mu_.get()->Unlock(); 3219 } 3220 { 3221 mu_.get()->Lock(); 3222 (*mu_).Unlock(); 3223 } 3224 { 3225 (*mu_).Lock(); 3226 mu_->Unlock(); 3227 } 3228 } 3229 3230 3231 void Foo::test8() { 3232 mu_->Lock(); // expected-note 2 {{mutex acquired here}} 3233 mu_.get()->Lock(); // expected-warning {{acquiring mutex 'mu_' that is already held}} 3234 (*mu_).Lock(); // expected-warning {{acquiring mutex 'mu_' that is already held}} 3235 mu_.get()->Unlock(); // expected-note {{mutex released here}} 3236 Unlock(); // expected-warning {{releasing mutex 'mu_' that was not held}} 3237 } 3238 3239 3240 class Bar { 3241 SmartPtr<Foo> foo; 3242 3243 void test0(); 3244 void test1(); 3245 void test2(); 3246 void test3(); 3247 }; 3248 3249 3250 void Bar::test0() { 3251 foo->a = 0; // expected-warning {{writing variable 'a' requires holding mutex 'foo->mu_' exclusively}} 3252 (*foo).b = 0; // expected-warning {{writing variable 'b' requires holding mutex 'foo->mu_' exclusively}} 3253 foo.get()->c = 0; // expected-warning {{writing variable 'c' requires holding mutex 'foo->mu_' exclusively}} 3254 } 3255 3256 3257 void Bar::test1() { 3258 foo->mu_->Lock(); 3259 foo->a = 0; 3260 (*foo).b = 0; 3261 foo.get()->c = 0; 3262 foo->mu_->Unlock(); 3263 } 3264 3265 3266 void Bar::test2() { 3267 (*foo).mu_->Lock(); 3268 foo->a = 0; 3269 (*foo).b = 0; 3270 foo.get()->c = 0; 3271 foo.get()->mu_->Unlock(); 3272 } 3273 3274 3275 void Bar::test3() { 3276 MutexLock lock(foo->mu_.get()); 3277 foo->a = 0; 3278 (*foo).b = 0; 3279 foo.get()->c = 0; 3280 } 3281 3282 } // end namespace SmartPointerTests 3283 3284 3285 3286 namespace DuplicateAttributeTest { 3287 3288 class LOCKABLE Foo { 3289 public: 3290 Mutex mu1_; 3291 Mutex mu2_; 3292 Mutex mu3_; 3293 int a GUARDED_BY(mu1_); 3294 int b GUARDED_BY(mu2_); 3295 int c GUARDED_BY(mu3_); 3296 3297 void lock() EXCLUSIVE_LOCK_FUNCTION(); 3298 void unlock() UNLOCK_FUNCTION(); 3299 3300 void lock1() EXCLUSIVE_LOCK_FUNCTION(mu1_); 3301 void slock1() SHARED_LOCK_FUNCTION(mu1_); 3302 void lock3() EXCLUSIVE_LOCK_FUNCTION(mu1_, mu2_, mu3_); 3303 void locklots() 3304 EXCLUSIVE_LOCK_FUNCTION(mu1_) 3305 EXCLUSIVE_LOCK_FUNCTION(mu2_) 3306 EXCLUSIVE_LOCK_FUNCTION(mu1_, mu2_, mu3_); 3307 3308 void unlock1() UNLOCK_FUNCTION(mu1_); 3309 void unlock3() UNLOCK_FUNCTION(mu1_, mu2_, mu3_); 3310 void unlocklots() 3311 UNLOCK_FUNCTION(mu1_) 3312 UNLOCK_FUNCTION(mu2_) 3313 UNLOCK_FUNCTION(mu1_, mu2_, mu3_); 3314 }; 3315 3316 3317 void Foo::lock() EXCLUSIVE_LOCK_FUNCTION() { } 3318 void Foo::unlock() UNLOCK_FUNCTION() { } 3319 3320 void Foo::lock1() EXCLUSIVE_LOCK_FUNCTION(mu1_) { 3321 mu1_.Lock(); 3322 } 3323 3324 void Foo::slock1() SHARED_LOCK_FUNCTION(mu1_) { 3325 mu1_.ReaderLock(); 3326 } 3327 3328 void Foo::lock3() EXCLUSIVE_LOCK_FUNCTION(mu1_, mu2_, mu3_) { 3329 mu1_.Lock(); 3330 mu2_.Lock(); 3331 mu3_.Lock(); 3332 } 3333 3334 void Foo::locklots() 3335 EXCLUSIVE_LOCK_FUNCTION(mu1_, mu2_) 3336 EXCLUSIVE_LOCK_FUNCTION(mu2_, mu3_) { 3337 mu1_.Lock(); 3338 mu2_.Lock(); 3339 mu3_.Lock(); 3340 } 3341 3342 void Foo::unlock1() UNLOCK_FUNCTION(mu1_) { 3343 mu1_.Unlock(); 3344 } 3345 3346 void Foo::unlock3() UNLOCK_FUNCTION(mu1_, mu2_, mu3_) { 3347 mu1_.Unlock(); 3348 mu2_.Unlock(); 3349 mu3_.Unlock(); 3350 } 3351 3352 void Foo::unlocklots() 3353 UNLOCK_FUNCTION(mu1_, mu2_) 3354 UNLOCK_FUNCTION(mu2_, mu3_) { 3355 mu1_.Unlock(); 3356 mu2_.Unlock(); 3357 mu3_.Unlock(); 3358 } 3359 3360 3361 void test0() { 3362 Foo foo; 3363 foo.lock(); 3364 foo.unlock(); 3365 3366 foo.lock(); // expected-note{{mutex acquired here}} 3367 foo.lock(); // expected-warning {{acquiring mutex 'foo' that is already held}} 3368 foo.unlock(); // expected-note{{mutex released here}} 3369 foo.unlock(); // expected-warning {{releasing mutex 'foo' that was not held}} 3370 } 3371 3372 3373 void test1() { 3374 Foo foo; 3375 foo.lock1(); 3376 foo.a = 0; 3377 foo.unlock1(); 3378 3379 foo.lock1(); // expected-note{{mutex acquired here}} 3380 foo.lock1(); // expected-warning {{acquiring mutex 'foo.mu1_' that is already held}} 3381 foo.a = 0; 3382 foo.unlock1(); // expected-note{{mutex released here}} 3383 foo.unlock1(); // expected-warning {{releasing mutex 'foo.mu1_' that was not held}} 3384 } 3385 3386 3387 int test2() { 3388 Foo foo; 3389 foo.slock1(); 3390 int d1 = foo.a; 3391 foo.unlock1(); 3392 3393 foo.slock1(); // expected-note{{mutex acquired here}} 3394 foo.slock1(); // expected-warning {{acquiring mutex 'foo.mu1_' that is already held}} 3395 int d2 = foo.a; 3396 foo.unlock1(); // expected-note{{mutex released here}} 3397 foo.unlock1(); // expected-warning {{releasing mutex 'foo.mu1_' that was not held}} 3398 return d1 + d2; 3399 } 3400 3401 3402 void test3() { 3403 Foo foo; 3404 foo.lock3(); 3405 foo.a = 0; 3406 foo.b = 0; 3407 foo.c = 0; 3408 foo.unlock3(); 3409 3410 foo.lock3(); // expected-note 3 {{mutex acquired here}} 3411 foo.lock3(); // \ 3412 // expected-warning {{acquiring mutex 'foo.mu1_' that is already held}} \ 3413 // expected-warning {{acquiring mutex 'foo.mu2_' that is already held}} \ 3414 // expected-warning {{acquiring mutex 'foo.mu3_' that is already held}} 3415 foo.a = 0; 3416 foo.b = 0; 3417 foo.c = 0; 3418 foo.unlock3(); // expected-note 3 {{mutex released here}} 3419 foo.unlock3(); // \ 3420 // expected-warning {{releasing mutex 'foo.mu1_' that was not held}} \ 3421 // expected-warning {{releasing mutex 'foo.mu2_' that was not held}} \ 3422 // expected-warning {{releasing mutex 'foo.mu3_' that was not held}} 3423 } 3424 3425 3426 void testlots() { 3427 Foo foo; 3428 foo.locklots(); 3429 foo.a = 0; 3430 foo.b = 0; 3431 foo.c = 0; 3432 foo.unlocklots(); 3433 3434 foo.locklots(); // expected-note 3 {{mutex acquired here}} 3435 foo.locklots(); // \ 3436 // expected-warning {{acquiring mutex 'foo.mu1_' that is already held}} \ 3437 // expected-warning {{acquiring mutex 'foo.mu2_' that is already held}} \ 3438 // expected-warning {{acquiring mutex 'foo.mu3_' that is already held}} 3439 foo.a = 0; 3440 foo.b = 0; 3441 foo.c = 0; 3442 foo.unlocklots(); // expected-note 3 {{mutex released here}} 3443 foo.unlocklots(); // \ 3444 // expected-warning {{releasing mutex 'foo.mu1_' that was not held}} \ 3445 // expected-warning {{releasing mutex 'foo.mu2_' that was not held}} \ 3446 // expected-warning {{releasing mutex 'foo.mu3_' that was not held}} 3447 } 3448 3449 } // end namespace DuplicateAttributeTest 3450 3451 3452 3453 namespace TryLockEqTest { 3454 3455 class Foo { 3456 Mutex mu_; 3457 int a GUARDED_BY(mu_); 3458 bool c; 3459 3460 int tryLockMutexI() EXCLUSIVE_TRYLOCK_FUNCTION(1, mu_); 3461 Mutex* tryLockMutexP() EXCLUSIVE_TRYLOCK_FUNCTION(1, mu_); 3462 void unlock() UNLOCK_FUNCTION(mu_); 3463 3464 void test1(); 3465 void test2(); 3466 }; 3467 3468 3469 void Foo::test1() { 3470 if (tryLockMutexP() == 0) { 3471 a = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}} 3472 return; 3473 } 3474 a = 0; 3475 unlock(); 3476 3477 if (tryLockMutexP() != 0) { 3478 a = 0; 3479 unlock(); 3480 } 3481 3482 if (0 != tryLockMutexP()) { 3483 a = 0; 3484 unlock(); 3485 } 3486 3487 if (!(tryLockMutexP() == 0)) { 3488 a = 0; 3489 unlock(); 3490 } 3491 3492 if (tryLockMutexI() == 0) { 3493 a = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}} 3494 return; 3495 } 3496 a = 0; 3497 unlock(); 3498 3499 if (0 == tryLockMutexI()) { 3500 a = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}} 3501 return; 3502 } 3503 a = 0; 3504 unlock(); 3505 3506 if (tryLockMutexI() == 1) { 3507 a = 0; 3508 unlock(); 3509 } 3510 3511 if (mu_.TryLock() == false) { 3512 a = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}} 3513 return; 3514 } 3515 a = 0; 3516 unlock(); 3517 3518 if (mu_.TryLock() == true) { 3519 a = 0; 3520 unlock(); 3521 } 3522 else { 3523 a = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}} 3524 } 3525 3526 #if __has_feature(cxx_nullptr) 3527 if (tryLockMutexP() == nullptr) { 3528 a = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}} 3529 return; 3530 } 3531 a = 0; 3532 unlock(); 3533 #endif 3534 } 3535 3536 } // end namespace TryLockEqTest 3537 3538 3539 namespace ExistentialPatternMatching { 3540 3541 class Graph { 3542 public: 3543 Mutex mu_; 3544 }; 3545 3546 void LockAllGraphs() EXCLUSIVE_LOCK_FUNCTION(&Graph::mu_); 3547 void UnlockAllGraphs() UNLOCK_FUNCTION(&Graph::mu_); 3548 3549 class Node { 3550 public: 3551 int a GUARDED_BY(&Graph::mu_); 3552 3553 void foo() EXCLUSIVE_LOCKS_REQUIRED(&Graph::mu_) { 3554 a = 0; 3555 } 3556 void foo2() LOCKS_EXCLUDED(&Graph::mu_); 3557 }; 3558 3559 void test() { 3560 Graph g1; 3561 Graph g2; 3562 Node n1; 3563 3564 n1.a = 0; // expected-warning {{writing variable 'a' requires holding mutex '&ExistentialPatternMatching::Graph::mu_' exclusively}} 3565 n1.foo(); // expected-warning {{calling function 'foo' requires holding mutex '&ExistentialPatternMatching::Graph::mu_' exclusively}} 3566 n1.foo2(); 3567 3568 g1.mu_.Lock(); 3569 n1.a = 0; 3570 n1.foo(); 3571 n1.foo2(); // expected-warning {{cannot call function 'foo2' while mutex '&ExistentialPatternMatching::Graph::mu_' is held}} 3572 g1.mu_.Unlock(); 3573 3574 g2.mu_.Lock(); 3575 n1.a = 0; 3576 n1.foo(); 3577 n1.foo2(); // expected-warning {{cannot call function 'foo2' while mutex '&ExistentialPatternMatching::Graph::mu_' is held}} 3578 g2.mu_.Unlock(); 3579 3580 LockAllGraphs(); 3581 n1.a = 0; 3582 n1.foo(); 3583 n1.foo2(); // expected-warning {{cannot call function 'foo2' while mutex '&ExistentialPatternMatching::Graph::mu_' is held}} 3584 UnlockAllGraphs(); 3585 3586 LockAllGraphs(); 3587 g1.mu_.Unlock(); 3588 3589 LockAllGraphs(); 3590 g2.mu_.Unlock(); 3591 3592 LockAllGraphs(); // expected-note{{mutex acquired here}} 3593 g1.mu_.Lock(); // expected-warning {{acquiring mutex 'g1.mu_' that is already held}} 3594 g1.mu_.Unlock(); 3595 } 3596 3597 } // end namespace ExistentialPatternMatching 3598 3599 3600 namespace StringIgnoreTest { 3601 3602 class Foo { 3603 public: 3604 Mutex mu_; 3605 void lock() EXCLUSIVE_LOCK_FUNCTION(""); 3606 void unlock() UNLOCK_FUNCTION(""); 3607 void goober() EXCLUSIVE_LOCKS_REQUIRED(""); 3608 void roober() SHARED_LOCKS_REQUIRED(""); 3609 }; 3610 3611 3612 class Bar : public Foo { 3613 public: 3614 void bar(Foo* f) { 3615 f->unlock(); 3616 f->goober(); 3617 f->roober(); 3618 f->lock(); 3619 }; 3620 }; 3621 3622 } // end namespace StringIgnoreTest 3623 3624 3625 namespace LockReturnedScopeFix { 3626 3627 class Base { 3628 protected: 3629 struct Inner; 3630 bool c; 3631 3632 const Mutex& getLock(const Inner* i); 3633 3634 void lockInner (Inner* i) EXCLUSIVE_LOCK_FUNCTION(getLock(i)); 3635 void unlockInner(Inner* i) UNLOCK_FUNCTION(getLock(i)); 3636 void foo(Inner* i) EXCLUSIVE_LOCKS_REQUIRED(getLock(i)); 3637 3638 void bar(Inner* i); 3639 }; 3640 3641 3642 struct Base::Inner { 3643 Mutex lock_; 3644 void doSomething() EXCLUSIVE_LOCKS_REQUIRED(lock_); 3645 }; 3646 3647 3648 const Mutex& Base::getLock(const Inner* i) LOCK_RETURNED(i->lock_) { 3649 return i->lock_; 3650 } 3651 3652 3653 void Base::foo(Inner* i) { 3654 i->doSomething(); 3655 } 3656 3657 void Base::bar(Inner* i) { 3658 if (c) { 3659 i->lock_.Lock(); 3660 unlockInner(i); 3661 } 3662 else { 3663 lockInner(i); 3664 i->lock_.Unlock(); 3665 } 3666 } 3667 3668 } // end namespace LockReturnedScopeFix 3669 3670 3671 namespace TrylockWithCleanups { 3672 3673 struct Foo { 3674 Mutex mu_; 3675 int a GUARDED_BY(mu_); 3676 }; 3677 3678 Foo* GetAndLockFoo(const MyString& s) 3679 EXCLUSIVE_TRYLOCK_FUNCTION(true, &Foo::mu_); 3680 3681 static void test() { 3682 Foo* lt = GetAndLockFoo("foo"); 3683 if (!lt) return; 3684 int a = lt->a; 3685 lt->mu_.Unlock(); 3686 } 3687 3688 } // end namespace TrylockWithCleanups 3689 3690 3691 namespace UniversalLock { 3692 3693 class Foo { 3694 Mutex mu_; 3695 bool c; 3696 3697 int a GUARDED_BY(mu_); 3698 void r_foo() SHARED_LOCKS_REQUIRED(mu_); 3699 void w_foo() EXCLUSIVE_LOCKS_REQUIRED(mu_); 3700 3701 void test1() { 3702 int b; 3703 3704 beginNoWarnOnReads(); 3705 b = a; 3706 r_foo(); 3707 endNoWarnOnReads(); 3708 3709 beginNoWarnOnWrites(); 3710 a = 0; 3711 w_foo(); 3712 endNoWarnOnWrites(); 3713 } 3714 3715 // don't warn on joins with universal lock 3716 void test2() { 3717 if (c) { 3718 beginNoWarnOnWrites(); 3719 } 3720 a = 0; // \ 3721 // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}} 3722 endNoWarnOnWrites(); // \ 3723 // expected-warning {{releasing mutex '*' that was not held}} 3724 } 3725 3726 3727 // make sure the universal lock joins properly 3728 void test3() { 3729 if (c) { 3730 mu_.Lock(); 3731 beginNoWarnOnWrites(); 3732 } 3733 else { 3734 beginNoWarnOnWrites(); 3735 mu_.Lock(); 3736 } 3737 a = 0; 3738 endNoWarnOnWrites(); 3739 mu_.Unlock(); 3740 } 3741 3742 3743 // combine universal lock with other locks 3744 void test4() { 3745 beginNoWarnOnWrites(); 3746 mu_.Lock(); 3747 mu_.Unlock(); 3748 endNoWarnOnWrites(); 3749 3750 mu_.Lock(); 3751 beginNoWarnOnWrites(); 3752 endNoWarnOnWrites(); 3753 mu_.Unlock(); 3754 3755 mu_.Lock(); 3756 beginNoWarnOnWrites(); 3757 mu_.Unlock(); 3758 endNoWarnOnWrites(); 3759 } 3760 }; 3761 3762 } // end namespace UniversalLock 3763 3764 3765 namespace TemplateLockReturned { 3766 3767 template<class T> 3768 class BaseT { 3769 public: 3770 virtual void baseMethod() = 0; 3771 Mutex* get_mutex() LOCK_RETURNED(mutex_) { return &mutex_; } 3772 3773 Mutex mutex_; 3774 int a GUARDED_BY(mutex_); 3775 }; 3776 3777 3778 class Derived : public BaseT<int> { 3779 public: 3780 void baseMethod() EXCLUSIVE_LOCKS_REQUIRED(get_mutex()) { 3781 a = 0; 3782 } 3783 }; 3784 3785 } // end namespace TemplateLockReturned 3786 3787 3788 namespace ExprMatchingBugFix { 3789 3790 class Foo { 3791 public: 3792 Mutex mu_; 3793 }; 3794 3795 3796 class Bar { 3797 public: 3798 bool c; 3799 Foo* foo; 3800 Bar(Foo* f) : foo(f) { } 3801 3802 struct Nested { 3803 Foo* foo; 3804 Nested(Foo* f) : foo(f) { } 3805 3806 void unlockFoo() UNLOCK_FUNCTION(&Foo::mu_); 3807 }; 3808 3809 void test(); 3810 }; 3811 3812 3813 void Bar::test() { 3814 foo->mu_.Lock(); 3815 if (c) { 3816 Nested *n = new Nested(foo); 3817 n->unlockFoo(); 3818 } 3819 else { 3820 foo->mu_.Unlock(); 3821 } 3822 } 3823 3824 }; // end namespace ExprMatchingBugfix 3825 3826 3827 namespace ComplexNameTest { 3828 3829 class Foo { 3830 public: 3831 static Mutex mu_; 3832 3833 Foo() EXCLUSIVE_LOCKS_REQUIRED(mu_) { } 3834 ~Foo() EXCLUSIVE_LOCKS_REQUIRED(mu_) { } 3835 3836 int operator[](int i) EXCLUSIVE_LOCKS_REQUIRED(mu_) { return 0; } 3837 }; 3838 3839 class Bar { 3840 public: 3841 static Mutex mu_; 3842 3843 Bar() LOCKS_EXCLUDED(mu_) { } 3844 ~Bar() LOCKS_EXCLUDED(mu_) { } 3845 3846 int operator[](int i) LOCKS_EXCLUDED(mu_) { return 0; } 3847 }; 3848 3849 3850 void test1() { 3851 Foo f; // expected-warning {{calling function 'Foo' requires holding mutex 'mu_' exclusively}} 3852 int a = f[0]; // expected-warning {{calling function 'operator[]' requires holding mutex 'mu_' exclusively}} 3853 } // expected-warning {{calling function '~Foo' requires holding mutex 'mu_' exclusively}} 3854 3855 3856 void test2() { 3857 Bar::mu_.Lock(); 3858 { 3859 Bar b; // expected-warning {{cannot call function 'Bar' while mutex 'mu_' is held}} 3860 int a = b[0]; // expected-warning {{cannot call function 'operator[]' while mutex 'mu_' is held}} 3861 } // expected-warning {{cannot call function '~Bar' while mutex 'mu_' is held}} 3862 Bar::mu_.Unlock(); 3863 } 3864 3865 }; // end namespace ComplexNameTest 3866 3867 3868 namespace UnreachableExitTest { 3869 3870 class FemmeFatale { 3871 public: 3872 FemmeFatale(); 3873 ~FemmeFatale() __attribute__((noreturn)); 3874 }; 3875 3876 void exitNow() __attribute__((noreturn)); 3877 void exitDestruct(const MyString& ms) __attribute__((noreturn)); 3878 3879 Mutex fatalmu_; 3880 3881 void test1() EXCLUSIVE_LOCKS_REQUIRED(fatalmu_) { 3882 exitNow(); 3883 } 3884 3885 void test2() EXCLUSIVE_LOCKS_REQUIRED(fatalmu_) { 3886 FemmeFatale femme; 3887 } 3888 3889 bool c; 3890 3891 void test3() EXCLUSIVE_LOCKS_REQUIRED(fatalmu_) { 3892 if (c) { 3893 exitNow(); 3894 } 3895 else { 3896 FemmeFatale femme; 3897 } 3898 } 3899 3900 void test4() EXCLUSIVE_LOCKS_REQUIRED(fatalmu_) { 3901 exitDestruct("foo"); 3902 } 3903 3904 } // end namespace UnreachableExitTest 3905 3906 3907 namespace VirtualMethodCanonicalizationTest { 3908 3909 class Base { 3910 public: 3911 virtual Mutex* getMutex() = 0; 3912 }; 3913 3914 class Base2 : public Base { 3915 public: 3916 Mutex* getMutex(); 3917 }; 3918 3919 class Base3 : public Base2 { 3920 public: 3921 Mutex* getMutex(); 3922 }; 3923 3924 class Derived : public Base3 { 3925 public: 3926 Mutex* getMutex(); // overrides Base::getMutex() 3927 }; 3928 3929 void baseFun(Base *b) EXCLUSIVE_LOCKS_REQUIRED(b->getMutex()) { } 3930 3931 void derivedFun(Derived *d) EXCLUSIVE_LOCKS_REQUIRED(d->getMutex()) { 3932 baseFun(d); 3933 } 3934 3935 } // end namespace VirtualMethodCanonicalizationTest 3936 3937 3938 namespace TemplateFunctionParamRemapTest { 3939 3940 template <class T> 3941 struct Cell { 3942 T dummy_; 3943 Mutex* mu_; 3944 }; 3945 3946 class Foo { 3947 public: 3948 template <class T> 3949 void elr(Cell<T>* c) EXCLUSIVE_LOCKS_REQUIRED(c->mu_); 3950 3951 void test(); 3952 }; 3953 3954 template<class T> 3955 void Foo::elr(Cell<T>* c1) { } 3956 3957 void Foo::test() { 3958 Cell<int> cell; 3959 elr(&cell); // \ 3960 // expected-warning {{calling function 'elr<int>' requires holding mutex 'cell.mu_' exclusively}} 3961 } 3962 3963 3964 template<class T> 3965 void globalELR(Cell<T>* c) EXCLUSIVE_LOCKS_REQUIRED(c->mu_); 3966 3967 template<class T> 3968 void globalELR(Cell<T>* c1) { } 3969 3970 void globalTest() { 3971 Cell<int> cell; 3972 globalELR(&cell); // \ 3973 // expected-warning {{calling function 'globalELR<int>' requires holding mutex 'cell.mu_' exclusively}} 3974 } 3975 3976 3977 template<class T> 3978 void globalELR2(Cell<T>* c) EXCLUSIVE_LOCKS_REQUIRED(c->mu_); 3979 3980 // second declaration 3981 template<class T> 3982 void globalELR2(Cell<T>* c2); 3983 3984 template<class T> 3985 void globalELR2(Cell<T>* c3) { } 3986 3987 // re-declaration after definition 3988 template<class T> 3989 void globalELR2(Cell<T>* c4); 3990 3991 void globalTest2() { 3992 Cell<int> cell; 3993 globalELR2(&cell); // \ 3994 // expected-warning {{calling function 'globalELR2<int>' requires holding mutex 'cell.mu_' exclusively}} 3995 } 3996 3997 3998 template<class T> 3999 class FooT { 4000 public: 4001 void elr(Cell<T>* c) EXCLUSIVE_LOCKS_REQUIRED(c->mu_); 4002 }; 4003 4004 template<class T> 4005 void FooT<T>::elr(Cell<T>* c1) { } 4006 4007 void testFooT() { 4008 Cell<int> cell; 4009 FooT<int> foo; 4010 foo.elr(&cell); // \ 4011 // expected-warning {{calling function 'elr' requires holding mutex 'cell.mu_' exclusively}} 4012 } 4013 4014 } // end namespace TemplateFunctionParamRemapTest 4015 4016 4017 namespace SelfConstructorTest { 4018 4019 class SelfLock { 4020 public: 4021 SelfLock() EXCLUSIVE_LOCK_FUNCTION(mu_); 4022 ~SelfLock() UNLOCK_FUNCTION(mu_); 4023 4024 void foo() EXCLUSIVE_LOCKS_REQUIRED(mu_); 4025 4026 Mutex mu_; 4027 }; 4028 4029 class LOCKABLE SelfLock2 { 4030 public: 4031 SelfLock2() EXCLUSIVE_LOCK_FUNCTION(); 4032 ~SelfLock2() UNLOCK_FUNCTION(); 4033 4034 void foo() EXCLUSIVE_LOCKS_REQUIRED(this); 4035 }; 4036 4037 4038 void test() { 4039 SelfLock s; 4040 s.foo(); 4041 } 4042 4043 void test2() { 4044 SelfLock2 s2; 4045 s2.foo(); 4046 } 4047 4048 } // end namespace SelfConstructorTest 4049 4050 4051 namespace MultipleAttributeTest { 4052 4053 class Foo { 4054 Mutex mu1_; 4055 Mutex mu2_; 4056 int a GUARDED_BY(mu1_); 4057 int b GUARDED_BY(mu2_); 4058 int c GUARDED_BY(mu1_) GUARDED_BY(mu2_); 4059 int* d PT_GUARDED_BY(mu1_) PT_GUARDED_BY(mu2_); 4060 4061 void foo1() EXCLUSIVE_LOCKS_REQUIRED(mu1_) 4062 EXCLUSIVE_LOCKS_REQUIRED(mu2_); 4063 void foo2() SHARED_LOCKS_REQUIRED(mu1_) 4064 SHARED_LOCKS_REQUIRED(mu2_); 4065 void foo3() LOCKS_EXCLUDED(mu1_) 4066 LOCKS_EXCLUDED(mu2_); 4067 void lock() EXCLUSIVE_LOCK_FUNCTION(mu1_) 4068 EXCLUSIVE_LOCK_FUNCTION(mu2_); 4069 void readerlock() SHARED_LOCK_FUNCTION(mu1_) 4070 SHARED_LOCK_FUNCTION(mu2_); 4071 void unlock() UNLOCK_FUNCTION(mu1_) 4072 UNLOCK_FUNCTION(mu2_); 4073 bool trylock() EXCLUSIVE_TRYLOCK_FUNCTION(true, mu1_) 4074 EXCLUSIVE_TRYLOCK_FUNCTION(true, mu2_); 4075 bool readertrylock() SHARED_TRYLOCK_FUNCTION(true, mu1_) 4076 SHARED_TRYLOCK_FUNCTION(true, mu2_); 4077 void assertBoth() ASSERT_EXCLUSIVE_LOCK(mu1_) 4078 ASSERT_EXCLUSIVE_LOCK(mu2_); 4079 4080 void alsoAssertBoth() ASSERT_EXCLUSIVE_LOCK(mu1_, mu2_); 4081 4082 void assertShared() ASSERT_SHARED_LOCK(mu1_) 4083 ASSERT_SHARED_LOCK(mu2_); 4084 4085 void alsoAssertShared() ASSERT_SHARED_LOCK(mu1_, mu2_); 4086 4087 void test(); 4088 void testAssert(); 4089 void testAssertShared(); 4090 }; 4091 4092 4093 void Foo::foo1() { 4094 a = 1; 4095 b = 2; 4096 } 4097 4098 void Foo::foo2() { 4099 int result = a + b; 4100 } 4101 4102 void Foo::foo3() { } 4103 void Foo::lock() { mu1_.Lock(); mu2_.Lock(); } 4104 void Foo::readerlock() { mu1_.ReaderLock(); mu2_.ReaderLock(); } 4105 void Foo::unlock() { mu1_.Unlock(); mu2_.Unlock(); } 4106 bool Foo::trylock() { return true; } 4107 bool Foo::readertrylock() { return true; } 4108 4109 4110 void Foo::test() { 4111 mu1_.Lock(); 4112 foo1(); // expected-warning {{}} 4113 c = 0; // expected-warning {{}} 4114 *d = 0; // expected-warning {{}} 4115 mu1_.Unlock(); 4116 4117 mu1_.ReaderLock(); 4118 foo2(); // expected-warning {{}} 4119 int x = c; // expected-warning {{}} 4120 int y = *d; // expected-warning {{}} 4121 mu1_.Unlock(); 4122 4123 mu2_.Lock(); 4124 foo3(); // expected-warning {{}} 4125 mu2_.Unlock(); 4126 4127 lock(); 4128 a = 0; 4129 b = 0; 4130 unlock(); 4131 4132 readerlock(); 4133 int z = a + b; 4134 unlock(); 4135 4136 if (trylock()) { 4137 a = 0; 4138 b = 0; 4139 unlock(); 4140 } 4141 4142 if (readertrylock()) { 4143 int zz = a + b; 4144 unlock(); 4145 } 4146 } 4147 4148 // Force duplication of attributes 4149 void Foo::assertBoth() { } 4150 void Foo::alsoAssertBoth() { } 4151 void Foo::assertShared() { } 4152 void Foo::alsoAssertShared() { } 4153 4154 void Foo::testAssert() { 4155 { 4156 assertBoth(); 4157 a = 0; 4158 b = 0; 4159 } 4160 { 4161 alsoAssertBoth(); 4162 a = 0; 4163 b = 0; 4164 } 4165 } 4166 4167 void Foo::testAssertShared() { 4168 { 4169 assertShared(); 4170 int zz = a + b; 4171 } 4172 4173 { 4174 alsoAssertShared(); 4175 int zz = a + b; 4176 } 4177 } 4178 4179 4180 } // end namespace MultipleAttributeTest 4181 4182 4183 namespace GuardedNonPrimitiveTypeTest { 4184 4185 4186 class Data { 4187 public: 4188 Data(int i) : dat(i) { } 4189 4190 int getValue() const { return dat; } 4191 void setValue(int i) { dat = i; } 4192 4193 int operator[](int i) const { return dat; } 4194 int& operator[](int i) { return dat; } 4195 4196 void operator()() { } 4197 4198 private: 4199 int dat; 4200 }; 4201 4202 4203 class DataCell { 4204 public: 4205 DataCell(const Data& d) : dat(d) { } 4206 4207 private: 4208 Data dat; 4209 }; 4210 4211 4212 void showDataCell(const DataCell& dc); 4213 4214 4215 class Foo { 4216 public: 4217 // method call tests 4218 void test() { 4219 data_.setValue(0); // FIXME -- should be writing \ 4220 // expected-warning {{reading variable 'data_' requires holding mutex 'mu_'}} 4221 int a = data_.getValue(); // \ 4222 // expected-warning {{reading variable 'data_' requires holding mutex 'mu_'}} 4223 4224 datap1_->setValue(0); // FIXME -- should be writing \ 4225 // expected-warning {{reading variable 'datap1_' requires holding mutex 'mu_'}} 4226 a = datap1_->getValue(); // \ 4227 // expected-warning {{reading variable 'datap1_' requires holding mutex 'mu_'}} 4228 4229 datap2_->setValue(0); // FIXME -- should be writing \ 4230 // expected-warning {{reading the value pointed to by 'datap2_' requires holding mutex 'mu_'}} 4231 a = datap2_->getValue(); // \ 4232 // expected-warning {{reading the value pointed to by 'datap2_' requires holding mutex 'mu_'}} 4233 4234 (*datap2_).setValue(0); // FIXME -- should be writing \ 4235 // expected-warning {{reading the value pointed to by 'datap2_' requires holding mutex 'mu_'}} 4236 a = (*datap2_).getValue(); // \ 4237 // expected-warning {{reading the value pointed to by 'datap2_' requires holding mutex 'mu_'}} 4238 4239 mu_.Lock(); 4240 data_.setValue(1); 4241 datap1_->setValue(1); 4242 datap2_->setValue(1); 4243 mu_.Unlock(); 4244 4245 mu_.ReaderLock(); 4246 a = data_.getValue(); 4247 datap1_->setValue(0); // reads datap1_, writes *datap1_ 4248 a = datap1_->getValue(); 4249 a = datap2_->getValue(); 4250 mu_.Unlock(); 4251 } 4252 4253 // operator tests 4254 void test2() { 4255 data_ = Data(1); // expected-warning {{writing variable 'data_' requires holding mutex 'mu_' exclusively}} 4256 *datap1_ = data_; // expected-warning {{reading variable 'datap1_' requires holding mutex 'mu_'}} \ 4257 // expected-warning {{reading variable 'data_' requires holding mutex 'mu_'}} 4258 *datap2_ = data_; // expected-warning {{writing the value pointed to by 'datap2_' requires holding mutex 'mu_' exclusively}} \ 4259 // expected-warning {{reading variable 'data_' requires holding mutex 'mu_'}} 4260 data_ = *datap1_; // expected-warning {{writing variable 'data_' requires holding mutex 'mu_' exclusively}} \ 4261 // expected-warning {{reading variable 'datap1_' requires holding mutex 'mu_'}} 4262 data_ = *datap2_; // expected-warning {{writing variable 'data_' requires holding mutex 'mu_' exclusively}} \ 4263 // expected-warning {{reading the value pointed to by 'datap2_' requires holding mutex 'mu_'}} 4264 4265 data_[0] = 0; // expected-warning {{reading variable 'data_' requires holding mutex 'mu_'}} 4266 (*datap2_)[0] = 0; // expected-warning {{reading the value pointed to by 'datap2_' requires holding mutex 'mu_'}} 4267 4268 data_(); // expected-warning {{reading variable 'data_' requires holding mutex 'mu_'}} 4269 } 4270 4271 // const operator tests 4272 void test3() const { 4273 Data mydat(data_); // expected-warning {{reading variable 'data_' requires holding mutex 'mu_'}} 4274 4275 //FIXME 4276 //showDataCell(data_); // xpected-warning {{reading variable 'data_' requires holding mutex 'mu_'}} 4277 //showDataCell(*datap2_); // xpected-warning {{reading the value pointed to by 'datap2_' requires holding mutex 'mu_'}} 4278 4279 int a = data_[0]; // expected-warning {{reading variable 'data_' requires holding mutex 'mu_'}} 4280 } 4281 4282 private: 4283 Mutex mu_; 4284 Data data_ GUARDED_BY(mu_); 4285 Data* datap1_ GUARDED_BY(mu_); 4286 Data* datap2_ PT_GUARDED_BY(mu_); 4287 }; 4288 4289 } // end namespace GuardedNonPrimitiveTypeTest 4290 4291 4292 namespace GuardedNonPrimitive_MemberAccess { 4293 4294 class Cell { 4295 public: 4296 Cell(int i); 4297 4298 void cellMethod(); 4299 4300 int a; 4301 }; 4302 4303 4304 class Foo { 4305 public: 4306 int a; 4307 Cell c GUARDED_BY(cell_mu_); 4308 Cell* cp PT_GUARDED_BY(cell_mu_); 4309 4310 void myMethod(); 4311 4312 Mutex cell_mu_; 4313 }; 4314 4315 4316 class Bar { 4317 private: 4318 Mutex mu_; 4319 Foo foo GUARDED_BY(mu_); 4320 Foo* foop PT_GUARDED_BY(mu_); 4321 4322 void test() { 4323 foo.myMethod(); // expected-warning {{reading variable 'foo' requires holding mutex 'mu_'}} 4324 4325 int fa = foo.a; // expected-warning {{reading variable 'foo' requires holding mutex 'mu_'}} 4326 foo.a = fa; // expected-warning {{writing variable 'foo' requires holding mutex 'mu_' exclusively}} 4327 4328 fa = foop->a; // expected-warning {{reading the value pointed to by 'foop' requires holding mutex 'mu_'}} 4329 foop->a = fa; // expected-warning {{writing the value pointed to by 'foop' requires holding mutex 'mu_' exclusively}} 4330 4331 fa = (*foop).a; // expected-warning {{reading the value pointed to by 'foop' requires holding mutex 'mu_'}} 4332 (*foop).a = fa; // expected-warning {{writing the value pointed to by 'foop' requires holding mutex 'mu_' exclusively}} 4333 4334 foo.c = Cell(0); // expected-warning {{writing variable 'foo' requires holding mutex 'mu_'}} \ 4335 // expected-warning {{writing variable 'c' requires holding mutex 'foo.cell_mu_' exclusively}} 4336 foo.c.cellMethod(); // expected-warning {{reading variable 'foo' requires holding mutex 'mu_'}} \ 4337 // expected-warning {{reading variable 'c' requires holding mutex 'foo.cell_mu_'}} 4338 4339 foop->c = Cell(0); // expected-warning {{writing the value pointed to by 'foop' requires holding mutex 'mu_'}} \ 4340 // expected-warning {{writing variable 'c' requires holding mutex 'foop->cell_mu_' exclusively}} 4341 foop->c.cellMethod(); // expected-warning {{reading the value pointed to by 'foop' requires holding mutex 'mu_'}} \ 4342 // expected-warning {{reading variable 'c' requires holding mutex 'foop->cell_mu_'}} 4343 4344 (*foop).c = Cell(0); // expected-warning {{writing the value pointed to by 'foop' requires holding mutex 'mu_'}} \ 4345 // expected-warning {{writing variable 'c' requires holding mutex 'foop->cell_mu_' exclusively}} 4346 (*foop).c.cellMethod(); // expected-warning {{reading the value pointed to by 'foop' requires holding mutex 'mu_'}} \ 4347 // expected-warning {{reading variable 'c' requires holding mutex 'foop->cell_mu_'}} 4348 }; 4349 }; 4350 4351 } // namespace GuardedNonPrimitive_MemberAccess 4352 4353 4354 namespace TestThrowExpr { 4355 4356 class Foo { 4357 Mutex mu_; 4358 4359 bool hasError(); 4360 4361 void test() { 4362 mu_.Lock(); 4363 if (hasError()) { 4364 throw "ugly"; 4365 } 4366 mu_.Unlock(); 4367 } 4368 }; 4369 4370 } // end namespace TestThrowExpr 4371 4372 4373 namespace UnevaluatedContextTest { 4374 4375 // parse attribute expressions in an unevaluated context. 4376 4377 static inline Mutex* getMutex1(); 4378 static inline Mutex* getMutex2(); 4379 4380 void bar() EXCLUSIVE_LOCKS_REQUIRED(getMutex1()); 4381 4382 void bar2() EXCLUSIVE_LOCKS_REQUIRED(getMutex1(), getMutex2()); 4383 4384 } // end namespace UnevaluatedContextTest 4385 4386 4387 namespace LockUnlockFunctionTest { 4388 4389 // Check built-in lock functions 4390 class LOCKABLE MyLockable { 4391 public: 4392 void lock() EXCLUSIVE_LOCK_FUNCTION() { mu_.Lock(); } 4393 void readerLock() SHARED_LOCK_FUNCTION() { mu_.ReaderLock(); } 4394 void unlock() UNLOCK_FUNCTION() { mu_.Unlock(); } 4395 4396 private: 4397 Mutex mu_; 4398 }; 4399 4400 4401 class Foo { 4402 public: 4403 // Correct lock/unlock functions 4404 void lock() EXCLUSIVE_LOCK_FUNCTION(mu_) { 4405 mu_.Lock(); 4406 } 4407 4408 void readerLock() SHARED_LOCK_FUNCTION(mu_) { 4409 mu_.ReaderLock(); 4410 } 4411 4412 void unlock() UNLOCK_FUNCTION(mu_) { 4413 mu_.Unlock(); 4414 } 4415 4416 void unlockExclusive() EXCLUSIVE_UNLOCK_FUNCTION(mu_) { 4417 mu_.Unlock(); 4418 } 4419 4420 void unlockShared() SHARED_UNLOCK_FUNCTION(mu_) { 4421 mu_.ReaderUnlock(); 4422 } 4423 4424 // Check failure to lock. 4425 void lockBad() EXCLUSIVE_LOCK_FUNCTION(mu_) { // expected-note {{mutex acquired here}} 4426 mu2_.Lock(); 4427 mu2_.Unlock(); 4428 } // expected-warning {{expecting mutex 'mu_' to be held at the end of function}} 4429 4430 void readerLockBad() SHARED_LOCK_FUNCTION(mu_) { // expected-note {{mutex acquired here}} 4431 mu2_.Lock(); 4432 mu2_.Unlock(); 4433 } // expected-warning {{expecting mutex 'mu_' to be held at the end of function}} 4434 4435 void unlockBad() UNLOCK_FUNCTION(mu_) { // expected-note {{mutex acquired here}} 4436 mu2_.Lock(); 4437 mu2_.Unlock(); 4438 } // expected-warning {{mutex 'mu_' is still held at the end of function}} 4439 4440 // Check locking the wrong thing. 4441 void lockBad2() EXCLUSIVE_LOCK_FUNCTION(mu_) { // expected-note {{mutex acquired here}} 4442 mu2_.Lock(); // expected-note {{mutex acquired here}} 4443 } // expected-warning {{expecting mutex 'mu_' to be held at the end of function}} \ 4444 // expected-warning {{mutex 'mu2_' is still held at the end of function}} 4445 4446 4447 void readerLockBad2() SHARED_LOCK_FUNCTION(mu_) { // expected-note {{mutex acquired here}} 4448 mu2_.ReaderLock(); // expected-note {{mutex acquired here}} 4449 } // expected-warning {{expecting mutex 'mu_' to be held at the end of function}} \ 4450 // expected-warning {{mutex 'mu2_' is still held at the end of function}} 4451 4452 4453 void unlockBad2() UNLOCK_FUNCTION(mu_) { // expected-note {{mutex acquired here}} 4454 mu2_.Unlock(); // expected-warning {{releasing mutex 'mu2_' that was not held}} 4455 } // expected-warning {{mutex 'mu_' is still held at the end of function}} 4456 4457 private: 4458 Mutex mu_; 4459 Mutex mu2_; 4460 }; 4461 4462 } // end namespace LockUnlockFunctionTest 4463 4464 4465 namespace AssertHeldTest { 4466 4467 class Foo { 4468 public: 4469 int c; 4470 int a GUARDED_BY(mu_); 4471 Mutex mu_; 4472 4473 void test1() { 4474 mu_.AssertHeld(); 4475 int b = a; 4476 a = 0; 4477 } 4478 4479 void test2() { 4480 mu_.AssertReaderHeld(); 4481 int b = a; 4482 a = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}} 4483 } 4484 4485 void test3() { 4486 if (c) { 4487 mu_.AssertHeld(); 4488 } 4489 else { 4490 mu_.AssertHeld(); 4491 } 4492 int b = a; 4493 a = 0; 4494 } 4495 4496 void test4() EXCLUSIVE_LOCKS_REQUIRED(mu_) { 4497 mu_.AssertHeld(); 4498 int b = a; 4499 a = 0; 4500 } 4501 4502 void test5() UNLOCK_FUNCTION(mu_) { 4503 mu_.AssertHeld(); 4504 mu_.Unlock(); 4505 } 4506 4507 void test6() { 4508 mu_.AssertHeld(); 4509 mu_.Unlock(); 4510 } // should this be a warning? 4511 4512 void test7() { 4513 if (c) { 4514 mu_.AssertHeld(); 4515 } 4516 else { 4517 mu_.Lock(); 4518 } 4519 int b = a; 4520 a = 0; 4521 mu_.Unlock(); 4522 } 4523 4524 void test8() { 4525 if (c) { 4526 mu_.Lock(); 4527 } 4528 else { 4529 mu_.AssertHeld(); 4530 } 4531 int b = a; 4532 a = 0; 4533 mu_.Unlock(); 4534 } 4535 4536 void test9() { 4537 if (c) { 4538 mu_.AssertHeld(); 4539 } 4540 else { 4541 mu_.Lock(); // expected-note {{mutex acquired here}} 4542 } 4543 } // expected-warning {{mutex 'mu_' is still held at the end of function}} 4544 4545 void test10() { 4546 if (c) { 4547 mu_.Lock(); // expected-note {{mutex acquired here}} 4548 } 4549 else { 4550 mu_.AssertHeld(); 4551 } 4552 } // expected-warning {{mutex 'mu_' is still held at the end of function}} 4553 4554 void assertMu() ASSERT_EXCLUSIVE_LOCK(mu_); 4555 4556 void test11() { 4557 assertMu(); 4558 int b = a; 4559 a = 0; 4560 } 4561 }; 4562 4563 } // end namespace AssertHeldTest 4564 4565 4566 namespace LogicalConditionalTryLock { 4567 4568 class Foo { 4569 public: 4570 Mutex mu; 4571 int a GUARDED_BY(mu); 4572 bool c; 4573 4574 bool newc(); 4575 4576 void test1() { 4577 if (c && mu.TryLock()) { 4578 a = 0; 4579 mu.Unlock(); 4580 } 4581 } 4582 4583 void test2() { 4584 bool b = mu.TryLock(); 4585 if (c && b) { 4586 a = 0; 4587 mu.Unlock(); 4588 } 4589 } 4590 4591 void test3() { 4592 if (c || !mu.TryLock()) 4593 return; 4594 a = 0; 4595 mu.Unlock(); 4596 } 4597 4598 void test4() { 4599 while (c && mu.TryLock()) { 4600 a = 0; 4601 c = newc(); 4602 mu.Unlock(); 4603 } 4604 } 4605 4606 void test5() { 4607 while (c) { 4608 if (newc() || !mu.TryLock()) 4609 break; 4610 a = 0; 4611 mu.Unlock(); 4612 } 4613 } 4614 4615 void test6() { 4616 mu.Lock(); 4617 do { 4618 a = 0; 4619 mu.Unlock(); 4620 } while (newc() && mu.TryLock()); 4621 } 4622 4623 void test7() { 4624 for (bool b = mu.TryLock(); c && b;) { 4625 a = 0; 4626 mu.Unlock(); 4627 } 4628 } 4629 4630 void test8() { 4631 if (c && newc() && mu.TryLock()) { 4632 a = 0; 4633 mu.Unlock(); 4634 } 4635 } 4636 4637 void test9() { 4638 if (!(c && newc() && mu.TryLock())) 4639 return; 4640 a = 0; 4641 mu.Unlock(); 4642 } 4643 4644 void test10() { 4645 if (!(c || !mu.TryLock())) { 4646 a = 0; 4647 mu.Unlock(); 4648 } 4649 } 4650 }; 4651 4652 } // end namespace LogicalConditionalTryLock 4653 4654 4655 4656 namespace PtGuardedByTest { 4657 4658 void doSomething(); 4659 4660 class Cell { 4661 public: 4662 int a; 4663 }; 4664 4665 4666 // This mainly duplicates earlier tests, but just to make sure... 4667 class PtGuardedBySanityTest { 4668 Mutex mu1; 4669 Mutex mu2; 4670 int* a GUARDED_BY(mu1) PT_GUARDED_BY(mu2); 4671 Cell* c GUARDED_BY(mu1) PT_GUARDED_BY(mu2); 4672 int sa[10] GUARDED_BY(mu1); 4673 Cell sc[10] GUARDED_BY(mu1); 4674 4675 void test1() { 4676 mu1.Lock(); 4677 if (a == 0) doSomething(); // OK, we don't dereference. 4678 a = 0; 4679 c = 0; 4680 if (sa[0] == 42) doSomething(); 4681 sa[0] = 57; 4682 if (sc[0].a == 42) doSomething(); 4683 sc[0].a = 57; 4684 mu1.Unlock(); 4685 } 4686 4687 void test2() { 4688 mu1.ReaderLock(); 4689 if (*a == 0) doSomething(); // expected-warning {{reading the value pointed to by 'a' requires holding mutex 'mu2'}} 4690 *a = 0; // expected-warning {{writing the value pointed to by 'a' requires holding mutex 'mu2' exclusively}} 4691 4692 if (c->a == 0) doSomething(); // expected-warning {{reading the value pointed to by 'c' requires holding mutex 'mu2'}} 4693 c->a = 0; // expected-warning {{writing the value pointed to by 'c' requires holding mutex 'mu2' exclusively}} 4694 4695 if ((*c).a == 0) doSomething(); // expected-warning {{reading the value pointed to by 'c' requires holding mutex 'mu2'}} 4696 (*c).a = 0; // expected-warning {{writing the value pointed to by 'c' requires holding mutex 'mu2' exclusively}} 4697 4698 if (a[0] == 42) doSomething(); // expected-warning {{reading the value pointed to by 'a' requires holding mutex 'mu2'}} 4699 a[0] = 57; // expected-warning {{writing the value pointed to by 'a' requires holding mutex 'mu2' exclusively}} 4700 if (c[0].a == 42) doSomething(); // expected-warning {{reading the value pointed to by 'c' requires holding mutex 'mu2'}} 4701 c[0].a = 57; // expected-warning {{writing the value pointed to by 'c' requires holding mutex 'mu2' exclusively}} 4702 mu1.Unlock(); 4703 } 4704 4705 void test3() { 4706 mu2.Lock(); 4707 if (*a == 0) doSomething(); // expected-warning {{reading variable 'a' requires holding mutex 'mu1'}} 4708 *a = 0; // expected-warning {{reading variable 'a' requires holding mutex 'mu1'}} 4709 4710 if (c->a == 0) doSomething(); // expected-warning {{reading variable 'c' requires holding mutex 'mu1'}} 4711 c->a = 0; // expected-warning {{reading variable 'c' requires holding mutex 'mu1'}} 4712 4713 if ((*c).a == 0) doSomething(); // expected-warning {{reading variable 'c' requires holding mutex 'mu1'}} 4714 (*c).a = 0; // expected-warning {{reading variable 'c' requires holding mutex 'mu1'}} 4715 4716 if (a[0] == 42) doSomething(); // expected-warning {{reading variable 'a' requires holding mutex 'mu1'}} 4717 a[0] = 57; // expected-warning {{reading variable 'a' requires holding mutex 'mu1'}} 4718 if (c[0].a == 42) doSomething(); // expected-warning {{reading variable 'c' requires holding mutex 'mu1'}} 4719 c[0].a = 57; // expected-warning {{reading variable 'c' requires holding mutex 'mu1'}} 4720 mu2.Unlock(); 4721 } 4722 4723 void test4() { // Literal arrays 4724 if (sa[0] == 42) doSomething(); // expected-warning {{reading variable 'sa' requires holding mutex 'mu1'}} 4725 sa[0] = 57; // expected-warning {{writing variable 'sa' requires holding mutex 'mu1' exclusively}} 4726 if (sc[0].a == 42) doSomething(); // expected-warning {{reading variable 'sc' requires holding mutex 'mu1'}} 4727 sc[0].a = 57; // expected-warning {{writing variable 'sc' requires holding mutex 'mu1' exclusively}} 4728 4729 if (*sa == 42) doSomething(); // expected-warning {{reading variable 'sa' requires holding mutex 'mu1'}} 4730 *sa = 57; // expected-warning {{writing variable 'sa' requires holding mutex 'mu1' exclusively}} 4731 if ((*sc).a == 42) doSomething(); // expected-warning {{reading variable 'sc' requires holding mutex 'mu1'}} 4732 (*sc).a = 57; // expected-warning {{writing variable 'sc' requires holding mutex 'mu1' exclusively}} 4733 if (sc->a == 42) doSomething(); // expected-warning {{reading variable 'sc' requires holding mutex 'mu1'}} 4734 sc->a = 57; // expected-warning {{writing variable 'sc' requires holding mutex 'mu1' exclusively}} 4735 } 4736 4737 void test5() { 4738 mu1.ReaderLock(); // OK -- correct use. 4739 mu2.Lock(); 4740 if (*a == 0) doSomething(); 4741 *a = 0; 4742 4743 if (c->a == 0) doSomething(); 4744 c->a = 0; 4745 4746 if ((*c).a == 0) doSomething(); 4747 (*c).a = 0; 4748 mu2.Unlock(); 4749 mu1.Unlock(); 4750 } 4751 }; 4752 4753 4754 class SmartPtr_PtGuardedBy_Test { 4755 Mutex mu1; 4756 Mutex mu2; 4757 SmartPtr<int> sp GUARDED_BY(mu1) PT_GUARDED_BY(mu2); 4758 SmartPtr<Cell> sq GUARDED_BY(mu1) PT_GUARDED_BY(mu2); 4759 4760 void test1() { 4761 mu1.ReaderLock(); 4762 mu2.Lock(); 4763 4764 sp.get(); 4765 if (*sp == 0) doSomething(); 4766 *sp = 0; 4767 sq->a = 0; 4768 4769 if (sp[0] == 0) doSomething(); 4770 sp[0] = 0; 4771 4772 mu2.Unlock(); 4773 mu1.Unlock(); 4774 } 4775 4776 void test2() { 4777 mu2.Lock(); 4778 4779 sp.get(); // expected-warning {{reading variable 'sp' requires holding mutex 'mu1'}} 4780 if (*sp == 0) doSomething(); // expected-warning {{reading variable 'sp' requires holding mutex 'mu1'}} 4781 *sp = 0; // expected-warning {{reading variable 'sp' requires holding mutex 'mu1'}} 4782 sq->a = 0; // expected-warning {{reading variable 'sq' requires holding mutex 'mu1'}} 4783 4784 if (sp[0] == 0) doSomething(); // expected-warning {{reading variable 'sp' requires holding mutex 'mu1'}} 4785 sp[0] = 0; // expected-warning {{reading variable 'sp' requires holding mutex 'mu1'}} 4786 if (sq[0].a == 0) doSomething(); // expected-warning {{reading variable 'sq' requires holding mutex 'mu1'}} 4787 sq[0].a = 0; // expected-warning {{reading variable 'sq' requires holding mutex 'mu1'}} 4788 4789 mu2.Unlock(); 4790 } 4791 4792 void test3() { 4793 mu1.Lock(); 4794 4795 sp.get(); 4796 if (*sp == 0) doSomething(); // expected-warning {{reading the value pointed to by 'sp' requires holding mutex 'mu2'}} 4797 *sp = 0; // expected-warning {{reading the value pointed to by 'sp' requires holding mutex 'mu2'}} 4798 sq->a = 0; // expected-warning {{reading the value pointed to by 'sq' requires holding mutex 'mu2'}} 4799 4800 if (sp[0] == 0) doSomething(); // expected-warning {{reading the value pointed to by 'sp' requires holding mutex 'mu2'}} 4801 sp[0] = 0; // expected-warning {{reading the value pointed to by 'sp' requires holding mutex 'mu2'}} 4802 if (sq[0].a == 0) doSomething(); // expected-warning {{reading the value pointed to by 'sq' requires holding mutex 'mu2'}} 4803 sq[0].a = 0; // expected-warning {{reading the value pointed to by 'sq' requires holding mutex 'mu2'}} 4804 4805 mu1.Unlock(); 4806 } 4807 }; 4808 4809 } // end namespace PtGuardedByTest 4810 4811 4812 namespace NonMemberCalleeICETest { 4813 4814 class A { 4815 void Run() { 4816 (RunHelper)(); // expected-warning {{calling function 'RunHelper' requires holding mutex 'M' exclusively}} 4817 } 4818 4819 void RunHelper() EXCLUSIVE_LOCKS_REQUIRED(M); 4820 Mutex M; 4821 }; 4822 4823 } // end namespace NonMemberCalleeICETest 4824 4825 4826 namespace pt_guard_attribute_type { 4827 int i PT_GUARDED_BY(sls_mu); // expected-warning {{'pt_guarded_by' only applies to pointer types; type here is 'int'}} 4828 int j PT_GUARDED_VAR; // expected-warning {{'pt_guarded_var' only applies to pointer types; type here is 'int'}} 4829 4830 void test() { 4831 int i PT_GUARDED_BY(sls_mu); // expected-warning {{'pt_guarded_by' attribute only applies to non-static data members and global variables}} 4832 int j PT_GUARDED_VAR; // expected-warning {{'pt_guarded_var' attribute only applies to non-static data members and global variables}} 4833 4834 typedef int PT_GUARDED_BY(sls_mu) bad1; // expected-warning {{'pt_guarded_by' attribute only applies to}} 4835 typedef int PT_GUARDED_VAR bad2; // expected-warning {{'pt_guarded_var' attribute only applies to}} 4836 } 4837 } // end namespace pt_guard_attribute_type 4838 4839 4840 namespace ThreadAttributesOnLambdas { 4841 4842 class Foo { 4843 Mutex mu_; 4844 4845 void LockedFunction() EXCLUSIVE_LOCKS_REQUIRED(mu_); 4846 4847 void test() { 4848 auto func1 = [this]() EXCLUSIVE_LOCKS_REQUIRED(mu_) { 4849 LockedFunction(); 4850 }; 4851 4852 auto func2 = [this]() NO_THREAD_SAFETY_ANALYSIS { 4853 LockedFunction(); 4854 }; 4855 4856 auto func3 = [this]() EXCLUSIVE_LOCK_FUNCTION(mu_) { 4857 mu_.Lock(); 4858 }; 4859 4860 func1(); // expected-warning {{calling function 'operator()' requires holding mutex 'mu_' exclusively}} 4861 func2(); 4862 func3(); 4863 mu_.Unlock(); 4864 } 4865 }; 4866 4867 } // end namespace ThreadAttributesOnLambdas 4868 4869 4870 4871 namespace AttributeExpressionCornerCases { 4872 4873 class Foo { 4874 int a GUARDED_BY(getMu()); 4875 4876 Mutex* getMu() LOCK_RETURNED(""); 4877 Mutex* getUniv() LOCK_RETURNED("*"); 4878 4879 void test1() { 4880 a = 0; 4881 } 4882 4883 void test2() EXCLUSIVE_LOCKS_REQUIRED(getUniv()) { 4884 a = 0; 4885 } 4886 4887 void foo(Mutex* mu) EXCLUSIVE_LOCKS_REQUIRED(mu); 4888 4889 void test3() { 4890 foo(nullptr); 4891 } 4892 }; 4893 4894 4895 class MapTest { 4896 struct MuCell { Mutex* mu; }; 4897 4898 MyMap<MyString, Mutex*> map; 4899 MyMap<MyString, MuCell> mapCell; 4900 4901 int a GUARDED_BY(map["foo"]); 4902 int b GUARDED_BY(mapCell["foo"].mu); 4903 4904 void test() { 4905 map["foo"]->Lock(); 4906 a = 0; 4907 map["foo"]->Unlock(); 4908 } 4909 4910 void test2() { 4911 mapCell["foo"].mu->Lock(); 4912 b = 0; 4913 mapCell["foo"].mu->Unlock(); 4914 } 4915 }; 4916 4917 4918 class PreciseSmartPtr { 4919 SmartPtr<Mutex> mu; 4920 int val GUARDED_BY(mu); 4921 4922 static bool compare(PreciseSmartPtr& a, PreciseSmartPtr &b) { 4923 a.mu->Lock(); 4924 bool result = (a.val == b.val); // expected-warning {{reading variable 'val' requires holding mutex 'b.mu'}} \ 4925 // expected-note {{found near match 'a.mu'}} 4926 a.mu->Unlock(); 4927 return result; 4928 } 4929 }; 4930 4931 4932 class SmartRedeclare { 4933 SmartPtr<Mutex> mu; 4934 int val GUARDED_BY(mu); 4935 4936 void test() EXCLUSIVE_LOCKS_REQUIRED(mu); 4937 void test2() EXCLUSIVE_LOCKS_REQUIRED(mu.get()); 4938 void test3() EXCLUSIVE_LOCKS_REQUIRED(mu.get()); 4939 }; 4940 4941 4942 void SmartRedeclare::test() EXCLUSIVE_LOCKS_REQUIRED(mu.get()) { 4943 val = 0; 4944 } 4945 4946 void SmartRedeclare::test2() EXCLUSIVE_LOCKS_REQUIRED(mu) { 4947 val = 0; 4948 } 4949 4950 void SmartRedeclare::test3() { 4951 val = 0; 4952 } 4953 4954 4955 namespace CustomMutex { 4956 4957 4958 class LOCKABLE BaseMutex { }; 4959 class DerivedMutex : public BaseMutex { }; 4960 4961 void customLock(const BaseMutex *m) EXCLUSIVE_LOCK_FUNCTION(m); 4962 void customUnlock(const BaseMutex *m) UNLOCK_FUNCTION(m); 4963 4964 static struct DerivedMutex custMu; 4965 4966 static void doSomethingRequiringLock() EXCLUSIVE_LOCKS_REQUIRED(custMu) { } 4967 4968 void customTest() { 4969 customLock(reinterpret_cast<BaseMutex*>(&custMu)); // ignore casts 4970 doSomethingRequiringLock(); 4971 customUnlock(reinterpret_cast<BaseMutex*>(&custMu)); 4972 } 4973 4974 } // end namespace CustomMutex 4975 4976 } // end AttributeExpressionCornerCases 4977 4978 4979 namespace ScopedLockReturnedInvalid { 4980 4981 class Opaque; 4982 4983 Mutex* getMutex(Opaque* o) LOCK_RETURNED(""); 4984 4985 void test(Opaque* o) { 4986 MutexLock lock(getMutex(o)); 4987 } 4988 4989 } // end namespace ScopedLockReturnedInvalid 4990 4991 4992 namespace NegativeRequirements { 4993 4994 class Bar { 4995 Mutex mu; 4996 int a GUARDED_BY(mu); 4997 4998 public: 4999 void baz() EXCLUSIVE_LOCKS_REQUIRED(!mu) { 5000 mu.Lock(); 5001 a = 0; 5002 mu.Unlock(); 5003 } 5004 }; 5005 5006 5007 class Foo { 5008 Mutex mu; 5009 int a GUARDED_BY(mu); 5010 5011 public: 5012 void foo() { 5013 mu.Lock(); // warning? needs !mu? 5014 baz(); // expected-warning {{cannot call function 'baz' while mutex 'mu' is held}} 5015 bar(); 5016 mu.Unlock(); 5017 } 5018 5019 void bar() { 5020 bar2(); // expected-warning {{calling function 'bar2' requires negative capability '!mu'}} 5021 } 5022 5023 void bar2() EXCLUSIVE_LOCKS_REQUIRED(!mu) { 5024 baz(); 5025 } 5026 5027 void baz() EXCLUSIVE_LOCKS_REQUIRED(!mu) { 5028 mu.Lock(); 5029 a = 0; 5030 mu.Unlock(); 5031 } 5032 5033 void test() { 5034 Bar b; 5035 b.baz(); // no warning -- in different class. 5036 } 5037 }; 5038 5039 } // end namespace NegativeRequirements 5040 5041 5042 namespace NegativeThreadRoles { 5043 5044 typedef int __attribute__((capability("role"))) ThreadRole; 5045 5046 void acquire(ThreadRole R) EXCLUSIVE_LOCK_FUNCTION(R) NO_THREAD_SAFETY_ANALYSIS {} 5047 void release(ThreadRole R) UNLOCK_FUNCTION(R) NO_THREAD_SAFETY_ANALYSIS {} 5048 5049 ThreadRole FlightControl, Logger; 5050 5051 extern void enque_log_msg(const char *msg); 5052 void log_msg(const char *msg) { 5053 enque_log_msg(msg); 5054 } 5055 5056 void dispatch_log(const char *msg) __attribute__((requires_capability(!FlightControl))) {} 5057 void dispatch_log2(const char *msg) __attribute__((requires_capability(Logger))) {} 5058 5059 void flight_control_entry(void) __attribute__((requires_capability(FlightControl))) { 5060 dispatch_log("wrong"); /* expected-warning {{cannot call function 'dispatch_log' while mutex 'FlightControl' is held}} */ 5061 dispatch_log2("also wrong"); /* expected-warning {{calling function 'dispatch_log2' requires holding role 'Logger' exclusively}} */ 5062 } 5063 5064 void spawn_fake_flight_control_thread(void) { 5065 acquire(FlightControl); 5066 flight_control_entry(); 5067 release(FlightControl); 5068 } 5069 5070 extern const char *deque_log_msg(void) __attribute__((requires_capability(Logger))); 5071 void logger_entry(void) __attribute__((requires_capability(Logger))) 5072 __attribute__((requires_capability(!FlightControl))) { 5073 const char *msg; 5074 5075 while ((msg = deque_log_msg())) { 5076 dispatch_log(msg); 5077 } 5078 } 5079 5080 void spawn_fake_logger_thread(void) __attribute__((requires_capability(!FlightControl))) { 5081 acquire(Logger); 5082 logger_entry(); 5083 release(Logger); 5084 } 5085 5086 int main(void) __attribute__((requires_capability(!FlightControl))) { 5087 spawn_fake_flight_control_thread(); 5088 spawn_fake_logger_thread(); 5089 5090 for (;;) 5091 ; /* Pretend to dispatch things. */ 5092 5093 return 0; 5094 } 5095 5096 } // end namespace NegativeThreadRoles 5097 5098 5099 namespace AssertSharedExclusive { 5100 5101 void doSomething(); 5102 5103 class Foo { 5104 Mutex mu; 5105 int a GUARDED_BY(mu); 5106 5107 void test() SHARED_LOCKS_REQUIRED(mu) { 5108 mu.AssertHeld(); 5109 if (a > 0) 5110 doSomething(); 5111 } 5112 }; 5113 5114 } // end namespace AssertSharedExclusive 5115 5116 5117 namespace RangeBasedForAndReferences { 5118 5119 class Foo { 5120 struct MyStruct { 5121 int a; 5122 }; 5123 5124 Mutex mu; 5125 int a GUARDED_BY(mu); 5126 MyContainer<int> cntr GUARDED_BY(mu); 5127 MyStruct s GUARDED_BY(mu); 5128 int arr[10] GUARDED_BY(mu); 5129 5130 void nonref_test() { 5131 int b = a; // expected-warning {{reading variable 'a' requires holding mutex 'mu'}} 5132 b = 0; // no warning 5133 } 5134 5135 void auto_test() { 5136 auto b = a; // expected-warning {{reading variable 'a' requires holding mutex 'mu'}} 5137 b = 0; // no warning 5138 auto &c = a; // no warning 5139 c = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu' exclusively}} 5140 } 5141 5142 void ref_test() { 5143 int &b = a; 5144 int &c = b; 5145 int &d = c; 5146 b = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu' exclusively}} 5147 c = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu' exclusively}} 5148 d = 0; // expected-warning {{writing variable 'a' requires holding mutex 'mu' exclusively}} 5149 5150 MyStruct &rs = s; 5151 rs.a = 0; // expected-warning {{writing variable 's' requires holding mutex 'mu' exclusively}} 5152 5153 int (&rarr)[10] = arr; 5154 rarr[2] = 0; // expected-warning {{writing variable 'arr' requires holding mutex 'mu' exclusively}} 5155 } 5156 5157 void ptr_test() { 5158 int *b = &a; 5159 *b = 0; // no expected warning yet 5160 } 5161 5162 void for_test() { 5163 int total = 0; 5164 for (int i : cntr) { // expected-warning2 {{reading variable 'cntr' requires holding mutex 'mu'}} 5165 total += i; 5166 } 5167 } 5168 }; 5169 5170 5171 } // end namespace RangeBasedForAndReferences 5172 5173 5174 5175 namespace PassByRefTest { 5176 5177 class Foo { 5178 public: 5179 Foo() : a(0), b(0) { } 5180 5181 int a; 5182 int b; 5183 5184 void operator+(const Foo& f); 5185 5186 void operator[](const Foo& g); 5187 5188 void operator()(); 5189 }; 5190 5191 template<class T> 5192 T&& mymove(T& f); 5193 5194 5195 // test top-level functions 5196 void copy(Foo f); 5197 void write1(Foo& f); 5198 void write2(int a, Foo& f); 5199 void read1(const Foo& f); 5200 void read2(int a, const Foo& f); 5201 void destroy(Foo&& f); 5202 5203 void operator/(const Foo& f, const Foo& g); 5204 void operator*(const Foo& f, const Foo& g); 5205 5206 // Test constructors. 5207 struct FooRead { 5208 FooRead(const Foo &); 5209 }; 5210 struct FooWrite { 5211 FooWrite(Foo &); 5212 }; 5213 5214 // Test variadic functions 5215 template<typename... T> 5216 void copyVariadic(T...) {} 5217 template<typename... T> 5218 void writeVariadic(T&...) {} 5219 template<typename... T> 5220 void readVariadic(const T&...) {} 5221 5222 void copyVariadicC(int, ...); 5223 5224 class Bar { 5225 public: 5226 Mutex mu; 5227 Foo foo GUARDED_BY(mu); 5228 Foo foo2 GUARDED_BY(mu); 5229 Foo* foop PT_GUARDED_BY(mu); 5230 SmartPtr<Foo> foosp PT_GUARDED_BY(mu); 5231 5232 // test methods. 5233 void mwrite1(Foo& f); 5234 void mwrite2(int a, Foo& f); 5235 void mread1(const Foo& f); 5236 void mread2(int a, const Foo& f); 5237 5238 // static methods 5239 static void smwrite1(Foo& f); 5240 static void smwrite2(int a, Foo& f); 5241 static void smread1(const Foo& f); 5242 static void smread2(int a, const Foo& f); 5243 5244 void operator<<(const Foo& f); 5245 5246 void test1() { 5247 copy(foo); // expected-warning {{reading variable 'foo' requires holding mutex 'mu'}} 5248 write1(foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} 5249 write2(10, foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} 5250 read1(foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} 5251 read2(10, foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} 5252 destroy(mymove(foo)); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} 5253 5254 copyVariadic(foo); // expected-warning {{reading variable 'foo' requires holding mutex 'mu'}} 5255 readVariadic(foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} 5256 writeVariadic(foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} 5257 copyVariadicC(1, foo); // expected-warning {{reading variable 'foo' requires holding mutex 'mu'}} 5258 5259 FooRead reader(foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} 5260 FooWrite writer(foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} 5261 5262 mwrite1(foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} 5263 mwrite2(10, foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} 5264 mread1(foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} 5265 mread2(10, foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} 5266 5267 smwrite1(foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} 5268 smwrite2(10, foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} 5269 smread1(foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} 5270 smread2(10, foo); // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} 5271 5272 foo + foo2; // expected-warning {{reading variable 'foo' requires holding mutex 'mu'}} \ 5273 // expected-warning {{passing variable 'foo2' by reference requires holding mutex 'mu'}} 5274 foo / foo2; // expected-warning {{reading variable 'foo' requires holding mutex 'mu'}} \ 5275 // expected-warning {{passing variable 'foo2' by reference requires holding mutex 'mu'}} 5276 foo * foo2; // expected-warning {{reading variable 'foo' requires holding mutex 'mu'}} \ 5277 // expected-warning {{passing variable 'foo2' by reference requires holding mutex 'mu'}} 5278 foo[foo2]; // expected-warning {{reading variable 'foo' requires holding mutex 'mu'}} \ 5279 // expected-warning {{passing variable 'foo2' by reference requires holding mutex 'mu'}} 5280 foo(); // expected-warning {{reading variable 'foo' requires holding mutex 'mu'}} 5281 (*this) << foo; // expected-warning {{passing variable 'foo' by reference requires holding mutex 'mu'}} 5282 5283 copy(*foop); // expected-warning {{reading the value pointed to by 'foop' requires holding mutex 'mu'}} 5284 write1(*foop); // expected-warning {{passing the value that 'foop' points to by reference requires holding mutex 'mu'}} 5285 write2(10, *foop); // expected-warning {{passing the value that 'foop' points to by reference requires holding mutex 'mu'}} 5286 read1(*foop); // expected-warning {{passing the value that 'foop' points to by reference requires holding mutex 'mu'}} 5287 read2(10, *foop); // expected-warning {{passing the value that 'foop' points to by reference requires holding mutex 'mu'}} 5288 destroy(mymove(*foop)); // expected-warning {{passing the value that 'foop' points to by reference requires holding mutex 'mu'}} 5289 5290 copy(*foosp); // expected-warning {{reading the value pointed to by 'foosp' requires holding mutex 'mu'}} 5291 write1(*foosp); // expected-warning {{reading the value pointed to by 'foosp' requires holding mutex 'mu'}} 5292 write2(10, *foosp); // expected-warning {{reading the value pointed to by 'foosp' requires holding mutex 'mu'}} 5293 read1(*foosp); // expected-warning {{reading the value pointed to by 'foosp' requires holding mutex 'mu'}} 5294 read2(10, *foosp); // expected-warning {{reading the value pointed to by 'foosp' requires holding mutex 'mu'}} 5295 destroy(mymove(*foosp)); // expected-warning {{reading the value pointed to by 'foosp' requires holding mutex 'mu'}} 5296 5297 // TODO -- these require better smart pointer handling. 5298 copy(*foosp.get()); 5299 write1(*foosp.get()); 5300 write2(10, *foosp.get()); 5301 read1(*foosp.get()); 5302 read2(10, *foosp.get()); 5303 destroy(mymove(*foosp.get())); 5304 } 5305 }; 5306 5307 5308 } // end namespace PassByRefTest 5309 5310 5311 namespace AcquiredBeforeAfterText { 5312 5313 class Foo { 5314 Mutex mu1 ACQUIRED_BEFORE(mu2, mu3); 5315 Mutex mu2; 5316 Mutex mu3; 5317 5318 void test1() { 5319 mu1.Lock(); 5320 mu2.Lock(); 5321 mu3.Lock(); 5322 5323 mu3.Unlock(); 5324 mu2.Unlock(); 5325 mu1.Unlock(); 5326 } 5327 5328 void test2() { 5329 mu2.Lock(); 5330 mu1.Lock(); // expected-warning {{mutex 'mu1' must be acquired before 'mu2'}} 5331 mu1.Unlock(); 5332 mu2.Unlock(); 5333 } 5334 5335 void test3() { 5336 mu3.Lock(); 5337 mu1.Lock(); // expected-warning {{mutex 'mu1' must be acquired before 'mu3'}} 5338 mu1.Unlock(); 5339 mu3.Unlock(); 5340 } 5341 5342 void test4() EXCLUSIVE_LOCKS_REQUIRED(mu1) { 5343 mu2.Lock(); 5344 mu2.Unlock(); 5345 } 5346 5347 void test5() EXCLUSIVE_LOCKS_REQUIRED(mu2) { 5348 mu1.Lock(); // expected-warning {{mutex 'mu1' must be acquired before 'mu2'}} 5349 mu1.Unlock(); 5350 } 5351 5352 void test6() EXCLUSIVE_LOCKS_REQUIRED(mu2) { 5353 mu1.AssertHeld(); 5354 } 5355 5356 void test7() EXCLUSIVE_LOCKS_REQUIRED(mu1, mu2, mu3) { } 5357 5358 void test8() EXCLUSIVE_LOCKS_REQUIRED(mu3, mu2, mu1) { } 5359 }; 5360 5361 5362 class Foo2 { 5363 Mutex mu1; 5364 Mutex mu2 ACQUIRED_AFTER(mu1); 5365 Mutex mu3 ACQUIRED_AFTER(mu1); 5366 5367 void test1() { 5368 mu1.Lock(); 5369 mu2.Lock(); 5370 mu3.Lock(); 5371 5372 mu3.Unlock(); 5373 mu2.Unlock(); 5374 mu1.Unlock(); 5375 } 5376 5377 void test2() { 5378 mu2.Lock(); 5379 mu1.Lock(); // expected-warning {{mutex 'mu1' must be acquired before 'mu2'}} 5380 mu1.Unlock(); 5381 mu2.Unlock(); 5382 } 5383 5384 void test3() { 5385 mu3.Lock(); 5386 mu1.Lock(); // expected-warning {{mutex 'mu1' must be acquired before 'mu3'}} 5387 mu1.Unlock(); 5388 mu3.Unlock(); 5389 } 5390 }; 5391 5392 5393 class Foo3 { 5394 Mutex mu1 ACQUIRED_BEFORE(mu2); 5395 Mutex mu2; 5396 Mutex mu3 ACQUIRED_AFTER(mu2) ACQUIRED_BEFORE(mu4); 5397 Mutex mu4; 5398 5399 void test1() { 5400 mu1.Lock(); 5401 mu2.Lock(); 5402 mu3.Lock(); 5403 mu4.Lock(); 5404 5405 mu4.Unlock(); 5406 mu3.Unlock(); 5407 mu2.Unlock(); 5408 mu1.Unlock(); 5409 } 5410 5411 void test2() { 5412 mu4.Lock(); 5413 mu2.Lock(); // expected-warning {{mutex 'mu2' must be acquired before 'mu4'}} 5414 5415 mu2.Unlock(); 5416 mu4.Unlock(); 5417 } 5418 5419 void test3() { 5420 mu4.Lock(); 5421 mu1.Lock(); // expected-warning {{mutex 'mu1' must be acquired before 'mu4'}} 5422 5423 mu1.Unlock(); 5424 mu4.Unlock(); 5425 } 5426 5427 void test4() { 5428 mu3.Lock(); 5429 mu1.Lock(); // expected-warning {{mutex 'mu1' must be acquired before 'mu3'}} 5430 5431 mu1.Unlock(); 5432 mu3.Unlock(); 5433 } 5434 }; 5435 5436 5437 // Test transitive DAG traversal with AFTER 5438 class Foo4 { 5439 Mutex mu1; 5440 Mutex mu2 ACQUIRED_AFTER(mu1); 5441 Mutex mu3 ACQUIRED_AFTER(mu1); 5442 Mutex mu4 ACQUIRED_AFTER(mu2, mu3); 5443 Mutex mu5 ACQUIRED_AFTER(mu4); 5444 Mutex mu6 ACQUIRED_AFTER(mu4); 5445 Mutex mu7 ACQUIRED_AFTER(mu5, mu6); 5446 Mutex mu8 ACQUIRED_AFTER(mu7); 5447 5448 void test() { 5449 mu8.Lock(); 5450 mu1.Lock(); // expected-warning {{mutex 'mu1' must be acquired before 'mu8'}} 5451 mu1.Unlock(); 5452 mu8.Unlock(); 5453 } 5454 }; 5455 5456 5457 // Test transitive DAG traversal with BEFORE 5458 class Foo5 { 5459 Mutex mu1 ACQUIRED_BEFORE(mu2, mu3); 5460 Mutex mu2 ACQUIRED_BEFORE(mu4); 5461 Mutex mu3 ACQUIRED_BEFORE(mu4); 5462 Mutex mu4 ACQUIRED_BEFORE(mu5, mu6); 5463 Mutex mu5 ACQUIRED_BEFORE(mu7); 5464 Mutex mu6 ACQUIRED_BEFORE(mu7); 5465 Mutex mu7 ACQUIRED_BEFORE(mu8); 5466 Mutex mu8; 5467 5468 void test() { 5469 mu8.Lock(); 5470 mu1.Lock(); // expected-warning {{mutex 'mu1' must be acquired before 'mu8'}} 5471 mu1.Unlock(); 5472 mu8.Unlock(); 5473 } 5474 }; 5475 5476 5477 class Foo6 { 5478 Mutex mu1 ACQUIRED_AFTER(mu3); // expected-warning {{Cycle in acquired_before/after dependencies, starting with 'mu1'}} 5479 Mutex mu2 ACQUIRED_AFTER(mu1); // expected-warning {{Cycle in acquired_before/after dependencies, starting with 'mu2'}} 5480 Mutex mu3 ACQUIRED_AFTER(mu2); // expected-warning {{Cycle in acquired_before/after dependencies, starting with 'mu3'}} 5481 5482 Mutex mu_b ACQUIRED_BEFORE(mu_b); // expected-warning {{Cycle in acquired_before/after dependencies, starting with 'mu_b'}} 5483 Mutex mu_a ACQUIRED_AFTER(mu_a); // expected-warning {{Cycle in acquired_before/after dependencies, starting with 'mu_a'}} 5484 5485 void test0() { 5486 mu_a.Lock(); 5487 mu_b.Lock(); 5488 mu_b.Unlock(); 5489 mu_a.Unlock(); 5490 } 5491 5492 void test1a() { 5493 mu1.Lock(); 5494 mu1.Unlock(); 5495 } 5496 5497 void test1b() { 5498 mu1.Lock(); 5499 mu_a.Lock(); 5500 mu_b.Lock(); 5501 mu_b.Unlock(); 5502 mu_a.Unlock(); 5503 mu1.Unlock(); 5504 } 5505 5506 void test() { 5507 mu2.Lock(); 5508 mu2.Unlock(); 5509 } 5510 5511 void test3() { 5512 mu3.Lock(); 5513 mu3.Unlock(); 5514 } 5515 }; 5516 5517 } // end namespace AcquiredBeforeAfterTest 5518 5519 5520 namespace ScopedAdoptTest { 5521 5522 class Foo { 5523 Mutex mu; 5524 int a GUARDED_BY(mu); 5525 int b; 5526 5527 void test1() EXCLUSIVE_UNLOCK_FUNCTION(mu) { 5528 MutexLock slock(&mu, true); 5529 a = 0; 5530 } 5531 5532 void test2() SHARED_UNLOCK_FUNCTION(mu) { 5533 ReaderMutexLock slock(&mu, true); 5534 b = a; 5535 } 5536 5537 void test3() EXCLUSIVE_LOCKS_REQUIRED(mu) { // expected-note {{mutex acquired here}} 5538 MutexLock slock(&mu, true); 5539 a = 0; 5540 } // expected-warning {{expecting mutex 'mu' to be held at the end of function}} 5541 5542 void test4() SHARED_LOCKS_REQUIRED(mu) { // expected-note {{mutex acquired here}} 5543 ReaderMutexLock slock(&mu, true); 5544 b = a; 5545 } // expected-warning {{expecting mutex 'mu' to be held at the end of function}} 5546 5547 }; 5548 5549 } // end namespace ScopedAdoptTest 5550 5551 5552 namespace TestReferenceNoThreadSafetyAnalysis { 5553 5554 #define TS_UNCHECKED_READ(x) ts_unchecked_read(x) 5555 5556 // Takes a reference to a guarded data member, and returns an unguarded 5557 // reference. 5558 template <class T> 5559 inline const T& ts_unchecked_read(const T& v) NO_THREAD_SAFETY_ANALYSIS { 5560 return v; 5561 } 5562 5563 template <class T> 5564 inline T& ts_unchecked_read(T& v) NO_THREAD_SAFETY_ANALYSIS { 5565 return v; 5566 } 5567 5568 5569 class Foo { 5570 public: 5571 Foo(): a(0) { } 5572 5573 int a; 5574 }; 5575 5576 5577 class Bar { 5578 public: 5579 Bar() : a(0) { } 5580 5581 Mutex mu; 5582 int a GUARDED_BY(mu); 5583 Foo foo GUARDED_BY(mu); 5584 }; 5585 5586 5587 void test() { 5588 Bar bar; 5589 const Bar cbar; 5590 5591 int a = TS_UNCHECKED_READ(bar.a); // nowarn 5592 TS_UNCHECKED_READ(bar.a) = 1; // nowarn 5593 5594 int b = TS_UNCHECKED_READ(bar.foo).a; // nowarn 5595 TS_UNCHECKED_READ(bar.foo).a = 1; // nowarn 5596 5597 int c = TS_UNCHECKED_READ(cbar.a); // nowarn 5598 } 5599 5600 #undef TS_UNCHECKED_READ 5601 5602 } // end namespace TestReferenceNoThreadSafetyAnalysis 5603 5604 5605 namespace GlobalAcquiredBeforeAfterTest { 5606 5607 Mutex mu1; 5608 Mutex mu2 ACQUIRED_AFTER(mu1); 5609 5610 void test3() { 5611 mu2.Lock(); 5612 mu1.Lock(); // expected-warning {{mutex 'mu1' must be acquired before 'mu2'}} 5613 mu1.Unlock(); 5614 mu2.Unlock(); 5615 } 5616 5617 } // end namespace GlobalAcquiredBeforeAfterTest 5618 5619 5620 namespace LifetimeExtensionText { 5621 5622 struct Holder { 5623 virtual ~Holder() throw() {} 5624 int i = 0; 5625 }; 5626 5627 void test() { 5628 // Should not crash. 5629 const auto &value = Holder().i; 5630 } 5631 5632 } // end namespace LifetimeExtensionTest 5633 5634 5635 namespace LockableUnions { 5636 5637 union LOCKABLE MutexUnion { 5638 int a; 5639 char* b; 5640 5641 void Lock() EXCLUSIVE_LOCK_FUNCTION(); 5642 void Unlock() UNLOCK_FUNCTION(); 5643 }; 5644 5645 MutexUnion muun2; 5646 MutexUnion muun1 ACQUIRED_BEFORE(muun2); 5647 5648 void test() { 5649 muun2.Lock(); 5650 muun1.Lock(); // expected-warning {{mutex 'muun1' must be acquired before 'muun2'}} 5651 muun1.Unlock(); 5652 muun2.Unlock(); 5653 } 5654 5655 } // end namespace LockableUnions 5656 5657 // This used to crash. 5658 class acquired_before_empty_str { 5659 void WaitUntilSpaceAvailable() { 5660 lock_.ReaderLock(); // expected-note {{acquired here}} 5661 } // expected-warning {{mutex 'lock_' is still held at the end of function}} 5662 Mutex lock_ ACQUIRED_BEFORE(""); 5663 }; 5664 5665 namespace PR34800 { 5666 struct A { 5667 operator int() const; 5668 }; 5669 struct B { 5670 bool g() __attribute__((locks_excluded(h))); // expected-warning {{'locks_excluded' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'int'}} 5671 int h; 5672 }; 5673 struct C { 5674 B *operator[](int); 5675 }; 5676 C c; 5677 void f() { c[A()]->g(); } 5678 } // namespace PR34800 5679 5680 namespace ReturnScopedLockable { 5681 template<typename Object> class SCOPED_LOCKABLE ReadLockedPtr { 5682 public: 5683 ReadLockedPtr(Object *ptr) SHARED_LOCK_FUNCTION((*this)->mutex); 5684 ReadLockedPtr(ReadLockedPtr &&) SHARED_LOCK_FUNCTION((*this)->mutex); 5685 ~ReadLockedPtr() UNLOCK_FUNCTION(); 5686 5687 Object *operator->() const { return object; } 5688 5689 private: 5690 Object *object; 5691 }; 5692 5693 struct Object { 5694 int f() SHARED_LOCKS_REQUIRED(mutex); 5695 Mutex mutex; 5696 }; 5697 5698 ReadLockedPtr<Object> get(); 5699 int use() { 5700 auto ptr = get(); 5701 return ptr->f(); 5702 } 5703 void use_constructor() { 5704 auto ptr = ReadLockedPtr<Object>(nullptr); 5705 ptr->f(); 5706 auto ptr2 = ReadLockedPtr<Object>{nullptr}; 5707 ptr2->f(); 5708 auto ptr3 = (ReadLockedPtr<Object>{nullptr}); 5709 ptr3->f(); 5710 } 5711 struct Convertible { 5712 Convertible(); 5713 operator ReadLockedPtr<Object>(); 5714 }; 5715 void use_conversion() { 5716 ReadLockedPtr<Object> ptr = Convertible(); 5717 ptr->f(); 5718 } 5719 } 5720 5721 namespace PR38640 { 5722 void f() { 5723 // Self-referencing assignment previously caused an infinite loop when thread 5724 // safety analysis was enabled. 5725 int &i = i; // expected-warning {{reference 'i' is not yet bound to a value when used within its own initialization}} 5726 } 5727 } 5728 5729 namespace Derived_Smart_Pointer { 5730 template <class T> 5731 class SmartPtr_Derived : public SmartPtr<T> {}; 5732 5733 class Foo { 5734 public: 5735 SmartPtr_Derived<Mutex> mu_; 5736 int a GUARDED_BY(mu_); 5737 int b GUARDED_BY(mu_.get()); 5738 int c GUARDED_BY(*mu_); 5739 5740 void Lock() EXCLUSIVE_LOCK_FUNCTION(mu_); 5741 void Unlock() UNLOCK_FUNCTION(mu_); 5742 5743 void test0() { 5744 a = 1; // expected-warning {{writing variable 'a' requires holding mutex 'mu_' exclusively}} 5745 b = 1; // expected-warning {{writing variable 'b' requires holding mutex 'mu_' exclusively}} 5746 c = 1; // expected-warning {{writing variable 'c' requires holding mutex 'mu_' exclusively}} 5747 } 5748 5749 void test1() { 5750 Lock(); 5751 a = 1; 5752 b = 1; 5753 c = 1; 5754 Unlock(); 5755 } 5756 }; 5757 5758 class Bar { 5759 SmartPtr_Derived<Foo> foo; 5760 5761 void test0() { 5762 foo->a = 1; // expected-warning {{writing variable 'a' requires holding mutex 'foo->mu_' exclusively}} 5763 (*foo).b = 1; // expected-warning {{writing variable 'b' requires holding mutex 'foo->mu_' exclusively}} 5764 foo.get()->c = 1; // expected-warning {{writing variable 'c' requires holding mutex 'foo->mu_' exclusively}} 5765 } 5766 5767 void test1() { 5768 foo->Lock(); 5769 foo->a = 1; 5770 foo->Unlock(); 5771 5772 foo->mu_->Lock(); 5773 foo->b = 1; 5774 foo->mu_->Unlock(); 5775 5776 MutexLock lock(foo->mu_.get()); 5777 foo->c = 1; 5778 } 5779 }; 5780 5781 class PointerGuard { 5782 Mutex mu1; 5783 Mutex mu2; 5784 SmartPtr_Derived<int> i GUARDED_BY(mu1) PT_GUARDED_BY(mu2); 5785 5786 void test0() { 5787 i.get(); // expected-warning {{reading variable 'i' requires holding mutex 'mu1'}} 5788 *i = 2; // expected-warning {{reading variable 'i' requires holding mutex 'mu1'}} \ 5789 // expected-warning {{reading the value pointed to by 'i' requires holding mutex 'mu2'}} 5790 5791 } 5792 5793 void test1() { 5794 mu1.Lock(); 5795 5796 i.get(); 5797 *i = 2; // expected-warning {{reading the value pointed to by 'i' requires holding mutex 'mu2'}} 5798 5799 mu1.Unlock(); 5800 } 5801 5802 void test2() { 5803 mu2.Lock(); 5804 5805 i.get(); // expected-warning {{reading variable 'i' requires holding mutex 'mu1'}} 5806 *i = 2; // expected-warning {{reading variable 'i' requires holding mutex 'mu1'}} 5807 5808 mu2.Unlock(); 5809 } 5810 5811 void test3() { 5812 mu1.Lock(); 5813 mu2.Lock(); 5814 5815 i.get(); 5816 *i = 2; 5817 5818 mu2.Unlock(); 5819 mu1.Unlock(); 5820 } 5821 }; 5822 } 5823