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/c_types.h>
18 #include <util/expr_util.h>
19 #include <util/pointer_expr.h>
20 #include <util/std_code.h>
21 #include <util/std_expr.h>
22 
23 #include <langapi/language_util.h>
24 
25 void local_bitvector_analysist::flagst::print(std::ostream &out) const
26 {
27  if(is_unknown())
28  out << "+unknown";
29  if(is_uninitialized())
30  out << "+uninitialized";
31  if(is_uses_offset())
32  out << "+uses_offset";
33  if(is_dynamic_local())
34  out << "+dynamic_local";
35  if(is_dynamic_heap())
36  out << "+dynamic_heap";
37  if(is_null())
38  out << "+null";
39  if(is_static_lifetime())
40  out << "+static_lifetime";
41  if(is_integer_address())
42  out << "+integer_address";
43 }
44 
46 {
47  bool result=false;
48 
49  std::size_t max_index=
50  std::max(a.size(), b.size());
51 
52  for(std::size_t i=0; i<max_index; i++)
53  {
54  if(a[i].merge(b[i]))
55  result=true;
56  }
57 
58  return result;
59 }
60 
63 {
64  localst::locals_sett::const_iterator it = locals.locals.find(identifier);
65  return it != locals.locals.end() && ns.lookup(*it).type.id() == ID_pointer &&
66  !dirty(identifier);
67 }
68 
70  const exprt &lhs,
71  const exprt &rhs,
72  points_tot &loc_info_src,
73  points_tot &loc_info_dest)
74 {
75  if(lhs.id()==ID_symbol)
76  {
77  const irep_idt &identifier=to_symbol_expr(lhs).get_identifier();
78 
79  if(is_tracked(identifier))
80  {
81  unsigned dest_pointer=pointers.number(identifier);
82  flagst rhs_flags=get_rec(rhs, loc_info_src);
83  loc_info_dest[dest_pointer]=rhs_flags;
84  }
85  }
86  else if(lhs.id()==ID_dereference)
87  {
88  }
89  else if(lhs.id()==ID_index)
90  {
91  assign_lhs(to_index_expr(lhs).array(), rhs, loc_info_src, loc_info_dest);
92  }
93  else if(lhs.id()==ID_member)
94  {
95  assign_lhs(
96  to_member_expr(lhs).struct_op(), rhs, loc_info_src, loc_info_dest);
97  }
98  else if(lhs.id()==ID_typecast)
99  {
100  assign_lhs(to_typecast_expr(lhs).op(), rhs, loc_info_src, loc_info_dest);
101  }
102  else if(lhs.id()==ID_if)
103  {
104  assign_lhs(to_if_expr(lhs).true_case(), rhs, loc_info_src, loc_info_dest);
105  assign_lhs(to_if_expr(lhs).false_case(), rhs, loc_info_src, loc_info_dest);
106  }
107 }
108 
111  const exprt &rhs)
112 {
113  local_cfgt::loc_mapt::const_iterator loc_it=cfg.loc_map.find(t);
114 
115  assert(loc_it!=cfg.loc_map.end());
116 
117  return get_rec(rhs, loc_infos[loc_it->second]);
118 }
119 
121  const exprt &rhs,
122  points_tot &loc_info_src)
123 {
124  if(rhs.id()==ID_constant)
125  {
126  if(rhs.is_zero())
127  return flagst::mk_null();
128  else
130  }
131  else if(rhs.id()==ID_symbol)
132  {
133  const irep_idt &identifier=to_symbol_expr(rhs).get_identifier();
134  if(is_tracked(identifier))
135  {
136  unsigned src_pointer=pointers.number(identifier);
137  return loc_info_src[src_pointer];
138  }
139  else
140  return flagst::mk_unknown();
141  }
142  else if(rhs.id()==ID_address_of)
143  {
144  const exprt &object=to_address_of_expr(rhs).object();
145 
146  if(object.id()==ID_symbol)
147  {
148  if(locals.is_local(to_symbol_expr(object).get_identifier()))
149  return flagst::mk_dynamic_local();
150  else
152  }
153  else if(object.id()==ID_index)
154  {
155  const index_exprt &index_expr=to_index_expr(object);
156  if(index_expr.array().id()==ID_symbol)
157  {
158  if(locals.is_local(
159  to_symbol_expr(index_expr.array()).get_identifier()))
161  else
163  }
164  else
165  return flagst::mk_unknown();
166  }
167  else
168  return flagst::mk_unknown();
169  }
170  else if(rhs.id()==ID_typecast)
171  {
172  return get_rec(to_typecast_expr(rhs).op(), loc_info_src);
173  }
174  else if(rhs.id()==ID_uninitialized)
175  {
176  return flagst::mk_uninitialized();
177  }
178  else if(rhs.id()==ID_plus)
179  {
180  const auto &plus_expr = to_plus_expr(rhs);
181 
182  if(plus_expr.operands().size() >= 3)
183  {
185  plus_expr.op0().type().id() == ID_pointer,
186  "pointer in pointer-typed sum must be op0");
187  return get_rec(plus_expr.op0(), loc_info_src) | flagst::mk_uses_offset();
188  }
189  else if(plus_expr.operands().size() == 2)
190  {
191  // one must be pointer, one an integer
192  if(plus_expr.op0().type().id() == ID_pointer)
193  {
194  return get_rec(plus_expr.op0(), loc_info_src) |
196  }
197  else if(plus_expr.op1().type().id() == ID_pointer)
198  {
199  return get_rec(plus_expr.op1(), loc_info_src) |
201  }
202  else
203  return flagst::mk_unknown();
204  }
205  else
206  return flagst::mk_unknown();
207  }
208  else if(rhs.id()==ID_minus)
209  {
210  const auto &op0 = to_minus_expr(rhs).op0();
211 
212  if(op0.type().id() == ID_pointer)
213  {
214  return get_rec(op0, loc_info_src) | flagst::mk_uses_offset();
215  }
216  else
217  return flagst::mk_unknown();
218  }
219  else if(rhs.id()==ID_member)
220  {
221  return flagst::mk_unknown();
222  }
223  else if(rhs.id()==ID_index)
224  {
225  return flagst::mk_unknown();
226  }
227  else if(rhs.id()==ID_dereference)
228  {
229  return flagst::mk_unknown();
230  }
231  else if(rhs.id()==ID_side_effect)
232  {
233  const side_effect_exprt &side_effect_expr=to_side_effect_expr(rhs);
234  const irep_idt &statement=side_effect_expr.get_statement();
235 
236  if(statement==ID_allocate)
237  return flagst::mk_dynamic_heap();
238  else
239  return flagst::mk_unknown();
240  }
241  else
242  return flagst::mk_unknown();
243 }
244 
246 {
247  if(cfg.nodes.empty())
248  return;
249 
250  work_queuet work_queue;
251  work_queue.push(0);
252 
253  loc_infos.resize(cfg.nodes.size());
254 
255  // Gather the objects we track, and
256  // feed in sufficiently bad defaults for their value
257  // in the entry location.
258  for(const auto &local : locals.locals)
259  {
260  if(is_tracked(local))
262  }
263 
264  while(!work_queue.empty())
265  {
266  unsigned loc_nr=work_queue.top();
267  const local_cfgt::nodet &node=cfg.nodes[loc_nr];
268  const goto_programt::instructiont &instruction=*node.t;
269  work_queue.pop();
270 
271  auto &loc_info_src=loc_infos[loc_nr];
272  auto loc_info_dest=loc_infos[loc_nr];
273 
274  switch(instruction.type)
275  {
276  case ASSIGN:
277  {
278  const code_assignt &code_assign = instruction.get_assign();
279  assign_lhs(
280  code_assign.lhs(), code_assign.rhs(), loc_info_src, loc_info_dest);
281  break;
282  }
283 
284  case DECL:
285  assign_lhs(
286  instruction.decl_symbol(),
287  exprt(ID_uninitialized),
288  loc_info_src,
289  loc_info_dest);
290  break;
291 
292  case DEAD:
293  assign_lhs(
294  instruction.dead_symbol(),
295  exprt(ID_uninitialized),
296  loc_info_src,
297  loc_info_dest);
298  break;
299 
300  case FUNCTION_CALL:
301  {
302  const code_function_callt &code_function_call =
303  instruction.get_function_call();
304  if(code_function_call.lhs().is_not_nil())
305  assign_lhs(
306  code_function_call.lhs(), nil_exprt(), loc_info_src, loc_info_dest);
307  break;
308  }
309 
310  case CATCH:
311  case THROW:
312 #if 0
313  DATA_INVARIANT(false, "Exceptions must be removed before analysis");
314  break;
315 #endif
316  case RETURN:
317 #if 0
318  DATA_INVARIANT(false, "Returns must be removed before analysis");
319  break;
320 #endif
321  case ATOMIC_BEGIN: // Ignoring is a valid over-approximation
322  case ATOMIC_END: // Ignoring is a valid over-approximation
323  case LOCATION: // No action required
324  case START_THREAD: // Require a concurrent analysis at higher level
325  case END_THREAD: // Require a concurrent analysis at higher level
326  case SKIP: // No action required
327  case ASSERT: // No action required
328  case ASSUME: // Ignoring is a valid over-approximation
329  case GOTO: // Ignoring the guard is a valid over-approximation
330  case END_FUNCTION: // No action required
331  break;
332  case OTHER:
333 #if 0
335  false, "Unclear what is a safe over-approximation of OTHER");
336 #endif
337  break;
338  case INCOMPLETE_GOTO:
339  case NO_INSTRUCTION_TYPE:
340  DATA_INVARIANT(false, "Only complete instructions can be analyzed");
341  break;
342  }
343 
344  for(const auto &succ : node.successors)
345  {
346  assert(succ<loc_infos.size());
347  if(merge(loc_infos[succ], (loc_info_dest)))
348  work_queue.push(succ);
349  }
350  }
351 }
352 
354  std::ostream &out,
355  const goto_functiont &goto_function,
356  const namespacet &ns) const
357 {
358  unsigned l=0;
359 
360  for(const auto &instruction : goto_function.body.instructions)
361  {
362  out << "**** " << instruction.source_location << "\n";
363 
364  const auto &loc_info=loc_infos[l];
365 
367  p_it=loc_info.begin();
368  p_it!=loc_info.end();
369  p_it++)
370  {
371  out << " " << pointers[p_it-loc_info.begin()]
372  << ": "
373  << *p_it
374  << "\n";
375  }
376 
377  out << "\n";
378  goto_function.body.output_instruction(ns, irep_idt(), out, instruction);
379  out << "\n";
380 
381  l++;
382  }
383 }
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:37
goto_functiont::body
goto_programt body
Definition: goto_function.h:29
local_bitvector_analysist::ns
const namespacet & ns
Definition: local_bitvector_analysis.h:181
local_bitvector_analysis.h
Field-insensitive, location-sensitive bitvector analysis.
address_of_exprt::object
exprt & object()
Definition: pointer_expr.h:339
local_cfgt::loc_map
loc_mapt loc_map
Definition: local_cfg.h:33
local_bitvector_analysist::flagst::mk_null
static flagst mk_null()
Definition: local_bitvector_analysis.h:136
local_bitvector_analysist::flagst::mk_dynamic_local
static flagst mk_dynamic_local()
Definition: local_bitvector_analysis.h:116
code_assignt::rhs
exprt & rhs()
Definition: std_code.h:317
to_index_expr
const index_exprt & to_index_expr(const exprt &expr)
Cast an exprt to an index_exprt.
Definition: std_expr.h:1296
to_if_expr
const if_exprt & to_if_expr(const exprt &expr)
Cast an exprt to an if_exprt.
Definition: std_expr.h:2151
local_bitvector_analysist::assign_lhs
void assign_lhs(const exprt &lhs, const exprt &rhs, points_tot &loc_info_src, points_tot &loc_info_dest)
Definition: local_bitvector_analysis.cpp:69
expanding_vectort::const_iterator
data_typet::const_iterator const_iterator
Definition: expanding_vector.h:27
local_bitvector_analysist::flagst::is_dynamic_heap
bool is_dynamic_heap() const
Definition: local_bitvector_analysis.h:131
goto_programt::instructiont::type
goto_program_instruction_typet type
What kind of instruction?
Definition: goto_program.h:350
local_bitvector_analysist::build
void build()
Definition: local_bitvector_analysis.cpp:245
exprt
Base class for all expressions.
Definition: expr.h:54
localst::locals
locals_sett locals
Definition: locals.h:38
irep_idt
dstringt irep_idt
Definition: irep.h:37
to_side_effect_expr
side_effect_exprt & to_side_effect_expr(exprt &expr)
Definition: std_code.h:1954
local_bitvector_analysist::flagst::mk_unknown
static flagst mk_unknown()
Definition: local_bitvector_analysis.h:86
local_bitvector_analysist::is_tracked
bool is_tracked(const irep_idt &identifier)
Definition: local_bitvector_analysis.cpp:62
goto_programt::instructiont::decl_symbol
const symbol_exprt & decl_symbol() const
Get the declared symbol for DECL.
Definition: goto_program.h:225
expanding_vectort
Definition: expanding_vector.h:17
local_bitvector_analysist::pointers
numberingt< irep_idt > pointers
Definition: local_bitvector_analysis.h:186
to_minus_expr
const minus_exprt & to_minus_expr(const exprt &expr)
Cast an exprt to a minus_exprt.
Definition: std_expr.h:914
code_function_callt::lhs
exprt & lhs()
Definition: std_code.h:1240
goto_programt::instructiont::get_assign
const code_assignt & get_assign() const
Get the assignment for ASSIGN.
Definition: goto_program.h:199
local_bitvector_analysist::loc_infos
loc_infost loc_infos
Definition: local_bitvector_analysis.h:195
local_bitvector_analysist::cfg
local_cfgt cfg
Definition: local_bitvector_analysis.h:46
namespacet
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:92
THROW
@ THROW
Definition: goto_program.h:51
namespacet::lookup
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See documentation for namespace_baset::lookup().
Definition: namespace.cpp:141
code_function_callt
codet representation of a function call statement.
Definition: std_code.h:1215
irept::is_not_nil
bool is_not_nil() const
Definition: irep.h:391
GOTO
@ GOTO
Definition: goto_program.h:35
DATA_INVARIANT
#define DATA_INVARIANT(CONDITION, REASON)
This condition should be used to document that assumptions that are made on goto_functions,...
Definition: invariant.h:511
goto_programt::instructiont::dead_symbol
const symbol_exprt & dead_symbol() const
Get the symbol for DEAD.
Definition: goto_program.h:267
goto_programt::output_instruction
std::ostream & output_instruction(const namespacet &ns, const irep_idt &identifier, std::ostream &out, const instructionst::value_type &instruction) const
Output a single instruction.
Definition: goto_program.cpp:42
language_util.h
local_bitvector_analysist::get_rec
flagst get_rec(const exprt &rhs, points_tot &loc_info_src)
Definition: local_bitvector_analysis.cpp:120
symbol_exprt::get_identifier
const irep_idt & get_identifier() const
Definition: std_expr.h:109
local_bitvector_analysist::locals
localst locals
Definition: local_bitvector_analysis.h:45
nil_exprt
The NIL expression.
Definition: std_expr.h:2734
local_bitvector_analysist::flagst::mk_integer_address
static flagst mk_integer_address()
Definition: local_bitvector_analysis.h:156
local_bitvector_analysist::flagst::is_integer_address
bool is_integer_address() const
Definition: local_bitvector_analysis.h:161
to_plus_expr
const plus_exprt & to_plus_expr(const exprt &expr)
Cast an exprt to a plus_exprt.
Definition: std_expr.h:869
pointer_expr.h
API to expression classes for Pointers.
code_assignt::lhs
exprt & lhs()
Definition: std_code.h:312
goto_programt::instructiont::get_function_call
const code_function_callt & get_function_call() const
Get the function call for FUNCTION_CALL.
Definition: goto_program.h:319
NO_INSTRUCTION_TYPE
@ NO_INSTRUCTION_TYPE
Definition: goto_program.h:34
local_cfgt::nodes
nodest nodes
Definition: local_cfg.h:36
localst::is_local
bool is_local(const irep_idt &identifier) const
Definition: locals.h:32
index_exprt::array
exprt & array()
Definition: std_expr.h:1258
local_bitvector_analysist::flagst::is_null
bool is_null() const
Definition: local_bitvector_analysis.h:141
to_symbol_expr
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
Definition: std_expr.h:189
OTHER
@ OTHER
Definition: goto_program.h:38
local_bitvector_analysist::flagst::is_static_lifetime
bool is_static_lifetime() const
Definition: local_bitvector_analysis.h:151
irept::id
const irep_idt & id() const
Definition: irep.h:407
goto_functiont
A goto function, consisting of function body (see body) and parameter identifiers (see parameter_iden...
Definition: goto_function.h:27
local_cfgt::nodet
Definition: local_cfg.h:26
END_FUNCTION
@ END_FUNCTION
Definition: goto_program.h:43
SKIP
@ SKIP
Definition: goto_program.h:39
local_bitvector_analysist::flagst::mk_dynamic_heap
static flagst mk_dynamic_heap()
Definition: local_bitvector_analysis.h:126
local_bitvector_analysist::output
void output(std::ostream &out, const goto_functiont &goto_function, const namespacet &ns) const
Definition: local_bitvector_analysis.cpp:353
local_bitvector_analysist::flagst::is_dynamic_local
bool is_dynamic_local() const
Definition: local_bitvector_analysis.h:121
local_bitvector_analysist::work_queuet
std::stack< unsigned > work_queuet
Definition: local_bitvector_analysis.h:184
std_code.h
side_effect_exprt::get_statement
const irep_idt & get_statement() const
Definition: std_code.h:1920
local_bitvector_analysist::flagst::mk_uninitialized
static flagst mk_uninitialized()
Definition: local_bitvector_analysis.h:96
local_bitvector_analysist::flagst::is_uses_offset
bool is_uses_offset() const
Definition: local_bitvector_analysis.h:111
RETURN
@ RETURN
Definition: goto_program.h:46
exprt::is_zero
bool is_zero() const
Return whether the expression is a constant representing 0.
Definition: expr.cpp:91
local_bitvector_analysist::flagst::is_unknown
bool is_unknown() const
Definition: local_bitvector_analysis.h:91
local_bitvector_analysist::dirty
dirtyt dirty
Definition: local_bitvector_analysis.h:44
goto_programt::instructions
instructionst instructions
The list of instructions in the goto program.
Definition: goto_program.h:569
expr_util.h
Deprecated expression utility functions.
ASSIGN
@ ASSIGN
Definition: goto_program.h:47
local_bitvector_analysist::flagst::mk_uses_offset
static flagst mk_uses_offset()
Definition: local_bitvector_analysis.h:106
CATCH
@ CATCH
Definition: goto_program.h:52
DECL
@ DECL
Definition: goto_program.h:48
expanding_vectort::size
size_type size() const
Definition: expanding_vector.h:46
local_bitvector_analysist::flagst
Definition: local_bitvector_analysis.h:50
numberingt::number
number_type number(const key_type &a)
Definition: numbering.h:37
ASSUME
@ ASSUME
Definition: goto_program.h:36
to_typecast_expr
const typecast_exprt & to_typecast_expr(const exprt &expr)
Cast an exprt to a typecast_exprt.
Definition: std_expr.h:1814
local_bitvector_analysist::flagst::print
void print(std::ostream &) const
Definition: local_bitvector_analysis.cpp:25
local_cfgt::nodet::successors
successorst successors
Definition: local_cfg.h:29
local_bitvector_analysist::flagst::is_uninitialized
bool is_uninitialized() const
Definition: local_bitvector_analysis.h:101
START_THREAD
@ START_THREAD
Definition: goto_program.h:40
FUNCTION_CALL
@ FUNCTION_CALL
Definition: goto_program.h:50
local_bitvector_analysist::get
flagst get(const goto_programt::const_targett t, const exprt &src)
Definition: local_bitvector_analysis.cpp:109
to_member_expr
const member_exprt & to_member_expr(const exprt &expr)
Cast an exprt to a member_exprt.
Definition: std_expr.h:2611
ATOMIC_END
@ ATOMIC_END
Definition: goto_program.h:45
goto_programt::const_targett
instructionst::const_iterator const_targett
Definition: goto_program.h:564
to_address_of_expr
const address_of_exprt & to_address_of_expr(const exprt &expr)
Cast an exprt to an address_of_exprt.
Definition: pointer_expr.h:367
DEAD
@ DEAD
Definition: goto_program.h:49
local_cfgt::nodet::t
goto_programt::const_targett t
Definition: local_cfg.h:28
index_exprt
Array index operator.
Definition: std_expr.h:1242
ATOMIC_BEGIN
@ ATOMIC_BEGIN
Definition: goto_program.h:44
code_assignt
A codet representing an assignment in the program.
Definition: std_code.h:295
LOCATION
@ LOCATION
Definition: goto_program.h:42
ASSERT
@ ASSERT
Definition: goto_program.h:37
goto_programt::instructiont
This class represents an instruction in the GOTO intermediate representation.
Definition: goto_program.h:180
std_expr.h
API to expression classes.
c_types.h
local_bitvector_analysist::merge
static bool merge(points_tot &a, points_tot &b)
Definition: local_bitvector_analysis.cpp:45
END_THREAD
@ END_THREAD
Definition: goto_program.h:41
INCOMPLETE_GOTO
@ INCOMPLETE_GOTO
Definition: goto_program.h:53
side_effect_exprt
An expression containing a side effect.
Definition: std_code.h:1898
binary_exprt::op0
exprt & op0()
Definition: expr.h:103
local_bitvector_analysist::flagst::mk_static_lifetime
static flagst mk_static_lifetime()
Definition: local_bitvector_analysis.h:146