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
);
159 scoped_state(name
, my_id
, sym
);
166 static int is_parent(struct expression
*expr
)
168 if (expr
->type
== EXPR_DEREF
)
173 static void match_assign(struct expression
*expr
)
175 struct expression
*left
, *right
;
176 char *left_name
= NULL
;
177 char *right_name
= NULL
;
178 struct symbol
*left_sym
, *right_sym
;
179 struct smatch_state
*state
;
181 left
= strip_expr(expr
->left
);
182 left_name
= get_variable_from_expr_complex(left
, &left_sym
);
184 right
= strip_expr(expr
->right
);
185 if (left_name
&& left_sym
&& is_allocation(right
) &&
186 !(left_sym
->ctype
.modifiers
&
187 (MOD_NONLOCAL
| MOD_STATIC
| MOD_ADDRESSABLE
)) &&
188 !parent_is_assigned(left_sym
)) {
189 set_state(left_name
, my_id
, left_sym
, &malloced
);
193 right_name
= get_variable_from_expr_complex(right
, &right_sym
);
195 if (right_name
&& (state
= get_state(right_name
, my_id
, right_sym
))) {
196 if (state
== &isfree
)
197 smatch_msg("error: assigning freed pointer");
198 set_state(right_name
, my_id
, right_sym
, &assigned
);
201 if (is_freed(left_name
, left_sym
)) {
202 set_state(left_name
, my_id
, left_sym
, &unfree
);
204 if (left_name
&& is_parent(left
))
205 assign_parent(left_sym
);
206 if (right_name
&& is_parent(right
))
207 assign_parent(right_sym
);
209 free_string(left_name
);
210 free_string(right_name
);
213 static int is_null(const char *name
, struct symbol
*sym
)
215 struct smatch_state
*state
;
217 state
= get_state(name
, my_id
, sym
);
218 if (state
&& !strcmp(state
->name
, "isnull"))
223 static void match_free_func(const char *fn
, struct expression
*expr
, void *data
)
225 struct expression
*ptr_expr
;
227 struct symbol
*ptr_sym
;
228 int arg_num
= PTR_INT(data
);
230 ptr_expr
= get_argument_from_call_expr(expr
->args
, arg_num
);
231 ptr_name
= get_variable_from_expr_complex(ptr_expr
, &ptr_sym
);
232 if (is_freed(ptr_name
, ptr_sym
) && !is_null(ptr_name
, ptr_sym
)) {
233 smatch_msg("error: double free of %s", ptr_name
);
235 set_state(ptr_name
, my_id
, ptr_sym
, &isfree
);
236 free_string(ptr_name
);
239 static int possibly_allocated(struct state_list
*slist
)
241 struct sm_state
*tmp
;
243 FOR_EACH_PTR(slist
, tmp
) {
244 if (tmp
->state
== &allocated
)
246 if (tmp
->state
== &malloced
)
248 } END_FOR_EACH_PTR(tmp
);
252 static void check_for_allocated(void)
254 struct state_list
*slist
;
255 struct sm_state
*tmp
;
257 slist
= get_all_states(my_id
);
258 FOR_EACH_PTR(slist
, tmp
) {
259 if (possibly_allocated(tmp
->possible
) &&
260 !is_null(tmp
->name
, tmp
->sym
) &&
261 !is_argument(tmp
->sym
) &&
262 !parent_is_assigned(tmp
->sym
))
263 smatch_msg("error: memery leak of %s", tmp
->name
);
264 } END_FOR_EACH_PTR(tmp
);
268 static void match_return(struct statement
*stmt
)
273 name
= get_variable_from_expr_complex(stmt
->ret_value
, &sym
);
277 check_for_allocated();
280 static void set_new_true_false_paths(const char *name
, struct symbol
*sym
)
282 struct smatch_state
*tmp
;
284 tmp
= get_state(name
, my_id
, sym
);
290 if (tmp
== &isfree
) {
291 smatch_msg("warn: why do you care about freed memory?");
294 if (tmp
== &assigned
) {
295 /* we don't care about assigned pointers any more */
298 set_true_false_states(name
, my_id
, sym
, &allocated
, &isnull
);
301 static void match_condition(struct expression
*expr
)
306 expr
= strip_expr(expr
);
311 name
= get_variable_from_expr_complex(expr
, &sym
);
314 set_new_true_false_paths(name
, sym
);
317 case EXPR_ASSIGNMENT
:
318 /* You have to deal with stuff like if (a = b = c) */
319 match_condition(expr
->right
);
320 match_condition(expr
->left
);
327 static void match_function_call(struct expression
*expr
)
329 struct expression
*tmp
;
332 struct sm_state
*state
;
334 FOR_EACH_PTR(expr
->args
, tmp
) {
335 tmp
= strip_expr(tmp
);
336 name
= get_variable_from_expr_complex(tmp
, &sym
);
339 if ((state
= get_sm_state(name
, my_id
, sym
))) {
340 if (possibly_allocated(state
->possible
)) {
341 set_state(name
, my_id
, sym
, &assigned
);
346 } END_FOR_EACH_PTR(tmp
);
349 static void match_dereferences(struct expression
*expr
)
352 struct symbol
*sym
= NULL
;
354 deref
= get_variable_from_expr(expr
->deref
->unop
, &sym
);
357 if (is_freed(deref
, sym
)) {
358 smatch_msg("error: dereferencing freed memory '%s'", deref
);
359 set_state(deref
, my_id
, sym
, &unfree
);
364 static void match_end_func(struct symbol
*sym
)
366 check_for_allocated();
367 free_trackers_and_list(&arguments
);
370 static void register_funcs_from_file(void)
376 token
= get_tokens_file("kernel.frees_argument");
379 if (token_type(token
) != TOKEN_STREAMBEGIN
)
382 while (token_type(token
) != TOKEN_STREAMEND
) {
383 if (token_type(token
) != TOKEN_IDENT
)
385 func
= show_ident(token
->ident
);
387 if (token_type(token
) != TOKEN_NUMBER
)
389 arg
= atoi(token
->number
);
390 add_function_hook(func
, &match_free_func
, INT_PTR(arg
));
396 void check_memory(int id
)
399 add_unmatched_state_hook(my_id
, &unmatched_state
);
400 add_hook(&match_function_def
, FUNC_DEF_HOOK
);
401 add_hook(&match_declarations
, DECLARATION_HOOK
);
402 add_hook(&match_function_call
, FUNCTION_CALL_HOOK
);
403 add_hook(&match_condition
, CONDITION_HOOK
);
404 add_hook(&match_dereferences
, DEREF_HOOK
);
405 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
406 add_hook(&match_return
, RETURN_HOOK
);
407 add_hook(&match_end_func
, END_FUNC_HOOK
);
408 add_function_hook("kfree", &match_free_func
, (void *)0);
409 register_funcs_from_file();