cprover
string_abstraction.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: String Abstraction
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
11 
12 #ifndef CPROVER_GOTO_PROGRAMS_STRING_ABSTRACTION_H
13 #define CPROVER_GOTO_PROGRAMS_STRING_ABSTRACTION_H
14 
15 #include <util/bitvector_types.h>
16 #include <util/config.h>
17 #include <util/deprecate.h>
18 #include <util/std_expr.h>
19 #include <util/symbol_table.h>
20 
21 #include "goto_model.h"
22 
23 class message_handlert;
24 
31 {
32 public:
33  // To be deprecated once the operator() methods have been removed, at which
34  // point a new constructor that only takes a message_handler should be
35  // introduced.
37  symbol_tablet &_symbol_table,
38  message_handlert &_message_handler);
39 
40  DEPRECATED(SINCE(2020, 12, 14, "Use apply(goto_modelt &)"))
41  void operator()(goto_programt &dest);
42  DEPRECATED(SINCE(2020, 12, 14, "Use apply(goto_modelt &)"))
43  void operator()(goto_functionst &dest);
44 
48  void apply(goto_modelt &goto_model);
49 
50 protected:
51  std::string sym_suffix;
56 
57  typedef ::std::map< typet, typet > abstraction_types_mapt;
59 
60  static bool has_string_macros(const exprt &expr);
61 
63  exprt &expr,
64  bool lhs,
65  const source_locationt &);
66 
67  void move_lhs_arithmetic(exprt &lhs, exprt &rhs);
68 
69  bool is_char_type(const typet &type) const
70  {
71  if(type.id()!=ID_signedbv &&
72  type.id()!=ID_unsignedbv)
73  return false;
74 
76  }
77 
78  bool is_ptr_string_struct(const typet &type) const;
79 
80  void make_type(exprt &dest, const typet &type)
81  {
82  if(dest.is_not_nil() &&
83  ns.follow(dest.type())!=ns.follow(type))
84  dest = typecast_exprt(dest, type);
85  }
86 
87  goto_programt::targett abstract(
95 
97  goto_programt &dest,
99  const exprt &new_lhs,
100  const exprt &lhs,
101  const exprt &rhs);
102 
104 
107  const exprt &lhs, const exprt &rhs);
108 
110  goto_programt &dest,
111  goto_programt::targett target,
112  const exprt &lhs, const if_exprt &rhs);
113 
115  goto_programt &dest,
116  goto_programt::targett target,
117  const exprt &lhs, const exprt &rhs);
118 
119  enum class whatt { IS_ZERO, LENGTH, SIZE };
120 
121  static typet build_type(whatt what);
122  exprt build(
123  const exprt &pointer,
124  whatt what,
125  bool write,
126  const source_locationt &);
127 
128  bool build(const exprt &object, exprt &dest, bool write);
129  bool build_wrap(const exprt &object, exprt &dest, bool write);
130  bool build_if(const if_exprt &o_if, exprt &dest, bool write);
131  bool build_array(const array_exprt &object, exprt &dest, bool write);
132  bool build_symbol(const symbol_exprt &sym, exprt &dest);
133  bool build_symbol_constant(const mp_integer &zero_length,
134  const mp_integer &buf_size, exprt &dest);
135 
136  exprt build_unknown(whatt what, bool write);
137  exprt build_unknown(const typet &type, bool write);
138  const typet &build_abstraction_type(const typet &type);
139  const typet &build_abstraction_type_rec(const typet &type,
140  const abstraction_types_mapt &known);
141  bool build_pointer(const exprt &object, exprt &dest, bool write);
142  void build_new_symbol(const symbolt &symbol,
143  const irep_idt &identifier, const typet &type);
144 
145  exprt member(const exprt &a, whatt what);
146 
149 
150  typedef std::unordered_map<irep_idt, irep_idt> localst;
153 
154  void abstract(goto_programt &dest);
155 
156  void add_str_parameters(
157  symbolt &fct_symbol,
158  goto_functiont::parameter_identifierst &parameter_identifiers);
159 
161  const symbolt &fct_symbol,
162  const typet &type,
163  const irep_idt &identifier);
164 
166  const irep_idt &identifier, const irep_idt &source_sym);
167 
169  goto_programt::targett ref_instr,
170  const symbolt &symbol, const typet &source_type);
171 
173  goto_programt &dest,
174  goto_programt::targett ref_instr,
175  const symbolt &symbol,
176  const irep_idt &component_name,
177  const typet &type,
178  const typet &source_type);
179 
181 };
182 
183 // keep track of length of strings
184 
185 void string_abstraction(
186  goto_modelt &,
187  message_handlert &);
188 
189 DEPRECATED(
190  SINCE(2020, 12, 14, "Use string_abstraction(goto_model, message_handler)"))
191 void string_abstraction(
192  symbol_tablet &,
194  goto_functionst &);
195 
196 #endif // CPROVER_GOTO_PROGRAMS_STRING_ABSTRACTION_H
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:37
string_abstractiont::string_abstractiont
string_abstractiont(symbol_tablet &_symbol_table, message_handlert &_message_handler)
Definition: string_abstraction.cpp:86
symbol_tablet
The symbol table.
Definition: symbol_table.h:20
string_abstractiont::member
exprt member(const exprt &a, whatt what)
Definition: string_abstraction.cpp:1366
string_abstractiont::build_type
static typet build_type(whatt what)
Definition: string_abstraction.cpp:105
string_abstractiont::build
exprt build(const exprt &pointer, whatt what, bool write, const source_locationt &)
Definition: string_abstraction.cpp:643
string_abstractiont::abstract_assign
goto_programt::targett abstract_assign(goto_programt &dest, goto_programt::targett it)
Definition: string_abstraction.cpp:515
string_abstraction
void string_abstraction(goto_modelt &, message_handlert &)
Definition: string_abstraction.cpp:78
typet
The type of an expression, extends irept.
Definition: type.h:28
string_abstractiont::build_pointer
bool build_pointer(const exprt &object, exprt &dest, bool write)
Definition: string_abstraction.cpp:885
string_abstractiont::abstraction_types_map
abstraction_types_mapt abstraction_types_map
Definition: string_abstraction.h:58
mp_integer
BigInt mp_integer
Definition: mp_arith.h:19
if_exprt
The trinary if-then-else operator.
Definition: std_expr.h:2086
string_abstractiont::string_struct
typet string_struct
Definition: string_abstraction.h:147
string_abstractiont::whatt::LENGTH
@ LENGTH
string_abstractiont::locals
localst locals
Definition: string_abstraction.h:151
goto_model.h
Symbol Table + CFG.
exprt
Base class for all expressions.
Definition: expr.h:54
goto_modelt
Definition: goto_model.h:26
string_abstractiont::build_abstraction_type_rec
const typet & build_abstraction_type_rec(const typet &type, const abstraction_types_mapt &known)
Definition: string_abstraction.cpp:699
configt::ansi_c
struct configt::ansi_ct ansi_c
to_bitvector_type
const bitvector_typet & to_bitvector_type(const typet &type)
Cast a typet to a bitvector_typet.
Definition: bitvector_types.h:32
symbol_exprt
Expression to hold a symbol (variable)
Definition: std_expr.h:80
string_abstractiont::is_ptr_string_struct
bool is_ptr_string_struct(const typet &type) const
Definition: string_abstraction.cpp:59
string_abstractiont::add_parameter
code_typet::parametert add_parameter(const symbolt &fct_symbol, const typet &type, const irep_idt &identifier)
Definition: string_abstraction.cpp:213
string_abstractiont::abstract_function_call
void abstract_function_call(goto_programt::targett it)
Definition: string_abstraction.cpp:547
string_abstractiont::abstract_char_assign
goto_programt::targett abstract_char_assign(goto_programt &dest, goto_programt::targett it)
Definition: string_abstraction.cpp:1151
string_abstractiont::value_assignments
goto_programt::targett value_assignments(goto_programt &dest, goto_programt::targett it, const exprt &lhs, const exprt &rhs)
Definition: string_abstraction.cpp:1248
string_abstractiont::make_type
void make_type(exprt &dest, const typet &type)
Definition: string_abstraction.h:80
configt::ansi_ct::char_width
std::size_t char_width
Definition: config.h:114
string_abstractiont::is_char_type
bool is_char_type(const typet &type) const
Definition: string_abstraction.h:69
namespacet
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:92
string_abstractiont::whatt
whatt
Definition: string_abstraction.h:119
string_abstractiont::sym_suffix
std::string sym_suffix
Definition: string_abstraction.h:51
exprt::type
typet & type()
Return the type of the expression.
Definition: expr.h:82
string_abstractiont::abstraction_types_mapt
::std::map< typet, typet > abstraction_types_mapt
Definition: string_abstraction.h:57
irept::is_not_nil
bool is_not_nil() const
Definition: irep.h:391
string_abstractiont::has_string_macros
static bool has_string_macros(const exprt &expr)
Definition: string_abstraction.cpp:598
string_abstractiont::symbol_table
symbol_tablet & symbol_table
Definition: string_abstraction.h:52
string_abstractiont
Replace all uses of char * by a struct that carries that string, and also the underlying allocation a...
Definition: string_abstraction.h:31
string_abstractiont::build_unknown
exprt build_unknown(whatt what, bool write)
Definition: string_abstraction.cpp:917
string_abstractiont::whatt::SIZE
@ SIZE
deprecate.h
string_abstractiont::declare_define_locals
void declare_define_locals(goto_programt &dest)
Definition: string_abstraction.cpp:260
bitvector_types.h
Pre-defined bitvector types.
string_abstractiont::make_val_or_dummy_rec
exprt make_val_or_dummy_rec(goto_programt &dest, goto_programt::targett ref_instr, const symbolt &symbol, const typet &source_type)
Definition: string_abstraction.cpp:326
string_abstractiont::add_dummy_symbol_and_value
symbol_exprt add_dummy_symbol_and_value(goto_programt &dest, goto_programt::targett ref_instr, const symbolt &symbol, const irep_idt &component_name, const typet &type, const typet &source_type)
Definition: string_abstraction.cpp:397
string_abstractiont::parameter_map
localst parameter_map
Definition: string_abstraction.h:152
string_abstractiont::ns
namespacet ns
Definition: string_abstraction.h:53
string_abstractiont::build_new_symbol
void build_new_symbol(const symbolt &symbol, const irep_idt &identifier, const typet &type)
Definition: string_abstraction.cpp:1008
string_abstractiont::build_abstraction_type
const typet & build_abstraction_type(const typet &type)
Definition: string_abstraction.cpp:680
irept::id
const irep_idt & id() const
Definition: irep.h:407
message_handlert
Definition: message.h:28
string_abstractiont::char_assign
goto_programt::targett char_assign(goto_programt &dest, goto_programt::targett target, const exprt &new_lhs, const exprt &lhs, const exprt &rhs)
Definition: string_abstraction.cpp:1216
SINCE
#define SINCE(year, month, day, msg)
Definition: deprecate.h:26
string_abstractiont::value_assignments_if
goto_programt::targett value_assignments_if(goto_programt &dest, goto_programt::targett target, const exprt &lhs, const if_exprt &rhs)
Definition: string_abstraction.cpp:1295
string_abstractiont::initialization
goto_programt initialization
Definition: string_abstraction.h:148
string_abstractiont::build_wrap
bool build_wrap(const exprt &object, exprt &dest, bool write)
Definition: string_abstraction.cpp:28
string_abstractiont::replace_string_macros
void replace_string_macros(exprt &expr, bool lhs, const source_locationt &)
Definition: string_abstraction.cpp:612
config
configt config
Definition: config.cpp:24
source_locationt
Definition: source_location.h:20
bitvector_typet::get_width
std::size_t get_width() const
Definition: std_types.h:837
string_abstractiont::add_str_parameters
void add_str_parameters(symbolt &fct_symbol, goto_functiont::parameter_identifierst &parameter_identifiers)
Definition: string_abstraction.cpp:186
goto_functionst
A collection of goto functions.
Definition: goto_functions.h:23
string_abstractiont::abstract_pointer_assign
goto_programt::targett abstract_pointer_assign(goto_programt &dest, goto_programt::targett it)
Definition: string_abstraction.cpp:1109
DEPRECATED
#define DEPRECATED(msg)
Definition: deprecate.h:23
namespace_baset::follow
const typet & follow(const typet &) const
Resolve type symbol to the type it points to.
Definition: namespace.cpp:52
string_abstractiont::temporary_counter
unsigned temporary_counter
Definition: string_abstraction.h:54
symbolt
Symbol table entry.
Definition: symbol.h:28
string_abstractiont::build_symbol_constant
bool build_symbol_constant(const mp_integer &zero_length, const mp_integer &buf_size, exprt &dest)
Definition: string_abstraction.cpp:1040
string_abstractiont::make_decl_and_def
void make_decl_and_def(goto_programt &dest, goto_programt::targett ref_instr, const irep_idt &identifier, const irep_idt &source_sym)
Definition: string_abstraction.cpp:295
code_typet::parametert
Definition: std_types.h:550
goto_programt
A generic container class for the GOTO intermediate representation of one function.
Definition: goto_program.h:74
string_abstractiont::whatt::IS_ZERO
@ IS_ZERO
config.h
goto_functiont::parameter_identifierst
std::vector< irep_idt > parameter_identifierst
Definition: goto_function.h:31
string_abstractiont::build_array
bool build_array(const array_exprt &object, exprt &dest, bool write)
Definition: string_abstraction.cpp:859
string_abstractiont::move_lhs_arithmetic
void move_lhs_arithmetic(exprt &lhs, exprt &rhs)
Definition: string_abstraction.cpp:1083
symbol_table.h
Author: Diffblue Ltd.
typecast_exprt
Semantic type conversion.
Definition: std_expr.h:1780
string_abstractiont::build_if
bool build_if(const if_exprt &o_if, exprt &dest, bool write)
Definition: string_abstraction.cpp:831
string_abstractiont::localst
std::unordered_map< irep_idt, irep_idt > localst
Definition: string_abstraction.h:150
std_expr.h
API to expression classes.
string_abstractiont::message_handler
message_handlert & message_handler
Definition: string_abstraction.h:55
string_abstractiont::operator()
void operator()(goto_programt &dest)
Definition: string_abstraction.cpp:176
array_exprt
Array constructor from list of elements.
Definition: std_expr.h:1381
string_abstractiont::build_symbol
bool build_symbol(const symbol_exprt &sym, exprt &dest)
Definition: string_abstraction.cpp:968
goto_programt::targett
instructionst::iterator targett
Definition: goto_program.h:563
string_abstractiont::value_assignments_string_struct
goto_programt::targett value_assignments_string_struct(goto_programt &dest, goto_programt::targett target, const exprt &lhs, const exprt &rhs)
Definition: string_abstraction.cpp:1326
string_abstractiont::apply
void apply(goto_modelt &goto_model)
Apply string abstraction to goto_model.
Definition: string_abstraction.cpp:121