cprover
string_refinement.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: String support via creating string constraints and progressively
4  instantiating the universal constraints as needed.
5  The procedure is described in the PASS paper at HVC'13:
6  "PASS: String Solving with Parameterized Array and Interval Automaton"
7  by Guodong Li and Indradeep Ghosh.
8 
9 Author: Alberto Griggio, alberto.griggio@gmail.com
10 
11 \*******************************************************************/
12 
19 
20 #include "string_refinement.h"
21 
22 #include <solvers/sat/satcheck.h>
23 #include <stack>
24 #include <unordered_set>
25 
26 #include <util/expr_iterator.h>
27 #include <util/expr_util.h>
28 #include <util/format_type.h>
29 #include <util/magic.h>
30 #include <util/range.h>
31 #include <util/simplify_expr.h>
32 
35 #include "string_dependencies.h"
37 
39  messaget::mstreamt &stream,
40  const namespacet &ns,
41  const string_constraintt &constraint);
42 
44  const namespacet &ns,
45  const exprt &axiom,
46  const symbol_exprt &var,
47  message_handlert &message_handler);
48 
65 static std::pair<bool, std::vector<exprt>> check_axioms(
66  const string_axiomst &axioms,
68  const std::function<exprt(const exprt &)> &get,
69  messaget::mstreamt &stream,
70  const namespacet &ns,
71  bool use_counter_example,
72  const union_find_replacet &symbol_resolve,
73  const std::unordered_map<string_not_contains_constraintt, symbol_exprt>
74  &not_contain_witnesses);
75 
76 static void initial_index_set(
77  index_set_pairt &index_set,
78  const namespacet &ns,
79  const string_constraintt &axiom);
80 
81 static void initial_index_set(
82  index_set_pairt &index_set,
83  const namespacet &ns,
84  const string_not_contains_constraintt &axiom);
85 
86 static void initial_index_set(
87  index_set_pairt &index_set,
88  const namespacet &ns,
89  const string_axiomst &axioms);
90 
91 exprt simplify_sum(const exprt &f);
92 
93 static void update_index_set(
94  index_set_pairt &index_set,
95  const namespacet &ns,
96  const std::vector<exprt> &current_constraints);
97 
98 static void update_index_set(
99  index_set_pairt &index_set,
100  const namespacet &ns,
101  const exprt &formula);
102 
103 static std::vector<exprt> instantiate(
104  const string_not_contains_constraintt &axiom,
105  const index_set_pairt &index_set,
106  const std::unordered_map<string_not_contains_constraintt, symbol_exprt>
107  &witnesses);
108 
110  const std::function<exprt(const exprt &)> &super_get,
111  const namespacet &ns,
112  messaget::mstreamt &stream,
113  const array_string_exprt &arr,
114  const array_poolt &array_pool);
115 
117  const index_exprt &index_expr,
118  symbol_generatort &symbol_generator,
119  const bool left_propagate);
120 
128 template <typename T>
129 static std::vector<T>
130 fill_in_map_as_vector(const std::map<std::size_t, T> &index_value)
131 {
132  std::vector<T> result;
133  if(!index_value.empty())
134  {
135  result.resize(index_value.rbegin()->first + 1);
136  for(auto it = index_value.rbegin(); it != index_value.rend(); ++it)
137  {
138  const std::size_t index = it->first;
139  const T &value = it->second;
140  const auto next = std::next(it);
141  const std::size_t leftmost_index_to_pad =
142  next != index_value.rend() ? next->first + 1 : 0;
143  for(std::size_t j = leftmost_index_to_pad; j <= index; j++)
144  result[j] = value;
145  }
146  }
147  return result;
148 }
149 
150 static bool validate(const string_refinementt::infot &info)
151 {
152  PRECONDITION(info.ns);
153  PRECONDITION(info.prop);
154  return true;
155 }
156 
158  : supert(info),
159  config_(info),
160  loop_bound_(info.refinement_bound),
161  generator(*info.ns)
162 {
163 }
164 
166  : string_refinementt(info, validate(info))
167 {
168 }
169 
171 static void
173 {
174  std::size_t count = 0;
175  std::size_t count_current = 0;
176  for(const auto &i : index_set.cumulative)
177  {
178  const exprt &s = i.first;
179  stream << "IS(" << format(s) << ")=={" << messaget::eom;
180 
181  for(const auto &j : i.second)
182  {
183  const auto it = index_set.current.find(i.first);
184  if(
185  it != index_set.current.end() && it->second.find(j) != it->second.end())
186  {
187  count_current++;
188  stream << "**";
189  }
190  stream << " " << format(j) << ";" << messaget::eom;
191  count++;
192  }
193  stream << "}" << messaget::eom;
194  }
195  stream << count << " elements in index set (" << count_current
196  << " newly added)" << messaget::eom;
197 }
198 
220 static std::vector<exprt> generate_instantiations(
221  const index_set_pairt &index_set,
222  const string_axiomst &axioms,
223  const std::unordered_map<string_not_contains_constraintt, symbol_exprt>
224  &not_contain_witnesses)
225 {
226  std::vector<exprt> lemmas;
227  for(const auto &i : index_set.current)
228  {
229  for(const auto &univ_axiom : axioms.universal)
230  {
231  for(const auto &j : i.second)
232  lemmas.push_back(instantiate(univ_axiom, i.first, j));
233  }
234  }
235  for(const auto &nc_axiom : axioms.not_contains)
236  {
237  for(const auto &instance :
238  instantiate(nc_axiom, index_set, not_contain_witnesses))
239  lemmas.push_back(instance);
240  }
241  return lemmas;
242 }
243 
248  string_constraint_generatort &generator,
249  exprt &expr)
250 {
251  if(const auto equal_expr = expr_try_dynamic_cast<equal_exprt>(expr))
252  {
253  if(
254  const auto fun_app = expr_try_dynamic_cast<function_application_exprt>(
255  as_const(equal_expr->rhs())))
256  {
257  const auto new_equation =
258  generator.make_array_pointer_association(equal_expr->lhs(), *fun_app);
259  if(new_equation)
260  {
261  expr =
262  equal_exprt{from_integer(true, new_equation->type()), *new_equation};
263  }
264  }
265  }
266 }
267 
272 static exprt
273 replace_expr_copy(const union_find_replacet &symbol_resolve, exprt expr)
274 {
275  symbol_resolve.replace_expr(expr);
276  return expr;
277 }
278 
283 void string_refinementt::set_to(const exprt &expr, bool value)
284 {
285  PRECONDITION(expr.type().id() == ID_bool);
287  if(!value)
288  equations.push_back(not_exprt{expr});
289  else
290  equations.push_back(expr);
291 }
292 
302  union_find_replacet &symbol_solver,
303  const std::vector<exprt> &equations,
304  const namespacet &ns,
305  messaget::mstreamt &stream)
306 {
307  const std::string log_message =
308  "WARNING string_refinement.cpp generate_symbol_resolution_from_equations:";
309  auto equalities = make_range(equations).filter(
310  [&](const exprt &e) { return can_cast_expr<equal_exprt>(e); });
311  for(const exprt &e : equalities)
312  {
313  const equal_exprt &eq = to_equal_expr(e);
314  const exprt &lhs = to_equal_expr(eq).lhs();
315  const exprt &rhs = to_equal_expr(eq).rhs();
316  if(lhs.id() != ID_symbol)
317  {
318  stream << log_message << "non symbol lhs: " << format(lhs)
319  << " with rhs: " << format(rhs) << messaget::eom;
320  continue;
321  }
322 
323  if(lhs.type() != rhs.type())
324  {
325  stream << log_message << "non equal types lhs: " << format(lhs)
326  << "\n####################### rhs: " << format(rhs)
327  << messaget::eom;
328  continue;
329  }
330 
331  if(is_char_pointer_type(rhs.type()))
332  {
333  symbol_solver.make_union(lhs, simplify_expr(rhs, ns));
334  }
335  else if(rhs.id() == ID_function_application)
336  {
337  // function applications can be ignored because they will be replaced
338  // in the convert_function_application step of dec_solve
339  }
340  else if(
341  lhs.type().id() != ID_pointer && has_char_pointer_subtype(lhs.type(), ns))
342  {
343  if(rhs.type().id() == ID_struct || rhs.type().id() == ID_struct_tag)
344  {
345  const struct_typet &struct_type = to_struct_type(ns.follow(rhs.type()));
346  for(const auto &comp : struct_type.components())
347  {
348  if(is_char_pointer_type(comp.type()))
349  {
350  const member_exprt lhs_data(lhs, comp.get_name(), comp.type());
351  const exprt rhs_data = simplify_expr(
352  member_exprt(rhs, comp.get_name(), comp.type()), ns);
353  symbol_solver.make_union(lhs_data, rhs_data);
354  }
355  }
356  }
357  else
358  {
359  stream << log_message << "non struct with char pointer subexpr "
360  << format(rhs) << "\n * of type " << format(rhs.type())
361  << messaget::eom;
362  }
363  }
364  }
365 }
366 
373 static std::vector<exprt>
375 {
376  std::vector<exprt> result;
377  if(lhs.type() == string_typet())
378  result.push_back(lhs);
379  else if(lhs.type().id() == ID_struct || lhs.type().id() == ID_struct_tag)
380  {
381  const struct_typet &struct_type = to_struct_type(ns.follow(lhs.type()));
382  for(const auto &comp : struct_type.components())
383  {
384  const std::vector<exprt> strings_in_comp = extract_strings_from_lhs(
385  member_exprt(lhs, comp.get_name(), comp.type()), ns);
386  result.insert(
387  result.end(), strings_in_comp.begin(), strings_in_comp.end());
388  }
389  }
390  return result;
391 }
392 
398 static std::vector<exprt>
399 extract_strings(const exprt &expr, const namespacet &ns)
400 {
401  std::vector<exprt> result;
402  for(auto it = expr.depth_begin(); it != expr.depth_end();)
403  {
404  if(it->type() == string_typet() && it->id() != ID_if)
405  {
406  result.push_back(*it);
407  it.next_sibling_or_parent();
408  }
409  else if(it->id() == ID_symbol)
410  {
411  for(const exprt &e : extract_strings_from_lhs(*it, ns))
412  result.push_back(e);
413  it.next_sibling_or_parent();
414  }
415  else
416  ++it;
417  }
418  return result;
419 }
420 
428  const equal_exprt &eq,
429  union_find_replacet &symbol_resolve,
430  const namespacet &ns)
431 {
432  if(eq.rhs().type() == string_typet())
433  {
434  symbol_resolve.make_union(eq.lhs(), simplify_expr(eq.rhs(), ns));
435  }
436  else if(has_subtype(eq.lhs().type(), ID_string, ns))
437  {
438  if(
439  eq.rhs().type().id() == ID_struct ||
440  eq.rhs().type().id() == ID_struct_tag)
441  {
442  const struct_typet &struct_type =
443  to_struct_type(ns.follow(eq.rhs().type()));
444  for(const auto &comp : struct_type.components())
445  {
446  const member_exprt lhs_data(eq.lhs(), comp.get_name(), comp.type());
447  const exprt rhs_data = simplify_expr(
448  member_exprt(eq.rhs(), comp.get_name(), comp.type()), ns);
450  equal_exprt(lhs_data, rhs_data), symbol_resolve, ns);
451  }
452  }
453  }
454 }
455 
464  const std::vector<equal_exprt> &equations,
465  const namespacet &ns,
466  messaget::mstreamt &stream)
467 {
468  const std::string log_message =
469  "WARNING string_refinement.cpp "
470  "string_identifiers_resolution_from_equations:";
471 
472  equation_symbol_mappingt equation_map;
473 
474  // Indexes of equations that need to be added to the result
475  std::unordered_set<size_t> required_equations;
476  std::stack<size_t> equations_to_treat;
477 
478  for(std::size_t i = 0; i < equations.size(); ++i)
479  {
480  const equal_exprt &eq = equations[i];
481  if(eq.rhs().id() == ID_function_application)
482  {
483  if(required_equations.insert(i).second)
484  equations_to_treat.push(i);
485 
486  std::vector<exprt> rhs_strings = extract_strings(eq.rhs(), ns);
487  for(const auto &expr : rhs_strings)
488  equation_map.add(i, expr);
489  }
490  else if(
491  eq.lhs().type().id() != ID_pointer &&
492  has_subtype(eq.lhs().type(), ID_string, ns))
493  {
494  std::vector<exprt> lhs_strings = extract_strings_from_lhs(eq.lhs(), ns);
495 
496  for(const auto &expr : lhs_strings)
497  equation_map.add(i, expr);
498 
499  if(lhs_strings.empty())
500  {
501  stream << log_message << "non struct with string subtype "
502  << format(eq.lhs()) << "\n * of type "
503  << format(eq.lhs().type()) << messaget::eom;
504  }
505 
506  for(const exprt &expr : extract_strings(eq.rhs(), ns))
507  equation_map.add(i, expr);
508  }
509  }
510 
511  // transitively add all equations which depend on the equations to treat
512  while(!equations_to_treat.empty())
513  {
514  const std::size_t i = equations_to_treat.top();
515  equations_to_treat.pop();
516  for(const exprt &string : equation_map.find_expressions(i))
517  {
518  for(const std::size_t j : equation_map.find_equations(string))
519  {
520  if(required_equations.insert(j).second)
521  equations_to_treat.push(j);
522  }
523  }
524  }
525 
526  union_find_replacet result;
527  for(const std::size_t i : required_equations)
528  add_string_equation_to_symbol_resolution(equations[i], result, ns);
529 
530  return result;
531 }
532 
533 #ifdef DEBUG
534 static void
536 output_equations(std::ostream &output, const std::vector<exprt> &equations)
537 {
538  for(std::size_t i = 0; i < equations.size(); ++i)
539  output << " [" << i << "] " << format(equations[i]) << std::endl;
540 }
541 #endif
542 
553 // NOLINTNEXTLINE
556 // NOLINTNEXTLINE
561 // NOLINTNEXTLINE
608 {
609 #ifdef DEBUG
610  log.debug() << "dec_solve: Initial set of equations" << messaget::eom;
611  output_equations(log.debug(), equations);
612 #endif
613 
614  log.debug() << "dec_solve: Build symbol solver from equations"
615  << messaget::eom;
616  // symbol_resolve is used by get and is kept between calls to dec_solve,
617  // that's why we use a class member here
620 #ifdef DEBUG
621  log.debug() << "symbol resolve:" << messaget::eom;
622  for(const auto &pair : symbol_resolve.to_vector())
623  log.debug() << format(pair.first) << " --> " << format(pair.second)
624  << messaget::eom;
625 #endif
626 
627  const union_find_replacet string_id_symbol_resolve =
629  [&] {
630  std::vector<equal_exprt> equalities;
631  for(const auto &eq : equations)
632  {
633  if(auto equal_expr = expr_try_dynamic_cast<equal_exprt>(eq))
634  equalities.push_back(*equal_expr);
635  }
636  return equalities;
637  }(),
638  ns,
639  log.debug());
640 #ifdef DEBUG
641  log.debug() << "symbol resolve string:" << messaget::eom;
642  for(const auto &pair : string_id_symbol_resolve.to_vector())
643  {
644  log.debug() << format(pair.first) << " --> " << format(pair.second)
645  << messaget::eom;
646  }
647 #endif
648 
649  log.debug() << "dec_solve: Replacing string ids and simplifying arguments"
650  " in function applications"
651  << messaget::eom;
652  for(exprt &expr : equations)
653  {
654  auto it = expr.depth_begin();
655  while(it != expr.depth_end())
656  {
658  {
659  // Simplification is required because the array pool may not realize
660  // that an expression like
661  // `(unsignedbv[16]*)((signedbv[8]*)&constarray[0] + 0)` is the
662  // same pointer as `&constarray[0]
663  simplify(it.mutate(), ns);
664  string_id_symbol_resolve.replace_expr(it.mutate());
665  it.next_sibling_or_parent();
666  }
667  else
668  ++it;
669  }
670  }
671 
672  // Constraints start clear at each `dec_solve` call.
673  string_constraintst constraints;
674  for(auto &expr : equations)
676 
677 #ifdef DEBUG
678  output_equations(log.debug(), equations);
679 #endif
680 
681  log.debug() << "dec_solve: compute dependency graph and remove function "
682  << "applications captured by the dependencies:" << messaget::eom;
683  std::vector<exprt> local_equations;
684  for(const exprt &eq : equations)
685  {
686  // Ensures that arrays that are equal, are associated to the same nodes
687  // in the graph.
688  const exprt eq_with_char_array_replaced_with_representative_elements =
690  const optionalt<exprt> new_equation = add_node(
691  dependencies,
692  eq_with_char_array_replaced_with_representative_elements,
695  if(new_equation)
696  local_equations.push_back(*new_equation);
697  else
698  local_equations.push_back(eq);
699  }
700  equations.clear();
701 
702 #ifdef DEBUG
704 #endif
705 
706  log.debug() << "dec_solve: add constraints" << messaget::eom;
708 
709 #ifdef DEBUG
710  output_equations(log.debug(), equations);
711 #endif
712 
713 #ifdef DEBUG
714  log.debug() << "dec_solve: arrays_of_pointers:" << messaget::eom;
715  for(auto pair : generator.array_pool.get_arrays_of_pointers())
716  {
717  log.debug() << " * " << format(pair.first) << "\t--> "
718  << format(pair.second) << " : " << format(pair.second.type())
719  << messaget::eom;
720  }
721 #endif
722 
723  for(const auto &eq : local_equations)
724  {
725 #ifdef DEBUG
726  log.debug() << "dec_solve: set_to " << format(eq) << messaget::eom;
727 #endif
728  supert::set_to(eq, true);
729  }
730 
732  constraints.universal.begin(),
733  constraints.universal.end(),
734  std::back_inserter(axioms.universal),
735  [&](string_constraintt constraint) {
736  constraint.replace_expr(symbol_resolve);
737  DATA_INVARIANT(
738  is_valid_string_constraint(log.error(), ns, constraint),
739  string_refinement_invariantt(
740  "string constraints satisfy their invariant"));
741  return constraint;
742  });
743 
745  constraints.not_contains.begin(),
746  constraints.not_contains.end(),
747  std::back_inserter(axioms.not_contains),
749  replace(symbol_resolve, axiom);
750  return axiom;
751  });
752 
753  // Used to store information about witnesses for not_contains constraints
754  std::unordered_map<string_not_contains_constraintt, symbol_exprt>
755  not_contain_witnesses;
756  for(const auto &nc_axiom : axioms.not_contains)
757  {
758  const auto &witness_type = [&] {
759  const auto &rtype = to_array_type(nc_axiom.s0.type());
760  const typet &index_type = rtype.size().type();
762  }();
763  not_contain_witnesses.emplace(
764  nc_axiom, generator.fresh_symbol("not_contains_witness", witness_type));
765  }
766 
767  for(const exprt &lemma : constraints.existential)
768  {
770  }
771 
772  // All generated strings should have non-negative length
773  for(const auto &pair : generator.array_pool.created_strings())
774  {
775  exprt length = generator.array_pool.get_or_create_length(pair.first);
776  add_lemma(
777  binary_relation_exprt{length, ID_ge, from_integer(0, length.type())});
778  }
779 
780  // Initial try without index set
781  const auto get = [this](const exprt &expr) { return this->get(expr); };
783  const decision_proceduret::resultt initial_result = supert::dec_solve();
784  if(initial_result == resultt::D_SATISFIABLE)
785  {
786  bool satisfied;
787  std::vector<exprt> counter_examples;
788  std::tie(satisfied, counter_examples) = check_axioms(
789  axioms,
790  generator,
791  get,
792  log.debug(),
793  ns,
796  not_contain_witnesses);
797  if(satisfied)
798  {
799  log.debug() << "check_SAT: the model is correct" << messaget::eom;
800  return resultt::D_SATISFIABLE;
801  }
802  log.debug() << "check_SAT: got SAT but the model is not correct"
803  << messaget::eom;
804  }
805  else
806  {
807  log.debug() << "check_SAT: got UNSAT or ERROR" << messaget::eom;
808  return initial_result;
809  }
810 
813  current_constraints.clear();
814  const auto initial_instances =
815  generate_instantiations(index_sets, axioms, not_contain_witnesses);
816  for(const auto &instance : initial_instances)
817  {
819  }
820 
821  while((loop_bound_--) > 0)
822  {
824  const decision_proceduret::resultt refined_result = supert::dec_solve();
825 
826  if(refined_result == resultt::D_SATISFIABLE)
827  {
828  bool satisfied;
829  std::vector<exprt> counter_examples;
830  std::tie(satisfied, counter_examples) = check_axioms(
831  axioms,
832  generator,
833  get,
834  log.debug(),
835  ns,
838  not_contain_witnesses);
839  if(satisfied)
840  {
841  log.debug() << "check_SAT: the model is correct" << messaget::eom;
842  return resultt::D_SATISFIABLE;
843  }
844 
845  log.debug()
846  << "check_SAT: got SAT but the model is not correct, refining..."
847  << messaget::eom;
848 
849  // Since the model is not correct although we got SAT, we need to refine
850  // the property we are checking by adding more indices to the index set,
851  // and instantiating universal formulas with this indices.
852  // We will then relaunch the solver with these added lemmas.
853  index_sets.current.clear();
855 
857 
858  if(index_sets.current.empty())
859  {
860  if(axioms.not_contains.empty())
861  {
862  log.error() << "dec_solve: current index set is empty, "
863  << "this should not happen" << messaget::eom;
864  return resultt::D_ERROR;
865  }
866  else
867  {
868  log.debug() << "dec_solve: current index set is empty, "
869  << "adding counter examples" << messaget::eom;
870  for(const auto &counter : counter_examples)
871  add_lemma(counter);
872  }
873  }
874  current_constraints.clear();
875  const auto instances =
876  generate_instantiations(index_sets, axioms, not_contain_witnesses);
877  for(const auto &instance : instances)
878  add_lemma(
880  }
881  else
882  {
883  log.debug() << "check_SAT: default return "
884  << static_cast<int>(refined_result) << messaget::eom;
885  return refined_result;
886  }
887  }
888  log.debug() << "string_refinementt::dec_solve reached the maximum number"
889  << "of steps allowed" << messaget::eom;
890  return resultt::D_ERROR;
891 }
897  const exprt &lemma,
898  const bool simplify_lemma)
899 {
900  if(!seen_instances.insert(lemma).second)
901  return;
902 
903  current_constraints.push_back(lemma);
904 
905  exprt simple_lemma = lemma;
906  if(simplify_lemma)
907  {
908  simplify(simple_lemma, ns);
909  }
910 
911  if(simple_lemma.is_true())
912  {
913 #if 0
914  log.debug() << "string_refinementt::add_lemma : tautology" << messaget::eom;
915 #endif
916  return;
917  }
918 
919  symbol_resolve.replace_expr(simple_lemma);
920 
921  // Replace empty arrays with array_of expression because the solver cannot
922  // handle empty arrays.
923  for(auto it = simple_lemma.depth_begin(); it != simple_lemma.depth_end();)
924  {
925  if(it->id() == ID_array && it->operands().empty())
926  {
927  it.mutate() = array_of_exprt(
928  from_integer(CHARACTER_FOR_UNKNOWN, it->type().subtype()),
929  to_array_type(it->type()));
930  it.next_sibling_or_parent();
931  }
932  else
933  ++it;
934  }
935 
936  log.debug() << "adding lemma " << format(simple_lemma) << messaget::eom;
937 
938  prop.l_set_to_true(convert(simple_lemma));
939 }
940 
956  const std::function<exprt(const exprt &)> &super_get,
957  const namespacet &ns,
958  messaget::mstreamt &stream,
959  const array_string_exprt &arr,
960  const array_poolt &array_pool)
961 {
962  const auto &size_from_pool = array_pool.get_length_if_exists(arr);
963  exprt size_val;
964  if(size_from_pool.has_value())
965  {
966  const exprt size = size_from_pool.value();
967  size_val = simplify_expr(super_get(size), ns);
968  if(size_val.id() != ID_constant)
969  {
970  stream << "(sr::get_valid_array_size) string of unknown size: "
971  << format(size_val) << messaget::eom;
972  return {};
973  }
974  }
975  else if(to_array_type(arr.type()).size().id() == ID_constant)
976  size_val = simplify_expr(to_array_type(arr.type()).size(), ns);
977  else
978  return {};
979 
980  auto n_opt = numeric_cast<std::size_t>(size_val);
981  if(!n_opt)
982  {
983  stream << "(sr::get_valid_array_size) size is not valid" << messaget::eom;
984  return {};
985  }
986 
987  return size_val;
988 }
989 
1000  const std::function<exprt(const exprt &)> &super_get,
1001  const namespacet &ns,
1002  messaget::mstreamt &stream,
1003  const array_string_exprt &arr,
1004  const array_poolt &array_pool)
1005 {
1006  const auto size =
1007  get_valid_array_size(super_get, ns, stream, arr, array_pool);
1008  if(!size.has_value())
1009  {
1010  return {};
1011  }
1012 
1013  const size_t n = numeric_cast<std::size_t>(size.value()).value();
1014 
1015  if(n > MAX_CONCRETE_STRING_SIZE)
1016  {
1017  stream << "(sr::get_valid_array_size) long string (size "
1018  << " = " << n << ") " << format(arr) << messaget::eom;
1019  stream << "(sr::get_valid_array_size) consider reducing "
1020  "max-nondet-string-length so "
1021  "that no string exceeds "
1023  << " in length and "
1024  "make sure all functions returning strings are loaded"
1025  << messaget::eom;
1026  stream << "(sr::get_valid_array_size) this can also happen on invalid "
1027  "object access"
1028  << messaget::eom;
1029  return nil_exprt();
1030  }
1031 
1032  const exprt arr_val = simplify_expr(super_get(arr), ns);
1033  const typet char_type = arr.type().subtype();
1034  const typet &index_type = size.value().type();
1035 
1036  if(
1037  const auto &array = interval_sparse_arrayt::of_expr(
1039  return array->concretize(n, index_type);
1040  return {};
1041 }
1042 
1047 static std::string string_of_array(const array_exprt &arr)
1048 {
1049  if(arr.type().id() != ID_array)
1050  return std::string("");
1051 
1052  exprt size_expr = to_array_type(arr.type()).size();
1053  auto n = numeric_cast_v<std::size_t>(to_constant_expr(size_expr));
1054  return utf16_constant_array_to_java(arr, n);
1055 }
1056 
1066  const std::function<exprt(const exprt &)> &super_get,
1067  const namespacet &ns,
1068  messaget::mstreamt &stream,
1069  const array_string_exprt &arr,
1070  array_poolt &array_pool)
1071 {
1072  stream << "- " << format(arr) << ":\n";
1073  stream << std::string(4, ' ') << "- type: " << format(arr.type())
1074  << messaget::eom;
1075  const auto arr_model_opt = get_array(super_get, ns, stream, arr, array_pool);
1076  if(arr_model_opt)
1077  {
1078  stream << std::string(4, ' ') << "- char_array: " << format(*arr_model_opt)
1079  << '\n';
1080  stream << std::string(4, ' ')
1081  << "- type : " << format(arr_model_opt->type()) << messaget::eom;
1082  const exprt simple = simplify_expr(*arr_model_opt, ns);
1083  stream << std::string(4, ' ')
1084  << "- simplified_char_array: " << format(simple) << messaget::eom;
1085  if(
1086  const auto concretized_array = get_array(
1087  super_get, ns, stream, to_array_string_expr(simple), array_pool))
1088  {
1089  stream << std::string(4, ' ')
1090  << "- concretized_char_array: " << format(*concretized_array)
1091  << messaget::eom;
1092 
1093  if(
1094  const auto array_expr =
1095  expr_try_dynamic_cast<array_exprt>(*concretized_array))
1096  {
1097  stream << std::string(4, ' ') << "- as_string: \""
1098  << string_of_array(*array_expr) << "\"\n";
1099  }
1100  else
1101  stream << std::string(2, ' ') << "- warning: not an array"
1102  << messaget::eom;
1103  return *concretized_array;
1104  }
1105  return simple;
1106  }
1107  stream << std::string(4, ' ') << "- incomplete model" << messaget::eom;
1108  return arr;
1109 }
1110 
1114  const string_constraint_generatort &generator,
1115  messaget::mstreamt &stream,
1116  const namespacet &ns,
1117  const std::function<exprt(const exprt &)> &super_get,
1118  const std::vector<symbol_exprt> &symbols,
1119  array_poolt &array_pool)
1120 {
1121  stream << "debug_model:" << '\n';
1122  for(const auto &pointer_array : generator.array_pool.get_arrays_of_pointers())
1123  {
1124  const auto arr = pointer_array.second;
1125  const exprt model =
1126  get_char_array_and_concretize(super_get, ns, stream, arr, array_pool);
1127 
1128  stream << "- " << format(arr) << ":\n"
1129  << " - pointer: " << format(pointer_array.first) << "\n"
1130  << " - model: " << format(model) << messaget::eom;
1131  }
1132 
1133  for(const auto &symbol : symbols)
1134  {
1135  stream << " - " << symbol.get_identifier() << ": "
1136  << format(super_get(symbol)) << '\n';
1137  }
1138  stream << messaget::eom;
1139 }
1140 
1154  const with_exprt &expr,
1155  const exprt &index,
1156  const bool left_propagate)
1157 {
1158  return left_propagate ? interval_sparse_arrayt(expr).to_if_expression(index)
1159  : sparse_arrayt::to_if_expression(expr, index);
1160 }
1161 
1169  const array_exprt &array_expr,
1170  const exprt &index,
1171  symbol_generatort &symbol_generator)
1172 {
1173  const typet &char_type = array_expr.type().subtype();
1174  const exprt default_val = symbol_generator("out_of_bound_access", char_type);
1175  const interval_sparse_arrayt sparse_array(array_expr, default_val);
1176  return sparse_array.to_if_expression(index);
1177 }
1178 
1180  const if_exprt &if_expr,
1181  const exprt &index,
1182  symbol_generatort &symbol_generator,
1183  const bool left_propagate)
1184 {
1185  exprt true_index = index_exprt(if_expr.true_case(), index);
1186  exprt false_index = index_exprt(if_expr.false_case(), index);
1187 
1188  // Substitute recursively in branches of conditional expressions
1189  optionalt<exprt> substituted_true_case =
1190  substitute_array_access(true_index, symbol_generator, left_propagate);
1191  optionalt<exprt> substituted_false_case =
1192  substitute_array_access(false_index, symbol_generator, left_propagate);
1193 
1194  return if_exprt(
1195  if_expr.cond(),
1196  substituted_true_case ? *substituted_true_case : true_index,
1197  substituted_false_case ? *substituted_false_case : false_index);
1198 }
1199 
1201  const index_exprt &index_expr,
1202  symbol_generatort &symbol_generator,
1203  const bool left_propagate)
1204 {
1205  const exprt &array = index_expr.array();
1206  if(auto array_of = expr_try_dynamic_cast<array_of_exprt>(array))
1207  return array_of->op();
1208  if(auto array_with = expr_try_dynamic_cast<with_exprt>(array))
1209  return substitute_array_access(
1210  *array_with, index_expr.index(), left_propagate);
1211  if(auto array_expr = expr_try_dynamic_cast<array_exprt>(array))
1212  return substitute_array_access(
1213  *array_expr, index_expr.index(), symbol_generator);
1214  if(auto if_expr = expr_try_dynamic_cast<if_exprt>(array))
1215  return substitute_array_access(
1216  *if_expr, index_expr.index(), symbol_generator, left_propagate);
1217 
1218  INVARIANT(
1219  array.is_nil() || array.id() == ID_symbol || array.id() == ID_nondet_symbol,
1220  std::string(
1221  "in case the array is unknown, it should be a symbol or nil, id: ") +
1222  id2string(array.id()));
1223  return {};
1224 }
1225 
1230  exprt &expr,
1231  symbol_generatort &symbol_generator,
1232  const bool left_propagate)
1233 {
1234  // Recurse down structure and modify on the way.
1235  for(auto it = expr.depth_begin(), itend = expr.depth_end(); it != itend; ++it)
1236  {
1237  if(const auto index_expr = expr_try_dynamic_cast<index_exprt>(*it))
1238  {
1239  optionalt<exprt> result =
1240  substitute_array_access(*index_expr, symbol_generator, left_propagate);
1241 
1242  // Only perform a write when we have something changed.
1243  if(result)
1244  it.mutate() = *result;
1245  }
1246  }
1247 }
1248 
1269  exprt expr,
1270  symbol_generatort &symbol_generator,
1271  const bool left_propagate)
1272 {
1273  substitute_array_access_in_place(expr, symbol_generator, left_propagate);
1274  return expr;
1275 }
1276 
1288  const string_not_contains_constraintt &constraint,
1289  const symbol_exprt &univ_var,
1290  const std::function<exprt(const exprt &)> &get)
1291 {
1292  // If the for all is vacuously true, the negation is false.
1293  const auto lbe = numeric_cast_v<mp_integer>(
1294  to_constant_expr(get(constraint.exists_lower_bound)));
1295  const auto ube = numeric_cast_v<mp_integer>(
1296  to_constant_expr(get(constraint.exists_upper_bound)));
1297  const auto univ_bounds = and_exprt(
1298  binary_relation_exprt(get(constraint.univ_lower_bound), ID_le, univ_var),
1299  binary_relation_exprt(get(constraint.univ_upper_bound), ID_gt, univ_var));
1300 
1301  // The negated existential becomes an universal, and this is the unrolling of
1302  // that universal quantifier.
1303  // Ff the upper bound is smaller than the lower bound (specifically, it might
1304  // actually be negative as it is initially unconstrained) then there is
1305  // nothing to do (and the reserve call would fail).
1306  if(ube < lbe)
1307  return and_exprt(univ_bounds, get(constraint.premise));
1308 
1309  std::vector<exprt> conjuncts;
1310  conjuncts.reserve(numeric_cast_v<std::size_t>(ube - lbe));
1311  for(mp_integer i = lbe; i < ube; ++i)
1312  {
1313  const constant_exprt i_expr = from_integer(i, univ_var.type());
1314  const exprt s0_char =
1315  get(index_exprt(constraint.s0, plus_exprt(univ_var, i_expr)));
1316  const exprt s1_char = get(index_exprt(constraint.s1, i_expr));
1317  conjuncts.push_back(equal_exprt(s0_char, s1_char));
1318  }
1319  const exprt equal_strings = conjunction(conjuncts);
1320  return and_exprt(univ_bounds, get(constraint.premise), equal_strings);
1321 }
1322 
1327 template <typename T>
1329  messaget::mstreamt &stream,
1330  const T &axiom,
1331  const T &axiom_in_model,
1332  const exprt &negaxiom,
1333  const exprt &with_concretized_arrays)
1334 {
1335  stream << std::string(4, ' ') << "- axiom:\n" << std::string(6, ' ');
1336  stream << to_string(axiom);
1337  stream << '\n'
1338  << std::string(4, ' ') << "- axiom_in_model:\n"
1339  << std::string(6, ' ');
1340  stream << to_string(axiom_in_model) << '\n'
1341  << std::string(4, ' ') << "- negated_axiom:\n"
1342  << std::string(6, ' ') << format(negaxiom) << '\n';
1343  stream << std::string(4, ' ') << "- negated_axiom_with_concretized_arrays:\n"
1344  << std::string(6, ' ') << format(with_concretized_arrays) << '\n';
1345 }
1346 
1348 static std::pair<bool, std::vector<exprt>> check_axioms(
1349  const string_axiomst &axioms,
1350  string_constraint_generatort &generator,
1351  const std::function<exprt(const exprt &)> &get,
1352  messaget::mstreamt &stream,
1353  const namespacet &ns,
1354  bool use_counter_example,
1355  const union_find_replacet &symbol_resolve,
1356  const std::unordered_map<string_not_contains_constraintt, symbol_exprt>
1357  &not_contain_witnesses)
1358 {
1359  stream << "string_refinementt::check_axioms:" << messaget::eom;
1360 
1361  stream << "symbol_resolve:" << messaget::eom;
1362  auto pairs = symbol_resolve.to_vector();
1363  for(const auto &pair : pairs)
1364  stream << " - " << format(pair.first) << " --> " << format(pair.second)
1365  << messaget::eom;
1366 
1367 #ifdef DEBUG
1368  debug_model(
1369  generator,
1370  stream,
1371  ns,
1372  get,
1373  generator.fresh_symbol.created_symbols,
1374  generator.array_pool);
1375 #endif
1376 
1377  // Maps from indexes of violated universal axiom to a witness of violation
1378  std::map<size_t, exprt> violated;
1379 
1380  stream << "string_refinement::check_axioms: " << axioms.universal.size()
1381  << " universal axioms:" << messaget::eom;
1382  for(size_t i = 0; i < axioms.universal.size(); i++)
1383  {
1384  const string_constraintt &axiom = axioms.universal[i];
1385  const string_constraintt axiom_in_model(
1386  axiom.univ_var,
1387  get(axiom.lower_bound),
1388  get(axiom.upper_bound),
1389  get(axiom.body));
1390 
1391  exprt negaxiom = axiom_in_model.negation();
1392  negaxiom = simplify_expr(negaxiom, ns);
1393 
1394  stream << std::string(2, ' ') << i << ".\n";
1395  const exprt with_concretized_arrays =
1396  substitute_array_access(negaxiom, generator.fresh_symbol, true);
1398  stream, axiom, axiom_in_model, negaxiom, with_concretized_arrays);
1399 
1400  if(
1401  const auto &witness = find_counter_example(
1402  ns,
1403  with_concretized_arrays,
1404  axiom.univ_var,
1405  stream.message.get_message_handler()))
1406  {
1407  stream << std::string(4, ' ')
1408  << "- violated_for: " << format(axiom.univ_var) << "="
1409  << format(*witness) << messaget::eom;
1410  violated[i] = *witness;
1411  }
1412  else
1413  stream << std::string(4, ' ') << "- correct" << messaget::eom;
1414  }
1415 
1416  // Maps from indexes of violated not_contains axiom to a witness of violation
1417  std::map<std::size_t, exprt> violated_not_contains;
1418 
1419  stream << "there are " << axioms.not_contains.size() << " not_contains axioms"
1420  << messaget::eom;
1421  for(std::size_t i = 0; i < axioms.not_contains.size(); i++)
1422  {
1423  const string_not_contains_constraintt &nc_axiom = axioms.not_contains[i];
1424  const symbol_exprt univ_var = generator.fresh_symbol(
1425  "not_contains_univ_var", nc_axiom.s0.length_type());
1426  const exprt negated_axiom = negation_of_not_contains_constraint(
1427  nc_axiom, univ_var, [&](const exprt &expr) {
1428  return simplify_expr(get(expr), ns);
1429  });
1430 
1431  stream << std::string(2, ' ') << i << ".\n";
1433  stream, nc_axiom, nc_axiom, negated_axiom, negated_axiom);
1434 
1435  if(
1436  const auto witness = find_counter_example(
1437  ns, negated_axiom, univ_var, stream.message.get_message_handler()))
1438  {
1439  stream << std::string(4, ' ')
1440  << "- violated_for: " << univ_var.get_identifier() << "="
1441  << format(*witness) << messaget::eom;
1442  violated_not_contains[i] = *witness;
1443  }
1444  }
1445 
1446  if(violated.empty() && violated_not_contains.empty())
1447  {
1448  stream << "no violated property" << messaget::eom;
1449  return {true, std::vector<exprt>()};
1450  }
1451  else
1452  {
1453  stream << violated.size() << " universal string axioms can be violated"
1454  << messaget::eom;
1455  stream << violated_not_contains.size()
1456  << " not_contains string axioms can be violated" << messaget::eom;
1457 
1458  if(use_counter_example)
1459  {
1460  std::vector<exprt> lemmas;
1461 
1462  for(const auto &v : violated)
1463  {
1464  const exprt &val = v.second;
1465  const string_constraintt &axiom = axioms.universal[v.first];
1466 
1467  exprt instance(axiom.body);
1468  replace_expr(axiom.univ_var, val, instance);
1469  // We are not sure the index set contains only positive numbers
1470  and_exprt bounds(
1471  axiom.univ_within_bounds(),
1472  binary_relation_exprt(from_integer(0, val.type()), ID_le, val));
1473  replace_expr(axiom.univ_var, val, bounds);
1474  const implies_exprt counter(bounds, instance);
1475  lemmas.push_back(counter);
1476  }
1477 
1478  for(const auto &v : violated_not_contains)
1479  {
1480  const exprt &val = v.second;
1481  const string_not_contains_constraintt &axiom =
1482  axioms.not_contains[v.first];
1483 
1484  const exprt func_val =
1485  index_exprt(not_contain_witnesses.at(axiom), val);
1486  const exprt comp_val = simplify_sum(plus_exprt(val, func_val));
1487 
1488  std::set<std::pair<exprt, exprt>> indices;
1489  indices.insert(std::pair<exprt, exprt>(comp_val, func_val));
1490  const exprt counter =
1491  ::instantiate_not_contains(axiom, indices, not_contain_witnesses)[0];
1492  lemmas.push_back(counter);
1493  }
1494  return {false, lemmas};
1495  }
1496  }
1497  return {false, std::vector<exprt>()};
1498 }
1499 
1503 {
1504  return linear_functiont{f}.to_expr();
1505 }
1506 
1512 static void initial_index_set(
1513  index_set_pairt &index_set,
1514  const namespacet &ns,
1515  const string_axiomst &axioms)
1516 {
1517  for(const auto &axiom : axioms.universal)
1518  initial_index_set(index_set, ns, axiom);
1519  for(const auto &axiom : axioms.not_contains)
1520  initial_index_set(index_set, ns, axiom);
1521 }
1522 
1527 static void update_index_set(
1528  index_set_pairt &index_set,
1529  const namespacet &ns,
1530  const std::vector<exprt> &current_constraints)
1531 {
1532  for(const auto &axiom : current_constraints)
1533  update_index_set(index_set, ns, axiom);
1534 }
1535 
1542 static void get_sub_arrays(const exprt &array_expr, std::vector<exprt> &accu)
1543 {
1544  if(array_expr.id() == ID_if)
1545  {
1546  get_sub_arrays(to_if_expr(array_expr).true_case(), accu);
1547  get_sub_arrays(to_if_expr(array_expr).false_case(), accu);
1548  }
1549  else
1550  {
1551  if(array_expr.type().id() == ID_array)
1552  {
1553  // TODO: check_that it does not contain any sub_array
1554  accu.push_back(array_expr);
1555  }
1556  else
1557  {
1558  for(const auto &operand : array_expr.operands())
1559  get_sub_arrays(operand, accu);
1560  }
1561  }
1562 }
1563 
1569 static void add_to_index_set(
1570  index_set_pairt &index_set,
1571  const namespacet &ns,
1572  const exprt &s,
1573  exprt i)
1574 {
1575  simplify(i, ns);
1576  const bool is_size_t = numeric_cast<std::size_t>(i).has_value();
1577  if(i.id() != ID_constant || is_size_t)
1578  {
1579  std::vector<exprt> sub_arrays;
1580  get_sub_arrays(s, sub_arrays);
1581  for(const auto &sub : sub_arrays)
1582  if(index_set.cumulative[sub].insert(i).second)
1583  index_set.current[sub].insert(i);
1584  }
1585 }
1586 
1602 static void initial_index_set(
1603  index_set_pairt &index_set,
1604  const namespacet &ns,
1605  const exprt &qvar,
1606  const exprt &upper_bound,
1607  const exprt &s,
1608  const exprt &i)
1609 {
1610  PRECONDITION(
1611  s.id() == ID_symbol || s.id() == ID_nondet_symbol || s.id() == ID_array ||
1612  s.id() == ID_if);
1613  if(s.id() == ID_array)
1614  {
1615  for(std::size_t j = 0; j < s.operands().size(); ++j)
1616  add_to_index_set(index_set, ns, s, from_integer(j, i.type()));
1617  return;
1618  }
1619  if(auto ite = expr_try_dynamic_cast<if_exprt>(s))
1620  {
1621  initial_index_set(index_set, ns, qvar, upper_bound, ite->true_case(), i);
1622  initial_index_set(index_set, ns, qvar, upper_bound, ite->false_case(), i);
1623  return;
1624  }
1625  const minus_exprt u_minus_1(upper_bound, from_integer(1, upper_bound.type()));
1626  exprt i_copy = i;
1627  replace_expr(qvar, u_minus_1, i_copy);
1628  add_to_index_set(index_set, ns, s, i_copy);
1629 }
1630 
1631 static void initial_index_set(
1632  index_set_pairt &index_set,
1633  const namespacet &ns,
1634  const string_constraintt &axiom)
1635 {
1636  const symbol_exprt &qvar = axiom.univ_var;
1637  const auto &bound = axiom.upper_bound;
1638  auto it = axiom.body.depth_begin();
1639  const auto end = axiom.body.depth_end();
1640  while(it != end)
1641  {
1642  if(it->id() == ID_index && is_char_type(it->type()))
1643  {
1644  const auto &index_expr = to_index_expr(*it);
1645  const auto &s = index_expr.array();
1646  initial_index_set(index_set, ns, qvar, bound, s, index_expr.index());
1647  it.next_sibling_or_parent();
1648  }
1649  else
1650  ++it;
1651  }
1652 }
1653 
1654 static void initial_index_set(
1655  index_set_pairt &index_set,
1656  const namespacet &ns,
1657  const string_not_contains_constraintt &axiom)
1658 {
1659  auto it = axiom.premise.depth_begin();
1660  const auto end = axiom.premise.depth_end();
1661  while(it != end)
1662  {
1663  if(it->id() == ID_index && is_char_type(it->type()))
1664  {
1665  const exprt &s = to_index_expr(*it).array();
1666  const exprt &i = to_index_expr(*it).index();
1667 
1668  // cur is of the form s[i] and no quantified variable appears in i
1669  add_to_index_set(index_set, ns, s, i);
1670 
1671  it.next_sibling_or_parent();
1672  }
1673  else
1674  ++it;
1675  }
1676 
1677  const minus_exprt kminus1(
1679  add_to_index_set(index_set, ns, axiom.s1.content(), kminus1);
1680 }
1681 
1686 static void update_index_set(
1687  index_set_pairt &index_set,
1688  const namespacet &ns,
1689  const exprt &formula)
1690 {
1691  std::list<exprt> to_process;
1692  to_process.push_back(formula);
1693 
1694  while(!to_process.empty())
1695  {
1696  exprt cur = to_process.back();
1697  to_process.pop_back();
1698  if(cur.id() == ID_index && is_char_type(cur.type()))
1699  {
1700  const exprt &s = to_index_expr(cur).array();
1701  const exprt &i = to_index_expr(cur).index();
1703  s.type().id() == ID_array,
1704  string_refinement_invariantt("index expressions must index on arrays"));
1705  exprt simplified = simplify_sum(i);
1706  if(s.id() != ID_array) // do not update index set of constant arrays
1707  add_to_index_set(index_set, ns, s, simplified);
1708  }
1709  else
1710  {
1711  forall_operands(it, cur)
1712  to_process.push_back(*it);
1713  }
1714  }
1715 }
1716 
1735 static std::vector<exprt> instantiate(
1736  const string_not_contains_constraintt &axiom,
1737  const index_set_pairt &index_set,
1738  const std::unordered_map<string_not_contains_constraintt, symbol_exprt>
1739  &witnesses)
1740 {
1741  const array_string_exprt &s0 = axiom.s0;
1742  const array_string_exprt &s1 = axiom.s1;
1743 
1744  const auto &index_set0 = index_set.cumulative.find(s0.content());
1745  const auto &index_set1 = index_set.cumulative.find(s1.content());
1746  const auto &current_index_set0 = index_set.current.find(s0.content());
1747  const auto &current_index_set1 = index_set.current.find(s1.content());
1748 
1749  if(
1750  index_set0 != index_set.cumulative.end() &&
1751  index_set1 != index_set.cumulative.end() &&
1752  current_index_set0 != index_set.current.end() &&
1753  current_index_set1 != index_set.current.end())
1754  {
1755  typedef std::pair<exprt, exprt> expr_pairt;
1756  std::set<expr_pairt> index_pairs;
1757 
1758  for(const auto &ic0 : current_index_set0->second)
1759  for(const auto &i1 : index_set1->second)
1760  index_pairs.insert(expr_pairt(ic0, i1));
1761  for(const auto &ic1 : current_index_set1->second)
1762  for(const auto &i0 : index_set0->second)
1763  index_pairs.insert(expr_pairt(i0, ic1));
1764 
1765  return ::instantiate_not_contains(axiom, index_pairs, witnesses);
1766  }
1767  return {};
1768 }
1769 
1777 exprt substitute_array_lists(exprt expr, size_t string_max_length)
1778 {
1779  for(auto &operand : expr.operands())
1780  operand = substitute_array_lists(operand, string_max_length);
1781 
1782  if(expr.id() == ID_array_list)
1783  {
1785  expr.operands().size() >= 2,
1786  string_refinement_invariantt("array-lists must have at least two "
1787  "operands"));
1788  const typet &char_type = expr.operands()[1].type();
1790  exprt ret_expr = array_of_exprt(from_integer(0, char_type), arr_type);
1791 
1792  for(size_t i = 0; i < expr.operands().size(); i += 2)
1793  {
1794  const exprt &index = expr.operands()[i];
1795  const exprt &value = expr.operands()[i + 1];
1796  const auto index_value = numeric_cast<std::size_t>(index);
1797  if(
1798  !index.is_constant() ||
1799  (index_value && *index_value < string_max_length))
1800  ret_expr = with_exprt(ret_expr, index, value);
1801  }
1802  return ret_expr;
1803  }
1804 
1805  return expr;
1806 }
1807 
1816 {
1817  const auto super_get = [this](const exprt &expr) {
1818  return supert::get(expr);
1819  };
1820  exprt ecopy(expr);
1821  (void)symbol_resolve.replace_expr(ecopy);
1822 
1823  // Special treatment for index expressions
1824  const auto &index_expr = expr_try_dynamic_cast<index_exprt>(ecopy);
1825  if(index_expr && is_char_type(index_expr->type()))
1826  {
1827  std::reference_wrapper<const exprt> current(index_expr->array());
1828  while(current.get().id() == ID_if)
1829  {
1830  const auto &if_expr = expr_dynamic_cast<if_exprt>(current.get());
1831  const exprt cond = get(if_expr.cond());
1832  if(cond.is_true())
1833  current = std::cref(if_expr.true_case());
1834  else if(cond.is_false())
1835  current = std::cref(if_expr.false_case());
1836  else
1837  UNREACHABLE;
1838  }
1839  const auto array = supert::get(current.get());
1840  const auto index = get(index_expr->index());
1841 
1842  // If the underlying solver does not know about the existence of an array,
1843  // it can return nil, which cannot be used in the expression returned here.
1844  if(array.is_nil())
1845  return index_exprt(current, index);
1846 
1847  const exprt unknown =
1848  from_integer(CHARACTER_FOR_UNKNOWN, index_expr->type());
1849  if(
1850  const auto sparse_array = interval_sparse_arrayt::of_expr(array, unknown))
1851  {
1852  if(const auto index_value = numeric_cast<std::size_t>(index))
1853  return sparse_array->at(*index_value);
1854  return sparse_array->to_if_expression(index);
1855  }
1856 
1857  INVARIANT(array.id() == ID_symbol || array.id() == ID_nondet_symbol,
1858  "Apart from symbols, array valuations can be interpreted as "
1859  "sparse arrays. Array model : " + array.pretty());
1860  return index_exprt(array, index);
1861  }
1862 
1863  if(is_char_array_type(ecopy.type(), ns))
1864  {
1866 
1867  if(
1868  const auto from_dependencies =
1869  dependencies.eval(arr, [&](const exprt &expr) { return get(expr); }))
1870  return *from_dependencies;
1871 
1872  if(
1873  const auto arr_model_opt =
1874  get_array(super_get, ns, log.debug(), arr, generator.array_pool))
1875  return *arr_model_opt;
1876 
1877  if(
1878  const auto &length_from_pool =
1880  {
1881  const exprt length = super_get(length_from_pool.value());
1882 
1883  if(const auto n = numeric_cast<std::size_t>(length))
1884  {
1885  const interval_sparse_arrayt sparse_array(
1887  return sparse_array.concretize(*n, length.type());
1888  }
1889  }
1890  return arr;
1891  }
1892  return supert::get(ecopy);
1893 }
1894 
1905  const namespacet &ns,
1906  const exprt &axiom,
1907  const symbol_exprt &var,
1908  message_handlert &message_handler)
1909 {
1910  satcheck_no_simplifiert sat_check(message_handler);
1911  boolbvt solver(ns, sat_check, message_handler);
1912  solver << axiom;
1913 
1915  return solver.get(var);
1916  else
1917  return {};
1918 }
1919 
1921 typedef std::map<exprt, std::vector<exprt>> array_index_mapt;
1922 
1925 {
1926  array_index_mapt indices;
1927  // clang-format off
1928  std::for_each(
1929  expr.depth_begin(),
1930  expr.depth_end(),
1931  [&](const exprt &expr)
1932  {
1933  const auto index_expr = expr_try_dynamic_cast<const index_exprt>(expr);
1934  if(index_expr)
1935  indices[index_expr->array()].push_back(index_expr->index());
1936  });
1937  // clang-format on
1938  return indices;
1939 }
1940 
1946 static bool
1948 {
1949  for(auto it = expr.depth_begin(); it != expr.depth_end();)
1950  {
1951  if(
1952  it->id() != ID_plus && it->id() != ID_minus &&
1953  it->id() != ID_unary_minus && *it != var)
1954  {
1955  if(std::find(it->depth_begin(), it->depth_end(), var) != it->depth_end())
1956  return false;
1957  else
1958  it.next_sibling_or_parent();
1959  }
1960  else
1961  ++it;
1962  }
1963  return true;
1964 }
1965 
1974 {
1975  for(auto it = constr.body.depth_begin(); it != constr.body.depth_end();)
1976  {
1977  if(*it == constr.univ_var)
1978  return false;
1979  if(it->id() == ID_index)
1980  it.next_sibling_or_parent();
1981  else
1982  ++it;
1983  }
1984  return true;
1985 }
1986 
1994  messaget::mstreamt &stream,
1995  const namespacet &ns,
1996  const string_constraintt &constraint)
1997 {
1998  const array_index_mapt body_indices = gather_indices(constraint.body);
1999  // Must validate for each string. Note that we have an invariant that the
2000  // second value in the pair is non-empty.
2001  for(const auto &pair : body_indices)
2002  {
2003  // Condition 1: All indices of the same string must be the of the same form
2004  const exprt rep = pair.second.back();
2005  for(size_t j = 0; j < pair.second.size() - 1; j++)
2006  {
2007  const exprt i = pair.second[j];
2008  const equal_exprt equals(rep, i);
2009  const exprt result = simplify_expr(equals, ns);
2010  if(result.is_false())
2011  {
2012  stream << "Indices not equal: " << to_string(constraint)
2013  << ", str: " << format(pair.first) << messaget::eom;
2014  return false;
2015  }
2016  }
2017 
2018  // Condition 2: f must be linear in the quantified variable
2019  if(!is_linear_arithmetic_expr(rep, constraint.univ_var))
2020  {
2021  stream << "f is not linear: " << to_string(constraint)
2022  << ", str: " << format(pair.first) << messaget::eom;
2023  return false;
2024  }
2025 
2026  // Condition 3: the quantified variable can only occur in indices in the
2027  // body
2028  if(!universal_only_in_index(constraint))
2029  {
2030  stream << "Universal variable outside of index:" << to_string(constraint)
2031  << messaget::eom;
2032  return false;
2033  }
2034  }
2035 
2036  return true;
2037 }
string_refinementt::loop_bound_
std::size_t loop_bound_
Definition: string_refinement.h:97
with_exprt
Operator to update elements in structs and arrays.
Definition: std_expr.h:2258
string_axiomst::universal
std::vector< string_constraintt > universal
Definition: string_refinement_util.h:67
UNREACHABLE
#define UNREACHABLE
This should be used to mark dead code.
Definition: invariant.h:503
struct_union_typet::components
const componentst & components() const
Definition: std_types.h:147
array_poolt::created_strings
const std::unordered_map< array_string_exprt, exprt, irep_hash > & created_strings() const
Return a map mapping all array_string_exprt of the array_pool to their length.
Definition: array_pool.cpp:179
format
static format_containert< T > format(const T &o)
Definition: format.h:37
prop_conv_solvert::equality_propagation
bool equality_propagation
Definition: prop_conv_solver.h:71
extract_strings
static std::vector< exprt > extract_strings(const exprt &expr, const namespacet &ns)
Definition: string_refinement.cpp:399
check_axioms
static std::pair< bool, std::vector< exprt > > check_axioms(const string_axiomst &axioms, string_constraint_generatort &generator, const std::function< exprt(const exprt &)> &get, messaget::mstreamt &stream, const namespacet &ns, bool use_counter_example, const union_find_replacet &symbol_resolve, const std::unordered_map< string_not_contains_constraintt, symbol_exprt > &not_contain_witnesses)
Check axioms takes the model given by the underlying solver and answers whether it satisfies the stri...
Definition: string_refinement.cpp:1348
string_refinementt::generator
string_constraint_generatort generator
Definition: string_refinement.h:98
array_string_exprt::length_type
const typet & length_type() const
Definition: string_expr.h:69
typet::subtype
const typet & subtype() const
Definition: type.h:47
array_poolt::get_arrays_of_pointers
const std::unordered_map< exprt, array_string_exprt, irep_hash > & get_arrays_of_pointers() const
Definition: array_pool.h:50
mp_integer
BigInt mp_integer
Definition: smt_terms.h:12
conjunction
exprt conjunction(const exprt::operandst &op)
1) generates a conjunction for two or more operands 2) for one operand, returns the operand 3) return...
Definition: std_expr.cpp:34
binary_exprt::rhs
exprt & rhs()
Definition: std_expr.h:590
union_find_replacet::to_vector
std::vector< std::pair< exprt, exprt > > to_vector() const
Definition: union_find_replace.cpp:46
fill_in_map_as_vector
static std::vector< T > fill_in_map_as_vector(const std::map< std::size_t, T > &index_value)
Convert index-value map to a vector of values.
Definition: string_refinement.cpp:130
generate_instantiations
static std::vector< exprt > generate_instantiations(const index_set_pairt &index_set, const string_axiomst &axioms, const std::unordered_map< string_not_contains_constraintt, symbol_exprt > &not_contain_witnesses)
Instantiation of all constraints.
Definition: string_refinement.cpp:220
to_struct_type
const struct_typet & to_struct_type(const typet &type)
Cast a typet to a struct_typet.
Definition: std_types.h:308
arrayst::log
messaget log
Definition: arrays.h:55
exprt::depth_begin
depth_iteratort depth_begin()
Definition: expr.cpp:265
instantiate_not_contains
std::vector< exprt > instantiate_not_contains(const string_not_contains_constraintt &axiom, const std::set< std::pair< exprt, exprt >> &index_pairs, const std::unordered_map< string_not_contains_constraintt, symbol_exprt > &witnesses)
get_valid_array_size
static optionalt< exprt > get_valid_array_size(const std::function< exprt(const exprt &)> &super_get, const namespacet &ns, messaget::mstreamt &stream, const array_string_exprt &arr, const array_poolt &array_pool)
Get a model of the size of the input string.
Definition: string_refinement.cpp:955
string_not_contains_constraintt::univ_upper_bound
exprt univ_upper_bound
Definition: string_constraint.h:123
typet
The type of an expression, extends irept.
Definition: type.h:28
negation_of_not_contains_constraint
static exprt negation_of_not_contains_constraint(const string_not_contains_constraintt &constraint, const symbol_exprt &univ_var, const std::function< exprt(const exprt &)> &get)
Negates the constraint to be fed to a solver.
Definition: string_refinement.cpp:1287
debug_check_axioms_step
static void debug_check_axioms_step(messaget::mstreamt &stream, const T &axiom, const T &axiom_in_model, const exprt &negaxiom, const exprt &with_concretized_arrays)
Debugging function which outputs the different steps an axiom goes through to be checked in check axi...
Definition: string_refinement.cpp:1328
equation_symbol_mapping.h
transform
static abstract_object_pointert transform(const exprt &expr, const std::vector< abstract_object_pointert > &operands, const abstract_environmentt &environment, const namespacet &ns)
Definition: abstract_value_object.cpp:159
display_index_set
static void display_index_set(messaget::mstreamt &stream, const index_set_pairt &index_set)
Write index set to the given stream, use for debugging.
Definition: string_refinement.cpp:172
to_index_expr
const index_exprt & to_index_expr(const exprt &expr)
Cast an exprt to an index_exprt.
Definition: std_expr.h:1382
to_if_expr
const if_exprt & to_if_expr(const exprt &expr)
Cast an exprt to an if_exprt.
Definition: std_expr.h:2237
array_poolt
Correspondance between arrays and pointers string representations.
Definition: array_pool.h:42
boolbvt::set_to
void set_to(const exprt &expr, bool value) override
For a Boolean expression expr, add the constraint 'expr' if value is true, otherwise add 'not expr'.
Definition: boolbv.cpp:514
string_refinementt::current_constraints
std::vector< exprt > current_constraints
Definition: string_refinement.h:106
string_constraintt
Definition: string_constraint.h:54
if_exprt
The trinary if-then-else operator.
Definition: std_expr.h:2172
is_char_type
bool is_char_type(const typet &type)
For now, any unsigned bitvector type of width smaller or equal to 16 is considered a character.
Definition: string_refinement_util.cpp:22
index_set_pairt
Definition: string_refinement_util.h:60
interval_sparse_arrayt
Represents arrays by the indexes up to which the value remains the same.
Definition: string_refinement_util.h:99
string_constraint_generatort::fresh_symbol
symbol_generatort fresh_symbol
Definition: string_constraint_generator.h:59
interval_sparse_arrayt::to_if_expression
exprt to_if_expression(const exprt &index) const
Definition: string_refinement_util.cpp:103
union_find_replacet::replace_expr
bool replace_expr(exprt &expr) const
Replace subexpressions of expr by the representative element of the set they belong to.
Definition: union_find_replace.cpp:28
and_exprt
Boolean AND.
Definition: std_expr.h:1920
s1
int8_t s1
Definition: bytecode_info.h:59
array_poolt::get_or_create_length
exprt get_or_create_length(const array_string_exprt &s)
Get the length of an array_string_exprt from the array_pool.
Definition: array_pool.cpp:26
string_refinement_invariant.h
MAX_CONCRETE_STRING_SIZE
const std::size_t MAX_CONCRETE_STRING_SIZE
Definition: magic.h:14
plus_exprt
The plus expression Associativity is not specified.
Definition: std_expr.h:914
exprt
Base class for all expressions.
Definition: expr.h:54
binary_exprt::lhs
exprt & lhs()
Definition: std_expr.h:580
propt::l_set_to_true
void l_set_to_true(literalt a)
Definition: prop.h:50
string_constraintst
Collection of constraints of different types: existential formulas, universal formulas,...
Definition: string_constraint_generator.h:39
array_string_exprt::content
exprt & content()
Definition: string_expr.h:74
interval_sparse_arrayt::of_expr
static optionalt< interval_sparse_arrayt > of_expr(const exprt &expr, const exprt &extra_value)
If the expression is an array_exprt or a with_exprt uses the appropriate constructor,...
Definition: string_refinement_util.cpp:156
get_sub_arrays
static void get_sub_arrays(const exprt &array_expr, std::vector< exprt > &accu)
An expression representing an array of characters can be in the form of an if expression for instance...
Definition: string_refinement.cpp:1542
to_string
std::string to_string(const string_not_contains_constraintt &expr)
Used for debug printing.
Definition: string_constraint.cpp:57
messaget::eom
static eomt eom
Definition: message.h:297
linear_functiont
Canonical representation of linear function, for instance, expression $x + x - y + 5 - 3$ would given...
Definition: string_constraint_instantiation.h:52
exprt::is_true
bool is_true() const
Return whether the expression is a constant representing true.
Definition: expr.cpp:33
string_refinementt::index_sets
index_set_pairt index_sets
Definition: string_refinement.h:111
string_not_contains_constraintt::exists_upper_bound
exprt exists_upper_bound
Definition: string_constraint.h:126
symbol_exprt
Expression to hold a symbol (variable)
Definition: std_expr.h:80
make_char_array_pointer_associations
static void make_char_array_pointer_associations(string_constraint_generatort &generator, exprt &expr)
If expr is an equation whose right-hand-side is a associate_array_to_pointer call,...
Definition: string_refinement.cpp:247
string_axiomst
Definition: string_refinement_util.h:66
index_type
bitvector_typet index_type()
Definition: c_types.cpp:16
string_dependencies.h
Keeps track of dependencies between strings.
equal_exprt
Equality.
Definition: std_expr.h:1225
magic.h
Magic numbers used throughout the codebase.
utf16_constant_array_to_java
std::string utf16_constant_array_to_java(const array_exprt &arr, std::size_t length)
Construct a string from a constant array.
Definition: string_refinement_util.cpp:52
string_constraintt::univ_var
symbol_exprt univ_var
Definition: string_constraint.h:58
string_constraint_generatort::array_pool
array_poolt array_pool
Definition: string_constraint_generator.h:61
string_not_contains_constraintt::premise
exprt premise
Definition: string_constraint.h:124
can_cast_expr< equal_exprt >
bool can_cast_expr< equal_exprt >(const exprt &base)
Definition: std_expr.h:1249
infinity_exprt
An expression denoting infinity.
Definition: std_expr.h:2836
if_exprt::false_case
exprt & false_case()
Definition: std_expr.h:2209
sparse_arrayt::to_if_expression
static exprt to_if_expression(const with_exprt &expr, const exprt &index)
Creates an if_expr corresponding to the result of accessing the array at the given index.
Definition: string_refinement_util.cpp:82
string_constraint_generatort
Definition: string_constraint_generator.h:48
exprt::is_false
bool is_false() const
Return whether the expression is a constant representing false.
Definition: expr.cpp:42
initial_index_set
static void initial_index_set(index_set_pairt &index_set, const namespacet &ns, const string_constraintt &axiom)
Definition: string_refinement.cpp:1631
merge
void merge(string_constraintst &result, string_constraintst other)
Merge two sets of constraints by appending to the first one.
Definition: string_constraint_generator_main.cpp:99
string_constraintt::upper_bound
exprt upper_bound
Definition: string_constraint.h:60
string_constraintt::array_index_mapt
std::map< exprt, std::vector< exprt > > array_index_mapt
Definition: string_refinement.cpp:1921
instantiate
static std::vector< exprt > instantiate(const string_not_contains_constraintt &axiom, const index_set_pairt &index_set, const std::unordered_map< string_not_contains_constraintt, symbol_exprt > &witnesses)
Instantiates a quantified formula representing not_contains by substituting the quantifiers and gener...
Definition: string_refinement.cpp:1735
string_constraintt::body
exprt body
Definition: string_constraint.h:61
decision_proceduret::resultt::D_SATISFIABLE
@ D_SATISFIABLE
string_constraintt::lower_bound
exprt lower_bound
Definition: string_constraint.h:59
array_typet::size
const exprt & size() const
Definition: std_types.h:771
namespacet
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:91
exprt::type
typet & type()
Return the type of the expression.
Definition: expr.h:82
array_exprt::type
const array_typet & type() const
Definition: std_expr.h:1474
symbol_generatort
Generation of fresh symbols of a given type.
Definition: array_pool.h:22
validate
static bool validate(const string_refinementt::infot &info)
Definition: string_refinement.cpp:150
is_char_pointer_type
bool is_char_pointer_type(const typet &type)
For now, any unsigned bitvector type is considered a character.
Definition: string_refinement_util.cpp:35
bv_refinementt::dec_solve
decision_proceduret::resultt dec_solve() override
Run the decision procedure to solve the problem.
Definition: bv_refinement_loop.cpp:24
add_node
optionalt< exprt > add_node(string_dependenciest &dependencies, const exprt &expr, array_poolt &array_pool, symbol_generatort &fresh_symbol)
When a sub-expression of expr is a builtin_function, add a "string_builtin_function" node to the grap...
Definition: string_dependencies.cpp:198
as_const
const T & as_const(T &value)
Return a reference to the same object but ensures the type is const.
Definition: as_const.h:14
messaget::error
mstreamt & error() const
Definition: message.h:399
string_dependenciest::clean_cache
void clean_cache()
Clean the cache used by eval
Definition: string_dependencies.cpp:191
find_counter_example
static optionalt< exprt > find_counter_example(const namespacet &ns, const exprt &axiom, const symbol_exprt &var, message_handlert &message_handler)
Creates a solver with axiom as the only formula added and runs it.
Definition: string_refinement.cpp:1904
DATA_INVARIANT
#define DATA_INVARIANT(CONDITION, REASON)
This condition should be used to document that assumptions that are made on goto_functions,...
Definition: invariant.h:510
CHARACTER_FOR_UNKNOWN
#define CHARACTER_FOR_UNKNOWN
Module: String solver Author: Diffblue Ltd.
Definition: string_builtin_function.h:15
id2string
const std::string & id2string(const irep_idt &d)
Definition: irep.h:49
debug_model
void debug_model(const string_constraint_generatort &generator, messaget::mstreamt &stream, const namespacet &ns, const std::function< exprt(const exprt &)> &super_get, const std::vector< symbol_exprt > &symbols, array_poolt &array_pool)
Display part of the current model by mapping the variables created by the solver to constant expressi...
Definition: string_refinement.cpp:1113
substitute_array_lists
exprt substitute_array_lists(exprt expr, size_t string_max_length)
Replace array-lists by 'with' expressions.
Definition: string_refinement.cpp:1777
forall_operands
#define forall_operands(it, expr)
Definition: expr.h:18
string_of_array
static std::string string_of_array(const array_exprt &arr)
convert the content of a string to a more readable representation.
Definition: string_refinement.cpp:1047
string_refinementt::get
exprt get(const exprt &expr) const override
Evaluates the given expression in the valuation found by string_refinementt::dec_solve.
Definition: string_refinement.cpp:1815
string_dependenciest::eval
optionalt< exprt > eval(const array_string_exprt &s, const std::function< exprt(const exprt &)> &get_value) const
Attempt to evaluate the given string from the dependencies and valuation of strings on which it depen...
Definition: string_dependencies.cpp:169
string_not_contains_constraintt::exists_lower_bound
exprt exists_lower_bound
Definition: string_constraint.h:125
PRECONDITION
#define PRECONDITION(CONDITION)
Definition: invariant.h:463
symbol_exprt::get_identifier
const irep_idt & get_identifier() const
Definition: std_expr.h:109
has_subtype
bool has_subtype(const typet &type, const std::function< bool(const typet &)> &pred, const namespacet &ns)
returns true if any of the contained types satisfies pred
Definition: expr_util.cpp:151
arrayst::ns
const namespacet & ns
Definition: arrays.h:54
string_dependenciest::output_dot
void output_dot(std::ostream &stream) const
Definition: string_dependencies.cpp:323
nil_exprt
The NIL expression.
Definition: std_expr.h:2820
array_of_exprt
Array constructor from single element.
Definition: std_expr.h:1402
bv_refinementt::infot::prop
propt * prop
Definition: bv_refinement.h:36
string_identifiers_resolution_from_equations
union_find_replacet string_identifiers_resolution_from_equations(const std::vector< equal_exprt > &equations, const namespacet &ns, messaget::mstreamt &stream)
Symbol resolution for expressions of type string typet.
Definition: string_refinement.cpp:463
get_array
static optionalt< exprt > get_array(const std::function< exprt(const exprt &)> &super_get, const namespacet &ns, messaget::mstreamt &stream, const array_string_exprt &arr, const array_poolt &array_pool)
Get a model of an array and put it in a certain form.
Definition: string_refinement.cpp:999
string_constraintt::is_valid_string_constraint
static bool is_valid_string_constraint(messaget::mstreamt &stream, const namespacet &ns, const string_constraintt &constraint)
Checks the data invariant for string_constraintt.
Definition: string_refinement.cpp:1993
simplify
bool simplify(exprt &expr, const namespacet &ns)
Definition: simplify_expr.cpp:2654
simplify_expr
exprt simplify_expr(exprt src, const namespacet &ns)
Definition: simplify_expr.cpp:2659
is_char_array_type
bool is_char_array_type(const typet &type, const namespacet &ns)
Distinguish char array from other types.
Definition: string_refinement_util.cpp:28
index_exprt::index
exprt & index()
Definition: std_expr.h:1354
index_exprt::array
exprt & array()
Definition: std_expr.h:1344
decision_proceduret::resultt::D_ERROR
@ D_ERROR
irept::is_nil
bool is_nil() const
Definition: irep.h:387
irept::id
const irep_idt & id() const
Definition: irep.h:407
message_handlert
Definition: message.h:28
substitute_array_access_in_place
static void substitute_array_access_in_place(exprt &expr, symbol_generatort &symbol_generator, const bool left_propagate)
Auxiliary function for substitute_array_access Performs the same operation but modifies the argument ...
Definition: string_refinement.cpp:1229
range.h
Ranges: pair of begin and end iterators, which can be initialized from containers,...
string_refinementt::axioms
string_axiomst axioms
Definition: string_refinement.h:103
equation_symbol_mappingt::add
void add(const std::size_t i, const exprt &expr)
Record the fact that equation i contains expression expr
Definition: equation_symbol_mapping.cpp:11
string_refinement.h
String support via creating string constraints and progressively instantiating the universal constrai...
update_index_set
static void update_index_set(index_set_pairt &index_set, const namespacet &ns, const std::vector< exprt > &current_constraints)
Add to the index set all the indices that appear in the formulas.
Definition: string_refinement.cpp:1527
equation_symbol_mappingt
Maps equation to expressions contained in them and conversely expressions to equations that contain t...
Definition: equation_symbol_mapping.h:22
add_to_index_set
static void add_to_index_set(index_set_pairt &index_set, const namespacet &ns, const exprt &s, exprt i)
Add i to the index set all the indices that appear in the formula.
Definition: string_refinement.cpp:1569
string_dependenciest::add_constraints
NODISCARD string_constraintst add_constraints(string_constraint_generatort &generatort)
For all builtin call on which a test (or an unsupported buitin) result depends, add the corresponding...
Definition: string_dependencies.cpp:349
to_array_string_expr
array_string_exprt & to_array_string_expr(exprt &expr)
Definition: string_expr.h:95
string_constraint_generatort::make_array_pointer_association
optionalt< exprt > make_array_pointer_association(const exprt &return_code, const function_application_exprt &expr)
Associate array to pointer, and array to length.
Definition: string_constraint_generator_main.cpp:192
optionalt
nonstd::optional< T > optionalt
Definition: optional.h:35
bv_refinementt
Definition: bv_refinement.h:20
string_refinementt::symbol_resolve
union_find_replacet symbol_resolve
Definition: string_refinement.h:112
prop_conv_solvert::convert
literalt convert(const exprt &expr) override
Convert a Boolean expression and return the corresponding literal.
Definition: prop_conv_solver.cpp:156
decision_proceduret::resultt
resultt
Result of running the decision procedure.
Definition: decision_procedure.h:44
minus_exprt
Binary minus.
Definition: std_expr.h:973
union_find_replacet::make_union
exprt make_union(const exprt &a, const exprt &b)
Merge the set containing a and the set containing b.
Definition: union_find_replace.cpp:15
messaget::mstreamt::message
messaget & message
Definition: message.h:246
string_constraintt::negation
exprt negation() const
Definition: string_constraint.h:95
string_refinementt::dec_solve
decision_proceduret::resultt dec_solve() override
Main decision procedure of the solver.
Definition: string_refinement.cpp:607
string_refinementt::dependencies
string_dependenciest dependencies
Definition: string_refinement.h:116
char_type
bitvector_typet char_type()
Definition: c_types.cpp:114
simplify_expr.h
string_not_contains_constraintt::univ_lower_bound
exprt univ_lower_bound
Definition: string_constraint.h:122
member_exprt
Extract member of struct or union.
Definition: std_expr.h:2613
expr_util.h
Deprecated expression utility functions.
satcheck.h
string_constraintt::univ_within_bounds
exprt univ_within_bounds() const
Definition: string_constraint.h:81
messaget::get_message_handler
message_handlert & get_message_handler()
Definition: message.h:184
string_refinementt
Definition: string_refinement.h:65
add_equations_for_symbol_resolution
static void add_equations_for_symbol_resolution(union_find_replacet &symbol_solver, const std::vector< exprt > &equations, const namespacet &ns, messaget::mstreamt &stream)
Add association for each char pointer in the equation.
Definition: string_refinement.cpp:301
interval_sparse_arrayt::concretize
array_exprt concretize(std::size_t size, const typet &index_type) const
Convert to an array representation, ignores elements at index >= size.
Definition: string_refinement_util.cpp:174
struct_typet
Structure type, corresponds to C style structs.
Definition: std_types.h:231
array_typet
Arrays with given size.
Definition: std_types.h:763
expr_iterator.h
Forward depth-first search iterators These iterators' copy operations are expensive,...
string_constraintt::universal_only_in_index
static bool universal_only_in_index(const string_constraintt &constr)
The universally quantified variable is only allowed to occur in index expressions in the body of a st...
Definition: string_refinement.cpp:1973
if_exprt::true_case
exprt & true_case()
Definition: std_expr.h:2199
replace_expr_copy
static exprt replace_expr_copy(const union_find_replacet &symbol_resolve, exprt expr)
Substitute sub-expressions in equation by representative elements of symbol_resolve whenever possible...
Definition: string_refinement.cpp:273
namespace_baset::follow
const typet & follow(const typet &) const
Resolve type symbol to the type it points to.
Definition: namespace.cpp:49
can_cast_expr< function_application_exprt >
bool can_cast_expr< function_application_exprt >(const exprt &base)
Definition: mathematical_expr.h:225
union_find_replacet
Similar interface to union-find for expressions, with a function for replacing sub-expressions by the...
Definition: union_find_replace.h:17
replace_expr
bool replace_expr(const exprt &what, const exprt &by, exprt &dest)
Definition: replace_expr.cpp:12
from_integer
constant_exprt from_integer(const mp_integer &int_value, const typet &type)
Definition: arith_tools.cpp:99
string_refinementt::instantiate_not_contains
std::vector< exprt > instantiate_not_contains(const string_not_contains_constraintt &axiom, const std::set< std::pair< exprt, exprt >> &index_pairs, const std::unordered_map< string_not_contains_constraintt, symbol_exprt > &witnesses)
Instantiates a quantified formula representing not_contains by substituting the quantifiers and gener...
Definition: string_constraint_instantiation.cpp:201
string_refinement_invariantt
#define string_refinement_invariantt(reason)
Definition: string_refinement_invariant.h:12
array_poolt::get_length_if_exists
optionalt< exprt > get_length_if_exists(const array_string_exprt &s) const
As opposed to get_length(), do not create a new symbol if the length of the array_string_exprt does n...
Definition: array_pool.cpp:48
solver
int solver(std::istream &in)
Definition: smt2_solver.cpp:407
has_char_pointer_subtype
bool has_char_pointer_subtype(const typet &type, const namespacet &ns)
Definition: string_refinement_util.cpp:40
exprt::is_constant
bool is_constant() const
Return whether the expression is a constant.
Definition: expr.cpp:26
if_exprt::cond
exprt & cond()
Definition: std_expr.h:2189
string_typet
String type.
Definition: std_types.h:880
string_constraint_instantiation.h
Defines related function for string constraints.
binary_relation_exprt
A base class for relations, i.e., binary predicates whose two operands have the same type.
Definition: std_expr.h:674
simplify_sum
exprt simplify_sum(const exprt &f)
Definition: string_refinement.cpp:1502
to_array_type
const array_typet & to_array_type(const typet &type)
Cast a typet to an array_typet.
Definition: std_types.h:813
to_equal_expr
const equal_exprt & to_equal_expr(const exprt &expr)
Cast an exprt to an equal_exprt.
Definition: std_expr.h:1265
extract_strings_from_lhs
static std::vector< exprt > extract_strings_from_lhs(const exprt &lhs, const namespacet &ns)
This is meant to be used on the lhs of an equation with string subtype.
Definition: string_refinement.cpp:374
string_not_contains_constraintt::s0
array_string_exprt s0
Definition: string_constraint.h:127
substitute_array_access
static optionalt< exprt > substitute_array_access(const index_exprt &index_expr, symbol_generatort &symbol_generator, const bool left_propagate)
Definition: string_refinement.cpp:1200
boolbvt
Definition: boolbv.h:42
index_set_pairt::current
std::map< exprt, std::set< exprt > > current
Definition: string_refinement_util.h:62
equation_symbol_mappingt::find_expressions
std::vector< exprt > find_expressions(const std::size_t i)
Definition: equation_symbol_mapping.cpp:18
messaget::mstreamt
Definition: message.h:224
string_refinementt::equations
std::vector< exprt > equations
Definition: string_refinement.h:114
string_constraintst::not_contains
std::vector< string_not_contains_constraintt > not_contains
Definition: string_constraint_generator.h:42
implies_exprt
Boolean implication.
Definition: std_expr.h:1983
string_refinementt::add_lemma
void add_lemma(const exprt &lemma, bool simplify_lemma=true)
Add the given lemma to the solver.
Definition: string_refinement.cpp:896
string_not_contains_constraintt
Constraints to encode non containement of strings.
Definition: string_constraint.h:121
index_set_pairt::cumulative
std::map< exprt, std::set< exprt > > cumulative
Definition: string_refinement_util.h:61
string_constraintt::is_linear_arithmetic_expr
static bool is_linear_arithmetic_expr(const exprt &expr, const symbol_exprt &var)
Definition: string_refinement.cpp:1947
string_refinementt::infot
string_refinementt constructor arguments
Definition: string_refinement.h:76
exprt::operands
operandst & operands()
Definition: expr.h:92
messaget::debug
mstreamt & debug() const
Definition: message.h:429
string_refinementt::config_
const configt config_
Definition: string_refinement.h:96
string_constraintt::gather_indices
static array_index_mapt gather_indices(const exprt &expr)
Definition: string_refinement.cpp:1924
string_refinementt::set_to
void set_to(const exprt &expr, bool value) override
Record the constraints to ensure that the expression is true when the boolean is true and false other...
Definition: string_refinement.cpp:283
index_exprt
Array index operator.
Definition: std_expr.h:1328
INVARIANT
#define INVARIANT(CONDITION, REASON)
This macro uses the wrapper function 'invariant_violated_string'.
Definition: invariant.h:423
string_not_contains_constraintt::s1
array_string_exprt s1
Definition: string_constraint.h:128
bv_refinementt::infot::ns
const namespacet * ns
Definition: bv_refinement.h:35
constant_exprt
A constant literal expression.
Definition: std_expr.h:2753
exprt::depth_end
depth_iteratort depth_end()
Definition: expr.cpp:267
string_refinementt::configt::use_counter_example
bool use_counter_example
Definition: string_refinement.h:70
string_refinementt::string_refinementt
string_refinementt(const infot &)
Definition: string_refinement.cpp:165
equation_symbol_mappingt::find_equations
std::vector< std::size_t > find_equations(const exprt &expr)
Definition: equation_symbol_mapping.cpp:24
array_exprt
Array constructor from list of elements.
Definition: std_expr.h:1467
add_string_equation_to_symbol_resolution
static void add_string_equation_to_symbol_resolution(const equal_exprt &eq, union_find_replacet &symbol_resolve, const namespacet &ns)
Given an equation on strings, mark these strings as belonging to the same set in the symbol_resolve s...
Definition: string_refinement.cpp:427
make_range
ranget< iteratort > make_range(iteratort begin, iteratort end)
Definition: range.h:524
string_axiomst::not_contains
std::vector< string_not_contains_constraintt > not_contains
Definition: string_refinement_util.h:68
format_type.h
array_string_exprt
Definition: string_expr.h:67
boolbvt::get
exprt get(const exprt &expr) const override
Return expr with variables replaced by values from satisfying assignment if available.
Definition: boolbv_get.cpp:19
get_char_array_and_concretize
static exprt get_char_array_and_concretize(const std::function< exprt(const exprt &)> &super_get, const namespacet &ns, messaget::mstreamt &stream, const array_string_exprt &arr, array_poolt &array_pool)
Debugging function which finds the valuation of the given array in super_get and concretize unknown c...
Definition: string_refinement.cpp:1065
string_refinementt::seen_instances
std::set< exprt > seen_instances
Definition: string_refinement.h:101
prop_conv_solvert::prop
propt & prop
Definition: prop_conv_solver.h:126
string_constraintst::existential
std::vector< exprt > existential
Definition: string_constraint_generator.h:40
not_exprt
Boolean negation.
Definition: std_expr.h:2127
to_constant_expr
const constant_exprt & to_constant_expr(const exprt &expr)
Cast an exprt to a constant_exprt.
Definition: std_expr.h:2786
string_constraintst::universal
std::vector< string_constraintt > universal
Definition: string_constraint_generator.h:41