blob: 55e6e707f013092ca4ac46fa30055e6980dad1b9 [file] [log] [blame]
Stephen Hines651f13c2014-04-23 16:59:28 -07001// RUN: %clang_cc1 -fsyntax-only -verify -Wthread-safety -Wthread-safety-beta %s
David Blaikie88c4b5e2013-07-29 18:24:03 +00002
David Blaikie88c4b5e2013-07-29 18:24:03 +00003#define LOCKABLE __attribute__ ((lockable))
4#define SCOPED_LOCKABLE __attribute__ ((scoped_lockable))
5#define GUARDED_BY(x) __attribute__ ((guarded_by(x)))
6#define GUARDED_VAR __attribute__ ((guarded_var))
7#define PT_GUARDED_BY(x) __attribute__ ((pt_guarded_by(x)))
8#define PT_GUARDED_VAR __attribute__ ((pt_guarded_var))
9#define ACQUIRED_AFTER(...) __attribute__ ((acquired_after(__VA_ARGS__)))
10#define ACQUIRED_BEFORE(...) __attribute__ ((acquired_before(__VA_ARGS__)))
11#define EXCLUSIVE_LOCK_FUNCTION(...) __attribute__ ((exclusive_lock_function(__VA_ARGS__)))
12#define SHARED_LOCK_FUNCTION(...) __attribute__ ((shared_lock_function(__VA_ARGS__)))
13#define ASSERT_EXCLUSIVE_LOCK(...) __attribute__ ((assert_exclusive_lock(__VA_ARGS__)))
14#define ASSERT_SHARED_LOCK(...) __attribute__ ((assert_shared_lock(__VA_ARGS__)))
15#define EXCLUSIVE_TRYLOCK_FUNCTION(...) __attribute__ ((exclusive_trylock_function(__VA_ARGS__)))
16#define SHARED_TRYLOCK_FUNCTION(...) __attribute__ ((shared_trylock_function(__VA_ARGS__)))
17#define UNLOCK_FUNCTION(...) __attribute__ ((unlock_function(__VA_ARGS__)))
18#define LOCK_RETURNED(x) __attribute__ ((lock_returned(x)))
19#define LOCKS_EXCLUDED(...) __attribute__ ((locks_excluded(__VA_ARGS__)))
20#define EXCLUSIVE_LOCKS_REQUIRED(...) \
21 __attribute__ ((exclusive_locks_required(__VA_ARGS__)))
22#define SHARED_LOCKS_REQUIRED(...) \
23 __attribute__ ((shared_locks_required(__VA_ARGS__)))
24#define NO_THREAD_SAFETY_ANALYSIS __attribute__ ((no_thread_safety_analysis))
25
26// Define the mutex struct.
27// Simplified only for test purpose.
28struct LOCKABLE Mutex {};
29
30struct Foo {
31 struct Mutex *mu_;
32};
33
Stephen Hines651f13c2014-04-23 16:59:28 -070034// Declare mutex lock/unlock functions.
35void mutex_exclusive_lock(struct Mutex *mu) EXCLUSIVE_LOCK_FUNCTION(mu);
36void mutex_shared_lock(struct Mutex *mu) SHARED_LOCK_FUNCTION(mu);
37void mutex_unlock(struct Mutex *mu) UNLOCK_FUNCTION(mu);
38void mutex_shared_unlock(struct Mutex *mu) __attribute__((release_shared_capability(mu)));
39void mutex_exclusive_unlock(struct Mutex *mu) __attribute__((release_capability(mu)));
David Blaikie88c4b5e2013-07-29 18:24:03 +000040
41// Define global variables.
42struct Mutex mu1;
43struct Mutex mu2 ACQUIRED_AFTER(mu1);
44struct Foo foo_ = {&mu1};
45int a_ GUARDED_BY(foo_.mu_);
46int *b_ PT_GUARDED_BY(foo_.mu_) = &a_;
47int c_ GUARDED_VAR;
48int *d_ PT_GUARDED_VAR = &c_;
49
50// Define test functions.
51int Foo_fun1(int i) SHARED_LOCKS_REQUIRED(mu2) EXCLUSIVE_LOCKS_REQUIRED(mu1) {
52 return i;
53}
54
55int Foo_fun2(int i) EXCLUSIVE_LOCKS_REQUIRED(mu2) SHARED_LOCKS_REQUIRED(mu1) {
56 return i;
57}
58
59int Foo_func3(int i) LOCKS_EXCLUDED(mu1, mu2) {
60 return i;
61}
62
63static int Bar_fun1(int i) EXCLUSIVE_LOCKS_REQUIRED(mu1) {
64 return i;
65}
66
67void set_value(int *a, int value) EXCLUSIVE_LOCKS_REQUIRED(foo_.mu_) {
68 *a = value;
69}
70
71int get_value(int *p) SHARED_LOCKS_REQUIRED(foo_.mu_){
72 return *p;
73}
74
75int main() {
76
Stephen Hines651f13c2014-04-23 16:59:28 -070077 Foo_fun1(1); // expected-warning{{calling function 'Foo_fun1' requires holding mutex 'mu2'}} \
78 expected-warning{{calling function 'Foo_fun1' requires holding mutex 'mu1' exclusively}}
David Blaikie88c4b5e2013-07-29 18:24:03 +000079
80 mutex_exclusive_lock(&mu1);
81 mutex_shared_lock(&mu2);
82 Foo_fun1(1);
83
Stephen Hines651f13c2014-04-23 16:59:28 -070084 mutex_shared_lock(&mu1); // expected-warning{{acquiring mutex 'mu1' that is already held}}
David Blaikie88c4b5e2013-07-29 18:24:03 +000085 mutex_unlock(&mu1);
86 mutex_unlock(&mu2);
87 mutex_shared_lock(&mu1);
88 mutex_exclusive_lock(&mu2);
89 Foo_fun2(2);
90
91 mutex_unlock(&mu2);
92 mutex_unlock(&mu1);
93 mutex_exclusive_lock(&mu1);
94 Bar_fun1(3);
95 mutex_unlock(&mu1);
96
97 mutex_exclusive_lock(&mu1);
Stephen Hines651f13c2014-04-23 16:59:28 -070098 Foo_func3(4); // expected-warning{{cannot call function 'Foo_func3' while mutex 'mu1' is held}}
David Blaikie88c4b5e2013-07-29 18:24:03 +000099 mutex_unlock(&mu1);
100
101 Foo_func3(5);
102
Stephen Hines651f13c2014-04-23 16:59:28 -0700103 set_value(&a_, 0); // expected-warning{{calling function 'set_value' requires holding mutex 'foo_.mu_' exclusively}}
104 get_value(b_); // expected-warning{{calling function 'get_value' requires holding mutex 'foo_.mu_'}}
David Blaikie88c4b5e2013-07-29 18:24:03 +0000105 mutex_exclusive_lock(foo_.mu_);
106 set_value(&a_, 1);
107 mutex_unlock(foo_.mu_);
108 mutex_shared_lock(foo_.mu_);
David Blaikie13a29a22013-07-29 22:37:42 +0000109 (void)(get_value(b_) == 1);
David Blaikie88c4b5e2013-07-29 18:24:03 +0000110 mutex_unlock(foo_.mu_);
111
Stephen Hines651f13c2014-04-23 16:59:28 -0700112 c_ = 0; // expected-warning{{writing variable 'c_' requires holding any mutex exclusively}}
113 (void)(*d_ == 0); // expected-warning{{reading the value pointed to by 'd_' requires holding any mutex}}
David Blaikie88c4b5e2013-07-29 18:24:03 +0000114 mutex_exclusive_lock(foo_.mu_);
115 c_ = 1;
David Blaikie13a29a22013-07-29 22:37:42 +0000116 (void)(*d_ == 1);
David Blaikie88c4b5e2013-07-29 18:24:03 +0000117 mutex_unlock(foo_.mu_);
118
Stephen Hines651f13c2014-04-23 16:59:28 -0700119 mutex_exclusive_lock(&mu1);
Stephen Hines176edba2014-12-01 14:53:08 -0800120 mutex_shared_unlock(&mu1); // expected-warning {{releasing mutex 'mu1' using shared access, expected exclusive access}}
121 mutex_exclusive_unlock(&mu1); // expected-warning {{releasing mutex 'mu1' that was not held}}
Stephen Hines651f13c2014-04-23 16:59:28 -0700122
123 mutex_shared_lock(&mu1);
124 mutex_exclusive_unlock(&mu1); // expected-warning {{releasing mutex 'mu1' using exclusive access, expected shared access}}
Stephen Hines176edba2014-12-01 14:53:08 -0800125 mutex_shared_unlock(&mu1); // expected-warning {{releasing mutex 'mu1' that was not held}}
Stephen Hines651f13c2014-04-23 16:59:28 -0700126
David Blaikie88c4b5e2013-07-29 18:24:03 +0000127 return 0;
128}