cprover
java_bytecode_convert_method.cpp
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 #ifdef DEBUG
13 #include <iostream>
14 #endif
15 
16 #include "bytecode_info.h"
19 #include "java_expr.h"
23 #include "java_types.h"
24 #include "java_utils.h"
25 #include "lambda_synthesis.h"
26 #include "pattern.h"
27 #include "remove_exceptions.h"
28 
29 #include <util/arith_tools.h>
30 #include <util/c_types.h>
31 #include <util/expr_initializer.h>
32 #include <util/ieee_float.h>
33 #include <util/invariant.h>
34 #include <util/namespace.h>
35 #include <util/prefix.h>
36 #include <util/simplify_expr.h>
37 #include <util/std_expr.h>
38 #include <util/string2int.h>
39 #include <util/threeval.h>
40 
41 #include <goto-programs/cfg.h>
45 
48 
49 #include <algorithm>
50 #include <functional>
51 #include <limits>
52 #include <regex>
53 #include <unordered_set>
54 
68  java_method_typet &ftype,
69  const irep_idt &name_prefix,
70  symbol_table_baset &symbol_table)
71 {
72  java_method_typet::parameterst &parameters = ftype.parameters();
73 
74  // Mostly borrowed from java_bytecode_convert.cpp; maybe factor this out.
75  // assign names to parameters
76  for(std::size_t i=0; i<parameters.size(); ++i)
77  {
78  irep_idt base_name, identifier;
79 
80  if(i==0 && parameters[i].get_this())
81  base_name = ID_this;
82  else
83  base_name="stub_ignored_arg"+std::to_string(i);
84 
85  identifier=id2string(name_prefix)+"::"+id2string(base_name);
86  parameters[i].set_base_name(base_name);
87  parameters[i].set_identifier(identifier);
88 
89  // add to symbol table
90  parameter_symbolt parameter_symbol;
91  parameter_symbol.base_name=base_name;
92  parameter_symbol.mode=ID_java;
93  parameter_symbol.name=identifier;
94  parameter_symbol.type=parameters[i].type();
95  symbol_table.add(parameter_symbol);
96  }
97 }
98 
100  const irep_idt &identifier,
101  const irep_idt &base_name,
102  const irep_idt &pretty_name,
103  const typet &type,
104  const irep_idt &declaring_class,
105  symbol_table_baset &symbol_table,
106  message_handlert &message_handler)
107 {
108  messaget log(message_handler);
109 
110  symbolt symbol;
111  symbol.name = identifier;
112  symbol.base_name = base_name;
113  symbol.pretty_name = pretty_name;
114  symbol.type = type;
115  symbol.type.set(ID_access, ID_private);
116  to_java_method_type(symbol.type).set_is_final(true);
117  symbol.value.make_nil();
118  symbol.mode = ID_java;
120  to_java_method_type(symbol.type), symbol.name, symbol_table);
122 
123  log.debug() << "Generating codet: new opaque symbol: method '" << symbol.name
124  << "'" << messaget::eom;
125  symbol_table.add(symbol);
126 }
127 
128 static bool is_constructor(const irep_idt &method_name)
129 {
130  return id2string(method_name).find("<init>") != std::string::npos;
131 }
132 
134 {
135  if(stack.size()<n)
136  {
137  error() << "malformed bytecode (pop too high)" << eom;
138  throw 0;
139  }
140 
141  exprt::operandst operands;
142  operands.resize(n);
143  for(std::size_t i=0; i<n; i++)
144  operands[i]=stack[stack.size()-n+i];
145 
146  stack.resize(stack.size()-n);
147  return operands;
148 }
149 
152 {
153  std::size_t residue_size=std::min(n, stack.size());
154 
155  stack.resize(stack.size()-residue_size);
156 }
157 
159 {
160  stack.resize(stack.size()+o.size());
161 
162  for(std::size_t i=0; i<o.size(); i++)
163  stack[stack.size()-o.size()+i]=o[i];
164 }
165 
166 // JVM program locations
168 {
169  return "pc"+id2string(address);
170 }
171 
173  const std::string &prefix,
174  const typet &type)
175 {
176  irep_idt base_name=prefix+"_tmp"+std::to_string(tmp_vars.size());
177  irep_idt identifier=id2string(current_method)+"::"+id2string(base_name);
178 
179  auxiliary_symbolt tmp_symbol;
180  tmp_symbol.base_name=base_name;
181  tmp_symbol.is_static_lifetime=false;
182  tmp_symbol.mode=ID_java;
183  tmp_symbol.name=identifier;
184  tmp_symbol.type=type;
185  symbol_table.add(tmp_symbol);
186 
187  symbol_exprt result(identifier, type);
188  result.set(ID_C_base_name, base_name);
189  tmp_vars.push_back(result);
190 
191  return result;
192 }
193 
206  const exprt &arg,
207  char type_char,
208  size_t address)
209 {
210  const std::size_t number_int =
211  numeric_cast_v<std::size_t>(to_constant_expr(arg));
212  variablest &var_list=variables[number_int];
213 
214  // search variable in list for correct frame / address if necessary
215  const variablet &var=
216  find_variable_for_slot(address, var_list);
217 
218  if(!var.symbol_expr.get_identifier().empty())
219  return var.symbol_expr;
220 
221  // an unnamed local variable
222  irep_idt base_name = "anonlocal::" + std::to_string(number_int) + type_char;
223  irep_idt identifier = id2string(current_method) + "::" + id2string(base_name);
224 
225  symbol_exprt result(identifier, java_type_from_char(type_char));
226  result.set(ID_C_base_name, base_name);
227  used_local_names.insert(result);
228  return std::move(result);
229 }
230 
239  const std::string &descriptor,
240  const optionalt<std::string> &signature,
241  const std::string &class_name,
242  const std::string &method_name,
243  message_handlert &message_handler)
244 {
245  // In order to construct the method type, we can either use signature or
246  // descriptor. Since only signature contains the generics info, we want to
247  // use signature whenever possible. We revert to using descriptor if (1) the
248  // signature does not exist, (2) an unsupported generics are present or
249  // (3) in the special case when the number of parameters in the descriptor
250  // does not match the number of parameters in the signature - e.g. for
251  // certain types of inner classes and enums (see unit tests for examples).
252 
253  messaget message(message_handler);
254 
255  auto member_type_from_descriptor = java_type_from_string(descriptor);
256  INVARIANT(
257  member_type_from_descriptor.has_value() &&
258  member_type_from_descriptor->id() == ID_code,
259  "Must be code type");
260  if(signature.has_value())
261  {
262  try
263  {
264  auto member_type_from_signature =
265  java_type_from_string(signature.value(), class_name);
266  INVARIANT(
267  member_type_from_signature.has_value() &&
268  member_type_from_signature->id() == ID_code,
269  "Must be code type");
270  if(
271  to_java_method_type(*member_type_from_signature).parameters().size() ==
272  to_java_method_type(*member_type_from_descriptor).parameters().size())
273  {
274  return to_java_method_type(*member_type_from_signature);
275  }
276  else
277  {
278  message.debug() << "Method: " << class_name << "." << method_name
279  << "\n signature: " << signature.value()
280  << "\n descriptor: " << descriptor
281  << "\n different number of parameters, reverting to "
282  "descriptor"
283  << message.eom;
284  }
285  }
287  {
288  message.debug() << "Method: " << class_name << "." << method_name
289  << "\n could not parse signature: " << signature.value()
290  << "\n " << e.what() << "\n"
291  << " reverting to descriptor: " << descriptor
292  << message.eom;
293  }
294  }
295  return to_java_method_type(*member_type_from_descriptor);
296 }
297 
309  symbolt &class_symbol,
310  const irep_idt &method_identifier,
312  symbol_tablet &symbol_table,
313  message_handlert &message_handler)
314 {
315  symbolt method_symbol;
316 
317  java_method_typet member_type = member_type_lazy(
318  m.descriptor,
319  m.signature,
320  id2string(class_symbol.name),
321  id2string(m.base_name),
322  message_handler);
323 
324  method_symbol.name=method_identifier;
325  method_symbol.base_name=m.base_name;
326  method_symbol.mode=ID_java;
327  method_symbol.location=m.source_location;
328  method_symbol.location.set_function(method_identifier);
329 
330  if(m.is_public)
331  member_type.set_access(ID_public);
332  else if(m.is_protected)
333  member_type.set_access(ID_protected);
334  else if(m.is_private)
335  member_type.set_access(ID_private);
336  else
337  member_type.set_access(ID_default);
338 
339  if(m.is_synchronized)
340  member_type.set(ID_is_synchronized, true);
341  if(m.is_static)
342  member_type.set(ID_is_static, true);
343  member_type.set_native(m.is_native);
344  member_type.set_is_varargs(m.is_varargs);
345  member_type.set_is_synthetic(m.is_synthetic);
346 
347  if(m.is_bridge)
348  member_type.set(ID_is_bridge_method, m.is_bridge);
349 
350  // do we need to add 'this' as a parameter?
351  if(!m.is_static)
352  {
353  java_method_typet::parameterst &parameters = member_type.parameters();
354  const reference_typet object_ref_type =
356  java_method_typet::parametert this_p(object_ref_type);
357  this_p.set_this();
358  parameters.insert(parameters.begin(), this_p);
359  }
360 
361  const std::string signature_string = pretty_signature(member_type);
362 
363  if(is_constructor(method_symbol.base_name))
364  {
365  // we use full.class_name(...) as pretty name
366  // for constructors -- the idea is that they have
367  // an empty declarator.
368  method_symbol.pretty_name=
369  id2string(class_symbol.pretty_name) + signature_string;
370 
371  member_type.set_is_constructor();
372  }
373  else
374  {
375  method_symbol.pretty_name = id2string(class_symbol.pretty_name) + "." +
376  id2string(m.base_name) + signature_string;
377  }
378 
379  // Load annotations
380  if(!m.annotations.empty())
381  {
383  m.annotations,
384  type_checked_cast<annotated_typet>(static_cast<typet &>(member_type))
385  .get_annotations());
386  }
387 
388  method_symbol.type=member_type;
389  // Not used in jbmc at present, but other codebases that use jbmc as a library
390  // use this information.
391  method_symbol.type.set(ID_C_abstract, m.is_abstract);
392  set_declaring_class(method_symbol, class_symbol.name);
393 
395  m.annotations, "java::org.cprover.MustNotThrow"))
396  {
397  method_symbol.type.set(ID_C_must_not_throw, true);
398  }
399 
400  // Assign names to parameters in the method symbol
401  java_method_typet &method_type = to_java_method_type(method_symbol.type);
402  method_type.set_is_final(m.is_final);
403  java_method_typet::parameterst &parameters = method_type.parameters();
404  java_bytecode_convert_methodt::method_offsett slots_for_parameters =
405  java_method_parameter_slots(method_type);
407  m, method_identifier, parameters, slots_for_parameters);
408 
409  symbol_table.add(method_symbol);
410 
411  if(!m.is_static)
412  {
413  class_typet::methodt new_method;
414  new_method.set_name(method_symbol.name);
415  new_method.set_base_name(method_symbol.base_name);
416  new_method.set_pretty_name(method_symbol.pretty_name);
417  new_method.set_access(member_type.get_access());
418  new_method.type() = method_symbol.type;
419 
420  to_class_type(class_symbol.type)
421  .methods()
422  .emplace_back(std::move(new_method));
423  }
424 }
425 
427  const irep_idt &class_identifier,
429 {
430  return
431  id2string(class_identifier) + "." + id2string(method.name) + ":" +
432  method.descriptor;
433 }
434 
437  const irep_idt &method_identifier,
438  java_method_typet::parameterst &parameters,
439  const java_bytecode_convert_methodt::method_offsett &slots_for_parameters)
440 {
441  auto variables = variablest{};
442  // Find parameter names in the local variable table
443  // and store the result in the external variables vector
444  for(const auto &v : m.local_variable_table)
445  {
446  // Skip this variable if it is not a method parameter
447  if(v.index >= slots_for_parameters)
448  continue;
449 
450  std::ostringstream id_oss;
451  id_oss << method_identifier << "::" << v.name;
452  irep_idt identifier(id_oss.str());
453  symbol_exprt result = symbol_exprt::typeless(identifier);
454  result.set(ID_C_base_name, v.name);
455 
456  // Create a new variablet in the variables vector; in fact this entry will
457  // be rewritten below in the loop that iterates through the method
458  // parameters; the only field that seem to be useful to write here is the
459  // symbol_expr, others will be rewritten
460  variables[v.index].emplace_back(result, v.start_pc, v.length);
461  }
462 
463  // The variables is a expanding_vectort, and the loop above may have expanded
464  // the vector introducing gaps where the entries are empty vectors. We now
465  // make sure that the vector of each LV slot contains exactly one variablet,
466  // which we will add below
467  std::size_t param_index = 0;
468  for(const auto &param : parameters)
469  {
470  INVARIANT(
471  variables[param_index].size() <= 1,
472  "should have at most one entry per index");
473  param_index += java_local_variable_slots(param.type());
474  }
475  INVARIANT(
476  param_index == slots_for_parameters,
477  "java_parameter_count and local computation must agree");
478  param_index = 0;
479  for(auto &param : parameters)
480  {
481  irep_idt base_name, identifier;
482 
483  // Construct a sensible base name (base_name) and a fully qualified name
484  // (identifier) for every parameter of the method under translation,
485  // regardless of whether we have an LVT or not; and assign it to the
486  // parameter object (which is stored in the type of the symbol, not in the
487  // symbol table)
488  if(param_index == 0 && param.get_this())
489  {
490  // my.package.ClassName.myMethodName:(II)I::this
491  base_name = ID_this;
492  identifier = id2string(method_identifier) + "::" + id2string(base_name);
493  }
494  else if(!variables[param_index].empty())
495  {
496  // if already present in the LVT ...
497  base_name = variables[param_index][0].symbol_expr.get(ID_C_base_name);
498  identifier = variables[param_index][0].symbol_expr.get(ID_identifier);
499  }
500  else
501  {
502  // my.package.ClassName.myMethodName:(II)I::argNT, where N is the local
503  // variable slot where the parameter is stored and T is a character
504  // indicating the type
505  char suffix = java_char_from_type(param.type());
506  base_name = "arg" + std::to_string(param_index) + suffix;
507  identifier = id2string(method_identifier) + "::" + id2string(base_name);
508  }
509  param.set_base_name(base_name);
510  param.set_identifier(identifier);
511  param_index += java_local_variable_slots(param.type());
512  }
513  // The parameter slots detected in this function should agree with what
514  // java_parameter_count() thinks about this method
515  INVARIANT(
516  param_index == slots_for_parameters,
517  "java_parameter_count and local computation must agree");
518 }
519 
521  const java_method_typet::parameterst &parameters,
522  expanding_vectort<std::vector<java_bytecode_convert_methodt::variablet>>
523  &variables,
524  symbol_table_baset &symbol_table)
525 {
526  std::size_t param_index = 0;
527  for(const auto &param : parameters)
528  {
529  parameter_symbolt parameter_symbol;
530  parameter_symbol.base_name = param.get_base_name();
531  parameter_symbol.mode = ID_java;
532  parameter_symbol.name = param.get_identifier();
533  parameter_symbol.type = param.type();
534  symbol_table.add(parameter_symbol);
535 
536  // Add as a JVM local variable
537  variables[param_index].clear();
538  variables[param_index].emplace_back(
539  parameter_symbol.symbol_expr(),
540  0,
541  std::numeric_limits<size_t>::max(),
542  true);
543  param_index += java_local_variable_slots(param.type());
544  }
545 }
546 
548  const symbolt &class_symbol,
549  const methodt &m,
550  const optionalt<prefix_filtert> &method_context)
551 {
552  // Construct the fully qualified method name
553  // (e.g. "my.package.ClassName.myMethodName:(II)I") and query the symbol table
554  // to retrieve the symbol (constructed by java_bytecode_convert_method_lazy)
555  // associated to the method
556  const irep_idt method_identifier =
557  get_method_identifier(class_symbol.name, m);
558 
559  method_id = method_identifier;
561  symbol_table.get_writeable_ref(method_identifier), class_symbol.name);
562 
563  // Obtain a std::vector of java_method_typet::parametert objects from the
564  // (function) type of the symbol
565  // Don't try to hang on to this reference into the symbol table, as we're
566  // about to create symbols for the method's parameters, which would invalidate
567  // the reference. Instead, copy the type here, set it up, then assign it back
568  // to the symbol later.
569  java_method_typet method_type =
570  to_java_method_type(symbol_table.lookup_ref(method_identifier).type);
571  method_type.set_is_final(m.is_final);
572  method_return_type = method_type.return_type();
573  java_method_typet::parameterst &parameters = method_type.parameters();
574 
575  // Determine the number of local variable slots used by the JVM to maintain
576  // the formal parameters
578 
579  debug() << "Generating codet: class " << class_symbol.name << ", method "
580  << m.name << eom;
581 
582  // Add parameter symbols to the symbol table
584 
585  symbolt &method_symbol = symbol_table.get_writeable_ref(method_identifier);
586 
587  // Check the fields that can't change are valid
588  INVARIANT(
589  method_symbol.name == method_identifier,
590  "Name of method symbol shouldn't change");
591  INVARIANT(
592  method_symbol.base_name == m.base_name,
593  "Base name of method symbol shouldn't change");
594  INVARIANT(
595  method_symbol.module.empty(),
596  "Method symbol shouldn't have module");
597  // Update the symbol for the method
598  method_symbol.mode=ID_java;
599  method_symbol.location=m.source_location;
600  method_symbol.location.set_function(method_identifier);
601 
602  for(const auto &exception_name : m.throws_exception_table)
603  method_type.add_throws_exception(exception_name);
604 
605  const std::string signature_string = pretty_signature(method_type);
606 
607  // Set up the pretty name for the method entry in the symbol table.
608  // The pretty name of a constructor includes the base name of the class
609  // instead of the internal method name "<init>". For regular methods, it's
610  // just the base name of the method.
611  if(is_constructor(method_symbol.base_name))
612  {
613  // we use full.class_name(...) as pretty name
614  // for constructors -- the idea is that they have
615  // an empty declarator.
616  method_symbol.pretty_name =
617  id2string(class_symbol.pretty_name) + signature_string;
618  INVARIANT(
619  method_type.get_is_constructor(),
620  "Member type should have already been marked as a constructor");
621  }
622  else
623  {
624  method_symbol.pretty_name = id2string(class_symbol.pretty_name) + "." +
625  id2string(m.base_name) + signature_string;
626  }
627 
628  method_symbol.type = method_type;
629  current_method = method_symbol.name;
630  method_has_this = method_type.has_this();
631  if((!m.is_abstract) && (!m.is_native))
632  {
633  // Do not convert if method is not in context
634  if(!method_context || (*method_context)(id2string(method_identifier)))
635  {
636  code_blockt code{convert_parameter_annotations(m, method_type)};
637  code.append(convert_instructions(m));
638  method_symbol.value = std::move(code);
639  }
640  }
641 }
642 
643 static irep_idt get_if_cmp_operator(const u1 bytecode)
644 {
645  if(bytecode == patternt("if_?cmplt"))
646  return ID_lt;
647  if(bytecode == patternt("if_?cmple"))
648  return ID_le;
649  if(bytecode == patternt("if_?cmpgt"))
650  return ID_gt;
651  if(bytecode == patternt("if_?cmpge"))
652  return ID_ge;
653  if(bytecode == patternt("if_?cmpeq"))
654  return ID_equal;
655  if(bytecode == patternt("if_?cmpne"))
656  return ID_notequal;
657 
658  throw "unhandled java comparison instruction";
659 }
660 
670  const exprt &pointer,
671  const fieldref_exprt &field_reference,
672  const namespacet &ns)
673 {
674  struct_tag_typet class_type(field_reference.class_name());
675 
676  const exprt typed_pointer =
678 
679  const irep_idt &component_name = field_reference.component_name();
680 
681  exprt accessed_object = checked_dereference(typed_pointer);
682  const auto type_of = [&ns](const exprt &object) {
683  return to_struct_type(ns.follow(object.type()));
684  };
685 
686  // The field access is described as being against a particular type, but it
687  // may in fact belong to any ancestor type.
688  while(type_of(accessed_object).get_component(component_name).is_nil())
689  {
690  const auto components = type_of(accessed_object).components();
691  INVARIANT(
692  components.size() != 0,
693  "infer_opaque_type_fields should guarantee that a member access has a "
694  "corresponding field");
695 
696  // Base class access is always done through the first component
697  accessed_object = member_exprt(accessed_object, components.front());
698  }
699  return member_exprt(
700  accessed_object, type_of(accessed_object).get_component(component_name));
701 }
702 
709  codet &repl,
710  const irep_idt &old_label,
711  const irep_idt &new_label)
712 {
713  const auto &stmt=repl.get_statement();
714  if(stmt==ID_goto)
715  {
716  auto &g=to_code_goto(repl);
717  if(g.get_destination()==old_label)
718  g.set_destination(new_label);
719  }
720  else
721  {
722  for(auto &op : repl.operands())
723  if(op.id()==ID_code)
724  replace_goto_target(to_code(op), old_label, new_label);
725  }
726 }
727 
743  block_tree_nodet &tree,
744  code_blockt &this_block,
745  method_offsett address_start,
746  method_offsett address_limit,
747  method_offsett next_block_start_address)
748 {
749  address_mapt dummy;
751  tree,
752  this_block,
753  address_start,
754  address_limit,
755  next_block_start_address,
756  dummy,
757  false);
758 }
759 
780  block_tree_nodet &tree,
781  code_blockt &this_block,
782  method_offsett address_start,
783  method_offsett address_limit,
784  method_offsett next_block_start_address,
785  const address_mapt &amap,
786  bool allow_merge)
787 {
788  // Check the tree shape invariant:
789  assert(tree.branch.size()==tree.branch_addresses.size());
790 
791  // If there are no child blocks, return this.
792  if(tree.leaf)
793  return this_block;
794  assert(!tree.branch.empty());
795 
796  // Find child block starting > address_start:
797  const auto afterstart=
798  std::upper_bound(
799  tree.branch_addresses.begin(),
800  tree.branch_addresses.end(),
801  address_start);
802  assert(afterstart!=tree.branch_addresses.begin());
803  auto findstart=afterstart;
804  --findstart;
805  auto child_offset=
806  std::distance(tree.branch_addresses.begin(), findstart);
807 
808  // Find child block starting >= address_limit:
809  auto findlim=
810  std::lower_bound(
811  tree.branch_addresses.begin(),
812  tree.branch_addresses.end(),
813  address_limit);
814  const auto findlim_block_start_address =
815  findlim == tree.branch_addresses.end() ? next_block_start_address
816  : (*findlim);
817 
818  // If all children are in scope, return this.
819  if(findstart==tree.branch_addresses.begin() &&
820  findlim==tree.branch_addresses.end())
821  return this_block;
822 
823  // Find the child code_blockt where the queried range begins:
824  auto child_iter = this_block.statements().begin();
825  // Skip any top-of-block declarations;
826  // all other children are labelled subblocks.
827  while(child_iter != this_block.statements().end() &&
828  child_iter->get_statement() == ID_decl)
829  ++child_iter;
830  assert(child_iter != this_block.statements().end());
831  std::advance(child_iter, child_offset);
832  assert(child_iter != this_block.statements().end());
833  auto &child_label=to_code_label(*child_iter);
834  auto &child_block=to_code_block(child_label.code());
835 
836  bool single_child(afterstart==findlim);
837  if(single_child)
838  {
839  // Range wholly contained within a child block
841  tree.branch[child_offset],
842  child_block,
843  address_start,
844  address_limit,
845  findlim_block_start_address,
846  amap,
847  allow_merge);
848  }
849 
850  // Otherwise we're being asked for a range of subblocks, but not all of them.
851  // If it's legal to draw a new lexical scope around the requested subset,
852  // do so; otherwise just return this block.
853 
854  // This can be a new lexical scope if all incoming edges target the
855  // new block header, or come from within the suggested new block.
856 
857  // If modifying the block tree is forbidden, give up and return this:
858  if(!allow_merge)
859  return this_block;
860 
861  // Check for incoming control-flow edges targeting non-header
862  // blocks of the new proposed block range:
863  auto checkit=amap.find(*findstart);
864  assert(checkit!=amap.end());
865  ++checkit; // Skip the header, which can have incoming edges from outside.
866  for(;
867  checkit!=amap.end() && (checkit->first)<(findlim_block_start_address);
868  ++checkit)
869  {
870  for(auto p : checkit->second.predecessors)
871  {
872  if(p<(*findstart) || p>=findlim_block_start_address)
873  {
874  debug() << "Generating codet: "
875  << "warning: refusing to create lexical block spanning "
876  << (*findstart) << "-" << findlim_block_start_address
877  << " due to incoming edge " << p << " -> "
878  << checkit->first << eom;
879  return this_block;
880  }
881  }
882  }
883 
884  // All incoming edges are acceptable! Create a new block wrapping
885  // the relevant children. Borrow the header block's label, and redirect
886  // any block-internal edges to target the inner header block.
887 
888  const irep_idt child_label_name=child_label.get_label();
889  std::string new_label_str = id2string(child_label_name);
890  new_label_str+='$';
891  irep_idt new_label_irep(new_label_str);
892 
893  code_labelt newlabel(child_label_name, code_blockt());
894  code_blockt &newblock=to_code_block(newlabel.code());
895  auto nblocks=std::distance(findstart, findlim);
896  assert(nblocks>=2);
897  debug() << "Generating codet: combining "
898  << std::distance(findstart, findlim)
899  << " blocks for addresses " << (*findstart) << "-"
900  << findlim_block_start_address << eom;
901 
902  // Make a new block containing every child of interest:
903  auto &this_block_children = this_block.statements();
904  assert(tree.branch.size()==this_block_children.size());
905  for(auto blockidx=child_offset, blocklim=child_offset+nblocks;
906  blockidx!=blocklim;
907  ++blockidx)
908  newblock.add(this_block_children[blockidx]);
909 
910  // Relabel the inner header:
911  to_code_label(newblock.statements()[0]).set_label(new_label_irep);
912  // Relabel internal gotos:
913  replace_goto_target(newblock, child_label_name, new_label_irep);
914 
915  // Remove the now-empty sibling blocks:
916  auto delfirst=this_block_children.begin();
917  std::advance(delfirst, child_offset+1);
918  auto dellim=delfirst;
919  std::advance(dellim, nblocks-1);
920  this_block_children.erase(delfirst, dellim);
921  this_block_children[child_offset].swap(newlabel);
922 
923  // Perform the same transformation on the index tree:
924  block_tree_nodet newnode;
925  auto branchstart=tree.branch.begin();
926  std::advance(branchstart, child_offset);
927  auto branchlim=branchstart;
928  std::advance(branchlim, nblocks);
929  for(auto branchiter=branchstart; branchiter!=branchlim; ++branchiter)
930  newnode.branch.push_back(std::move(*branchiter));
931  ++branchstart;
932  tree.branch.erase(branchstart, branchlim);
933 
934  assert(tree.branch.size()==this_block_children.size());
935 
936  auto branchaddriter=tree.branch_addresses.begin();
937  std::advance(branchaddriter, child_offset);
938  auto branchaddrlim=branchaddriter;
939  std::advance(branchaddrlim, nblocks);
940  newnode.branch_addresses.insert(
941  newnode.branch_addresses.begin(),
942  branchaddriter,
943  branchaddrlim);
944 
945  ++branchaddriter;
946  tree.branch_addresses.erase(branchaddriter, branchaddrlim);
947 
948  tree.branch[child_offset]=std::move(newnode);
949 
950  assert(tree.branch.size()==tree.branch_addresses.size());
951 
952  return
955  this_block_children[child_offset]).code());
956 }
957 
960  const exprt &e,
961  std::map<irep_idt, java_bytecode_convert_methodt::variablet> &result)
962 {
963  if(e.id()==ID_symbol)
964  {
965  const auto &symexpr=to_symbol_expr(e);
966  auto findit = result.emplace(
967  std::piecewise_construct,
968  std::forward_as_tuple(symexpr.get_identifier()),
969  std::forward_as_tuple(symexpr, pc, 1));
970  if(!findit.second)
971  {
972  auto &var = findit.first->second;
973 
974  if(pc<var.start_pc)
975  {
976  var.length+=(var.start_pc-pc);
977  var.start_pc=pc;
978  }
979  else
980  {
981  var.length=std::max(var.length, (pc-var.start_pc)+1);
982  }
983  }
984  }
985  else
986  {
987  forall_operands(it, e)
988  gather_symbol_live_ranges(pc, *it, result);
989  }
990 }
991 
998  const irep_idt &classname)
999 {
1000  auto findit = symbol_table.symbols.find(clinit_wrapper_name(classname));
1001  if(findit == symbol_table.symbols.end())
1002  return code_skipt();
1003  else
1004  {
1005  const code_function_callt ret(findit->second.symbol_expr());
1007  needed_lazy_methods->add_needed_method(findit->second.name);
1008  return ret;
1009  }
1010 }
1011 
1012 static std::size_t get_bytecode_type_width(const typet &ty)
1013 {
1014  if(ty.id()==ID_pointer)
1015  return 32;
1016  return to_bitvector_type(ty).get_width();
1017 }
1018 
1020  const methodt &method,
1021  const java_method_typet &method_type)
1022 {
1023  code_blockt code;
1024 
1025  // Consider parameter annotations
1026  const java_method_typet::parameterst &parameters(method_type.parameters());
1027  std::size_t param_index = method_type.has_this() ? 1 : 0;
1029  parameters.size() >= method.parameter_annotations.size() + param_index,
1030  "parameters and parameter annotations mismatch");
1031  for(const auto &param_annotations : method.parameter_annotations)
1032  {
1033  // NotNull annotations are not standardized. We support these:
1034  if(
1036  param_annotations, "java::javax.validation.constraints.NotNull") ||
1038  param_annotations, "java::org.jetbrains.annotations.NotNull") ||
1040  param_annotations, "org.eclipse.jdt.annotation.NonNull") ||
1042  param_annotations, "java::edu.umd.cs.findbugs.annotations.NonNull"))
1043  {
1044  const irep_idt &param_identifier =
1045  parameters[param_index].get_identifier();
1046  const symbolt &param_symbol = symbol_table.lookup_ref(param_identifier);
1047  const auto param_type =
1048  type_try_dynamic_cast<pointer_typet>(param_symbol.type);
1049  if(param_type)
1050  {
1051  code_assertt code_assert(notequal_exprt(
1052  param_symbol.symbol_expr(), null_pointer_exprt(*param_type)));
1053  source_locationt check_loc = method.source_location;
1054  check_loc.set_comment("Not null annotation check");
1055  check_loc.set_property_class("not-null-annotation-check");
1056  code_assert.add_source_location() = check_loc;
1057 
1058  code.add(std::move(code_assert));
1059  }
1060  }
1061  ++param_index;
1062  }
1063 
1064  return code;
1065 }
1066 
1069 {
1070  const instructionst &instructions=method.instructions;
1071 
1072  // Run a worklist algorithm, assuming that the bytecode has not
1073  // been tampered with. See "Leroy, X. (2003). Java bytecode
1074  // verification: algorithms and formalizations. Journal of Automated
1075  // Reasoning, 30(3-4), 235-269." for a more complete treatment.
1076 
1077  // first pass: get targets and map addresses to instructions
1078 
1079  address_mapt address_map;
1080  std::set<method_offsett> targets;
1081 
1082  std::vector<method_offsett> jsr_ret_targets;
1083  std::vector<instructionst::const_iterator> ret_instructions;
1084 
1085  for(auto i_it = instructions.begin(); i_it != instructions.end(); i_it++)
1086  {
1088  std::pair<address_mapt::iterator, bool> a_entry=
1089  address_map.insert(std::make_pair(i_it->address, ins));
1090  assert(a_entry.second);
1091  // addresses are strictly increasing, hence we must have inserted
1092  // a new maximal key
1093  assert(a_entry.first==--address_map.end());
1094 
1095  const auto bytecode = i_it->bytecode;
1096  const std::string statement = bytecode_info[i_it->bytecode].mnemonic;
1097 
1098  // clang-format off
1099  if(bytecode != BC_goto &&
1100  bytecode != BC_return &&
1101  bytecode != patternt("?return") &&
1102  bytecode != BC_athrow &&
1103  bytecode != BC_jsr &&
1104  bytecode != BC_jsr_w &&
1105  bytecode != BC_ret)
1106  {
1107  // clang-format on
1108  instructionst::const_iterator next=i_it;
1109  if(++next!=instructions.end())
1110  a_entry.first->second.successors.push_back(next->address);
1111  }
1112 
1113  // clang-format off
1114  if(bytecode == BC_athrow ||
1115  bytecode == BC_putfield ||
1116  bytecode == BC_getfield ||
1117  bytecode == BC_checkcast ||
1118  bytecode == BC_newarray ||
1119  bytecode == BC_anewarray ||
1120  bytecode == BC_idiv ||
1121  bytecode == BC_ldiv ||
1122  bytecode == BC_irem ||
1123  bytecode == BC_lrem ||
1124  bytecode == patternt("?astore") ||
1125  bytecode == patternt("?aload") ||
1126  bytecode == BC_invokestatic ||
1127  bytecode == BC_invokevirtual ||
1128  bytecode == BC_invokespecial ||
1129  bytecode == BC_invokeinterface ||
1130  (threading_support &&
1131  (bytecode == BC_monitorenter || bytecode == BC_monitorexit)))
1132  {
1133  // clang-format on
1134  const std::vector<method_offsett> handler =
1135  try_catch_handler(i_it->address, method.exception_table);
1136  std::list<method_offsett> &successors = a_entry.first->second.successors;
1137  successors.insert(successors.end(), handler.begin(), handler.end());
1138  targets.insert(handler.begin(), handler.end());
1139  }
1140 
1141  // clang-format off
1142  if(bytecode == BC_goto ||
1143  bytecode == patternt("if_?cmp??") ||
1144  bytecode == patternt("if??") ||
1145  bytecode == BC_ifnonnull ||
1146  bytecode == BC_ifnull ||
1147  bytecode == BC_jsr ||
1148  bytecode == BC_jsr_w)
1149  {
1150  // clang-format on
1151  PRECONDITION(!i_it->args.empty());
1152 
1153  auto target = numeric_cast_v<unsigned>(to_constant_expr(i_it->args[0]));
1154  targets.insert(target);
1155 
1156  a_entry.first->second.successors.push_back(target);
1157 
1158  if(bytecode == BC_jsr || bytecode == BC_jsr_w)
1159  {
1160  auto next = std::next(i_it);
1162  next != instructions.end(), "jsr should have valid return address");
1163  targets.insert(next->address);
1164  jsr_ret_targets.push_back(next->address);
1165  }
1166  }
1167  else if(bytecode == BC_tableswitch || bytecode == BC_lookupswitch)
1168  {
1169  bool is_label=true;
1170  for(const auto &arg : i_it->args)
1171  {
1172  if(is_label)
1173  {
1174  auto target = numeric_cast_v<unsigned>(to_constant_expr(arg));
1175  targets.insert(target);
1176  a_entry.first->second.successors.push_back(target);
1177  }
1178  is_label=!is_label;
1179  }
1180  }
1181  else if(bytecode == BC_ret)
1182  {
1183  // Finish these later, once we've seen all jsr instructions.
1184  ret_instructions.push_back(i_it);
1185  }
1186  }
1187  draw_edges_from_ret_to_jsr(address_map, jsr_ret_targets, ret_instructions);
1188 
1189  for(const auto &address : address_map)
1190  {
1191  for(auto s : address.second.successors)
1192  {
1193  const auto a_it = address_map.find(s);
1194  CHECK_RETURN(a_it != address_map.end());
1195  a_it->second.predecessors.insert(address.first);
1196  }
1197  }
1198 
1199  // Clean the list of temporary variables created by a call to `tmp_variable`.
1200  // These are local variables in the goto function used to represent temporary
1201  // values of the JVM operand stack, newly allocated objects before the
1202  // constructor is called, ...
1203  tmp_vars.clear();
1204 
1205  // Now that the control flow graph is built, set up our local variables
1206  // (these require the graph to determine live ranges)
1207  setup_local_variables(method, address_map);
1208 
1209  std::set<method_offsett> working_set;
1210 
1211  if(!instructions.empty())
1212  working_set.insert(instructions.front().address);
1213 
1214  while(!working_set.empty())
1215  {
1216  auto cur = working_set.begin();
1217  auto address_it = address_map.find(*cur);
1218  CHECK_RETURN(address_it != address_map.end());
1219  auto &instruction = address_it->second;
1220  const method_offsett cur_pc = *cur;
1221  working_set.erase(cur);
1222 
1223  if(instruction.done)
1224  continue;
1225  working_set.insert(
1226  instruction.successors.begin(), instruction.successors.end());
1227 
1228  instructionst::const_iterator i_it = instruction.source;
1229  stack.swap(instruction.stack);
1230  instruction.stack.clear();
1231  codet &c = instruction.code;
1232 
1233  assert(
1234  stack.empty() || instruction.predecessors.size() <= 1 ||
1235  has_prefix(stack.front().get_string(ID_C_base_name), "$stack"));
1236 
1237  exprt arg0=i_it->args.size()>=1?i_it->args[0]:nil_exprt();
1238  exprt arg1=i_it->args.size()>=2?i_it->args[1]:nil_exprt();
1239 
1240  const auto bytecode = i_it->bytecode;
1241  const bytecode_infot &stmt_bytecode_info = bytecode_info[i_it->bytecode];
1242  const std::string statement = stmt_bytecode_info.mnemonic;
1243 
1244  // deal with _idx suffixes
1245  if(statement.size()>=2 &&
1246  statement[statement.size()-2]=='_' &&
1247  isdigit(statement[statement.size()-1]))
1248  {
1249  arg0=
1250  from_integer(
1252  std::string(id2string(statement), statement.size()-1, 1)),
1253  java_int_type());
1254  }
1255 
1256  typet catch_type;
1257 
1258  // Find catch blocks that begin here. For now we assume if more than
1259  // one catch targets the same bytecode then we must be indifferent to
1260  // its type and just call it a Throwable.
1261  auto it=method.exception_table.begin();
1262  for(; it!=method.exception_table.end(); ++it)
1263  {
1264  if(cur_pc==it->handler_pc)
1265  {
1266  if(
1267  catch_type != typet() ||
1268  it->catch_type == struct_tag_typet(irep_idt()))
1269  {
1270  catch_type = struct_tag_typet("java::java.lang.Throwable");
1271  break;
1272  }
1273  else
1274  catch_type=it->catch_type;
1275  }
1276  }
1277 
1278  optionalt<codet> catch_instruction;
1279 
1280  if(catch_type!=typet())
1281  {
1282  // at the beginning of a handler, clear the stack and
1283  // push the corresponding exceptional return variable
1284  // We also create a catch exception instruction that
1285  // precedes the catch block, and which remove_exceptionst
1286  // will transform into something like:
1287  // catch_var = GLOBAL_THROWN_EXCEPTION;
1288  // GLOBAL_THROWN_EXCEPTION = null;
1289  stack.clear();
1290  symbol_exprt catch_var=
1291  tmp_variable(
1292  "caught_exception",
1293  java_reference_type(catch_type));
1294  stack.push_back(catch_var);
1295  catch_instruction = code_landingpadt(catch_var);
1296  }
1297 
1298  exprt::operandst op = pop(stmt_bytecode_info.pop);
1299  exprt::operandst results;
1300  results.resize(stmt_bytecode_info.push, nil_exprt());
1301 
1302  if(bytecode == BC_aconst_null)
1303  {
1304  assert(results.size()==1);
1306  }
1307  else if(bytecode == BC_athrow)
1308  {
1309  PRECONDITION(op.size() == 1 && results.size() == 1);
1310  convert_athrow(i_it->source_location, op, c, results);
1311  }
1312  else if(bytecode == BC_checkcast)
1313  {
1314  // checkcast throws an exception in case a cast of object
1315  // on stack to given type fails.
1316  // The stack isn't modified.
1317  PRECONDITION(op.size() == 1 && results.size() == 1);
1318  convert_checkcast(arg0, op, c, results);
1319  }
1320  else if(bytecode == BC_invokedynamic)
1321  {
1322  if(
1323  const auto res =
1324  convert_invoke_dynamic(i_it->source_location, i_it->address, arg0, c))
1325  {
1326  results.resize(1);
1327  results[0] = *res;
1328  }
1329  }
1330  else if(
1331  bytecode == BC_invokestatic && id2string(arg0.get(ID_identifier)) ==
1332  "java::org.cprover.CProver.assume:(Z)V")
1333  {
1334  const java_method_typet &method_type = to_java_method_type(arg0.type());
1335  INVARIANT(
1336  method_type.parameters().size() == 1,
1337  "function expected to have exactly one parameter");
1338  c = replace_call_to_cprover_assume(i_it->source_location, c);
1339  }
1340  // replace calls to CProver.atomicBegin
1341  else if(
1342  bytecode == BC_invokestatic &&
1343  arg0.get(ID_identifier) == "java::org.cprover.CProver.atomicBegin:()V")
1344  {
1345  c = codet(ID_atomic_begin);
1346  }
1347  // replace calls to CProver.atomicEnd
1348  else if(
1349  bytecode == BC_invokestatic &&
1350  arg0.get(ID_identifier) == "java::org.cprover.CProver.atomicEnd:()V")
1351  {
1352  c = codet(ID_atomic_end);
1353  }
1354  else if(
1355  bytecode == BC_invokeinterface || bytecode == BC_invokespecial ||
1356  bytecode == BC_invokevirtual || bytecode == BC_invokestatic)
1357  {
1358  class_method_descriptor_exprt *class_method_descriptor =
1359  expr_try_dynamic_cast<class_method_descriptor_exprt>(arg0);
1360 
1361  INVARIANT(
1362  class_method_descriptor,
1363  "invokeinterface, invokespecial, invokevirtual and invokestatic should "
1364  "be called with a class method descriptor expression as arg0");
1365 
1367  i_it->source_location, statement, *class_method_descriptor, c, results);
1368  }
1369  else if(bytecode == BC_return)
1370  {
1371  PRECONDITION(op.empty() && results.empty());
1372  c=code_returnt();
1373  }
1374  else if(bytecode == patternt("?return"))
1375  {
1376  // Return types are promoted in java, so this might need
1377  // conversion.
1378  PRECONDITION(op.size() == 1 && results.empty());
1379  const exprt r =
1381  c=code_returnt(r);
1382  }
1383  else if(bytecode == patternt("?astore"))
1384  {
1385  PRECONDITION(results.empty());
1386  c = convert_astore(statement, op, i_it->source_location);
1387  }
1388  else if(bytecode == patternt("?store") || bytecode == patternt("?store_?"))
1389  {
1390  // store value into some local variable
1391  PRECONDITION(op.size() == 1 && results.empty());
1392  c = convert_store(
1393  statement, arg0, op, i_it->address, i_it->source_location);
1394  }
1395  else if(bytecode == patternt("?aload"))
1396  {
1397  PRECONDITION(results.size() == 1);
1398  results[0] = convert_aload(statement, op);
1399  }
1400  else if(bytecode == patternt("?load") || bytecode == patternt("?load_?"))
1401  {
1402  // load a value from a local variable
1403  results[0] = convert_load(arg0, statement[0], i_it->address);
1404  }
1405  else if(bytecode == BC_ldc || bytecode == BC_ldc_w || bytecode == BC_ldc2_w)
1406  {
1407  PRECONDITION(op.empty() && results.size() == 1);
1408 
1409  INVARIANT(
1410  !can_cast_expr<java_string_literal_exprt>(arg0) && arg0.id() != ID_type,
1411  "String and Class literals should have been lowered in "
1412  "generate_constant_global_variables");
1413 
1414  results[0] = arg0;
1415  }
1416  else if(bytecode == BC_goto || bytecode == BC_goto_w)
1417  {
1418  PRECONDITION(op.empty() && results.empty());
1419  const mp_integer number =
1420  numeric_cast_v<mp_integer>(to_constant_expr(arg0));
1421  code_gotot code_goto(label(integer2string(number)));
1422  c=code_goto;
1423  }
1424  else if(bytecode == BC_jsr || bytecode == BC_jsr_w)
1425  {
1426  // As 'goto', except we must also push the subroutine return address:
1427  PRECONDITION(op.empty() && results.size() == 1);
1428  const mp_integer number =
1429  numeric_cast_v<mp_integer>(to_constant_expr(arg0));
1430  code_gotot code_goto(label(integer2string(number)));
1431  c=code_goto;
1432  results[0]=
1433  from_integer(
1434  std::next(i_it)->address,
1435  unsignedbv_typet(64));
1436  results[0].type() = pointer_type(java_void_type());
1437  }
1438  else if(bytecode == BC_ret)
1439  {
1440  // Since we have a bounded target set, make life easier on our analyses
1441  // and write something like:
1442  // if(retaddr==5) goto 5; else if(retaddr==10) goto 10; ...
1443  PRECONDITION(op.empty() && results.empty());
1444  assert(!jsr_ret_targets.empty());
1445  c = convert_ret(
1446  jsr_ret_targets, arg0, i_it->source_location, i_it->address);
1447  }
1448  else if(bytecode == BC_iconst_m1)
1449  {
1450  assert(results.size()==1);
1451  results[0]=from_integer(-1, java_int_type());
1452  }
1453  else if(bytecode == patternt("?const_?"))
1454  {
1455  assert(results.size()==1);
1456  results = convert_const(statement, to_constant_expr(arg0), results);
1457  }
1458  else if(bytecode == patternt("?ipush"))
1459  {
1460  PRECONDITION(results.size()==1);
1462  arg0.id()==ID_constant,
1463  "ipush argument expected to be constant");
1464  results[0] = typecast_exprt::conditional_cast(arg0, java_int_type());
1465  }
1466  else if(bytecode == patternt("if_?cmp??"))
1467  {
1468  PRECONDITION(op.size() == 2 && results.empty());
1469  const mp_integer number =
1470  numeric_cast_v<mp_integer>(to_constant_expr(arg0));
1471  c = convert_if_cmp(
1472  address_map, bytecode, op, number, i_it->source_location);
1473  }
1474  else if(bytecode == patternt("if??"))
1475  {
1476  // clang-format off
1477  const irep_idt id=
1478  bytecode == BC_ifeq ? ID_equal :
1479  bytecode == BC_ifne ? ID_notequal :
1480  bytecode == BC_iflt ? ID_lt :
1481  bytecode == BC_ifge ? ID_ge :
1482  bytecode == BC_ifgt ? ID_gt :
1483  bytecode == BC_ifle ? ID_le :
1484  irep_idt();
1485  // clang-format on
1486 
1487  INVARIANT(!id.empty(), "unexpected bytecode-if");
1488  PRECONDITION(op.size() == 1 && results.empty());
1489  const mp_integer number =
1490  numeric_cast_v<mp_integer>(to_constant_expr(arg0));
1491  c = convert_if(address_map, op, id, number, i_it->source_location);
1492  }
1493  else if(bytecode == patternt("ifnonnull"))
1494  {
1495  PRECONDITION(op.size() == 1 && results.empty());
1496  const mp_integer number =
1497  numeric_cast_v<mp_integer>(to_constant_expr(arg0));
1498  c = convert_ifnonull(address_map, op, number, i_it->source_location);
1499  }
1500  else if(bytecode == patternt("ifnull"))
1501  {
1502  PRECONDITION(op.size() == 1 && results.empty());
1503  const mp_integer number =
1504  numeric_cast_v<mp_integer>(to_constant_expr(arg0));
1505  c = convert_ifnull(address_map, op, number, i_it->source_location);
1506  }
1507  else if(bytecode == BC_iinc)
1508  {
1509  c = convert_iinc(arg0, arg1, i_it->source_location, i_it->address);
1510  }
1511  else if(bytecode == patternt("?xor"))
1512  {
1513  PRECONDITION(op.size() == 2 && results.size() == 1);
1514  results[0]=bitxor_exprt(op[0], op[1]);
1515  }
1516  else if(bytecode == patternt("?or"))
1517  {
1518  PRECONDITION(op.size() == 2 && results.size() == 1);
1519  results[0]=bitor_exprt(op[0], op[1]);
1520  }
1521  else if(bytecode == patternt("?and"))
1522  {
1523  PRECONDITION(op.size() == 2 && results.size() == 1);
1524  results[0]=bitand_exprt(op[0], op[1]);
1525  }
1526  else if(bytecode == patternt("?shl"))
1527  {
1528  PRECONDITION(op.size() == 2 && results.size() == 1);
1529  results[0]=shl_exprt(op[0], op[1]);
1530  }
1531  else if(bytecode == patternt("?shr"))
1532  {
1533  PRECONDITION(op.size() == 2 && results.size() == 1);
1534  results[0]=ashr_exprt(op[0], op[1]);
1535  }
1536  else if(bytecode == patternt("?ushr"))
1537  {
1538  PRECONDITION(op.size() == 2 && results.size() == 1);
1539  results = convert_ushr(statement, op, results);
1540  }
1541  else if(bytecode == patternt("?add"))
1542  {
1543  PRECONDITION(op.size() == 2 && results.size() == 1);
1544  results[0]=plus_exprt(op[0], op[1]);
1545  }
1546  else if(bytecode == patternt("?sub"))
1547  {
1548  PRECONDITION(op.size() == 2 && results.size() == 1);
1549  results[0]=minus_exprt(op[0], op[1]);
1550  }
1551  else if(bytecode == patternt("?div"))
1552  {
1553  PRECONDITION(op.size() == 2 && results.size() == 1);
1554  results[0]=div_exprt(op[0], op[1]);
1555  }
1556  else if(bytecode == patternt("?mul"))
1557  {
1558  PRECONDITION(op.size() == 2 && results.size() == 1);
1559  results[0]=mult_exprt(op[0], op[1]);
1560  }
1561  else if(bytecode == patternt("?neg"))
1562  {
1563  PRECONDITION(op.size() == 1 && results.size() == 1);
1564  results[0]=unary_minus_exprt(op[0], op[0].type());
1565  }
1566  else if(bytecode == patternt("?rem"))
1567  {
1568  PRECONDITION(op.size() == 2 && results.size() == 1);
1569  if(bytecode == BC_frem || bytecode == BC_drem)
1570  results[0]=rem_exprt(op[0], op[1]);
1571  else
1572  results[0]=mod_exprt(op[0], op[1]);
1573  }
1574  else if(bytecode == patternt("?cmp"))
1575  {
1576  PRECONDITION(op.size() == 2 && results.size() == 1);
1577  results = convert_cmp(op, results);
1578  }
1579  else if(bytecode == patternt("?cmp?"))
1580  {
1581  PRECONDITION(op.size() == 2 && results.size() == 1);
1582  results = convert_cmp2(statement, op, results);
1583  }
1584  else if(bytecode == patternt("?cmpl"))
1585  {
1586  PRECONDITION(op.size() == 2 && results.size() == 1);
1587  results[0]=binary_relation_exprt(op[0], ID_lt, op[1]);
1588  }
1589  else if(bytecode == BC_dup)
1590  {
1591  PRECONDITION(op.size() == 1 && results.size() == 2);
1592  results[0]=results[1]=op[0];
1593  }
1594  else if(bytecode == BC_dup_x1)
1595  {
1596  PRECONDITION(op.size() == 2 && results.size() == 3);
1597  results[0]=op[1];
1598  results[1]=op[0];
1599  results[2]=op[1];
1600  }
1601  else if(bytecode == BC_dup_x2)
1602  {
1603  PRECONDITION(op.size() == 3 && results.size() == 4);
1604  results[0]=op[2];
1605  results[1]=op[0];
1606  results[2]=op[1];
1607  results[3]=op[2];
1608  }
1609  // dup2* behaviour depends on the size of the operands on the
1610  // stack
1611  else if(bytecode == BC_dup2)
1612  {
1613  PRECONDITION(!stack.empty() && results.empty());
1614  convert_dup2(op, results);
1615  }
1616  else if(bytecode == BC_dup2_x1)
1617  {
1618  PRECONDITION(!stack.empty() && results.empty());
1619  convert_dup2_x1(op, results);
1620  }
1621  else if(bytecode == BC_dup2_x2)
1622  {
1623  PRECONDITION(!stack.empty() && results.empty());
1624  convert_dup2_x2(op, results);
1625  }
1626  else if(bytecode == BC_getfield)
1627  {
1628  PRECONDITION(op.size() == 1 && results.size() == 1);
1629  results[0] = java_bytecode_promotion(
1630  to_member(op[0], expr_dynamic_cast<fieldref_exprt>(arg0), ns));
1631  }
1632  else if(bytecode == BC_getstatic)
1633  {
1634  PRECONDITION(op.empty() && results.size() == 1);
1635  const auto &field_name=arg0.get_string(ID_component_name);
1636  const bool is_assertions_disabled_field=
1637  field_name.find("$assertionsDisabled")!=std::string::npos;
1638 
1639  const irep_idt field_id(
1640  get_static_field(arg0.get_string(ID_class), field_name));
1641 
1642  // Symbol should have been populated by java_bytecode_convert_class:
1643  const symbol_exprt symbol_expr(
1644  symbol_table.lookup_ref(field_id).symbol_expr());
1645 
1647  i_it->source_location,
1648  arg0,
1649  symbol_expr,
1650  is_assertions_disabled_field,
1651  c,
1652  results);
1653  }
1654  else if(bytecode == BC_putfield)
1655  {
1656  PRECONDITION(op.size() == 2 && results.empty());
1657  c = convert_putfield(expr_dynamic_cast<fieldref_exprt>(arg0), op);
1658  }
1659  else if(bytecode == BC_putstatic)
1660  {
1661  PRECONDITION(op.size() == 1 && results.empty());
1662  const auto &field_name=arg0.get_string(ID_component_name);
1663 
1664  const irep_idt field_id(
1665  get_static_field(arg0.get_string(ID_class), field_name));
1666 
1667  // Symbol should have been populated by java_bytecode_convert_class:
1668  const symbol_exprt symbol_expr(
1669  symbol_table.lookup_ref(field_id).symbol_expr());
1670 
1671  c = convert_putstatic(i_it->source_location, arg0, op, symbol_expr);
1672  }
1673  else if(bytecode == patternt("?2?")) // i2c etc.
1674  {
1675  PRECONDITION(op.size() == 1 && results.size() == 1);
1676  typet type=java_type_from_char(statement[2]);
1677  results[0] = typecast_exprt::conditional_cast(op[0], type);
1678 
1679  // These types get converted/truncated then immediately turned back into
1680  // ints again, so we just double-cast here.
1681  if(
1682  type == java_char_type() || type == java_byte_type() ||
1683  type == java_short_type())
1684  {
1685  results[0] = typecast_exprt(results[0], java_int_type());
1686  }
1687  }
1688  else if(bytecode == BC_new)
1689  {
1690  // use temporary since the stack symbol might get duplicated
1691  PRECONDITION(op.empty() && results.size() == 1);
1692  convert_new(i_it->source_location, arg0, c, results);
1693  }
1694  else if(bytecode == BC_newarray || bytecode == BC_anewarray)
1695  {
1696  // the op is the array size
1697  PRECONDITION(op.size() == 1 && results.size() == 1);
1698  c = convert_newarray(i_it->source_location, statement, arg0, op, results);
1699  }
1700  else if(bytecode == BC_multianewarray)
1701  {
1702  // The first argument is the type, the second argument is the number of
1703  // dimensions. The size of each dimension is on the stack.
1704  const std::size_t dimension =
1705  numeric_cast_v<std::size_t>(to_constant_expr(arg1));
1706 
1707  op=pop(dimension);
1708  assert(results.size()==1);
1709  c = convert_multianewarray(i_it->source_location, arg0, op, results);
1710  }
1711  else if(bytecode == BC_arraylength)
1712  {
1713  PRECONDITION(op.size() == 1 && results.size() == 1);
1714 
1715  // any array type is fine here, so we go for a reference array
1716  dereference_exprt array{typecast_exprt{op[0], java_array_type('a')}};
1717  PRECONDITION(array.type().id() == ID_struct_tag);
1718  array.set(ID_java_member_access, true);
1719 
1720  results[0] = member_exprt{std::move(array), "length", java_int_type()};
1721  }
1722  else if(bytecode == BC_tableswitch || bytecode == BC_lookupswitch)
1723  {
1724  PRECONDITION(op.size() == 1 && results.empty());
1725  c = convert_switch(op, i_it->args, i_it->source_location);
1726  }
1727  else if(bytecode == BC_pop || bytecode == BC_pop2)
1728  {
1729  c = convert_pop(statement, op);
1730  }
1731  else if(bytecode == BC_instanceof)
1732  {
1733  PRECONDITION(op.size() == 1 && results.size() == 1);
1734 
1735  results[0] =
1737  }
1738  else if(bytecode == BC_monitorenter || bytecode == BC_monitorexit)
1739  {
1740  c = convert_monitorenterexit(statement, op, i_it->source_location);
1741  }
1742  else if(bytecode == BC_swap)
1743  {
1744  PRECONDITION(op.size() == 2 && results.size() == 2);
1745  results[1]=op[0];
1746  results[0]=op[1];
1747  }
1748  else if(bytecode == BC_nop)
1749  {
1750  c=code_skipt();
1751  }
1752  else
1753  {
1754  c=codet(statement);
1755  c.operands()=op;
1756  }
1757 
1758  c = do_exception_handling(method, working_set, cur_pc, c);
1759 
1760  // Finally if this is the beginning of a catch block (already determined
1761  // before the big bytecode switch), insert the exception 'landing pad'
1762  // instruction before the actual instruction:
1763  if(catch_instruction.has_value())
1764  {
1765  c.make_block();
1766  c.operands().insert(c.operands().begin(), *catch_instruction);
1767  }
1768 
1769  if(!i_it->source_location.get_line().empty())
1771 
1772  push(results);
1773 
1774  instruction.done = true;
1775  for(const auto address : instruction.successors)
1776  {
1777  address_mapt::iterator a_it2=address_map.find(address);
1778  CHECK_RETURN(a_it2 != address_map.end());
1779 
1780  // clear the stack if this is an exception handler
1781  for(const auto &exception_row : method.exception_table)
1782  {
1783  if(address==exception_row.handler_pc)
1784  {
1785  stack.clear();
1786  break;
1787  }
1788  }
1789 
1790  if(!stack.empty() && a_it2->second.predecessors.size()>1)
1791  {
1792  // copy into temporaries
1793  code_blockt more_code;
1794 
1795  // introduce temporaries when successor is seen for the first
1796  // time
1797  if(a_it2->second.stack.empty())
1798  {
1799  for(stackt::iterator s_it=stack.begin();
1800  s_it!=stack.end();
1801  ++s_it)
1802  {
1803  symbol_exprt lhs=tmp_variable("$stack", s_it->type());
1804  code_assignt a(lhs, *s_it);
1805  more_code.add(a);
1806 
1807  s_it->swap(lhs);
1808  }
1809  }
1810  else
1811  {
1812  INVARIANT(
1813  a_it2->second.stack.size() == stack.size(),
1814  "Stack sizes should be the same.");
1815  stackt::const_iterator os_it=a_it2->second.stack.begin();
1816  for(auto &expr : stack)
1817  {
1818  assert(has_prefix(os_it->get_string(ID_C_base_name), "$stack"));
1819  symbol_exprt lhs=to_symbol_expr(*os_it);
1820  code_assignt a(lhs, expr);
1821  more_code.add(a);
1822 
1823  expr.swap(lhs);
1824  ++os_it;
1825  }
1826  }
1827 
1828  if(results.empty())
1829  {
1830  more_code.add(c);
1831  c.swap(more_code);
1832  }
1833  else
1834  {
1835  c.make_block();
1836  auto &last_statement=to_code_block(c).find_last_statement();
1837  if(last_statement.get_statement()==ID_goto)
1838  {
1839  // Insert stack twiddling before branch:
1840  last_statement.make_block();
1841  last_statement.operands().insert(
1842  last_statement.operands().begin(),
1843  more_code.statements().begin(),
1844  more_code.statements().end());
1845  }
1846  else
1847  to_code_block(c).append(more_code);
1848  }
1849  }
1850  a_it2->second.stack=stack;
1851  }
1852  }
1853 
1854  code_blockt code;
1855 
1856  // Add anonymous locals to the symtab:
1857  for(const auto &var : used_local_names)
1858  {
1859  symbolt new_symbol;
1860  new_symbol.name=var.get_identifier();
1861  new_symbol.type=var.type();
1862  new_symbol.base_name=var.get(ID_C_base_name);
1863  new_symbol.pretty_name=strip_java_namespace_prefix(var.get_identifier());
1864  new_symbol.mode=ID_java;
1865  new_symbol.is_type=false;
1866  new_symbol.is_file_local=true;
1867  new_symbol.is_thread_local=true;
1868  new_symbol.is_lvalue=true;
1869  symbol_table.add(new_symbol);
1870  }
1871 
1872  // Try to recover block structure as indicated in the local variable table:
1873 
1874  // The block tree node mirrors the block structure of root_block,
1875  // indexing the Java PCs where each subblock starts and ends.
1876  block_tree_nodet root;
1877  code_blockt root_block;
1878 
1879  // First create a simple flat list of basic blocks. We'll add lexical nesting
1880  // constructs as variable live-ranges require next.
1881  bool start_new_block=true;
1882  bool has_seen_previous_address=false;
1883  method_offsett previous_address = 0;
1884  for(const auto &address_pair : address_map)
1885  {
1886  const method_offsett address = address_pair.first;
1887  assert(address_pair.first==address_pair.second.source->address);
1888  const codet &c=address_pair.second.code;
1889 
1890  // Start a new lexical block if this is a branch target:
1891  if(!start_new_block)
1892  start_new_block=targets.find(address)!=targets.end();
1893  // Start a new lexical block if this is a control flow join
1894  // (e.g. due to exceptional control flow)
1895  if(!start_new_block)
1896  start_new_block=address_pair.second.predecessors.size()>1;
1897  // Start a new lexical block if we've just entered a block in which
1898  // exceptions are handled. This is usually the start of a try block, but a
1899  // single try can be represented as multiple non-contiguous blocks in the
1900  // exception table.
1901  if(!start_new_block && has_seen_previous_address)
1902  {
1903  for(const auto &exception_row : method.exception_table)
1904  if(exception_row.start_pc==previous_address)
1905  {
1906  start_new_block=true;
1907  break;
1908  }
1909  }
1910 
1911  if(start_new_block)
1912  {
1913  root_block.add(
1915  root.branch.push_back(block_tree_nodet::get_leaf());
1916  assert((root.branch_addresses.empty() ||
1917  root.branch_addresses.back()<address) &&
1918  "Block addresses should be unique and increasing");
1919  root.branch_addresses.push_back(address);
1920  }
1921 
1922  if(c.get_statement()!=ID_skip)
1923  {
1924  auto &lastlabel = to_code_label(root_block.statements().back());
1925  auto &add_to_block=to_code_block(lastlabel.code());
1926  add_to_block.add(c);
1927  }
1928  start_new_block=address_pair.second.successors.size()>1;
1929 
1930  previous_address=address;
1931  has_seen_previous_address=true;
1932  }
1933 
1934  // Find out where temporaries are used:
1935  std::map<irep_idt, variablet> temporary_variable_live_ranges;
1936  for(const auto &aentry : address_map)
1938  aentry.first,
1939  aentry.second.code,
1940  temporary_variable_live_ranges);
1941 
1942  std::vector<const variablet*> vars_to_process;
1943  for(const auto &vlist : variables)
1944  for(const auto &v : vlist)
1945  vars_to_process.push_back(&v);
1946 
1947  for(const auto &v : tmp_vars)
1948  vars_to_process.push_back(
1949  &temporary_variable_live_ranges.at(v.get_identifier()));
1950 
1951  for(const auto &v : used_local_names)
1952  vars_to_process.push_back(
1953  &temporary_variable_live_ranges.at(v.get_identifier()));
1954 
1955  for(const auto vp : vars_to_process)
1956  {
1957  const auto &v=*vp;
1958  if(v.is_parameter)
1959  continue;
1960  // Merge lexical scopes as far as possible to allow us to
1961  // declare these variable scopes faithfully.
1962  // Don't insert yet, as for the time being the blocks' only
1963  // operands must be other blocks.
1964  // The declarations will be inserted in the next pass instead.
1966  root,
1967  root_block,
1968  v.start_pc,
1969  v.start_pc + v.length,
1970  std::numeric_limits<method_offsett>::max(),
1971  address_map);
1972  }
1973  for(const auto vp : vars_to_process)
1974  {
1975  const auto &v=*vp;
1976  if(v.is_parameter)
1977  continue;
1978  // Skip anonymous variables:
1979  if(v.symbol_expr.get_identifier().empty())
1980  continue;
1981  auto &block = get_block_for_pcrange(
1982  root,
1983  root_block,
1984  v.start_pc,
1985  v.start_pc + v.length,
1986  std::numeric_limits<method_offsett>::max());
1987  code_declt d(v.symbol_expr);
1988  block.statements().insert(block.statements().begin(), d);
1989  }
1990 
1991  for(auto &block : root_block.statements())
1992  code.add(block);
1993 
1994  return code;
1995 }
1996 
1998  const irep_idt &statement,
1999  const exprt::operandst &op)
2000 {
2001  // these are skips
2002  codet c = code_skipt();
2003 
2004  // pop2 removes two single-word items from the stack (e.g. two
2005  // integers, or an integer and an object reference) or one
2006  // two-word item (i.e. a double or a long).
2007  // http://cs.au.dk/~mis/dOvs/jvmspec/ref-pop2.html
2008  if(statement == "pop2" && get_bytecode_type_width(op[0].type()) == 32)
2009  pop(1);
2010  return c;
2011 }
2012 
2014  const exprt::operandst &op,
2016  const source_locationt &location)
2017 {
2018  // we turn into switch-case
2019  code_blockt code_block;
2020  code_block.add_source_location() = location;
2021 
2022  bool is_label = true;
2023  for(auto a_it = args.begin(); a_it != args.end();
2024  a_it++, is_label = !is_label)
2025  {
2026  if(is_label)
2027  {
2028  const mp_integer number =
2029  numeric_cast_v<mp_integer>(to_constant_expr(*a_it));
2030  // The switch case does not contain any code, it just branches via a GOTO
2031  // to the jump target of the tableswitch/lookupswitch case at
2032  // hand. Therefore we consider this code to belong to the source bytecode
2033  // instruction and not the target instruction.
2034  const method_offsett label_number =
2035  numeric_cast_v<method_offsett>(number);
2036  code_gotot code(label(std::to_string(label_number)));
2037  code.add_source_location() = location;
2038 
2039  if(a_it == args.begin())
2040  {
2041  code_switch_caset code_case(nil_exprt(), std::move(code));
2042  code_case.set_default();
2043 
2044  code_block.add(std::move(code_case), location);
2045  }
2046  else
2047  {
2048  exprt case_op =
2049  typecast_exprt::conditional_cast(*std::prev(a_it), op[0].type());
2050  case_op.add_source_location() = location;
2051 
2052  code_switch_caset code_case(std::move(case_op), std::move(code));
2053  code_block.add(std::move(code_case), location);
2054  }
2055  }
2056  }
2057 
2058  code_switcht code_switch(op[0], std::move(code_block));
2059  code_switch.add_source_location() = location;
2060  return code_switch;
2061 }
2062 
2064  const irep_idt &statement,
2065  const exprt::operandst &op,
2066  const source_locationt &source_location)
2067 {
2068  const irep_idt descriptor = (statement == "monitorenter") ?
2069  "java::java.lang.Object.monitorenter:(Ljava/lang/Object;)V" :
2070  "java::java.lang.Object.monitorexit:(Ljava/lang/Object;)V";
2071 
2072  if(!threading_support || !symbol_table.has_symbol(descriptor))
2073  return code_skipt();
2074 
2075  // becomes a function call
2076  java_method_typet type(
2078  java_void_type());
2079  code_function_callt call(symbol_exprt(descriptor, type), {op[0]});
2080  call.add_source_location() = source_location;
2081  if(needed_lazy_methods && symbol_table.has_symbol(descriptor))
2082  needed_lazy_methods->add_needed_method(descriptor);
2083  return std::move(call);
2084 }
2085 
2087  exprt::operandst &op,
2088  exprt::operandst &results)
2089 {
2090  if(get_bytecode_type_width(stack.back().type()) == 32)
2091  op = pop(2);
2092  else
2093  op = pop(1);
2094 
2095  results.insert(results.end(), op.begin(), op.end());
2096  results.insert(results.end(), op.begin(), op.end());
2097 }
2098 
2100  exprt::operandst &op,
2101  exprt::operandst &results)
2102 {
2103  if(get_bytecode_type_width(stack.back().type()) == 32)
2104  op = pop(3);
2105  else
2106  op = pop(2);
2107 
2108  results.insert(results.end(), op.begin() + 1, op.end());
2109  results.insert(results.end(), op.begin(), op.end());
2110 }
2111 
2113  exprt::operandst &op,
2114  exprt::operandst &results)
2115 {
2116  if(get_bytecode_type_width(stack.back().type()) == 32)
2117  op = pop(2);
2118  else
2119  op = pop(1);
2120 
2121  exprt::operandst op2;
2122 
2123  if(get_bytecode_type_width(stack.back().type()) == 32)
2124  op2 = pop(2);
2125  else
2126  op2 = pop(1);
2127 
2128  results.insert(results.end(), op.begin(), op.end());
2129  results.insert(results.end(), op2.begin(), op2.end());
2130  results.insert(results.end(), op.begin(), op.end());
2131 }
2132 
2134  const irep_idt &statement,
2135  const constant_exprt &arg0,
2136  exprt::operandst &results) const
2137 {
2138  const char type_char = statement[0];
2139  const bool is_double('d' == type_char);
2140  const bool is_float('f' == type_char);
2141 
2142  if(is_double || is_float)
2143  {
2144  const ieee_float_spect spec(
2147 
2148  ieee_floatt value(spec);
2149  if(arg0.type().id() != ID_floatbv)
2150  {
2151  const mp_integer number = numeric_cast_v<mp_integer>(arg0);
2152  value.from_integer(number);
2153  }
2154  else
2155  value.from_expr(arg0);
2156 
2157  results[0] = value.to_expr();
2158  }
2159  else
2160  {
2161  const mp_integer value = numeric_cast_v<mp_integer>(arg0);
2162  const typet type = java_type_from_char(statement[0]);
2163  results[0] = from_integer(value, type);
2164  }
2165  return results;
2166 }
2167 
2169  const java_method_typet::parameterst &parameters,
2171 {
2172  // do some type adjustment for the arguments,
2173  // as Java promotes arguments
2174  // Also cast pointers since intermediate locals
2175  // can be void*.
2176  INVARIANT(
2177  parameters.size() == arguments.size(),
2178  "for each parameter there must be exactly one argument");
2179  for(std::size_t i = 0; i < parameters.size(); i++)
2180  {
2181  const typet &type = parameters[i].type();
2182  if(
2183  type == java_boolean_type() || type == java_char_type() ||
2184  type == java_byte_type() || type == java_short_type() ||
2185  type.id() == ID_pointer)
2186  {
2187  arguments[i] = typecast_exprt::conditional_cast(arguments[i], type);
2188  }
2189  }
2190 }
2191 
2193  source_locationt location,
2194  const irep_idt &statement,
2195  class_method_descriptor_exprt &class_method_descriptor,
2196  codet &c,
2197  exprt::operandst &results)
2198 {
2199  const bool use_this(statement != "invokestatic");
2200  const bool is_virtual(
2201  statement == "invokevirtual" || statement == "invokeinterface");
2202 
2203  const irep_idt &invoked_method_id = class_method_descriptor.get_identifier();
2204  INVARIANT(
2205  !invoked_method_id.empty(),
2206  "invoke statement arg0 must have an identifier");
2207 
2208  auto method_symbol = symbol_table.symbols.find(invoked_method_id);
2209 
2210  // Use the most precise type available: the actual symbol has generic info,
2211  // whereas the type given by the invoke instruction doesn't and is therefore
2212  // less accurate.
2213  if(method_symbol != symbol_table.symbols.end())
2214  {
2215  // Note the number of parameters might change here due to constructors using
2216  // invokespecial will have zero arguments (the `this` is added below)
2217  // but the symbol for the <init> will have the this parameter.
2218  INVARIANT(
2219  to_java_method_type(class_method_descriptor.type()).return_type().id() ==
2220  to_code_type(method_symbol->second.type).return_type().id(),
2221  "Function return type must not change in kind");
2222  class_method_descriptor.type() = method_symbol->second.type;
2223  }
2224 
2225  // Note arg0 and arg0.type() are subject to many side-effects in this method,
2226  // then finally used to populate the call instruction.
2227  java_method_typet &method_type =
2228  to_java_method_type(class_method_descriptor.type());
2229 
2230  java_method_typet::parameterst &parameters(method_type.parameters());
2231 
2232  if(use_this)
2233  {
2234  const irep_idt class_id = class_method_descriptor.class_id();
2235 
2236  if(parameters.empty() || !parameters[0].get_this())
2237  {
2238  typet thistype = struct_tag_typet(class_id);
2239  reference_typet object_ref_type = java_reference_type(thistype);
2240  java_method_typet::parametert this_p(object_ref_type);
2241  this_p.set_this();
2242  this_p.set_base_name(ID_this);
2243  parameters.insert(parameters.begin(), this_p);
2244  }
2245 
2246  // Note invokespecial is used for super-method calls as well as
2247  // constructors.
2248  if(statement == "invokespecial")
2249  {
2250  if(is_constructor(invoked_method_id))
2251  {
2253  needed_lazy_methods->add_needed_class(class_id);
2254  method_type.set_is_constructor();
2255  }
2256  else
2257  method_type.set(ID_java_super_method_call, true);
2258  }
2259  }
2260 
2261  location.set_function(method_id);
2262 
2263  code_function_callt::argumentst arguments = pop(parameters.size());
2264 
2265  // double-check a bit
2266  INVARIANT(
2267  !use_this || arguments.front().type().id() == ID_pointer,
2268  "first argument must be a pointer");
2269 
2270  adjust_invoke_argument_types(parameters, arguments);
2271 
2272  // do some type adjustment for return values
2273  exprt lhs = nil_exprt();
2274  const typet &return_type = method_type.return_type();
2275 
2276  if(return_type.id() != ID_empty)
2277  {
2278  // return types are promoted in Java
2279  lhs = tmp_variable("return", return_type);
2280  exprt promoted = java_bytecode_promotion(lhs);
2281  results.resize(1);
2282  results[0] = promoted;
2283  }
2284 
2285  // If we don't have a definition for the called symbol, and we won't
2286  // inherit a definition from a super-class, we create a new symbol and
2287  // insert it in the symbol table. The name and type of the method are
2288  // derived from the information we have in the call.
2289  // We fix the access attribute to ID_private, because of the following
2290  // reasons:
2291  // - We don't know the original access attribute and since the .class file is
2292  // unavailable, we have no way to know.
2293  // - The translated method could be an inherited protected method, hence
2294  // accessible from the original caller, but not from the generated test.
2295  // Therefore we must assume that the method is not accessible.
2296  // We set opaque methods as final to avoid assuming they can be overridden.
2297  if(
2298  method_symbol == symbol_table.symbols.end() &&
2299  !(is_virtual && is_method_inherited(
2300  class_method_descriptor.class_id(),
2301  class_method_descriptor.mangled_method_name())))
2302  {
2304  invoked_method_id,
2305  class_method_descriptor.base_method_name(),
2306  id2string(class_method_descriptor.class_id()).substr(6) + "." +
2307  id2string(class_method_descriptor.base_method_name()) + "()",
2308  method_type,
2309  class_method_descriptor.class_id(),
2310  symbol_table,
2312  }
2313 
2314  exprt function;
2315  if(is_virtual)
2316  {
2317  // dynamic binding
2318  PRECONDITION(use_this);
2319  PRECONDITION(!arguments.empty());
2320  function = class_method_descriptor;
2321  // Populate needed methods later,
2322  // once we know what object types can exist.
2323  }
2324  else
2325  {
2326  // static binding
2327  function = symbol_exprt(invoked_method_id, method_type);
2329  {
2330  needed_lazy_methods->add_needed_method(invoked_method_id);
2331  // Calling a static method causes static initialization:
2332  needed_lazy_methods->add_needed_class(class_method_descriptor.class_id());
2333  }
2334  }
2335 
2336  code_function_callt call(
2337  std::move(lhs), std::move(function), std::move(arguments));
2338  call.add_source_location() = location;
2339  call.function().add_source_location() = location;
2340 
2341  // Replacing call if it is a function of the Character library,
2342  // returning the same call otherwise
2344 
2345  if(!use_this)
2346  {
2347  codet clinit_call = get_clinit_call(class_method_descriptor.class_id());
2348  if(clinit_call.get_statement() != ID_skip)
2349  c = code_blockt({clinit_call, c});
2350  }
2351 }
2352 
2354  source_locationt location,
2355  codet &c)
2356 {
2357  exprt operand = pop(1)[0];
2358 
2359  // we may need to adjust the type of the argument
2360  operand = typecast_exprt::conditional_cast(operand, bool_typet());
2361 
2362  c = code_assumet(operand);
2363  location.set_function(method_id);
2364  c.add_source_location() = location;
2365  return c;
2366 }
2367 
2369  const exprt &arg0,
2370  const exprt::operandst &op,
2371  codet &c,
2372  exprt::operandst &results) const
2373 {
2374  java_instanceof_exprt check(op[0], to_struct_tag_type(arg0.type()));
2375  code_assertt assert_class(check);
2376  assert_class.add_source_location().set_comment("Dynamic cast check");
2377  assert_class.add_source_location().set_property_class("bad-dynamic-cast");
2378  // we add this assert such that we can recognise it
2379  // during the instrumentation phase
2380  c = std::move(assert_class);
2381  results[0] = op[0];
2382 }
2383 
2385  const source_locationt &location,
2386  const exprt::operandst &op,
2387  codet &c,
2388  exprt::operandst &results) const
2389 {
2390  if(
2393  "java::java.lang.AssertionError") &&
2395  {
2396  // we translate athrow into
2397  // ASSERT false;
2398  // ASSUME false:
2399  code_assertt assert_code(false_exprt{});
2400  source_locationt assert_location = location; // copy
2401  assert_location.set_comment("assertion at " + location.as_string());
2402  assert_location.set("user-provided", true);
2403  assert_location.set_property_class(ID_assertion);
2404  assert_code.add_source_location() = assert_location;
2405 
2406  code_assumet assume_code(false_exprt{});
2407  source_locationt assume_location = location; // copy
2408  assume_location.set("user-provided", true);
2409  assume_code.add_source_location() = assume_location;
2410 
2411  c = code_blockt({assert_code, assume_code});
2412  }
2413  else
2414  {
2415  side_effect_expr_throwt throw_expr(irept(), typet(), location);
2416  throw_expr.copy_to_operands(op[0]);
2417  c = code_expressiont(throw_expr);
2418  }
2419  results[0] = op[0];
2420 }
2421 
2424  const std::set<method_offsett> &working_set,
2425  method_offsett cur_pc,
2426  codet &code)
2427 {
2428  // For each exception handler range that starts here add a CATCH-PUSH marker
2429  // Each CATCH-PUSH records a list of all the exception id and handler label
2430  // pairs handled for its exact block
2431 
2432  // Gather all try-catch blocks that have cur_pc as the starting pc
2433  typedef std::vector<std::reference_wrapper<
2435  std::map<std::size_t, exceptionst> exceptions_by_end;
2437  : method.exception_table)
2438  {
2439  if(exception.start_pc == cur_pc)
2440  exceptions_by_end[exception.end_pc].push_back(exception);
2441  }
2442  for(const auto &exceptions : exceptions_by_end)
2443  {
2444  // For each block with a distinct end position create one CATCH-PUSH
2445  code_push_catcht catch_push;
2446  // Fill in its exception_list
2447  code_push_catcht::exception_listt &exception_list =
2448  catch_push.exception_list();
2450  : exceptions.second)
2451  {
2452  exception_list.emplace_back(
2453  exception.catch_type.get_identifier(),
2454  // Record the exception handler in the CATCH-PUSH instruction by
2455  // generating a label corresponding to the handler's pc
2456  label(std::to_string(exception.handler_pc)));
2457  }
2458  // Prepend the CATCH-PUSH instruction
2459  code = code_blockt({ std::move(catch_push), code });
2460  }
2461 
2462  // Next add the CATCH-POP instructions
2463  // exception_row.end_pc is exclusive so append a CATCH-POP instruction if
2464  // this is the instruction before it.
2465  // To do this, attempt to find all exception handlers that end at the
2466  // earliest known instruction after this one.
2467 
2468  // Dangerously, we assume that the next opcode in the bytecode after the end
2469  // of the exception handler block (whose address matches the exclusive end
2470  // position of the block) will be a successor of some code investigated
2471  // before the instruction at the end of that handler and therefore in the
2472  // working set.
2473  // As an example of where this may fail, for non-obfuscated bytecode
2474  // generated by most compilers the next opcode after the block ending at the
2475  // end of the try block is the lexically next bit of code after the try
2476  // block, i.e. the catch block. When there aren't any throwing statements in
2477  // the try block this block will not be the successor of any instruction.
2478 
2479  auto next_opcode_it = std::find_if(
2480  working_set.begin(),
2481  working_set.end(),
2482  [cur_pc](method_offsett offset) { return offset > cur_pc; });
2483  if(next_opcode_it != working_set.end())
2484  {
2485  // Count the distinct start positions of handlers that end at this location
2486  std::set<std::size_t> start_positions; // Use a set to deduplicate
2487  for(const auto &exception_row : method.exception_table)
2488  {
2489  // Check if the next instruction found is the (exclusive) end of a block
2490  if(*next_opcode_it == exception_row.end_pc)
2491  start_positions.insert(exception_row.start_pc);
2492  }
2493  for(std::size_t handler = 0; handler < start_positions.size(); ++handler)
2494  {
2495  // Append a CATCH-POP instruction before the end of the block
2496  code = code_blockt({ code, code_pop_catcht() });
2497  }
2498  }
2499 
2500  return code;
2501 }
2502 
2504  const source_locationt &location,
2505  const exprt &arg0,
2506  const exprt::operandst &op,
2507  exprt::operandst &results)
2508 {
2509  const reference_typet ref_type = java_reference_type(arg0.type());
2510  side_effect_exprt java_new_array(ID_java_new_array, ref_type, location);
2511  java_new_array.operands() = op;
2512 
2513  code_blockt create;
2514 
2515  if(max_array_length != 0)
2516  {
2518  binary_relation_exprt le_max_size(op[0], ID_le, size_limit);
2519  code_assumet assume_le_max_size(le_max_size);
2520  create.add(assume_le_max_size);
2521  }
2522 
2523  const exprt tmp = tmp_variable("newarray", ref_type);
2524  create.add(code_assignt(tmp, java_new_array));
2525  results[0] = tmp;
2526 
2527  return create;
2528 }
2529 
2531  const source_locationt &location,
2532  const irep_idt &statement,
2533  const exprt &arg0,
2534  const exprt::operandst &op,
2535  exprt::operandst &results)
2536 {
2537  java_reference_typet ref_type = [&]() {
2538  if(statement == "newarray")
2539  {
2540  irep_idt id = arg0.type().id();
2541 
2542  char element_type;
2543  if(id == ID_bool)
2544  element_type = 'z';
2545  else if(id == ID_char)
2546  element_type = 'c';
2547  else if(id == ID_float)
2548  element_type = 'f';
2549  else if(id == ID_double)
2550  element_type = 'd';
2551  else if(id == ID_byte)
2552  element_type = 'b';
2553  else if(id == ID_short)
2554  element_type = 's';
2555  else if(id == ID_int)
2556  element_type = 'i';
2557  else if(id == ID_long)
2558  element_type = 'j';
2559  else
2560  element_type = '?';
2561  return java_array_type(element_type);
2562  }
2563  else
2564  {
2566  }
2567  }();
2568 
2569  side_effect_exprt java_new_array(ID_java_new_array, ref_type, location);
2570  java_new_array.copy_to_operands(op[0]);
2571 
2572  code_blockt block;
2573 
2574  if(max_array_length != 0)
2575  {
2577  binary_relation_exprt le_max_size(op[0], ID_le, size_limit);
2578  code_assumet assume_le_max_size(le_max_size);
2579  block.add(std::move(assume_le_max_size));
2580  }
2581  const exprt tmp = tmp_variable("newarray", ref_type);
2582  block.add(code_assignt(tmp, java_new_array));
2583  results[0] = tmp;
2584 
2585  return block;
2586 }
2587 
2589  const source_locationt &location,
2590  const exprt &arg0,
2591  codet &c,
2592  exprt::operandst &results)
2593 {
2594  const reference_typet ref_type = java_reference_type(arg0.type());
2595  side_effect_exprt java_new_expr(ID_java_new, ref_type, location);
2596 
2597  if(!location.get_line().empty())
2598  java_new_expr.add_source_location() = location;
2599 
2600  const exprt tmp = tmp_variable("new", ref_type);
2601  c = code_assignt(tmp, java_new_expr);
2602  c.add_source_location() = location;
2603  codet clinit_call =
2605  if(clinit_call.get_statement() != ID_skip)
2606  {
2607  c = code_blockt({clinit_call, c});
2608  }
2609  results[0] = tmp;
2610 }
2611 
2613  const source_locationt &location,
2614  const exprt &arg0,
2615  const exprt::operandst &op,
2616  const symbol_exprt &symbol_expr)
2617 {
2618  if(needed_lazy_methods && arg0.type().id() == ID_struct_tag)
2619  {
2620  needed_lazy_methods->add_needed_class(
2622  }
2623 
2624  code_blockt block;
2625  block.add_source_location() = location;
2626 
2627  // Note this initializer call deliberately inits the class used to make
2628  // the reference, which may be a child of the class that actually defines
2629  // the field.
2630  codet clinit_call = get_clinit_call(arg0.get_string(ID_class));
2631  if(clinit_call.get_statement() != ID_skip)
2632  block.add(clinit_call);
2633 
2635  "stack_static_field",
2636  block,
2638  symbol_expr.get_identifier());
2639  block.add(code_assignt(symbol_expr, op[0]));
2640  return block;
2641 }
2642 
2644  const fieldref_exprt &arg0,
2645  const exprt::operandst &op)
2646 {
2647  code_blockt block;
2649  "stack_field", block, bytecode_write_typet::FIELD, arg0.component_name());
2650  block.add(code_assignt(to_member(op[0], arg0, ns), op[1]));
2651  return block;
2652 }
2653 
2655  const source_locationt &source_location,
2656  const exprt &arg0,
2657  const symbol_exprt &symbol_expr,
2658  const bool is_assertions_disabled_field,
2659  codet &c,
2660  exprt::operandst &results)
2661 {
2663  {
2664  if(arg0.type().id() == ID_struct_tag)
2665  {
2666  needed_lazy_methods->add_needed_class(
2668  }
2669  else if(arg0.type().id() == ID_pointer)
2670  {
2671  const auto &pointer_type = to_pointer_type(arg0.type());
2672  if(pointer_type.subtype().id() == ID_struct_tag)
2673  {
2674  needed_lazy_methods->add_needed_class(
2676  }
2677  }
2678  else if(is_assertions_disabled_field)
2679  {
2680  needed_lazy_methods->add_needed_class(arg0.get_string(ID_class));
2681  }
2682  }
2683  symbol_exprt symbol_with_location = symbol_expr;
2684  symbol_with_location.add_source_location() = source_location;
2685  results[0] = java_bytecode_promotion(symbol_with_location);
2686 
2687  // Note this initializer call deliberately inits the class used to make
2688  // the reference, which may be a child of the class that actually defines
2689  // the field.
2690  codet clinit_call = get_clinit_call(arg0.get_string(ID_class));
2691  if(clinit_call.get_statement() != ID_skip)
2692  c = clinit_call;
2693  else if(is_assertions_disabled_field)
2694  {
2695  // set $assertionDisabled to false
2696  c = code_assignt(symbol_expr, false_exprt());
2697  }
2698 }
2699 
2701  const irep_idt &statement,
2702  const exprt::operandst &op,
2703  exprt::operandst &results) const
2704 {
2705  const int nan_value(statement[4] == 'l' ? -1 : 1);
2706  const typet result_type(java_int_type());
2707  const exprt nan_result(from_integer(nan_value, result_type));
2708 
2709  // (value1 == NaN || value2 == NaN) ?
2710  // nan_value : value1 < value2 ? -1 : value2 < value1 1 ? 1 : 0;
2711  // (value1 == NaN || value2 == NaN) ?
2712  // nan_value : value1 == value2 ? 0 : value1 < value2 -1 ? 1 : 0;
2713 
2714  isnan_exprt nan_op0(op[0]);
2715  isnan_exprt nan_op1(op[1]);
2716  exprt one = from_integer(1, result_type);
2717  exprt minus_one = from_integer(-1, result_type);
2718  results[0] = if_exprt(
2719  or_exprt(nan_op0, nan_op1),
2720  nan_result,
2721  if_exprt(
2722  ieee_float_equal_exprt(op[0], op[1]),
2723  from_integer(0, result_type),
2724  if_exprt(binary_relation_exprt(op[0], ID_lt, op[1]), minus_one, one)));
2725  return results;
2726 }
2727 
2729  const exprt::operandst &op,
2730  exprt::operandst &results) const
2731 { // The integer result on the stack is:
2732  // 0 if op[0] equals op[1]
2733  // -1 if op[0] is less than op[1]
2734  // 1 if op[0] is greater than op[1]
2735 
2736  const typet t = java_int_type();
2737  exprt one = from_integer(1, t);
2738  exprt minus_one = from_integer(-1, t);
2739 
2740  if_exprt greater =
2741  if_exprt(binary_relation_exprt(op[0], ID_gt, op[1]), one, minus_one);
2742 
2743  results[0] = if_exprt(
2744  binary_relation_exprt(op[0], ID_equal, op[1]), from_integer(0, t), greater);
2745  return results;
2746 }
2747 
2749  const irep_idt &statement,
2750  const exprt::operandst &op,
2751  exprt::operandst &results) const
2752 {
2753  const typet type = java_type_from_char(statement[0]);
2754 
2755  const std::size_t width = get_bytecode_type_width(type);
2756  typet target = unsignedbv_typet(width);
2757 
2758  exprt lhs = typecast_exprt::conditional_cast(op[0], target);
2759  exprt rhs = typecast_exprt::conditional_cast(op[1], target);
2760 
2761  results[0] =
2762  typecast_exprt::conditional_cast(lshr_exprt(lhs, rhs), op[0].type());
2763 
2764  return results;
2765 }
2766 
2768  const exprt &arg0,
2769  const exprt &arg1,
2770  const source_locationt &location,
2771  const method_offsett address)
2772 {
2773  code_blockt block;
2774  block.add_source_location() = location;
2775  // search variable on stack
2776  const exprt &locvar = variable(arg0, 'i', address);
2778  "stack_iinc",
2779  block,
2781  to_symbol_expr(locvar).get_identifier());
2782 
2783  const exprt arg1_int_type =
2785  code_assignt code_assign(
2786  variable(arg0, 'i', address),
2787  plus_exprt(
2789  variable(arg0, 'i', address), java_int_type()),
2790  arg1_int_type));
2791  block.add(std::move(code_assign));
2792 
2793  return block;
2794 }
2795 
2798  const exprt::operandst &op,
2799  const mp_integer &number,
2800  const source_locationt &location) const
2801 {
2802  const typecast_exprt lhs(op[0], java_reference_type(java_void_type()));
2803  const exprt rhs(null_pointer_exprt(to_pointer_type(lhs.type())));
2804 
2805  const method_offsett label_number = numeric_cast_v<method_offsett>(number);
2806 
2807  code_ifthenelset code_branch(
2808  binary_relation_exprt(lhs, ID_equal, rhs),
2809  code_gotot(label(std::to_string(label_number))));
2810 
2811  code_branch.then_case().add_source_location() =
2812  address_map.at(label_number).source->source_location;
2813  code_branch.add_source_location() = location;
2814 
2815  return code_branch;
2816 }
2817 
2820  const exprt::operandst &op,
2821  const mp_integer &number,
2822  const source_locationt &location) const
2823 {
2824  const typecast_exprt lhs(op[0], java_reference_type(java_void_type()));
2825  const exprt rhs(null_pointer_exprt(to_pointer_type(lhs.type())));
2826 
2827  const method_offsett label_number = numeric_cast_v<method_offsett>(number);
2828 
2829  code_ifthenelset code_branch(
2830  binary_relation_exprt(lhs, ID_notequal, rhs),
2831  code_gotot(label(std::to_string(label_number))));
2832 
2833  code_branch.then_case().add_source_location() =
2834  address_map.at(label_number).source->source_location;
2835  code_branch.add_source_location() = location;
2836 
2837  return code_branch;
2838 }
2839 
2842  const exprt::operandst &op,
2843  const irep_idt &id,
2844  const mp_integer &number,
2845  const source_locationt &location) const
2846 {
2847  const method_offsett label_number = numeric_cast_v<method_offsett>(number);
2848 
2849  code_ifthenelset code_branch(
2850  binary_relation_exprt(op[0], id, from_integer(0, op[0].type())),
2851  code_gotot(label(std::to_string(label_number))));
2852 
2853  code_branch.cond().add_source_location() = location;
2855  code_branch.then_case().add_source_location() =
2856  address_map.at(label_number).source->source_location;
2858  code_branch.add_source_location() = location;
2860 
2861  return code_branch;
2862 }
2863 
2866  const u1 bytecode,
2867  const exprt::operandst &op,
2868  const mp_integer &number,
2869  const source_locationt &location) const
2870 {
2871  const irep_idt cmp_op = get_if_cmp_operator(bytecode);
2872  binary_relation_exprt condition(
2873  op[0], cmp_op, typecast_exprt::conditional_cast(op[1], op[0].type()));
2874  condition.add_source_location() = location;
2875 
2876  const method_offsett label_number = numeric_cast_v<method_offsett>(number);
2877 
2878  code_ifthenelset code_branch(
2879  std::move(condition), code_gotot(label(std::to_string(label_number))));
2880 
2881  code_branch.then_case().add_source_location() =
2882  address_map.at(label_number).source->source_location;
2883  code_branch.add_source_location() = location;
2884 
2885  return code_branch;
2886 }
2887 
2889  const std::vector<method_offsett> &jsr_ret_targets,
2890  const exprt &arg0,
2891  const source_locationt &location,
2892  const method_offsett address)
2893 {
2894  code_blockt c;
2895  auto retvar = variable(arg0, 'a', address);
2896  for(size_t idx = 0, idxlim = jsr_ret_targets.size(); idx != idxlim; ++idx)
2897  {
2898  irep_idt number = std::to_string(jsr_ret_targets[idx]);
2899  code_gotot g(label(number));
2900  g.add_source_location() = location;
2901  if(idx == idxlim - 1)
2902  c.add(g);
2903  else
2904  {
2905  auto address_ptr =
2906  from_integer(jsr_ret_targets[idx], unsignedbv_typet(64));
2907  address_ptr.type() = pointer_type(java_void_type());
2908 
2909  code_ifthenelset branch(equal_exprt(retvar, address_ptr), std::move(g));
2910 
2911  branch.cond().add_source_location() = location;
2912  branch.add_source_location() = location;
2913 
2914  c.add(std::move(branch));
2915  }
2916  }
2917  return c;
2918 }
2919 
2925 static exprt conditional_array_cast(const exprt &expr, char type_char)
2926 {
2927  const auto ref_type =
2928  type_try_dynamic_cast<java_reference_typet>(expr.type());
2929  const bool typecast_not_needed =
2930  ref_type && ((type_char == 'b' && ref_type->subtype().get_identifier() ==
2931  "java::array[boolean]") ||
2932  *ref_type == java_array_type(type_char));
2933  return typecast_not_needed ? expr
2934  : typecast_exprt(expr, java_array_type(type_char));
2935 }
2936 
2938  const irep_idt &statement,
2939  const exprt::operandst &op)
2940 {
2941  PRECONDITION(op.size() == 2);
2942  const char type_char = statement[0];
2943  const exprt op_with_right_type = conditional_array_cast(op[0], type_char);
2944  dereference_exprt deref{op_with_right_type};
2945  deref.set(ID_java_member_access, true);
2946 
2947  auto java_array_type = type_try_dynamic_cast<struct_tag_typet>(deref.type());
2948  INVARIANT(java_array_type, "Java array type should be a struct_tag_typet");
2949  member_exprt data_ptr{
2951  plus_exprt data_plus_offset{std::move(data_ptr), op[1]};
2952  // tag it so it's easy to identify during instrumentation
2953  data_plus_offset.set(ID_java_array_access, true);
2954  return java_bytecode_promotion(dereference_exprt{data_plus_offset});
2955 }
2956 
2958  const exprt &index,
2959  char type_char,
2960  size_t address)
2961 {
2962  const exprt var = variable(index, type_char, address);
2963  if(type_char == 'i')
2964  {
2965  INVARIANT(
2967  type_try_dynamic_cast<bitvector_typet>(var.type())->get_width() <= 32,
2968  "iload can be used for boolean, byte, short, int and char");
2970  }
2971  INVARIANT(
2972  (type_char == 'a' && can_cast_type<reference_typet>(var.type())) ||
2973  var.type() == java_type_from_char(type_char),
2974  "Variable type must match [adflv]load return type");
2975  return var;
2976 }
2977 
2979  const irep_idt &statement,
2980  const exprt &arg0,
2981  const exprt::operandst &op,
2982  const method_offsett address,
2983  const source_locationt &location)
2984 {
2985  const exprt var = variable(arg0, statement[0], address);
2986  const irep_idt &var_name = to_symbol_expr(var).get_identifier();
2987 
2988  code_blockt block;
2989  block.add_source_location() = location;
2990 
2992  "stack_store",
2993  block,
2995  var_name);
2996 
2997  block.add(
2999  location);
3000  return block;
3001 }
3002 
3004  const irep_idt &statement,
3005  const exprt::operandst &op,
3006  const source_locationt &location)
3007 {
3008  PRECONDITION(op.size() == 3);
3009  const char type_char = statement[0];
3010  const exprt op_with_right_type = conditional_array_cast(op[0], type_char);
3011  dereference_exprt deref{op_with_right_type};
3012  deref.set(ID_java_member_access, true);
3013 
3014  auto java_array_type = type_try_dynamic_cast<struct_tag_typet>(deref.type());
3015  INVARIANT(java_array_type, "Java array type should be a struct_tag_typet");
3016  member_exprt data_ptr{
3018  plus_exprt data_plus_offset{std::move(data_ptr), op[1]};
3019  // tag it so it's easy to identify during instrumentation
3020  data_plus_offset.set(ID_java_array_access, true);
3021 
3022  code_blockt block;
3023  block.add_source_location() = location;
3024 
3026  "stack_astore", block, bytecode_write_typet::ARRAY_REF, "");
3027 
3028  code_assignt array_put{dereference_exprt{data_plus_offset}, op[2]};
3029  block.add(std::move(array_put), location);
3030  return block;
3031 }
3032 
3034  const source_locationt &location,
3035  std::size_t instruction_address,
3036  const exprt &arg0,
3037  codet &result_code)
3038 {
3039  const java_method_typet &method_type = to_java_method_type(arg0.type());
3040  const java_method_typet::parameterst &parameters(method_type.parameters());
3041  const typet &return_type = method_type.return_type();
3042 
3043  // Note these must be popped regardless of whether we understand the lambda
3044  // method or not
3045  code_function_callt::argumentst arguments = pop(parameters.size());
3046 
3047  irep_idt synthetic_class_name =
3048  lambda_synthetic_class_name(method_id, instruction_address);
3049 
3050  if(!symbol_table.has_symbol(synthetic_class_name))
3051  {
3052  // We failed to parse the invokedynamic handle as a Java 8+ lambda;
3053  // give up and return null.
3054  const auto value = zero_initializer(return_type, location, ns);
3055  if(!value.has_value())
3056  {
3057  error().source_location = location;
3058  error() << "failed to zero-initialize return type" << eom;
3059  throw 0;
3060  }
3061  return value;
3062  }
3063 
3064  // Construct an instance of the synthetic class created for this invokedynamic
3065  // site:
3066 
3067  irep_idt constructor_name = id2string(synthetic_class_name) + ".<init>";
3068 
3069  const symbolt &constructor_symbol = ns.lookup(constructor_name);
3070 
3072 
3073  // SyntheticType lambda_new = new SyntheticType;
3074  const reference_typet ref_type =
3075  java_reference_type(struct_tag_typet(synthetic_class_name));
3076  side_effect_exprt java_new_expr(ID_java_new, ref_type, location);
3077  const exprt new_instance = tmp_variable("lambda_new", ref_type);
3078  result.add(code_assignt(new_instance, java_new_expr, location));
3079 
3080  // lambda_new.<init>(capture_1, capture_2, ...);
3081  // Add the implicit 'this' parameter:
3082  arguments.insert(arguments.begin(), new_instance);
3085 
3086  code_function_callt constructor_call(
3087  constructor_symbol.symbol_expr(), arguments);
3088  constructor_call.add_source_location() = location;
3089  result.add(constructor_call);
3091  {
3092  needed_lazy_methods->add_needed_method(constructor_symbol.name);
3093  needed_lazy_methods->add_needed_class(synthetic_class_name);
3094  }
3095 
3096  result_code = std::move(result);
3097 
3098  if(return_type.id() == ID_empty)
3099  return {};
3100  else
3101  return new_instance;
3102 }
3103 
3106  const std::vector<method_offsett> &jsr_ret_targets,
3107  const std::vector<
3108  std::vector<java_bytecode_parse_treet::instructiont>::const_iterator>
3109  &ret_instructions) const
3110 { // Draw edges from every `ret` to every `jsr` successor. Could do better with
3111  // flow analysis to distinguish multiple subroutines within the same function.
3112  for(const auto &retinst : ret_instructions)
3113  {
3114  auto &a_entry = address_map.at(retinst->address);
3115  a_entry.successors.insert(
3116  a_entry.successors.end(), jsr_ret_targets.begin(), jsr_ret_targets.end());
3117  }
3118 }
3119 
3120 std::vector<java_bytecode_convert_methodt::method_offsett>
3122  const method_offsett address,
3124  const
3125 {
3126  std::vector<method_offsett> result;
3127  for(const auto &exception_row : exception_table)
3128  {
3129  if(address >= exception_row.start_pc && address < exception_row.end_pc)
3130  result.push_back(exception_row.handler_pc);
3131  }
3132  return result;
3133 }
3134 
3148  symbolt &method_symbol,
3150  &local_variable_table,
3151  symbol_table_baset &symbol_table)
3152 {
3153  // Obtain a std::vector of java_method_typet::parametert objects from the
3154  // (function) type of the symbol
3155  java_method_typet &method_type = to_java_method_type(method_symbol.type);
3156  java_method_typet::parameterst &parameters = method_type.parameters();
3157 
3158  // Find number of parameters
3159  unsigned slots_for_parameters = java_method_parameter_slots(method_type);
3160 
3161  // Find parameter names in the local variable table:
3162  typedef std::pair<irep_idt, irep_idt> base_name_and_identifiert;
3163  std::map<std::size_t, base_name_and_identifiert> param_names;
3164  for(const auto &v : local_variable_table)
3165  {
3166  if(v.index < slots_for_parameters)
3167  param_names.emplace(
3168  v.index,
3169  make_pair(
3170  v.name, id2string(method_symbol.name) + "::" + id2string(v.name)));
3171  }
3172 
3173  // Assign names to parameters
3174  std::size_t param_index = 0;
3175  for(auto &param : parameters)
3176  {
3177  irep_idt base_name, identifier;
3178 
3179  // Construct a sensible base name (base_name) and a fully qualified name
3180  // (identifier) for every parameter of the method under translation,
3181  // regardless of whether we have an LVT or not; and assign it to the
3182  // parameter object (which is stored in the type of the symbol, not in the
3183  // symbol table)
3184  if(param_index == 0 && param.get_this())
3185  {
3186  // my.package.ClassName.myMethodName:(II)I::this
3187  base_name = ID_this;
3188  identifier = id2string(method_symbol.name) + "::" + id2string(base_name);
3189  }
3190  else
3191  {
3192  auto param_name = param_names.find(param_index);
3193  if(param_name != param_names.end())
3194  {
3195  base_name = param_name->second.first;
3196  identifier = param_name->second.second;
3197  }
3198  else
3199  {
3200  // my.package.ClassName.myMethodName:(II)I::argNT, where N is the local
3201  // variable slot where the parameter is stored and T is a character
3202  // indicating the type
3203  const typet &type = param.type();
3204  char suffix = java_char_from_type(type);
3205  base_name = "arg" + std::to_string(param_index) + suffix;
3206  identifier =
3207  id2string(method_symbol.name) + "::" + id2string(base_name);
3208  }
3209  }
3210  param.set_base_name(base_name);
3211  param.set_identifier(identifier);
3212 
3213  // Build a new symbol for the parameter and add it to the symbol table
3214  parameter_symbolt parameter_symbol;
3215  parameter_symbol.base_name = base_name;
3216  parameter_symbol.mode = ID_java;
3217  parameter_symbol.name = identifier;
3218  parameter_symbol.type = param.type();
3219  symbol_table.insert(parameter_symbol);
3220 
3221  param_index += java_local_variable_slots(param.type());
3222  }
3223 }
3224 
3226  const symbolt &class_symbol,
3227  const java_bytecode_parse_treet::methodt &method,
3228  symbol_table_baset &symbol_table,
3229  message_handlert &message_handler,
3230  size_t max_array_length,
3231  bool throw_assertion_error,
3232  optionalt<ci_lazy_methods_neededt> needed_lazy_methods,
3233  java_string_library_preprocesst &string_preprocess,
3234  const class_hierarchyt &class_hierarchy,
3235  bool threading_support,
3236  const optionalt<prefix_filtert> &method_context,
3237  bool assert_no_exceptions_thrown)
3238 
3239 {
3241  symbol_table,
3242  message_handler,
3243  max_array_length,
3244  throw_assertion_error,
3245  needed_lazy_methods,
3246  string_preprocess,
3247  class_hierarchy,
3248  threading_support,
3249  assert_no_exceptions_thrown);
3250 
3251  java_bytecode_convert_method(class_symbol, method, method_context);
3252 }
3253 
3260  const irep_idt &classname,
3261  const irep_idt &mangled_method_name) const
3262 {
3263  const auto inherited_method = get_inherited_component(
3264  classname, mangled_method_name, symbol_table, false);
3265  return inherited_method.has_value();
3266 }
3267 
3274  const irep_idt &class_identifier,
3275  const irep_idt &component_name) const
3276 {
3277  const auto inherited_method = get_inherited_component(
3278  class_identifier, component_name, symbol_table, true);
3279 
3280  INVARIANT(
3281  inherited_method.has_value(), "static field should be in symbol table");
3282 
3283  return inherited_method->get_full_component_identifier();
3284 }
3285 
3293  const std::string &tmp_var_prefix,
3294  code_blockt &block,
3295  const bytecode_write_typet write_type,
3296  const irep_idt &identifier)
3297 {
3298  const std::function<bool(
3299  const std::function<tvt(const exprt &expr)>, const exprt &expr)>
3300  entry_matches = [&entry_matches](
3301  const std::function<tvt(const exprt &expr)> predicate,
3302  const exprt &expr) {
3303  const tvt &tvres = predicate(expr);
3304  if(tvres.is_unknown())
3305  {
3306  return std::any_of(
3307  expr.operands().begin(),
3308  expr.operands().end(),
3309  [&predicate, &entry_matches](const exprt &expr) {
3310  return entry_matches(predicate, expr);
3311  });
3312  }
3313  else
3314  {
3315  return tvres.is_true();
3316  }
3317  };
3318 
3319  // Function that checks whether the expression accesses a member with the
3320  // given identifier name. These accesses are created in the case of `iinc`, or
3321  // non-array `?store` instructions.
3322  const std::function<tvt(const exprt &expr)> has_member_entry = [&identifier](
3323  const exprt &expr) {
3324  const auto member_expr = expr_try_dynamic_cast<member_exprt>(expr);
3325  return !member_expr ? tvt::unknown()
3326  : tvt(member_expr->get_component_name() == identifier);
3327  };
3328 
3329  // Function that checks whether the expression is a symbol with the given
3330  // identifier name. These accesses are created in the case of `putstatic` or
3331  // `putfield` instructions.
3332  const std::function<tvt(const exprt &expr)> is_symbol_entry =
3333  [&identifier](const exprt &expr) {
3334  const auto symbol_expr = expr_try_dynamic_cast<symbol_exprt>(expr);
3335  return !symbol_expr ? tvt::unknown()
3336  : tvt(symbol_expr->get_identifier() == identifier);
3337  };
3338 
3339  // Function that checks whether the expression is a dereference
3340  // expression. These accesses are created in `?astore` array write
3341  // instructions.
3342  const std::function<tvt(const exprt &expr)> is_dereference_entry =
3343  [](const exprt &expr) {
3344  const auto dereference_expr =
3345  expr_try_dynamic_cast<dereference_exprt>(expr);
3346  return !dereference_expr ? tvt::unknown() : tvt(true);
3347  };
3348 
3349  for(auto &stack_entry : stack)
3350  {
3351  bool replace = false;
3352  switch(write_type)
3353  {
3356  replace = entry_matches(is_symbol_entry, stack_entry);
3357  break;
3359  replace = entry_matches(is_dereference_entry, stack_entry);
3360  break;
3362  replace = entry_matches(has_member_entry, stack_entry);
3363  break;
3364  }
3365  if(replace)
3366  {
3368  tmp_var_prefix, stack_entry.type(), block, stack_entry);
3369  }
3370  }
3371 }
3372 
3375  const std::string &tmp_var_prefix,
3376  const typet &tmp_var_type,
3377  code_blockt &block,
3378  exprt &stack_entry)
3379 {
3380  const exprt tmp_var=
3381  tmp_variable(tmp_var_prefix, tmp_var_type);
3382  block.add(code_assignt(tmp_var, stack_entry));
3383  stack_entry=tmp_var;
3384 }
messaget
Class that provides messages with a built-in verbosity 'level'.
Definition: message.h:152
java_bytecode_convert_methodt::instructionst
methodt::instructionst instructionst
Definition: java_bytecode_convert_method_class.h:62
java_bytecode_convert_methodt::convert_invoke
void convert_invoke(source_locationt location, const irep_idt &statement, class_method_descriptor_exprt &class_method_descriptor, codet &c, exprt::operandst &results)
Definition: java_bytecode_convert_method.cpp:2192
exprt::copy_to_operands
void copy_to_operands(const exprt &expr)
Copy the given argument to the end of exprt's operands.
Definition: expr.h:148
java_bytecode_convert_methodt::convert_pop
codet convert_pop(const irep_idt &statement, const exprt::operandst &op)
Definition: java_bytecode_convert_method.cpp:1997
tag_typet::get_identifier
const irep_idt & get_identifier() const
Definition: std_types.h:451
java_static_initializers.h
BC_iinc
#define BC_iinc
Definition: bytecode_info.h:197
symbol_table_baset::has_symbol
bool has_symbol(const irep_idt &name) const
Check whether a symbol exists in the symbol table.
Definition: symbol_table_base.h:87
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:37
java_bytecode_parse_treet::methodt::throws_exception_table
std::vector< irep_idt > throws_exception_table
Definition: java_bytecode_parse_tree.h:124
java_bytecode_parse_treet::membert::signature
optionalt< std::string > signature
Definition: java_bytecode_parse_tree.h:67
java_char_from_type
char java_char_from_type(const typet &type)
Definition: java_types.cpp:706
ieee_floatt
Definition: ieee_float.h:120
code_blockt
A codet representing sequential composition of program statements.
Definition: std_code.h:173
typecast_exprt::conditional_cast
static exprt conditional_cast(const exprt &expr, const typet &type)
Definition: std_expr.h:2050
cfg.h
java_method_typet::add_throws_exception
void add_throws_exception(irep_idt exception)
Definition: java_types.h:134
symbol_tablet
The symbol table.
Definition: symbol_table.h:20
java_bytecode_convert_methodt::threading_support
const bool threading_support
Definition: java_bytecode_convert_method_class.h:82
symbol_table_baset::lookup_ref
const symbolt & lookup_ref(const irep_idt &name) const
Find a symbol in the symbol table for read-only access.
Definition: symbol_table_base.h:104
typet::subtype
const typet & subtype() const
Definition: type.h:47
java_bytecode_convert_methodt::convert_load
exprt convert_load(const exprt &index, char type_char, size_t address)
Load reference from local variable.
Definition: java_bytecode_convert_method.cpp:2957
java_bytecode_parse_treet::methodt::exceptiont::catch_type
struct_tag_typet catch_type
Definition: java_bytecode_parse_tree.h:118
code_switch_caset
codet representation of a switch-case, i.e. a case statement within a switch.
Definition: std_code.h:1449
source_locationt::as_string
std::string as_string() const
Definition: source_location.h:26
java_bytecode_parse_treet::membert::is_final
bool is_final
Definition: java_bytecode_parse_tree.h:69
class_hierarchyt
Non-graph-based representation of the class hierarchy.
Definition: class_hierarchy.h:43
bytecode_infot
Definition: bytecode_info.h:45
BC_ifeq
#define BC_ifeq
Definition: bytecode_info.h:218
source_locationt::set_comment
void set_comment(const irep_idt &comment)
Definition: source_location.h:141
java_bytecode_convert_methodt::current_method
irep_idt current_method
A copy of method_id :/.
Definition: java_bytecode_convert_method_class.h:91
class_method_descriptor_exprt::mangled_method_name
const irep_idt & mangled_method_name() const
The method name after mangling it by combining it with the descriptor.
Definition: std_expr.h:4574
BC_putstatic
#define BC_putstatic
Definition: bytecode_info.h:244
java_bytecode_convert_methodt::method_id
irep_idt method_id
Fully qualified name of the method under translation.
Definition: java_bytecode_convert_method_class.h:88
bytecode_infot::mnemonic
const char * mnemonic
Definition: bytecode_info.h:46
arith_tools.h
java_reference_type
reference_typet java_reference_type(const typet &subtype)
Definition: java_types.cpp:89
source_locationt::set_function
void set_function(const irep_idt &function)
Definition: source_location.h:126
java_bytecode_parse_treet::methodt::is_abstract
bool is_abstract
Definition: java_bytecode_parse_tree.h:87
to_struct_type
const struct_typet & to_struct_type(const typet &type)
Cast a typet to a struct_typet.
Definition: std_types.h:303
java_bytecode_parse_treet::membert::annotations
annotationst annotations
Definition: java_bytecode_parse_tree.h:70
java_bytecode_convert_methodt::setup_local_variables
void setup_local_variables(const methodt &m, const address_mapt &amap)
See find_initializers_for_slot above for more detail.
Definition: java_local_variable_table.cpp:744
struct_union_typet::componentt::set_access
void set_access(const irep_idt &access)
Definition: std_types.h:99
java_bytecode_convert_methodt::convert_if_cmp
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
Definition: java_bytecode_convert_method.cpp:2864
assign_parameter_names
static void assign_parameter_names(java_method_typet &ftype, const irep_idt &name_prefix, symbol_table_baset &symbol_table)
Iterates through the parameters of the function type ftype, finds a new new name for each parameter a...
Definition: java_bytecode_convert_method.cpp:67
set_declaring_class
void set_declaring_class(symbolt &symbol, const irep_idt &declaring_class)
Sets the identifier of the class which declared a given symbol to declaring_class.
Definition: java_utils.cpp:506
reference_typet
The reference type.
Definition: std_types.h:1553
BC_goto_w
#define BC_goto_w
Definition: bytecode_info.h:265
java_bytecode_convert_methodt::convert_dup2
void convert_dup2(exprt::operandst &op, exprt::operandst &results)
Definition: java_bytecode_convert_method.cpp:2086
BC_ifnonnull
#define BC_ifnonnull
Definition: bytecode_info.h:264
irept::make_nil
void make_nil()
Definition: irep.h:475
CHECK_RETURN
#define CHECK_RETURN(CONDITION)
Definition: invariant.h:496
exprt::size
std::size_t size() const
Amount of nodes this expression tree contains.
Definition: expr.cpp:26
create_parameter_symbols
void create_parameter_symbols(const java_method_typet::parameterst &parameters, expanding_vectort< std::vector< java_bytecode_convert_methodt::variablet >> &variables, symbol_table_baset &symbol_table)
Definition: java_bytecode_convert_method.cpp:520
java_bytecode_convert_method_class.h
typet
The type of an expression, extends irept.
Definition: type.h:29
can_cast_type< reference_typet >
bool can_cast_type< reference_typet >(const typet &type)
Check whether a reference to a typet is a reference_typet.
Definition: std_types.h:1566
BC_pop2
#define BC_pop2
Definition: bytecode_info.h:153
java_bytecode_parse_treet::methodt
Definition: java_bytecode_parse_tree.h:85
code_ifthenelset::then_case
const codet & then_case() const
Definition: std_code.h:777
BC_jsr
#define BC_jsr
Definition: bytecode_info.h:233
BC_ifgt
#define BC_ifgt
Definition: bytecode_info.h:222
code_switch_caset::set_default
void set_default()
Definition: std_code.h:1461
threeval.h
to_class_type
const class_typet & to_class_type(const typet &type)
Cast a typet to a class_typet.
Definition: std_types.h:376
get_if_cmp_operator
static irep_idt get_if_cmp_operator(const u1 bytecode)
Definition: java_bytecode_convert_method.cpp:643
java_array_element_type
const typet & java_array_element_type(const struct_tag_typet &array_symbol)
Return a const reference to the element type of a given java array type.
Definition: java_types.cpp:145
symbolt::type
typet type
Type of symbol.
Definition: symbol.h:31
dereference_exprt
Operator to dereference a pointer.
Definition: std_expr.h:2917
java_bytecode_convert_methodt::try_catch_handler
std::vector< method_offsett > try_catch_handler(method_offsett address, const java_bytecode_parse_treet::methodt::exception_tablet &exception_table) const
Definition: java_bytecode_convert_method.cpp:3121
unsupported_java_class_signature_exceptiont
An exception that is raised for unsupported class signature.
Definition: java_types.h:1075
java_bytecode_parse_treet::methodt::local_variable_table
local_variable_tablet local_variable_table
Definition: java_bytecode_parse_tree.h:137
checked_dereference
dereference_exprt checked_dereference(const exprt &expr)
Dereference an expression and flag it for a null-pointer check.
Definition: java_utils.cpp:212
mp_integer
BigInt mp_integer
Definition: mp_arith.h:19
java_bytecode_convert_methodt::needed_lazy_methods
optionalt< ci_lazy_methods_neededt > needed_lazy_methods
Definition: java_bytecode_convert_method_class.h:83
if_exprt
The trinary if-then-else operator.
Definition: std_expr.h:2993
fieldref_exprt
Represents the argument of an instruction that uses a CONSTANT_Fieldref This is used for example as a...
Definition: java_bytecode_parse_tree.h:329
java_bytecode_convert_methodt::ns
namespacet ns
Definition: java_bytecode_convert_method_class.h:78
symbol_exprt::typeless
static symbol_exprt typeless(const irep_idt &id)
Generate a symbol_exprt without a proper type.
Definition: std_expr.h:101
string2integer
const mp_integer string2integer(const std::string &n, unsigned base)
Definition: mp_arith.cpp:57
java_bytecode_convert_methodt::label
static irep_idt label(const irep_idt &address)
Definition: java_bytecode_convert_method.cpp:167
declaring_class
optionalt< irep_idt > declaring_class(const symbolt &symbol)
Gets the identifier of the class which declared a given symbol.
Definition: java_utils.cpp:500
java_bytecode_convert_methodt::variable
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...
Definition: java_bytecode_convert_method.cpp:205
irept::find
const irept & find(const irep_namet &name) const
Definition: irep.cpp:103
prefix.h
replace
void replace(const union_find_replacet &replace_map, string_not_contains_constraintt &constraint)
Definition: string_constraint.cpp:67
invariant.h
code_declt
A codet representing the declaration of a local variable.
Definition: std_code.h:405
code_assertt
A non-fatal assertion, which checks a condition then permits execution to continue.
Definition: std_code.h:590
java_string_literal_expr.h
java_method_typet::parameterst
std::vector< parametert > parameterst
Definition: std_types.h:738
can_cast_expr< java_string_literal_exprt >
bool can_cast_expr< java_string_literal_exprt >(const exprt &base)
Definition: java_string_literal_expr.h:33
java_bytecode_convert_methodt::convert_iinc
code_blockt convert_iinc(const exprt &arg0, const exprt &arg1, const source_locationt &location, method_offsett address)
Definition: java_bytecode_convert_method.cpp:2767
plus_exprt
The plus expression Associativity is not specified.
Definition: std_expr.h:887
rem_exprt
Remainder of division.
Definition: std_expr.h:1151
exprt
Base class for all expressions.
Definition: expr.h:53
java_bytecode_convert_methodt::convert_ifnonull
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
Definition: java_bytecode_convert_method.cpp:2818
java_string_library_preprocess.h
symbolt::base_name
irep_idt base_name
Base (non-scoped) name.
Definition: symbol.h:46
code_push_catcht
Pushes an exception handler, of the form: exception_tag1 -> label1 exception_tag2 -> label2 ....
Definition: std_code.h:2258
struct_tag_typet
A struct tag type, i.e., struct_typet with an identifier.
Definition: std_types.h:490
java_bytecode_convert_methodt::block_tree_nodet::branch
std::vector< block_tree_nodet > branch
Definition: java_bytecode_convert_method_class.h:261
java_bytecode_convert_method
void java_bytecode_convert_method(const symbolt &class_symbol, const java_bytecode_parse_treet::methodt &method, 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, const optionalt< prefix_filtert > &method_context, bool assert_no_exceptions_thrown)
Definition: java_bytecode_convert_method.cpp:3225
java_bytecode_convert_methodt::convert_multianewarray
code_blockt convert_multianewarray(const source_locationt &location, const exprt &arg0, const exprt::operandst &op, exprt::operandst &results)
Definition: java_bytecode_convert_method.cpp:2503
java_bytecode_parse_treet::find_annotation
static optionalt< annotationt > find_annotation(const annotationst &annotations, const irep_idt &annotation_type_name)
Find an annotation given its name.
Definition: java_bytecode_parse_tree.cpp:97
java_expr.h
fieldref_exprt::component_name
irep_idt component_name() const
Definition: java_bytecode_parse_tree.h:346
irep_idt
dstringt irep_idt
Definition: irep.h:32
java_bytecode_convert_methodt::converted_instructiont
Definition: java_bytecode_convert_method_class.h:221
bool_typet
The Boolean type.
Definition: std_types.h:37
tvt::is_unknown
bool is_unknown() const
Definition: threeval.h:27
code_ifthenelset::cond
const exprt & cond() const
Definition: std_code.h:767
to_string
std::string to_string(const string_not_contains_constraintt &expr)
Used for debug printing.
Definition: string_constraint.cpp:55
BC_getstatic
#define BC_getstatic
Definition: bytecode_info.h:243
messaget::eom
static eomt eom
Definition: message.h:283
create_parameter_names
void create_parameter_names(const java_bytecode_parse_treet::methodt &m, const irep_idt &method_identifier, java_method_typet::parameterst &parameters, const java_bytecode_convert_methodt::method_offsett &slots_for_parameters)
Extracts the names of parameters from the local variable table in the method, and uses it to construc...
Definition: java_bytecode_convert_method.cpp:435
java_bytecode_convert_methodt::convert_newarray
code_blockt convert_newarray(const source_locationt &location, const irep_idt &statement, const exprt &arg0, const exprt::operandst &op, exprt::operandst &results)
Definition: java_bytecode_convert_method.cpp:2530
auxiliary_symbolt
Internally generated symbol table entry.
Definition: symbol.h:160
BC_irem
#define BC_irem
Definition: bytecode_info.h:177
java_bytecode_convert_methodt::convert_ret
code_blockt convert_ret(const std::vector< method_offsett > &jsr_ret_targets, const exprt &arg0, const source_locationt &location, const method_offsett address)
Definition: java_bytecode_convert_method.cpp:2888
adjust_invoke_argument_types
static void adjust_invoke_argument_types(const java_method_typet::parameterst &parameters, code_function_callt::argumentst &arguments)
Definition: java_bytecode_convert_method.cpp:2168
lshr_exprt
Logical right shift.
Definition: std_expr.h:2643
java_method_typet::set_is_varargs
void set_is_varargs(bool is_varargs)
Definition: java_types.h:164
get_method_identifier
static irep_idt get_method_identifier(const irep_idt &class_identifier, const java_bytecode_parse_treet::methodt &method)
Definition: java_bytecode_convert_method.cpp:426
div_exprt
Division.
Definition: std_expr.h:1037
symbol_exprt
Expression to hold a symbol (variable)
Definition: std_expr.h:82
code_pop_catcht
Pops an exception handler from the stack of active handlers (i.e.
Definition: std_code.h:2352
java_bytecode_convert_methodt::variables
expanding_vectort< variablest > variables
Definition: java_bytecode_convert_method_class.h:167
java_bytecode_parse_treet::methodt::parameter_annotations
std::vector< annotationst > parameter_annotations
Java annotations that were applied to parameters of this method.
Definition: java_bytecode_parse_tree.h:106
namespace.h
java_bytecode_convert_methodt::convert_cmp2
exprt::operandst & convert_cmp2(const irep_idt &statement, const exprt::operandst &op, exprt::operandst &results) const
Definition: java_bytecode_convert_method.cpp:2700
get_bytecode_type_width
static std::size_t get_bytecode_type_width(const typet &ty)
Definition: java_bytecode_convert_method.cpp:1012
equal_exprt
Equality.
Definition: std_expr.h:1196
struct_union_typet::componentt::set_name
void set_name(const irep_idt &name)
Definition: std_types.h:79
java_bytecode_convert_methodt::convert_cmp
exprt::operandst & convert_cmp(const exprt::operandst &op, exprt::operandst &results) const
Definition: java_bytecode_convert_method.cpp:2728
expanding_vectort
Definition: expanding_vector.h:17
BC_putfield
#define BC_putfield
Definition: bytecode_info.h:246
code_typet::get_is_constructor
bool get_is_constructor() const
Definition: std_types.h:887
pattern.h
code_ifthenelset
codet representation of an if-then-else statement.
Definition: std_code.h:749
zero_initializer
optionalt< exprt > zero_initializer(const typet &type, const source_locationt &source_location, const namespacet &ns)
Create the equivalent of zero for type type.
Definition: expr_initializer.cpp:318
source_locationt::get_line
const irep_idt & get_line() const
Definition: source_location.h:46
notequal_exprt
Disequality.
Definition: std_expr.h:1254
code_labelt::code
codet & code()
Definition: std_code.h:1403
unsignedbv_typet
Fixed-width bit-vector with unsigned binary interpretation.
Definition: std_types.h:1216
resolve_inherited_component.h
conditional_array_cast
static exprt conditional_array_cast(const exprt &expr, char type_char)
Add typecast if necessary to expr to make it compatible with array type corresponding to type_char (s...
Definition: java_bytecode_convert_method.cpp:2925
java_bytecode_convert_methodt::convert_instructions
code_blockt convert_instructions(const methodt &)
Definition: java_bytecode_convert_method.cpp:1068
symbolt::pretty_name
irep_idt pretty_name
Language-specific display name.
Definition: symbol.h:52
cfg_dominators.h
ieee_float_spect
Definition: ieee_float.h:26
to_code
const codet & to_code(const exprt &expr)
Definition: std_code.h:158
java_bytecode_convert_methodt::block_tree_nodet::get_leaf
static block_tree_nodet get_leaf()
Definition: java_bytecode_convert_method_class.h:271
class_method_descriptor_exprt
An expression describing a method on a class.
Definition: std_expr.h:4537
BC_drem
#define BC_drem
Definition: bytecode_info.h:180
BC_monitorenter
#define BC_monitorenter
Definition: bytecode_info.h:259
java_bytecode_convert_methodt::variablet::symbol_expr
symbol_exprt symbol_expr
Definition: java_bytecode_convert_method_class.h:124
java_bytecode_parse_treet::membert::descriptor
std::string descriptor
Definition: java_bytecode_parse_tree.h:66
strip_java_namespace_prefix
irep_idt strip_java_namespace_prefix(const irep_idt &to_strip)
Strip java:: prefix from given identifier.
Definition: java_utils.cpp:338
BC_ldc
#define BC_ldc
Definition: bytecode_info.h:83
code_blockt::statements
code_operandst & statements()
Definition: std_code.h:181
namespacet
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:92
message
static const char * message(const static_verifier_resultt::statust &status)
Makes a status message string from a status.
Definition: static_verifier.cpp:74
string2int.h
class_method_descriptor_exprt::get_identifier
const irep_idt & get_identifier() const
A unique identifier of the combination of class and method overload to which this expression refers.
Definition: std_expr.h:4597
exprt::type
typet & type()
Return the type of the expression.
Definition: expr.h:81
symbolt::is_thread_local
bool is_thread_local
Definition: symbol.h:65
namespacet::lookup
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See documentation for namespace_baset::lookup().
Definition: namespace.cpp:140
code_function_callt
codet representation of a function call statement.
Definition: std_code.h:1186
java_method_typet::set_native
void set_native(bool is_native)
Definition: java_types.h:154
to_code_type
const code_typet & to_code_type(const typet &type)
Cast a typet to a code_typet.
Definition: std_types.h:946
expr_initializer.h
java_bytecode_convert_methodt::convert_new
void convert_new(const source_locationt &location, const exprt &arg0, codet &c, exprt::operandst &results)
Definition: java_bytecode_convert_method.cpp:2588
bitxor_exprt
Bit-wise XOR.
Definition: std_expr.h:2453
symbolt::mode
irep_idt mode
Language mode.
Definition: symbol.h:49
java_bytecode_convert_methodt::convert_switch
code_switcht convert_switch(const exprt::operandst &op, const java_bytecode_parse_treet::instructiont::argst &args, const source_locationt &location)
Definition: java_bytecode_convert_method.cpp:2013
java_type_from_char
typet java_type_from_char(char t)
Constructs a type indicated by the given character:
Definition: java_types.cpp:247
bitor_exprt
Bit-wise OR.
Definition: std_expr.h:2417
messaget::result
mstreamt & result() const
Definition: message.h:395
messaget::error
mstreamt & error() const
Definition: message.h:385
ieee_float_spect::double_precision
static ieee_float_spect double_precision()
Definition: ieee_float.h:80
BC_dup2
#define BC_dup2
Definition: bytecode_info.h:157
or_exprt
Boolean OR.
Definition: std_expr.h:2274
java_bytecode_convert_methodt::get_clinit_call
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...
Definition: java_bytecode_convert_method.cpp:997
code_typet::get_access
const irep_idt & get_access() const
Definition: std_types.h:877
java_bytecode_convert_methodt::convert_if
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
Definition: java_bytecode_convert_method.cpp:2840
convert_annotations
void convert_annotations(const java_bytecode_parse_treet::annotationst &parsed_annotations, std::vector< java_annotationt > &java_annotations)
Convert parsed annotations into the symbol table.
Definition: java_bytecode_convert_class.cpp:1133
java_bytecode_parse_treet::membert::is_static
bool is_static
Definition: java_bytecode_parse_tree.h:69
DATA_INVARIANT
#define DATA_INVARIANT(CONDITION, REASON)
This condition should be used to document that assumptions that are made on goto_functions,...
Definition: invariant.h:511
class_method_descriptor_exprt::base_method_name
const irep_idt & base_method_name() const
The name of the method to which this expression is applied as would be seen in the source code.
Definition: std_expr.h:4589
member_type_lazy
java_method_typet member_type_lazy(const std::string &descriptor, const optionalt< std::string > &signature, const std::string &class_name, const std::string &method_name, message_handlert &message_handler)
Returns the member type for a method, based on signature or descriptor.
Definition: java_bytecode_convert_method.cpp:238
java_bytecode_convert_methodt::method_return_type
typet method_return_type
Return type of the method under conversion.
Definition: java_bytecode_convert_method_class.h:95
java_bytecode_convert_methodt::max_array_length
const size_t max_array_length
Definition: java_bytecode_convert_method_class.h:79
null_pointer_exprt
The null pointer constant.
Definition: std_expr.h:4018
symbol_table_baset::get_writeable_ref
symbolt & get_writeable_ref(const irep_idt &name)
Find a symbol in the symbol table for read-write access.
Definition: symbol_table_base.h:121
id2string
const std::string & id2string(const irep_idt &d)
Definition: irep.h:44
java_bytecode_convert_methodt::tmp_vars
std::list< symbol_exprt > tmp_vars
Definition: java_bytecode_convert_method_class.h:195
BC_nop
#define BC_nop
Definition: bytecode_info.h:65
java_bytecode_convert_methodt::get_static_field
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...
Definition: java_bytecode_convert_method.cpp:3273
java_bytecode_convert_methodt::get_or_create_block_for_pcrange
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...
Definition: java_bytecode_convert_method.cpp:779
java_bytecode_convert_methodt::method_offsett
uint16_t method_offsett
Definition: java_bytecode_convert_method_class.h:74
java_bytecode_parse_treet::methodt::source_location
source_locationt source_location
Definition: java_bytecode_parse_tree.h:89
messaget::mstreamt::source_location
source_locationt source_location
Definition: message.h:244
branch
void branch(goto_modelt &goto_model, const irep_idt &id)
Definition: branch.cpp:20
java_bytecode_convert_methodt::throw_assertion_error
const bool throw_assertion_error
Definition: java_bytecode_convert_method_class.h:80
BC_newarray
#define BC_newarray
Definition: bytecode_info.h:253
to_code_goto
const code_gotot & to_code_goto(const codet &code)
Definition: std_code.h:1164
forall_operands
#define forall_operands(it, expr)
Definition: expr.h:18
code_typet::set_is_constructor
void set_is_constructor()
Definition: std_types.h:892
bytecode_infot::pop
unsigned pop
Definition: bytecode_info.h:49
java_bytecode_parse_treet::methodt::instructions
instructionst instructions
Definition: java_bytecode_parse_tree.h:92
code_gotot
codet representation of a goto statement.
Definition: std_code.h:1130
java_bytecode_convert_methodt::assert_no_exceptions_thrown
const bool assert_no_exceptions_thrown
Definition: java_bytecode_convert_method_class.h:81
codet::make_block
class code_blockt & make_block()
If this codet is a code_blockt (i.e. it represents a block of statements), return the unmodified inpu...
Definition: std_code.cpp:23
java_bytecode_convert_methodt::bytecode_write_typet::STATIC_FIELD
@ STATIC_FIELD
code_labelt
codet representation of a label for branch targets.
Definition: std_code.h:1378
PRECONDITION
#define PRECONDITION(CONDITION)
Definition: invariant.h:464
BC_new
#define BC_new
Definition: bytecode_info.h:252
BC_multianewarray
#define BC_multianewarray
Definition: bytecode_info.h:262
symbol_exprt::get_identifier
const irep_idt & get_identifier() const
Definition: std_expr.h:111
symbol_table_baset
The symbol table base class interface.
Definition: symbol_table_base.h:22
nil_exprt
The NIL expression.
Definition: std_expr.h:4002
struct_union_typet::componentt::set_pretty_name
void set_pretty_name(const irep_idt &name)
Definition: std_types.h:109
code_assumet
An assumption, which must hold in subsequent code.
Definition: std_code.h:538
java_bytecode_convert_methodt::find_variable_for_slot
const variablet & find_variable_for_slot(size_t address, variablest &var_list)
See above.
Definition: java_local_variable_table.cpp:858
struct_union_typet::componentt::set_base_name
void set_base_name(const irep_idt &base_name)
Definition: std_types.h:89
BC_frem
#define BC_frem
Definition: bytecode_info.h:179
BC_swap
#define BC_swap
Definition: bytecode_info.h:160
java_string_library_preprocesst::replace_character_call
codet replace_character_call(code_function_callt call)
Definition: java_string_library_preprocess.h:54
to_code_label
const code_labelt & to_code_label(const codet &code)
Definition: std_code.h:1430
BC_iconst_m1
#define BC_iconst_m1
Definition: bytecode_info.h:67
symbolt::symbol_expr
class symbol_exprt symbol_expr() const
Produces a symbol_exprt for a symbol.
Definition: symbol.cpp:122
java_array_type
java_reference_typet java_array_type(const char subtype)
Construct an array pointer type.
Definition: java_types.cpp:110
BC_aconst_null
#define BC_aconst_null
Definition: bytecode_info.h:66
java_bytecode_convert_methodt::block_tree_nodet::branch_addresses
std::vector< method_offsett > branch_addresses
Definition: java_bytecode_convert_method_class.h:260
java_bytecode_convert_methodt::slots_for_parameters
method_offsett slots_for_parameters
Number of local variable slots used by the JVM to pass parameters upon invocation of the method under...
Definition: java_bytecode_convert_method_class.h:102
BC_ifge
#define BC_ifge
Definition: bytecode_info.h:221
bytecode_info.h
mult_exprt
Binary multiplication Associativity is not specified.
Definition: std_expr.h:992
uncaught_exceptions_domaint::get_exception_type
static irep_idt get_exception_type(const typet &type)
Returns the compile type of an exception.
Definition: uncaught_exceptions_analysis.cpp:18
java_bytecode_parse_treet::methodt::exceptiont
Definition: java_bytecode_parse_tree.h:109
code_push_catcht::exception_list
exception_listt & exception_list()
Definition: std_code.h:2313
BC_anewarray
#define BC_anewarray
Definition: bytecode_info.h:254
BC_invokedynamic
#define BC_invokedynamic
Definition: bytecode_info.h:251
pointer_type
pointer_typet pointer_type(const typet &subtype)
Definition: c_types.cpp:243
BC_invokespecial
#define BC_invokespecial
Definition: bytecode_info.h:248
unary_minus_exprt
The unary minus expression.
Definition: std_expr.h:378
BC_iflt
#define BC_iflt
Definition: bytecode_info.h:220
java_bytecode_convert_methodt::convert_dup2_x1
void convert_dup2_x1(exprt::operandst &op, exprt::operandst &results)
Definition: java_bytecode_convert_method.cpp:2099
java_bytecode_parse_treet::methodt::base_name
irep_idt base_name
Definition: java_bytecode_parse_tree.h:86
irept::swap
void swap(irept &irep)
Definition: irep.h:463
to_symbol_expr
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
Definition: std_expr.h:177
java_bytecode_convert_methodt::method_has_this
bool method_has_this
Definition: java_bytecode_convert_method_class.h:169
pretty_signature
std::string pretty_signature(const java_method_typet &method_type)
Definition: java_types.cpp:1123
java_bytecode_convert_methodt::convert_putstatic
code_blockt convert_putstatic(const source_locationt &location, const exprt &arg0, const exprt::operandst &op, const symbol_exprt &symbol_expr)
Definition: java_bytecode_convert_method.cpp:2612
java_bytecode_convert_methodt::bytecode_write_typet::VARIABLE
@ VARIABLE
java_bytecode_parse_treet::methodt::exceptiont::handler_pc
std::size_t handler_pc
Definition: java_bytecode_parse_tree.h:117
irept::id
const irep_idt & id() const
Definition: irep.h:418
message_handlert
Definition: message.h:27
create_method_stub_symbol
void create_method_stub_symbol(const irep_idt &identifier, const irep_idt &base_name, const irep_idt &pretty_name, const typet &type, const irep_idt &declaring_class, symbol_table_baset &symbol_table, message_handlert &message_handler)
Definition: java_bytecode_convert_method.cpp:99
java_bytecode_convert_methodt::convert_ushr
exprt::operandst & convert_ushr(const irep_idt &statement, const exprt::operandst &op, exprt::operandst &results) const
Definition: java_bytecode_convert_method.cpp:2748
java_bytecode_convert_method.h
to_struct_tag_type
const struct_tag_typet & to_struct_tag_type(const typet &type)
Cast a typet to a struct_tag_typet.
Definition: std_types.h:515
tvt::unknown
static tvt unknown()
Definition: threeval.h:33
java_bytecode_convert_methodt::convert_getstatic
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)
Definition: java_bytecode_convert_method.cpp:2654
exprt::operandst
std::vector< exprt > operandst
Definition: expr.h:55
code_function_callt::argumentst
exprt::operandst argumentst
Definition: std_code.h:1195
dstringt::empty
bool empty() const
Definition: dstring.h:88
BC_ifne
#define BC_ifne
Definition: bytecode_info.h:219
code_blockt::add
void add(const codet &code)
Definition: std_code.h:211
false_exprt
The Boolean constant false.
Definition: std_expr.h:3993
java_reference_array_type
java_reference_typet java_reference_array_type(const struct_tag_typet &subtype)
Definition: java_types.cpp:540
java_bytecode_convert_methodt::bytecode_write_typet::ARRAY_REF
@ ARRAY_REF
BC_dup_x1
#define BC_dup_x1
Definition: bytecode_info.h:155
BC_invokevirtual
#define BC_invokevirtual
Definition: bytecode_info.h:247
java_bytecode_parse_treet::membert::is_public
bool is_public
Definition: java_bytecode_parse_tree.h:69
code_labelt::set_label
void set_label(const irep_idt &label)
Definition: std_code.h:1398
java_local_variable_slots
unsigned java_local_variable_slots(const typet &t)
Returns the number of JVM local variables (slots) taken by a local variable that, when translated to ...
Definition: java_utils.cpp:55
bitand_exprt
Bit-wise AND.
Definition: std_expr.h:2489
BC_ifle
#define BC_ifle
Definition: bytecode_info.h:223
java_int_type
signedbv_typet java_int_type()
Definition: java_types.cpp:32
code_typet::parameters
const parameterst & parameters() const
Definition: std_types.h:857
to_pointer_type
const pointer_typet & to_pointer_type(const typet &type)
Cast a typet to a pointer_typet.
Definition: std_types.h:1526
ieee_floatt::from_integer
void from_integer(const mp_integer &i)
Definition: ieee_float.cpp:511
java_bytecode_convert_methodt::convert_checkcast
void convert_checkcast(const exprt &arg0, const exprt::operandst &op, codet &c, exprt::operandst &results) const
Definition: java_bytecode_convert_method.cpp:2368
java_bytecode_convert_methodt::get_block_for_pcrange
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 ...
Definition: java_bytecode_convert_method.cpp:742
optionalt
nonstd::optional< T > optionalt
Definition: optional.h:35
java_type_from_string
optionalt< typet > java_type_from_string(const std::string &src, const std::string &class_name_prefix)
Transforms a string representation of a Java type into an internal type representation thereof.
Definition: java_types.cpp:560
get_inherited_component
optionalt< resolve_inherited_componentt::inherited_componentt > get_inherited_component(const irep_idt &component_class_id, const irep_idt &component_name, const symbol_tablet &symbol_table, bool include_interfaces)
Finds an inherited component (method or field), taking component visibility into account.
Definition: java_utils.cpp:379
tvt
Definition: threeval.h:20
java_short_type
signedbv_typet java_short_type()
Definition: java_types.cpp:50
minus_exprt
Binary minus.
Definition: std_expr.h:946
java_bytecode_parse_treet::methodt::exceptiont::end_pc
std::size_t end_pc
Definition: java_bytecode_parse_tree.h:116
code_typet::has_this
bool has_this() const
Definition: std_types.h:818
code_typet::parametert::set_base_name
void set_base_name(const irep_idt &name)
Definition: std_types.h:787
java_bytecode_parse_treet::methodt::exception_tablet
std::vector< exceptiont > exception_tablet
Definition: java_bytecode_parse_tree.h:121
remove_returns.h
remove_exceptions.h
java_bytecode_convert_methodt
Definition: java_bytecode_convert_method_class.h:33
code_typet::parametert::set_this
void set_this()
Definition: std_types.h:807
source_locationt
Definition: source_location.h:20
java_bytecode_convert_methodt::convert_putfield
code_blockt convert_putfield(const fieldref_exprt &arg0, const exprt::operandst &op)
Definition: java_bytecode_convert_method.cpp:2643
java_bytecode_parse_treet::methodt::is_synthetic
bool is_synthetic
Definition: java_bytecode_parse_tree.h:88
simplify_expr.h
java_byte_type
signedbv_typet java_byte_type()
Definition: java_types.cpp:56
bitvector_typet::get_width
std::size_t get_width() const
Definition: std_types.h:1041
java_bytecode_convert_methodt::convert_const
exprt::operandst & convert_const(const irep_idt &statement, const constant_exprt &arg0, exprt::operandst &results) const
Definition: java_bytecode_convert_method.cpp:2133
bytecode_infot::push
unsigned push
Definition: bytecode_info.h:49
symbol_table_baset::add
bool add(const symbolt &symbol)
Add a new symbol to the symbol table.
Definition: symbol_table_base.cpp:18
member_exprt
Extract member of struct or union.
Definition: std_expr.h:3434
java_bytecode_convert_methodt::string_preprocess
java_string_library_preprocesst & string_preprocess
Definition: java_bytecode_convert_method_class.h:97
struct_union_typet::componentt
Definition: std_types.h:64
lambda_synthesis.h
merge_source_location_rec
void merge_source_location_rec(exprt &expr, const source_locationt &source_location)
Attaches a source location to an expression and all of its subexpressions.
Definition: java_utils.cpp:128
java_bytecode_convert_methodt::do_exception_handling
codet & do_exception_handling(const methodt &method, const std::set< method_offsett > &working_set, method_offsett cur_pc, codet &c)
Definition: java_bytecode_convert_method.cpp:2422
irept::get_string
const std::string & get_string(const irep_namet &name) const
Definition: irep.h:431
java_method_typet::set_is_final
void set_is_final(bool is_final)
Definition: java_types.h:144
java_bytecode_convert_methodt::create_stack_tmp_var
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
Definition: java_bytecode_convert_method.cpp:3374
symbolt::value
exprt value
Initial value of symbol.
Definition: symbol.h:34
messaget::get_message_handler
message_handlert & get_message_handler()
Definition: message.h:181
BC_ret
#define BC_ret
Definition: bytecode_info.h:234
BC_athrow
#define BC_athrow
Definition: bytecode_info.h:256
BC_dup_x2
#define BC_dup_x2
Definition: bytecode_info.h:156
class_hierarchy.h
BC_tableswitch
#define BC_tableswitch
Definition: bytecode_info.h:235
java_bytecode_parse_treet::membert::name
irep_idt name
Definition: java_bytecode_parse_tree.h:68
fieldref_exprt::class_name
irep_idt class_name() const
Definition: java_bytecode_parse_tree.h:341
java_bytecode_convert_methodt::block_tree_nodet::leaf
bool leaf
Definition: java_bytecode_convert_method_class.h:259
java_bytecode_parse_treet::methodt::local_variable_tablet
std::vector< local_variablet > local_variable_tablet
Definition: java_bytecode_parse_tree.h:136
class_typet::methods
const methodst & methods() const
Definition: std_types.h:330
java_bytecode_parse_treet::instructiont::argst
std::vector< exprt > argst
Definition: java_bytecode_parse_tree.h:60
code_skipt
A codet representing a skip statement.
Definition: std_code.h:273
java_bytecode_convert_method_lazy
void java_bytecode_convert_method_lazy(symbolt &class_symbol, const irep_idt &method_identifier, const java_bytecode_parse_treet::methodt &m, symbol_tablet &symbol_table, message_handlert &message_handler)
This creates a method symbol in the symtab, but doesn't actually perform method conversion just yet.
Definition: java_bytecode_convert_method.cpp:308
java_method_parameter_slots
unsigned java_method_parameter_slots(const java_method_typet &t)
Returns the the number of JVM local variables (slots) used by the JVM to pass, upon call,...
Definition: java_utils.cpp:74
code_returnt
codet representation of a "return from a function" statement.
Definition: std_code.h:1313
java_bytecode_parse_treet::membert::is_protected
bool is_protected
Definition: java_bytecode_parse_tree.h:69
java_bytecode_convert_methodt::convert_ifnull
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
Definition: java_bytecode_convert_method.cpp:2796
code_landingpadt
A statement that catches an exception, assigning the exception in flight to an expression (e....
Definition: std_code.h:2389
u1
uint8_t u1
Definition: bytecode_info.h:55
java_bytecode_parse_treet::membert::is_private
bool is_private
Definition: java_bytecode_parse_tree.h:69
java_bytecode_convert_methodt::pop
exprt::operandst pop(std::size_t n)
Definition: java_bytecode_convert_method.cpp:133
java_bytecode_parse_treet::methodt::is_native
bool is_native
Definition: java_bytecode_parse_tree.h:87
BC_idiv
#define BC_idiv
Definition: bytecode_info.h:173
namespace_baset::follow
const typet & follow(const typet &) const
Resolve type symbol to the type it points to.
Definition: namespace.cpp:51
java_bytecode_convert_methodt::used_local_names
std::set< symbol_exprt > used_local_names
Definition: java_bytecode_convert_method_class.h:168
irept::get
const irep_idt & get(const irep_namet &name) const
Definition: irep.cpp:51
symbolt::location
source_locationt location
Source code location of definition of symbol.
Definition: symbol.h:37
java_bytecode_parse_treet::methodt::is_bridge
bool is_bridge
Definition: java_bytecode_parse_tree.h:88
code_push_catcht::exception_listt
std::vector< exception_list_entryt > exception_listt
Definition: std_code.h:2302
code_switcht
codet representing a switch statement.
Definition: std_code.h:837
java_bytecode_parse_treet::methodt::exception_table
exception_tablet exception_table
Definition: java_bytecode_parse_tree.h:122
symbolt
Symbol table entry.
Definition: symbol.h:28
irept::set
void set(const irep_namet &name, const irep_idt &value)
Definition: irep.h:442
from_integer
constant_exprt from_integer(const mp_integer &int_value, const typet &type)
Definition: arith_tools.cpp:99
ieee_float.h
code_blockt::append
void append(const code_blockt &extra_block)
Add all the codets from extra_block to the current code_blockt.
Definition: std_code.cpp:105
java_bytecode_convert_methodt::address_mapt
std::map< method_offsett, converted_instructiont > address_mapt
Definition: java_bytecode_convert_method_class.h:238
ashr_exprt
Arithmetic right shift.
Definition: std_expr.h:2628
uncaught_exceptions_analysis.h
symbol_table_baset::symbols
const symbolst & symbols
Read-only field, used to look up symbols given their names.
Definition: symbol_table_base.h:30
java_char_type
unsignedbv_typet java_char_type()
Definition: java_types.cpp:62
symbolt::is_type
bool is_type
Definition: symbol.h:61
java_string_library_preprocesst
Definition: java_string_library_preprocess.h:36
java_bytecode_parse_treet::methodt::is_varargs
bool is_varargs
Definition: java_bytecode_parse_tree.h:88
binary_relation_exprt
A base class for relations, i.e., binary predicates whose two operands have the same type.
Definition: std_expr.h:725
BC_monitorexit
#define BC_monitorexit
Definition: bytecode_info.h:260
BC_return
#define BC_return
Definition: bytecode_info.h:242
java_bytecode_parse_treet::methodt::is_synchronized
bool is_synchronized
Definition: java_bytecode_parse_tree.h:87
BC_lookupswitch
#define BC_lookupswitch
Definition: bytecode_info.h:236
java_bytecode_promotion
typet java_bytecode_promotion(const typet &type)
Java does not support byte/short return types. These are always promoted.
Definition: java_types.cpp:268
BC_dup
#define BC_dup
Definition: bytecode_info.h:154
code_typet::parametert
Definition: std_types.h:753
java_bytecode_parse_treet::methodt::exceptiont::start_pc
std::size_t start_pc
Definition: java_bytecode_parse_tree.h:115
java_bytecode_convert_methodt::push
void push(const exprt::operandst &o)
Definition: java_bytecode_convert_method.cpp:158
symbolt::is_static_lifetime
bool is_static_lifetime
Definition: symbol.h:65
BC_invokestatic
#define BC_invokestatic
Definition: bytecode_info.h:249
clinit_wrapper_name
irep_idt clinit_wrapper_name(const irep_idt &class_name)
Get the Java static initializer wrapper name for a given class (the wrapper checks if static initiali...
Definition: java_static_initializers.cpp:65
BC_instanceof
#define BC_instanceof
Definition: bytecode_info.h:258
class_method_descriptor_exprt::class_id
const irep_idt & class_id() const
Unique identifier in the symbol table, of the compile time type of the class which this expression is...
Definition: std_expr.h:4582
java_boolean_type
c_bool_typet java_boolean_type()
Definition: java_types.cpp:80
symbol_table_baset::insert
virtual std::pair< symbolt &, bool > insert(symbolt symbol)=0
Move or copy a new symbol to the symbol table.
java_bytecode_convert_methodt::convert_monitorenterexit
codet convert_monitorenterexit(const irep_idt &statement, const exprt::operandst &op, const source_locationt &source_location)
Definition: java_bytecode_convert_method.cpp:2063
side_effect_expr_throwt
A side_effect_exprt representation of a side effect that throws an exception.
Definition: std_code.h:2210
java_bytecode_convert_methodt::convert_parameter_annotations
code_blockt convert_parameter_annotations(const methodt &method, const java_method_typet &method_type)
Definition: java_bytecode_convert_method.cpp:1019
code_typet::return_type
const typet & return_type() const
Definition: std_types.h:847
java_bytecode_convert_methodt::block_tree_nodet
Definition: java_bytecode_convert_method_class.h:258
to_code_block
const code_blockt & to_code_block(const codet &code)
Definition: std_code.h:259
java_bytecode_convert_methodt::draw_edges_from_ret_to_jsr
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
Definition: java_bytecode_convert_method.cpp:3104
java_method_typet::set_is_synthetic
void set_is_synthetic(bool is_synthetic)
Definition: java_types.h:174
java_bytecode_convert_methodt::tmp_variable
symbol_exprt tmp_variable(const std::string &prefix, const typet &type)
Definition: java_bytecode_convert_method.cpp:172
has_prefix
bool has_prefix(const std::string &s, const std::string &prefix)
Definition: converter.cpp:13
irept
There are a large number of kinds of tree structured or tree-like data in CPROVER.
Definition: irep.h:394
java_bytecode_convert_methodt::convert_astore
code_blockt convert_astore(const irep_idt &statement, const exprt::operandst &op, const source_locationt &location)
Definition: java_bytecode_convert_method.cpp:3003
constructor_symbol
static symbolt constructor_symbol(synthetic_methods_mapt &synthetic_methods, const irep_idt &synthetic_class_name, java_method_typet constructor_type)
Definition: lambda_synthesis.cpp:184
ieee_floatt::to_expr
constant_exprt to_expr() const
Definition: ieee_float.cpp:698
code_blockt::find_last_statement
codet & find_last_statement()
Definition: std_code.cpp:115
java_reference_typet
This is a specialization of reference_typet.
Definition: java_types.h:548
patternt
Given a string of the format '?blah?', will return true when compared against a string that matches a...
Definition: pattern.h:21
java_bytecode_convert_methodt::variablest
std::vector< variablet > variablest
Definition: java_bytecode_convert_method_class.h:166
symbolt::is_file_local
bool is_file_local
Definition: symbol.h:66
exprt::operands
operandst & operands()
Definition: expr.h:95
r
static int8_t r
Definition: irep_hash.h:59
symbolt::is_lvalue
bool is_lvalue
Definition: symbol.h:66
messaget::debug
mstreamt & debug() const
Definition: message.h:415
java_bytecode_convert_methodt::convert
void convert(const symbolt &class_symbol, const methodt &, const optionalt< prefix_filtert > &method_context)
Definition: java_bytecode_convert_method.cpp:547
BC_arraylength
#define BC_arraylength
Definition: bytecode_info.h:255
parameter_symbolt
Symbol table entry of function parameter.
Definition: symbol.h:184
BC_jsr_w
#define BC_jsr_w
Definition: bytecode_info.h:266
INVARIANT
#define INVARIANT(CONDITION, REASON)
This macro uses the wrapper function 'invariant_violated_string'.
Definition: invariant.h:424
java_types.h
java_bytecode_convert_methodt::convert_dup2_x2
void convert_dup2_x2(exprt::operandst &op, exprt::operandst &results)
Definition: java_bytecode_convert_method.cpp:2112
exprt::add_source_location
source_locationt & add_source_location()
Definition: expr.h:257
BC_dup2_x1
#define BC_dup2_x1
Definition: bytecode_info.h:158
to_java_method_type
const java_method_typet & to_java_method_type(const typet &type)
Definition: java_types.h:186
java_bytecode_convert_methodt::pop_residue
void pop_residue(std::size_t n)
removes minimum(n, stack.size()) elements from the stack
Definition: java_bytecode_convert_method.cpp:151
BC_ldc_w
#define BC_ldc_w
Definition: bytecode_info.h:84
typecast_exprt
Semantic type conversion.
Definition: std_expr.h:2042
bytecode_info
struct bytecode_infot const bytecode_info[]
Definition: bytecode_info.cpp:16
java_void_type
empty_typet java_void_type()
Definition: java_types.cpp:38
is_constructor
static bool is_constructor(const irep_idt &method_name)
Definition: java_bytecode_convert_method.cpp:128
code_assignt
A codet representing an assignment in the program.
Definition: std_code.h:298
java_bytecode_convert_methodt::convert_invoke_dynamic
optionalt< exprt > convert_invoke_dynamic(const source_locationt &location, std::size_t instruction_address, const exprt &arg0, codet &result_code)
Definition: java_bytecode_convert_method.cpp:3033
BC_lrem
#define BC_lrem
Definition: bytecode_info.h:178
java_utils.h
codet::get_statement
const irep_idt & get_statement() const
Definition: std_code.h:71
constant_exprt
A constant literal expression.
Definition: std_expr.h:3935
symbolt::module
irep_idt module
Name of module the symbol belongs to.
Definition: symbol.h:43
ieee_float_equal_exprt
IEEE-floating-point equality.
Definition: std_expr.h:3718
BC_checkcast
#define BC_checkcast
Definition: bytecode_info.h:257
BC_ldiv
#define BC_ldiv
Definition: bytecode_info.h:174
to_member
static member_exprt to_member(const exprt &pointer, const fieldref_exprt &field_reference, const namespacet &ns)
Build a member exprt for accessing a specific field that may come from a base class.
Definition: java_bytecode_convert_method.cpp:669
BC_pop
#define BC_pop
Definition: bytecode_info.h:152
std_expr.h
java_bytecode_convert_methodt::variablet
Definition: java_bytecode_convert_method_class.h:122
java_bytecode_convert_methodt::bytecode_write_typet
bytecode_write_typet
Definition: java_bytecode_convert_method_class.h:320
java_bytecode_convert_methodt::convert_athrow
void convert_athrow(const source_locationt &location, const exprt::operandst &op, codet &c, exprt::operandst &results) const
Definition: java_bytecode_convert_method.cpp:2384
ieee_float_spect::single_precision
static ieee_float_spect single_precision()
Definition: ieee_float.h:74
exprt::source_location
const source_locationt & source_location() const
Definition: expr.h:252
java_method_typet
Definition: java_types.h:103
BC_goto
#define BC_goto
Definition: bytecode_info.h:232
lambda_synthetic_class_name
irep_idt lambda_synthetic_class_name(const irep_idt &method_identifier, std::size_t instruction_address)
Definition: lambda_synthesis.cpp:37
java_bytecode_convert_methodt::replace_call_to_cprover_assume
codet & replace_call_to_cprover_assume(source_locationt location, codet &c)
Definition: java_bytecode_convert_method.cpp:2353
java_bytecode_convert_methodt::is_method_inherited
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 (and not an ...
Definition: java_bytecode_convert_method.cpp:3259
c_types.h
java_bytecode_convert_methodt::replace_goto_target
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'.
Definition: java_bytecode_convert_method.cpp:708
code_typet::set_access
void set_access(const irep_idt &access)
Definition: std_types.h:882
java_bytecode_convert_methodt::stack
stackt stack
Definition: java_bytecode_convert_method_class.h:204
symbolt::name
irep_idt name
The unique identifier.
Definition: symbol.h:40
java_bytecode_convert_methodt::bytecode_write_typet::FIELD
@ FIELD
gather_symbol_live_ranges
static void gather_symbol_live_ranges(java_bytecode_convert_methodt::method_offsett pc, const exprt &e, std::map< irep_idt, java_bytecode_convert_methodt::variablet > &result)
Definition: java_bytecode_convert_method.cpp:958
java_bytecode_initialize_parameter_names
void java_bytecode_initialize_parameter_names(symbolt &method_symbol, const java_bytecode_parse_treet::methodt::local_variable_tablet &local_variable_table, symbol_table_baset &symbol_table)
This uses a cut-down version of the logic in java_bytecode_convert_methodt::convert to initialize sym...
Definition: java_bytecode_convert_method.cpp:3147
java_bytecode_convert_methodt::symbol_table
symbol_table_baset & symbol_table
Definition: java_bytecode_convert_method_class.h:77
code_function_callt::function
exprt & function()
Definition: std_code.h:1221
side_effect_exprt
An expression containing a side effect.
Definition: std_code.h:1876
can_cast_type< bitvector_typet >
bool can_cast_type< bitvector_typet >(const typet &type)
Check whether a reference to a typet is a bitvector_typet.
Definition: std_types.h:1064
java_instanceof_exprt
Definition: java_expr.h:18
BC_invokeinterface
#define BC_invokeinterface
Definition: bytecode_info.h:250
validation_modet::INVARIANT
@ INVARIANT
mod_exprt
Modulo.
Definition: std_expr.h:1106
to_bitvector_type
const bitvector_typet & to_bitvector_type(const typet &type)
Cast a typet to a bitvector_typet.
Definition: std_types.h:1089
java_bytecode_convert_methodt::convert_aload
static exprt convert_aload(const irep_idt &statement, const exprt::operandst &op)
Definition: java_bytecode_convert_method.cpp:2937
tvt::is_true
bool is_true() const
Definition: threeval.h:25
BC_ldc2_w
#define BC_ldc2_w
Definition: bytecode_info.h:85
code_expressiont
codet representation of an expression statement.
Definition: std_code.h:1820
shl_exprt
Left shift.
Definition: std_expr.h:2590
isnan_exprt
Evaluates to true if the operand is NaN.
Definition: std_expr.h:3538
source_locationt::set_property_class
void set_property_class(const irep_idt &property_class)
Definition: source_location.h:136
ieee_floatt::from_expr
void from_expr(const constant_exprt &expr)
Definition: ieee_float.cpp:1063
codet
Data structure for representing an arbitrary statement in a program.
Definition: std_code.h:35
BC_getfield
#define BC_getfield
Definition: bytecode_info.h:245
java_bytecode_convert_methodt::convert_store
code_blockt convert_store(const irep_idt &statement, const exprt &arg0, const exprt::operandst &op, const method_offsett address, const source_locationt &location)
Definition: java_bytecode_convert_method.cpp:2978
java_bytecode_convert_methodt::save_stack_entries
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.
Definition: java_bytecode_convert_method.cpp:3292
BC_dup2_x2
#define BC_dup2_x2
Definition: bytecode_info.h:159
to_constant_expr
const constant_exprt & to_constant_expr(const exprt &expr)
Cast an exprt to a constant_exprt.
Definition: std_expr.h:3968
integer2string
const std::string integer2string(const mp_integer &n, unsigned base)
Definition: mp_arith.cpp:106
BC_ifnull
#define BC_ifnull
Definition: bytecode_info.h:263