blob: bc7ef66353f7fdff2f713bc3f69fed9714eb2281 [file] [log] [blame]
Ted Kremenek722398f2012-08-24 20:39:55 +00001// RUN: %clang_cc1 -analyze -analyzer-checker=alpha.unix.PthreadLock -verify %s
Jordy Rosed9c52212011-07-19 20:21:41 +00002
3// Tests performing normal locking patterns and wrong locking orders
4
5typedef struct {
6 void *foo;
7} pthread_mutex_t;
8
9typedef pthread_mutex_t lck_mtx_t;
10
11extern int pthread_mutex_lock(pthread_mutex_t *);
12extern int pthread_mutex_unlock(pthread_mutex_t *);
13extern int pthread_mutex_trylock(pthread_mutex_t *);
14extern int lck_mtx_lock(lck_mtx_t *);
15extern int lck_mtx_unlock(lck_mtx_t *);
16extern int lck_mtx_try_lock(lck_mtx_t *);
17
18pthread_mutex_t mtx1, mtx2;
19lck_mtx_t lck1, lck2;
20
21void
22ok1(void)
23{
24 pthread_mutex_lock(&mtx1); // no-warning
25}
26
27void
28ok2(void)
29{
30 pthread_mutex_unlock(&mtx1); // no-warning
31}
32
33void
34ok3(void)
35{
36 pthread_mutex_lock(&mtx1); // no-warning
37 pthread_mutex_unlock(&mtx1); // no-warning
38 pthread_mutex_lock(&mtx1); // no-warning
39 pthread_mutex_unlock(&mtx1); // no-warning
40}
41
42void
43ok4(void)
44{
45 pthread_mutex_lock(&mtx1); // no-warning
46 pthread_mutex_unlock(&mtx1); // no-warning
47 pthread_mutex_lock(&mtx2); // no-warning
48 pthread_mutex_unlock(&mtx2); // no-warning
49}
50
51void
52ok5(void)
53{
54 if (pthread_mutex_trylock(&mtx1) == 0) // no-warning
55 pthread_mutex_unlock(&mtx1); // no-warning
56}
57
58void
59ok6(void)
60{
61 lck_mtx_lock(&lck1); // no-warning
62}
63
64void
65ok7(void)
66{
67 if (lck_mtx_try_lock(&lck1) != 0) // no-warning
68 lck_mtx_unlock(&lck1); // no-warning
69}
70
71void
Jordan Rose0696bb42014-04-01 03:40:38 +000072ok8(void)
73{
74 pthread_mutex_lock(&mtx1); // no-warning
75 pthread_mutex_lock(&mtx2); // no-warning
76 pthread_mutex_unlock(&mtx2); // no-warning
77 pthread_mutex_unlock(&mtx1); // no-warning
78}
79
80void
81ok9(void)
82{
83 pthread_mutex_unlock(&mtx1); // no-warning
84 if (pthread_mutex_trylock(&mtx1) == 0) // no-warning
85 pthread_mutex_unlock(&mtx1); // no-warning
86}
87
88void
89ok10(void)
90{
91 if (pthread_mutex_trylock(&mtx1) != 0) // no-warning
92 pthread_mutex_lock(&mtx1); // no-warning
93 pthread_mutex_unlock(&mtx1); // no-warning
94}
95
96void
Jordy Rosed9c52212011-07-19 20:21:41 +000097bad1(void)
98{
99 pthread_mutex_lock(&mtx1); // no-warning
100 pthread_mutex_lock(&mtx1); // expected-warning{{This lock has already been acquired}}
101}
102
103void
104bad2(void)
105{
106 pthread_mutex_lock(&mtx1); // no-warning
107 pthread_mutex_unlock(&mtx1); // no-warning
108 pthread_mutex_lock(&mtx1); // no-warning
109 pthread_mutex_lock(&mtx1); // expected-warning{{This lock has already been acquired}}
110}
111
112void
113bad3(void)
114{
115 pthread_mutex_lock(&mtx1); // no-warning
116 pthread_mutex_lock(&mtx2); // no-warning
117 pthread_mutex_unlock(&mtx1); // expected-warning{{This was not the most recently acquired lock}}
118 pthread_mutex_unlock(&mtx2);
119}
120
121void
122bad4(void)
123{
124 if (pthread_mutex_trylock(&mtx1)) // no-warning
125 return;
126 pthread_mutex_lock(&mtx2); // no-warning
127 pthread_mutex_unlock(&mtx1); // expected-warning{{This was not the most recently acquired lock}}
128}
129
130void
131bad5(void)
132{
133 lck_mtx_lock(&lck1); // no-warning
134 lck_mtx_lock(&lck1); // expected-warning{{This lock has already been acquired}}
135}
136
137void
138bad6(void)
139{
140 lck_mtx_lock(&lck1); // no-warning
141 lck_mtx_unlock(&lck1); // no-warning
142 lck_mtx_lock(&lck1); // no-warning
143 lck_mtx_lock(&lck1); // expected-warning{{This lock has already been acquired}}
144}
145
146void
147bad7(void)
148{
149 lck_mtx_lock(&lck1); // no-warning
150 lck_mtx_lock(&lck2); // no-warning
151 lck_mtx_unlock(&lck1); // expected-warning{{This was not the most recently acquired lock}}
152 lck_mtx_unlock(&lck2);
153}
154
155void
156bad8(void)
157{
158 if (lck_mtx_try_lock(&lck1) == 0) // no-warning
159 return;
160 lck_mtx_lock(&lck2); // no-warning
161 lck_mtx_unlock(&lck1); // expected-warning{{This was not the most recently acquired lock}}
162}
Jordan Rose0696bb42014-04-01 03:40:38 +0000163
164void
165bad9(void)
166{
167 lck_mtx_unlock(&lck1); // no-warning
168 lck_mtx_unlock(&lck1); // expected-warning{{This lock has already been unlocked}}
169}
170
171void
172bad10(void)
173{
174 lck_mtx_lock(&lck1); // no-warning
175 lck_mtx_unlock(&lck1); // no-warning
176 lck_mtx_unlock(&lck1); // expected-warning{{This lock has already been unlocked}}
177}
178
179static void
180bad11_sub(pthread_mutex_t *lock)
181{
182 lck_mtx_unlock(lock); // expected-warning{{This lock has already been unlocked}}
183}
184
185void
186bad11(int i)
187{
188 lck_mtx_lock(&lck1); // no-warning
189 lck_mtx_unlock(&lck1); // no-warning
190 if (i < 5)
191 bad11_sub(&lck1);
192}
193
194void
195bad12(void)
196{
197 pthread_mutex_lock(&mtx1); // no-warning
198 pthread_mutex_unlock(&mtx1); // no-warning
199 pthread_mutex_lock(&mtx1); // no-warning
200 pthread_mutex_unlock(&mtx1); // no-warning
201 pthread_mutex_unlock(&mtx1); // expected-warning{{This lock has already been unlocked}}
202}
203
204void
205bad13(void)
206{
207 pthread_mutex_lock(&mtx1); // no-warning
208 pthread_mutex_unlock(&mtx1); // no-warning
209 pthread_mutex_lock(&mtx2); // no-warning
210 pthread_mutex_unlock(&mtx2); // no-warning
211 pthread_mutex_unlock(&mtx1); // expected-warning{{This lock has already been unlocked}}
212}
213
214void
215bad14(void)
216{
217 pthread_mutex_lock(&mtx1); // no-warning
218 pthread_mutex_lock(&mtx2); // no-warning
219 pthread_mutex_unlock(&mtx2); // no-warning
220 pthread_mutex_unlock(&mtx1); // no-warning
221 pthread_mutex_unlock(&mtx2); // expected-warning{{This lock has already been unlocked}}
222}
223
224void
225bad15(void)
226{
227 pthread_mutex_lock(&mtx1); // no-warning
228 pthread_mutex_lock(&mtx2); // no-warning
229 pthread_mutex_unlock(&mtx2); // no-warning
230 pthread_mutex_unlock(&mtx1); // no-warning
231 pthread_mutex_lock(&mtx1); // no-warning
232 pthread_mutex_unlock(&mtx2); // expected-warning{{This lock has already been unlocked}}
233}