-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathlocal_bitvector_analysis.cpp
373 lines (333 loc) · 9.44 KB
/
local_bitvector_analysis.cpp
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*******************************************************************\
Module: Field-insensitive, location-sensitive may-alias analysis
Author: Daniel Kroening, [email protected]
\*******************************************************************/
/// \file
/// Field-insensitive, location-sensitive may-alias analysis
#include "local_bitvector_analysis.h"
#include <util/namespace.h>
#include <util/pointer_expr.h>
#include <util/std_code.h>
#include <util/symbol.h>
#include <algorithm>
void local_bitvector_analysist::flagst::print(std::ostream &out) const
{
if(is_unknown())
out << "+unknown";
if(is_uninitialized())
out << "+uninitialized";
if(is_uses_offset())
out << "+uses_offset";
if(is_dynamic_local())
out << "+dynamic_local";
if(is_dynamic_heap())
out << "+dynamic_heap";
if(is_null())
out << "+null";
if(is_static_lifetime())
out << "+static_lifetime";
if(is_integer_address())
out << "+integer_address";
}
bool local_bitvector_analysist::merge(points_tot &a, points_tot &b)
{
bool result=false;
std::size_t max_index=
std::max(a.size(), b.size());
for(std::size_t i=0; i<max_index; i++)
result |= a[i].merge(b[i]);
return result;
}
/// \return return 'true' iff we track the object with given identifier
bool local_bitvector_analysist::is_tracked(const irep_idt &identifier)
{
localst::locals_sett::const_iterator it = locals.locals.find(identifier);
return it != locals.locals.end() && ns.lookup(*it).type.id() == ID_pointer &&
!dirty(identifier);
}
void local_bitvector_analysist::assign_lhs(
const exprt &lhs,
const exprt &rhs,
points_tot &loc_info_src,
points_tot &loc_info_dest)
{
if(lhs.id()==ID_symbol)
{
const irep_idt &identifier=to_symbol_expr(lhs).get_identifier();
if(is_tracked(identifier))
{
const auto dest_pointer = pointers.number(identifier);
flagst rhs_flags=get_rec(rhs, loc_info_src);
loc_info_dest[dest_pointer]=rhs_flags;
}
}
else if(lhs.id()==ID_dereference)
{
}
else if(lhs.id()==ID_index)
{
assign_lhs(to_index_expr(lhs).array(), rhs, loc_info_src, loc_info_dest);
}
else if(lhs.id()==ID_member)
{
assign_lhs(
to_member_expr(lhs).struct_op(), rhs, loc_info_src, loc_info_dest);
}
else if(lhs.id()==ID_typecast)
{
assign_lhs(to_typecast_expr(lhs).op(), rhs, loc_info_src, loc_info_dest);
}
else if(lhs.id()==ID_if)
{
assign_lhs(to_if_expr(lhs).true_case(), rhs, loc_info_src, loc_info_dest);
assign_lhs(to_if_expr(lhs).false_case(), rhs, loc_info_src, loc_info_dest);
}
}
local_bitvector_analysist::flagst local_bitvector_analysist::get(
const goto_programt::const_targett t,
const exprt &rhs)
{
local_cfgt::loc_mapt::const_iterator loc_it=cfg.loc_map.find(t);
CHECK_RETURN(loc_it != cfg.loc_map.end());
return get_rec(rhs, loc_infos[loc_it->second]);
}
local_bitvector_analysist::flagst local_bitvector_analysist::get_rec(
const exprt &rhs,
points_tot &loc_info_src)
{
if(rhs.is_constant())
{
if(to_constant_expr(rhs).is_zero())
return flagst::mk_null();
else
return flagst::mk_integer_address();
}
else if(rhs.id()==ID_symbol)
{
const irep_idt &identifier=to_symbol_expr(rhs).get_identifier();
if(is_tracked(identifier))
{
const auto src_pointer = pointers.number(identifier);
return loc_info_src[src_pointer];
}
else
return flagst::mk_unknown();
}
else if(rhs.id()==ID_address_of)
{
const exprt &object=to_address_of_expr(rhs).object();
if(object.id()==ID_symbol)
{
if(locals.is_local(to_symbol_expr(object).get_identifier()))
return flagst::mk_dynamic_local();
else
return flagst::mk_static_lifetime();
}
else if(object.id()==ID_index)
{
const index_exprt &index_expr=to_index_expr(object);
if(index_expr.array().id()==ID_symbol)
{
if(locals.is_local(
to_symbol_expr(index_expr.array()).get_identifier()))
return flagst::mk_dynamic_local() | flagst::mk_uses_offset();
else
return flagst::mk_static_lifetime() | flagst::mk_uses_offset();
}
else
return flagst::mk_unknown();
}
else
return flagst::mk_unknown();
}
else if(rhs.id()==ID_typecast)
{
return get_rec(to_typecast_expr(rhs).op(), loc_info_src);
}
else if(rhs.id()==ID_uninitialized)
{
return flagst::mk_uninitialized();
}
else if(rhs.id()==ID_plus)
{
const auto &plus_expr = to_plus_expr(rhs);
if(plus_expr.operands().size() >= 3)
{
DATA_INVARIANT(
plus_expr.op0().type().id() == ID_pointer,
"pointer in pointer-typed sum must be op0");
return get_rec(plus_expr.op0(), loc_info_src) | flagst::mk_uses_offset();
}
else if(plus_expr.operands().size() == 2)
{
// one must be pointer, one an integer
if(plus_expr.op0().type().id() == ID_pointer)
{
return get_rec(plus_expr.op0(), loc_info_src) |
flagst::mk_uses_offset();
}
else if(plus_expr.op1().type().id() == ID_pointer)
{
return get_rec(plus_expr.op1(), loc_info_src) |
flagst::mk_uses_offset();
}
else
return flagst::mk_unknown();
}
else
return flagst::mk_unknown();
}
else if(rhs.id()==ID_minus)
{
const auto &op0 = to_minus_expr(rhs).op0();
if(op0.type().id() == ID_pointer)
{
return get_rec(op0, loc_info_src) | flagst::mk_uses_offset();
}
else
return flagst::mk_unknown();
}
else if(rhs.id()==ID_member)
{
return flagst::mk_unknown();
}
else if(rhs.id()==ID_index)
{
return flagst::mk_unknown();
}
else if(rhs.id()==ID_dereference)
{
return flagst::mk_unknown();
}
else if(rhs.id()==ID_side_effect)
{
const side_effect_exprt &side_effect_expr=to_side_effect_expr(rhs);
const irep_idt &statement=side_effect_expr.get_statement();
if(statement==ID_allocate)
return flagst::mk_dynamic_heap();
else
return flagst::mk_unknown();
}
else
return flagst::mk_unknown();
}
void local_bitvector_analysist::build()
{
if(cfg.nodes.empty())
return;
std::set<local_cfgt::node_nrt> work_queue;
work_queue.insert(0);
loc_infos.resize(cfg.nodes.size());
// Gather the objects we track, and
// feed in sufficiently bad defaults for their value
// in the entry location.
for(const auto &local : locals.locals)
{
if(is_tracked(local))
loc_infos[0][pointers.number(local)] = flagst::mk_unknown();
}
while(!work_queue.empty())
{
const auto loc_nr = *work_queue.begin();
const local_cfgt::nodet &node=cfg.nodes[loc_nr];
const goto_programt::instructiont &instruction=*node.t;
work_queue.erase(work_queue.begin());
auto &loc_info_src=loc_infos[loc_nr];
auto loc_info_dest=loc_infos[loc_nr];
switch(instruction.type())
{
case ASSIGN:
assign_lhs(
instruction.assign_lhs(),
instruction.assign_rhs(),
loc_info_src,
loc_info_dest);
break;
case DECL:
assign_lhs(
instruction.decl_symbol(),
exprt(ID_uninitialized),
loc_info_src,
loc_info_dest);
break;
case DEAD:
assign_lhs(
instruction.dead_symbol(),
exprt(ID_uninitialized),
loc_info_src,
loc_info_dest);
break;
case FUNCTION_CALL:
{
const auto &lhs = instruction.call_lhs();
if(lhs.is_not_nil())
assign_lhs(lhs, nil_exprt(), loc_info_src, loc_info_dest);
break;
}
case CATCH:
case THROW:
#if 0
DATA_INVARIANT(false, "Exceptions must be removed before analysis");
break;
#endif
case SET_RETURN_VALUE:
#if 0
DATA_INVARIANT(false, "SET_RETURN_VALUE must be removed before analysis");
break;
#endif
case ATOMIC_BEGIN: // Ignoring is a valid over-approximation
case ATOMIC_END: // Ignoring is a valid over-approximation
case LOCATION: // No action required
case START_THREAD: // Require a concurrent analysis at higher level
case END_THREAD: // Require a concurrent analysis at higher level
case SKIP: // No action required
case ASSERT: // No action required
case ASSUME: // Ignoring is a valid over-approximation
case GOTO: // Ignoring the guard is a valid over-approximation
case END_FUNCTION: // No action required
break;
case OTHER:
#if 0
DATA_INVARIANT(
false, "Unclear what is a safe over-approximation of OTHER");
#endif
break;
case INCOMPLETE_GOTO:
case NO_INSTRUCTION_TYPE:
DATA_INVARIANT(false, "Only complete instructions can be analyzed");
break;
}
for(const auto &succ : node.successors)
{
INVARIANT(succ < loc_infos.size(), "successor outside function");
if(merge(loc_infos[succ], (loc_info_dest)))
work_queue.insert(succ);
}
}
}
void local_bitvector_analysist::output(
std::ostream &out,
const goto_functiont &goto_function,
const namespacet &ns) const
{
std::size_t l = 0;
for(const auto &instruction : goto_function.body.instructions)
{
out << "**** " << instruction.source_location() << "\n";
const auto &loc_info=loc_infos[l];
for(points_tot::const_iterator
p_it=loc_info.begin();
p_it!=loc_info.end();
p_it++)
{
out << " " << pointers[p_it-loc_info.begin()]
<< ": "
<< *p_it
<< "\n";
}
out << "\n";
instruction.output(out);
out << "\n";
l++;
}
}