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
)
58 if (is_impossible_path())
63 name
= expr_to_var(expr
);
64 sm_msg("warn: '%s' was already freed.", name
);
68 static void match_dereferences(struct expression
*expr
)
72 if (expr
->type
!= EXPR_PREOP
)
75 if (is_impossible_path())
78 expr
= strip_expr(expr
->unop
);
81 name
= expr_to_var(expr
);
82 sm_msg("error: dereferencing freed memory '%s'", name
);
83 set_state_expr(my_id
, expr
, &ok
);
87 static int ignored_params
[16];
89 static void set_ignored_params(struct expression
*call
)
91 struct expression
*arg
;
95 memset(&ignored_params
, 0, sizeof(ignored_params
));
98 FOR_EACH_PTR(call
->args
, arg
) {
100 if (arg
->type
!= EXPR_STRING
)
103 } END_FOR_EACH_PTR(arg
);
109 p
= arg
->string
->data
;
110 while ((p
= strchr(p
, '%'))) {
111 if (i
>= ARRAY_SIZE(ignored_params
))
123 ignored_params
[i
] = 1;
128 static int is_free_func(struct expression
*fn
)
133 name
= expr_to_str(fn
);
136 if (strstr(name
, "free"))
143 static void match_call(struct expression
*expr
)
145 struct expression
*arg
;
149 if (is_impossible_path())
152 set_ignored_params(expr
);
155 FOR_EACH_PTR(expr
->args
, arg
) {
157 if (!is_pointer(arg
))
161 if (ignored_params
[i
])
164 name
= expr_to_var(arg
);
165 if (is_free_func(expr
->fn
))
166 sm_msg("error: double free of '%s'", name
);
168 sm_msg("warn: passing freed memory '%s'", name
);
169 set_state_expr(my_id
, arg
, &ok
);
171 } END_FOR_EACH_PTR(arg
);
174 static void match_return(struct expression
*expr
)
178 if (is_impossible_path())
186 name
= expr_to_var(expr
);
187 sm_msg("warn: returning freed memory '%s'", name
);
188 set_state_expr(my_id
, expr
, &ok
);
192 static void match_free(const char *fn
, struct expression
*expr
, void *param
)
194 struct expression
*arg
;
196 if (is_impossible_path())
199 arg
= get_argument_from_call_expr(expr
->args
, PTR_INT(param
));
203 char *name
= expr_to_var(arg
);
205 sm_msg("error: double free of '%s'", name
);
208 set_state_expr(my_id
, arg
, &freed
);
211 static void set_param_freed(struct expression
*arg
, char *key
, char *unused
)
216 name
= get_variable_from_key(arg
, key
, &sym
);
220 set_state(my_id
, name
, sym
, &freed
);
225 int parent_is_free_var_sym(const char *name
, struct symbol
*sym
)
230 struct smatch_state
*state
;
232 strncpy(buf
, name
, sizeof(buf
) - 1);
233 buf
[sizeof(buf
) - 1] = '\0';
236 while ((*start
== '&'))
239 while ((end
= strrchr(start
, '-'))) {
241 state
= get_state(my_id
, start
, sym
);
248 int parent_is_free(struct expression
*expr
)
254 expr
= strip_expr(expr
);
255 var
= expr_to_var_sym(expr
, &sym
);
258 ret
= parent_is_free_var_sym(var
, sym
);
264 void check_free(int id
)
268 if (option_project
== PROJ_KERNEL
) {
269 add_function_hook("kfree", &match_free
, INT_PTR(0));
270 add_function_hook("kmem_cache_free", &match_free
, INT_PTR(1));
272 add_function_hook("free", &match_free
, INT_PTR(0));
276 add_hook(&match_symbol
, SYM_HOOK
);
277 add_hook(&match_dereferences
, DEREF_HOOK
);
278 add_hook(&match_call
, FUNCTION_CALL_HOOK
);
279 add_hook(&match_return
, RETURN_HOOK
);
281 add_modification_hook(my_id
, &ok_to_use
);
282 select_call_implies_hook(PARAM_FREED
, &set_param_freed
);
283 add_pre_merge_hook(my_id
, &pre_merge_hook
);