cprover
symex_main.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Symbolic Execution
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
11 
12 #include "goto_symex.h"
13 
14 #include <memory>
15 
17 
18 #include <util/exception_utils.h>
19 #include <util/expr_iterator.h>
20 #include <util/expr_util.h>
21 #include <util/invariant.h>
22 #include <util/make_unique.h>
23 #include <util/mathematical_expr.h>
24 #include <util/replace_symbol.h>
25 #include <util/std_expr.h>
26 #include <util/string2int.h>
27 #include <util/symbol_table.h>
28 
29 #include <util/format.h>
30 #include <util/format_expr.h>
31 #include <util/format_type.h>
32 #include <util/std_types.h>
33 
35  : max_depth(options.get_unsigned_int_option("depth")),
36  doing_path_exploration(options.is_set("paths")),
37  allow_pointer_unsoundness(
38  options.get_bool_option("allow-pointer-unsoundness")),
39  constant_propagation(options.get_bool_option("propagation")),
40  self_loops_to_assumptions(
41  options.get_bool_option("self-loops-to-assumptions")),
42  simplify_opt(options.get_bool_option("simplify")),
43  unwinding_assertions(options.get_bool_option("unwinding-assertions")),
44  partial_loops(options.get_bool_option("partial-loops")),
45  havoc_undefined_functions(
46  options.get_bool_option("havoc-undefined-functions")),
47  debug_level(unsafe_string2int(options.get_option("debug-level"))),
48  run_validation_checks(options.get_bool_option("validate-ssa-equation")),
49  show_symex_steps(options.get_bool_option("show-goto-symex-steps")),
50  show_points_to_sets(options.get_bool_option("show-points-to-sets")),
51  max_field_sensitivity_array_size(
52  options.is_set("no-array-field-sensitivity")
53  ? 0
54  : options.is_set("max-field-sensitivity-array-size")
55  ? options.get_unsigned_int_option(
56  "max-field-sensitivity-array-size")
58  complexity_limits_active(
59  options.get_signed_int_option("symex-complexity-limit") > 0)
60 {
61 }
62 
66 static void pop_exited_loops(
68  std::vector<framet::active_loop_infot> &active_loops)
69 {
70  while(!active_loops.empty())
71  {
72  if(!active_loops.back().loop.contains(to))
73  active_loops.pop_back();
74  else
75  break;
76  }
77 }
78 
80  goto_symext::statet &state,
82  bool is_backwards_goto)
83 {
84  if(!state.call_stack().empty())
85  {
86  // initialize the loop counter of any loop we are newly entering
87  // upon this transition; we are entering a loop if
88  // 1. the transition from state.source.pc to "to" is not a backwards goto
89  // or
90  // 2. we are arriving from an outer loop
91 
92  // TODO: This should all be replaced by natural loop analysis.
93  // This is because the way we detect loops is pretty imprecise.
94 
95  framet &frame = state.call_stack().top();
96  const goto_programt::instructiont &instruction=*to;
97  for(const auto &i_e : instruction.incoming_edges)
98  {
99  if(
100  i_e->is_backwards_goto() && i_e->get_target() == to &&
101  (!is_backwards_goto ||
102  state.source.pc->location_number > i_e->location_number))
103  {
104  const auto loop_id =
106  auto &current_loop_info = frame.loop_iterations[loop_id];
107  current_loop_info.count = 0;
108 
109  // We've found a loop, put it on the stack and say it's our current
110  // active loop.
111  if(
112  frame.loops_info && frame.loops_info->loop_map.find(to) !=
113  frame.loops_info->loop_map.end())
114  {
115  frame.active_loops.emplace_back(frame.loops_info->loop_map[to]);
116  }
117  }
118  }
119 
120  // Only do this if we have active loop analysis going.
121  if(!frame.active_loops.empty())
122  {
123  // Otherwise if we find we're transitioning out of a loop, make sure
124  // to remove any loops we're not currently iterating over.
125 
126  // Match the do-while pattern.
127  if(
128  state.source.pc->is_backwards_goto() &&
129  state.source.pc->location_number < to->location_number)
130  {
131  pop_exited_loops(to, frame.active_loops);
132  }
133 
134  // Match for-each or while.
135  for(const auto &incoming_edge : state.source.pc->incoming_edges)
136  {
137  if(
138  incoming_edge->is_backwards_goto() &&
139  incoming_edge->location_number < to->location_number)
140  {
141  pop_exited_loops(to, frame.active_loops);
142  }
143  }
144  }
145  }
146 
147  state.source.pc=to;
148 }
149 
151 {
153  ++next;
154  symex_transition(state, next, false);
155 }
156 
158  const goto_programt::instructiont &instruction,
159  statet &state)
160 {
161  exprt condition = clean_expr(instruction.get_condition(), state, false);
162 
163  // First, push negations in and perhaps convert existential quantifiers into
164  // universals:
165  if(has_subexpr(condition, ID_exists) || has_subexpr(condition, ID_forall))
166  do_simplify(condition);
167 
168  // Second, L2-rename universal quantifiers:
169  if(has_subexpr(condition, ID_forall))
170  rewrite_quantifiers(condition, state);
171 
172  // now rename, enables propagation
173  exprt l2_condition = state.rename(std::move(condition), ns).get();
174 
175  // now try simplifier on it
176  do_simplify(l2_condition);
177 
178  std::string msg = id2string(instruction.source_location.get_comment());
179  if(msg.empty())
180  msg = "assertion";
181 
182  vcc(l2_condition, msg, state);
183 }
184 
186  const exprt &condition,
187  const std::string &msg,
188  statet &state)
189 {
190  state.total_vccs++;
192 
193  if(condition.is_true())
194  return;
195 
196  const exprt guarded_condition = state.guard.guard_expr(condition);
197 
198  state.remaining_vccs++;
199  target.assertion(state.guard.as_expr(), guarded_condition, msg, state.source);
200 }
201 
202 void goto_symext::symex_assume(statet &state, const exprt &cond)
203 {
204  exprt simplified_cond = clean_expr(cond, state, false);
205  simplified_cond = state.rename(std::move(simplified_cond), ns).get();
206  do_simplify(simplified_cond);
207 
208  // It would be better to call try_filter_value_sets after apply_condition,
209  // but it is not currently possible. See the comment at the beginning of
210  // \ref apply_goto_condition for more information.
211 
213  state, cond, state.value_set, &state.value_set, nullptr, ns);
214 
215  // apply_condition must come after rename because it might change the
216  // constant propagator and the value-set and we read from those in rename
217  state.apply_condition(simplified_cond, state, ns);
218 
219  symex_assume_l2(state, simplified_cond);
220 }
221 
222 void goto_symext::symex_assume_l2(statet &state, const exprt &cond)
223 {
224  if(cond.is_true())
225  return;
226 
227  if(cond.is_false())
228  state.reachable = false;
229 
230  // we are willing to re-write some quantified expressions
231  exprt rewritten_cond = cond;
232  if(has_subexpr(rewritten_cond, ID_exists))
233  rewrite_quantifiers(rewritten_cond, state);
234 
235  if(state.threads.size()==1)
236  {
237  exprt tmp = state.guard.guard_expr(rewritten_cond);
238  target.assumption(state.guard.as_expr(), tmp, state.source);
239  }
240  // symex_target_equationt::convert_assertions would fail to
241  // consider assumptions of threads that have a thread-id above that
242  // of the thread containing the assertion:
243  // T0 T1
244  // x=0; assume(x==1);
245  // assert(x!=42); x=42;
246  else
247  state.guard.add(rewritten_cond);
248 
249  if(state.atomic_section_id!=0 &&
250  state.guard.is_false())
251  symex_atomic_end(state);
252 }
253 
255 {
256  const bool is_assert = state.source.pc->is_assert();
257 
258  if(
259  (is_assert && expr.id() == ID_forall) ||
260  (!is_assert && expr.id() == ID_exists))
261  {
262  // for assertions e can rewrite "forall X. P" to "P", and
263  // for assumptions we can rewrite "exists X. P" to "P"
264  // we keep the quantified variable unique by means of L2 renaming
265  auto &quant_expr = to_quantifier_expr(expr);
266  symbol_exprt tmp0 =
267  to_symbol_expr(to_ssa_expr(quant_expr.symbol()).get_original_expr());
268  symex_decl(state, tmp0);
269  instruction_local_symbols.push_back(tmp0);
270  exprt tmp = quant_expr.where();
271  rewrite_quantifiers(tmp, state);
272  quant_expr.swap(tmp);
273  }
274  else if(expr.id() == ID_or || expr.id() == ID_and)
275  {
276  for(auto &op : expr.operands())
277  rewrite_quantifiers(op, state);
278  }
279 }
280 
281 static void
282 switch_to_thread(goto_symex_statet &state, const unsigned int thread_nb)
283 {
284  PRECONDITION(state.source.thread_nr < state.threads.size());
285  PRECONDITION(thread_nb < state.threads.size());
286 
287  // save PC
288  state.threads[state.source.thread_nr].pc = state.source.pc;
289  state.threads[state.source.thread_nr].atomic_section_id =
290  state.atomic_section_id;
291 
292  // get new PC
293  state.source.thread_nr = thread_nb;
294  state.source.pc = state.threads[thread_nb].pc;
295 
296  state.guard = state.threads[thread_nb].guard;
297  // A thread's initial state is certainly reachable:
298  state.reachable = true;
299 }
300 
302  statet &state, const get_goto_functiont &get_goto_function)
303 {
305 
306  _total_vccs = state.total_vccs;
308 
310  return;
311 
312  // is there another thread to execute?
313  if(state.call_stack().empty() &&
314  state.source.thread_nr+1<state.threads.size())
315  {
316  unsigned t=state.source.thread_nr+1;
317 #if 0
318  std::cout << "********* Now executing thread " << t << '\n';
319 #endif
320  switch_to_thread(state, t);
321  symex_transition(state, state.source.pc, false);
322  }
323 }
324 
326  statet &state,
327  const get_goto_functiont &get_goto_function,
328  symbol_tablet &new_symbol_table)
329 {
330  // resets the namespace to only wrap a single symbol table, and does so upon
331  // destruction of an object of this type; instantiating the type is thus all
332  // that's needed to achieve a reset upon exiting this method
333  struct reset_namespacet
334  {
335  explicit reset_namespacet(namespacet &ns) : ns(ns)
336  {
337  }
338 
339  ~reset_namespacet()
340  {
341  // Get symbol table 1, the outer symbol table from the GOTO program
342  const symbol_tablet &st = ns.get_symbol_table();
343  // Move a new namespace containing this symbol table over the top of the
344  // current one
345  ns = namespacet(st);
346  }
347 
348  namespacet &ns;
349  };
350 
351  // We'll be using ns during symbolic execution and it needs to know
352  // about the names minted in `state`, so make it point both to
353  // `state`'s symbol table and the symbol table of the original
354  // goto-program.
356 
357  // whichever way we exit this method, reset the namespace back to a sane state
358  // as state.symbol_table might go out of scope
359  reset_namespacet reset_ns(ns);
360 
361  PRECONDITION(state.call_stack().top().end_of_function->is_end_function());
362 
365  return;
366  while(!state.call_stack().empty())
367  {
368  state.has_saved_jump_target = false;
369  state.has_saved_next_instruction = false;
372  return;
373  }
374 
375  // Clients may need to construct a namespace with both the names in
376  // the original goto-program and the names generated during symbolic
377  // execution, so return the names generated through symbolic execution
378  // through `new_symbol_table`.
379  new_symbol_table = state.symbol_table;
380 }
381 
383  const get_goto_functiont &get_goto_function,
384  const statet &saved_state,
385  symex_target_equationt *const saved_equation,
386  symbol_tablet &new_symbol_table)
387 {
388  // saved_state contains a pointer to a symex_target_equationt that is
389  // almost certainly stale. This is because equations are owned by bmcts,
390  // and we construct a new bmct for every path that we execute. We're on a
391  // new path now, so the old bmct and the equation that it owned have now
392  // been deallocated. So, construct a new state from the old one, and make
393  // its equation member point to the (valid) equation passed as an argument.
394  statet state(saved_state, saved_equation);
395 
396  // Do NOT do the same initialization that `symex_with_state` does for a
397  // fresh state, as that would clobber the saved state's program counter
399  state,
401  new_symbol_table);
402 }
403 
404 std::unique_ptr<goto_symext::statet> goto_symext::initialize_entry_point_state(
405  const get_goto_functiont &get_goto_function)
406 {
407  const irep_idt entry_point_id = goto_functionst::entry_point();
408 
409  const goto_functionst::goto_functiont *start_function;
410  try
411  {
412  start_function = &get_goto_function(entry_point_id);
413  }
414  catch(const std::out_of_range &)
415  {
416  throw unsupported_operation_exceptiont("the program has no entry point");
417  }
418 
419  // Get our path_storage pointer because this state will live beyond
420  // this instance of goto_symext, so we can't take the reference directly.
421  auto *storage = &path_storage;
422 
423  // create and prepare the state
424  auto state = util_make_unique<statet>(
425  symex_targett::sourcet(entry_point_id, start_function->body),
428  [storage](const irep_idt &id) { return storage->get_unique_l2_index(id); });
429 
430  CHECK_RETURN(!state->threads.empty());
431  CHECK_RETURN(!state->call_stack().empty());
432 
434  std::prev(start_function->body.instructions.end());
435  state->call_stack().top().end_of_function = limit;
436  state->call_stack().top().calling_location.pc =
437  state->call_stack().top().end_of_function;
438  state->call_stack().top().hidden_function = start_function->is_hidden();
439 
440  state->symex_target = &target;
441 
442  state->run_validation_checks = symex_config.run_validation_checks;
443 
444  // initialize support analyses
445  auto emplace_safe_pointers_result =
446  path_storage.safe_pointers.emplace(entry_point_id, local_safe_pointerst{});
447  if(emplace_safe_pointers_result.second)
448  emplace_safe_pointers_result.first->second(start_function->body);
449 
451  entry_point_id, *start_function);
452  state->dirty = &path_storage.dirty;
453 
454  // Only enable loop analysis when complexity is enabled.
456  {
457  // Set initial loop analysis.
458  path_storage.add_function_loops(entry_point_id, start_function->body);
459  state->call_stack().top().loops_info =
460  path_storage.get_loop_analysis(entry_point_id);
461  }
462 
463  // make the first step onto the instruction pointed to by the initial program
464  // counter
465  symex_transition(*state, state->source.pc, false);
466 
467  return state;
468 }
469 
471  const get_goto_functiont &get_goto_function,
472  symbol_tablet &new_symbol_table)
473 {
475 
476  symex_with_state(*state, get_goto_function, new_symbol_table);
477 }
478 
480  const get_goto_functiont &get_goto_function,
481  symbol_tablet &new_symbol_table)
482 {
484 
485  path_storaget::patht entry_point_start(target, *state);
486  entry_point_start.state.saved_target = state->source.pc;
487  entry_point_start.state.has_saved_next_instruction = true;
488 
489  path_storage.push(entry_point_start);
490 }
491 
494 {
495  return [&goto_model](
496  const irep_idt &id) -> const goto_functionst::goto_functiont & {
497  return goto_model.get_goto_function(id);
498  };
499 }
500 
503 {
504  log.status() << source.function_id
505  << " location number: " << source.pc->location_number;
506 
507  return log.status();
508 }
509 
511 {
512  // If we're showing the route, begin outputting debug info, and don't print
513  // instructions we don't run.
514 
515  // We also skip dead instructions as they don't add much to step-based
516  // debugging and if there's no code block at this point.
517  if(
519  state.source.pc->type == DEAD ||
520  (state.source.pc->code.is_nil() && state.source.pc->type != END_FUNCTION))
521  {
522  return;
523  }
524 
525  if(state.source.pc->code.is_not_nil())
526  {
527  auto guard_expression = state.guard.as_expr();
528  std::size_t size = 0;
529  for(auto it = guard_expression.depth_begin();
530  it != guard_expression.depth_end();
531  ++it)
532  {
533  size++;
534  }
535 
536  log.status() << "[Guard size: " << size << "] "
537  << format(state.source.pc->code);
538 
539  if(
540  state.source.pc->source_location.is_not_nil() &&
541  !state.source.pc->source_location.get_java_bytecode_index().empty())
542  {
543  log.status()
544  << " bytecode index: "
545  << state.source.pc->source_location.get_java_bytecode_index();
546  }
547 
548  log.status() << messaget::eom;
549  }
550 
551  // Print the method we're returning too.
552  const auto &call_stack = state.threads[state.source.thread_nr].call_stack;
553  if(state.source.pc->type == END_FUNCTION)
554  {
555  log.status() << messaget::eom;
556 
557  if(!call_stack.empty())
558  {
559  log.status() << "Returning to: ";
560  print_callstack_entry(call_stack.back().calling_location)
561  << messaget::eom;
562  }
563 
564  log.status() << messaget::eom;
565  }
566 
567  // On a function call print the entire call stack.
568  if(state.source.pc->type == FUNCTION_CALL)
569  {
570  log.status() << messaget::eom;
571 
572  if(!call_stack.empty())
573  {
574  log.status() << "Call stack:" << messaget::eom;
575 
576  for(auto &frame : call_stack)
577  {
578  print_callstack_entry(frame.calling_location) << messaget::eom;
579  }
580 
582 
583  // Add the method we're about to enter with no location number.
584  log.status() << format(
585  to_code_function_call(state.source.pc->code).function())
587  }
588  }
589 }
590 
593  const get_goto_functiont &get_goto_function,
594  statet &state)
595 {
596  // Print debug statements if they've been enabled.
597  print_symex_step(state);
600 }
601 
603  const get_goto_functiont &get_goto_function,
604  statet &state)
605 {
606  PRECONDITION(!state.threads.empty());
607  PRECONDITION(!state.call_stack().empty());
608 
609  const goto_programt::instructiont &instruction=*state.source.pc;
610 
612  merge_gotos(state);
613 
614  // depth exceeded?
616  {
617  // Rule out this path:
618  symex_assume_l2(state, false_exprt());
619  }
620  state.depth++;
621 
622  // actually do instruction
623  switch(instruction.type)
624  {
625  case SKIP:
626  if(state.reachable)
627  target.location(state.guard.as_expr(), state.source);
628  symex_transition(state);
629  break;
630 
631  case END_FUNCTION:
632  // do even if !state.reachable to clear out frame created
633  // in symex_start_thread
634  symex_end_of_function(state);
635  symex_transition(state);
636  break;
637 
638  case LOCATION:
639  if(state.reachable)
640  target.location(state.guard.as_expr(), state.source);
641  symex_transition(state);
642  break;
643 
644  case GOTO:
645  if(state.reachable)
646  symex_goto(state);
647  else
648  symex_unreachable_goto(state);
649  break;
650 
651  case ASSUME:
652  if(state.reachable)
653  symex_assume(state, instruction.get_condition());
654  symex_transition(state);
655  break;
656 
657  case ASSERT:
658  if(state.reachable && !ignore_assertions)
659  symex_assert(instruction, state);
660  symex_transition(state);
661  break;
662 
663  case RETURN:
664  // This case should have been removed by return-value removal
665  UNREACHABLE;
666  break;
667 
668  case ASSIGN:
669  if(state.reachable)
670  symex_assign(
671  state, instruction.get_assign().lhs(), instruction.get_assign().rhs());
672 
673  symex_transition(state);
674  break;
675 
676  case FUNCTION_CALL:
677  if(state.reachable)
678  {
680  get_goto_function, state, instruction.get_function_call());
681  }
682  else
683  symex_transition(state);
684  break;
685 
686  case OTHER:
687  if(state.reachable)
688  symex_other(state);
689  symex_transition(state);
690  break;
691 
692  case DECL:
693  if(state.reachable)
694  symex_decl(state);
695  symex_transition(state);
696  break;
697 
698  case DEAD:
699  symex_dead(state);
700  symex_transition(state);
701  break;
702 
703  case START_THREAD:
704  symex_start_thread(state);
705  symex_transition(state);
706  break;
707 
708  case END_THREAD:
709  // behaves like assume(0);
710  if(state.reachable)
711  state.reachable = false;
712  symex_transition(state);
713  break;
714 
715  case ATOMIC_BEGIN:
716  symex_atomic_begin(state);
717  symex_transition(state);
718  break;
719 
720  case ATOMIC_END:
721  symex_atomic_end(state);
722  symex_transition(state);
723  break;
724 
725  case CATCH:
726  symex_catch(state);
727  symex_transition(state);
728  break;
729 
730  case THROW:
731  symex_throw(state);
732  symex_transition(state);
733  break;
734 
735  case NO_INSTRUCTION_TYPE:
736  throw unsupported_operation_exceptiont("symex got NO_INSTRUCTION");
737 
738  case INCOMPLETE_GOTO:
739  DATA_INVARIANT(false, "symex got unexpected instruction type");
740  }
741 
742  complexity_violationt complexity_result =
744  if(complexity_result != complexity_violationt::NONE)
745  complexity_module.run_transformations(complexity_result, state);
746 }
747 
749 {
750  for(const auto &symbol_expr : instruction_local_symbols)
751  symex_dead(state, symbol_expr);
753 }
754 
762 {
763  optionalt<symbol_exprt> return_value;
764  for(auto it = expr.depth_cbegin(); it != expr.depth_cend(); ++it)
765  {
766  const symbol_exprt *symbol_expr = expr_try_dynamic_cast<symbol_exprt>(*it);
767  if(symbol_expr && can_cast_type<pointer_typet>(symbol_expr->type()))
768  {
769  // If we already have a potential return value, check if it is the same
770  // symbol, and return an empty optionalt if not
771  if(return_value && *symbol_expr != *return_value)
772  {
773  return {};
774  }
775  return_value = *symbol_expr;
776  }
777  }
778 
779  // Either expr contains no pointer-typed symbols or it contains one unique
780  // pointer-typed symbol, possibly repeated multiple times
781  return return_value;
782 }
783 
785  goto_symex_statet &state,
786  exprt condition,
787  const value_sett &original_value_set,
788  value_sett *jump_taken_value_set,
789  value_sett *jump_not_taken_value_set,
790  const namespacet &ns)
791 {
792  condition = state.rename<L1>(std::move(condition), ns).get();
793 
794  optionalt<symbol_exprt> symbol_expr =
796 
797  if(!symbol_expr)
798  {
799  return;
800  }
801 
802  const pointer_typet &symbol_type = to_pointer_type(symbol_expr->type());
803 
804  const std::vector<exprt> value_set_elements =
805  original_value_set.get_value_set(*symbol_expr, ns);
806 
807  std::unordered_set<exprt, irep_hash> erase_from_jump_taken_value_set;
808  std::unordered_set<exprt, irep_hash> erase_from_jump_not_taken_value_set;
809  erase_from_jump_taken_value_set.reserve(value_set_elements.size());
810  erase_from_jump_not_taken_value_set.reserve(value_set_elements.size());
811 
812  // Try evaluating the condition with the symbol replaced by a pointer to each
813  // one of its possible values in turn. If that leads to a true for some
814  // value_set_element then we can delete it from the value set that will be
815  // used if the condition is false, and vice versa.
816  for(const exprt &value_set_element : value_set_elements)
817  {
818  if(
819  value_set_element.id() == ID_unknown ||
820  value_set_element.id() == ID_invalid)
821  {
822  continue;
823  }
824 
825  const bool exclude_null_derefs = false;
827  value_set_element, exclude_null_derefs, language_mode))
828  {
829  continue;
830  }
831 
834  value_set_element, *symbol_expr, ns);
835 
836  if(value.pointer.is_nil())
837  continue;
838 
839  exprt modified_condition(condition);
840 
841  address_of_aware_replace_symbolt replace_symbol{};
842  replace_symbol.insert(*symbol_expr, value.pointer);
843  replace_symbol(modified_condition);
844 
845  // This do_simplify() is needed for the following reason: if `condition` is
846  // `*p == a` and we replace `p` with `&a` then we get `*&a == a`. Suppose
847  // our constant propagation knows that `a` is `1`. Without this call to
848  // do_simplify(), state.rename() turns this into `*&a == 1` (because
849  // rename() doesn't do constant propagation inside addresses), which
850  // do_simplify() turns into `a == 1`, which cannot be evaluated as true
851  // without another round of constant propagation.
852  // It would be sufficient to replace this call to do_simplify() with
853  // something that just replaces `*&x` with `x` whenever it finds it.
854  do_simplify(modified_condition);
855 
856  state.record_events.push(false);
857  modified_condition = state.rename(std::move(modified_condition), ns).get();
858  state.record_events.pop();
859 
860  do_simplify(modified_condition);
861 
862  if(jump_taken_value_set && modified_condition.is_false())
863  {
864  erase_from_jump_taken_value_set.insert(value_set_element);
865  }
866  else if(jump_not_taken_value_set && modified_condition.is_true())
867  {
868  erase_from_jump_not_taken_value_set.insert(value_set_element);
869  }
870  }
871  if(jump_taken_value_set && !erase_from_jump_taken_value_set.empty())
872  {
873  auto entry_index = jump_taken_value_set->get_index_of_symbol(
874  symbol_expr->get_identifier(), symbol_type, "", ns);
875  jump_taken_value_set->erase_values_from_entry(
876  *entry_index, erase_from_jump_taken_value_set);
877  }
878  if(jump_not_taken_value_set && !erase_from_jump_not_taken_value_set.empty())
879  {
880  auto entry_index = jump_not_taken_value_set->get_index_of_symbol(
881  symbol_expr->get_identifier(), symbol_type, "", ns);
882  jump_not_taken_value_set->erase_values_from_entry(
883  *entry_index, erase_from_jump_not_taken_value_set);
884  }
885 }
Abstract interface to eager or lazy GOTO models.
virtual const goto_functionst::goto_functiont & get_goto_function(const irep_idt &id)=0
Get a GOTO function by name, or throw if no such function exists.
Replace symbols with constants while maintaining syntactically valid expressions.
framet & top()
Definition: call_stack.h:17
exprt & rhs()
Definition: std_code.h:317
exprt & lhs()
Definition: std_code.h:312
exprt & function()
Definition: std_code.h:1250
complexity_violationt check_complexity(goto_symex_statet &state)
Checks the passed-in state to see if its become too complex for us to deal with, and if so set its gu...
void run_transformations(complexity_violationt complexity_violation, goto_symex_statet &current_state)
Runs a suite of transformations on the state and symex executable, performing whatever transformation...
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:37
Base class for all expressions.
Definition: expr.h:54
bool is_true() const
Return whether the expression is a constant representing true.
Definition: expr.cpp:47
const_depth_iteratort depth_cend() const
Definition: expr.cpp:289
bool is_false() const
Return whether the expression is a constant representing false.
Definition: expr.cpp:56
typet & type()
Return the type of the expression.
Definition: expr.h:82
const_depth_iteratort depth_cbegin() const
Definition: expr.cpp:287
operandst & operands()
Definition: expr.h:96
The Boolean constant false.
Definition: std_expr.h:2726
::goto_functiont goto_functiont
static irep_idt entry_point()
Get the identifier of the entry point to a goto model.
This class represents an instruction in the GOTO intermediate representation.
Definition: goto_program.h:180
std::set< targett > incoming_edges
Definition: goto_program.h:399
source_locationt source_location
The location of the instruction in the source file.
Definition: goto_program.h:334
goto_program_instruction_typet type
What kind of instruction?
Definition: goto_program.h:337
const code_function_callt & get_function_call() const
Get the function call for FUNCTION_CALL.
Definition: goto_program.h:306
const exprt & get_condition() const
Get the condition of gotos, assume, assert.
Definition: goto_program.h:350
const code_assignt & get_assign() const
Get the assignment for ASSIGN.
Definition: goto_program.h:186
static irep_idt loop_id(const irep_idt &function_id, const instructiont &instruction)
Human-readable loop name.
Definition: goto_program.h:726
instructionst::const_iterator const_targett
Definition: goto_program.h:551
guardt guard
Definition: goto_state.h:54
unsigned depth
Distance from entry.
Definition: goto_state.h:35
bool reachable
Is this code reachable? If not we can take shortcuts such as not entering function calls,...
Definition: goto_state.h:58
void apply_condition(const exprt &condition, const goto_symex_statet &previous_state, const namespacet &ns)
Given a condition that must hold on this path, propagate as much knowledge as possible.
Definition: goto_state.cpp:42
unsigned atomic_section_id
Threads.
Definition: goto_state.h:72
value_sett value_set
Uses level 1 names, and is used to do dereferencing.
Definition: goto_state.h:47
Central data structure: state.
goto_programt::const_targett saved_target
std::stack< bool > record_events
NODISCARD renamedt< exprt, level > rename(exprt expr, const namespacet &ns)
Rewrites symbol expressions in exprt, applying a suffix to each symbol reflecting its most recent ver...
call_stackt & call_stack()
symbol_tablet symbol_table
contains symbols that are minted during symbolic execution, such as dynamically created objects etc.
symex_targett::sourcet source
bool has_saved_jump_target
This state is saved, with the PC pointing to the target of a GOTO.
std::vector< threadt > threads
bool has_saved_next_instruction
This state is saved, with the PC pointing to the next instruction of a GOTO.
void try_filter_value_sets(goto_symex_statet &state, exprt condition, const value_sett &original_value_set, value_sett *jump_taken_value_set, value_sett *jump_not_taken_value_set, const namespacet &ns)
Try to filter value sets based on whether possible values of a pointer-typed symbol make the conditio...
Definition: symex_main.cpp:784
void rewrite_quantifiers(exprt &, statet &)
Definition: symex_main.cpp:254
virtual void symex_assume(statet &state, const exprt &cond)
Symbolically execute an ASSUME instruction or simulate such an execution for a synthetic assumption.
Definition: symex_main.cpp:202
void symex_threaded_step(statet &state, const get_goto_functiont &get_goto_function)
Invokes symex_step and verifies whether additional threads can be executed.
Definition: symex_main.cpp:301
void symex_unreachable_goto(statet &state)
Symbolically execute a GOTO instruction in the context of unreachable code.
Definition: symex_goto.cpp:520
complexity_limitert complexity_module
Definition: goto_symex.h:823
virtual void symex_atomic_begin(statet &state)
Symbolically execute an ATOMIC_BEGIN instruction.
irep_idt language_mode
language_mode: ID_java, ID_C or another language identifier if we know the source language in use,...
Definition: goto_symex.h:239
virtual void vcc(const exprt &, const std::string &msg, statet &)
Definition: symex_main.cpp:185
bool ignore_assertions
If this flag is set to true then assertions will be temporarily ignored by the symbolic executions.
Definition: goto_symex.h:169
static get_goto_functiont get_goto_function(abstract_goto_modelt &goto_model)
Return a function to get/load a goto function from the given goto model Create a default delegate to ...
Definition: symex_main.cpp:493
virtual void symex_step(const get_goto_functiont &get_goto_function, statet &state)
Called for each step in the symbolic execution This calls print_symex_step to print symex's current i...
Definition: symex_main.cpp:592
virtual void symex_goto(statet &state)
Symbolically execute a GOTO instruction.
Definition: symex_goto.cpp:215
virtual void symex_decl(statet &state)
Symbolically execute a DECL instruction.
Definition: symex_decl.cpp:16
void symex_catch(statet &state)
Symbolically execute a CATCH instruction.
Definition: symex_catch.cpp:14
path_storaget & path_storage
Symbolic execution paths to be resumed later.
Definition: goto_symex.h:797
std::unique_ptr< statet > initialize_entry_point_state(const get_goto_functiont &get_goto_function)
Initialize the symbolic execution and the given state with the beginning of the entry point function.
Definition: symex_main.cpp:404
unsigned _total_vccs
Definition: goto_symex.h:820
symex_target_equationt & target
The equation that this execution is building up.
Definition: goto_symex.h:264
guard_managert & guard_manager
Used to create guards.
Definition: goto_symex.h:261
void symex_assert(const goto_programt::instructiont &, statet &)
Definition: symex_main.cpp:157
exprt clean_expr(exprt expr, statet &state, bool write)
Clean up an expression.
virtual void initialize_path_storage_from_entry_point_of(const get_goto_functiont &get_goto_function, symbol_tablet &new_symbol_table)
Puts the initial state of the entry point function into the path storage.
Definition: symex_main.cpp:479
virtual void resume_symex_from_saved_state(const get_goto_functiont &get_goto_function, const statet &saved_state, symex_target_equationt *saved_equation, symbol_tablet &new_symbol_table)
Performs symbolic execution using a state and equation that have already been used to symbolically ex...
Definition: symex_main.cpp:382
virtual void symex_dead(statet &state)
Symbolically execute a DEAD instruction.
Definition: symex_dead.cpp:19
const symbol_tablet & outer_symbol_table
The symbol table associated with the goto-program being executed.
Definition: goto_symex.h:247
virtual void symex_with_state(statet &state, const get_goto_functiont &get_goto_functions, symbol_tablet &new_symbol_table)
Symbolically execute the entire program starting from entry point.
Definition: symex_main.cpp:325
std::size_t path_segment_vccs
Number of VCCs generated during the run of this goto_symext object.
Definition: goto_symex.h:809
virtual void symex_start_thread(statet &state)
Symbolically execute a START_THREAD instruction.
virtual void symex_from_entry_point_of(const get_goto_functiont &get_goto_function, symbol_tablet &new_symbol_table)
Symbolically execute the entire program starting from entry point.
Definition: symex_main.cpp:470
namespacet ns
Initialized just before symbolic execution begins, to point to both outer_symbol_table and the symbol...
Definition: goto_symex.h:256
void kill_instruction_local_symbols(statet &state)
Kills any variables in instruction_local_symbols (these are currently always let-bound variables defi...
Definition: symex_main.cpp:748
virtual void symex_end_of_function(statet &)
Symbolically execute a END_FUNCTION instruction.
void symex_assign(statet &state, const exprt &lhs, const exprt &rhs)
Symbolically execute an ASSIGN instruction or simulate such an execution for a synthetic assignment.
Definition: goto_symex.cpp:39
void print_symex_step(statet &state)
Prints the route of symex as it walks through the code.
Definition: symex_main.cpp:510
virtual void symex_function_call(const get_goto_functiont &get_goto_function, statet &state, const code_function_callt &code)
Symbolically execute a FUNCTION_CALL instruction.
void symex_throw(statet &state)
Symbolically execute a THROW instruction.
Definition: symex_throw.cpp:14
virtual void do_simplify(exprt &expr)
Definition: goto_symex.cpp:33
unsigned _remaining_vccs
Definition: goto_symex.h:820
virtual void symex_other(statet &state)
Symbolically execute an OTHER instruction.
Definition: symex_other.cpp:74
std::function< const goto_functionst::goto_functiont &(const irep_idt &)> get_goto_functiont
The type of delegate functions that retrieve a goto_functiont for a particular function identifier.
Definition: goto_symex.h:95
messaget log
The messaget to write log messages to.
Definition: goto_symex.h:276
const symex_configt symex_config
The configuration to use for this symbolic execution.
Definition: goto_symex.h:183
bool should_pause_symex
Set when states are pushed onto the workqueue If this flag is set at the end of a symbolic execution ...
Definition: goto_symex.h:165
void symex_assume_l2(statet &, const exprt &cond)
Definition: symex_main.cpp:222
void merge_gotos(statet &state)
Merge all branches joining at the current program point.
Definition: symex_goto.cpp:599
messaget::mstreamt & print_callstack_entry(const symex_targett::sourcet &target)
Definition: symex_main.cpp:502
std::vector< symbol_exprt > instruction_local_symbols
Variables that should be killed at the end of the current symex_step invocation.
Definition: goto_symex.h:273
virtual void symex_atomic_end(statet &state)
Symbolically execute an ATOMIC_END instruction.
void execute_next_instruction(const get_goto_functiont &get_goto_function, statet &state)
Executes the instruction state.source.pc Case-switches over the type of the instruction being execute...
Definition: symex_main.cpp:602
void add(const exprt &expr)
Definition: guard_expr.cpp:40
exprt as_expr() const
Definition: guard_expr.h:49
bool is_false() const
Definition: guard_expr.h:68
exprt guard_expr(exprt expr) const
Return guard => dest or a simplified variant thereof if either guard or dest are trivial.
Definition: guard_expr.cpp:20
void populate_dirty_for_function(const irep_idt &id, const goto_functionst::goto_functiont &function)
Analyse the given function with dirtyt if it hasn't been seen before.
Definition: dirty.cpp:78
const irep_idt & id() const
Definition: irep.h:407
bool is_nil() const
Definition: irep.h:387
A very simple, cheap analysis to determine when dereference operations are trivially guarded by a che...
mstreamt & status() const
Definition: message.h:414
static eomt eom
Definition: message.h:297
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:92
const symbol_table_baset & get_symbol_table() const
Return first symbol table registered with the namespace.
Definition: namespace.h:124
incremental_dirtyt dirty
Local variables are considered 'dirty' if they've had an address taken and therefore may be referred ...
Definition: path_storage.h:116
std::shared_ptr< lexical_loopst > get_loop_analysis(const irep_idt &function_id)
Definition: path_storage.h:131
std::unordered_map< irep_idt, local_safe_pointerst > safe_pointers
Map function identifiers to local_safe_pointerst instances.
Definition: path_storage.h:100
virtual void push(const patht &)=0
Add a path to resume to the storage.
void add_function_loops(const irep_idt &identifier, const goto_programt &body)
Generates a loop analysis for the instructions in goto_programt and keys it against function ID.
Definition: path_storage.h:120
The pointer type These are both 'bitvector_typet' (they have a width) and 'type_with_subtypet' (they ...
Definition: std_types.h:1495
const irep_idt & get_comment() const
const exprt & get_original_expr() const
Definition: ssa_expr.h:33
Expression to hold a symbol (variable)
Definition: std_expr.h:81
The symbol table.
Definition: symbol_table.h:20
Inheriting the interface of symex_targett this class represents the SSA form of the input program as ...
virtual void assumption(const exprt &guard, const exprt &cond, const sourcet &source)
Record an assumption.
virtual void assertion(const exprt &guard, const exprt &cond, const std::string &msg, const sourcet &source)
Record an assertion.
virtual void location(const exprt &guard, const sourcet &source)
Record a location.
void insert(const symbol_exprt &old_expr, const exprt &new_expr)
Thrown when we encounter an instruction, parameters to an instruction etc.
Return value for build_reference_to; see that method for documentation.
static valuet build_reference_to(const exprt &what, const exprt &pointer, const namespacet &ns)
static bool should_ignore_value(const exprt &what, bool exclude_null_derefs, const irep_idt &language_mode)
Determine whether possible alias what should be ignored when replacing a pointer by its referees.
State type in value_set_domaint, used in value-set analysis and goto-symex.
Definition: value_set.h:45
optionalt< irep_idt > get_index_of_symbol(irep_idt identifier, const typet &type, const std::string &suffix, const namespacet &ns) const
Get the index of the symbol and suffix.
Definition: value_set.cpp:396
std::vector< exprt > get_value_set(exprt expr, const namespacet &ns) const
Gets values pointed to by expr, including following dereference operators (i.e.
Definition: value_set.cpp:352
void erase_values_from_entry(const irep_idt &index, const std::unordered_set< exprt, irep_hash > &values_to_erase)
Update the entry stored at index by erasing any values listed in values_to_erase.
Definition: value_set.cpp:1643
complexity_violationt
What sort of symex-complexity violation has taken place.
Forward depth-first search iterators These iterators' copy operations are expensive,...
bool has_subexpr(const exprt &expr, const std::function< bool(const exprt &)> &pred)
returns true if the expression has a subexpression that satisfies pred
Definition: expr_util.cpp:140
Deprecated expression utility functions.
static format_containert< T > format(const T &o)
Definition: format.h:37
@ FUNCTION_CALL
Definition: goto_program.h:50
@ ATOMIC_END
Definition: goto_program.h:45
@ DEAD
Definition: goto_program.h:49
@ END_FUNCTION
Definition: goto_program.h:43
@ RETURN
Definition: goto_program.h:46
@ ASSIGN
Definition: goto_program.h:47
@ ASSERT
Definition: goto_program.h:37
@ ATOMIC_BEGIN
Definition: goto_program.h:44
@ CATCH
Definition: goto_program.h:52
@ END_THREAD
Definition: goto_program.h:41
@ SKIP
Definition: goto_program.h:39
@ NO_INSTRUCTION_TYPE
Definition: goto_program.h:34
@ START_THREAD
Definition: goto_program.h:40
@ THROW
Definition: goto_program.h:51
@ DECL
Definition: goto_program.h:48
@ OTHER
Definition: goto_program.h:38
@ GOTO
Definition: goto_program.h:35
@ INCOMPLETE_GOTO
Definition: goto_program.h:53
@ ASSUME
Definition: goto_program.h:36
Symbolic Execution.
const std::string & id2string(const irep_idt &d)
Definition: irep.h:49
constexpr std::size_t DEFAULT_MAX_FIELD_SENSITIVITY_ARRAY_SIZE
Limit the size of arrays for which field_sensitivity gets applied.
Definition: magic.h:21
API to expression classes for 'mathematical' expressions.
const quantifier_exprt & to_quantifier_expr(const exprt &expr)
Cast an exprt to a quantifier_exprt.
nonstd::optional< T > optionalt
Definition: optional.h:35
@ L1
Definition: renamed.h:18
#define CHECK_RETURN(CONDITION)
Definition: invariant.h:496
#define UNREACHABLE
This should be used to mark dead code.
Definition: invariant.h:504
#define DATA_INVARIANT(CONDITION, REASON)
This condition should be used to document that assumptions that are made on goto_functions,...
Definition: invariant.h:511
#define PRECONDITION(CONDITION)
Definition: invariant.h:464
const ssa_exprt & to_ssa_expr(const exprt &expr)
Cast a generic exprt to an ssa_exprt.
Definition: ssa_expr.h:145
const code_function_callt & to_code_function_call(const codet &code)
Definition: std_code.h:1326
API to expression classes.
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
Definition: std_expr.h:190
Pre-defined types.
bool can_cast_type< pointer_typet >(const typet &type)
Check whether a reference to a typet is a pointer_typet.
Definition: std_types.h:1520
const pointer_typet & to_pointer_type(const typet &type)
Cast a typet to a pointer_typet.
Definition: std_types.h:1533
int unsafe_string2int(const std::string &str, int base)
Definition: string2int.cpp:33
Stack frames – these are used for function calls and for exceptions.
Definition: frame.h:21
std::vector< active_loop_infot > active_loops
Definition: frame.h:69
std::unordered_map< irep_idt, loop_infot > loop_iterations
Definition: frame.h:71
goto_programt::const_targett end_of_function
Definition: frame.h:32
std::shared_ptr< lexical_loopst > loops_info
Definition: frame.h:68
Information saved at a conditional goto to resume execution.
Definition: path_storage.h:42
goto_symex_statet state
Definition: path_storage.h:44
bool complexity_limits_active
Whether this run of symex is under complexity limits.
Definition: symex_config.h:56
unsigned max_depth
The maximum depth to take the execution to.
Definition: symex_config.h:21
std::size_t max_field_sensitivity_array_size
Maximum sizes for which field sensitivity will be applied to array cells.
Definition: symex_config.h:52
bool run_validation_checks
Should the additional validation checks be run? If this flag is set the checks for renaming (both lev...
Definition: symex_config.h:44
bool show_symex_steps
Prints out the path that symex is actively taking during execution, includes diagnostic information a...
Definition: symex_config.h:48
symex_configt(const optionst &options)
Construct a symex_configt using options specified in an optionst.
Definition: symex_main.cpp:34
bool doing_path_exploration
Definition: symex_config.h:23
Identifies source in the context of symbolic execution.
Definition: symex_target.h:38
goto_programt::const_targett pc
Definition: symex_target.h:43
Author: Diffblue Ltd.
void symex_transition(goto_symext::statet &state, goto_programt::const_targett to, bool is_backwards_goto)
Definition: symex_main.cpp:79
static void switch_to_thread(goto_symex_statet &state, const unsigned int thread_nb)
Definition: symex_main.cpp:282
static void pop_exited_loops(const goto_programt::const_targett &to, std::vector< framet::active_loop_infot > &active_loops)
If 'to' is not an instruction in our currently top-most active loop, pop and re-check until we find a...
Definition: symex_main.cpp:66
static optionalt< symbol_exprt > find_unique_pointer_typed_symbol(const exprt &expr)
Check if an expression only contains one unique symbol (possibly repeated multiple times)
Definition: symex_main.cpp:761
Pointer Dereferencing.