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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
|
/* Classes for representing the state of interest at a given path of analysis.
Copyright (C) 2019-2022 Free Software Foundation, Inc.
Contributed by David Malcolm <dmalcolm@redhat.com>.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GCC is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#ifndef GCC_ANALYZER_PROGRAM_STATE_H
#define GCC_ANALYZER_PROGRAM_STATE_H
namespace ana {
/* Data shared by all program_state instances. */
class extrinsic_state
{
public:
extrinsic_state (auto_delete_vec <state_machine> &checkers,
engine *eng,
logger *logger = NULL)
: m_checkers (checkers), m_logger (logger), m_engine (eng)
{
}
const state_machine &get_sm (int idx) const
{
return *m_checkers[idx];
}
const char *get_name (int idx) const
{
return m_checkers[idx]->get_name ();
}
unsigned get_num_checkers () const { return m_checkers.length (); }
logger *get_logger () const { return m_logger; }
void dump_to_pp (pretty_printer *pp) const;
void dump_to_file (FILE *outf) const;
void dump () const;
json::object *to_json () const;
engine *get_engine () const { return m_engine; }
region_model_manager *get_model_manager () const;
bool get_sm_idx_by_name (const char *name, unsigned *out) const;
private:
/* The state machines. */
auto_delete_vec <state_machine> &m_checkers;
logger *m_logger;
engine *m_engine;
};
/* Map from svalue * to state machine state, also capturing the origin of
each state. */
class sm_state_map
{
public:
/* An entry in the hash_map. */
struct entry_t
{
/* Default ctor needed by hash_map::empty. */
entry_t ()
: m_state (0), m_origin (NULL)
{
}
entry_t (state_machine::state_t state,
const svalue *origin)
: m_state (state), m_origin (origin)
{}
bool operator== (const entry_t &other) const
{
return (m_state == other.m_state
&& m_origin == other.m_origin);
}
bool operator!= (const entry_t &other) const
{
return !(*this == other);
}
static int cmp (const entry_t &entry_a, const entry_t &entry_b);
state_machine::state_t m_state;
const svalue *m_origin;
};
typedef hash_map <const svalue *, entry_t> map_t;
typedef map_t::iterator iterator_t;
sm_state_map (const state_machine &sm);
sm_state_map *clone () const;
void print (const region_model *model,
bool simple, bool multiline,
pretty_printer *pp) const;
void dump (bool simple) const;
json::object *to_json () const;
bool is_empty_p () const;
hashval_t hash () const;
bool operator== (const sm_state_map &other) const;
bool operator!= (const sm_state_map &other) const
{
return !(*this == other);
}
state_machine::state_t get_state (const svalue *sval,
const extrinsic_state &ext_state) const;
const svalue *get_origin (const svalue *sval,
const extrinsic_state &ext_state) const;
void set_state (region_model *model,
const svalue *sval,
state_machine::state_t state,
const svalue *origin,
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);
bool impl_set_state (const svalue *sval,
state_machine::state_t state,
const svalue *origin,
const extrinsic_state &ext_state);
void set_global_state (state_machine::state_t state);
state_machine::state_t get_global_state () const;
void on_svalue_leak (const svalue *sval,
impl_region_model_context *ctxt);
void on_liveness_change (const svalue_set &live_svalues,
const region_model *model,
impl_region_model_context *ctxt);
void on_unknown_change (const svalue *sval,
bool is_mutable,
const extrinsic_state &ext_state);
void purge_state_involving (const svalue *sval,
const extrinsic_state &ext_state);
iterator_t begin () const { return m_map.begin (); }
iterator_t end () const { return m_map.end (); }
size_t elements () const { return m_map.elements (); }
static int cmp (const sm_state_map &smap_a, const sm_state_map &smap_b);
static const svalue *
canonicalize_svalue (const svalue *sval, const extrinsic_state &ext_state);
private:
const state_machine &m_sm;
map_t m_map;
state_machine::state_t m_global_state;
};
/* A class for representing the state of interest at a given path of
analysis.
Currently this is a combination of:
(a) a region_model, giving:
(a.1) a hierarchy of memory regions
(a.2) values for the regions
(a.3) inequalities between values
(b) sm_state_maps per state machine, giving a sparse mapping of
values to states. */
class program_state
{
public:
program_state (const extrinsic_state &ext_state);
program_state (const program_state &other);
program_state& operator= (const program_state &other);
program_state (program_state &&other);
~program_state ();
hashval_t hash () const;
bool operator== (const program_state &other) const;
bool operator!= (const program_state &other) const
{
return !(*this == other);
}
void print (const extrinsic_state &ext_state,
pretty_printer *pp) const;
void dump_to_pp (const extrinsic_state &ext_state, bool simple,
bool multiline, pretty_printer *pp) const;
void dump_to_file (const extrinsic_state &ext_state, bool simple,
bool multiline, FILE *outf) const;
void dump (const extrinsic_state &ext_state, bool simple) const;
json::object *to_json (const extrinsic_state &ext_state) const;
void push_frame (const extrinsic_state &ext_state, function *fun);
function * get_current_function () const;
void push_call (exploded_graph &eg,
exploded_node *enode,
const gcall *call_stmt,
uncertainty_t *uncertainty);
void returning_call (exploded_graph &eg,
exploded_node *enode,
const gcall *call_stmt,
uncertainty_t *uncertainty);
bool on_edge (exploded_graph &eg,
exploded_node *enode,
const superedge *succ,
uncertainty_t *uncertainty);
program_state prune_for_point (exploded_graph &eg,
const program_point &point,
exploded_node *enode_for_diag,
uncertainty_t *uncertainty) const;
tree get_representative_tree (const svalue *sval) const;
bool can_purge_p (const extrinsic_state &ext_state,
const svalue *sval) const
{
/* Don't purge vars that have non-purgeable sm state, to avoid
generating false "leak" complaints. */
int i;
sm_state_map *smap;
FOR_EACH_VEC_ELT (m_checker_states, i, smap)
{
const state_machine &sm = ext_state.get_sm (i);
if (!sm.can_purge_p (smap->get_state (sval, ext_state)))
return false;
}
return true;
}
bool can_purge_base_region_p (const extrinsic_state &ext_state,
const region *base_reg) const;
bool can_merge_with_p (const program_state &other,
const extrinsic_state &ext_state,
const program_point &point,
program_state *out) const;
void validate (const extrinsic_state &ext_state) 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 impl_call_analyzer_dump_state (const gcall *call,
const extrinsic_state &ext_state,
region_model_context *ctxt);
/* TODO: lose the pointer here (const-correctness issues?). */
region_model *m_region_model;
auto_delete_vec<sm_state_map> m_checker_states;
/* If false, then don't attempt to explore further states along this path.
For use in "handling" lvalues for tree codes we haven't yet
implemented. */
bool m_valid;
};
/* An abstract base class for use with for_each_state_change. */
class state_change_visitor
{
public:
virtual ~state_change_visitor () {}
/* Return true for early exit, false to keep iterating. */
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;
/* Return true for early exit, false to keep iterating. */
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;
};
extern bool for_each_state_change (const program_state &src_state,
const program_state &dst_state,
const extrinsic_state &ext_state,
state_change_visitor *visitor);
} // namespace ana
#endif /* GCC_ANALYZER_PROGRAM_STATE_H */
|