cprover
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/prefix.h>
15 #include <util/cprover_prefix.h>
16 #include <util/symbol_table.h>
17 #include <util/std_expr.h>
18 
20 {
22 
23  // produce auto-object symbol
24  symbolt symbol;
25 
26  symbol.base_name="auto_object"+std::to_string(dynamic_counter);
27  symbol.name="symex::"+id2string(symbol.base_name);
28  symbol.is_lvalue=true;
29  symbol.type=type;
30  symbol.mode=ID_C;
31 
32  state.symbol_table.add(symbol);
33 
34  return symbol_exprt(symbol.name, symbol.type);
35 }
36 
38 {
39  const typet &type=ns.follow(expr.type());
40 
41  if(type.id()==ID_struct)
42  {
43  const struct_typet &struct_type=to_struct_type(type);
44 
45  for(const auto &comp : struct_type.components())
46  {
47  member_exprt member_expr(expr, comp.get_name(), comp.type());
48 
49  initialize_auto_object(member_expr, state);
50  }
51  }
52  else if(type.id()==ID_pointer)
53  {
55  const typet &subtype = pointer_type.subtype();
56 
57  // we don't like function pointers and
58  // we don't like void *
59  if(subtype.id()!=ID_code &&
60  subtype.id()!=ID_empty)
61  {
62  // could be NULL nondeterministically
63 
64  address_of_exprt address_of_expr(
66 
67  if_exprt rhs(
70  address_of_expr);
71 
72  code_assignt assignment(expr, rhs);
73  symex_assign(state, assignment);
74  }
75  }
76 }
77 
79 {
80  expr.visit_pre([&state, this](const exprt &e) {
81  if(e.id() == ID_symbol && e.get_bool(ID_C_SSA_symbol))
82  {
83  const ssa_exprt &ssa_expr = to_ssa_expr(e);
84  const irep_idt &obj_identifier = ssa_expr.get_object_name();
85 
86  if(obj_identifier != statet::guard_identifier())
87  {
88  const symbolt &symbol = ns.lookup(obj_identifier);
89 
90  if(has_prefix(id2string(symbol.base_name), "auto_object"))
91  {
92  // done already?
93  if(!state.get_level2().current_names.has_key(
94  ssa_expr.get_identifier()))
95  {
96  initialize_auto_object(e, state);
97  }
98  }
99  }
100  }
101  });
102 }
struct_union_typet::components
const componentst & components() const
Definition: std_types.h:142
typet::subtype
const typet & subtype() const
Definition: type.h:47
to_struct_type
const struct_typet & to_struct_type(const typet &type)
Cast a typet to a struct_typet.
Definition: std_types.h:303
goto_symext::dynamic_counter
static unsigned dynamic_counter
A monotonically increasing index for each created dynamic object.
Definition: goto_symex.h:788
typet
The type of an expression, extends irept.
Definition: type.h:29
symbolt::type
typet type
Type of symbol.
Definition: symbol.h:31
goto_symex_statet
Central data structure: state.
Definition: goto_symex_state.h:46
if_exprt
The trinary if-then-else operator.
Definition: std_expr.h:2964
prefix.h
exprt
Base class for all expressions.
Definition: expr.h:53
symbolt::base_name
irep_idt base_name
Base (non-scoped) name.
Definition: symbol.h:46
bool_typet
The Boolean type.
Definition: std_types.h:37
to_string
std::string to_string(const string_not_contains_constraintt &expr)
Used for debug printing.
Definition: string_constraint.cpp:55
goto_symext::trigger_auto_object
void trigger_auto_object(const exprt &, statet &)
Definition: auto_objects.cpp:78
symbol_exprt
Expression to hold a symbol (variable)
Definition: std_expr.h:82
goto_symext::symex_assign
void symex_assign(statet &state, const code_assignt &code)
Symbolically execute an ASSIGN instruction or simulate such an execution for a synthetic assignment.
Definition: goto_symex.cpp:38
exprt::type
typet & type()
Return the type of the expression.
Definition: expr.h:81
irept::get_bool
bool get_bool(const irep_namet &name) const
Definition: irep.cpp:64
symbolt::mode
irep_idt mode
Language mode.
Definition: symbol.h:49
null_pointer_exprt
The null pointer constant.
Definition: std_expr.h:3989
id2string
const std::string & id2string(const irep_idt &d)
Definition: irep.h:44
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:69
goto_symext::initialize_auto_object
void initialize_auto_object(const exprt &, statet &)
Definition: auto_objects.cpp:37
goto_symex.h
Symbolic Execution.
goto_symext::make_auto_object
exprt make_auto_object(const typet &, statet &)
Definition: auto_objects.cpp:19
pointer_type
pointer_typet pointer_type(const typet &subtype)
Definition: c_types.cpp:243
irept::id
const irep_idt & id() const
Definition: irep.h:418
to_pointer_type
const pointer_typet & to_pointer_type(const typet &type)
Cast a typet to a pointer_typet.
Definition: std_types.h:1526
cprover_prefix.h
side_effect_expr_nondett
A side_effect_exprt that returns a non-deterministically chosen value.
Definition: std_code.h:1945
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:256
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:3405
struct_typet
Structure type, corresponds to C style structs.
Definition: std_types.h:226
namespace_baset::follow
const typet & follow(const typet &) const
Resolve type symbol to the type it points to.
Definition: namespace.cpp:51
symbolt
Symbol table entry.
Definition: symbol.h:28
exprt::visit_pre
void visit_pre(std::function< void(exprt &)>)
Definition: expr.cpp:311
symbolt::is_lvalue
bool is_lvalue
Definition: symbol.h:66
address_of_exprt
Operator to return the address of an object.
Definition: std_expr.h:2786
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: std_types.h:1488
code_assignt
A codet representing an assignment in the program.
Definition: std_code.h:295
std_expr.h
API to expression classes.
exprt::source_location
const source_locationt & source_location() const
Definition: expr.h:254
symbolt::name
irep_idt name
The unique identifier.
Definition: symbol.h:40