1// RUN: %clang_cc1 -analyze -analyzer-checker=alpha.unix.PthreadLock -verify %s
2
3// Tests performing normal locking patterns and wrong locking orders
4
5typedef struct {
6	void	*foo;
7} pthread_mutex_t;
8
9typedef struct {
10	void	*foo;
11} pthread_mutexattr_t;
12
13typedef struct {
14	void	*foo;
15} lck_grp_t;
16
17typedef pthread_mutex_t lck_mtx_t;
18
19extern int pthread_mutex_lock(pthread_mutex_t *);
20extern int pthread_mutex_unlock(pthread_mutex_t *);
21extern int pthread_mutex_trylock(pthread_mutex_t *);
22extern int pthread_mutex_destroy(pthread_mutex_t *);
23extern int pthread_mutex_init(pthread_mutex_t  *mutex, const pthread_mutexattr_t *mutexattr);
24extern int lck_mtx_lock(lck_mtx_t *);
25extern int lck_mtx_unlock(lck_mtx_t *);
26extern int lck_mtx_try_lock(lck_mtx_t *);
27extern void lck_mtx_destroy(lck_mtx_t *lck, lck_grp_t *grp);
28
29pthread_mutex_t mtx1, mtx2;
30lck_mtx_t lck1, lck2;
31lck_grp_t grp1;
32
33#define NULL 0
34
35void
36ok1(void)
37{
38	pthread_mutex_lock(&mtx1); // no-warning
39}
40
41void
42ok2(void)
43{
44	pthread_mutex_unlock(&mtx1); // no-warning
45}
46
47void
48ok3(void)
49{
50	pthread_mutex_lock(&mtx1);	// no-warning
51	pthread_mutex_unlock(&mtx1);	// no-warning
52	pthread_mutex_lock(&mtx1);	// no-warning
53	pthread_mutex_unlock(&mtx1);	// no-warning
54}
55
56void
57ok4(void)
58{
59	pthread_mutex_lock(&mtx1);	// no-warning
60	pthread_mutex_unlock(&mtx1);	// no-warning
61	pthread_mutex_lock(&mtx2);	// no-warning
62	pthread_mutex_unlock(&mtx2);	// no-warning
63}
64
65void
66ok5(void)
67{
68	if (pthread_mutex_trylock(&mtx1) == 0)	// no-warning
69		pthread_mutex_unlock(&mtx1);	// no-warning
70}
71
72void
73ok6(void)
74{
75	lck_mtx_lock(&lck1);		// no-warning
76}
77
78void
79ok7(void)
80{
81	if (lck_mtx_try_lock(&lck1) != 0)	// no-warning
82		lck_mtx_unlock(&lck1);		// no-warning
83}
84
85void
86ok8(void)
87{
88	pthread_mutex_lock(&mtx1);	// no-warning
89	pthread_mutex_lock(&mtx2);	// no-warning
90	pthread_mutex_unlock(&mtx2);	// no-warning
91	pthread_mutex_unlock(&mtx1);	// no-warning
92}
93
94void
95ok9(void)
96{
97	pthread_mutex_unlock(&mtx1);		// no-warning
98	if (pthread_mutex_trylock(&mtx1) == 0)	// no-warning
99		pthread_mutex_unlock(&mtx1);	// no-warning
100}
101
102void
103ok10(void)
104{
105	if (pthread_mutex_trylock(&mtx1) != 0)	// no-warning
106		pthread_mutex_lock(&mtx1);	// no-warning
107	pthread_mutex_unlock(&mtx1);		// no-warning
108}
109
110void
111ok11(void)
112{
113	pthread_mutex_destroy(&mtx1);	// no-warning
114}
115
116void
117ok12(void)
118{
119	pthread_mutex_destroy(&mtx1);	// no-warning
120	pthread_mutex_destroy(&mtx2);	// no-warning
121}
122
123void
124ok13(void)
125{
126	pthread_mutex_unlock(&mtx1);	// no-warning
127	pthread_mutex_destroy(&mtx1);	// no-warning
128}
129
130void
131ok14(void)
132{
133	pthread_mutex_unlock(&mtx1);	// no-warning
134	pthread_mutex_destroy(&mtx1);	// no-warning
135	pthread_mutex_unlock(&mtx2);	// no-warning
136	pthread_mutex_destroy(&mtx2);	// no-warning
137}
138
139void
140ok15(void)
141{
142	pthread_mutex_lock(&mtx1);	// no-warning
143	pthread_mutex_unlock(&mtx1);	// no-warning
144	pthread_mutex_destroy(&mtx1);	// no-warning
145}
146
147void
148ok16(void)
149{
150	pthread_mutex_init(&mtx1, NULL);	// no-warning
151}
152
153void
154ok17(void)
155{
156	pthread_mutex_init(&mtx1, NULL);	// no-warning
157	pthread_mutex_init(&mtx2, NULL);	// no-warning
158}
159
160void
161ok18(void)
162{
163	pthread_mutex_destroy(&mtx1);		// no-warning
164	pthread_mutex_init(&mtx1, NULL);	// no-warning
165}
166
167void
168ok19(void)
169{
170	pthread_mutex_destroy(&mtx1);		// no-warning
171	pthread_mutex_init(&mtx1, NULL);	// no-warning
172	pthread_mutex_destroy(&mtx2);		// no-warning
173	pthread_mutex_init(&mtx2, NULL);	// no-warning
174}
175
176void
177ok20(void)
178{
179	pthread_mutex_unlock(&mtx1);		// no-warning
180	pthread_mutex_destroy(&mtx1);		// no-warning
181	pthread_mutex_init(&mtx1, NULL);	// no-warning
182	pthread_mutex_destroy(&mtx1);		// no-warning
183	pthread_mutex_init(&mtx1, NULL);	// no-warning
184}
185
186void
187bad1(void)
188{
189	pthread_mutex_lock(&mtx1);	// no-warning
190	pthread_mutex_lock(&mtx1);	// expected-warning{{This lock has already been acquired}}
191}
192
193void
194bad2(void)
195{
196	pthread_mutex_lock(&mtx1);	// no-warning
197	pthread_mutex_unlock(&mtx1);	// no-warning
198	pthread_mutex_lock(&mtx1);	// no-warning
199	pthread_mutex_lock(&mtx1);	// expected-warning{{This lock has already been acquired}}
200}
201
202void
203bad3(void)
204{
205	pthread_mutex_lock(&mtx1);	// no-warning
206	pthread_mutex_lock(&mtx2);	// no-warning
207	pthread_mutex_unlock(&mtx1);	// expected-warning{{This was not the most recently acquired lock}}
208	pthread_mutex_unlock(&mtx2);
209}
210
211void
212bad4(void)
213{
214	if (pthread_mutex_trylock(&mtx1)) // no-warning
215		return;
216	pthread_mutex_lock(&mtx2);	// no-warning
217	pthread_mutex_unlock(&mtx1);	// expected-warning{{This was not the most recently acquired lock}}
218}
219
220void
221bad5(void)
222{
223	lck_mtx_lock(&lck1);	// no-warning
224	lck_mtx_lock(&lck1);	// expected-warning{{This lock has already been acquired}}
225}
226
227void
228bad6(void)
229{
230	lck_mtx_lock(&lck1);	// no-warning
231	lck_mtx_unlock(&lck1);	// no-warning
232	lck_mtx_lock(&lck1);	// no-warning
233	lck_mtx_lock(&lck1);	// expected-warning{{This lock has already been acquired}}
234}
235
236void
237bad7(void)
238{
239	lck_mtx_lock(&lck1);	// no-warning
240	lck_mtx_lock(&lck2);	// no-warning
241	lck_mtx_unlock(&lck1);	// expected-warning{{This was not the most recently acquired lock}}
242	lck_mtx_unlock(&lck2);
243}
244
245void
246bad8(void)
247{
248	if (lck_mtx_try_lock(&lck1) == 0) // no-warning
249		return;
250	lck_mtx_lock(&lck2);		// no-warning
251	lck_mtx_unlock(&lck1);		// expected-warning{{This was not the most recently acquired lock}}
252}
253
254void
255bad9(void)
256{
257	lck_mtx_unlock(&lck1);		// no-warning
258	lck_mtx_unlock(&lck1);		// expected-warning{{This lock has already been unlocked}}
259}
260
261void
262bad10(void)
263{
264	lck_mtx_lock(&lck1);		// no-warning
265	lck_mtx_unlock(&lck1);		// no-warning
266	lck_mtx_unlock(&lck1);		// expected-warning{{This lock has already been unlocked}}
267}
268
269static void
270bad11_sub(pthread_mutex_t *lock)
271{
272	lck_mtx_unlock(lock);		// expected-warning{{This lock has already been unlocked}}
273}
274
275void
276bad11(int i)
277{
278	lck_mtx_lock(&lck1);		// no-warning
279	lck_mtx_unlock(&lck1);		// no-warning
280	if (i < 5)
281		bad11_sub(&lck1);
282}
283
284void
285bad12(void)
286{
287	pthread_mutex_lock(&mtx1);	// no-warning
288	pthread_mutex_unlock(&mtx1);	// no-warning
289	pthread_mutex_lock(&mtx1);	// no-warning
290	pthread_mutex_unlock(&mtx1);	// no-warning
291	pthread_mutex_unlock(&mtx1);	// expected-warning{{This lock has already been unlocked}}
292}
293
294void
295bad13(void)
296{
297	pthread_mutex_lock(&mtx1);	// no-warning
298	pthread_mutex_unlock(&mtx1);	// no-warning
299	pthread_mutex_lock(&mtx2);	// no-warning
300	pthread_mutex_unlock(&mtx2);	// no-warning
301	pthread_mutex_unlock(&mtx1);	// expected-warning{{This lock has already been unlocked}}
302}
303
304void
305bad14(void)
306{
307	pthread_mutex_lock(&mtx1);	// no-warning
308	pthread_mutex_lock(&mtx2);	// no-warning
309	pthread_mutex_unlock(&mtx2);	// no-warning
310	pthread_mutex_unlock(&mtx1);	// no-warning
311	pthread_mutex_unlock(&mtx2);	// expected-warning{{This lock has already been unlocked}}
312}
313
314void
315bad15(void)
316{
317	pthread_mutex_lock(&mtx1);	// no-warning
318	pthread_mutex_lock(&mtx2);	// no-warning
319	pthread_mutex_unlock(&mtx2);	// no-warning
320	pthread_mutex_unlock(&mtx1);	// no-warning
321	pthread_mutex_lock(&mtx1);	// no-warning
322	pthread_mutex_unlock(&mtx2);	// expected-warning{{This lock has already been unlocked}}
323}
324
325void
326bad16(void)
327{
328	pthread_mutex_destroy(&mtx1);	// no-warning
329	pthread_mutex_lock(&mtx1);	// expected-warning{{This lock has already been destroyed}}
330}
331
332void
333bad17(void)
334{
335	pthread_mutex_destroy(&mtx1);	// no-warning
336	pthread_mutex_unlock(&mtx1);	// expected-warning{{This lock has already been destroyed}}
337}
338
339void
340bad18(void)
341{
342	pthread_mutex_destroy(&mtx1);	// no-warning
343	pthread_mutex_destroy(&mtx1);	// expected-warning{{This lock has already been destroyed}}
344}
345
346void
347bad19(void)
348{
349	pthread_mutex_lock(&mtx1);	// no-warning
350	pthread_mutex_destroy(&mtx1);	// expected-warning{{This lock is still locked}}
351}
352
353void
354bad20(void)
355{
356	lck_mtx_destroy(&mtx1, &grp1);	// no-warning
357	lck_mtx_lock(&mtx1);		// expected-warning{{This lock has already been destroyed}}
358}
359
360void
361bad21(void)
362{
363	lck_mtx_destroy(&mtx1, &grp1);	// no-warning
364	lck_mtx_unlock(&mtx1);		// expected-warning{{This lock has already been destroyed}}
365}
366
367void
368bad22(void)
369{
370	lck_mtx_destroy(&mtx1, &grp1);	// no-warning
371	lck_mtx_destroy(&mtx1, &grp1);	// expected-warning{{This lock has already been destroyed}}
372}
373
374void
375bad23(void)
376{
377	lck_mtx_lock(&mtx1);		// no-warning
378	lck_mtx_destroy(&mtx1, &grp1);	// expected-warning{{This lock is still locked}}
379}
380
381void
382bad24(void)
383{
384	pthread_mutex_init(&mtx1, NULL);	// no-warning
385	pthread_mutex_init(&mtx1, NULL);	// expected-warning{{This lock has already been initialized}}
386}
387
388void
389bad25(void)
390{
391	pthread_mutex_lock(&mtx1);		// no-warning
392	pthread_mutex_init(&mtx1, NULL);	// expected-warning{{This lock is still being held}}
393}
394
395void
396bad26(void)
397{
398	pthread_mutex_unlock(&mtx1);		// no-warning
399	pthread_mutex_init(&mtx1, NULL);	// expected-warning{{This lock has already been initialized}}
400}
401