xref: /netbsd-src/external/gpl3/gcc.old/dist/gcc/analyzer/sm.cc (revision 4c3eb207d36f67d31994830c0a694161fc1ca39b)
1*4c3eb207Smrg /* Modeling API uses and misuses via state machines.
2*4c3eb207Smrg    Copyright (C) 2019-2020 Free Software Foundation, Inc.
3*4c3eb207Smrg    Contributed by David Malcolm <dmalcolm@redhat.com>.
4*4c3eb207Smrg 
5*4c3eb207Smrg This file is part of GCC.
6*4c3eb207Smrg 
7*4c3eb207Smrg GCC is free software; you can redistribute it and/or modify it
8*4c3eb207Smrg under the terms of the GNU General Public License as published by
9*4c3eb207Smrg the Free Software Foundation; either version 3, or (at your option)
10*4c3eb207Smrg any later version.
11*4c3eb207Smrg 
12*4c3eb207Smrg GCC is distributed in the hope that it will be useful, but
13*4c3eb207Smrg WITHOUT ANY WARRANTY; without even the implied warranty of
14*4c3eb207Smrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15*4c3eb207Smrg General Public License for more details.
16*4c3eb207Smrg 
17*4c3eb207Smrg You should have received a copy of the GNU General Public License
18*4c3eb207Smrg along with GCC; see the file COPYING3.  If not see
19*4c3eb207Smrg <http://www.gnu.org/licenses/>.  */
20*4c3eb207Smrg 
21*4c3eb207Smrg #include "config.h"
22*4c3eb207Smrg #include "system.h"
23*4c3eb207Smrg #include "coretypes.h"
24*4c3eb207Smrg #include "tree.h"
25*4c3eb207Smrg #include "function.h"
26*4c3eb207Smrg #include "basic-block.h"
27*4c3eb207Smrg #include "gimple.h"
28*4c3eb207Smrg #include "options.h"
29*4c3eb207Smrg #include "function.h"
30*4c3eb207Smrg #include "diagnostic-core.h"
31*4c3eb207Smrg #include "pretty-print.h"
32*4c3eb207Smrg #include "analyzer/analyzer.h"
33*4c3eb207Smrg #include "analyzer/analyzer-logging.h"
34*4c3eb207Smrg #include "analyzer/sm.h"
35*4c3eb207Smrg 
36*4c3eb207Smrg #if ENABLE_ANALYZER
37*4c3eb207Smrg 
38*4c3eb207Smrg /* If STMT is an assignment from zero, return the LHS.  */
39*4c3eb207Smrg 
40*4c3eb207Smrg tree
is_zero_assignment(const gimple * stmt)41*4c3eb207Smrg is_zero_assignment (const gimple *stmt)
42*4c3eb207Smrg {
43*4c3eb207Smrg   const gassign *assign_stmt = dyn_cast <const gassign *> (stmt);
44*4c3eb207Smrg   if (!assign_stmt)
45*4c3eb207Smrg     return NULL_TREE;
46*4c3eb207Smrg 
47*4c3eb207Smrg   enum tree_code op = gimple_assign_rhs_code (assign_stmt);
48*4c3eb207Smrg   if (TREE_CODE_CLASS (op) != tcc_constant)
49*4c3eb207Smrg     return NULL_TREE;
50*4c3eb207Smrg 
51*4c3eb207Smrg   if (!zerop (gimple_assign_rhs1 (assign_stmt)))
52*4c3eb207Smrg     return NULL_TREE;
53*4c3eb207Smrg 
54*4c3eb207Smrg   return gimple_assign_lhs (assign_stmt);
55*4c3eb207Smrg }
56*4c3eb207Smrg 
57*4c3eb207Smrg /* Return true if VAR has pointer or reference type.  */
58*4c3eb207Smrg 
59*4c3eb207Smrg bool
any_pointer_p(tree var)60*4c3eb207Smrg any_pointer_p (tree var)
61*4c3eb207Smrg {
62*4c3eb207Smrg   return POINTER_TYPE_P (TREE_TYPE (var));
63*4c3eb207Smrg }
64*4c3eb207Smrg 
65*4c3eb207Smrg namespace ana {
66*4c3eb207Smrg 
67*4c3eb207Smrg /* Add a state with name NAME to this state_machine.
68*4c3eb207Smrg    The string is required to outlive the state_machine.
69*4c3eb207Smrg 
70*4c3eb207Smrg    Return the state_t for the new state.  */
71*4c3eb207Smrg 
72*4c3eb207Smrg state_machine::state_t
add_state(const char * name)73*4c3eb207Smrg state_machine::add_state (const char *name)
74*4c3eb207Smrg {
75*4c3eb207Smrg   m_state_names.safe_push (name);
76*4c3eb207Smrg   return m_state_names.length () - 1;
77*4c3eb207Smrg }
78*4c3eb207Smrg 
79*4c3eb207Smrg /* Get the name of state S within this state_machine.  */
80*4c3eb207Smrg 
81*4c3eb207Smrg const char *
get_state_name(state_t s) const82*4c3eb207Smrg state_machine::get_state_name (state_t s) const
83*4c3eb207Smrg {
84*4c3eb207Smrg   return m_state_names[s];
85*4c3eb207Smrg }
86*4c3eb207Smrg 
87*4c3eb207Smrg /* Get the state with name NAME, which must exist.
88*4c3eb207Smrg    This is purely intended for use in selftests.  */
89*4c3eb207Smrg 
90*4c3eb207Smrg state_machine::state_t
get_state_by_name(const char * name)91*4c3eb207Smrg state_machine::get_state_by_name (const char *name)
92*4c3eb207Smrg {
93*4c3eb207Smrg   unsigned i;
94*4c3eb207Smrg   const char *iter_name;
95*4c3eb207Smrg   FOR_EACH_VEC_ELT (m_state_names, i, iter_name)
96*4c3eb207Smrg     if (!strcmp (name, iter_name))
97*4c3eb207Smrg       return i;
98*4c3eb207Smrg   /* Name not found.  */
99*4c3eb207Smrg   gcc_unreachable ();
100*4c3eb207Smrg }
101*4c3eb207Smrg 
102*4c3eb207Smrg /* Assert that S is a valid state for this state_machine.  */
103*4c3eb207Smrg 
104*4c3eb207Smrg void
validate(state_t s) const105*4c3eb207Smrg state_machine::validate (state_t s) const
106*4c3eb207Smrg {
107*4c3eb207Smrg   gcc_assert (s < m_state_names.length ());
108*4c3eb207Smrg }
109*4c3eb207Smrg 
110*4c3eb207Smrg /* Dump a multiline representation of this state machine to PP.  */
111*4c3eb207Smrg 
112*4c3eb207Smrg void
dump_to_pp(pretty_printer * pp) const113*4c3eb207Smrg state_machine::dump_to_pp (pretty_printer *pp) const
114*4c3eb207Smrg {
115*4c3eb207Smrg   unsigned i;
116*4c3eb207Smrg   const char *name;
117*4c3eb207Smrg   FOR_EACH_VEC_ELT (m_state_names, i, name)
118*4c3eb207Smrg     pp_printf (pp, "  state %i: %qs\n", i, name);
119*4c3eb207Smrg }
120*4c3eb207Smrg 
121*4c3eb207Smrg /* Create instances of the various state machines, each using LOGGER,
122*4c3eb207Smrg    and populate OUT with them.  */
123*4c3eb207Smrg 
124*4c3eb207Smrg void
make_checkers(auto_delete_vec<state_machine> & out,logger * logger)125*4c3eb207Smrg make_checkers (auto_delete_vec <state_machine> &out, logger *logger)
126*4c3eb207Smrg {
127*4c3eb207Smrg   out.safe_push (make_malloc_state_machine (logger));
128*4c3eb207Smrg   out.safe_push (make_fileptr_state_machine (logger));
129*4c3eb207Smrg   /* The "taint" checker must be explicitly enabled (as it currently
130*4c3eb207Smrg      leads to state explosions that stop the other checkers working).  */
131*4c3eb207Smrg   if (flag_analyzer_checker)
132*4c3eb207Smrg     out.safe_push (make_taint_state_machine (logger));
133*4c3eb207Smrg   out.safe_push (make_sensitive_state_machine (logger));
134*4c3eb207Smrg   out.safe_push (make_signal_state_machine (logger));
135*4c3eb207Smrg 
136*4c3eb207Smrg   /* We only attempt to run the pattern tests if it might have been manually
137*4c3eb207Smrg      enabled (for DejaGnu purposes).  */
138*4c3eb207Smrg   if (flag_analyzer_checker)
139*4c3eb207Smrg     out.safe_push (make_pattern_test_state_machine (logger));
140*4c3eb207Smrg 
141*4c3eb207Smrg   if (flag_analyzer_checker)
142*4c3eb207Smrg     {
143*4c3eb207Smrg       unsigned read_index, write_index;
144*4c3eb207Smrg       state_machine **sm;
145*4c3eb207Smrg 
146*4c3eb207Smrg       /* TODO: this leaks the machines
147*4c3eb207Smrg 	 Would be nice to log the things that were removed.  */
148*4c3eb207Smrg       VEC_ORDERED_REMOVE_IF (out, read_index, write_index, sm,
149*4c3eb207Smrg 			     0 != strcmp (flag_analyzer_checker,
150*4c3eb207Smrg 					  (*sm)->get_name ()));
151*4c3eb207Smrg     }
152*4c3eb207Smrg }
153*4c3eb207Smrg 
154*4c3eb207Smrg } // namespace ana
155*4c3eb207Smrg 
156*4c3eb207Smrg #endif /* #if ENABLE_ANALYZER */
157