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