function_hooks: pass the function name as well.
[smatch.git] / check_locking.c
blob005794676ce3a7f81e69fedeff7c4838c30b755f
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.
17 #include "parse.h"
18 #include "smatch.h"
19 #include "smatch_slist.h"
21 static const char *lock_funcs[] = {
22 "_spin_lock",
23 "_spin_lock_irqsave",
24 "_spin_lock_irq",
25 "_spin_lock_bh",
26 "_spin_lock_nested",
27 "_spin_lock_irqsave_nested",
28 "_raw_spin_lock",
29 "_read_lock",
30 "_read_lock_irqsave",
31 "_read_lock_irq",
32 "_read_lock_bh",
33 "_write_lock",
34 "_write_lock_irqsave",
35 "_write_lock_irq",
36 "_write_lock_bh",
37 "down",
38 "mutex_lock_nested",
39 "mutex_lock",
40 NULL,
43 static const char *unlock_funcs[] = {
44 "_spin_unlock",
45 "_spin_unlock_irqrestore",
46 "_spin_unlock_irq",
47 "_spin_unlock_bh",
48 "_raw_spin_unlock",
49 "_read_unlock",
50 "_read_unlock_irqrestore",
51 "_read_unlock_irq",
52 "_read_unlock_bh",
53 "_write_unlock",
54 "_write_unlock_irqrestore",
55 "_write_unlock_irq",
56 "_write_unlock_bh",
57 "up",
58 "mutex_unlock",
59 NULL,
62 /* These are return 1 if they aquire the lock */
63 static const char *conditional_funcs[] = {
64 "_spin_trylock",
65 "_spin_trylock_bh",
66 "_read_trylock",
67 "_write_trylock",
68 "generic__raw_read_trylock",
69 "_raw_spin_trylock",
70 "_raw_read_trylock",
71 "_raw_write_trylock",
72 "__raw_spin_trylock",
73 "__raw_read_trylock",
74 "__raw_write_trylock",
75 "__raw_write_trylock",
76 "mutex_trylock",
77 NULL,
80 /* These functions return 0 on success */
81 static const char *reverse_cond_funcs[] = {
82 "down_trylock",
83 "down_interruptible",
84 "mutex_lock_interruptible",
85 "mutex_lock_interruptible_nested",
86 "mutex_lock_killable",
87 "mutex_lock_killable_nested",
88 NULL,
91 /* todo still need to handle__raw_spin_is_locked */
93 struct locked_call {
94 const char *function;
95 const char *lock;
98 static struct locked_call lock_needed[] = {
99 {"tty_ldisc_ref_wait", "tty_ldisc_lock"},
102 static int my_id;
104 static struct tracker_list *starts_locked;
105 static struct tracker_list *starts_unlocked;
107 struct locks_on_return {
108 int line;
109 struct tracker_list *locked;
110 struct tracker_list *unlocked;
112 DECLARE_PTR_LIST(return_list, struct locks_on_return);
113 static struct return_list *all_returns;
115 STATE(locked);
116 STATE(start_state);
117 STATE(unlocked);
119 static struct smatch_state *get_start_state(struct sm_state *sm)
121 int is_locked = 0;
122 int is_unlocked = 0;
124 if (in_tracker_list(starts_locked, sm->name, my_id, sm->sym))
125 is_locked = 1;
126 if (in_tracker_list(starts_unlocked, sm->name, my_id, sm->sym))
127 is_unlocked = 1;
128 if (is_locked && is_unlocked)
129 return &undefined;
130 if (is_locked)
131 return &locked;
132 if (is_unlocked)
133 return &unlocked;
134 return &undefined;
137 static struct smatch_state *unmatched_state(struct sm_state *sm)
139 return &start_state;
142 static char *match_func(const char *list[], char *fn_name,
143 struct expression_list *args)
145 struct expression *lock_expr;
146 int i;
148 for (i = 0; list[i]; i++) {
149 if (!strcmp(fn_name, list[i])) {
150 lock_expr = get_argument_from_call_expr(args, 0);
151 return get_variable_from_expr(lock_expr, NULL);
154 return NULL;
157 static char *get_lock_name(struct expression *expr, void *data)
159 struct expression *lock_expr;
161 if (data) {
162 return alloc_string((char *)data);
163 } else {
164 lock_expr = get_argument_from_call_expr(expr->args, 0);
165 return get_variable_from_expr(lock_expr, NULL);
169 static void match_lock_func(const char *fn, struct expression *expr, void *data)
171 char *lock_name;
172 struct sm_state *sm;
174 lock_name = get_lock_name(expr, data);
175 if (!lock_name)
176 return;
177 sm = get_sm_state(lock_name, my_id, NULL);
178 if (!sm)
179 add_tracker(&starts_unlocked, lock_name, my_id, NULL);
180 if (sm && slist_has_state(sm->possible, &locked))
181 smatch_msg("error: double lock '%s'", lock_name);
182 set_state(lock_name, my_id, NULL, &locked);
183 free_string(lock_name);
186 static void match_unlock_func(const char *fn, struct expression *expr,
187 void *data)
189 char *lock_name;
190 struct sm_state *sm;
192 lock_name = get_lock_name(expr, data);
193 if (!lock_name)
194 return;
195 sm = get_sm_state(lock_name, my_id, NULL);
196 if (!sm)
197 add_tracker(&starts_locked, lock_name, my_id, NULL);
198 if (sm && slist_has_state(sm->possible, &unlocked))
199 smatch_msg("error: double unlock '%s'", lock_name);
200 set_state(lock_name, my_id, NULL, &unlocked);
201 free_string(lock_name);
204 static void match_lock_needed(const char *fn, struct expression *expr,
205 void *data)
207 struct smatch_state *state;
208 char *fn_name;
210 state = get_state((char *)data, my_id, NULL);
211 if (state == &locked)
212 return;
213 fn_name = get_variable_from_expr(expr->fn, NULL);
214 if (!fn_name) {
215 smatch_msg("Internal error.");
216 exit(1);
218 smatch_msg("error: %s called without holding '%s' lock", fn_name,
219 (char *)data);
220 free_string(fn_name);
223 static void match_condition(struct expression *expr)
225 char *fn_name;
226 char *lock_name;
228 if (expr->type != EXPR_CALL)
229 return;
231 fn_name = get_variable_from_expr(expr->fn, NULL);
232 if (!fn_name)
233 return;
235 if ((lock_name = match_func(conditional_funcs, fn_name, expr->args))) {
236 if (!get_state(lock_name, my_id, NULL))
237 add_tracker(&starts_unlocked, lock_name, my_id, NULL);
238 set_true_false_states(lock_name, my_id, NULL, &locked, &unlocked);
240 if ((lock_name = match_func(reverse_cond_funcs, fn_name, expr->args))) {
241 if (!get_state(lock_name, my_id, NULL))
242 add_tracker(&starts_unlocked, lock_name, my_id, NULL);
243 set_true_false_states(lock_name, my_id, NULL, &unlocked, &locked);
245 free_string(lock_name);
246 free_string(fn_name);
247 return;
250 static struct locks_on_return *alloc_return(int line)
252 struct locks_on_return *ret;
254 ret = malloc(sizeof(*ret));
255 ret->line = line;
256 ret->locked = NULL;
257 ret->unlocked = NULL;
258 return ret;
261 static void check_possible(struct sm_state *sm)
263 struct sm_state *tmp;
264 int islocked = 0;
265 int isunlocked = 0;
266 int undef = 0;
268 FOR_EACH_PTR(sm->possible, tmp) {
269 if (tmp->state == &locked)
270 islocked = 1;
271 if (tmp->state == &unlocked)
272 isunlocked = 1;
273 if (tmp->state == &start_state) {
274 struct smatch_state *s;
276 s = get_start_state(tmp);
277 if (s == &locked)
278 islocked = 1;
279 else if (s == &unlocked)
280 isunlocked = 1;
281 else
282 undef = 1;
284 if (tmp->state == &undefined)
285 undef = 1; // i don't think this is possible any more.
286 } END_FOR_EACH_PTR(tmp);
287 if ((islocked && isunlocked) || undef)
288 smatch_msg("warn: '%s' is sometimes locked here and "
289 "sometimes unlocked.", sm->name);
292 static void match_return(struct statement *stmt)
294 struct locks_on_return *ret;
295 struct state_list *slist;
296 struct sm_state *tmp;
298 ret = alloc_return(get_lineno());
300 slist = get_all_states(my_id);
301 FOR_EACH_PTR(slist, tmp) {
302 if (tmp->state == &locked) {
303 add_tracker(&ret->locked, tmp->name, tmp->owner,
304 tmp->sym);
305 } else if (tmp->state == &unlocked) {
306 add_tracker(&ret->unlocked, tmp->name, tmp->owner,
307 tmp->sym);
308 } else if (tmp->state == &start_state) {
309 struct smatch_state *s;
311 s = get_start_state(tmp);
312 if (s == &locked)
313 add_tracker(&ret->locked, tmp->name, tmp->owner,
314 tmp->sym);
315 if (s == &unlocked)
316 add_tracker(&ret->unlocked, tmp->name,
317 tmp->owner, tmp->sym);
318 }else {
319 check_possible(tmp);
321 } END_FOR_EACH_PTR(tmp);
322 free_slist(&slist);
323 add_ptr_list(&all_returns, ret);
326 static void check_returns_consistently(struct tracker *lock,
327 struct smatch_state *start)
329 int returns_locked = 0;
330 int returns_unlocked = 0;
331 struct locks_on_return *tmp;
333 FOR_EACH_PTR(all_returns, tmp) {
334 if (in_tracker_list(tmp->unlocked, lock->name, lock->owner,
335 lock->sym))
336 returns_unlocked = tmp->line;
337 else if (in_tracker_list(tmp->locked, lock->name, lock->owner,
338 lock->sym))
339 returns_locked = tmp->line;
340 else if (start == &locked)
341 returns_locked = tmp->line;
342 else if (start == &unlocked)
343 returns_unlocked = tmp->line;
344 } END_FOR_EACH_PTR(tmp);
346 if (returns_locked && returns_unlocked)
347 smatch_msg("warn: lock '%s' held on line %d but not on %d.",
348 lock->name, returns_locked, returns_unlocked);
352 static void check_consistency(struct symbol *sym)
354 struct tracker *tmp;
356 if (is_reachable())
357 match_return(NULL);
359 FOR_EACH_PTR(starts_locked, tmp) {
360 if (in_tracker_list(starts_unlocked, tmp->name, tmp->owner,
361 tmp->sym))
362 smatch_msg("error: locking inconsistency. We assume "
363 "'%s' is both locked and unlocked at the "
364 "start.",
365 tmp->name);
366 } END_FOR_EACH_PTR(tmp);
368 FOR_EACH_PTR(starts_locked, tmp) {
369 check_returns_consistently(tmp, &locked);
370 } END_FOR_EACH_PTR(tmp);
372 FOR_EACH_PTR(starts_unlocked, tmp) {
373 check_returns_consistently(tmp, &unlocked);
374 } END_FOR_EACH_PTR(tmp);
378 static void clear_lists(void)
380 struct locks_on_return *tmp;
382 free_trackers_and_list(&starts_locked);
383 free_trackers_and_list(&starts_unlocked);
385 FOR_EACH_PTR(all_returns, tmp) {
386 free_trackers_and_list(&tmp->locked);
387 free_trackers_and_list(&tmp->unlocked);
388 } END_FOR_EACH_PTR(tmp);
389 __free_ptr_list((struct ptr_list **)&all_returns);
392 static void match_func_end(struct symbol *sym)
394 check_consistency(sym);
395 clear_lists();
398 void check_locking(int id)
400 int i;
402 my_id = id;
403 add_unmatched_state_hook(my_id, &unmatched_state);
404 add_hook(&match_condition, CONDITION_HOOK);
405 add_hook(&match_return, RETURN_HOOK);
406 add_hook(&match_func_end, END_FUNC_HOOK);
408 for (i = 0; lock_funcs[i]; i++) {
409 add_function_hook(lock_funcs[i], &match_lock_func, NULL);
411 add_function_hook("lock_kernel", &match_lock_func, (void *)"kernel");
412 for (i = 0; unlock_funcs[i]; i++) {
413 add_function_hook(unlock_funcs[i], &match_unlock_func, NULL);
415 add_function_hook("unlock_kernel", &match_unlock_func, (void *)"kernel");
416 for (i = 0; i < sizeof(lock_needed)/sizeof(struct locked_call); i++) {
417 add_function_hook(lock_needed[i].function, &match_lock_needed,
418 (void *)lock_needed[i].lock);