cprover
precondition.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Symbolic Execution
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
11 
12 #include "precondition.h"
13 #include "goto_symex_state.h"
14 
15 #include <util/find_symbols.h>
16 
18 
20 
22 {
23 public:
25  const namespacet &_ns,
26  value_setst &_value_sets,
27  const goto_programt::const_targett _target,
28  const symex_target_equationt::SSA_stept &_SSA_step,
29  const goto_symex_statet &_s):
30  ns(_ns),
31  value_sets(_value_sets),
32  target(_target),
33  SSA_step(_SSA_step),
34  s(_s)
35  {
36  }
37 
38 protected:
39  const namespacet &ns;
44  void compute_rec(exprt &dest);
45 
46 public:
47  void compute(exprt &dest);
48 
49 protected:
50  void compute_address_of(exprt &dest);
51 };
52 
54  const namespacet &ns,
55  value_setst &value_sets,
56  const goto_programt::const_targett target,
57  const symex_target_equationt &equation,
58  const goto_symex_statet &s,
59  exprt &dest)
60 {
61  for(symex_target_equationt::SSA_stepst::const_reverse_iterator
62  it=equation.SSA_steps.rbegin();
63  it!=equation.SSA_steps.rend();
64  it++)
65  {
66  preconditiont precondition(ns, value_sets, target, *it, s);
67  precondition.compute(dest);
68  if(dest.is_false())
69  return;
70  }
71 }
72 
74 {
75  if(dest.id()==ID_symbol)
76  {
77  // leave alone
78  }
79  else if(dest.id()==ID_index)
80  {
81  auto &index_expr = to_index_expr(dest);
82  compute_address_of(index_expr.array());
83  compute(index_expr.index());
84  }
85  else if(dest.id()==ID_member)
86  {
87  compute_address_of(to_member_expr(dest).compound());
88  }
89  else if(dest.id()==ID_dereference)
90  {
91  compute(to_dereference_expr(dest).pointer());
92  }
93 }
94 
96 {
97  compute_rec(dest);
98 }
99 
101 {
102  if(dest.id()==ID_address_of)
103  {
104  // only do index!
105  compute_address_of(to_address_of_expr(dest).object());
106  }
107  else if(dest.id()==ID_dereference)
108  {
109  auto &deref_expr = to_dereference_expr(dest);
110 
111  const irep_idt &lhs_identifier=SSA_step.ssa_lhs.get_object_name();
112 
113  // aliasing may happen here
114 
115  value_setst::valuest expr_set;
116  value_sets.get_values(target, deref_expr.pointer(), expr_set);
117  std::unordered_set<irep_idt> symbols;
118 
119  for(value_setst::valuest::const_iterator
120  it=expr_set.begin();
121  it!=expr_set.end();
122  it++)
123  find_symbols(*it, symbols);
124 
125  if(symbols.find(lhs_identifier)!=symbols.end())
126  {
127  // may alias!
128  exprt tmp;
129  tmp.swap(deref_expr.pointer());
131  deref_expr.swap(tmp);
132  compute_rec(deref_expr);
133  }
134  else
135  {
136  // nah, ok
137  compute_rec(deref_expr.pointer());
138  }
139  }
140  else if(dest==SSA_step.ssa_lhs.get_original_expr())
141  {
142  dest=SSA_step.ssa_rhs;
143  s.get_original_name(dest);
144  }
145  else
146  Forall_operands(it, dest)
147  compute_rec(*it);
148 }
void precondition(const namespacet &ns, value_setst &value_sets, const goto_programt::const_targett target, const symex_target_equationt &equation, const goto_symex_statet &s, exprt &dest)
virtual void get_values(goto_programt::const_targett l, const exprt &expr, valuest &dest)=0
value_setst & value_sets
preconditiont(const namespacet &_ns, value_setst &_value_sets, const goto_programt::const_targett _target, const symex_target_equationt::SSA_stept &_SSA_step, const goto_symex_statet &_s)
bool is_false() const
Return whether the expression is a constant representing false.
Definition: expr.cpp:99
Inheriting the interface of symex_targett this class represents the SSA form of the input program as ...
Symbolic Execution.
const symex_target_equationt::SSA_stept & SSA_step
const address_of_exprt & to_address_of_expr(const exprt &expr)
Cast an exprt to an address_of_exprt.
Definition: std_expr.h:3282
void compute_rec(exprt &dest)
const dereference_exprt & to_dereference_expr(const exprt &expr)
Cast an exprt to a dereference_exprt.
Definition: std_expr.h:3397
void get_original_name(exprt &expr) const
const irep_idt & id() const
Definition: irep.h:259
Symbol Table + CFG.
exprt dereference(const exprt &pointer, const namespacet &ns)
Dereference the given pointer-expression.
Definition: dereference.h:75
void compute(exprt &dest)
instructionst::const_iterator const_targett
Definition: goto_program.h:415
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:93
const member_exprt & to_member_expr(const exprt &expr)
Cast an exprt to a member_exprt.
Definition: std_expr.h:3959
const namespacet & ns
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:35
const goto_programt::const_targett target
const goto_symex_statet & s
void compute_address_of(exprt &dest)
Single SSA step in the equation.
Base class for all expressions.
Definition: expr.h:54
irep_idt get_object_name() const
Definition: ssa_expr.h:46
const exprt & get_original_expr() const
Definition: ssa_expr.h:41
void swap(irept &irep)
Definition: irep.h:303
#define Forall_operands(it, expr)
Definition: expr.h:26
std::list< exprt > valuest
Definition: value_sets.h:28
const index_exprt & to_index_expr(const exprt &expr)
Cast an exprt to an index_exprt.
Definition: std_expr.h:1648
void find_symbols(const exprt &src, find_symbols_sett &dest)
Generate Equation using Symbolic Execution.