2 * sparse/check_memory.c
4 * Copyright (C) 2008 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
14 #include "smatch_slist.h"
26 malloced --> allocated --> assigned --> isfree +
27 \-> isnull. \-> isfree +
33 static struct tracker_list
*arguments
;
35 static const char *allocation_funcs
[] = {
42 static char *get_parent_name(struct symbol
*sym
)
46 if (!sym
|| !sym
->ident
)
49 snprintf(buf
, 255, "-%s", sym
->ident
->name
);
51 return alloc_string(buf
);
54 static int is_parent_sym(const char *name
)
56 if (!strncmp(name
, "-", 1))
61 static struct smatch_state
*unmatched_state(struct sm_state
*sm
)
63 if (is_parent_sym(sm
->name
))
68 static void assign_parent(struct symbol
*sym
)
72 name
= get_parent_name(sym
);
75 set_state(name
, my_id
, sym
, &assigned
);
79 static int parent_is_assigned(struct symbol
*sym
)
81 struct smatch_state
*state
;
84 name
= get_parent_name(sym
);
87 state
= get_state(name
, my_id
, sym
);
89 if (state
== &assigned
)
94 static int is_allocation(struct expression
*expr
)
99 if (expr
->type
!= EXPR_CALL
)
102 if (!(fn_name
= get_variable_from_expr(expr
->fn
, NULL
)))
105 for (i
= 0; allocation_funcs
[i
]; i
++) {
106 if (!strcmp(fn_name
, allocation_funcs
[i
])) {
107 free_string(fn_name
);
111 free_string(fn_name
);
115 static int is_freed(const char *name
, struct symbol
*sym
)
117 struct state_list
*slist
;
119 slist
= get_possible_states(name
, my_id
, sym
);
120 if (slist_has_state(slist
, &isfree
)) {
126 static int is_argument(struct symbol
*sym
)
130 FOR_EACH_PTR(arguments
, arg
) {
133 } END_FOR_EACH_PTR(arg
);
137 static void match_function_def(struct symbol
*sym
)
141 FOR_EACH_PTR(sym
->ctype
.base_type
->arguments
, arg
) {
142 add_tracker(&arguments
, (arg
->ident
?arg
->ident
->name
:"NULL"), my_id
, arg
);
143 } END_FOR_EACH_PTR(arg
);
146 static void match_declarations(struct symbol
*sym
)
150 if ((get_base_type(sym
))->type
== SYM_ARRAY
) {
154 name
= sym
->ident
->name
;
156 if (sym
->initializer
) {
157 if (is_allocation(sym
->initializer
)) {
158 set_state(name
, my_id
, sym
, &malloced
);
165 static int is_parent(struct expression
*expr
)
167 if (expr
->type
== EXPR_DEREF
)
172 static void match_assign(struct expression
*expr
)
174 struct expression
*left
, *right
;
175 char *left_name
= NULL
;
176 char *right_name
= NULL
;
177 struct symbol
*left_sym
, *right_sym
;
178 struct smatch_state
*state
;
180 left
= strip_expr(expr
->left
);
181 left_name
= get_variable_from_expr_complex(left
, &left_sym
);
183 right
= strip_expr(expr
->right
);
184 if (left_name
&& left_sym
&& is_allocation(right
) &&
185 !(left_sym
->ctype
.modifiers
&
186 (MOD_NONLOCAL
| MOD_STATIC
| MOD_ADDRESSABLE
)) &&
187 !parent_is_assigned(left_sym
)) {
188 set_state(left_name
, my_id
, left_sym
, &malloced
);
192 right_name
= get_variable_from_expr_complex(right
, &right_sym
);
194 if (right_name
&& (state
= get_state(right_name
, my_id
, right_sym
))) {
195 if (state
== &isfree
)
196 smatch_msg("error: assigning freed pointer");
197 set_state(right_name
, my_id
, right_sym
, &assigned
);
200 if (is_freed(left_name
, left_sym
)) {
201 set_state(left_name
, my_id
, left_sym
, &unfree
);
203 if (left_name
&& is_parent(left
))
204 assign_parent(left_sym
);
205 if (right_name
&& is_parent(right
))
206 assign_parent(right_sym
);
208 free_string(left_name
);
209 free_string(right_name
);
212 static int is_null(const char *name
, struct symbol
*sym
)
214 struct smatch_state
*state
;
216 state
= get_state(name
, my_id
, sym
);
217 if (state
&& !strcmp(state
->name
, "isnull"))
222 static void match_free_func(const char *fn
, struct expression
*expr
, void *data
)
224 struct expression
*ptr_expr
;
226 struct symbol
*ptr_sym
;
227 int arg_num
= PTR_INT(data
);
229 ptr_expr
= get_argument_from_call_expr(expr
->args
, arg_num
);
230 ptr_name
= get_variable_from_expr_complex(ptr_expr
, &ptr_sym
);
231 if (is_freed(ptr_name
, ptr_sym
) && !is_null(ptr_name
, ptr_sym
)) {
232 smatch_msg("error: double free of %s", ptr_name
);
234 set_state(ptr_name
, my_id
, ptr_sym
, &isfree
);
235 free_string(ptr_name
);
238 static int possibly_allocated(struct state_list
*slist
)
240 struct sm_state
*tmp
;
242 FOR_EACH_PTR(slist
, tmp
) {
243 if (tmp
->state
== &allocated
)
245 if (tmp
->state
== &malloced
)
247 } END_FOR_EACH_PTR(tmp
);
251 static void check_for_allocated(void)
253 struct state_list
*slist
;
254 struct sm_state
*tmp
;
256 slist
= get_all_states(my_id
);
257 FOR_EACH_PTR(slist
, tmp
) {
258 if (possibly_allocated(tmp
->possible
) &&
259 !is_null(tmp
->name
, tmp
->sym
) &&
260 !is_argument(tmp
->sym
) &&
261 !parent_is_assigned(tmp
->sym
))
262 smatch_msg("error: memery leak of %s", tmp
->name
);
263 } END_FOR_EACH_PTR(tmp
);
267 static void match_return(struct statement
*stmt
)
272 name
= get_variable_from_expr_complex(stmt
->ret_value
, &sym
);
276 check_for_allocated();
279 static void set_new_true_false_paths(const char *name
, struct symbol
*sym
)
281 struct smatch_state
*tmp
;
283 tmp
= get_state(name
, my_id
, sym
);
289 if (tmp
== &isfree
) {
290 smatch_msg("warn: why do you care about freed memory?");
293 if (tmp
== &assigned
) {
294 /* we don't care about assigned pointers any more */
297 set_true_false_states(name
, my_id
, sym
, &allocated
, &isnull
);
300 static void match_condition(struct expression
*expr
)
305 expr
= strip_expr(expr
);
310 name
= get_variable_from_expr_complex(expr
, &sym
);
313 set_new_true_false_paths(name
, sym
);
316 case EXPR_ASSIGNMENT
:
317 /* You have to deal with stuff like if (a = b = c) */
318 match_condition(expr
->right
);
319 match_condition(expr
->left
);
326 static void match_function_call(struct expression
*expr
)
328 struct expression
*tmp
;
331 struct sm_state
*state
;
333 FOR_EACH_PTR(expr
->args
, tmp
) {
334 tmp
= strip_expr(tmp
);
335 name
= get_variable_from_expr_complex(tmp
, &sym
);
338 if ((state
= get_sm_state(name
, my_id
, sym
))) {
339 if (possibly_allocated(state
->possible
)) {
340 set_state(name
, my_id
, sym
, &assigned
);
345 } END_FOR_EACH_PTR(tmp
);
348 static void match_dereferences(struct expression
*expr
)
351 struct symbol
*sym
= NULL
;
353 deref
= get_variable_from_expr(expr
->deref
->unop
, &sym
);
356 if (is_freed(deref
, sym
)) {
357 smatch_msg("error: dereferencing freed memory '%s'", deref
);
358 set_state(deref
, my_id
, sym
, &unfree
);
363 static void match_end_func(struct symbol
*sym
)
365 check_for_allocated();
366 free_trackers_and_list(&arguments
);
369 static void register_funcs_from_file(void)
375 token
= get_tokens_file("kernel.frees_argument");
378 if (token_type(token
) != TOKEN_STREAMBEGIN
)
381 while (token_type(token
) != TOKEN_STREAMEND
) {
382 if (token_type(token
) != TOKEN_IDENT
)
384 func
= show_ident(token
->ident
);
386 if (token_type(token
) != TOKEN_NUMBER
)
388 arg
= atoi(token
->number
);
389 add_function_hook(func
, &match_free_func
, INT_PTR(arg
));
395 void check_memory(int id
)
398 add_unmatched_state_hook(my_id
, &unmatched_state
);
399 add_hook(&match_function_def
, FUNC_DEF_HOOK
);
400 add_hook(&match_declarations
, DECLARATION_HOOK
);
401 add_hook(&match_function_call
, FUNCTION_CALL_HOOK
);
402 add_hook(&match_condition
, CONDITION_HOOK
);
403 add_hook(&match_dereferences
, DEREF_HOOK
);
404 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
405 add_hook(&match_return
, RETURN_HOOK
);
406 add_hook(&match_end_func
, END_FUNC_HOOK
);
407 add_function_hook("kfree", &match_free_func
, (void *)0);
408 register_funcs_from_file();