Skip to content

Commit 668c4f3

Browse files
author
Matthias Güdemann
authored
Merge pull request #2772 from mgudemann/doc/doc-21/java_load_jar
[DOC-21] Documentation for java loading from jar / class files
2 parents ccd9783 + fc2e8da commit 668c4f3

File tree

6 files changed

+78
-5
lines changed

6 files changed

+78
-5
lines changed

jbmc/src/java_bytecode/jar_file.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,20 @@ optionalt<std::string> jar_filet::get_entry(const std::string &name)
7777
}
7878
}
7979

80+
/// Wrapper for `std::isspace` from `cctype`
81+
/// \param ch: the character to check
82+
/// \return true if the parameter is considered to be a space in the current
83+
/// locale, else false
8084
static bool is_space(const char ch)
8185
{
8286
return std::isspace(ch) != 0;
8387
}
8488

8589
/// Remove leading and trailing whitespace characters from string
90+
/// \param begin: iterator to start search in string
91+
/// \param end: iterator to end search in string
92+
/// \return string truncated from begin to end and all whitespace removed at the
93+
/// begin and end
8694
static std::string trim(
8795
const std::string::const_iterator begin,
8896
const std::string::const_iterator end)

jbmc/src/java_bytecode/jar_file.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ Author: Diffblue Ltd
1818

1919
#include "mz_zip_archive.h"
2020

21-
/// Class representing a .jar archive
21+
/// Class representing a .jar archive. Uses miniz to decompress and index
22+
/// archive.
2223
class jar_filet final
2324
{
2425
public:
@@ -44,7 +45,8 @@ class jar_filet final
4445
/// \param filename Name of the file in the archive
4546
optionalt<std::string> get_entry(const std::string &filename);
4647

47-
/// Get contents of the Manifest file in the jar archive
48+
/// Get contents of the Manifest file in the jar archive as a key-value map
49+
/// (both as strings)
4850
std::unordered_map<std::string, std::string> get_manifest();
4951

5052
/// Get list of filenames in the archive

jbmc/src/java_bytecode/java_class_loader.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ void java_class_loadert::add_classpath_entry(const std::string &path)
8181
}
8282
}
8383

