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
;
18 pthread_mutex_lock(&mtx1
); // no-warning
24 pthread_mutex_unlock(&mtx1
); // no-warning
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
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
48 if (pthread_mutex_trylock(&mtx1
) == 0) // no-warning
49 pthread_mutex_unlock(&mtx1
); // no-warning
55 lck_mtx_lock(&lck1
); // no-warning
61 if (lck_mtx_try_lock(&lck1
) != 0) // no-warning
62 lck_mtx_unlock(&lck1
); // no-warning
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
77 pthread_mutex_unlock(&mtx1
); // no-warning
78 if (pthread_mutex_trylock(&mtx1
) == 0) // no-warning
79 pthread_mutex_unlock(&mtx1
); // no-warning
85 if (pthread_mutex_trylock(&mtx1
) != 0) // no-warning
86 pthread_mutex_lock(&mtx1
); // no-warning
87 pthread_mutex_unlock(&mtx1
); // no-warning
93 pthread_mutex_destroy(&mtx1
); // no-warning
99 pthread_mutex_destroy(&mtx1
); // no-warning
100 pthread_mutex_destroy(&mtx2
); // no-warning
106 pthread_mutex_unlock(&mtx1
); // no-warning
107 pthread_mutex_destroy(&mtx1
); // no-warning
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
122 pthread_mutex_lock(&mtx1
); // no-warning
123 pthread_mutex_unlock(&mtx1
); // no-warning
124 pthread_mutex_destroy(&mtx1
); // no-warning
130 pthread_mutex_init(&mtx1
, NULL
); // no-warning
136 pthread_mutex_init(&mtx1
, NULL
); // no-warning
137 pthread_mutex_init(&mtx2
, NULL
); // no-warning
143 pthread_mutex_destroy(&mtx1
); // no-warning
144 pthread_mutex_init(&mtx1
, NULL
); // no-warning
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
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
168 pthread_mutex_lock(pmtx
); // no-warning
169 pthread_mutex_unlock(pmtx
); // no-warning
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
181 if (pthread_mutex_destroy(&mtx1
) != 0) // no-warning
182 pthread_mutex_destroy(&mtx1
); // no-warning
186 if (pthread_mutex_destroy(&mtx1
) != 0) // no-warning
187 pthread_mutex_lock(&mtx1
); // no-warning
191 if (pthread_mutex_destroy(&mtx1
) != 0) // no-warning
192 pthread_mutex_unlock(&mtx1
); // no-warning
196 pthread_mutex_unlock(&mtx1
); // no-warning
197 if (pthread_mutex_destroy(&mtx1
) != 0) // no-warning
198 pthread_mutex_lock(&mtx1
); // no-warning
202 pthread_mutex_unlock(&mtx1
); // no-warning
203 if (pthread_mutex_destroy(&mtx1
) != 0) // no-warning
204 pthread_mutex_lock(&mtx1
); // no-warning
206 pthread_mutex_init(&mtx1
, NULL
); // no-warning
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
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
);
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
);
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
);
248 pthread_mutex_lock(&mtx1
); // no-warning
249 pthread_mutex_lock(&mtx1
); // expected-warning{{This lock has already been acquired}}
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}}
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
);
273 if (pthread_mutex_trylock(&mtx1
)) // no-warning
275 pthread_mutex_lock(&mtx2
); // no-warning
276 pthread_mutex_unlock(&mtx1
); // expected-warning{{This was not the most recently acquired lock}}
282 lck_mtx_lock(&lck1
); // no-warning
283 lck_mtx_lock(&lck1
); // expected-warning{{This lock has already been acquired}}
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}}
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
);
307 if (lck_mtx_try_lock(&lck1
) == 0) // no-warning
309 lck_mtx_lock(&lck2
); // no-warning
310 lck_mtx_unlock(&lck1
); // expected-warning{{This was not the most recently acquired lock}}
316 lck_mtx_unlock(&lck1
); // no-warning
317 lck_mtx_unlock(&lck1
); // expected-warning{{This lock has already been unlocked}}
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}}
329 bad11_sub(pthread_mutex_t
*lock
)
331 lck_mtx_unlock(lock
); // expected-warning{{This lock has already been unlocked}}
337 lck_mtx_lock(&lck1
); // no-warning
338 lck_mtx_unlock(&lck1
); // no-warning
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}}
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}}
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}}
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}}
387 pthread_mutex_destroy(&mtx1
); // no-warning
388 pthread_mutex_lock(&mtx1
); // expected-warning{{This lock has already been destroyed}}
394 pthread_mutex_destroy(&mtx1
); // no-warning
395 pthread_mutex_unlock(&mtx1
); // expected-warning{{This lock has already been destroyed}}
401 pthread_mutex_destroy(&mtx1
); // no-warning
402 pthread_mutex_destroy(&mtx1
); // expected-warning{{This lock has already been destroyed}}
408 pthread_mutex_lock(&mtx1
); // no-warning
409 pthread_mutex_destroy(&mtx1
); // expected-warning{{This lock is still locked}}
415 lck_mtx_destroy(&mtx1
, &grp1
); // no-warning
416 lck_mtx_lock(&mtx1
); // expected-warning{{This lock has already been destroyed}}
422 lck_mtx_destroy(&mtx1
, &grp1
); // no-warning
423 lck_mtx_unlock(&mtx1
); // expected-warning{{This lock has already been destroyed}}
429 lck_mtx_destroy(&mtx1
, &grp1
); // no-warning
430 lck_mtx_destroy(&mtx1
, &grp1
); // expected-warning{{This lock has already been destroyed}}
436 lck_mtx_lock(&mtx1
); // no-warning
437 lck_mtx_destroy(&mtx1
, &grp1
); // expected-warning{{This lock is still locked}}
443 pthread_mutex_init(&mtx1
, NULL
); // no-warning
444 pthread_mutex_init(&mtx1
, NULL
); // expected-warning{{This lock has already been initialized}}
450 pthread_mutex_lock(&mtx1
); // no-warning
451 pthread_mutex_init(&mtx1
, NULL
); // expected-warning{{This lock is still being held}}
457 pthread_mutex_unlock(&mtx1
); // no-warning
458 pthread_mutex_init(&mtx1
, NULL
); // expected-warning{{This lock has already been initialized}}
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
467 pthread_mutex_unlock(&mtx1
); // expected-warning{{This lock has already been destroyed}}
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
476 pthread_mutex_lock(&mtx1
); // expected-warning{{This lock has already been destroyed}}
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}}
485 pthread_mutex_init(&mtx1
, NULL
); // no-warning
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}}
494 pthread_mutex_destroy(&mtx1
); // expected-warning{{This lock has already been destroyed}}
498 int ret
= pthread_mutex_destroy(&mtx1
); // no-warning
499 pthread_mutex_lock(&mtx1
); // expected-warning{{This lock has already been destroyed}}
501 pthread_mutex_lock(&mtx1
);
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?
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