Clang Project

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