Initial commit of analyzer
[gcc.git] / gcc / analyzer / sm.cc
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>.
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 #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 "analyzer/analyzer.h"
32 #include "analyzer/analyzer-logging.h"
33 #include "analyzer/sm.h"
34
35 #if ENABLE_ANALYZER
36
37 /* If STMT is an assignment from zero, return the LHS. */
38
39 tree
40 is_zero_assignment (const gimple *stmt)
41 {
42 const gassign *assign_stmt = dyn_cast <const gassign *> (stmt);
43 if (!assign_stmt)
44 return NULL_TREE;
45
46 enum tree_code op = gimple_assign_rhs_code (assign_stmt);
47 if (TREE_CODE_CLASS (op) != tcc_constant)
48 return NULL_TREE;
49
50 if (!zerop (gimple_assign_rhs1 (assign_stmt)))
51 return NULL_TREE;
52
53 return gimple_assign_lhs (assign_stmt);
54 }
55
56 /* Return true if VAR has pointer or reference type. */
57
58 bool
59 any_pointer_p (tree var)
60 {
61 return POINTER_TYPE_P (TREE_TYPE (var));
62 }
63
64 /* Add a state with name NAME to this state_machine.
65 The string is required to outlive the state_machine.
66
67 Return the state_t for the new state. */
68
69 state_machine::state_t
70 state_machine::add_state (const char *name)
71 {
72 m_state_names.safe_push (name);
73 return m_state_names.length () - 1;
74 }
75
76 /* Get the name of state S within this state_machine. */
77
78 const char *
79 state_machine::get_state_name (state_t s) const
80 {
81 return m_state_names[s];
82 }
83
84 /* Assert that S is a valid state for this state_machine. */
85
86 void
87 state_machine::validate (state_t s) const
88 {
89 gcc_assert (s < m_state_names.length ());
90 }
91
92 /* Create instances of the various state machines, each using LOGGER,
93 and populate OUT with them. */
94
95 void
96 make_checkers (auto_delete_vec <state_machine> &out, logger *logger)
97 {
98 out.safe_push (make_malloc_state_machine (logger));
99 out.safe_push (make_fileptr_state_machine (logger));
100 out.safe_push (make_taint_state_machine (logger));
101 out.safe_push (make_sensitive_state_machine (logger));
102 out.safe_push (make_signal_state_machine (logger));
103
104 /* We only attempt to run the pattern tests if it might have been manually
105 enabled (for DejaGnu purposes). */
106 if (flag_analyzer_checker)
107 out.safe_push (make_pattern_test_state_machine (logger));
108
109 if (flag_analyzer_checker)
110 {
111 unsigned read_index, write_index;
112 state_machine **sm;
113
114 /* TODO: this leaks the machines
115 Would be nice to log the things that were removed. */
116 VEC_ORDERED_REMOVE_IF (out, read_index, write_index, sm,
117 0 != strcmp (flag_analyzer_checker,
118 (*sm)->get_name ()));
119 }
120 }
121
122 #endif /* #if ENABLE_ANALYZER */