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 
56  jump_taken_state.apply_condition(new_guard, current_state, ns);
57 
58  // Could use not_exprt + simplify, but let's avoid paying that cost on quite
59  // a hot path:
60  const exprt negated_new_guard = boolean_negate(new_guard);
61  jump_not_taken_state.apply_condition(negated_new_guard, current_state, ns);
62 }
63 
76  const irep_idt &operation,
77  const symbol_exprt &symbol_expr,
78  const exprt &other_operand,
79  const value_sett &value_set,
80  const irep_idt language_mode,
81  const namespacet &ns)
82 {
83  const constant_exprt *constant_expr =
84  expr_try_dynamic_cast<constant_exprt>(other_operand);
85  const exprt other_without_typecast = skip_typecast(other_operand);
86 
87  if(
88  other_without_typecast.id() != ID_address_of &&
89  (!constant_expr || constant_expr->get_value() != ID_NULL))
90  {
91  return {};
92  }
93 
94  const ssa_exprt *ssa_symbol_expr =
95  expr_try_dynamic_cast<ssa_exprt>(symbol_expr);
96 
97  ssa_exprt l1_expr{*ssa_symbol_expr};
98  l1_expr.remove_level_2();
99  const std::vector<exprt> value_set_elements =
100  value_set.get_value_set(l1_expr, ns);
101 
102  bool constant_found = false;
103 
104  for(const auto &value_set_element : value_set_elements)
105  {
106  if(
107  value_set_element.id() == ID_unknown ||
108  value_set_element.id() == ID_invalid ||
110  to_object_descriptor_expr(value_set_element).root_object()) ||
111  to_object_descriptor_expr(value_set_element).offset().id() == ID_unknown)
112  {
113  // We can't conclude anything about the value-set
114  return {};
115  }
116 
117  if(!constant_found)
118  {
120  value_set_element, false, language_mode))
121  {
122  continue;
123  }
124 
127  value_set_element, symbol_expr, ns);
128 
129  if(skip_typecast(value.pointer) == other_without_typecast)
130  {
131  constant_found = true;
132  // We can't break because we have to make sure we find any instances of
133  // ID_unknown or ID_invalid
134  }
135  }
136  }
137 
138  if(!constant_found)
139  {
140  // The symbol cannot possibly have the value \p other_operand because it
141  // isn't in the symbol's value-set
142  return operation == ID_equal ? make_renamed<L2>(false_exprt{})
143  : make_renamed<L2>(true_exprt{});
144  }
145  else if(value_set_elements.size() == 1)
146  {
147  // The symbol must have the value \p other_operand because it is the only
148  // thing in the symbol's value-set
149  return operation == ID_equal ? make_renamed<L2>(true_exprt{})
150  : make_renamed<L2>(false_exprt{});
151  }
152  else
153  {
154  return {};
155  }
156 }
157 
166  const renamedt<exprt, L2> &renamed_expr,
167  const value_sett &value_set,
168  const irep_idt &language_mode,
169  const namespacet &ns)
170 {
171  const exprt &expr = renamed_expr.get();
172 
173  if(expr.id() != ID_equal && expr.id() != ID_notequal)
174  return {};
175 
176  if(!can_cast_type<pointer_typet>(to_binary_expr(expr).op0().type()))
177  return {};
178 
179  exprt lhs = to_binary_expr(expr).op0(), rhs = to_binary_expr(expr).op1();
181  std::swap(lhs, rhs);
182 
183  const symbol_exprt *symbol_expr_lhs =
184  expr_try_dynamic_cast<symbol_exprt>(lhs);
185 
186  if(!symbol_expr_lhs)
187  return {};
188 
189  if(!goto_symex_is_constantt()(rhs))
190  return {};
191 
193  expr.id(), *symbol_expr_lhs, rhs, value_set, language_mode, ns);
194 }
195 
197  renamedt<exprt, L2> condition,
198  const value_sett &value_set,
199  const irep_idt &language_mode,
200  const namespacet &ns)
201 {
203  condition,
204  [&value_set, &language_mode, &ns](const renamedt<exprt, L2> &expr) {
206  expr, value_set, language_mode, ns);
207  });
208 
209  return condition;
210 }
211 
213 {
214  PRECONDITION(state.reachable);
215 
216  const goto_programt::instructiont &instruction=*state.source.pc;
217 
218  exprt new_guard = clean_expr(instruction.get_condition(), state, false);
219 
220  renamedt<exprt, L2> renamed_guard = state.rename(std::move(new_guard), ns);
221  renamed_guard = try_evaluate_pointer_comparisons(
222  std::move(renamed_guard), state.value_set, language_mode, ns);
224  renamed_guard.simplify(ns);
225  new_guard = renamed_guard.get();
226 
227  if(new_guard.is_false())
228  {
229  target.location(state.guard.as_expr(), state.source);
230 
231  // next instruction
232  symex_transition(state);
233  return; // nothing to do
234  }
235 
236  target.goto_instruction(state.guard.as_expr(), renamed_guard, state.source);
237 
239  !instruction.targets.empty(), "goto should have at least one target");
240 
241  // we only do deterministic gotos for now
243  instruction.targets.size() == 1, "no support for non-deterministic gotos");
244 
245  goto_programt::const_targett goto_target=
246  instruction.get_target();
247 
248  const bool backward = instruction.is_backwards_goto();
249 
250  if(backward)
251  {
252  // is it label: goto label; or while(cond); - popular in SV-COMP
253  if(
255  (goto_target == state.source.pc ||
256  (instruction.incoming_edges.size() == 1 &&
257  *instruction.incoming_edges.begin() == goto_target)))
258  {
259  // generate assume(false) or a suitable negation if this
260  // instruction is a conditional goto
261  if(new_guard.is_true())
262  symex_assume_l2(state, false_exprt());
263  else
264  symex_assume_l2(state, not_exprt(new_guard));
265 
266  // next instruction
267  symex_transition(state);
268  return;
269  }
270 
271  const auto loop_id =
273 
274  unsigned &unwind = state.call_stack().top().loop_iterations[loop_id].count;
275  unwind++;
276 
277  if(should_stop_unwind(state.source, state.call_stack(), unwind))
278  {
279  // we break the loop
280  loop_bound_exceeded(state, new_guard);
281 
282  // next instruction
283  symex_transition(state);
284  return;
285  }
286 
287  if(new_guard.is_true())
288  {
289  // we continue executing the loop
290  if(check_break(loop_id, unwind))
291  {
292  should_pause_symex = true;
293  }
294  symex_transition(state, goto_target, true);
295  return; // nothing else to do
296  }
297  }
298 
299  // No point executing both branches of an unconditional goto.
300  if(
301  new_guard.is_true() && // We have an unconditional goto, AND
302  // either there are no reachable blocks between us and the target in the
303  // surrounding scope (because state.guard == true implies there is no path
304  // around this GOTO instruction)
305  (state.guard.is_true() ||
306  // or there is another block, but we're doing path exploration so
307  // we're going to skip over it for now and return to it later.
309  {
311  instruction.targets.size() > 0,
312  "Instruction is an unconditional goto with no target: " +
313  instruction.code.pretty());
314  symex_transition(state, instruction.get_target(), true);
315  return;
316  }
317 
318  goto_programt::const_targett new_state_pc, state_pc;
319  symex_targett::sourcet original_source=state.source;
320 
321  if(!backward)
322  {
323  new_state_pc=goto_target;
324  state_pc=state.source.pc;
325  state_pc++;
326 
327  // skip dead instructions
328  if(new_guard.is_true())
329  while(state_pc!=goto_target && !state_pc->is_target())
330  ++state_pc;
331 
332  if(state_pc==goto_target)
333  {
334  symex_transition(state, goto_target, false);
335  return; // nothing else to do
336  }
337  }
338  else
339  {
340  new_state_pc=state.source.pc;
341  new_state_pc++;
342  state_pc=goto_target;
343  }
344 
345  // Normally the next instruction to execute would be state_pc and we save
346  // new_state_pc for later. But if we're executing from a saved state, then
347  // new_state_pc should be the state that we saved from earlier, so let's
348  // execute that instead.
349  if(state.has_saved_jump_target)
350  {
351  INVARIANT(
352  new_state_pc == state.saved_target,
353  "Tried to explore the other path of a branch, but the next "
354  "instruction along that path is not the same as the instruction "
355  "that we saved at the branch point. Saved instruction is " +
356  state.saved_target->code.pretty() +
357  "\nwe were intending "
358  "to explore " +
359  new_state_pc->code.pretty() +
360  "\nthe "
361  "instruction we think we saw on a previous path exploration is " +
362  state_pc->code.pretty());
363  goto_programt::const_targett tmp = new_state_pc;
364  new_state_pc = state_pc;
365  state_pc = tmp;
366 
367  log.debug() << "Resuming from jump target '" << state_pc->source_location
368  << "'" << log.eom;
369  }
370  else if(state.has_saved_next_instruction)
371  {
372  log.debug() << "Resuming from next instruction '"
373  << state_pc->source_location << "'" << log.eom;
374  }
376  {
377  // We should save both the instruction after this goto, and the target of
378  // the goto.
379 
380  path_storaget::patht next_instruction(target, state);
381  next_instruction.state.saved_target = state_pc;
382  next_instruction.state.has_saved_next_instruction = true;
383 
384  path_storaget::patht jump_target(target, state);
385  jump_target.state.saved_target = new_state_pc;
386  jump_target.state.has_saved_jump_target = true;
387  // `forward` tells us where the branch we're _currently_ executing is
388  // pointing to; this needs to be inverted for the branch that we're saving,
389  // so let its truth value for `backwards` be the same as ours for `forward`.
390 
391  log.debug() << "Saving next instruction '"
392  << next_instruction.state.saved_target->source_location << "'"
393  << log.eom;
394  log.debug() << "Saving jump target '"
395  << jump_target.state.saved_target->source_location << "'"
396  << log.eom;
397  path_storage.push(next_instruction);
398  path_storage.push(jump_target);
399 
400  // It is now up to the caller of symex to decide which path to continue
401  // executing. Signal to the caller that states have been pushed (therefore
402  // symex has not yet completed and must be resumed), and bail out.
403  should_pause_symex = true;
404  return;
405  }
406 
407  // put a copy of the current state into the state-queue, to be used by
408  // merge_gotos when we visit new_state_pc
409  framet::goto_state_listt &goto_state_list =
410  state.call_stack().top().goto_state_map[new_state_pc];
411 
412  // On an unconditional GOTO we don't need our state, as it will be overwritten
413  // by merge_goto. Therefore we move it onto goto_state_list instead of copying
414  // as usual.
415  if(new_guard.is_true())
416  {
417  // The move here only moves goto_statet, the base class of goto_symex_statet
418  // and not the entire thing.
419  goto_state_list.emplace_back(state.source, std::move(state));
420 
421  symex_transition(state, state_pc, backward);
422 
424  state.reachable = false;
425  }
426  else
427  {
428  goto_state_list.emplace_back(state.source, state);
429 
430  symex_transition(state, state_pc, backward);
431 
433  {
434  // This doesn't work for --paths (single-path mode) yet, as in multi-path
435  // mode we remove the implied constants at a control-flow merge, but in
436  // single-path mode we don't run merge_gotos.
437  auto &taken_state = backward ? state : goto_state_list.back().second;
438  auto &not_taken_state = backward ? goto_state_list.back().second : state;
439 
441  state,
442  taken_state,
443  not_taken_state,
444  instruction.get_condition(),
445  new_guard,
446  ns);
447  }
448 
449  // produce new guard symbol
450  exprt guard_expr;
451 
452  if(
453  new_guard.id() == ID_symbol ||
454  (new_guard.id() == ID_not &&
455  to_not_expr(new_guard).op().id() == ID_symbol))
456  {
457  guard_expr=new_guard;
458  }
459  else
460  {
461  symbol_exprt guard_symbol_expr =
463  exprt new_rhs = boolean_negate(new_guard);
464 
465  ssa_exprt new_lhs =
466  state.rename_ssa<L1>(ssa_exprt{guard_symbol_expr}, ns).get();
467  new_lhs =
468  state.assignment(std::move(new_lhs), new_rhs, ns, true, false).get();
469 
470  guardt guard{true_exprt{}, guard_manager};
471 
473  log.debug(),
474  [this, &new_lhs](messaget::mstreamt &mstream) {
475  mstream << "Assignment to " << new_lhs.get_identifier()
476  << " [" << pointer_offset_bits(new_lhs.type(), ns).value_or(0) << " bits]"
477  << messaget::eom;
478  });
479 
481  guard.as_expr(),
482  new_lhs, new_lhs, guard_symbol_expr,
483  new_rhs,
484  original_source,
486 
487  guard_expr = state.rename(boolean_negate(guard_symbol_expr), ns).get();
488  }
489 
490  if(state.has_saved_jump_target)
491  {
492  if(!backward)
493  state.guard.add(guard_expr);
494  else
495  state.guard.add(boolean_negate(guard_expr));
496  }
497  else
498  {
499  goto_statet &new_state = goto_state_list.back().second;
500  if(!backward)
501  {
502  new_state.guard.add(guard_expr);
503  state.guard.add(boolean_negate(guard_expr));
504  }
505  else
506  {
507  state.guard.add(guard_expr);
508  new_state.guard.add(boolean_negate(guard_expr));
509  }
510  }
511  }
512 }
513 
515 {
516  PRECONDITION(!state.reachable);
517  // This is like symex_goto, except the state is unreachable. We try to take
518  // some arbitrary choice that maintains the state guard in a reasonable state
519  // in order that it simplifies properly when states are merged (in
520  // merge_gotos). Note we can't try to evaluate the actual GOTO guard because
521  // our constant propagator might be out of date, since we haven't been
522  // executing assign instructions.
523 
524  // If the guard is already false then there's no value in this state; just
525  // carry on and check the next instruction.
526  if(state.guard.is_false())
527  {
528  symex_transition(state);
529  return;
530  }
531 
532  const goto_programt::instructiont &instruction = *state.source.pc;
533  PRECONDITION(instruction.is_goto());
534  goto_programt::const_targett goto_target = instruction.get_target();
535 
536  auto queue_unreachable_state_at_target = [&]() {
537  framet::goto_state_listt &goto_state_list =
538  state.call_stack().top().goto_state_map[goto_target];
539  goto_statet new_state(state.guard_manager);
540  new_state.guard = state.guard;
541  new_state.reachable = false;
542  goto_state_list.emplace_back(state.source, std::move(new_state));
543  };
544 
545  if(instruction.get_condition().is_true())
546  {
547  if(instruction.is_backwards_goto())
548  {
549  // Give up trying to salvage the guard
550  // (this state's guard is squashed, without queueing it at the target)
551  }
552  else
553  {
554  // Take the branch:
555  queue_unreachable_state_at_target();
556  }
557 
558  state.guard.add(false_exprt());
559  }
560  else
561  {
562  // Arbitrarily assume a conditional branch is not-taken, except for when
563  // there's an incoming backedge, when we guess that the taken case is less
564  // likely to lead to that backedge than the not-taken case.
565  bool maybe_loop_head = std::any_of(
566  instruction.incoming_edges.begin(),
567  instruction.incoming_edges.end(),
568  [&instruction](const goto_programt::const_targett predecessor) {
569  return predecessor->location_number > instruction.location_number;
570  });
571 
572  if(instruction.is_backwards_goto() || !maybe_loop_head)
573  {
574  // Assume branch not taken (fall through)
575  }
576  else
577  {
578  // Assume branch taken:
579  queue_unreachable_state_at_target();
580  state.guard.add(false_exprt());
581  }
582  }
583 
584  symex_transition(state);
585 }
586 
587 bool goto_symext::check_break(const irep_idt &loop_id, unsigned unwind)
588 {
589  // dummy implementation
590  return false;
591 }
592 
594 {
595  framet &frame = state.call_stack().top();
596 
597  // first, see if this is a target at all
598  auto state_map_it = frame.goto_state_map.find(state.source.pc);
599 
600  if(state_map_it==frame.goto_state_map.end())
601  return; // nothing to do
602 
603  // we need to merge
604  framet::goto_state_listt &state_list = state_map_it->second;
605 
606  for(auto list_it = state_list.rbegin(); list_it != state_list.rend();
607  ++list_it)
608  {
609  merge_goto(list_it->first, std::move(list_it->second), state);
610  }
611 
612  // clean up to save some memory
613  frame.goto_state_map.erase(state_map_it);
614 }
615 
616 static guardt
618 {
619  // adjust guard, even using guards from unreachable states. This helps to
620  // shrink the state guard if the incoming edge is from a path that was
621  // truncated by config.unwind, config.depth or an assume-false instruction.
622 
623  // Note when an unreachable state contributes its guard, merging it in is
624  // optional, since the formula already implies the unreachable guard is
625  // impossible. Therefore we only integrate it when to do so simplifies the
626  // state guard.
627 
628  // This function can trash either state's guards, since goto_state is dying
629  // and state's guard will shortly be overwritten.
630 
631  if(
632  (goto_state.reachable && state.reachable) ||
633  state.guard.disjunction_may_simplify(goto_state.guard))
634  {
635  state.guard |= goto_state.guard;
636  return std::move(state.guard);
637  }
638  else if(!state.reachable && goto_state.reachable)
639  {
640  return std::move(goto_state.guard);
641  }
642  else
643  {
644  return std::move(state.guard);
645  }
646 }
647 
649  const symex_targett::sourcet &,
650  goto_statet &&goto_state,
651  statet &state)
652 {
653  // check atomic section
654  if(state.atomic_section_id != goto_state.atomic_section_id)
656  "atomic sections differ across branches",
657  state.source.pc->source_location);
658 
659  // Merge guards. Don't write this to `state` yet because we might move
660  // goto_state over it below.
661  guardt new_guard = merge_state_guards(goto_state, state);
662 
663  // Merge constant propagator, value-set etc. only if the incoming state is
664  // reachable:
665  if(goto_state.reachable)
666  {
667  if(!state.reachable)
668  {
669  // Important to note that we're moving into our base class here.
670  // Note this overwrites state.guard, but we restore it below.
671  static_cast<goto_statet &>(state) = std::move(goto_state);
672  }
673  else
674  {
675  // do SSA phi functions
676  phi_function(goto_state, state);
677 
678  // merge value sets
679  state.value_set.make_union(goto_state.value_set);
680 
681  // adjust depth
682  state.depth = std::min(state.depth, goto_state.depth);
683  }
684  }
685 
686  // Save the new state guard
687  state.guard = std::move(new_guard);
688 }
689 
705 static void merge_names(
706  const goto_statet &goto_state,
707  goto_symext::statet &dest_state,
708  const namespacet &ns,
709  const guardt &diff_guard,
710  messaget &log,
711  const bool do_simplify,
712  symex_target_equationt &target,
713  const incremental_dirtyt &dirty,
714  const ssa_exprt &ssa,
715  const unsigned goto_count,
716  const unsigned dest_count)
717 {
718  const irep_idt l1_identifier = ssa.get_identifier();
719  const irep_idt &obj_identifier = ssa.get_object_name();
720 
721  if(obj_identifier == goto_symext::statet::guard_identifier())
722  return; // just a guard, don't bother
723 
724  if(goto_count == dest_count)
725  return; // not at all changed
726 
727  // changed - but only on a branch that is now dead, and the other branch is
728  // uninitialized/invalid
729  if(
730  (!dest_state.reachable && goto_count == 0) ||
731  (!goto_state.reachable && dest_count == 0))
732  {
733  return;
734  }
735 
736  // field sensitivity: only merge on individual fields
737  if(dest_state.field_sensitivity.is_divisible(ssa))
738  return;
739 
740  // shared variables are renamed on every access anyway, we don't need to
741  // merge anything
742  const symbolt &symbol = ns.lookup(obj_identifier);
743 
744  // shared?
745  if(
746  dest_state.atomic_section_id == 0 && dest_state.threads.size() >= 2 &&
747  (symbol.is_shared() || dirty(symbol.name)))
748  {
749  return; // no phi nodes for shared stuff
750  }
751 
752  // don't merge (thread-)locals across different threads, which
753  // may have been introduced by symex_start_thread (and will
754  // only later be removed from level2.current_names by pop_frame
755  // once the thread is executed)
756  const irep_idt level_0 = ssa.get_level_0();
757  if(!level_0.empty() && level_0 != std::to_string(dest_state.source.thread_nr))
758  return;
759 
760  exprt goto_state_rhs = ssa, dest_state_rhs = ssa;
761 
762  {
763  const auto p_it = goto_state.propagation.find(l1_identifier);
764 
765  if(p_it.has_value())
766  goto_state_rhs = *p_it;
767  else
768  to_ssa_expr(goto_state_rhs).set_level_2(goto_count);
769  }
770 
771  {
772  const auto p_it = dest_state.propagation.find(l1_identifier);
773 
774  if(p_it.has_value())
775  dest_state_rhs = *p_it;
776  else
777  to_ssa_expr(dest_state_rhs).set_level_2(dest_count);
778  }
779 
780  exprt rhs;
781 
782  // Don't add a conditional to the assignment when:
783  // 1. Either guard is false, so we can't follow that branch.
784  // 2. Either identifier is of generation zero, and so hasn't been
785  // initialized and therefore an invalid target.
786 
787  // These rules only apply to dynamic objects and locals.
788  if(!dest_state.reachable)
789  rhs = goto_state_rhs;
790  else if(!goto_state.reachable)
791  rhs = dest_state_rhs;
792  else if(goto_count == 0)
793  rhs = dest_state_rhs;
794  else if(dest_count == 0)
795  rhs = goto_state_rhs;
796  else
797  {
798  rhs = if_exprt(diff_guard.as_expr(), goto_state_rhs, dest_state_rhs);
799  if(do_simplify)
800  simplify(rhs, ns);
801  }
802 
803  dest_state.record_events.push(false);
804  const ssa_exprt new_lhs =
805  dest_state.assignment(ssa, rhs, ns, true, true).get();
806  dest_state.record_events.pop();
807 
808  log.conditional_output(
809  log.debug(), [ns, &new_lhs](messaget::mstreamt &mstream) {
810  mstream << "Assignment to " << new_lhs.get_identifier() << " ["
811  << pointer_offset_bits(new_lhs.type(), ns).value_or(0) << " bits]"
812  << messaget::eom;
813  });
814 
815  target.assignment(
816  true_exprt(),
817  new_lhs,
818  new_lhs,
819  new_lhs.get_original_expr(),
820  rhs,
821  dest_state.source,
823 }
824 
826  const goto_statet &goto_state,
827  statet &dest_state)
828 {
829  if(
830  goto_state.get_level2().current_names.empty() &&
831  dest_state.get_level2().current_names.empty())
832  return;
833 
834  guardt diff_guard = goto_state.guard;
835  // this gets the diff between the guards
836  diff_guard -= dest_state.guard;
837 
840  dest_state.get_level2().current_names, delta_view, false);
841 
842  for(const auto &delta_item : delta_view)
843  {
844  const ssa_exprt &ssa = delta_item.m.first;
845  unsigned goto_count = delta_item.m.second;
846  unsigned dest_count = !delta_item.is_in_both_maps()
847  ? 0
848  : delta_item.get_other_map_value().second;
849 
850  merge_names(
851  goto_state,
852  dest_state,
853  ns,
854  diff_guard,
855  log,
857  target,
859  ssa,
860  goto_count,
861  dest_count);
862  }
863 
864  delta_view.clear();
866  goto_state.get_level2().current_names, delta_view, false);
867 
868  for(const auto &delta_item : delta_view)
869  {
870  if(delta_item.is_in_both_maps())
871  continue;
872 
873  const ssa_exprt &ssa = delta_item.m.first;
874  unsigned goto_count = 0;
875  unsigned dest_count = delta_item.m.second;
876 
877  merge_names(
878  goto_state,
879  dest_state,
880  ns,
881  diff_guard,
882  log,
884  target,
886  ssa,
887  goto_count,
888  dest_count);
889  }
890 }
891 
893  statet &state,
894  const exprt &guard)
895 {
896  const unsigned loop_number=state.source.pc->loop_number;
897 
898  exprt negated_cond;
899 
900  if(guard.is_true())
901  negated_cond=false_exprt();
902  else
903  negated_cond=not_exprt(guard);
904 
906  {
908  {
909  // Generate VCC for unwinding assertion.
910  vcc(
911  negated_cond,
912  "unwinding assertion loop " + std::to_string(loop_number),
913  state);
914  }
915 
916  // generate unwinding assumption, unless we permit partial loops
917  symex_assume_l2(state, negated_cond);
918 
920  {
921  // add to state guard to prevent further assignments
922  state.guard.add(negated_cond);
923  }
924  }
925 }
926 
928  const symex_targett::sourcet &,
929  const call_stackt &,
930  unsigned)
931 {
932  // by default, we keep going
933  return false;
934 }
messaget
Class that provides messages with a built-in verbosity 'level'.
Definition: message.h:152
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:405
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
can_cast_expr< symbol_exprt >
bool can_cast_expr< symbol_exprt >(const exprt &base)
Definition: std_expr.h:161
value_sett::get_value_set
void get_value_set(exprt expr, value_setst::valuest &dest, const namespacet &ns) const
Gets values pointed to by expr, including following dereference operators (i.e.
Definition: value_set.cpp:359
goto_symext::symex_assume_l2
void symex_assume_l2(statet &, const exprt &cond)
Definition: symex_main.cpp:219
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:881
value_set_dereferencet::valuet
Return value for build_reference_to; see that method for documentation.
Definition: value_set_dereference.h:59
skip_typecast
const exprt & skip_typecast(const exprt &expr)
find the expression nested inside typecasts, if any
Definition: expr_util.cpp:217
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:705
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:514
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:76
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:781
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:54
irept::pretty
std::string pretty(unsigned indent=0, unsigned max_indent=0) const
Definition: irep.cpp:488
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
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:796
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:2993
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:322
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:53
goto_symext::merge_gotos
void merge_gotos(statet &state)
Merge all branches joining at the current program point.
Definition: symex_goto.cpp:593
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:37
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:306
messaget::eom
static eomt eom
Definition: message.h:283
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:92
symbol_exprt
Expression to hold a symbol (variable)
Definition: std_expr.h:82
call_stackt
Definition: call_stack.h:15
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_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:927
value_sett::make_union
bool make_union(object_mapt &dest, const object_mapt &src) const
Merges two RHS expression sets.
Definition: value_set.cpp:293
exprt::is_false
bool is_false() const
Return whether the expression is a constant representing false.
Definition: expr.cpp:101
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_statet::propagation
sharing_mapt< irep_idt, exprt > propagation
Definition: goto_state.h:63
to_binary_expr
const binary_exprt & to_binary_expr(const exprt &expr)
Cast an exprt to a binary_exprt.
Definition: std_expr.h:678
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:755
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:162
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
namespacet::lookup
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See documentation for namespace_baset::lookup().
Definition: namespace.cpp:140
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:648
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:148
goto_symext::symex_goto
virtual void symex_goto(statet &state)
Symbolically execute a GOTO instruction.
Definition: symex_goto.cpp:212
value_set_dereferencet::valuet::pointer
exprt pointer
Definition: value_set_dereference.h:62
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:31
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
goto_programt::instructiont::code
codet code
Do not read or modify directly – use get_X() instead.
Definition: goto_program.h:182
messaget::mstreamt::source_location
source_locationt source_location
Definition: message.h:244
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:111
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:127
symex_configt::unwinding_assertions
bool unwinding_assertions
Definition: symex_config.h:33
goto_symex.h
goto_statet::guard
guardt guard
Definition: goto_state.h:50
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:537
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:75
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:825
to_object_descriptor_expr
const object_descriptor_exprt & to_object_descriptor_expr(const exprt &expr)
Cast an exprt to an object_descriptor_exprt.
Definition: std_expr.h:1915
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:337
simplify
bool simplify(exprt &expr, const namespacet &ns)
Definition: simplify_expr.cpp:2693
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:93
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:418
dstringt::empty
bool empty() const
Definition: dstring.h:88
goto_symext::clean_expr
exprt clean_expr(exprt expr, statet &state, bool write)
Clean up an expression.
Definition: symex_clean_expr.cpp:229
false_exprt
The Boolean constant false.
Definition: std_expr.h:3993
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:28
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:147
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:39
goto_programt::instructiont::is_goto
bool is_goto() const
Definition: goto_program.h:458
simplify_expr.h
framet::goto_state_listt
std::list< std::pair< symex_targett::sourcet, goto_statet > > goto_state_listt
Definition: frame.h:24
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: std_types.h:1513
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:291
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:587
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:83
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:343
goto_programt::instructiont::incoming_edges
std::set< targett > incoming_edges
Definition: goto_program.h:334
to_not_expr
const not_exprt & to_not_expr(const exprt &expr)
Cast an exprt to an not_exprt.
Definition: std_expr.h:2897
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:68
goto_statet::get_level2
const symex_level2t & get_level2() const
Definition: goto_state.h:37
goto_symext::vcc
virtual void vcc(const exprt &, const std::string &msg, statet &)
Definition: symex_main.cpp:182
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:133
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:359
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:221
goto_programt::const_targett
instructionst::const_iterator const_targett
Definition: goto_program.h:580
goto_symext::loop_bound_exceeded
virtual void loop_bound_exceeded(statet &state, const exprt &guard)
Definition: symex_goto.cpp:892
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:415
framet::goto_state_map
std::map< goto_programt::const_targett, goto_state_listt > goto_state_map
Definition: frame.h:28
add_failed_symbols.h
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
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:3984
goto_programt::instructiont::get_condition
const exprt & get_condition() const
Get the condition of gotos, assume, assert.
Definition: goto_program.h:285
constant_exprt
A constant literal expression.
Definition: std_expr.h:3935
merge_state_guards
static guardt merge_state_guards(goto_statet &goto_state, goto_symex_statet &state)
Definition: symex_goto.cpp:617
binary_exprt::op1
exprt & op1()
Definition: expr.h:104
goto_programt::instructiont
This class represents an instruction in the GOTO intermediate representation.
Definition: goto_program.h:179
std_expr.h
constant_exprt::get_value
const irep_idt & get_value() const
Definition: std_expr.h:3943
goto_statet::value_set
value_sett value_set
Uses level 1 names, and is used to do dereferencing.
Definition: goto_state.h:43
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:310
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:101
validation_modet::INVARIANT
@ INVARIANT
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:196
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:2872
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:106