-
Notifications
You must be signed in to change notification settings - Fork 273
Add support for inheritence of Java static fields. Fixes TG-2457 #1826
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
smowton
merged 14 commits into
diffblue:develop
from
smowton:smowton/fix/java-inherited-static-fields
Feb 14, 2018
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f3160e1
resolve_concrete_function_callt -> resolve_inherited_componentt
smowton 168c2a8
Generalise get_inherited_method
smowton bea6371
Annotate static fields with accessibility
smowton e73e756
Create stub globals: check for inherited globals
smowton 5b3cde5
Use a common instance of class_hierarchyt in get_inherited_component
smowton 045ac05
Create stub globals on the first parent incomplete class
smowton b2d3d61
Search static fields inherited from interfaces
smowton 32cc538
Insert stub static globals onto any incomplete ancestor class
smowton d6783d8
Java method converter: look for inherited static fields
smowton 873e1f6
Guess public access for stub globals
smowton d4d4a9a
Tolerate stub static fields defined on non-stub types
smowton afa443c
US spelling of initialize
smowton f15c312
Exclude j.l.O from possible static field hosts.
smowton 1da5be1
Add tests for inherited static fields
smowton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
public class Test extends Parent { | ||
|
||
public static void main(int nondet) { | ||
|
||
x = nondet; | ||
assert x == Parent.x; | ||
|
||
} | ||
|
||
} | ||
|
||
class Parent { | ||
|
||
public static int x; | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
CORE | ||
Test.class | ||
|
||
^VERIFICATION SUCCESSFUL$ | ||
-- | ||
-- | ||
This checks that jbmc knows that test.x and parent.x refer to the same field. | ||
Both test and parent are concrete (available) classes. |
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
|
||
public class Parent { | ||
|
||
// This is the version of Parent we will run jbmc against, which has | ||
// a static field 'x', but that field is private and cannot correspond | ||
// to the reference 'Test.x'. | ||
private static int x; | ||
|
||
} |
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
public class Test extends Parent { | ||
|
||
public static void main(int nondet) { | ||
|
||
x = nondet; | ||
assert x == Parent.x; | ||
|
||
} | ||
|
||
} |
Binary file added
BIN
+202 Bytes
regression/cbmc-java/inherited_static_field10/compile_against/Parent.class
Binary file not shown.
8 changes: 8 additions & 0 deletions
8
regression/cbmc-java/inherited_static_field10/compile_against/Parent.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
public class Parent { | ||
|
||
// This is the version of Parent we compile Test.java against, which does | ||
// have a static field 'x'. | ||
public static int x; | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
CORE | ||
Test.class | ||
|
||
Stub static field x found for non-stub type java::Test\. In future this will be a fatal error\. | ||
assertion at file Test\.java line 6 .* FAILURE | ||
^VERIFICATION FAILED$ | ||
-- | ||
Stub static field x found for non-stub type java::Parent\. In future this will be a fatal error\. | ||
-- | ||
This test is the same as inherited_static_field9, except that the version of Parent we run against | ||
has a private static field that Test's 'x' cannot be referring to. |
Binary file added
BIN
+428 Bytes
regression/cbmc-java/inherited_static_field2/InterfaceWithStatic.class
Binary file not shown.
12 changes: 12 additions & 0 deletions
12
regression/cbmc-java/inherited_static_field2/InterfaceWithStatic.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import java.util.ArrayList; | ||
|
||
interface InterfaceWithStatic { | ||
|
||
// The ArrayList is only here to define a constant x that | ||
// is complicated enough for javac to generate a static init | ||
// rather than just propagate the constant to all users. | ||
static ArrayList<Integer> al = new ArrayList<Integer>(); | ||
static int x = al.size(); | ||
|
||
} | ||
|
Binary file not shown.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
public class Test extends Parent implements InterfaceWithStatic { | ||
|
||
public static void main() { | ||
|
||
int val1 = InterfaceWithStatic.x; | ||
int val2 = x; | ||
assert val1 == val2; | ||
|
||
} | ||
|
||
} | ||
|
||
class Parent { | ||
|
||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
CORE | ||
Test.class | ||
|
||
^VERIFICATION SUCCESSFUL$ | ||
-- | ||
-- | ||
This checks that jbmc knows that Test.x and InterfaceWithStatic.x are the same field. | ||
All types are concrete (not stubs). |
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
public class Test extends OpaqueParent { | ||
|
||
public static void main() { | ||
|
||
assert x == OpaqueParent.x; | ||
|
||
} | ||
|
||
} | ||
|
||
class OpaqueParent { | ||
|
||
public static int x; | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
CORE | ||
Test.class | ||
|
||
^VERIFICATION SUCCESSFUL$ | ||
-- | ||
-- | ||
This is the same as test inherited_static_field1, except Test's parent is opaque. |
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
public class Test extends Parent { | ||
|
||
public static void main(int nondet) { | ||
|
||
x = nondet; | ||
assert x == Grandparent.x; | ||
|
||
} | ||
|
||
} | ||
|
||
class Parent extends Grandparent { | ||
|
||
public static int x; | ||
|
||
} | ||
|
||
class Grandparent { | ||
|
||
public static int x; | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
CORE | ||
Test.class | ||
|
||
assertion at file Test\.java line 6 .* FAILURE | ||
^VERIFICATION FAILED$ | ||
-- | ||
-- | ||
This checks that jbmc knows that the hidden field Grandparent.x is not the same | ||
as Test.x, which resolves to Parent.x. |
Binary file added
BIN
+428 Bytes
regression/cbmc-java/inherited_static_field5/InterfaceWithStatic.class
Binary file not shown.
12 changes: 12 additions & 0 deletions
12
regression/cbmc-java/inherited_static_field5/InterfaceWithStatic.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import java.util.ArrayList; | ||
|
||
interface InterfaceWithStatic { | ||
|
||
// The ArrayList is only here to define a constant x that | ||
// is complicated enough for javac to generate a static init | ||
// rather than just propagate the constant to all users. | ||
static ArrayList<Integer> al = new ArrayList<Integer>(); | ||
static int x = al.size(); | ||
|
||
} | ||
|
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
public class Test extends OpaqueParent implements InterfaceWithStatic { | ||
|
||
public static void main() { | ||
|
||
int val1 = x; | ||
int val2 = InterfaceWithStatic.x; | ||
assert val1 == val2; | ||
|
||
} | ||
|
||
} | ||
|
||
class OpaqueParent { | ||
|
||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
CORE | ||
Test.class | ||
|
||
^VERIFICATION SUCCESSFUL$ | ||
-- | ||
-- | ||
This is the same as inherited_static_field2, except that Test's parent is opaque. It must pass because | ||
the field 'x' should be found on InterfaceWithStatic, and so not created as a stub on OpaqueParent. |
Binary file not shown.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
public class Test extends OpaqueParent { | ||
|
||
public static void main(int nondet) { | ||
|
||
x = nondet; | ||
assert x == Grandparent.x; | ||
|
||
} | ||
|
||
} | ||
|
||
class OpaqueParent extends Grandparent { | ||
|
||
public static int x; | ||
|
||
} | ||
|
||
class Grandparent { | ||
|
||
public static int x; | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
CORE | ||
Test.class | ||
|
||
assertion at file Test\.java line 6 .* FAILURE | ||
^VERIFICATION FAILED$ | ||
-- | ||
-- | ||
This test is the same as inherited_static_field4, except that Test's parent is opaque. | ||
This checks that jbmc guesses that the hidden field Grandparent.x is not the same | ||
as Test.x, which may resolve to the (unseen) Parent.x. |
12 changes: 12 additions & 0 deletions
12
regression/cbmc-java/inherited_static_field7/OpaqueInterfaceWithStatic.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import java.util.ArrayList; | ||
|
||
interface OpaqueInterfaceWithStatic { | ||
|
||
// The ArrayList is only here to define a constant x that | ||
// is complicated enough for javac to generate a static init | ||
// rather than just propagate the constant to all users. | ||
static ArrayList<Integer> al = new ArrayList<Integer>(); | ||
static int x = al.size(); | ||
|
||
} | ||
|
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
public class Test implements OpaqueInterfaceWithStatic { | ||
|
||
public static void main() { | ||
|
||
int val1 = x; | ||
int val2 = OpaqueInterfaceWithStatic.x; | ||
assert val1 == val2; | ||
|
||
} | ||
|
||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
CORE | ||
Test.class | ||
|
||
^VERIFICATION SUCCESSFUL$ | ||
-- | ||
-- | ||
This test must pass, as there is only one opaque parent (interface_with_static). | ||
In particular this one makes sure we don't identify Object as opaque and guess that | ||
java.lang.Object.x exists (since we know Object doesn't have static fields). |
Binary file not shown.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
public class Test extends Parent { | ||
|
||
public static void main(int nondet) { | ||
|
||
x = nondet; | ||
assert x == Parent.x; | ||
assert x == OpaqueGrandparent.x; | ||
|
||
} | ||
|
||
} | ||
|
||
class Parent extends OpaqueGrandparent { | ||
|
||
public static int x; | ||
|
||
} | ||
|
||
class OpaqueGrandparent { | ||
|
||
public static int x; | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
CORE | ||
Test.class | ||
|
||
assertion at file Test\.java line 6 .* SUCCESS | ||
assertion at file Test\.java line 7 .* FAILURE | ||
^VERIFICATION FAILED$ | ||
-- | ||
-- | ||
This test is the same as inherited_static_field4, except that Test's grandparent is opaque. | ||
This checks that jbmc infers that the hidden field Grandparent.x is not the same | ||
as Test.x, which is known to resolve to Parent.x. |
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
public class Parent { | ||
|
||
// This is the version of Parent we will run jbmc against, which does | ||
// not have a static field 'x'. | ||
|
||
} |
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
public class Test extends Parent { | ||
|
||
public static void main(int nondet) { | ||
|
||
x = nondet; | ||
assert x == Parent.x; | ||
|
||
} | ||
|
||
} |
Binary file added
BIN
+202 Bytes
regression/cbmc-java/inherited_static_field9/compile_against/Parent.class
Binary file not shown.
8 changes: 8 additions & 0 deletions
8
regression/cbmc-java/inherited_static_field9/compile_against/Parent.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
public class Parent { | ||
|
||
// This is the version of Parent we compile Test.java against, which does | ||
// have a static field 'x'. | ||
public static int x; | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
CORE | ||
Test.class | ||
|
||
Stub static field x found for non-stub type java::Test\. In future this will be a fatal error\. | ||
Stub static field x found for non-stub type java::Parent\. In future this will be a fatal error\. | ||
assertion at file Test\.java line 6 .* FAILURE | ||
^VERIFICATION FAILED$ | ||
-- | ||
-- | ||
This tests the corner case where static fields are found on non-stub types, here caused | ||
by compiling Test.class against a version of Parent.class (in directory compile_against) | ||
that has a static field but then analysing it with one that doesn't have that field. | ||
In future, as the warning messages we check for note, this will be a fatal error. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did not know it is valid Java to have a static main method that doesn't take
String[]
- awesome.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It isn't in Java, but it is in jbmc :) The JVM would say "that's not a proper main()", but jbmc only cares about the name, not the arg types.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not blocking - but I'd suggest this isn't a great thing to therefore rely on in tests...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With #1768, you'll have to provide a --function to make this test run.