ranges: rename possibly_true_range_lists() to possibly_true_rl()
[smatch.git] / check_locking.c
blob589071d936f97ac74c0135d042110ed0ad78bc74
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 strcmp(name, "bottom_half:") != 0)
422 sm_msg("error: double lock '%s'", name);
423 if (sm)
424 func_has_transition = TRUE;
425 set_state(my_id, name, NULL, &locked);
428 static void do_lock_failed(const char *name)
430 struct sm_state *sm;
432 sm = get_sm_state(my_id, name, NULL);
433 if (!sm)
434 add_tracker(&starts_unlocked, my_id, name, NULL);
435 set_state(my_id, name, NULL, &unlocked);
438 static void do_unlock(const char *name)
440 struct sm_state *sm;
442 if (__path_is_null())
443 return;
444 sm = get_sm_state(my_id, name, NULL);
445 if (!sm)
446 add_tracker(&starts_locked, my_id, name, NULL);
447 if (sm && slist_has_state(sm->possible, &unlocked) &&
448 strcmp(name, "bottom_half:") != 0)
449 sm_msg("error: double unlock '%s'", name);
450 if (sm)
451 func_has_transition = TRUE;
452 set_state(my_id, name, NULL, &unlocked);
455 static void match_lock_held(const char *fn, struct expression *call_expr,
456 struct expression *assign_expr, void *_index)
458 int index = PTR_INT(_index);
459 char *lock_name;
460 struct lock_info *lock = &lock_table[index];
462 if (lock->arg == NO_ARG) {
463 lock_name = get_full_name(NULL, index);
464 } else if (lock->arg == RETURN_VAL) {
465 if (!assign_expr)
466 return;
467 lock_name = get_full_name(assign_expr, index);
468 } else {
469 lock_name = get_full_name(call_expr, index);
471 if (!lock_name)
472 return;
473 do_lock(lock_name);
474 free_string(lock_name);
477 static void match_lock_failed(const char *fn, struct expression *call_expr,
478 struct expression *assign_expr, void *_index)
480 int index = PTR_INT(_index);
481 char *lock_name;
482 struct lock_info *lock = &lock_table[index];
484 if (lock->arg == NO_ARG) {
485 lock_name = get_full_name(NULL, index);
486 } else if (lock->arg == RETURN_VAL) {
487 if (!assign_expr)
488 return;
489 lock_name = get_full_name(assign_expr, index);
490 } else {
491 lock_name = get_full_name(call_expr, index);
493 if (!lock_name)
494 return;
495 do_lock_failed(lock_name);
496 free_string(lock_name);
499 static void match_returns_locked(const char *fn, struct expression *expr,
500 void *_index)
502 char *full_name = NULL;
503 int index = PTR_INT(_index);
504 struct lock_info *lock = &lock_table[index];
506 if (lock->arg != RETURN_VAL)
507 return;
508 full_name = get_full_name(expr, index);
509 do_lock(full_name);
512 static void match_lock_unlock(const char *fn, struct expression *expr, void *_index)
514 char *full_name = NULL;
515 int index = PTR_INT(_index);
516 struct lock_info *lock = &lock_table[index];
518 full_name = get_full_name(expr, index);
519 if (!full_name)
520 return;
521 if (lock->action == LOCK)
522 do_lock(full_name);
523 else
524 do_unlock(full_name);
525 free_string(full_name);
528 static struct locks_on_return *alloc_return(int line)
530 struct locks_on_return *ret;
532 ret = malloc(sizeof(*ret));
533 ret->line = line;
534 ret->locked = NULL;
535 ret->unlocked = NULL;
536 return ret;
539 static void check_possible(struct sm_state *sm)
541 struct sm_state *tmp;
542 int islocked = 0;
543 int isunlocked = 0;
544 int undef = 0;
546 if (!option_spammy)
547 return;
549 FOR_EACH_PTR(sm->possible, tmp) {
550 if (tmp->state == &locked)
551 islocked = 1;
552 if (tmp->state == &unlocked)
553 isunlocked = 1;
554 if (tmp->state == &start_state) {
555 struct smatch_state *s;
557 s = get_start_state(tmp);
558 if (s == &locked)
559 islocked = 1;
560 else if (s == &unlocked)
561 isunlocked = 1;
562 else
563 undef = 1;
565 if (tmp->state == &undefined)
566 undef = 1; // i don't think this is possible any more.
567 } END_FOR_EACH_PTR(tmp);
568 if ((islocked && isunlocked) || undef)
569 sm_msg("warn: '%s' is sometimes locked here and sometimes unlocked.", sm->name);
572 static void match_return(struct expression *ret_value)
574 struct locks_on_return *ret;
575 struct state_list *slist;
576 struct sm_state *tmp;
578 if (!final_pass)
579 return;
581 ret = alloc_return(get_lineno());
583 slist = get_all_states(my_id);
584 FOR_EACH_PTR(slist, tmp) {
585 if (tmp->state == &locked) {
586 add_tracker(&ret->locked, tmp->owner, tmp->name,
587 tmp->sym);
588 } else if (tmp->state == &unlocked) {
589 add_tracker(&ret->unlocked, tmp->owner, tmp->name,
590 tmp->sym);
591 } else if (tmp->state == &start_state) {
592 struct smatch_state *s;
594 s = get_start_state(tmp);
595 if (s == &locked)
596 add_tracker(&ret->locked, tmp->owner, tmp->name,
597 tmp->sym);
598 if (s == &unlocked)
599 add_tracker(&ret->unlocked, tmp->owner,tmp->name,
600 tmp->sym);
601 }else {
602 check_possible(tmp);
604 } END_FOR_EACH_PTR(tmp);
605 free_slist(&slist);
606 add_ptr_list(&all_returns, ret);
609 static void print_inconsistent_returns(struct tracker *lock,
610 struct smatch_state *start)
612 struct locks_on_return *tmp;
613 int i;
615 sm_prefix();
616 sm_printf("warn: inconsistent returns %s:", lock->name);
617 sm_printf(" locked (");
618 i = 0;
619 FOR_EACH_PTR(all_returns, tmp) {
620 if (in_tracker_list(tmp->unlocked, lock->owner, lock->name, lock->sym))
621 continue;
622 if (in_tracker_list(tmp->locked, lock->owner, lock->name, lock->sym)) {
623 if (i++)
624 sm_printf(",");
625 sm_printf("%d", tmp->line);
626 continue;
628 if (start == &locked) {
629 if (i++)
630 sm_printf(",");
631 sm_printf("%d", tmp->line);
633 } END_FOR_EACH_PTR(tmp);
635 sm_printf(") unlocked (");
636 i = 0;
637 FOR_EACH_PTR(all_returns, tmp) {
638 if (in_tracker_list(tmp->unlocked, lock->owner, lock->name, lock->sym)) {
639 if (i++)
640 sm_printf(",");
641 sm_printf("%d", tmp->line);
642 continue;
644 if (in_tracker_list(tmp->locked, lock->owner, lock->name, lock->sym)) {
645 continue;
647 if (start == &unlocked) {
648 if (i++)
649 sm_printf(",");
650 sm_printf("%d", tmp->line);
652 } END_FOR_EACH_PTR(tmp);
653 sm_printf(")\n");
656 static void check_returns_consistently(struct tracker *lock,
657 struct smatch_state *start)
659 int returns_locked = 0;
660 int returns_unlocked = 0;
661 struct locks_on_return *tmp;
663 FOR_EACH_PTR(all_returns, tmp) {
664 if (in_tracker_list(tmp->unlocked, lock->owner, lock->name,
665 lock->sym))
666 returns_unlocked = tmp->line;
667 else if (in_tracker_list(tmp->locked, lock->owner, lock->name,
668 lock->sym))
669 returns_locked = tmp->line;
670 else if (start == &locked)
671 returns_locked = tmp->line;
672 else if (start == &unlocked)
673 returns_unlocked = tmp->line;
674 } END_FOR_EACH_PTR(tmp);
676 if (returns_locked && returns_unlocked)
677 print_inconsistent_returns(lock, start);
680 static void check_consistency(struct symbol *sym)
682 struct tracker *tmp;
684 if (is_reachable())
685 match_return(NULL);
687 FOR_EACH_PTR(starts_locked, tmp) {
688 if (in_tracker_list(starts_unlocked, tmp->owner, tmp->name,
689 tmp->sym))
690 sm_msg("error: locking inconsistency. We assume "
691 "'%s' is both locked and unlocked at the "
692 "start.",
693 tmp->name);
694 } END_FOR_EACH_PTR(tmp);
696 FOR_EACH_PTR(starts_locked, tmp) {
697 check_returns_consistently(tmp, &locked);
698 } END_FOR_EACH_PTR(tmp);
700 FOR_EACH_PTR(starts_unlocked, tmp) {
701 check_returns_consistently(tmp, &unlocked);
702 } END_FOR_EACH_PTR(tmp);
705 static void clear_lists(void)
707 struct locks_on_return *tmp;
709 func_has_transition = FALSE;
711 free_trackers_and_list(&starts_locked);
712 free_trackers_and_list(&starts_unlocked);
714 FOR_EACH_PTR(all_returns, tmp) {
715 free_trackers_and_list(&tmp->locked);
716 free_trackers_and_list(&tmp->unlocked);
717 free(tmp);
718 } END_FOR_EACH_PTR(tmp);
719 __free_ptr_list((struct ptr_list **)&all_returns);
722 static void match_func_end(struct symbol *sym)
724 if (func_has_transition)
725 check_consistency(sym);
726 clear_lists();
729 static void register_lock(int index)
731 struct lock_info *lock = &lock_table[index];
732 void *idx = INT_PTR(index);
734 if (lock->return_type == ret_non_zero) {
735 return_implies_state(lock->function, valid_ptr_min, valid_ptr_max, &match_lock_held, idx);
736 return_implies_state(lock->function, 0, 0, &match_lock_failed, idx);
737 } else if (lock->return_type == ret_any && lock->arg == RETURN_VAL) {
738 add_function_assign_hook(lock->function, &match_returns_locked, idx);
739 } else if (lock->return_type == ret_any) {
740 add_function_hook(lock->function, &match_lock_unlock, idx);
741 } else if (lock->return_type == ret_zero) {
742 return_implies_state(lock->function, 0, 0, &match_lock_held, idx);
743 return_implies_state(lock->function, -4095, -1, &match_lock_failed, idx);
747 static void load_table(struct lock_info *_lock_table, int size)
749 int i;
751 lock_table = _lock_table;
753 for (i = 0; i < size; i++) {
754 if (lock_table[i].action == LOCK)
755 register_lock(i);
756 else
757 add_function_hook(lock_table[i].function, &match_lock_unlock, INT_PTR(i));
761 /* print_held_locks() is used in check_call_tree.c */
762 void print_held_locks()
764 struct state_list *slist;
765 struct sm_state *sm;
766 int i = 0;
768 slist = get_all_states(my_id);
769 FOR_EACH_PTR(slist, sm) {
770 if (sm->state != &locked)
771 continue;
772 if (i++)
773 sm_printf(" ");
774 sm_printf("'%s'", sm->name);
775 } END_FOR_EACH_PTR(sm);
776 free_slist(&slist);
779 void check_locking(int id)
781 my_id = id;
783 if (option_project == PROJ_WINE)
784 load_table(wine_lock_table, ARRAY_SIZE(wine_lock_table));
785 else if (option_project == PROJ_KERNEL)
786 load_table(kernel_lock_table, ARRAY_SIZE(kernel_lock_table));
787 else
788 return;
790 add_unmatched_state_hook(my_id, &unmatched_state);
791 add_hook(&match_return, RETURN_HOOK);
792 add_hook(&match_func_end, END_FUNC_HOOK);