Fix handling of continue inside do while statements.
[smatch.git] / check_locking.c
bloba01f581730fcdf5737df107294b2e6e09562c840
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 NULL,
41 static const char *unlock_funcs[] = {
42 "_spin_unlock",
43 "_spin_unlock_irqrestore",
44 "_spin_unlock_irq",
45 "_spin_unlock_bh",
46 "_raw_spin_unlock",
47 "_read_unlock",
48 "_read_unlock_irqrestore",
49 "_read_unlock_irq",
50 "_read_unlock_bh",
51 "_write_unlock",
52 "_write_unlock_irqrestore",
53 "_write_unlock_irq",
54 "_write_unlock_bh",
55 "up",
56 NULL,
59 static const char *conditional_funcs[] = {
60 "_spin_trylock",
61 "_read_trylock",
62 "_write_trylock",
63 "generic__raw_read_trylock",
64 "_raw_spin_trylock",
65 "_raw_read_trylock",
66 "_raw_write_trylock",
67 "__raw_spin_trylock",
68 "__raw_read_trylock",
69 "__raw_write_trylock",
70 "__raw_write_trylock",
71 NULL,
74 /* todo still need to handle__raw_spin_is_locked */
77 struct locked_call {
78 const char *function;
79 const char *lock;
82 static struct locked_call lock_needed[] = {
83 {"tty_ldisc_ref_wait", "tty_ldisc_lock"},
86 static int my_id;
88 static struct tracker_list *starts_locked;
89 static struct tracker_list *starts_unlocked;
91 struct locks_on_return {
92 int line;
93 struct tracker_list *locked;
94 struct tracker_list *unlocked;
96 DECLARE_PTR_LIST(return_list, struct locks_on_return);
97 static struct return_list *all_returns;
99 STATE(locked);
100 STATE(unlocked);
102 static char kernel[] = "kernel";
103 static char *match_lock_func(char *fn_name, struct expression_list *args)
105 struct expression *lock_expr;
106 int i;
108 for (i = 0; lock_funcs[i]; i++) {
109 if (!strcmp(fn_name, lock_funcs[i])) {
110 lock_expr = get_argument_from_call_expr(args, 0);
111 return get_variable_from_expr(lock_expr, NULL);
114 if (!strcmp(fn_name, "lock_kernel"))
115 return kernel;
116 return NULL;
119 static char *match_unlock_func(char *fn_name, struct expression_list *args)
121 struct expression *lock_expr;
122 int i;
124 for (i = 0; unlock_funcs[i]; i++) {
125 if (!strcmp(fn_name, unlock_funcs[i])) {
126 lock_expr = get_argument_from_call_expr(args, 0);
127 return get_variable_from_expr(lock_expr, NULL);
130 if (!strcmp(fn_name, "unlock_kernel"))
131 return kernel;
132 return NULL;
135 static char *match_conditional_func(char *fn_name, struct expression_list *args)
137 struct expression *lock_expr;
138 int i;
140 for (i = 0; conditional_funcs[i]; i++) {
141 if (!strcmp(fn_name, conditional_funcs[i])) {
142 lock_expr = get_argument_from_call_expr(args, 0);
143 return get_variable_from_expr(lock_expr, NULL);
146 return NULL;
149 static void check_locks_needed(const char *fn_name)
151 struct smatch_state *state;
152 int i;
154 for (i = 0; i < sizeof(lock_needed)/sizeof(struct locked_call); i++) {
155 if (!strcmp(fn_name, lock_needed[i].function)) {
156 state = get_state(lock_needed[i].lock, my_id, NULL);
157 if (state != &locked) {
158 smatch_msg("%s called without holding '%s' lock",
159 lock_needed[i].function,
160 lock_needed[i].lock);
166 static void match_call(struct expression *expr)
168 char *fn_name;
169 char *lock_name;
171 fn_name = get_variable_from_expr(expr->fn, NULL);
172 if (!fn_name)
173 return;
175 if ((lock_name = match_lock_func(fn_name, expr->args))) {
176 if (!get_state(lock_name, my_id, NULL))
177 add_tracker(&starts_unlocked, lock_name, my_id, NULL);
178 set_state(lock_name, my_id, NULL, &locked);
179 } else if ((lock_name = match_unlock_func(fn_name, expr->args))) {
180 if (!get_state(lock_name, my_id, NULL))
181 add_tracker(&starts_locked, lock_name, my_id, NULL);
182 set_state(lock_name, my_id, NULL, &unlocked);
183 } else
184 check_locks_needed(fn_name);
185 free_string(fn_name);
186 return;
189 static void match_condition(struct expression *expr)
191 char *fn_name;
192 char *lock_name;
194 if (expr->type != EXPR_CALL)
195 return;
197 fn_name = get_variable_from_expr(expr->fn, NULL);
198 if (!fn_name)
199 return;
201 if ((lock_name = match_conditional_func(fn_name, expr->args))) {
202 if (!get_state(lock_name, my_id, NULL))
203 add_tracker(&starts_unlocked, lock_name, my_id, NULL);
204 set_true_false_states(lock_name, my_id, NULL, &locked, &unlocked);
206 free_string(fn_name);
207 return;
210 static struct locks_on_return *alloc_return(int line)
212 struct locks_on_return *ret;
214 ret = malloc(sizeof(*ret));
215 ret->line = line;
216 ret->locked = NULL;
217 ret->unlocked = NULL;
218 return ret;
221 static void match_return(struct statement *stmt)
223 struct locks_on_return *ret;
224 struct state_list *slist;
225 struct sm_state *tmp;
227 ret = alloc_return(get_lineno());
229 slist = get_all_states(my_id);
230 FOR_EACH_PTR(slist, tmp) {
231 if (tmp->state == &locked) {
232 add_tracker(&ret->locked, tmp->name, tmp->owner,
233 tmp->sym);
234 } else if (tmp->state == &unlocked) {
235 add_tracker(&ret->unlocked, tmp->name, tmp->owner,
236 tmp->sym);
237 } else {
238 smatch_msg("Unclear if '%s' is locked or unlocked.",
239 tmp->name);
241 } END_FOR_EACH_PTR(tmp);
242 add_ptr_list(&all_returns, ret);
245 static void check_returns_consistently(struct tracker *lock,
246 struct smatch_state *start)
248 int returns_locked = 0;
249 int returns_unlocked = 0;
250 struct locks_on_return *tmp;
252 FOR_EACH_PTR(all_returns, tmp) {
253 if (in_tracker_list(tmp->unlocked, lock->name, lock->owner,
254 lock->sym))
255 returns_unlocked = tmp->line;
256 else if (in_tracker_list(tmp->locked, lock->name, lock->owner,
257 lock->sym))
258 returns_locked = tmp->line;
259 else if (start == &locked)
260 returns_locked = tmp->line;
261 else if (start == &unlocked)
262 returns_unlocked = tmp->line;
263 } END_FOR_EACH_PTR(tmp);
265 if (returns_locked && returns_unlocked)
266 smatch_msg("Lock '%s' held on line %d but not on %d.",
267 lock->name, returns_locked, returns_unlocked);
271 static void clear_lists()
273 struct locks_on_return *tmp;
275 __free_ptr_list((struct ptr_list **)&starts_locked);
276 __free_ptr_list((struct ptr_list **)&starts_unlocked);
278 FOR_EACH_PTR(all_returns, tmp) {
279 __free_ptr_list((struct ptr_list **)&tmp->locked);
280 __free_ptr_list((struct ptr_list **)&tmp->unlocked);
281 } END_FOR_EACH_PTR(tmp);
282 __free_ptr_list((struct ptr_list **)&all_returns);
285 static void check_consistency(struct symbol *sym)
287 struct tracker *tmp;
289 FOR_EACH_PTR(starts_locked, tmp) {
290 if (in_tracker_list(starts_unlocked, tmp->name, tmp->owner,
291 tmp->sym))
292 smatch_msg("Locking inconsistency. We assume '%s' is "
293 "both locked and unlocked at the start.",
294 tmp->name);
295 } END_FOR_EACH_PTR(tmp);
297 FOR_EACH_PTR(starts_locked, tmp) {
298 check_returns_consistently(tmp, &locked);
299 } END_FOR_EACH_PTR(tmp);
301 FOR_EACH_PTR(starts_unlocked, tmp) {
302 check_returns_consistently(tmp, &unlocked);
303 } END_FOR_EACH_PTR(tmp);
305 clear_lists();
308 void register_locking(int id)
310 my_id = id;
311 add_hook(&match_condition, CONDITION_HOOK);
312 add_hook(&match_call, FUNCTION_CALL_HOOK);
313 add_hook(&match_return, RETURN_HOOK);
314 add_hook(&check_consistency, END_FUNC_HOOK);