]>
Commit | Line | Data |
---|---|---|
757bf1df | 1 | /* Modeling API uses and misuses via state machines. |
7adcbafe | 2 | Copyright (C) 2019-2022 Free Software Foundation, Inc. |
757bf1df DM |
3 | Contributed by David Malcolm <dmalcolm@redhat.com>. |
4 | ||
5 | This file is part of GCC. | |
6 | ||
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. | |
11 | ||
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. | |
16 | ||
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/>. */ | |
20 | ||
21 | #ifndef GCC_ANALYZER_SM_H | |
22 | #define GCC_ANALYZER_SM_H | |
23 | ||
24 | /* Utility functions for use by state machines. */ | |
25 | ||
75038aa6 DM |
26 | namespace ana { |
27 | ||
757bf1df DM |
28 | class state_machine; |
29 | class sm_context; | |
30 | class pending_diagnostic; | |
31 | ||
48e8a7a6 DM |
32 | extern bool any_pointer_p (tree expr); |
33 | extern bool any_pointer_p (const svalue *sval); | |
808f4dfe | 34 | |
757bf1df | 35 | /* An abstract base class for a state machine describing an API. |
10fc42a8 | 36 | Manages a set of state objects, and has various virtual functions |
757bf1df DM |
37 | for pattern-matching on statements. */ |
38 | ||
39 | class state_machine : public log_user | |
40 | { | |
41 | public: | |
10fc42a8 DM |
42 | /* States are represented by immutable objects, owned by the state |
43 | machine. */ | |
44 | class state | |
45 | { | |
46 | public: | |
47 | state (const char *name, unsigned id) : m_name (name), m_id (id) {} | |
48 | virtual ~state () {} | |
49 | ||
50 | const char *get_name () const { return m_name; } | |
51 | virtual void dump_to_pp (pretty_printer *pp) const; | |
809192e7 | 52 | virtual json::value *to_json () const; |
10fc42a8 DM |
53 | |
54 | unsigned get_id () const { return m_id; } | |
757bf1df | 55 | |
10fc42a8 DM |
56 | private: |
57 | const char *m_name; | |
58 | unsigned m_id; | |
59 | }; | |
60 | typedef const state_machine::state *state_t; | |
757bf1df | 61 | |
10fc42a8 | 62 | state_machine (const char *name, logger *logger); |
757bf1df DM |
63 | virtual ~state_machine () {} |
64 | ||
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; | |
71 | ||
b9365b93 DM |
72 | /* A vfunc for more general handling of inheritance. */ |
73 | virtual state_t | |
74 | alt_get_inherited_state (const sm_state_map &, | |
75 | const svalue *, | |
76 | const extrinsic_state &) const | |
77 | { | |
78 | return NULL; | |
79 | } | |
80 | ||
808f4dfe DM |
81 | virtual state_machine::state_t get_default_state (const svalue *) const |
82 | { | |
10fc42a8 | 83 | return m_start; |
808f4dfe DM |
84 | } |
85 | ||
757bf1df DM |
86 | const char *get_name () const { return m_name; } |
87 | ||
10fc42a8 | 88 | state_t get_state_by_name (const char *name) const; |
41f99ba6 | 89 | |
757bf1df DM |
90 | /* Return true if STMT is a function call recognized by this sm. */ |
91 | virtual bool on_stmt (sm_context *sm_ctxt, | |
92 | const supernode *node, | |
93 | const gimple *stmt) const = 0; | |
94 | ||
8525d1f5 DM |
95 | virtual void on_phi (sm_context *sm_ctxt ATTRIBUTE_UNUSED, |
96 | const supernode *node ATTRIBUTE_UNUSED, | |
97 | const gphi *phi ATTRIBUTE_UNUSED, | |
98 | tree rhs ATTRIBUTE_UNUSED) const | |
99 | { | |
100 | } | |
101 | ||
48e8a7a6 DM |
102 | virtual void on_condition (sm_context *sm_ctxt ATTRIBUTE_UNUSED, |
103 | const supernode *node ATTRIBUTE_UNUSED, | |
104 | const gimple *stmt ATTRIBUTE_UNUSED, | |
105 | const svalue *lhs ATTRIBUTE_UNUSED, | |
106 | enum tree_code op ATTRIBUTE_UNUSED, | |
107 | const svalue *rhs ATTRIBUTE_UNUSED) const | |
108 | { | |
109 | } | |
757bf1df | 110 | |
2c044ff1 DM |
111 | virtual void |
112 | on_bounded_ranges (sm_context *sm_ctxt ATTRIBUTE_UNUSED, | |
113 | const supernode *node ATTRIBUTE_UNUSED, | |
114 | const gimple *stmt ATTRIBUTE_UNUSED, | |
115 | const svalue &sval ATTRIBUTE_UNUSED, | |
116 | const bounded_ranges &ranges ATTRIBUTE_UNUSED) const | |
117 | { | |
118 | } | |
119 | ||
757bf1df DM |
120 | /* Return true if it safe to discard the given state (to help |
121 | when simplifying state objects). | |
122 | States that need leak detection should return false. */ | |
123 | virtual bool can_purge_p (state_t s) const = 0; | |
124 | ||
125 | /* Called when VAR leaks (and !can_purge_p). */ | |
126 | virtual pending_diagnostic *on_leak (tree var ATTRIBUTE_UNUSED) const | |
127 | { | |
128 | return NULL; | |
129 | } | |
130 | ||
808f4dfe DM |
131 | /* Return true if S should be reset to "start" for values passed (or reachable |
132 | from) calls to unknown functions. IS_MUTABLE is true for pointers as | |
133 | non-const, false if only passed as const-pointers. | |
134 | ||
135 | For example, in sm-malloc.cc, an on-stack ptr doesn't stop being | |
136 | stack-allocated when passed to an unknown fn, but a malloc-ed pointer | |
137 | could be freed when passed to an unknown fn (unless passed as "const"). */ | |
138 | virtual bool reset_when_passed_to_unknown_fn_p (state_t s ATTRIBUTE_UNUSED, | |
139 | bool is_mutable) const | |
140 | { | |
141 | return is_mutable; | |
142 | } | |
143 | ||
757bf1df DM |
144 | void validate (state_t s) const; |
145 | ||
42f36563 DM |
146 | void dump_to_pp (pretty_printer *pp) const; |
147 | ||
809192e7 DM |
148 | json::object *to_json () const; |
149 | ||
10fc42a8 DM |
150 | state_t get_start_state () const { return m_start; } |
151 | ||
757bf1df DM |
152 | protected: |
153 | state_t add_state (const char *name); | |
1690a839 DM |
154 | state_t add_custom_state (state *s) |
155 | { | |
156 | m_states.safe_push (s); | |
157 | return s; | |
158 | } | |
159 | ||
10fc42a8 | 160 | unsigned alloc_state_id () { return m_next_state_id++; } |
757bf1df DM |
161 | |
162 | private: | |
163 | DISABLE_COPY_AND_ASSIGN (state_machine); | |
164 | ||
165 | const char *m_name; | |
757bf1df | 166 | |
10fc42a8 DM |
167 | /* States are owned by the state_machine. */ |
168 | auto_delete_vec<state> m_states; | |
757bf1df | 169 | |
10fc42a8 DM |
170 | unsigned m_next_state_id; |
171 | ||
172 | protected: | |
173 | /* Must be inited after m_next_state_id. */ | |
174 | state_t m_start; | |
175 | }; | |
757bf1df DM |
176 | |
177 | /* Abstract base class for state machines to pass to | |
178 | sm_context::on_custom_transition for handling non-standard transitions | |
179 | (e.g. adding a node and edge to simulate registering a callback and having | |
180 | the callback be called later). */ | |
181 | ||
182 | class custom_transition | |
183 | { | |
184 | public: | |
185 | virtual ~custom_transition () {} | |
186 | virtual void impl_transition (exploded_graph *eg, | |
187 | exploded_node *src_enode, | |
188 | int sm_idx) = 0; | |
189 | }; | |
190 | ||
191 | /* Abstract base class giving an interface for the state machine to call | |
192 | the checker engine, at a particular stmt. */ | |
193 | ||
194 | class sm_context | |
195 | { | |
196 | public: | |
197 | virtual ~sm_context () {} | |
198 | ||
199 | /* Get the fndecl used at call, or NULL_TREE. | |
200 | Use in preference to gimple_call_fndecl (and gimple_call_addr_fndecl), | |
201 | since it can look through function pointer assignments and | |
202 | other callback handling. */ | |
203 | virtual tree get_fndecl_for_call (const gcall *call) = 0; | |
204 | ||
6d9ca8c8 DM |
205 | /* Get the old state of VAR at STMT. */ |
206 | virtual state_machine::state_t get_state (const gimple *stmt, | |
207 | tree var) = 0; | |
48e8a7a6 DM |
208 | virtual state_machine::state_t get_state (const gimple *stmt, |
209 | const svalue *) = 0; | |
6d9ca8c8 DM |
210 | /* Set the next state of VAR to be TO, recording the "origin" of the |
211 | state as ORIGIN. | |
212 | Use STMT for location information. */ | |
213 | virtual void set_next_state (const gimple *stmt, | |
214 | tree var, | |
215 | state_machine::state_t to, | |
216 | tree origin = NULL_TREE) = 0; | |
48e8a7a6 DM |
217 | virtual void set_next_state (const gimple *stmt, |
218 | const svalue *var, | |
219 | state_machine::state_t to, | |
220 | tree origin = NULL_TREE) = 0; | |
6d9ca8c8 | 221 | |
757bf1df DM |
222 | /* Called by state_machine in response to pattern matches: |
223 | if VAR is in state FROM, transition it to state TO, potentially | |
224 | recording the "origin" of the state as ORIGIN. | |
225 | Use NODE and STMT for location information. */ | |
6d9ca8c8 DM |
226 | void on_transition (const supernode *node ATTRIBUTE_UNUSED, |
227 | const gimple *stmt, | |
228 | tree var, | |
229 | state_machine::state_t from, | |
230 | state_machine::state_t to, | |
231 | tree origin = NULL_TREE) | |
232 | { | |
233 | state_machine::state_t current = get_state (stmt, var); | |
234 | if (current == from) | |
235 | set_next_state (stmt, var, to, origin); | |
236 | } | |
757bf1df | 237 | |
48e8a7a6 DM |
238 | void on_transition (const supernode *node ATTRIBUTE_UNUSED, |
239 | const gimple *stmt, | |
240 | const svalue *var, | |
241 | state_machine::state_t from, | |
242 | state_machine::state_t to, | |
243 | tree origin = NULL_TREE) | |
244 | { | |
245 | state_machine::state_t current = get_state (stmt, var); | |
246 | if (current == from) | |
247 | set_next_state (stmt, var, to, origin); | |
248 | } | |
249 | ||
757bf1df | 250 | /* Called by state_machine in response to pattern matches: |
25ef215a DM |
251 | issue a diagnostic D using NODE and STMT for location information. */ |
252 | virtual void warn (const supernode *node, const gimple *stmt, | |
253 | tree var, pending_diagnostic *d) = 0; | |
2402dc6b DM |
254 | virtual void warn (const supernode *node, const gimple *stmt, |
255 | const svalue *var, pending_diagnostic *d) = 0; | |
757bf1df | 256 | |
808f4dfe DM |
257 | /* For use when generating trees when creating pending_diagnostics, so that |
258 | rather than e.g. | |
259 | "double-free of '<unknown>'" | |
260 | we can print: | |
261 | "double-free of 'inbuf.data'". */ | |
262 | virtual tree get_diagnostic_tree (tree expr) | |
757bf1df DM |
263 | { |
264 | return expr; | |
265 | } | |
48e8a7a6 | 266 | virtual tree get_diagnostic_tree (const svalue *) = 0; |
757bf1df DM |
267 | |
268 | virtual state_machine::state_t get_global_state () const = 0; | |
269 | virtual void set_global_state (state_machine::state_t) = 0; | |
270 | ||
271 | /* A vfunc for handling custom transitions, such as when registering | |
272 | a signal handler. */ | |
273 | virtual void on_custom_transition (custom_transition *transition) = 0; | |
274 | ||
808f4dfe DM |
275 | /* If STMT is an assignment known to assign zero to its LHS, return |
276 | the LHS. | |
277 | Otherwise return NULL_TREE. */ | |
278 | virtual tree is_zero_assignment (const gimple *stmt) = 0; | |
279 | ||
eafa9d96 DM |
280 | virtual path_context *get_path_context () const |
281 | { | |
282 | return NULL; | |
283 | } | |
284 | ||
2c16dfe6 DM |
285 | /* Are we handling an external function with unknown side effects? */ |
286 | virtual bool unknown_side_effects_p () const { return false; } | |
287 | ||
a61aaee6 | 288 | virtual const program_state *get_old_program_state () const = 0; |
2402dc6b | 289 | virtual const program_state *get_new_program_state () const = 0; |
a61aaee6 | 290 | |
897b3b31 DM |
291 | const region_model *get_old_region_model () const; |
292 | ||
757bf1df DM |
293 | protected: |
294 | sm_context (int sm_idx, const state_machine &sm) | |
295 | : m_sm_idx (sm_idx), m_sm (sm) {} | |
296 | ||
297 | int m_sm_idx; | |
298 | const state_machine &m_sm; | |
299 | }; | |
300 | ||
301 | ||
302 | /* The various state_machine subclasses are hidden in their respective | |
303 | implementation files. */ | |
304 | ||
305 | extern void make_checkers (auto_delete_vec <state_machine> &out, | |
306 | logger *logger); | |
307 | ||
308 | extern state_machine *make_malloc_state_machine (logger *logger); | |
309 | extern state_machine *make_fileptr_state_machine (logger *logger); | |
310 | extern state_machine *make_taint_state_machine (logger *logger); | |
311 | extern state_machine *make_sensitive_state_machine (logger *logger); | |
312 | extern state_machine *make_signal_state_machine (logger *logger); | |
313 | extern state_machine *make_pattern_test_state_machine (logger *logger); | |
2402dc6b | 314 | extern state_machine *make_va_list_state_machine (logger *logger); |
97baacba | 315 | extern state_machine *make_fd_state_machine (logger *logger); |
757bf1df | 316 | |
75038aa6 DM |
317 | } // namespace ana |
318 | ||
757bf1df | 319 | #endif /* GCC_ANALYZER_SM_H */ |