get_variable_from_expr_complex(): return proper symbol for array expressions
[smatch.git] / check_locking.c
blob48eae189d13d7d90e25eb9a1229a58aedfbe54fd
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 STATE(locked);
28 STATE(start_state);
29 STATE(unlocked);
31 enum action {
32 LOCK,
33 UNLOCK,
36 enum return_type {
37 ret_any,
38 ret_non_zero,
39 ret_zero,
42 #define RETURN_VAL -1
43 #define NO_ARG -2
45 struct lock_info {
46 const char *function;
47 enum action action;
48 const char *name;
49 int arg;
50 enum return_type return_type;
53 static struct lock_info wine_lock_table[] = {
54 {"create_window_handle", LOCK, "create_window_handle", RETURN_VAL, ret_non_zero},
55 {"WIN_GetPtr", LOCK, "create_window_handle", RETURN_VAL, ret_non_zero},
56 {"WIN_ReleasePtr", UNLOCK, "create_window_handle", 0, ret_any},
57 {"EnterCriticalSection", LOCK, "CriticalSection", 0, ret_any},
58 {"LeaveCriticalSection", UNLOCK, "CriticalSection", 0, ret_any},
59 {"RtlEnterCriticalSection", LOCK, "RtlCriticalSection", 0, ret_any},
60 {"RtlLeaveCriticalSection", UNLOCK, "RtlCriticalSection", 0, ret_any},
61 {"GDI_GetObjPtr", LOCK, "GDI_Get", 0, ret_non_zero},
62 {"GDI_ReleaseObj", UNLOCK, "GDI_Get", 0, ret_any},
63 {"LdrLockLoaderLock", LOCK, "LdrLockLoaderLock", 2, ret_any},
64 {"LdrUnlockLoaderLock", UNLOCK, "LdrLockLoaderLock", 1, ret_any},
65 {"_lock", LOCK, "_lock", 0, ret_any},
66 {"_unlock", UNLOCK, "_lock", 0, ret_any},
67 {"msiobj_lock", LOCK, "msiobj_lock", 0, ret_any},
68 {"msiobj_unlock", UNLOCK, "msiobj_lock", 0, ret_any},
69 {"RtlAcquirePebLock", LOCK, "PebLock", NO_ARG, ret_any},
70 {"RtlReleasePebLock", UNLOCK, "PebLock", NO_ARG, ret_any},
71 {"server_enter_uninterrupted_section", LOCK, "server_uninterrupted_section", 0, ret_any},
72 {"server_leave_uninterrupted_section", UNLOCK, "server_uninterrupted_section", 0, ret_any},
73 {"RtlLockHeap", LOCK, "RtlLockHeap", 0, ret_any},
74 {"RtlUnlockHeap", UNLOCK, "RtlLockHeap", 0, ret_any},
75 {"_EnterSysLevel", LOCK, "SysLevel", 0, ret_any},
76 {"_LeaveSysLevel", UNLOCK, "SysLevel", 0, ret_any},
77 {"USER_Lock", LOCK, "USER_Lock", NO_ARG, ret_any},
78 {"USER_Unlock", UNLOCK, "USER_Lock", NO_ARG, ret_any},
79 {"wine_tsx11_lock", LOCK, "wine_tsx11_lock", NO_ARG, ret_any},
80 {"wine_tsx11_unlock", UNLOCK, "wine_tsx11_lock", NO_ARG, ret_any},
81 {"wine_tsx11_lock_ptr", LOCK, "wine_tsx11_lock_ptr", NO_ARG, ret_any},
82 {"wine_tsx11_unlock_ptr", UNLOCK, "wine_tsx11_lock_ptr", NO_ARG, ret_any},
83 {"wined3d_mutex_lock", LOCK, "wined3d_mutex_lock", NO_ARG, ret_any},
84 {"wined3d_mutex_unlock", UNLOCK, "wined3d_mutex_lock", NO_ARG, ret_any},
85 {"X11DRV_DIB_Lock", LOCK, "X11DRV_DIB_Lock", 0, ret_any},
86 {"X11DRV_DIB_Unlock", UNLOCK, "X11DRV_DIB_Lock", 0, ret_any},
89 static struct lock_info kernel_lock_table[] = {
90 {"lock_kernel", LOCK, "BKL", NO_ARG, ret_any},
91 {"unlock_kernel", UNLOCK, "BKL", NO_ARG, ret_any},
93 {"spin_lock", LOCK, "spin_lock", 0, ret_any},
94 {"spin_unlock", UNLOCK, "spin_lock", 0, ret_any},
95 {"spin_lock_nested", LOCK, "spin_lock", 0, ret_any},
96 {"_spin_lock", LOCK, "spin_lock", 0, ret_any},
97 {"_spin_unlock", UNLOCK, "spin_lock", 0, ret_any},
98 {"_spin_lock_nested", LOCK, "spin_lock", 0, ret_any},
99 {"__spin_lock", LOCK, "spin_lock", 0, ret_any},
100 {"__spin_unlock", UNLOCK, "spin_lock", 0, ret_any},
101 {"__spin_lock_nested", LOCK, "spin_lock", 0, ret_any},
102 {"raw_spin_lock", LOCK, "spin_lock", 0, ret_any},
103 {"raw_spin_unlock", UNLOCK, "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_unlock", UNLOCK, "spin_lock", 0, ret_any},
109 {"spin_lock_irq", LOCK, "spin_lock", 0, ret_any},
110 {"spin_unlock_irq", UNLOCK, "spin_lock", 0, ret_any},
111 {"_spin_lock_irq", LOCK, "spin_lock", 0, ret_any},
112 {"_spin_unlock_irq", UNLOCK, "spin_lock", 0, ret_any},
113 {"__spin_lock_irq", LOCK, "spin_lock", 0, ret_any},
114 {"__spin_unlock_irq", UNLOCK, "spin_lock", 0, ret_any},
115 {"_raw_spin_lock_irq", LOCK, "spin_lock", 0, ret_any},
116 {"_raw_spin_unlock_irq", UNLOCK, "spin_lock", 0, ret_any},
117 {"spin_lock_irqsave", LOCK, "spin_lock", 0, ret_any},
118 {"spin_unlock_irqrestore", UNLOCK, "spin_lock", 0, ret_any},
119 {"_spin_lock_irqsave", LOCK, "spin_lock", 0, ret_any},
120 {"_spin_unlock_irqrestore", 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 {"_raw_spin_lock_irqsave", LOCK, "spin_lock", 0, ret_any},
124 {"_raw_spin_unlock_irqrestore", UNLOCK, "spin_lock", 0, ret_any},
125 {"__raw_spin_lock_irqsave", LOCK, "spin_lock", 0, ret_any},
126 {"__raw_spin_unlock_irqrestore", UNLOCK, "spin_lock", 0, ret_any},
127 {"spin_lock_irqsave_nested", LOCK, "spin_lock", 0, ret_any},
128 {"_spin_lock_irqsave_nested", LOCK, "spin_lock", 0, ret_any},
129 {"__spin_lock_irqsave_nested", LOCK, "spin_lock", 0, ret_any},
130 {"_raw_spin_lock_irqsave_nested", LOCK, "spin_lock", 0, ret_any},
131 {"spin_lock_bh", LOCK, "spin_lock", 0, ret_any},
132 {"spin_unlock_bh", UNLOCK, "spin_lock", 0, ret_any},
133 {"_spin_lock_bh", LOCK, "spin_lock", 0, ret_any},
134 {"_spin_unlock_bh", UNLOCK, "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},
138 {"spin_trylock", LOCK, "spin_lock", 0, ret_non_zero},
139 {"_spin_trylock", LOCK, "spin_lock", 0, ret_non_zero},
140 {"__spin_trylock", LOCK, "spin_lock", 0, ret_non_zero},
141 {"raw_spin_trylock", LOCK, "spin_lock", 0, ret_non_zero},
142 {"_raw_spin_trylock", LOCK, "spin_lock", 0, ret_non_zero},
143 {"spin_trylock_irq", LOCK, "spin_lock", 0, ret_non_zero},
144 {"spin_trylock_irqsave", LOCK, "spin_lock", 0, ret_non_zero},
145 {"spin_trylock_bh", LOCK, "spin_lock", 0, ret_non_zero},
146 {"_spin_trylock_bh", LOCK, "spin_lock", 0, ret_non_zero},
147 {"__spin_trylock_bh", LOCK, "spin_lock", 0, ret_non_zero},
148 {"__raw_spin_trylock", LOCK, "spin_lock", 0, ret_non_zero},
150 {"read_lock", LOCK, "read_lock", 0, ret_any},
151 {"read_unlock", UNLOCK, "read_lock", 0, ret_any},
152 {"_read_lock", LOCK, "read_lock", 0, ret_any},
153 {"_read_unlock", UNLOCK, "read_lock", 0, ret_any},
154 {"__read_lock", LOCK, "read_lock", 0, ret_any},
155 {"__read_unlock", UNLOCK, "read_lock", 0, ret_any},
156 {"read_lock_irq", LOCK, "read_lock", 0, ret_any},
157 {"read_unlock_irq" , UNLOCK, "read_lock", 0, ret_any},
158 {"_read_lock_irq", LOCK, "read_lock", 0, ret_any},
159 {"_read_unlock_irq", UNLOCK, "read_lock", 0, ret_any},
160 {"__read_lock_irq", LOCK, "read_lock", 0, ret_any},
161 {"__read_unlock_irq", UNLOCK, "read_lock", 0, ret_any},
162 {"read_lock_irqsave", LOCK, "read_lock", 0, ret_any},
163 {"read_unlock_irqrestore", UNLOCK, "read_lock", 0, ret_any},
164 {"_read_lock_irqsave", LOCK, "read_lock", 0, ret_any},
165 {"_read_unlock_irqrestore", UNLOCK, "read_lock", 0, ret_any},
166 {"__read_lock_irqsave", LOCK, "read_lock", 0, ret_any},
167 {"__read_unlock_irqrestore", UNLOCK, "read_lock", 0, ret_any},
168 {"read_lock_bh", LOCK, "read_lock", 0, ret_any},
169 {"read_unlock_bh", UNLOCK, "read_lock", 0, ret_any},
170 {"_read_lock_bh", LOCK, "read_lock", 0, ret_any},
171 {"_read_unlock_bh", UNLOCK, "read_lock", 0, ret_any},
172 {"__read_lock_bh", LOCK, "read_lock", 0, ret_any},
173 {"__read_unlock_bh", UNLOCK, "read_lock", 0, ret_any},
175 {"generic__raw_read_trylock", LOCK, "read_lock", 0, ret_non_zero},
176 {"read_trylock", LOCK, "read_lock", 0, ret_non_zero},
177 {"_read_trylock", LOCK, "read_lock", 0, ret_non_zero},
178 {"raw_read_trylock", LOCK, "read_lock", 0, ret_non_zero},
179 {"_raw_read_trylock", LOCK, "read_lock", 0, ret_non_zero},
180 {"__raw_read_trylock", LOCK, "read_lock", 0, ret_non_zero},
181 {"__read_trylock", LOCK, "read_lock", 0, ret_non_zero},
183 {"write_lock", LOCK, "write_lock", 0, ret_any},
184 {"write_unlock", UNLOCK, "write_lock", 0, ret_any},
185 {"_write_lock", LOCK, "write_lock", 0, ret_any},
186 {"_write_unlock", UNLOCK, "write_lock", 0, ret_any},
187 {"__write_lock", LOCK, "write_lock", 0, ret_any},
188 {"__write_unlock", UNLOCK, "write_lock", 0, ret_any},
189 {"write_lock_irq", LOCK, "write_lock", 0, ret_any},
190 {"write_unlock_irq", UNLOCK, "write_lock", 0, ret_any},
191 {"_write_lock_irq", LOCK, "write_lock", 0, ret_any},
192 {"_write_unlock_irq", UNLOCK, "write_lock", 0, ret_any},
193 {"__write_lock_irq", LOCK, "write_lock", 0, ret_any},
194 {"__write_unlock_irq", UNLOCK, "write_lock", 0, ret_any},
195 {"write_lock_irqsave", LOCK, "write_lock", 0, ret_any},
196 {"write_unlock_irqrestore", UNLOCK, "write_lock", 0, ret_any},
197 {"_write_lock_irqsave", LOCK, "write_lock", 0, ret_any},
198 {"_write_unlock_irqrestore", UNLOCK, "write_lock", 0, ret_any},
199 {"__write_lock_irqsave", LOCK, "write_lock", 0, ret_any},
200 {"__write_unlock_irqrestore", UNLOCK, "write_lock", 0, ret_any},
201 {"write_lock_bh", LOCK, "write_lock", 0, ret_any},
202 {"write_unlock_bh", UNLOCK, "write_lock", 0, ret_any},
203 {"_write_lock_bh", LOCK, "write_lock", 0, ret_any},
204 {"_write_unlock_bh", UNLOCK, "write_lock", 0, ret_any},
205 {"__write_lock_bh", LOCK, "write_lock", 0, ret_any},
206 {"__write_unlock_bh", UNLOCK, "write_lock", 0, ret_any},
208 {"write_trylock", LOCK, "write_lock", 0, ret_non_zero},
209 {"_write_trylock", LOCK, "write_lock", 0, ret_non_zero},
210 {"raw_write_trylock", LOCK, "write_lock", 0, ret_non_zero},
211 {"_raw_write_trylock", LOCK, "write_lock", 0, ret_non_zero},
212 {"__write_trylock", LOCK, "write_lock", 0, ret_non_zero},
213 {"__raw_write_trylock", LOCK, "write_lock", 0, ret_non_zero},
215 {"down", LOCK, "sem", 0, ret_any},
216 {"up", UNLOCK, "sem", 0, ret_any},
217 {"down_trylock", LOCK, "sem", 0, ret_zero},
218 {"down_interruptible", LOCK, "sem", 0, ret_zero},
220 {"mutex_lock", LOCK, "mutex", 0, ret_any},
221 {"mutex_unlock", UNLOCK, "mutex", 0, ret_any},
222 {"mutex_lock_nested", LOCK, "mutex", 0, ret_any},
224 {"mutex_lock_interruptible", LOCK, "mutex", 0, ret_zero},
225 {"mutex_lock_interruptible_nested", LOCK, "mutex", 0, ret_zero},
226 {"mutex_lock_killable", LOCK, "mutex", 0, ret_zero},
227 {"mutex_lock_killable_nested", LOCK, "mutex", 0, ret_zero},
229 {"mutex_trylock", LOCK, "mutex", 0, ret_non_zero},
231 {"raw_local_irq_disable", LOCK, "irq", NO_ARG, ret_any},
232 {"raw_local_irq_enable", UNLOCK, "irq", NO_ARG, ret_any},
233 {"spin_lock_irq", LOCK, "irq", NO_ARG, ret_any},
234 {"spin_unlock_irq", UNLOCK, "irq", NO_ARG, ret_any},
235 {"_spin_lock_irq", LOCK, "irq", NO_ARG, ret_any},
236 {"_spin_unlock_irq", UNLOCK, "irq", NO_ARG, ret_any},
237 {"__spin_lock_irq", LOCK, "irq", NO_ARG, ret_any},
238 {"__spin_unlock_irq", UNLOCK, "irq", NO_ARG, ret_any},
239 {"_raw_spin_lock_irq", LOCK, "irq", NO_ARG, ret_any},
240 {"_raw_spin_unlock_irq", UNLOCK, "irq", NO_ARG, ret_any},
241 {"spin_trylock_irq", LOCK, "irq", NO_ARG, ret_non_zero},
242 {"read_lock_irq", LOCK, "irq", NO_ARG, ret_any},
243 {"read_unlock_irq", UNLOCK, "irq", NO_ARG, ret_any},
244 {"_read_lock_irq", LOCK, "irq", NO_ARG, ret_any},
245 {"_read_unlock_irq", UNLOCK, "irq", NO_ARG, ret_any},
246 {"__read_lock_irq", LOCK, "irq", NO_ARG, ret_any},
247 {"__read_unlock_irq", UNLOCK, "irq", NO_ARG, ret_any},
248 {"write_lock_irq", LOCK, "irq", NO_ARG, ret_any},
249 {"write_unlock_irq", UNLOCK, "irq", NO_ARG, ret_any},
250 {"_write_lock_irq", LOCK, "irq", NO_ARG, ret_any},
251 {"_write_unlock_irq", UNLOCK, "irq", NO_ARG, ret_any},
252 {"__write_lock_irq", LOCK, "irq", NO_ARG, ret_any},
253 {"__write_unlock_irq", UNLOCK, "irq", NO_ARG, ret_any},
255 {"__raw_local_irq_save", LOCK, "irqsave", RETURN_VAL, ret_any},
256 {"raw_local_irq_restore", UNLOCK, "irqsave", 0, ret_any},
257 {"spin_lock_irqsave_nested", LOCK, "irqsave", RETURN_VAL, ret_any},
258 {"spin_lock_irqsave", LOCK, "irqsave", RETURN_VAL, ret_any},
259 {"spin_lock_irqsave", LOCK, "irqsave", 1, ret_any},
260 {"spin_unlock_irqrestore", UNLOCK, "irqsave", 1, ret_any},
261 {"_spin_lock_irqsave_nested", LOCK, "irqsave", RETURN_VAL, ret_any},
262 {"_spin_lock_irqsave", LOCK, "irqsave", RETURN_VAL, ret_any},
263 {"_spin_lock_irqsave", LOCK, "irqsave", 1, ret_any},
264 {"_spin_unlock_irqrestore", UNLOCK, "irqsave", 1, ret_any},
265 {"__spin_lock_irqsave_nested", LOCK, "irqsave", 1, ret_any},
266 {"__spin_lock_irqsave", LOCK, "irqsave", 1, ret_any},
267 {"__spin_unlock_irqrestore", UNLOCK, "irqsave", 1, ret_any},
268 {"_raw_spin_lock_irqsave", LOCK, "irqsave", RETURN_VAL, ret_any},
269 {"_raw_spin_lock_irqsave", LOCK, "irqsave", 1, ret_any},
270 {"_raw_spin_unlock_irqrestore",UNLOCK, "irqsave", 1, ret_any},
271 {"__raw_spin_lock_irqsave", LOCK, "irqsave", RETURN_VAL, ret_any},
272 {"__raw_spin_unlock_irqrestore",UNLOCK, "irqsave", 1, ret_any},
273 {"_raw_spin_lock_irqsave_nested", LOCK, "irqsave", RETURN_VAL, ret_any},
274 {"spin_trylock_irqsave", LOCK, "irqsave", 1, ret_non_zero},
275 {"read_lock_irqsave", LOCK, "irqsave", RETURN_VAL, ret_any},
276 {"read_lock_irqsave", LOCK, "irqsave", 1, ret_any},
277 {"read_unlock_irqrestore", UNLOCK, "irqsave", 1, ret_any},
278 {"_read_lock_irqsave", LOCK, "irqsave", RETURN_VAL, ret_any},
279 {"_read_lock_irqsave", LOCK, "irqsave", 1, ret_any},
280 {"_read_unlock_irqrestore", UNLOCK, "irqsave", 1, ret_any},
281 {"__read_lock_irqsave", LOCK, "irqsave", RETURN_VAL, ret_any},
282 {"__read_unlock_irqrestore", UNLOCK, "irqsave", 1, ret_any},
283 {"write_lock_irqsave", LOCK, "irqsave", RETURN_VAL, ret_any},
284 {"write_lock_irqsave", LOCK, "irqsave", 1, ret_any},
285 {"write_unlock_irqrestore", UNLOCK, "irqsave", 1, ret_any},
286 {"_write_lock_irqsave", LOCK, "irqsave", RETURN_VAL, ret_any},
287 {"_write_lock_irqsave", LOCK, "irqsave", 1, ret_any},
288 {"_write_unlock_irqrestore", UNLOCK, "irqsave", 1, ret_any},
289 {"__write_lock_irqsave", LOCK, "irqsave", RETURN_VAL, ret_any},
290 {"__write_unlock_irqrestore", UNLOCK, "irqsave", 1, ret_any},
292 {"spin_lock_bh", LOCK, "bottom_half", NO_ARG, ret_any},
293 {"spin_unlock_bh", UNLOCK, "bottom_half", NO_ARG, ret_any},
294 {"_spin_lock_bh", LOCK, "bottom_half", NO_ARG, ret_any},
295 {"_spin_unlock_bh", UNLOCK, "bottom_half", NO_ARG, ret_any},
296 {"__spin_lock_bh", LOCK, "bottom_half", NO_ARG, ret_any},
297 {"__spin_unlock_bh", UNLOCK, "bottom_half", NO_ARG, ret_any},
298 {"read_lock_bh", LOCK, "bottom_half", NO_ARG, ret_any},
299 {"read_unlock_bh", UNLOCK, "bottom_half", NO_ARG, ret_any},
300 {"_read_lock_bh", LOCK, "bottom_half", NO_ARG, ret_any},
301 {"_read_unlock_bh", UNLOCK, "bottom_half", NO_ARG, ret_any},
302 {"__read_lock_bh", LOCK, "bottom_half", NO_ARG, ret_any},
303 {"__read_unlock_bh", UNLOCK, "bottom_half", NO_ARG, ret_any},
304 {"write_lock_bh", LOCK, "bottom_half", NO_ARG, ret_any},
305 {"write_unlock_bh", UNLOCK, "bottom_half", NO_ARG, ret_any},
306 {"_write_lock_bh", LOCK, "bottom_half", NO_ARG, ret_any},
307 {"_write_unlock_bh", UNLOCK, "bottom_half", NO_ARG, ret_any},
308 {"__write_lock_bh", LOCK, "bottom_half", NO_ARG, ret_any},
309 {"__write_unlock_bh", UNLOCK, "bottom_half", NO_ARG, ret_any},
310 {"spin_trylock_bh", LOCK, "bottom_half", NO_ARG, ret_non_zero},
311 {"_spin_trylock_bh", LOCK, "bottom_half", NO_ARG, ret_non_zero},
312 {"__spin_trylock_bh", LOCK, "bottom_half", NO_ARG, ret_non_zero},
315 static struct lock_info *lock_table;
317 static struct tracker_list *starts_locked;
318 static struct tracker_list *starts_unlocked;
320 struct locks_on_return {
321 int line;
322 struct tracker_list *locked;
323 struct tracker_list *unlocked;
325 DECLARE_PTR_LIST(return_list, struct locks_on_return);
326 static struct return_list *all_returns;
328 static char *make_full_name(const char *lock, const char *var)
330 static char tmp_buf[512];
332 snprintf(tmp_buf, 512, "%s:%s", lock, var);
333 tmp_buf[511] = '\0';
334 return alloc_string(tmp_buf);
337 static struct expression *remove_spinlock_check(struct expression *expr)
339 if (expr->type != EXPR_CALL)
340 return expr;
341 if (expr->fn->type != EXPR_SYMBOL)
342 return expr;
343 if (strcmp(expr->fn->symbol_name->name, "spinlock_check"))
344 return expr;
345 expr = get_argument_from_call_expr(expr->args, 0);
346 return expr;
349 static char *get_full_name(struct expression *expr, int index)
351 struct expression *arg;
352 char *name = NULL;
353 char *full_name = NULL;
354 struct lock_info *lock = &lock_table[index];
356 if (lock->arg == RETURN_VAL) {
357 name = get_variable_from_expr(expr->left, NULL);
358 full_name = make_full_name(lock->name, name);
359 } else if (lock->arg == NO_ARG) {
360 full_name = make_full_name(lock->name, "");
361 } else {
362 arg = get_argument_from_call_expr(expr->args, lock->arg);
363 if (!arg)
364 goto free;
365 arg = remove_spinlock_check(arg);
366 name = get_variable_from_expr(arg, NULL);
367 if (!name)
368 goto free;
369 full_name = make_full_name(lock->name, name);
371 free:
372 free_string(name);
373 return full_name;
376 static struct smatch_state *get_start_state(struct sm_state *sm)
378 int is_locked = 0;
379 int is_unlocked = 0;
381 if (in_tracker_list(starts_locked, my_id, sm->name, sm->sym))
382 is_locked = 1;
383 if (in_tracker_list(starts_unlocked, my_id, sm->name, sm->sym))
384 is_unlocked = 1;
385 if (is_locked && is_unlocked)
386 return &undefined;
387 if (is_locked)
388 return &locked;
389 if (is_unlocked)
390 return &unlocked;
391 return &undefined;
394 static struct smatch_state *unmatched_state(struct sm_state *sm)
396 return &start_state;
399 static void do_lock(const char *name)
401 struct sm_state *sm;
403 sm = get_sm_state(my_id, name, NULL);
404 if (!sm)
405 add_tracker(&starts_unlocked, my_id, name, NULL);
406 if (sm && slist_has_state(sm->possible, &locked))
407 sm_msg("error: double lock '%s'", name);
408 set_state(my_id, name, NULL, &locked);
411 static void do_lock_failed(const char *name)
413 struct sm_state *sm;
415 sm = get_sm_state(my_id, name, NULL);
416 if (!sm)
417 add_tracker(&starts_unlocked, my_id, name, NULL);
418 set_state(my_id, name, NULL, &unlocked);
421 static void do_unlock(const char *name)
423 struct sm_state *sm;
425 sm = get_sm_state(my_id, name, NULL);
426 if (!sm)
427 add_tracker(&starts_locked, my_id, name, NULL);
428 if (sm && slist_has_state(sm->possible, &unlocked))
429 sm_msg("error: double unlock '%s'", name);
430 set_state(my_id, name, NULL, &unlocked);
434 static void match_lock_held(const char *fn, struct expression *call_expr,
435 struct expression *assign_expr, void *_index)
437 int index = (int)_index;
438 char *lock_name;
439 struct lock_info *lock = &lock_table[index];
441 if (lock->arg == NO_ARG) {
442 lock_name = get_full_name(NULL, index);
443 } else if (lock->arg == RETURN_VAL) {
444 if (!assign_expr)
445 return;
446 lock_name = get_full_name(assign_expr, index);
447 } else {
448 lock_name = get_full_name(call_expr, index);
450 if (!lock_name)
451 return;
452 do_lock(lock_name);
453 free_string(lock_name);
456 static void match_lock_failed(const char *fn, struct expression *call_expr,
457 struct expression *assign_expr, void *_index)
459 int index = (int)_index;
460 char *lock_name;
461 struct lock_info *lock = &lock_table[index];
463 if (lock->arg == NO_ARG) {
464 lock_name = get_full_name(NULL, index);
465 } else if (lock->arg == RETURN_VAL) {
466 if (!assign_expr)
467 return;
468 lock_name = get_full_name(assign_expr, index);
469 } else {
470 lock_name = get_full_name(call_expr, index);
472 if (!lock_name)
473 return;
474 do_lock_failed(lock_name);
475 free_string(lock_name);
478 static void match_returns_locked(const char *fn, struct expression *expr,
479 void *_index)
481 char *full_name = NULL;
482 int index = (int)_index;
483 struct lock_info *lock = &lock_table[index];
485 if (lock->arg != RETURN_VAL)
486 return;
487 full_name = get_full_name(expr, index);
488 do_lock(full_name);
491 static void match_lock_unlock(const char *fn, struct expression *expr, void *_index)
493 char *full_name = NULL;
494 int index = (int)_index;
495 struct lock_info *lock = &lock_table[index];
497 full_name = get_full_name(expr, index);
498 if (!full_name)
499 return;
500 if (lock->action == LOCK)
501 do_lock(full_name);
502 else
503 do_unlock(full_name);
504 free_string(full_name);
507 static struct locks_on_return *alloc_return(int line)
509 struct locks_on_return *ret;
511 ret = malloc(sizeof(*ret));
512 ret->line = line;
513 ret->locked = NULL;
514 ret->unlocked = NULL;
515 return ret;
518 static void check_possible(struct sm_state *sm)
520 struct sm_state *tmp;
521 int islocked = 0;
522 int isunlocked = 0;
523 int undef = 0;
525 if (!option_spammy)
526 return;
528 FOR_EACH_PTR(sm->possible, tmp) {
529 if (tmp->state == &locked)
530 islocked = 1;
531 if (tmp->state == &unlocked)
532 isunlocked = 1;
533 if (tmp->state == &start_state) {
534 struct smatch_state *s;
536 s = get_start_state(tmp);
537 if (s == &locked)
538 islocked = 1;
539 else if (s == &unlocked)
540 isunlocked = 1;
541 else
542 undef = 1;
544 if (tmp->state == &undefined)
545 undef = 1; // i don't think this is possible any more.
546 } END_FOR_EACH_PTR(tmp);
547 if ((islocked && isunlocked) || undef)
548 sm_msg("warn: '%s' is sometimes locked here and sometimes unlocked.", sm->name);
551 static void match_return(struct expression *ret_value)
553 struct locks_on_return *ret;
554 struct state_list *slist;
555 struct sm_state *tmp;
557 if (!final_pass)
558 return;
560 ret = alloc_return(get_lineno());
562 slist = get_all_states(my_id);
563 FOR_EACH_PTR(slist, tmp) {
564 if (tmp->state == &locked) {
565 add_tracker(&ret->locked, tmp->owner, tmp->name,
566 tmp->sym);
567 } else if (tmp->state == &unlocked) {
568 add_tracker(&ret->unlocked, tmp->owner, tmp->name,
569 tmp->sym);
570 } else if (tmp->state == &start_state) {
571 struct smatch_state *s;
573 s = get_start_state(tmp);
574 if (s == &locked)
575 add_tracker(&ret->locked, tmp->owner, tmp->name,
576 tmp->sym);
577 if (s == &unlocked)
578 add_tracker(&ret->unlocked, tmp->owner,tmp->name,
579 tmp->sym);
580 }else {
581 check_possible(tmp);
583 } END_FOR_EACH_PTR(tmp);
584 free_slist(&slist);
585 add_ptr_list(&all_returns, ret);
588 static void print_inconsistent_returns(struct tracker *lock,
589 struct smatch_state *start)
591 struct locks_on_return *tmp;
592 int i;
594 sm_printf("%s +%d %s(%d) ", get_filename(), get_lineno(), get_function(), get_func_pos());
595 sm_printf("warn: inconsistent returns %s:", lock->name);
596 sm_printf(" locked (");
597 i = 0;
598 FOR_EACH_PTR(all_returns, tmp) {
599 if (in_tracker_list(tmp->unlocked, lock->owner, lock->name, lock->sym))
600 continue;
601 if (in_tracker_list(tmp->locked, lock->owner, lock->name, lock->sym)) {
602 if (i++)
603 sm_printf(",");
604 sm_printf("%d", tmp->line);
605 continue;
607 if (start == &locked) {
608 if (i++)
609 sm_printf(",");
610 sm_printf("%d", tmp->line);
612 } END_FOR_EACH_PTR(tmp);
614 sm_printf(") unlocked (");
615 i = 0;
616 FOR_EACH_PTR(all_returns, tmp) {
617 if (in_tracker_list(tmp->unlocked, lock->owner, lock->name, lock->sym)) {
618 if (i++)
619 sm_printf(",");
620 sm_printf("%d", tmp->line);
621 continue;
623 if (in_tracker_list(tmp->locked, lock->owner, lock->name, lock->sym)) {
624 continue;
626 if (start == &unlocked) {
627 if (i++)
628 sm_printf(",");
629 sm_printf("%d", tmp->line);
631 } END_FOR_EACH_PTR(tmp);
632 sm_printf(")\n");
635 static void check_returns_consistently(struct tracker *lock,
636 struct smatch_state *start)
638 int returns_locked = 0;
639 int returns_unlocked = 0;
640 struct locks_on_return *tmp;
642 FOR_EACH_PTR(all_returns, tmp) {
643 if (in_tracker_list(tmp->unlocked, lock->owner, lock->name,
644 lock->sym))
645 returns_unlocked = tmp->line;
646 else if (in_tracker_list(tmp->locked, lock->owner, lock->name,
647 lock->sym))
648 returns_locked = tmp->line;
649 else if (start == &locked)
650 returns_locked = tmp->line;
651 else if (start == &unlocked)
652 returns_unlocked = tmp->line;
653 } END_FOR_EACH_PTR(tmp);
655 if (returns_locked && returns_unlocked)
656 print_inconsistent_returns(lock, start);
659 static void check_consistency(struct symbol *sym)
661 struct tracker *tmp;
663 if (is_reachable())
664 match_return(NULL);
666 FOR_EACH_PTR(starts_locked, tmp) {
667 if (in_tracker_list(starts_unlocked, tmp->owner, tmp->name,
668 tmp->sym))
669 sm_msg("error: locking inconsistency. We assume "
670 "'%s' is both locked and unlocked at the "
671 "start.",
672 tmp->name);
673 } END_FOR_EACH_PTR(tmp);
675 FOR_EACH_PTR(starts_locked, tmp) {
676 check_returns_consistently(tmp, &locked);
677 } END_FOR_EACH_PTR(tmp);
679 FOR_EACH_PTR(starts_unlocked, tmp) {
680 check_returns_consistently(tmp, &unlocked);
681 } END_FOR_EACH_PTR(tmp);
685 static void clear_lists(void)
687 struct locks_on_return *tmp;
689 free_trackers_and_list(&starts_locked);
690 free_trackers_and_list(&starts_unlocked);
692 FOR_EACH_PTR(all_returns, tmp) {
693 free_trackers_and_list(&tmp->locked);
694 free_trackers_and_list(&tmp->unlocked);
695 free(tmp);
696 } END_FOR_EACH_PTR(tmp);
697 __free_ptr_list((struct ptr_list **)&all_returns);
700 static void match_func_end(struct symbol *sym)
702 check_consistency(sym);
703 clear_lists();
706 static void register_lock(int index)
708 struct lock_info *lock = &lock_table[index];
709 void *idx = (void *)index;
711 if (lock->return_type == ret_non_zero) {
712 return_implies_state(lock->function, 1, POINTER_MAX, &match_lock_held, idx);
713 return_implies_state(lock->function, 0, 0, &match_lock_failed, idx);
714 } else if (lock->return_type == ret_any && lock->arg == RETURN_VAL) {
715 add_function_assign_hook(lock->function, &match_returns_locked, idx);
716 } else if (lock->return_type == ret_any) {
717 add_function_hook(lock->function, &match_lock_unlock, idx);
718 } else if (lock->return_type == ret_zero) {
719 return_implies_state(lock->function, 0, 0, &match_lock_held, idx);
720 return_implies_state(lock->function, whole_range.min, -1, &match_lock_failed, idx);
724 static void load_table(struct lock_info *_lock_table, int size)
726 int i;
728 lock_table = _lock_table;
730 for (i = 0; i < size; i++) {
731 if (lock_table[i].action == LOCK)
732 register_lock(i);
733 else
734 add_function_hook(lock_table[i].function, &match_lock_unlock, (void *)i);
738 /* print_held_locks() is used in check_call_tree.c */
739 void print_held_locks()
741 struct state_list *slist;
742 struct sm_state *sm;
743 int i = 0;
745 slist = get_all_states(my_id);
746 FOR_EACH_PTR(slist, sm) {
747 if (sm->state != &locked)
748 continue;
749 if (i++)
750 sm_printf(" ");
751 sm_printf("'%s'", sm->name);
752 } END_FOR_EACH_PTR(sm);
753 free_slist(&slist);
756 void check_locking(int id)
758 my_id = id;
760 if (option_project == PROJ_WINE)
761 load_table(wine_lock_table, ARRAY_SIZE(wine_lock_table));
762 else if (option_project == PROJ_KERNEL)
763 load_table(kernel_lock_table, ARRAY_SIZE(kernel_lock_table));
764 else
765 return;
767 add_unmatched_state_hook(my_id, &unmatched_state);
768 add_hook(&match_return, RETURN_HOOK);
769 add_hook(&match_func_end, END_FUNC_HOOK);