LCOV - code coverage report
Current view: top level - gcc/analyzer - poisoned-value-diagnostic.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 90.8 % 98 89
Test Date: 2026-11-14 16:39:50 Functions: 100.0 % 12 12
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Implementation of class ana::poisoned_value_diagnostic.
       2              :    Copyright (C) 2019-2026 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 "analyzer/common.h"
      22              : 
      23              : #include "analyzer/region-model.h"
      24              : #include "analyzer/feasible-graph.h"
      25              : #include "diagnostics/sarif-sink.h"
      26              : 
      27              : #if ENABLE_ANALYZER
      28              : 
      29              : namespace ana {
      30              : 
      31              : // struct poison_finder : public symbolic::node_visitor
      32              : 
      33              : bool
      34         6600 : poison_finder::visit_svalue (const svalue &sval)
      35              : {
      36         6600 :   if (!symbolic::node_visitor::visit_svalue (sval))
      37              :     return false;
      38         6600 :   if (auto *poisoned = sval.dyn_cast_poisoned_svalue ())
      39              :     {
      40            0 :       m_found_poison = true;
      41            0 :       m_pkind = poisoned->get_poison_kind ();
      42              :     }
      43              :   return true;
      44              : }
      45              : 
      46              : bool
      47         2959 : poison_finder::visit_bit_vector (const symbolic::bit_vector &bv)
      48              : {
      49         2959 :   if (!symbolic::node_visitor::visit_bit_vector (bv))
      50              :     return false;
      51         2959 :   if (auto *poisoned = bv.dyn_cast_poisoned_bit_vector ())
      52              :     {
      53         1412 :       m_found_poison = true;
      54         1412 :       m_pkind = poisoned->get_poison_kind ();
      55              :     }
      56              :   return true;
      57              : }
      58              : 
      59              : /* A subclass of pending_diagnostic for complaining about uses of
      60              :    poisoned values.  */
      61              : 
      62              : class poisoned_value_diagnostic
      63              : : public pending_diagnostic_subclass<poisoned_value_diagnostic>
      64              : {
      65              : public:
      66         2189 :   poisoned_value_diagnostic (tree expr, enum poison_kind pkind,
      67              :                              const region *src_region,
      68              :                              tree check_expr)
      69         2189 :   : m_expr (expr), m_pkind (pkind),
      70         2189 :     m_src_region (src_region),
      71         2189 :     m_check_expr (check_expr)
      72              :   {}
      73              : 
      74        14824 :   const char *get_kind () const final override { return "poisoned_value_diagnostic"; }
      75              : 
      76           98 :   bool use_of_uninit_p () const final override
      77              :   {
      78           98 :     return m_pkind == poison_kind::uninit;
      79              :   }
      80              : 
      81         1426 :   bool operator== (const poisoned_value_diagnostic &other) const
      82              :   {
      83         1426 :     return (m_expr == other.m_expr
      84         1401 :             && m_pkind == other.m_pkind
      85         2827 :             && m_src_region == other.m_src_region);
      86              :   }
      87              : 
      88         2062 :   int get_controlling_option () const final override
      89              :   {
      90         2062 :     switch (m_pkind)
      91              :       {
      92            0 :       default:
      93            0 :         gcc_unreachable ();
      94              :       case poison_kind::uninit:
      95              :         return OPT_Wanalyzer_use_of_uninitialized_value;
      96              :       case poison_kind::freed:
      97              :       case poison_kind::deleted:
      98              :         return OPT_Wanalyzer_use_after_free;
      99              :       case poison_kind::popped_stack:
     100              :         return OPT_Wanalyzer_use_of_pointer_in_stale_stack_frame;
     101              :       }
     102              :   }
     103              : 
     104         1478 :   bool terminate_path_p () const final override { return true; }
     105              : 
     106          584 :   bool emit (diagnostic_emission_context &ctxt) final override
     107              :   {
     108          584 :     switch (m_pkind)
     109              :       {
     110            0 :       default:
     111            0 :         gcc_unreachable ();
     112          552 :       case poison_kind::uninit:
     113          552 :         {
     114          552 :           ctxt.add_cwe (457); /* "CWE-457: Use of Uninitialized Variable".  */
     115          552 :           return ctxt.warn ("use of uninitialized value %qE",
     116          552 :                             m_expr);
     117              :         }
     118            8 :         break;
     119            8 :       case poison_kind::freed:
     120            8 :         {
     121            8 :           ctxt.add_cwe (416); /* "CWE-416: Use After Free".  */
     122            8 :           return ctxt.warn ("use after %<free%> of %qE",
     123            8 :                             m_expr);
     124              :         }
     125            9 :         break;
     126            9 :       case poison_kind::deleted:
     127            9 :         {
     128            9 :           ctxt.add_cwe (416); /* "CWE-416: Use After Free".  */
     129            9 :           return ctxt.warn ("use after %<delete%> of %qE",
     130            9 :                             m_expr);
     131              :         }
     132           15 :         break;
     133           15 :       case poison_kind::popped_stack:
     134           15 :         {
     135              :           /* TODO: which CWE?  */
     136           15 :           return ctxt.warn
     137           15 :             ("dereferencing pointer %qE to within stale stack frame",
     138           15 :              m_expr);
     139              :         }
     140              :         break;
     141              :       }
     142              :   }
     143              : 
     144              :   bool
     145         1168 :   describe_final_event (pretty_printer &pp,
     146              :                         const evdesc::final_event &) final override
     147              :   {
     148         1168 :     switch (m_pkind)
     149              :       {
     150            0 :       default:
     151            0 :         gcc_unreachable ();
     152         1104 :       case poison_kind::uninit:
     153         1104 :         {
     154         1104 :           pp_printf (&pp,
     155              :                      "use of uninitialized value %qE here",
     156              :                      m_expr);
     157         1104 :           return true;
     158              :         }
     159           16 :       case poison_kind::freed:
     160           16 :         {
     161           16 :           pp_printf (&pp,
     162              :                      "use after %<free%> of %qE here",
     163              :                      m_expr);
     164           16 :           return true;
     165              :         }
     166           18 :       case poison_kind::deleted:
     167           18 :         {
     168           18 :           pp_printf (&pp,
     169              :                      "use after %<delete%> of %qE here",
     170              :                      m_expr);
     171           18 :           return true;
     172              :         }
     173           30 :       case poison_kind::popped_stack:
     174           30 :         {
     175           30 :           pp_printf (&pp,
     176              :                      "dereferencing pointer %qE to within stale stack frame",
     177              :                      m_expr);
     178           30 :           return true;
     179              :         }
     180              :       }
     181              :   }
     182              : 
     183         1168 :   void mark_interesting_stuff (interesting_t *interest) final override
     184              :   {
     185         1168 :     if (m_src_region)
     186              :       {
     187         1088 :         interest->add_region_creation (m_src_region);
     188         1088 :         interest->add_read_region (m_src_region, "poisoned value");
     189              :       }
     190         1168 :   }
     191              : 
     192              :   /* Attempt to suppress false positives.
     193              :      Reject paths where the value of the underlying region isn't poisoned.
     194              :      This can happen due to state merging when exploring the exploded graph,
     195              :      where the more precise analysis during feasibility analysis finds that
     196              :      the region is in fact valid.
     197              :      To do this we need to get the value from the fgraph.  Unfortunately
     198              :      we can't simply query the state of m_src_region (from the enode),
     199              :      since it might be a different region in the fnode state (e.g. with
     200              :      heap-allocated regions, the numbering could be different).
     201              :      Hence we access m_check_expr, if available.  */
     202              : 
     203         1386 :   bool check_valid_fpath_p (const feasible_node &fnode)
     204              :     const final override
     205              :   {
     206         1386 :     if (!m_check_expr)
     207              :       return true;
     208         1241 :     const svalue *fsval = fnode.get_model ().get_rvalue (m_check_expr, nullptr);
     209              :     /* Check to see if the expr is also poisoned in FNODE (and in the
     210              :        same way).  */
     211         1241 :     poison_finder v;
     212         1241 :     fsval->accept_node_visitor (v);
     213         1241 :     if (!v.m_found_poison)
     214              :       return false;
     215         1241 :     if (v.m_pkind != m_pkind)
     216            0 :       return false;
     217              :     return true;
     218         1241 :   }
     219              : 
     220              :   void
     221            8 :   maybe_add_sarif_properties (diagnostics::sarif_object &result_obj)
     222              :     const final override
     223              :   {
     224            8 :     auto &props = result_obj.get_or_create_properties ();
     225              : #define PROPERTY_PREFIX "gcc/analyzer/poisoned_value_diagnostic/"
     226            8 :     props.set (PROPERTY_PREFIX "expr", tree_to_json (m_expr));
     227            8 :     props.set_string (PROPERTY_PREFIX "kind", poison_kind_to_str (m_pkind));
     228            8 :     if (m_src_region)
     229            8 :       props.set (PROPERTY_PREFIX "src_region", m_src_region->to_json ());
     230            8 :     props.set (PROPERTY_PREFIX "check_expr", tree_to_json (m_check_expr));
     231              : #undef PROPERTY_PREFIX
     232            8 :   }
     233              : 
     234              : private:
     235              :   tree m_expr;
     236              :   enum poison_kind m_pkind;
     237              :   const region *m_src_region;
     238              :   tree m_check_expr;
     239              : };
     240              : 
     241              : std::unique_ptr<pending_diagnostic>
     242         2189 : make_poisoned_value_diagnostic (tree expr, enum poison_kind pkind,
     243              :                                 const region *src_region,
     244              :                                 tree check_expr)
     245              : {
     246         2189 :   return std::make_unique<poisoned_value_diagnostic> (expr, pkind,
     247         2189 :                                                       src_region, check_expr);
     248              : }
     249              : 
     250              : } // namespace ana
     251              : 
     252              : #endif /* #if ENABLE_ANALYZER */
        

Generated by: LCOV version 2.5.0-beta

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,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.