forked from error27/smatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_dereferences_param.c
203 lines (168 loc) · 4.47 KB
/
check_dereferences_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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
* Copyright (C) 2012 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 is an --info recipe. The goal is to print a message for every parameter
* which we can not avoid dereferencing. This is maybe a bit restrictive but it
* avoids some false positives.
*/
#include "smatch.h"
#include "smatch_extra.h"
#include "smatch_slist.h"
static int my_id;
STATE(derefed);
STATE(ignore);
STATE(param);
static void pre_merge_hook(struct sm_state *cur, struct sm_state *other)
{
if (cur->state == &derefed || other->state != &derefed)
return;
if (is_impossible_path())
set_state(my_id, cur->name, cur->sym, &derefed);
}
static void set_ignore(struct sm_state *sm, struct expression *mod_expr)
{
if (sm->state == &derefed)
return;
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 int is_ignored_param(struct expression *expr)
{
struct sm_state *sm;
if (param_was_set(expr))
return 1;
sm = get_sm_state_expr(my_id, expr);
if (sm && slist_has_state(sm->possible, &ignore))
return 1;
return 0;
}
static void check_deref(struct expression *expr)
{
struct expression *tmp;
if (is_impossible_path())
return;
tmp = get_assigned_expr(expr);
if (tmp)
expr = tmp;
if (expr->type == EXPR_PREOP &&
expr->op == '&') {
expr = strip_expr(expr->unop);
if (expr->type != EXPR_DEREF)
return;
expr = strip_expr(expr->deref);
if (expr->type != EXPR_PREOP ||
expr->op != '*')
return;
expr = strip_expr(expr->unop);
}
expr = strip_expr(expr);
if (!expr)
return;
if (expr->type == EXPR_PREOP && expr->op == '&')
return;
if (get_param_num(expr) < 0)
return;
if (is_ignored_param(expr))
return;
if (param_was_set(expr))
return;
/*
* At this point we really only care about potential NULL dereferences.
* Potentially in the future we will care about everything.
*/
if (implied_not_equal(expr, 0))
return;
set_state_expr(my_id, expr, &derefed);
}
static void match_dereference(struct expression *expr)
{
if (expr->type != EXPR_PREOP)
return;
check_deref(expr->unop);
}
static void find_inner_dereferences(struct expression *expr)
{
while (expr->type == EXPR_PREOP) {
if (expr->op == '*')
check_deref(expr->unop);
expr = strip_expr(expr->unop);
}
}
static void set_param_dereferenced(struct expression *call, struct expression *arg, char *key, char *unused)
{
struct symbol *sym;
char *name;
check_deref(arg);
name = get_variable_from_key(arg, key, &sym);
if (!name || !sym)
goto free;
if (is_ignored_param(symbol_expression(sym)))
goto free;
if (get_param_num_from_sym(sym) < 0)
goto free;
if (param_was_set_var_sym(name, sym))
goto free;
set_state(my_id, name, sym, &derefed);
find_inner_dereferences(arg);
free:
free_string(name);
}
static void process_states(void)
{
struct sm_state *tmp;
int arg;
const char *name;
FOR_EACH_MY_SM(my_id, __get_cur_stree(), tmp) {
if (tmp->state != &derefed)
continue;
arg = get_param_num_from_sym(tmp->sym);
if (arg < 0)
continue;
name = get_param_name(tmp);
if (!name || name[0] == '&')
continue;
sql_insert_return_implies(DEREFERENCE, arg, name, "1");
} END_FOR_EACH_SM(tmp);
}
static void match_pointer_as_array(struct expression *expr)
{
if (!is_array(expr))
return;
check_deref(get_array_base(expr));
}
void check_dereferences_param(int id)
{
my_id = id;
add_hook(&match_function_def, FUNC_DEF_HOOK);
add_hook(&match_dereference, DEREF_HOOK);
add_hook(&match_pointer_as_array, OP_HOOK);
select_return_implies_hook(DEREFERENCE, &set_param_dereferenced);
add_modification_hook(my_id, &set_ignore);
add_pre_merge_hook(my_id, &pre_merge_hook);
all_return_states_hook(&process_states);
}