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 "goto_symex.h"
13 
14 #include <util/pointer_expr.h>
15 #include <util/prefix.h>
16 #include <util/std_code.h>
17 #include <util/std_expr.h>
18 #include <util/symbol_table.h>
19 
21 {
23 
24  // produce auto-object symbol
25  symbolt symbol;
26 
27  symbol.base_name="auto_object"+std::to_string(dynamic_counter);
28  symbol.name="symex::"+id2string(symbol.base_name);
29  symbol.is_lvalue=true;
30  symbol.type=type;
31  symbol.mode=ID_C;
32 
33  state.symbol_table.add(symbol);
34 
35  return symbol_exprt(symbol.name, symbol.type);
36 }
37 
39 {
40  const typet &type=ns.follow(expr.type());
41 
42  if(type.id()==ID_struct)
43  {
44  const struct_typet &struct_type=to_struct_type(type);
45 
46  for(const auto &comp : struct_type.components())
47  {
48  member_exprt member_expr(expr, comp.get_name(), comp.type());
49 
50  initialize_auto_object(member_expr, state);
51  }
52  }
53  else if(type.id()==ID_pointer)
54  {
56  const typet &base_type = pointer_type.base_type();
57 
58  // we don't like function pointers and
59  // we don't like void *
60  if(base_type.id() != ID_code && base_type.id() != ID_empty)
61  {
62  // could be NULL nondeterministically
63 
64  address_of_exprt address_of_expr(
65  make_auto_object(base_type, state), pointer_type);
66 
67  if_exprt rhs(
70  address_of_expr);
71 
72  symex_assign(state, expr, rhs);
73  }
74  }
75 }
76 
78 {
79  expr.visit_pre([&state, this](const exprt &e) {
80  if(is_ssa_expr(e))
81  {
82  const ssa_exprt &ssa_expr = to_ssa_expr(e);
83  const irep_idt &obj_identifier = ssa_expr.get_object_name();
84 
85  if(obj_identifier != statet::guard_identifier())
86  {
87  const symbolt &symbol = ns.lookup(obj_identifier);
88 
89  if(has_prefix(id2string(symbol.base_name), "auto_object"))
90  {
91  // done already?
92  if(!state.get_level2().current_names.has_key(
93  ssa_expr.get_identifier()))
94  {
95  initialize_auto_object(e, state);
96  }
97  }
98  }
99  }
100  });
101 }
struct_union_typet::components
const componentst & components() const
Definition: std_types.h:147
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:36
to_struct_type
const struct_typet & to_struct_type(const typet &type)
Cast a typet to a struct_typet.
Definition: std_types.h:308
goto_symext::dynamic_counter
static unsigned dynamic_counter
A monotonically increasing index for each created dynamic object.
Definition: goto_symex.h:780
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
symbolt::type
typet type
Type of symbol.
Definition: symbol.h:31
goto_symex_statet
Central data structure: state.
Definition: goto_symex_state.h:41
if_exprt
The trinary if-then-else operator.
Definition: std_expr.h:2322
prefix.h
exprt
Base class for all expressions.
Definition: expr.h:55
ssa_exprt::get_object_name
irep_idt get_object_name() const
symbolt::base_name
irep_idt base_name
Base (non-scoped) name.
Definition: symbol.h:46
bool_typet
The Boolean type.
Definition: std_types.h:35
to_string
std::string to_string(const string_not_contains_constraintt &expr)
Used for debug printing.
Definition: string_constraint.cpp:58
goto_symext::trigger_auto_object
void trigger_auto_object(const exprt &, statet &)
Definition: auto_objects.cpp:77
symbol_exprt
Expression to hold a symbol (variable)
Definition: std_expr.h:112
ssa_exprt
Expression providing an SSA-renamed symbol of expressions.
Definition: ssa_expr.h:16
exprt::type
typet & type()
Return the type of the expression.
Definition: expr.h:84
namespacet::lookup
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See documentation for namespace_baset::lookup().
Definition: namespace.cpp:138
is_ssa_expr
bool is_ssa_expr(const exprt &expr)
Definition: ssa_expr.h:125
symbolt::mode
irep_idt mode
Language mode.
Definition: symbol.h:49
has_prefix
bool has_prefix(const std::string &s, const std::string &prefix)
Definition: converter.cpp:13
to_ssa_expr
const ssa_exprt & to_ssa_expr(const exprt &expr)
Cast a generic exprt to an ssa_exprt.
Definition: ssa_expr.h:145
null_pointer_exprt
The null pointer constant.
Definition: pointer_expr.h:734
id2string
const std::string & id2string(const irep_idt &d)
Definition: irep.h:47
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_symext::initialize_auto_object
void initialize_auto_object(const exprt &, statet &)
Definition: auto_objects.cpp:38
symbol_exprt::get_identifier
const irep_idt & get_identifier() const
Definition: std_expr.h:142
goto_symex.h
pointer_expr.h
to_pointer_type
const pointer_typet & to_pointer_type(const typet &type)
Cast a typet to a pointer_typet.
Definition: pointer_expr.h:79
goto_symext::make_auto_object
exprt make_auto_object(const typet &, statet &)
Definition: auto_objects.cpp:20
pointer_type
pointer_typet pointer_type(const typet &subtype)
Definition: c_types.cpp:253
irept::id
const irep_idt & id() const
Definition: irep.h:396
std_code.h
sharing_mapt::has_key
bool has_key(const key_type &k) const
Check if key is in map.
Definition: sharing_map.h:377
side_effect_expr_nondett
A side_effect_exprt that returns a non-deterministically chosen value.
Definition: std_code.h:1519
goto_symext::ns
namespacet ns
Initialized just before symbolic execution begins, to point to both outer_symbol_table and the symbol...
Definition: goto_symex.h:243
symbol_table_baset::add
bool add(const symbolt &symbol)
Add a new symbol to the symbol table.
Definition: symbol_table_base.cpp:18
member_exprt
Extract member of struct or union.
Definition: std_expr.h:2793
struct_typet
Structure type, corresponds to C style structs.
Definition: std_types.h:230
symex_level2t::current_names
symex_renaming_levelt current_names
Definition: renaming_level.h:70
namespace_baset::follow
const typet & follow(const typet &) const
Resolve type symbol to the type it points to.
Definition: namespace.cpp:49
symbolt
Symbol table entry.
Definition: symbol.h:27
goto_statet::get_level2
const symex_level2t & get_level2() const
Definition: goto_state.h:45
pointer_typet::base_type
const typet & base_type() const
The type of the data what we point to.
Definition: pointer_expr.h:35
exprt::visit_pre
void visit_pre(std::function< void(exprt &)>)
Definition: expr.cpp:245
symbolt::is_lvalue
bool is_lvalue
Definition: symbol.h:66
address_of_exprt
Operator to return the address of an object.
Definition: pointer_expr.h:370
symbol_table.h
Author: Diffblue Ltd.
pointer_typet
The pointer type These are both 'bitvector_typet' (they have a width) and 'type_with_subtypet' (they ...
Definition: pointer_expr.h:23
std_expr.h
goto_symext::symex_assign
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:40
exprt::source_location
const source_locationt & source_location() const
Definition: expr.h:211
symbolt::name
irep_idt name
The unique identifier.
Definition: symbol.h:40