Skip to content

Make disjuncts in instanceof and virtual method removal stable #2058

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 3 commits into from
Apr 16, 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 added regression/goto-diff/java-instanceof/new.jar
Binary file not shown.
16 changes: 16 additions & 0 deletions regression/goto-diff/java-instanceof/new/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class A {
}

class B extends A {
}

public class Test {

public boolean foo(A x) {
if (x instanceof A) {
return true;
} else {
return false;
}
}
}
Binary file added regression/goto-diff/java-instanceof/old.jar
Binary file not shown.
22 changes: 22 additions & 0 deletions regression/goto-diff/java-instanceof/old/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
old.jar needs to be created by first adding B.class and Test.class and then
updated to add A.class. This makes the class loader load the classes A and B
in reverse order.
*/

class A {
}

class B extends A {
}

public class Test {

public boolean foo(A x) {
if (x instanceof A) {
return true;
} else {
return false;
}
}
}
10 changes: 10 additions & 0 deletions regression/goto-diff/java-instanceof/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CORE
new.jar
old.jar
// Enable multi-line checking
activate-multi-line-match
EXIT=0
SIGNAL=0
new functions:\nmodified functions:\ndeleted functions:
--
^warning: ignoring
Binary file added regression/goto-diff/java-virtual-methods/new.jar
Binary file not shown.
18 changes: 18 additions & 0 deletions regression/goto-diff/java-virtual-methods/new/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class A {
boolean bar() {
return true;
}
}

class B extends A {
boolean bar() {
return false;
}
}

public class Test {

public boolean foo(A x) {
return x.bar();
}
}
Binary file added regression/goto-diff/java-virtual-methods/old.jar
Binary file not shown.
24 changes: 24 additions & 0 deletions regression/goto-diff/java-virtual-methods/old/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
old.jar needs to be created by first adding B.class and Test.class and then
updated to add A.class. This makes the class loader load the classes A and B
in reverse order.
*/

class A {
boolean bar() {
return true;
}
}

class B extends A {
boolean bar() {
return false;
}
}

public class Test {

public boolean foo(A x) {
return x.bar();
}
}
10 changes: 10 additions & 0 deletions regression/goto-diff/java-virtual-methods/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CORE
new.jar
old.jar
// Enable multi-line checking
activate-multi-line-match
EXIT=0
SIGNAL=0
new functions:\nmodified functions:\ndeleted functions:
--
^warning: ignoring
8 changes: 8 additions & 0 deletions src/goto-programs/remove_instanceof.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ std::size_t remove_instanceoft::lower_instanceof(
std::vector<irep_idt> children=
class_hierarchy.get_children_trans(target_name);
children.push_back(target_name);
// Sort alphabetically to make order of generated disjuncts
// independent of class loading order
std::sort(
children.begin(),
children.end(),
[](const irep_idt &a, const irep_idt &b) { // NOLINT
return a.compare(b) < 0;
});

// Insert an instruction before the new check that assigns the clsid we're
// checking for to a temporary, as GOTO program if-expressions should
Expand Down
2 changes: 2 additions & 0 deletions src/goto-programs/remove_virtual_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,8 @@ void remove_virtual_functionst::get_functions(
has_prefix(
id2string(b.symbol_expr.get_identifier()), "java::java.lang.Object"))
return true;
else if(a.symbol_expr.get_identifier() == b.symbol_expr.get_identifier())
Copy link
Contributor

Choose a reason for hiding this comment

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

So if the function names are equal then sort by class names? Don't the function names include the class names though?

Copy link
Member Author

Choose a reason for hiding this comment

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

class_id is the name of the class, whereas symbol_expr is the name of the target method to call (which might be defined in a parent class). We are sorting by target method and if the same target method is used by several classes (these generate the big or expression in the dispatch condition) then we sort by these classes.

return a.class_id < b.class_id;
else
return a.symbol_expr.get_identifier() < b.symbol_expr.get_identifier();
});
Expand Down