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
24namespace ana {
25
26/* Data shared by all program_state instances. */
27
29{
30public:
37
38 const state_machine &get_sm (int idx) const
39 {
40 return *m_checkers[idx];
41 }
42
43 const char *get_name (int idx) const
44 {
45 return m_checkers[idx]->get_name ();
46 }
47
48 unsigned get_num_checkers () const { return m_checkers.length (); }
49
50 logger *get_logger () const { return m_logger; }
51
52 void dump_to_pp (pretty_printer *pp) const;
53 void dump_to_file (FILE *outf) const;
54 void dump () const;
55
57
58 engine *get_engine () const { return m_engine; }
60
61 bool get_sm_idx_by_name (const char *name, unsigned *out) const;
62
63private:
64 /* The state machines. */
66
69};
70
71/* Map from svalue * to state machine state, also capturing the origin of
72 each state. */
73
75{
76public:
77 /* An entry in the hash_map. */
78 struct entry_t
79 {
80 /* Default ctor needed by hash_map::empty. */
82 : m_state (0), m_origin (NULL)
83 {
84 }
85
87 const svalue *origin)
88 : m_state (state), m_origin (origin)
89 {}
90
91 bool operator== (const entry_t &other) const
92 {
93 return (m_state == other.m_state
94 && m_origin == other.m_origin);
95 }
96 bool operator!= (const entry_t &other) const
97 {
98 return !(*this == other);
99 }
100
101 static int cmp (const entry_t &entry_a, const entry_t &entry_b);
102
105 };
106 typedef hash_map <const svalue *, entry_t> map_t;
107 typedef map_t::iterator iterator_t;
108
110
112
114 bool simple, bool multiline,
115 pretty_printer *pp) const;
116 void dump (bool simple) const;
117
119
120 bool is_empty_p () const;
121
122 hashval_t hash () const;
123
124 bool operator== (const sm_state_map &other) const;
125 bool operator!= (const sm_state_map &other) const
126 {
127 return !(*this == other);
128 }
129
131 const extrinsic_state &ext_state) const;
132 const svalue *get_origin (const svalue *sval,
133 const extrinsic_state &ext_state) const;
134
136 const svalue *sval,
138 const svalue *origin,
142 const svalue *origin,
144 bool impl_set_state (const svalue *sval,
146 const svalue *origin,
148 void clear_any_state (const svalue *sval);
150
153
154 void on_svalue_leak (const svalue *sval,
157 const region_model *model,
160
161 void on_unknown_change (const svalue *sval,
162 bool is_mutable,
164
165 void purge_state_involving (const svalue *sval,
167
168 iterator_t begin () const { return m_map.begin (); }
169 iterator_t end () const { return m_map.end (); }
170 size_t elements () const { return m_map.elements (); }
171
172 static int cmp (const sm_state_map &smap_a, const sm_state_map &smap_b);
173
174 static const svalue *
176
178 const sm_state_map &summary);
179
180 bool can_merge_with_p (const sm_state_map &other,
181 const state_machine &sm,
183 sm_state_map **out) const;
184
185private:
189};
190
191/* A class for representing the state of interest at a given path of
192 analysis.
193
194 Currently this is a combination of:
195 (a) a region_model, giving:
196 (a.1) a hierarchy of memory regions
197 (a.2) values for the regions
198 (a.3) inequalities between values
199 (b) sm_state_maps per state machine, giving a sparse mapping of
200 values to states. */
201
203{
204public:
210
211 hashval_t hash () const;
212 bool operator== (const program_state &other) const;
213 bool operator!= (const program_state &other) const
214 {
215 return !(*this == other);
216 }
217
219 pretty_printer *pp) const;
220
222 bool multiline, pretty_printer *pp) const;
224 bool multiline, FILE *outf) const;
225 void dump (const extrinsic_state &ext_state, bool simple) const;
226
228
229 void push_frame (const extrinsic_state &ext_state, const function &fun);
231
234 const gcall *call_stmt,
236
239 const gcall *call_stmt,
241
242
245 const superedge *succ,
247
249 const program_point &point,
252
254
256 const svalue *sval) const
257 {
258 /* Don't purge vars that have non-purgeable sm state, to avoid
259 generating false "leak" complaints. */
260 int i;
263 {
264 const state_machine &sm = ext_state.get_sm (i);
265 if (!sm.can_purge_p (smap->get_state (sval, ext_state)))
266 return false;
267 }
268 return true;
269 }
270
272 const region *base_reg) const;
273
274 bool can_merge_with_p (const program_state &other,
276 const program_point &point,
277 program_state *out) const;
278
279 void validate (const extrinsic_state &ext_state) const;
280
283 const svalue *extra_sval,
286
288 const program_state &summary);
289
293
294 /* TODO: lose the pointer here (const-correctness issues?). */
297
298 /* If false, then don't attempt to explore further states along this path.
299 For use in "handling" lvalues for tree codes we haven't yet
300 implemented. */
302};
303
304/* An abstract base class for use with for_each_state_change. */
305
307{
308public:
310
311 /* Return true for early exit, false to keep iterating. */
315
316 /* Return true for early exit, false to keep iterating. */
317 virtual bool on_state_change (const state_machine &sm,
320 const svalue *dst_sval,
321 const svalue *dst_origin_sval) = 0;
322};
323
328
329} // namespace ana
330
331#endif /* GCC_ANALYZER_PROGRAM_STATE_H */
Definition call-summary.h:68
Definition region-model.h:1225
Definition constraint-manager.h:249
Definition exploded-graph.h:791
Definition exploded-graph.h:203
Definition program-state.h:29
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:48
logger * get_logger() const
Definition program-state.h:50
const char * get_name(int idx) const
Definition program-state.h:43
logger * m_logger
Definition program-state.h:67
json::object * to_json() const
void dump_to_pp(pretty_printer *pp) const
region_model_manager * get_model_manager() const
engine * m_engine
Definition program-state.h:68
engine * get_engine() const
Definition program-state.h:58
extrinsic_state(auto_delete_vec< state_machine > &checkers, engine *eng, logger *logger=NULL)
Definition program-state.h:31
const state_machine & get_sm(int idx) const
Definition program-state.h:38
auto_delete_vec< state_machine > & m_checkers
Definition program-state.h:65
Definition exploded-graph.h:39
Definition analyzer-logging.h:34
Definition program-point.h:175
Definition program-state.h:203
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 extrinsic_state &ext_state, bool simple) const
bool operator!=(const program_state &other) const
Definition program-state.h:213
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:296
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:295
bool can_purge_p(const extrinsic_state &ext_state, const svalue *sval) const
Definition program-state.h:255
void returning_call(exploded_graph &eg, exploded_node *enode, const gcall *call_stmt, uncertainty_t *uncertainty)
const function * get_current_function() const
json::object * to_json(const extrinsic_state &ext_state) 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:301
program_state(const extrinsic_state &ext_state)
Definition region-model.h:702
Definition region-model-manager.h:32
Definition region-model.h:258
Definition region.h:125
Definition program-state.h:75
map_t::iterator iterator_t
Definition program-state.h:107
bool operator==(const sm_state_map &other) const
json::object * to_json() 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:168
state_machine::state_t m_global_state
Definition program-state.h:188
state_machine::state_t get_state(const svalue *sval, const extrinsic_state &ext_state) const
const state_machine & m_sm
Definition program-state.h:186
sm_state_map * clone() const
size_t elements() const
Definition program-state.h:170
hash_map< const svalue *, entry_t > map_t
Definition program-state.h:106
void print(const region_model *model, bool simple, bool multiline, pretty_printer *pp) const
map_t m_map
Definition program-state.h:187
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:125
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:169
void on_unknown_change(const svalue *sval, bool is_mutable, const extrinsic_state &ext_state)
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)
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)
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:307
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:309
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
Definition supergraph.h:314
Definition svalue.h:90
Definition store.h:159
Definition region-model.h:217
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 json.h:95
Definition pretty-print.h:244
union tree_node * tree
Definition coretypes.h:97
T * ggc_alloc(ALONE_CXX_MEM_STAT_INFO)
Definition ggc.h:184
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:770
i
Definition poly-int.h:772
Definition program-state.h:79
static int cmp(const entry_t &entry_a, const entry_t &entry_b)
bool operator==(const entry_t &other) const
Definition program-state.h:91
entry_t(state_machine::state_t state, const svalue *origin)
Definition program-state.h:86
entry_t()
Definition program-state.h:81
state_machine::state_t m_state
Definition program-state.h:103
bool operator!=(const entry_t &other) const
Definition program-state.h:96
const svalue * m_origin
Definition program-state.h:104
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