-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathcounterexample_found.cpp
213 lines (182 loc) · 5.82 KB
/
counterexample_found.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*******************************************************************\
Module: Counterexample Found
Author: Daniel Kroening, [email protected]
\*******************************************************************/
/// \file
/// Counterexample Found
#include "counterexample_found.h"
#include <util/cout_message.h>
#include <util/simplify_expr.h>
#include <solvers/sat/satcheck.h>
#include "axioms.h"
#include "bv_pointers_wide.h"
#include "simplify_state_expr.h"
#include "state.h"
void show_assignment(const bv_pointers_widet &solver)
{
#if 0
for(auto &entry : solver.get_cache())
{
const auto &expr = entry.first;
if(expr.id() == ID_and || expr.id() == ID_or || expr.id() == ID_not)
continue;
auto value = solver.l_get(entry.second);
# if 0
std::cout << "|| " << format(expr) << " --> " << value << "\n";
# endif
}
#endif
#if 0
for(auto &entry : solver.get_map().get_mapping())
{
const auto &identifier = entry.first;
auto symbol = symbol_exprt(identifier, entry.second.type);
auto value = solver.get(symbol);
std::cout << "|| " << format(symbol) << " --> " << format(value) << "\n";
}
#endif
#if 0
for(auto &entry : solver.get_symbols())
{
const auto &identifier = entry.first;
auto value = solver.l_get(entry.second);
std::cout << "|| " << identifier << " --> " << value << "\n";
}
#endif
}
static exprt evaluator_rec(
const std::unordered_map<exprt, exprt, irep_hash> &memory,
const decision_proceduret &solver,
exprt src,
const namespacet &ns)
{
if(src.id() == ID_evaluate)
{
const auto &evaluate_expr = to_evaluate_expr(src);
// recursively get the address
auto address_evaluated =
evaluator_rec(memory, solver, evaluate_expr.address(), ns);
auto address_simplified =
simplify_expr(simplify_state_expr(address_evaluated, {}, ns), ns);
auto m_it = memory.find(address_simplified);
if(m_it == memory.end())
return src;
else
return m_it->second;
}
else if(src.id() == ID_symbol)
{
// nondet -- ask the solver
return solver.get(src);
}
else
{
for(auto &op : src.operands())
op = evaluator_rec(memory, solver, op, ns);
return src;
}
}
static exprt evaluator(
const std::unordered_map<exprt, exprt, irep_hash> &memory,
const decision_proceduret &solver,
exprt src,
const namespacet &ns)
{
auto tmp = evaluator_rec(memory, solver, src, ns);
return simplify_expr(simplify_state_expr(tmp, {}, ns), ns);
}
propertyt::tracet counterexample(
const std::vector<framet> &frames,
const workt &work,
const bv_pointers_widet &solver,
const axiomst &axioms,
const namespacet &ns)
{
propertyt::tracet trace;
// map from memory addresses to memory values
std::unordered_map<exprt, exprt, irep_hash> memory;
// heap object counter
std::size_t heap_object_counter = 0;
// work.path goes backwards, we want a forwards trace
for(auto r_it = work.path.rbegin(); r_it != work.path.rend(); ++r_it)
{
const auto &frame = frames[r_it->index];
propertyt::trace_statet state;
state.frame = *r_it;
for(auto &implication : frame.implications)
{
if(implication.rhs.arguments().size() != 1)
continue;
auto &argument = implication.rhs.arguments().front();
if(argument.id() == ID_update_state)
{
const auto &update_state = to_update_state_expr(argument);
auto address = evaluator(memory, solver, update_state.address(), ns);
auto value = evaluator(memory, solver, update_state.new_value(), ns);
if(value.id() == ID_allocate)
{
// replace by a numbered 'heap object'
heap_object_counter++;
auto object_type = to_pointer_type(value.type()).base_type();
auto identifier = "heap-" + std::to_string(heap_object_counter);
value = object_address_exprt(symbol_exprt(identifier, object_type));
}
state.updates.emplace_back(address, value);
memory[address] = value;
}
else if(argument.id() == ID_enter_scope_state)
{
// do we have a value?
const auto &enter_scope_state = to_enter_scope_state_expr(argument);
auto address = enter_scope_state.address();
auto evaluate_expr = evaluate_exprt(enter_scope_state.state(), address);
auto translated = axioms.translate(evaluate_expr);
auto value = solver.get(translated);
if(value.is_not_nil() && value != evaluate_expr)
{
state.updates.emplace_back(address, value);
memory[address] = value;
}
}
}
trace.push_back(std::move(state));
}
return trace;
}
std::optional<propertyt::tracet> counterexample_found(
const std::vector<framet> &frames,
const workt &work,
const std::unordered_set<symbol_exprt, irep_hash> &address_taken,
bool verbose,
const namespacet &ns)
{
auto &f = frames[work.frame.index];
for(const auto &implication : f.implications)
{
if(implication.lhs.id() == ID_initial_state)
{
cout_message_handlert message_handler;
message_handler.set_verbosity(verbose ? 10 : 1);
satcheckt satcheck(message_handler);
bv_pointers_widet solver(ns, satcheck, message_handler);
axiomst axioms(solver, address_taken, verbose, ns);
// These are initial states, i.e., initial_state(ς) ⇒ SInitial(ς).
// Ask the solver whether the invariant is 'true'.
axioms.set_to_false(work.invariant);
axioms.set_to_true(implication.lhs);
axioms.emit();
switch(solver())
{
case decision_proceduret::resultt::D_SATISFIABLE:
if(verbose)
show_assignment(solver);
return counterexample(frames, work, solver, axioms, ns);
case decision_proceduret::resultt::D_UNSATISFIABLE:
break;
case decision_proceduret::resultt::D_ERROR:
throw "error reported by solver";
}
}
}
return {};
}