Skip to content

Fix bugs in String.indexOf(c) TG-1846 #1668

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
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/jbmc-strings/StringIndexOf/Test.class
Binary file not shown.
46 changes: 46 additions & 0 deletions regression/jbmc-strings/StringIndexOf/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
public class Test {

public boolean check(String input_String, char input_char, int input_int) {
// Verify indexOf is conform to its specification
int i = input_String.indexOf(input_char, input_int);

assert i < input_String.length();

int lower_bound;
if (input_int < 0)
lower_bound = 0;
else
lower_bound = input_int;

if (i == -1) {
for (int j = lower_bound; j < input_String.length(); j++)
assert input_String.charAt(j) != input_char;
} else {
assert i >= lower_bound;
assert input_String.charAt(i) == input_char;

for (int j = lower_bound; j < i; j++)
assert input_String.charAt(j) != input_char;
}
return true;
}

public boolean check2() {
// Verification should fail, this is to check the solver does
// not get a contradiction
int i = "hello".indexOf('o', 1);
assert i == 4;
i = "hello".indexOf('h', 1);
assert i == -1;
i = "hello".indexOf('e', 4);
assert i == -1;
i = "hello".indexOf('e', 8);
assert i == -1;
i = "hello".indexOf('x', 0);
assert i == -1;
i = "hello".indexOf('h', -1000);
assert i == 0;
assert false;
return true;
}
}
7 changes: 7 additions & 0 deletions regression/jbmc-strings/StringIndexOf/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CORE
Test.class
--refine-strings --function Test.check --unwind 4 --string-max-length 3 --java-assume-inputs-non-null
^EXIT=0$
^SIGNAL=0$
^VERIFICATION SUCCESSFUL$
--
14 changes: 14 additions & 0 deletions regression/jbmc-strings/StringIndexOf/test2.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
CORE
Test.class
--refine-strings --function Test.check2 --unwind 10 --string-max-length 10 --java-assume-inputs-non-null
^EXIT=10$
^SIGNAL=0$
assertion at file Test.java line 32 .* SUCCESS
assertion at file Test.java line 34 .* SUCCESS
assertion at file Test.java line 36 .* SUCCESS
assertion at file Test.java line 38 .* SUCCESS
assertion at file Test.java line 40 .* SUCCESS
assertion at file Test.java line 42 .* SUCCESS
assertion at file Test.java line 43 .* FAILURE
^VERIFICATION FAILED$
--
7 changes: 7 additions & 0 deletions regression/jbmc-strings/StringIndexOf/test_thorough.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
THOROUGH
Test.class
--refine-strings --function Test.check --unwind 10 --string-max-length 10 --java-assume-inputs-non-null
^EXIT=0$
^SIGNAL=0$
^VERIFICATION SUCCESSFUL$
--
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,19 @@ exprt string_constraint_generatort::add_axioms_for_index_of(
equal_exprt(str[index], c)));
axioms.push_back(a3);

const auto zero = from_integer(0, index_type);
const if_exprt lower_bound(
binary_relation_exprt(from_index, ID_le, zero), zero, from_index);

symbol_exprt n=fresh_univ_index("QA_index_of", index_type);
string_constraintt a4(
n, from_index, index, contains, not_exprt(equal_exprt(str[n], c)));
n, lower_bound, index, contains, not_exprt(equal_exprt(str[n], c)));
axioms.push_back(a4);