84+
/// Load class from jar file.
85+
/// \param class_name: name of class to load in Java source format
86+
/// \param jar_file: path of the jar file
87+
/// \param jar_index: the index of the jar file
88+
/// \return optional value of parse tree, empty if class cannot be loaded
8489
optionalt<java_bytecode_parse_treet> java_class_loadert::get_class_from_jar(
8590
const irep_idt &class_name,
8691
const std::string &jar_file,
@@ -102,6 +107,11 @@ optionalt<java_bytecode_parse_treet> java_class_loadert::get_class_from_jar(
102107
return java_bytecode_parse(istream, get_message_handler());
103108
}
104109

110+
/// Check if class is an overlay class by searching for `ID_overlay_class` in
111+
/// its list of annotations. TODO(nathan) give a short explanation about what
112+
/// overlay classes are.
113+
/// \param c: a `classt` object from a java byte code parse tree
114+
/// \return true if parsed class is an overlay class, else false
105115
static bool is_overlay_class(const java_bytecode_parse_treet::classt &c)
106116
{
107117
return java_bytecode_parse_treet::find_annotation(
@@ -228,6 +238,9 @@ java_class_loadert::get_parse_tree(
228238
return parse_trees;
229239
}
230240

241+
/// Load all class files from a .jar file, and store name of .jar in
242+
/// `classpath_entreies`.
243+
/// \param jar_path: the path for the .jar to load
231244
void java_class_loadert::load_entire_jar(
232245
const std::string &jar_path)
233246
{
@@ -280,6 +293,12 @@ java_class_loadert::jar_index_optcreft java_class_loadert::read_jar_file(
280293
return std::cref(jar_index);
281294
}
282295

296+
/// Convert a file name to the class name. Java interprets folders as packages,
297+
/// therefore a prefix of `./` is removed if necessary, and all `/` are
298+
/// converted to `.`. For example a class file `./com/diffblue/test.class` is
299+
/// converted to the class name `com.diffblue.test`.
300+
/// \param file: the name of the class file
301+
/// \return the file name converted to Java class name
283302
std::string java_class_loadert::file_to_class_name(const std::string &file)
284303
{
285304
std::string result=file;
@@ -307,6 +326,10 @@ std::string java_class_loadert::file_to_class_name(const std::string &file)
307326
return result;
308327
}
309328

329+
/// Convert a class name to a file name, does the inverse of \ref
330+
/// file_to_class_name.
331+
/// \param class_name: the name of the class
332+
/// \return the class name converted to file name
310333
std::string java_class_loadert::class_name_to_file(const irep_idt &class_name)
311334
{
312335
std::string result=id2string(class_name);

jbmc/src/java_bytecode/java_class_loader.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,19 @@ Author: Daniel Kroening, [email protected]
2222
#include "java_bytecode_parse_tree.h"
2323
#include "java_class_loader_limit.h"
2424

25+
/// Class responsible to load .class files. Either directly from a specific
26+
/// file, from a classpath specification or from a Java archive (JAR) file.
2527
class java_class_loadert:public messaget
2628
{
2729
public:
2830
/// A map associating logical class names with the name of the .class file
2931
/// implementing it for all classes inside a single JAR file
3032
typedef std::map<irep_idt, std::string> jar_indext;
3133

34+
/// A list of parse trees supporting overlay classes
3235
typedef std::list<java_bytecode_parse_treet> parse_tree_with_overlayst;
36+
/// A map from class names to list of parse tree where multiple entries can
37+
/// exist in case of overlay classes.
3338
typedef std::map<irep_idt, parse_tree_with_overlayst>
3439
parse_tree_with_overridest_mapt;
3540

@@ -54,6 +59,8 @@ class java_class_loadert:public messaget
5459
java_class_loader_limitt &class_loader_limit,
5560
const irep_idt &class_name);
5661

62+
/// Set the argument of the class loader limit \ref java_class_loader_limitt
63+
/// \param java_cp_include_files: argument string for java_class_loader_limit
5764
void set_java_cp_include_files(const std::string &java_cp_include_files)
5865
{
5966
this->java_cp_include_files = java_cp_include_files;

jbmc/src/java_bytecode/java_class_loader_limit.cpp

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,38 @@ Author: Daniel Kroening, [email protected]
1313

1414
#include <json/json_parser.h>
1515

16-
/// initializes class with either regex matcher or match set
17-
/// \par parameters: parameter from `java-cp-include-files`
16+
/// Initializes class with either regex matcher or match set. If the string
17+
/// starts with an `@` it is treated as a path to a JSON file. Otherwise, it is
18+
/// treated as a regex.
19+
///
20+
/// The regex case describes which class files should be loaded in the form of a
21+
/// regular expression used with `regex_match`.
22+
///
23+
/// The match set is a list of files to load in JSON format, the argument is the
24+
/// name of the JSON file, prefixed with `@`. The file contains one section to
25+
/// list the .jar files to load and one section to list the .class files to load
26+
/// from the .jar.
27+
///
28+
/// for example a file called `load.json` with the following content:
29+
/// {
30+
/// "jar":
31+
/// [
32+
/// "A.jar",
33+
/// "B.jar"
34+
/// ],
35+
/// "classFiles":
36+
/// [
37+
/// "jarfile3$A.class",
38+
/// "jarfile3.class"
39+
/// ]
40+
/// }
41+
/// would be specified via `--java-cp-include-files @load.json` and would
42+
/// instruct the driver to load `A.jar` and `B.jar` and the two .class files
43+
/// `jarfile3$A.class` and `jarfile3.class`. All the rest of the .jar files is
44+
/// ignored.
45+
///
46+
/// \param java_cp_include_files: parameter from `java-cp-include-files` in the
47+
/// format as described above
1848
void java_class_loader_limitt::setup_class_load_limit(
1949
const std::string &java_cp_include_files)
2050
{
@@ -51,7 +81,9 @@ void java_class_loader_limitt::setup_class_load_limit(
5181
}
5282
}
5383

54-
/// \par parameters: class file name
84+
/// Use the class load limiter to decide whether a class file should be loaded
85+
/// or not.
86+
/// \param file_name: the name of the class file to load
5587
/// \return true if file should be loaded, else false
5688
bool java_class_loader_limitt::load_class_file(const std::string &file_name)
5789
{

jbmc/src/java_bytecode/java_class_loader_limit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Author: Daniel Kroening, [email protected]
1818
#include <util/irep.h>
1919
#include <util/message.h>
2020

21+
/// Class representing a filter for class file loading.
2122
class java_class_loader_limitt:public messaget
2223
{
2324
/// Whether to use regex_matcher instead of set_matcher

0 commit comments

Comments
 (0)