CBMC
goto_symex_state.h
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 #ifndef CPROVER_GOTO_SYMEX_GOTO_SYMEX_STATE_H
13 #define CPROVER_GOTO_SYMEX_GOTO_SYMEX_STATE_H
14 
15 #include <functional>
16 #include <memory>
17 
18 #include <analyses/guard.h>
19 
20 #include <util/invariant.h>
21 #include <util/nodiscard.h>
22 #include <util/ssa_expr.h>
23 #include <util/std_expr.h>
24 
25 #include "call_stack.h"
26 #include "field_sensitivity.h"
27 #include "goto_state.h"
28 #include "renaming_level.h"
29 #include "symex_target_equation.h"
30 
31 class incremental_dirtyt;
32 
41 class goto_symex_statet final : public goto_statet
42 {
43 public:
45  const symex_targett::sourcet &,
46  std::size_t max_field_sensitive_array_size,
47  bool should_simplify,
48  guard_managert &manager,
49  std::function<std::size_t(const irep_idt &)> fresh_l2_name_provider);
51 
54  const goto_symex_statet &other,
55  symex_target_equationt *const target)
56  : goto_symex_statet(other) // NOLINT
57  {
58  symex_target = target;
59  }
60 
62 
67 
68  // Manager is required to be able to resize the thread vector
71 
73 
98  template <levelt level = L2>
100 
103  template <levelt level>
105  rename_ssa(ssa_exprt ssa, const namespacet &ns);
106 
107  template <levelt level = L2>
108  void rename(typet &type, const irep_idt &l1_identifier, const namespacet &ns);
109 
110  NODISCARD exprt l2_rename_rvalues(exprt lvalue, const namespacet &ns);
111 
114  ssa_exprt lhs, // L0/L1
115  const exprt &rhs, // L2
116  const namespacet &ns,
117  bool rhs_is_simplified,
118  bool record_value,
119  bool allow_pointer_unsoundness = false);
120 
122 
123 protected:
124  template <levelt>
125  void rename_address(exprt &expr, const namespacet &ns);
126 
128  template <levelt level>
130  set_indices(ssa_exprt expr, const namespacet &ns);
131 
132  // this maps L1 names to (L2) types
133  typedef std::unordered_map<irep_idt, typet> l1_typest;
135 
136 public:
137  // guards
139  {
140  static irep_idt id = "goto_symex::\\guard";
141  return id;
142  }
143 
145  {
147  return threads[source.thread_nr].call_stack;
148  }
149 
150  const call_stackt &call_stack() const
151  {
153  return threads[source.thread_nr].call_stack;
154  }
155 
163  const symbol_exprt &expr,
164  std::function<std::size_t(const irep_idt &)> index_generator,
165  const namespacet &ns);
166 
171  ssa_exprt declare(ssa_exprt ssa, const namespacet &ns);
172 
173  void print_backtrace(std::ostream &) const;
174 
175  // threads
176  typedef std::pair<unsigned, std::list<guardt> > a_s_r_entryt;
177  typedef std::list<guardt> a_s_w_entryt;
178  std::unordered_map<ssa_exprt, a_s_r_entryt, irep_hash> read_in_atomic_section;
179  std::unordered_map<ssa_exprt, a_s_w_entryt, irep_hash>
181 
182  struct threadt
183  {
188  std::map<irep_idt, unsigned> function_frame;
189  unsigned atomic_section_id = 0;
192  {
193  }
194  };
195 
196  std::vector<threadt> threads;
197 
199  {
200  NOT_SHARED,
202  SHARED
203  };
204 
205  bool l2_thread_read_encoding(ssa_exprt &expr, const namespacet &ns);
206  bool l2_thread_write_encoding(const ssa_exprt &expr, const namespacet &ns);
208  write_is_shared(const ssa_exprt &expr, const namespacet &ns) const;
209 
210  std::stack<bool> record_events;
211 
212  const incremental_dirtyt *dirty = nullptr;
213 
215 
218 
222 
225 
226  unsigned total_vccs = 0;
227  unsigned remaining_vccs = 0;
228 
230  void drop_existing_l1_name(const irep_idt &l1_identifier)
231  {
232  level2.current_names.erase(l1_identifier);
233  }
234 
236  void drop_l1_name(const irep_idt &l1_identifier)
237  {
238  level2.current_names.erase_if_exists(l1_identifier);
239  }
240 
241  std::function<std::size_t(const irep_idt &)> get_l2_name_provider() const
242  {
243  return fresh_l2_name_provider;
244  }
245 
247  static bool is_read_only_object(const exprt &lvalue)
248  {
249  // Note ID_constant can occur due to a partial write to a string constant,
250  // (i.e. something like byte_extract int from "hello" offset 2), which
251  // simplifies to a plain constant.
252  return lvalue.id() == ID_string_constant || lvalue.id() == ID_null_object ||
253  lvalue.id() == "zero_string" || lvalue.id() == "is_zero_string" ||
254  lvalue.id() == "zero_string_length" || lvalue.id() == ID_constant ||
255  lvalue.id() == ID_array;
256  }
257 
258 private:
259  std::function<std::size_t(const irep_idt &)> fresh_l2_name_provider;
260 
271  goto_symex_statet(const goto_symex_statet &other) = default;
272 };
273 
274 #endif // CPROVER_GOTO_SYMEX_GOTO_SYMEX_STATE_H
guard_exprt
Definition: guard_expr.h:23
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:36
symex_target_equation.h
goto_symex_statet::threadt::atomic_section_id
unsigned atomic_section_id
Definition: goto_symex_state.h:189
symbol_tablet
The symbol table.
Definition: symbol_table.h:13
goto_symex_statet::drop_l1_name
void drop_l1_name(const irep_idt &l1_identifier)
Drops an L1 name from the local L2 map.
Definition: goto_symex_state.h:236
goto_symex_statet::print_backtrace
void print_backtrace(std::ostream &) const
Dumps the current state of symex, printing the function name and location number for each stack frame...
Definition: goto_symex_state.cpp:769
goto_symex_statet::a_s_r_entryt
std::pair< unsigned, std::list< guardt > > a_s_r_entryt
Definition: goto_symex_state.h:176
goto_symex_statet::remaining_vccs
unsigned remaining_vccs
Definition: goto_symex_state.h:227
goto_symex_statet::threadt::pc
goto_programt::const_targett pc
Definition: goto_symex_state.h:184
NODISCARD
#define NODISCARD
Definition: nodiscard.h:22
goto_symex_statet::level1
symex_level1t level1
Definition: goto_symex_state.h:72
goto_symex_statet::assignment
renamedt< ssa_exprt, L2 > assignment(ssa_exprt lhs, const exprt &rhs, const namespacet &ns, bool rhs_is_simplified, bool record_value, bool allow_pointer_unsoundness=false)
Definition: goto_symex_state.cpp:73
goto_symex_statet::guard_identifier
static irep_idt guard_identifier()
Definition: goto_symex_state.h:138
typet
The type of an expression, extends irept.
Definition: type.h:28
goto_symex_statet::l2_rename_rvalues
exprt l2_rename_rvalues(exprt lvalue, const namespacet &ns)
Definition: goto_symex_state.cpp:285
goto_symex_statet::dirty
const incremental_dirtyt * dirty
Definition: goto_symex_state.h:212
goto_symex_statet::read_in_atomic_section
std::unordered_map< ssa_exprt, a_s_r_entryt, irep_hash > read_in_atomic_section
Definition: goto_symex_state.h:178
goto_symex_statet::has_saved_jump_target
bool has_saved_jump_target
This state is saved, with the PC pointing to the target of a GOTO.
Definition: goto_symex_state.h:217
goto_symex_statet
Central data structure: state.
Definition: goto_symex_state.h:41
goto_symex_statet::~goto_symex_statet
~goto_symex_statet()
sharing_mapt::erase_if_exists
void erase_if_exists(const key_type &k)
Erase element if it exists.
Definition: sharing_map.h:274
goto_symex_statet::call_stack
const call_stackt & call_stack() const
Definition: goto_symex_state.h:150
goto_symex_statet::fresh_l2_name_provider
std::function< std::size_t(const irep_idt &)> fresh_l2_name_provider
Definition: goto_symex_state.h:259
goto_symex_statet::guard_manager
guard_managert & guard_manager
Definition: goto_symex_state.h:69
symex_targett::sourcet::thread_nr
unsigned thread_nr
Definition: symex_target.h:38
invariant.h
goto_symex_statet::written_in_atomic_section
std::unordered_map< ssa_exprt, a_s_w_entryt, irep_hash > written_in_atomic_section
Definition: goto_symex_state.h:180
nodiscard.h
exprt
Base class for all expressions.
Definition: expr.h:55
goto_symex_statet::source
symex_targett::sourcet source
Definition: goto_symex_state.h:61
goto_symex_statet::threadt::threadt
threadt(guard_managert &guard_manager)
Definition: goto_symex_state.h:190
goto_symex_statet::is_read_only_object
static bool is_read_only_object(const exprt &lvalue)
Returns true if lvalue is a read-only object, such as the null object.
Definition: goto_symex_state.h:247
goto_symex_statet::set_indices
renamedt< ssa_exprt, level > set_indices(ssa_exprt expr, const namespacet &ns)
Update values up to level.
field_sensitivityt
Control granularity of object accesses.
Definition: field_sensitivity.h:82
symbol_exprt
Expression to hold a symbol (variable)
Definition: std_expr.h:112
goto_symex_statet::write_is_shared_resultt::SHARED
@ SHARED
call_stackt
Definition: call_stack.h:14
goto_state.h
ssa_exprt
Expression providing an SSA-renamed symbol of expressions.
Definition: ssa_expr.h:16
namespacet
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:90
guard.h
guard_expr_managert
This is unused by this implementation of guards, but can be used by other implementations of the same...
Definition: guard_expr.h:19
goto_symex_statet::l2_thread_write_encoding
bool l2_thread_write_encoding(const ssa_exprt &expr, const namespacet &ns)
thread encoding
Definition: goto_symex_state.cpp:522
goto_symex_statet::threadt::guard
guardt guard
Definition: goto_symex_state.h:186
goto_symex_statet::threads
std::vector< threadt > threads
Definition: goto_symex_state.h:196
goto_symex_statet::call_stack
call_stackt & call_stack()
Definition: goto_symex_state.h:144
goto_symex_statet::has_saved_next_instruction
bool has_saved_next_instruction
This state is saved, with the PC pointing to the next instruction of a GOTO.
Definition: goto_symex_state.h:221
goto_symex_statet::symbol_table
symbol_tablet symbol_table
contains symbols that are minted during symbolic execution, such as dynamically created objects etc.
Definition: goto_symex_state.h:66
goto_symex_statet::l2_thread_read_encoding
bool l2_thread_read_encoding(ssa_exprt &expr, const namespacet &ns)
thread encoding
Definition: goto_symex_state.cpp:356
goto_symex_statet::total_vccs
unsigned total_vccs
Definition: goto_symex_state.h:226
goto_symex_statet::threadt::function_id
irep_idt function_id
Definition: goto_symex_state.h:185
PRECONDITION
#define PRECONDITION(CONDITION)
Definition: invariant.h:463
renaming_level.h
goto_statet::level2
symex_level2t level2
Definition: goto_state.h:38
goto_symex_statet::rename
renamedt< exprt, level > rename(exprt expr, const namespacet &ns)
Rewrites symbol expressions in exprt, applying a suffix to each symbol reflecting its most recent ver...
Definition: goto_symex_state.cpp:169
goto_symex_statet::write_is_shared_resultt
write_is_shared_resultt
Definition: goto_symex_state.h:198
goto_symex_statet::symex_target
symex_target_equationt * symex_target
Definition: goto_symex_state.h:70
irept::id
const irep_idt & id() const
Definition: irep.h:396
goto_symex_statet::rename_ssa
renamedt< ssa_exprt, level > rename_ssa(ssa_exprt ssa, const namespacet &ns)
Version of rename which is specialized for SSA exprt.
Definition: goto_symex_state.cpp:150
goto_symex_statet::l1_typest
std::unordered_map< irep_idt, typet > l1_typest
Definition: goto_symex_state.h:133
goto_symex_statet::write_is_shared
write_is_shared_resultt write_is_shared(const ssa_exprt &expr, const namespacet &ns) const
Definition: goto_symex_state.cpp:494
goto_statet
Container for data that varies per program point, e.g.
Definition: goto_state.h:31
goto_symex_statet::field_sensitivity
field_sensitivityt field_sensitivity
Definition: goto_symex_state.h:121
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:33
goto_symex_statet::goto_symex_statet
goto_symex_statet(const symex_targett::sourcet &, std::size_t max_field_sensitive_array_size, bool should_simplify, guard_managert &manager, std::function< std::size_t(const irep_idt &)> fresh_l2_name_provider)
Definition: goto_symex_state.cpp:31
goto_symex_statet::goto_symex_statet
goto_symex_statet(const goto_symex_statet &other, symex_target_equationt *const target)
Fake "copy constructor" that initializes the symex_target member.
Definition: goto_symex_state.h:53
goto_symex_statet::run_validation_checks
bool run_validation_checks
Should the additional validation checks be run?
Definition: goto_symex_state.h:224
goto_symex_statet::a_s_w_entryt
std::list< guardt > a_s_w_entryt
Definition: goto_symex_state.h:177
ssa_expr.h
symex_level2t::current_names
symex_renaming_levelt current_names
Definition: renaming_level.h:70
goto_symex_statet::threadt
Definition: goto_symex_state.h:182
goto_symex_statet::declare
ssa_exprt declare(ssa_exprt ssa, const namespacet &ns)
Add invalid (or a failed symbol) to the value_set if ssa is a pointer, ensure that level2 index of sy...
Definition: goto_symex_state.cpp:815
goto_symex_statet::threadt::function_frame
std::map< irep_idt, unsigned > function_frame
Definition: goto_symex_state.h:188
goto_symex_statet::write_is_shared_resultt::NOT_SHARED
@ NOT_SHARED
goto_symex_statet::write_is_shared_resultt::IN_ATOMIC_SECTION
@ IN_ATOMIC_SECTION
goto_symex_statet::record_events
std::stack< bool > record_events
Definition: goto_symex_state.h:210
symex_level1t
Functor to set the level 1 renaming of SSA expressions.
Definition: renaming_level.h:39
call_stack.h
goto_programt::const_targett
instructionst::const_iterator const_targett
Definition: goto_program.h:587
goto_symex_statet::saved_target
goto_programt::const_targett saved_target
Definition: goto_symex_state.h:214
symex_targett::sourcet
Identifies source in the context of symbolic execution.
Definition: symex_target.h:36
field_sensitivity.h
incremental_dirtyt
Wrapper for dirtyt that permits incremental population, ensuring each function is analysed exactly on...
Definition: dirty.h:117
goto_symex_statet::threadt::call_stack
call_stackt call_stack
Definition: goto_symex_state.h:187
goto_symex_statet::get_l2_name_provider
std::function< std::size_t(const irep_idt &)> get_l2_name_provider() const
Definition: goto_symex_state.h:241
true_exprt
The Boolean constant true.
Definition: std_expr.h:3007
sharing_mapt::erase
void erase(const key_type &k)
Erase element, element must exist in map.
Definition: sharing_map.h:1203
std_expr.h
goto_symex_statet::add_object
ssa_exprt add_object(const symbol_exprt &expr, std::function< std::size_t(const irep_idt &)> index_generator, const namespacet &ns)
Instantiate the object expr.
Definition: goto_symex_state.cpp:790
goto_symex_statet::rename_address
void rename_address(exprt &expr, const namespacet &ns)
Definition: goto_symex_state.cpp:551
goto_symex_statet::l1_types
l1_typest l1_types
Definition: goto_symex_state.h:134
renamedt
Wrapper for expressions or types which have been renamed up to a given level.
Definition: renamed.h:32
goto_symex_statet::drop_existing_l1_name
void drop_existing_l1_name(const irep_idt &l1_identifier)
Drops an L1 name from the local L2 map.
Definition: goto_symex_state.h:230