flow: handle EXPR_OFFSETOF and EXPR_ALIGNOF
[smatch.git] / check_locking.c
blob00b80911a106d49d6a58e3e11484d0b73e411f13
1 /*
2 * sparse/check_locking.c
4 * Copyright (C) 2009 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
11 * This test checks that locks are held the same across all returns.
13 * Of course, some functions are designed to only hold the locks on success.
14 * Oh well... We can rewrite it later if we want.
16 * The list of wine locking functions came from an earlier script written
17 * by Michael Stefaniuc.
21 #include "parse.h"
22 #include "smatch.h"
23 #include "smatch_slist.h"
25 static int my_id;
27 static int func_has_transition;
29 STATE(locked);
30 STATE(start_state);
31 STATE(unlocked);
33 enum action {
34 LOCK,
35 UNLOCK,
38 enum return_type {
39 ret_any,
40 ret_non_zero,
41 ret_zero,
44 #define RETURN_VAL -1
45 #define NO_ARG -2
47 struct lock_info {
48 const char *function;
49 enum action action;
50 const char *name;
51 int arg;
52 enum return_type return_type;
55 static struct lock_info wine_lock_table[] = {
56 {"create_window_handle", LOCK, "create_window_handle", RETURN_VAL, ret_non_zero},
57 {"WIN_GetPtr", LOCK, "create_window_handle", RETURN_VAL, ret_non_zero},
58 {"WIN_ReleasePtr", UNLOCK, "create_window_handle", 0, ret_any},
59 {"EnterCriticalSection", LOCK, "CriticalSection", 0, ret_any},
60 {"LeaveCriticalSection", UNLOCK, "CriticalSection", 0, ret_any},
61 {"RtlEnterCriticalSection", LOCK, "RtlCriticalSection", 0, ret_any},
62 {"RtlLeaveCriticalSection", UNLOCK, "RtlCriticalSection", 0, ret_any},
63 {"GDI_GetObjPtr", LOCK, "GDI_Get", 0, ret_non_zero},
64 {"GDI_ReleaseObj", UNLOCK, "GDI_Get", 0, ret_any},
65 {"LdrLockLoaderLock", LOCK, "LdrLockLoaderLock", 2, ret_any},
66 {"LdrUnlockLoaderLock", UNLOCK, "LdrLockLoaderLock", 1, ret_any},
67 {"_lock", LOCK, "_lock", 0, ret_any},
68 {"_unlock", UNLOCK, "_lock", 0, ret_any},
69 {"msiobj_lock", LOCK, "msiobj_lock", 0, ret_any},
70 {"msiobj_unlock", UNLOCK, "msiobj_lock", 0, ret_any},
71 {"RtlAcquirePebLock", LOCK, "PebLock", NO_ARG, ret_any},
72 {"RtlReleasePebLock", UNLOCK, "PebLock", NO_ARG, ret_any},
73 {"server_enter_uninterrupted_section", LOCK, "server_uninterrupted_section", 0, ret_any},
74 {"server_leave_uninterrupted_section", UNLOCK, "server_uninterrupted_section", 0, ret_any},
75 {"RtlLockHeap", LOCK, "RtlLockHeap", 0, ret_any},
76 {"RtlUnlockHeap", UNLOCK, "RtlLockHeap", 0, ret_any},
77 {"_EnterSysLevel", LOCK, "SysLevel", 0, ret_any},
78 {"_LeaveSysLevel", UNLOCK, "SysLevel", 0, ret_any},
79 {"USER_Lock", LOCK, "USER_Lock", NO_ARG, ret_any},
80 {"USER_Unlock", UNLOCK, "USER_Lock", NO_ARG, ret_any},
81 {"wine_tsx11_lock", LOCK, "wine_tsx11_lock", NO_ARG, ret_any},
82 {"wine_tsx11_unlock", UNLOCK, "wine_tsx11_lock", NO_ARG, ret_any},
83 {"wine_tsx11_lock_ptr", LOCK, "wine_tsx11_lock_ptr", NO_ARG, ret_any},
84 {"wine_tsx11_unlock_ptr", UNLOCK, "wine_tsx11_lock_ptr", NO_ARG, ret_any},
85 {"wined3d_mutex_lock", LOCK, "wined3d_mutex_lock", NO_ARG, ret_any},
86 {"wined3d_mutex_unlock", UNLOCK, "wined3d_mutex_lock", NO_ARG, ret_any},
87 {"X11DRV_DIB_Lock", LOCK, "X11DRV_DIB_Lock", 0, ret_any},
88 {"X11DRV_DIB_Unlock", UNLOCK, "X11DRV_DIB_Lock", 0, ret_any},
91 static struct lock_info kernel_lock_table[] = {
92 {"lock_kernel", LOCK, "BKL", NO_ARG, ret_any},
93 {"unlock_kernel", UNLOCK, "BKL", NO_ARG, ret_any},
95 {"spin_lock", LOCK, "spin_lock", 0, ret_any},
96 {"spin_unlock", UNLOCK, "spin_lock", 0, ret_any},
97 {"spin_lock_nested", LOCK, "spin_lock", 0, ret_any},
98 {"_spin_lock", LOCK, "spin_lock", 0, ret_any},
99 {"_spin_unlock", UNLOCK, "spin_lock", 0, ret_any},
100 {"_spin_lock_nested", LOCK, "spin_lock", 0, ret_any},
101 {"__spin_lock", LOCK, "spin_lock", 0, ret_any},
102 {"__spin_unlock", UNLOCK, "spin_lock", 0, ret_any},
103 {"__spin_lock_nested", LOCK, "spin_lock", 0, ret_any},
104 {"raw_spin_lock", LOCK, "spin_lock", 0, ret_any},
105 {"raw_spin_unlock", UNLOCK, "spin_lock", 0, ret_any},
106 {"_raw_spin_lock", LOCK, "spin_lock", 0, ret_any},
107 {"_raw_spin_lock_nested", LOCK, "spin_lock", 0, ret_any},
108 {"_raw_spin_unlock", UNLOCK, "spin_lock", 0, ret_any},
109 {"__raw_spin_lock", LOCK, "spin_lock", 0, ret_any},
110 {"__raw_spin_unlock", UNLOCK, "spin_lock", 0, ret_any},
112 {"spin_lock_irq", LOCK, "spin_lock", 0, ret_any},
113 {"spin_unlock_irq", UNLOCK, "spin_lock", 0, ret_any},
114 {"_spin_lock_irq", LOCK, "spin_lock", 0, ret_any},
115 {"_spin_unlock_irq", UNLOCK, "spin_lock", 0, ret_any},
116 {"__spin_lock_irq", LOCK, "spin_lock", 0, ret_any},
117 {"__spin_unlock_irq", UNLOCK, "spin_lock", 0, ret_any},
118 {"_raw_spin_lock_irq", LOCK, "spin_lock", 0, ret_any},
119 {"_raw_spin_unlock_irq", UNLOCK, "spin_lock", 0, ret_any},
120 {"__raw_spin_unlock_irq", UNLOCK, "spin_lock", 0, ret_any},
121 {"spin_lock_irqsave", LOCK, "spin_lock", 0, ret_any},
122 {"spin_unlock_irqrestore", UNLOCK, "spin_lock", 0, ret_any},
123 {"_spin_lock_irqsave", LOCK, "spin_lock", 0, ret_any},
124 {"_spin_unlock_irqrestore", UNLOCK, "spin_lock", 0, ret_any},
125 {"__spin_lock_irqsave", LOCK, "spin_lock", 0, ret_any},
126 {"__spin_unlock_irqrestore", UNLOCK, "spin_lock", 0, ret_any},
127 {"_raw_spin_lock_irqsave", LOCK, "spin_lock", 0, ret_any},
128 {"_raw_spin_unlock_irqrestore", UNLOCK, "spin_lock", 0, ret_any},
129 {"__raw_spin_lock_irqsave", LOCK, "spin_lock", 0, ret_any},
130 {"__raw_spin_unlock_irqrestore", UNLOCK, "spin_lock", 0, ret_any},
131 {"spin_lock_irqsave_nested", LOCK, "spin_lock", 0, ret_any},
132 {"_spin_lock_irqsave_nested", LOCK, "spin_lock", 0, ret_any},
133 {"__spin_lock_irqsave_nested", LOCK, "spin_lock", 0, ret_any},
134 {"_raw_spin_lock_irqsave_nested", LOCK, "spin_lock", 0, ret_any},
135 {"spin_lock_bh", LOCK, "spin_lock", 0, ret_any},
136 {"spin_unlock_bh", UNLOCK, "spin_lock", 0, ret_any},
137 {"_spin_lock_bh", LOCK, "spin_lock", 0, ret_any},
138 {"_spin_unlock_bh", UNLOCK, "spin_lock", 0, ret_any},
139 {"__spin_lock_bh", LOCK, "spin_lock", 0, ret_any},
140 {"__spin_unlock_bh", UNLOCK, "spin_lock", 0, ret_any},
142 {"spin_trylock", LOCK, "spin_lock", 0, ret_non_zero},
143 {"_spin_trylock", LOCK, "spin_lock", 0, ret_non_zero},
144 {"__spin_trylock", LOCK, "spin_lock", 0, ret_non_zero},
145 {"raw_spin_trylock", LOCK, "spin_lock", 0, ret_non_zero},
146 {"_raw_spin_trylock", LOCK, "spin_lock", 0, ret_non_zero},
147 {"spin_trylock_irq", LOCK, "spin_lock", 0, ret_non_zero},
148 {"spin_trylock_irqsave", LOCK, "spin_lock", 0, ret_non_zero},
149 {"spin_trylock_bh", LOCK, "spin_lock", 0, ret_non_zero},
150 {"_spin_trylock_bh", LOCK, "spin_lock", 0, ret_non_zero},
151 {"__spin_trylock_bh", LOCK, "spin_lock", 0, ret_non_zero},
152 {"__raw_spin_trylock", LOCK, "spin_lock", 0, ret_non_zero},
153 {"_atomic_dec_and_lock", LOCK, "spin_lock", 1, ret_non_zero},
155 {"read_lock", LOCK, "read_lock", 0, ret_any},
156 {"read_unlock", UNLOCK, "read_lock", 0, ret_any},
157 {"_read_lock", LOCK, "read_lock", 0, ret_any},
158 {"_read_unlock", UNLOCK, "read_lock", 0, ret_any},
159 {"__read_lock", LOCK, "read_lock", 0, ret_any},
160 {"__read_unlock", UNLOCK, "read_lock", 0, ret_any},
161 {"_raw_read_lock", LOCK, "read_lock", 0, ret_any},
162 {"_raw_read_unlock", UNLOCK, "read_lock", 0, ret_any},
163 {"read_lock_irq", LOCK, "read_lock", 0, ret_any},
164 {"read_unlock_irq" , UNLOCK, "read_lock", 0, ret_any},
165 {"_read_lock_irq", LOCK, "read_lock", 0, ret_any},
166 {"_read_unlock_irq", UNLOCK, "read_lock", 0, ret_any},
167 {"__read_lock_irq", LOCK, "read_lock", 0, ret_any},
168 {"__read_unlock_irq", UNLOCK, "read_lock", 0, ret_any},
169 {"read_lock_irqsave", LOCK, "read_lock", 0, ret_any},
170 {"read_unlock_irqrestore", UNLOCK, "read_lock", 0, ret_any},
171 {"_read_lock_irqsave", LOCK, "read_lock", 0, ret_any},
172 {"_read_unlock_irqrestore", UNLOCK, "read_lock", 0, ret_any},
173 {"__read_lock_irqsave", LOCK, "read_lock", 0, ret_any},
174 {"__read_unlock_irqrestore", UNLOCK, "read_lock", 0, ret_any},
175 {"read_lock_bh", LOCK, "read_lock", 0, ret_any},
176 {"read_unlock_bh", UNLOCK, "read_lock", 0, ret_any},
177 {"_read_lock_bh", LOCK, "read_lock", 0, ret_any},
178 {"_read_unlock_bh", UNLOCK, "read_lock", 0, ret_any},
179 {"__read_lock_bh", LOCK, "read_lock", 0, ret_any},
180 {"__read_unlock_bh", UNLOCK, "read_lock", 0, ret_any},
181 {"_raw_read_lock_bh", LOCK, "read_lock", 0, ret_any},
182 {"_raw_read_unlock_bh", UNLOCK, "read_lock", 0, ret_any},
184 {"generic__raw_read_trylock", LOCK, "read_lock", 0, ret_non_zero},
185 {"read_trylock", LOCK, "read_lock", 0, ret_non_zero},
186 {"_read_trylock", LOCK, "read_lock", 0, ret_non_zero},
187 {"raw_read_trylock", LOCK, "read_lock", 0, ret_non_zero},
188 {"_raw_read_trylock", LOCK, "read_lock", 0, ret_non_zero},
189 {"__raw_read_trylock", LOCK, "read_lock", 0, ret_non_zero},
190 {"__read_trylock", LOCK, "read_lock", 0, ret_non_zero},
192 {"write_lock", LOCK, "write_lock", 0, ret_any},
193 {"write_unlock", UNLOCK, "write_lock", 0, ret_any},
194 {"_write_lock", LOCK, "write_lock", 0, ret_any},
195 {"_write_unlock", UNLOCK, "write_lock", 0, ret_any},
196 {"__write_lock", LOCK, "write_lock", 0, ret_any},
197 {"__write_unlock", UNLOCK, "write_lock", 0, ret_any},
198 {"write_lock_irq", LOCK, "write_lock", 0, ret_any},
199 {"write_unlock_irq", UNLOCK, "write_lock", 0, ret_any},
200 {"_write_lock_irq", LOCK, "write_lock", 0, ret_any},
201 {"_write_unlock_irq", UNLOCK, "write_lock", 0, ret_any},
202 {"__write_lock_irq", LOCK, "write_lock", 0, ret_any},
203 {"__write_unlock_irq", UNLOCK, "write_lock", 0, ret_any},
204 {"write_lock_irqsave", LOCK, "write_lock", 0, ret_any},
205 {"write_unlock_irqrestore", UNLOCK, "write_lock", 0, ret_any},
206 {"_write_lock_irqsave", LOCK, "write_lock", 0, ret_any},
207 {"_write_unlock_irqrestore", UNLOCK, "write_lock", 0, ret_any},
208 {"__write_lock_irqsave", LOCK, "write_lock", 0, ret_any},
209 {"__write_unlock_irqrestore", UNLOCK, "write_lock", 0, ret_any},
210 {"write_lock_bh", LOCK, "write_lock", 0, ret_any},
211 {"write_unlock_bh", UNLOCK, "write_lock", 0, ret_any},
212 {"_write_lock_bh", LOCK, "write_lock", 0, ret_any},
213 {"_write_unlock_bh", UNLOCK, "write_lock", 0, ret_any},
214 {"__write_lock_bh", LOCK, "write_lock", 0, ret_any},
215 {"__write_unlock_bh", UNLOCK, "write_lock", 0, ret_any},
217 {"write_trylock", LOCK, "write_lock", 0, ret_non_zero},
218 {"_write_trylock", LOCK, "write_lock", 0, ret_non_zero},
219 {"raw_write_trylock", LOCK, "write_lock", 0, ret_non_zero},
220 {"_raw_write_trylock", LOCK, "write_lock", 0, ret_non_zero},
221 {"__write_trylock", LOCK, "write_lock", 0, ret_non_zero},
222 {"__raw_write_trylock", LOCK, "write_lock", 0, ret_non_zero},
224 {"down", LOCK, "sem", 0, ret_any},
225 {"up", UNLOCK, "sem", 0, ret_any},
226 {"down_trylock", LOCK, "sem", 0, ret_zero},
227 {"down_interruptible", LOCK, "sem", 0, ret_zero},
229 {"mutex_lock", LOCK, "mutex", 0, ret_any},
230 {"mutex_unlock", UNLOCK, "mutex", 0, ret_any},
231 {"mutex_lock_nested", LOCK, "mutex", 0, ret_any},
233 {"mutex_lock_interruptible", LOCK, "mutex", 0, ret_zero},
234 {"mutex_lock_interruptible_nested", LOCK, "mutex", 0, ret_zero},
235 {"mutex_lock_killable", LOCK, "mutex", 0, ret_zero},
236 {"mutex_lock_killable_nested", LOCK, "mutex", 0, ret_zero},
238 {"mutex_trylock", LOCK, "mutex", 0, ret_non_zero},
240 {"raw_local_irq_disable", LOCK, "irq", NO_ARG, ret_any},
241 {"raw_local_irq_enable", UNLOCK, "irq", NO_ARG, ret_any},
242 {"spin_lock_irq", LOCK, "irq", NO_ARG, ret_any},
243 {"spin_unlock_irq", UNLOCK, "irq", NO_ARG, ret_any},
244 {"_spin_lock_irq", LOCK, "irq", NO_ARG, ret_any},
245 {"_spin_unlock_irq", UNLOCK, "irq", NO_ARG, ret_any},
246 {"__spin_lock_irq", LOCK, "irq", NO_ARG, ret_any},
247 {"__spin_unlock_irq", UNLOCK, "irq", NO_ARG, ret_any},
248 {"_raw_spin_lock_irq", LOCK, "irq", NO_ARG, ret_any},
249 {"_raw_spin_unlock_irq", UNLOCK, "irq", NO_ARG, ret_any},
250 {"__raw_spin_unlock_irq", UNLOCK, "irq", NO_ARG, ret_any},
251 {"spin_trylock_irq", LOCK, "irq", NO_ARG, ret_non_zero},
252 {"read_lock_irq", LOCK, "irq", NO_ARG, ret_any},
253 {"read_unlock_irq", UNLOCK, "irq", NO_ARG, ret_any},
254 {"_read_lock_irq", LOCK, "irq", NO_ARG, ret_any},
255 {"_read_unlock_irq", UNLOCK, "irq", NO_ARG, ret_any},
256 {"__read_lock_irq", LOCK, "irq", NO_ARG, ret_any},
257 {"__read_unlock_irq", UNLOCK, "irq", NO_ARG, ret_any},
258 {"write_lock_irq", LOCK, "irq", NO_ARG, ret_any},
259 {"write_unlock_irq", UNLOCK, "irq", NO_ARG, ret_any},
260 {"_write_lock_irq", LOCK, "irq", NO_ARG, ret_any},
261 {"_write_unlock_irq", UNLOCK, "irq", NO_ARG, ret_any},
262 {"__write_lock_irq", LOCK, "irq", NO_ARG, ret_any},
263 {"__write_unlock_irq", UNLOCK, "irq", NO_ARG, ret_any},
265 {"arch_local_irq_save", LOCK, "irqsave", RETURN_VAL, ret_any},
266 {"arch_local_irq_restore", UNLOCK, "irqsave", 0, ret_any},
267 {"__raw_local_irq_save", LOCK, "irqsave", RETURN_VAL, ret_any},
268 {"raw_local_irq_restore", UNLOCK, "irqsave", 0, ret_any},
269 {"spin_lock_irqsave_nested", LOCK, "irqsave", RETURN_VAL, ret_any},
270 {"spin_lock_irqsave", LOCK, "irqsave", RETURN_VAL, ret_any},
271 {"spin_lock_irqsave", LOCK, "irqsave", 1, ret_any},
272 {"spin_unlock_irqrestore", UNLOCK, "irqsave", 1, ret_any},
273 {"_spin_lock_irqsave_nested", LOCK, "irqsave", RETURN_VAL, ret_any},
274 {"_spin_lock_irqsave", LOCK, "irqsave", RETURN_VAL, ret_any},
275 {"_spin_lock_irqsave", LOCK, "irqsave", 1, ret_any},
276 {"_spin_unlock_irqrestore", UNLOCK, "irqsave", 1, ret_any},
277 {"__spin_lock_irqsave_nested", LOCK, "irqsave", 1, ret_any},
278 {"__spin_lock_irqsave", LOCK, "irqsave", 1, ret_any},
279 {"__spin_unlock_irqrestore", UNLOCK, "irqsave", 1, ret_any},
280 {"_raw_spin_lock_irqsave", LOCK, "irqsave", RETURN_VAL, ret_any},
281 {"_raw_spin_lock_irqsave", LOCK, "irqsave", 1, ret_any},
282 {"_raw_spin_unlock_irqrestore",UNLOCK, "irqsave", 1, ret_any},
283 {"__raw_spin_lock_irqsave", LOCK, "irqsave", RETURN_VAL, ret_any},
284 {"__raw_spin_unlock_irqrestore",UNLOCK, "irqsave", 1, ret_any},
285 {"_raw_spin_lock_irqsave_nested", LOCK, "irqsave", RETURN_VAL, ret_any},
286 {"spin_trylock_irqsave", LOCK, "irqsave", 1, ret_non_zero},
287 {"read_lock_irqsave", LOCK, "irqsave", RETURN_VAL, ret_any},
288 {"read_lock_irqsave", LOCK, "irqsave", 1, ret_any},
289 {"read_unlock_irqrestore", UNLOCK, "irqsave", 1, ret_any},
290 {"_read_lock_irqsave", LOCK, "irqsave", RETURN_VAL, ret_any},
291 {"_read_lock_irqsave", LOCK, "irqsave", 1, ret_any},
292 {"_read_unlock_irqrestore", UNLOCK, "irqsave", 1, ret_any},
293 {"__read_lock_irqsave", LOCK, "irqsave", RETURN_VAL, ret_any},
294 {"__read_unlock_irqrestore", UNLOCK, "irqsave", 1, ret_any},
295 {"write_lock_irqsave", LOCK, "irqsave", RETURN_VAL, ret_any},
296 {"write_lock_irqsave", LOCK, "irqsave", 1, ret_any},
297 {"write_unlock_irqrestore", UNLOCK, "irqsave", 1, ret_any},
298 {"_write_lock_irqsave", LOCK, "irqsave", RETURN_VAL, ret_any},
299 {"_write_lock_irqsave", LOCK, "irqsave", 1, ret_any},
300 {"_write_unlock_irqrestore", UNLOCK, "irqsave", 1, ret_any},
301 {"__write_lock_irqsave", LOCK, "irqsave", RETURN_VAL, ret_any},
302 {"__write_unlock_irqrestore", UNLOCK, "irqsave", 1, ret_any},
304 {"spin_lock_bh", LOCK, "bottom_half", NO_ARG, ret_any},
305 {"spin_unlock_bh", UNLOCK, "bottom_half", NO_ARG, ret_any},
306 {"_spin_lock_bh", LOCK, "bottom_half", NO_ARG, ret_any},
307 {"_spin_unlock_bh", UNLOCK, "bottom_half", NO_ARG, ret_any},
308 {"__spin_lock_bh", LOCK, "bottom_half", NO_ARG, ret_any},
309 {"__spin_unlock_bh", UNLOCK, "bottom_half", NO_ARG, ret_any},
310 {"read_lock_bh", LOCK, "bottom_half", NO_ARG, ret_any},
311 {"read_unlock_bh", UNLOCK, "bottom_half", NO_ARG, ret_any},
312 {"_read_lock_bh", LOCK, "bottom_half", NO_ARG, ret_any},
313 {"_read_unlock_bh", UNLOCK, "bottom_half", NO_ARG, ret_any},
314 {"__read_lock_bh", LOCK, "bottom_half", NO_ARG, ret_any},
315 {"__read_unlock_bh", UNLOCK, "bottom_half", NO_ARG, ret_any},
316 {"_raw_read_lock_bh", LOCK, "bottom_half", NO_ARG, ret_any},
317 {"_raw_read_unlock_bh", UNLOCK, "bottom_half", NO_ARG, ret_any},
318 {"write_lock_bh", LOCK, "bottom_half", NO_ARG, ret_any},
319 {"write_unlock_bh", UNLOCK, "bottom_half", NO_ARG, ret_any},
320 {"_write_lock_bh", LOCK, "bottom_half", NO_ARG, ret_any},
321 {"_write_unlock_bh", UNLOCK, "bottom_half", NO_ARG, ret_any},
322 {"__write_lock_bh", LOCK, "bottom_half", NO_ARG, ret_any},
323 {"__write_unlock_bh", UNLOCK, "bottom_half", NO_ARG, ret_any},
324 {"spin_trylock_bh", LOCK, "bottom_half", NO_ARG, ret_non_zero},
325 {"_spin_trylock_bh", LOCK, "bottom_half", NO_ARG, ret_non_zero},
326 {"__spin_trylock_bh", LOCK, "bottom_half", NO_ARG, ret_non_zero},
329 static struct lock_info *lock_table;
331 static struct tracker_list *starts_locked;
332 static struct tracker_list *starts_unlocked;
334 struct locks_on_return {
335 int line;
336 struct tracker_list *locked;
337 struct tracker_list *unlocked;
339 DECLARE_PTR_LIST(return_list, struct locks_on_return);
340 static struct return_list *all_returns;
342 static char *make_full_name(const char *lock, const char *var)
344 static char tmp_buf[512];
346 snprintf(tmp_buf, 512, "%s:%s", lock, var);
347 tmp_buf[511] = '\0';
348 return alloc_string(tmp_buf);
351 static struct expression *remove_spinlock_check(struct expression *expr)
353 if (expr->type != EXPR_CALL)
354 return expr;
355 if (expr->fn->type != EXPR_SYMBOL)
356 return expr;
357 if (strcmp(expr->fn->symbol_name->name, "spinlock_check"))
358 return expr;
359 expr = get_argument_from_call_expr(expr->args, 0);
360 return expr;
363 static char *get_full_name(struct expression *expr, int index)
365 struct expression *arg;
366 char *name = NULL;
367 char *full_name = NULL;
368 struct lock_info *lock = &lock_table[index];
370 if (lock->arg == RETURN_VAL) {
371 name = get_variable_from_expr(expr->left, NULL);
372 full_name = make_full_name(lock->name, name);
373 } else if (lock->arg == NO_ARG) {
374 full_name = make_full_name(lock->name, "");
375 } else {
376 arg = get_argument_from_call_expr(expr->args, lock->arg);
377 if (!arg)
378 goto free;
379 arg = remove_spinlock_check(arg);
380 name = get_variable_from_expr_complex(arg, NULL);
381 if (!name)
382 goto free;
383 full_name = make_full_name(lock->name, name);
385 free:
386 free_string(name);
387 return full_name;
390 static struct smatch_state *get_start_state(struct sm_state *sm)
392 int is_locked = 0;
393 int is_unlocked = 0;
395 if (in_tracker_list(starts_locked, my_id, sm->name, sm->sym))
396 is_locked = 1;
397 if (in_tracker_list(starts_unlocked, my_id, sm->name, sm->sym))
398 is_unlocked = 1;
399 if (is_locked && is_unlocked)
400 return &undefined;
401 if (is_locked)
402 return &locked;
403 if (is_unlocked)
404 return &unlocked;
405 return &undefined;
408 static struct smatch_state *unmatched_state(struct sm_state *sm)
410 return &start_state;
413 static void do_lock(const char *name)
415 struct sm_state *sm;
417 sm = get_sm_state(my_id, name, NULL);
418 if (!sm)
419 add_tracker(&starts_unlocked, my_id, name, NULL);
420 if (sm && slist_has_state(sm->possible, &locked))
421 sm_msg("error: double lock '%s'", name);
422 if (sm)
423 func_has_transition = TRUE;
424 set_state(my_id, name, NULL, &locked);
427 static void do_lock_failed(const char *name)
429 struct sm_state *sm;
431 sm = get_sm_state(my_id, name, NULL);
432 if (!sm)
433 add_tracker(&starts_unlocked, my_id, name, NULL);
434 set_state(my_id, name, NULL, &unlocked);
437 static void do_unlock(const char *name)
439 struct sm_state *sm;
441 if (__path_is_null())
442 return;
443 sm = get_sm_state(my_id, name, NULL);
444 if (!sm)
445 add_tracker(&starts_locked, my_id, name, NULL);
446 if (sm && slist_has_state(sm->possible, &unlocked))
447 sm_msg("error: double unlock '%s'", name);
448 if (sm)
449 func_has_transition = TRUE;
450 set_state(my_id, name, NULL, &unlocked);
453 static void match_lock_held(const char *fn, struct expression *call_expr,
454 struct expression *assign_expr, void *_index)
456 int index = PTR_INT(_index);
457 char *lock_name;
458 struct lock_info *lock = &lock_table[index];
460 if (lock->arg == NO_ARG) {
461 lock_name = get_full_name(NULL, index);
462 } else if (lock->arg == RETURN_VAL) {
463 if (!assign_expr)
464 return;
465 lock_name = get_full_name(assign_expr, index);
466 } else {
467 lock_name = get_full_name(call_expr, index);
469 if (!lock_name)
470 return;
471 do_lock(lock_name);
472 free_string(lock_name);
475 static void match_lock_failed(const char *fn, struct expression *call_expr,
476 struct expression *assign_expr, void *_index)
478 int index = PTR_INT(_index);
479 char *lock_name;
480 struct lock_info *lock = &lock_table[index];
482 if (lock->arg == NO_ARG) {
483 lock_name = get_full_name(NULL, index);
484 } else if (lock->arg == RETURN_VAL) {
485 if (!assign_expr)
486 return;
487 lock_name = get_full_name(assign_expr, index);
488 } else {
489 lock_name = get_full_name(call_expr, index);
491 if (!lock_name)
492 return;
493 do_lock_failed(lock_name);
494 free_string(lock_name);
497 static void match_returns_locked(const char *fn, struct expression *expr,
498 void *_index)
500 char *full_name = NULL;
501 int index = PTR_INT(_index);
502 struct lock_info *lock = &lock_table[index];
504 if (lock->arg != RETURN_VAL)
505 return;
506 full_name = get_full_name(expr, index);
507 do_lock(full_name);
510 static void match_lock_unlock(const char *fn, struct expression *expr, void *_index)
512 char *full_name = NULL;
513 int index = PTR_INT(_index);
514 struct lock_info *lock = &lock_table[index];
516 full_name = get_full_name(expr, index);
517 if (!full_name)
518 return;
519 if (lock->action == LOCK)
520 do_lock(full_name);
521 else
522 do_unlock(full_name);
523 free_string(full_name);
526 static struct locks_on_return *alloc_return(int line)
528 struct locks_on_return *ret;
530 ret = malloc(sizeof(*ret));
531 ret->line = line;
532 ret->locked = NULL;
533 ret->unlocked = NULL;
534 return ret;
537 static void check_possible(struct sm_state *sm)
539 struct sm_state *tmp;
540 int islocked = 0;
541 int isunlocked = 0;
542 int undef = 0;
544 if (!option_spammy)
545 return;
547 FOR_EACH_PTR(sm->possible, tmp) {
548 if (tmp->state == &locked)
549 islocked = 1;
550 if (tmp->state == &unlocked)
551 isunlocked = 1;
552 if (tmp->state == &start_state) {
553 struct smatch_state *s;
555 s = get_start_state(tmp);
556 if (s == &locked)
557 islocked = 1;
558 else if (s == &unlocked)
559 isunlocked = 1;
560 else
561 undef = 1;
563 if (tmp->state == &undefined)
564 undef = 1; // i don't think this is possible any more.
565 } END_FOR_EACH_PTR(tmp);
566 if ((islocked && isunlocked) || undef)
567 sm_msg("warn: '%s' is sometimes locked here and sometimes unlocked.", sm->name);
570 static void match_return(struct expression *ret_value)
572 struct locks_on_return *ret;
573 struct state_list *slist;
574 struct sm_state *tmp;
576 if (!final_pass)
577 return;
579 ret = alloc_return(get_lineno());
581 slist = get_all_states(my_id);
582 FOR_EACH_PTR(slist, tmp) {
583 if (tmp->state == &locked) {
584 add_tracker(&ret->locked, tmp->owner, tmp->name,
585 tmp->sym);
586 } else if (tmp->state == &unlocked) {
587 add_tracker(&ret->unlocked, tmp->owner, tmp->name,
588 tmp->sym);
589 } else if (tmp->state == &start_state) {
590 struct smatch_state *s;
592 s = get_start_state(tmp);
593 if (s == &locked)
594 add_tracker(&ret->locked, tmp->owner, tmp->name,
595 tmp->sym);
596 if (s == &unlocked)
597 add_tracker(&ret->unlocked, tmp->owner,tmp->name,
598 tmp->sym);
599 }else {
600 check_possible(tmp);
602 } END_FOR_EACH_PTR(tmp);
603 free_slist(&slist);
604 add_ptr_list(&all_returns, ret);
607 static void print_inconsistent_returns(struct tracker *lock,
608 struct smatch_state *start)
610 struct locks_on_return *tmp;
611 int i;
613 sm_prefix();
614 sm_printf("warn: inconsistent returns %s:", lock->name);
615 sm_printf(" locked (");
616 i = 0;
617 FOR_EACH_PTR(all_returns, tmp) {
618 if (in_tracker_list(tmp->unlocked, lock->owner, lock->name, lock->sym))
619 continue;
620 if (in_tracker_list(tmp->locked, lock->owner, lock->name, lock->sym)) {
621 if (i++)
622 sm_printf(",");
623 sm_printf("%d", tmp->line);
624 continue;
626 if (start == &locked) {
627 if (i++)
628 sm_printf(",");
629 sm_printf("%d", tmp->line);
631 } END_FOR_EACH_PTR(tmp);
633 sm_printf(") unlocked (");
634 i = 0;
635 FOR_EACH_PTR(all_returns, tmp) {
636 if (in_tracker_list(tmp->unlocked, lock->owner, lock->name, lock->sym)) {
637 if (i++)
638 sm_printf(",");
639 sm_printf("%d", tmp->line);
640 continue;
642 if (in_tracker_list(tmp->locked, lock->owner, lock->name, lock->sym)) {
643 continue;
645 if (start == &unlocked) {
646 if (i++)
647 sm_printf(",");
648 sm_printf("%d", tmp->line);
650 } END_FOR_EACH_PTR(tmp);
651 sm_printf(")\n");
654 static void check_returns_consistently(struct tracker *lock,
655 struct smatch_state *start)
657 int returns_locked = 0;
658 int returns_unlocked = 0;
659 struct locks_on_return *tmp;
661 FOR_EACH_PTR(all_returns, tmp) {
662 if (in_tracker_list(tmp->unlocked, lock->owner, lock->name,
663 lock->sym))
664 returns_unlocked = tmp->line;
665 else if (in_tracker_list(tmp->locked, lock->owner, lock->name,
666 lock->sym))
667 returns_locked = tmp->line;
668 else if (start == &locked)
669 returns_locked = tmp->line;
670 else if (start == &unlocked)
671 returns_unlocked = tmp->line;
672 } END_FOR_EACH_PTR(tmp);
674 if (returns_locked && returns_unlocked)
675 print_inconsistent_returns(lock, start);
678 static void check_consistency(struct symbol *sym)
680 struct tracker *tmp;
682 if (is_reachable())
683 match_return(NULL);
685 FOR_EACH_PTR(starts_locked, tmp) {
686 if (in_tracker_list(starts_unlocked, tmp->owner, tmp->name,
687 tmp->sym))
688 sm_msg("error: locking inconsistency. We assume "
689 "'%s' is both locked and unlocked at the "
690 "start.",
691 tmp->name);
692 } END_FOR_EACH_PTR(tmp);
694 FOR_EACH_PTR(starts_locked, tmp) {
695 check_returns_consistently(tmp, &locked);
696 } END_FOR_EACH_PTR(tmp);
698 FOR_EACH_PTR(starts_unlocked, tmp) {
699 check_returns_consistently(tmp, &unlocked);
700 } END_FOR_EACH_PTR(tmp);
703 static void clear_lists(void)
705 struct locks_on_return *tmp;
707 func_has_transition = FALSE;
709 free_trackers_and_list(&starts_locked);
710 free_trackers_and_list(&starts_unlocked);
712 FOR_EACH_PTR(all_returns, tmp) {
713 free_trackers_and_list(&tmp->locked);
714 free_trackers_and_list(&tmp->unlocked);
715 free(tmp);
716 } END_FOR_EACH_PTR(tmp);
717 __free_ptr_list((struct ptr_list **)&all_returns);
720 static void match_func_end(struct symbol *sym)
722 if (func_has_transition)
723 check_consistency(sym);
724 clear_lists();
727 static void register_lock(int index)
729 struct lock_info *lock = &lock_table[index];
730 void *idx = INT_PTR(index);
732 if (lock->return_type == ret_non_zero) {
733 return_implies_state(lock->function, valid_ptr_min, valid_ptr_max, &match_lock_held, idx);
734 return_implies_state(lock->function, 0, 0, &match_lock_failed, idx);
735 } else if (lock->return_type == ret_any && lock->arg == RETURN_VAL) {
736 add_function_assign_hook(lock->function, &match_returns_locked, idx);
737 } else if (lock->return_type == ret_any) {
738 add_function_hook(lock->function, &match_lock_unlock, idx);
739 } else if (lock->return_type == ret_zero) {
740 return_implies_state(lock->function, 0, 0, &match_lock_held, idx);
741 return_implies_state(lock->function, whole_range.min, -1, &match_lock_failed, idx);
745 static void load_table(struct lock_info *_lock_table, int size)
747 int i;
749 lock_table = _lock_table;
751 for (i = 0; i < size; i++) {
752 if (lock_table[i].action == LOCK)
753 register_lock(i);
754 else
755 add_function_hook(lock_table[i].function, &match_lock_unlock, INT_PTR(i));
759 /* print_held_locks() is used in check_call_tree.c */
760 void print_held_locks()
762 struct state_list *slist;
763 struct sm_state *sm;
764 int i = 0;
766 slist = get_all_states(my_id);
767 FOR_EACH_PTR(slist, sm) {
768 if (sm->state != &locked)
769 continue;
770 if (i++)
771 sm_printf(" ");
772 sm_printf("'%s'", sm->name);
773 } END_FOR_EACH_PTR(sm);
774 free_slist(&slist);
777 void check_locking(int id)
779 my_id = id;
781 if (option_project == PROJ_WINE)
782 load_table(wine_lock_table, ARRAY_SIZE(wine_lock_table));
783 else if (option_project == PROJ_KERNEL)
784 load_table(kernel_lock_table, ARRAY_SIZE(kernel_lock_table));
785 else
786 return;
788 add_unmatched_state_hook(my_id, &unmatched_state);
789 add_hook(&match_return, RETURN_HOOK);
790 add_hook(&match_func_end, END_FUNC_HOOK);