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_types.h>
22 #include <util/std_expr.h>
23 
25 
26 #include <vector>
27 #include <list>
28 
29 class symbol_tablet;
30 class symbolt;
31 
33 {
34 public:
37  message_handlert &_message_handler,
38  size_t _max_array_length,
41  java_string_library_preprocesst &_string_preprocess,
43  bool threading_support)
44  : messaget(_message_handler),
46  max_array_length(_max_array_length),
50  string_preprocess(_string_preprocess),
52  method_has_this(false),
54  {
55  }
56 
62 
63  void operator()(const symbolt &class_symbol, const methodt &method)
64  {
65  convert(class_symbol, method);
66  }
67 
68 protected:
70  const size_t max_array_length;
72  const bool threading_support;
74 
79 
82 
86 
88 
93 
94 public:
95  struct holet
96  {
97  unsigned start_pc;
98  unsigned length;
99  };
100 
102  {
105  std::vector<holet> holes;
106  };
107 
108  typedef std::vector<local_variable_with_holest>
110 
111  class variablet
112  {
113  public:
115  size_t start_pc;
116  size_t length;
118  std::vector<holet> holes;
119 
121  {
122  }
123  };
124 
125 protected:
126  typedef std::vector<variablet> variablest;
128  std::set<symbol_exprt> used_local_names;
130  std::map<irep_idt, bool> class_has_clinit_method;
131  std::map<irep_idt, bool> any_superclass_has_clinit_method;
133 
135  {
138  };
139 
140  // return corresponding reference of variable
141  const variablet &find_variable_for_slot(
142  size_t address,
143  variablest &var_list);
144 
145  // JVM local variables
147  {
150  };
151 
152  const exprt variable(
153  const exprt &arg,
154  char type_char,
155  size_t address,
156  variable_cast_argumentt do_cast);
157 
158  // temporary variables
159  std::list<symbol_exprt> tmp_vars;
160 
161  symbol_exprt tmp_variable(const std::string &prefix, const typet &type);
162 
163  // JVM program locations
164  static irep_idt label(const irep_idt &address);
165 
166  // JVM Stack
167  typedef std::vector<exprt> stackt;
169 
170  exprt::operandst pop(std::size_t n);
171 
172  void pop_residue(std::size_t n);
173 
174  void push(const exprt::operandst &o);
175 
180  {
181  return v.index < slots_for_parameters;
182  }
183 
185  {
187  const instructionst::const_iterator &it,
188  const codet &_code)
189  : source(it), code(_code), done(false)
190  {
191  }
192 
193  instructionst::const_iterator source;
194  std::list<unsigned> successors;
195  std::set<unsigned> predecessors;
198  bool done;
199  };
200 
201 public:
202  typedef std::map<unsigned, converted_instructiont> address_mapt;
203  typedef std::pair<const methodt &, const address_mapt &> method_with_amapt;
206 
207 protected:
208  void find_initializers(
210  const address_mapt &amap,
211  const java_cfg_dominatorst &doms);
212 
214  local_variable_table_with_holest::iterator firstvar,
215  local_variable_table_with_holest::iterator varlimit,
216  const address_mapt &amap,
217  const java_cfg_dominatorst &doms);
218 
219  void setup_local_variables(const methodt &m, const address_mapt &amap);
220 
222  {
223  bool leaf;
224  std::vector<unsigned> branch_addresses;
225  std::vector<block_tree_nodet> branch;
226 
228  {
229  }
230 
231  explicit block_tree_nodet(bool l) : leaf(l)
232  {
233  }
234 
236  {
237  return block_tree_nodet(true);
238  }
239  };
240 
241  static void replace_goto_target(
242  codet &repl,
243  const irep_idt &old_label,
244  const irep_idt &new_label);
245 
247  block_tree_nodet &tree,
248  code_blockt &this_block,
249  unsigned address_start,
250  unsigned address_limit,
251  unsigned next_block_start_address);
252 
254  block_tree_nodet &tree,
255  code_blockt &this_block,
256  unsigned address_start,
257  unsigned address_limit,
258  unsigned next_block_start_address,
259  const address_mapt &amap,
260  bool allow_merge = true);
261 
263  const java_class_typet::java_lambda_method_handlest &lambda_method_handles,
264  const size_t index);
265 
266  // conversion
267  void convert(const symbolt &class_symbol, const methodt &);
268 
270  const methodt &,
272 
273  const bytecode_infot &get_bytecode_info(const irep_idt &statement);
274 
275  codet get_clinit_call(const irep_idt &classname);
276 
277  bool is_method_inherited(
278  const irep_idt &classname,
279  const irep_idt &methodid) const;
280 
282  const irep_idt &class_identifier, const irep_idt &component_name) const;
283 
285  {
286  VARIABLE,
287  ARRAY_REF,
288  STATIC_FIELD,
289  FIELD
290  };
291 
292  void save_stack_entries(
293  const std::string &,
294  const typet &,
295  code_blockt &,
296  const bytecode_write_typet,
297  const irep_idt &);
298 
300  const std::string &,
301  const typet &,
302  code_blockt &,
303  exprt &);
304 
305  std::vector<unsigned int> try_catch_handler(
306  unsigned int address,
308  const;
309 
311  address_mapt &address_map,
312  const std::vector<unsigned int> &jsr_ret_targets,
313  const std::vector<
314  std::vector<java_bytecode_parse_treet::instructiont>::const_iterator>
315  &ret_instructions) const;
316 
318  const java_class_typet::java_lambda_method_handlest &lambda_method_handles,
319  const source_locationt &location,
320  const exprt &arg0);
321 
323  const irep_idt &statement,
324  const exprt::operandst &op,
325  const source_locationt &location);
326 
328  const irep_idt &statement,
329  const exprt &arg0,
330  const exprt::operandst &op,
331  const unsigned address,
332  const source_locationt &location);
333 
334  exprt
335  convert_aload(const irep_idt &statement, const exprt::operandst &op) const;
336 
338  const std::vector<unsigned int> &jsr_ret_targets,
339  const exprt &arg0,
340  const source_locationt &location,
341  const unsigned address);
342 
345  const irep_idt &statement,
346  const exprt::operandst &op,
347  const mp_integer &number,
348  const source_locationt &location) const;
349 
352  const exprt::operandst &op,
353  const irep_idt &id,
354  const mp_integer &number,
355  const source_locationt &location) const;
356 
359  const exprt::operandst &op,
360  const mp_integer &number,
361  const source_locationt &location) const;
362 
365  const exprt::operandst &op,
366  const mp_integer &number,
367  const source_locationt &location) const;
368 
370  const exprt &arg0,
371  const exprt &arg1,
372  const source_locationt &location,
373  unsigned address);
374 
376  const irep_idt &statement,
377  const exprt::operandst &op,
378  exprt::operandst &results) const;
379 
381  convert_cmp(const exprt::operandst &op, exprt::operandst &results) const;
382 
384  const irep_idt &statement,
385  const exprt::operandst &op,
386  exprt::operandst &results) const;
387 
388  void convert_getstatic(
389  const exprt &arg0,
390  const symbol_exprt &symbol_expr,
391  bool is_assertions_disabled_field,
392  codet &c,
393  exprt::operandst &results);
394 
395  codet convert_putfield(const exprt &arg0, const exprt::operandst &op);
396 
398  const source_locationt &location,
399  const exprt &arg0,
400  const exprt::operandst &op,
401  const symbol_exprt &symbol_expr);
402 
403  void convert_new(
404  const source_locationt &location,
405  const exprt &arg0,
406  codet &c,
407  exprt::operandst &results);
408 
409  void convert_newarray(
410  const source_locationt &location,
411  const irep_idt &statement,
412  const exprt &arg0,
413  const exprt::operandst &op,
414  codet &c,
415  exprt::operandst &results);
416 
418  const source_locationt &location,
419  const exprt &arg0,
420  const exprt::operandst &op,
421  codet &c,
422  exprt::operandst &results);
423 
425  const methodt &method,
426  const std::set<unsigned int> &working_set,
427  unsigned int cur_pc,
428  codet &c);
429 
430  void convert_athrow(
431  const source_locationt &location,
432  const exprt::operandst &op,
433  codet &c,
434  exprt::operandst &results) const;
435 
436  void convert_checkcast(
437  const exprt &arg0,
438  const exprt::operandst &op,
439  codet &c,
440  exprt::operandst &results) const;
441 
443  const irep_idt &statement,
444  const exprt::operandst &op,
445  const source_locationt &source_location);
446 
448 
449  void convert_invoke(
450  source_locationt location,
451  const irep_idt &statement,
452  exprt &arg0,
453  codet &c,
454  exprt::operandst &results);
455 
457  const irep_idt &statement,
458  const exprt &arg0,
459  exprt::operandst &results) const;
460 
462 
464 
465  void convert_dup2(exprt::operandst &op, exprt::operandst &results);
466 
469  const exprt::operandst &op,
471  const source_locationt &location);
472 
473  codet convert_pop(const irep_idt &statement, const exprt::operandst &op);
474 };
475 #endif
The type of an expression.
Definition: type.h:22
void draw_edges_from_ret_to_jsr(address_mapt &address_map, const std::vector< unsigned int > &jsr_ret_targets, const std::vector< std::vector< java_bytecode_parse_treet::instructiont >::const_iterator > &ret_instructions) const
java_bytecode_convert_methodt::address_mapt address_mapt
codet convert_pop(const irep_idt &statement, const exprt::operandst &op)
methodt::local_variable_tablet local_variable_tablet
void pop_residue(std::size_t n)
removes minimum(n, stack.size()) elements from the stack
BigInt mp_integer
Definition: mp_arith.h:22
codet convert_putstatic(const source_locationt &location, const exprt &arg0, const exprt::operandst &op, const symbol_exprt &symbol_expr)
void convert(const symbolt &class_symbol, const methodt &)
codet convert_ifnull(const java_bytecode_convert_methodt::address_mapt &address_map, const exprt::operandst &op, const mp_integer &number, const source_locationt &location) const
std::vector< symbol_exprt > java_lambda_method_handlest
Definition: java_types.h:174
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.
void convert_athrow(const source_locationt &location, const exprt::operandst &op, codet &c, exprt::operandst &results) const
std::pair< const methodt &, const address_mapt & > method_with_amapt
irep_idt method_id
Fully qualified name of the method under translation.
const exprt variable(const exprt &arg, char type_char, size_t address, variable_cast_argumentt do_cast)
Returns a symbol_exprt indicating a local variable suitable to load/store from a bytecode at address ...
Non-graph-based representation of the class hierarchy.
optionalt< symbolt > get_lambda_method_symbol(const java_class_typet::java_lambda_method_handlest &lambda_method_handles, const size_t index)
Retrieves the symbol of the lambda method associated with the given lambda method handle (bootstrap m...
std::vector< instructiont > instructionst
void operator()(const symbolt &class_symbol, const methodt &method)
bool is_method_inherited(const irep_idt &classname, const irep_idt &methodid) const
Returns true iff method methodid from class classname is a method inherited from a class (and not an ...
optionalt< ci_lazy_methods_neededt > needed_lazy_methods
code_blockt & get_or_create_block_for_pcrange(block_tree_nodet &tree, code_blockt &this_block, unsigned address_start, unsigned address_limit, unsigned 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...
static irep_idt label(const irep_idt &address)
void setup_local_variables(const methodt &m, const address_mapt &amap)
See find_initializers_for_slot above for more detail.
STL namespace.
codet convert_iinc(const exprt &arg0, const exprt &arg1, const source_locationt &location, unsigned address)
Symbol table entry.This is a symbol in the symbol table, stored in an object of type symbol_tablet...
Definition: symbol.h:30
std::vector< unsigned int > try_catch_handler(unsigned int address, const java_bytecode_parse_treet::methodt::exception_tablet &exception_table) const
void convert_new(const source_locationt &location, const exprt &arg0, codet &c, exprt::operandst &results)
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...
std::map< unsigned, converted_instructiont > address_mapt
codet convert_switch(java_bytecode_convert_methodt::address_mapt &address_map, const exprt::operandst &op, const java_bytecode_parse_treet::instructiont::argst &args, const source_locationt &location)
code_blockt & get_block_for_pcrange(block_tree_nodet &tree, code_blockt &this_block, unsigned address_start, unsigned address_limit, unsigned next_block_start_address)
&#39;tree&#39; describes a tree of code_blockt objects; this_block is the corresponding block (thus they are ...
codet convert_putfield(const exprt &arg0, const exprt::operandst &op)
irep_idt current_method
A copy of method_id :/.
std::vector< local_variable_with_holest > local_variable_table_with_holest
exprt::operandst & convert_ushr(const irep_idt &statement, const exprt::operandst &op, exprt::operandst &results) const
exprt::operandst & convert_cmp2(const irep_idt &statement, const exprt::operandst &op, exprt::operandst &results) const
java_bytecode_parse_treet::methodt methodt
JAVA Bytecode Language Conversion.
void convert_multianewarray(const source_locationt &location, const exprt &arg0, const exprt::operandst &op, codet &c, exprt::operandst &results)
nonstd::optional< T > optionalt
Definition: optional.h:35
exprt::operandst & convert_cmp(const exprt::operandst &op, exprt::operandst &results) const
API to expression classes.
exprt::operandst & convert_const(const irep_idt &statement, const exprt &arg0, exprt::operandst &results) const
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...
codet convert_store(const irep_idt &statement, const exprt &arg0, const exprt::operandst &op, const unsigned address, const source_locationt &location)
The symbol table.
Definition: symbol_table.h:19
void convert_invoke(source_locationt location, const irep_idt &statement, exprt &arg0, codet &c, exprt::operandst &results)
java_string_library_preprocesst & string_preprocess
dstringt has one field, an unsigned integer no which is an index into a static table of strings...
Definition: dstring.h:33
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...
void convert_checkcast(const exprt &arg0, const exprt::operandst &op, codet &c, exprt::operandst &results) const
java_bytecode_parse_treet::instructiont instructiont
typet method_return_type
Return type of the method under conversion.
exprt::operandst pop(std::size_t n)
std::vector< exprt > operandst
Definition: expr.h:45
API to type classes.
const bytecode_infot & get_bytecode_info(const irep_idt &statement)
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
codet convert_monitorenterexit(const irep_idt &statement, const exprt::operandst &op, const source_locationt &source_location)
void convert_dup2_x1(exprt::operandst &op, exprt::operandst &results)
symbol_exprt tmp_variable(const std::string &prefix, const typet &type)
void convert_getstatic(const exprt &arg0, const symbol_exprt &symbol_expr, bool is_assertions_disabled_field, codet &c, exprt::operandst &results)
Base class for all expressions.
Definition: expr.h:42
The symbol table base class interface.
const variablet & find_variable_for_slot(size_t address, variablest &var_list)
See above.
std::vector< local_variablet > local_variable_tablet
void save_stack_entries(const std::string &, const typet &, code_blockt &, const bytecode_write_typet, const irep_idt &)
Create temporary variables if a write instruction can have undesired side- effects.
codet & replace_call_to_cprover_assume(source_locationt location, codet &c)
void push(const exprt::operandst &o)
Sequential composition.
Definition: std_code.h:89
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...
Context-insensitive lazy methods container.
codet & do_exception_handling(const methodt &method, const std::set< unsigned int > &working_set, unsigned int cur_pc, codet &c)
Expression to hold a symbol (variable)
Definition: std_expr.h:90
void convert_dup2_x2(exprt::operandst &op, exprt::operandst &results)
void convert_dup2(exprt::operandst &op, exprt::operandst &results)
A statement in a programming language.
Definition: std_code.h:21
unsigned slots_for_parameters
Number of local variable slots used by the JVM to pass parameters upon invocation of the method under...
converted_instructiont(const instructionst::const_iterator &it, const codet &_code)
codet convert_ifnonull(const java_bytecode_convert_methodt::address_mapt &address_map, const exprt::operandst &op, const mp_integer &number, const source_locationt &location) const
Compute dominators for CFG of goto_function.
codet 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
static void replace_goto_target(codet &repl, const irep_idt &old_label, const irep_idt &new_label)
Find all goto statements in &#39;repl&#39; that target &#39;old_label&#39; and redirect them to &#39;new_label&#39;.
optionalt< exprt > convert_invoke_dynamic(const java_class_typet::java_lambda_method_handlest &lambda_method_handles, const source_locationt &location, const exprt &arg0)
std::map< irep_idt, bool > any_superclass_has_clinit_method
void convert_newarray(const source_locationt &location, const irep_idt &statement, const exprt &arg0, const exprt::operandst &op, codet &c, exprt::operandst &results)
codet convert_astore(const irep_idt &statement, const exprt::operandst &op, const source_locationt &location)
cfg_dominators_templatet< method_with_amapt, unsigned, false > java_cfg_dominatorst
codet convert_if_cmp(const java_bytecode_convert_methodt::address_mapt &address_map, const irep_idt &statement, const exprt::operandst &op, const mp_integer &number, const source_locationt &location) const
exprt convert_aload(const irep_idt &statement, const exprt::operandst &op) const
code_blockt convert_ret(const std::vector< unsigned int > &jsr_ret_targets, const exprt &arg0, const source_locationt &location, const unsigned address)
codet convert_instructions(const methodt &, const java_class_typet::java_lambda_method_handlest &)
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)