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"
32 static void ok_to_use(struct sm_state
*sm
, struct expression
*mod_expr
)
35 set_state(my_id
, sm
->name
, sm
->sym
, &ok
);
38 static void pre_merge_hook(struct sm_state
*sm
)
40 if (is_impossible_path())
41 set_state(my_id
, sm
->name
, sm
->sym
, &ok
);
44 static int is_freed(struct expression
*expr
)
48 sm
= get_sm_state_expr(my_id
, expr
);
49 if (sm
&& slist_has_state(sm
->possible
, &freed
))
54 static void match_symbol(struct expression
*expr
)
56 struct expression
*parent
;
59 if (is_impossible_path())
62 parent
= expr_get_parent_expr(expr
);
63 while (parent
&& parent
->type
== EXPR_PREOP
&& parent
->op
== '(')
64 parent
= expr_get_parent_expr(parent
);
65 if (parent
&& parent
->type
== EXPR_PREOP
&& parent
->op
== '&')
70 name
= expr_to_var(expr
);
71 sm_msg("warn: '%s' was already freed.", name
);
75 static void match_dereferences(struct expression
*expr
)
79 if (expr
->type
!= EXPR_PREOP
)
82 if (is_impossible_path())
85 expr
= strip_expr(expr
->unop
);
88 name
= expr_to_var(expr
);
89 sm_msg("error: dereferencing freed memory '%s'", name
);
90 set_state_expr(my_id
, expr
, &ok
);
94 static int ignored_params
[16];
96 static void set_ignored_params(struct expression
*call
)
98 struct expression
*arg
;
102 memset(&ignored_params
, 0, sizeof(ignored_params
));
105 FOR_EACH_PTR(call
->args
, arg
) {
107 if (arg
->type
!= EXPR_STRING
)
110 } END_FOR_EACH_PTR(arg
);
116 p
= arg
->string
->data
;
117 while ((p
= strchr(p
, '%'))) {
118 if (i
>= ARRAY_SIZE(ignored_params
))
130 ignored_params
[i
] = 1;
135 static int is_free_func(struct expression
*fn
)
140 name
= expr_to_str(fn
);
143 if (strstr(name
, "free"))
150 static void match_call(struct expression
*expr
)
152 struct expression
*arg
;
156 if (is_impossible_path())
159 set_ignored_params(expr
);
162 FOR_EACH_PTR(expr
->args
, arg
) {
164 if (!is_pointer(arg
))
168 if (ignored_params
[i
])
171 name
= expr_to_var(arg
);
172 if (is_free_func(expr
->fn
))
173 sm_msg("error: double free of '%s'", name
);
175 sm_msg("warn: passing freed memory '%s'", name
);
176 set_state_expr(my_id
, arg
, &ok
);
178 } END_FOR_EACH_PTR(arg
);
181 static void match_return(struct expression
*expr
)
185 if (is_impossible_path())
193 name
= expr_to_var(expr
);
194 sm_msg("warn: returning freed memory '%s'", name
);
195 set_state_expr(my_id
, expr
, &ok
);
199 static void match_free(const char *fn
, struct expression
*expr
, void *param
)
201 struct expression
*arg
;
203 if (is_impossible_path())
206 arg
= get_argument_from_call_expr(expr
->args
, PTR_INT(param
));
210 char *name
= expr_to_var(arg
);
212 sm_msg("error: double free of '%s'", name
);
215 set_state_expr(my_id
, arg
, &freed
);
218 static void set_param_freed(struct expression
*arg
, char *key
, char *unused
)
223 name
= get_variable_from_key(arg
, key
, &sym
);
227 set_state(my_id
, name
, sym
, &freed
);
232 int parent_is_free_var_sym(const char *name
, struct symbol
*sym
)
237 struct smatch_state
*state
;
239 strncpy(buf
, name
, sizeof(buf
) - 1);
240 buf
[sizeof(buf
) - 1] = '\0';
243 while ((*start
== '&'))
246 while ((end
= strrchr(start
, '-'))) {
248 state
= get_state(my_id
, start
, sym
);
255 int parent_is_free(struct expression
*expr
)
261 expr
= strip_expr(expr
);
262 var
= expr_to_var_sym(expr
, &sym
);
265 ret
= parent_is_free_var_sym(var
, sym
);
271 void check_free(int id
)
275 if (option_project
== PROJ_KERNEL
) {
276 add_function_hook("kfree", &match_free
, INT_PTR(0));
277 add_function_hook("kmem_cache_free", &match_free
, INT_PTR(1));
279 add_function_hook("free", &match_free
, INT_PTR(0));
283 add_hook(&match_symbol
, SYM_HOOK
);
284 add_hook(&match_dereferences
, DEREF_HOOK
);
285 add_hook(&match_call
, FUNCTION_CALL_HOOK
);
286 add_hook(&match_return
, RETURN_HOOK
);
288 add_modification_hook(my_id
, &ok_to_use
);
289 select_call_implies_hook(PARAM_FREED
, &set_param_freed
);
290 add_pre_merge_hook(my_id
, &pre_merge_hook
);