forked from diffblue/cbmc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjava_bytecode_convert_method_class.h
547 lines (446 loc) · 14.5 KB
/
java_bytecode_convert_method_class.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/*******************************************************************\
Module: JAVA Bytecode Language Conversion
Author: Daniel Kroening, [email protected]
\*******************************************************************/
/// \file
/// JAVA Bytecode Language Conversion
#ifndef CPROVER_JAVA_BYTECODE_JAVA_BYTECODE_CONVERT_METHOD_CLASS_H
#define CPROVER_JAVA_BYTECODE_JAVA_BYTECODE_CONVERT_METHOD_CLASS_H
#include "java_bytecode_convert_class.h"
#include <util/expanding_vector.h>
#include <util/message.h>
#include <util/namespace.h>
#include <util/std_code.h>
#include <util/std_expr.h>
#include <analyses/cfg_dominators.h>
#include "ci_lazy_methods_needed.h"
#include "java_bytecode_parse_tree.h"
#include <list>
#include <vector>
class class_hierarchyt;
class prefix_filtert;
class symbolt;
class java_bytecode_convert_methodt
{
public:
java_bytecode_convert_methodt(
symbol_table_baset &symbol_table,
message_handlert &_message_handler,
size_t _max_array_length,
bool throw_assertion_error,
std::optional<ci_lazy_methods_neededt> needed_lazy_methods,
java_string_library_preprocesst &_string_preprocess,
const class_hierarchyt &class_hierarchy,
bool threading_support,
bool assert_no_exceptions_thrown)
: log(_message_handler),
symbol_table(symbol_table),
ns(symbol_table),
max_array_length(_max_array_length),
throw_assertion_error(throw_assertion_error),
assert_no_exceptions_thrown(assert_no_exceptions_thrown),
threading_support(threading_support),
needed_lazy_methods(std::move(needed_lazy_methods)),
string_preprocess(_string_preprocess),
slots_for_parameters(0),
method_has_this(false),
class_hierarchy(class_hierarchy)
{
}
typedef java_bytecode_parse_treet::methodt methodt;
typedef java_bytecode_parse_treet::instructiont instructiont;
typedef methodt::instructionst instructionst;
typedef methodt::local_variable_tablet local_variable_tablet;
typedef methodt::local_variablet local_variablet;
void operator()(
const symbolt &class_symbol,
const methodt &method,
const std::optional<prefix_filtert> &method_context)
{
convert(class_symbol, method, method_context);
}
typedef uint16_t method_offsett;
protected:
messaget log;
symbol_table_baset &symbol_table;
namespacet ns;
const size_t max_array_length;
const bool throw_assertion_error;
const bool assert_no_exceptions_thrown;
const bool threading_support;
std::optional<ci_lazy_methods_neededt> needed_lazy_methods;
/// Fully qualified name of the method under translation.
/// Initialized by `convert`.
/// Example: "my.package.ClassName.myMethodName:(II)I"
irep_idt method_id;
/// A copy of `method_id` :/
irep_idt current_method;
/// Return type of the method under conversion.
/// Initialized by `convert`.
typet method_return_type;
java_string_library_preprocesst &string_preprocess;
/// Number of local variable slots used by the JVM to pass parameters upon
/// invocation of the method under translation.
/// Initialized in `convert`.
method_offsett slots_for_parameters;
public:
struct holet
{
method_offsett start_pc;
method_offsett length;
};
struct local_variable_with_holest
{
local_variablet var;
bool is_parameter;
std::vector<holet> holes;
};
typedef std::vector<local_variable_with_holest>
local_variable_table_with_holest;
class variablet
{
public:
symbol_exprt symbol_expr;
size_t start_pc;
size_t length;
bool is_parameter = false;
std::vector<holet> holes;
variablet(
const symbol_exprt &_symbol_expr,
std::size_t _start_pc,
std::size_t _length)
: symbol_expr(_symbol_expr), start_pc(_start_pc), length(_length)
{
}
variablet(
const symbol_exprt &_symbol_expr,
std::size_t _start_pc,
std::size_t _length,
bool _is_parameter)
: symbol_expr(_symbol_expr),
start_pc(_start_pc),
length(_length),
is_parameter(_is_parameter)
{
}
variablet(
const symbol_exprt &_symbol_expr,
std::size_t _start_pc,
std::size_t _length,
bool _is_parameter,
std::vector<holet> &&_holes)
: symbol_expr(_symbol_expr),
start_pc(_start_pc),
length(_length),
is_parameter(_is_parameter),
holes(std::move(_holes))
{
}
};
protected:
typedef std::vector<variablet> variablest;
expanding_vectort<variablest> variables;
std::set<symbol_exprt> used_local_names;
bool method_has_this;
std::map<irep_idt, bool> class_has_clinit_method;
std::map<irep_idt, bool> any_superclass_has_clinit_method;
const class_hierarchyt &class_hierarchy;
enum instruction_sizet
{
INST_INDEX = 2,
INST_INDEX_CONST = 3
};
// return corresponding reference of variable
const variablet &find_variable_for_slot(
size_t address,
variablest &var_list);
// JVM local variables
enum variable_cast_argumentt
{
CAST_AS_NEEDED,
NO_CAST
};
exprt variable(const exprt &arg, char type_char, size_t address);
// temporary variables
std::list<symbol_exprt> tmp_vars;
symbol_exprt tmp_variable(const std::string &prefix, const typet &type);
// JVM program locations
static irep_idt label(const irep_idt &address);
// JVM Stack
typedef std::vector<exprt> stackt;
stackt stack;
exprt::operandst pop(std::size_t n);
void pop_residue(std::size_t n);
void push(const exprt::operandst &o);
/// Returns true iff the slot index of the local variable of a method (coming
/// from the LVT) is a parameter of that method. Assumes that
/// `slots_for_parameters` is initialized upon call.
bool is_parameter(const local_variablet &v)
{
return v.index < slots_for_parameters;
}
struct converted_instructiont
{
converted_instructiont(
const instructionst::const_iterator &it,
const codet &_code)
: source(it), code(_code), done(false)
{
}
instructionst::const_iterator source;
std::list<method_offsett> successors;
std::set<method_offsett> predecessors;
codet code;
stackt stack;
bool done;
};
public:
typedef std::map<method_offsett, converted_instructiont> address_mapt;
struct method_with_amapt
{
method_with_amapt(const methodt &m, const address_mapt &a)
: method_with_amap(m, a)
{
}
std::pair<const methodt &, const address_mapt &> method_with_amap;
struct target_less_than // NOLINT(readability/identifiers)
{
bool operator()(const method_offsett &a, const method_offsett &b) const
{
return a < b;
}
};
};
typedef cfg_dominators_templatet<method_with_amapt, method_offsett, false>
java_cfg_dominatorst;
protected:
void find_initializers(
local_variable_table_with_holest &vars,
const address_mapt &amap,
const java_cfg_dominatorst &doms);
void find_initializers_for_slot(
local_variable_table_with_holest::iterator firstvar,
local_variable_table_with_holest::iterator varlimit,
const address_mapt &amap,
const java_cfg_dominatorst &doms);
void setup_local_variables(const methodt &m, const address_mapt &amap);
struct block_tree_nodet
{
bool leaf;
std::vector<method_offsett> branch_addresses;
std::vector<block_tree_nodet> branch;
block_tree_nodet() : leaf(false)
{
}
explicit block_tree_nodet(bool l) : leaf(l)
{
}
static block_tree_nodet get_leaf()
{
return block_tree_nodet(true);
}
};
static void replace_goto_target(
codet &repl,
const irep_idt &old_label,
const irep_idt &new_label);
code_blockt &get_block_for_pcrange(
block_tree_nodet &tree,
code_blockt &this_block,
method_offsett address_start,
method_offsett address_limit,
method_offsett next_block_start_address);
code_blockt &get_or_create_block_for_pcrange(
block_tree_nodet &tree,
code_blockt &this_block,
method_offsett address_start,
method_offsett address_limit,
method_offsett next_block_start_address,
const address_mapt &amap,
bool allow_merge = true);
// conversion
void convert(
const symbolt &class_symbol,
const methodt &,
const std::optional<prefix_filtert> &method_context);
code_blockt convert_parameter_annotations(
const methodt &method,
const java_method_typet &method_type);
code_blockt convert_instructions(const methodt &);
codet get_clinit_call(const irep_idt &classname);
bool is_method_inherited(
const irep_idt &classname,
const irep_idt &mangled_method_name) const;
irep_idt get_static_field(
const irep_idt &class_identifier, const irep_idt &component_name) const;
enum class bytecode_write_typet
{
VARIABLE,
ARRAY_REF,
STATIC_FIELD,
FIELD
};
void save_stack_entries(
const std::string &,
code_blockt &,
const bytecode_write_typet,
const irep_idt &);
void create_stack_tmp_var(
const std::string &,
const typet &,
code_blockt &,
exprt &);
std::vector<method_offsett> try_catch_handler(
method_offsett address,
const java_bytecode_parse_treet::methodt::exception_tablet &exception_table)
const;
void draw_edges_from_ret_to_jsr(
address_mapt &address_map,
const std::vector<method_offsett> &jsr_ret_targets,
const std::vector<
std::vector<java_bytecode_parse_treet::instructiont>::const_iterator>
&ret_instructions) const;
std::optional<exprt> convert_invoke_dynamic(
const source_locationt &location,
std::size_t instruction_address,
const exprt &arg0,
codet &result_code);
code_blockt convert_astore(
const irep_idt &statement,
const exprt::operandst &op,
const source_locationt &location);
code_blockt convert_store(
const irep_idt &statement,
const exprt &arg0,
const exprt::operandst &op,
const method_offsett address,
const source_locationt &location);
static exprt
convert_aload(const irep_idt &statement, const exprt::operandst &op);
/// Load reference from local variable.
/// \p index must be an unsigned byte and an index in the local variable array
/// of the current frame. The type of the local variable at index \p index
/// must:
/// - be a reference type if \p type_char is 'a'
/// - be either boolean, byte, short, int, or char if \p type_char is 'i', a
/// typecast to `int` is added if needed
/// - match \c java_type_of_char(type_char) otherwise
/// \return the local variable at \p index
exprt convert_load(const exprt &index, char type_char, size_t address);
code_blockt convert_ret(
const std::vector<method_offsett> &jsr_ret_targets,
const exprt &arg0,
const source_locationt &location,
const method_offsett address);
code_ifthenelset convert_if_cmp(
const java_bytecode_convert_methodt::address_mapt &address_map,
const u1 bytecode,
const exprt::operandst &op,
const mp_integer &number,
const source_locationt &location) const;
code_ifthenelset convert_if(
const java_bytecode_convert_methodt::address_mapt &address_map,
const exprt::operandst &op,
const irep_idt &id,
const mp_integer &number,
const source_locationt &location) const;
code_ifthenelset convert_ifnonull(
const java_bytecode_convert_methodt::address_mapt &address_map,
const exprt::operandst &op,
const mp_integer &number,
const source_locationt &location) const;
code_ifthenelset convert_ifnull(
const java_bytecode_convert_methodt::address_mapt &address_map,
const exprt::operandst &op,
const mp_integer &number,
const source_locationt &location) const;
code_blockt convert_iinc(
const exprt &arg0,
const exprt &arg1,
const source_locationt &location,
method_offsett address);
exprt::operandst &convert_shl(
const irep_idt &statement,
const exprt::operandst &op,
exprt::operandst &results) const;
exprt::operandst &convert_ushr(
const irep_idt &statement,
const exprt::operandst &op,
exprt::operandst &results) const;
exprt::operandst &
convert_cmp(const exprt::operandst &op, exprt::operandst &results) const;
exprt::operandst &convert_cmp2(
const irep_idt &statement,
const exprt::operandst &op,
exprt::operandst &results) const;
void convert_getstatic(
const source_locationt &source_location,
const exprt &arg0,
const symbol_exprt &symbol_expr,
bool is_assertions_disabled_field,
codet &c,
exprt::operandst &results);
code_blockt
convert_putfield(const fieldref_exprt &arg0, const exprt::operandst &op);
code_blockt convert_putstatic(
const source_locationt &location,
const exprt &arg0,
const exprt::operandst &op,
const symbol_exprt &symbol_expr);
void convert_new(
const source_locationt &location,
const exprt &arg0,
codet &c,
exprt::operandst &results);
code_blockt convert_newarray(
const source_locationt &location,
const irep_idt &statement,
const exprt &arg0,
const exprt::operandst &op,
exprt::operandst &results);
code_blockt convert_multianewarray(
const source_locationt &location,
const exprt &arg0,
const exprt::operandst &op,
exprt::operandst &results);
codet &do_exception_handling(
const methodt &method,
const std::set<method_offsett> &working_set,
method_offsett cur_pc,
codet &c);
void convert_athrow(
const source_locationt &location,
const exprt::operandst &op,
codet &c,
exprt::operandst &results) const;
void convert_checkcast(
const exprt &arg0,
const exprt::operandst &op,
codet &c,
exprt::operandst &results) const;
codet convert_monitorenterexit(
const irep_idt &statement,
const exprt::operandst &op,
const source_locationt &source_location);
codet &replace_call_to_cprover_assume(source_locationt location, codet &c);
void convert_invoke(
source_locationt location,
const irep_idt &statement,
class_method_descriptor_exprt &class_method_descriptor,
codet &c,
exprt::operandst &results);
exprt::operandst &convert_const(
const irep_idt &statement,
const constant_exprt &arg0,
exprt::operandst &results) const;
void convert_dup2_x2(exprt::operandst &op, exprt::operandst &results);
void convert_dup2_x1(exprt::operandst &op, exprt::operandst &results);
void convert_dup2(exprt::operandst &op, exprt::operandst &results);
code_switcht convert_switch(
const exprt::operandst &op,
const java_bytecode_parse_treet::instructiont::argst &args,
const source_locationt &location);
codet convert_pop(const irep_idt &statement, const exprt::operandst &op);
friend class java_bytecode_convert_method_unit_testt;
};
#endif