-
Notifications
You must be signed in to change notification settings - Fork 273
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
Changes from all commits
28a2ada
682cd1a
9a7fa2d
f3b4c9b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
} | ||
} |
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$ | ||
-- |
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$ | ||
-- |
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 |
---|---|---|
|
@@ -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) | ||
{ | ||
|
@@ -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; | ||
} | ||
|
@@ -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) | ||
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) | ||
|
@@ -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); | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
// clang-format on | ||
|
||
stream << "string_refinementt::check_axioms:" << eom; | ||
|
||
|
@@ -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( | ||
|
@@ -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( | ||
|
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.
Why? Where do you recognise that this should throw an exception in java?
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.
@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.