type: export get_pointer_type()
[smatch.git] / check_locking.c
blob55dc2542982a7b1a2bffbba3fae1e37f996a84f0
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(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 sm = get_sm_state(my_id, name, NULL);
442 if (!sm)
443 add_tracker(&starts_locked, my_id, name, NULL);
444 if (sm && slist_has_state(sm->possible, &unlocked))
445 sm_msg("error: double unlock '%s'", name);
446 if (sm)
447 func_has_transition = TRUE;
448 set_state(my_id, name, NULL, &unlocked);
452 static void match_lock_held(const char *fn, struct expression *call_expr,
453 struct expression *assign_expr, void *_index)
455 int index = PTR_INT(_index);
456 char *lock_name;
457 struct lock_info *lock = &lock_table[index];
459 if (lock->arg == NO_ARG) {
460 lock_name = get_full_name(NULL, index);
461 } else if (lock->arg == RETURN_VAL) {
462 if (!assign_expr)
463 return;
464 lock_name = get_full_name(assign_expr, index);
465 } else {
466 lock_name = get_full_name(call_expr, index);
468 if (!lock_name)
469 return;
470 do_lock(lock_name);
471 free_string(lock_name);
474 static void match_lock_failed(const char *fn, struct expression *call_expr,
475 struct expression *assign_expr, void *_index)
477 int index = PTR_INT(_index);
478 char *lock_name;
479 struct lock_info *lock = &lock_table[index];
481 if (lock->arg == NO_ARG) {
482 lock_name = get_full_name(NULL, index);
483 } else if (lock->arg == RETURN_VAL) {
484 if (!assign_expr)
485 return;
486 lock_name = get_full_name(assign_expr, index);
487 } else {
488 lock_name = get_full_name(call_expr, index);
490 if (!lock_name)
491 return;
492 do_lock_failed(lock_name);
493 free_string(lock_name);
496 static void match_returns_locked(const char *fn, struct expression *expr,
497 void *_index)
499 char *full_name = NULL;
500 int index = PTR_INT(_index);
501 struct lock_info *lock = &lock_table[index];
503 if (lock->arg != RETURN_VAL)
504 return;
505 full_name = get_full_name(expr, index);
506 do_lock(full_name);
509 static void match_lock_unlock(const char *fn, struct expression *expr, void *_index)
511 char *full_name = NULL;
512 int index = PTR_INT(_index);
513 struct lock_info *lock = &lock_table[index];
515 full_name = get_full_name(expr, index);
516 if (!full_name)
517 return;
518 if (lock->action == LOCK)
519 do_lock(full_name);
520 else
521 do_unlock(full_name);
522 free_string(full_name);
525 static struct locks_on_return *alloc_return(int line)
527 struct locks_on_return *ret;
529 ret = malloc(sizeof(*ret));
530 ret->line = line;
531 ret->locked = NULL;
532 ret->unlocked = NULL;
533 return ret;
536 static void check_possible(struct sm_state *sm)
538 struct sm_state *tmp;
539 int islocked = 0;
540 int isunlocked = 0;
541 int undef = 0;
543 if (!option_spammy)
544 return;
546 FOR_EACH_PTR(sm->possible, tmp) {
547 if (tmp->state == &locked)
548 islocked = 1;
549 if (tmp->state == &unlocked)
550 isunlocked = 1;
551 if (tmp->state == &start_state) {
552 struct smatch_state *s;
554 s = get_start_state(tmp);
555 if (s == &locked)
556 islocked = 1;
557 else if (s == &unlocked)
558 isunlocked = 1;
559 else
560 undef = 1;
562 if (tmp->state == &undefined)
563 undef = 1; // i don't think this is possible any more.
564 } END_FOR_EACH_PTR(tmp);
565 if ((islocked && isunlocked) || undef)
566 sm_msg("warn: '%s' is sometimes locked here and sometimes unlocked.", sm->name);
569 static void match_return(struct expression *ret_value)
571 struct locks_on_return *ret;
572 struct state_list *slist;
573 struct sm_state *tmp;
575 if (!final_pass)
576 return;
578 ret = alloc_return(get_lineno());
580 slist = get_all_states(my_id);
581 FOR_EACH_PTR(slist, tmp) {
582 if (tmp->state == &locked) {
583 add_tracker(&ret->locked, tmp->owner, tmp->name,
584 tmp->sym);
585 } else if (tmp->state == &unlocked) {
586 add_tracker(&ret->unlocked, tmp->owner, tmp->name,
587 tmp->sym);
588 } else if (tmp->state == &start_state) {
589 struct smatch_state *s;
591 s = get_start_state(tmp);
592 if (s == &locked)
593 add_tracker(&ret->locked, tmp->owner, tmp->name,
594 tmp->sym);
595 if (s == &unlocked)
596 add_tracker(&ret->unlocked, tmp->owner,tmp->name,
597 tmp->sym);
598 }else {
599 check_possible(tmp);
601 } END_FOR_EACH_PTR(tmp);
602 free_slist(&slist);
603 add_ptr_list(&all_returns, ret);
606 static void print_inconsistent_returns(struct tracker *lock,
607 struct smatch_state *start)
609 struct locks_on_return *tmp;
610 int i;
612 sm_prefix();
613 sm_printf("warn: inconsistent returns %s:", lock->name);
614 sm_printf(" locked (");
615 i = 0;
616 FOR_EACH_PTR(all_returns, tmp) {
617 if (in_tracker_list(tmp->unlocked, lock->owner, lock->name, lock->sym))
618 continue;
619 if (in_tracker_list(tmp->locked, lock->owner, lock->name, lock->sym)) {
620 if (i++)
621 sm_printf(",");
622 sm_printf("%d", tmp->line);
623 continue;
625 if (start == &locked) {
626 if (i++)
627 sm_printf(",");
628 sm_printf("%d", tmp->line);
630 } END_FOR_EACH_PTR(tmp);
632 sm_printf(") unlocked (");
633 i = 0;
634 FOR_EACH_PTR(all_returns, tmp) {
635 if (in_tracker_list(tmp->unlocked, lock->owner, lock->name, lock->sym)) {
636 if (i++)
637 sm_printf(",");
638 sm_printf("%d", tmp->line);
639 continue;
641 if (in_tracker_list(tmp->locked, lock->owner, lock->name, lock->sym)) {
642 continue;
644 if (start == &unlocked) {
645 if (i++)
646 sm_printf(",");
647 sm_printf("%d", tmp->line);
649 } END_FOR_EACH_PTR(tmp);
650 sm_printf(")\n");
653 static void check_returns_consistently(struct tracker *lock,
654 struct smatch_state *start)
656 int returns_locked = 0;
657 int returns_unlocked = 0;
658 struct locks_on_return *tmp;
660 FOR_EACH_PTR(all_returns, tmp) {
661 if (in_tracker_list(tmp->unlocked, lock->owner, lock->name,
662 lock->sym))
663 returns_unlocked = tmp->line;
664 else if (in_tracker_list(tmp->locked, lock->owner, lock->name,
665 lock->sym))
666 returns_locked = tmp->line;
667 else if (start == &locked)
668 returns_locked = tmp->line;
669 else if (start == &unlocked)
670 returns_unlocked = tmp->line;
671 } END_FOR_EACH_PTR(tmp);
673 if (returns_locked && returns_unlocked)
674 print_inconsistent_returns(lock, start);
677 static void check_consistency(struct symbol *sym)
679 struct tracker *tmp;
681 if (is_reachable())
682 match_return(NULL);
684 FOR_EACH_PTR(starts_locked, tmp) {
685 if (in_tracker_list(starts_unlocked, tmp->owner, tmp->name,
686 tmp->sym))
687 sm_msg("error: locking inconsistency. We assume "
688 "'%s' is both locked and unlocked at the "
689 "start.",
690 tmp->name);
691 } END_FOR_EACH_PTR(tmp);
693 FOR_EACH_PTR(starts_locked, tmp) {
694 check_returns_consistently(tmp, &locked);
695 } END_FOR_EACH_PTR(tmp);
697 FOR_EACH_PTR(starts_unlocked, tmp) {
698 check_returns_consistently(tmp, &unlocked);
699 } 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, 1, POINTER_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);