-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcheck_frees_param.c
122 lines (101 loc) · 2.97 KB
/
check_frees_param.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* Copyright (C) 2014 Oracle.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
*/
/*
* This file is sort of like check_dereferences_param.c. In theory the one
* difference should be that the param is NULL it should still be counted as a
* free. But for now I don't handle that case.
*/
#include "smatch.h"
#include "smatch_extra.h"
#include "smatch_slist.h"
static int my_id;
STATE(freed);
STATE(ignore);
STATE(param);
static void set_ignore(struct sm_state *sm, struct expression *mod_expr)
{
set_state(my_id, sm->name, sm->sym, &ignore);
}
static void match_function_def(struct symbol *sym)
{
struct symbol *arg;
int i;
i = -1;
FOR_EACH_PTR(sym->ctype.base_type->arguments, arg) {
i++;
if (!arg->ident)
continue;
set_state(my_id, arg->ident->name, arg, ¶m);
} END_FOR_EACH_PTR(arg);
}
static void freed_variable(struct expression *expr)
{
struct sm_state *sm;
expr = strip_expr(expr);
if (get_param_num(expr) < 0)
return;
sm = get_sm_state_expr(my_id, expr);
if (sm && slist_has_state(sm->possible, &ignore))
return;
set_state_expr(my_id, expr, &freed);
}
static void match_free(const char *fn, struct expression *expr, void *param)
{
struct expression *arg;
arg = get_argument_from_call_expr(expr->args, PTR_INT(param));
if (!arg)
return;
freed_variable(arg);
}
static void set_param_freed(struct expression *call, struct expression *arg, char *key, char *unused)
{
/* XXX FIXME: call_implies has been updated with more information */
if (strcmp(key, "$") != 0)
return;
freed_variable(arg);
}
static void process_states(void)
{
struct sm_state *sm;
int param;
const char *param_name;
FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
if (sm->state != &freed)
continue;
param = get_param_num_from_sym(sm->sym);
if (param < 0)
continue;
param_name = get_param_name(sm);
if (!param_name)
continue;
sql_insert_call_implies(PARAM_FREED, param, param_name, "1");
} END_FOR_EACH_SM(sm);
}
void check_frees_param(int id)
{
my_id = id;
add_hook(&match_function_def, FUNC_DEF_HOOK);
if (option_project == PROJ_KERNEL) {
add_function_hook("kfree", &match_free, INT_PTR(0));
add_function_hook("kmem_cache_free", &match_free, INT_PTR(1));
} else {
add_function_hook("free", &match_free, INT_PTR(0));
}
select_call_implies_hook(PARAM_FREED, &set_param_freed);
add_modification_hook(my_id, &set_ignore);
all_return_states_hook(&process_states);
}