blob: 4735d20eaa5c28225c7479effd21d105c1b73639 [file] [log] [blame]
Ted Kremenek033a07e2011-08-03 23:14:55 +00001// RUN: %clang_cc1 -analyze -analyzer-checker=experimental.unix.PthreadLock -verify %s
Jordy Rosedcb1d5d2011-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
9typedef pthread_mutex_t lck_mtx_t;
10
11extern int pthread_mutex_lock(pthread_mutex_t *);
12extern int pthread_mutex_unlock(pthread_mutex_t *);
13extern int pthread_mutex_trylock(pthread_mutex_t *);
14extern int lck_mtx_lock(lck_mtx_t *);
15extern int lck_mtx_unlock(lck_mtx_t *);
16extern int lck_mtx_try_lock(lck_mtx_t *);
17
18pthread_mutex_t mtx1, mtx2;
19lck_mtx_t lck1, lck2;
20
21void
22ok1(void)
23{
24 pthread_mutex_lock(&mtx1); // no-warning
25}
26
27void
28ok2(void)
29{
30 pthread_mutex_unlock(&mtx1); // no-warning
31}
32
33void
34ok3(void)
35{
36 pthread_mutex_lock(&mtx1); // no-warning
37 pthread_mutex_unlock(&mtx1); // no-warning
38 pthread_mutex_lock(&mtx1); // no-warning
39 pthread_mutex_unlock(&mtx1); // no-warning
40}
41
42void
43ok4(void)
44{
45 pthread_mutex_lock(&mtx1); // no-warning
46 pthread_mutex_unlock(&mtx1); // no-warning
47 pthread_mutex_lock(&mtx2); // no-warning
48 pthread_mutex_unlock(&mtx2); // no-warning
49}
50
51void
52ok5(void)
53{
54 if (pthread_mutex_trylock(&mtx1) == 0) // no-warning
55 pthread_mutex_unlock(&mtx1); // no-warning
56}
57
58void
59ok6(void)
60{
61 lck_mtx_lock(&lck1); // no-warning
62}
63
64void
65ok7(void)
66{
67 if (lck_mtx_try_lock(&lck1) != 0) // no-warning
68 lck_mtx_unlock(&lck1); // no-warning
69}
70
71void
72bad1(void)
73{
74 pthread_mutex_lock(&mtx1); // no-warning
75 pthread_mutex_lock(&mtx1); // expected-warning{{This lock has already been acquired}}
76}
77
78void
79bad2(void)
80{
81 pthread_mutex_lock(&mtx1); // no-warning
82 pthread_mutex_unlock(&mtx1); // no-warning
83 pthread_mutex_lock(&mtx1); // no-warning
84 pthread_mutex_lock(&mtx1); // expected-warning{{This lock has already been acquired}}
85}
86
87void
88bad3(void)
89{
90 pthread_mutex_lock(&mtx1); // no-warning
91 pthread_mutex_lock(&mtx2); // no-warning
92 pthread_mutex_unlock(&mtx1); // expected-warning{{This was not the most recently acquired lock}}
93 pthread_mutex_unlock(&mtx2);
94}
95
96void
97bad4(void)
98{
99 if (pthread_mutex_trylock(&mtx1)) // no-warning
100 return;
101 pthread_mutex_lock(&mtx2); // no-warning
102 pthread_mutex_unlock(&mtx1); // expected-warning{{This was not the most recently acquired lock}}
103}
104
105void
106bad5(void)
107{
108 lck_mtx_lock(&lck1); // no-warning
109 lck_mtx_lock(&lck1); // expected-warning{{This lock has already been acquired}}
110}
111
112void
113bad6(void)
114{
115 lck_mtx_lock(&lck1); // no-warning
116 lck_mtx_unlock(&lck1); // no-warning
117 lck_mtx_lock(&lck1); // no-warning
118 lck_mtx_lock(&lck1); // expected-warning{{This lock has already been acquired}}
119}
120
121void
122bad7(void)
123{
124 lck_mtx_lock(&lck1); // no-warning
125 lck_mtx_lock(&lck2); // no-warning
126 lck_mtx_unlock(&lck1); // expected-warning{{This was not the most recently acquired lock}}
127 lck_mtx_unlock(&lck2);
128}
129
130void
131bad8(void)
132{
133 if (lck_mtx_try_lock(&lck1) == 0) // no-warning
134 return;
135 lck_mtx_lock(&lck2); // no-warning
136 lck_mtx_unlock(&lck1); // expected-warning{{This was not the most recently acquired lock}}
137}