1 /* Modeling API uses and misuses via state machines.
2 Copyright (C) 2019-2020 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)
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/>. */
23 #include "coretypes.h"
26 #include "basic-block.h"
30 #include "diagnostic-core.h"
31 #include "pretty-print.h"
32 #include "analyzer/analyzer.h"
33 #include "analyzer/analyzer-logging.h"
34 #include "analyzer/sm.h"
40 /* Return true if VAR has pointer or reference type. */
43 any_pointer_p (tree var
)
45 return POINTER_TYPE_P (TREE_TYPE (var
));
48 /* Add a state with name NAME to this state_machine.
49 The string is required to outlive the state_machine.
51 Return the state_t for the new state. */
53 state_machine::state_t
54 state_machine::add_state (const char *name
)
56 m_state_names
.safe_push (name
);
57 return m_state_names
.length () - 1;
60 /* Get the name of state S within this state_machine. */
63 state_machine::get_state_name (state_t s
) const
65 return m_state_names
[s
];
68 /* Get the state with name NAME, which must exist.
69 This is purely intended for use in selftests. */
71 state_machine::state_t
72 state_machine::get_state_by_name (const char *name
)
75 const char *iter_name
;
76 FOR_EACH_VEC_ELT (m_state_names
, i
, iter_name
)
77 if (!strcmp (name
, iter_name
))
83 /* Assert that S is a valid state for this state_machine. */
86 state_machine::validate (state_t s
) const
88 gcc_assert (s
< m_state_names
.length ());
91 /* Dump a multiline representation of this state machine to PP. */
94 state_machine::dump_to_pp (pretty_printer
*pp
) const
98 FOR_EACH_VEC_ELT (m_state_names
, i
, name
)
99 pp_printf (pp
, " state %i: %qs\n", i
, name
);
102 /* Create instances of the various state machines, each using LOGGER,
103 and populate OUT with them. */
106 make_checkers (auto_delete_vec
<state_machine
> &out
, logger
*logger
)
108 out
.safe_push (make_malloc_state_machine (logger
));
109 out
.safe_push (make_fileptr_state_machine (logger
));
110 /* The "taint" checker must be explicitly enabled (as it currently
111 leads to state explosions that stop the other checkers working). */
112 if (flag_analyzer_checker
)
113 out
.safe_push (make_taint_state_machine (logger
));
114 out
.safe_push (make_sensitive_state_machine (logger
));
115 out
.safe_push (make_signal_state_machine (logger
));
117 /* We only attempt to run the pattern tests if it might have been manually
118 enabled (for DejaGnu purposes). */
119 if (flag_analyzer_checker
)
120 out
.safe_push (make_pattern_test_state_machine (logger
));
122 if (flag_analyzer_checker
)
124 unsigned read_index
, write_index
;
127 /* TODO: this leaks the machines
128 Would be nice to log the things that were removed. */
129 VEC_ORDERED_REMOVE_IF (out
, read_index
, write_index
, sm
,
130 0 != strcmp (flag_analyzer_checker
,
131 (*sm
)->get_name ()));
137 #endif /* #if ENABLE_ANALYZER */