ESBMC
Efficient SMT-based Context-Bounded Model Checker
Loading...
Searching...
No Matches
ai.h
Go to the documentation of this file.
1
3
4#ifndef CPROVER_ANALYSES_AI_H
5#define CPROVER_ANALYSES_AI_H
6
7#include <iosfwd>
8#include <map>
9#include <memory>
12#include <util/base/xml.h>
13#include <util/irep/expr.h>
14
27{
28public:
30
31 ai_baset() = default;
32
39 void operator()(const goto_programt &goto_program, const namespacet &ns)
40 {
41 goto_functionst goto_functions;
42 initialize(goto_program);
43 entry_state(goto_program);
44 fixedpoint(goto_program, goto_functions, ns);
45 finalize();
46 }
47
54 void operator()(const goto_functionst &goto_functions, const namespacet &ns)
55 {
56 initialize(goto_functions);
57 entry_state(goto_functions);
58 fixedpoint(goto_functions, ns);
59 finalize();
60 }
61
62 // TODO: add history (for widening!)
63
64 virtual std::unique_ptr<statet>
66
67 // Returns the abstract state after the given instruction
68 virtual std::unique_ptr<statet>
70 {
71 assert(!l->is_end_function()); // No state after the last instruction
72 return abstract_state_before(std::next(l));
73 }
74
76 virtual void clear()
77 {
78 }
79
86
87 virtual void
88 output(const goto_functionst &goto_functions, std::ostream &out) const;
89
90protected:
91 // overload to add a factory
92 virtual void initialize(const goto_programt &);
93 virtual void initialize(const goto_functiont &);
94 virtual void initialize(const goto_functionst &);
95
96 // override to add a cleanup step after fixedpoint has run
97 virtual void finalize();
98
99 void entry_state(const goto_programt &);
100 void entry_state(const goto_functionst &);
101
102 /* The fixedpoint is computed through a Work set algorithm which
103 * consists in adding nodes that have changed with the current merge
104 */
105 // the work-queue is sorted by location number
106 typedef std::unordered_map<unsigned, goto_programt::const_targett>
108
110
111 void
113 {
114 working_set.insert(
115 std::pair<unsigned, goto_programt::const_targett>(l->location_number, l));
116 }
117
118 // true = found something new
119 bool fixedpoint(
120 const goto_programt &goto_program,
121 const goto_functionst &goto_functions,
122 const namespacet &ns);
123
124 virtual void
125 fixedpoint(const goto_functionst &goto_functions, const namespacet &ns) = 0;
126
128 const goto_functionst &goto_functions,
129 const namespacet &ns);
130
131 // Visit performs one step of abstract interpretation from location l
132 // Depending on the instruction type it may compute a number of "edges"
133 // or applications of the abstract transformer
134 // true = found something new
135 bool visit(
137 working_sett &working_set,
138 const goto_programt &goto_program,
139 const goto_functionst &goto_functions,
140 const namespacet &ns);
141
142 // function calls
146 const expr2tc &function,
147 const goto_functionst &goto_functions,
148 const namespacet &ns);
149
150 bool do_function_call(
153 const goto_functionst &goto_functions,
154 const goto_functionst::function_mapt::const_iterator f_it,
155 const namespacet &ns);
156
157 // abstract methods
158
159 virtual bool merge(
160 const statet &src,
163 // for concurrent fixedpoint
164 virtual bool merge_shared(
165 const statet &src,
168 const namespacet &ns) = 0;
170 virtual const statet &find_state(goto_programt::const_targett l) const = 0;
171 virtual std::unique_ptr<statet> make_temporary_state(const statet &s) = 0;
172};
173
174// domainT is expected to be derived from ai_domain_baseT
175template <typename domainT>
176class ait : public ai_baset
177{
178public:
179 // constructor
181 {
182 }
183
185 {
186 typename state_mapt::iterator it = state_map.find(l);
187 if (it == state_map.end())
188 throw "failed to find state";
189
190 return it->second;
191 }
192
194 {
195 return state_map.count(l) != 0;
196 }
197
199 {
200 typename state_mapt::const_iterator it = state_map.find(l);
201 if (it == state_map.end())
202 throw "failed to find state";
203
204 return it->second;
205 }
206
207 std::unique_ptr<statet>
209 {
210 typename state_mapt::const_iterator it = state_map.find(t);
211 if (it == state_map.end())
212 {
213 std::unique_ptr<statet> d = std::make_unique<domainT>();
214 assert(d->is_bottom());
215 return d;
216 }
217
218 return std::make_unique<domainT>(it->second);
219 }
220
221 void clear() override
222 {
223 state_map.clear();
225 }
226
227 typedef std::unordered_map<
229 domainT,
234
235protected:
236 // this one creates states, if need be
238 {
239 return state_map[l]; // calls default constructor
240 }
241
242 // this one just finds states
244 {
245 typename state_mapt::const_iterator it = state_map.find(l);
246 if (it == state_map.end())
247 throw "failed to find state";
248
249 return it->second;
250 }
251
252 bool merge(
253 const statet &src,
256 {
257 statet &dest = get_state(to);
258 return static_cast<domainT &>(dest).merge(
259 static_cast<const domainT &>(src), from, to);
260 }
261
262 std::unique_ptr<statet> make_temporary_state(const statet &s) override
263 {
264 return std::make_unique<domainT>(static_cast<const domainT &>(s));
265 }
266
267 void fixedpoint(const goto_functionst &goto_functions, const namespacet &ns)
268 override
269 {
270 sequential_fixedpoint(goto_functions, ns);
271 }
272
273private:
274 // to enforce that domainT is derived from ai_domain_baset
275 void dummy(const domainT &s)
276 {
277 const statet &x = s;
278 (void)x;
279 }
280
281 // not implemented in sequential analyses
282 bool merge_shared(
283 const statet &,
286 const namespacet &) override
287 {
288 throw "not implemented";
289 }
290};
291
292#endif // CPROVER_ANALYSES_AI_H
Abstract Interpretation Domain.
virtual bool merge_shared(const statet &src, goto_programt::const_targett from, goto_programt::const_targett to, const namespacet &ns)=0
bool visit(goto_programt::const_targett l, working_sett &working_set, const goto_programt &goto_program, const goto_functionst &goto_functions, const namespacet &ns)
Definition ai.cpp:115
void operator()(const goto_functionst &goto_functions, const namespacet &ns)
Run analysis over Module (Goto Functions).
Definition ai.h:54
void sequential_fixedpoint(const goto_functionst &goto_functions, const namespacet &ns)
Definition ai.cpp:279
virtual bool merge(const statet &src, goto_programt::const_targett from, goto_programt::const_targett to)=0
virtual void fixedpoint(const goto_functionst &goto_functions, const namespacet &ns)=0
virtual const statet & find_state(goto_programt::const_targett l) const =0
ai_baset()=default
std::unordered_map< unsigned, goto_programt::const_targett > working_sett
Definition ai.h:107
bool fixedpoint(const goto_programt &goto_program, const goto_functionst &goto_functions, const namespacet &ns)
Definition ai.cpp:90
void entry_state(const goto_programt &)
Definition ai.cpp:49
virtual void clear()
Resets the domain.
Definition ai.h:76
ai_domain_baset statet
Definition ai.h:29
bool continue_past_failed_assertions
goto_programt::get_successors gives an ASSERT whose guard is false no successor, as though a violatio...
Definition ai.h:85
virtual void output(const goto_functionst &goto_functions, std::ostream &out) const
Definition ai.cpp:13
virtual std::unique_ptr< statet > make_temporary_state(const statet &s)=0
virtual statet & get_state(goto_programt::const_targett l)=0
void put_in_working_set(working_sett &working_set, goto_programt::const_targett l)
Definition ai.h:112
bool do_function_call(goto_programt::const_targett l_call, goto_programt::const_targett l_return, const goto_functionst &goto_functions, const goto_functionst::function_mapt::const_iterator f_it, const namespacet &ns)
Definition ai.cpp:171
virtual std::unique_ptr< statet > abstract_state_before(goto_programt::const_targett l) const =0
void operator()(const goto_programt &goto_program, const namespacet &ns)
Run analysis over Program.
Definition ai.h:39
bool do_function_call_rec(goto_programt::const_targett l_call, goto_programt::const_targett l_return, const expr2tc &function, const goto_functionst &goto_functions, const namespacet &ns)
Definition ai.cpp:242
goto_programt::const_targett get_next(working_sett &working_set)
Definition ai.cpp:79
virtual std::unique_ptr< statet > abstract_state_after(goto_programt::const_targett l) const
Definition ai.h:69
virtual void finalize()
Definition ai.cpp:74
The interface offered by a domain, allows code to manipulate domains without knowing their exact type...
Definition ai_domain.h:16
virtual statet & get_state(goto_programt::const_targett l) override
Definition ai.h:237
state_mapt state_map
Definition ai.h:233
const statet & find_state(goto_programt::const_targett l) const override
Definition ai.h:243
void fixedpoint(const goto_functionst &goto_functions, const namespacet &ns) override
Definition ai.h:267
std::unique_ptr< statet > make_temporary_state(const statet &s) override
Definition ai.h:262
std::unordered_map< goto_programt::const_targett, domainT, const_target_hash, pointee_address_equalt > state_mapt
Definition ai.h:232
std::unique_ptr< statet > abstract_state_before(goto_programt::const_targett t) const override
Definition ai.h:208
bool target_is_mapped(goto_programt::const_targett l) const
Definition ai.h:193
bool merge(const statet &src, goto_programt::const_targett from, goto_programt::const_targett to) override
Definition ai.h:252
ait()
Definition ai.h:180
const domainT & operator[](goto_programt::const_targett l) const
Definition ai.h:198
void clear() override
Resets the domain.
Definition ai.h:221
domainT & operator[](goto_programt::const_targett l)
Definition ai.h:184
Definition goto_functions.h:56
Definition goto_functions.h:23
A generic container class for a control flow graph for one function, in the form of a goto-program.
Definition goto_program.h:63
instructionst::const_iterator const_targett
Definition goto_program.h:591
Definition namespace.h:10
irep_container< expr2t > expr2tc
Definition irep2.h:431
void initialize()
Definition solidity_misc.c:90
Definition goto_program.h:764
Definition module.h:12
Functor to check whether iterators from different collections point at the same object.
Definition goto_program.h:775