2 * sparse/check_locking.c
4 * Copyright (C) 2009 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
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.
19 #include "smatch_slist.h"
21 static const char *lock_funcs
[] = {
27 "_spin_lock_irqsave_nested",
34 "_write_lock_irqsave",
43 static const char *unlock_funcs
[] = {
45 "_spin_unlock_irqrestore",
50 "_read_unlock_irqrestore",
54 "_write_unlock_irqrestore",
62 /* These are return 1 if they aquire the lock */
63 static const char *conditional_funcs
[] = {
68 "generic__raw_read_trylock",
74 "__raw_write_trylock",
75 "__raw_write_trylock",
80 /* These functions return 0 on success and negative on failure */
81 static const char *reverse_cond_funcs
[] = {
84 "mutex_lock_interruptible",
85 "mutex_lock_interruptible_nested",
86 "mutex_lock_killable",
87 "mutex_lock_killable_nested",
91 /* todo still need to handle__raw_spin_is_locked */
98 static struct locked_call lock_needed
[] = {
99 {"tty_ldisc_ref_wait", "tty_ldisc_lock"},
104 static struct tracker_list
*starts_locked
;
105 static struct tracker_list
*starts_unlocked
;
107 struct locks_on_return
{
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
;
119 static struct smatch_state
*get_start_state(struct sm_state
*sm
)
124 if (in_tracker_list(starts_locked
, my_id
, sm
->name
, sm
->sym
))
126 if (in_tracker_list(starts_unlocked
, my_id
, sm
->name
, sm
->sym
))
128 if (is_locked
&& is_unlocked
)
137 static struct smatch_state
*unmatched_state(struct sm_state
*sm
)
142 static char *get_lock_name(struct expression
*expr
, void *data
)
144 struct expression
*lock_expr
;
147 return alloc_string((char *)data
);
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
)
159 lock_name
= get_lock_name(expr
, data
);
162 sm
= get_sm_state(my_id
, lock_name
, NULL
);
164 add_tracker(&starts_unlocked
, my_id
, lock_name
, NULL
);
165 if (sm
&& slist_has_state(sm
->possible
, &locked
))
166 sm_msg("error: double lock '%s'", lock_name
);
167 set_state(my_id
, lock_name
, NULL
, &locked
);
168 free_string(lock_name
);
171 static void match_unlock_func(const char *fn
, struct expression
*expr
,
177 lock_name
= get_lock_name(expr
, data
);
180 sm
= get_sm_state(my_id
, lock_name
, NULL
);
182 add_tracker(&starts_locked
, my_id
, lock_name
, NULL
);
183 if (sm
&& slist_has_state(sm
->possible
, &unlocked
))
184 sm_msg("error: double unlock '%s'", lock_name
);
185 set_state(my_id
, lock_name
, NULL
, &unlocked
);
186 free_string(lock_name
);
189 static void match_lock_failed(const char *fn
, struct expression
*expr
, void *data
)
194 lock_name
= get_lock_name(expr
, data
);
197 sm
= get_sm_state(my_id
, lock_name
, NULL
);
199 add_tracker(&starts_unlocked
, my_id
, lock_name
, NULL
);
200 set_state(my_id
, lock_name
, NULL
, &unlocked
);
201 free_string(lock_name
);
204 static void match_lock_aquired(const char *fn
, struct expression
*expr
, void *data
)
209 lock_name
= get_lock_name(expr
, data
);
212 sm
= get_sm_state(my_id
, lock_name
, NULL
);
214 add_tracker(&starts_unlocked
, my_id
, lock_name
, NULL
);
215 if (sm
&& slist_has_state(sm
->possible
, &locked
))
216 sm_msg("error: double lock '%s'", lock_name
);
217 set_state(my_id
, lock_name
, NULL
, &locked
);
218 free_string(lock_name
);
221 static void match_lock_needed(const char *fn
, struct expression
*expr
,
224 struct smatch_state
*state
;
227 state
= get_state(my_id
, (char *)data
, NULL
);
228 if (state
== &locked
)
230 fn_name
= get_variable_from_expr(expr
->fn
, NULL
);
232 sm_msg("Internal error.");
235 sm_msg("error: %s called without holding '%s' lock", fn_name
,
237 free_string(fn_name
);
240 static void match_locks_on_non_zero(const char *fn
, struct expression
*expr
,
246 lock_name
= get_lock_name(expr
, data
);
249 sm
= get_sm_state(my_id
, lock_name
, NULL
);
251 add_tracker(&starts_unlocked
, my_id
, lock_name
, NULL
);
252 set_true_false_states(my_id
, lock_name
, NULL
, &locked
, &unlocked
);
253 free_string(lock_name
);
256 static struct locks_on_return
*alloc_return(int line
)
258 struct locks_on_return
*ret
;
260 ret
= malloc(sizeof(*ret
));
263 ret
->unlocked
= NULL
;
267 static void check_possible(struct sm_state
*sm
)
269 struct sm_state
*tmp
;
274 FOR_EACH_PTR(sm
->possible
, tmp
) {
275 if (tmp
->state
== &locked
)
277 if (tmp
->state
== &unlocked
)
279 if (tmp
->state
== &start_state
) {
280 struct smatch_state
*s
;
282 s
= get_start_state(tmp
);
285 else if (s
== &unlocked
)
290 if (tmp
->state
== &undefined
)
291 undef
= 1; // i don't think this is possible any more.
292 } END_FOR_EACH_PTR(tmp
);
293 if ((islocked
&& isunlocked
) || undef
)
294 sm_msg("warn: '%s' is sometimes locked here and "
295 "sometimes unlocked.", sm
->name
);
298 static void match_return(struct statement
*stmt
)
300 struct locks_on_return
*ret
;
301 struct state_list
*slist
;
302 struct sm_state
*tmp
;
307 ret
= alloc_return(get_lineno());
309 slist
= get_all_states(my_id
);
310 FOR_EACH_PTR(slist
, tmp
) {
311 if (tmp
->state
== &locked
) {
312 add_tracker(&ret
->locked
, tmp
->owner
, tmp
->name
,
314 } else if (tmp
->state
== &unlocked
) {
315 add_tracker(&ret
->unlocked
, tmp
->owner
, tmp
->name
,
317 } else if (tmp
->state
== &start_state
) {
318 struct smatch_state
*s
;
320 s
= get_start_state(tmp
);
322 add_tracker(&ret
->locked
, tmp
->owner
, tmp
->name
,
325 add_tracker(&ret
->unlocked
, tmp
->owner
,tmp
->name
,
330 } END_FOR_EACH_PTR(tmp
);
332 add_ptr_list(&all_returns
, ret
);
335 static void print_inconsistent_returns(struct tracker
*lock
,
336 struct smatch_state
*start
)
338 struct locks_on_return
*tmp
;
341 sm_printf("%s +%d %s(%d) ", get_filename(), get_lineno(), get_function(), get_func_pos());
342 sm_printf("warn: inconsistent returns %s:", lock
->name
);
343 sm_printf(" locked (");
345 FOR_EACH_PTR(all_returns
, tmp
) {
346 if (in_tracker_list(tmp
->unlocked
, lock
->owner
, lock
->name
, lock
->sym
))
348 if (in_tracker_list(tmp
->locked
, lock
->owner
, lock
->name
, lock
->sym
)) {
351 sm_printf("%d", tmp
->line
);
354 if (start
== &locked
) {
357 sm_printf("%d", tmp
->line
);
359 } END_FOR_EACH_PTR(tmp
);
361 sm_printf(") unlocked (");
363 FOR_EACH_PTR(all_returns
, tmp
) {
364 if (in_tracker_list(tmp
->unlocked
, lock
->owner
, lock
->name
, lock
->sym
)) {
367 sm_printf("%d", tmp
->line
);
370 if (in_tracker_list(tmp
->locked
, lock
->owner
, lock
->name
, lock
->sym
)) {
373 if (start
== &unlocked
) {
376 sm_printf("%d", tmp
->line
);
378 } END_FOR_EACH_PTR(tmp
);
382 static void check_returns_consistently(struct tracker
*lock
,
383 struct smatch_state
*start
)
385 int returns_locked
= 0;
386 int returns_unlocked
= 0;
387 struct locks_on_return
*tmp
;
389 FOR_EACH_PTR(all_returns
, tmp
) {
390 if (in_tracker_list(tmp
->unlocked
, lock
->owner
, lock
->name
,
392 returns_unlocked
= tmp
->line
;
393 else if (in_tracker_list(tmp
->locked
, lock
->owner
, lock
->name
,
395 returns_locked
= tmp
->line
;
396 else if (start
== &locked
)
397 returns_locked
= tmp
->line
;
398 else if (start
== &unlocked
)
399 returns_unlocked
= tmp
->line
;
400 } END_FOR_EACH_PTR(tmp
);
402 if (returns_locked
&& returns_unlocked
)
403 print_inconsistent_returns(lock
, start
);
406 static void check_consistency(struct symbol
*sym
)
413 FOR_EACH_PTR(starts_locked
, tmp
) {
414 if (in_tracker_list(starts_unlocked
, tmp
->owner
, tmp
->name
,
416 sm_msg("error: locking inconsistency. We assume "
417 "'%s' is both locked and unlocked at the "
420 } END_FOR_EACH_PTR(tmp
);
422 FOR_EACH_PTR(starts_locked
, tmp
) {
423 check_returns_consistently(tmp
, &locked
);
424 } END_FOR_EACH_PTR(tmp
);
426 FOR_EACH_PTR(starts_unlocked
, tmp
) {
427 check_returns_consistently(tmp
, &unlocked
);
428 } END_FOR_EACH_PTR(tmp
);
432 static void clear_lists(void)
434 struct locks_on_return
*tmp
;
436 free_trackers_and_list(&starts_locked
);
437 free_trackers_and_list(&starts_unlocked
);
439 FOR_EACH_PTR(all_returns
, tmp
) {
440 free_trackers_and_list(&tmp
->locked
);
441 free_trackers_and_list(&tmp
->unlocked
);
443 } END_FOR_EACH_PTR(tmp
);
444 __free_ptr_list((struct ptr_list
**)&all_returns
);
447 static void match_func_end(struct symbol
*sym
)
449 check_consistency(sym
);
453 void check_locking(int id
)
458 add_unmatched_state_hook(my_id
, &unmatched_state
);
459 add_hook(&match_return
, RETURN_HOOK
);
460 add_hook(&match_func_end
, END_FUNC_HOOK
);
462 for (i
= 0; lock_funcs
[i
]; i
++) {
463 add_function_hook(lock_funcs
[i
], &match_lock_func
, NULL
);
465 add_function_hook("lock_kernel", &match_lock_func
, (void *)"kernel");
466 for (i
= 0; unlock_funcs
[i
]; i
++) {
467 add_function_hook(unlock_funcs
[i
], &match_unlock_func
, NULL
);
469 add_function_hook("unlock_kernel", &match_unlock_func
, (void *)"kernel");
470 for (i
= 0; i
< sizeof(lock_needed
)/sizeof(struct locked_call
); i
++) {
471 add_function_hook(lock_needed
[i
].function
, &match_lock_needed
,
472 (void *)lock_needed
[i
].lock
);
475 for (i
= 0; conditional_funcs
[i
]; i
++) {
476 add_conditional_hook(conditional_funcs
[i
],
477 &match_locks_on_non_zero
, NULL
);
480 for (i
= 0; reverse_cond_funcs
[i
]; i
++) {
481 return_implies_state(reverse_cond_funcs
[i
], whole_range
.min
, -1,
482 &match_lock_failed
, NULL
);
483 return_implies_state(reverse_cond_funcs
[i
], 0, 0,
484 &match_lock_aquired
, NULL
);