GCC Middle and Back End API Reference
program-state.h
Go to the documentation of this file.
1/* Classes for representing the state of interest at a given path of analysis.
2 Copyright (C) 2019-2024 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it
8under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 3, or (at your option)
10any later version.
11
12GCC is distributed in the hope that it will be useful, but
13WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
20
21#ifndef GCC_ANALYZER_PROGRAM_STATE_H
22#define GCC_ANALYZER_PROGRAM_STATE_H
23
24#include "text-art/widget.h"
25
26namespace ana {
27
28/* Data shared by all program_state instances. */
29
31{
32public:
33 extrinsic_state (auto_delete_vec <state_machine> &checkers,
34 engine *eng,
36 : m_checkers (checkers), m_logger (logger), m_engine (eng)
37 {
38 }
39
40 const state_machine &get_sm (int idx) const
41 {
42 return *m_checkers[idx];
43 }
44
45 const char *get_name (int idx) const
46 {
47 return m_checkers[idx]->get_name ();
48 }
49
50 unsigned get_num_checkers () const { return m_checkers.length (); }
51
52 logger *get_logger () const { return m_logger; }
53
54 void dump_to_pp (pretty_printer *pp) const;
55 void dump_to_file (FILE *outf) const;
56 void dump () const;
57
58 std::unique_ptr<json::object> to_json () const;
59
60 engine *get_engine () const { return m_engine; }
62
63 bool get_sm_idx_by_name (const char *name, unsigned *out) const;
64
65private:
66 /* The state machines. */
67 auto_delete_vec <state_machine> &m_checkers;
68
71};
72
73/* Map from svalue * to state machine state, also capturing the origin of
74 each state. */
75
77{
78public:
79 /* An entry in the hash_map. */
80 struct entry_t
81 {
82 /* Default ctor needed by hash_map::empty. */
84 : m_state (0), m_origin (NULL)
85 {
86 }
87
89 const svalue *origin)
90 : m_state (state), m_origin (origin)
91 {}
92
93 bool operator== (const entry_t &other) const
94 {
95 return (m_state == other.m_state
96 && m_origin == other.m_origin);
97 }
98 bool operator!= (const entry_t &other) const
99 {
100 return !(*this == other);
101 }
102
103 static int cmp (const entry_t &entry_a, const entry_t &entry_b);
104
107 };
108 typedef hash_map <const svalue *, entry_t> map_t;
109 typedef map_t::iterator iterator_t;
110
112
114
115 void print (const region_model *model,
116 bool simple, bool multiline,
117 pretty_printer *pp) const;
118 void dump (bool simple) const;
119
120 std::unique_ptr<json::object> to_json () const;
121
122 std::unique_ptr<text_art::tree_widget>
123 make_dump_widget (const text_art::dump_widget_info &dwi,
124 const region_model *model) const;
125
126 bool is_empty_p () const;
127
128 hashval_t hash () const;
129
130 bool operator== (const sm_state_map &other) const;
131 bool operator!= (const sm_state_map &other) const
132 {
133 return !(*this == other);
134 }
135
137 const extrinsic_state &ext_state) const;
138 const svalue *get_origin (const svalue *sval,
139 const extrinsic_state &ext_state) const;
140
142 const svalue *sval,
144 const svalue *origin,
146 bool set_state (const equiv_class &ec,
148 const svalue *origin,
150 bool impl_set_state (const svalue *sval,
152 const svalue *origin,
154 void clear_any_state (const svalue *sval);
156
159
160 void on_svalue_leak (const svalue *sval,
162 void on_liveness_change (const svalue_set &live_svalues,
163 const region_model *model,
166
167 void on_unknown_change (const svalue *sval,
168 bool is_mutable,
170
171 void purge_state_involving (const svalue *sval,
173
174 iterator_t begin () const { return m_map.begin (); }
175 iterator_t end () const { return m_map.end (); }
176 size_t elements () const { return m_map.elements (); }
177
178 static int cmp (const sm_state_map &smap_a, const sm_state_map &smap_b);
179
180 static const svalue *
182
184 const sm_state_map &summary);
185
186 bool can_merge_with_p (const sm_state_map &other,
187 const state_machine &sm,
189 sm_state_map **out) const;
190
191private:
195};
196
197/* A class for representing the state of interest at a given path of
198 analysis.
199
200 Currently this is a combination of:
201 (a) a region_model, giving:
202 (a.1) a hierarchy of memory regions
203 (a.2) values for the regions
204 (a.3) inequalities between values
205 (b) sm_state_maps per state machine, giving a sparse mapping of
206 values to states. */
207
209{
210public:
216
217 hashval_t hash () const;
218 bool operator== (const program_state &other) const;
219 bool operator!= (const program_state &other) const
220 {
221 return !(*this == other);
222 }
223
225 pretty_printer *pp) const;
226
227 void dump_to_pp (const extrinsic_state &ext_state, bool simple,
228 bool multiline, pretty_printer *pp) const;
229 void dump_to_file (const extrinsic_state &ext_state, bool simple,
230 bool multiline, FILE *outf) const;
231 void dump (const extrinsic_state &ext_state, bool simple) const;
232 void dump () const;
233
234 std::unique_ptr<json::object>
236
237 std::unique_ptr<text_art::tree_widget>
238 make_dump_widget (const text_art::dump_widget_info &dwi) const;
239
240 void push_frame (const extrinsic_state &ext_state, const function &fun);
242
244 exploded_node *enode,
245 const gcall *call_stmt,
246 uncertainty_t *uncertainty);
247
249 exploded_node *enode,
250 const gcall *call_stmt,
251 uncertainty_t *uncertainty);
252
253
255 exploded_node *enode,
256 const superedge *succ,
257 uncertainty_t *uncertainty);
258
260 const program_point &point,
261 exploded_node *enode_for_diag,
262 uncertainty_t *uncertainty) const;
263
265
267 const svalue *sval) const
268 {
269 /* Don't purge vars that have non-purgeable sm state, to avoid
270 generating false "leak" complaints. */
271 int i;
272 sm_state_map *smap;
274 {
275 const state_machine &sm = ext_state.get_sm (i);
276 if (!sm.can_purge_p (smap->get_state (sval, ext_state)))
277 return false;
278 }
279 return true;
280 }
281
283 const region *base_reg) const;
284
285 bool can_merge_with_p (const program_state &other,
287 const program_point &point,
288 program_state *out) const;
289
290 void validate (const extrinsic_state &ext_state) const;
291
292 static void detect_leaks (const program_state &src_state,
293 const program_state &dest_state,
294 const svalue *extra_sval,
297
299 const program_state &summary);
300
304
305 /* TODO: lose the pointer here (const-correctness issues?). */
308
309 /* If false, then don't attempt to explore further states along this path.
310 For use in "handling" lvalues for tree codes we haven't yet
311 implemented. */
313};
314
315/* An abstract base class for use with for_each_state_change. */
316
318{
319public:
321
322 /* Return true for early exit, false to keep iterating. */
323 virtual bool on_global_state_change (const state_machine &sm,
324 state_machine::state_t src_sm_val,
325 state_machine::state_t dst_sm_val) = 0;
326
327 /* Return true for early exit, false to keep iterating. */
328 virtual bool on_state_change (const state_machine &sm,
329 state_machine::state_t src_sm_val,
330 state_machine::state_t dst_sm_val,
331 const svalue *dst_sval,
332 const svalue *dst_origin_sval) = 0;
333};
334
335extern bool for_each_state_change (const program_state &src_state,
336 const program_state &dst_state,
339
340} // namespace ana
341
342#endif /* GCC_ANALYZER_PROGRAM_STATE_H */
Definition call-summary.h:68
Definition region-model.h:1241
Definition constraint-manager.h:255
Definition exploded-graph.h:791
Definition exploded-graph.h:203
Definition program-state.h:31
void dump_to_file(FILE *outf) const
bool get_sm_idx_by_name(const char *name, unsigned *out) const
unsigned get_num_checkers() const
Definition program-state.h:50
logger * get_logger() const
Definition program-state.h:52
std::unique_ptr< json::object > to_json() const
const char * get_name(int idx) const
Definition program-state.h:45
logger * m_logger
Definition program-state.h:69
void dump_to_pp(pretty_printer *pp) const
region_model_manager * get_model_manager() const
engine * m_engine
Definition program-state.h:70
engine * get_engine() const
Definition program-state.h:60
extrinsic_state(auto_delete_vec< state_machine > &checkers, engine *eng, logger *logger=NULL)
Definition program-state.h:33
const state_machine & get_sm(int idx) const
Definition program-state.h:40
auto_delete_vec< state_machine > & m_checkers
Definition program-state.h:67
Definition exploded-graph.h:39
Definition analyzer-logging.h:34
Definition program-point.h:175
Definition program-state.h:209
void push_call(exploded_graph &eg, exploded_node *enode, const gcall *call_stmt, uncertainty_t *uncertainty)
bool can_purge_base_region_p(const extrinsic_state &ext_state, const region *base_reg) const
program_state prune_for_point(exploded_graph &eg, const program_point &point, exploded_node *enode_for_diag, uncertainty_t *uncertainty) const
void dump() const
std::unique_ptr< json::object > to_json(const extrinsic_state &ext_state) const
void dump(const extrinsic_state &ext_state, bool simple) const
bool operator!=(const program_state &other) const
Definition program-state.h:219
program_state(const program_state &other)
void push_frame(const extrinsic_state &ext_state, const function &fun)
bool operator==(const program_state &other) const
static void detect_leaks(const program_state &src_state, const program_state &dest_state, const svalue *extra_sval, const extrinsic_state &ext_state, region_model_context *ctxt)
void dump_to_file(const extrinsic_state &ext_state, bool simple, bool multiline, FILE *outf) const
void print(const extrinsic_state &ext_state, pretty_printer *pp) const
bool replay_call_summary(call_summary_replay &r, const program_state &summary)
program_state & operator=(const program_state &other)
bool on_edge(exploded_graph &eg, exploded_node *enode, const superedge *succ, uncertainty_t *uncertainty)
program_state(program_state &&other)
void validate(const extrinsic_state &ext_state) const
auto_delete_vec< sm_state_map > m_checker_states
Definition program-state.h:307
void impl_call_analyzer_dump_state(const gcall *call, const extrinsic_state &ext_state, region_model_context *ctxt)
bool can_merge_with_p(const program_state &other, const extrinsic_state &ext_state, const program_point &point, program_state *out) const
region_model * m_region_model
Definition program-state.h:306
bool can_purge_p(const extrinsic_state &ext_state, const svalue *sval) const
Definition program-state.h:266
void returning_call(exploded_graph &eg, exploded_node *enode, const gcall *call_stmt, uncertainty_t *uncertainty)
std::unique_ptr< text_art::tree_widget > make_dump_widget(const text_art::dump_widget_info &dwi) const
const function * get_current_function() const
tree get_representative_tree(const svalue *sval) const
hashval_t hash() const
void dump_to_pp(const extrinsic_state &ext_state, bool simple, bool multiline, pretty_printer *pp) const
bool m_valid
Definition program-state.h:312
program_state(const extrinsic_state &ext_state)
Definition region-model.h:718
Definition region-model-manager.h:32
Definition region-model.h:263
Definition region.h:126
Definition program-state.h:77
bool operator==(const sm_state_map &other) const
bool can_merge_with_p(const sm_state_map &other, const state_machine &sm, const extrinsic_state &ext_state, sm_state_map **out) const
static const svalue * canonicalize_svalue(const svalue *sval, const extrinsic_state &ext_state)
static int cmp(const sm_state_map &smap_a, const sm_state_map &smap_b)
iterator_t begin() const
Definition program-state.h:174
state_machine::state_t m_global_state
Definition program-state.h:194
state_machine::state_t get_state(const svalue *sval, const extrinsic_state &ext_state) const
const state_machine & m_sm
Definition program-state.h:192
sm_state_map * clone() const
size_t elements() const
Definition program-state.h:176
void print(const region_model *model, bool simple, bool multiline, pretty_printer *pp) const
map_t m_map
Definition program-state.h:193
std::unique_ptr< json::object > to_json() const
void set_state(region_model *model, const svalue *sval, state_machine::state_t state, const svalue *origin, const extrinsic_state &ext_state)
bool operator!=(const sm_state_map &other) const
Definition program-state.h:131
bool impl_set_state(const svalue *sval, state_machine::state_t state, const svalue *origin, const extrinsic_state &ext_state)
hashval_t hash() const
void clear_any_state(const svalue *sval)
void on_svalue_leak(const svalue *sval, impl_region_model_context *ctxt)
state_machine::state_t get_global_state() const
const svalue * get_origin(const svalue *sval, const extrinsic_state &ext_state) const
iterator_t end() const
Definition program-state.h:175
void on_unknown_change(const svalue *sval, bool is_mutable, const extrinsic_state &ext_state)
map_t::iterator iterator_t
Definition program-state.h:109
void clear_all_per_svalue_state()
void purge_state_involving(const svalue *sval, const extrinsic_state &ext_state)
bool set_state(const equiv_class &ec, state_machine::state_t state, const svalue *origin, const extrinsic_state &ext_state)
std::unique_ptr< text_art::tree_widget > make_dump_widget(const text_art::dump_widget_info &dwi, const region_model *model) const
void set_global_state(state_machine::state_t state)
bool is_empty_p() const
void on_liveness_change(const svalue_set &live_svalues, const region_model *model, const extrinsic_state &ext_state, impl_region_model_context *ctxt)
hash_map< const svalue *, entry_t > map_t
Definition program-state.h:108
bool replay_call_summary(call_summary_replay &r, const sm_state_map &summary)
sm_state_map(const state_machine &sm)
void dump(bool simple) const
Definition program-state.h:318
virtual bool on_state_change(const state_machine &sm, state_machine::state_t src_sm_val, state_machine::state_t dst_sm_val, const svalue *dst_sval, const svalue *dst_origin_sval)=0
virtual ~state_change_visitor()
Definition program-state.h:320
virtual bool on_global_state_change(const state_machine &sm, state_machine::state_t src_sm_val, state_machine::state_t dst_sm_val)=0
Definition sm.h:45
Definition sm.h:40
virtual bool can_purge_p(state_t s) const =0
Definition supergraph.h:314
Definition svalue.h:92
Definition store.h:161
Definition region-model.h:222
Definition vec.h:1802
Definition ree.cc:583
iterator begin() const
Definition hash-map.h:302
size_t elements() const
Definition hash-map.h:247
iterator end() const
Definition hash-map.h:303
Definition pretty-print.h:241
union tree_node * tree
Definition coretypes.h:97
Definition access-diagram.h:30
bool for_each_state_change(const program_state &src_state, const program_state &dst_state, const extrinsic_state &ext_state, state_change_visitor *visitor)
poly_int< N, C > r
Definition poly-int.h:774
i
Definition poly-int.h:776
Definition program-state.h:81
static int cmp(const entry_t &entry_a, const entry_t &entry_b)
bool operator==(const entry_t &other) const
Definition program-state.h:93
entry_t(state_machine::state_t state, const svalue *origin)
Definition program-state.h:88
entry_t()
Definition program-state.h:83
state_machine::state_t m_state
Definition program-state.h:105
bool operator!=(const entry_t &other) const
Definition program-state.h:98
const svalue * m_origin
Definition program-state.h:106
Definition function.h:249
Definition gimple.h:352
Definition gengtype.h:377
Definition genautomata.cc:669
#define NULL
Definition system.h:50
#define FOR_EACH_VEC_ELT(V, I, P)
Definition vec.h:1884