forked from error27/smatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_double_checking.c
284 lines (234 loc) · 5.95 KB
/
check_double_checking.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
/*
* 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
*/
#define _GNU_SOURCE
#include <string.h>
#include "smatch.h"
#include "smatch_slist.h"
static int my_id;
STATE(checked);
STATE(modified);
struct stree *to_check;
static struct statement *get_cur_stmt(void)
{
return last_ptr_list((struct ptr_list *)big_statement_stack);
}
static void set_modified(struct sm_state *sm, struct expression *mod_expr)
{
set_state(my_id, sm->name, sm->sym, &modified);
}
static struct expression *strip_condition(struct expression *expr)
{
expr = strip_expr(expr);
if (expr->type == EXPR_PREOP && expr->op == '!')
return strip_condition(expr->unop);
if (expr->type == EXPR_COMPARE &&
(expr->op == SPECIAL_EQUAL ||
expr->op == SPECIAL_NOTEQUAL)) {
if (expr_is_zero(expr->left))
return strip_condition(expr->right);
if (expr_is_zero(expr->right))
return strip_condition(expr->left);
}
return expr;
}
static int conditions_match(struct expression *cond, struct expression *prev)
{
prev = strip_condition(prev);
if (prev == cond)
return 1;
if (prev->type == EXPR_LOGICAL) {
if (conditions_match(cond, prev->left) ||
conditions_match(cond, prev->right))
return 1;
}
return 0;
}
/*
* People like to do "if (foo) { ... } else if (!foo) { ... }". Don't
* complain when they do that even though it is nonsense.
*/
static int is_obvious_else(struct expression *cond)
{
struct statement *parent;
struct expression *prev;
if (!get_cur_stmt())
return 0;
parent = get_cur_stmt()->parent;
if (!parent)
return 0;
if (parent->type != STMT_IF)
return 0;
if (!parent->if_false)
return 0;
if (parent->if_false != get_cur_stmt())
return 0;
prev = strip_condition(parent->if_conditional);
return conditions_match(cond, prev);
}
static int name_means_synchronize(const char *name)
{
if (!name)
return 0;
if (strcasestr(name, "wait"))
return 1;
if (strcasestr(name, "down"))
return 1;
if (strcasestr(name, "lock") && !strcasestr(name, "unlock"))
return 1;
if (strcasestr(name, "delay"))
return 1;
if (strcasestr(name, "schedule"))
return 1;
if (strcmp(name, "smp_rmb") == 0)
return 1;
if (strcmp(name, "mb") == 0)
return 1;
if (strcmp(name, "barrier") == 0)
return 1;
return 0;
}
static int previous_statement_was_synchronize(void)
{
struct statement *stmt;
struct position pos;
struct position prev_pos;
char *ident;
if (!__cur_stmt)
return 0;
if (__prev_stmt) {
prev_pos = __prev_stmt->pos;
prev_pos.line -= 3;
} else {
prev_pos = __cur_stmt->pos;
prev_pos.line -= 5;
}
FOR_EACH_PTR_REVERSE(big_statement_stack, stmt) {
if (stmt->pos.line < prev_pos.line)
return 0;
pos = stmt->pos;
ident = get_macro_name(pos);
if (name_means_synchronize(ident))
return 1;
ident = pos_ident(pos);
if (!ident)
continue;
if (strcmp(ident, "if") == 0) {
pos.pos += 4;
ident = pos_ident(pos);
if (!ident)
continue;
}
if (name_means_synchronize(ident))
return 1;
} END_FOR_EACH_PTR_REVERSE(stmt);
return 0;
}
static void match_condition(struct expression *expr)
{
struct smatch_state *state;
sval_t dummy;
char *name;
if (inside_loop())
return;
if (get_value(expr, &dummy))
return;
if (get_macro_name(expr->pos))
return;
state = get_stored_condition(expr);
if (!state || !state->data)
return;
if (get_macro_name(((struct expression *)state->data)->pos))
return;
/*
* we allow double checking for NULL because people do this all the time
* and trying to stop them is a losers' battle.
*/
if (is_pointer(expr) && implied_condition_true(expr))
return;
if (definitely_inside_loop()) {
struct symbol *sym;
if (__inline_fn)
return;
name = expr_to_var_sym(expr, &sym);
if (!name)
return;
set_state_expr(my_id, expr, &checked);
set_state_stree(&to_check, my_id, name, sym, &checked);
free_string(name);
return;
}
if (is_obvious_else(state->data))
return;
/*
* It's common to test something, then take a lock and test if it is
* still true.
*/
if (previous_statement_was_synchronize())
return;
name = expr_to_str(expr);
sm_warning("we tested '%s' before and it was '%s'", name, state->name);
free_string(name);
}
int get_check_line(struct sm_state *sm)
{
struct sm_state *tmp;
FOR_EACH_PTR(sm->possible, tmp) {
if (tmp->state == &checked)
return tmp->line;
} END_FOR_EACH_PTR(tmp);
return get_lineno();
}
static void after_loop(struct statement *stmt)
{
struct sm_state *check, *sm;
if (!stmt || stmt->type != STMT_ITERATOR)
return;
if (definitely_inside_loop())
return;
if (__inline_fn)
return;
FOR_EACH_SM(to_check, check) {
continue;
sm = get_sm_state(my_id, check->name, check->sym);
continue;
if (!sm)
continue;
if (slist_has_state(sm->possible, &modified))
continue;
sm_printf("%s:%d %s() ", get_filename(), get_check_line(sm), get_function());
sm_printf("warn: we tested '%s' already\n", check->name);
} END_FOR_EACH_SM(check);
free_stree(&to_check);
}
static void match_func_end(struct symbol *sym)
{
if (__inline_fn)
return;
if (to_check)
sm_msg("debug: odd... found an function without an end.");
free_stree(&to_check);
}
void check_double_checking(int id)
{
my_id = id;
if (!option_spammy)
return;
add_hook(&match_condition, CONDITION_HOOK);
add_modification_hook(my_id, &set_modified);
add_hook(after_loop, STMT_HOOK_AFTER);
add_hook(&match_func_end, AFTER_FUNC_HOOK);
}