CBMC
java_string_library_preprocess.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Produce code for simple implementation of String Java libraries
4 
5 Author: Romain Brenguier
6 
7 Date: March 2017
8 
9 \*******************************************************************/
10 
13 
14 #ifndef CPROVER_JAVA_BYTECODE_JAVA_STRING_LIBRARY_PREPROCESS_H
15 #define CPROVER_JAVA_BYTECODE_JAVA_STRING_LIBRARY_PREPROCESS_H
16 
18 #include <util/std_code.h>
19 #include <util/string_expr.h>
20 
22 
24 #include "java_types.h"
25 
26 #include <array>
27 #include <functional>
28 #include <unordered_set>
29 
30 class message_handlert;
31 class symbol_table_baset;
32 class symbolt;
33 
34 // Arbitrary limit of 10 arguments for the number of arguments to String.format
35 #define MAX_FORMAT_ARGS 10
36 
38 {
39 public:
44  {
45  }
46 
49 
50  bool implements_function(const irep_idt &function_id) const;
51  void get_all_function_names(std::unordered_set<irep_idt> &methods) const;
52 
54  const symbolt &symbol,
55  symbol_table_baset &symbol_table,
56  message_handlert &message_handler);
57 
59  {
61  }
62  std::vector<irep_idt> get_string_type_base_classes(
63  const irep_idt &class_name);
64  void
65  add_string_type(const irep_idt &class_name, symbol_table_baset &symbol_table);
66  bool is_known_string_type(irep_idt class_name);
67 
69  {
74  }
75  static bool implements_java_char_sequence(const typet &type)
76  {
77  return is_java_char_sequence_type(type)
80  || is_java_string_type(type);
81  }
82 
83 private:
84  // We forbid copies of the object
86  const java_string_library_preprocesst &)=delete;
87 
88  static bool java_type_matches_tag(const typet &type, const std::string &tag);
89  static bool is_java_string_pointer_type(const typet &type);
90  static bool is_java_string_type(const typet &type);
91  static bool is_java_string_builder_type(const typet &type);
92  static bool is_java_string_builder_pointer_type(const typet &type);
93  static bool is_java_string_buffer_type(const typet &type);
94  static bool is_java_string_buffer_pointer_type(const typet &type);
95  static bool is_java_char_sequence_type(const typet &type);
96  static bool is_java_char_sequence_pointer_type(const typet &type);
97  static bool is_java_char_array_type(const typet &type);
98  static bool is_java_char_array_pointer_type(const typet &type);
99 
101 
105 
106  typedef std::function<codet(
107  const java_method_typet &,
108  const source_locationt &,
109  const irep_idt &,
111  message_handlert &)>
113 
114  typedef std::unordered_map<irep_idt, irep_idt> id_mapt;
115 
116  // Table mapping each java method signature to the code generating function
117  std::unordered_map<irep_idt, conversion_functiont> conversion_table;
118 
119  // Some Java functions have an equivalent in the solver that we will
120  // call with the same argument and will return the same result
122 
123  // Some Java functions have an equivalent except that they should
124  // return Java Strings instead of string_exprt
126 
127  // Some Java initialization function initialize strings with the
128  // same result as some function of the solver
130 
131  // Some Java functions have an equivalent in the solver except that
132  // in addition they assign the result to the object on which it is called
134 
135  // Some Java functions have an equivalent in the solver except that
136  // they assign the result to the object on which it is called instead
137  // of returning it
139 
140  const std::array<id_mapt *, 5> id_maps = std::array<id_mapt *, 5>
141  {
142  {
148  }
149  };
150 
151  // A set tells us what java types should be considered as string objects
152  std::unordered_set<irep_idt> string_types;
153 
155  const java_method_typet &type,
156  const source_locationt &loc,
157  const irep_idt &function_id,
158  symbol_table_baset &symbol_table,
159  message_handlert &message_handler);
160 
162  const java_method_typet &type,
163  const source_locationt &loc,
164  const irep_idt &function_id,
165  symbol_table_baset &symbol_table,
166  message_handlert &message_handler);
167 
169  const java_method_typet &type,
170  const source_locationt &loc,
171  const irep_idt &function_id,
172  symbol_table_baset &symbol_table,
173  message_handlert &message_handler);
174 
176  const java_method_typet &type,
177  const source_locationt &loc,
178  const irep_idt &function_id,
179  symbol_table_baset &symbol_table,
180  message_handlert &message_handler);
181 
183  const java_method_typet &type,
184  const source_locationt &loc,
185  const irep_idt &function_id,
186  symbol_table_baset &symbol_table,
187  message_handlert &message_handler);
188 
189  // Helper functions
191  const java_method_typet::parameterst &params,
192  const source_locationt &loc,
193  const irep_idt &function_name,
194  symbol_table_baset &symbol_table,
195  code_blockt &init_code);
196 
197  // Friending this function for unit testing convert_exprt_to_string_exprt
200  const exprt &deref,
201  const source_locationt &loc,
202  const irep_idt &function_id,
203  symbol_table_baset &symbol_table,
204  code_blockt &init_code);
205 
207  const exprt &deref,
208  const source_locationt &loc,
209  symbol_table_baset &symbol_table,
210  const irep_idt &function_name,
211  code_blockt &init_code);
212 
214  const exprt::operandst &operands,
215  const source_locationt &loc,
216  const irep_idt &function_name,
217  symbol_table_baset &symbol_table,
218  code_blockt &init_code);
219 
221  const exprt &array_pointer,
222  const source_locationt &loc,
223  const irep_idt &function_name,
224  symbol_table_baset &symbol_table,
225  code_blockt &code);
226 
228  const typet &type,
229  const source_locationt &loc,
230  const irep_idt &function_id,
231  symbol_table_baset &symbol_table);
232 
234  const source_locationt &loc,
235  const irep_idt &function_id,
236  symbol_table_baset &symbol_table,
237  code_blockt &code);
238 
240  const source_locationt &loc,
241  const irep_idt &function_id,
242  symbol_table_baset &symbol_table,
243  code_blockt &code);
244 
246  const typet &type,
247  const source_locationt &loc,
248  const irep_idt &function_id,
249  symbol_table_baset &symbol_table,
250  code_blockt &code);
251 
253  const irep_idt &function_id,
254  const exprt::operandst &arguments,
255  const typet &type,
256  symbol_table_baset &symbol_table);
257 
259  const irep_idt &function_id,
260  const exprt::operandst &arguments,
261  const source_locationt &loc,
262  symbol_table_baset &symbol_table,
263  code_blockt &code);
264 
266  const exprt &lhs,
267  const exprt &rhs_array,
268  const exprt &rhs_length,
269  const symbol_table_baset &symbol_table,
270  bool is_constructor);
271 
273  const exprt &lhs,
274  const refined_string_exprt &rhs,
275  const symbol_table_baset &symbol_table,
276  bool is_constructor);
277 
279  const refined_string_exprt &lhs,
280  const exprt &rhs,
281  const source_locationt &loc,
282  const symbol_table_baset &symbol_table,
283  code_blockt &code);
284 
286  const std::string &s,
287  const source_locationt &loc,
288  symbol_table_baset &symbol_table,
289  code_blockt &code);
290 
292  const irep_idt &function_id,
293  const java_method_typet &type,
294  const source_locationt &loc,
295  symbol_table_baset &symbol_table);
296 
298  const irep_idt &function_id,
299  const java_method_typet &type,
300  const source_locationt &loc,
301  symbol_table_baset &symbol_table,
302  bool is_constructor = true);
303 
305  const irep_idt &function_id,
306  const java_method_typet &type,
307  const source_locationt &loc,
308  symbol_table_baset &symbol_table);
309 
311  const irep_idt &function_id,
312  const java_method_typet &type,
313  const source_locationt &loc,
314  symbol_table_baset &symbol_table);
315 
317  const irep_idt &function_id,
318  const java_method_typet &type,
319  const source_locationt &loc,
320  symbol_table_baset &symbol_table);
321 };
322 
324  symbol_table_baset &symbol_table,
325  const source_locationt &loc,
326  const irep_idt &function_id,
327  code_blockt &code);
328 
330  const exprt &pointer,
331  const exprt &array,
332  symbol_table_baset &symbol_table,
333  const source_locationt &loc,
334  const irep_idt &function_id,
335  code_blockt &code);
336 
338  const exprt &array,
339  const exprt &length,
340  symbol_table_baset &symbol_table,
341  const source_locationt &loc,
342  const irep_idt &function_id,
343  code_blockt &code);
344 
346  const exprt &pointer,
347  const exprt &length,
348  const irep_idt &char_range,
349  symbol_table_baset &symbol_table,
350  const source_locationt &loc,
351  const irep_idt &function_id,
352  code_blockt &code);
353 
354 #endif // CPROVER_JAVA_BYTECODE_JAVA_STRING_LIBRARY_PREPROCESS_H
pointer_typet pointer_type(const typet &subtype)
Definition: c_types.cpp:235
static std::string preprocess(const std::string &source_file, const c_wranglert &c_wrangler)
Definition: c_wrangler.cpp:321
Preprocess a goto-programs so that calls to the java Character library are replaced by simple express...
codet replace_character_call(const code_function_callt &call) const
replace function calls to functions of the Character by an affectation if possible,...
A codet representing sequential composition of program statements.
Definition: std_code.h:130
goto_instruction_codet representation of a function call statement.
goto_instruction_codet representation of a "return from a function" statement.
std::vector< parametert > parameterst
Definition: std_types.h:585
Data structure for representing an arbitrary statement in a program.
Definition: std_code_base.h:29
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:38
Base class for all expressions.
Definition: expr.h:56
std::vector< exprt > operandst
Definition: expr.h:58
code_blockt make_float_to_string_code(const java_method_typet &type, const source_locationt &loc, const irep_idt &function_id, symbol_table_baset &symbol_table, message_handlert &message_handler)
Provide code for the String.valueOf(F) function.
std::unordered_map< irep_idt, irep_idt > id_mapt
code_blockt make_string_returning_function_from_call(const irep_idt &function_id, const java_method_typet &type, const source_locationt &loc, symbol_table_baset &symbol_table)
Provide code for a function that calls a function from the solver and return the string_expr result a...
code_returnt make_string_length_code(const java_method_typet &type, const source_locationt &loc, const irep_idt &function_id, symbol_table_baset &symbol_table, message_handlert &message_handler)
Generates code for the String.length method.
refined_string_exprt string_literal_to_string_expr(const std::string &s, const source_locationt &loc, symbol_table_baset &symbol_table, code_blockt &code)
Create a string expression whose value is given by a literal.
codet code_assign_components_to_java_string(const exprt &lhs, const exprt &rhs_array, const exprt &rhs_length, const symbol_table_baset &symbol_table, bool is_constructor)
Produce code for an assignment of a string expr to a Java string.
refined_string_exprt convert_exprt_to_string_exprt(const exprt &deref, const source_locationt &loc, symbol_table_baset &symbol_table, const irep_idt &function_name, code_blockt &init_code)
Creates a string_exprt from the input exprt representing a char sequence.
java_string_library_preprocesst(const java_string_library_preprocesst &)=delete
code_blockt make_assign_and_return_function_from_call(const irep_idt &function_id, const java_method_typet &type, const source_locationt &loc, symbol_table_baset &symbol_table)
Call a cprover internal function, assign the result to object this and return it.
void get_all_function_names(std::unordered_set< irep_idt > &methods) const
static bool implements_java_char_sequence_pointer(const typet &type)
std::function< codet(const java_method_typet &, const source_locationt &, const irep_idt &, symbol_table_baset &, message_handlert &)> conversion_functiont
std::unordered_map< irep_idt, conversion_functiont > conversion_table
refined_string_exprt string_expr_of_function(const irep_idt &function_id, const exprt::operandst &arguments, const source_locationt &loc, symbol_table_baset &symbol_table, code_blockt &code)
Create a refined_string_exprt str whose content and length are fresh symbols, calls the string primit...
code_blockt make_class_identifier_code(const java_method_typet &type, const source_locationt &loc, const irep_idt &function_id, symbol_table_baset &symbol_table, message_handlert &message_handler)
Used to provide our own implementation of the CProver.classIdentifier() function.
static bool is_java_char_sequence_type(const typet &type)
static bool is_java_string_buffer_type(const typet &type)
bool implements_function(const irep_idt &function_id) const
code_blockt make_copy_string_code(const java_method_typet &type, const source_locationt &loc, const irep_idt &function_id, symbol_table_baset &symbol_table, message_handlert &message_handler)
Generates code for a function which copies a string object to a new string object.
std::vector< irep_idt > get_string_type_base_classes(const irep_idt &class_name)
Gets the base classes for known String and String-related types, or returns an empty list for other t...
static bool is_java_char_array_pointer_type(const typet &type)
refined_string_exprt make_nondet_string_expr(const source_locationt &loc, const irep_idt &function_id, symbol_table_baset &symbol_table, code_blockt &code)
add symbols with prefix cprover_string_length and cprover_string_data and construct a string_expr fro...
code_blockt make_assign_function_from_call(const irep_idt &function_id, const java_method_typet &type, const source_locationt &loc, symbol_table_baset &symbol_table)
Call a cprover internal function and assign the result to object this.
void add_string_type(const irep_idt &class_name, symbol_table_baset &symbol_table)
Add to the symbol table type declaration for a String-like Java class.
exprt allocate_fresh_string(const typet &type, const source_locationt &loc, const irep_idt &function_id, symbol_table_baset &symbol_table, code_blockt &code)
declare a new String and allocate it
static bool is_java_string_pointer_type(const typet &type)
refined_string_exprt replace_char_array(const exprt &array_pointer, const source_locationt &loc, const irep_idt &function_name, symbol_table_baset &symbol_table, code_blockt &code)
we declare a new cprover_string whose contents is deduced from the char array.
static bool is_java_string_builder_pointer_type(const typet &type)
exprt::operandst process_parameters(const java_method_typet::parameterst &params, const source_locationt &loc, const irep_idt &function_name, symbol_table_baset &symbol_table, code_blockt &init_code)
calls string_refine_preprocesst::process_operands with a list of parameters.
static bool is_java_char_sequence_pointer_type(const typet &type)
static bool is_java_char_array_type(const typet &type)
code_blockt make_init_function_from_call(const irep_idt &function_id, const java_method_typet &type, const source_locationt &loc, symbol_table_baset &symbol_table, bool is_constructor=true)
Generate the goto code for string initialization.
codet replace_character_call(code_function_callt call)
character_refine_preprocesst character_preprocess
static bool is_java_string_builder_type(const typet &type)
code_blockt make_function_from_call(const irep_idt &function_id, const java_method_typet &type, const source_locationt &loc, symbol_table_baset &symbol_table)
Provide code for a function that calls a function from the solver and simply returns it.
bool is_known_string_type(irep_idt class_name)
Check whether a class name is known as a string type.
std::unordered_set< irep_idt > string_types
codet code_assign_string_expr_to_java_string(const exprt &lhs, const refined_string_exprt &rhs, const symbol_table_baset &symbol_table, bool is_constructor)
Produce code for an assignemnt of a string expr to a Java string.
static bool is_java_string_buffer_pointer_type(const typet &type)
codet code_return_function_application(const irep_idt &function_id, const exprt::operandst &arguments, const typet &type, symbol_table_baset &symbol_table)
return the result of a function call
symbol_exprt fresh_string(const typet &type, const source_locationt &loc, const irep_idt &function_id, symbol_table_baset &symbol_table)
add a symbol with static lifetime and name containing cprover_string and given type
friend refined_string_exprt convert_exprt_to_string_exprt_unit_test(java_string_library_preprocesst &preprocess, const exprt &deref, const source_locationt &loc, const irep_idt &function_id, symbol_table_baset &symbol_table, code_blockt &init_code)
code_blockt make_copy_constructor_code(const java_method_typet &type, const source_locationt &loc, const irep_idt &function_id, symbol_table_baset &symbol_table, message_handlert &message_handler)
Generates code for a constructor of a string object from another string object.
void initialize_conversion_table()
fill maps with correspondence from java method names to conversion functions
static bool implements_java_char_sequence(const typet &type)
exprt::operandst process_operands(const exprt::operandst &operands, const source_locationt &loc, const irep_idt &function_name, symbol_table_baset &symbol_table, code_blockt &init_code)
for each expression that is of a type implementing strings, we declare a new cprover_string whose con...
static bool is_java_string_type(const typet &type)
const std::array< id_mapt *, 5 > id_maps
codet code_for_function(const symbolt &symbol, symbol_table_baset &symbol_table, message_handlert &message_handler)
Should be called to provide code for string functions that are used in the code but for which no impl...
static bool java_type_matches_tag(const typet &type, const std::string &tag)
refined_string_exprt decl_string_expr(const source_locationt &loc, const irep_idt &function_id, symbol_table_baset &symbol_table, code_blockt &code)
Add declaration of a refined string expr whose content and length are fresh symbols.
void code_assign_java_string_to_string_expr(const refined_string_exprt &lhs, const exprt &rhs, const source_locationt &loc, const symbol_table_baset &symbol_table, code_blockt &code)
Expression to hold a symbol (variable)
Definition: std_expr.h:131
The symbol table base class interface.
Symbol table entry.
Definition: symbol.h:28
The type of an expression, extends irept.
Definition: type.h:29
static bool is_constructor(const irep_idt &method_name)
void add_character_set_constraint(const exprt &pointer, const exprt &length, const irep_idt &char_range, symbol_table_baset &symbol_table, const source_locationt &loc, const irep_idt &function_id, code_blockt &code)
Add a call to a primitive of the string solver which ensures all characters belong to the character s...
void add_array_to_length_association(const exprt &array, const exprt &length, symbol_table_baset &symbol_table, const source_locationt &loc, const irep_idt &function_id, code_blockt &code)
Add a call to a primitive of the string solver, letting it know that the actual length of array is le...
exprt make_nondet_infinite_char_array(symbol_table_baset &symbol_table, const source_locationt &loc, const irep_idt &function_id, code_blockt &code)
Declare a fresh symbol of type array of character with infinite size.
void add_pointer_to_array_association(const exprt &pointer, const exprt &array, symbol_table_baset &symbol_table, const source_locationt &loc, const irep_idt &function_id, code_blockt &code)
Add a call to a primitive of the string solver, letting it know that pointer points to the first char...
signedbv_typet java_int_type()
Definition: java_types.cpp:31
unsignedbv_typet java_char_type()
Definition: java_types.cpp:61
Type for string expressions used by the string solver.
String expressions for the string solver.