CBMC
bmc_util.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Bounded Model Checking Utilities
4 
5 Author: Daniel Kroening, Peter Schrammel
6 
7 \*******************************************************************/
8 
11 
12 #include "bmc_util.h"
13 
14 #include <iostream>
15 
19 
22 #include <goto-symex/slice.h>
24 
26 
28 
29 #include <util/json_stream.h>
30 #include <util/ui_message.h>
31 
33 #include "symex_bmc.h"
34 
36 {
37  log.status() << "Building error trace" << messaget::eom;
38 }
39 
41  goto_tracet &goto_trace,
42  const namespacet &ns,
43  const symex_target_equationt &symex_target_equation,
44  const decision_proceduret &decision_procedure,
45  ui_message_handlert &ui_message_handler)
46 {
47  messaget log(ui_message_handler);
49 
50  build_goto_trace(symex_target_equation, decision_procedure, ns, goto_trace);
51 }
52 
55 {
56  return [property_id](
57  symex_target_equationt::SSA_stepst::const_iterator step,
58  const decision_proceduret &decision_procedure) {
59  return step->is_assert() && step->property_id == property_id &&
60  decision_procedure.get(step->cond_handle).is_false();
61  };
62 }
63 
65  const goto_tracet &goto_trace,
66  const namespacet &ns,
67  const trace_optionst &trace_options,
68  ui_message_handlert &ui_message_handler)
69 {
70  messaget msg(ui_message_handler);
71  switch(ui_message_handler.get_ui())
72  {
74  msg.result() << "Counterexample:" << messaget::eom;
75  show_goto_trace(msg.result(), ns, goto_trace, trace_options);
76  msg.result() << messaget::eom;
77  break;
78 
80  {
81  const goto_trace_stept &last_step = goto_trace.get_last_step();
82  property_infot info{
83  last_step.pc, last_step.comment, property_statust::FAIL};
84  xmlt xml_result = xml(last_step.property_id, info);
85  convert(ns, goto_trace, xml_result.new_element());
86  msg.result() << xml_result;
87  }
88  break;
89 
91  {
92  json_stream_objectt &json_result =
93  ui_message_handler.get_json_stream().push_back_stream_object();
94  const goto_trace_stept &step = goto_trace.get_last_step();
95  json_result["property"] = json_stringt(step.property_id);
96  json_result["description"] = json_stringt(step.comment);
97  json_result["status"] = json_stringt("failed");
98  json_stream_arrayt &json_trace =
99  json_result.push_back_stream_array("trace");
100  convert<json_stream_arrayt>(ns, goto_trace, json_trace, trace_options);
101  }
102  break;
103  }
104 }
105 
108  const goto_tracet &goto_trace,
109  const namespacet &ns,
110  const optionst &options)
111 {
112  const std::string graphml = options.get_option("graphml-witness");
113  if(graphml.empty())
114  return;
115 
116  graphml_witnesst graphml_witness(ns);
117  graphml_witness(goto_trace);
118 
119  if(graphml == "-")
120  write_graphml(graphml_witness.graph(), std::cout);
121  else
122  {
123  std::ofstream out(graphml);
124  write_graphml(graphml_witness.graph(), out);
125  }
126 }
127 
130  const symex_target_equationt &symex_target_equation,
131  const namespacet &ns,
132  const optionst &options)
133 {
134  const std::string graphml = options.get_option("graphml-witness");
135  if(graphml.empty())
136  return;
137 
138  graphml_witnesst graphml_witness(ns);
139  graphml_witness(symex_target_equation);
140 
141  if(graphml == "-")
142  write_graphml(graphml_witness.graph(), std::cout);
143  else
144  {
145  std::ofstream out(graphml);
146  write_graphml(graphml_witness.graph(), out);
147  }
148 }
149 
151  symex_target_equationt &equation,
152  decision_proceduret &decision_procedure,
153  message_handlert &message_handler)
154 {
155  messaget msg(message_handler);
156  msg.status() << "converting SSA" << messaget::eom;
157 
158  equation.convert(decision_procedure);
159 }
160 
161 std::unique_ptr<memory_model_baset>
162 get_memory_model(const optionst &options, const namespacet &ns)
163 {
164  const std::string mm = options.get_option("mm");
165 
166  if(mm.empty() || mm == "sc")
167  return std::make_unique<memory_model_sct>(ns);
168  else if(mm == "tso")
169  return std::make_unique<memory_model_tsot>(ns);
170  else if(mm == "pso")
171  return std::make_unique<memory_model_psot>(ns);
172  else
173  {
174  throw "invalid memory model '" + mm + "': use one of sc, tso, pso";
175  }
176 }
177 
179  symex_bmct &symex,
180  const namespacet &ns,
181  const optionst &options,
182  ui_message_handlert &ui_message_handler)
183 {
184  messaget msg(ui_message_handler);
185  const symbolt *init_symbol;
187  symex.language_mode = init_symbol->mode;
188 
189  msg.status() << "Starting Bounded Model Checking" << messaget::eom;
190 
192 
193  symex.unwindset.parse_unwind(options.get_option("unwind"));
195  options.get_list_option("unwindset"), ui_message_handler);
196 }
197 
198 void slice(
199  symex_bmct &symex,
200  symex_target_equationt &symex_target_equation,
201  const namespacet &ns,
202  const optionst &options,
203  ui_message_handlert &ui_message_handler)
204 {
205  messaget msg(ui_message_handler);
206 
207  // any properties to check at all?
208  if(symex_target_equation.has_threads())
209  {
210  // we should build a thread-aware SSA slicer
211  msg.statistics() << "no slicing due to threads" << messaget::eom;
212  }
213  else
214  {
215  if(options.get_bool_option("slice-formula"))
216  {
217  ::slice(symex_target_equation);
218  msg.statistics() << "slicing removed "
219  << symex_target_equation.count_ignored_SSA_steps()
220  << " assignments" << messaget::eom;
221  }
222  else
223  {
224  if(options.get_bool_option("simple-slice"))
225  {
226  simple_slice(symex_target_equation);
227  msg.statistics() << "simple slicing removed "
228  << symex_target_equation.count_ignored_SSA_steps()
229  << " assignments" << messaget::eom;
230  }
231  }
232  }
233  msg.statistics() << "Generated " << symex.get_total_vccs() << " VCC(s), "
234  << symex.get_remaining_vccs()
235  << " remaining after simplification" << messaget::eom;
236 }
237 
239  propertiest &properties,
240  std::unordered_set<irep_idt> &updated_properties,
241  const symex_target_equationt &equation)
242 {
243  for(const auto &step : equation.SSA_steps)
244  {
245  if(!step.is_assert())
246  continue;
247 
248  const irep_idt &property_id = step.property_id;
249  CHECK_RETURN(!property_id.empty());
250 
251  // Don't update status of properties that are constant 'false';
252  // we wouldn't have traces for them.
253  const auto status = step.cond_expr.is_true() ? property_statust::PASS
255  auto emplace_result = properties.emplace(
256  property_id, property_infot{step.source.pc, step.comment, status});
257 
258  if(emplace_result.second)
259  {
260  updated_properties.insert(property_id);
261  }
262  else
263  {
264  property_infot &property_info = emplace_result.first->second;
265  property_statust old_status = property_info.status;
266  property_info.status |= status;
267 
268  if(property_info.status != old_status)
269  updated_properties.insert(property_id);
270  }
271  }
272 }
273 
275  propertiest &properties,
276  std::unordered_set<irep_idt> &updated_properties)
277 {
278  for(auto &property_pair : properties)
279  {
280  if(property_pair.second.status == property_statust::NOT_CHECKED)
281  {
282  // This could be a NOT_CHECKED, NOT_REACHABLE or PASS,
283  // but the equation doesn't give us precise information.
284  property_pair.second.status = property_statust::PASS;
285  updated_properties.insert(property_pair.first);
286  }
287  }
288 }
289 
291  propertiest &properties,
292  std::unordered_set<irep_idt> &updated_properties)
293 {
294  for(auto &property_pair : properties)
295  {
296  if(property_pair.second.status == property_statust::UNKNOWN)
297  {
298  // This could have any status except NOT_CHECKED.
299  // We consider them PASS because we do verification modulo bounds.
300  property_pair.second.status = property_statust::PASS;
301  updated_properties.insert(property_pair.first);
302  }
303  }
304 }
305 
307  const std::string &cov_out,
308  const abstract_goto_modelt &goto_model,
309  const symex_bmct &symex,
310  ui_message_handlert &ui_message_handler)
311 {
312  if(
313  !cov_out.empty() &&
314  symex.output_coverage_report(goto_model.get_goto_functions(), cov_out))
315  {
316  messaget log(ui_message_handler);
317  log.error() << "Failed to write symex coverage report to '" << cov_out
318  << "'" << messaget::eom;
319  }
320 }
321 
323  symex_bmct &symex,
324  symex_target_equationt &equation,
325  const optionst &options,
326  const namespacet &ns,
327  ui_message_handlert &ui_message_handler)
328 {
329  const auto postprocess_equation_start = std::chrono::steady_clock::now();
330  // add a partial ordering, if required
331  if(equation.has_threads())
332  {
333  std::unique_ptr<memory_model_baset> memory_model =
334  get_memory_model(options, ns);
335  (*memory_model)(equation, ui_message_handler);
336  }
337 
338  messaget log(ui_message_handler);
339  log.statistics() << "size of program expression: "
340  << equation.SSA_steps.size() << " steps" << messaget::eom;
341 
342  slice(symex, equation, ns, options, ui_message_handler);
343 
344  if(options.get_bool_option("validate-ssa-equation"))
345  {
347  }
348 
349  const auto postprocess_equation_stop = std::chrono::steady_clock::now();
350  std::chrono::duration<double> postprocess_equation_runtime =
351  std::chrono::duration<double>(
352  postprocess_equation_stop - postprocess_equation_start);
353  log.statistics() << "Runtime Postprocess Equation: "
354  << postprocess_equation_runtime.count() << "s"
355  << messaget::eom;
356 }
357 
358 std::chrono::duration<double> prepare_property_decider(
359  propertiest &properties,
360  symex_target_equationt &equation,
361  goto_symex_property_decidert &property_decider,
362  ui_message_handlert &ui_message_handler)
363 {
364  auto solver_start = std::chrono::steady_clock::now();
365 
366  messaget log(ui_message_handler);
367  log.status()
368  << "Passing problem to "
369  << property_decider.get_decision_procedure().decision_procedure_text()
370  << messaget::eom;
371 
373  equation, property_decider.get_decision_procedure(), ui_message_handler);
375  properties);
376  property_decider.convert_goals();
377 
378  auto solver_stop = std::chrono::steady_clock::now();
379  return std::chrono::duration<double>(solver_stop - solver_start);
380 }
381 
384  propertiest &properties,
385  goto_symex_property_decidert &property_decider,
386  ui_message_handlert &ui_message_handler,
387  std::chrono::duration<double> solver_runtime,
388  bool set_pass)
389 {
390  auto solver_start = std::chrono::steady_clock::now();
391 
392  messaget log(ui_message_handler);
393  log.status()
394  << "Running "
395  << property_decider.get_decision_procedure().decision_procedure_text()
396  << messaget::eom;
397 
398  property_decider.add_constraint_from_goals(
399  [&properties](const irep_idt &property_id) {
400  return is_property_to_check(properties.at(property_id).status);
401  });
402 
403  auto const sat_solver_start = std::chrono::steady_clock::now();
404 
405  decision_proceduret::resultt dec_result = property_decider.solve();
406 
407  auto const sat_solver_stop = std::chrono::steady_clock::now();
408  std::chrono::duration<double> sat_solver_runtime =
409  std::chrono::duration<double>(sat_solver_stop - sat_solver_start);
410  log.statistics() << "Runtime Solver: " << sat_solver_runtime.count() << "s"
411  << messaget::eom;
412 
413  property_decider.update_properties_status_from_goals(
414  properties, result.updated_properties, dec_result, set_pass);
415 
416  auto solver_stop = std::chrono::steady_clock::now();
417  solver_runtime += std::chrono::duration<double>(solver_stop - solver_start);
418  log.statistics() << "Runtime decision procedure: " << solver_runtime.count()
419  << "s" << messaget::eom;
420 
422  {
424  }
425 }
void run_property_decider(incremental_goto_checkert::resultt &result, propertiest &properties, goto_symex_property_decidert &property_decider, ui_message_handlert &ui_message_handler, std::chrono::duration< double > solver_runtime, bool set_pass)
Runs the property decider to solve the equation.
Definition: bmc_util.cpp:382
std::chrono::duration< double > prepare_property_decider(propertiest &properties, symex_target_equationt &equation, goto_symex_property_decidert &property_decider, ui_message_handlert &ui_message_handler)
Converts the equation and sets up the property decider, but does not call solve.
Definition: bmc_util.cpp:358
void slice(symex_bmct &symex, symex_target_equationt &symex_target_equation, const namespacet &ns, const optionst &options, ui_message_handlert &ui_message_handler)
Definition: bmc_util.cpp:198
void output_coverage_report(const std::string &cov_out, const abstract_goto_modelt &goto_model, const symex_bmct &symex, ui_message_handlert &ui_message_handler)
Output a coverage report as generated by symex_coveraget if cov_out is non-empty.
Definition: bmc_util.cpp:306
std::unique_ptr< memory_model_baset > get_memory_model(const optionst &options, const namespacet &ns)
Definition: bmc_util.cpp:162
void output_error_trace(const goto_tracet &goto_trace, const namespacet &ns, const trace_optionst &trace_options, ui_message_handlert &ui_message_handler)
Definition: bmc_util.cpp:64
void update_status_of_unknown_properties(propertiest &properties, std::unordered_set< irep_idt > &updated_properties)
Sets the property status of UNKNOWN properties to PASS.
Definition: bmc_util.cpp:290
void message_building_error_trace(messaget &log)
Outputs a message that an error trace is being built.
Definition: bmc_util.cpp:35
void update_properties_status_from_symex_target_equation(propertiest &properties, std::unordered_set< irep_idt > &updated_properties, const symex_target_equationt &equation)
Sets property status to PASS for properties whose conditions are constant true in the equation.
Definition: bmc_util.cpp:238
void update_status_of_not_checked_properties(propertiest &properties, std::unordered_set< irep_idt > &updated_properties)
Sets the property status of NOT_CHECKED properties to PASS.
Definition: bmc_util.cpp:274
ssa_step_predicatet ssa_step_matches_failing_property(const irep_idt &property_id)
Returns a function that checks whether an SSA step is an assertion with property_id.
Definition: bmc_util.cpp:54
void build_error_trace(goto_tracet &goto_trace, const namespacet &ns, const symex_target_equationt &symex_target_equation, const decision_proceduret &decision_procedure, ui_message_handlert &ui_message_handler)
Definition: bmc_util.cpp:40
void convert_symex_target_equation(symex_target_equationt &equation, decision_proceduret &decision_procedure, message_handlert &message_handler)
Definition: bmc_util.cpp:150
void output_graphml(const goto_tracet &goto_trace, const namespacet &ns, const optionst &options)
outputs an error witness in graphml format
Definition: bmc_util.cpp:107
void postprocess_equation(symex_bmct &symex, symex_target_equationt &equation, const optionst &options, const namespacet &ns, ui_message_handlert &ui_message_handler)
Post process the equation.
Definition: bmc_util.cpp:322
void setup_symex(symex_bmct &symex, const namespacet &ns, const optionst &options, ui_message_handlert &ui_message_handler)
Definition: bmc_util.cpp:178
Bounded Model Checking Utilities.
void build_goto_trace(const symex_target_equationt &target, ssa_step_predicatet is_last_step_to_keep, const decision_proceduret &decision_procedure, const namespacet &ns, goto_tracet &goto_trace)
Build a trace by going through the steps of target and stopping after the step matching a given condi...
Traces of GOTO Programs.
std::function< bool(symex_target_equationt::SSA_stepst::const_iterator, const decision_proceduret &)> ssa_step_predicatet
static bool convert(const irep_idt &identifier, const std::ostringstream &s, symbol_table_baset &symbol_table, message_handlert &message_handler)
Abstract interface to eager or lazy GOTO models.
virtual const goto_functionst & get_goto_functions() const =0
Accessor to get a raw goto_functionst.
An interface for a decision procedure for satisfiability problems.
resultt
Result of running the decision procedure.
virtual std::string decision_procedure_text() const =0
Return a textual description of the decision procedure.
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
Provides management of goal variables that encode properties.
stack_decision_proceduret & get_decision_procedure() const
Returns the solver instance.
decision_proceduret::resultt solve()
Calls solve() on the solver instance.
void update_properties_goals_from_symex_target_equation(propertiest &properties)
Get the conditions for the properties from the equation and collect all 'instances' of the properties...
void update_properties_status_from_goals(propertiest &properties, std::unordered_set< irep_idt > &updated_properties, decision_proceduret::resultt dec_result, bool set_pass=true) const
Update the property status from the truth value of the goal variable.
void add_constraint_from_goals(std::function< bool(const irep_idt &property_id)> select_property)
Add disjunction of negated selected properties to the equation.
void convert_goals()
Convert the instances of a property into a goal variable.
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:241
unsigned get_remaining_vccs() const
Definition: goto_symex.h:851
unsigned get_total_vccs() const
Definition: goto_symex.h:842
void validate(const validation_modet vm) const
Definition: goto_symex.h:860
Step of the trace of a GOTO program.
Definition: goto_trace.h:50
std::string comment
Definition: goto_trace.h:124
goto_programt::const_targett pc
Definition: goto_trace.h:112
irep_idt property_id
Definition: goto_trace.h:123
Trace of a GOTO program.
Definition: goto_trace.h:177
goto_trace_stept & get_last_step()
Retrieves the final step in the trace for manipulation (used to fill a trace from code,...
Definition: goto_trace.h:205
const graphmlt & graph()
void make_nil()
Definition: irep.h:446
Provides methods for streaming JSON arrays.
Definition: json_stream.h:93
json_stream_objectt & push_back_stream_object()
Add a JSON object child stream.
Definition: json_stream.cpp:82
Provides methods for streaming JSON objects.
Definition: json_stream.h:140
json_stream_arrayt & push_back_stream_array(const std::string &key)
Add a JSON array stream for a specific key.
Class that provides messages with a built-in verbosity 'level'.
Definition: message.h:154
mstreamt & statistics() const
Definition: message.h:411
mstreamt & status() const
Definition: message.h:406
mstreamt & result() const
Definition: message.h:401
static eomt eom
Definition: message.h:289
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:94
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See documentation for namespace_baset::lookup().
Definition: namespace.cpp:148
bool get_bool_option(const std::string &option) const
Definition: options.cpp:44
const std::string get_option(const std::string &option) const
Definition: options.cpp:67
const value_listt & get_list_option(const std::string &option) const
Definition: options.cpp:80
Symbol table entry.
Definition: symbol.h:28
unwindsett & unwindset
Definition: symex_bmc.h:84
source_locationt last_source_location
Definition: symex_bmc.h:36
bool output_coverage_report(const goto_functionst &goto_functions, const std::string &path) const
Definition: symex_bmc.h:75
Inheriting the interface of symex_targett this class represents the SSA form of the input program as ...
std::size_t count_ignored_SSA_steps() const
void convert(decision_proceduret &decision_procedure)
Interface method to initiate the conversion into a decision procedure format.
virtual uit get_ui() const
Definition: ui_message.h:33
virtual json_stream_arrayt & get_json_stream()
Definition: ui_message.h:40
void parse_unwind(const std::string &unwind)
Definition: unwindset.cpp:23
void parse_unwindset(const std::list< std::string > &unwindset, message_handlert &message_handler)
Definition: unwindset.cpp:182
Definition: xml.h:21
xmlt & new_element(const std::string &key)
Definition: xml.h:95
Decision Procedure Interface.
Property Decider for Goto-Symex.
void show_goto_trace(messaget::mstreamt &out, const namespacet &ns, const goto_tracet &goto_trace, const trace_optionst &options)
Output the trace on the given stream out.
Definition: goto_trace.cpp:788
bool write_graphml(const graphmlt &src, std::ostream &os)
Definition: graphml.cpp:203
Witnesses for Traces and Proofs.
static std::unordered_set< irep_idt > init_symbol(const symbolt &sym, code_blockt &code_block, symbol_table_baset &symbol_table, const source_locationt &source_location, bool assume_init_pointers_not_null, const java_object_factory_parameterst &object_factory_parameters, const select_pointer_typet &pointer_type_selector, bool string_refinement_enabled, message_handlert &message_handler)
Traces of GOTO Programs.
double log(double x)
Definition: math.c:2776
Memory models for partial order concurrency.
bool is_property_to_check(property_statust status)
Return true if the status is NOT_CHECKED or UNKNOWN.
Definition: properties.cpp:175
xmlt xml(const irep_idt &property_id, const property_infot &property_info)
Definition: properties.cpp:110
property_statust
The status of a property.
Definition: properties.h:26
@ UNKNOWN
The checker was unable to determine the status of the property.
@ PASS
The property was not violated.
@ FAIL
The property was violated.
@ NOT_CHECKED
The property was not checked (also used for initialization)
std::map< irep_idt, property_infot > propertiest
A map of property IDs to property infos.
Definition: properties.h:76
void simple_slice(symex_target_equationt &equation)
Definition: slice.cpp:234
Slicer for symex traces.
#define CHECK_RETURN(CONDITION)
Definition: invariant.h:495
#define INITIALIZE_FUNCTION
std::unordered_set< irep_idt > updated_properties
Changed properties since the last call to incremental_goto_checkert::operator()
@ FOUND_FAIL
The goto checker may be able to find another FAILed property if operator() is called again.
property_statust status
The status of the property.
Definition: properties.h:72
goto_programt::const_targett pc
A pointer to the corresponding goto instruction.
Definition: properties.h:66
Options for printing the trace using show_goto_trace.
Definition: goto_trace.h:221
Bounded Model Checking for ANSI-C.
Generate Equation using Symbolic Execution.
Traces of GOTO Programs.