LCOV - code coverage report
Current view: top level - gcc/analyzer - sm.cc (source / functions) Hit Total Coverage
Test: gcc.info Lines: 37 44 84.1 %
Date: 2020-03-28 11:57:23 Functions: 7 8 87.5 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 0 -

           Branch data     Line data    Source code
       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 "pretty-print.h"
      32                 :            : #include "analyzer/analyzer.h"
      33                 :            : #include "analyzer/analyzer-logging.h"
      34                 :            : #include "analyzer/sm.h"
      35                 :            : 
      36                 :            : #if ENABLE_ANALYZER
      37                 :            : 
      38                 :            : /* If STMT is an assignment from zero, return the LHS.  */
      39                 :            : 
      40                 :            : tree
      41                 :      14345 : is_zero_assignment (const gimple *stmt)
      42                 :            : {
      43                 :      14345 :   const gassign *assign_stmt = dyn_cast <const gassign *> (stmt);
      44                 :       6697 :   if (!assign_stmt)
      45                 :            :     return NULL_TREE;
      46                 :            : 
      47                 :       6697 :   enum tree_code op = gimple_assign_rhs_code (assign_stmt);
      48                 :       6697 :   if (TREE_CODE_CLASS (op) != tcc_constant)
      49                 :            :     return NULL_TREE;
      50                 :            : 
      51                 :        707 :   if (!zerop (gimple_assign_rhs1 (assign_stmt)))
      52                 :            :     return NULL_TREE;
      53                 :            : 
      54                 :        330 :   return gimple_assign_lhs (assign_stmt);
      55                 :            : }
      56                 :            : 
      57                 :            : /* Return true if VAR has pointer or reference type.  */
      58                 :            : 
      59                 :            : bool
      60                 :       2169 : any_pointer_p (tree var)
      61                 :            : {
      62                 :       2169 :   return POINTER_TYPE_P (TREE_TYPE (var));
      63                 :            : }
      64                 :            : 
      65                 :            : namespace ana {
      66                 :            : 
      67                 :            : /* Add a state with name NAME to this state_machine.
      68                 :            :    The string is required to outlive the state_machine.
      69                 :            : 
      70                 :            :    Return the state_t for the new state.  */
      71                 :            : 
      72                 :            : state_machine::state_t
      73                 :       7233 : state_machine::add_state (const char *name)
      74                 :            : {
      75                 :       7233 :   m_state_names.safe_push (name);
      76                 :       7233 :   return m_state_names.length () - 1;
      77                 :            : }
      78                 :            : 
      79                 :            : /* Get the name of state S within this state_machine.  */
      80                 :            : 
      81                 :            : const char *
      82                 :        942 : state_machine::get_state_name (state_t s) const
      83                 :            : {
      84                 :        942 :   return m_state_names[s];
      85                 :            : }
      86                 :            : 
      87                 :            : /* Get the state with name NAME, which must exist.
      88                 :            :    This is purely intended for use in selftests.  */
      89                 :            : 
      90                 :            : state_machine::state_t
      91                 :          2 : state_machine::get_state_by_name (const char *name)
      92                 :            : {
      93                 :          2 :   unsigned i;
      94                 :          2 :   const char *iter_name;
      95                 :          4 :   FOR_EACH_VEC_ELT (m_state_names, i, iter_name)
      96                 :          4 :     if (!strcmp (name, iter_name))
      97                 :          2 :       return i;
      98                 :            :   /* Name not found.  */
      99                 :          0 :   gcc_unreachable ();
     100                 :            : }
     101                 :            : 
     102                 :            : /* Assert that S is a valid state for this state_machine.  */
     103                 :            : 
     104                 :            : void
     105                 :     105894 : state_machine::validate (state_t s) const
     106                 :            : {
     107                 :     211788 :   gcc_assert (s < m_state_names.length ());
     108                 :     105894 : }
     109                 :            : 
     110                 :            : /* Dump a multiline representation of this state machine to PP.  */
     111                 :            : 
     112                 :            : void
     113                 :          0 : state_machine::dump_to_pp (pretty_printer *pp) const
     114                 :            : {
     115                 :          0 :   unsigned i;
     116                 :          0 :   const char *name;
     117                 :          0 :   FOR_EACH_VEC_ELT (m_state_names, i, name)
     118                 :          0 :     pp_printf (pp, "  state %i: %qs\n", i, name);
     119                 :          0 : }
     120                 :            : 
     121                 :            : /* Create instances of the various state machines, each using LOGGER,
     122                 :            :    and populate OUT with them.  */
     123                 :            : 
     124                 :            : void
     125                 :        377 : make_checkers (auto_delete_vec <state_machine> &out, logger *logger)
     126                 :            : {
     127                 :        377 :   out.safe_push (make_malloc_state_machine (logger));
     128                 :        377 :   out.safe_push (make_fileptr_state_machine (logger));
     129                 :            :   /* The "taint" checker must be explicitly enabled (as it currently
     130                 :            :      leads to state explosions that stop the other checkers working).  */
     131                 :        377 :   if (flag_analyzer_checker)
     132                 :          6 :     out.safe_push (make_taint_state_machine (logger));
     133                 :        377 :   out.safe_push (make_sensitive_state_machine (logger));
     134                 :        377 :   out.safe_push (make_signal_state_machine (logger));
     135                 :            : 
     136                 :            :   /* We only attempt to run the pattern tests if it might have been manually
     137                 :            :      enabled (for DejaGnu purposes).  */
     138                 :        377 :   if (flag_analyzer_checker)
     139                 :          6 :     out.safe_push (make_pattern_test_state_machine (logger));
     140                 :            : 
     141                 :        377 :   if (flag_analyzer_checker)
     142                 :            :     {
     143                 :          6 :       unsigned read_index, write_index;
     144                 :          6 :       state_machine **sm;
     145                 :            : 
     146                 :            :       /* TODO: this leaks the machines
     147                 :            :          Would be nice to log the things that were removed.  */
     148                 :         48 :       VEC_ORDERED_REMOVE_IF (out, read_index, write_index, sm,
     149                 :            :                              0 != strcmp (flag_analyzer_checker,
     150                 :            :                                           (*sm)->get_name ()));
     151                 :            :     }
     152                 :        377 : }
     153                 :            : 
     154                 :            : } // namespace ana
     155                 :            : 
     156                 :            : #endif /* #if ENABLE_ANALYZER */

Generated by: LCOV version 1.0

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto --enable-host-shared. GCC test suite is run with the built compiler.