[clang] Handle __declspec() attributes in using
[llvm-project.git] / clang / test / Analysis / pthreadlock.c
blob85b34cbed918d5141a02dec522925bd32d30e1ab
1 // RUN: %clang_analyze_cc1 -analyzer-checker=alpha.unix.PthreadLock -verify %s
3 // Tests performing normal locking patterns and wrong locking orders
5 #include "Inputs/system-header-simulator-for-pthread-lock.h"
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;
13 #define NULL 0
15 void
16 ok1(void)
18 pthread_mutex_lock(&mtx1); // no-warning
21 void
22 ok2(void)
24 pthread_mutex_unlock(&mtx1); // no-warning
27 void
28 ok3(void)
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
36 void
37 ok4(void)
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
45 void
46 ok5(void)
48 if (pthread_mutex_trylock(&mtx1) == 0) // no-warning
49 pthread_mutex_unlock(&mtx1); // no-warning
52 void
53 ok6(void)
55 lck_mtx_lock(&lck1); // no-warning
58 void
59 ok7(void)
61 if (lck_mtx_try_lock(&lck1) != 0) // no-warning
62 lck_mtx_unlock(&lck1); // no-warning
65 void
66 ok8(void)
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
74 void
75 ok9(void)
77 pthread_mutex_unlock(&mtx1); // no-warning
78 if (pthread_mutex_trylock(&mtx1) == 0) // no-warning
79 pthread_mutex_unlock(&mtx1); // no-warning
82 void
83 ok10(void)
85 if (pthread_mutex_trylock(&mtx1) != 0) // no-warning
86 pthread_mutex_lock(&mtx1); // no-warning
87 pthread_mutex_unlock(&mtx1); // no-warning
90 void
91 ok11(void)
93 pthread_mutex_destroy(&mtx1); // no-warning
96 void
97 ok12(void)
99 pthread_mutex_destroy(&mtx1); // no-warning
100 pthread_mutex_destroy(&mtx2); // no-warning
103 void
104 ok13(void)
106 pthread_mutex_unlock(&mtx1); // no-warning
107 pthread_mutex_destroy(&mtx1); // no-warning
110 void
111 ok14(void)
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
119 void
120 ok15(void)
122 pthread_mutex_lock(&mtx1); // no-warning
123 pthread_mutex_unlock(&mtx1); // no-warning
124 pthread_mutex_destroy(&mtx1); // no-warning
127 void
128 ok16(void)
130 pthread_mutex_init(&mtx1, NULL); // no-warning
133 void
134 ok17(void)
136 pthread_mutex_init(&mtx1, NULL); // no-warning
137 pthread_mutex_init(&mtx2, NULL); // no-warning
140 void
141 ok18(void)
143 pthread_mutex_destroy(&mtx1); // no-warning
144 pthread_mutex_init(&mtx1, NULL); // no-warning
147 void
148 ok19(void)
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
156 void
157 ok20(void)
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
166 void
167 ok21(void) {
168 pthread_mutex_lock(pmtx); // no-warning
169 pthread_mutex_unlock(pmtx); // no-warning
172 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
180 void ok23(void) {
181 if (pthread_mutex_destroy(&mtx1) != 0) // no-warning
182 pthread_mutex_destroy(&mtx1); // no-warning
185 void ok24(void) {
186 if (pthread_mutex_destroy(&mtx1) != 0) // no-warning
187 pthread_mutex_lock(&mtx1); // no-warning
190 void ok25(void) {
191 if (pthread_mutex_destroy(&mtx1) != 0) // no-warning
192 pthread_mutex_unlock(&mtx1); // no-warning
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
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
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
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
224 void escape_mutex(pthread_mutex_t *m);
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);
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);
245 void
246 bad1(void)
248 pthread_mutex_lock(&mtx1); // no-warning
249 pthread_mutex_lock(&mtx1); // expected-warning{{This lock has already been acquired}}
252 void
253 bad2(void)
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}}
261 void
262 bad3(void)
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);
270 void
271 bad4(void)
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}}
279 void
280 bad5(void)
282 lck_mtx_lock(&lck1); // no-warning
283 lck_mtx_lock(&lck1); // expected-warning{{This lock has already been acquired}}
286 void
287 bad6(void)
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}}
295 void
296 bad7(void)
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);
304 void
305 bad8(void)
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}}
313 void
314 bad9(void)
316 lck_mtx_unlock(&lck1); // no-warning
317 lck_mtx_unlock(&lck1); // expected-warning{{This lock has already been unlocked}}
320 void
321 bad10(void)
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}}
328 static void
329 bad11_sub(pthread_mutex_t *lock)
331 lck_mtx_unlock(lock); // expected-warning{{This lock has already been unlocked}}
334 void
335 bad11(int i)
337 lck_mtx_lock(&lck1); // no-warning
338 lck_mtx_unlock(&lck1); // no-warning
339 if (i < 5)
340 bad11_sub(&lck1);
343 void
344 bad12(void)
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}}
353 void
354 bad13(void)
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}}
363 void
364 bad14(void)
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}}
373 void
374 bad15(void)
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}}
384 void
385 bad16(void)
387 pthread_mutex_destroy(&mtx1); // no-warning
388 pthread_mutex_lock(&mtx1); // expected-warning{{This lock has already been destroyed}}
391 void
392 bad17(void)
394 pthread_mutex_destroy(&mtx1); // no-warning
395 pthread_mutex_unlock(&mtx1); // expected-warning{{This lock has already been destroyed}}
398 void
399 bad18(void)
401 pthread_mutex_destroy(&mtx1); // no-warning
402 pthread_mutex_destroy(&mtx1); // expected-warning{{This lock has already been destroyed}}
405 void
406 bad19(void)
408 pthread_mutex_lock(&mtx1); // no-warning
409 pthread_mutex_destroy(&mtx1); // expected-warning{{This lock is still locked}}
412 void
413 bad20(void)
415 lck_mtx_destroy(&mtx1, &grp1); // no-warning
416 lck_mtx_lock(&mtx1); // expected-warning{{This lock has already been destroyed}}
419 void
420 bad21(void)
422 lck_mtx_destroy(&mtx1, &grp1); // no-warning
423 lck_mtx_unlock(&mtx1); // expected-warning{{This lock has already been destroyed}}
426 void
427 bad22(void)
429 lck_mtx_destroy(&mtx1, &grp1); // no-warning
430 lck_mtx_destroy(&mtx1, &grp1); // expected-warning{{This lock has already been destroyed}}
433 void
434 bad23(void)
436 lck_mtx_lock(&mtx1); // no-warning
437 lck_mtx_destroy(&mtx1, &grp1); // expected-warning{{This lock is still locked}}
440 void
441 bad24(void)
443 pthread_mutex_init(&mtx1, NULL); // no-warning
444 pthread_mutex_init(&mtx1, NULL); // expected-warning{{This lock has already been initialized}}
447 void
448 bad25(void)
450 pthread_mutex_lock(&mtx1); // no-warning
451 pthread_mutex_init(&mtx1, NULL); // expected-warning{{This lock is still being held}}
454 void
455 bad26(void)
457 pthread_mutex_unlock(&mtx1); // no-warning
458 pthread_mutex_init(&mtx1, NULL); // expected-warning{{This lock has already been initialized}}
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}}
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}}
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
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}}
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);
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?
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}}
517 void nocrash1(pthread_mutex_t *mutex) {
518 int ret = pthread_mutex_destroy(mutex);
519 if (ret == 0) // no crash