cprover
Loading...
Searching...
No Matches
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 <util/json.h>
14#include <util/make_unique.h>
15#include <util/prefix_filter.h>
16#include <util/symbol.h> // IWYU pragma: keep
17
18#include <langapi/language.h>
19
20#include "ci_lazy_methods.h"
22#include "code_with_references.h" // IWYU pragma: keep
23#include "java_class_loader.h"
27#include "select_pointer_type.h"
29
30#include <memory>
31
32// clang-format off
33#define JAVA_BYTECODE_LANGUAGE_OPTIONS /*NOLINT*/ \
34 "(disable-uncaught-exception-check)" \
35 "(throw-assertion-error)" \
36 "(assert-no-exceptions-thrown)" \
37 "(java-assume-inputs-non-null)" \
38 "(java-assume-inputs-interval):" \
39 "(java-assume-inputs-integral)" \
40 "(throw-runtime-exceptions)" \
41 "(max-nondet-array-length):" \
42 "(max-nondet-tree-depth):" \
43 "(java-max-vla-length):" \
44 "(java-cp-include-files):" \
45 "(ignore-manifest-main-class)" \
46 "(context-include):" \
47 "(context-exclude):" \
48 "(no-lazy-methods)" \
49 "(lazy-methods-extra-entry-point):" \
50 "(java-load-class):" \
51 "(java-no-load-class):" \
52 "(static-values):" \
53 "(java-lift-clinit-calls)"
54
55#define JAVA_BYTECODE_LANGUAGE_OPTIONS_HELP \
56 " {y--disable-uncaught-exception-check} \t " \
57 "ignore uncaught exceptions and errors\n" \
58 " {y--throw-assertion-error} \t " \
59 "throw java.lang.AssertionError on violated assert statements instead of " \
60 "failing at the location of the assert statement\n" \
61 " {y--throw-runtime-exceptions} \t " \
62 "make implicit runtime exceptions explicit\n" \
63 " {y--assert-no-exceptions-thrown} \t " \
64 "transform `throw` instructions into `assert FALSE` followed by " \
65 "`assume FALSE`.\n" \
66 " {y--max-nondet-array-length} {uN} \t " \
67 "limit nondet (e.g. input) array size to <= {uN}\n" \
68 " {y--max-nondet-tree-depth} {uN} \t " \
69 "limit size of nondet (e.g. input) object tree; at level {uN} references " \
70 "are set to null\n" \
71 " {y--java-assume-inputs-non-null} \t " \
72 "never initialize reference-typed parameter to the entry point with null\n" \
73 " {y--java-assume-inputs-interval} {y[}{uL}{y:}{uU}|{uL}{y:}|{y:}{uU}{y]} " \
74 "\t " \
75 "force numerical primitive-typed inputs (byte, short, int, long, float, " \
76 "double) to be initialized within the given range; lower bound {uL} and " \
77 "upper bound {uU} must be integers; does not work for arrays\n" \
78 " {y--java-assume-inputs-integral} \t " \
79 "force float and double inputs to have integer values; does not work for " \
80 "arrays\n" \
81 " {y--java-max-vla-length} {uN} \t " \
82 "limit the length of user-code-created arrays\n" \
83 " {y--java-cp-include-files} {ur} \t " \
84 "regexp or JSON list of files to load (with '@' prefix)\n" \
85 " {y--java-load-class} {uCLASS} \t also load code from class {uCLASS}\n" \
86 " {y--java-no-load-class} {uCLASS} \t never load code from class " \
87 "{uCLASS}\n" \
88 " {y--ignore-manifest-main-class} \t " \
89 "ignore Main-Class entries in JAR manifest files. If this option is " \
90 "specified and the options {y--function} and {y--main-class} are not, we " \
91 "can be certain that all classes in the JAR file are loaded.\n" \
92 " {y--context-include} {ui} \t " \
93 "only analyze code matching specification {ui}\n" \
94 " {y--context-exclude} {ue} \t " \
95 "only analyze code does not match specification {ue}. All other methods " \
96 "are excluded, i.e. we load their signatures and meta-information, but not " \
97 "their bodies. A specification is any prefix of a package, class or method " \
98 "name, e.g. \"org.cprover.\" or \"org.cprover.MyClass.\" or " \
99 "\"org.cprover.MyClass.methodToStub:(I)Z\". These options can be given " \
100 "multiple times. The default for context-include is 'all included'; " \
101 "default for context-exclude is 'nothing excluded'.\n" \
102 " {y--no-lazy-methods} \t " \
103 "load and translate all methods given on the command line and in " \
104 "{y--classpath}. Default is to load methods that appear to be reachable " \
105 "from the {y--function} entry point or main class Note that " \
106 "{y--show-symbol-table}, {y--show-goto-functions} and " \
107 "{y--show-properties} output are restricted to loaded methods by default.\n" \
108 " {y--lazy-methods-extra-entry-point} {uMETHODNAME} \t " \
109 "treat {uMETHODNAME} as a possible program entry point for the purpose of " \
110 "lazy method loading {uMETHODNAME} can be a regex that will be matched " \
111 "against all symbols. If missing a java:: prefix will be added. If no " \
112 "descriptor is found, all overloads of a method will also be added.\n" \
113 " {y--static-values} {uf} \t " \
114 "Load initial values of static fields from the given JSON file {uf}. We " \
115 "assign static fields to these values instead of calling the normal " \
116 "static initializer (clinit) method. The argument can be a relative or " \
117 "absolute path to the file.\n" \
118 " {y--java-lift-clinit-calls} \t " \
119 "Lifts clinit calls in function bodies to the top of the function. This " \
120 "may reduce the overall cost of static initialisation, but may be unsound " \
121 "if there are cyclic dependencies between static initializers due to " \
122 "potentially changing their order of execution, or if static initializers " \
123 "have side-effects such as updating another class' static field.\n" \
124
125#ifdef _WIN32
126 #define JAVA_CLASSPATH_SEPARATOR ";"
127#else
128 #define JAVA_CLASSPATH_SEPARATOR ":"
129#endif
130
131#define HELP_JAVA_CLASSPATH \
132 " {y-classpath} {udirs/jars}, {y-cp} {udirs/jars}, " \
133 "{y--classpath} {udirs/jars} \t " \
134 "set class search path of directories and jar files to a " \
135 JAVA_CLASSPATH_SEPARATOR "-separated list of directories and JAR " \
136 "archives to search for class files\n" \
137 " {y--main-class} {uclass-name} \t set the name of the main class\n"
138
139#define HELP_JAVA_METHOD_NAME \
140 " {umethod-name} \t " \
141 "fully qualified name of method used as entry point, e.g. " \
142 "mypackage.Myclass.foo:(I)Z\n"
143
144#define HELP_JAVA_CLASS_NAME \
145 " {uclass-name} \t " \
146 "name of class. The entry point is the method specified by --function, " \
147 "or otherwise, the public static void main(String[]) method of the given " \
148 "class.\n"
149
150#define OPT_JAVA_JAR \
151 "(jar):"
152
153#define HELP_JAVA_JAR \
154 " {y-jar} {ujarfile} \t " \
155 "JAR file to be checked. The entry point is the method specified by " \
156 "{y--function} or otherwise, the public static void main(String[]) method " \
157 "of the class specified by {y--main-class} or the main class specified in " \
158 "the JAR manifest (checked in this order).\n"
159
160#define OPT_JAVA_GOTO_BINARY \
161 "(gb):"
162
163#define HELP_JAVA_GOTO_BINARY \
164 " {y--gb} {ugoto-binary} \t " \
165 "goto-binary file to be checked. The entry point is the method specified " \
166 "by {y--function}, or otherwise, the public static void main(String[]) of " \
167 "the class specified by {y--main-class} (checked in this order).\n"
168// clang-format on
169
176
188{
189public:
191
192 std::unordered_multimap<irep_idt, symbolt> &
193 get(const symbol_table_baset &symbol_table);
194
195 void reinitialize();
196
197private:
198 bool initialized = false;
199 std::unordered_multimap<irep_idt, symbolt> map;
200};
201
257
258#define JAVA_CLASS_MODEL_SUFFIX "@class_model"
259
261{
262public:
263 void set_language_options(const optionst &) override;
264
266
267 virtual bool preprocess(
268 std::istream &instream,
269 const std::string &path,
270 std::ostream &outstream) override;
271
272 // This is an extension to languaget
273 // required because parsing of Java programs can be initiated without
274 // opening a file first or providing a path to a file
275 // as dictated by \ref languaget.
276 virtual bool parse();
277
278 bool parse(
279 std::istream &instream,
280 const std::string &path) override;
281
282 bool generate_support_functions(symbol_table_baset &symbol_table) override;
283
284 bool
285 typecheck(symbol_table_baset &context, const std::string &module) override;
286
287 virtual bool final(symbol_table_baset &context) override;
288
289 void show_parse(std::ostream &out) override;
290
291 virtual ~java_bytecode_languaget();
293 std::unique_ptr<select_pointer_typet> pointer_type_selector)
296 {
297 }
298
304
305 bool from_expr(
306 const exprt &expr,
307 std::string &code,
308 const namespacet &ns) override;
309
310 bool from_type(
311 const typet &type,
312 std::string &code,
313 const namespacet &ns) override;
314
315 bool to_expr(
316 const std::string &code,
317 const std::string &module,
318 exprt &expr,
319 const namespacet &ns) override;
320
321 std::unique_ptr<languaget> new_language() override
323
324 std::string id() const override { return "java"; }
325 std::string description() const override { return "Java Bytecode"; }
326 std::set<std::string> extensions() const override;
327
328 void modules_provided(std::set<std::string> &modules) override;
329 virtual void
330 methods_provided(std::unordered_set<irep_idt> &methods) const override;
331 virtual void convert_lazy_method(
332 const irep_idt &function_id,
333 symbol_table_baset &symbol_table) override;
334
335protected:
337 const irep_idt &function_id,
338 symbol_table_baset &symbol_table,
340 {
342 function_id,
343 symbol_table,
346 }
348 const irep_idt &function_id,
349 symbol_table_baset &symbol_table,
350 optionalt<ci_lazy_methods_neededt> needed_lazy_methods,
353 const irep_idt &function_id,
354 symbol_table_baset &symbol_table,
355 optionalt<ci_lazy_methods_neededt> needed_lazy_methods,
357
360
363 std::vector<irep_idt> main_jar_classes;
368
369private:
370 virtual std::vector<load_extra_methodst>
371 build_extra_entry_points(const optionst &) const;
372 const std::unique_ptr<const select_pointer_typet> pointer_type_selector;
373
380
385 std::unordered_map<std::string, object_creation_referencet> references;
386
389};
390
391std::unique_ptr<languaget> new_java_bytecode_language();
392
393void parse_java_language_options(const cmdlinet &cmd, optionst &options);
394
395prefix_filtert get_context(const optionst &options);
396
397#endif // CPROVER_JAVA_BYTECODE_JAVA_BYTECODE_LANGUAGE_H
Collect methods needed to be loaded using the lazy method.
Context-insensitive lazy methods container.
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:563
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:39
Base class for all expressions.
Definition expr.h:56
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
bool generate_support_functions(symbol_table_baset &symbol_table) override
Create language-specific support functions, such as __CPROVER_start, __CPROVER_initialize and languag...
void set_message_handler(message_handlert &message_handler) override
bool typecheck(symbol_table_baset &context, const std::string &module) 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
java_object_factory_parameterst object_factory_parameters
bool do_ci_lazy_method_conversion(symbol_table_baset &)
Uses a simple context-insensitive ('ci') analysis to determine which methods may be reachable from th...
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 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_table_baset &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.
The symbol table base class interface.
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_table_baset &symbol_table)
Produce code for simple implementation of String Java libraries.
Abstract interface to support a programming language.
STL namespace.
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
Symbol table entry.
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.