Skip to content

Commit 38e6e4a

Browse files
authored
Merge pull request diffblue#1813 from smowton/smowton/fix/cleanup-unused-clinits
Java frontend: clean up unused clinit symbols
2 parents 278e4e6 + 19d622b commit 38e6e4a

21 files changed

+134
-5
lines changed
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
public class Test {
2+
3+
// Refers to, but never instantiates or refers to static fields of, Unused.
4+
// Thus a clinit-wrapper for Unused should be created, but subsequently
5+
// discarded.
6+
Unused u;
7+
8+
public static void main() {
9+
10+
}
11+
12+
}
13+
14+
class Unused {
15+
16+
static int x;
17+
18+
static {
19+
x = 1;
20+
}
21+
22+
}
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CORE
2+
Test.class
3+
--show-goto-functions
4+
java::Unused::clinit_wrapper
5+
Unused\.<clinit>\(\)
6+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CORE
2+
Test.class
3+
--lazy-methods --show-goto-functions
4+
--
5+
java::Unused::clinit_wrapper
6+
Unused\.<clinit>\(\)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CORE
2+
Test.class
3+
--lazy-methods
4+
VERIFICATION SUCCESSFUL
5+
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
public class Test {
2+
3+
// Refers to, but never instantiates or refers to static fields of, Unused.
4+
// Thus a clinit-wrapper for Unused should be created, but subsequently
5+
// discarded.
6+
Unused1 u;
7+
8+
public static void main() {
9+
10+
}
11+
12+
}
13+
14+
class Unused1 extends Unused2 {
15+
16+
}
17+
18+
class Unused2 {
19+
20+
static int x;
21+
22+
static {
23+
x = 1;
24+
}
25+
26+
}
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CORE
2+
Test.class
3+
--show-goto-functions
4+
java::Unused1::clinit_wrapper
5+
java::Unused2::clinit_wrapper
6+
Unused2\.<clinit>\(\)
7+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CORE
2+
Test.class
3+
--lazy-methods --show-goto-functions
4+
--
5+
java::Unused1::clinit_wrapper
6+
java::Unused2::clinit_wrapper
7+
Unused2\.<clinit>\(\)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CORE
2+
Test.class
3+
--lazy-methods
4+
VERIFICATION SUCCESSFUL
5+
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
public class Test {
2+
3+
public static void main() {
4+
5+
}
6+
7+
public static Opaque unused() {
8+
9+
// Should cause jbmc to create a synthetic static initialiser for Opaque,
10+
// but because this function isn't called by main() it should then be
11+
// cleaned up as unused.
12+
return Opaque.field;
13+
14+
}
15+
16+
}
17+
18+
class Opaque {
19+
20+
public static Opaque field;
21+
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CORE
2+
Test.class
3+
--show-goto-functions
4+
java::Opaque\.<clinit>:\(\)V
5+
java::Opaque::clinit_wrapper
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CORE
2+
Test.class
3+
--lazy-methods --show-goto-functions
4+
--
5+
java::Opaque\.<clinit>:\(\)V
6+
java::Opaque::clinit_wrapper
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CORE
2+
Test.class
3+
--lazy-methods
4+
VERIFICATION SUCCESSFUL
5+

src/java_bytecode/ci_lazy_methods.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,16 @@ ci_lazy_methodst::ci_lazy_methodst(
3636
java_class_loadert &java_class_loader,
3737
const std::vector<irep_idt> &extra_needed_classes,
3838
const select_pointer_typet &pointer_type_selector,
39-
message_handlert &message_handler)
39+
message_handlert &message_handler,
40+
const synthetic_methods_mapt &synthetic_methods)
4041
: messaget(message_handler),
4142
main_class(main_class),
4243
main_jar_classes(main_jar_classes),
4344
lazy_methods_extra_entry_points(lazy_methods_extra_entry_points),
4445
java_class_loader(java_class_loader),
4546
extra_needed_classes(extra_needed_classes),
46-
pointer_type_selector(pointer_type_selector)
47+
pointer_type_selector(pointer_type_selector),
48+
synthetic_methods(synthetic_methods)
4749
{
4850
// build the class hierarchy
4951
class_hierarchy(symbol_table);
@@ -196,7 +198,8 @@ bool ci_lazy_methodst::operator()(
196198
// Don't keep functions that belong to this language that we haven't
197199
// converted above
198200
if(
199-
method_bytecode.contains_method(sym.first) &&
201+
(method_bytecode.contains_method(sym.first) ||
202+
synthetic_methods.count(sym.first)) &&
200203
!methods_already_populated.count(sym.first))
201204
{
202205
continue;

src/java_bytecode/ci_lazy_methods.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <java_bytecode/java_class_loader.h>
2424
#include <java_bytecode/ci_lazy_methods_needed.h>
2525
#include <java_bytecode/select_pointer_type.h>
26+
#include <java_bytecode/synthetic_methods_map.h>
2627

2728
class java_string_library_preprocesst;
2829

@@ -99,7 +100,8 @@ class ci_lazy_methodst:public messaget
99100
java_class_loadert &java_class_loader,
100101
const std::vector<irep_idt> &extra_needed_classes,
101102
const select_pointer_typet &pointer_type_selector,
102-
message_handlert &message_handler);
103+
message_handlert &message_handler,
104+
const synthetic_methods_mapt &synthetic_methods);
103105

104106
// not const since messaget
105107
bool operator()(
@@ -164,6 +166,7 @@ class ci_lazy_methodst:public messaget
164166
java_class_loadert &java_class_loader;
165167
const std::vector<irep_idt> &extra_needed_classes;
166168
const select_pointer_typet &pointer_type_selector;
169+
const synthetic_methods_mapt &synthetic_methods;
167170
};
168171

169172
#endif // CPROVER_JAVA_BYTECODE_GATHER_METHODS_LAZILY_H

src/java_bytecode/java_bytecode_language.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,8 @@ bool java_bytecode_languaget::do_ci_lazy_method_conversion(
788788
java_class_loader,
789789
java_load_classes,
790790
get_pointer_type_selector(),
791-
get_message_handler());
791+
get_message_handler(),
792+
synthetic_methods);
792793

793794
return method_gather(symbol_table, method_bytecode, method_converter);
794795
}

0 commit comments

Comments
 (0)