-
Notifications
You must be signed in to change notification settings - Fork 14
/
check_frees_argument.c
135 lines (114 loc) · 3.19 KB
/
check_frees_argument.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
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* Copyright (C) 2009 Dan Carpenter.
*
* 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 script is for finding functions like hcd_buffer_free() which free
* their arguments. After running it, add those functions to check_memory.c
*/
#include "smatch.h"
#include "smatch_slist.h"
static int my_id;
STATE(freed);
static struct symbol *this_func;
static struct tracker_list *freed_args = NULL;
static void match_function_def(struct symbol *sym)
{
this_func = sym;
}
static int is_arg(char *name, struct symbol *sym)
{
struct symbol *arg;
const char *arg_name;
FOR_EACH_PTR(this_func->ctype.base_type->arguments, arg) {
arg_name = (arg->ident?arg->ident->name:"-");
if (sym == arg && !strcmp(name, arg_name))
return 1;
} END_FOR_EACH_PTR(arg);
return 0;
}
static void match_kfree(const char *fn, struct expression *expr, void *info)
{
struct expression *tmp;
struct symbol *sym;
char *name;
tmp = get_argument_from_call_expr(expr->args, 0);
tmp = strip_expr(tmp);
name = expr_to_var_sym(tmp, &sym);
if (is_arg(name, sym)) {
set_state(my_id, name, sym, &freed);
}
free_string(name);
}
static int return_count = 0;
static void match_return(struct expression *ret_value)
{
struct stree *stree;
struct sm_state *tmp;
struct tracker *tracker;
if (__inline_fn)
return;
if (!return_count) {
stree = __get_cur_stree();
FOR_EACH_MY_SM(my_id, stree, tmp) {
if (tmp->state == &freed)
add_tracker(&freed_args, my_id, tmp->name,
tmp->sym);
} END_FOR_EACH_SM(tmp);
} else {
FOR_EACH_PTR(freed_args, tracker) {
tmp = get_sm_state(my_id, tracker->name, tracker->sym);
if (tmp && tmp->state != &freed)
del_tracker(&freed_args, my_id, tracker->name,
tracker->sym);
} END_FOR_EACH_PTR(tracker);
}
}
static void print_arg(struct symbol *sym)
{
struct symbol *arg;
int i = 0;
FOR_EACH_PTR(this_func->ctype.base_type->arguments, arg) {
if (sym == arg) {
sm_info("free_arg %s %d", get_function(), i);
return;
}
i++;
} END_FOR_EACH_PTR(arg);
}
static void match_after_func(struct symbol *sym)
{
struct tracker *tracker;
if (__inline_fn)
return;
FOR_EACH_PTR(freed_args, tracker) {
print_arg(tracker->sym);
} END_FOR_EACH_PTR(tracker);
free_trackers_and_list(&freed_args);
return_count = 0;
}
void check_frees_argument(int id)
{
if (!option_info)
return;
my_id = id;
add_hook(&match_function_def, FUNC_DEF_HOOK);
if (option_project == PROJ_KERNEL)
add_function_hook("kfree", &match_kfree, NULL);
else
add_function_hook("free", &match_kfree, NULL);
add_hook(&match_return, RETURN_HOOK);
add_hook(&match_after_func, AFTER_FUNC_HOOK);
}