LCOV - code coverage report
Current view: top level - gcc/analyzer - bounds-checking.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 90.6 % 596 540
Test Date: 2026-11-14 16:39:50 Functions: 96.6 % 59 57
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Bounds-checking of reads and writes to memory regions.
       2              :    Copyright (C) 2019-2026 Free Software Foundation, Inc.
       3              : 
       4              : This file is part of GCC.
       5              : 
       6              : GCC is free software; you can redistribute it and/or modify it
       7              : under the terms of the GNU General Public License as published by
       8              : the Free Software Foundation; either version 3, or (at your option)
       9              : any later version.
      10              : 
      11              : GCC is distributed in the hope that it will be useful, but
      12              : WITHOUT ANY WARRANTY; without even the implied warranty of
      13              : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14              : General Public License for more details.
      15              : 
      16              : You should have received a copy of the GNU General Public License
      17              : along with GCC; see the file COPYING3.  If not see
      18              : <http://www.gnu.org/licenses/>.  */
      19              : 
      20              : #include "analyzer/common.h"
      21              : 
      22              : #include "intl.h"
      23              : #include "diagnostics/diagram.h"
      24              : #include "diagnostics/sarif-sink.h"
      25              : 
      26              : #include "analyzer/analyzer-logging.h"
      27              : #include "analyzer/region-model.h"
      28              : #include "analyzer/checker-event.h"
      29              : #include "analyzer/checker-path.h"
      30              : #include "analyzer/access-diagram.h"
      31              : 
      32              : #if ENABLE_ANALYZER
      33              : 
      34              : namespace ana {
      35              : 
      36              : /* Abstract base class for all out-of-bounds warnings.  */
      37              : 
      38              : class out_of_bounds : public pending_diagnostic
      39              : {
      40              : public:
      41              :   class oob_region_creation_event_capacity : public region_creation_event_capacity
      42              :   {
      43              :   public:
      44          490 :     oob_region_creation_event_capacity (tree byte_capacity,
      45              :                                         const event_loc_info &loc_info,
      46              :                                         out_of_bounds &oob)
      47              :     : region_creation_event_capacity (byte_capacity,
      48              :                                       loc_info),
      49          490 :       m_oob (oob)
      50              :     {
      51              :     }
      52          490 :     void prepare_for_emission (checker_path *path,
      53              :                                pending_diagnostic *pd,
      54              :                                diagnostics::paths::event_id_t emission_id) override
      55              :     {
      56          490 :       region_creation_event_capacity::prepare_for_emission (path,
      57              :                                                             pd,
      58              :                                                             emission_id);
      59          490 :       m_oob.m_region_creation_event_id = emission_id;
      60          490 :     }
      61              :   private:
      62              :     out_of_bounds &m_oob;
      63              :   };
      64              : 
      65          851 :   out_of_bounds (const region_model &model,
      66              :                  const symbolic::memory_range &access_range,
      67              :                  tree diag_arg,
      68              :                  const svalue *sval_hint)
      69          851 :   : m_model (model),
      70          851 :     m_access_range (access_range),
      71          851 :     m_diag_arg (diag_arg),
      72          706 :     m_sval_hint (sval_hint)
      73              :   {}
      74              : 
      75          804 :   bool subclass_equal_p (const pending_diagnostic &base_other) const override
      76              :   {
      77          804 :     const out_of_bounds &other
      78              :       (static_cast <const out_of_bounds &>(base_other));
      79          804 :     return (m_access_range == other.m_access_range
      80          804 :             && pending_diagnostic::same_tree_p (m_diag_arg, other.m_diag_arg));
      81              :   }
      82              : 
      83         1453 :   int get_controlling_option () const final override
      84              :   {
      85         1453 :     return OPT_Wanalyzer_out_of_bounds;
      86              :   }
      87              : 
      88         1242 :   void mark_interesting_stuff (interesting_t *interest) final override
      89              :   {
      90         1242 :     interest->add_region_creation (&m_access_range.get_base_region ());
      91         1242 :   }
      92              : 
      93          226 :   void add_region_creation_events (const region *,
      94              :                                    tree byte_capacity,
      95              :                                    const event_loc_info &loc_info,
      96              :                                    checker_path &emission_path) override
      97              :   {
      98              :     /* The memory space is described in the diagnostic message itself,
      99              :        so we don't need an event for that.  */
     100          226 :     if (byte_capacity)
     101          136 :       emission_path.add_event
     102          136 :         (std::make_unique<oob_region_creation_event_capacity> (byte_capacity,
     103              :                                                                loc_info,
     104              :                                                                *this));
     105          226 :   }
     106              : 
     107              :   void
     108            4 :   maybe_add_sarif_properties (diagnostics::sarif_object &result_obj)
     109              :     const override
     110              :   {
     111            4 :     auto &props = result_obj.get_or_create_properties ();
     112              : #define PROPERTY_PREFIX "gcc/analyzer/out_of_bounds/"
     113            8 :     props.set_string (PROPERTY_PREFIX "dir",
     114            4 :                       get_dir () == access_direction::read ? "read" : "write");
     115            4 :     props.set (PROPERTY_PREFIX "model", m_model.to_json ());
     116            4 :     props.set (PROPERTY_PREFIX "access_range", m_access_range.to_json ());
     117            4 :     props.set (PROPERTY_PREFIX "diag_arg", tree_to_json (m_diag_arg));
     118            4 :     if (m_sval_hint)
     119            4 :       props.set (PROPERTY_PREFIX "sval_hint", m_sval_hint->to_json ());
     120            8 :     props.set (PROPERTY_PREFIX "region_creation_event_id",
     121            4 :                diagnostic_event_id_to_json (m_region_creation_event_id));
     122              : #undef PROPERTY_PREFIX
     123            4 :   }
     124              : 
     125              :   virtual enum access_direction get_dir () const = 0;
     126              : 
     127              : protected:
     128          621 :   enum memory_space get_memory_space () const
     129              :   {
     130          621 :     return m_access_range.get_base_region ().get_memory_space ();
     131              :   }
     132              : 
     133              :   void
     134          621 :   maybe_show_notes (diagnostic_emission_context &ctxt) const
     135              :   {
     136          621 :     maybe_describe_array_bounds (ctxt.get_location ());
     137          621 :     maybe_show_diagram (ctxt.get_logger ());
     138          621 :   }
     139              : 
     140              :   /* Potentially add a note about valid ways to index this array, such
     141              :      as (given "int arr[10];"):
     142              :        note: valid subscripts for 'arr' are '[0]' to '[9]'
     143              :      We print the '[' and ']' characters so as to express the valid
     144              :      subscripts using C syntax, rather than just as byte ranges,
     145              :      which hopefully is more clear to the user.  */
     146              :   void
     147          621 :   maybe_describe_array_bounds (location_t loc) const
     148              :   {
     149          621 :     if (!m_diag_arg)
     150              :       return;
     151          447 :     tree t = TREE_TYPE (m_diag_arg);
     152          447 :     if (!t)
     153              :       return;
     154          447 :     if (TREE_CODE (t) != ARRAY_TYPE)
     155              :       return;
     156          356 :     tree domain = TYPE_DOMAIN (t);
     157          356 :     if (!domain)
     158              :       return;
     159          355 :     tree max_idx = TYPE_MAX_VALUE (domain);
     160          355 :     if (!max_idx)
     161              :       return;
     162          353 :     tree min_idx = TYPE_MIN_VALUE (domain);
     163          353 :     inform (loc,
     164              :             "valid subscripts for %qE are %<[%E]%> to %<[%E]%>",
     165              :             m_diag_arg, min_idx, max_idx);
     166              :   }
     167              : 
     168              :   void
     169          621 :   maybe_show_diagram (logger *logger) const
     170              :   {
     171          621 :     access_operation op (m_model, get_dir (), m_access_range, m_sval_hint);
     172              : 
     173              :     /* Don't attempt to make a diagram if there's no valid way of
     174              :        accessing the base region (e.g. a 0-element array).  */
     175          621 :     if (op.get_valid_bits ().get_size ().is_zero_p ())
     176            4 :       return;
     177              : 
     178          617 :     if (const text_art::theme *theme = global_dc->get_diagram_theme ())
     179              :       {
     180           68 :         text_art::style_manager sm;
     181           68 :         text_art::canvas canvas (make_access_diagram (op, sm, *theme, logger));
     182           68 :         if (canvas.get_size ().w == 0 && canvas.get_size ().h == 0)
     183              :           {
     184              :             /* In lieu of exceptions, return a zero-sized diagram if there's
     185              :                a problem.  Give up if that's happened.  */
     186            0 :             return;
     187              :           }
     188           68 :         diagnostics::diagram diagram
     189              :           (canvas,
     190              :            /* Alt text.  */
     191           68 :            _("Diagram visualizing the predicted out-of-bounds access"));
     192           68 :         global_dc->emit_diagram (diagram);
     193           68 :       }
     194              :   }
     195              : 
     196              :   text_art::canvas
     197           68 :   make_access_diagram (const access_operation &op,
     198              :                        text_art::style_manager &sm,
     199              :                        const text_art::theme &theme,
     200              :                        logger *logger) const
     201              :   {
     202           68 :     access_diagram d (op, m_region_creation_event_id, sm, theme, logger);
     203           68 :     return d.to_canvas (sm);
     204           68 :   }
     205              : 
     206              :   region_model m_model;
     207              :   symbolic::memory_range m_access_range;
     208              :   tree m_diag_arg;
     209              :   const svalue *m_sval_hint;
     210              :   diagnostics::paths::event_id_t m_region_creation_event_id;
     211              : };
     212              : 
     213              : /* Abstract base class for all out-of-bounds warnings where the
     214              :    out-of-bounds range is concrete.  */
     215              : 
     216              : class concrete_out_of_bounds : public out_of_bounds
     217              : {
     218              : public:
     219          706 :   concrete_out_of_bounds (const region_model &model,
     220              :                           const symbolic::memory_range &access_range,
     221              :                           tree diag_arg,
     222              :                           concrete_bit_range out_of_bounds_bits,
     223              :                           const svalue *sval_hint)
     224              :   : out_of_bounds (model, access_range, diag_arg, sval_hint),
     225          706 :     m_out_of_bounds_bits (out_of_bounds_bits)
     226          706 :   {}
     227              : 
     228          659 :   bool subclass_equal_p (const pending_diagnostic &base_other) const override
     229              :   {
     230          659 :     const concrete_out_of_bounds &other
     231              :       (static_cast <const concrete_out_of_bounds &>(base_other));
     232          659 :     return (out_of_bounds::subclass_equal_p (other)
     233          659 :             && m_out_of_bounds_bits == other.m_out_of_bounds_bits);
     234              :   }
     235              : 
     236            4 :   void maybe_add_sarif_properties (diagnostics::sarif_object &result_obj)
     237              :     const override
     238              :   {
     239            4 :     out_of_bounds::maybe_add_sarif_properties (result_obj);
     240            4 :     auto &props = result_obj.get_or_create_properties ();
     241              : #define PROPERTY_PREFIX "gcc/analyzer/concrete_out_of_bounds/"
     242            8 :     props.set (PROPERTY_PREFIX "out_of_bounds_bits",
     243            4 :                m_out_of_bounds_bits.to_json ());
     244              : #undef PROPERTY_PREFIX
     245            4 :   }
     246              : 
     247          970 :   bool get_out_of_bounds_bytes (concrete_byte_range *out) const
     248              :   {
     249          970 :     return maybe_bytes_from_bits (m_out_of_bounds_bits, out);
     250              :   }
     251              : 
     252              : protected:
     253              :   concrete_bit_range m_out_of_bounds_bits;
     254              : };
     255              : 
     256              : /* Abstract subclass to complain about concrete out-of-bounds
     257              :    past the end of the buffer.  */
     258              : 
     259              : class concrete_past_the_end : public concrete_out_of_bounds
     260              : {
     261              : public:
     262          497 :   concrete_past_the_end (const region_model &model,
     263              :                          const symbolic::memory_range &access_range,tree diag_arg,
     264              :                          concrete_bit_range bad_range,
     265              :                          concrete_bit_size conc_bit_capacity,
     266              :                          const svalue *sval_hint)
     267              :   : concrete_out_of_bounds (model, access_range, diag_arg, bad_range, sval_hint),
     268          994 :     m_conc_bit_capacity (conc_bit_capacity),
     269          994 :     m_conc_byte_capacity (nullptr)
     270              :   {
     271          497 :     concrete_byte_size bytes;
     272          497 :     if (maybe_bytes_from_bits (m_conc_bit_capacity, &bytes))
     273          497 :       m_conc_byte_capacity
     274          497 :         = std::make_unique<concrete_byte_size> (bytes);
     275          497 :   }
     276              : 
     277              :   bool
     278          471 :   subclass_equal_p (const pending_diagnostic &base_other) const final override
     279              :   {
     280          471 :     const concrete_past_the_end &other
     281              :       (static_cast <const concrete_past_the_end &>(base_other));
     282          471 :     return (concrete_out_of_bounds::subclass_equal_p (other)
     283          471 :             && m_conc_bit_capacity == other.m_conc_bit_capacity);
     284              :   }
     285              : 
     286          354 :   void add_region_creation_events (const region *,
     287              :                                    tree,
     288              :                                    const event_loc_info &loc_info,
     289              :                                    checker_path &emission_path) final override
     290              :   {
     291          354 :     if (m_conc_byte_capacity)
     292              :       {
     293          354 :         tree byte_bound
     294          354 :           = make_integer_cst_from_integer (size_type_node,
     295              :                                            m_conc_byte_capacity->get_integer (),
     296          354 :                                            true);
     297          354 :         emission_path.add_event
     298          354 :           (std::make_unique<oob_region_creation_event_capacity> (byte_bound,
     299              :                                                                  loc_info,
     300              :                                                                  *this));
     301              :       }
     302          354 :   }
     303              : 
     304            4 :   void maybe_add_sarif_properties (diagnostics::sarif_object &result_obj)
     305              :     const final override
     306              :   {
     307            4 :     concrete_out_of_bounds::maybe_add_sarif_properties (result_obj);
     308            4 :     auto &props = result_obj.get_or_create_properties ();
     309              : #define PROPERTY_PREFIX "gcc/analyzer/concrete_past_the_end/"
     310            8 :     props.set (PROPERTY_PREFIX "conc_bit_capacity",
     311            4 :                m_conc_bit_capacity.to_json ());
     312            4 :     if (m_conc_byte_capacity)
     313            8 :       props.set (PROPERTY_PREFIX "conc_byte_capacity",
     314            4 :                  m_conc_byte_capacity->to_json ());
     315              : #undef PROPERTY_PREFIX
     316            4 :   }
     317              : 
     318              : protected:
     319              :   concrete_bit_size  m_conc_bit_capacity;
     320              :   std::unique_ptr<concrete_byte_size> m_conc_byte_capacity;
     321              : };
     322              : 
     323              : /* Concrete subclass to complain about buffer overflows.  */
     324              : 
     325              : class concrete_buffer_overflow : public concrete_past_the_end
     326              : {
     327              : public:
     328          212 :   concrete_buffer_overflow (const region_model &model,
     329              :                             const symbolic::memory_range &access_range,
     330              :                             tree diag_arg,
     331              :                             concrete_bit_range bad_range,
     332              :                             concrete_bit_size conc_bit_capacity,
     333              :                             const svalue *sval_hint)
     334              :   : concrete_past_the_end (model, access_range, diag_arg, bad_range,
     335          424 :                            conc_bit_capacity, sval_hint)
     336          212 :   {}
     337              : 
     338         2426 :   const char *get_kind () const final override
     339              :   {
     340         2426 :     return "concrete_buffer_overflow";
     341              :   }
     342              : 
     343          176 :   bool emit (diagnostic_emission_context &ctxt) final override
     344              :   {
     345          176 :     bool warned;
     346          176 :     switch (get_memory_space ())
     347              :       {
     348           70 :       default:
     349           70 :         ctxt.add_cwe (787);
     350           70 :         warned = ctxt.warn ("buffer overflow");
     351           70 :         break;
     352           73 :       case MEMSPACE_STACK:
     353           73 :         ctxt.add_cwe (121);
     354           73 :         warned = ctxt.warn ("stack-based buffer overflow");
     355           73 :         break;
     356           33 :       case MEMSPACE_HEAP:
     357           33 :         ctxt.add_cwe (122);
     358           33 :         warned = ctxt.warn ("heap-based buffer overflow");
     359           33 :         break;
     360              :       }
     361              : 
     362          176 :     if (warned)
     363              :       {
     364          176 :         auto num_bad_bits = m_out_of_bounds_bits.get_size ();
     365          176 :         concrete_byte_size num_bad_bytes;
     366          176 :         if (maybe_bytes_from_bits (num_bad_bits, &num_bad_bytes))
     367              :           {
     368          172 :             pp_element_integer e_num_bad_bytes (num_bad_bytes.get_integer ());
     369          172 :             if (m_diag_arg)
     370          280 :               inform_n (ctxt.get_location (),
     371          140 :                         (size_t)num_bad_bytes.as_unsigned_long (),
     372              :                         "write of %e byte to beyond the end of %qE",
     373              :                         "write of %e bytes to beyond the end of %qE",
     374              :                         &e_num_bad_bytes,
     375              :                         m_diag_arg);
     376              :             else
     377           64 :               inform_n (ctxt.get_location (),
     378           32 :                         (size_t)num_bad_bytes.as_unsigned_long (),
     379              :                         "write of %e byte to beyond the end of the region",
     380              :                         "write of %e bytes to beyond the end of the region",
     381              :                         &e_num_bad_bytes);
     382          172 :           }
     383              :         else
     384              :           {
     385            4 :             pp_element_integer e_num_bad_bits (num_bad_bits.get_integer ());
     386            4 :             if (m_diag_arg)
     387            0 :               inform_n (ctxt.get_location (),
     388            0 :                         (size_t)num_bad_bits.as_unsigned_long (),
     389              :                         "write of %e bit to beyond the end of %qE",
     390              :                         "write of %e bits to beyond the end of %qE",
     391              :                         &e_num_bad_bits,
     392              :                         m_diag_arg);
     393              :             else
     394            8 :               inform_n (ctxt.get_location (),
     395            4 :                         (size_t)num_bad_bits.as_unsigned_long (),
     396              :                         "write of %e bit to beyond the end of the region",
     397              :                         "write of %e bits to beyond the end of the region",
     398              :                         &e_num_bad_bits);
     399            4 :           }
     400              : 
     401          176 :         maybe_show_notes (ctxt);
     402          176 :       }
     403              : 
     404          176 :     return warned;
     405              :   }
     406              : 
     407              :   bool
     408          352 :   describe_final_event (pretty_printer &pp,
     409              :                         const evdesc::final_event &) final override
     410              :   {
     411          352 :     if (m_conc_byte_capacity)
     412              :       {
     413          352 :         concrete_byte_range out_of_bounds_bytes;
     414          352 :         if (get_out_of_bounds_bytes (&out_of_bounds_bytes))
     415              :           {
     416          344 :             describe_final_event_as_bytes (pp, out_of_bounds_bytes);
     417          344 :             return true;
     418              :           }
     419          352 :       }
     420            8 :     describe_final_event_as_bits (pp);
     421            8 :     return true;
     422              :   }
     423              : 
     424              :   void
     425          344 :   describe_final_event_as_bytes (pretty_printer &pp,
     426              :                                  const concrete_byte_range &out_of_bounds_bytes)
     427              :   {
     428          344 :     gcc_assert (m_conc_byte_capacity);
     429              : 
     430          344 :     auto start = out_of_bounds_bytes.get_start_offset ();
     431          344 :     auto last = out_of_bounds_bytes.get_last_offset ();
     432              : 
     433          344 :     pp_element_integer e_start (start.get_integer ());
     434          344 :     pp_element_integer e_byte_capacity (m_conc_byte_capacity->get_integer ());
     435          344 :     if (start == last)
     436              :       {
     437           84 :         if (m_diag_arg)
     438           56 :           pp_printf (&pp,
     439              :                      "out-of-bounds write at byte %e but %qE"
     440              :                      " ends at byte %e", &e_start, m_diag_arg,
     441              :                      &e_byte_capacity);
     442              :         else
     443           28 :           pp_printf (&pp,
     444              :                      "out-of-bounds write at byte %e but region"
     445              :                      " ends at byte %e", &e_start,
     446              :                      &e_byte_capacity);
     447              :       }
     448              :     else
     449              :       {
     450          260 :         pp_element_integer e_last (last.get_integer ());
     451          260 :         if (m_diag_arg)
     452          224 :           pp_printf (&pp,
     453              :                      "out-of-bounds write from byte %e till"
     454              :                      " byte %e but %qE ends at byte %e",
     455              :                      &e_start, &e_last, m_diag_arg,
     456              :                      &e_byte_capacity);
     457              :         else
     458           36 :           pp_printf (&pp,
     459              :                      "out-of-bounds write from byte %e till"
     460              :                      " byte %e but region ends at byte %e",
     461              :                      &e_start, &e_last, &e_byte_capacity);
     462          260 :       }
     463          344 :   }
     464              : 
     465              :   void
     466            8 :   describe_final_event_as_bits (pretty_printer &pp)
     467              :   {
     468            8 :     auto start = m_out_of_bounds_bits.get_start_offset ();
     469            8 :     auto last = m_out_of_bounds_bits.get_last_offset ();
     470              : 
     471            8 :     pp_element_integer e_start (start.get_integer ());
     472            8 :     pp_element_integer e_bit_capacity (m_conc_bit_capacity.get_integer ());
     473            8 :     if (start == last)
     474              :       {
     475            0 :         if (m_diag_arg)
     476            0 :           pp_printf (&pp,
     477              :                      "out-of-bounds write at bit %e but %qE"
     478              :                      " ends at bit %e", &e_start, m_diag_arg,
     479              :                      &e_bit_capacity);
     480              :         else
     481            0 :           pp_printf (&pp,
     482              :                      "out-of-bounds write at bit %e but region"
     483              :                      " ends at bit %e", &e_start,
     484              :                      &e_bit_capacity);
     485              :       }
     486              :     else
     487              :       {
     488            8 :         pp_element_integer e_last (last.get_integer ());
     489            8 :         if (m_diag_arg)
     490            0 :           pp_printf (&pp,
     491              :                      "out-of-bounds write from bit %e till"
     492              :                      " bit %e but %qE ends at bit %e",
     493              :                      &e_start, &e_last, m_diag_arg,
     494              :                      &e_bit_capacity);
     495              :         else
     496            8 :           pp_printf (&pp,
     497              :                      "out-of-bounds write from bit %e till"
     498              :                      " bit %e but region ends at bit %e",
     499              :                      &e_start, &e_last, &e_bit_capacity);
     500            8 :       }
     501            8 :   }
     502              : 
     503              :   enum access_direction
     504          180 :   get_dir () const final override { return access_direction::write; }
     505              : };
     506              : 
     507              : /* Concrete subclass to complain about buffer over-reads.  */
     508              : 
     509              : class concrete_buffer_over_read : public concrete_past_the_end
     510              : {
     511              : public:
     512          285 :   concrete_buffer_over_read (const region_model &model,
     513              :                              const symbolic::memory_range &access_range,
     514              :                              tree diag_arg,
     515              :                              concrete_bit_range bad_range,
     516              :                              concrete_bit_size conc_bit_capacity)
     517              :   : concrete_past_the_end (model, access_range, diag_arg,
     518          570 :                            bad_range, conc_bit_capacity, nullptr)
     519          285 :   {}
     520              : 
     521         2453 :   const char *get_kind () const final override
     522              :   {
     523         2453 :     return "concrete_buffer_over_read";
     524              :   }
     525              : 
     526          181 :   bool emit (diagnostic_emission_context &ctxt) final override
     527              :   {
     528          181 :     bool warned;
     529          181 :     ctxt.add_cwe (126);
     530          181 :     switch (get_memory_space ())
     531              :       {
     532           60 :       default:
     533           60 :         warned = ctxt.warn ("buffer over-read");
     534           60 :         break;
     535          108 :       case MEMSPACE_STACK:
     536          108 :         warned = ctxt.warn ("stack-based buffer over-read");
     537          108 :         break;
     538           13 :       case MEMSPACE_HEAP:
     539           13 :         warned = ctxt.warn ("heap-based buffer over-read");
     540           13 :         break;
     541              :       }
     542              : 
     543          181 :     if (warned)
     544              :       {
     545          181 :         auto num_bad_bits = m_out_of_bounds_bits.get_size ();
     546          181 :         concrete_byte_size num_bad_bytes;
     547          181 :         if (maybe_bytes_from_bits (num_bad_bits, &num_bad_bytes))
     548              :           {
     549          177 :             pp_element_integer e_num_bad_bytes (num_bad_bytes.get_integer ());
     550          177 :             if (m_diag_arg)
     551          336 :               inform_n (ctxt.get_location (),
     552          168 :                         (size_t)num_bad_bytes.as_unsigned_long (),
     553              :                         "read of %e byte from after the end of %qE",
     554              :                         "read of %e bytes from after the end of %qE",
     555              :                         &e_num_bad_bytes,
     556              :                         m_diag_arg);
     557              :             else
     558           18 :               inform_n (ctxt.get_location (),
     559            9 :                         (size_t)num_bad_bytes.as_unsigned_long (),
     560              :                         "read of %e byte from after the end of the region",
     561              :                         "read of %e bytes from after the end of the region",
     562              :                         &e_num_bad_bytes);
     563          177 :           }
     564              :         else
     565              :           {
     566            4 :             pp_element_integer e_num_bad_bits (num_bad_bits.get_integer ());
     567            4 :             if (m_diag_arg)
     568            0 :               inform_n (ctxt.get_location (),
     569            0 :                         (size_t)num_bad_bits.as_unsigned_long (),
     570              :                         "read of %e bit from after the end of %qE",
     571              :                         "read of %e bits from after the end of %qE",
     572              :                         &e_num_bad_bits,
     573              :                         m_diag_arg);
     574              :             else
     575            8 :               inform_n (ctxt.get_location (),
     576            4 :                         (size_t)num_bad_bits.as_unsigned_long (),
     577              :                         "read of %e bit from after the end of the region",
     578              :                         "read of %e bits from after the end of the region",
     579              :                         &e_num_bad_bits);
     580            4 :           }
     581              : 
     582          181 :         maybe_show_notes (ctxt);
     583          181 :       }
     584              : 
     585          181 :     return warned;
     586              :   }
     587              : 
     588              :   bool
     589          362 :   describe_final_event (pretty_printer &pp,
     590              :                         const evdesc::final_event &) final override
     591              :   {
     592          362 :     if (m_conc_byte_capacity)
     593              :       {
     594          362 :         concrete_byte_range out_of_bounds_bytes;
     595          362 :         if (get_out_of_bounds_bytes (&out_of_bounds_bytes))
     596              :           {
     597          354 :             describe_final_event_as_bytes (pp, out_of_bounds_bytes);
     598          354 :             return true;
     599              :           }
     600          362 :       }
     601            8 :     describe_final_event_as_bits (pp);
     602            8 :     return true;
     603              :   }
     604              : 
     605              :   void
     606          354 :   describe_final_event_as_bytes (pretty_printer &pp,
     607              :                                  const concrete_byte_range &out_of_bounds_bytes)
     608              :   {
     609          354 :     gcc_assert (m_conc_byte_capacity);
     610              : 
     611          354 :     auto start = out_of_bounds_bytes.get_start_offset ();
     612          354 :     auto last = out_of_bounds_bytes.get_last_offset ();
     613              : 
     614          354 :     pp_element_integer e_start (start.get_integer ());
     615          354 :     pp_element_integer e_byte_capacity (m_conc_byte_capacity->get_integer ());
     616              : 
     617          354 :     if (start == last)
     618              :       {
     619          218 :         if (m_diag_arg)
     620          210 :           pp_printf (&pp,
     621              :                      "out-of-bounds read at byte %e but %qE"
     622              :                      " ends at byte %e", &e_start, m_diag_arg,
     623              :                      &e_byte_capacity);
     624              :         else
     625            8 :           pp_printf (&pp,
     626              :                      "out-of-bounds read at byte %e but region"
     627              :                      " ends at byte %e", &e_start,
     628              :                      &e_byte_capacity);
     629              :       }
     630              :     else
     631              :       {
     632          136 :         pp_element_integer e_last (last.get_integer ());
     633          136 :         if (m_diag_arg)
     634          126 :           pp_printf (&pp,
     635              :                      "out-of-bounds read from byte %e till"
     636              :                      " byte %e but %qE ends at byte %e",
     637              :                      &e_start, &e_last, m_diag_arg,
     638              :                      &e_byte_capacity);
     639              :         else
     640           10 :           pp_printf (&pp,
     641              :                      "out-of-bounds read from byte %e till"
     642              :                      " byte %e but region ends at byte %e",
     643              :                      &e_start, &e_last, &e_byte_capacity);
     644          136 :       }
     645          354 :   }
     646              : 
     647              :   void
     648            8 :   describe_final_event_as_bits (pretty_printer &pp)
     649              :   {
     650            8 :     auto start = m_out_of_bounds_bits.get_start_offset ();
     651            8 :     auto last = m_out_of_bounds_bits.get_last_offset ();
     652              : 
     653            8 :     pp_element_integer e_start (start.get_integer ());
     654            8 :     pp_element_integer e_bit_capacity (m_conc_bit_capacity.get_integer ());
     655              : 
     656            8 :     if (start == last)
     657              :       {
     658            0 :         if (m_diag_arg)
     659            0 :           pp_printf (&pp,
     660              :                      "out-of-bounds read at bit %e but %qE"
     661              :                      " ends at bit %e", &e_start, m_diag_arg,
     662              :                      &e_bit_capacity);
     663              :         else
     664            0 :           pp_printf (&pp,
     665              :                      "out-of-bounds read at bit %e but region"
     666              :                      " ends at bit %e", &e_start,
     667              :                      &e_bit_capacity);
     668              :       }
     669              :     else
     670              :       {
     671            8 :         pp_element_integer e_last (last.get_integer ());
     672            8 :         if (m_diag_arg)
     673            0 :           pp_printf (&pp,
     674              :                      "out-of-bounds read from bit %e till"
     675              :                      " bit %e but %qE ends at bit %e",
     676              :                      &e_start, &e_last, m_diag_arg,
     677              :                      &e_bit_capacity);
     678              :         else
     679            8 :           pp_printf (&pp,
     680              :                      "out-of-bounds read from bit %e till"
     681              :                      " bit %e but region ends at bit %e",
     682              :                      &e_start, &e_last, &e_bit_capacity);
     683            8 :       }
     684            8 :   }
     685              : 
     686              :   enum access_direction
     687          181 :   get_dir () const final override { return access_direction::read; }
     688              : };
     689              : 
     690              : /* Concrete subclass to complain about buffer underwrites.  */
     691              : 
     692              : class concrete_buffer_underwrite : public concrete_out_of_bounds
     693              : {
     694              : public:
     695           66 :   concrete_buffer_underwrite (const region_model &model,
     696              :                               const symbolic::memory_range &access_range,
     697              :                               tree diag_arg,
     698              :                               concrete_bit_range bad_range,
     699              :                               const svalue *sval_hint)
     700              :   : concrete_out_of_bounds (model, access_range, diag_arg,
     701           66 :                             bad_range, sval_hint)
     702           66 :   {}
     703              : 
     704          658 :   const char *get_kind () const final override
     705              :   {
     706          658 :     return "concrete_buffer_underwrite";
     707              :   }
     708              : 
     709           62 :   bool emit (diagnostic_emission_context &ctxt) final override
     710              :   {
     711           62 :     bool warned;
     712           62 :     ctxt.add_cwe (124);
     713           62 :     switch (get_memory_space ())
     714              :       {
     715           40 :       default:
     716           40 :         warned = ctxt.warn ("buffer underwrite");
     717           40 :         break;
     718           14 :       case MEMSPACE_STACK:
     719           14 :         warned = ctxt.warn ("stack-based buffer underwrite");
     720           14 :         break;
     721            8 :       case MEMSPACE_HEAP:
     722            8 :         warned = ctxt.warn ("heap-based buffer underwrite");
     723            8 :         break;
     724              :       }
     725           62 :     if (warned)
     726           62 :       maybe_show_notes (ctxt);
     727           62 :     return warned;
     728              :   }
     729              : 
     730              :   bool
     731          124 :   describe_final_event (pretty_printer &pp,
     732              :                         const evdesc::final_event &) final override
     733              :   {
     734          124 :     concrete_byte_range out_of_bounds_bytes;
     735          124 :     if (get_out_of_bounds_bytes (&out_of_bounds_bytes))
     736          116 :       describe_final_event_as_bytes (pp, out_of_bounds_bytes);
     737              :     else
     738            8 :       describe_final_event_as_bits (pp);
     739          124 :     return true;
     740          124 :   }
     741              : 
     742              :   void
     743          116 :   describe_final_event_as_bytes (pretty_printer &pp,
     744              :                                  const concrete_byte_range &out_of_bounds_bytes)
     745              :   {
     746          116 :     auto start = out_of_bounds_bytes.get_start_offset ();
     747          116 :     auto last = out_of_bounds_bytes.get_last_offset ();
     748              : 
     749          116 :     pp_element_integer e_start (start.get_integer ());
     750              : 
     751          116 :     if (start == last)
     752              :       {
     753           24 :         if (m_diag_arg)
     754           24 :           pp_printf (&pp,
     755              :                      "out-of-bounds write at byte %e but %qE"
     756              :                      " starts at byte 0",
     757              :                      &e_start, m_diag_arg);
     758              :         else
     759            0 :           pp_printf (&pp,
     760              :                      "out-of-bounds write at byte %e but region"
     761              :                      " starts at byte 0", &e_start);
     762              :       }
     763              :     else
     764              :       {
     765           92 :         pp_element_integer e_last (last.get_integer ());
     766           92 :         if (m_diag_arg)
     767           82 :           pp_printf (&pp,
     768              :                      "out-of-bounds write from byte %e till"
     769              :                      " byte %e but %qE starts at byte 0",
     770              :                      &e_start, &e_last, m_diag_arg);
     771              :         else
     772           10 :           pp_printf (&pp,
     773              :                      "out-of-bounds write from byte %e till"
     774              :                      " byte %e but region starts at byte 0",
     775              :                      &e_start, &e_last);
     776           92 :       }
     777          116 :   }
     778              : 
     779              :   void
     780            8 :   describe_final_event_as_bits (pretty_printer &pp)
     781              :   {
     782            8 :     auto start = m_out_of_bounds_bits.get_start_offset ();
     783            8 :     auto last = m_out_of_bounds_bits.get_last_offset ();
     784              : 
     785            8 :     pp_element_integer e_start (start.get_integer ());
     786            8 :     if (start == last)
     787              :       {
     788            0 :         if (m_diag_arg)
     789            0 :           pp_printf (&pp,
     790              :                      "out-of-bounds write at bit %e but %qE"
     791              :                      " starts at bit 0",
     792              :                      &e_start, m_diag_arg);
     793              :         else
     794            0 :           pp_printf (&pp,
     795              :                      "out-of-bounds write at bit %e but region"
     796              :                      " starts at bit 0", &e_start);
     797              :       }
     798              :     else
     799              :       {
     800            8 :         pp_element_integer e_last (last.get_integer ());
     801            8 :         if (m_diag_arg)
     802            0 :           pp_printf (&pp,
     803              :                      "out-of-bounds write from bit %e till"
     804              :                      " bit %e but %qE starts at bit 0",
     805              :                      &e_start, &e_last, m_diag_arg);
     806              :         else
     807            8 :           pp_printf (&pp,
     808              :                      "out-of-bounds write from bit %e till"
     809              :                      " bit %e but region starts at bit 0",
     810              :                      &e_start, &e_last);
     811            8 :       }
     812            8 :   }
     813              : 
     814              :   enum access_direction
     815           62 :   get_dir () const final override { return access_direction::write; }
     816              : };
     817              : 
     818              : /* Concrete subclass to complain about buffer under-reads.  */
     819              : 
     820              : class concrete_buffer_under_read : public concrete_out_of_bounds
     821              : {
     822              : public:
     823          143 :   concrete_buffer_under_read (const region_model &model,
     824              :                               const symbolic::memory_range &access_range,
     825              :                               tree diag_arg,
     826              :                               concrete_bit_range bad_range)
     827          143 :   : concrete_out_of_bounds (model, access_range, diag_arg, bad_range, nullptr)
     828          143 :   {}
     829              : 
     830         1063 :   const char *get_kind () const final override
     831              :   {
     832         1063 :     return "concrete_buffer_under_read";
     833              :   }
     834              : 
     835           66 :   bool emit (diagnostic_emission_context &ctxt) final override
     836              :   {
     837           66 :     bool warned;
     838           66 :     ctxt.add_cwe (127);
     839           66 :     switch (get_memory_space ())
     840              :       {
     841           44 :       default:
     842           44 :         warned = ctxt.warn ("buffer under-read");
     843           44 :         break;
     844           14 :       case MEMSPACE_STACK:
     845           14 :         warned = ctxt.warn ("stack-based buffer under-read");
     846           14 :         break;
     847            8 :       case MEMSPACE_HEAP:
     848            8 :         warned = ctxt.warn ("heap-based buffer under-read");
     849            8 :         break;
     850              :       }
     851           66 :     if (warned)
     852           66 :       maybe_show_notes (ctxt);
     853           66 :     return warned;
     854              :   }
     855              : 
     856              :   bool
     857          132 :   describe_final_event (pretty_printer &pp,
     858              :                         const evdesc::final_event &) final override
     859              :   {
     860          132 :     concrete_byte_range out_of_bounds_bytes;
     861          132 :     if (get_out_of_bounds_bytes (&out_of_bounds_bytes))
     862          124 :       describe_final_event_as_bytes (pp, out_of_bounds_bytes);
     863              :     else
     864            8 :       describe_final_event_as_bits (pp);
     865          132 :     return true;
     866          132 :   }
     867              : 
     868              :   void
     869          124 :   describe_final_event_as_bytes (pretty_printer &pp,
     870              :                                  const concrete_byte_range &out_of_bounds_bytes)
     871              :   {
     872          124 :     auto start = out_of_bounds_bytes.get_start_offset ();
     873          124 :     auto last = out_of_bounds_bytes.get_last_offset ();
     874              : 
     875          124 :     pp_element_integer e_start (start.get_integer ());
     876          124 :     if (start == last)
     877              :       {
     878           34 :         if (m_diag_arg)
     879           34 :           pp_printf (&pp,
     880              :                      "out-of-bounds read at byte %e but %qE"
     881              :                      " starts at byte 0",
     882              :                      &e_start, m_diag_arg);
     883              :         else
     884            0 :           pp_printf (&pp,
     885              :                      "out-of-bounds read at byte %e but region"
     886              :                      " starts at byte 0",
     887              :                      &e_start);
     888              :       }
     889              :     else
     890              :       {
     891           90 :         pp_element_integer e_last (last.get_integer ());
     892           90 :         if (m_diag_arg)
     893           82 :           pp_printf (&pp,
     894              :                      "out-of-bounds read from byte %e till"
     895              :                      " byte %e but %qE starts at byte 0",
     896              :                      &e_start, &e_last, m_diag_arg);
     897              :         else
     898            8 :           pp_printf (&pp,
     899              :                      "out-of-bounds read from byte %e till"
     900              :                      " byte %e but region starts at byte 0",
     901              :                      &e_start, &e_last);
     902           90 :       }
     903          124 :   }
     904              : 
     905              :   void
     906            8 :   describe_final_event_as_bits (pretty_printer &pp)
     907              :   {
     908            8 :     auto start = m_out_of_bounds_bits.get_start_offset ();
     909            8 :     auto last = m_out_of_bounds_bits.get_last_offset ();
     910              : 
     911            8 :     pp_element_integer e_start (start.get_integer ());
     912              : 
     913            8 :     if (start == last)
     914              :       {
     915            0 :         if (m_diag_arg)
     916            0 :           pp_printf (&pp,
     917              :                      "out-of-bounds read at bit %e but %qE"
     918              :                      " starts at bit 0",
     919              :                      &e_start, m_diag_arg);
     920              :         else
     921            0 :           pp_printf (&pp,
     922              :                      "out-of-bounds read at bit %e but region"
     923              :                      " starts at bit 0", &e_start);
     924              :       }
     925              :     else
     926              :       {
     927            8 :         pp_element_integer e_last (last.get_integer ());
     928            8 :         if (m_diag_arg)
     929            0 :           pp_printf (&pp,
     930              :                      "out-of-bounds read from bit %e till"
     931              :                      " bit %e but %qE starts at bit 0",
     932              :                      &e_start, &e_last, m_diag_arg);
     933              :         else
     934            8 :           pp_printf (&pp,
     935              :                      "out-of-bounds read from bit %e till"
     936              :                      " bit %e but region starts at bit 0",
     937              :                      &e_start, &e_last);
     938            8 :       }
     939            8 :   }
     940              : 
     941              :   enum access_direction
     942           66 :   get_dir () const final override { return access_direction::read; }
     943              : };
     944              : 
     945              : /* Abstract class to complain about out-of-bounds read/writes where
     946              :    the values are symbolic.  */
     947              : 
     948              : class symbolic_past_the_end : public out_of_bounds
     949              : {
     950              : public:
     951          145 :   symbolic_past_the_end (const region_model &model,
     952              :                          const symbolic::memory_range &access_range,
     953              :                          tree diag_arg,
     954              :                          tree offset,
     955              :                          tree num_bytes,
     956              :                          tree capacity,
     957              :                          const svalue *sval_hint)
     958              :   : out_of_bounds (model, access_range, diag_arg, sval_hint),
     959          145 :     m_offset (offset),
     960          145 :     m_num_bytes (num_bytes),
     961          290 :     m_capacity (capacity)
     962              :   {}
     963              : 
     964              :   bool
     965          145 :   subclass_equal_p (const pending_diagnostic &base_other) const final override
     966              :   {
     967          145 :     const symbolic_past_the_end &other
     968              :       (static_cast <const symbolic_past_the_end &>(base_other));
     969          145 :     return (out_of_bounds::subclass_equal_p (other)
     970          145 :             && pending_diagnostic::same_tree_p (m_offset, other.m_offset)
     971          145 :             && pending_diagnostic::same_tree_p (m_num_bytes, other.m_num_bytes)
     972          290 :             && pending_diagnostic::same_tree_p (m_capacity, other.m_capacity));
     973              :   }
     974              : 
     975              :   void
     976            0 :   maybe_add_sarif_properties (diagnostics::sarif_object &result_obj)
     977              :     const final override
     978              :   {
     979            0 :     out_of_bounds::maybe_add_sarif_properties (result_obj);
     980            0 :     auto &props = result_obj.get_or_create_properties ();
     981              : #define PROPERTY_PREFIX "gcc/analyzer/symbolic_past_the_end/"
     982            0 :     props.set (PROPERTY_PREFIX "offset", tree_to_json (m_offset));
     983            0 :     props.set (PROPERTY_PREFIX "num_bytes", tree_to_json (m_num_bytes));
     984            0 :     props.set (PROPERTY_PREFIX "capacity", tree_to_json (m_capacity));
     985              : #undef PROPERTY_PREFIX
     986            0 :   }
     987              : 
     988              : protected:
     989              :   tree m_offset;
     990              :   tree m_num_bytes;
     991              :   tree m_capacity;
     992              : };
     993              : 
     994              : /* Concrete subclass to complain about overflows with symbolic values.  */
     995              : 
     996              : class symbolic_buffer_overflow : public symbolic_past_the_end
     997              : {
     998              : public:
     999          116 :   symbolic_buffer_overflow (const region_model &model,
    1000              :                             const symbolic::memory_range &access_range,
    1001              :                             tree diag_arg,
    1002              :                             tree offset,
    1003              :                             tree num_bytes,
    1004              :                             tree capacity,
    1005              :                             const svalue *sval_hint)
    1006              :   : symbolic_past_the_end (model, access_range, diag_arg,
    1007              :                            offset, num_bytes,
    1008              :                            capacity,
    1009          116 :                            sval_hint)
    1010              :   {
    1011              :   }
    1012              : 
    1013         1885 :   const char *get_kind () const final override
    1014              :   {
    1015         1885 :     return "symbolic_buffer_overflow";
    1016              :   }
    1017              : 
    1018          115 :   bool emit (diagnostic_emission_context &ctxt) final override
    1019              :   {
    1020          115 :     bool warned;
    1021          115 :     switch (get_memory_space ())
    1022              :       {
    1023           40 :       default:
    1024           40 :         ctxt.add_cwe (787);
    1025           40 :         warned = ctxt.warn ("buffer overflow");
    1026           40 :         break;
    1027           30 :       case MEMSPACE_STACK:
    1028           30 :         ctxt.add_cwe (121);
    1029           30 :         warned = ctxt.warn ("stack-based buffer overflow");
    1030           30 :         break;
    1031           45 :       case MEMSPACE_HEAP:
    1032           45 :         ctxt.add_cwe (122);
    1033           45 :         warned =  ctxt.warn ("heap-based buffer overflow");
    1034           45 :         break;
    1035              :       }
    1036          115 :     if (warned)
    1037          115 :       maybe_show_notes (ctxt);
    1038          115 :     return warned;
    1039              :   }
    1040              : 
    1041              :   bool
    1042          230 :   describe_final_event (pretty_printer &pp,
    1043              :                         const evdesc::final_event &) final override
    1044              :   {
    1045          230 :     if (m_offset)
    1046              :       {
    1047              :         /* Known offset.  */
    1048          196 :         if (m_num_bytes)
    1049              :           {
    1050              :             /* Known offset, known size.  */
    1051          190 :             if (TREE_CODE (m_num_bytes) == INTEGER_CST)
    1052              :               {
    1053              :                 /* Known offset, known constant size.  */
    1054          164 :                 if (pending_diagnostic::same_tree_p (m_num_bytes,
    1055              :                                                      integer_one_node))
    1056              :                   {
    1057              :                     /* Singular m_num_bytes.  */
    1058          120 :                     if (m_diag_arg)
    1059           32 :                       pp_printf (&pp,
    1060              :                                  "write of %E byte at offset %qE exceeds %qE",
    1061              :                                  m_num_bytes, m_offset, m_diag_arg);
    1062              :                     else
    1063           88 :                       pp_printf (&pp,
    1064              :                                  "write of %E byte at offset %qE exceeds"
    1065              :                                  " the buffer",
    1066              :                                  m_num_bytes, m_offset);
    1067              :                   }
    1068              :                 else
    1069              :                   {
    1070              :                     /* Plural m_num_bytes.  */
    1071           44 :                     if (m_diag_arg)
    1072            8 :                       pp_printf (&pp,
    1073              :                                  "write of %E bytes at offset %qE exceeds %qE",
    1074              :                                  m_num_bytes, m_offset, m_diag_arg);
    1075              :                     else
    1076           36 :                       pp_printf (&pp,
    1077              :                                  "write of %E bytes at offset %qE exceeds"
    1078              :                                  " the buffer",
    1079              :                                  m_num_bytes, m_offset);
    1080              :                   }
    1081              :               }
    1082              :             else
    1083              :               {
    1084              :                 /* Known offset, known symbolic size.  */
    1085           26 :                 if (m_diag_arg)
    1086            8 :                   pp_printf (&pp,
    1087              :                              "write of %qE bytes at offset %qE exceeds %qE",
    1088              :                              m_num_bytes, m_offset, m_diag_arg);
    1089              :                 else
    1090           18 :                   pp_printf (&pp,
    1091              :                              "write of %qE bytes at offset %qE exceeds"
    1092              :                              " the buffer",
    1093              :                              m_num_bytes, m_offset);
    1094              :               }
    1095              :           }
    1096              :         else
    1097              :           {
    1098              :             /* Known offset, unknown size.  */
    1099            6 :             if (m_diag_arg)
    1100            0 :               pp_printf (&pp,
    1101              :                          "write at offset %qE exceeds %qE",
    1102              :                          m_offset, m_diag_arg);
    1103              :             else
    1104            6 :               pp_printf (&pp,
    1105              :                          "write at offset %qE exceeds the buffer",
    1106              :                          m_offset);
    1107              :           }
    1108              :       }
    1109              :     else
    1110              :       {
    1111              :         /* Unknown offset.  */
    1112           34 :         if (m_diag_arg)
    1113            0 :           pp_printf (&pp,
    1114              :                      "out-of-bounds write on %qE",
    1115              :                      m_diag_arg);
    1116              :         else
    1117           34 :           pp_printf (&pp, "out-of-bounds write");
    1118              :       }
    1119          230 :     return true;
    1120              :   }
    1121              : 
    1122              :   enum access_direction
    1123          115 :   get_dir () const final override { return access_direction::write; }
    1124              : };
    1125              : 
    1126              : /* Concrete subclass to complain about over-reads with symbolic values.  */
    1127              : 
    1128              : class symbolic_buffer_over_read : public symbolic_past_the_end
    1129              : {
    1130              : public:
    1131           29 :   symbolic_buffer_over_read (const region_model &model,
    1132              :                              const symbolic::memory_range &access_range,
    1133              :                              tree diag_arg,
    1134              :                              tree offset,
    1135              :                              tree num_bytes,
    1136              :                              tree capacity)
    1137              :   : symbolic_past_the_end (model, access_range, diag_arg,
    1138              :                            offset, num_bytes,
    1139              :                            capacity,
    1140           29 :                            nullptr)
    1141              :   {
    1142              :   }
    1143              : 
    1144          551 :   const char *get_kind () const final override
    1145              :   {
    1146          551 :     return "symbolic_buffer_over_read";
    1147              :   }
    1148              : 
    1149           21 :   bool emit (diagnostic_emission_context &ctxt) final override
    1150              :   {
    1151           21 :     ctxt.add_cwe (126);
    1152           21 :     bool warned;
    1153           21 :     switch (get_memory_space ())
    1154              :       {
    1155            4 :       default:
    1156            4 :         ctxt.add_cwe (787);
    1157            4 :         warned = ctxt.warn ("buffer over-read");
    1158            4 :         break;
    1159           17 :       case MEMSPACE_STACK:
    1160           17 :         ctxt.add_cwe (121);
    1161           17 :         warned = ctxt.warn ("stack-based buffer over-read");
    1162           17 :         break;
    1163            0 :       case MEMSPACE_HEAP:
    1164            0 :         ctxt.add_cwe (122);
    1165            0 :         warned = ctxt.warn ("heap-based buffer over-read");
    1166            0 :         break;
    1167              :       }
    1168           21 :     if (warned)
    1169           21 :       maybe_show_notes (ctxt);
    1170           21 :     return warned;
    1171              :   }
    1172              : 
    1173              :   bool
    1174           42 :   describe_final_event (pretty_printer &pp,
    1175              :                         const evdesc::final_event &) final override
    1176              :   {
    1177           42 :     if (m_offset)
    1178              :       {
    1179              :         /* Known offset.  */
    1180           42 :         if (m_num_bytes)
    1181              :           {
    1182              :             /* Known offset, known size.  */
    1183           42 :             if (TREE_CODE (m_num_bytes) == INTEGER_CST)
    1184              :               {
    1185              :                 /* Known offset, known constant size.  */
    1186           16 :                 if (pending_diagnostic::same_tree_p (m_num_bytes,
    1187              :                                                      integer_one_node))
    1188              :                   {
    1189              :                     /* Singular m_num_bytes.  */
    1190            0 :                     if (m_diag_arg)
    1191            0 :                       pp_printf (&pp,
    1192              :                                  "read of %E byte at offset %qE exceeds %qE",
    1193              :                                  m_num_bytes, m_offset, m_diag_arg);
    1194              :                     else
    1195            0 :                       pp_printf (&pp,
    1196              :                                  "read of %E byte at offset %qE exceeds"
    1197              :                                  " the buffer",
    1198              :                                  m_num_bytes, m_offset);
    1199              :                   }
    1200              :                 else
    1201              :                   {
    1202              :                     /* Plural m_num_bytes.  */
    1203           16 :                     if (m_diag_arg)
    1204            0 :                       pp_printf (&pp,
    1205              :                                  "read of %E bytes at offset %qE exceeds %qE",
    1206              :                                  m_num_bytes, m_offset, m_diag_arg);
    1207              :                     else
    1208           16 :                       pp_printf (&pp,
    1209              :                                  "read of %E bytes at offset %qE exceeds"
    1210              :                                  " the buffer",
    1211              :                                  m_num_bytes, m_offset);
    1212              :                   }
    1213              :               }
    1214              :             else
    1215              :               {
    1216              :                 /* Known offset, known symbolic size.  */
    1217           26 :                 if (m_diag_arg)
    1218            8 :                   pp_printf (&pp,
    1219              :                              "read of %qE bytes at offset %qE exceeds %qE",
    1220              :                              m_num_bytes, m_offset, m_diag_arg);
    1221              :                 else
    1222           18 :                   pp_printf (&pp,
    1223              :                              "read of %qE bytes at offset %qE exceeds"
    1224              :                              " the buffer",
    1225              :                              m_num_bytes, m_offset);
    1226              :               }
    1227              :           }
    1228              :         else
    1229              :           {
    1230              :             /* Known offset, unknown size.  */
    1231            0 :             if (m_diag_arg)
    1232            0 :               pp_printf (&pp,
    1233              :                          "read at offset %qE exceeds %qE",
    1234              :                          m_offset, m_diag_arg);
    1235              :             else
    1236            0 :               pp_printf (&pp,
    1237              :                          "read at offset %qE exceeds the buffer",
    1238              :                          m_offset);
    1239              :           }
    1240              :       }
    1241              :     else
    1242              :       {
    1243              :         /* Unknown offset.  */
    1244            0 :         if (m_diag_arg)
    1245            0 :           pp_printf (&pp,
    1246              :                      "out-of-bounds read on %qE",
    1247              :                      m_diag_arg);
    1248              :         else
    1249            0 :           pp_printf (&pp,
    1250              :                      "out-of-bounds read");
    1251              :       }
    1252           42 :     return true;
    1253              :   }
    1254              : 
    1255              :   enum access_direction
    1256           21 :   get_dir () const final override { return access_direction::read; }
    1257              : };
    1258              : 
    1259              : /* Check whether an access is past the end of the BASE_REG.
    1260              :    Return TRUE if the access was valid, FALSE otherwise.  */
    1261              : 
    1262              : bool
    1263        38852 : region_model::check_symbolic_bounds (const symbolic::memory_range &attempted_range,
    1264              :                                      const svalue *capacity,
    1265              :                                      enum access_direction dir,
    1266              :                                      const svalue *sval_hint,
    1267              :                                      region_model_context *ctxt) const
    1268              : {
    1269        38852 :   gcc_assert (ctxt);
    1270              : 
    1271        38852 :   auto &next_bit = attempted_range.get_bit_range ().get_next_offset ();
    1272        38852 :   auto *next_byte_as_scalar = maybe_bytes_from_bits (next_bit);
    1273        38852 :   if (!next_byte_as_scalar)
    1274              :     return true;
    1275              :   
    1276        36223 :   auto &start_bit = attempted_range.get_bit_range ().get_start_offset ();
    1277        36223 :   auto *start_byte_as_scalar = maybe_bytes_from_bits (start_bit);
    1278        36223 :   if (!start_byte_as_scalar)
    1279              :     return true;
    1280              : 
    1281        36223 :   auto &attempted_size_in_bits = attempted_range.get_bit_range ().get_size ();
    1282        36223 :   auto *attempted_size_in_bytes
    1283        36223 :     = maybe_bytes_from_bits (attempted_size_in_bits);
    1284        36223 :   if (!attempted_size_in_bytes)
    1285              :     return true;
    1286              : 
    1287        36223 :   const svalue *next_byte
    1288        36223 :     = m_mgr->get_svalue_from_scalar (size_type_node, *next_byte_as_scalar);
    1289              : 
    1290        36223 :   if (eval_condition (next_byte, GT_EXPR, capacity).is_true ())
    1291              :     {
    1292          145 :       auto &base_reg = attempted_range.get_base_region ();
    1293          145 :       tree diag_arg = get_representative_tree (&base_reg);
    1294          145 :       const svalue *sym_byte_offset
    1295          145 :         = m_mgr->get_svalue_from_scalar (size_type_node, *start_byte_as_scalar);
    1296          145 :       tree offset_tree = get_representative_tree (sym_byte_offset);
    1297          145 :       const svalue *num_bytes_sval
    1298          145 :         = m_mgr->get_svalue_from_scalar (size_type_node,
    1299              :                                          *attempted_size_in_bytes);
    1300          145 :       tree num_bytes_tree = get_representative_tree (num_bytes_sval);
    1301          145 :       tree capacity_tree = get_representative_tree (capacity);
    1302          145 :       switch (dir)
    1303              :         {
    1304            0 :         default:
    1305            0 :           gcc_unreachable ();
    1306           29 :           break;
    1307           29 :         case access_direction::read:
    1308           29 :           gcc_assert (sval_hint == nullptr);
    1309           29 :           ctxt->warn
    1310           29 :             (std::make_unique<symbolic_buffer_over_read> (*this,
    1311              :                                                           attempted_range,
    1312              :                                                           diag_arg,
    1313              :                                                           offset_tree,
    1314              :                                                           num_bytes_tree,
    1315              :                                                           capacity_tree));
    1316           29 :           return false;
    1317          116 :           break;
    1318          116 :         case access_direction::write:
    1319          116 :           ctxt->warn
    1320          116 :             (std::make_unique<symbolic_buffer_overflow> (*this,
    1321              :                                                          attempted_range,
    1322              :                                                          diag_arg,
    1323              :                                                          offset_tree,
    1324              :                                                          num_bytes_tree,
    1325              :                                                          capacity_tree,
    1326              :                                                          sval_hint));
    1327          116 :           return false;
    1328          145 :           break;
    1329              :         }
    1330              :     }
    1331              : 
    1332              :   return true;
    1333              : }
    1334              : 
    1335              : /* Complain to CTXT if an access of ATTEMPTED_RANGE is outside the bounds
    1336              :    of the underlying region.  SVAL_HINT can be non-null for writes and, if
    1337              :    so, may be used to make the diagnostic more readable.
    1338              :    Return true if the access was valid, false otherwise.  */
    1339              : 
    1340              : bool
    1341       661237 : region_model::
    1342              : check_memory_range_bounds (const symbolic::memory_range &attempted_range,
    1343              :                            enum access_direction dir,
    1344              :                            const svalue *sval_hint,
    1345              :                            region_model_context *ctxt) const
    1346              : {
    1347       661237 :   gcc_assert (ctxt);
    1348              : 
    1349              :   /* Bail out if 0 bits are accessed.  */
    1350       661237 :   if (attempted_range.get_bit_range ().get_size ().is_zero_p ())
    1351              :     return true;
    1352              : 
    1353       661201 :   const region &base_reg = attempted_range.get_base_region ();
    1354              : 
    1355              :   /* Get the capacity of the buffer (in bytes).  */
    1356       661201 :   auto &byte_capacity = get_capacity_as_scalar (&base_reg);
    1357              : 
    1358       661201 :   integer_with_unit<units::byte> conc_byte_capacity;
    1359       661201 :   bool capacity_is_concrete
    1360       661201 :     = concrete_from_symbolic (byte_capacity, &conc_byte_capacity);
    1361              : 
    1362              :   /* If any of the base region, the offset, or the number of bytes accessed
    1363              :      are symbolic, we have to reason about symbolic values.  */
    1364              : 
    1365       661201 :   concrete_bit_range conc_attempted_range;
    1366       661201 :   bool attempted_range_is_concrete
    1367       661201 :     = concrete_from_symbolic (attempted_range.get_bit_range (),
    1368              :                               &conc_attempted_range);
    1369       661201 :   if (base_reg.symbolic_p () || !attempted_range_is_concrete)
    1370              :     {
    1371        38852 :       auto *byte_capacity_as_sval = get_capacity (&base_reg);
    1372        38852 :       gcc_assert (byte_capacity_as_sval);
    1373        38852 :       return check_symbolic_bounds (attempted_range,
    1374              :                                     byte_capacity_as_sval,
    1375        38852 :                                     dir, sval_hint, ctxt);
    1376              :     }
    1377              : 
    1378              :   /* Otherwise continue to check with concrete values.  */
    1379       622349 :   concrete_bit_range bits_outside;
    1380       622349 :   bool oob_safe = true;
    1381              :   /* If conc_attempted_range has a subset < 0, we do have an underwrite.  */
    1382       622349 :   if (conc_attempted_range.falls_short_of_p (0_bits, &bits_outside))
    1383              :     {
    1384          209 :       tree diag_arg = get_representative_tree (&base_reg);
    1385          209 :       switch (dir)
    1386              :         {
    1387            0 :         default:
    1388            0 :           gcc_unreachable ();
    1389          143 :           break;
    1390          143 :         case access_direction::read:
    1391          143 :           gcc_assert (sval_hint == nullptr);
    1392          143 :           ctxt->warn
    1393          143 :             (std::make_unique<concrete_buffer_under_read> (*this,
    1394              :                                                            attempted_range,
    1395              :                                                            diag_arg,
    1396              :                                                            bits_outside));
    1397          143 :           oob_safe = false;
    1398          143 :           break;
    1399           66 :         case access_direction::write:
    1400           66 :           ctxt->warn
    1401           66 :             (std::make_unique<concrete_buffer_underwrite> (*this,
    1402              :                                                            attempted_range,
    1403              :                                                            diag_arg,
    1404              :                                                            bits_outside,
    1405              :                                                            sval_hint));
    1406           66 :           oob_safe = false;
    1407           66 :           break;
    1408              :         }
    1409              :     }
    1410              : 
    1411              :   /* For accesses past the end, we do need a concrete capacity.  No need to
    1412              :      do a symbolic check here because the inequality check does not reason
    1413              :      whether constants are greater than symbolic values.  */
    1414       622349 :   if (!capacity_is_concrete)
    1415              :     return oob_safe;
    1416              : 
    1417       616739 :   auto conc_bit_capacity = bits_from_bytes (conc_byte_capacity);
    1418       616739 :   if (conc_attempted_range.exceeds_p (conc_bit_capacity, &bits_outside))
    1419              :     {
    1420          497 :       tree diag_arg = get_representative_tree (&base_reg);
    1421              : 
    1422          497 :       switch (dir)
    1423              :         {
    1424            0 :         default:
    1425            0 :           gcc_unreachable ();
    1426          285 :           break;
    1427          285 :         case access_direction::read:
    1428          285 :           gcc_assert (sval_hint == nullptr);
    1429          285 :           ctxt->warn
    1430          285 :             (std::make_unique<concrete_buffer_over_read> (*this,
    1431              :                                                           attempted_range,
    1432              :                                                           diag_arg,
    1433              :                                                           bits_outside,
    1434              :                                                           conc_bit_capacity));
    1435          285 :           oob_safe = false;
    1436          285 :           break;
    1437          212 :         case access_direction::write:
    1438          212 :           ctxt->warn
    1439          212 :             (std::make_unique<concrete_buffer_overflow> (*this,
    1440              :                                                          attempted_range,
    1441              :                                                          diag_arg,
    1442              :                                                          bits_outside,
    1443              :                                                          conc_bit_capacity,
    1444              :                                                          sval_hint));
    1445          212 :           oob_safe = false;
    1446          212 :           break;
    1447              :         }
    1448              :     }
    1449       616739 :   return oob_safe;
    1450      1900289 : }
    1451              : 
    1452              : /* May complain when the access on REG is out-of-bounds.
    1453              :    Return TRUE if the access was valid, FALSE otherwise.  */
    1454              : 
    1455              : bool
    1456            0 : region_model::check_region_bounds (const region *reg,
    1457              :                                    enum access_direction dir,
    1458              :                                    const svalue *sval_hint,
    1459              :                                    region_model_context *ctxt) const
    1460              : {
    1461            0 :   gcc_assert (ctxt);
    1462              : 
    1463            0 :   return check_memory_range_bounds (reg->get_memory_range (*m_mgr),
    1464              :                                     dir,
    1465              :                                     sval_hint,
    1466            0 :                                     ctxt);
    1467              : }
    1468              : 
    1469              : } // namespace ana
    1470              : 
    1471              : #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.