CBMC
symex_bmc.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Bounded Model Checking for ANSI-C
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
11 
12 #include "symex_bmc.h"
13 
14 #include <limits>
15 
16 #include <util/simplify_expr.h>
17 #include <util/source_location.h>
18 
20 
22  message_handlert &mh,
23  const symbol_tablet &outer_symbol_table,
24  symex_target_equationt &_target,
25  const optionst &options,
26  path_storaget &path_storage,
27  guard_managert &guard_manager,
28  unwindsett &unwindset)
29  : goto_symext(
30  mh,
31  outer_symbol_table,
32  _target,
33  options,
34  path_storage,
35  guard_manager),
36  record_coverage(!options.get_option("symex-coverage-report").empty()),
37  unwindset(unwindset),
38  symex_coverage(ns)
39 {
40 }
41 
44  const get_goto_functiont &get_goto_function,
45  statet &state)
46 {
47  const source_locationt &source_location = state.source.pc->source_location();
48 
49  if(!source_location.is_nil() && last_source_location != source_location)
50  {
51  log.debug() << "BMC at " << source_location.as_string() << " (depth "
52  << state.depth << ')' << log.eom;
53 
54  last_source_location = source_location;
55  }
56 
57  const goto_programt::const_targett cur_pc = state.source.pc;
58  const guardt cur_guard = state.guard;
59 
60  if(
61  !state.guard.is_false() && state.source.pc->is_assume() &&
62  simplify_expr(state.source.pc->condition(), ns).is_false())
63  {
64  log.statistics() << "aborting path on assume(false) at "
65  << state.source.pc->source_location() << " thread "
66  << state.source.thread_nr;
67 
68  const irep_idt &c = state.source.pc->source_location().get_comment();
69  if(!c.empty())
70  log.statistics() << ": " << c;
71 
72  log.statistics() << log.eom;
73  }
74 
76 
77  if(
79  // avoid an invalid iterator in state.source.pc
80  (!cur_pc->is_end_function() ||
82  {
83  // forward goto will effectively be covered via phi function,
84  // which does not invoke symex_step; as symex_step is called
85  // before merge_gotos, also state.guard will be false (we have
86  // taken an impossible transition); thus we synthesize a
87  // transition from the goto instruction to its target to make
88  // sure the goto is considered covered
89  if(
90  cur_pc->is_goto() && cur_pc->get_target() != state.source.pc &&
91  cur_pc->condition().is_true())
92  symex_coverage.covered(cur_pc, cur_pc->get_target());
93  else if(!state.guard.is_false())
94  symex_coverage.covered(cur_pc, state.source.pc);
95  }
96 }
97 
99  const symex_targett::sourcet &prev_source,
100  goto_statet &&goto_state,
101  statet &state)
102 {
103  const goto_programt::const_targett prev_pc = prev_source.pc;
104  const guardt prev_guard = goto_state.guard;
105 
106  goto_symext::merge_goto(prev_source, std::move(goto_state), state);
107 
108  PRECONDITION(prev_pc->is_goto());
109  if(
110  record_coverage &&
111  // could the branch possibly be taken?
112  !prev_guard.is_false() && !state.guard.is_false() &&
113  // branches only, no single-successor goto
114  !prev_pc->condition().is_true())
115  symex_coverage.covered(prev_pc, state.source.pc);
116 }
117 
119  const symex_targett::sourcet &source,
120  const call_stackt &context,
121  unsigned unwind)
122 {
123  const irep_idt id = goto_programt::loop_id(source.function_id, *source.pc);
124 
125  tvt abort_unwind_decision;
126  unsigned this_loop_limit = std::numeric_limits<unsigned>::max();
127 
128  for(auto handler : loop_unwind_handlers)
129  {
130  abort_unwind_decision =
131  handler(context, source.pc->loop_number, unwind, this_loop_limit);
132  if(abort_unwind_decision.is_known())
133  break;
134  }
135 
136  // If no handler gave an opinion, use standard command-line --unwindset
137  // / --unwind options to decide:
138  if(abort_unwind_decision.is_unknown())
139  {
140  auto limit = unwindset.get_limit(id, source.thread_nr);
141 
142  if(!limit.has_value())
143  abort_unwind_decision = tvt(false);
144  else
145  abort_unwind_decision = tvt(unwind >= *limit);
146  }
147 
148  INVARIANT(
149  abort_unwind_decision.is_known(), "unwind decision should be taken by now");
150  bool abort = abort_unwind_decision.is_true();
151 
152  log.statistics() << (abort ? "Not unwinding" : "Unwinding") << " loop " << id
153  << " iteration " << unwind;
154 
155  if(this_loop_limit != std::numeric_limits<unsigned>::max())
156  log.statistics() << " (" << this_loop_limit << " max)";
157 
158  log.statistics() << " " << source.pc->source_location() << " thread "
159  << source.thread_nr << log.eom;
160 
161  return abort;
162 }
163 
165  const irep_idt &id,
166  unsigned thread_nr,
167  unsigned unwind)
168 {
169  tvt abort_unwind_decision;
170  unsigned this_loop_limit = std::numeric_limits<unsigned>::max();
171 
172  for(auto handler : recursion_unwind_handlers)
173  {
174  abort_unwind_decision = handler(id, unwind, this_loop_limit);
175  if(abort_unwind_decision.is_known())
176  break;
177  }
178 
179  // If no handler gave an opinion, use standard command-line --unwindset
180  // / --unwind options to decide:
181  if(abort_unwind_decision.is_unknown())
182  {
183  auto limit = unwindset.get_limit(id, thread_nr);
184 
185  if(!limit.has_value())
186  abort_unwind_decision = tvt(false);
187  else
188  abort_unwind_decision = tvt(unwind > *limit);
189  }
190 
191  INVARIANT(
192  abort_unwind_decision.is_known(), "unwind decision should be taken by now");
193  bool abort = abort_unwind_decision.is_true();
194 
195  if(unwind > 0 || abort)
196  {
197  const symbolt &symbol = ns.lookup(id);
198 
199  log.statistics() << (abort ? "Not unwinding" : "Unwinding") << " recursion "
200  << symbol.display_name() << " iteration " << unwind;
201 
202  if(this_loop_limit != std::numeric_limits<unsigned>::max())
203  log.statistics() << " (" << this_loop_limit << " max)";
204 
205  log.statistics() << log.eom;
206  }
207 
208  return abort;
209 }
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:38
bool empty() const
Definition: dstring.h:89
bool is_false() const
Return whether the expression is a constant representing false.
Definition: expr.cpp:34
static irep_idt entry_point()
Get the identifier of the entry point to a goto model.
static irep_idt loop_id(const irep_idt &function_id, const instructiont &instruction)
Human-readable loop name.
Definition: goto_program.h:792
instructionst::const_iterator const_targett
Definition: goto_program.h:615
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
Central data structure: state.
symex_targett::sourcet source
The main class for the forward symbolic simulator.
Definition: goto_symex.h:37
static get_goto_functiont get_goto_function(abstract_goto_modelt &goto_model)
Return a function to get/load a goto function from the given goto model Create a default delegate to ...
Definition: symex_main.cpp:492
virtual void symex_step(const get_goto_functiont &get_goto_function, statet &state)
Called for each step in the symbolic execution This calls print_symex_step to print symex's current i...
Definition: symex_main.cpp:591
namespacet ns
Initialized just before symbolic execution begins, to point to both outer_symbol_table and the symbol...
Definition: goto_symex.h:258
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:678
std::function< const goto_functionst::goto_functiont &(const irep_idt &)> get_goto_functiont
The type of delegate functions that retrieve a goto_functiont for a particular function identifier.
Definition: goto_symex.h:93
messaget log
The messaget to write log messages to.
Definition: goto_symex.h:278
bool is_false() const
Definition: guard_expr.h:65
bool is_nil() const
Definition: irep.h:368
mstreamt & statistics() const
Definition: message.h:411
mstreamt & debug() const
Definition: message.h:421
static eomt eom
Definition: message.h:289
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See documentation for namespace_baset::lookup().
Definition: namespace.cpp:148
Storage for symbolic execution paths to be resumed later.
Definition: path_storage.h:38
std::string as_string() const
The symbol table.
Definition: symbol_table.h:14
Symbol table entry.
Definition: symbol.h:28
const irep_idt & display_name() const
Return language specific display name if present.
Definition: symbol.h:55
symex_coveraget symex_coverage
Definition: symex_bmc.h:112
bool get_unwind_recursion(const irep_idt &identifier, unsigned thread_nr, unsigned unwind) override
Definition: symex_bmc.cpp:164
std::vector< loop_unwind_handlert > loop_unwind_handlers
Callbacks that may provide an unwind/do-not-unwind decision for a loop.
Definition: symex_bmc.h:88
unwindsett & unwindset
Definition: symex_bmc.h:84
void merge_goto(const symex_targett::sourcet &source, goto_statet &&goto_state, statet &state) override
Merge a single branch, the symbolic state of which is held in goto_state, into the current overall sy...
Definition: symex_bmc.cpp:98
source_locationt last_source_location
Definition: symex_bmc.h:36
std::vector< recursion_unwind_handlert > recursion_unwind_handlers
Callbacks that may provide an unwind/do-not-unwind decision for a recursive call.
Definition: symex_bmc.h:92
symex_bmct(message_handlert &mh, const symbol_tablet &outer_symbol_table, symex_target_equationt &_target, const optionst &options, path_storaget &path_storage, guard_managert &guard_manager, unwindsett &unwindset)
Definition: symex_bmc.cpp:21
void symex_step(const get_goto_functiont &get_goto_function, statet &state) override
show progress
Definition: symex_bmc.cpp:43
bool should_stop_unwind(const symex_targett::sourcet &source, const call_stackt &context, unsigned unwind) override
Determine whether to unwind a loop.
Definition: symex_bmc.cpp:118
const bool record_coverage
Definition: symex_bmc.h:82
void covered(goto_programt::const_targett from, goto_programt::const_targett to)
Inheriting the interface of symex_targett this class represents the SSA form of the input program as ...
Definition: threeval.h:20
bool is_known() const
Definition: threeval.h:28
bool is_unknown() const
Definition: threeval.h:27
bool is_true() const
Definition: threeval.h:25
std::optional< unsigned > get_limit(const irep_idt &loop, unsigned thread_id) const
Definition: unwindset.cpp:191
exprt simplify_expr(exprt src, const namespacet &ns)
#define PRECONDITION(CONDITION)
Definition: invariant.h:463
void abort(void)
Definition: stdlib.c:128
This is unused by this implementation of guards, but can be used by other implementations of the same...
Definition: guard_expr.h:20
Identifies source in the context of symbolic execution.
Definition: symex_target.h:37
goto_programt::const_targett pc
Definition: symex_target.h:42
Bounded Model Checking for ANSI-C.
Loop unwinding.