CBMC
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>
27 #include <util/namespace.h>
29 #include <util/replace_expr.h>
30 #include <util/string_expr.h>
31 
32 #include "array_pool.h"
33 
35 
38 struct string_constraintst final
39 {
40  std::vector<exprt> existential;
41  std::vector<string_constraintt> universal;
42  std::vector<string_not_contains_constraintt> not_contains;
43 };
44 
45 void merge(string_constraintst &result, string_constraintst other);
46 
48 {
49 public:
50  // This module keeps a list of axioms. It has methods which generate
51  // string constraints for different string functions and add them
52  // to the axiom list.
53 
55  const namespacet &ns,
57 
58  std::pair<exprt, string_constraintst>
60 
62 
64 
65  const namespacet ns;
66 
70  std::optional<exprt> make_array_pointer_association(
71  const exprt &return_code,
72  const function_application_exprt &expr);
73 
74 private:
76 
78  const exprt &return_code,
80 
82  const exprt &return_code,
84 
85 public:
86  std::pair<exprt, string_constraintst> add_axioms_for_concat(
87  const array_string_exprt &res,
88  const array_string_exprt &s1,
89  const array_string_exprt &s2);
90 
91  std::pair<exprt, string_constraintst> add_axioms_for_concat_substr(
92  const array_string_exprt &res,
93  const array_string_exprt &s1,
94  const array_string_exprt &s2,
95  const exprt &start_index,
96  const exprt &end_index);
97  std::pair<exprt, string_constraintst> add_axioms_for_insert(
98  const array_string_exprt &res,
99  const array_string_exprt &s1,
100  const array_string_exprt &s2,
101  const exprt &offset);
102  std::pair<exprt, string_constraintst> add_axioms_for_string_of_int_with_radix(
103  const array_string_exprt &res,
104  const exprt &input_int,
105  const exprt &radix,
106  size_t max_size);
107 
109  const array_string_exprt &s,
110  const exprt &start,
111  const exprt &end,
112  const std::string &char_set);
113  std::pair<exprt, string_constraintst>
115 
116  // The following functions add axioms for the returned value
117  // to be equal to the result of the function given as argument.
118  // They are not accessed directly from other classes: they call
119  // `add_axioms_for_function_application` which determines which of
120  // these methods should be called.
121 
122  std::pair<exprt, string_constraintst>
124  std::pair<exprt, string_constraintst>
126 
127  std::pair<exprt, string_constraintst>
129  std::pair<exprt, string_constraintst>
131  std::pair<exprt, string_constraintst>
133  std::pair<exprt, string_constraintst>
135 
136  std::pair<exprt, string_constraintst>
138  std::pair<exprt, string_constraintst> add_axioms_for_is_prefix(
139  const array_string_exprt &prefix,
140  const array_string_exprt &str,
141  const exprt &offset);
142  std::pair<exprt, string_constraintst> add_axioms_for_is_prefix(
144  bool swap_arguments);
145 
146  std::pair<exprt, string_constraintst> add_axioms_for_is_suffix(
148  bool swap_arguments);
149  std::pair<exprt, string_constraintst>
151  std::pair<exprt, string_constraintst>
153 
154  std::pair<exprt, string_constraintst>
156 
157  std::pair<exprt, string_constraintst>
159  std::pair<exprt, string_constraintst> add_axioms_for_constant(
160  const array_string_exprt &res,
161  irep_idt sval,
162  const exprt &guard = true_exprt());
163 
164  std::pair<exprt, string_constraintst> add_axioms_for_delete(
165  const array_string_exprt &res,
166  const array_string_exprt &str,
167  const exprt &start,
168  const exprt &end);
169  std::pair<exprt, string_constraintst>
171  std::pair<exprt, string_constraintst>
173 
174  std::pair<exprt, string_constraintst> add_axioms_for_cprover_string(
175  const array_string_exprt &res,
176  const exprt &arg,
177  const exprt &guard);
178  std::pair<exprt, string_constraintst>
180 
181  std::pair<exprt, string_constraintst> add_axioms_for_string_of_int(
182  const array_string_exprt &res,
183  const exprt &input_int,
184  size_t max_size);
185  std::pair<exprt, string_constraintst>
186  add_axioms_from_int_hex(const array_string_exprt &res, const exprt &i);
187  std::pair<exprt, string_constraintst>
189  std::pair<exprt, string_constraintst>
191  std::pair<exprt, string_constraintst>
193  std::pair<exprt, string_constraintst>
194  add_axioms_from_bool(const array_string_exprt &res, const exprt &b);
195  std::pair<exprt, string_constraintst>
197  std::pair<exprt, string_constraintst>
199  std::pair<exprt, string_constraintst> add_axioms_for_index_of(
200  const array_string_exprt &str,
201  const exprt &c,
202  const exprt &from_index);
203  std::pair<exprt, string_constraintst> add_axioms_for_index_of_string(
204  const array_string_exprt &haystack,
205  const array_string_exprt &needle,
206  const exprt &from_index);
207  std::pair<exprt, string_constraintst>
209  std::pair<exprt, string_constraintst> add_axioms_for_last_index_of_string(
210  const array_string_exprt &haystack,
211  const array_string_exprt &needle,
212  const exprt &from_index);
213  std::pair<exprt, string_constraintst> add_axioms_for_last_index_of(
214  const array_string_exprt &str,
215  const exprt &c,
216  const exprt &from_index);
217 
218  std::pair<exprt, string_constraintst>
220 
226  std::pair<exprt, string_constraintst>
228  std::pair<exprt, string_constraintst>
230  std::pair<exprt, string_constraintst> add_axioms_for_fractional_part(
231  const array_string_exprt &res,
232  const exprt &int_expr,
233  size_t max_size);
234  std::pair<exprt, string_constraintst>
236  const array_string_exprt &res,
237  const exprt &f);
238  std::pair<exprt, string_constraintst>
240  const function_application_exprt &f);
241 
244  std::pair<exprt, string_constraintst>
246 
247  std::pair<exprt, string_constraintst>
249  std::pair<exprt, string_constraintst>
251 
255  std::pair<exprt, string_constraintst> add_axioms_for_substring(
256  const array_string_exprt &res,
257  const array_string_exprt &str,
258  const exprt &start,
259  const exprt &end);
260  std::pair<exprt, string_constraintst>
262 
263  std::pair<exprt, string_constraintst>
265 
266  std::pair<exprt, string_constraintst> add_axioms_for_code_point(
267  const array_string_exprt &res,
268  const exprt &code_point);
269  std::pair<exprt, string_constraintst>
271 
276  DEPRECATED(SINCE(2017, 10, 5, "Java specific, should be implemented in Java"))
277  std::pair<exprt, string_constraintst>
279 
286  DEPRECATED(SINCE(2017, 10, 5, "Java specific, should be implemented in Java"))
287  std::pair<exprt, string_constraintst>
289 
291  const exprt &input_int,
292  const typet &type,
293  const bool strict_formatting,
294  const array_string_exprt &str,
295  const std::size_t max_string_length,
296  const exprt &radix,
297  const unsigned long radix_ul);
299  const array_string_exprt &str,
300  const exprt &radix_as_char,
301  const unsigned long radix_ul,
302  const std::size_t max_size,
303  const bool strict_formatting);
304  std::pair<exprt, string_constraintst>
306  std::pair<exprt, string_constraintst>
308  std::pair<exprt, string_constraintst>
310 
312  std::pair<exprt, string_constraintst> result1,
313  std::pair<exprt, string_constraintst> result2);
314 
318  {
324  unsigned long radix_ul;
326  // (or pessimistic assumption of base-2 if unknown) and result type. For
327  // example, the longest possible decimal int64 is 16 characters long in hex.
328  std::size_t max_string_length;
329  };
330 
333  const typet &target_int_type);
334 };
335 
337  const array_string_exprt &res,
338  const array_string_exprt &s1,
339  array_poolt &array_pool);
341  const array_string_exprt &res,
342  const array_string_exprt &s1,
343  const array_string_exprt &s2,
344  array_poolt &array_pool);
346  const array_string_exprt &res,
347  const array_string_exprt &s1,
348  const array_string_exprt &s2,
349  const exprt &start,
350  const exprt &end,
351  array_poolt &array_pool);
352 
353 size_t max_printed_string_length(const typet &type, unsigned long ul_radix);
354 
355 exprt is_positive(const exprt &x);
356 
358 exprt minimum(const exprt &a, const exprt &b);
359 
361 exprt maximum(const exprt &a, const exprt &b);
362 
364 exprt sum_overflows(const plus_exprt &sum);
365 
366 // Type used by primitives to signal errors
368 
369 exprt zero_if_negative(const exprt &expr);
370 
372  const exprt &chr,
373  const bool strict_formatting,
374  const exprt &radix_as_char,
375  const unsigned long radix_ul);
376 
378  const exprt &chr,
379  const typet &char_type,
380  const typet &type,
381  const bool strict_formatting,
382  unsigned long radix_ul);
383 
384 #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
static exprt guard(const exprt::operandst &guards, exprt cond)
bitvector_typet char_type()
Definition: c_types.cpp:106
Correspondance between arrays and pointers string representations.
Definition: array_pool.h:42
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:38
Base class for all expressions.
Definition: expr.h:56
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:94
The plus expression Associativity is not specified.
Definition: std_expr.h:1002
Fixed-width bit-vector with two's complement interpretation.
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.
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::optional< 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_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_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_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:22
The Boolean constant true.
Definition: std_expr.h:3055
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
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