-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathlocal_may_alias.cpp
503 lines (439 loc) · 12.7 KB
/
local_may_alias.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/*******************************************************************\
Module: Field-insensitive, location-sensitive may-alias analysis
Author: Daniel Kroening, [email protected]
\*******************************************************************/
/// \file
/// Field-insensitive, location-sensitive may-alias analysis
#include "local_may_alias.h"
#include <iterator>
#include <algorithm>
#include <util/arith_tools.h>
#include <util/pointer_expr.h>
#include <util/std_code.h>
#include <util/c_types.h>
#include <langapi/language_util.h>
/// \return return 'true' iff changed
bool local_may_aliast::loc_infot::merge(const loc_infot &src)
{
bool changed=false;
// do union; this should be amortized linear
for(std::size_t i=0; i<src.aliases.size(); i++)
{
std::size_t root=src.aliases.find(i);
if(!aliases.same_set(i, root))
{
aliases.make_union(i, root);
changed=true;
}
}
return changed;
}
void local_may_aliast::assign_lhs(
const exprt &lhs,
const exprt &rhs,
const loc_infot &loc_info_src,
loc_infot &loc_info_dest)
{
if(lhs.id()==ID_symbol)
{
if(lhs.type().id()==ID_pointer)
{
unsigned dest_pointer=objects.number(lhs);
// isolate the lhs pointer
loc_info_dest.aliases.isolate(dest_pointer);
object_sett rhs_set;
get_rec(rhs_set, rhs, loc_info_src);
// make these all aliases
for(object_sett::const_iterator
p_it=rhs_set.begin();
p_it!=rhs_set.end();
p_it++)
{
loc_info_dest.aliases.make_union(dest_pointer, *p_it);
}
}
}
else if(lhs.id()==ID_dereference)
{
// this might invalidate all pointers that are
// a) local and dirty
// b) global
if(lhs.type().id()==ID_pointer)
{
for(std::size_t i=0; i<objects.size(); i++)
{
if(objects[i].id()==ID_symbol)
{
const irep_idt &identifier=
to_symbol_expr(objects[i]).get_identifier();
if(dirty(identifier) || !locals.is_local(identifier))
{
loc_info_dest.aliases.isolate(i);
loc_info_dest.aliases.make_union(i, unknown_object);
}
}
}
}
}
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);
}
}
std::set<exprt> local_may_aliast::get(
const goto_programt::const_targett t,
const exprt &rhs) const
{
local_cfgt::loc_mapt::const_iterator loc_it=cfg.loc_map.find(t);
CHECK_RETURN(loc_it != cfg.loc_map.end());
const loc_infot &loc_info_src=loc_infos[loc_it->second];
object_sett result_tmp;
get_rec(result_tmp, rhs, loc_info_src);
std::set<exprt> result;
for(object_sett::const_iterator
it=result_tmp.begin();
it!=result_tmp.end();
it++)
{
result.insert(objects[*it]);
}
return result;
}
bool local_may_aliast::aliases(
const goto_programt::const_targett t,
const exprt &src1, const exprt &src2) const
{
local_cfgt::loc_mapt::const_iterator loc_it=cfg.loc_map.find(t);
CHECK_RETURN(loc_it != cfg.loc_map.end());
const loc_infot &loc_info_src=loc_infos[loc_it->second];
object_sett tmp1, tmp2;
get_rec(tmp1, src1, loc_info_src);
get_rec(tmp2, src2, loc_info_src);
if(tmp1.find(unknown_object)!=tmp1.end() ||
tmp2.find(unknown_object)!=tmp2.end())
return true;
std::list<unsigned> result;
std::set_intersection(
tmp1.begin(), tmp1.end(),
tmp2.begin(), tmp2.end(),
std::back_inserter(result));
return !result.empty();
}
void local_may_aliast::get_rec(
object_sett &dest,
const exprt &rhs,
const loc_infot &loc_info_src) const
{
if(rhs.is_constant())
{
if(to_constant_expr(rhs).is_zero())
dest.insert(objects.number(exprt(ID_null_object)));
else
dest.insert(objects.number(exprt(ID_integer_address_object)));
}
else if(rhs.id()==ID_symbol)
{
if(rhs.type().id()==ID_pointer)
{
unsigned src_pointer=objects.number(rhs);
dest.insert(src_pointer);
for(std::size_t i=0; i<loc_info_src.aliases.size(); i++)
if(loc_info_src.aliases.same_set(src_pointer, i))
dest.insert(i);
}
else
dest.insert(unknown_object);
}
else if(rhs.id()==ID_if)
{
get_rec(dest, to_if_expr(rhs).false_case(), loc_info_src);
get_rec(dest, to_if_expr(rhs).true_case(), loc_info_src);
}
else if(rhs.id()==ID_address_of)
{
const exprt &object=to_address_of_expr(rhs).object();
if(object.id()==ID_symbol)
{
unsigned object_nr=objects.number(rhs);
dest.insert(object_nr);
for(std::size_t i=0; i<loc_info_src.aliases.size(); i++)
if(loc_info_src.aliases.same_set(object_nr, i))
dest.insert(i);
}
else if(object.id()==ID_index)
{
const index_exprt &index_expr=to_index_expr(object);
if(index_expr.array().id()==ID_symbol)
{
index_exprt tmp1=index_expr;
tmp1.index() = from_integer(0, c_index_type());
address_of_exprt tmp2(tmp1);
unsigned object_nr=objects.number(tmp2);
dest.insert(object_nr);
for(std::size_t i=0; i<loc_info_src.aliases.size(); i++)
if(loc_info_src.aliases.same_set(object_nr, i))
dest.insert(i);
}
else if(index_expr.array().id()==ID_string_constant)
{
index_exprt tmp1=index_expr;
tmp1.index() = from_integer(0, c_index_type());
address_of_exprt tmp2(tmp1);
unsigned object_nr=objects.number(tmp2);
dest.insert(object_nr);
for(std::size_t i=0; i<loc_info_src.aliases.size(); i++)
if(loc_info_src.aliases.same_set(object_nr, i))
dest.insert(i);
}
else
dest.insert(unknown_object);
}
else
dest.insert(unknown_object);
}
else if(rhs.id()==ID_typecast)
{
get_rec(dest, to_typecast_expr(rhs).op(), loc_info_src);
}
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");
get_rec(dest, plus_expr.op0(), loc_info_src);
}
else if(plus_expr.operands().size() == 2)
{
// one must be pointer, one an integer
if(plus_expr.op0().type().id() == ID_pointer)
{
get_rec(dest, plus_expr.op0(), loc_info_src);
}
else if(plus_expr.op1().type().id() == ID_pointer)
{
get_rec(dest, plus_expr.op1(), loc_info_src);
}
else
dest.insert(unknown_object);
}
else
dest.insert(unknown_object);
}
else if(rhs.id()==ID_minus)
{
const auto &op0 = to_minus_expr(rhs).op0();
if(op0.type().id() == ID_pointer)
{
get_rec(dest, op0, loc_info_src);
}
else
dest.insert(unknown_object);
}
else if(rhs.id()==ID_member)
{
dest.insert(unknown_object);
}
else if(rhs.id()==ID_index)
{
dest.insert(unknown_object);
}
else if(rhs.id()==ID_dereference)
{
dest.insert(unknown_object);
}
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)
{
dest.insert(objects.number(exprt(ID_dynamic_object)));
}
else
dest.insert(unknown_object);
}
else if(rhs.is_nil())
{
// this means 'empty'
}
else
dest.insert(unknown_object);
}
void local_may_aliast::build(const goto_functiont &goto_function)
{
if(cfg.nodes.empty())
return;
work_queuet work_queue;
// put all nodes into work queue
for(local_cfgt::node_nrt n=0; n<cfg.nodes.size(); n++)
work_queue.push(n);
unknown_object=objects.number(exprt(ID_unknown));
loc_infos.resize(cfg.nodes.size());
(void)goto_function; // unused parameter
#if 0
// feed in sufficiently bad defaults
for(code_typet::parameterst::const_iterator
it=goto_function.type.parameters().begin();
it!=goto_function.type.parameters().end();
it++)
{
const irep_idt &identifier=it->get_identifier();
if(is_tracked(identifier))
loc_infos[0].points_to[objects.number(identifier)].objects.insert(
unknown_object);
}
#endif
#if 0
for(localst::locals_mapt::const_iterator
l_it=locals.locals_map.begin();
l_it!=locals.locals_map.end();
l_it++)
{
if(is_tracked(l_it->first))
loc_infos[0].aliases.make_union(
objects.number(l_it->second), unknown_object);
}
#endif
while(!work_queue.empty())
{
local_cfgt::node_nrt loc_nr=work_queue.top();
const local_cfgt::nodet &node=cfg.nodes[loc_nr];
const goto_programt::instructiont &instruction=*node.t;
work_queue.pop();
const loc_infot &loc_info_src=loc_infos[loc_nr];
loc_infot 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(), nil_exprt(), loc_info_src, loc_info_dest);
break;
case DEAD:
assign_lhs(
instruction.dead_symbol(), nil_exprt(), 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);
// this might invalidate all pointers that are
// a) local and dirty
// b) global
for(std::size_t i = 0; i < objects.size(); i++)
{
if(objects[i].id() == ID_symbol)
{
const irep_idt &identifier =
to_symbol_expr(objects[i]).get_identifier();
if(dirty(identifier) || !locals.is_local(identifier))
{
loc_info_dest.aliases.isolate(i);
loc_info_dest.aliases.make_union(i, unknown_object);
}
}
}
break;
}
case CATCH:
case THROW:
DATA_INVARIANT(false, "Exceptions must be removed before analysis");
break;
case SET_RETURN_VALUE:
#if 0
DATA_INVARIANT(false, "SET_RETURN_VALUE must be removed before analysis");
#endif
break;
case GOTO: // Ignoring the guard is a valid over-approximation
case START_THREAD: // Require a concurrent analysis at higher level
case END_THREAD: // Require a concurrent analysis at higher level
case ATOMIC_BEGIN: // Ignoring is a valid over-approximation
case ATOMIC_END: // Ignoring is a valid over-approximation
case LOCATION: // No action required
case SKIP: // No action required
case END_FUNCTION: // No action required
case ASSERT: // No action required
case ASSUME: // Ignoring is a valid over-approximation
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(local_cfgt::successorst::const_iterator
it=node.successors.begin();
it!=node.successors.end();
it++)
{
if(loc_infos[*it].merge(loc_info_dest))
work_queue.push(*it);
}
}
}
void local_may_aliast::output(
std::ostream &out,
const goto_functiont &goto_function,
const namespacet &ns) const
{
unsigned l=0;
for(const auto &instruction : goto_function.body.instructions)
{
out << "**** " << instruction.source_location() << "\n";
const loc_infot &loc_info=loc_infos[l];
for(std::size_t i=0; i<loc_info.aliases.size(); i++)
{
if(loc_info.aliases.count(i)!=1 &&
loc_info.aliases.find(i)==i) // root?
{
out << '{';
for(std::size_t j=0; j<loc_info.aliases.size(); j++)
if(loc_info.aliases.find(j)==i)
{
INVARIANT(j < objects.size(), "invalid object index");
irep_idt identifier;
if(objects[j].id() == ID_symbol)
identifier = to_symbol_expr(objects[j]).get_identifier();
out << ' ' << from_expr(ns, identifier, objects[j]);
}
out << " }";
out << "\n";
}
}
out << "\n";
instruction.output(out);
out << "\n";
l++;
}
}