blob: 6a75a6e480e491dacffababfffa83fc732993aa1 [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
Jordan Rose7fcaa142014-04-01 03:40:47 +00009typedef struct {
10 void *foo;
11} lck_grp_t;
12
Jordy Rosed9c52212011-07-19 20:21:41 +000013typedef pthread_mutex_t lck_mtx_t;
14
15extern int pthread_mutex_lock(pthread_mutex_t *);
16extern int pthread_mutex_unlock(pthread_mutex_t *);
17extern int pthread_mutex_trylock(pthread_mutex_t *);
Jordan Rose7fcaa142014-04-01 03:40:47 +000018extern int pthread_mutex_destroy(pthread_mutex_t *);
Jordy Rosed9c52212011-07-19 20:21:41 +000019extern int lck_mtx_lock(lck_mtx_t *);
20extern int lck_mtx_unlock(lck_mtx_t *);
21extern int lck_mtx_try_lock(lck_mtx_t *);
Jordan Rose7fcaa142014-04-01 03:40:47 +000022extern void lck_mtx_destroy(lck_mtx_t *lck, lck_grp_t *grp);
Jordy Rosed9c52212011-07-19 20:21:41 +000023
24pthread_mutex_t mtx1, mtx2;
25lck_mtx_t lck1, lck2;
Jordan Rose7fcaa142014-04-01 03:40:47 +000026lck_grp_t grp1;
Jordy Rosed9c52212011-07-19 20:21:41 +000027
28void
29ok1(void)
30{
31 pthread_mutex_lock(&mtx1); // no-warning
32}
33
34void
35ok2(void)
36{
37 pthread_mutex_unlock(&mtx1); // no-warning
38}
39
40void
41ok3(void)
42{
43 pthread_mutex_lock(&mtx1); // no-warning
44 pthread_mutex_unlock(&mtx1); // no-warning
45 pthread_mutex_lock(&mtx1); // no-warning
46 pthread_mutex_unlock(&mtx1); // no-warning
47}
48
49void
50ok4(void)
51{
52 pthread_mutex_lock(&mtx1); // no-warning
53 pthread_mutex_unlock(&mtx1); // no-warning
54 pthread_mutex_lock(&mtx2); // no-warning
55 pthread_mutex_unlock(&mtx2); // no-warning
56}
57
58void
59ok5(void)
60{
61 if (pthread_mutex_trylock(&mtx1) == 0) // no-warning
62 pthread_mutex_unlock(&mtx1); // no-warning
63}
64
65void
66ok6(void)
67{
68 lck_mtx_lock(&lck1); // no-warning
69}
70
71void
72ok7(void)
73{
74 if (lck_mtx_try_lock(&lck1) != 0) // no-warning
75 lck_mtx_unlock(&lck1); // no-warning
76}
77
78void
Jordan Rose0696bb42014-04-01 03:40:38 +000079ok8(void)
80{
81 pthread_mutex_lock(&mtx1); // no-warning
82 pthread_mutex_lock(&mtx2); // no-warning
83 pthread_mutex_unlock(&mtx2); // no-warning
84 pthread_mutex_unlock(&mtx1); // no-warning
85}
86
87void
88ok9(void)
89{
90 pthread_mutex_unlock(&mtx1); // no-warning
91 if (pthread_mutex_trylock(&mtx1) == 0) // no-warning
92 pthread_mutex_unlock(&mtx1); // no-warning
93}
94
95void
96ok10(void)
97{
98 if (pthread_mutex_trylock(&mtx1) != 0) // no-warning
99 pthread_mutex_lock(&mtx1); // no-warning
100 pthread_mutex_unlock(&mtx1); // no-warning
101}
102
103void
Jordan Rose7fcaa142014-04-01 03:40:47 +0000104ok11(void)
105{
106 pthread_mutex_destroy(&mtx1); // no-warning
107}
108
109void
110ok12(void)
111{
112 pthread_mutex_destroy(&mtx1); // no-warning
113 pthread_mutex_destroy(&mtx2); // no-warning
114}
115
116void
117ok13(void)
118{
119 pthread_mutex_unlock(&mtx1); // no-warning
120 pthread_mutex_destroy(&mtx1); // no-warning
121}
122
123void
124ok14(void)
125{
126 pthread_mutex_unlock(&mtx1); // no-warning
127 pthread_mutex_destroy(&mtx1); // no-warning
128 pthread_mutex_unlock(&mtx2); // no-warning
129 pthread_mutex_destroy(&mtx2); // no-warning
130}
131
132void
133ok15(void)
134{
135 pthread_mutex_lock(&mtx1); // no-warning
136 pthread_mutex_unlock(&mtx1); // no-warning
137 pthread_mutex_destroy(&mtx1); // no-warning
138}
139
140void
Jordy Rosed9c52212011-07-19 20:21:41 +0000141bad1(void)
142{
143 pthread_mutex_lock(&mtx1); // no-warning
144 pthread_mutex_lock(&mtx1); // expected-warning{{This lock has already been acquired}}
145}
146
147void
148bad2(void)
149{
150 pthread_mutex_lock(&mtx1); // no-warning
151 pthread_mutex_unlock(&mtx1); // no-warning
152 pthread_mutex_lock(&mtx1); // no-warning
153 pthread_mutex_lock(&mtx1); // expected-warning{{This lock has already been acquired}}
154}
155
156void
157bad3(void)
158{
159 pthread_mutex_lock(&mtx1); // no-warning
160 pthread_mutex_lock(&mtx2); // no-warning
161 pthread_mutex_unlock(&mtx1); // expected-warning{{This was not the most recently acquired lock}}
162 pthread_mutex_unlock(&mtx2);
163}
164
165void
166bad4(void)
167{
168 if (pthread_mutex_trylock(&mtx1)) // no-warning
169 return;
170 pthread_mutex_lock(&mtx2); // no-warning
171 pthread_mutex_unlock(&mtx1); // expected-warning{{This was not the most recently acquired lock}}
172}
173
174void
175bad5(void)
176{
177 lck_mtx_lock(&lck1); // no-warning
178 lck_mtx_lock(&lck1); // expected-warning{{This lock has already been acquired}}
179}
180
181void
182bad6(void)
183{
184 lck_mtx_lock(&lck1); // no-warning
185 lck_mtx_unlock(&lck1); // no-warning
186 lck_mtx_lock(&lck1); // no-warning
187 lck_mtx_lock(&lck1); // expected-warning{{This lock has already been acquired}}
188}
189
190void
191bad7(void)
192{
193 lck_mtx_lock(&lck1); // no-warning
194 lck_mtx_lock(&lck2); // no-warning
195 lck_mtx_unlock(&lck1); // expected-warning{{This was not the most recently acquired lock}}
196 lck_mtx_unlock(&lck2);
197}
198
199void
200bad8(void)
201{
202 if (lck_mtx_try_lock(&lck1) == 0) // no-warning
203 return;
204 lck_mtx_lock(&lck2); // no-warning
205 lck_mtx_unlock(&lck1); // expected-warning{{This was not the most recently acquired lock}}
206}
Jordan Rose0696bb42014-04-01 03:40:38 +0000207
208void
209bad9(void)
210{
211 lck_mtx_unlock(&lck1); // no-warning
212 lck_mtx_unlock(&lck1); // expected-warning{{This lock has already been unlocked}}
213}
214
215void
216bad10(void)
217{
218 lck_mtx_lock(&lck1); // no-warning
219 lck_mtx_unlock(&lck1); // no-warning
220 lck_mtx_unlock(&lck1); // expected-warning{{This lock has already been unlocked}}
221}
222
223static void
224bad11_sub(pthread_mutex_t *lock)
225{
226 lck_mtx_unlock(lock); // expected-warning{{This lock has already been unlocked}}
227}
228
229void
230bad11(int i)
231{
232 lck_mtx_lock(&lck1); // no-warning
233 lck_mtx_unlock(&lck1); // no-warning
234 if (i < 5)
235 bad11_sub(&lck1);
236}
237
238void
239bad12(void)
240{
241 pthread_mutex_lock(&mtx1); // no-warning
242 pthread_mutex_unlock(&mtx1); // no-warning
243 pthread_mutex_lock(&mtx1); // no-warning
244 pthread_mutex_unlock(&mtx1); // no-warning
245 pthread_mutex_unlock(&mtx1); // expected-warning{{This lock has already been unlocked}}
246}
247
248void
249bad13(void)
250{
251 pthread_mutex_lock(&mtx1); // no-warning
252 pthread_mutex_unlock(&mtx1); // no-warning
253 pthread_mutex_lock(&mtx2); // no-warning
254 pthread_mutex_unlock(&mtx2); // no-warning
255 pthread_mutex_unlock(&mtx1); // expected-warning{{This lock has already been unlocked}}
256}
257
258void
259bad14(void)
260{
261 pthread_mutex_lock(&mtx1); // no-warning
262 pthread_mutex_lock(&mtx2); // no-warning
263 pthread_mutex_unlock(&mtx2); // no-warning
264 pthread_mutex_unlock(&mtx1); // no-warning
265 pthread_mutex_unlock(&mtx2); // expected-warning{{This lock has already been unlocked}}
266}
267
268void
269bad15(void)
270{
271 pthread_mutex_lock(&mtx1); // no-warning
272 pthread_mutex_lock(&mtx2); // no-warning
273 pthread_mutex_unlock(&mtx2); // no-warning
274 pthread_mutex_unlock(&mtx1); // no-warning
275 pthread_mutex_lock(&mtx1); // no-warning
276 pthread_mutex_unlock(&mtx2); // expected-warning{{This lock has already been unlocked}}
277}
Jordan Rose7fcaa142014-04-01 03:40:47 +0000278
279void
280bad16(void)
281{
282 pthread_mutex_destroy(&mtx1); // no-warning
283 pthread_mutex_lock(&mtx1); // expected-warning{{This lock has already been destroyed}}
284}
285
286void
287bad17(void)
288{
289 pthread_mutex_destroy(&mtx1); // no-warning
290 pthread_mutex_unlock(&mtx1); // expected-warning{{This lock has already been destroyed}}
291}
292
293void
294bad18(void)
295{
296 pthread_mutex_destroy(&mtx1); // no-warning
297 pthread_mutex_destroy(&mtx1); // expected-warning{{This lock has already been destroyed}}
298}
299
300void
301bad19(void)
302{
303 pthread_mutex_lock(&mtx1); // no-warning
304 pthread_mutex_destroy(&mtx1); // expected-warning{{This lock is still locked}}
305}
306
307void
308bad20(void)
309{
310 lck_mtx_destroy(&mtx1, &grp1); // no-warning
311 lck_mtx_lock(&mtx1); // expected-warning{{This lock has already been destroyed}}
312}
313
314void
315bad21(void)
316{
317 lck_mtx_destroy(&mtx1, &grp1); // no-warning
318 lck_mtx_unlock(&mtx1); // expected-warning{{This lock has already been destroyed}}
319}
320
321void
322bad22(void)
323{
324 lck_mtx_destroy(&mtx1, &grp1); // no-warning
325 lck_mtx_destroy(&mtx1, &grp1); // expected-warning{{This lock has already been destroyed}}
326}
327
328void
329bad23(void)
330{
331 lck_mtx_lock(&mtx1); // no-warning
332 lck_mtx_destroy(&mtx1, &grp1); // expected-warning{{This lock is still locked}}
333}