-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathglobal_may_alias.cpp
228 lines (200 loc) · 5.83 KB
/
global_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
/*******************************************************************\
Module: Field-insensitive, location-sensitive global may alias analysis
Author: Daniel Kroening, [email protected]
\*******************************************************************/
/// \file
/// Field-insensitive, location-sensitive global may alias analysis
#include "global_may_alias.h"
#include <util/pointer_expr.h>
/// Populate list of aliases for a given identifier in a context in
/// which an object is being written to.
/// \param lhs: A left hand side expression, containing an identifier.
/// \param alias_set: An external set of aliases to populate internal
/// aliases set.
void global_may_alias_domaint::assign_lhs_aliases(
const exprt &lhs,
const std::set<irep_idt> &alias_set)
{
if(lhs.id()==ID_symbol)
{
irep_idt identifier=to_symbol_expr(lhs).get_identifier();
aliases.isolate(identifier);
for(const auto &alias : alias_set)
{
aliases.make_union(identifier, alias);
}
}
}
/// Populate list of aliases for a given identifier in a context in
/// which is an object is being read.
/// \param rhs: A right hand side expression.
/// \param alias_set: A external set of aliases to populate internal
/// aliases set.
void global_may_alias_domaint::get_rhs_aliases(
const exprt &rhs,
std::set<irep_idt> &alias_set)
{
if(rhs.id()==ID_symbol)
{
irep_idt identifier=to_symbol_expr(rhs).get_identifier();
alias_set.insert(identifier);
for(const auto &alias : alias_set)
if(aliases.same_set(alias, identifier))
alias_set.insert(alias);
}
else if(rhs.id()==ID_if)
{
get_rhs_aliases(to_if_expr(rhs).true_case(), alias_set);
get_rhs_aliases(to_if_expr(rhs).false_case(), alias_set);
}
else if(rhs.id()==ID_typecast)
{
get_rhs_aliases(to_typecast_expr(rhs).op(), alias_set);
}
else if(rhs.id()==ID_address_of)
{
get_rhs_aliases_address_of(to_address_of_expr(rhs).object(), alias_set);
}
}
/// Specialisation of \ref global_may_alias_domaint::get_rhs_aliases
/// to deal with address_of expressions.
/// \param rhs: A right hand side expression.
/// \param alias_set: A external set of aliases to populate internal
/// aliases set.
void global_may_alias_domaint::get_rhs_aliases_address_of(
const exprt &rhs,
std::set<irep_idt> &alias_set)
{
if(rhs.id()==ID_symbol)
{
irep_idt identifier=to_symbol_expr(rhs).get_identifier();
alias_set.insert("&"+id2string(identifier));
}
else if(rhs.id()==ID_if)
{
get_rhs_aliases_address_of(to_if_expr(rhs).true_case(), alias_set);
get_rhs_aliases_address_of(to_if_expr(rhs).false_case(), alias_set);
}
else if(rhs.id()==ID_dereference)
{
}
}
void global_may_alias_domaint::transform(
const irep_idt &function_from,
trace_ptrt trace_from,
const irep_idt &function_to,
trace_ptrt trace_to,
ai_baset &ai,
const namespacet &ns)
{
locationt from{trace_from->current_location()};
if(has_values.is_false())
return;
const goto_programt::instructiont &instruction=*from;
switch(instruction.type())
{
case ASSIGN:
{
std::set<irep_idt> rhs_aliases;
get_rhs_aliases(instruction.assign_rhs(), rhs_aliases);
assign_lhs_aliases(instruction.assign_lhs(), rhs_aliases);
break;
}
case DECL:
aliases.isolate(instruction.decl_symbol().get_identifier());
break;
case DEAD:
aliases.isolate(instruction.dead_symbol().get_identifier());
break;
case FUNCTION_CALL: // Probably safe
case GOTO: // Ignoring the guard is a valid over-approximation
break;
case CATCH:
case THROW:
DATA_INVARIANT(false, "Exceptions must be removed before analysis");
break;
case SET_RETURN_VALUE:
DATA_INVARIANT(false, "SET_RETURN_VALUE must be removed before analysis");
break;
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 ASSERT: // No action required
case ASSUME: // Ignoring is a valid over-approximation
case SKIP: // No action required
case END_FUNCTION: // No action required
break;
case OTHER:
DATA_INVARIANT(false, "Unclear what is a safe over-approximation of OTHER");
break;
case INCOMPLETE_GOTO:
case NO_INSTRUCTION_TYPE:
DATA_INVARIANT(false, "Only complete instructions can be analyzed");
break;
}
}
void global_may_alias_domaint::output(
std::ostream &out,
const ai_baset &,
const namespacet &) const
{
if(has_values.is_known())
{
out << has_values.to_string() << '\n';
return;
}
for(aliasest::const_iterator a_it1=aliases.begin();
a_it1!=aliases.end();
a_it1++)
{
bool first=true;
for(aliasest::const_iterator a_it2=aliases.begin();
a_it2!=aliases.end();
a_it2++)
{
if(aliases.is_root(a_it1) && a_it1!=a_it2 &&
aliases.same_set(a_it1, a_it2))
{
if(first)
{
out << "Aliases: " << *a_it1;
first=false;
}
out << ' ' << *a_it2;
}
}
if(!first)
out << '\n';
}
}
bool global_may_alias_domaint::merge(
const global_may_alias_domaint &b,
trace_ptrt,
trace_ptrt)
{
bool changed=has_values.is_false();
has_values=tvt::unknown();
// do union
for(aliasest::const_iterator it=b.aliases.begin();
it!=b.aliases.end(); it++)
{
irep_idt b_root=b.aliases.find(it);
if(!aliases.same_set(*it, b_root))
{
aliases.make_union(*it, b_root);
changed=true;
}
}
// isolate non-tracked ones
#if 0
for(aliasest::const_iterator it=aliases.begin();
it!=aliases.end(); it++)
{
if(cleanup_map.find(*it)==cleanup_map.end())
aliases.isolate(it);
}
#endif
return changed;
}