Skip to content

Java frontend: only run cproverNondetInitialize after all fields are initialized #1769

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
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/cbmc-java/NondetInit2/Test.class
Binary file not shown.
16 changes: 16 additions & 0 deletions regression/cbmc-java/NondetInit2/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import org.cprover.CProver;

class Test {

int[] arr;

void cproverNondetInitialize() {
CProver.assume(arr != null && arr.length == 1);
// The following access should now be legal:
arr[0] = 100;
}

public static void main(Test nondetInput) {
}

}
5 changes: 5 additions & 0 deletions regression/cbmc-java/NondetInit2/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CORE
Test.class

^VERIFICATION SUCCESSFUL$

Binary file added regression/cbmc-java/NondetInit3/Subclass.class
Binary file not shown.
Binary file added regression/cbmc-java/NondetInit3/Test.class
Binary file not shown.
22 changes: 22 additions & 0 deletions regression/cbmc-java/NondetInit3/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import org.cprover.CProver;

class Test {

int[] arr;

void cproverNondetInitialize() {
CProver.assume(arr != null && arr.length == 1);
// The following access should now be legal:
arr[0] = 100;
}

public static void main(Subclass nondetInput) {
// The condition enforced by cproverNondetInitialize should hold
// even though the parameter is a subtype of Test, not directly an
// instance of Test itself.
assert nondetInput.arr.length == 1;
}

}

class Subclass extends Test { }
5 changes: 5 additions & 0 deletions regression/cbmc-java/NondetInit3/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CORE
Test.class

^VERIFICATION SUCCESSFUL$

10 changes: 5 additions & 5 deletions src/java_bytecode/java_object_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1076,16 +1076,16 @@ void java_object_factoryt::gen_nondet_struct_init(
}
}

// If <class_identifier>.cproverValidate() can be found in the
// If <class_identifier>.cproverNondetInitialize() can be found in the
// symbol table, we add a call:
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// expr.cproverValidate();
// expr.cproverNondetInitialize();
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

if(const auto func = symbol_table.lookup(validate_method_name))
if(const auto func = symbol_table.lookup(init_method_name))
{
const code_typet &type = to_code_type(func->type);
code_function_callt fun_call;
Expand Down