cprover
local_bitvector_analysis.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Field-insensitive, location-sensitive may-alias analysis
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
11 
13 
14 #include <iterator>
15 #include <algorithm>
16 
17 #include <util/std_expr.h>
18 #include <util/std_code.h>
19 #include <util/expr_util.h>
20 
21 #include <util/c_types.h>
22 #include <langapi/language_util.h>
23 
24 void local_bitvector_analysist::flagst::print(std::ostream &out) const
25 {
26  if(is_unknown())
27  out << "+unknown";
28  if(is_uninitialized())
29  out << "+uninitialized";
30  if(is_uses_offset())
31  out << "+uses_offset";
32  if(is_dynamic_local())
33  out << "+dynamic_local";
34  if(is_dynamic_heap())
35  out << "+dynamic_heap";
36  if(is_null())
37  out << "+null";
38  if(is_static_lifetime())
39  out << "+static_lifetime";
40  if(is_integer_address())
41  out << "+integer_address";
42 }
43 
45 {
46  bool result=false;
47 
48  std::size_t max_index=
49  std::max(src.points_to.size(), points_to.size());
50 
51  for(std::size_t i=0; i<max_index; i++)
52  {
53  if(points_to[i].merge(src.points_to[i]))
54  result=true;
55  }
56 
57  return result;
58 }
59 
62 {
63  localst::locals_mapt::const_iterator it=locals.locals_map.find(identifier);
64  if(it==locals.locals_map.end() ||
65  it->second.id()!=ID_pointer ||
66  dirty(identifier))
67  return false;
68 
69  return true;
70 }
71 
73  const exprt &lhs,
74  const exprt &rhs,
75  const loc_infot &loc_info_src,
76  loc_infot &loc_info_dest)
77 {
78  if(lhs.id()==ID_symbol)
79  {
80  const irep_idt &identifier=to_symbol_expr(lhs).get_identifier();
81 
82  if(is_tracked(identifier))
83  {
84  unsigned dest_pointer=pointers.number(identifier);
85  flagst rhs_flags=get_rec(rhs, loc_info_src);
86  loc_info_dest.points_to[dest_pointer]=rhs_flags;
87  }
88  }
89  else if(lhs.id()==ID_dereference)
90  {
91  }
92  else if(lhs.id()==ID_index)
93  {
94  assign_lhs(to_index_expr(lhs).array(), rhs, loc_info_src, loc_info_dest);
95  }
96  else if(lhs.id()==ID_member)
97  {
98  assign_lhs(
99  to_member_expr(lhs).struct_op(), rhs, loc_info_src, loc_info_dest);
100  }
101  else if(lhs.id()==ID_typecast)
102  {
103  assign_lhs(to_typecast_expr(lhs).op(), rhs, loc_info_src, loc_info_dest);
104  }
105  else if(lhs.id()==ID_if)
106  {
107  assign_lhs(to_if_expr(lhs).true_case(), rhs, loc_info_src, loc_info_dest);
108  assign_lhs(to_if_expr(lhs).false_case(), rhs, loc_info_src, loc_info_dest);
109  }
110 }
111 
114  const exprt &rhs)
115 {
116  local_cfgt::loc_mapt::const_iterator loc_it=cfg.loc_map.find(t);
117 
118  assert(loc_it!=cfg.loc_map.end());
119 
120  const loc_infot &loc_info_src=loc_infos[loc_it->second];
121 
122  return get_rec(rhs, loc_info_src);
123 }
124 
126  const exprt &rhs,
127  const loc_infot &loc_info_src)
128 {
129  if(rhs.id()==ID_constant)
130  {
131  if(rhs.is_zero())
132  return flagst::mk_null();
133  else
135  }
136  else if(rhs.id()==ID_symbol)
137  {
138  const irep_idt &identifier=to_symbol_expr(rhs).get_identifier();
139  if(is_tracked(identifier))
140  {
141  unsigned src_pointer=pointers.number(identifier);
142  return loc_info_src.points_to[src_pointer];
143  }
144  else
145  return flagst::mk_unknown();
146  }
147  else if(rhs.id()==ID_address_of)
148  {
149  const exprt &object=to_address_of_expr(rhs).object();
150 
151  if(object.id()==ID_symbol)
152  {
153  if(locals.is_local(to_symbol_expr(object).get_identifier()))
154  return flagst::mk_dynamic_local();
155  else
157  }
158  else if(object.id()==ID_index)
159  {
160  const index_exprt &index_expr=to_index_expr(object);
161  if(index_expr.array().id()==ID_symbol)
162  {
163  if(locals.is_local(
164  to_symbol_expr(index_expr.array()).get_identifier()))
166  else
168  }
169  else
170  return flagst::mk_unknown();
171  }
172  else
173  return flagst::mk_unknown();
174  }
175  else if(rhs.id()==ID_typecast)
176  {
177  return get_rec(to_typecast_expr(rhs).op(), loc_info_src);
178  }
179  else if(rhs.id()==ID_uninitialized)
180  {
181  return flagst::mk_uninitialized();
182  }
183  else if(rhs.id()==ID_plus)
184  {
185  if(rhs.operands().size()>=3)
186  {
187  assert(rhs.op0().type().id()==ID_pointer);
188  return get_rec(rhs.op0(), loc_info_src) |
190  }
191  else if(rhs.operands().size()==2)
192  {
193  // one must be pointer, one an integer
194  if(rhs.op0().type().id()==ID_pointer)
195  {
196  return get_rec(rhs.op0(), loc_info_src) |
198  }
199  else if(rhs.op1().type().id()==ID_pointer)
200  {
201  return get_rec(rhs.op1(), loc_info_src) |
203  }
204  else
205  return flagst::mk_unknown();
206  }
207  else
208  return flagst::mk_unknown();
209  }
210  else if(rhs.id()==ID_minus)
211  {
212  if(rhs.op0().type().id()==ID_pointer)
213  {
214  return get_rec(rhs.op0(), loc_info_src) |
216  }
217  else
218  return flagst::mk_unknown();
219  }
220  else if(rhs.id()==ID_member)
221  {
222  return flagst::mk_unknown();
223  }
224  else if(rhs.id()==ID_index)
225  {
226  return flagst::mk_unknown();
227  }
228  else if(rhs.id()==ID_dereference)
229  {
230  return flagst::mk_unknown();
231  }
232  else if(rhs.id()==ID_side_effect)
233  {
234  const side_effect_exprt &side_effect_expr=to_side_effect_expr(rhs);
235  const irep_idt &statement=side_effect_expr.get_statement();
236 
237  if(statement==ID_malloc)
238  return flagst::mk_dynamic_heap();
239  else
240  return flagst::mk_unknown();
241  }
242  else
243  return flagst::mk_unknown();
244 }
245 
247 {
248  if(cfg.nodes.empty())
249  return;
250 
251  work_queuet work_queue;
252  work_queue.push(0);
253 
254  loc_infos.resize(cfg.nodes.size());
255 
256  // Gather the objects we track, and
257  // feed in sufficiently bad defaults for their value
258  // in the entry location.
259  for(const auto &local : locals.locals_map)
260  if(is_tracked(local.first))
261  loc_infos[0].points_to[pointers.number(local.first)]=flagst::mk_unknown();
262 
263  while(!work_queue.empty())
264  {
265  unsigned loc_nr=work_queue.top();
266  const local_cfgt::nodet &node=cfg.nodes[loc_nr];
267  const goto_programt::instructiont &instruction=*node.t;
268  work_queue.pop();
269 
270  const loc_infot &loc_info_src=loc_infos[loc_nr];
271  loc_infot loc_info_dest=loc_infos[loc_nr];
272 
273  switch(instruction.type)
274  {
275  case ASSIGN:
276  {
277  const code_assignt &code_assign=to_code_assign(instruction.code);
278  assign_lhs(
279  code_assign.lhs(), code_assign.rhs(), loc_info_src, loc_info_dest);
280  }
281  break;
282 
283  case DECL:
284  {
285  const code_declt &code_decl=to_code_decl(instruction.code);
286  assign_lhs(
287  code_decl.symbol(),
288  exprt(ID_uninitialized),
289  loc_info_src,
290  loc_info_dest);
291  }
292  break;
293 
294  case DEAD:
295  {
296  const code_deadt &code_dead=to_code_dead(instruction.code);
297  assign_lhs(
298  code_dead.symbol(),
299  exprt(ID_uninitialized),
300  loc_info_src,
301  loc_info_dest);
302  }
303  break;
304 
305  case FUNCTION_CALL:
306  {
307  const code_function_callt &code_function_call=
308  to_code_function_call(instruction.code);
309  if(code_function_call.lhs().is_not_nil())
310  assign_lhs(
311  code_function_call.lhs(), nil_exprt(), loc_info_src, loc_info_dest);
312  }
313  break;
314 
315  default:
316  {
317  }
318  }
319 
320  for(const auto &succ : node.successors)
321  {
322  assert(succ<loc_infos.size());
323  if(loc_infos[succ].merge(loc_info_dest))
324  work_queue.push(succ);
325  }
326  }
327 }
328 
330  std::ostream &out,
331  const goto_functiont &goto_function,
332  const namespacet &ns) const
333 {
334  unsigned l=0;
335 
336  forall_goto_program_instructions(i_it, goto_function.body)
337  {
338  out << "**** " << i_it->source_location << "\n";
339 
340  const loc_infot &loc_info=loc_infos[l];
341 
342  for(points_tot::const_iterator
343  p_it=loc_info.points_to.begin();
344  p_it!=loc_info.points_to.end();
345  p_it++)
346  {
347  out << " " << pointers[p_it-loc_info.points_to.begin()]
348  << ": "
349  << *p_it
350  << "\n";
351  }
352 
353  out << "\n";
354  goto_function.body.output_instruction(ns, "", out, i_it);
355  out << "\n";
356 
357  l++;
358  }
359 }
const typecast_exprt & to_typecast_expr(const exprt &expr)
Cast a generic exprt to a typecast_exprt.
Definition: std_expr.h:1760
const code_declt & to_code_decl(const codet &code)
Definition: std_code.h:218
bool is_not_nil() const
Definition: irep.h:104
loc_mapt loc_map
Definition: local_cfg.h:33
exprt & object()
Definition: std_expr.h:2608
void build(const goto_functiont &goto_function)
exprt & op0()
Definition: expr.h:84
const code_deadt & to_code_dead(const codet &code)
Definition: std_code.h:260
exprt & symbol()
Definition: std_code.h:205
locals_mapt locals_map
Definition: locals.h:40
bool is_tracked(const irep_idt &identifier)
Deprecated expression utility functions.
goto_functionst::goto_functiont goto_functiont
const irep_idt & get_identifier() const
Definition: std_expr.h:120
const member_exprt & to_member_expr(const exprt &expr)
Cast a generic exprt to a member_exprt.
Definition: std_expr.h:3302
void assign_lhs(const exprt &lhs, const exprt &rhs, const loc_infot &loc_info_src, loc_infot &loc_info_dest)
typet & type()
Definition: expr.h:60
Field-insensitive, location-sensitive bitvector analysis.
side_effect_exprt & to_side_effect_expr(exprt &expr)
Definition: std_code.h:1023
const index_exprt & to_index_expr(const exprt &expr)
Cast a generic exprt to an index_exprt.
Definition: std_expr.h:1229
nodest nodes
Definition: local_cfg.h:36
bool is_local(const irep_idt &identifier) const
Definition: locals.h:34
const code_assignt & to_code_assign(const codet &code)
Definition: std_code.h:178
const irep_idt & id() const
Definition: irep.h:189
exprt & lhs()
Definition: std_code.h:157
instructionst::const_iterator const_targett
const address_of_exprt & to_address_of_expr(const exprt &expr)
Cast a generic exprt to an address_of_exprt.
Definition: std_expr.h:2629
A declaration of a local variable.
Definition: std_code.h:192
const if_exprt & to_if_expr(const exprt &expr)
Cast a generic exprt to an if_exprt.
Definition: std_expr.h:2836
The NIL expression.
Definition: std_expr.h:3764
exprt & rhs()
Definition: std_code.h:162
API to expression classes.
exprt & op1()
Definition: expr.h:87
TO_BE_DOCUMENTED.
Definition: namespace.h:62
A function call.
Definition: std_code.h:657
void output(std::ostream &out, const goto_functiont &goto_function, const namespacet &ns) const
exprt & symbol()
Definition: std_code.h:247
goto_programt::const_targett t
Definition: local_cfg.h:28
flagst get(const goto_programt::const_targett t, const exprt &src)
Base class for all expressions.
Definition: expr.h:46
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast a generic exprt to a symbol_exprt.
Definition: std_expr.h:202
A removal of a local variable.
Definition: std_code.h:234
bool is_zero() const
Definition: expr.cpp:236
flagst get_rec(const exprt &rhs, const loc_infot &loc_info_src)
number_type number(const T &a)
Definition: numbering.h:27
An expression containing a side effect.
Definition: std_code.h:997
operandst & operands()
Definition: expr.h:70
std::stack< unsigned > work_queuet
#define forall_goto_program_instructions(it, program)
Definition: goto_program.h:68
exprt & array()
Definition: std_expr.h:1198
Assignment.
Definition: std_code.h:144
const irep_idt & get_statement() const
Definition: std_code.h:1012
successorst successors
Definition: local_cfg.h:29
const code_function_callt & to_code_function_call(const codet &code)
Definition: std_code.h:700
array index operator
Definition: std_expr.h:1170