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
[] = {
63 "generic__raw_read_trylock",
69 "__raw_write_trylock",
70 "__raw_write_trylock",
74 /* todo still need to handle__raw_spin_is_locked */
82 static struct locked_call lock_needed
[] = {
83 {"tty_ldisc_ref_wait", "tty_ldisc_lock"},
88 static struct tracker_list
*starts_locked
;
89 static struct tracker_list
*starts_unlocked
;
91 struct locks_on_return
{
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
;
102 static char kernel
[] = "kernel";
103 static char *match_lock_func(char *fn_name
, struct expression_list
*args
)
105 struct expression
*lock_expr
;
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"))
119 static char *match_unlock_func(char *fn_name
, struct expression_list
*args
)
121 struct expression
*lock_expr
;
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"))
135 static char *match_conditional_func(char *fn_name
, struct expression_list
*args
)
137 struct expression
*lock_expr
;
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
);
149 static void check_locks_needed(const char *fn_name
)
151 struct smatch_state
*state
;
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
)
171 fn_name
= get_variable_from_expr(expr
->fn
, NULL
);
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
);
184 check_locks_needed(fn_name
);
185 free_string(fn_name
);
189 static void match_condition(struct expression
*expr
)
194 if (expr
->type
!= EXPR_CALL
)
197 fn_name
= get_variable_from_expr(expr
->fn
, NULL
);
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
);
210 static struct locks_on_return
*alloc_return(int line
)
212 struct locks_on_return
*ret
;
214 ret
= malloc(sizeof(*ret
));
217 ret
->unlocked
= NULL
;
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
,
234 } else if (tmp
->state
== &unlocked
) {
235 add_tracker(&ret
->unlocked
, tmp
->name
, tmp
->owner
,
238 smatch_msg("Unclear if '%s' is locked or unlocked.",
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
,
255 returns_unlocked
= tmp
->line
;
256 else if (in_tracker_list(tmp
->locked
, lock
->name
, lock
->owner
,
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
)
289 FOR_EACH_PTR(starts_locked
, tmp
) {
290 if (in_tracker_list(starts_unlocked
, tmp
->name
, tmp
->owner
,
292 smatch_msg("Locking inconsistency. We assume '%s' is "
293 "both locked and unlocked at the start.",
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
);
308 void register_locking(int 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
);