Add scoped_state() and add_scope_hook().
[smatch.git] / check_memory.c
blobab4cd2ce28113e7deeba260360dafb6e775517ee
1 /*
2 * sparse/check_memory.c
4 * Copyright (C) 2008 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
10 #include <fcntl.h>
11 #include <unistd.h>
12 #include "parse.h"
13 #include "smatch.h"
14 #include "smatch_slist.h"
16 static int my_id;
18 STATE(allocated);
19 STATE(assigned);
20 STATE(isfree);
21 STATE(malloced);
22 STATE(isnull);
23 STATE(unfree);
26 malloced --> allocated --> assigned --> isfree +
27 \-> isnull. \-> isfree +
29 isfree --> unfree.
30 \-> isnull.
33 static struct tracker_list *arguments;
35 static const char *allocation_funcs[] = {
36 "malloc",
37 "kmalloc",
38 "kzalloc",
39 NULL,
42 static char *get_parent_name(struct symbol *sym)
44 static char buf[256];
46 if (!sym || !sym->ident)
47 return NULL;
49 snprintf(buf, 255, "-%s", sym->ident->name);
50 buf[255] = '\0';
51 return alloc_string(buf);
54 static int is_parent_sym(const char *name)
56 if (!strncmp(name, "-", 1))
57 return 1;
58 return 0;
61 static struct smatch_state *unmatched_state(struct sm_state *sm)
63 if (is_parent_sym(sm->name))
64 return &assigned;
65 return &undefined;
68 static void assign_parent(struct symbol *sym)
70 char *name;
72 name = get_parent_name(sym);
73 if (!name)
74 return;
75 set_state(name, my_id, sym, &assigned);
76 free_string(name);
79 static int parent_is_assigned(struct symbol *sym)
81 struct smatch_state *state;
82 char *name;
84 name = get_parent_name(sym);
85 if (!name)
86 return 0;
87 state = get_state(name, my_id, sym);
88 free_string(name);
89 if (state == &assigned)
90 return 1;
91 return 0;
94 static int is_allocation(struct expression *expr)
96 char *fn_name;
97 int i;
99 if (expr->type != EXPR_CALL)
100 return 0;
102 if (!(fn_name = get_variable_from_expr(expr->fn, NULL)))
103 return 0;
105 for (i = 0; allocation_funcs[i]; i++) {
106 if (!strcmp(fn_name, allocation_funcs[i])) {
107 free_string(fn_name);
108 return 1;
111 free_string(fn_name);
112 return 0;
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)) {
121 return 1;
123 return 0;
126 static int is_argument(struct symbol *sym)
128 struct tracker *arg;
130 FOR_EACH_PTR(arguments, arg) {
131 if (arg->sym == sym)
132 return 1;
133 } END_FOR_EACH_PTR(arg);
134 return 0;
137 static void match_function_def(struct symbol *sym)
139 struct symbol *arg;
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)
148 const char *name;
150 if ((get_base_type(sym))->type == SYM_ARRAY) {
151 return;
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);
160 } else {
161 assign_parent(sym);
166 static int is_parent(struct expression *expr)
168 if (expr->type == EXPR_DEREF)
169 return 0;
170 return 1;
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);
190 goto exit;
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);
208 exit:
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"))
219 return 1;
220 return 0;
223 static void match_free_func(const char *fn, struct expression *expr, void *data)
225 struct expression *ptr_expr;
226 char *ptr_name;
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)
245 return 1;
246 if (tmp->state == &malloced)
247 return 1;
248 } END_FOR_EACH_PTR(tmp);
249 return 0;
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);
265 free_slist(&slist);
268 static void match_return(struct statement *stmt)
270 char *name;
271 struct symbol *sym;
273 name = get_variable_from_expr_complex(stmt->ret_value, &sym);
274 if (sym)
275 assign_parent(sym);
276 free_string(name);
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);
286 if (!tmp) {
287 return;
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 */
296 return;
298 set_true_false_states(name, my_id, sym, &allocated, &isnull);
301 static void match_condition(struct expression *expr)
303 struct symbol *sym;
304 char *name;
306 expr = strip_expr(expr);
307 switch(expr->type) {
308 case EXPR_PREOP:
309 case EXPR_SYMBOL:
310 case EXPR_DEREF:
311 name = get_variable_from_expr_complex(expr, &sym);
312 if (!name)
313 return;
314 set_new_true_false_paths(name, sym);
315 free_string(name);
316 return;
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);
321 return;
322 default:
323 return;
327 static void match_function_call(struct expression *expr)
329 struct expression *tmp;
330 struct symbol *sym;
331 char *name;
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);
337 if (!name)
338 continue;
339 if ((state = get_sm_state(name, my_id, sym))) {
340 if (possibly_allocated(state->possible)) {
341 set_state(name, my_id, sym, &assigned);
344 assign_parent(sym);
345 free_string(name);
346 } END_FOR_EACH_PTR(tmp);
349 static void match_dereferences(struct expression *expr)
351 char *deref = NULL;
352 struct symbol *sym = NULL;
354 deref = get_variable_from_expr(expr->deref->unop, &sym);
355 if (!deref)
356 return;
357 if (is_freed(deref, sym)) {
358 smatch_msg("error: dereferencing freed memory '%s'", deref);
359 set_state(deref, my_id, sym, &unfree);
361 free_string(deref);
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)
372 struct token *token;
373 const char *func;
374 int arg;
376 token = get_tokens_file("kernel.frees_argument");
377 if (!token)
378 return;
379 if (token_type(token) != TOKEN_STREAMBEGIN)
380 return;
381 token = token->next;
382 while (token_type(token) != TOKEN_STREAMEND) {
383 if (token_type(token) != TOKEN_IDENT)
384 return;
385 func = show_ident(token->ident);
386 token = token->next;
387 if (token_type(token) != TOKEN_NUMBER)
388 return;
389 arg = atoi(token->number);
390 add_function_hook(func, &match_free_func, INT_PTR(arg));
391 token = token->next;
393 clear_token_alloc();
396 void check_memory(int id)
398 my_id = 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();