new api: add_conditional_hook(), set_cond_states();
[smatch.git] / check_locking.c
blob655302773e908fb03db23285a3084f1d3d902471
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 *get_lock_name(struct expression *expr, void *data)
144 struct expression *lock_expr;
146 if (data) {
147 return alloc_string((char *)data);
148 } else {
149 lock_expr = get_argument_from_call_expr(expr->args, 0);
150 return get_variable_from_expr(lock_expr, NULL);
154 static void match_lock_func(const char *fn, struct expression *expr, void *data)
156 char *lock_name;
157 struct sm_state *sm;
159 lock_name = get_lock_name(expr, data);
160 if (!lock_name)
161 return;
162 sm = get_sm_state(lock_name, my_id, NULL);
163 if (!sm)
164 add_tracker(&starts_unlocked, lock_name, my_id, NULL);
165 if (sm && slist_has_state(sm->possible, &locked))
166 smatch_msg("error: double lock '%s'", lock_name);
167 set_state(lock_name, my_id, NULL, &locked);
168 free_string(lock_name);
171 static void match_unlock_func(const char *fn, struct expression *expr,
172 void *data)
174 char *lock_name;
175 struct sm_state *sm;
177 lock_name = get_lock_name(expr, data);
178 if (!lock_name)
179 return;
180 sm = get_sm_state(lock_name, my_id, NULL);
181 if (!sm)
182 add_tracker(&starts_locked, lock_name, my_id, NULL);
183 if (sm && slist_has_state(sm->possible, &unlocked))
184 smatch_msg("error: double unlock '%s'", lock_name);
185 set_state(lock_name, my_id, NULL, &unlocked);
186 free_string(lock_name);
189 static void match_lock_needed(const char *fn, struct expression *expr,
190 void *data)
192 struct smatch_state *state;
193 char *fn_name;
195 state = get_state((char *)data, my_id, NULL);
196 if (state == &locked)
197 return;
198 fn_name = get_variable_from_expr(expr->fn, NULL);
199 if (!fn_name) {
200 smatch_msg("Internal error.");
201 exit(1);
203 smatch_msg("error: %s called without holding '%s' lock", fn_name,
204 (char *)data);
205 free_string(fn_name);
208 static void match_locks_on_zero(const char *fn, struct expression *expr,
209 void *data)
211 char *lock_name;
212 struct sm_state *sm;
214 lock_name = get_lock_name(expr, data);
215 if (!lock_name)
216 return;
217 sm = get_sm_state(lock_name, my_id, NULL);
218 if (!sm)
219 add_tracker(&starts_unlocked, lock_name, my_id, NULL);
220 set_cond_states(lock_name, my_id, NULL, &unlocked, &locked);
221 free_string(lock_name);
224 static void match_locks_on_non_zero(const char *fn, struct expression *expr,
225 void *data)
227 char *lock_name;
228 struct sm_state *sm;
230 lock_name = get_lock_name(expr, data);
231 if (!lock_name)
232 return;
233 sm = get_sm_state(lock_name, my_id, NULL);
234 if (!sm)
235 add_tracker(&starts_unlocked, lock_name, my_id, NULL);
236 set_cond_states(lock_name, my_id, NULL, &locked, &unlocked);
237 free_string(lock_name);
240 static struct locks_on_return *alloc_return(int line)
242 struct locks_on_return *ret;
244 ret = malloc(sizeof(*ret));
245 ret->line = line;
246 ret->locked = NULL;
247 ret->unlocked = NULL;
248 return ret;
251 static void check_possible(struct sm_state *sm)
253 struct sm_state *tmp;
254 int islocked = 0;
255 int isunlocked = 0;
256 int undef = 0;
258 FOR_EACH_PTR(sm->possible, tmp) {
259 if (tmp->state == &locked)
260 islocked = 1;
261 if (tmp->state == &unlocked)
262 isunlocked = 1;
263 if (tmp->state == &start_state) {
264 struct smatch_state *s;
266 s = get_start_state(tmp);
267 if (s == &locked)
268 islocked = 1;
269 else if (s == &unlocked)
270 isunlocked = 1;
271 else
272 undef = 1;
274 if (tmp->state == &undefined)
275 undef = 1; // i don't think this is possible any more.
276 } END_FOR_EACH_PTR(tmp);
277 if ((islocked && isunlocked) || undef)
278 smatch_msg("warn: '%s' is sometimes locked here and "
279 "sometimes unlocked.", sm->name);
282 static void match_return(struct statement *stmt)
284 struct locks_on_return *ret;
285 struct state_list *slist;
286 struct sm_state *tmp;
288 ret = alloc_return(get_lineno());
290 slist = get_all_states(my_id);
291 FOR_EACH_PTR(slist, tmp) {
292 if (tmp->state == &locked) {
293 add_tracker(&ret->locked, tmp->name, tmp->owner,
294 tmp->sym);
295 } else if (tmp->state == &unlocked) {
296 add_tracker(&ret->unlocked, tmp->name, tmp->owner,
297 tmp->sym);
298 } else if (tmp->state == &start_state) {
299 struct smatch_state *s;
301 s = get_start_state(tmp);
302 if (s == &locked)
303 add_tracker(&ret->locked, tmp->name, tmp->owner,
304 tmp->sym);
305 if (s == &unlocked)
306 add_tracker(&ret->unlocked, tmp->name,
307 tmp->owner, tmp->sym);
308 }else {
309 check_possible(tmp);
311 } END_FOR_EACH_PTR(tmp);
312 free_slist(&slist);
313 add_ptr_list(&all_returns, ret);
316 static void check_returns_consistently(struct tracker *lock,
317 struct smatch_state *start)
319 int returns_locked = 0;
320 int returns_unlocked = 0;
321 struct locks_on_return *tmp;
323 FOR_EACH_PTR(all_returns, tmp) {
324 if (in_tracker_list(tmp->unlocked, lock->name, lock->owner,
325 lock->sym))
326 returns_unlocked = tmp->line;
327 else if (in_tracker_list(tmp->locked, lock->name, lock->owner,
328 lock->sym))
329 returns_locked = tmp->line;
330 else if (start == &locked)
331 returns_locked = tmp->line;
332 else if (start == &unlocked)
333 returns_unlocked = tmp->line;
334 } END_FOR_EACH_PTR(tmp);
336 if (returns_locked && returns_unlocked)
337 smatch_msg("warn: lock '%s' held on line %d but not on %d.",
338 lock->name, returns_locked, returns_unlocked);
342 static void check_consistency(struct symbol *sym)
344 struct tracker *tmp;
346 if (is_reachable())
347 match_return(NULL);
349 FOR_EACH_PTR(starts_locked, tmp) {
350 if (in_tracker_list(starts_unlocked, tmp->name, tmp->owner,
351 tmp->sym))
352 smatch_msg("error: locking inconsistency. We assume "
353 "'%s' is both locked and unlocked at the "
354 "start.",
355 tmp->name);
356 } END_FOR_EACH_PTR(tmp);
358 FOR_EACH_PTR(starts_locked, tmp) {
359 check_returns_consistently(tmp, &locked);
360 } END_FOR_EACH_PTR(tmp);
362 FOR_EACH_PTR(starts_unlocked, tmp) {
363 check_returns_consistently(tmp, &unlocked);
364 } END_FOR_EACH_PTR(tmp);
368 static void clear_lists(void)
370 struct locks_on_return *tmp;
372 free_trackers_and_list(&starts_locked);
373 free_trackers_and_list(&starts_unlocked);
375 FOR_EACH_PTR(all_returns, tmp) {
376 free_trackers_and_list(&tmp->locked);
377 free_trackers_and_list(&tmp->unlocked);
378 } END_FOR_EACH_PTR(tmp);
379 __free_ptr_list((struct ptr_list **)&all_returns);
382 static void match_func_end(struct symbol *sym)
384 check_consistency(sym);
385 clear_lists();
388 void check_locking(int id)
390 int i;
392 my_id = id;
393 add_unmatched_state_hook(my_id, &unmatched_state);
394 add_hook(&match_return, RETURN_HOOK);
395 add_hook(&match_func_end, END_FUNC_HOOK);
397 for (i = 0; lock_funcs[i]; i++) {
398 add_function_hook(lock_funcs[i], &match_lock_func, NULL);
400 add_function_hook("lock_kernel", &match_lock_func, (void *)"kernel");
401 for (i = 0; unlock_funcs[i]; i++) {
402 add_function_hook(unlock_funcs[i], &match_unlock_func, NULL);
404 add_function_hook("unlock_kernel", &match_unlock_func, (void *)"kernel");
405 for (i = 0; i < sizeof(lock_needed)/sizeof(struct locked_call); i++) {
406 add_function_hook(lock_needed[i].function, &match_lock_needed,
407 (void *)lock_needed[i].lock);
410 for (i = 0; conditional_funcs[i]; i++) {
411 add_conditional_hook(conditional_funcs[i],
412 &match_locks_on_non_zero, NULL);
415 for (i = 0; reverse_cond_funcs[i]; i++) {
416 add_conditional_hook(reverse_cond_funcs[i],
417 &match_locks_on_zero, NULL);