]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/analyzer/sm.h
Update copyright years.
[thirdparty/gcc.git] / gcc / analyzer / sm.h
CommitLineData
757bf1df 1/* Modeling API uses and misuses via state machines.
99dee823 2 Copyright (C) 2019-2021 Free Software Foundation, Inc.
757bf1df
DM
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_SM_H
22#define GCC_ANALYZER_SM_H
23
24/* Utility functions for use by state machines. */
25
75038aa6
DM
26namespace ana {
27
757bf1df
DM
28class state_machine;
29class sm_context;
30class pending_diagnostic;
31
808f4dfe
DM
32extern bool any_pointer_p (tree var);
33
757bf1df 34/* An abstract base class for a state machine describing an API.
10fc42a8 35 Manages a set of state objects, and has various virtual functions
757bf1df
DM
36 for pattern-matching on statements. */
37
38class state_machine : public log_user
39{
40public:
10fc42a8
DM
41 /* States are represented by immutable objects, owned by the state
42 machine. */
43 class state
44 {
45 public:
46 state (const char *name, unsigned id) : m_name (name), m_id (id) {}
47 virtual ~state () {}
48
49 const char *get_name () const { return m_name; }
50 virtual void dump_to_pp (pretty_printer *pp) const;
809192e7 51 virtual json::value *to_json () const;
10fc42a8
DM
52
53 unsigned get_id () const { return m_id; }
757bf1df 54
10fc42a8
DM
55 private:
56 const char *m_name;
57 unsigned m_id;
58 };
59 typedef const state_machine::state *state_t;
757bf1df 60
10fc42a8 61 state_machine (const char *name, logger *logger);
757bf1df
DM
62 virtual ~state_machine () {}
63
64 /* Should states be inherited from a parent region to a child region,
65 when first accessing a child region?
66 For example we should inherit the taintedness of a subregion,
67 but we should not inherit the "malloc:non-null" state of a field
68 within a heap-allocated struct. */
69 virtual bool inherited_state_p () const = 0;
70
808f4dfe
DM
71 virtual state_machine::state_t get_default_state (const svalue *) const
72 {
10fc42a8 73 return m_start;
808f4dfe
DM
74 }
75
757bf1df
DM
76 const char *get_name () const { return m_name; }
77
10fc42a8 78 state_t get_state_by_name (const char *name) const;
41f99ba6 79
757bf1df
DM
80 /* Return true if STMT is a function call recognized by this sm. */
81 virtual bool on_stmt (sm_context *sm_ctxt,
82 const supernode *node,
83 const gimple *stmt) const = 0;
84
8525d1f5
DM
85 virtual void on_phi (sm_context *sm_ctxt ATTRIBUTE_UNUSED,
86 const supernode *node ATTRIBUTE_UNUSED,
87 const gphi *phi ATTRIBUTE_UNUSED,
88 tree rhs ATTRIBUTE_UNUSED) const
89 {
90 }
91
757bf1df
DM
92 virtual void on_condition (sm_context *sm_ctxt,
93 const supernode *node,
94 const gimple *stmt,
95 tree lhs, enum tree_code op, tree rhs) const = 0;
96
97 /* Return true if it safe to discard the given state (to help
98 when simplifying state objects).
99 States that need leak detection should return false. */
100 virtual bool can_purge_p (state_t s) const = 0;
101
102 /* Called when VAR leaks (and !can_purge_p). */
103 virtual pending_diagnostic *on_leak (tree var ATTRIBUTE_UNUSED) const
104 {
105 return NULL;
106 }
107
808f4dfe
DM
108 /* Return true if S should be reset to "start" for values passed (or reachable
109 from) calls to unknown functions. IS_MUTABLE is true for pointers as
110 non-const, false if only passed as const-pointers.
111
112 For example, in sm-malloc.cc, an on-stack ptr doesn't stop being
113 stack-allocated when passed to an unknown fn, but a malloc-ed pointer
114 could be freed when passed to an unknown fn (unless passed as "const"). */
115 virtual bool reset_when_passed_to_unknown_fn_p (state_t s ATTRIBUTE_UNUSED,
116 bool is_mutable) const
117 {
118 return is_mutable;
119 }
120
757bf1df
DM
121 void validate (state_t s) const;
122
42f36563
DM
123 void dump_to_pp (pretty_printer *pp) const;
124
809192e7
DM
125 json::object *to_json () const;
126
10fc42a8
DM
127 state_t get_start_state () const { return m_start; }
128
757bf1df
DM
129protected:
130 state_t add_state (const char *name);
1690a839
DM
131 state_t add_custom_state (state *s)
132 {
133 m_states.safe_push (s);
134 return s;
135 }
136
10fc42a8 137 unsigned alloc_state_id () { return m_next_state_id++; }
757bf1df
DM
138
139private:
140 DISABLE_COPY_AND_ASSIGN (state_machine);
141
142 const char *m_name;
757bf1df 143
10fc42a8
DM
144 /* States are owned by the state_machine. */
145 auto_delete_vec<state> m_states;
757bf1df 146
10fc42a8
DM
147 unsigned m_next_state_id;
148
149protected:
150 /* Must be inited after m_next_state_id. */
151 state_t m_start;
152};
757bf1df
DM
153
154/* Abstract base class for state machines to pass to
155 sm_context::on_custom_transition for handling non-standard transitions
156 (e.g. adding a node and edge to simulate registering a callback and having
157 the callback be called later). */
158
159class custom_transition
160{
161public:
162 virtual ~custom_transition () {}
163 virtual void impl_transition (exploded_graph *eg,
164 exploded_node *src_enode,
165 int sm_idx) = 0;
166};
167
168/* Abstract base class giving an interface for the state machine to call
169 the checker engine, at a particular stmt. */
170
171class sm_context
172{
173public:
174 virtual ~sm_context () {}
175
176 /* Get the fndecl used at call, or NULL_TREE.
177 Use in preference to gimple_call_fndecl (and gimple_call_addr_fndecl),
178 since it can look through function pointer assignments and
179 other callback handling. */
180 virtual tree get_fndecl_for_call (const gcall *call) = 0;
181
6d9ca8c8
DM
182 /* Get the old state of VAR at STMT. */
183 virtual state_machine::state_t get_state (const gimple *stmt,
184 tree var) = 0;
185 /* Set the next state of VAR to be TO, recording the "origin" of the
186 state as ORIGIN.
187 Use STMT for location information. */
188 virtual void set_next_state (const gimple *stmt,
189 tree var,
190 state_machine::state_t to,
191 tree origin = NULL_TREE) = 0;
192
757bf1df
DM
193 /* Called by state_machine in response to pattern matches:
194 if VAR is in state FROM, transition it to state TO, potentially
195 recording the "origin" of the state as ORIGIN.
196 Use NODE and STMT for location information. */
6d9ca8c8
DM
197 void on_transition (const supernode *node ATTRIBUTE_UNUSED,
198 const gimple *stmt,
199 tree var,
200 state_machine::state_t from,
201 state_machine::state_t to,
202 tree origin = NULL_TREE)
203 {
204 state_machine::state_t current = get_state (stmt, var);
205 if (current == from)
206 set_next_state (stmt, var, to, origin);
207 }
757bf1df
DM
208
209 /* Called by state_machine in response to pattern matches:
25ef215a
DM
210 issue a diagnostic D using NODE and STMT for location information. */
211 virtual void warn (const supernode *node, const gimple *stmt,
212 tree var, pending_diagnostic *d) = 0;
757bf1df 213
808f4dfe
DM
214 /* For use when generating trees when creating pending_diagnostics, so that
215 rather than e.g.
216 "double-free of '<unknown>'"
217 we can print:
218 "double-free of 'inbuf.data'". */
219 virtual tree get_diagnostic_tree (tree expr)
757bf1df
DM
220 {
221 return expr;
222 }
223
224 virtual state_machine::state_t get_global_state () const = 0;
225 virtual void set_global_state (state_machine::state_t) = 0;
226
227 /* A vfunc for handling custom transitions, such as when registering
228 a signal handler. */
229 virtual void on_custom_transition (custom_transition *transition) = 0;
230
808f4dfe
DM
231 /* If STMT is an assignment known to assign zero to its LHS, return
232 the LHS.
233 Otherwise return NULL_TREE. */
234 virtual tree is_zero_assignment (const gimple *stmt) = 0;
235
757bf1df
DM
236protected:
237 sm_context (int sm_idx, const state_machine &sm)
238 : m_sm_idx (sm_idx), m_sm (sm) {}
239
240 int m_sm_idx;
241 const state_machine &m_sm;
242};
243
244
245/* The various state_machine subclasses are hidden in their respective
246 implementation files. */
247
248extern void make_checkers (auto_delete_vec <state_machine> &out,
249 logger *logger);
250
251extern state_machine *make_malloc_state_machine (logger *logger);
252extern state_machine *make_fileptr_state_machine (logger *logger);
253extern state_machine *make_taint_state_machine (logger *logger);
254extern state_machine *make_sensitive_state_machine (logger *logger);
255extern state_machine *make_signal_state_machine (logger *logger);
256extern state_machine *make_pattern_test_state_machine (logger *logger);
257
75038aa6
DM
258} // namespace ana
259
757bf1df 260#endif /* GCC_ANALYZER_SM_H */