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",
41 static const char *unlock_funcs
[] = {
43 "_spin_unlock_irqrestore",
48 "_read_unlock_irqrestore",
52 "_write_unlock_irqrestore",
59 static const char *conditional_funcs
[] = {
64 "generic__raw_read_trylock",
70 "__raw_write_trylock",
71 "__raw_write_trylock",
75 /* These functions return 0 on success */
76 static const char *reverse_cond_funcs
[] = {
82 /* todo still need to handle__raw_spin_is_locked */
90 static struct locked_call lock_needed
[] = {
91 {"tty_ldisc_ref_wait", "tty_ldisc_lock"},
96 static struct tracker_list
*starts_locked
;
97 static struct tracker_list
*starts_unlocked
;
99 struct locks_on_return
{
101 struct tracker_list
*locked
;
102 struct tracker_list
*unlocked
;
104 DECLARE_PTR_LIST(return_list
, struct locks_on_return
);
105 static struct return_list
*all_returns
;
110 static char *match_func(const char *list
[], char *fn_name
,
111 struct expression_list
*args
)
113 struct expression
*lock_expr
;
116 for (i
= 0; list
[i
]; i
++) {
117 if (!strcmp(fn_name
, list
[i
])) {
118 lock_expr
= get_argument_from_call_expr(args
, 0);
119 return get_variable_from_expr(lock_expr
, NULL
);
125 static char kernel
[] = "kernel";
126 static char *match_lock_func(char *fn_name
, struct expression_list
*args
)
130 arg
= match_func(lock_funcs
, fn_name
, args
);
133 if (!strcmp(fn_name
, "lock_kernel"))
138 static char *match_unlock_func(char *fn_name
, struct expression_list
*args
)
142 arg
= match_func(unlock_funcs
, fn_name
, args
);
145 if (!strcmp(fn_name
, "unlock_kernel"))
150 static void check_locks_needed(const char *fn_name
)
152 struct smatch_state
*state
;
155 for (i
= 0; i
< sizeof(lock_needed
)/sizeof(struct locked_call
); i
++) {
156 if (!strcmp(fn_name
, lock_needed
[i
].function
)) {
157 state
= get_state(lock_needed
[i
].lock
, my_id
, NULL
);
158 if (state
!= &locked
) {
159 smatch_msg("%s called without holding '%s' lock",
160 lock_needed
[i
].function
,
161 lock_needed
[i
].lock
);
167 static void match_call(struct expression
*expr
)
173 fn_name
= get_variable_from_expr(expr
->fn
, NULL
);
177 if ((lock_name
= match_lock_func(fn_name
, expr
->args
))) {
178 sm
= get_sm_state(lock_name
, my_id
, NULL
);
180 add_tracker(&starts_unlocked
, lock_name
, my_id
, NULL
);
181 if (sm
&& slist_has_state(sm
->possible
, &locked
))
182 smatch_msg("double lock '%s'", lock_name
);
183 set_state(lock_name
, my_id
, NULL
, &locked
);
184 } else if ((lock_name
= match_unlock_func(fn_name
, expr
->args
))) {
185 sm
= get_sm_state(lock_name
, my_id
, NULL
);
187 add_tracker(&starts_locked
, lock_name
, my_id
, NULL
);
188 if (sm
&& slist_has_state(sm
->possible
, &unlocked
))
189 smatch_msg("double unlock '%s'", lock_name
);
190 set_state(lock_name
, my_id
, NULL
, &unlocked
);
192 check_locks_needed(fn_name
);
193 free_string(fn_name
);
197 static void match_condition(struct expression
*expr
)
202 if (expr
->type
!= EXPR_CALL
)
205 fn_name
= get_variable_from_expr(expr
->fn
, NULL
);
209 if ((lock_name
= match_func(conditional_funcs
, fn_name
, expr
->args
))) {
210 if (!get_state(lock_name
, my_id
, NULL
))
211 add_tracker(&starts_unlocked
, lock_name
, my_id
, NULL
);
212 set_true_false_states(lock_name
, my_id
, NULL
, &locked
, &unlocked
);
214 if ((lock_name
= match_func(reverse_cond_funcs
, fn_name
, expr
->args
))) {
215 if (!get_state(lock_name
, my_id
, NULL
))
216 add_tracker(&starts_unlocked
, lock_name
, my_id
, NULL
);
217 set_true_false_states(lock_name
, my_id
, NULL
, &unlocked
, &locked
);
219 free_string(fn_name
);
223 static struct locks_on_return
*alloc_return(int line
)
225 struct locks_on_return
*ret
;
227 ret
= malloc(sizeof(*ret
));
230 ret
->unlocked
= NULL
;
234 static void check_possible(struct sm_state
*sm
)
236 struct sm_state
*tmp
;
241 FOR_EACH_PTR(sm
->possible
, tmp
) {
242 if (tmp
->state
== &locked
)
244 else if (tmp
->state
== &unlocked
)
246 else if (tmp
->state
== &undefined
)
248 } END_FOR_EACH_PTR(tmp
);
249 if (islocked
&& (isunlocked
|| undef
))
250 smatch_msg("Unclear if '%s' is locked or unlocked.", tmp
->name
);
253 static void match_return(struct statement
*stmt
)
255 struct locks_on_return
*ret
;
256 struct state_list
*slist
;
257 struct sm_state
*tmp
;
259 ret
= alloc_return(get_lineno());
261 slist
= get_all_states(my_id
);
262 FOR_EACH_PTR(slist
, tmp
) {
263 if (tmp
->state
== &locked
) {
264 add_tracker(&ret
->locked
, tmp
->name
, tmp
->owner
,
266 } else if (tmp
->state
== &unlocked
) {
267 add_tracker(&ret
->unlocked
, tmp
->name
, tmp
->owner
,
272 } END_FOR_EACH_PTR(tmp
);
273 add_ptr_list(&all_returns
, ret
);
276 static void check_returns_consistently(struct tracker
*lock
,
277 struct smatch_state
*start
)
279 int returns_locked
= 0;
280 int returns_unlocked
= 0;
281 struct locks_on_return
*tmp
;
283 FOR_EACH_PTR(all_returns
, tmp
) {
284 if (in_tracker_list(tmp
->unlocked
, lock
->name
, lock
->owner
,
286 returns_unlocked
= tmp
->line
;
287 else if (in_tracker_list(tmp
->locked
, lock
->name
, lock
->owner
,
289 returns_locked
= tmp
->line
;
290 else if (start
== &locked
)
291 returns_locked
= tmp
->line
;
292 else if (start
== &unlocked
)
293 returns_unlocked
= tmp
->line
;
294 } END_FOR_EACH_PTR(tmp
);
296 if (returns_locked
&& returns_unlocked
)
297 smatch_msg("Lock '%s' held on line %d but not on %d.",
298 lock
->name
, returns_locked
, returns_unlocked
);
302 static void clear_lists()
304 struct locks_on_return
*tmp
;
306 __free_ptr_list((struct ptr_list
**)&starts_locked
);
307 __free_ptr_list((struct ptr_list
**)&starts_unlocked
);
309 FOR_EACH_PTR(all_returns
, tmp
) {
310 __free_ptr_list((struct ptr_list
**)&tmp
->locked
);
311 __free_ptr_list((struct ptr_list
**)&tmp
->unlocked
);
312 } END_FOR_EACH_PTR(tmp
);
313 __free_ptr_list((struct ptr_list
**)&all_returns
);
316 static void check_consistency(struct symbol
*sym
)
320 FOR_EACH_PTR(starts_locked
, tmp
) {
321 if (in_tracker_list(starts_unlocked
, tmp
->name
, tmp
->owner
,
323 smatch_msg("Locking inconsistency. We assume '%s' is "
324 "both locked and unlocked at the start.",
326 } END_FOR_EACH_PTR(tmp
);
328 FOR_EACH_PTR(starts_locked
, tmp
) {
329 check_returns_consistently(tmp
, &locked
);
330 } END_FOR_EACH_PTR(tmp
);
332 FOR_EACH_PTR(starts_unlocked
, tmp
) {
333 check_returns_consistently(tmp
, &unlocked
);
334 } END_FOR_EACH_PTR(tmp
);
339 void register_locking(int id
)
342 add_hook(&match_condition
, CONDITION_HOOK
);
343 add_hook(&match_call
, FUNCTION_CALL_HOOK
);
344 add_hook(&match_return
, RETURN_HOOK
);
345 add_hook(&check_consistency
, END_FUNC_HOOK
);