-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathjava_string_library_preprocess.h
360 lines (298 loc) · 11.4 KB
/
java_string_library_preprocess.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*******************************************************************\
Module: Produce code for simple implementation of String Java libraries
Author: Romain Brenguier
Date: March 2017
\*******************************************************************/
/// \file
/// Produce code for simple implementation of String Java libraries
#ifndef CPROVER_JAVA_BYTECODE_JAVA_STRING_LIBRARY_PREPROCESS_H
#define CPROVER_JAVA_BYTECODE_JAVA_STRING_LIBRARY_PREPROCESS_H
#include <util/ui_message.h>
#include <util/std_code.h>
#include <util/symbol_table.h>
#include <util/refined_string_type.h>
#include <util/string_expr.h>
#include <util/ieee_float.h> // should get rid of this
#include <util/optional.h>
#include <array>
#include <unordered_set>
#include <functional>
#include "character_refine_preprocess.h"
#include "java_types.h"
// Arbitrary limit of 10 arguments for the number of arguments to String.format
#define MAX_FORMAT_ARGS 10
class java_string_library_preprocesst:public messaget
{
public:
java_string_library_preprocesst()
: char_type(java_char_type()),
index_type(java_int_type()),
refined_string_type(index_type, pointer_type(char_type))
{
}
void initialize_known_type_table();
void initialize_conversion_table();
void set_max_intermediate_string_length(
optionalt<std::size_t> max_intermediate_string_length_)
{
this->max_intermediate_string_length = max_intermediate_string_length_;
}
void set_use_fixed_size_arrays_for_bounded_strings(bool value)
{
use_fixed_size_arrays_for_bounded_strings = value;
}
bool implements_function(const irep_idt &function_id) const;
void get_all_function_names(std::unordered_set<irep_idt> &methods) const;
codet
code_for_function(const symbolt &symbol, symbol_table_baset &symbol_table);
codet replace_character_call(code_function_callt call)
{
return character_preprocess.replace_character_call(call);
}
std::vector<irep_idt> get_string_type_base_classes(
const irep_idt &class_name);
void add_string_type(const irep_idt &class_name, symbol_tablet &symbol_table);
bool is_known_string_type(irep_idt class_name);
static bool implements_java_char_sequence_pointer(const typet &type)
{
return is_java_char_sequence_pointer_type(type)
|| is_java_string_builder_pointer_type(type)
|| is_java_string_buffer_pointer_type(type)
|| is_java_string_pointer_type(type);
}
static bool implements_java_char_sequence(const typet &type)
{
return is_java_char_sequence_type(type)
|| is_java_string_builder_type(type)
|| is_java_string_buffer_type(type)
|| is_java_string_type(type);
}
private:
// We forbid copies of the object
java_string_library_preprocesst(
const java_string_library_preprocesst &)=delete;
static bool java_type_matches_tag(const typet &type, const std::string &tag);
static bool is_java_string_pointer_type(const typet &type);
static bool is_java_string_type(const typet &type);
static bool is_java_string_builder_type(const typet &type);
static bool is_java_string_builder_pointer_type(const typet &type);
static bool is_java_string_buffer_type(const typet &type);
static bool is_java_string_buffer_pointer_type(const typet &type);
static bool is_java_char_sequence_type(const typet &type);
static bool is_java_char_sequence_pointer_type(const typet &type);
static bool is_java_char_array_type(const typet &type);
static bool is_java_char_array_pointer_type(const typet &type);
character_refine_preprocesst character_preprocess;
const typet char_type;
const typet index_type;
const refined_string_typet refined_string_type;
typedef std::function<codet(
const java_method_typet &,
const source_locationt &,
const irep_idt &,
symbol_table_baset &)>
conversion_functiont;
typedef std::unordered_map<irep_idt, irep_idt> id_mapt;
// Table mapping each java method signature to the code generating function
std::unordered_map<irep_idt, conversion_functiont> conversion_table;
// Some Java functions have an equivalent in the solver that we will
// call with the same argument and will return the same result
id_mapt cprover_equivalent_to_java_function;
// Some Java functions have an equivalent except that they should
// return Java Strings instead of string_exprt
id_mapt cprover_equivalent_to_java_string_returning_function;
// Some Java initialization function initialize strings with the
// same result as some function of the solver
id_mapt cprover_equivalent_to_java_constructor;
// Some Java functions have an equivalent in the solver except that
// in addition they assign the result to the object on which it is called
id_mapt cprover_equivalent_to_java_assign_and_return_function;
// Some Java functions have an equivalent in the solver except that
// they assign the result to the object on which it is called instead
// of returning it
id_mapt cprover_equivalent_to_java_assign_function;
const std::array<id_mapt *, 5> id_maps = std::array<id_mapt *, 5>
{
{
&cprover_equivalent_to_java_function,
&cprover_equivalent_to_java_string_returning_function,
&cprover_equivalent_to_java_constructor,
&cprover_equivalent_to_java_assign_and_return_function,
&cprover_equivalent_to_java_assign_function,
}
};
// A set tells us what java types should be considered as string objects
std::unordered_set<irep_idt> string_types;
// Maximum length enforced on freshly-allocated strings
optionalt<std::size_t> max_intermediate_string_length;
// If true, allocate fixed-size arrays to hold small (currently hard-coded
// to 256 chars) bounded strings
bool use_fixed_size_arrays_for_bounded_strings = false;
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);
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);
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);
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);
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);
// Helper functions
exprt::operandst process_parameters(
const java_method_typet::parameterst ¶ms,
const source_locationt &loc,
const irep_idt &function_name,
symbol_table_baset &symbol_table,
code_blockt &init_code);
// Friending this function for unit testing convert_exprt_to_string_exprt
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_tablet &symbol_table,
code_blockt &init_code);
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);
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);
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);
symbol_exprt fresh_string(
const typet &type,
const source_locationt &loc,
const irep_idt &function_id,
symbol_table_baset &symbol_table);
refined_string_exprt decl_string_expr(
const source_locationt &loc,
const irep_idt &function_id,
symbol_table_baset &symbol_table,
code_blockt &code);
refined_string_exprt make_nondet_string_expr(
const source_locationt &loc,
const irep_idt &function_id,
symbol_table_baset &symbol_table,
code_blockt &code);
exprt allocate_fresh_string(
const typet &type,
const source_locationt &loc,
const irep_idt &function_id,
symbol_table_baset &symbol_table,
code_blockt &code);
codet code_return_function_application(
const irep_idt &function_id,
const exprt::operandst &arguments,
const typet &type,
symbol_table_baset &symbol_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);
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);
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);
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);
refined_string_exprt string_literal_to_string_expr(
const std::string &s,
const source_locationt &loc,
symbol_table_baset &symbol_table,
code_blockt &code);
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);
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);
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);
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);
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);
};
exprt make_nondet_char_array(
symbol_table_baset &symbol_table,
const source_locationt &loc,
const irep_idt &function_id,
code_blockt &code,
optionalt<std::size_t> max_length);
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);
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);
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);
#endif // CPROVER_JAVA_BYTECODE_JAVA_STRING_LIBRARY_PREPROCESS_H