Skip to content

Commit 5669d9b

Browse files
committed
Java: run nondet-initialize method *after* initialization
Previously this method was run after each supertype of a relevant type was initialised, e.g. if MyClass <: MySupertype <: Object, then MyClass.cproverNondetInitialize would get run after my inner Object was set up, then again after MySupertype was set up, then one more time after the whole class was set up. Now it will only run after the whole initialisation is complete. This fixes a bug where array members could have inconsistent length and data field values because they had not been initialised yet, leading to an assertion failure during nondetInitialize, and also has the desirable side-effect that an initialiser will still be run when creating a subtype.
1 parent 0b5a5c3 commit 5669d9b

File tree

8 files changed

+53
-5
lines changed

8 files changed

+53
-5
lines changed
452 Bytes
Binary file not shown.
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import org.cprover.CProver;
2+
3+
class Test {
4+
5+
int[] arr;
6+
7+
void cproverNondetInitialize() {
8+
CProver.assume(arr != null && arr.length == 1);
9+
// The following access should now be legal:
10+
arr[0] = 100;
11+
}
12+
13+
public static void main(Test nondetInput) {
14+
}
15+
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CORE
2+
Test.class
3+
4+
^VERIFICATION SUCCESSFUL$
5+
174 Bytes
Binary file not shown.
729 Bytes
Binary file not shown.
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import org.cprover.CProver;
2+
3+
class Test {
4+
5+
int[] arr;
6+
7+
void cproverNondetInitialize() {
8+
CProver.assume(arr != null && arr.length == 1);
9+
// The following access should now be legal:
10+
arr[0] = 100;
11+
}
12+
13+
public static void main(Subclass nondetInput) {
14+
// The condition enforced by cproverNondetInitialize should hold
15+
// even though the parameter is a subtype of Test, not directly an
16+
// instance of Test itself.
17+
assert nondetInput.arr.length == 1;
18+
}
19+
20+
}
21+
22+
class Subclass extends Test { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CORE
2+
Test.class
3+
4+
^VERIFICATION SUCCESSFUL$
5+

src/java_bytecode/java_object_factory.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -1076,16 +1076,16 @@ void java_object_factoryt::gen_nondet_struct_init(
10761076
}
10771077
}
10781078

1079-
// If <class_identifier>.cproverValidate() can be found in the
1079+
// If <class_identifier>.cproverNondetInitialize() can be found in the
10801080
// symbol table, we add a call:
10811081
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1082-
// expr.cproverValidate();
1082+
// expr.cproverNondetInitialize();
10831083
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10841084

1085-
const irep_idt validate_method_name =
1086-
"java::" + id2string(class_identifier) + ".cproverNondetInitialize:()V";
1085+
const irep_idt init_method_name =
1086+
"java::" + id2string(struct_tag) + ".cproverNondetInitialize:()V";
10871087

1088-
if(const auto func = symbol_table.lookup(validate_method_name))
1088+
if(const auto func = symbol_table.lookup(init_method_name))
10891089
{
10901090
const code_typet &type = to_code_type(func->type);
10911091
code_function_callt fun_call;

0 commit comments

Comments
 (0)