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 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  const std::vector<exprt> expr_set = value_sets.get_values(
116  SSA_step.source.function_id, target, deref_expr.pointer());
117  const std::unordered_set<irep_idt> symbols =
118  find_symbols_or_nexts(expr_set.begin(), expr_set.end());
119 
120  if(symbols.find(lhs_identifier)!=symbols.end())
121  {
122  // may alias!
123  exprt tmp;
124  tmp.swap(deref_expr.pointer());
126  deref_expr.swap(tmp);
127  compute_rec(deref_expr);
128  }
129  else
130  {
131  // nah, ok
132  compute_rec(deref_expr.pointer());
133  }
134  }
135  else if(dest==SSA_step.ssa_lhs.get_original_expr())
136  {
138  }
139  else
140  Forall_operands(it, dest)
141  compute_rec(*it);
142 }
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:37
Forall_operands
#define Forall_operands(it, expr)
Definition: expr.h:24
SSA_stept
Single SSA step in the equation.
Definition: ssa_step.h:45
find_symbols_or_nexts
void find_symbols_or_nexts(const exprt &src, find_symbols_sett &dest)
Add to the set dest the sub-expressions of src with id ID_symbol or ID_next_symbol.
Definition: find_symbols.cpp:18
to_index_expr
const index_exprt & to_index_expr(const exprt &expr)
Cast an exprt to an index_exprt.
Definition: std_expr.h:1347
goto_symex_statet
Central data structure: state.
Definition: goto_symex_state.h:46
goto_model.h
Symbol Table + CFG.
exprt
Base class for all expressions.
Definition: expr.h:53
ssa_exprt::get_object_name
irep_idt get_object_name() const
SSA_stept::source
symex_targett::sourcet source
Definition: ssa_step.h:47
ssa_exprt::get_original_expr
const exprt & get_original_expr() const
Definition: ssa_expr.h:33
preconditiont::target
const goto_programt::const_targett target
Definition: precondition.cpp:41
exprt::is_false
bool is_false() const
Return whether the expression is a constant representing false.
Definition: expr.cpp:101
preconditiont::compute_address_of
void compute_address_of(exprt &dest)
Definition: precondition.cpp:73
precondition.h
Generate Equation using Symbolic Execution.
preconditiont::compute_rec
void compute_rec(exprt &dest)
Definition: precondition.cpp:100
namespacet
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:92
preconditiont::s
const goto_symex_statet & s
Definition: precondition.cpp:43
find_symbols.h
preconditiont
Definition: precondition.cpp:22
SSA_stept::ssa_lhs
ssa_exprt ssa_lhs
Definition: ssa_step.h:141
to_address_of_expr
const address_of_exprt & to_address_of_expr(const exprt &expr)
Cast an exprt to an address_of_exprt.
Definition: std_expr.h:2823
preconditiont::SSA_step
const SSA_stept & SSA_step
Definition: precondition.cpp:42
preconditiont::compute
void compute(exprt &dest)
Definition: precondition.cpp:95
irept::swap
void swap(irept &irep)
Definition: irep.h:463
get_original_name
exprt get_original_name(exprt expr)
Undo all levels of renaming.
Definition: renaming_level.cpp:169
irept::id
const irep_idt & id() const
Definition: irep.h:418
symex_target_equationt
Inheriting the interface of symex_targett this class represents the SSA form of the input program as ...
Definition: symex_target_equation.h:41
to_dereference_expr
const dereference_exprt & to_dereference_expr(const exprt &expr)
Cast an exprt to a dereference_exprt.
Definition: std_expr.h:2944
value_setst
Definition: value_sets.h:22
goto_symex_state.h
Symbolic Execution.
symex_target_equationt::SSA_steps
SSA_stepst SSA_steps
Definition: symex_target_equation.h:257
preconditiont::preconditiont
preconditiont(const namespacet &_ns, value_setst &_value_sets, const goto_programt::const_targett _target, const SSA_stept &_SSA_step, const goto_symex_statet &_s)
Definition: precondition.cpp:24
preconditiont::ns
const namespacet & ns
Definition: precondition.cpp:39
SSA_stept::ssa_rhs
exprt ssa_rhs
Definition: ssa_step.h:143
goto_program_dereference.h
Value Set.
precondition
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)
Definition: precondition.cpp:53
to_member_expr
const member_exprt & to_member_expr(const exprt &expr)
Cast an exprt to a member_exprt.
Definition: std_expr.h:3489
goto_programt::const_targett
instructionst::const_iterator const_targett
Definition: goto_program.h:580
value_setst::get_values
virtual void get_values(const irep_idt &function_id, goto_programt::const_targett l, const exprt &expr, valuest &dest)=0
dereference
void dereference(const irep_idt &function_id, goto_programt::const_targett target, exprt &expr, const namespacet &ns, value_setst &value_sets)
Remove dereferences in expr using value_sets to determine to what objects the pointers may be pointin...
Definition: goto_program_dereference.cpp:303
symex_targett::sourcet::function_id
irep_idt function_id
Definition: symex_target.h:40
preconditiont::value_sets
value_setst & value_sets
Definition: precondition.cpp:40