slist: put get_sm_state_stree_stack() in the .h file
[smatch.git] / check_frees_param.c
blobd2f8fec8be3f820bad4c839d5075c7c623e767a9
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 *key, char *unused)
95 /* XXX FIXME: call_implies has been updated with more information */
96 if (strcmp(key, "$$") != 0)
97 return;
98 freed_variable(arg);
101 static void process_states(struct stree *stree)
103 struct symbol *arg;
104 int i;
106 i = -1;
107 FOR_EACH_PTR(cur_func_sym->ctype.base_type->arguments, arg) {
108 i++;
109 if (!arg->ident)
110 continue;
111 if (get_state_stree(stree, my_id, arg->ident->name, arg) == &freed)
112 sql_insert_call_implies(PARAM_FREED, i, "$$", "1");
113 } END_FOR_EACH_PTR(arg);
116 void check_frees_param(int id)
118 my_id = id;
120 add_hook(&match_function_def, FUNC_DEF_HOOK);
122 if (option_project == PROJ_KERNEL) {
123 add_function_hook("kfree", &match_free, INT_PTR(0));
124 add_function_hook("kmem_cache_free", &match_free, INT_PTR(1));
125 } else {
126 add_function_hook("free", &match_free, INT_PTR(0));
129 select_call_implies_hook(PARAM_FREED, &set_param_freed);
130 add_modification_hook(my_id, &set_ignore);
132 all_return_states_hook(&process_states);