-
Notifications
You must be signed in to change notification settings - Fork 13.3k
/
Copy pathwarn-thread-safety-analysis.cpp
6845 lines (5421 loc) · 174 KB
/
warn-thread-safety-analysis.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -Wthread-safety -Wthread-safety-pointer -Wthread-safety-beta -Wno-thread-safety-negative -fcxx-exceptions -DUSE_CAPABILITY=0 %s
// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -Wthread-safety -Wthread-safety-pointer -Wthread-safety-beta -Wno-thread-safety-negative -fcxx-exceptions -DUSE_CAPABILITY=1 %s
// RUN: %clang_cc1 -fsyntax-only -verify -std=c++17 -Wthread-safety -Wthread-safety-pointer -Wthread-safety-beta -Wno-thread-safety-negative -fcxx-exceptions -DUSE_CAPABILITY=0 %s
// RUN: %clang_cc1 -fsyntax-only -verify -std=c++17 -Wthread-safety -Wthread-safety-pointer -Wthread-safety-beta -Wno-thread-safety-negative -fcxx-exceptions -DUSE_CAPABILITY=1 %s
// FIXME: should also run %clang_cc1 -fsyntax-only -verify -Wthread-safety -std=c++11 -Wc++98-compat %s
// FIXME: should also run %clang_cc1 -fsyntax-only -verify -Wthread-safety %s
#include "thread-safety-annotations.h"
class LOCKABLE Mutex {
public:
void Lock() EXCLUSIVE_LOCK_FUNCTION();
void ReaderLock() SHARED_LOCK_FUNCTION();
void Unlock() UNLOCK_FUNCTION();
void ExclusiveUnlock() EXCLUSIVE_UNLOCK_FUNCTION();
void ReaderUnlock() SHARED_UNLOCK_FUNCTION();
bool TryLock() EXCLUSIVE_TRYLOCK_FUNCTION(true);
bool ReaderTryLock() SHARED_TRYLOCK_FUNCTION(true);
void LockWhen(const int &cond) EXCLUSIVE_LOCK_FUNCTION();
void PromoteShared() SHARED_UNLOCK_FUNCTION() EXCLUSIVE_LOCK_FUNCTION();
void DemoteExclusive() EXCLUSIVE_UNLOCK_FUNCTION() SHARED_LOCK_FUNCTION();
// for negative capabilities
const Mutex& operator!() const { return *this; }
void AssertHeld() ASSERT_EXCLUSIVE_LOCK();
void AssertReaderHeld() ASSERT_SHARED_LOCK();
};
class SCOPED_LOCKABLE MutexLock {
public:
MutexLock(Mutex *mu) EXCLUSIVE_LOCK_FUNCTION(mu);
MutexLock(Mutex *mu, bool adopt) EXCLUSIVE_LOCKS_REQUIRED(mu);
~MutexLock() UNLOCK_FUNCTION();
};
class SCOPED_LOCKABLE ReaderMutexLock {
public:
ReaderMutexLock(Mutex *mu) SHARED_LOCK_FUNCTION(mu);
ReaderMutexLock(Mutex *mu, bool adopt) SHARED_LOCKS_REQUIRED(mu);
~ReaderMutexLock() UNLOCK_FUNCTION();
};
class SCOPED_LOCKABLE ReleasableMutexLock {
public:
ReleasableMutexLock(Mutex *mu) EXCLUSIVE_LOCK_FUNCTION(mu);
~ReleasableMutexLock() UNLOCK_FUNCTION();
void Release() UNLOCK_FUNCTION();
};
class SCOPED_LOCKABLE DoubleMutexLock {
public:
DoubleMutexLock(Mutex *mu1, Mutex *mu2) EXCLUSIVE_LOCK_FUNCTION(mu1, mu2);
~DoubleMutexLock() UNLOCK_FUNCTION();
};
class DeferTraits {};
struct SharedTraits {};
struct ExclusiveTraits {};
class SCOPED_LOCKABLE RelockableMutexLock {
public:
RelockableMutexLock(Mutex *mu, DeferTraits) LOCKS_EXCLUDED(mu);
RelockableMutexLock(Mutex *mu, SharedTraits) SHARED_LOCK_FUNCTION(mu);
RelockableMutexLock(Mutex *mu, ExclusiveTraits) EXCLUSIVE_LOCK_FUNCTION(mu);
~RelockableMutexLock() UNLOCK_FUNCTION();
void Lock() EXCLUSIVE_LOCK_FUNCTION();
void Unlock() UNLOCK_FUNCTION();
void ReaderLock() SHARED_LOCK_FUNCTION();
void ReaderUnlock() UNLOCK_FUNCTION();
void PromoteShared() UNLOCK_FUNCTION() EXCLUSIVE_LOCK_FUNCTION();
void DemoteExclusive() UNLOCK_FUNCTION() SHARED_LOCK_FUNCTION();
};
// The universal lock, written "*", allows checking to be selectively turned
// off for a particular piece of code.
void beginNoWarnOnReads() SHARED_LOCK_FUNCTION("*");
void endNoWarnOnReads() UNLOCK_FUNCTION("*");
void beginNoWarnOnWrites() EXCLUSIVE_LOCK_FUNCTION("*");
void endNoWarnOnWrites() UNLOCK_FUNCTION("*");
// For testing handling of smart pointers.
template<class T>
class SmartPtr {
public:
SmartPtr(T* p) : ptr_(p) { }
SmartPtr(const SmartPtr<T>& p) : ptr_(p.ptr_) { }
~SmartPtr();
T* get() const { return ptr_; }
T* operator->() const { return ptr_; }
T& operator*() const { return *ptr_; }
T& operator[](int i) const { return ptr_[i]; }
private:
T* ptr_;
};
template<typename T, typename U>
U& operator->*(const SmartPtr<T>& ptr, U T::*p) { return ptr->*p; }
// For testing destructor calls and cleanup.
class MyString {
public:
MyString(const char* s);
~MyString();
};
// For testing operator overloading
template <class K, class T>
class MyMap {
public:
T& operator[](const K& k);
};
// For testing handling of containers.
template <class T>
class MyContainer {
public:
MyContainer();
typedef T* iterator;
typedef const T* const_iterator;
T* begin();
T* end();
const T* cbegin();
const T* cend();
T& operator[](int i);
const T& operator[](int i) const;
private:
T* ptr_;
};
Mutex sls_mu;
Mutex sls_mu2 __attribute__((acquired_after(sls_mu)));
int sls_guard_var __attribute__((guarded_var)) = 0;
int sls_guardby_var __attribute__((guarded_by(sls_mu))) = 0;
bool getBool();
class MutexWrapper {
public:
Mutex mu;
int x __attribute__((guarded_by(mu)));
void MyLock() EXCLUSIVE_LOCK_FUNCTION(mu);
};
struct TestingMoreComplexAttributes {
Mutex lock;
struct { Mutex lock; } strct;
union {
bool a __attribute__((guarded_by(lock)));
bool b __attribute__((guarded_by(strct.lock)));
bool *ptr_a __attribute__((pt_guarded_by(lock)));
bool *ptr_b __attribute__((pt_guarded_by(strct.lock)));
Mutex lock1 __attribute__((acquired_before(lock))) __attribute__((acquired_before(strct.lock)));
Mutex lock2 __attribute__((acquired_after(lock))) __attribute__((acquired_after(strct.lock)));
};
} more_complex_atttributes;
void more_complex_attributes() {
more_complex_atttributes.a = true; // expected-warning{{writing variable 'a' requires holding mutex 'lock' exclusively}}
more_complex_atttributes.b = true; // expected-warning{{writing variable 'b' requires holding mutex 'strct.lock' exclusively}}
*more_complex_atttributes.ptr_a = true; // expected-warning{{writing the value pointed to by 'ptr_a' requires holding mutex 'lock' exclusively}}
*more_complex_atttributes.ptr_b = true; // expected-warning{{writing the value pointed to by 'ptr_b' requires holding mutex 'strct.lock' exclusively}}
more_complex_atttributes.lock.Lock();
more_complex_atttributes.lock1.Lock(); // expected-warning{{mutex 'lock1' must be acquired before 'lock'}}
more_complex_atttributes.lock1.Unlock();
more_complex_atttributes.lock.Unlock();
more_complex_atttributes.lock2.Lock();
more_complex_atttributes.lock.Lock(); // expected-warning{{mutex 'lock' must be acquired before 'lock2'}}
more_complex_atttributes.lock.Unlock();
more_complex_atttributes.lock2.Unlock();
}
MutexWrapper sls_mw;
void sls_fun_0() {
sls_mw.mu.Lock();
sls_mw.x = 5;
sls_mw.mu.Unlock();
}
void sls_fun_2() {
sls_mu.Lock();
int x = sls_guard_var;
sls_mu.Unlock();
}
void sls_fun_3() {
sls_mu.Lock();
sls_guard_var = 2;
sls_mu.Unlock();
}
void sls_fun_4() {
sls_mu2.Lock();
sls_guard_var = 2;
sls_mu2.Unlock();
}
void sls_fun_5() {
sls_mu.Lock();
int x = sls_guardby_var;
sls_mu.Unlock();
}
void sls_fun_6() {
sls_mu.Lock();
sls_guardby_var = 2;
sls_mu.Unlock();
}
void sls_fun_7() {
sls_mu.Lock();
sls_mu2.Lock();
sls_mu2.Unlock();
sls_mu.Unlock();
}
void sls_fun_8() {
sls_mu.Lock();
if (getBool())
sls_mu.Unlock();
else
sls_mu.Unlock();
}
void sls_fun_9() {
if (getBool())
sls_mu.Lock();
else
sls_mu.Lock();
sls_mu.Unlock();
}
void sls_fun_good_6() {
if (getBool()) {
sls_mu.Lock();
} else {
if (getBool()) {
getBool(); // EMPTY
} else {
getBool(); // EMPTY
}
sls_mu.Lock();
}
sls_mu.Unlock();
}
void sls_fun_good_7() {
sls_mu.Lock();
while (getBool()) {
sls_mu.Unlock();
if (getBool()) {
if (getBool()) {
sls_mu.Lock();
continue;
}
}
sls_mu.Lock();
}
sls_mu.Unlock();
}
void sls_fun_good_8() {
sls_mw.MyLock();
sls_mw.mu.Unlock();
}
void sls_fun_bad_1() {
sls_mu.Unlock(); // \
// expected-warning{{releasing mutex 'sls_mu' that was not held}}
}
void sls_fun_bad_2() {
sls_mu.Lock(); // expected-note{{mutex acquired here}}
sls_mu.Lock(); // \
// expected-warning{{acquiring mutex 'sls_mu' that is already held}}
sls_mu.Unlock();
}
void sls_fun_bad_3() {
sls_mu.Lock(); // expected-note {{mutex acquired here}}
} // expected-warning{{mutex 'sls_mu' is still held at the end of function}}
void sls_fun_bad_4() {
if (getBool())
sls_mu.Lock(); // expected-note{{mutex acquired here}}
else
sls_mu2.Lock(); // expected-note{{mutex acquired here}}
} // expected-warning{{mutex 'sls_mu' is not held on every path through here}} \
// expected-warning{{mutex 'sls_mu2' is not held on every path through here}}
void sls_fun_bad_5() {
sls_mu.Lock(); // expected-note {{mutex acquired here}}
if (getBool())
sls_mu.Unlock();
} // expected-warning{{mutex 'sls_mu' is not held on every path through here}}
void sls_fun_bad_6() {
if (getBool()) {
sls_mu.Lock(); // expected-note {{mutex acquired here}}
} else {
if (getBool()) {
getBool(); // EMPTY
} else {
getBool(); // EMPTY
}
}
sls_mu.Unlock(); // \
expected-warning{{mutex 'sls_mu' is not held on every path through here}}\
expected-warning{{releasing mutex 'sls_mu' that was not held}}
}
void sls_fun_bad_7() {
sls_mu.Lock();
while (getBool()) { // \
expected-warning{{expecting mutex 'sls_mu' to be held at start of each loop}}
sls_mu.Unlock();
if (getBool()) {
if (getBool()) {
continue;
}
}
sls_mu.Lock(); // expected-note {{mutex acquired here}}
}
sls_mu.Unlock();
}
void sls_fun_bad_8() {
sls_mu.Lock(); // expected-note{{mutex acquired here}}
do {
sls_mu.Unlock(); // expected-warning{{expecting mutex 'sls_mu' to be held at start of each loop}}
} while (getBool());
}
void sls_fun_bad_9() {
do {
sls_mu.Lock(); // \
// expected-warning{{expecting mutex 'sls_mu' to be held at start of each loop}} \
// expected-note{{mutex acquired here}}
} while (getBool());
sls_mu.Unlock();
}
void sls_fun_bad_10() {
sls_mu.Lock(); // expected-note 2{{mutex acquired here}}
while(getBool()) { // expected-warning{{expecting mutex 'sls_mu' to be held at start of each loop}}
sls_mu.Unlock();
}
} // expected-warning{{mutex 'sls_mu' is still held at the end of function}}
void sls_fun_bad_11() {
while (getBool()) { // \
expected-warning{{expecting mutex 'sls_mu' to be held at start of each loop}}
sls_mu.Lock(); // expected-note {{mutex acquired here}}
}
sls_mu.Unlock(); // \
// expected-warning{{releasing mutex 'sls_mu' that was not held}}
}
void sls_fun_bad_12() {
sls_mu.Lock(); // expected-note {{mutex acquired here}}
while (getBool()) {
sls_mu.Unlock();
if (getBool()) {
if (getBool()) {
break;
}
}
sls_mu.Lock();
}
sls_mu.Unlock(); // \
expected-warning{{mutex 'sls_mu' is not held on every path through here}} \
expected-warning{{releasing mutex 'sls_mu' that was not held}}
}
//-----------------------------------------//
// Handling lock expressions in attribute args
// -------------------------------------------//
Mutex aa_mu;
class GlobalLocker {
public:
void globalLock() EXCLUSIVE_LOCK_FUNCTION(aa_mu);
void globalUnlock() UNLOCK_FUNCTION(aa_mu);
};
GlobalLocker glock;
void aa_fun_1() {
glock.globalLock();
glock.globalUnlock();
}
void aa_fun_bad_1() {
glock.globalUnlock(); // \
// expected-warning{{releasing mutex 'aa_mu' that was not held}}
}
void aa_fun_bad_2() {
glock.globalLock(); // expected-note{{mutex acquired here}}
glock.globalLock(); // \
// expected-warning{{acquiring mutex 'aa_mu' that is already held}}
glock.globalUnlock();
}
void aa_fun_bad_3() {
glock.globalLock(); // expected-note{{mutex acquired here}}
} // expected-warning{{mutex 'aa_mu' is still held at the end of function}}
//--------------------------------------------------//
// Regression tests for unusual method names
//--------------------------------------------------//
Mutex wmu;
// Test diagnostics for other method names.
class WeirdMethods {
// FIXME: can't currently check inside constructors and destructors.
WeirdMethods() {
wmu.Lock(); // EXPECTED-NOTE {{mutex acquired here}}
} // EXPECTED-WARNING {{mutex 'wmu' is still held at the end of function}}
~WeirdMethods() {
wmu.Lock(); // EXPECTED-NOTE {{mutex acquired here}}
} // EXPECTED-WARNING {{mutex 'wmu' is still held at the end of function}}
void operator++() {
wmu.Lock(); // expected-note {{mutex acquired here}}
} // expected-warning {{mutex 'wmu' is still held at the end of function}}
operator int*() {
wmu.Lock(); // expected-note {{mutex acquired here}}
return 0;
} // expected-warning {{mutex 'wmu' is still held at the end of function}}
};
//-----------------------------------------------//
// Errors for guarded by or guarded var variables
// ----------------------------------------------//
int *pgb_gvar __attribute__((pt_guarded_var));
int *pgb_var __attribute__((pt_guarded_by(sls_mu)));
class PGBFoo {
public:
int x;
int *pgb_field __attribute__((guarded_by(sls_mu2)))
__attribute__((pt_guarded_by(sls_mu)));
void testFoo() {
pgb_field = &x; // \
// expected-warning {{writing variable 'pgb_field' requires holding mutex 'sls_mu2' exclusively}}
*pgb_field = x; // expected-warning {{reading variable 'pgb_field' requires holding mutex 'sls_mu2'}} \
// expected-warning {{writing the value pointed to by 'pgb_field' requires holding mutex 'sls_mu' exclusively}}
x = *pgb_field; // expected-warning {{reading variable 'pgb_field' requires holding mutex 'sls_mu2'}} \
// expected-warning {{reading the value pointed to by 'pgb_field' requires holding mutex 'sls_mu'}}
(*pgb_field)++; // expected-warning {{reading variable 'pgb_field' requires holding mutex 'sls_mu2'}} \
// expected-warning {{writing the value pointed to by 'pgb_field' requires holding mutex 'sls_mu' exclusively}}
}
};
class GBFoo {
public:
int gb_field __attribute__((guarded_by(sls_mu)));
void testFoo() {
gb_field = 0; // \
// expected-warning {{writing variable 'gb_field' requires holding mutex 'sls_mu' exclusively}}
}
void testNoAnal() NO_THREAD_SAFETY_ANALYSIS {
gb_field = 0;
}
};
GBFoo GlobalGBFoo __attribute__((guarded_by(sls_mu)));
void gb_fun_0() {
sls_mu.Lock();
int x = *pgb_var;
sls_mu.Unlock();
}
void gb_fun_1() {
sls_mu.Lock();
*pgb_var = 2;
sls_mu.Unlock();
}
void gb_fun_2() {
int x;
pgb_var = &x;
}
void gb_fun_3() {
int *x = pgb_var;
}
void gb_bad_0() {
sls_guard_var = 1; // \
// expected-warning{{writing variable 'sls_guard_var' requires holding any mutex exclusively}}
}
void gb_bad_1() {
int x = sls_guard_var; // \
// expected-warning{{reading variable 'sls_guard_var' requires holding any mutex}}
}
void gb_bad_2() {
sls_guardby_var = 1; // \
// expected-warning {{writing variable 'sls_guardby_var' requires holding mutex 'sls_mu' exclusively}}
}
void gb_bad_3() {
int x = sls_guardby_var; // \
// expected-warning {{reading variable 'sls_guardby_var' requires holding mutex 'sls_mu'}}
}
void gb_bad_4() {
*pgb_gvar = 1; // \
// expected-warning {{writing the value pointed to by 'pgb_gvar' requires holding any mutex exclusively}}
}
void gb_bad_5() {
int x = *pgb_gvar; // \
// expected-warning {{reading the value pointed to by 'pgb_gvar' requires holding any mutex}}
}
void gb_bad_6() {
*pgb_var = 1; // \
// expected-warning {{writing the value pointed to by 'pgb_var' requires holding mutex 'sls_mu' exclusively}}
}
void gb_bad_7() {
int x = *pgb_var; // \
// expected-warning {{reading the value pointed to by 'pgb_var' requires holding mutex 'sls_mu'}}
}
void gb_bad_8() {
GBFoo G;
G.gb_field = 0; // \
// expected-warning {{writing variable 'gb_field' requires holding mutex 'sls_mu'}}
}
void gb_bad_9() {
sls_guard_var++; // \
// expected-warning{{writing variable 'sls_guard_var' requires holding any mutex exclusively}}
sls_guard_var--; // \
// expected-warning{{writing variable 'sls_guard_var' requires holding any mutex exclusively}}
++sls_guard_var; // \
// expected-warning{{writing variable 'sls_guard_var' requires holding any mutex exclusively}}
--sls_guard_var;// \
// expected-warning{{writing variable 'sls_guard_var' requires holding any mutex exclusively}}
}
//-----------------------------------------------//
// Warnings on variables with late parsed attributes
// ----------------------------------------------//
class LateFoo {
public:
int a __attribute__((guarded_by(mu)));
int b;
void foo() EXCLUSIVE_LOCKS_REQUIRED(mu) { }
void test() {
a = 0; // \
// expected-warning{{writing variable 'a' requires holding mutex 'mu' exclusively}}
b = a; // \
// expected-warning {{reading variable 'a' requires holding mutex 'mu'}}
c = 0; // \
// expected-warning {{writing variable 'c' requires holding mutex 'mu' exclusively}}
}
int c __attribute__((guarded_by(mu)));
Mutex mu;
};
class LateBar {
public:
int a_ __attribute__((guarded_by(mu1_)));
int b_;
int *q __attribute__((pt_guarded_by(mu)));
Mutex mu1_;
Mutex mu;
LateFoo Foo;
LateFoo Foo2;
LateFoo *FooPointer;
};
LateBar b1, *b3;
void late_0() {
LateFoo FooA;
LateFoo FooB;
FooA.mu.Lock();
FooA.a = 5;
FooA.mu.Unlock();
}
void late_1() {
LateBar BarA;
BarA.FooPointer->mu.Lock();
BarA.FooPointer->a = 2;
BarA.FooPointer->mu.Unlock();
}
void late_bad_0() {
LateFoo fooA;
LateFoo fooB;
fooA.mu.Lock();
fooB.a = 5; // \
// expected-warning{{writing variable 'a' requires holding mutex 'fooB.mu' exclusively}} \
// expected-note{{found near match 'fooA.mu'}}
fooA.mu.Unlock();
}
void late_bad_1() {
Mutex mu;
mu.Lock();
b1.mu1_.Lock();
int res = b1.a_ + b3->b_;
b3->b_ = *b1.q; // \
// expected-warning{{reading the value pointed to by 'q' requires holding mutex 'b1.mu'}}
b1.mu1_.Unlock();
b1.b_ = res;
mu.Unlock();
}
void late_bad_2() {
LateBar BarA;
BarA.FooPointer->mu.Lock();
BarA.Foo.a = 2; // \
// expected-warning{{writing variable 'a' requires holding mutex 'BarA.Foo.mu' exclusively}} \
// expected-note{{found near match 'BarA.FooPointer->mu'}}
BarA.FooPointer->mu.Unlock();
}
void late_bad_3() {
LateBar BarA;
BarA.Foo.mu.Lock();
BarA.FooPointer->a = 2; // \
// expected-warning{{writing variable 'a' requires holding mutex 'BarA.FooPointer->mu' exclusively}} \
// expected-note{{found near match 'BarA.Foo.mu'}}
BarA.Foo.mu.Unlock();
}
void late_bad_4() {
LateBar BarA;
BarA.Foo.mu.Lock();
BarA.Foo2.a = 2; // \
// expected-warning{{writing variable 'a' requires holding mutex 'BarA.Foo2.mu' exclusively}} \
// expected-note{{found near match 'BarA.Foo.mu'}}
BarA.Foo.mu.Unlock();
}
//-----------------------------------------------//
// Extra warnings for shared vs. exclusive locks
// ----------------------------------------------//
void shared_fun_0() {
sls_mu.Lock();
do {
sls_mu.Unlock();
sls_mu.Lock();
} while (getBool());
sls_mu.Unlock();
}
void shared_fun_1() {
sls_mu.ReaderLock(); // \
// expected-note {{the other acquisition of mutex 'sls_mu' is here}}
do {
sls_mu.Unlock();
sls_mu.Lock(); // \
// expected-warning {{mutex 'sls_mu' is acquired exclusively and shared in the same scope}}
} while (getBool());
sls_mu.Unlock();
}
void shared_fun_3() {
if (getBool())
sls_mu.Lock();
else
sls_mu.Lock();
*pgb_var = 1;
sls_mu.Unlock();
}
void shared_fun_4() {
if (getBool())
sls_mu.ReaderLock();
else
sls_mu.ReaderLock();
int x = sls_guardby_var;
sls_mu.Unlock();
}
void shared_fun_8() {
if (getBool())
sls_mu.Lock(); // \
// expected-warning {{mutex 'sls_mu' is acquired exclusively and shared in the same scope}}
else
sls_mu.ReaderLock(); // \
// expected-note {{the other acquisition of mutex 'sls_mu' is here}}
sls_mu.Unlock();
}
void shared_fun_9() {
sls_mu.Lock();
sls_mu.ExclusiveUnlock();
sls_mu.ReaderLock();
sls_mu.ReaderUnlock();
}
void shared_fun_10() {
sls_mu.Lock();
sls_mu.DemoteExclusive();
sls_mu.ReaderUnlock();
}
void shared_fun_11() {
sls_mu.ReaderLock();
sls_mu.PromoteShared();
sls_mu.Unlock();
}
void shared_bad_0() {
sls_mu.Lock(); // \
// expected-note {{the other acquisition of mutex 'sls_mu' is here}}
do {
sls_mu.Unlock();
sls_mu.ReaderLock(); // \
// expected-warning {{mutex 'sls_mu' is acquired exclusively and shared in the same scope}}
} while (getBool());
sls_mu.Unlock();
}
void shared_bad_1() {
if (getBool())
sls_mu.Lock(); // \
// expected-warning {{mutex 'sls_mu' is acquired exclusively and shared in the same scope}}
else
sls_mu.ReaderLock(); // \
// expected-note {{the other acquisition of mutex 'sls_mu' is here}}
*pgb_var = 1;
sls_mu.Unlock();
}
void shared_bad_2() {
if (getBool())
sls_mu.ReaderLock(); // \
// expected-warning {{mutex 'sls_mu' is acquired exclusively and shared in the same scope}}
else
sls_mu.Lock(); // \
// expected-note {{the other acquisition of mutex 'sls_mu' is here}}
*pgb_var = 1;
sls_mu.Unlock();
}
void shared_bad_3() {
sls_mu.Lock(); // expected-note {{mutex acquired here}}
sls_mu.ReaderUnlock(); // \
// expected-warning {{releasing mutex 'sls_mu' using shared access, expected exclusive access}}
}
void shared_bad_4() {
sls_mu.ReaderLock(); // expected-note {{mutex acquired here}}
sls_mu.ExclusiveUnlock(); // \
// expected-warning {{releasing mutex 'sls_mu' using exclusive access, expected shared access}}
}
void shared_bad_5() {
sls_mu.Lock(); // expected-note {{mutex acquired here}}
sls_mu.PromoteShared(); // \
// expected-warning {{releasing mutex 'sls_mu' using shared access, expected exclusive access}}
sls_mu.ExclusiveUnlock();
}
void shared_bad_6() {
sls_mu.ReaderLock(); // expected-note {{mutex acquired here}}
sls_mu.DemoteExclusive(); // \
// expected-warning {{releasing mutex 'sls_mu' using exclusive access, expected shared access}}
sls_mu.ReaderUnlock();
}
// FIXME: Add support for functions (not only methods)
class LRBar {
public:
void aa_elr_fun() EXCLUSIVE_LOCKS_REQUIRED(aa_mu);
void aa_elr_fun_s() SHARED_LOCKS_REQUIRED(aa_mu);
void le_fun() __attribute__((locks_excluded(sls_mu)));
};
class LRFoo {
public:
void test() EXCLUSIVE_LOCKS_REQUIRED(sls_mu);
void testShared() SHARED_LOCKS_REQUIRED(sls_mu2);
};
void elr_fun() EXCLUSIVE_LOCKS_REQUIRED(sls_mu);
void elr_fun() {}
LRFoo MyLRFoo;
LRBar Bar;
void es_fun_0() {
aa_mu.Lock();
Bar.aa_elr_fun();
aa_mu.Unlock();
}
void es_fun_1() {
aa_mu.Lock();
Bar.aa_elr_fun_s();
aa_mu.Unlock();
}
void es_fun_2() {
aa_mu.ReaderLock();
Bar.aa_elr_fun_s();
aa_mu.Unlock();
}
void es_fun_3() {
sls_mu.Lock();
MyLRFoo.test();
sls_mu.Unlock();
}
void es_fun_4() {
sls_mu2.Lock();
MyLRFoo.testShared();
sls_mu2.Unlock();
}
void es_fun_5() {
sls_mu2.ReaderLock();
MyLRFoo.testShared();
sls_mu2.Unlock();
}
void es_fun_6() {
Bar.le_fun();
}
void es_fun_7() {
sls_mu.Lock();
elr_fun();
sls_mu.Unlock();
}
void es_fun_8() NO_THREAD_SAFETY_ANALYSIS;
void es_fun_8() {
Bar.aa_elr_fun_s();
}
void es_fun_9() SHARED_LOCKS_REQUIRED(aa_mu);
void es_fun_9() {
Bar.aa_elr_fun_s();
}
void es_fun_10() EXCLUSIVE_LOCKS_REQUIRED(aa_mu);
void es_fun_10() {
Bar.aa_elr_fun_s();
}
void es_bad_0() {
Bar.aa_elr_fun(); // \
// expected-warning {{calling function 'aa_elr_fun' requires holding mutex 'aa_mu' exclusively}}
}
void es_bad_1() {
aa_mu.ReaderLock();
Bar.aa_elr_fun(); // \
// expected-warning {{calling function 'aa_elr_fun' requires holding mutex 'aa_mu' exclusively}}
aa_mu.Unlock();
}
void es_bad_2() {
Bar.aa_elr_fun_s(); // \
// expected-warning {{calling function 'aa_elr_fun_s' requires holding mutex 'aa_mu'}}
}
void es_bad_3() {
MyLRFoo.test(); // \
// expected-warning {{calling function 'test' requires holding mutex 'sls_mu' exclusively}}
}
void es_bad_4() {
MyLRFoo.testShared(); // \
// expected-warning {{calling function 'testShared' requires holding mutex 'sls_mu2'}}
}
void es_bad_5() {
sls_mu.ReaderLock();
MyLRFoo.test(); // \
// expected-warning {{calling function 'test' requires holding mutex 'sls_mu' exclusively}}
sls_mu.Unlock();
}
void es_bad_6() {
sls_mu.Lock();
Bar.le_fun(); // \
// expected-warning {{cannot call function 'le_fun' while mutex 'sls_mu' is held}}
sls_mu.Unlock();
}
void es_bad_7() {
sls_mu.ReaderLock();
Bar.le_fun(); // \
// expected-warning {{cannot call function 'le_fun' while mutex 'sls_mu' is held}}
sls_mu.Unlock();
}
//-----------------------------------------------//
// Unparseable lock expressions
// ----------------------------------------------//
// FIXME -- derive new tests for unhandled expressions
//----------------------------------------------------------------------------//
// The following test cases are ported from the gcc thread safety implementation
// They are each wrapped inside a namespace with the test number of the gcc test
//
// FIXME: add all the gcc tests, once this analysis passes them.
//----------------------------------------------------------------------------//
//-----------------------------------------//
// Good testcases (no errors)
//-----------------------------------------//
namespace thread_annot_lock_20 {
class Bar {
public:
static int func1() EXCLUSIVE_LOCKS_REQUIRED(mu1_);
static int b_ GUARDED_BY(mu1_);
static Mutex mu1_;
static int a_ GUARDED_BY(mu1_);
};
Bar b1;
int Bar::func1()
{
int res = 5;
if (a_ == 4)
res = b_;
return res;
}
} // end namespace thread_annot_lock_20
namespace thread_annot_lock_22 {
// Test various usage of GUARDED_BY and PT_GUARDED_BY annotations, especially
// uses in class definitions.
Mutex mu;
class Bar {
public:
int a_ GUARDED_BY(mu1_);
int b_;
int *q PT_GUARDED_BY(mu);
Mutex mu1_ ACQUIRED_AFTER(mu);
};
Bar b1, *b3;
int *p GUARDED_BY(mu) PT_GUARDED_BY(mu);
int res GUARDED_BY(mu) = 5;
int func(int i)
{
int x;
mu.Lock();