symbol_exprt m=fresh_univ_index("QA_index_of", index_type);
string_constraintt a5(
m,
from_index,
lower_bound,
str.length(),
not_exprt(contains),
not_exprt(equal_exprt(str[m], c)));
Expand Down
34 changes: 26 additions & 8 deletions src/solvers/refinement/string_refinement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1076,12 +1076,19 @@ exprt fill_in_array_expr(const array_exprt &expr, std::size_t string_max_length)
/// expression will be: `index==0 ? 24 : index==2 ? 42 : 12`
/// * for an array access `(g1?arr1:arr2)[x]` where `arr1 := {12}` and
/// `arr2 := {34}`, the constructed expression will be: `g1 ? 12 : 34`
/// * for an access in an empty array `{ }[x]` returns a fresh symbol, this
/// corresponds to a non-deterministic result
/// \param expr: an expression containing array accesses
/// \param symbol_generator: function which given a prefix and a type generates
/// a fresh symbol of the given type
/// \return an expression containing no array access
static void substitute_array_access(exprt &expr)
static void substitute_array_access(
exprt &expr,
const std::function<symbol_exprt(const irep_idt &, const typet &)>
&symbol_generator)
{
for(auto &op : expr.operands())
substitute_array_access(op);
substitute_array_access(op, symbol_generator);

if(expr.id()==ID_index)
{
Expand Down Expand Up @@ -1110,9 +1117,9 @@ static void substitute_array_access(exprt &expr)
// Substitute recursively in branches of conditional expressions
if_exprt if_expr=to_if_expr(index_expr.array());
exprt true_case=index_exprt(if_expr.true_case(), index_expr.index());
substitute_array_access(true_case);
substitute_array_access(true_case, symbol_generator);
exprt false_case=index_exprt(if_expr.false_case(), index_expr.index());
substitute_array_access(false_case);
substitute_array_access(false_case, symbol_generator);
expr=if_exprt(if_expr.cond(), true_case, false_case);
return;
}
Expand All @@ -1124,13 +1131,17 @@ static void substitute_array_access(exprt &expr)
"above"));
array_exprt &array_expr=to_array_expr(index_expr.array());

// Empty arrays do not need to be substituted.
const typet &char_type = index_expr.array().type().subtype();

// Access to an empty array is undefined (non deterministic result)
Copy link
Member

Choose a reason for hiding this comment

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

Why? Where do you recognise that this should throw an exception in java?

Copy link
Contributor

Choose a reason for hiding this comment

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

@peterschrammel The exception is (intended) to be thrown at the model level. That's now the contract of the string methods in jbmc. I'm not sure whether this is sufficiently clearly stated, though.

if(array_expr.operands().empty())
{
expr = symbol_generator("out_of_bound_access", char_type);
return;
}

size_t last_index=array_expr.operands().size()-1;

const typet &char_type=index_expr.array().type().subtype();
exprt ite=array_expr.operands().back();

if(ite.type()!=char_type)
Expand Down Expand Up @@ -1342,6 +1353,12 @@ static std::pair<bool, std::vector<exprt>> check_axioms(
const auto eom=messaget::eom;
static const std::string indent = " ";
static const std::string indent2 = " ";
// clang-format off
const auto gen_symbol = [&](const irep_idt &id, const typet &type)
{
return generator.fresh_symbol(id, type);
};
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why have the function as a parameter; why not just call generator.fresh_symbol? Fresh symbol functions feel kind of stateful to me so I think I'd prefer them to be member functions.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Also, can we change the description of the PR as this looks like a refactoring rather than a bug fix.

Copy link
Contributor

Choose a reason for hiding this comment

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

@martin-cs @romainbrenguier is on holiday now so I'm taking over his open PRs. About fresh_symbol, it seems @romainbrenguier already adressed your comment. About the name of the PR, the main goal was to fix some bugs. The refactoring is just a side-effect of that and the commits messages are explicit enough. Do you agree?

// clang-format on

stream << "string_refinementt::check_axioms:" << eom;

Expand Down Expand Up @@ -1383,7 +1400,8 @@ static std::pair<bool, std::vector<exprt>> check_axioms(
negaxiom = simplify_expr(negaxiom, ns);
exprt with_concretized_arrays =
concretize_arrays_in_expression(negaxiom, max_string_length, ns);
substitute_array_access(with_concretized_arrays);

substitute_array_access(with_concretized_arrays, gen_symbol);

stream << indent << i << ".\n";
debug_check_axioms_step(
Expand Down Expand Up @@ -1439,7 +1457,7 @@ static std::pair<bool, std::vector<exprt>> check_axioms(
exprt with_concrete_arrays =
concretize_arrays_in_expression(negaxiom, max_string_length, ns);

substitute_array_access(with_concrete_arrays);
substitute_array_access(with_concrete_arrays, gen_symbol);

stream << indent << i << ".\n";
debug_check_axioms_step(
Expand Down