CBMC
ci_lazy_methods.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Java Bytecode
4 
5 Author: Diffblue Ltd.
6 
7 \*******************************************************************/
8 
11 
12 #ifndef CPROVER_JAVA_BYTECODE_GATHER_METHODS_LAZILY_H
13 #define CPROVER_JAVA_BYTECODE_GATHER_METHODS_LAZILY_H
14 
16 #include "synthetic_methods_map.h"
17 
18 #include <functional>
19 #include <map>
20 #include <unordered_set>
21 
22 #include <util/irep.h>
23 
25 
27 class java_class_loadert;
28 class message_handlert;
30 class symbol_table_baset;
31 
32 // Map from method id to class_method_and_bytecodet
34 {
35 public:
38  {
42  };
43 
44  typedef std::optional<
45  std::reference_wrapper<const class_method_and_bytecodet>>
47 
48 private:
49  typedef std::map<irep_idt, class_method_and_bytecodet> mapt;
51 
52 public:
53  bool contains_method(const irep_idt &method_id) const
54  {
55  return map.count(method_id) != 0;
56  }
57 
58  void add(const class_method_and_bytecodet &method_class_and_bytecode)
59  {
60  map.emplace(
61  std::make_pair(
62  method_class_and_bytecode.method_id, method_class_and_bytecode));
63  }
64 
65  void add(
66  const irep_idt &class_id,
67  const irep_idt &method_id,
69  {
70  add(class_method_and_bytecodet{class_id, method_id, method});
71  }
72 
73  mapt::const_iterator begin() const
74  {
75  return map.begin();
76  }
77  mapt::const_iterator end() const
78  {
79  return map.end();
80  }
81 
82  opt_reft get(const irep_idt &method_id)
83  {
84  const auto it = map.find(method_id);
85  if(it == map.end())
86  return std::nullopt;
87  return std::cref(it->second);
88  }
89 };
90 
91 typedef std::function<
92  bool(const irep_idt &function_id, ci_lazy_methods_neededt)>
94 
95 typedef std::function<std::vector<irep_idt>(const symbol_table_baset &)>
97 
99 {
100 public:
102  const symbol_table_baset &symbol_table,
103  const irep_idt &main_class,
104  const std::vector<irep_idt> &main_jar_classes,
105  const std::vector<load_extra_methodst> &lazy_methods_extra_entry_points,
107  const std::vector<irep_idt> &extra_instantiated_classes,
110 
111  // not const since messaget
112  bool operator()(
113  symbol_table_baset &symbol_table,
114  method_bytecodet &method_bytecode,
115  const method_convertert &method_converter,
116  message_handlert &message_handler);
117 
118 private:
120  const std::unordered_set<irep_idt> &entry_points,
121  const namespacet &ns,
122  ci_lazy_methods_neededt &needed_lazy_methods);
123 
125  const exprt &e,
126  std::unordered_set<class_method_descriptor_exprt, irep_hash> &result);
127 
129  const class_method_descriptor_exprt &called_function,
130  const std::unordered_set<irep_idt> &instantiated_classes,
131  std::unordered_set<irep_idt> &callable_methods,
132  symbol_table_baset &symbol_table);
133 
135  const exprt &e,
136  const symbol_table_baset &symbol_table,
137  symbol_table_baset &needed);
138 
140  const std::unordered_set<irep_idt> &instantiated_classes,
141  const irep_idt &call_basename,
142  const irep_idt &classname,
143  const symbol_table_baset &symbol_table);
144 
146  const irep_idt &class_name,
147  const irep_idt &component_method_name);
148 
151  std::vector<irep_idt> main_jar_classes;
152  const std::vector<load_extra_methodst> &lazy_methods_extra_entry_points;
154  const std::vector<irep_idt> &extra_instantiated_classes;
157 
158  std::unordered_set<irep_idt> entry_point_methods(
159  const symbol_table_baset &symbol_table,
160  message_handlert &message_handler);
161 
163  {
165  bool new_method_seen = false;
166  };
167 
169  const method_convertert &method_converter,
170  std::unordered_set<irep_idt> &methods_already_populated,
171  const bool class_initializer_already_seen,
172  const irep_idt &method_name,
173  symbol_table_baset &symbol_table,
174  std::unordered_set<irep_idt> &methods_to_convert_later,
175  std::unordered_set<irep_idt> &instantiated_classes,
176  std::unordered_set<class_method_descriptor_exprt, irep_hash>
177  &called_virtual_functions,
178  message_handlert &message_handler);
179 
181  std::unordered_set<irep_idt> &methods_to_convert_later,
182  std::unordered_set<irep_idt> &instantiated_classes,
183  const std::unordered_set<class_method_descriptor_exprt, irep_hash>
184  &virtual_functions,
185  symbol_table_baset &symbol_table);
186 };
187 
188 #endif // CPROVER_JAVA_BYTECODE_GATHER_METHODS_LAZILY_H
std::function< bool(const irep_idt &function_id, ci_lazy_methods_neededt)> method_convertert
std::function< std::vector< irep_idt >const symbol_table_baset &)> load_extra_methodst
Class Hierarchy.
convert_method_resultt convert_and_analyze_method(const method_convertert &method_converter, std::unordered_set< irep_idt > &methods_already_populated, const bool class_initializer_already_seen, const irep_idt &method_name, symbol_table_baset &symbol_table, std::unordered_set< irep_idt > &methods_to_convert_later, std::unordered_set< irep_idt > &instantiated_classes, std::unordered_set< class_method_descriptor_exprt, irep_hash > &called_virtual_functions, message_handlert &message_handler)
Convert a method, add it to the populated set, add needed methods to methods_to_convert_later and add...
const std::vector< irep_idt > & extra_instantiated_classes
static irep_idt build_virtual_method_name(const irep_idt &class_name, const irep_idt &component_method_name)
irep_idt get_virtual_method_target(const std::unordered_set< irep_idt > &instantiated_classes, const irep_idt &call_basename, const irep_idt &classname, const symbol_table_baset &symbol_table)
Find a virtual callee, if one is defined and the callee type is known to exist.
void get_virtual_method_targets(const class_method_descriptor_exprt &called_function, const std::unordered_set< irep_idt > &instantiated_classes, std::unordered_set< irep_idt > &callable_methods, symbol_table_baset &symbol_table)
Find possible callees, excluding types that are not known to be instantiated.
std::vector< irep_idt > main_jar_classes
const select_pointer_typet & pointer_type_selector
const std::vector< load_extra_methodst > & lazy_methods_extra_entry_points
class_hierarchyt class_hierarchy
void gather_virtual_callsites(const exprt &e, std::unordered_set< class_method_descriptor_exprt, irep_hash > &result)
Get places where virtual functions are called.
java_class_loadert & java_class_loader
bool operator()(symbol_table_baset &symbol_table, method_bytecodet &method_bytecode, const method_convertert &method_converter, message_handlert &message_handler)
Uses a simple context-insensitive ('ci') analysis to determine which methods may be reachable from th...
const synthetic_methods_mapt & synthetic_methods
ci_lazy_methodst(const symbol_table_baset &symbol_table, const irep_idt &main_class, const std::vector< irep_idt > &main_jar_classes, const std::vector< load_extra_methodst > &lazy_methods_extra_entry_points, java_class_loadert &java_class_loader, const std::vector< irep_idt > &extra_instantiated_classes, const select_pointer_typet &pointer_type_selector, const synthetic_methods_mapt &synthetic_methods)
Constructor for lazy-method loading.
void gather_needed_globals(const exprt &e, const symbol_table_baset &symbol_table, symbol_table_baset &needed)
See output.
void initialize_instantiated_classes(const std::unordered_set< irep_idt > &entry_points, const namespacet &ns, ci_lazy_methods_neededt &needed_lazy_methods)
Build up a list of methods whose type may be passed around reachable from the entry point.
bool handle_virtual_methods_with_no_callees(std::unordered_set< irep_idt > &methods_to_convert_later, std::unordered_set< irep_idt > &instantiated_classes, const std::unordered_set< class_method_descriptor_exprt, irep_hash > &virtual_functions, symbol_table_baset &symbol_table)
Look for virtual callsites with no candidate targets.
std::unordered_set< irep_idt > entry_point_methods(const symbol_table_baset &symbol_table, message_handlert &message_handler)
Entry point methods are either:
Non-graph-based representation of the class hierarchy.
An expression describing a method on a class.
Definition: std_expr.h:3495
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:38
Base class for all expressions.
Definition: expr.h:56
Class responsible to load .class files.
mapt::const_iterator begin() const
opt_reft get(const irep_idt &method_id)
std::optional< std::reference_wrapper< const class_method_and_bytecodet > > opt_reft
std::map< irep_idt, class_method_and_bytecodet > mapt
void add(const class_method_and_bytecodet &method_class_and_bytecode)
bool contains_method(const irep_idt &method_id) const
void add(const irep_idt &class_id, const irep_idt &method_id, const java_bytecode_parse_treet::methodt &method)
mapt::const_iterator end() const
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:94
The symbol table base class interface.
const java_bytecode_parse_treet::methodt & method
Synthetic methods are particular methods internally generated by the Java frontend,...
std::unordered_map< irep_idt, synthetic_method_typet > synthetic_methods_mapt
Maps method names on to a synthetic method kind.