Daily bump.
[official-gcc.git] / gcc / analyzer / sm.h
blob02faffbff99f6ed6460f466108b7e0d009000f0c
1 /* Modeling API uses and misuses via state machines.
2 Copyright (C) 2019-2021 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #ifndef GCC_ANALYZER_SM_H
22 #define GCC_ANALYZER_SM_H
24 /* Utility functions for use by state machines. */
26 namespace ana {
28 class state_machine;
29 class sm_context;
30 class pending_diagnostic;
32 extern bool any_pointer_p (tree expr);
33 extern bool any_pointer_p (const svalue *sval);
35 /* An abstract base class for a state machine describing an API.
36 Manages a set of state objects, and has various virtual functions
37 for pattern-matching on statements. */
39 class state_machine : public log_user
41 public:
42 /* States are represented by immutable objects, owned by the state
43 machine. */
44 class state
46 public:
47 state (const char *name, unsigned id) : m_name (name), m_id (id) {}
48 virtual ~state () {}
50 const char *get_name () const { return m_name; }
51 virtual void dump_to_pp (pretty_printer *pp) const;
52 virtual json::value *to_json () const;
54 unsigned get_id () const { return m_id; }
56 private:
57 const char *m_name;
58 unsigned m_id;
60 typedef const state_machine::state *state_t;
62 state_machine (const char *name, logger *logger);
63 virtual ~state_machine () {}
65 /* Should states be inherited from a parent region to a child region,
66 when first accessing a child region?
67 For example we should inherit the taintedness of a subregion,
68 but we should not inherit the "malloc:non-null" state of a field
69 within a heap-allocated struct. */
70 virtual bool inherited_state_p () const = 0;
72 virtual state_machine::state_t get_default_state (const svalue *) const
74 return m_start;
77 const char *get_name () const { return m_name; }
79 state_t get_state_by_name (const char *name) const;
81 /* Return true if STMT is a function call recognized by this sm. */
82 virtual bool on_stmt (sm_context *sm_ctxt,
83 const supernode *node,
84 const gimple *stmt) const = 0;
86 virtual void on_phi (sm_context *sm_ctxt ATTRIBUTE_UNUSED,
87 const supernode *node ATTRIBUTE_UNUSED,
88 const gphi *phi ATTRIBUTE_UNUSED,
89 tree rhs ATTRIBUTE_UNUSED) const
93 virtual void on_condition (sm_context *sm_ctxt ATTRIBUTE_UNUSED,
94 const supernode *node ATTRIBUTE_UNUSED,
95 const gimple *stmt ATTRIBUTE_UNUSED,
96 const svalue *lhs ATTRIBUTE_UNUSED,
97 enum tree_code op ATTRIBUTE_UNUSED,
98 const svalue *rhs ATTRIBUTE_UNUSED) const
102 /* Return true if it safe to discard the given state (to help
103 when simplifying state objects).
104 States that need leak detection should return false. */
105 virtual bool can_purge_p (state_t s) const = 0;
107 /* Called when VAR leaks (and !can_purge_p). */
108 virtual pending_diagnostic *on_leak (tree var ATTRIBUTE_UNUSED) const
110 return NULL;
113 /* Return true if S should be reset to "start" for values passed (or reachable
114 from) calls to unknown functions. IS_MUTABLE is true for pointers as
115 non-const, false if only passed as const-pointers.
117 For example, in sm-malloc.cc, an on-stack ptr doesn't stop being
118 stack-allocated when passed to an unknown fn, but a malloc-ed pointer
119 could be freed when passed to an unknown fn (unless passed as "const"). */
120 virtual bool reset_when_passed_to_unknown_fn_p (state_t s ATTRIBUTE_UNUSED,
121 bool is_mutable) const
123 return is_mutable;
126 void validate (state_t s) const;
128 void dump_to_pp (pretty_printer *pp) const;
130 json::object *to_json () const;
132 state_t get_start_state () const { return m_start; }
134 protected:
135 state_t add_state (const char *name);
136 state_t add_custom_state (state *s)
138 m_states.safe_push (s);
139 return s;
142 unsigned alloc_state_id () { return m_next_state_id++; }
144 private:
145 DISABLE_COPY_AND_ASSIGN (state_machine);
147 const char *m_name;
149 /* States are owned by the state_machine. */
150 auto_delete_vec<state> m_states;
152 unsigned m_next_state_id;
154 protected:
155 /* Must be inited after m_next_state_id. */
156 state_t m_start;
159 /* Abstract base class for state machines to pass to
160 sm_context::on_custom_transition for handling non-standard transitions
161 (e.g. adding a node and edge to simulate registering a callback and having
162 the callback be called later). */
164 class custom_transition
166 public:
167 virtual ~custom_transition () {}
168 virtual void impl_transition (exploded_graph *eg,
169 exploded_node *src_enode,
170 int sm_idx) = 0;
173 /* Abstract base class giving an interface for the state machine to call
174 the checker engine, at a particular stmt. */
176 class sm_context
178 public:
179 virtual ~sm_context () {}
181 /* Get the fndecl used at call, or NULL_TREE.
182 Use in preference to gimple_call_fndecl (and gimple_call_addr_fndecl),
183 since it can look through function pointer assignments and
184 other callback handling. */
185 virtual tree get_fndecl_for_call (const gcall *call) = 0;
187 /* Get the old state of VAR at STMT. */
188 virtual state_machine::state_t get_state (const gimple *stmt,
189 tree var) = 0;
190 virtual state_machine::state_t get_state (const gimple *stmt,
191 const svalue *) = 0;
192 /* Set the next state of VAR to be TO, recording the "origin" of the
193 state as ORIGIN.
194 Use STMT for location information. */
195 virtual void set_next_state (const gimple *stmt,
196 tree var,
197 state_machine::state_t to,
198 tree origin = NULL_TREE) = 0;
199 virtual void set_next_state (const gimple *stmt,
200 const svalue *var,
201 state_machine::state_t to,
202 tree origin = NULL_TREE) = 0;
204 /* Called by state_machine in response to pattern matches:
205 if VAR is in state FROM, transition it to state TO, potentially
206 recording the "origin" of the state as ORIGIN.
207 Use NODE and STMT for location information. */
208 void on_transition (const supernode *node ATTRIBUTE_UNUSED,
209 const gimple *stmt,
210 tree var,
211 state_machine::state_t from,
212 state_machine::state_t to,
213 tree origin = NULL_TREE)
215 state_machine::state_t current = get_state (stmt, var);
216 if (current == from)
217 set_next_state (stmt, var, to, origin);
220 void on_transition (const supernode *node ATTRIBUTE_UNUSED,
221 const gimple *stmt,
222 const svalue *var,
223 state_machine::state_t from,
224 state_machine::state_t to,
225 tree origin = NULL_TREE)
227 state_machine::state_t current = get_state (stmt, var);
228 if (current == from)
229 set_next_state (stmt, var, to, origin);
232 /* Called by state_machine in response to pattern matches:
233 issue a diagnostic D using NODE and STMT for location information. */
234 virtual void warn (const supernode *node, const gimple *stmt,
235 tree var, pending_diagnostic *d) = 0;
237 /* For use when generating trees when creating pending_diagnostics, so that
238 rather than e.g.
239 "double-free of '<unknown>'"
240 we can print:
241 "double-free of 'inbuf.data'". */
242 virtual tree get_diagnostic_tree (tree expr)
244 return expr;
246 virtual tree get_diagnostic_tree (const svalue *) = 0;
248 virtual state_machine::state_t get_global_state () const = 0;
249 virtual void set_global_state (state_machine::state_t) = 0;
251 /* A vfunc for handling custom transitions, such as when registering
252 a signal handler. */
253 virtual void on_custom_transition (custom_transition *transition) = 0;
255 /* If STMT is an assignment known to assign zero to its LHS, return
256 the LHS.
257 Otherwise return NULL_TREE. */
258 virtual tree is_zero_assignment (const gimple *stmt) = 0;
260 virtual path_context *get_path_context () const
262 return NULL;
265 protected:
266 sm_context (int sm_idx, const state_machine &sm)
267 : m_sm_idx (sm_idx), m_sm (sm) {}
269 int m_sm_idx;
270 const state_machine &m_sm;
274 /* The various state_machine subclasses are hidden in their respective
275 implementation files. */
277 extern void make_checkers (auto_delete_vec <state_machine> &out,
278 logger *logger);
280 extern state_machine *make_malloc_state_machine (logger *logger);
281 extern state_machine *make_fileptr_state_machine (logger *logger);
282 extern state_machine *make_taint_state_machine (logger *logger);
283 extern state_machine *make_sensitive_state_machine (logger *logger);
284 extern state_machine *make_signal_state_machine (logger *logger);
285 extern state_machine *make_pattern_test_state_machine (logger *logger);
287 } // namespace ana
289 #endif /* GCC_ANALYZER_SM_H */