-
Notifications
You must be signed in to change notification settings - Fork 14
/
check_free.c
315 lines (259 loc) · 6.03 KB
/
check_free.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
* Copyright (C) 2010 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
*/
/*
* check_memory() is getting too big and messy.
*
*/
#include <string.h>
#include "smatch.h"
#include "smatch_slist.h"
#include "smatch_extra.h"
static int my_id;
STATE(freed);
STATE(ok);
static void ok_to_use(struct sm_state *sm, struct expression *mod_expr)
{
if (sm->state != &ok)
set_state(my_id, sm->name, sm->sym, &ok);
}
static void pre_merge_hook(struct sm_state *cur, struct sm_state *other)
{
if (is_impossible_path())
set_state(my_id, cur->name, cur->sym, &ok);
}
static int is_freed(struct expression *expr)
{
struct sm_state *sm;
sm = get_sm_state_expr(my_id, expr);
if (sm && slist_has_state(sm->possible, &freed))
return 1;
return 0;
}
static void match_symbol(struct expression *expr)
{
struct expression *parent;
char *name;
if (is_impossible_path())
return;
if (__in_fake_parameter_assign)
return;
if (is_part_of_condition(expr))
return;
parent = expr_get_parent_expr(expr);
while (parent && parent->type == EXPR_PREOP && parent->op == '(')
parent = expr_get_parent_expr(parent);
if (parent && parent->type == EXPR_PREOP && parent->op == '&')
return;
if (!is_freed(expr))
return;
if (is_percent_p_print(expr))
return;
name = expr_to_var(expr);
sm_warning("'%s' was already freed.", name);
free_string(name);
}
static void match_dereferences(struct expression *expr)
{
char *name;
if (__in_fake_parameter_assign)
return;
if (expr->type != EXPR_PREOP)
return;
if (is_impossible_path())
return;
expr = strip_expr(expr->unop);
if (!is_freed(expr))
return;
name = expr_to_var(expr);
sm_error("dereferencing freed memory '%s'", name);
set_state_expr(my_id, expr, &ok);
free_string(name);
}
static int ignored_params[16];
static void set_ignored_params(struct expression *call)
{
struct expression *arg;
const char *p;
int i;
memset(&ignored_params, 0, sizeof(ignored_params));
i = -1;
FOR_EACH_PTR(call->args, arg) {
i++;
if (arg->type != EXPR_STRING)
continue;
goto found;
} END_FOR_EACH_PTR(arg);
return;
found:
i++;
p = arg->string->data;
while ((p = strchr(p, '%'))) {
if (i >= ARRAY_SIZE(ignored_params))
return;
p++;
if (*p == '%') {
p++;
continue;
}
if (*p == '.')
p++;
if (*p == '*')
i++;
if (*p == 'p')
ignored_params[i] = 1;
i++;
}
}
static int is_free_func(struct expression *fn)
{
char *name;
int ret = 0;
name = expr_to_str(fn);
if (!name)
return 0;
if (strstr(name, "free"))
ret = 1;
free_string(name);
return ret;
}
static void match_call(struct expression *expr)
{
struct expression *arg;
char *name;
int i;
if (is_impossible_path())
return;
set_ignored_params(expr);
i = -1;
FOR_EACH_PTR(expr->args, arg) {
i++;
if (!is_pointer(arg))
continue;
if (!is_freed(arg))
continue;
if (ignored_params[i])
continue;
if (is_percent_p_print(arg))
continue;
name = expr_to_var(arg);
if (is_free_func(expr->fn))
sm_error("double free of '%s'", name);
else
sm_warning("passing freed memory '%s'", name);
set_state_expr(my_id, arg, &ok);
free_string(name);
} END_FOR_EACH_PTR(arg);
}
static void match_return(struct expression *expr)
{
char *name;
if (is_impossible_path())
return;
if (__in_fake_parameter_assign)
return;
if (!expr)
return;
if (!is_freed(expr))
return;
name = expr_to_var(expr);
sm_warning("returning freed memory '%s'", name);
set_state_expr(my_id, expr, &ok);
free_string(name);
}
static void match_free(const char *fn, struct expression *expr, void *param)
{
struct expression *arg;
if (is_impossible_path())
return;
arg = get_argument_from_call_expr(expr->args, PTR_INT(param));
if (!arg)
return;
if (is_freed(arg)) {
char *name = expr_to_var(arg);
sm_error("double free of '%s'", name);
free_string(name);
}
set_state_expr(my_id, arg, &freed);
}
static void set_param_freed(struct expression *call, struct expression *arg, char *key, char *unused)
{
struct symbol *sym;
char *name;
name = get_variable_from_key(arg, key, &sym);
if (!name || !sym)
goto free;
set_state(my_id, name, sym, &freed);
free:
free_string(name);
}
int parent_is_free_var_sym(const char *name, struct symbol *sym)
{
char buf[256];
char *start;
char *end;
struct smatch_state *state;
char orig;
if (option_project == PROJ_KERNEL)
return parent_is_free_var_sym_strict(name, sym);
strncpy(buf, name, sizeof(buf) - 1);
buf[sizeof(buf) - 1] = '\0';
start = &buf[0];
while ((*start == '&'))
start++;
end = start;
while ((end = strrchr(end, '-'))) {
orig = *end;
*end = '\0';
state = __get_state(my_id, start, sym);
if (state == &freed)
return 1;
*end = orig;
end++;
}
return 0;
}
int parent_is_free(struct expression *expr)
{
struct symbol *sym;
char *var;
int ret = 0;
expr = strip_expr(expr);
var = expr_to_var_sym(expr, &sym);
if (!var || !sym)
goto free;
ret = parent_is_free_var_sym(var, sym);
free:
free_string(var);
return ret;
}
void check_free(int id)
{
my_id = id;
if (option_project == PROJ_KERNEL) {
/* The kernel use check_free_strict.c */
return;
}
add_function_hook("free", &match_free, INT_PTR(0));
if (option_spammy)
add_hook(&match_symbol, SYM_HOOK);
add_hook(&match_dereferences, DEREF_HOOK);
add_hook(&match_call, FUNCTION_CALL_HOOK);
add_hook(&match_return, RETURN_HOOK);
add_modification_hook(my_id, &ok_to_use);
select_return_implies_hook(PARAM_FREED, &set_param_freed);
add_pre_merge_hook(my_id, &pre_merge_hook);
}