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.
25 #include "smatch_extra.h"
26 #include "smatch_slist.h"
34 static int is_arg(struct expression
*expr
)
38 if (expr
->type
!= EXPR_SYMBOL
)
41 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
42 if (arg
== expr
->symbol
)
44 } END_FOR_EACH_PTR(arg
);
48 static void set_ignore(struct sm_state
*sm
, struct expression
*mod_expr
)
50 if (sm
->state
== &freed
)
52 set_state(my_id
, sm
->name
, sm
->sym
, &ignore
);
55 static void match_function_def(struct symbol
*sym
)
61 FOR_EACH_PTR(sym
->ctype
.base_type
->arguments
, arg
) {
65 set_state(my_id
, arg
->ident
->name
, arg
, ¶m
);
66 } END_FOR_EACH_PTR(arg
);
69 static void freed_variable(struct expression
*expr
)
73 expr
= strip_expr(expr
);
77 sm
= get_sm_state_expr(my_id
, expr
);
78 if (sm
&& slist_has_state(sm
->possible
, &ignore
))
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
));
93 static void set_param_freed(struct expression
*arg
, char *unused
)
98 static void process_states(struct state_list
*slist
)
104 FOR_EACH_PTR(cur_func_sym
->ctype
.base_type
->arguments
, arg
) {
108 if (get_state_slist(slist
, 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
)
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));
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
);