1 // RUN: %clang_analyze_cc1 -analyzer-checker=alpha.unix.PthreadLock -verify %s
2
3 // Tests performing normal locking patterns and wrong locking orders
4
5 #include "Inputs/system-header-simulator-for-pthread-lock.h"
6
7 pthread_mutex_t mtx1, mtx2;
8 pthread_mutex_t *pmtx;
9 lck_mtx_t lck1, lck2;
10 lck_grp_t grp1;
11 lck_rw_t rw;
12
13 #define NULL 0
14
15 void
ok1(void)16 ok1(void)
17 {
18 pthread_mutex_lock(&mtx1); // no-warning
19 }
20
21 void
ok2(void)22 ok2(void)
23 {
24 pthread_mutex_unlock(&mtx1); // no-warning
25 }
26
27 void
ok3(void)28 ok3(void)
29 {
30 pthread_mutex_lock(&mtx1); // no-warning
31 pthread_mutex_unlock(&mtx1); // no-warning
32 pthread_mutex_lock(&mtx1); // no-warning
33 pthread_mutex_unlock(&mtx1); // no-warning
34 }
35
36 void
ok4(void)37 ok4(void)
38 {
39 pthread_mutex_lock(&mtx1); // no-warning
40 pthread_mutex_unlock(&mtx1); // no-warning
41 pthread_mutex_lock(&mtx2); // no-warning
42 pthread_mutex_unlock(&mtx2); // no-warning
43 }
44
45 void
ok5(void)46 ok5(void)
47 {
48 if (pthread_mutex_trylock(&mtx1) == 0) // no-warning
49 pthread_mutex_unlock(&mtx1); // no-warning
50 }
51
52 void
ok6(void)53 ok6(void)
54 {
55 lck_mtx_lock(&lck1); // no-warning
56 }
57
58 void
ok7(void)59 ok7(void)
60 {
61 if (lck_mtx_try_lock(&lck1) != 0) // no-warning
62 lck_mtx_unlock(&lck1); // no-warning
63 }
64
65 void
ok8(void)66 ok8(void)
67 {
68 pthread_mutex_lock(&mtx1); // no-warning
69 pthread_mutex_lock(&mtx2); // no-warning
70 pthread_mutex_unlock(&mtx2); // no-warning
71 pthread_mutex_unlock(&mtx1); // no-warning
72 }
73
74 void
ok9(void)75 ok9(void)
76 {
77 pthread_mutex_unlock(&mtx1); // no-warning
78 if (pthread_mutex_trylock(&mtx1) == 0) // no-warning
79 pthread_mutex_unlock(&mtx1); // no-warning
80 }
81
82 void
ok10(void)83 ok10(void)
84 {
85 if (pthread_mutex_trylock(&mtx1) != 0) // no-warning
86 pthread_mutex_lock(&mtx1); // no-warning
87 pthread_mutex_unlock(&mtx1); // no-warning
88 }
89
90 void
ok11(void)91 ok11(void)
92 {
93 pthread_mutex_destroy(&mtx1); // no-warning
94 }
95
96 void
ok12(void)97 ok12(void)
98 {
99 pthread_mutex_destroy(&mtx1); // no-warning
100 pthread_mutex_destroy(&mtx2); // no-warning
101 }
102
103 void
ok13(void)104 ok13(void)
105 {
106 pthread_mutex_unlock(&mtx1); // no-warning
107 pthread_mutex_destroy(&mtx1); // no-warning
108 }
109
110 void
ok14(void)111 ok14(void)
112 {
113 pthread_mutex_unlock(&mtx1); // no-warning
114 pthread_mutex_destroy(&mtx1); // no-warning
115 pthread_mutex_unlock(&mtx2); // no-warning
116 pthread_mutex_destroy(&mtx2); // no-warning
117 }
118
119 void
ok15(void)120 ok15(void)
121 {
122 pthread_mutex_lock(&mtx1); // no-warning
123 pthread_mutex_unlock(&mtx1); // no-warning
124 pthread_mutex_destroy(&mtx1); // no-warning
125 }
126
127 void
ok16(void)128 ok16(void)
129 {
130 pthread_mutex_init(&mtx1, NULL); // no-warning
131 }
132
133 void
ok17(void)134 ok17(void)
135 {
136 pthread_mutex_init(&mtx1, NULL); // no-warning
137 pthread_mutex_init(&mtx2, NULL); // no-warning
138 }
139
140 void
ok18(void)141 ok18(void)
142 {
143 pthread_mutex_destroy(&mtx1); // no-warning
144 pthread_mutex_init(&mtx1, NULL); // no-warning
145 }
146
147 void
ok19(void)148 ok19(void)
149 {
150 pthread_mutex_destroy(&mtx1); // no-warning
151 pthread_mutex_init(&mtx1, NULL); // no-warning
152 pthread_mutex_destroy(&mtx2); // no-warning
153 pthread_mutex_init(&mtx2, NULL); // no-warning
154 }
155
156 void
ok20(void)157 ok20(void)
158 {
159 pthread_mutex_unlock(&mtx1); // no-warning
160 pthread_mutex_destroy(&mtx1); // no-warning
161 pthread_mutex_init(&mtx1, NULL); // no-warning
162 pthread_mutex_destroy(&mtx1); // no-warning
163 pthread_mutex_init(&mtx1, NULL); // no-warning
164 }
165
166 void
ok21(void)167 ok21(void) {
168 pthread_mutex_lock(pmtx); // no-warning
169 pthread_mutex_unlock(pmtx); // no-warning
170 }
171
172 void
ok22(void)173 ok22(void) {
174 pthread_mutex_lock(pmtx); // no-warning
175 pthread_mutex_unlock(pmtx); // no-warning
176 pthread_mutex_lock(pmtx); // no-warning
177 pthread_mutex_unlock(pmtx); // no-warning
178 }
179
ok23(void)180 void ok23(void) {
181 if (pthread_mutex_destroy(&mtx1) != 0) // no-warning
182 pthread_mutex_destroy(&mtx1); // no-warning
183 }
184
ok24(void)185 void ok24(void) {
186 if (pthread_mutex_destroy(&mtx1) != 0) // no-warning
187 pthread_mutex_lock(&mtx1); // no-warning
188 }
189
ok25(void)190 void ok25(void) {
191 if (pthread_mutex_destroy(&mtx1) != 0) // no-warning
192 pthread_mutex_unlock(&mtx1); // no-warning
193 }
194
ok26(void)195 void ok26(void) {
196 pthread_mutex_unlock(&mtx1); // no-warning
197 if (pthread_mutex_destroy(&mtx1) != 0) // no-warning
198 pthread_mutex_lock(&mtx1); // no-warning
199 }
200
ok27(void)201 void ok27(void) {
202 pthread_mutex_unlock(&mtx1); // no-warning
203 if (pthread_mutex_destroy(&mtx1) != 0) // no-warning
204 pthread_mutex_lock(&mtx1); // no-warning
205 else
206 pthread_mutex_init(&mtx1, NULL); // no-warning
207 }
208
ok28(void)209 void ok28(void) {
210 if (pthread_mutex_destroy(&mtx1) != 0) { // no-warning
211 pthread_mutex_lock(&mtx1); // no-warning
212 pthread_mutex_unlock(&mtx1); // no-warning
213 pthread_mutex_destroy(&mtx1); // no-warning
214 }
215 }
216
ok29(void)217 void ok29(void) {
218 lck_rw_lock_shared(&rw);
219 lck_rw_unlock_shared(&rw);
220 lck_rw_lock_exclusive(&rw); // no-warning
221 lck_rw_unlock_exclusive(&rw); // no-warning
222 }
223
224 void escape_mutex(pthread_mutex_t *m);
ok30(void)225 void ok30(void) {
226 pthread_mutex_t local_mtx;
227 pthread_mutex_init(&local_mtx, NULL);
228 pthread_mutex_lock(&local_mtx);
229 escape_mutex(&local_mtx);
230 pthread_mutex_lock(&local_mtx); // no-warning
231 pthread_mutex_unlock(&local_mtx);
232 pthread_mutex_destroy(&local_mtx);
233 }
234
ok31(void)235 void ok31(void) {
236 pthread_mutex_t local_mtx;
237 pthread_mutex_init(&local_mtx, NULL);
238 pthread_mutex_lock(&local_mtx);
239 fake_system_function_that_takes_a_mutex(&local_mtx);
240 pthread_mutex_lock(&local_mtx); // no-warning
241 pthread_mutex_unlock(&local_mtx);
242 pthread_mutex_destroy(&local_mtx);
243 }
244
245 void
bad1(void)246 bad1(void)
247 {
248 pthread_mutex_lock(&mtx1); // no-warning
249 pthread_mutex_lock(&mtx1); // expected-warning{{This lock has already been acquired}}
250 }
251
252 void
bad2(void)253 bad2(void)
254 {
255 pthread_mutex_lock(&mtx1); // no-warning
256 pthread_mutex_unlock(&mtx1); // no-warning
257 pthread_mutex_lock(&mtx1); // no-warning
258 pthread_mutex_lock(&mtx1); // expected-warning{{This lock has already been acquired}}
259 }
260
261 void
bad3(void)262 bad3(void)
263 {
264 pthread_mutex_lock(&mtx1); // no-warning
265 pthread_mutex_lock(&mtx2); // no-warning
266 pthread_mutex_unlock(&mtx1); // expected-warning{{This was not the most recently acquired lock}}
267 pthread_mutex_unlock(&mtx2);
268 }
269
270 void
bad4(void)271 bad4(void)
272 {
273 if (pthread_mutex_trylock(&mtx1)) // no-warning
274 return;
275 pthread_mutex_lock(&mtx2); // no-warning
276 pthread_mutex_unlock(&mtx1); // expected-warning{{This was not the most recently acquired lock}}
277 }
278
279 void
bad5(void)280 bad5(void)
281 {
282 lck_mtx_lock(&lck1); // no-warning
283 lck_mtx_lock(&lck1); // expected-warning{{This lock has already been acquired}}
284 }
285
286 void
bad6(void)287 bad6(void)
288 {
289 lck_mtx_lock(&lck1); // no-warning
290 lck_mtx_unlock(&lck1); // no-warning
291 lck_mtx_lock(&lck1); // no-warning
292 lck_mtx_lock(&lck1); // expected-warning{{This lock has already been acquired}}
293 }
294
295 void
bad7(void)296 bad7(void)
297 {
298 lck_mtx_lock(&lck1); // no-warning
299 lck_mtx_lock(&lck2); // no-warning
300 lck_mtx_unlock(&lck1); // expected-warning{{This was not the most recently acquired lock}}
301 lck_mtx_unlock(&lck2);
302 }
303
304 void
bad8(void)305 bad8(void)
306 {
307 if (lck_mtx_try_lock(&lck1) == 0) // no-warning
308 return;
309 lck_mtx_lock(&lck2); // no-warning
310 lck_mtx_unlock(&lck1); // expected-warning{{This was not the most recently acquired lock}}
311 }
312
313 void
bad9(void)314 bad9(void)
315 {
316 lck_mtx_unlock(&lck1); // no-warning
317 lck_mtx_unlock(&lck1); // expected-warning{{This lock has already been unlocked}}
318 }
319
320 void
bad10(void)321 bad10(void)
322 {
323 lck_mtx_lock(&lck1); // no-warning
324 lck_mtx_unlock(&lck1); // no-warning
325 lck_mtx_unlock(&lck1); // expected-warning{{This lock has already been unlocked}}
326 }
327
328 static void
bad11_sub(pthread_mutex_t * lock)329 bad11_sub(pthread_mutex_t *lock)
330 {
331 lck_mtx_unlock(lock); // expected-warning{{This lock has already been unlocked}}
332 }
333
334 void
bad11(int i)335 bad11(int i)
336 {
337 lck_mtx_lock(&lck1); // no-warning
338 lck_mtx_unlock(&lck1); // no-warning
339 if (i < 5)
340 bad11_sub(&lck1);
341 }
342
343 void
bad12(void)344 bad12(void)
345 {
346 pthread_mutex_lock(&mtx1); // no-warning
347 pthread_mutex_unlock(&mtx1); // no-warning
348 pthread_mutex_lock(&mtx1); // no-warning
349 pthread_mutex_unlock(&mtx1); // no-warning
350 pthread_mutex_unlock(&mtx1); // expected-warning{{This lock has already been unlocked}}
351 }
352
353 void
bad13(void)354 bad13(void)
355 {
356 pthread_mutex_lock(&mtx1); // no-warning
357 pthread_mutex_unlock(&mtx1); // no-warning
358 pthread_mutex_lock(&mtx2); // no-warning
359 pthread_mutex_unlock(&mtx2); // no-warning
360 pthread_mutex_unlock(&mtx1); // expected-warning{{This lock has already been unlocked}}
361 }
362
363 void
bad14(void)364 bad14(void)
365 {
366 pthread_mutex_lock(&mtx1); // no-warning
367 pthread_mutex_lock(&mtx2); // no-warning
368 pthread_mutex_unlock(&mtx2); // no-warning
369 pthread_mutex_unlock(&mtx1); // no-warning
370 pthread_mutex_unlock(&mtx2); // expected-warning{{This lock has already been unlocked}}
371 }
372
373 void
bad15(void)374 bad15(void)
375 {
376 pthread_mutex_lock(&mtx1); // no-warning
377 pthread_mutex_lock(&mtx2); // no-warning
378 pthread_mutex_unlock(&mtx2); // no-warning
379 pthread_mutex_unlock(&mtx1); // no-warning
380 pthread_mutex_lock(&mtx1); // no-warning
381 pthread_mutex_unlock(&mtx2); // expected-warning{{This lock has already been unlocked}}
382 }
383
384 void
bad16(void)385 bad16(void)
386 {
387 pthread_mutex_destroy(&mtx1); // no-warning
388 pthread_mutex_lock(&mtx1); // expected-warning{{This lock has already been destroyed}}
389 }
390
391 void
bad17(void)392 bad17(void)
393 {
394 pthread_mutex_destroy(&mtx1); // no-warning
395 pthread_mutex_unlock(&mtx1); // expected-warning{{This lock has already been destroyed}}
396 }
397
398 void
bad18(void)399 bad18(void)
400 {
401 pthread_mutex_destroy(&mtx1); // no-warning
402 pthread_mutex_destroy(&mtx1); // expected-warning{{This lock has already been destroyed}}
403 }
404
405 void
bad19(void)406 bad19(void)
407 {
408 pthread_mutex_lock(&mtx1); // no-warning
409 pthread_mutex_destroy(&mtx1); // expected-warning{{This lock is still locked}}
410 }
411
412 void
bad20(void)413 bad20(void)
414 {
415 lck_mtx_destroy(&mtx1, &grp1); // no-warning
416 lck_mtx_lock(&mtx1); // expected-warning{{This lock has already been destroyed}}
417 }
418
419 void
bad21(void)420 bad21(void)
421 {
422 lck_mtx_destroy(&mtx1, &grp1); // no-warning
423 lck_mtx_unlock(&mtx1); // expected-warning{{This lock has already been destroyed}}
424 }
425
426 void
bad22(void)427 bad22(void)
428 {
429 lck_mtx_destroy(&mtx1, &grp1); // no-warning
430 lck_mtx_destroy(&mtx1, &grp1); // expected-warning{{This lock has already been destroyed}}
431 }
432
433 void
bad23(void)434 bad23(void)
435 {
436 lck_mtx_lock(&mtx1); // no-warning
437 lck_mtx_destroy(&mtx1, &grp1); // expected-warning{{This lock is still locked}}
438 }
439
440 void
bad24(void)441 bad24(void)
442 {
443 pthread_mutex_init(&mtx1, NULL); // no-warning
444 pthread_mutex_init(&mtx1, NULL); // expected-warning{{This lock has already been initialized}}
445 }
446
447 void
bad25(void)448 bad25(void)
449 {
450 pthread_mutex_lock(&mtx1); // no-warning
451 pthread_mutex_init(&mtx1, NULL); // expected-warning{{This lock is still being held}}
452 }
453
454 void
bad26(void)455 bad26(void)
456 {
457 pthread_mutex_unlock(&mtx1); // no-warning
458 pthread_mutex_init(&mtx1, NULL); // expected-warning{{This lock has already been initialized}}
459 }
460
bad27(void)461 void bad27(void) {
462 pthread_mutex_unlock(&mtx1); // no-warning
463 int ret = pthread_mutex_destroy(&mtx1); // no-warning
464 if (ret != 0) // no-warning
465 pthread_mutex_lock(&mtx1); // no-warning
466 else
467 pthread_mutex_unlock(&mtx1); // expected-warning{{This lock has already been destroyed}}
468 }
469
bad28(void)470 void bad28(void) {
471 pthread_mutex_unlock(&mtx1); // no-warning
472 int ret = pthread_mutex_destroy(&mtx1); // no-warning
473 if (ret != 0) // no-warning
474 pthread_mutex_lock(&mtx1); // no-warning
475 else
476 pthread_mutex_lock(&mtx1); // expected-warning{{This lock has already been destroyed}}
477 }
478
bad29(void)479 void bad29(void) {
480 pthread_mutex_lock(&mtx1); // no-warning
481 pthread_mutex_unlock(&mtx1); // no-warning
482 if (pthread_mutex_destroy(&mtx1) != 0) // no-warning
483 pthread_mutex_init(&mtx1, NULL); // expected-warning{{This lock has already been initialized}}
484 else
485 pthread_mutex_init(&mtx1, NULL); // no-warning
486 }
487
bad30(void)488 void bad30(void) {
489 pthread_mutex_lock(&mtx1); // no-warning
490 pthread_mutex_unlock(&mtx1); // no-warning
491 if (pthread_mutex_destroy(&mtx1) != 0) // no-warning
492 pthread_mutex_init(&mtx1, NULL); // expected-warning{{This lock has already been initialized}}
493 else
494 pthread_mutex_destroy(&mtx1); // expected-warning{{This lock has already been destroyed}}
495 }
496
bad31(void)497 void bad31(void) {
498 int ret = pthread_mutex_destroy(&mtx1); // no-warning
499 pthread_mutex_lock(&mtx1); // expected-warning{{This lock has already been destroyed}}
500 if (ret != 0)
501 pthread_mutex_lock(&mtx1);
502 }
503
bad32(void)504 void bad32(void) {
505 lck_rw_lock_shared(&rw);
506 lck_rw_unlock_exclusive(&rw); // FIXME: warn - should be shared?
507 lck_rw_lock_exclusive(&rw);
508 lck_rw_unlock_shared(&rw); // FIXME: warn - should be exclusive?
509 }
510
bad33(void)511 void bad33(void) {
512 pthread_mutex_lock(pmtx);
513 fake_system_function();
514 pthread_mutex_lock(pmtx); // expected-warning{{This lock has already been acquired}}
515 }
516
nocrash1(pthread_mutex_t * mutex)517 void nocrash1(pthread_mutex_t *mutex) {
518 int ret = pthread_mutex_destroy(mutex);
519 if (ret == 0) // no crash
520 ;
521 }
522