2 * Copyright (C) 2010 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
19 * check_memory() is getting too big and messy.
25 #include "smatch_slist.h"
26 #include "smatch_extra.h"
33 static void ok_to_use(struct sm_state
*sm
, struct expression
*mod_expr
)
36 set_state(my_id
, sm
->name
, sm
->sym
, &ok
);
39 static void pre_merge_hook(struct sm_state
*cur
, struct sm_state
*other
)
41 if (is_impossible_path())
42 set_state(my_id
, cur
->name
, cur
->sym
, &ok
);
45 static struct smatch_state
*unmatched_state(struct sm_state
*sm
)
47 struct smatch_state
*state
;
50 if (sm
->state
!= &freed
)
53 state
= get_state(SMATCH_EXTRA
, sm
->name
, sm
->sym
);
56 if (!estate_get_single_value(state
, &sval
) || sval
.value
!= 0)
58 /* It makes it easier to consider NULL pointers as freed. */
62 static int is_freed(struct expression
*expr
)
66 sm
= get_sm_state_expr(my_id
, expr
);
67 if (sm
&& slist_has_state(sm
->possible
, &freed
))
72 static void match_symbol(struct expression
*expr
)
74 struct expression
*parent
;
77 if (is_impossible_path())
79 if (__in_fake_parameter_assign
)
82 parent
= expr_get_parent_expr(expr
);
83 while (parent
&& parent
->type
== EXPR_PREOP
&& parent
->op
== '(')
84 parent
= expr_get_parent_expr(parent
);
85 if (parent
&& parent
->type
== EXPR_PREOP
&& parent
->op
== '&')
90 name
= expr_to_var(expr
);
91 sm_warning("'%s' was already freed.", name
);
95 static void match_dereferences(struct expression
*expr
)
99 if (expr
->type
!= EXPR_PREOP
)
102 if (is_impossible_path())
104 if (__in_fake_parameter_assign
)
107 expr
= strip_expr(expr
->unop
);
110 name
= expr_to_var(expr
);
111 sm_error("dereferencing freed memory '%s'", name
);
112 set_state_expr(my_id
, expr
, &ok
);
116 static int ignored_params
[16];
118 static void set_ignored_params(struct expression
*call
)
120 struct expression
*arg
;
124 memset(&ignored_params
, 0, sizeof(ignored_params
));
127 FOR_EACH_PTR(call
->args
, arg
) {
129 if (arg
->type
!= EXPR_STRING
)
132 } END_FOR_EACH_PTR(arg
);
138 p
= arg
->string
->data
;
139 while ((p
= strchr(p
, '%'))) {
140 if (i
>= ARRAY_SIZE(ignored_params
))
152 ignored_params
[i
] = 1;
157 static int is_free_func(struct expression
*fn
)
162 name
= expr_to_str(fn
);
165 if (strstr(name
, "free"))
172 static void match_call(struct expression
*expr
)
174 struct expression
*arg
;
178 if (is_impossible_path())
181 set_ignored_params(expr
);
184 FOR_EACH_PTR(expr
->args
, arg
) {
186 if (!is_pointer(arg
))
190 if (ignored_params
[i
])
193 name
= expr_to_var(arg
);
194 if (is_free_func(expr
->fn
))
195 sm_error("double free of '%s'", name
);
197 sm_warning("passing freed memory '%s'", name
);
198 set_state_expr(my_id
, arg
, &ok
);
200 } END_FOR_EACH_PTR(arg
);
203 static void match_return(struct expression
*expr
)
207 if (is_impossible_path())
215 name
= expr_to_var(expr
);
216 sm_warning("returning freed memory '%s'", name
);
217 set_state_expr(my_id
, expr
, &ok
);
221 static void match_free(const char *fn
, struct expression
*expr
, void *param
)
223 struct expression
*arg
;
225 if (is_impossible_path())
228 arg
= get_argument_from_call_expr(expr
->args
, PTR_INT(param
));
232 char *name
= expr_to_var(arg
);
234 sm_error("double free of '%s'", name
);
237 set_state_expr(my_id
, arg
, &freed
);
240 static void set_param_freed(struct expression
*expr
, int param
, char *key
, char *value
)
242 struct expression
*arg
;
247 while (expr
->type
== EXPR_ASSIGNMENT
)
248 expr
= strip_expr(expr
->right
);
249 if (expr
->type
!= EXPR_CALL
)
252 arg
= get_argument_from_call_expr(expr
->args
, param
);
255 name
= get_variable_from_key(arg
, key
, &sym
);
259 if (!is_impossible_path()) {
260 sm
= get_sm_state(my_id
, name
, sym
);
261 if (sm
&& slist_has_state(sm
->possible
, &freed
)) {
262 sm_warning("'%s' double freed", name
);
263 set_state(my_id
, name
, sym
, &ok
); /* fixme: doesn't silence anything. I know */
267 set_state(my_id
, name
, sym
, &freed
);
272 int parent_is_free_var_sym_strict(const char *name
, struct symbol
*sym
)
277 struct smatch_state
*state
;
279 strncpy(buf
, name
, sizeof(buf
) - 1);
280 buf
[sizeof(buf
) - 1] = '\0';
283 while ((*start
== '&'))
286 while ((end
= strrchr(start
, '-'))) {
288 state
= __get_state(my_id
, start
, sym
);
295 int parent_is_free_strict(struct expression
*expr
)
301 expr
= strip_expr(expr
);
302 var
= expr_to_var_sym(expr
, &sym
);
305 ret
= parent_is_free_var_sym_strict(var
, sym
);
311 static void match_untracked(struct expression
*call
, int param
)
313 struct state_list
*slist
= NULL
;
314 struct expression
*arg
;
320 arg
= get_argument_from_call_expr(call
->args
, param
);
324 name
= expr_to_var(arg
);
327 snprintf(buf
, sizeof(buf
), "%s->", name
);
331 FOR_EACH_MY_SM(my_id
, __get_cur_stree(), sm
) {
332 if (strncmp(sm
->name
, buf
, len
) == 0)
333 add_ptr_list(&slist
, sm
);
334 } END_FOR_EACH_SM(sm
);
336 FOR_EACH_PTR(slist
, sm
) {
337 set_state(sm
->owner
, sm
->name
, sm
->sym
, &ok
);
338 } END_FOR_EACH_PTR(sm
);
343 void check_free_strict(int id
)
347 if (option_project
!= PROJ_KERNEL
)
350 add_function_hook("kfree", &match_free
, INT_PTR(0));
351 add_function_hook("kmem_cache_free", &match_free
, INT_PTR(1));
354 add_hook(&match_symbol
, SYM_HOOK
);
355 add_hook(&match_dereferences
, DEREF_HOOK
);
356 add_hook(&match_call
, FUNCTION_CALL_HOOK
);
357 add_hook(&match_return
, RETURN_HOOK
);
359 add_modification_hook_late(my_id
, &ok_to_use
);
360 add_pre_merge_hook(my_id
, &pre_merge_hook
);
361 add_unmatched_state_hook(my_id
, &unmatched_state
);
363 select_return_states_hook(PARAM_FREED
, &set_param_freed
);
364 add_untracked_param_hook(&match_untracked
);