cprover
Loading...
Searching...
No Matches
string_constraint_generator_main.cpp
Go to the documentation of this file.
1/*******************************************************************\
2
3Module: Generates string constraints to link results from string functions
4 with their arguments. This is inspired by the PASS paper at HVC'13:
5 "PASS: String Solving with Parameterized Array and Interval Automaton"
6 by Guodong Li and Indradeep Ghosh, which gives examples of constraints
7 for several functions.
8
9Author: Romain Brenguier, romain.brenguier@diffblue.com
10
11\*******************************************************************/
12
19
22
23#include <iterator>
24
25#include <util/arith_tools.h>
26#include <util/deprecate.h>
29#include <util/simplify_expr.h>
30#include <util/ssa_expr.h>
32
34 const namespacet &ns,
35 message_handlert &message_handler)
36 : array_pool(fresh_symbol), ns(ns), message_handler(message_handler)
37{
38}
39
41{
42 PRECONDITION(sum.operands().size() == 2);
43 const exprt zero = from_integer(0, sum.op0().type());
44 const binary_relation_exprt op0_negative(sum.op0(), ID_lt, zero);
45 const binary_relation_exprt op1_negative(sum.op1(), ID_lt, zero);
46 const binary_relation_exprt sum_negative(sum, ID_lt, zero);
47
48 // overflow happens when we add two values of same sign but their sum has a
49 // different sign
50 return and_exprt(
51 equal_exprt(op0_negative, op1_negative),
52 notequal_exprt(op1_negative, sum_negative));
53}
54
64 const exprt &return_code,
66{
67 PRECONDITION(f.arguments().size() == 2);
68
72 f.arguments()[0].id() == ID_index ? to_index_expr(f.arguments()[0]).array()
73 : f.arguments()[0]);
74
75 const exprt &pointer_expr = f.arguments()[1];
76 array_pool.insert(simplify_expr(pointer_expr, ns), array_expr);
77 // created_strings.emplace(to_array_string_expr(array_expr));
78 return equal_exprt{return_code, from_integer(0, f.type())};
79}
80
88 const exprt &return_code,
90{
91 PRECONDITION(f.arguments().size() == 2);
93 const exprt &new_length = f.arguments()[1];
94
95 const auto &length = array_pool.get_or_create_length(array_expr);
96 return and_exprt{equal_exprt{return_code, from_integer(0, f.type())},
97 equal_exprt(length, new_length)};
98}
99
102{
103 std::move(
104 other.existential.begin(),
105 other.existential.end(),
106 std::back_inserter(result.existential));
107 std::move(
108 other.universal.begin(),
109 other.universal.end(),
110 std::back_inserter(result.universal));
111 std::move(
112 other.not_contains.begin(),
113 other.not_contains.end(),
114 std::back_inserter(result.not_contains));
115}
116
130 const array_string_exprt &s,
131 const exprt &start,
132 const exprt &end,
133 const std::string &char_set)
134{
135 // Parse char_set
136 PRECONDITION(char_set.length() == 3);
137 PRECONDITION(char_set[1] == '-');
138 const integer_intervalt char_range(char_set[0], char_set[2]);
139
140 // Add constraint
141 const symbol_exprt qvar = fresh_symbol("char_constr", s.length_type());
142 const exprt chr = s[qvar];
143 const string_constraintt sc(
144 qvar,
145 zero_if_negative(start),
146 zero_if_negative(end),
147 interval_constraint(chr, char_range),
149 return {{}, {sc}, {}};
150}
151
162std::pair<exprt, string_constraintst>
165{
166 const auto &args = f.arguments();
167 PRECONDITION(3 <= args.size() && args.size() <= 5);
168 PRECONDITION(args[2].type().id() == ID_string);
169 PRECONDITION(args[2].is_constant());
170
171 const array_string_exprt s = array_pool.find(args[1], args[0]);
172 const irep_idt &char_set_string = to_constant_expr(args[2]).get_value();
173 const exprt &start =
174 args.size() >= 4 ? args[3] : from_integer(0, s.length_type());
175 const exprt &end =
176 args.size() >= 5 ? args[4] : array_pool.get_or_create_length(s);
177 auto constraints =
178 add_constraint_on_characters(s, start, end, char_set_string.c_str());
179 return {from_integer(0, get_return_code_type()), std::move(constraints)};
180}
181
186
188{
189 const exprt &name = expr.function();
190 PRECONDITION(name.id() == ID_symbol);
192 return to_symbol_expr(name).get_identifier();
193}
194
196 const exprt &return_code,
197 const function_application_exprt &expr)
198{
199 const irep_idt &id = get_function_name(expr);
200 if(id == ID_cprover_associate_array_to_pointer_func)
201 return associate_array_to_pointer(return_code, expr);
202 else if(id == ID_cprover_associate_length_to_array_func)
203 return associate_length_to_array(return_code, expr);
204 return {};
205}
206
212std::pair<exprt, string_constraintst>
214 const function_application_exprt &expr)
215{
216 const irep_idt &id = get_function_name(expr);
217
218 if(id == ID_cprover_char_literal_func)
219 return add_axioms_for_char_literal(expr);
220 else if(id == ID_cprover_string_length_func)
221 return add_axioms_for_length(expr);
222 else if(id == ID_cprover_string_equal_func)
223 return add_axioms_for_equals(expr);
224 else if(id == ID_cprover_string_equals_ignore_case_func)
226 else if(id == ID_cprover_string_is_empty_func)
227 return add_axioms_for_is_empty(expr);
228 else if(id == ID_cprover_string_char_at_func)
229 return add_axioms_for_char_at(expr);
230 else if(id == ID_cprover_string_is_prefix_func)
231 return add_axioms_for_is_prefix(expr, false);
232 else if(id == ID_cprover_string_is_suffix_func)
233 return add_axioms_for_is_suffix(expr, false);
234 else if(id == ID_cprover_string_startswith_func)
235 return add_axioms_for_is_prefix(expr, true);
236 else if(id == ID_cprover_string_endswith_func)
237 return add_axioms_for_is_suffix(expr, true);
238 else if(id == ID_cprover_string_contains_func)
239 return add_axioms_for_contains(expr);
240 else if(id == ID_cprover_string_index_of_func)
241 return add_axioms_for_index_of(expr);
242 else if(id == ID_cprover_string_last_index_of_func)
243 return add_axioms_for_last_index_of(expr);
244 else if(
245 id == ID_cprover_string_is_valid_int_func ||
246 id == ID_cprover_string_is_valid_long_func)
247 return add_axioms_for_is_valid_int(expr);
248 else if(id == ID_cprover_string_parse_int_func)
249 return add_axioms_for_parse_int(expr);
250 else if(id == ID_cprover_string_code_point_at_func)
251 return add_axioms_for_code_point_at(expr);
252 else if(id == ID_cprover_string_code_point_before_func)
254 else if(id == ID_cprover_string_code_point_count_func)
256 else if(id == ID_cprover_string_offset_by_code_point_func)
258 else if(id == ID_cprover_string_compare_to_func)
259 return add_axioms_for_compare_to(expr);
260 else if(id == ID_cprover_string_literal_func)
261 return add_axioms_from_literal(expr);
262 else if(id == ID_cprover_string_concat_code_point_func)
264 else if(id == ID_cprover_string_substring_func)
265 return add_axioms_for_substring(expr);
266 else if(id == ID_cprover_string_trim_func)
267 return add_axioms_for_trim(expr);
268 else if(id == ID_cprover_string_empty_string_func)
269 return add_axioms_for_empty_string(expr);
270 else if(id == ID_cprover_string_copy_func)
271 return add_axioms_for_copy(expr);
272 else if(id == ID_cprover_string_of_int_hex_func)
273 return add_axioms_from_int_hex(expr);
274 else if(id == ID_cprover_string_of_float_func)
276 else if(id == ID_cprover_string_of_float_scientific_notation_func)
278 else if(id == ID_cprover_string_of_double_func)
279 return add_axioms_from_double(expr);
280 else if(id == ID_cprover_string_of_long_func)
281 return add_axioms_from_long(expr);
282 else if(id == ID_cprover_string_set_length_func)
283 return add_axioms_for_set_length(expr);
284 else if(id == ID_cprover_string_delete_func)
285 return add_axioms_for_delete(expr);
286 else if(id == ID_cprover_string_delete_char_at_func)
288 else if(id == ID_cprover_string_replace_func)
289 return add_axioms_for_replace(expr);
290 else if(id == ID_cprover_string_constrain_characters_func)
292 else
293 {
294 std::string msg(
295 "string_constraint_generator::function_application: unknown symbol :");
296 msg += id2string(id);
298 }
300}
301
308DEPRECATED(SINCE(2017, 10, 5, "should use substring instead"))
310string_constraint_generatort::add_axioms_for_copy(
312{
313 const auto &args = f.arguments();
314 PRECONDITION(args.size() == 3 || args.size() == 5);
315 const array_string_exprt res = array_pool.find(args[1], args[0]);
316 const array_string_exprt str = get_string_expr(array_pool, args[2]);
317 const typet &index_type = str.length_type();
318 const exprt offset = args.size() == 3 ? from_integer(0, index_type) : args[3];
319 const exprt count =
320 args.size() == 3 ? array_pool.get_or_create_length(str) : args[4];
321 return add_axioms_for_substring(res, str, offset, plus_exprt(offset, count));
322}
323
329std::pair<exprt, string_constraintst>
337
341{
342 return binary_relation_exprt(x, ID_ge, from_integer(0, x.type()));
343}
344
348std::pair<exprt, string_constraintst>
351{
354 args.size() == 1); // there should be exactly 1 arg to char literal
355
356 const exprt &arg = args[0];
357 // for C programs argument to char literal should be one string constant
358 // of size 1.
359 if(
360 arg.operands().size() == 1 &&
361 to_unary_expr(arg).op().operands().size() == 1 &&
362 to_unary_expr(to_unary_expr(arg).op()).op().operands().size() == 2 &&
363 to_binary_expr(to_unary_expr(to_unary_expr(arg).op()).op()).op0().id() ==
364 ID_string_constant)
365 {
367 to_binary_expr(to_unary_expr(to_unary_expr(arg).op()).op()).op0());
368 const std::string &sval = id2string(s.get_value());
369 CHECK_RETURN(sval.size() == 1);
370 return {from_integer(unsigned(sval[0]), arg.type()), {}};
371 }
372 else
373 {
375 }
376}
377
385std::pair<exprt, string_constraintst>
388{
389 PRECONDITION(f.arguments().size() == 2);
391 symbol_exprt char_sym =
393 string_constraintst constraints;
394 constraints.existential = {equal_exprt(char_sym, str[f.arguments()[1]])};
395 return {std::move(char_sym), std::move(constraints)};
396}
397
398exprt minimum(const exprt &a, const exprt &b)
399{
400 return if_exprt(binary_relation_exprt(a, ID_le, b), a, b);
401}
402
403exprt maximum(const exprt &a, const exprt &b)
404{
405 return if_exprt(binary_relation_exprt(a, ID_le, b), b, a);
406}
407
412{
413 return maximum(from_integer(0, expr.type()), expr);
414}
415
418std::pair<exprt, string_constraintst>
420 std::pair<exprt, string_constraintst> result1,
421 std::pair<exprt, string_constraintst> result2)
422{
423 const exprt return_code = maximum(result1.first, result2.first);
424 merge(result2.second, std::move(result1.second));
425 return {simplify_expr(return_code, ns), std::move(result2.second)};
426}
constant_exprt from_integer(const mp_integer &int_value, const typet &type)
array_string_exprt get_string_expr(array_poolt &array_pool, const exprt &expr)
Fetch the string_exprt corresponding to the given refined_string_exprt.
bitvector_typet index_type()
Definition c_types.cpp:22
Boolean AND.
Definition std_expr.h:2071
exprt get_or_create_length(const array_string_exprt &s)
Get the length of an array_string_exprt from the array_pool.
void insert(const exprt &pointer_expr, const array_string_exprt &array)
array_string_exprt find(const exprt &pointer, const exprt &length)
Creates a new array if the pointer is not pointing to an array.
const typet & length_type() const
Definition string_expr.h:70
const typet & element_type() const
The type of the elements of the array.
Definition std_types.h:783
A base class for relations, i.e., binary predicates whose two operands have the same type.
Definition std_expr.h:707
const irep_idt & get_value() const
Definition std_expr.h:2950
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition dstring.h:39
const char * c_str() const
Definition dstring.h:117
Equality.
Definition std_expr.h:1306
Base class for all expressions.
Definition expr.h:56
typet & type()
Return the type of the expression.
Definition expr.h:84
operandst & operands()
Definition expr.h:94
Application of (mathematical) function.
The trinary if-then-else operator.
Definition std_expr.h:2323
exprt & array()
Definition std_expr.h:1440
const irept & find(const irep_idt &name) const
Definition irep.cpp:101
const irep_idt & id() const
Definition irep.h:396
exprt & op1()
Definition std_expr.h:883
exprt & op0()
Definition std_expr.h:877
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition namespace.h:91
Disequality.
Definition std_expr.h:1365
The plus expression Associativity is not specified.
Definition std_expr.h:947
Fixed-width bit-vector with two's complement interpretation.
const irep_idt & get_value() const
std::pair< exprt, string_constraintst > add_axioms_for_empty_string(const function_application_exprt &f)
Add axioms to say that the returned string expression is empty.
std::pair< exprt, string_constraintst > add_axioms_for_compare_to(const function_application_exprt &f)
Lexicographic comparison of two strings.
std::pair< exprt, string_constraintst > add_axioms_for_equals_ignore_case(const function_application_exprt &f)
Equality of the content ignoring case of characters.
std::pair< exprt, string_constraintst > add_axioms_for_delete(const array_string_exprt &res, const array_string_exprt &str, const exprt &start, const exprt &end)
Add axioms stating that res corresponds to the input str where we removed characters between the posi...
std::pair< exprt, string_constraintst > add_axioms_for_last_index_of(const array_string_exprt &str, const exprt &c, const exprt &from_index)
Add axioms stating that the returned value is the index within haystack (str) of the last occurrence ...
std::pair< exprt, string_constraintst > add_axioms_from_double(const function_application_exprt &f)
Add axioms corresponding to the String.valueOf(D) java function.
exprt associate_array_to_pointer(const exprt &return_code, const function_application_exprt &f)
Associate a char array to a char pointer.
std::pair< exprt, string_constraintst > add_axioms_for_substring(const array_string_exprt &res, const array_string_exprt &str, const exprt &start, const exprt &end)
Add axioms ensuring that res corresponds to the substring of str between indexes ‘start’ = max(start,...
std::pair< exprt, string_constraintst > add_axioms_for_equals(const function_application_exprt &f)
Equality of the content of two strings.
std::pair< exprt, string_constraintst > add_axioms_for_is_valid_int(const function_application_exprt &f)
Check a string is a valid integer, using the same rules as Java Integer.parseInt.
std::pair< exprt, string_constraintst > add_axioms_from_int_hex(const array_string_exprt &res, const exprt &i)
Add axioms stating that the string res corresponds to the integer argument written in hexadecimal.
std::pair< exprt, string_constraintst > add_axioms_for_is_empty(const function_application_exprt &f)
Add axioms stating that the returned value is true exactly when the argument string is empty.
optionalt< exprt > make_array_pointer_association(const exprt &return_code, const function_application_exprt &expr)
Associate array to pointer, and array to length.
std::pair< exprt, string_constraintst > add_axioms_for_index_of(const array_string_exprt &str, const exprt &c, const exprt &from_index)
Add axioms stating that the returned value is the index within haystack (str) of the first occurrence...
std::pair< exprt, string_constraintst > add_axioms_for_constrain_characters(const function_application_exprt &f)
Add axioms to ensure all characters of a string belong to a given set.
std::pair< exprt, string_constraintst > add_axioms_for_function_application(const function_application_exprt &expr)
strings contained in this call are converted to objects of type string_exprt, through adding axioms.
std::pair< exprt, string_constraintst > add_axioms_for_set_length(const function_application_exprt &f)
Reduce or extend a string to have the given length.
std::pair< exprt, string_constraintst > add_axioms_for_offset_by_code_point(const function_application_exprt &f)
Add axioms corresponding the String.offsetByCodePointCount java function.
std::pair< exprt, string_constraintst > add_axioms_for_is_prefix(const array_string_exprt &prefix, const array_string_exprt &str, const exprt &offset)
Add axioms stating that the returned expression is true exactly when the offset is greater or equal t...
std::pair< exprt, string_constraintst > add_axioms_for_trim(const function_application_exprt &f)
Remove leading and trailing whitespaces.
std::pair< exprt, string_constraintst > add_axioms_for_replace(const function_application_exprt &f)
Replace a character by another in a string.
std::pair< exprt, string_constraintst > add_axioms_for_parse_int(const function_application_exprt &f)
Integer value represented by a string.
exprt associate_length_to_array(const exprt &return_code, const function_application_exprt &f)
Associate an integer length to a char array.
std::pair< exprt, string_constraintst > add_axioms_from_literal(const function_application_exprt &f)
String corresponding to an internal cprover string.
std::pair< exprt, string_constraintst > add_axioms_for_is_suffix(const function_application_exprt &f, bool swap_arguments)
Test if the target is a suffix of the string.
std::pair< exprt, string_constraintst > add_axioms_for_contains(const function_application_exprt &f)
Test whether a string contains another.
string_constraint_generatort(const namespacet &ns, message_handlert &message_handler)
string_constraintst add_constraint_on_characters(const array_string_exprt &s, const exprt &start, const exprt &end, const std::string &char_set)
Add constraint on characters of a string.
std::pair< exprt, string_constraintst > add_axioms_from_float_scientific_notation(const array_string_exprt &res, const exprt &f)
Add axioms to write the float in scientific notation.
std::pair< exprt, string_constraintst > add_axioms_for_copy(const function_application_exprt &f)
add axioms to say that the returned string expression is equal to the argument of the function applic...
std::pair< exprt, string_constraintst > add_axioms_for_string_of_float(const function_application_exprt &f)
String representation of a float value.
std::pair< exprt, string_constraintst > add_axioms_from_long(const function_application_exprt &f)
Add axioms corresponding to the String.valueOf(J) java function.
std::pair< exprt, string_constraintst > add_axioms_for_code_point_count(const function_application_exprt &f)
Add axioms corresponding the String.codePointCount java function.
std::pair< exprt, string_constraintst > add_axioms_for_concat_code_point(const function_application_exprt &f)
Add axioms corresponding to the StringBuilder.appendCodePoint(I) function.
std::pair< exprt, string_constraintst > add_axioms_for_code_point_at(const function_application_exprt &f)
add axioms corresponding to the String.codePointAt java function
std::pair< exprt, string_constraintst > add_axioms_for_char_at(const function_application_exprt &f)
Character at a given position.
std::pair< exprt, string_constraintst > add_axioms_for_code_point_before(const function_application_exprt &f)
add axioms corresponding to the String.codePointBefore java function
std::pair< exprt, string_constraintst > add_axioms_for_delete_char_at(const function_application_exprt &expr)
add axioms corresponding to the StringBuilder.deleteCharAt java function
std::pair< exprt, string_constraintst > add_axioms_for_length(const function_application_exprt &f)
Length of a string.
std::pair< exprt, string_constraintst > add_axioms_for_char_literal(const function_application_exprt &f)
add axioms stating that the returned value is equal to the argument
std::pair< exprt, string_constraintst > combine_results(std::pair< exprt, string_constraintst > result1, std::pair< exprt, string_constraintst > result2)
Combine the results of two add_axioms function by taking the maximum of the return codes and merging ...
Expression to hold a symbol (variable)
Definition std_expr.h:113
const irep_idt & get_identifier() const
Definition std_expr.h:142
The type of an expression, extends irept.
Definition type.h:29
#define SINCE(year, month, day, msg)
Definition deprecate.h:26
#define DEPRECATED(msg)
Definition deprecate.h:23
exprt interval_constraint(const exprt &expr, const integer_intervalt &interval)
Given an exprt and an integer interval return an exprt that represents restricting the expression to ...
const std::string & id2string(const irep_idt &d)
Definition irep.h:47
API to expression classes for 'mathematical' expressions.
STL namespace.
nonstd::optional< T > optionalt
Definition optional.h:35
exprt simplify_expr(exprt src, const namespacet &ns)
#define CHECK_RETURN(CONDITION)
Definition invariant.h:495
#define UNREACHABLE
This should be used to mark dead code.
Definition invariant.h:525
#define DATA_INVARIANT(CONDITION, REASON)
This condition should be used to document that assumptions that are made on goto_functions,...
Definition invariant.h:534
#define PRECONDITION(CONDITION)
Definition invariant.h:463
#define UNIMPLEMENTED
Definition invariant.h:558
bool is_ssa_expr(const exprt &expr)
Definition ssa_expr.h:125
const index_exprt & to_index_expr(const exprt &expr)
Cast an exprt to an index_exprt.
Definition std_expr.h:1478
const binary_exprt & to_binary_expr(const exprt &expr)
Cast an exprt to a binary_exprt.
Definition std_expr.h:660
const unary_exprt & to_unary_expr(const exprt &expr)
Cast an exprt to a unary_exprt.
Definition std_expr.h:361
const constant_exprt & to_constant_expr(const exprt &expr)
Cast an exprt to a constant_exprt.
Definition std_expr.h:2992
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
Definition std_expr.h:222
bool is_constant(const typet &type)
This method tests, if the given typet is a constant.
Definition std_types.h:29
const array_typet & to_array_type(const typet &type)
Cast a typet to an array_typet.
Definition std_types.h:844
const string_constantt & to_string_constant(const exprt &expr)
Generates string constraints to link results from string functions with their arguments.
exprt maximum(const exprt &a, const exprt &b)
void merge(string_constraintst &result, string_constraintst other)
Merge two sets of constraints by appending to the first one.
exprt zero_if_negative(const exprt &expr)
Returns a non-negative version of the argument.
signedbv_typet get_return_code_type()
exprt is_positive(const exprt &x)
static irep_idt get_function_name(const function_application_exprt &expr)
exprt sum_overflows(const plus_exprt &sum)
exprt maximum(const exprt &a, const exprt &b)
void merge(string_constraintst &result, string_constraintst other)
Merge two sets of constraints by appending to the first one.
exprt minimum(const exprt &a, const exprt &b)
exprt zero_if_negative(const exprt &expr)
Returns a non-negative version of the argument.
signedbv_typet get_return_code_type()
array_string_exprt & to_array_string_expr(exprt &expr)
Definition string_expr.h:96
#define string_refinement_invariantt(reason)
Collection of constraints of different types: existential formulas, universal formulas,...
std::vector< string_not_contains_constraintt > not_contains
std::vector< exprt > existential
std::vector< string_constraintt > universal