cprover
string_constraint_generator.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: 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 
9 Author: Romain Brenguier, romain.brenguier@diffblue.com
10 
11 \*******************************************************************/
12 
19 
20 #ifndef CPROVER_SOLVERS_REFINEMENT_STRING_CONSTRAINT_GENERATOR_H
21 #define CPROVER_SOLVERS_REFINEMENT_STRING_CONSTRAINT_GENERATOR_H
22 
23 #include <limits>
25 #include <util/constexpr.def>
26 #include <util/deprecate.h>
28 #include <util/replace_expr.h>
29 #include <util/string_expr.h>
30 
31 #include "array_pool.h"
32 
35 struct string_constraintst final
36 {
37  std::vector<exprt> existential;
38  std::vector<string_constraintt> universal;
39  std::vector<string_not_contains_constraintt> not_contains;
40 };
41 
42 void merge(string_constraintst &result, string_constraintst other);
43 
45 {
46 public:
47  // This module keeps a list of axioms. It has methods which generate
48  // string constraints for different string functions and add them
49  // to the axiom list.
50 
52 
53  std::pair<exprt, string_constraintst>
55 
57 
59 
60  const namespacet ns;
61 
66  const exprt &return_code,
67  const function_application_exprt &expr);
68 
69 private:
71  const exprt &return_code,
73 
75  const exprt &return_code,
77 
78 public:
79  std::pair<exprt, string_constraintst> add_axioms_for_concat(
80  const array_string_exprt &res,
81  const array_string_exprt &s1,
82  const array_string_exprt &s2);
83 
84  std::pair<exprt, string_constraintst> add_axioms_for_concat_substr(
85  const array_string_exprt &res,
86  const array_string_exprt &s1,
87  const array_string_exprt &s2,
88  const exprt &start_index,
89  const exprt &end_index);
90  std::pair<exprt, string_constraintst> add_axioms_for_insert(
91  const array_string_exprt &res,
92  const array_string_exprt &s1,
93  const array_string_exprt &s2,
94  const exprt &offset);
95  std::pair<exprt, string_constraintst> add_axioms_for_string_of_int_with_radix(
96  const array_string_exprt &res,
97  const exprt &input_int,
98  const exprt &radix,
99  size_t max_size);
100 
102  const array_string_exprt &s,
103  const exprt &start,
104  const exprt &end,
105  const std::string &char_set);
106  std::pair<exprt, string_constraintst>
108 
109  // The following functions add axioms for the returned value
110  // to be equal to the result of the function given as argument.
111  // They are not accessed directly from other classes: they call
112  // `add_axioms_for_function_application` which determines which of
113  // these methods should be called.
114 
115  std::pair<exprt, string_constraintst>
117  std::pair<exprt, string_constraintst>
119 
120  std::pair<exprt, string_constraintst>
122  std::pair<exprt, string_constraintst>
124  std::pair<exprt, string_constraintst>
126  std::pair<exprt, string_constraintst>
128 
129  std::pair<exprt, string_constraintst>
131  std::pair<exprt, string_constraintst> add_axioms_for_is_prefix(
132  const array_string_exprt &prefix,
133  const array_string_exprt &str,
134  const exprt &offset);
135  std::pair<exprt, string_constraintst> add_axioms_for_is_prefix(
137  bool swap_arguments);
138 
139  std::pair<exprt, string_constraintst> add_axioms_for_is_suffix(
141  bool swap_arguments);
142  std::pair<exprt, string_constraintst>
144  std::pair<exprt, string_constraintst>
146 
147  std::pair<exprt, string_constraintst>
149 
150  std::pair<exprt, string_constraintst>
152  std::pair<exprt, string_constraintst> add_axioms_for_constant(
153  const array_string_exprt &res,
154  irep_idt sval,
155  const exprt &guard = true_exprt());
156 
157  std::pair<exprt, string_constraintst> add_axioms_for_delete(
158  const array_string_exprt &res,
159  const array_string_exprt &str,
160  const exprt &start,
161  const exprt &end);
162  std::pair<exprt, string_constraintst>
164  std::pair<exprt, string_constraintst>
166 
167  std::pair<exprt, string_constraintst> add_axioms_for_cprover_string(
168  const array_string_exprt &res,
169  const exprt &arg,
170  const exprt &guard);
171  std::pair<exprt, string_constraintst>
173 
174  std::pair<exprt, string_constraintst> add_axioms_for_string_of_int(
175  const array_string_exprt &res,
176  const exprt &input_int,
177  size_t max_size);
178  std::pair<exprt, string_constraintst>
179  add_axioms_from_int_hex(const array_string_exprt &res, const exprt &i);
180  std::pair<exprt, string_constraintst>
182  std::pair<exprt, string_constraintst>
184  std::pair<exprt, string_constraintst>
186  std::pair<exprt, string_constraintst>
187  add_axioms_from_bool(const array_string_exprt &res, const exprt &b);
188  std::pair<exprt, string_constraintst>
190  std::pair<exprt, string_constraintst>
192  std::pair<exprt, string_constraintst> add_axioms_for_index_of(
193  const array_string_exprt &str,
194  const exprt &c,
195  const exprt &from_index);
196  std::pair<exprt, string_constraintst> add_axioms_for_index_of_string(
197  const array_string_exprt &haystack,
198  const array_string_exprt &needle,
199  const exprt &from_index);
200  std::pair<exprt, string_constraintst>
202  std::pair<exprt, string_constraintst> add_axioms_for_last_index_of_string(
203  const array_string_exprt &haystack,
204  const array_string_exprt &needle,
205  const exprt &from_index);
206  std::pair<exprt, string_constraintst> add_axioms_for_last_index_of(
207  const array_string_exprt &str,
208  const exprt &c,
209  const exprt &from_index);
210 
211  std::pair<exprt, string_constraintst>
213 
219  std::pair<exprt, string_constraintst>
221  std::pair<exprt, string_constraintst>
223  std::pair<exprt, string_constraintst> add_axioms_for_fractional_part(
224  const array_string_exprt &res,
225  const exprt &int_expr,
226  size_t max_size);
227  std::pair<exprt, string_constraintst>
229  const array_string_exprt &res,
230  const exprt &f);
231  std::pair<exprt, string_constraintst>
233  const function_application_exprt &f);
234 
237  std::pair<exprt, string_constraintst>
239 
240  std::pair<exprt, string_constraintst>
242  std::pair<exprt, string_constraintst>
244 
248  std::pair<exprt, string_constraintst> add_axioms_for_substring(
249  const array_string_exprt &res,
250  const array_string_exprt &str,
251  const exprt &start,
252  const exprt &end);
253  std::pair<exprt, string_constraintst>
255 
256  std::pair<exprt, string_constraintst>
258 
259  std::pair<exprt, string_constraintst> add_axioms_for_code_point(
260  const array_string_exprt &res,
261  const exprt &code_point);
262  std::pair<exprt, string_constraintst>
264 
269  DEPRECATED(SINCE(2017, 10, 5, "Java specific, should be implemented in Java"))
270  std::pair<exprt, string_constraintst>
272 
279  DEPRECATED(SINCE(2017, 10, 5, "Java specific, should be implemented in Java"))
280  std::pair<exprt, string_constraintst>
282 
284  const exprt &input_int,
285  const typet &type,
286  const bool strict_formatting,
287  const array_string_exprt &str,
288  const std::size_t max_string_length,
289  const exprt &radix,
290  const unsigned long radix_ul);
292  const array_string_exprt &str,
293  const exprt &radix_as_char,
294  const unsigned long radix_ul,
295  const std::size_t max_size,
296  const bool strict_formatting);
297  std::pair<exprt, string_constraintst>
299  std::pair<exprt, string_constraintst>
301  std::pair<exprt, string_constraintst>
303 
305  std::pair<exprt, string_constraintst> result1,
306  std::pair<exprt, string_constraintst> result2);
307 
311  {
317  unsigned long radix_ul;
319  // (or pessimistic assumption of base-2 if unknown) and result type. For
320  // example, the longest possible decimal int64 is 16 characters long in hex.
321  std::size_t max_string_length;
322  };
323 
326  const typet &target_int_type);
327 };
328 
330  const array_string_exprt &res,
331  const array_string_exprt &s1,
332  array_poolt &array_pool);
334  const array_string_exprt &res,
335  const array_string_exprt &s1,
336  const array_string_exprt &s2,
337  array_poolt &array_pool);
339  const array_string_exprt &res,
340  const array_string_exprt &s1,
341  const array_string_exprt &s2,
342  const exprt &start,
343  const exprt &end,
344  array_poolt &array_pool);
345 
346 size_t max_printed_string_length(const typet &type, unsigned long ul_radix);
347 
348 exprt is_positive(const exprt &x);
349 
351 exprt minimum(const exprt &a, const exprt &b);
352 
354 exprt maximum(const exprt &a, const exprt &b);
355 
357 exprt sum_overflows(const plus_exprt &sum);
358 
359 // Type used by primitives to signal errors
361 
362 exprt zero_if_negative(const exprt &expr);
363 
365  const exprt &chr,
366  const bool strict_formatting,
367  const exprt &radix_as_char,
368  const unsigned long radix_ul);
369 
371  const exprt &chr,
372  const typet &char_type,
373  const typet &type,
374  const bool strict_formatting,
375  unsigned long radix_ul);
376 
377 #endif
Associates arrays and length to pointers, so that the string refinement can transform builtin functio...
int16_t s2
Definition: bytecode_info.h:60
int8_t s1
Definition: bytecode_info.h:59
bitvector_typet char_type()
Definition: c_types.cpp:114
Correspondance between arrays and pointers string representations.
Definition: array_pool.h:43
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:37
Base class for all expressions.
Definition: expr.h:54
Application of (mathematical) function.
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:92
The plus expression Associativity is not specified.
Definition: std_expr.h:831
Fixed-width bit-vector with two's complement interpretation.
Definition: std_types.h:1272
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_insert(const array_string_exprt &res, const array_string_exprt &s1, const array_string_exprt &s2, const exprt &offset)
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_from_char(const function_application_exprt &f)
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.
std::pair< exprt, string_constraintst > add_axioms_for_last_index_of_string(const array_string_exprt &haystack, const array_string_exprt &needle, const exprt &from_index)
Add axioms stating that the returned value is the index within haystack of the last occurrence of nee...
std::pair< exprt, string_constraintst > add_axioms_for_concat(const array_string_exprt &res, const array_string_exprt &s1, const array_string_exprt &s2)
Add axioms enforcing that res is equal to the concatenation of s1 and s2.
std::vector< exprt > get_conjuncts_for_correct_number_format(const array_string_exprt &str, const exprt &radix_as_char, const unsigned long radix_ul, const std::size_t max_size, const bool strict_formatting)
Add axioms making the return value true if the given string is a correct number in the given radix.
std::pair< exprt, string_constraintst > add_axioms_for_concat_substr(const array_string_exprt &res, const array_string_exprt &s1, const array_string_exprt &s2, const exprt &start_index, const exprt &end_index)
Add axioms enforcing that res is the concatenation of s1 with the substring of s2 starting at index ‘...
std::pair< exprt, string_constraintst > add_axioms_for_cprover_string(const array_string_exprt &res, const exprt &arg, const exprt &guard)
Convert an expression of type string_typet to a string_exprt.
optionalt< exprt > make_array_pointer_association(const exprt &return_code, const function_application_exprt &expr)
Associate array to pointer, and array to length.
parseint_argumentst unpack_parseint_arguments(const function_application_exprt &f, const typet &target_int_type)
std::pair< exprt, string_constraintst > add_axioms_for_fractional_part(const array_string_exprt &res, const exprt &int_expr, size_t max_size)
Add axioms for representing the fractional part of a floating point starting with a dot.
std::pair< exprt, string_constraintst > add_axioms_for_string_of_int_with_radix(const array_string_exprt &res, const exprt &input_int, const exprt &radix, size_t max_size)
Add axioms enforcing that the string corresponds to the result of String.valueOf(II) or String....
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_string_of_int(const array_string_exprt &res, const exprt &input_int, size_t max_size)
Add axioms enforcing that the string corresponds to the result of String.valueOf(I) or String....
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.
string_constraintst add_axioms_for_characters_in_integer_string(const exprt &input_int, const typet &type, const bool strict_formatting, const array_string_exprt &str, const std::size_t max_string_length, const exprt &radix, const unsigned long radix_ul)
Add axioms connecting the characters in the input string to the value of the output integer.
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.
std::pair< exprt, string_constraintst > add_axioms_for_index_of_string(const array_string_exprt &haystack, const array_string_exprt &needle, const exprt &from_index)
Add axioms stating that the returned value index is the index within haystack of the first occurrence...
std::pair< exprt, string_constraintst > add_axioms_from_bool(const function_application_exprt &f)
std::pair< exprt, string_constraintst > add_axioms_for_code_point(const array_string_exprt &res, const exprt &code_point)
add axioms for the conversion of an integer representing a java code point to a utf-16 string
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_from_char(const array_string_exprt &res, const exprt &c)
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_constant(const array_string_exprt &res, irep_idt sval, const exprt &guard=true_exprt())
Add axioms ensuring that the provided string expression and constant are equal.
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 ...
Generation of fresh symbols of a given type.
Definition: array_pool.h:23
The Boolean constant true.
Definition: std_expr.h:2717
The type of an expression, extends irept.
Definition: type.h:28
#define SINCE(year, month, day, msg)
Definition: deprecate.h:26
#define DEPRECATED(msg)
Definition: deprecate.h:23
nonstd::optional< T > optionalt
Definition: optional.h:35
Type for string expressions used by the string solver.
Defines string constraints.
exprt is_positive(const exprt &x)
exprt length_constraint_for_concat(const array_string_exprt &res, const array_string_exprt &s1, const array_string_exprt &s2, array_poolt &array_pool)
Add axioms enforcing that the length of res is that of the concatenation of s1 with s2
exprt length_constraint_for_concat_char(const array_string_exprt &res, const array_string_exprt &s1, array_poolt &array_pool)
Add axioms enforcing that the length of res is that of the concatenation of s1 with.
exprt sum_overflows(const plus_exprt &sum)
exprt length_constraint_for_concat_substr(const array_string_exprt &res, const array_string_exprt &s1, const array_string_exprt &s2, const exprt &start, const exprt &end, array_poolt &array_pool)
Add axioms enforcing that the length of res is that of the concatenation of s1 with the substring of ...
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 get_numeric_value_from_character(const exprt &chr, const typet &char_type, const typet &type, const bool strict_formatting, unsigned long radix_ul)
Get the numeric value of a character, assuming that the radix is large enough.
exprt minimum(const exprt &a, const exprt &b)
exprt zero_if_negative(const exprt &expr)
Returns a non-negative version of the argument.
exprt is_digit_with_radix(const exprt &chr, const bool strict_formatting, const exprt &radix_as_char, const unsigned long radix_ul)
Check if a character is a digit with respect to the given radix, e.g.
size_t max_printed_string_length(const typet &type, unsigned long ul_radix)
Calculate the string length needed to represent any value of the given type using the given radix.
signedbv_typet get_return_code_type()
String expressions for the string solver.
Argument block for parseInt and cousins, common to parseInt itself and CProverString....
unsigned long radix_ul
Radix as an unsigned long (or 0 if unknown)
std::size_t max_string_length
Max string length (assuming no leading zeroes) considering the radix.
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