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  // label: goto label; or do {} while(cond);
256  (goto_target == state.source.pc ||
257  // while(cond);
258  (instruction.incoming_edges.size() == 1 &&
259  *instruction.incoming_edges.begin() == goto_target &&
260  goto_target->is_goto() && new_guard.is_true())))
261  {
262  // generate assume(false) or a suitable negation if this
263  // instruction is a conditional goto
264  if(new_guard.is_true())
265  symex_assume_l2(state, false_exprt());
266  else
267  symex_assume_l2(state, not_exprt(new_guard));
268 
269  // next instruction
270  symex_transition(state);
271  return;
272  }
273 
274  const auto loop_id =
276 
277  unsigned &unwind = state.call_stack().top().loop_iterations[loop_id].count;
278  unwind++;
279 
280  if(should_stop_unwind(state.source, state.call_stack(), unwind))
281  {
282  // we break the loop
283  loop_bound_exceeded(state, new_guard);
284 
285  // next instruction
286  symex_transition(state);
287  return;
288  }
289 
290  if(new_guard.is_true())
291  {
292  // we continue executing the loop
293  if(check_break(loop_id, unwind))
294  {
295  should_pause_symex = true;
296  }
297  symex_transition(state, goto_target, true);
298  return; // nothing else to do
299  }
300  }
301 
302  // No point executing both branches of an unconditional goto.
303  if(
304  new_guard.is_true() && // We have an unconditional goto, AND
305  // either there are no reachable blocks between us and the target in the
306  // surrounding scope (because state.guard == true implies there is no path
307  // around this GOTO instruction)
308  (state.guard.is_true() ||
309  // or there is another block, but we're doing path exploration so
310  // we're going to skip over it for now and return to it later.
312  {
314  instruction.targets.size() > 0,
315  "Instruction is an unconditional goto with no target: " +
316  instruction.code.pretty());
317  symex_transition(state, instruction.get_target(), true);
318  return;
319  }
320 
321  goto_programt::const_targett new_state_pc, state_pc;
322  symex_targett::sourcet original_source=state.source;
323 
324  if(!backward)
325  {
326  new_state_pc=goto_target;
327  state_pc=state.source.pc;
328  state_pc++;
329 
330  // skip dead instructions
331  if(new_guard.is_true())
332  while(state_pc!=goto_target && !state_pc->is_target())
333  ++state_pc;
334 
335  if(state_pc==goto_target)
336  {
337  symex_transition(state, goto_target, false);
338  return; // nothing else to do
339  }
340  }
341  else
342  {
343  new_state_pc=state.source.pc;
344  new_state_pc++;
345  state_pc=goto_target;
346  }
347 
348  // Normally the next instruction to execute would be state_pc and we save
349  // new_state_pc for later. But if we're executing from a saved state, then
350  // new_state_pc should be the state that we saved from earlier, so let's
351  // execute that instead.
352  if(state.has_saved_jump_target)
353  {
354  INVARIANT(
355  new_state_pc == state.saved_target,
356  "Tried to explore the other path of a branch, but the next "
357  "instruction along that path is not the same as the instruction "
358  "that we saved at the branch point. Saved instruction is " +
359  state.saved_target->code.pretty() +
360  "\nwe were intending "
361  "to explore " +
362  new_state_pc->code.pretty() +
363  "\nthe "
364  "instruction we think we saw on a previous path exploration is " +
365  state_pc->code.pretty());
366  goto_programt::const_targett tmp = new_state_pc;
367  new_state_pc = state_pc;
368  state_pc = tmp;
369 
370  log.debug() << "Resuming from jump target '" << state_pc->source_location
371  << "'" << log.eom;
372  }
373  else if(state.has_saved_next_instruction)
374  {
375  log.debug() << "Resuming from next instruction '"
376  << state_pc->source_location << "'" << log.eom;
377  }
379  {
380  // We should save both the instruction after this goto, and the target of
381  // the goto.
382 
383  path_storaget::patht next_instruction(target, state);
384  next_instruction.state.saved_target = state_pc;
385  next_instruction.state.has_saved_next_instruction = true;
386 
387  path_storaget::patht jump_target(target, state);
388  jump_target.state.saved_target = new_state_pc;
389  jump_target.state.has_saved_jump_target = true;
390  // `forward` tells us where the branch we're _currently_ executing is
391  // pointing to; this needs to be inverted for the branch that we're saving,
392  // so let its truth value for `backwards` be the same as ours for `forward`.
393 
394  log.debug() << "Saving next instruction '"
395  << next_instruction.state.saved_target->source_location << "'"
396  << log.eom;
397  log.debug() << "Saving jump target '"
398  << jump_target.state.saved_target->source_location << "'"
399  << log.eom;
400  path_storage.push(next_instruction);
401  path_storage.push(jump_target);
402 
403  // It is now up to the caller of symex to decide which path to continue
404  // executing. Signal to the caller that states have been pushed (therefore
405  // symex has not yet completed and must be resumed), and bail out.
406  should_pause_symex = true;
407  return;
408  }
409 
410  // put a copy of the current state into the state-queue, to be used by
411  // merge_gotos when we visit new_state_pc
412  framet::goto_state_listt &goto_state_list =
413  state.call_stack().top().goto_state_map[new_state_pc];
414 
415  // On an unconditional GOTO we don't need our state, as it will be overwritten
416  // by merge_goto. Therefore we move it onto goto_state_list instead of copying
417  // as usual.
418  if(new_guard.is_true())
419  {
420  // The move here only moves goto_statet, the base class of goto_symex_statet
421  // and not the entire thing.
422  goto_state_list.emplace_back(state.source, std::move(state));
423 
424  symex_transition(state, state_pc, backward);
425 
427  state.reachable = false;
428  }
429  else
430  {
431  goto_state_list.emplace_back(state.source, state);
432 
433  symex_transition(state, state_pc, backward);
434 
436  {
437  // This doesn't work for --paths (single-path mode) yet, as in multi-path
438  // mode we remove the implied constants at a control-flow merge, but in
439  // single-path mode we don't run merge_gotos.
440  auto &taken_state = backward ? state : goto_state_list.back().second;
441  auto &not_taken_state = backward ? goto_state_list.back().second : state;
442 
444  state,
445  taken_state,
446  not_taken_state,
447  instruction.get_condition(),
448  new_guard,
449  ns);
450  }
451 
452  // produce new guard symbol
453  exprt guard_expr;
454 
455  if(
456  new_guard.id() == ID_symbol ||
457  (new_guard.id() == ID_not &&
458  to_not_expr(new_guard).op().id() == ID_symbol))
459  {
460  guard_expr=new_guard;
461  }
462  else
463  {
464  symbol_exprt guard_symbol_expr =
466  exprt new_rhs = boolean_negate(new_guard);
467 
468  ssa_exprt new_lhs =
469  state.rename_ssa<L1>(ssa_exprt{guard_symbol_expr}, ns).get();
470  new_lhs =
471  state.assignment(std::move(new_lhs), new_rhs, ns, true, false).get();
472 
473  guardt guard{true_exprt{}, guard_manager};
474 
476  log.debug(),
477  [this, &new_lhs](messaget::mstreamt &mstream) {
478  mstream << "Assignment to " << new_lhs.get_identifier()
479  << " [" << pointer_offset_bits(new_lhs.type(), ns).value_or(0) << " bits]"
480  << messaget::eom;
481  });
482 
484  guard.as_expr(),
485  new_lhs, new_lhs, guard_symbol_expr,
486  new_rhs,
487  original_source,
489 
490  guard_expr = state.rename(boolean_negate(guard_symbol_expr), ns).get();
491  }
492 
493  if(state.has_saved_jump_target)
494  {
495  if(!backward)
496  state.guard.add(guard_expr);
497  else
498  state.guard.add(boolean_negate(guard_expr));
499  }
500  else
501  {
502  goto_statet &new_state = goto_state_list.back().second;
503  if(!backward)
504  {
505  new_state.guard.add(guard_expr);
506  state.guard.add(boolean_negate(guard_expr));
507  }
508  else
509  {
510  state.guard.add(guard_expr);
511  new_state.guard.add(boolean_negate(guard_expr));
512  }
513  }
514  }
515 }
516 
518 {
519  PRECONDITION(!state.reachable);
520  // This is like symex_goto, except the state is unreachable. We try to take
521  // some arbitrary choice that maintains the state guard in a reasonable state
522  // in order that it simplifies properly when states are merged (in
523  // merge_gotos). Note we can't try to evaluate the actual GOTO guard because
524  // our constant propagator might be out of date, since we haven't been
525  // executing assign instructions.
526 
527  // If the guard is already false then there's no value in this state; just
528  // carry on and check the next instruction.
529  if(state.guard.is_false())
530  {
531  symex_transition(state);
532  return;
533  }
534 
535  const goto_programt::instructiont &instruction = *state.source.pc;
536  PRECONDITION(instruction.is_goto());
537  goto_programt::const_targett goto_target = instruction.get_target();
538 
539  auto queue_unreachable_state_at_target = [&]() {
540  framet::goto_state_listt &goto_state_list =
541  state.call_stack().top().goto_state_map[goto_target];
542  goto_statet new_state(state.guard_manager);
543  new_state.guard = state.guard;
544  new_state.reachable = false;
545  goto_state_list.emplace_back(state.source, std::move(new_state));
546  };
547 
548  if(instruction.get_condition().is_true())
549  {
550  if(instruction.is_backwards_goto())
551  {
552  // Give up trying to salvage the guard
553  // (this state's guard is squashed, without queueing it at the target)
554  }
555  else
556  {
557  // Take the branch:
558  queue_unreachable_state_at_target();
559  }
560 
561  state.guard.add(false_exprt());
562  }
563  else
564  {
565  // Arbitrarily assume a conditional branch is not-taken, except for when
566  // there's an incoming backedge, when we guess that the taken case is less
567  // likely to lead to that backedge than the not-taken case.
568  bool maybe_loop_head = std::any_of(
569  instruction.incoming_edges.begin(),
570  instruction.incoming_edges.end(),
571  [&instruction](const goto_programt::const_targett predecessor) {
572  return predecessor->location_number > instruction.location_number;
573  });
574 
575  if(instruction.is_backwards_goto() || !maybe_loop_head)
576  {
577  // Assume branch not taken (fall through)
578  }
579  else
580  {
581  // Assume branch taken:
582  queue_unreachable_state_at_target();
583  state.guard.add(false_exprt());
584  }
585  }
586 
587  symex_transition(state);
588 }
589 
590 bool goto_symext::check_break(const irep_idt &loop_id, unsigned unwind)
591 {
592  // dummy implementation
593  return false;
594 }
595 
597 {
598  framet &frame = state.call_stack().top();
599 
600  // first, see if this is a target at all
601  auto state_map_it = frame.goto_state_map.find(state.source.pc);
602 
603  if(state_map_it==frame.goto_state_map.end())
604  return; // nothing to do
605 
606  // we need to merge
607  framet::goto_state_listt &state_list = state_map_it->second;
608 
609  for(auto list_it = state_list.rbegin(); list_it != state_list.rend();
610  ++list_it)
611  {
612  merge_goto(list_it->first, std::move(list_it->second), state);
613  }
614 
615  // clean up to save some memory
616  frame.goto_state_map.erase(state_map_it);
617 }
618 
619 static guardt
621 {
622  // adjust guard, even using guards from unreachable states. This helps to
623  // shrink the state guard if the incoming edge is from a path that was
624  // truncated by config.unwind, config.depth or an assume-false instruction.
625 
626  // Note when an unreachable state contributes its guard, merging it in is
627  // optional, since the formula already implies the unreachable guard is
628  // impossible. Therefore we only integrate it when to do so simplifies the
629  // state guard.
630 
631  // This function can trash either state's guards, since goto_state is dying
632  // and state's guard will shortly be overwritten.
633 
634  if(
635  (goto_state.reachable && state.reachable) ||
636  state.guard.disjunction_may_simplify(goto_state.guard))
637  {
638  state.guard |= goto_state.guard;
639  return std::move(state.guard);
640  }
641  else if(!state.reachable && goto_state.reachable)
642  {
643  return std::move(goto_state.guard);
644  }
645  else
646  {
647  return std::move(state.guard);
648  }
649 }
650 
652  const symex_targett::sourcet &,
653  goto_statet &&goto_state,
654  statet &state)
655 {
656  // check atomic section
657  if(state.atomic_section_id != goto_state.atomic_section_id)
659  "atomic sections differ across branches",
660  state.source.pc->source_location);
661 
662  // Merge guards. Don't write this to `state` yet because we might move
663  // goto_state over it below.
664  guardt new_guard = merge_state_guards(goto_state, state);
665 
666  // Merge constant propagator, value-set etc. only if the incoming state is
667  // reachable:
668  if(goto_state.reachable)
669  {
670  if(!state.reachable)
671  {
672  // Important to note that we're moving into our base class here.
673  // Note this overwrites state.guard, but we restore it below.
674  static_cast<goto_statet &>(state) = std::move(goto_state);
675  }
676  else
677  {
678  // do SSA phi functions
679  phi_function(goto_state, state);
680 
681  // merge value sets
682  state.value_set.make_union(goto_state.value_set);
683 
684  // adjust depth
685  state.depth = std::min(state.depth, goto_state.depth);
686  }
687  }
688 
689  // Save the new state guard
690  state.guard = std::move(new_guard);
691 }
692 
708 static void merge_names(
709  const goto_statet &goto_state,
710  goto_symext::statet &dest_state,
711  const namespacet &ns,
712  const guardt &diff_guard,
713  messaget &log,
714  const bool do_simplify,
715  symex_target_equationt &target,
716  const incremental_dirtyt &dirty,
717  const ssa_exprt &ssa,
718  const unsigned goto_count,
719  const unsigned dest_count)
720 {
721  const irep_idt l1_identifier = ssa.get_identifier();
722  const irep_idt &obj_identifier = ssa.get_object_name();
723 
724  if(obj_identifier == goto_symext::statet::guard_identifier())
725  return; // just a guard, don't bother
726 
727  if(goto_count == dest_count)
728  return; // not at all changed
729 
730  // changed - but only on a branch that is now dead, and the other branch is
731  // uninitialized/invalid
732  if(
733  (!dest_state.reachable && goto_count == 0) ||
734  (!goto_state.reachable && dest_count == 0))
735  {
736  return;
737  }
738 
739  // field sensitivity: only merge on individual fields
740  if(dest_state.field_sensitivity.is_divisible(ssa))
741  return;
742 
743  // shared variables are renamed on every access anyway, we don't need to
744  // merge anything
745  const symbolt &symbol = ns.lookup(obj_identifier);
746 
747  // shared?
748  if(
749  dest_state.atomic_section_id == 0 && dest_state.threads.size() >= 2 &&
750  (symbol.is_shared() || dirty(symbol.name)))
751  {
752  return; // no phi nodes for shared stuff
753  }
754 
755  // don't merge (thread-)locals across different threads, which
756  // may have been introduced by symex_start_thread (and will
757  // only later be removed from level2.current_names by pop_frame
758  // once the thread is executed)
759  const irep_idt level_0 = ssa.get_level_0();
760  if(!level_0.empty() && level_0 != std::to_string(dest_state.source.thread_nr))
761  return;
762 
763  exprt goto_state_rhs = ssa, dest_state_rhs = ssa;
764 
765  {
766  const auto p_it = goto_state.propagation.find(l1_identifier);
767 
768  if(p_it.has_value())
769  goto_state_rhs = *p_it;
770  else
771  to_ssa_expr(goto_state_rhs).set_level_2(goto_count);
772  }
773 
774  {
775  const auto p_it = dest_state.propagation.find(l1_identifier);
776 
777  if(p_it.has_value())
778  dest_state_rhs = *p_it;
779  else
780  to_ssa_expr(dest_state_rhs).set_level_2(dest_count);
781  }
782 
783  exprt rhs;
784 
785  // Don't add a conditional to the assignment when:
786  // 1. Either guard is false, so we can't follow that branch.
787  // 2. Either identifier is of generation zero, and so hasn't been
788  // initialized and therefore an invalid target.
789 
790  // These rules only apply to dynamic objects and locals.
791  if(!dest_state.reachable)
792  rhs = goto_state_rhs;
793  else if(!goto_state.reachable)
794  rhs = dest_state_rhs;
795  else if(goto_count == 0)
796  rhs = dest_state_rhs;
797  else if(dest_count == 0)
798  rhs = goto_state_rhs;
799  else
800  {
801  rhs = if_exprt(diff_guard.as_expr(), goto_state_rhs, dest_state_rhs);
802  if(do_simplify)
803  simplify(rhs, ns);
804  }
805 
806  dest_state.record_events.push(false);
807  const ssa_exprt new_lhs =
808  dest_state.assignment(ssa, rhs, ns, true, true).get();
809  dest_state.record_events.pop();
810 
811  log.conditional_output(
812  log.debug(), [ns, &new_lhs](messaget::mstreamt &mstream) {
813  mstream << "Assignment to " << new_lhs.get_identifier() << " ["
814  << pointer_offset_bits(new_lhs.type(), ns).value_or(0) << " bits]"
815  << messaget::eom;
816  });
817 
818  target.assignment(
819  true_exprt(),
820  new_lhs,
821  new_lhs,
822  new_lhs.get_original_expr(),
823  rhs,
824  dest_state.source,
826 }
827 
829  const goto_statet &goto_state,
830  statet &dest_state)
831 {
832  if(
833  goto_state.get_level2().current_names.empty() &&
834  dest_state.get_level2().current_names.empty())
835  return;
836 
837  guardt diff_guard = goto_state.guard;
838  // this gets the diff between the guards
839  diff_guard -= dest_state.guard;
840 
843  dest_state.get_level2().current_names, delta_view, false);
844 
845  for(const auto &delta_item : delta_view)
846  {
847  const ssa_exprt &ssa = delta_item.m.first;
848  unsigned goto_count = delta_item.m.second;
849  unsigned dest_count = !delta_item.is_in_both_maps()
850  ? 0
851  : delta_item.get_other_map_value().second;
852 
853  merge_names(
854  goto_state,
855  dest_state,
856  ns,
857  diff_guard,
858  log,
860  target,
862  ssa,
863  goto_count,
864  dest_count);
865  }
866 
867  delta_view.clear();
869  goto_state.get_level2().current_names, delta_view, false);
870 
871  for(const auto &delta_item : delta_view)
872  {
873  if(delta_item.is_in_both_maps())
874  continue;
875 
876  const ssa_exprt &ssa = delta_item.m.first;
877  unsigned goto_count = 0;
878  unsigned dest_count = delta_item.m.second;
879 
880  merge_names(
881  goto_state,
882  dest_state,
883  ns,
884  diff_guard,
885  log,
887  target,
889  ssa,
890  goto_count,
891  dest_count);
892  }
893 }
894 
896  statet &state,
897  const exprt &guard)
898 {
899  const unsigned loop_number=state.source.pc->loop_number;
900 
901  exprt negated_cond;
902 
903  if(guard.is_true())
904  negated_cond=false_exprt();
905  else
906  negated_cond=not_exprt(guard);
907 
909  {
911  {
912  // Generate VCC for unwinding assertion.
913  vcc(
914  negated_cond,
915  "unwinding assertion loop " + std::to_string(loop_number),
916  state);
917  }
918 
919  // generate unwinding assumption, unless we permit partial loops
920  symex_assume_l2(state, negated_cond);
921 
923  {
924  // add to state guard to prevent further assignments
925  state.guard.add(negated_cond);
926  }
927  }
928 }
929 
931  const symex_targett::sourcet &,
932  const call_stackt &,
933  unsigned)
934 {
935  // by default, we keep going
936  return false;
937 }
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:405
goto_symext::clean_expr
exprt clean_expr(exprt expr, statet &state, bool write)
Clean up an expression.
Definition: symex_clean_expr.cpp:229
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: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:221
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:708
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:517
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:783
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
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: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:2964
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:596
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: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: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 Symex constant propagation.
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:930
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:171
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:651
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:247
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
Symbolic Execution.
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:828
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:1898
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
false_exprt
The Boolean constant false.
Definition: std_expr.h:3964
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:149
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: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:300
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:590
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:341
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:2868
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:184
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: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:224
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:895
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
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:3955
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:3906
merge_state_guards
static guardt merge_state_guards(goto_statet &goto_state, goto_symex_statet &state)
Definition: symex_goto.cpp:620
binary_exprt::op1
exprt & op1()
Definition: expr.h:105
goto_programt::instructiont
This class represents an instruction in the GOTO intermediate representation.
Definition: goto_program.h:179
std_expr.h
API to expression classes.
constant_exprt::get_value
const irep_idt & get_value() const
Definition: std_expr.h:3914
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:102
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:2843
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:115