cprover
symex_goto.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 #include "goto_symex_is_constant.h"
14 
15 #include <algorithm>
16 
17 #include <util/exception_utils.h>
18 #include <util/expr_util.h>
19 #include <util/invariant.h>
21 #include <util/simplify_expr.h>
22 #include <util/std_expr.h>
23 
26 
28  goto_symex_statet &current_state,
29  goto_statet &jump_taken_state,
30  goto_statet &jump_not_taken_state,
31  const exprt &original_guard,
32  const exprt &new_guard,
33  const namespacet &ns)
34 {
35  // It would be better to call try_filter_value_sets after apply_condition,
36  // and pass nullptr for value sets which apply_condition successfully updated
37  // already. However, try_filter_value_sets calls rename to do constant
38  // propagation, and apply_condition can update the constant propagator. Since
39  // apply condition will never succeed on both jump_taken_state and
40  // jump_not_taken_state, it should be possible to pass a reference to an
41  // unmodified goto_statet to use for renaming. But renaming needs a
42  // goto_symex_statet, not just a goto_statet, and we only have access to one
43  // of those. A future improvement would be to split rename into two parts:
44  // one part on goto_symex_statet which is non-const and deals with
45  // l2 thread reads, and one part on goto_statet which is const and could be
46  // used in try_filter_value_sets.
47 
49  current_state,
50  original_guard,
51  jump_taken_state.value_set,
52  &jump_taken_state.value_set,
53  &jump_not_taken_state.value_set,
54  ns);
55 
57  return;
58 
59  jump_taken_state.apply_condition(new_guard, current_state, ns);
60 
61  // Could use not_exprt + simplify, but let's avoid paying that cost on quite
62  // a hot path:
63  const exprt negated_new_guard = boolean_negate(new_guard);
64  jump_not_taken_state.apply_condition(negated_new_guard, current_state, ns);
65 }
66 
79  const irep_idt &operation,
80  const symbol_exprt &symbol_expr,
81  const exprt &other_operand,
82  const value_sett &value_set,
83  const irep_idt language_mode,
84  const namespacet &ns)
85 {
86  const constant_exprt *constant_expr =
87  expr_try_dynamic_cast<constant_exprt>(other_operand);
88 
89  if(
90  skip_typecast(other_operand).id() != ID_address_of &&
91  (!constant_expr || constant_expr->get_value() != ID_NULL))
92  {
93  return {};
94  }
95 
96  const ssa_exprt *ssa_symbol_expr =
97  expr_try_dynamic_cast<ssa_exprt>(symbol_expr);
98 
99  ssa_exprt l1_expr{*ssa_symbol_expr};
100  l1_expr.remove_level_2();
101  const std::vector<exprt> value_set_elements =
102  value_set.get_value_set(l1_expr, ns);
103 
104  bool constant_found = false;
105 
106  for(const auto &value_set_element : value_set_elements)
107  {
108  if(
109  value_set_element.id() == ID_unknown ||
110  value_set_element.id() == ID_invalid ||
112  to_object_descriptor_expr(value_set_element).root_object()) ||
113  to_object_descriptor_expr(value_set_element).offset().id() == ID_unknown)
114  {
115  // We can't conclude anything about the value-set
116  return {};
117  }
118 
119  if(!constant_found)
120  {
122  value_set_element, false, language_mode))
123  {
124  continue;
125  }
126 
129  value_set_element, symbol_expr, ns);
130 
131  // use the simplifier to test equality as we need to skip over typecasts
132  // and cannot rely on canonical representations, which would permit a
133  // simple syntactic equality test
134  exprt test_equal = simplify_expr(
135  equal_exprt{
136  typecast_exprt::conditional_cast(value.pointer, other_operand.type()),
137  other_operand},
138  ns);
139  if(test_equal.is_true())
140  {
141  constant_found = true;
142  // We can't break because we have to make sure we find any instances of
143  // ID_unknown or ID_invalid
144  }
145  else if(!test_equal.is_false())
146  {
147  // We can't conclude anything about the value-set
148  return {};
149  }
150  }
151  }
152 
153  if(!constant_found)
154  {
155  // The symbol cannot possibly have the value \p other_operand because it
156  // isn't in the symbol's value-set
157  return operation == ID_equal ? make_renamed<L2>(false_exprt{})
158  : make_renamed<L2>(true_exprt{});
159  }
160  else if(value_set_elements.size() == 1)
161  {
162  // The symbol must have the value \p other_operand because it is the only
163  // thing in the symbol's value-set
164  return operation == ID_equal ? make_renamed<L2>(true_exprt{})
165  : make_renamed<L2>(false_exprt{});
166  }
167  else
168  {
169  return {};
170  }
171 }
172 
181  const renamedt<exprt, L2> &renamed_expr,
182  const value_sett &value_set,
183  const irep_idt &language_mode,
184  const namespacet &ns)
185 {
186  const exprt &expr = renamed_expr.get();
187 
188  if(expr.id() != ID_equal && expr.id() != ID_notequal)
189  return {};
190 
191  if(!can_cast_type<pointer_typet>(to_binary_expr(expr).op0().type()))
192  return {};
193 
194  exprt lhs = to_binary_expr(expr).op0(), rhs = to_binary_expr(expr).op1();
196  std::swap(lhs, rhs);
197 
198  const symbol_exprt *symbol_expr_lhs =
199  expr_try_dynamic_cast<symbol_exprt>(lhs);
200 
201  if(!symbol_expr_lhs)
202  return {};
203 
204  if(!goto_symex_is_constantt()(rhs))
205  return {};
206 
208  expr.id(), *symbol_expr_lhs, rhs, value_set, language_mode, ns);
209 }
210 
212  renamedt<exprt, L2> condition,
213  const value_sett &value_set,
214  const irep_idt &language_mode,
215  const namespacet &ns)
216 {
218  condition,
219  [&value_set, &language_mode, &ns](const renamedt<exprt, L2> &expr) {
221  expr, value_set, language_mode, ns);
222  });
223 
224  return condition;
225 }
226 
228 {
229  PRECONDITION(state.reachable);
230 
231  const goto_programt::instructiont &instruction=*state.source.pc;
232 
233  exprt new_guard = clean_expr(instruction.get_condition(), state, false);
234 
235  renamedt<exprt, L2> renamed_guard = state.rename(std::move(new_guard), ns);
236  renamed_guard = try_evaluate_pointer_comparisons(
237  std::move(renamed_guard), state.value_set, language_mode, ns);
239  renamed_guard.simplify(ns);
240  new_guard = renamed_guard.get();
241 
242  if(new_guard.is_false())
243  {
244  target.location(state.guard.as_expr(), state.source);
245 
246  // next instruction
247  symex_transition(state);
248  return; // nothing to do
249  }
250 
251  target.goto_instruction(state.guard.as_expr(), renamed_guard, state.source);
252 
254  !instruction.targets.empty(), "goto should have at least one target");
255 
256  // we only do deterministic gotos for now
258  instruction.targets.size() == 1, "no support for non-deterministic gotos");
259 
260  goto_programt::const_targett goto_target=
261  instruction.get_target();
262 
263  const bool backward = instruction.is_backwards_goto();
264 
265  if(backward)
266  {
267  // is it label: goto label; or while(cond); - popular in SV-COMP
268  if(
270  // label: goto label; or do {} while(cond);
271  (goto_target == state.source.pc ||
272  // while(cond);
273  (instruction.incoming_edges.size() == 1 &&
274  *instruction.incoming_edges.begin() == goto_target &&
275  goto_target->is_goto() && new_guard.is_true())))
276  {
277  // generate assume(false) or a suitable negation if this
278  // instruction is a conditional goto
279  if(new_guard.is_true())
280  symex_assume_l2(state, false_exprt());
281  else
282  symex_assume_l2(state, not_exprt(new_guard));
283 
284  // next instruction
285  symex_transition(state);
286  return;
287  }
288 
289  const auto loop_id =
291 
292  unsigned &unwind = state.call_stack().top().loop_iterations[loop_id].count;
293  unwind++;
294 
295  if(should_stop_unwind(state.source, state.call_stack(), unwind))
296  {
297  // we break the loop
298  loop_bound_exceeded(state, new_guard);
299 
300  // next instruction
301  symex_transition(state);
302  return;
303  }
304 
305  if(new_guard.is_true())
306  {
307  // we continue executing the loop
308  if(check_break(loop_id, unwind))
309  {
310  should_pause_symex = true;
311  }
312  symex_transition(state, goto_target, true);
313  return; // nothing else to do
314  }
315  }
316 
317  // No point executing both branches of an unconditional goto.
318  if(
319  new_guard.is_true() && // We have an unconditional goto, AND
320  // either there are no reachable blocks between us and the target in the
321  // surrounding scope (because state.guard == true implies there is no path
322  // around this GOTO instruction)
323  (state.guard.is_true() ||
324  // or there is another block, but we're doing path exploration so
325  // we're going to skip over it for now and return to it later.
327  {
329  instruction.targets.size() > 0,
330  "Instruction is an unconditional goto with no target: " +
331  instruction.get_code().pretty());
332  symex_transition(state, instruction.get_target(), true);
333  return;
334  }
335 
336  goto_programt::const_targett new_state_pc, state_pc;
337  symex_targett::sourcet original_source=state.source;
338 
339  if(!backward)
340  {
341  new_state_pc=goto_target;
342  state_pc=state.source.pc;
343  state_pc++;
344 
345  // skip dead instructions
346  if(new_guard.is_true())
347  while(state_pc!=goto_target && !state_pc->is_target())
348  ++state_pc;
349 
350  if(state_pc==goto_target)
351  {
352  symex_transition(state, goto_target, false);
353  return; // nothing else to do
354  }
355  }
356  else
357  {
358  new_state_pc=state.source.pc;
359  new_state_pc++;
360  state_pc=goto_target;
361  }
362 
363  // Normally the next instruction to execute would be state_pc and we save
364  // new_state_pc for later. But if we're executing from a saved state, then
365  // new_state_pc should be the state that we saved from earlier, so let's
366  // execute that instead.
367  if(state.has_saved_jump_target)
368  {
369  INVARIANT(
370  new_state_pc == state.saved_target,
371  "Tried to explore the other path of a branch, but the next "
372  "instruction along that path is not the same as the instruction "
373  "that we saved at the branch point. Saved instruction is " +
374  state.saved_target->get_code().pretty() +
375  "\nwe were intending "
376  "to explore " +
377  new_state_pc->get_code().pretty() +
378  "\nthe "
379  "instruction we think we saw on a previous path exploration is " +
380  state_pc->get_code().pretty());
381  goto_programt::const_targett tmp = new_state_pc;
382  new_state_pc = state_pc;
383  state_pc = tmp;
384 
385  log.debug() << "Resuming from jump target '" << state_pc->source_location
386  << "'" << log.eom;
387  }
388  else if(state.has_saved_next_instruction)
389  {
390  log.debug() << "Resuming from next instruction '"
391  << state_pc->source_location << "'" << log.eom;
392  }
394  {
395  // We should save both the instruction after this goto, and the target of
396  // the goto.
397 
398  path_storaget::patht next_instruction(target, state);
399  next_instruction.state.saved_target = state_pc;
400  next_instruction.state.has_saved_next_instruction = true;
401 
402  path_storaget::patht jump_target(target, state);
403  jump_target.state.saved_target = new_state_pc;
404  jump_target.state.has_saved_jump_target = true;
405  // `forward` tells us where the branch we're _currently_ executing is
406  // pointing to; this needs to be inverted for the branch that we're saving,
407  // so let its truth value for `backwards` be the same as ours for `forward`.
408 
409  log.debug() << "Saving next instruction '"
410  << next_instruction.state.saved_target->source_location << "'"
411  << log.eom;
412  log.debug() << "Saving jump target '"
413  << jump_target.state.saved_target->source_location << "'"
414  << log.eom;
415  path_storage.push(next_instruction);
416  path_storage.push(jump_target);
417 
418  // It is now up to the caller of symex to decide which path to continue
419  // executing. Signal to the caller that states have been pushed (therefore
420  // symex has not yet completed and must be resumed), and bail out.
421  should_pause_symex = true;
422  return;
423  }
424 
425  // put a copy of the current state into the state-queue, to be used by
426  // merge_gotos when we visit new_state_pc
427  framet::goto_state_listt &goto_state_list =
428  state.call_stack().top().goto_state_map[new_state_pc];
429 
430  // On an unconditional GOTO we don't need our state, as it will be overwritten
431  // by merge_goto. Therefore we move it onto goto_state_list instead of copying
432  // as usual.
433  if(new_guard.is_true())
434  {
435  // The move here only moves goto_statet, the base class of goto_symex_statet
436  // and not the entire thing.
437  goto_state_list.emplace_back(state.source, std::move(state));
438 
439  symex_transition(state, state_pc, backward);
440 
442  state.reachable = false;
443  }
444  else
445  {
446  goto_state_list.emplace_back(state.source, state);
447 
448  symex_transition(state, state_pc, backward);
449 
451  {
452  // This doesn't work for --paths (single-path mode) yet, as in multi-path
453  // mode we remove the implied constants at a control-flow merge, but in
454  // single-path mode we don't run merge_gotos.
455  auto &taken_state = backward ? state : goto_state_list.back().second;
456  auto &not_taken_state = backward ? goto_state_list.back().second : state;
457 
459  state,
460  taken_state,
461  not_taken_state,
462  instruction.get_condition(),
463  new_guard,
464  ns);
465  }
466 
467  // produce new guard symbol
468  exprt guard_expr;
469 
470  if(
471  new_guard.id() == ID_symbol ||
472  (new_guard.id() == ID_not &&
473  to_not_expr(new_guard).op().id() == ID_symbol))
474  {
475  guard_expr=new_guard;
476  }
477  else
478  {
479  symbol_exprt guard_symbol_expr =
481  exprt new_rhs = boolean_negate(new_guard);
482 
483  ssa_exprt new_lhs =
484  state.rename_ssa<L1>(ssa_exprt{guard_symbol_expr}, ns).get();
485  new_lhs =
486  state.assignment(std::move(new_lhs), new_rhs, ns, true, false).get();
487 
488  guardt guard{true_exprt{}, guard_manager};
489 
491  log.debug(),
492  [this, &new_lhs](messaget::mstreamt &mstream) {
493  mstream << "Assignment to " << new_lhs.get_identifier()
494  << " [" << pointer_offset_bits(new_lhs.type(), ns).value_or(0) << " bits]"
495  << messaget::eom;
496  });
497 
499  guard.as_expr(),
500  new_lhs, new_lhs, guard_symbol_expr,
501  new_rhs,
502  original_source,
504 
505  guard_expr = state.rename(boolean_negate(guard_symbol_expr), ns).get();
506  }
507 
508  if(state.has_saved_jump_target)
509  {
510  if(!backward)
511  state.guard.add(guard_expr);
512  else
513  state.guard.add(boolean_negate(guard_expr));
514  }
515  else
516  {
517  goto_statet &new_state = goto_state_list.back().second;
518  if(!backward)
519  {
520  new_state.guard.add(guard_expr);
521  state.guard.add(boolean_negate(guard_expr));
522  }
523  else
524  {
525  state.guard.add(guard_expr);
526  new_state.guard.add(boolean_negate(guard_expr));
527  }
528  }
529  }
530 }
531 
533 {
534  PRECONDITION(!state.reachable);
535  // This is like symex_goto, except the state is unreachable. We try to take
536  // some arbitrary choice that maintains the state guard in a reasonable state
537  // in order that it simplifies properly when states are merged (in
538  // merge_gotos). Note we can't try to evaluate the actual GOTO guard because
539  // our constant propagator might be out of date, since we haven't been
540  // executing assign instructions.
541 
542  // If the guard is already false then there's no value in this state; just
543  // carry on and check the next instruction.
544  if(state.guard.is_false())
545  {
546  symex_transition(state);
547  return;
548  }
549 
550  const goto_programt::instructiont &instruction = *state.source.pc;
551  PRECONDITION(instruction.is_goto());
552  goto_programt::const_targett goto_target = instruction.get_target();
553 
554  auto queue_unreachable_state_at_target = [&]() {
555  framet::goto_state_listt &goto_state_list =
556  state.call_stack().top().goto_state_map[goto_target];
557  goto_statet new_state(state.guard_manager);
558  new_state.guard = state.guard;
559  new_state.reachable = false;
560  goto_state_list.emplace_back(state.source, std::move(new_state));
561  };
562 
563  if(instruction.get_condition().is_true())
564  {
565  if(instruction.is_backwards_goto())
566  {
567  // Give up trying to salvage the guard
568  // (this state's guard is squashed, without queueing it at the target)
569  }
570  else
571  {
572  // Take the branch:
573  queue_unreachable_state_at_target();
574  }
575 
576  state.guard.add(false_exprt());
577  }
578  else
579  {
580  // Arbitrarily assume a conditional branch is not-taken, except for when
581  // there's an incoming backedge, when we guess that the taken case is less
582  // likely to lead to that backedge than the not-taken case.
583  bool maybe_loop_head = std::any_of(
584  instruction.incoming_edges.begin(),
585  instruction.incoming_edges.end(),
586  [&instruction](const goto_programt::const_targett predecessor) {
587  return predecessor->location_number > instruction.location_number;
588  });
589 
590  if(instruction.is_backwards_goto() || !maybe_loop_head)
591  {
592  // Assume branch not taken (fall through)
593  }
594  else
595  {
596  // Assume branch taken:
597  queue_unreachable_state_at_target();
598  state.guard.add(false_exprt());
599  }
600  }
601 
602  symex_transition(state);
603 }
604 
605 bool goto_symext::check_break(const irep_idt &loop_id, unsigned unwind)
606 {
607  // dummy implementation
608  return false;
609 }
610 
612 {
613  framet &frame = state.call_stack().top();
614 
615  // first, see if this is a target at all
616  auto state_map_it = frame.goto_state_map.find(state.source.pc);
617 
618  if(state_map_it==frame.goto_state_map.end())
619  return; // nothing to do
620 
621  // we need to merge
622  framet::goto_state_listt &state_list = state_map_it->second;
623 
624  for(auto list_it = state_list.rbegin(); list_it != state_list.rend();
625  ++list_it)
626  {
627  merge_goto(list_it->first, std::move(list_it->second), state);
628  }
629 
630  // clean up to save some memory
631  frame.goto_state_map.erase(state_map_it);
632 }
633 
634 static guardt
636 {
637  // adjust guard, even using guards from unreachable states. This helps to
638  // shrink the state guard if the incoming edge is from a path that was
639  // truncated by config.unwind, config.depth or an assume-false instruction.
640 
641  // Note when an unreachable state contributes its guard, merging it in is
642  // optional, since the formula already implies the unreachable guard is
643  // impossible. Therefore we only integrate it when to do so simplifies the
644  // state guard.
645 
646  // This function can trash either state's guards, since goto_state is dying
647  // and state's guard will shortly be overwritten.
648 
649  if(
650  (goto_state.reachable && state.reachable) ||
651  state.guard.disjunction_may_simplify(goto_state.guard))
652  {
653  state.guard |= goto_state.guard;
654  return std::move(state.guard);
655  }
656  else if(!state.reachable && goto_state.reachable)
657  {
658  return std::move(goto_state.guard);
659  }
660  else
661  {
662  return std::move(state.guard);
663  }
664 }
665 
667  const symex_targett::sourcet &,
668  goto_statet &&goto_state,
669  statet &state)
670 {
671  // check atomic section
672  if(state.atomic_section_id != goto_state.atomic_section_id)
674  "atomic sections differ across branches",
675  state.source.pc->source_location);
676 
677  // Merge guards. Don't write this to `state` yet because we might move
678  // goto_state over it below.
679  guardt new_guard = merge_state_guards(goto_state, state);
680 
681  // Merge constant propagator, value-set etc. only if the incoming state is
682  // reachable:
683  if(goto_state.reachable)
684  {
685  if(!state.reachable)
686  {
687  // Important to note that we're moving into our base class here.
688  // Note this overwrites state.guard, but we restore it below.
689  static_cast<goto_statet &>(state) = std::move(goto_state);
690  }
691  else
692  {
693  // do SSA phi functions
694  phi_function(goto_state, state);
695 
696  // merge value sets
697  state.value_set.make_union(goto_state.value_set);
698 
699  // adjust depth
700  state.depth = std::min(state.depth, goto_state.depth);
701  }
702  }
703 
704  // Save the new state guard
705  state.guard = std::move(new_guard);
706 }
707 
723 static void merge_names(
724  const goto_statet &goto_state,
725  goto_symext::statet &dest_state,
726  const namespacet &ns,
727  const guardt &diff_guard,
728  messaget &log,
729  const bool do_simplify,
730  symex_target_equationt &target,
731  const incremental_dirtyt &dirty,
732  const ssa_exprt &ssa,
733  const unsigned goto_count,
734  const unsigned dest_count)
735 {
736  const irep_idt l1_identifier = ssa.get_identifier();
737  const irep_idt &obj_identifier = ssa.get_object_name();
738 
739  if(obj_identifier == goto_symext::statet::guard_identifier())
740  return; // just a guard, don't bother
741 
742  if(goto_count == dest_count)
743  return; // not at all changed
744 
745  // changed - but only on a branch that is now dead, and the other branch is
746  // uninitialized/invalid
747  if(
748  (!dest_state.reachable && goto_count == 0) ||
749  (!goto_state.reachable && dest_count == 0))
750  {
751  return;
752  }
753 
754  // field sensitivity: only merge on individual fields
755  if(dest_state.field_sensitivity.is_divisible(ssa))
756  return;
757 
758  // shared variables are renamed on every access anyway, we don't need to
759  // merge anything
760  const symbolt &symbol = ns.lookup(obj_identifier);
761 
762  // shared?
763  if(
764  dest_state.atomic_section_id == 0 && dest_state.threads.size() >= 2 &&
765  (symbol.is_shared() || dirty(symbol.name)))
766  {
767  return; // no phi nodes for shared stuff
768  }
769 
770  // don't merge (thread-)locals across different threads, which
771  // may have been introduced by symex_start_thread (and will
772  // only later be removed from level2.current_names by pop_frame
773  // once the thread is executed)
774  const irep_idt level_0 = ssa.get_level_0();
775  if(!level_0.empty() && level_0 != std::to_string(dest_state.source.thread_nr))
776  return;
777 
778  exprt goto_state_rhs = ssa, dest_state_rhs = ssa;
779 
780  {
781  const auto p_it = goto_state.propagation.find(l1_identifier);
782 
783  if(p_it.has_value())
784  goto_state_rhs = *p_it;
785  else
786  to_ssa_expr(goto_state_rhs).set_level_2(goto_count);
787  }
788 
789  {
790  const auto p_it = dest_state.propagation.find(l1_identifier);
791 
792  if(p_it.has_value())
793  dest_state_rhs = *p_it;
794  else
795  to_ssa_expr(dest_state_rhs).set_level_2(dest_count);
796  }
797 
798  exprt rhs;
799 
800  // Don't add a conditional to the assignment when:
801  // 1. Either guard is false, so we can't follow that branch.
802  // 2. Either identifier is of generation zero, and so hasn't been
803  // initialized and therefore an invalid target.
804 
805  // These rules only apply to dynamic objects and locals.
806  if(!dest_state.reachable)
807  rhs = goto_state_rhs;
808  else if(!goto_state.reachable)
809  rhs = dest_state_rhs;
810  else if(goto_count == 0)
811  rhs = dest_state_rhs;
812  else if(dest_count == 0)
813  rhs = goto_state_rhs;
814  else
815  {
816  rhs = if_exprt(diff_guard.as_expr(), goto_state_rhs, dest_state_rhs);
817  if(do_simplify)
818  simplify(rhs, ns);
819  }
820 
821  dest_state.record_events.push(false);
822  const ssa_exprt new_lhs =
823  dest_state.assignment(ssa, rhs, ns, true, true).get();
824  dest_state.record_events.pop();
825 
826  log.conditional_output(
827  log.debug(), [ns, &new_lhs](messaget::mstreamt &mstream) {
828  mstream << "Assignment to " << new_lhs.get_identifier() << " ["
829  << pointer_offset_bits(new_lhs.type(), ns).value_or(0) << " bits]"
830  << messaget::eom;
831  });
832 
833  target.assignment(
834  true_exprt(),
835  new_lhs,
836  new_lhs,
837  new_lhs.get_original_expr(),
838  rhs,
839  dest_state.source,
841 }
842 
844  const goto_statet &goto_state,
845  statet &dest_state)
846 {
847  if(
848  goto_state.get_level2().current_names.empty() &&
849  dest_state.get_level2().current_names.empty())
850  return;
851 
852  guardt diff_guard = goto_state.guard;
853  // this gets the diff between the guards
854  diff_guard -= dest_state.guard;
855 
858  dest_state.get_level2().current_names, delta_view, false);
859 
860  for(const auto &delta_item : delta_view)
861  {
862  const ssa_exprt &ssa = delta_item.m.first;
863  unsigned goto_count = delta_item.m.second;
864  unsigned dest_count = !delta_item.is_in_both_maps()
865  ? 0
866  : delta_item.get_other_map_value().second;
867 
868  merge_names(
869  goto_state,
870  dest_state,
871  ns,
872  diff_guard,
873  log,
875  target,
877  ssa,
878  goto_count,
879  dest_count);
880  }
881 
882  delta_view.clear();
884  goto_state.get_level2().current_names, delta_view, false);
885 
886  for(const auto &delta_item : delta_view)
887  {
888  if(delta_item.is_in_both_maps())
889  continue;
890 
891  const ssa_exprt &ssa = delta_item.m.first;
892  unsigned goto_count = 0;
893  unsigned dest_count = delta_item.m.second;
894 
895  merge_names(
896  goto_state,
897  dest_state,
898  ns,
899  diff_guard,
900  log,
902  target,
904  ssa,
905  goto_count,
906  dest_count);
907  }
908 }
909 
911  statet &state,
912  const exprt &guard)
913 {
914  const unsigned loop_number=state.source.pc->loop_number;
915 
916  exprt negated_cond;
917 
918  if(guard.is_true())
919  negated_cond=false_exprt();
920  else
921  negated_cond=not_exprt(guard);
922 
924  {
926  {
927  // Generate VCC for unwinding assertion.
928  vcc(
929  negated_cond,
930  "unwinding assertion loop " + std::to_string(loop_number),
931  state);
932  }
933 
934  // generate unwinding assumption, unless we permit partial loops
935  symex_assume_l2(state, negated_cond);
936 
938  {
939  // add to state guard to prevent further assignments
940  state.guard.add(negated_cond);
941  }
942  }
943 }
944 
946  const symex_targett::sourcet &,
947  const call_stackt &,
948  unsigned)
949 {
950  // by default, we keep going
951  return false;
952 }
messaget
Class that provides messages with a built-in verbosity 'level'.
Definition: message.h:155
guard_exprt
Definition: guard_expr.h:27
sharing_mapt< irep_idt, std::pair< ssa_exprt, std::size_t > >::delta_viewt
std::vector< delta_view_itemt > delta_viewt
Delta view of the key-value pairs in two maps.
Definition: sharing_map.h:439
goto_symext::clean_expr
exprt clean_expr(exprt expr, statet &state, bool write)
Clean up an expression.
Definition: symex_clean_expr.cpp:233
exception_utils.h
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:37
pointer_offset_size.h
Pointer Logic.
can_cast_expr< symbol_exprt >
bool can_cast_expr< symbol_exprt >(const exprt &base)
Definition: std_expr.h:173
typecast_exprt::conditional_cast
static exprt conditional_cast(const exprt &expr, const typet &type)
Definition: std_expr.h:1788
goto_symext::symex_assume_l2
void symex_assume_l2(statet &, const exprt &cond)
Definition: symex_main.cpp:223
symex_configt::self_loops_to_assumptions
bool self_loops_to_assumptions
Definition: symex_config.h:29
sharing_mapt::get_delta_view
void get_delta_view(const sharing_mapt &other, delta_viewt &delta_view, const bool only_common=true) const
Get a delta view of the elements in the map.
Definition: sharing_map.h:936
value_set_dereferencet::valuet
Return value for build_reference_to; see that method for documentation.
Definition: value_set_dereference.h:70
skip_typecast
const exprt & skip_typecast(const exprt &expr)
find the expression nested inside typecasts, if any
Definition: expr_util.cpp:219
symex_configt::partial_loops
bool partial_loops
Definition: symex_config.h:35
goto_statet::apply_condition
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
merge_names
static void merge_names(const goto_statet &goto_state, goto_symext::statet &dest_state, const namespacet &ns, const guardt &diff_guard, messaget &log, const bool do_simplify, symex_target_equationt &target, const incremental_dirtyt &dirty, const ssa_exprt &ssa, const unsigned goto_count, const unsigned dest_count)
Helper function for phi_function which merges the names of an identifier for two different states.
Definition: symex_goto.cpp:723
goto_symext::symex_unreachable_goto
void symex_unreachable_goto(statet &state)
Symbolically execute a GOTO instruction in the context of unreachable code.
Definition: symex_goto.cpp:532
ssa_exprt::remove_level_2
void remove_level_2()
goto_symex_statet::assignment
NODISCARD renamedt< ssa_exprt, L2 > assignment(ssa_exprt lhs, const exprt &rhs, const namespacet &ns, bool rhs_is_simplified, bool record_value, bool allow_pointer_unsoundness=false)
Definition: goto_symex_state.cpp:77
ssa_exprt::get_level_0
const irep_idt get_level_0() const
Definition: ssa_expr.h:63
L1
@ L1
Definition: renamed.h:18
goto_symext::try_filter_value_sets
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:785
goto_symex_statet::guard_identifier
static irep_idt guard_identifier()
Definition: goto_symex_state.h:141
goto_statet::reachable
bool reachable
Is this code reachable? If not we can take shortcuts such as not entering function calls,...
Definition: goto_state.h:62
irept::pretty
std::string pretty(unsigned indent=0, unsigned max_indent=0) const
Definition: irep.cpp:501
symex_targett::sourcet::pc
goto_programt::const_targett pc
Definition: symex_target.h:43
goto_symext::target
symex_target_equationt & target
The equation that this execution is building up.
Definition: goto_symex.h:264
value_set_dereference.h
Pointer Dereferencing.
goto_symex_statet::has_saved_jump_target
bool has_saved_jump_target
This state is saved, with the PC pointing to the target of a GOTO.
Definition: goto_symex_state.h:219
goto_symext::path_storage
path_storaget & path_storage
Symbolic execution paths to be resumed later.
Definition: goto_symex.h:802
goto_symex_statet
Central data structure: state.
Definition: goto_symex_state.h:46
if_exprt
The trinary if-then-else operator.
Definition: std_expr.h:2086
renamedt::simplify
void simplify(const namespacet &ns)
Definition: renamed.h:39
goto_symex_statet::guard_manager
guard_managert & guard_manager
Definition: goto_symex_state.h:72
goto_symext::guard_manager
guard_managert & guard_manager
Used to create guards.
Definition: goto_symex.h:261
symex_targett::sourcet::thread_nr
unsigned thread_nr
Definition: symex_target.h:39
invariant.h
value_set_dereferencet::should_ignore_value
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.
Definition: value_set_dereference.cpp:407
value_sett
State type in value_set_domaint, used in value-set analysis and goto-symex.
Definition: value_set.h:45
exprt
Base class for all expressions.
Definition: expr.h:54
goto_symext::merge_gotos
void merge_gotos(statet &state)
Merge all branches joining at the current program point.
Definition: symex_goto.cpp:611
ssa_exprt::get_object_name
irep_idt get_object_name() const
guard_exprt::is_true
bool is_true() const
Definition: guard_expr.h:63
goto_symex_statet::source
symex_targett::sourcet source
Definition: goto_symex_state.h:64
goto_symex_statet::rename_ssa
NODISCARD renamedt< ssa_exprt, level > rename_ssa(ssa_exprt ssa, const namespacet &ns)
Version of rename which is specialized for SSA exprt.
bool_typet
The Boolean type.
Definition: std_types.h:36
to_string
std::string to_string(const string_not_contains_constraintt &expr)
Used for debug printing.
Definition: string_constraint.cpp:55
goto_programt::instructiont::targets
targetst targets
The list of successor instructions.
Definition: goto_program.h:384
messaget::eom
static eomt eom
Definition: message.h:297
guardt
guard_exprt guardt
Definition: guard.h:27
exprt::is_true
bool is_true() const
Return whether the expression is a constant representing true.
Definition: expr.cpp:60
symbol_exprt
Expression to hold a symbol (variable)
Definition: std_expr.h:80
call_stackt
Definition: call_stack.h:15
equal_exprt
Equality.
Definition: std_expr.h:1139
ssa_exprt::get_original_expr
const exprt & get_original_expr() const
Definition: ssa_expr.h:33
renamedt::get
const underlyingt & get() const
Definition: renamed.h:34
goto_symex_is_constant.h
GOTO Symex constant propagation.
can_cast_type< pointer_typet >
bool can_cast_type< pointer_typet >(const typet &type)
Check whether a reference to a typet is a pointer_typet.
Definition: pointer_expr.h:49
goto_symext::should_stop_unwind
virtual bool should_stop_unwind(const symex_targett::sourcet &source, const call_stackt &context, unsigned unwind)
Determine whether to unwind a loop.
Definition: symex_goto.cpp:945
value_sett::make_union
bool make_union(object_mapt &dest, const object_mapt &src) const
Merges two RHS expression sets.
Definition: value_set.cpp:285
exprt::is_false
bool is_false() const
Return whether the expression is a constant representing false.
Definition: expr.cpp:69
incorrect_goto_program_exceptiont
Thrown when a goto program that's being processed is in an invalid format, for example passing the wr...
Definition: exception_utils.h:90
goto_programt::instructiont::get_code
const codet & get_code() const
Get the code represented by this instruction.
Definition: goto_program.h:187
goto_statet::propagation
sharing_mapt< irep_idt, exprt > propagation
Definition: goto_state.h:71
to_binary_expr
const binary_exprt & to_binary_expr(const exprt &expr)
Cast an exprt to a binary_exprt.
Definition: std_expr.h:627
is_failed_symbol
bool is_failed_symbol(const exprt &expr)
Return true if, and only if, expr is the result of failed dereferencing.
Definition: add_failed_symbols.h:39
goto_programt::loop_id
static irep_idt loop_id(const irep_idt &function_id, const instructiont &instruction)
Human-readable loop name.
Definition: goto_program.h:739
goto_symext::log
messaget log
The messaget to write log messages to.
Definition: goto_symex.h:276
symex_target_equationt::location
virtual void location(const exprt &guard, const sourcet &source)
Record a location.
Definition: symex_target_equation.cpp:173
to_object_descriptor_expr
const object_descriptor_exprt & to_object_descriptor_expr(const exprt &expr)
Cast an exprt to an object_descriptor_exprt.
Definition: pointer_expr.h:218
ssa_exprt
Expression providing an SSA-renamed symbol of expressions.
Definition: ssa_expr.h:17
namespacet
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:92
exprt::type
typet & type()
Return the type of the expression.
Definition: expr.h:82
namespacet::lookup
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See documentation for namespace_baset::lookup().
Definition: namespace.cpp:141
goto_symex_statet::threads
std::vector< threadt > threads
Definition: goto_symex_state.h:198
goto_symext::merge_goto
virtual void merge_goto(const symex_targett::sourcet &source, goto_statet &&goto_state, statet &state)
Merge a single branch, the symbolic state of which is held in goto_state, into the current overall sy...
Definition: symex_goto.cpp:666
goto_symex_statet::call_stack
call_stackt & call_stack()
Definition: goto_symex_state.h:147
goto_symex_statet::has_saved_next_instruction
bool has_saved_next_instruction
This state is saved, with the PC pointing to the next instruction of a GOTO.
Definition: goto_symex_state.h:223
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:511
call_stackt::top
framet & top()
Definition: call_stack.h:17
to_ssa_expr
const ssa_exprt & to_ssa_expr(const exprt &expr)
Cast a generic exprt to an ssa_exprt.
Definition: ssa_expr.h:145
goto_symext::symex_goto
virtual void symex_goto(statet &state)
Symbolically execute a GOTO instruction.
Definition: symex_goto.cpp:227
value_set_dereferencet::valuet::pointer
exprt pointer
Definition: value_set_dereference.h:73
path_storaget::push
virtual void push(const patht &)=0
Add a path to resume to the storage.
goto_statet::depth
unsigned depth
Distance from entry.
Definition: goto_state.h:35
selectively_mutate
void selectively_mutate(renamedt< exprt, level > &renamed, typename renamedt< exprt, level >::mutator_functiont get_mutated_expr)
This permits replacing subexpressions of the renamed value, so long as each replacement is consistent...
Definition: renamed.h:89
messaget::mstreamt::source_location
source_locationt source_location
Definition: message.h:247
value_sett::get_value_set
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
guard_exprt::as_expr
exprt as_expr() const
Definition: guard_expr.h:49
guard_exprt::add
void add(const exprt &expr)
Definition: guard_expr.cpp:40
PRECONDITION
#define PRECONDITION(CONDITION)
Definition: invariant.h:464
symbol_exprt::get_identifier
const irep_idt & get_identifier() const
Definition: std_expr.h:109
symex_configt::doing_path_exploration
bool doing_path_exploration
Definition: symex_config.h:23
boolean_negate
exprt boolean_negate(const exprt &src)
negate a Boolean expression, possibly removing a not_exprt, and swapping false and true
Definition: expr_util.cpp:129
symex_configt::unwinding_assertions
bool unwinding_assertions
Definition: symex_config.h:33
goto_symex.h
Symbolic Execution.
goto_statet::guard
guardt guard
Definition: goto_state.h:58
framet::loop_iterations
std::unordered_map< irep_idt, loop_infot > loop_iterations
Definition: frame.h:71
goto_programt::instructiont::is_backwards_goto
bool is_backwards_goto() const
Returns true if the instruction is a backwards branch.
Definition: goto_program.h:521
try_evaluate_pointer_comparison
static optionalt< renamedt< exprt, L2 > > try_evaluate_pointer_comparison(const irep_idt &operation, const symbol_exprt &symbol_expr, const exprt &other_operand, const value_sett &value_set, const irep_idt language_mode, const namespacet &ns)
Try to evaluate a simple pointer comparison.
Definition: symex_goto.cpp:78
goto_symext::phi_function
void phi_function(const goto_statet &goto_state, statet &dest_state)
Merge the SSA assignments from goto_state into dest_state.
Definition: symex_goto.cpp:843
symex_configt::simplify_opt
bool simplify_opt
Definition: symex_config.h:31
sharing_mapt::empty
bool empty() const
Check if map is empty.
Definition: sharing_map.h:360
simplify
bool simplify(exprt &expr, const namespacet &ns)
Definition: simplify_expr.cpp:2552
guard_exprt::disjunction_may_simplify
bool disjunction_may_simplify(const guard_exprt &other_guard)
Returns true if operator|= with other_guard may result in a simpler expression.
Definition: guard_expr.cpp:127
simplify_expr
exprt simplify_expr(exprt src, const namespacet &ns)
Definition: simplify_expr.cpp:2557
path_storaget::dirty
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
symbolt::is_shared
bool is_shared() const
Definition: symbol.h:95
irept::id
const irep_idt & id() const
Definition: irep.h:407
dstringt::empty
bool empty() const
Definition: dstring.h:88
false_exprt
The Boolean constant false.
Definition: std_expr.h:2725
framet
Stack frames – these are used for function calls and for exceptions.
Definition: frame.h:21
goto_symex_is_constantt
Definition: goto_symex_is_constant.h:19
guard_exprt::is_false
bool is_false() const
Definition: guard_expr.h:68
optionalt
nonstd::optional< T > optionalt
Definition: optional.h:35
path_storaget::patht
Information saved at a conditional goto to resume execution.
Definition: path_storage.h:42
goto_statet
Container for data that varies per program point, e.g.
Definition: goto_state.h:32
symex_transition
void symex_transition(goto_symext::statet &state)
Transition to the next instruction, which increments the internal program counter and initializes the...
Definition: symex_main.cpp:151
goto_symex_statet::field_sensitivity
field_sensitivityt field_sensitivity
Definition: goto_symex_state.h:124
symex_target_equationt
Inheriting the interface of symex_targett this class represents the SSA form of the input program as ...
Definition: symex_target_equation.h:41
goto_programt::instructiont::is_goto
bool is_goto() const
Definition: goto_program.h:442
simplify_expr.h
framet::goto_state_listt
std::list< std::pair< symex_targett::sourcet, goto_statet > > goto_state_listt
Definition: frame.h:24
goto_symext::ns
namespacet ns
Initialized just before symbolic execution begins, to point to both outer_symbol_table and the symbol...
Definition: goto_symex.h:256
expr_util.h
Deprecated expression utility functions.
symex_targett::assignment_typet::PHI
@ PHI
symex_target_equationt::goto_instruction
virtual void goto_instruction(const exprt &guard, const renamedt< exprt, L2 > &cond, const sourcet &source)
Record a goto instruction.
Definition: symex_target_equation.cpp:302
goto_symext::check_break
virtual bool check_break(const irep_idt &loop_id, unsigned unwind)
Defines condition for interrupting symbolic execution for a specific loop.
Definition: symex_goto.cpp:605
ssa_exprt::set_level_2
void set_level_2(std::size_t i)
symex_level2t::current_names
symex_renaming_levelt current_names
Definition: renaming_level.h:77
goto_symext::language_mode
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
field_sensitivityt::is_divisible
bool is_divisible(const ssa_exprt &expr) const
Determine whether expr would translate to an atomic SSA expression (returns false) or a composite obj...
Definition: field_sensitivity.cpp:349
goto_programt::instructiont::incoming_edges
std::set< targett > incoming_edges
Definition: goto_program.h:412
to_not_expr
const not_exprt & to_not_expr(const exprt &expr)
Cast an exprt to an not_exprt.
Definition: std_expr.h:2066
path_storaget::patht::state
goto_symex_statet state
Definition: path_storage.h:44
symbolt
Symbol table entry.
Definition: symbol.h:28
goto_statet::atomic_section_id
unsigned atomic_section_id
Threads.
Definition: goto_state.h:76
symex_configt::constant_propagation
bool constant_propagation
Definition: symex_config.h:27
goto_statet::get_level2
const symex_level2t & get_level2() const
Definition: goto_state.h:45
goto_symext::vcc
virtual void vcc(const exprt &, const std::string &msg, statet &)
Definition: symex_main.cpp:186
messaget::conditional_output
void conditional_output(mstreamt &mstream, const std::function< void(mstreamt &)> &output_generator) const
Generate output to message_stream using output_generator if the configured verbosity is at least as h...
Definition: message.cpp:138
goto_symext::symex_config
const symex_configt symex_config
The configuration to use for this symbolic execution.
Definition: goto_symex.h:183
value_set_dereferencet::build_reference_to
static valuet build_reference_to(const exprt &what, const exprt &pointer, const namespacet &ns)
Definition: value_set_dereference.cpp:444
goto_symex_statet::record_events
std::stack< bool > record_events
Definition: goto_symex_state.h:212
symex_targett::assignment_typet::GUARD
@ GUARD
messaget::mstreamt
Definition: message.h:224
goto_programt::const_targett
instructionst::const_iterator const_targett
Definition: goto_program.h:564
goto_symext::loop_bound_exceeded
virtual void loop_bound_exceeded(statet &state, const exprt &guard)
Definition: symex_goto.cpp:910
goto_symex_statet::rename
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...
goto_symex_statet::saved_target
goto_programt::const_targett saved_target
Definition: goto_symex_state.h:216
symex_targett::sourcet
Identifies source in the context of symbolic execution.
Definition: symex_target.h:38
messaget::debug
mstreamt & debug() const
Definition: message.h:429
framet::goto_state_map
std::map< goto_programt::const_targett, goto_state_listt > goto_state_map
Definition: frame.h:28
add_failed_symbols.h
Pointer Dereferencing.
goto_symext::apply_goto_condition
void apply_goto_condition(goto_symex_statet &current_state, goto_statet &jump_taken_state, goto_statet &jump_not_taken_state, const exprt &original_guard, const exprt &new_guard, const namespacet &ns)
Propagate constants and points-to information implied by a GOTO condition.
Definition: symex_goto.cpp:27
INVARIANT
#define INVARIANT(CONDITION, REASON)
This macro uses the wrapper function 'invariant_violated_string'.
Definition: invariant.h:424
incremental_dirtyt
Wrapper for dirtyt that permits incremental population, ensuring each function is analysed exactly on...
Definition: dirty.h:119
symex_targett::sourcet::function_id
irep_idt function_id
Definition: symex_target.h:40
true_exprt
The Boolean constant true.
Definition: std_expr.h:2716
goto_programt::instructiont::get_condition
const exprt & get_condition() const
Get the condition of gotos, assume, assert.
Definition: goto_program.h:363
constant_exprt
A constant literal expression.
Definition: std_expr.h:2667
merge_state_guards
static guardt merge_state_guards(goto_statet &goto_state, goto_symex_statet &state)
Definition: symex_goto.cpp:635
binary_exprt::op1
exprt & op1()
Definition: expr.h:106
goto_programt::instructiont
This class represents an instruction in the GOTO intermediate representation.
Definition: goto_program.h:180
std_expr.h
API to expression classes.
constant_exprt::get_value
const irep_idt & get_value() const
Definition: std_expr.h:2675
goto_statet::value_set
value_sett value_set
Uses level 1 names, and is used to do dereferencing.
Definition: goto_state.h:51
symbolt::name
irep_idt name
The unique identifier.
Definition: symbol.h:40
goto_programt::instructiont::get_target
targett get_target() const
Returns the first (and only) successor for the usual case of a single target.
Definition: goto_program.h:388
renamedt
Wrapper for expressions or types which have been renamed up to a given level.
Definition: renamed.h:27
binary_exprt::op0
exprt & op0()
Definition: expr.h:103
try_evaluate_pointer_comparisons
renamedt< exprt, L2 > try_evaluate_pointer_comparisons(renamedt< exprt, L2 > condition, const value_sett &value_set, const irep_idt &language_mode, const namespacet &ns)
Try to evaluate pointer comparisons where they can be trivially determined using the value-set.
Definition: symex_goto.cpp:211
goto_symext::should_pause_symex
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
not_exprt
Boolean negation.
Definition: std_expr.h:2041
symex_target_equationt::assignment
virtual void assignment(const exprt &guard, const ssa_exprt &ssa_lhs, const exprt &ssa_full_lhs, const exprt &original_full_lhs, const exprt &ssa_rhs, const sourcet &source, assignment_typet assignment_type)
Write to a local variable.
Definition: symex_target_equation.cpp:117