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 
31 // Map from method id to class_method_and_bytecodet
33 {
34 public:
37  {
41  };
42 
45 
46 private:
47  typedef std::map<irep_idt, class_method_and_bytecodet> mapt;
49 
50 public:
51  bool contains_method(const irep_idt &method_id) const
52  {
53  return map.count(method_id) != 0;
54  }
55 
56  void add(const class_method_and_bytecodet &method_class_and_bytecode)
57  {
58  map.emplace(
59  std::make_pair(
60  method_class_and_bytecode.method_id, method_class_and_bytecode));
61  }
62 
63  void add(
64  const irep_idt &class_id,
65  const irep_idt &method_id,
67  {
68  add(class_method_and_bytecodet{class_id, method_id, method});
69  }
70 
71  mapt::const_iterator begin() const
72  {
73  return map.begin();
74  }
75  mapt::const_iterator end() const
76  {
77  return map.end();
78  }
79 
80  opt_reft get(const irep_idt &method_id)
81  {
82  const auto it = map.find(method_id);
83  if(it == map.end())
84  return opt_reft();
85  return std::cref(it->second);
86  }
87 };
88 
89 typedef std::function<
90  bool(const irep_idt &function_id, ci_lazy_methods_neededt)>
92 
93 typedef std::function<std::vector<irep_idt>(const symbol_tablet &)>
95 
97 {
98 public:
100  const symbol_tablet &symbol_table,
101  const irep_idt &main_class,
102  const std::vector<irep_idt> &main_jar_classes,
103  const std::vector<load_extra_methodst> &lazy_methods_extra_entry_points,
105  const std::vector<irep_idt> &extra_instantiated_classes,
108 
109  // not const since messaget
110  bool operator()(
111  symbol_tablet &symbol_table,
112  method_bytecodet &method_bytecode,
113  const method_convertert &method_converter,
114  message_handlert &message_handler);
115 
116 private:
118  const std::unordered_set<irep_idt> &entry_points,
119  const namespacet &ns,
120  ci_lazy_methods_neededt &needed_lazy_methods);
121 
123  const exprt &e,
124  std::unordered_set<class_method_descriptor_exprt, irep_hash> &result);
125 
127  const class_method_descriptor_exprt &called_function,
128  const std::unordered_set<irep_idt> &instantiated_classes,
129  std::unordered_set<irep_idt> &callable_methods,
130  symbol_tablet &symbol_table);
131 
133  const exprt &e,
134  const symbol_tablet &symbol_table,
135  symbol_tablet &needed);
136 
138  const std::unordered_set<irep_idt> &instantiated_classes,
139  const irep_idt &call_basename,
140  const irep_idt &classname,
141  const symbol_tablet &symbol_table);
142 
144  const irep_idt &class_name,
145  const irep_idt &component_method_name);
146 
149  std::vector<irep_idt> main_jar_classes;
150  const std::vector<load_extra_methodst> &lazy_methods_extra_entry_points;
152  const std::vector<irep_idt> &extra_instantiated_classes;
155 
156  std::unordered_set<irep_idt> entry_point_methods(
157  const symbol_tablet &symbol_table,
158  message_handlert &message_handler);
159 
161  {
163  bool new_method_seen = false;
164  };
165 
167  const method_convertert &method_converter,
168  std::unordered_set<irep_idt> &methods_already_populated,
169  const bool class_initializer_already_seen,
170  const irep_idt &method_name,
171  symbol_tablet &symbol_table,
172  std::unordered_set<irep_idt> &methods_to_convert_later,
173  std::unordered_set<irep_idt> &instantiated_classes,
174  std::unordered_set<class_method_descriptor_exprt, irep_hash>
175  &called_virtual_functions,
176  message_handlert &message_handler);
177 
179  std::unordered_set<irep_idt> &methods_to_convert_later,
180  std::unordered_set<irep_idt> &instantiated_classes,
181  const std::unordered_set<class_method_descriptor_exprt, irep_hash>
182  &virtual_functions,
183  symbol_tablet &symbol_table);
184 };
185 
186 #endif // CPROVER_JAVA_BYTECODE_GATHER_METHODS_LAZILY_H
method_bytecodet
Definition: ci_lazy_methods.h:32
method_bytecodet::class_method_and_bytecodet::method_id
irep_idt method_id
Definition: ci_lazy_methods.h:39
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:36
method_bytecodet::class_method_and_bytecodet
Pair of class id and methodt.
Definition: ci_lazy_methods.h:36
method_bytecodet::class_method_and_bytecodet::method
const java_bytecode_parse_treet::methodt & method
Definition: ci_lazy_methods.h:40
symbol_tablet
The symbol table.
Definition: symbol_table.h:13
ci_lazy_methodst::convert_method_resultt
Definition: ci_lazy_methods.h:160
class_hierarchyt
Non-graph-based representation of the class hierarchy.
Definition: class_hierarchy.h:42
method_bytecodet::class_method_and_bytecodet::class_id
irep_idt class_id
Definition: ci_lazy_methods.h:38
method_bytecodet::add
void add(const irep_idt &class_id, const irep_idt &method_id, const java_bytecode_parse_treet::methodt &method)
Definition: ci_lazy_methods.h:63
java_bytecode_parse_treet::methodt
Definition: java_bytecode_parse_tree.h:84
ci_lazy_methodst::lazy_methods_extra_entry_points
const std::vector< load_extra_methodst > & lazy_methods_extra_entry_points
Definition: ci_lazy_methods.h:150
method_bytecodet::map
mapt map
Definition: ci_lazy_methods.h:48
method_bytecodet::opt_reft
optionalt< std::reference_wrapper< const class_method_and_bytecodet > > opt_reft
Definition: ci_lazy_methods.h:44
method_bytecodet::get
opt_reft get(const irep_idt &method_id)
Definition: ci_lazy_methods.h:80
exprt
Base class for all expressions.
Definition: expr.h:55
ci_lazy_methodst::build_virtual_method_name
static irep_idt build_virtual_method_name(const irep_idt &class_name, const irep_idt &component_method_name)
ci_lazy_methodst::main_jar_classes
std::vector< irep_idt > main_jar_classes
Definition: ci_lazy_methods.h:149
synthetic_methods_mapt
std::unordered_map< irep_idt, synthetic_method_typet > synthetic_methods_mapt
Maps method names on to a synthetic method kind.
Definition: synthetic_methods_map.h:57
method_convertert
std::function< bool(const irep_idt &function_id, ci_lazy_methods_neededt)> method_convertert
Definition: ci_lazy_methods.h:91
method_bytecodet::add
void add(const class_method_and_bytecodet &method_class_and_bytecode)
Definition: ci_lazy_methods.h:56
class_method_descriptor_exprt
An expression describing a method on a class.
Definition: std_expr.h:3440
namespacet
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:90
ci_lazy_methodst::class_hierarchy
class_hierarchyt class_hierarchy
Definition: ci_lazy_methods.h:147
ci_lazy_methodst::convert_method_resultt::class_initializer_seen
bool class_initializer_seen
Definition: ci_lazy_methods.h:162
method_bytecodet::end
mapt::const_iterator end() const
Definition: ci_lazy_methods.h:75
ci_lazy_methodst::pointer_type_selector
const select_pointer_typet & pointer_type_selector
Definition: ci_lazy_methods.h:153
java_bytecode_parse_tree.h
load_extra_methodst
std::function< std::vector< irep_idt >const symbol_tablet &)> load_extra_methodst
Definition: ci_lazy_methods.h:94
select_pointer_typet
Definition: select_pointer_type.h:22
ci_lazy_methodst::handle_virtual_methods_with_no_callees
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_tablet &symbol_table)
Look for virtual callsites with no candidate targets.
Definition: ci_lazy_methods.cpp:236
message_handlert
Definition: message.h:27
ci_lazy_methodst::get_virtual_method_targets
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_tablet &symbol_table)
Find possible callees, excluding types that are not known to be instantiated.
Definition: ci_lazy_methods.cpp:480
ci_lazy_methodst::convert_and_analyze_method
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_tablet &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...
Definition: ci_lazy_methods.cpp:319
method_bytecodet::mapt
std::map< irep_idt, class_method_and_bytecodet > mapt
Definition: ci_lazy_methods.h:47
ci_lazy_methodst::java_class_loader
java_class_loadert & java_class_loader
Definition: ci_lazy_methods.h:151
optionalt
nonstd::optional< T > optionalt
Definition: optional.h:35
ci_lazy_methodst::gather_needed_globals
void gather_needed_globals(const exprt &e, const symbol_tablet &symbol_table, symbol_tablet &needed)
See output.
Definition: ci_lazy_methods.cpp:507
java_class_loadert
Class responsible to load .class files.
Definition: java_class_loader.h:25
class_hierarchy.h
method_bytecodet::contains_method
bool contains_method(const irep_idt &method_id) const
Definition: ci_lazy_methods.h:51
ci_lazy_methodst
Definition: ci_lazy_methods.h:96
ci_lazy_methodst::synthetic_methods
const synthetic_methods_mapt & synthetic_methods
Definition: ci_lazy_methods.h:154
ci_lazy_methodst::main_class
irep_idt main_class
Definition: ci_lazy_methods.h:148
ci_lazy_methodst::gather_virtual_callsites
void gather_virtual_callsites(const exprt &e, std::unordered_set< class_method_descriptor_exprt, irep_hash > &result)
Get places where virtual functions are called.
Definition: ci_lazy_methods.cpp:448
ci_lazy_methodst::operator()
bool operator()(symbol_tablet &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...
Definition: ci_lazy_methods.cpp:98
ci_lazy_methodst::ci_lazy_methodst
ci_lazy_methodst(const symbol_tablet &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.
Definition: ci_lazy_methods.cpp:36
ci_lazy_methodst::entry_point_methods
std::unordered_set< irep_idt > entry_point_methods(const symbol_tablet &symbol_table, message_handlert &message_handler)
Entry point methods are either:
Definition: ci_lazy_methods.cpp:369
ci_lazy_methods_neededt
Definition: ci_lazy_methods_needed.h:25
method_bytecodet::begin
mapt::const_iterator begin() const
Definition: ci_lazy_methods.h:71
ci_lazy_methodst::extra_instantiated_classes
const std::vector< irep_idt > & extra_instantiated_classes
Definition: ci_lazy_methods.h:152
ci_lazy_methodst::initialize_instantiated_classes
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.
Definition: ci_lazy_methods.cpp:413
ci_lazy_methodst::convert_method_resultt::new_method_seen
bool new_method_seen
Definition: ci_lazy_methods.h:163
synthetic_methods_map.h
irep.h
ci_lazy_methodst::get_virtual_method_target
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_tablet &symbol_table)
Find a virtual callee, if one is defined and the callee type is known to exist.
Definition: ci_lazy_methods.cpp:545