Skip to content

Commit caed4f9

Browse files
author
Joel Allred
committed
Add --java-load-containing-class-only option
Add option to only load the class (and inner classes) that contain the method under test.
1 parent 0886132 commit caed4f9

File tree

8 files changed

+68
-7
lines changed

8 files changed

+68
-7
lines changed
Binary file not shown.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class External {
2+
3+
public static int get() {
4+
assert(false);
5+
return 40;
6+
}
7+
8+
}
Binary file not shown.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Test {
2+
3+
public static int test() {
4+
return External.get();
5+
}
6+
7+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CORE
2+
Test.class
3+
--function Test.test --load-containing-class-only
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring
9+
--
10+
Check that External.class is not loaded when using --load-containing-class-only

jbmc/src/java_bytecode/java_bytecode_language.cpp

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@ Author: Daniel Kroening, [email protected]
1010

1111
#include <string>
1212

13-
#include <util/symbol_table.h>
14-
#include <util/suffix.h>
15-
#include <util/config.h>
13+
#include <json/json_parser.h>
1614
#include <util/cmdline.h>
15+
#include <util/config.h>
1716
#include <util/expr_iterator.h>
17+
#include <util/invariant.h>
1818
#include <util/journalling_symbol_table.h>
1919
#include <util/string2int.h>
20-
#include <util/invariant.h>
21-
#include <json/json_parser.h>
20+
#include <util/string_utils.h>
21+
#include <util/suffix.h>
22+
#include <util/symbol_table.h>
2223

2324
#include <goto-programs/class_hierarchy.h>
2425

@@ -124,7 +125,32 @@ void java_bytecode_languaget::get_language_options(const cmdlinet &cmd)
124125
std::back_inserter(extra_methods),
125126
build_load_method_by_regex);
126127

127-
if(cmd.isset("java-cp-include-files"))
128+
if(cmd.isset("load-containing-class-only"))
129+
{
130+
const std::string function_to_analyse = cmd.get_value("function");
131+
const std::size_t first_dollar_index =
132+
function_to_analyse.find_first_of('$');
133+
const std::size_t last_dot_index = function_to_analyse.find_last_of('.');
134+
std::string class_name = function_to_analyse;
135+
if(first_dollar_index != std::string::npos)
136+
class_name = class_name.substr(0, first_dollar_index);
137+
else
138+
class_name = class_name.substr(0, last_dot_index);
139+
replace_all(class_name, ".", "\\/");
140+
java_cp_include_files = class_name +
141+
"(\\$.*)?\\.class|"
142+
"java\\/.*|"
143+
"javax\\/.*|"
144+
"sun\\/.*|"
145+
"org\\/cprover\\/.*|"
146+
"com\\/diffblue\\/annotation\\/.*";
147+
148+
if(cmd.isset("java-cp-include-files"))
149+
warning() << "--java-cp-include-files ignored due to "
150+
"--load-containing-class-only"
151+
<< eom;
152+
}
153+
else if(cmd.isset("java-cp-include-files"))
128154
{
129155
java_cp_include_files=cmd.get_value("java-cp-include-files");
130156
// load file list from JSON file

jbmc/src/java_bytecode/java_bytecode_language.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Author: Daniel Kroening, [email protected]
3838
"(java-max-input-tree-depth):" /* will go away */ \
3939
"(max-nondet-tree-depth):" \
4040
"(java-max-vla-length):" \
41+
"(load-containing-class-only)" \
4142
"(java-cp-include-files):" \
4243
"(lazy-methods)" /* will go away */ \
4344
"(no-lazy-methods)" \
@@ -59,6 +60,8 @@ Author: Daniel Kroening, [email protected]
5960
" never initialize reference-typed parameter to the\n" /* NOLINT(*) */ \
6061
" entry point with null\n" /* NOLINT(*) */ \
6162
" --java-max-vla-length N limit the length of user-code-created arrays\n" /* NOLINT(*) */ \
63+
" --load-containing-class-only only load the class containing the " \
64+
" method under test and the library classes" \
6265
" --java-cp-include-files r regexp or JSON list of files to load\n" \
6366
" (with '@' prefix)\n" \
6467
" --no-lazy-methods load and translate all methods given on\n" \

jbmc/src/java_bytecode/java_class_loader_limit.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ void java_class_loader_limitt::setup_class_load_limit(
2424
// '@' signals file reading with list of class files to load
2525
use_regex_match = java_cp_include_files[0] != '@';
2626
if(use_regex_match)
27+
{
2728
regex_matcher=std::regex(java_cp_include_files);
29+
debug() << "Limit loading to classes matching : " << java_cp_include_files
30+
<< eom;
31+
}
2832
else
2933
{
3034
assert(java_cp_include_files.length()>1);
@@ -54,7 +58,10 @@ bool java_class_loader_limitt::load_class_file(const std::string &file_name)
5458
if(use_regex_match)
5559
{
5660
std::smatch string_matches;
57-
return std::regex_match(file_name, string_matches, regex_matcher);
61+
if(std::regex_match(file_name, string_matches, regex_matcher))
62+
return true;
63+
debug() << file_name + " discarded since not matching loader regexp" << eom;
64+
return false;
5865
}
5966
else
6067
// load .class file only if it is in the match set

0 commit comments

Comments
 (0)