d: Merge upstream dmd 56589f0f4, druntime 651389b5, phobos 1516ecad9.
[official-gcc.git] / gcc / analyzer / sm.cc
blob24c20b894cdd31afd947e6a4a3bb8ec28725b576
1 /* Modeling API uses and misuses via state machines.
2 Copyright (C) 2019-2022 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 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tree.h"
25 #include "function.h"
26 #include "basic-block.h"
27 #include "gimple.h"
28 #include "options.h"
29 #include "function.h"
30 #include "diagnostic-core.h"
31 #include "pretty-print.h"
32 #include "diagnostic.h"
33 #include "tree-diagnostic.h"
34 #include "json.h"
35 #include "analyzer/analyzer.h"
36 #include "analyzer/analyzer-logging.h"
37 #include "analyzer/sm.h"
38 #include "tristate.h"
39 #include "analyzer/call-string.h"
40 #include "analyzer/program-point.h"
41 #include "analyzer/store.h"
42 #include "analyzer/svalue.h"
44 #if ENABLE_ANALYZER
46 namespace ana {
48 /* Return true if VAR has pointer or reference type. */
50 bool
51 any_pointer_p (tree var)
53 return POINTER_TYPE_P (TREE_TYPE (var));
56 /* Return true if SVAL has pointer or reference type. */
58 bool
59 any_pointer_p (const svalue *sval)
61 if (!sval->get_type ())
62 return false;
63 return POINTER_TYPE_P (sval->get_type ());
66 /* class state_machine::state. */
68 /* Base implementation of dump_to_pp vfunc. */
70 void
71 state_machine::state::dump_to_pp (pretty_printer *pp) const
73 pp_string (pp, m_name);
76 /* Return a new json::string describing the state. */
78 json::value *
79 state_machine::state::to_json () const
81 pretty_printer pp;
82 pp_format_decoder (&pp) = default_tree_printer;
83 dump_to_pp (&pp);
84 return new json::string (pp_formatted_text (&pp));
87 /* class state_machine. */
89 /* state_machine's ctor. */
91 state_machine::state_machine (const char *name, logger *logger)
92 : log_user (logger), m_name (name), m_next_state_id (0),
93 m_start (add_state ("start"))
97 /* Add a state with name NAME to this state_machine.
98 The string is required to outlive the state_machine.
100 Return the state_t for the new state. */
102 state_machine::state_t
103 state_machine::add_state (const char *name)
105 state *s = new state (name, alloc_state_id ());
106 m_states.safe_push (s);
107 return s;
110 /* Get the state with name NAME, which must exist.
111 This is purely intended for use in selftests. */
113 state_machine::state_t
114 state_machine::get_state_by_name (const char *name) const
116 unsigned i;
117 state *s;
118 FOR_EACH_VEC_ELT (m_states, i, s)
119 if (!strcmp (name, s->get_name ()))
120 return s;
121 /* Name not found. */
122 gcc_unreachable ();
125 /* Dump a multiline representation of this state machine to PP. */
127 void
128 state_machine::dump_to_pp (pretty_printer *pp) const
130 unsigned i;
131 state *s;
132 FOR_EACH_VEC_ELT (m_states, i, s)
134 pp_printf (pp, " state %i: ", i);
135 s->dump_to_pp (pp);
136 pp_newline (pp);
140 /* Return a new json::object of the form
141 {"name" : str,
142 "states" : [str]}. */
144 json::object *
145 state_machine::to_json () const
147 json::object *sm_obj = new json::object ();
149 sm_obj->set ("name", new json::string (m_name));
151 json::array *states_arr = new json::array ();
152 unsigned i;
153 state *s;
154 FOR_EACH_VEC_ELT (m_states, i, s)
155 states_arr->append (s->to_json ());
156 sm_obj->set ("states", states_arr);
159 return sm_obj;
162 /* Create instances of the various state machines, each using LOGGER,
163 and populate OUT with them. */
165 void
166 make_checkers (auto_delete_vec <state_machine> &out, logger *logger)
168 out.safe_push (make_malloc_state_machine (logger));
169 out.safe_push (make_fileptr_state_machine (logger));
170 out.safe_push (make_fd_state_machine (logger));
171 /* The "taint" checker must be explicitly enabled (as it currently
172 leads to state explosions that stop the other checkers working). */
173 if (flag_analyzer_checker)
174 out.safe_push (make_taint_state_machine (logger));
175 out.safe_push (make_sensitive_state_machine (logger));
176 out.safe_push (make_signal_state_machine (logger));
177 out.safe_push (make_va_list_state_machine (logger));
179 /* We only attempt to run the pattern tests if it might have been manually
180 enabled (for DejaGnu purposes). */
181 if (flag_analyzer_checker)
182 out.safe_push (make_pattern_test_state_machine (logger));
184 if (flag_analyzer_checker)
186 unsigned read_index, write_index;
187 state_machine **sm;
189 /* TODO: this leaks the machines
190 Would be nice to log the things that were removed. */
191 VEC_ORDERED_REMOVE_IF (out, read_index, write_index, sm,
192 0 != strcmp (flag_analyzer_checker,
193 (*sm)->get_name ()));
197 } // namespace ana
199 #endif /* #if ENABLE_ANALYZER */