-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsmatch_ssa.c
303 lines (251 loc) · 7.36 KB
/
smatch_ssa.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
/*
* Copyright (C) 2021 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
*/
/*
* The truth is I don't know very much about compilers. I've heard about
* Single Static Assignment and it seems like a useful feature but it's also
* possible I have misunderstood the whole thing.
*
* Anyway, the point is that say we have code like:
* ret = alloc();
* if (ret < 0)
* return ret;
* p->foo = ret;
* ret = something else;
*
* So the problem here is "p->foo" and "ret" are equivalent at first but then
* not at the end. We don't really care if "p->foo" is freed or "ret" is freed,
* we care if the value which was initially stored in "ret" is freed. This is
* different from equiv because "ret" and "p->foo" are not equiv at the end.
* The SSA module doesn't deal with == only with =.
*
* Using this is a bit different and more complicated than I would like. If
* there is a new state then call set_ssa_state(). When you're getting the
* state it's probably easiest to always use get_ssa_sm_state() because
* afterwards you will need to call update_ssa_state(my_id, sm->name, sm->sym,
* &state); to change the state. If you call set_ssa_state() that basically
* works too but it's slower because it adds a layer of indirection.
*
*/
#include "smatch.h"
#include "smatch_extra.h"
#include "smatch_slist.h"
static int my_id;
static unsigned long ssa_id = 1;
char *ssa_name(const char *name)
{
char *ret;
char *p;
ret = alloc_sname(name);
p = strchr(ret, ':');
if (p)
*p = '\0';
return ret;
}
static struct smatch_state *alloc_ssa_new(const char *name)
{
struct smatch_state *state;
char buf[64];
state = __alloc_smatch_state(0);
snprintf(buf, sizeof(buf), "%s:%ld", name, ssa_id);
state->name = alloc_sname(buf);
ssa_id++;
return state;
}
static struct smatch_state *alloc_ssa_copy(struct sm_state *sm)
{
struct smatch_state *state;
if (sm->state == &undefined || sm->state == &merged)
return sm->state;
state = __alloc_smatch_state(0);
state->name = alloc_sname(sm->state->name);
return state;
}
static bool whatever_close_enough(struct expression *left, struct expression *right)
{
struct symbol *left_type, *right_type;
left_type = get_type(left);
right_type = get_type(right);
if (type_bits(right_type) == 64) {
if (type_bits(left_type) == 64)
return true;
return false;
}
if (type_bits(right_type) == 32) {
if (type_bits(left_type) == 64 || type_bits(left_type) == 32)
return true;
return false;
}
return false;
}
static void match_assign(struct expression *expr)
{
struct smatch_state *left_state;
struct sm_state *orig, *clone;
struct symbol *left_sym, *right_sym;
char *left_name = NULL, *right_name = NULL;
if (__in_fake_assign)
return;
if (expr->op != '=')
return;
/* The whatever function is more likely to return true #Faster */
if (!whatever_close_enough(expr->left, expr->right) &&
!values_fit_type(expr->left, expr->right))
return;
left_name = expr_to_var_sym(expr->left, &left_sym);
if (!left_name)
goto free;
/*
* The ordering of this is really tricky. The issue here is that we
* have: "dev = of_node_get(node);". The first thing that happens is
* the modified hook sets "dev" to undefined. Then the check for
* tracking of_node_get/put() allocates an ssa state for "dev". So if
* it's set here we can just return. Otherwise track the SSA state.
*/
left_state = get_state(my_id, left_name, left_sym);
if (left_state && left_state != &undefined)
goto free;
right_name = expr_to_var_sym(expr->right, &right_sym);
if (!right_name)
goto free;
orig = get_sm_state(my_id, right_name, right_sym);
if (!orig || orig->state == &undefined)
orig = set_state(my_id, right_name, right_sym, alloc_ssa_new(right_name));
/* This can happen in unreachable code or outside of functions I gess */
if (!orig)
return;
/*
* Cloning is only really necessary for &merged states but it's better
* to only have one code path where possible.
*
*/
clone = clone_sm(orig);
clone->state = alloc_ssa_copy(orig);
clone->name = alloc_sname(left_name);
clone->sym = left_sym;
__set_sm(clone);
free:
free_string(left_name);
free_string(right_name);
}
void set_ssa_state(int owner, const char *name, struct symbol *sym,
struct smatch_state *state)
{
struct sm_state *sm;
if (!name)
return;
sm = get_sm_state(my_id, name, sym);
if (!sm || sm->state == &undefined)
sm = set_state(my_id, name, sym, alloc_ssa_new(name));
if (!sm)
return;
if (sm->state == &merged) {
sm = clone_sm(sm);
sm->state = alloc_ssa_new(name);
add_possible_sm(sm, sm);
__set_sm(sm);
}
if (!sm)
return;
set_state(owner, sm->state->name, NULL, state);
}
void update_ssa_state(int owner, const char *name, struct symbol *sym,
struct smatch_state *state)
{
set_state(owner, name, NULL, state);
}
void update_ssa_sm(int owner, struct sm_state *ssa_sm, struct smatch_state *state)
{
struct sm_state *tmp;
FOR_EACH_PTR(ssa_sm->possible, tmp) {
if (tmp->state == &merged ||
tmp->state == &undefined)
continue;
set_state(owner, tmp->state->name, NULL, state);
} END_FOR_EACH_PTR(tmp);
}
void set_ssa_state_expr(int owner, struct expression *expr,
struct smatch_state *state)
{
struct symbol *sym;
char *name;
name = expr_to_var_sym(expr, &sym);
if (!name)
return;
set_ssa_state(owner, name, sym, state);
free_string(name);
}
struct sm_state *get_ssa_sm_state(int owner, const char *name, struct symbol *sym)
{
struct sm_state *sm, *tmp, *owner_sm;
struct sm_state *ret = NULL;
sm = get_sm_state(my_id, name, sym);
if (!sm || sm->state == &undefined)
return NULL;
FOR_EACH_PTR(sm->possible, tmp) {
if (tmp->state == &merged ||
tmp->state == &undefined)
continue;
owner_sm = get_sm_state(owner, tmp->state->name, NULL);
if (owner_sm) {
if (!ret)
ret = clone_sm(owner_sm);
else
ret = merge_sm_states(ret, owner_sm);
}
} END_FOR_EACH_PTR(tmp);
if (!ret)
return NULL;
tmp = ret;
ret = clone_sm(sm);
ret->state = tmp->state;
return ret;
}
struct sm_state *get_ssa_sm_state_expr(int owner, struct expression *expr)
{
struct sm_state *ret;
struct symbol *sym;
char *name;
name = expr_to_var_sym(expr, &sym);
if (!name)
return NULL;
ret = get_ssa_sm_state(owner, name, sym);
free_string(name);
return ret;
}
struct smatch_state *get_ssa_state(int owner, const char *name, struct symbol *sym)
{
struct sm_state *sm;
sm = get_ssa_sm_state(my_id, name, sym);
if (!sm)
return NULL;
return sm->state;
}
struct smatch_state *get_ssa_state_expr(int owner, struct expression *expr)
{
struct sm_state *sm;
sm = get_ssa_sm_state_expr(my_id, expr);
if (!sm)
return NULL;
return sm->state;
}
void register_ssa(int id)
{
my_id = id;
set_dynamic_states(my_id);
add_hook(&match_assign, ASSIGNMENT_HOOK_AFTER);
add_modification_hook(my_id, &set_undefined);
}