CBMC
auto_objects.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Symbolic Execution of ANSI-C
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
11 
12 #include <util/fresh_symbol.h>
13 #include <util/pointer_expr.h>
14 #include <util/std_code.h>
15 #include <util/std_expr.h>
16 
17 #include "goto_symex.h"
18 
20 {
21  // produce auto-object symbol
22  symbolt &symbol = get_fresh_aux_symbol(
23  type,
24  "symex",
25  "auto_object",
26  state.source.pc->source_location(),
27  ID_C,
28  state.symbol_table);
29  symbol.is_thread_local = false;
30  symbol.is_file_local = false;
31 
32  return symbol.symbol_expr();
33 }
34 
36 {
37  const typet &type=ns.follow(expr.type());
38 
39  if(type.id()==ID_struct)
40  {
41  const struct_typet &struct_type=to_struct_type(type);
42 
43  for(const auto &comp : struct_type.components())
44  {
45  member_exprt member_expr(expr, comp.get_name(), comp.type());
46 
47  initialize_auto_object(member_expr, state);
48  }
49  }
50  else if(type.id()==ID_pointer)
51  {
53  const typet &base_type = pointer_type.base_type();
54 
55  // we don't like function pointers and
56  // we don't like void *
57  if(base_type.id() != ID_code && base_type.id() != ID_empty)
58  {
59  // could be NULL nondeterministically
60 
61  address_of_exprt address_of_expr(
62  make_auto_object(base_type, state), pointer_type);
63 
64  if_exprt rhs(
67  address_of_expr);
68 
69  symex_assign(state, expr, rhs);
70  }
71  }
72 }
73 
75 {
76  expr.visit_pre([&state, this](const exprt &e) {
77  if(is_ssa_expr(e))
78  {
79  const ssa_exprt &ssa_expr = to_ssa_expr(e);
80  const irep_idt &obj_identifier = ssa_expr.get_object_name();
81 
82  if(obj_identifier != statet::guard_identifier())
83  {
84  const symbolt &symbol = ns.lookup(obj_identifier);
85 
86  if(symbol.base_name.starts_with("symex::auto_object"))
87  {
88  // done already?
89  if(!state.get_level2().current_names.has_key(
90  ssa_expr.get_identifier()))
91  {
92  initialize_auto_object(e, state);
93  }
94  }
95  }
96  }
97  });
98 }
pointer_typet pointer_type(const typet &subtype)
Definition: c_types.cpp:235
Operator to return the address of an object.
Definition: pointer_expr.h:540
The Boolean type.
Definition: std_types.h:36
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:38
bool starts_with(const char *s) const
equivalent of as_string().starts_with(s)
Definition: dstring.h:95
Base class for all expressions.
Definition: expr.h:56
const source_locationt & source_location() const
Definition: expr.h:231
void visit_pre(std::function< void(exprt &)>)
Definition: expr.cpp:227
typet & type()
Return the type of the expression.
Definition: expr.h:84
const symex_level2t & get_level2() const
Definition: goto_state.h:45
Central data structure: state.
static irep_idt guard_identifier()
symbol_tablet symbol_table
contains symbols that are minted during symbolic execution, such as dynamically created objects etc.
symex_targett::sourcet source
void initialize_auto_object(const exprt &, statet &)
namespacet ns
Initialized just before symbolic execution begins, to point to both outer_symbol_table and the symbol...
Definition: goto_symex.h:258
void trigger_auto_object(const exprt &, statet &)
void symex_assign(statet &state, const exprt &lhs, const exprt &rhs)
Symbolically execute an ASSIGN instruction or simulate such an execution for a synthetic assignment.
Definition: goto_symex.cpp:38
exprt make_auto_object(const typet &, statet &)
The trinary if-then-else operator.
Definition: std_expr.h:2370
const irep_idt & id() const
Definition: irep.h:384
Extract member of struct or union.
Definition: std_expr.h:2841
const typet & follow(const typet &) const
Resolve type symbol to the type it points to.
Definition: namespace.cpp:49
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See documentation for namespace_baset::lookup().
Definition: namespace.cpp:148
The null pointer constant.
Definition: pointer_expr.h:909
The pointer type These are both 'bitvector_typet' (they have a width) and 'type_with_subtypet' (they ...
Definition: pointer_expr.h:24
const typet & base_type() const
The type of the data what we point to.
Definition: pointer_expr.h:35
bool has_key(const key_type &k) const
Check if key is in map.
Definition: sharing_map.h:377
A side_effect_exprt that returns a non-deterministically chosen value.
Definition: std_code.h:1520
Expression providing an SSA-renamed symbol of expressions.
Definition: ssa_expr.h:17
irep_idt get_object_name() const
Structure type, corresponds to C style structs.
Definition: std_types.h:231
const componentst & components() const
Definition: std_types.h:147
const irep_idt & get_identifier() const
Definition: std_expr.h:160
Symbol table entry.
Definition: symbol.h:28
irep_idt base_name
Base (non-scoped) name.
Definition: symbol.h:46
bool is_file_local
Definition: symbol.h:73
bool is_thread_local
Definition: symbol.h:71
class symbol_exprt symbol_expr() const
Produces a symbol_exprt for a symbol.
Definition: symbol.cpp:121
The type of an expression, extends irept.
Definition: type.h:29
symbolt & get_fresh_aux_symbol(const typet &type, const std::string &name_prefix, const std::string &basename_prefix, const source_locationt &source_location, const irep_idt &symbol_mode, const namespacet &ns, symbol_table_baset &symbol_table)
Installs a fresh-named symbol with respect to the given namespace ns with the requested name pattern ...
Fresh auxiliary symbol creation.
Symbolic Execution.
API to expression classes for Pointers.
const pointer_typet & to_pointer_type(const typet &type)
Cast a typet to a pointer_typet.
Definition: pointer_expr.h:93
const ssa_exprt & to_ssa_expr(const exprt &expr)
Cast a generic exprt to an ssa_exprt.
Definition: ssa_expr.h:145
bool is_ssa_expr(const exprt &expr)
Definition: ssa_expr.h:125
API to expression classes.
const struct_typet & to_struct_type(const typet &type)
Cast a typet to a struct_typet.
Definition: std_types.h:308
symex_renaming_levelt current_names
goto_programt::const_targett pc
Definition: symex_target.h:42