db: get return states from function pointers if possible
[smatch.git] / check_frees_param.c
blob828d7506d46ccb61ee5e9f061949df1ce340a46d
1 /*
2 * Copyright (C) 2014 Oracle.
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 * This file is sort of like check_dereferences_param.c. In theory the one
20 * difference should be that the param is NULL it should still be counted as a
21 * free. But for now I don't handle that case.
24 #include "smatch.h"
25 #include "smatch_extra.h"
26 #include "smatch_slist.h"
28 static int my_id;
30 STATE(freed);
31 STATE(ignore);
32 STATE(param);
34 static int is_arg(struct expression *expr)
36 struct symbol *arg;
38 if (expr->type != EXPR_SYMBOL)
39 return 0;
41 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
42 if (arg == expr->symbol)
43 return 1;
44 } END_FOR_EACH_PTR(arg);
45 return 0;
48 static void set_ignore(struct sm_state *sm, struct expression *mod_expr)
50 if (sm->state == &freed)
51 return;
52 set_state(my_id, sm->name, sm->sym, &ignore);
55 static void match_function_def(struct symbol *sym)
57 struct symbol *arg;
58 int i;
60 i = -1;
61 FOR_EACH_PTR(sym->ctype.base_type->arguments, arg) {
62 i++;
63 if (!arg->ident)
64 continue;
65 set_state(my_id, arg->ident->name, arg, &param);
66 } END_FOR_EACH_PTR(arg);
69 static void freed_variable(struct expression *expr)
71 struct sm_state *sm;
73 expr = strip_expr(expr);
74 if (!is_arg(expr))
75 return;
77 sm = get_sm_state_expr(my_id, expr);
78 if (sm && slist_has_state(sm->possible, &ignore))
79 return;
80 set_state_expr(my_id, expr, &freed);
83 static void match_free(const char *fn, struct expression *expr, void *param)
85 struct expression *arg;
87 arg = get_argument_from_call_expr(expr->args, PTR_INT(param));
88 if (!arg)
89 return;
90 freed_variable(arg);
93 static void set_param_freed(struct expression *arg, char *unused)
95 freed_variable(arg);
98 static void process_states(struct stree *stree)
100 struct symbol *arg;
101 int i;
103 i = -1;
104 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
105 i++;
106 if (!arg->ident)
107 continue;
108 if (get_state_stree(stree, my_id, arg->ident->name, arg) == &freed)
109 sql_insert_call_implies(PARAM_FREED, i, 1);
110 } END_FOR_EACH_PTR(arg);
113 void check_frees_param(int id)
115 my_id = id;
117 add_hook(&match_function_def, FUNC_DEF_HOOK);
119 if (option_project == PROJ_KERNEL) {
120 add_function_hook("kfree", &match_free, INT_PTR(0));
121 add_function_hook("kmem_cache_free", &match_free, INT_PTR(1));
122 } else {
123 add_function_hook("free", &match_free, INT_PTR(0));
126 select_call_implies_hook(PARAM_FREED, &set_param_freed);
127 add_modification_hook(my_id, &set_ignore);
129 all_return_states_hook(&process_states);