Skip to content

Use temporary variable to initialise array cells #4523

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 not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
public class My {
public int func(int[][] inta) {
if (inta[0][0] == 500) {
return 1;
}
return 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
CORE
My.class
--function My.func --unwind 3 --max-nondet-array-length 10 --show-vcc
^EXIT=0$
^SIGNAL=0$
--
^warning: ignoring
\$object
invalid_object
--
This checks that no \$object or invalid_object references are generated when creating a multidimensional
array (required for My.func's input parameter, which is an int[][]). Both of the unwanted terms are used
by goto-symex to describe the result of a failed or unknown pointer dereference, and the object factory's
initialisation code should be clear enough that this is never necessary.
36 changes: 33 additions & 3 deletions jbmc/src/java_bytecode/java_object_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1207,6 +1207,29 @@ void java_object_factoryt::array_loop_init_code(
plus_exprt(array_init_symexpr, counter_expr, array_init_symexpr.type()),
array_init_symexpr.type().subtype());

bool new_item_is_primitive = arraycellref.type().id() != ID_pointer;

// Use a temporary to initialise a new, or update an existing, non-primitive.
// This makes it clearer that in a sequence like
// `new_array_item->x = y; new_array_item->z = w;` that all the
// `new_array_item` references must alias, cf. the harder-to-analyse
// `some_expr[idx]->x = y; some_expr[idx]->z = w;`
exprt init_expr;
if(new_item_is_primitive)
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Any reason to not to do the primitives in the same way, just in the interest of keeping this code simpler?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just to keep it simple indeed. array[idx] = nondet(int) -> new_array_item = nondet(int); array[idx] = new_array_item; seems like a disservice to the reader.

{
init_expr = arraycellref;
}
else
{
init_expr = allocate_objects.allocate_automatic_local_object(
arraycellref.type(), "new_array_item");
Copy link
Contributor

Choose a reason for hiding this comment

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

Please use the same name as in the comment above (either this to new_item, or the comment to new_array_item


// If we're updating an existing array item, read the existing object that
// we (may) alter:
if(update_in_place != update_in_placet::NO_UPDATE_IN_PLACE)
assignments.add(code_assignt(init_expr, arraycellref));
}

// MUST_UPDATE_IN_PLACE only applies to this object.
// If this is a pointer to another object, offer the chance
// to leave it alone by setting MAY_UPDATE_IN_PLACE instead.
Expand All @@ -1216,16 +1239,23 @@ void java_object_factoryt::array_loop_init_code(
update_in_place;
gen_nondet_init(
assignments,
arraycellref,
false, // is_sub
false, // skip_classid
init_expr,
false, // is_sub
false, // skip_classid
// These are variable in number, so use dynamic allocator:
lifetimet::DYNAMIC,
element_type, // override
depth,
child_update_in_place,
location);

if(!new_item_is_primitive)
{
// We used a temporary variable to update or initialise an array item;
// now write it into the array:
assignments.add(code_assignt(arraycellref, init_expr));
}

exprt java_one=from_integer(1, java_int_type());
code_assignt incr(counter_expr, plus_exprt(counter_expr, java_one));

Expand Down