cprover
java_bytecode_language.h
Go to the documentation of this file.
1/*******************************************************************\
2
3Module:
4
5Author: Daniel Kroening, kroening@kroening.com
6
7\*******************************************************************/
8
9
10#ifndef CPROVER_JAVA_BYTECODE_JAVA_BYTECODE_LANGUAGE_H
11#define CPROVER_JAVA_BYTECODE_JAVA_BYTECODE_LANGUAGE_H
12
13#include "ci_lazy_methods.h"
15#include "java_class_loader.h"
19#include "select_pointer_type.h"
21
22#include <memory>
23
24#include <util/json.h>
25#include <util/make_unique.h>
26#include <util/prefix_filter.h>
27
28#include <langapi/language.h>
29
30// clang-format off
31#define JAVA_BYTECODE_LANGUAGE_OPTIONS /*NOLINT*/ \
32 "(disable-uncaught-exception-check)" \
33 "(throw-assertion-error)" \
34 "(assert-no-exceptions-thrown)" \
35 "(java-assume-inputs-non-null)" \
36 "(java-assume-inputs-interval):" \
37 "(java-assume-inputs-integral)" \
38 "(throw-runtime-exceptions)" \
39 "(max-nondet-array-length):" \
40 "(max-nondet-tree-depth):" \
41 "(java-max-vla-length):" \
42 "(java-cp-include-files):" \
43 "(ignore-manifest-main-class)" \
44 "(context-include):" \
45 "(context-exclude):" \
46 "(no-lazy-methods)" \
47 "(lazy-methods-extra-entry-point):" \
48 "(java-load-class):" \
49 "(java-no-load-class):" \
50 "(static-values):" \
51 "(java-lift-clinit-calls)"
52
53#define JAVA_BYTECODE_LANGUAGE_OPTIONS_HELP /*NOLINT*/ \
54 " --disable-uncaught-exception-check\n" \
55 " ignore uncaught exceptions and errors\n" \
56 " --throw-assertion-error throw java.lang.AssertionError on violated\n" \
57 " assert statements instead of failing\n" \
58 " at the location of the assert statement\n" \
59 " --throw-runtime-exceptions make implicit runtime exceptions explicit\n" \
60 " --assert-no-exceptions-thrown\n"\
61 " transform `throw` instructions into `assert FALSE`\n"/* NOLINT(*) */ \
62 " followed by `assume FALSE`.\n" \
63 " --max-nondet-array-length N limit nondet (e.g. input) array size to <= N\n" /* NOLINT(*) */ \
64 " --max-nondet-tree-depth N limit size of nondet (e.g. input) object tree;\n" /* NOLINT(*) */ \
65 " at level N references are set to null\n" /* NOLINT(*) */ \
66 " --java-assume-inputs-non-null\n" \
67 " never initialize reference-typed parameter to the\n" /* NOLINT(*) */ \
68 " entry point with null\n" /* NOLINT(*) */ \
69 " --java-assume-inputs-interval [L:U] or [L:] or [:U]\n" \
70 " force numerical primitive-typed inputs\n" /* NOLINT(*) */ \
71 " (byte, short, int, long, float, double) to be\n" /* NOLINT(*) */ \
72 " initialized within the given range; lower bound\n" /* NOLINT(*) */ \
73 " L and upper bound U must be integers;\n" /* NOLINT(*) */ \
74 " does not work for arrays;\n" /* NOLINT(*) */ \
75 " --java-assume-inputs-integral\n" \
76 " force float and double inputs to have integer values;\n" /* NOLINT(*) */ \
77 " does not work for arrays;\n" /* NOLINT(*) */ \
78 " --java-max-vla-length N limit the length of user-code-created arrays\n" /* NOLINT(*) */ \
79 " --java-cp-include-files r regexp or JSON list of files to load\n" \
80 " (with '@' prefix)\n" \
81 " --ignore-manifest-main-class ignore Main-Class entries in JAR manifest files.\n" /* NOLINT(*) */ \
82 " If this option is specified and the options\n" /* NOLINT(*) */ \
83 " --function and --main-class are not, we can be\n" /* NOLINT(*) */ \
84 " certain that all classes in the JAR file are\n" /* NOLINT(*) */ \
85 " loaded.\n" \
86 " --context-include i only analyze code matching specification i that\n" /* NOLINT(*) */ \
87 " --context-exclude e does not match specification e.\n" \
88 " All other methods are excluded, i.e. we load their\n" /* NOLINT(*) */ \
89 " signatures and meta-information, but not their\n" /* NOLINT(*) */ \
90 " bodies.\n" \
91 " A specification is any prefix of a package, class\n" /* NOLINT(*) */ \
92 " or method name, e.g. \"org.cprover.\" or\n" /* NOLINT(*) */ \
93 " \"org.cprover.MyClass.\" or\n" \
94 " \"org.cprover.MyClass.methodToStub:(I)Z\".\n" \
95 " These options can be given multiple times.\n" \
96 " The default for context-include is 'all\n" \
97 " included'; default for context-exclude is\n" \
98 " 'nothing excluded'.\n" \
99 " --no-lazy-methods load and translate all methods given on\n" \
100 " the command line and in --classpath\n" \
101 " Default is to load methods that appear to be\n" /* NOLINT(*) */ \
102 " reachable from the --function entry point\n" \
103 " or main class\n" \
104 " Note that --show-symbol-table, --show-goto-functions\n" /* NOLINT(*) */ \
105 " and --show-properties output are restricted to\n" /* NOLINT(*) */ \
106 " loaded methods by default.\n" \
107 " --lazy-methods-extra-entry-point METHODNAME\n" \
108 " treat METHODNAME as a possible program entry\n" /* NOLINT(*) */ \
109 " point for the purpose of lazy method loading\n" /* NOLINT(*) */ \
110 " METHODNAME can be a regex that will be matched\n" /* NOLINT(*) */ \
111 " against all symbols. If missing a java:: prefix\n" /* NOLINT(*) */ \
112 " will be added. If no descriptor is found, all\n"/* NOLINT(*) */ \
113 " overloads of a method will also be added.\n" \
114 " --static-values f Load initial values of static fields from the given\n"/* NOLINT(*) */ \
115 " JSON file. We assign static fields to these values\n"/* NOLINT(*) */ \
116 " instead of calling the normal static initializer\n"/* NOLINT(*) */ \
117 " (clinit) method.\n" \
118 " The argument can be a relative or absolute path to\n"/* NOLINT(*) */ \
119 " the file.\n" \
120 " --java-lift-clinit-calls Lifts clinit calls in function bodies to the top of the\n" /* NOLINT(*) */ \
121 " function. This may reduce the overall cost of static\n" /* NOLINT(*) */ \
122 " initialisation, but may be unsound if there are\n" /* NOLINT(*) */ \
123 " cyclic dependencies between static initializers due\n" /* NOLINT(*) */ \
124 " to potentially changing their order of execution,\n" /* NOLINT(*) */ \
125 " or if static initializers have side-effects such as\n" /* NOLINT(*) */ \
126 " updating another class' static field.\n" /* NOLINT(*) */
127
128#ifdef _WIN32
129 #define JAVA_CLASSPATH_SEPARATOR ";"
130#else
131 #define JAVA_CLASSPATH_SEPARATOR ":"
132#endif
133
134#define HELP_JAVA_CLASSPATH /* NOLINT(*) */ \
135 " -classpath dirs/jars\n" \
136 " -cp dirs/jars\n" \
137 " --classpath dirs/jars set class search path of directories and\n" \
138 " jar files\n" \
139 " A " JAVA_CLASSPATH_SEPARATOR \
140 " separated list of directories and JAR\n" \
141 " archives to search for class files.\n" \
142 " --main-class class-name set the name of the main class\n"
143
144#define HELP_JAVA_METHOD_NAME /* NOLINT(*) */ \
145 " method-name fully qualified name of method\n" \
146 " used as entry point, e.g.\n" \
147 " mypackage.Myclass.foo:(I)Z\n"
148
149#define HELP_JAVA_CLASS_NAME /* NOLINT(*) */ \
150 " class-name name of class\n" \
151 " The entry point is the method specified by\n" \
152 " --function, or otherwise, the\n" \
153 " public static void main(String[])\n" \
154 " method of the given class.\n"
155
156#define OPT_JAVA_JAR /* NOLINT(*) */ \
157 "(jar):"
158
159#define HELP_JAVA_JAR /* NOLINT(*) */ \
160 " -jar jarfile JAR file to be checked\n" \
161 " The entry point is the method specified by\n" \
162 " --function or otherwise, the\n" \
163 " public static void main(String[]) method\n" \
164 " of the class specified by --main-class or the main\n" /* NOLINT(*) */ \
165 " class specified in the JAR manifest\n" \
166 " (checked in this order).\n"
167
168#define OPT_JAVA_GOTO_BINARY /* NOLINT(*) */ \
169 "(gb):"
170
171#define HELP_JAVA_GOTO_BINARY /* NOLINT(*) */ \
172 " --gb goto-binary goto-binary file to be checked\n" \
173 " The entry point is the method specified by\n" \
174 " --function, or otherwise, the\n" \
175 " public static void main(String[])\n" \
176 " of the class specified by --main-class\n" \
177 " (checked in this order).\n"
178// clang-format on
179
180class symbolt;
181
183{
188
200{
201public:
203
204 std::unordered_multimap<irep_idt, symbolt> &
205 get(const symbol_tablet &symbol_table);
206
207 void reinitialize();
208
209private:
210 bool initialized = false;
211 std::unordered_multimap<irep_idt, symbolt> map;
212};
213
215{
217
219
226 bool threading_support = false;
227 bool nondet_static = false;
229
233
238
240 std::vector<irep_idt> java_load_classes;
246
248 std::unordered_set<std::string> no_load_classes;
249
250 std::vector<load_extra_methodst> extra_methods;
251
260
265
268};
269
270#define JAVA_CLASS_MODEL_SUFFIX "@class_model"
271
273{
274public:
275 void set_language_options(const optionst &) override;
276
278
279 virtual bool preprocess(
280 std::istream &instream,
281 const std::string &path,
282 std::ostream &outstream) override;
283
284 // This is an extension to languaget
285 // required because parsing of Java programs can be initiated without
286 // opening a file first or providing a path to a file
287 // as dictated by \ref languaget.
288 virtual bool parse();
289
290 bool parse(
291 std::istream &instream,
292 const std::string &path) override;
293
295 symbol_tablet &symbol_table) override;
296
297 bool typecheck(
298 symbol_tablet &context,
299 const std::string &module) override;
300
301 virtual bool final(symbol_table_baset &context) override;
302
303 void show_parse(std::ostream &out) override;
304
305 virtual ~java_bytecode_languaget();
307 std::unique_ptr<select_pointer_typet> pointer_type_selector)
310 {
311 }
312
316 {
317 }
318
319 bool from_expr(
320 const exprt &expr,
321 std::string &code,
322 const namespacet &ns) override;
323
324 bool from_type(
325 const typet &type,
326 std::string &code,
327 const namespacet &ns) override;
328
329 bool to_expr(
330 const std::string &code,
331 const std::string &module,
332 exprt &expr,
333 const namespacet &ns) override;
334
335 std::unique_ptr<languaget> new_language() override
336 { return util_make_unique<java_bytecode_languaget>(); }
337
338 std::string id() const override { return "java"; }
339 std::string description() const override { return "Java Bytecode"; }
340 std::set<std::string> extensions() const override;
341
342 void modules_provided(std::set<std::string> &modules) override;
343 virtual void
344 methods_provided(std::unordered_set<irep_idt> &methods) const override;
345 virtual void convert_lazy_method(
346 const irep_idt &function_id,
347 symbol_table_baset &symbol_table) override;
348
349protected:
351 const irep_idt &function_id,
352 symbol_table_baset &symbol_table,
354 {
356 function_id,
357 symbol_table,
360 }
362 const irep_idt &function_id,
363 symbol_table_baset &symbol_table,
364 optionalt<ci_lazy_methods_neededt> needed_lazy_methods,
367 const irep_idt &function_id,
368 symbol_table_baset &symbol_table,
369 optionalt<ci_lazy_methods_neededt> needed_lazy_methods,
371
374
377 std::vector<irep_idt> main_jar_classes;
382
383private:
384 virtual std::vector<load_extra_methodst>
385 build_extra_entry_points(const optionst &) const;
386 const std::unique_ptr<const select_pointer_typet> pointer_type_selector;
387
394
399 std::unordered_map<std::string, object_creation_referencet> references;
400
403};
404
405std::unique_ptr<languaget> new_java_bytecode_language();
406
407void parse_java_language_options(const cmdlinet &cmd, optionst &options);
408
409prefix_filtert get_context(const optionst &options);
410
411#endif // CPROVER_JAVA_BYTECODE_JAVA_BYTECODE_LANGUAGE_H
Collect methods needed to be loaded using the lazy method.
Context-insensitive lazy methods container.
Non-graph-based representation of the class hierarchy.
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:37
Base class for all expressions.
Definition: expr.h:54
virtual void methods_provided(std::unordered_set< irep_idt > &methods) const override
Provide feedback to language_filest so that when asked for a lazy method, it can delegate to this ins...
void show_parse(std::ostream &out) override
synthetic_methods_mapt synthetic_methods
Maps synthetic method names on to the particular type of synthetic method (static initializer,...
std::vector< irep_idt > main_jar_classes
virtual bool preprocess(std::istream &instream, const std::string &path, std::ostream &outstream) override
ANSI-C preprocessing.
std::string id() const override
stub_global_initializer_factoryt stub_global_initializer_factory
std::set< std::string > extensions() const override
void set_message_handler(message_handlert &message_handler) override
void set_language_options(const optionst &) override
Consume options that are java bytecode specific.
bool from_type(const typet &type, std::string &code, const namespacet &ns) override
Formats the given type in a language-specific way.
std::unique_ptr< languaget > new_language() override
bool to_expr(const std::string &code, const std::string &module, exprt &expr, const namespacet &ns) override
Parses the given string into an expression.
java_bytecode_languaget(std::unique_ptr< select_pointer_typet > pointer_type_selector)
std::string description() const override
const std::unique_ptr< const select_pointer_typet > pointer_type_selector
bool do_ci_lazy_method_conversion(symbol_tablet &)
Uses a simple context-insensitive ('ci') analysis to determine which methods may be reachable from th...
bool generate_support_functions(symbol_tablet &symbol_table) override
Create language-specific support functions, such as __CPROVER_start, __CPROVER_initialize and languag...
java_object_factory_parameterst object_factory_parameters
optionalt< java_bytecode_language_optionst > language_options
java_string_library_preprocesst string_preprocess
void modules_provided(std::set< std::string > &modules) override
java_class_loadert java_class_loader
bool convert_single_method_code(const irep_idt &function_id, symbol_table_baset &symbol_table, optionalt< ci_lazy_methods_neededt > needed_lazy_methods, lazy_class_to_declared_symbols_mapt &class_to_declared_symbols)
Convert a method (one whose type is known but whose body hasn't been converted) but don't run typeche...
virtual std::vector< load_extra_methodst > build_extra_entry_points(const optionst &) const
This method should be overloaded to provide alternative approaches for specifying extra entry points.
virtual bool parse()
We set the main class (i.e. class to start the class loading analysis, see java_class_loadert) when w...
const select_pointer_typet & get_pointer_type_selector() const
bool typecheck(symbol_tablet &context, const std::string &module) override
bool from_expr(const exprt &expr, std::string &code, const namespacet &ns) override
Formats the given expression in a language-specific way.
std::unordered_map< std::string, object_creation_referencet > references
Map used in all calls to functions that deterministically create objects (currently only assign_from_...
virtual void convert_lazy_method(const irep_idt &function_id, symbol_table_baset &symbol_table) override
Promote a lazy-converted method (one whose type is known but whose body hasn't been converted) into a...
void convert_single_method(const irep_idt &function_id, symbol_table_baset &symbol_table, lazy_class_to_declared_symbols_mapt &class_to_declared_symbols)
Class responsible to load .class files.
Map classes to the symbols they declare but is only computed once it is needed and the map is then ke...
std::unordered_multimap< irep_idt, symbolt > & get(const symbol_tablet &symbol_table)
std::unordered_multimap< irep_idt, symbolt > map
Class that provides messages with a built-in verbosity 'level'.
Definition: message.h:155
message_handlert * message_handler
Definition: message.h:439
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:91
Provides filtering of strings vai inclusion/exclusion lists of prefixes.
Definition: prefix_filter.h:20
The symbol table base class interface.
The symbol table.
Definition: symbol_table.h:14
Symbol table entry.
Definition: symbol.h:28
The type of an expression, extends irept.
Definition: type.h:29
void parse_java_language_options(const cmdlinet &cmd, optionst &options)
Parse options that are java bytecode specific.
std::unique_ptr< languaget > new_java_bytecode_language()
@ LAZY_METHODS_MODE_EAGER
@ LAZY_METHODS_MODE_EXTERNAL_DRIVER
@ LAZY_METHODS_MODE_CONTEXT_INSENSITIVE
prefix_filtert get_context(const optionst &options)
std::unordered_multimap< irep_idt, symbolt > class_to_declared_symbols(const symbol_tablet &symbol_table)
Produce code for simple implementation of String Java libraries.
Abstract interface to support a programming language.
STL namespace.
nonstd::optional< T > optionalt
Definition: optional.h:35
Prefix Filtering.
Handle selection of correct pointer type (for example changing abstract classes to concrete versions)...
bool assert_no_exceptions_thrown
Transform athrow bytecode instructions into assert FALSE followed by assume FALSE.
bool should_lift_clinit_calls
Should we lift clinit calls in function bodies to the top? For example, turning if(x) A....
optionalt< std::string > main_jar
If set then a JAR file has been given via the -jar option.
optionalt< prefix_filtert > method_context
If set, method bodies are only elaborated if they pass the filter.
optionalt< json_objectt > static_values_json
JSON which contains initial values of static fields (right after the static initializer of the class ...
bool assume_inputs_non_null
assume inputs variables to be non-null
size_t max_user_array_length
max size for user code created arrays
std::vector< irep_idt > java_load_classes
list of classes to force load even without reference from the entry point
std::unordered_set< std::string > no_load_classes
List of classes to never load.
std::vector< load_extra_methodst > extra_methods
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.