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 struct smatch_state
*unmatched_state(struct sm_state
*sm
)
44 if (!strcmp(sm
->name
, "-"))
49 static void assign_parent(struct symbol
*sym
)
51 set_state("-", my_id
, sym
, &assigned
);
54 static int parent_is_assigned(struct symbol
*sym
)
56 struct smatch_state
*state
;
58 state
= get_state("-", my_id
, sym
);
59 if (state
== &assigned
)
64 static int is_allocation(struct expression
*expr
)
69 if (expr
->type
!= EXPR_CALL
)
72 if (!(fn_name
= get_variable_from_expr(expr
->fn
, NULL
)))
75 for (i
= 0; allocation_funcs
[i
]; i
++) {
76 if (!strcmp(fn_name
, allocation_funcs
[i
])) {
85 static int is_freed(const char *name
, struct symbol
*sym
)
87 struct state_list
*slist
;
89 slist
= get_possible_states(name
, my_id
, sym
);
90 if (slist_has_state(slist
, &isfree
)) {
96 static int is_argument(struct symbol
*sym
)
100 FOR_EACH_PTR(arguments
, arg
) {
103 } END_FOR_EACH_PTR(arg
);
107 static void match_function_def(struct symbol
*sym
)
111 FOR_EACH_PTR(sym
->ctype
.base_type
->arguments
, arg
) {
112 add_tracker(&arguments
, (arg
->ident
?arg
->ident
->name
:"NULL"), my_id
, arg
);
113 } END_FOR_EACH_PTR(arg
);
116 static void match_declarations(struct symbol
*sym
)
120 if ((get_base_type(sym
))->type
== SYM_ARRAY
) {
124 name
= sym
->ident
->name
;
126 if (sym
->initializer
) {
127 if (is_allocation(sym
->initializer
)) {
128 set_state(name
, my_id
, sym
, &malloced
);
135 static int is_parent(struct expression
*expr
)
137 if (expr
->type
== EXPR_DEREF
)
142 static void match_assign(struct expression
*expr
)
144 struct expression
*left
, *right
;
145 char *left_name
= NULL
;
146 char *right_name
= NULL
;
147 struct symbol
*left_sym
, *right_sym
;
148 struct smatch_state
*state
;
150 left
= strip_expr(expr
->left
);
151 left_name
= get_variable_from_expr_complex(left
, &left_sym
);
153 right
= strip_expr(expr
->right
);
154 if (left_name
&& left_sym
&& is_allocation(right
) &&
155 !(left_sym
->ctype
.modifiers
&
156 (MOD_NONLOCAL
| MOD_STATIC
| MOD_ADDRESSABLE
)) &&
157 !parent_is_assigned(left_sym
)) {
158 set_state(left_name
, my_id
, left_sym
, &malloced
);
162 right_name
= get_variable_from_expr_complex(right
, &right_sym
);
164 if (right_name
&& (state
= get_state(right_name
, my_id
, right_sym
))) {
165 if (state
== &isfree
)
166 smatch_msg("error: assigning freed pointer");
167 set_state(right_name
, my_id
, right_sym
, &assigned
);
170 if (is_freed(left_name
, left_sym
)) {
171 set_state(left_name
, my_id
, left_sym
, &unfree
);
173 if (left_name
&& is_parent(left
))
174 assign_parent(left_sym
);
175 if (right_name
&& is_parent(right
))
176 assign_parent(right_sym
);
178 free_string(left_name
);
179 free_string(right_name
);
182 static int is_null(const char *name
, struct symbol
*sym
)
184 struct smatch_state
*state
;
186 state
= get_state(name
, my_id
, sym
);
187 if (state
&& !strcmp(state
->name
, "isnull"))
192 static void match_free_func(const char *fn
, struct expression
*expr
, void *data
)
194 struct expression
*ptr_expr
;
196 struct symbol
*ptr_sym
;
197 int arg_num
= (int)data
;
199 ptr_expr
= get_argument_from_call_expr(expr
->args
, arg_num
);
200 ptr_name
= get_variable_from_expr_complex(ptr_expr
, &ptr_sym
);
201 if (is_freed(ptr_name
, ptr_sym
) && !is_null(ptr_name
, ptr_sym
)) {
202 smatch_msg("error: double free of %s", ptr_name
);
204 set_state(ptr_name
, my_id
, ptr_sym
, &isfree
);
205 free_string(ptr_name
);
208 static int possibly_allocated(struct state_list
*slist
)
210 struct sm_state
*tmp
;
212 FOR_EACH_PTR(slist
, tmp
) {
213 if (tmp
->state
== &allocated
)
215 if (tmp
->state
== &malloced
)
217 } END_FOR_EACH_PTR(tmp
);
221 static void check_for_allocated(void)
223 struct state_list
*slist
;
224 struct sm_state
*tmp
;
226 slist
= get_all_states(my_id
);
227 FOR_EACH_PTR(slist
, tmp
) {
228 if (possibly_allocated(tmp
->possible
) &&
229 !is_null(tmp
->name
, tmp
->sym
) &&
230 !is_argument(tmp
->sym
) &&
231 !parent_is_assigned(tmp
->sym
))
232 smatch_msg("error: memery leak of %s", tmp
->name
);
233 } END_FOR_EACH_PTR(tmp
);
237 static void match_return(struct statement
*stmt
)
242 name
= get_variable_from_expr_complex(stmt
->ret_value
, &sym
);
246 check_for_allocated();
249 static void set_new_true_false_paths(const char *name
, struct symbol
*sym
)
251 struct smatch_state
*tmp
;
253 tmp
= get_state(name
, my_id
, sym
);
259 if (tmp
== &isfree
) {
260 smatch_msg("warn: why do you care about freed memory?");
263 if (tmp
== &assigned
) {
264 /* we don't care about assigned pointers any more */
267 set_true_false_states(name
, my_id
, sym
, &allocated
, &isnull
);
270 static void match_condition(struct expression
*expr
)
275 expr
= strip_expr(expr
);
280 name
= get_variable_from_expr_complex(expr
, &sym
);
283 set_new_true_false_paths(name
, sym
);
286 case EXPR_ASSIGNMENT
:
287 /* You have to deal with stuff like if (a = b = c) */
288 match_condition(expr
->right
);
289 match_condition(expr
->left
);
296 static void match_function_call(struct expression
*expr
)
298 struct expression
*tmp
;
301 struct sm_state
*state
;
303 FOR_EACH_PTR(expr
->args
, tmp
) {
304 tmp
= strip_expr(tmp
);
305 name
= get_variable_from_expr_complex(tmp
, &sym
);
308 if ((state
= get_sm_state(name
, my_id
, sym
))) {
309 if (possibly_allocated(state
->possible
)) {
310 set_state(name
, my_id
, sym
, &assigned
);
315 } END_FOR_EACH_PTR(tmp
);
318 static void match_dereferences(struct expression
*expr
)
321 struct symbol
*sym
= NULL
;
323 deref
= get_variable_from_expr(expr
->deref
->unop
, &sym
);
326 if (is_freed(deref
, sym
)) {
327 smatch_msg("error: dereferencing freed memory '%s'", deref
);
328 set_state(deref
, my_id
, sym
, &unfree
);
333 static void match_end_func(struct symbol
*sym
)
335 check_for_allocated();
336 free_trackers_and_list(&arguments
);
339 static void register_funcs_from_file(void)
345 token
= get_tokens_file("kernel.frees_argument");
348 if (token_type(token
) != TOKEN_STREAMBEGIN
)
351 while (token_type(token
) != TOKEN_STREAMEND
) {
352 if (token_type(token
) != TOKEN_IDENT
)
354 func
= show_ident(token
->ident
);
356 if (token_type(token
) != TOKEN_NUMBER
)
358 arg
= atoi(token
->number
);
359 add_function_hook(func
, &match_free_func
, (void *)arg
);
365 void check_memory(int id
)
368 add_unmatched_state_hook(my_id
, &unmatched_state
);
369 add_hook(&match_function_def
, FUNC_DEF_HOOK
);
370 add_hook(&match_declarations
, DECLARATION_HOOK
);
371 add_hook(&match_function_call
, FUNCTION_CALL_HOOK
);
372 add_hook(&match_condition
, CONDITION_HOOK
);
373 add_hook(&match_dereferences
, DEREF_HOOK
);
374 add_hook(&match_assign
, ASSIGNMENT_HOOK
);
375 add_hook(&match_return
, RETURN_HOOK
);
376 add_hook(&match_end_func
, END_FUNC_HOOK
);
377 add_function_hook("kfree", &match_free_func
, (void *)0);
378 register_funcs_from_file();