cprover
java_bytecode_convert_method_class.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: JAVA Bytecode Language Conversion
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
11 
12 #ifndef CPROVER_JAVA_BYTECODE_JAVA_BYTECODE_CONVERT_METHOD_CLASS_H
13 #define CPROVER_JAVA_BYTECODE_JAVA_BYTECODE_CONVERT_METHOD_CLASS_H
14 
15 #include "ci_lazy_methods_needed.h"
18 
19 #include <util/expanding_vector.h>
20 #include <util/message.h>
21 #include <util/std_expr.h>
22 
24 
25 #include <vector>
26 #include <list>
27 
28 class class_hierarchyt;
29 class prefix_filtert;
30 class symbol_tablet;
31 class symbolt;
32 
34 {
35 public:
38  message_handlert &_message_handler,
39  size_t _max_array_length,
42  java_string_library_preprocesst &_string_preprocess,
44  bool threading_support,
46  : log(_message_handler),
49  max_array_length(_max_array_length),
54  string_preprocess(_string_preprocess),
56  method_has_this(false),
58  {
59  }
60 
66 
67  void operator()(
68  const symbolt &class_symbol,
69  const methodt &method,
70  const optionalt<prefix_filtert> &method_context)
71  {
72  convert(class_symbol, method, method_context);
73  }
74 
75  typedef uint16_t method_offsett;
76 
77 protected:
81  const size_t max_array_length;
84  const bool threading_support;
86 
91 
94 
98 
100 
105 
106 public:
107  struct holet
108  {
111  };
112 
114  {
117  std::vector<holet> holes;
118  };
119 
120  typedef std::vector<local_variable_with_holest>
122 
123  class variablet
124  {
125  public:
127  size_t start_pc;
128  size_t length;
129  bool is_parameter = false;
130  std::vector<holet> holes;
131 
133  const symbol_exprt &_symbol_expr,
134  std::size_t _start_pc,
135  std::size_t _length)
136  : symbol_expr(_symbol_expr), start_pc(_start_pc), length(_length)
137  {
138  }
139 
141  const symbol_exprt &_symbol_expr,
142  std::size_t _start_pc,
143  std::size_t _length,
144  bool _is_parameter)
145  : symbol_expr(_symbol_expr),
146  start_pc(_start_pc),
147  length(_length),
148  is_parameter(_is_parameter)
149  {
150  }
151 
153  const symbol_exprt &_symbol_expr,
154  std::size_t _start_pc,
155  std::size_t _length,
156  bool _is_parameter,
157  std::vector<holet> &&_holes)
158  : symbol_expr(_symbol_expr),
159  start_pc(_start_pc),
160  length(_length),
161  is_parameter(_is_parameter),
162  holes(std::move(_holes))
163  {
164  }
165  };
166 
167 protected:
168  typedef std::vector<variablet> variablest;
170  std::set<symbol_exprt> used_local_names;
172  std::map<irep_idt, bool> class_has_clinit_method;
173  std::map<irep_idt, bool> any_superclass_has_clinit_method;
175 
177  {
179  INST_INDEX_CONST = 3
180  };
181 
182  // return corresponding reference of variable
183  const variablet &find_variable_for_slot(
184  size_t address,
185  variablest &var_list);
186 
187  // JVM local variables
189  {
191  NO_CAST
192  };
193 
194  exprt variable(const exprt &arg, char type_char, size_t address);
195 
196  // temporary variables
197  std::list<symbol_exprt> tmp_vars;
198 
199  symbol_exprt tmp_variable(const std::string &prefix, const typet &type);
200 
201  // JVM program locations
202  static irep_idt label(const irep_idt &address);
203 
204  // JVM Stack
205  typedef std::vector<exprt> stackt;
207 
208  exprt::operandst pop(std::size_t n);
209 
210  void pop_residue(std::size_t n);
211 
212  void push(const exprt::operandst &o);
213 
218  {
219  return v.index < slots_for_parameters;
220  }
221 
223  {
225  const instructionst::const_iterator &it,
226  const codet &_code)
227  : source(it), code(_code), done(false)
228  {
229  }
230 
231  instructionst::const_iterator source;
232  std::list<method_offsett> successors;
233  std::set<method_offsett> predecessors;
236  bool done;
237  };
238 
239 public:
240  typedef std::map<method_offsett, converted_instructiont> address_mapt;
241  typedef std::pair<const methodt &, const address_mapt &> method_with_amapt;
244 
245 protected:
246  void find_initializers(
248  const address_mapt &amap,
249  const java_cfg_dominatorst &doms);
250 
252  local_variable_table_with_holest::iterator firstvar,
253  local_variable_table_with_holest::iterator varlimit,
254  const address_mapt &amap,
255  const java_cfg_dominatorst &doms);
256 
257  void setup_local_variables(const methodt &m, const address_mapt &amap);
258 
260  {
261  bool leaf;
262  std::vector<method_offsett> branch_addresses;
263  std::vector<block_tree_nodet> branch;
264 
266  {
267  }
268 
269  explicit block_tree_nodet(bool l) : leaf(l)
270  {
271  }
272 
274  {
275  return block_tree_nodet(true);
276  }
277  };
278 
279  static void replace_goto_target(
280  codet &repl,
281  const irep_idt &old_label,
282  const irep_idt &new_label);
283 
285  block_tree_nodet &tree,
286  code_blockt &this_block,
287  method_offsett address_start,
288  method_offsett address_limit,
289  method_offsett next_block_start_address);
290 
292  block_tree_nodet &tree,
293  code_blockt &this_block,
294  method_offsett address_start,
295  method_offsett address_limit,
296  method_offsett next_block_start_address,
297  const address_mapt &amap,
298  bool allow_merge = true);
299 
300  // conversion
301  void convert(
302  const symbolt &class_symbol,
303  const methodt &,
304  const optionalt<prefix_filtert> &method_context);
305 
307  const methodt &method,
308  const java_method_typet &method_type);
309 
311 
312  codet get_clinit_call(const irep_idt &classname);
313 
314  bool is_method_inherited(
315  const irep_idt &classname,
316  const irep_idt &mangled_method_name) const;
317 
319  const irep_idt &class_identifier, const irep_idt &component_name) const;
320 
322  {
323  VARIABLE,
324  ARRAY_REF,
325  STATIC_FIELD,
326  FIELD
327  };
328 
329  void save_stack_entries(
330  const std::string &,
331  code_blockt &,
332  const bytecode_write_typet,
333  const irep_idt &);
334 
336  const std::string &,
337  const typet &,
338  code_blockt &,
339  exprt &);
340 
341  std::vector<method_offsett> try_catch_handler(
342  method_offsett address,
344  const;
345 
347  address_mapt &address_map,
348  const std::vector<method_offsett> &jsr_ret_targets,
349  const std::vector<
350  std::vector<java_bytecode_parse_treet::instructiont>::const_iterator>
351  &ret_instructions) const;
352 
354  const source_locationt &location,
355  std::size_t instruction_address,
356  const exprt &arg0,
357  codet &result_code);
358 
360  const irep_idt &statement,
361  const exprt::operandst &op,
362  const source_locationt &location);
363 
365  const irep_idt &statement,
366  const exprt &arg0,
367  const exprt::operandst &op,
368  const method_offsett address,
369  const source_locationt &location);
370 
371  static exprt
372  convert_aload(const irep_idt &statement, const exprt::operandst &op);
373 
383  exprt convert_load(const exprt &index, char type_char, size_t address);
384 
386  const std::vector<method_offsett> &jsr_ret_targets,
387  const exprt &arg0,
388  const source_locationt &location,
389  const method_offsett address);
390 
393  const u1 bytecode,
394  const exprt::operandst &op,
395  const mp_integer &number,
396  const source_locationt &location) const;
397 
400  const exprt::operandst &op,
401  const irep_idt &id,
402  const mp_integer &number,
403  const source_locationt &location) const;
404 
407  const exprt::operandst &op,
408  const mp_integer &number,
409  const source_locationt &location) const;
410 
413  const exprt::operandst &op,
414  const mp_integer &number,
415  const source_locationt &location) const;
416 
418  const exprt &arg0,
419  const exprt &arg1,
420  const source_locationt &location,
421  method_offsett address);
422 
424  const irep_idt &statement,
425  const exprt::operandst &op,
426  exprt::operandst &results) const;
427 
429  const irep_idt &statement,
430  const exprt::operandst &op,
431  exprt::operandst &results) const;
432 
434  convert_cmp(const exprt::operandst &op, exprt::operandst &results) const;
435 
437  const irep_idt &statement,
438  const exprt::operandst &op,
439  exprt::operandst &results) const;
440 
441  void convert_getstatic(
442  const source_locationt &source_location,
443  const exprt &arg0,
444  const symbol_exprt &symbol_expr,
445  bool is_assertions_disabled_field,
446  codet &c,
447  exprt::operandst &results);
448 
450  convert_putfield(const fieldref_exprt &arg0, const exprt::operandst &op);
451 
453  const source_locationt &location,
454  const exprt &arg0,
455  const exprt::operandst &op,
456  const symbol_exprt &symbol_expr);
457 
458  void convert_new(
459  const source_locationt &location,
460  const exprt &arg0,
461  codet &c,
462  exprt::operandst &results);
463 
465  const source_locationt &location,
466  const irep_idt &statement,
467  const exprt &arg0,
468  const exprt::operandst &op,
469  exprt::operandst &results);
470 
472  const source_locationt &location,
473  const exprt &arg0,
474  const exprt::operandst &op,
475  exprt::operandst &results);
476 
478  const methodt &method,
479  const std::set<method_offsett> &working_set,
480  method_offsett cur_pc,
481  codet &c);
482 
483  void convert_athrow(
484  const source_locationt &location,
485  const exprt::operandst &op,
486  codet &c,
487  exprt::operandst &results) const;
488 
489  void convert_checkcast(
490  const exprt &arg0,
491  const exprt::operandst &op,
492  codet &c,
493  exprt::operandst &results) const;
494 
496  const irep_idt &statement,
497  const exprt::operandst &op,
498  const source_locationt &source_location);
499 
501 
502  void convert_invoke(
503  source_locationt location,
504  const irep_idt &statement,
505  class_method_descriptor_exprt &class_method_descriptor,
506  codet &c,
507  exprt::operandst &results);
508 
510  const irep_idt &statement,
511  const constant_exprt &arg0,
512  exprt::operandst &results) const;
513 
515 
517 
518  void convert_dup2(exprt::operandst &op, exprt::operandst &results);
519 
521  const exprt::operandst &op,
523  const source_locationt &location);
524 
525  codet convert_pop(const irep_idt &statement, const exprt::operandst &op);
526 
528 };
529 #endif
uint8_t u1
Definition: bytecode_info.h:55
Compute dominators for CFG of goto_function.
Context-insensitive lazy methods container.
Non-graph-based representation of the class hierarchy.
An expression describing a method on a class.
Definition: std_expr.h:3218
A codet representing sequential composition of program statements.
Definition: std_code.h:168
codet representation of an if-then-else statement.
Definition: std_code.h:776
codet representing a switch statement.
Definition: std_code.h:864
Data structure for representing an arbitrary statement in a program.
Definition: std_code.h:33
A constant literal expression.
Definition: std_expr.h:2753
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:37
Base class for all expressions.
Definition: expr.h:54
std::vector< exprt > operandst
Definition: expr.h:56
Represents the argument of an instruction that uses a CONSTANT_Fieldref This is used for example as a...
variablet(const symbol_exprt &_symbol_expr, std::size_t _start_pc, std::size_t _length, bool _is_parameter, std::vector< holet > &&_holes)
variablet(const symbol_exprt &_symbol_expr, std::size_t _start_pc, std::size_t _length, bool _is_parameter)
variablet(const symbol_exprt &_symbol_expr, std::size_t _start_pc, std::size_t _length)
void convert_dup2_x2(exprt::operandst &op, exprt::operandst &results)
optionalt< ci_lazy_methods_neededt > needed_lazy_methods
method_offsett slots_for_parameters
Number of local variable slots used by the JVM to pass parameters upon invocation of the method under...
irep_idt get_static_field(const irep_idt &class_identifier, const irep_idt &component_name) const
Get static field identifier referred to by class_identifier.component_name Note this may be inherited...
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
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
void create_stack_tmp_var(const std::string &, const typet &, code_blockt &, exprt &)
actually create a temporary variable to hold the value of a stack entry
cfg_dominators_templatet< method_with_amapt, method_offsett, false > java_cfg_dominatorst
codet & do_exception_handling(const methodt &method, const std::set< method_offsett > &working_set, method_offsett cur_pc, codet &c)
exprt::operandst & convert_ushr(const irep_idt &statement, const exprt::operandst &op, exprt::operandst &results) const
exprt convert_load(const exprt &index, char type_char, size_t address)
Load reference from local variable.
void setup_local_variables(const methodt &m, const address_mapt &amap)
See find_initializers_for_slot above for more detail.
const variablet & find_variable_for_slot(size_t address, variablest &var_list)
See above.
void push(const exprt::operandst &o)
code_blockt convert_store(const irep_idt &statement, const exprt &arg0, const exprt::operandst &op, const method_offsett address, const source_locationt &location)
code_blockt convert_astore(const irep_idt &statement, const exprt::operandst &op, const source_locationt &location)
static irep_idt label(const irep_idt &address)
std::vector< method_offsett > try_catch_handler(method_offsett address, const java_bytecode_parse_treet::methodt::exception_tablet &exception_table) const
code_blockt convert_instructions(const methodt &)
codet & replace_call_to_cprover_assume(source_locationt location, codet &c)
java_string_library_preprocesst & string_preprocess
code_blockt convert_ret(const std::vector< method_offsett > &jsr_ret_targets, const exprt &arg0, const source_locationt &location, const method_offsett address)
void find_initializers(local_variable_table_with_holest &vars, const address_mapt &amap, const java_cfg_dominatorst &doms)
See find_initializers_for_slot above for more detail.
codet get_clinit_call(const irep_idt &classname)
Each static access to classname should be prefixed with a check for necessary static init; this retur...
code_blockt convert_putstatic(const source_locationt &location, const exprt &arg0, const exprt::operandst &op, const symbol_exprt &symbol_expr)
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
java_bytecode_parse_treet::methodt methodt
std::vector< local_variable_with_holest > local_variable_table_with_holest
std::pair< const methodt &, const address_mapt & > method_with_amapt
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 & 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)
'tree' describes a tree of code_blockt objects; this_block is the corresponding block (thus they are ...
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)
Given a sequence of users of the same local variable slot, this figures out which ones are related by...
codet convert_pop(const irep_idt &statement, const exprt::operandst &op)
exprt::operandst pop(std::size_t n)
java_bytecode_parse_treet::instructiont instructiont
exprt::operandst & convert_const(const irep_idt &statement, const constant_exprt &arg0, exprt::operandst &results) const
void operator()(const symbolt &class_symbol, const methodt &method, const optionalt< prefix_filtert > &method_context)
void convert_checkcast(const exprt &arg0, const exprt::operandst &op, codet &c, exprt::operandst &results) const
void convert_dup2_x1(exprt::operandst &op, exprt::operandst &results)
code_blockt convert_putfield(const fieldref_exprt &arg0, const exprt::operandst &op)
code_blockt convert_multianewarray(const source_locationt &location, const exprt &arg0, const exprt::operandst &op, exprt::operandst &results)
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
void convert(const symbolt &class_symbol, const methodt &, const optionalt< prefix_filtert > &method_context)
code_blockt convert_parameter_annotations(const methodt &method, const java_method_typet &method_type)
code_switcht convert_switch(const exprt::operandst &op, const java_bytecode_parse_treet::instructiont::argst &args, const source_locationt &location)
void save_stack_entries(const std::string &, code_blockt &, const bytecode_write_typet, const irep_idt &)
Create temporary variables if a write instruction can have undesired side- effects.
bool is_parameter(const local_variablet &v)
Returns true iff the slot index of the local variable of a method (coming from the LVT) is a paramete...
void convert_new(const source_locationt &location, const exprt &arg0, codet &c, exprt::operandst &results)
exprt variable(const exprt &arg, char type_char, size_t address)
Returns an expression indicating a local variable suitable to load/store from a bytecode at address a...
std::map< method_offsett, converted_instructiont > address_mapt
methodt::local_variable_tablet local_variable_tablet
std::map< irep_idt, bool > any_superclass_has_clinit_method
static void replace_goto_target(codet &repl, const irep_idt &old_label, const irep_idt &new_label)
Find all goto statements in 'repl' that target 'old_label' and redirect them to 'new_label'.
code_blockt convert_iinc(const exprt &arg0, const exprt &arg1, const source_locationt &location, method_offsett address)
exprt::operandst & convert_cmp2(const irep_idt &statement, const exprt::operandst &op, exprt::operandst &results) const
void convert_dup2(exprt::operandst &op, exprt::operandst &results)
optionalt< exprt > convert_invoke_dynamic(const source_locationt &location, std::size_t instruction_address, const exprt &arg0, codet &result_code)
bool is_method_inherited(const irep_idt &classname, const irep_idt &mangled_method_name) const
Returns true iff method methodid from class classname is a method inherited from a class or interface...
void convert_athrow(const source_locationt &location, const exprt::operandst &op, codet &c, exprt::operandst &results) const
typet method_return_type
Return type of the method under conversion.
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)
irep_idt method_id
Fully qualified name of the method under translation.
code_blockt convert_newarray(const source_locationt &location, const irep_idt &statement, const exprt &arg0, const exprt::operandst &op, exprt::operandst &results)
exprt::operandst & convert_shl(const irep_idt &statement, const exprt::operandst &op, exprt::operandst &results) const
exprt::operandst & convert_cmp(const exprt::operandst &op, exprt::operandst &results) const
void pop_residue(std::size_t n)
removes minimum(n, stack.size()) elements from the stack
void convert_invoke(source_locationt location, const irep_idt &statement, class_method_descriptor_exprt &class_method_descriptor, codet &c, exprt::operandst &results)
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)
As above, but this version can additionally create a new branch in the block_tree-node and code_block...
java_bytecode_convert_methodt(symbol_table_baset &symbol_table, message_handlert &_message_handler, size_t _max_array_length, bool throw_assertion_error, optionalt< 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)
codet convert_monitorenterexit(const irep_idt &statement, const exprt::operandst &op, const source_locationt &source_location)
irep_idt current_method
A copy of method_id :/.
static exprt convert_aload(const irep_idt &statement, const exprt::operandst &op)
symbol_exprt tmp_variable(const std::string &prefix, const typet &type)
Class that provides messages with a built-in verbosity 'level'.
Definition: message.h:155
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:91
Provides filtering of strings vai inclusion/exclusion lists of prefixes.
Definition: prefix_filter.h:20
Expression to hold a symbol (variable)
Definition: std_expr.h:80
The symbol table base class interface.
The symbol table.
Definition: symbol_table.h:14
Symbol table entry.
Definition: symbol.h:28
The type of an expression, extends irept.
Definition: type.h:28
JAVA Bytecode Language Conversion.
java_bytecode_convert_methodt::address_mapt address_mapt
nonstd::optional< T > optionalt
Definition: optional.h:35
BigInt mp_integer
Definition: smt_terms.h:12
API to expression classes.
converted_instructiont(const instructionst::const_iterator &it, const codet &_code)
std::vector< local_variablet > local_variable_tablet
std::vector< instructiont > instructionst