CBMC
counterexample_beautification.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Counterexample Beautification using Incremental SAT
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
11 
13 
14 #include <util/std_expr.h>
15 
17 
19  message_handlert &message_handler)
20  : log(message_handler)
21 {
22 }
23 
25  prop_convt &prop_conv,
26  const symex_target_equationt &equation,
27  minimization_listt &minimization_list)
28 {
29  // ignore the ones that are assigned under false guards
30 
31  for(symex_target_equationt::SSA_stepst::const_iterator it =
32  equation.SSA_steps.begin();
33  it != equation.SSA_steps.end();
34  it++)
35  {
36  if(
37  it->is_assignment() &&
38  it->assignment_type == symex_targett::assignment_typet::STATE)
39  {
40  if(!prop_conv.get(it->guard_handle).is_false())
41  {
42  const typet &type = it->ssa_lhs.type();
43 
44  if(type != bool_typet())
45  {
46  // we minimize the absolute value, if applicable
47  if(
48  type.id() == ID_signedbv || type.id() == ID_fixedbv ||
49  type.id() == ID_floatbv)
50  {
51  abs_exprt abs_expr(it->ssa_lhs);
52  minimization_list.insert(abs_expr);
53  }
54  else
55  minimization_list.insert(it->ssa_lhs);
56  }
57  }
58  }
59 
60  // reached failed assertion?
61  if(it == failed)
62  break;
63  }
64 }
65 
66 symex_target_equationt::SSA_stepst::const_iterator
68  const prop_convt &prop_conv,
69  const symex_target_equationt &equation)
70 {
71  // find failed property
72 
73  for(symex_target_equationt::SSA_stepst::const_iterator it =
74  equation.SSA_steps.begin();
75  it != equation.SSA_steps.end();
76  it++)
77  {
78  if(
79  it->is_assert() && prop_conv.get(it->guard_handle).is_true() &&
80  prop_conv.get(it->cond_handle).is_false())
81  {
82  return it;
83  }
84  }
85 
87  return equation.SSA_steps.end();
88 }
89 
91 operator()(boolbvt &boolbv, const symex_target_equationt &equation)
92 {
93  // find failed property
94 
95  failed = get_failed_property(boolbv, equation);
96 
97  // lock the failed assertion
98  boolbv.set_to(failed->cond_handle, false);
99 
100  {
101  log.status() << "Beautifying counterexample (guards)" << messaget::eom;
102 
103  // compute weights for guards
104  typedef std::map<literalt, unsigned> guard_countt;
105  guard_countt guard_count;
106 
107  for(symex_target_equationt::SSA_stepst::const_iterator it =
108  equation.SSA_steps.begin();
109  it != equation.SSA_steps.end();
110  it++)
111  {
112  if(
113  it->is_assignment() &&
114  it->assignment_type != symex_targett::assignment_typet::HIDDEN)
115  {
116  literalt l = boolbv.convert(it->guard_handle);
117  if(!l.is_constant())
118  guard_count[l]++;
119  }
120 
121  // reached failed assertion?
122  if(it == failed)
123  break;
124  }
125 
126  // give to propositional minimizer
127  prop_minimizet prop_minimize{boolbv, log.get_message_handler()};
128 
129  for(const auto &g : guard_count)
130  prop_minimize.objective(g.first, g.second);
131 
132  // minimize
133  prop_minimize();
134  }
135 
136  {
137  log.status() << "Beautifying counterexample (values)" << messaget::eom;
138 
139  // get symbols we care about
140  minimization_listt minimization_list;
141 
142  get_minimization_list(boolbv, equation, minimization_list);
143 
144  // minimize
145  bv_minimizet bv_minimize(boolbv, log.get_message_handler());
146  bv_minimize(minimization_list);
147  }
148 }
std::set< exprt > minimization_listt
Definition: bv_minimize.h:23
Absolute value.
Definition: std_expr.h:442
The Boolean type.
Definition: std_types.h:36
Definition: boolbv.h:46
void set_to(const exprt &expr, bool value) override
For a Boolean expression expr, add the constraint 'expr' if value is true, otherwise add 'not expr'.
Definition: boolbv.cpp:523
void operator()(boolbvt &boolbv, const symex_target_equationt &equation)
void get_minimization_list(prop_convt &prop_conv, const symex_target_equationt &equation, minimization_listt &minimization_list)
symex_target_equationt::SSA_stepst::const_iterator failed
symex_target_equationt::SSA_stepst::const_iterator get_failed_property(const prop_convt &prop_conv, const symex_target_equationt &equation)
counterexample_beautificationt(message_handlert &message_handler)
virtual exprt get(const exprt &) const =0
Return expr with variables replaced by values from satisfying assignment if available.
bool is_true() const
Return whether the expression is a constant representing true.
Definition: expr.cpp:27
bool is_false() const
Return whether the expression is a constant representing false.
Definition: expr.cpp:34
const irep_idt & id() const
Definition: irep.h:388
bool is_constant() const
Definition: literal.h:166
mstreamt & status() const
Definition: message.h:406
message_handlert & get_message_handler()
Definition: message.h:183
static eomt eom
Definition: message.h:289
literalt convert(const exprt &expr) override
Convert a Boolean expression and return the corresponding literal.
Computes a satisfying assignment of minimal cost according to a const function using incremental SAT.
Definition: prop_minimize.h:26
Inheriting the interface of symex_targett this class represents the SSA form of the input program as ...
The type of an expression, extends irept.
Definition: type.h:29
Counterexample Beautification.
double log(double x)
Definition: math.c:2776
SAT Minimizer.
#define UNREACHABLE
This should be used to mark dead code.
Definition: invariant.h:525
API to expression classes.