Skip to content

Java frontend: clean up unused clinit symbols #1813

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
public class Test {

// Refers to, but never instantiates or refers to static fields of, Unused.
// Thus a clinit-wrapper for Unused should be created, but subsequently
// discarded.
Unused u;

public static void main() {

}

}

class Unused {

static int x;

static {
x = 1;
}

}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CORE
Test.class
--show-goto-functions
java::Unused::clinit_wrapper
Unused\.<clinit>\(\)

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CORE
Test.class
--lazy-methods --show-goto-functions
--
java::Unused::clinit_wrapper
Unused\.<clinit>\(\)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CORE
Test.class
--lazy-methods
VERIFICATION SUCCESSFUL

Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
public class Test {

// Refers to, but never instantiates or refers to static fields of, Unused.
// Thus a clinit-wrapper for Unused should be created, but subsequently
// discarded.
Unused1 u;

public static void main() {

}

}

class Unused1 extends Unused2 {

}

class Unused2 {

static int x;

static {
x = 1;
}

}
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CORE
Test.class
--show-goto-functions
java::Unused1::clinit_wrapper
java::Unused2::clinit_wrapper
Unused2\.<clinit>\(\)

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CORE
Test.class
--lazy-methods --show-goto-functions
--
java::Unused1::clinit_wrapper
java::Unused2::clinit_wrapper
Unused2\.<clinit>\(\)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CORE
Test.class
--lazy-methods
VERIFICATION SUCCESSFUL

Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
public class Test {

public static void main() {

}

public static Opaque unused() {

// Should cause jbmc to create a synthetic static initialiser for Opaque,
// but because this function isn't called by main() it should then be
// cleaned up as unused.
return Opaque.field;

}

}

class Opaque {

public static Opaque field;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CORE
Test.class
--show-goto-functions
java::Opaque\.<clinit>:\(\)V
java::Opaque::clinit_wrapper
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CORE
Test.class
--lazy-methods --show-goto-functions
--
java::Opaque\.<clinit>:\(\)V
java::Opaque::clinit_wrapper
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CORE
Test.class
--lazy-methods
VERIFICATION SUCCESSFUL

9 changes: 6 additions & 3 deletions src/java_bytecode/ci_lazy_methods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,16 @@ ci_lazy_methodst::ci_lazy_methodst(
java_class_loadert &java_class_loader,
const std::vector<irep_idt> &extra_needed_classes,
const select_pointer_typet &pointer_type_selector,
message_handlert &message_handler)
message_handlert &message_handler,
const synthetic_methods_mapt &synthetic_methods)
: messaget(message_handler),
main_class(main_class),
main_jar_classes(main_jar_classes),
lazy_methods_extra_entry_points(lazy_methods_extra_entry_points),
java_class_loader(java_class_loader),
extra_needed_classes(extra_needed_classes),
pointer_type_selector(pointer_type_selector)
pointer_type_selector(pointer_type_selector),
synthetic_methods(synthetic_methods)
{
// build the class hierarchy
class_hierarchy(symbol_table);
Expand Down Expand Up @@ -196,7 +198,8 @@ bool ci_lazy_methodst::operator()(
// Don't keep functions that belong to this language that we haven't
// converted above
if(
method_bytecode.contains_method(sym.first) &&
(method_bytecode.contains_method(sym.first) ||
synthetic_methods.count(sym.first)) &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When is this first condition false for symbol entries that are code (surely it is either a user function or a synthetic function?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be read as is-normal-method || is-synthetic-method, which I think matches your expectation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes - but when is the above condition false?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, when the method belongs to a different frontend. For Java that case is currently unsupported, but cross-language or GOTO-binary + Java or similar possibilities are supported by our current architecture, so it would be best not to rule that case out when it's easy to support.

Better summary of this line: "if this method relates to this languaget" (otherwise leave it alone)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes - which matches with the comment 👍

!methods_already_populated.count(sym.first))
{
continue;
Expand Down
5 changes: 4 additions & 1 deletion src/java_bytecode/ci_lazy_methods.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <java_bytecode/java_class_loader.h>
#include <java_bytecode/ci_lazy_methods_needed.h>
#include <java_bytecode/select_pointer_type.h>
#include <java_bytecode/synthetic_methods_map.h>

class java_string_library_preprocesst;

Expand Down Expand Up @@ -99,7 +100,8 @@ class ci_lazy_methodst:public messaget
java_class_loadert &java_class_loader,
const std::vector<irep_idt> &extra_needed_classes,
const select_pointer_typet &pointer_type_selector,
message_handlert &message_handler);
message_handlert &message_handler,
const synthetic_methods_mapt &synthetic_methods);

// not const since messaget
bool operator()(
Expand Down Expand Up @@ -164,6 +166,7 @@ class ci_lazy_methodst:public messaget
java_class_loadert &java_class_loader;
const std::vector<irep_idt> &extra_needed_classes;
const select_pointer_typet &pointer_type_selector;
const synthetic_methods_mapt &synthetic_methods;
};

#endif // CPROVER_JAVA_BYTECODE_GATHER_METHODS_LAZILY_H
3 changes: 2 additions & 1 deletion src/java_bytecode/java_bytecode_language.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,8 @@ bool java_bytecode_languaget::do_ci_lazy_method_conversion(
java_class_loader,
java_load_classes,
get_pointer_type_selector(),
get_message_handler());
get_message_handler(),
synthetic_methods);

return method_gather(symbol_table, method_bytecode, method_converter);
}
Expand Down