LCOV - code coverage report
Current view: top level - gcc/analyzer - string-scan.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 91.9 % 369 339
Test Date: 2026-11-14 16:39:50 Functions: 88.0 % 25 22
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Support for scanning buffers for a NUL terminator (for C-style strings).
       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 "fold-const.h"
      24              : #include "tree-diagnostic.h"
      25              : #include "tm.h"
      26              : 
      27              : #include "selftest.h"
      28              : 
      29              : #include "analyzer/symbolic-bit-vectors.h"
      30              : #include "analyzer/region-model.h"
      31              : #include "analyzer/string-scan.h"
      32              : #include "analyzer/analyzer-selftests.h"
      33              : 
      34              : #if ENABLE_ANALYZER
      35              : 
      36              : namespace ana {
      37              : 
      38              : void
      39           16 : string_scan::dump_to_pp (pretty_printer *pp) const
      40              : {
      41           16 :   pp_string (pp, "string_scan::");
      42           16 :   pp_string (pp, state_to_string (m_state));
      43           16 :   pp_string (pp, "(");
      44           16 :   m_byte_offset.dump_to_pp (pp);
      45           16 :   pp_string (pp, "_bytes");
      46           16 :   if (m_state == state::found_poison)
      47            4 :     pp_printf (pp, ", %s", poison_kind_to_str (m_poison_kind));
      48           16 :   pp_string (pp, ")");
      49           16 : }
      50              : 
      51              : DEBUG_FUNCTION void
      52            0 : string_scan::dump () const
      53              : {
      54            0 :   tree_dump_pretty_printer pp (stderr);
      55            0 :   dump_to_pp (&pp);
      56            0 :   pp_newline (&pp);
      57            0 : }
      58              : 
      59              : const char *
      60           16 : string_scan::state_to_string (enum state s)
      61              : {
      62           16 :   switch (s)
      63              :     {
      64            0 :     default:
      65            0 :       gcc_unreachable ();
      66              : 
      67              :     case state::found_nul:
      68              :       return "found_nul";
      69              : 
      70            4 :     case state::found_poison:
      71            4 :       return "found_poison";
      72              : 
      73            4 :     case state::beyond_domain:
      74            4 :       return "beyond_domain";
      75              : 
      76            4 :     case state::cannot_scan:
      77            4 :       return "cannot_scan";
      78              :     }
      79              : }
      80              : 
      81              : namespace symbolic {
      82              : 
      83              : static string_scan
      84         3543 : scan_string_cst_for_null_terminator (tree string_cst,
      85              :                                      concrete_byte_offset start_byte_offset)
      86              : {
      87         3543 :   gcc_assert (TREE_CODE (string_cst) == STRING_CST);
      88              : 
      89         3543 :   tree cst_size = get_string_cst_size (string_cst);
      90         3543 :   if (!cst_size)
      91            0 :     return string_scan::cannot_scan (start_byte_offset);
      92         3543 :   if (TREE_CODE (cst_size) != INTEGER_CST)
      93            0 :     return string_scan::cannot_scan (start_byte_offset);
      94              : 
      95         3543 :   auto string_len
      96         3543 :     = integer_with_unit<units::byte> (TREE_STRING_LENGTH (string_cst));
      97              :   /* Assume all bytes after TREE_STRING_LENGTH are zero.  This handles
      98              :      the case where an array is initialized with a string_cst that isn't
      99              :      as long as the array, where the remaining elements are
     100              :      empty-initialized and thus zeroed.  */
     101         3543 :   if (start_byte_offset >= string_len)
     102            1 :     return string_scan::found_nul (start_byte_offset);
     103              : 
     104              :   /* Look for the first 0 byte within STRING_CST
     105              :      from START_READ_OFFSET onwards.  */
     106         3542 :   const char *start = (TREE_STRING_POINTER (string_cst)
     107         3542 :                        + start_byte_offset.as_unsigned_long ());
     108         3542 :   size_t remaining_bytes
     109         3542 :     = TREE_STRING_LENGTH (string_cst) - start_byte_offset.as_unsigned_long ();
     110         3542 :   if (const void *p = memchr (start, 0, remaining_bytes))
     111              :     {
     112              :       /* Found NUL.  */
     113         3434 :       return string_scan::found_nul
     114         3434 :         (concrete_byte_offset ((const char *)p
     115         3434 :                                - TREE_STRING_POINTER (string_cst)));
     116              :     }
     117              :   else
     118              :     {
     119              :       /* Didn't find NUL.  */
     120          108 :       return string_scan::beyond_domain (string_len);
     121              :     }
     122         3543 : }
     123              : 
     124              : static string_scan
     125           24 : scan_raw_data_cst_for_null_terminator (tree raw_data_cst,
     126              :                                        concrete_byte_offset start_byte_offset)
     127              : {
     128           24 :   gcc_assert (TREE_CODE (raw_data_cst) == RAW_DATA_CST);
     129           24 :   gcc_assert (start_byte_offset >= 0_bytes);
     130           24 :   gcc_assert (start_byte_offset
     131              :               < concrete_byte_offset (RAW_DATA_LENGTH (raw_data_cst)));
     132              : 
     133           24 :   const unsigned char *start
     134           24 :     = &RAW_DATA_UCHAR_ELT (raw_data_cst, start_byte_offset.as_unsigned_long ());
     135           24 :   size_t remaining_bytes
     136           24 :     = RAW_DATA_LENGTH (raw_data_cst) - start_byte_offset.as_unsigned_long ();
     137           24 :   if (const void *p = memchr (start, 0, remaining_bytes))
     138              :     {
     139              :       /* Found NUL.  */
     140           24 :       return string_scan::found_nul
     141           24 :         (concrete_byte_offset ((const unsigned char *)p
     142           24 :                                - &RAW_DATA_UCHAR_ELT (raw_data_cst, 0)));
     143              :     }
     144              :   else
     145              :     {
     146              :       /* Didn't find NUL.  */
     147            0 :       return string_scan::beyond_domain
     148            0 :         (concrete_byte_offset (RAW_DATA_LENGTH (raw_data_cst)));
     149              :     }
     150              : }
     151              : 
     152              : static string_scan
     153         1017 : scan_tree_cst_for_null_terminator (tree tree_cst,
     154              :                                    concrete_bit_size size_in_bits,
     155              :                                    concrete_byte_offset start_byte_offset)
     156              : {
     157         1017 :   gcc_assert (start_byte_offset >= 0_bytes);
     158              : 
     159         1017 :   switch (TREE_CODE (tree_cst))
     160              :     {
     161            0 :     default:
     162            0 :       gcc_unreachable ();
     163              : 
     164          423 :     case COMPLEX_CST:
     165          423 :     case VECTOR_CST:
     166          423 :     case INTEGER_CST:
     167          423 :       if (size_in_bits == concrete_bit_size (BITS_PER_UNIT))
     168              :         {
     169              :           /* We have a one-byte constant.
     170              :              It's either a zero byte or a non-zero byte.  */
     171          366 :           gcc_assert (start_byte_offset == 0_bytes);
     172          366 :           if (zerop (tree_cst))
     173            0 :             return string_scan::found_nul (0_bytes);
     174              :           else
     175          366 :             return string_scan::beyond_domain (1_byte);
     176              :         }
     177              :       else
     178              :         {
     179              :           // sizeof > 1:
     180           57 :           if (zerop (tree_cst))
     181           18 :             return string_scan::found_nul (0_bytes);
     182              :           else
     183              :             {
     184              :               /* Use native_encode_expr and extract the pertinent byte(s).  */
     185           48 :               unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
     186           48 :               int size = native_encode_expr (tree_cst, buf, sizeof buf, 0);
     187           48 :               if (size == 0)
     188            0 :                 string_scan::cannot_scan (start_byte_offset);
     189           48 :               if (start_byte_offset < 0_bytes)
     190            0 :                 return string_scan::cannot_scan (start_byte_offset);
     191           48 :               if (start_byte_offset >= concrete_byte_size (size))
     192            0 :                 return string_scan::beyond_domain (start_byte_offset);
     193          108 :               for (int offset = start_byte_offset.as_unsigned_long ();
     194           96 :                    offset < size; ++offset)
     195           80 :                 if (buf[offset] == '\0')
     196           32 :                   return string_scan::found_nul (concrete_byte_offset (offset));
     197           16 :               return string_scan::beyond_domain (concrete_byte_offset (size));
     198              :             }
     199              :         }
     200          570 :       break;
     201              : 
     202          570 :     case STRING_CST:
     203         1140 :       return scan_string_cst_for_null_terminator (tree_cst,
     204          570 :                                                   start_byte_offset);
     205              : 
     206           24 :     case RAW_DATA_CST:
     207           48 :       return scan_raw_data_cst_for_null_terminator (tree_cst,
     208           24 :                                                     start_byte_offset);
     209              :     }
     210              : 
     211              :   return string_scan::cannot_scan (start_byte_offset);
     212              : }
     213              : 
     214              : string_scan
     215          188 : concat_bit_vector::
     216              : scan_for_first_zero_byte (concrete_byte_offset byte_offset) const
     217              : {
     218          188 :   concrete_byte_offset byte_offset_relative_to_iter = byte_offset;
     219          188 :   concrete_byte_offset accum_byte_sizes = 0_bytes;
     220          552 :   for (auto iter : m_bit_vectors)
     221              :     {
     222          476 :       concrete_byte_size iter_byte_size;
     223          476 :       if (!iter->maybe_get_concrete_size_in_bytes (&iter_byte_size))
     224            0 :         return string_scan::cannot_scan (byte_offset);
     225          476 :       if (byte_offset_relative_to_iter < iter_byte_size)
     226              :         {
     227          344 :           auto nested_scan
     228          344 :             = iter->scan_for_first_zero_byte (byte_offset_relative_to_iter);
     229          344 :           if (nested_scan.m_state != string_scan::state::beyond_domain)
     230          224 :             return string_scan (accum_byte_sizes + nested_scan.m_byte_offset,
     231          112 :                                 nested_scan.m_state);
     232          344 :         }
     233              :       else
     234              :         {
     235              :           /* "iter" is a leading element fully before the initial byte offset:
     236              :              skip it altogether.  */
     237              :         }
     238          364 :       accum_byte_sizes += iter_byte_size;
     239          364 :       byte_offset_relative_to_iter -= iter_byte_size;
     240          364 :       if (byte_offset_relative_to_iter < 0_bytes)
     241          232 :         byte_offset_relative_to_iter = 0_bytes;
     242          476 :     }
     243           76 :   return string_scan::beyond_domain (accum_byte_sizes);
     244          188 : }
     245              : 
     246              : string_scan
     247           91 : conjured_bit_vector::
     248              : scan_for_first_zero_byte (concrete_byte_offset byte_offset) const
     249              : {
     250           91 :   return string_scan::cannot_scan (byte_offset);
     251              : }
     252              : 
     253              : string_scan
     254          172 : cropped_bit_vector::
     255              : scan_for_first_zero_byte (concrete_byte_offset start_byte_offset) const
     256              : {
     257          172 :   concrete_bit_range uncropped_bits_to_use;
     258          172 :   if (!concrete_from_symbolic (get_uncropped_bits_to_use (),
     259              :                                &uncropped_bits_to_use))
     260            0 :     return string_scan::cannot_scan (start_byte_offset);
     261          172 :   concrete_byte_range uncropped_bytes_to_use;
     262          344 :   if (!maybe_bytes_from_bits (uncropped_bits_to_use,
     263              :                               &uncropped_bytes_to_use))
     264            0 :     return string_scan::cannot_scan (start_byte_offset);
     265              : 
     266              :   /* Scan the uncropped bit_vector, starting at START_BYTE_OFFSET
     267              :      adjusted relative to the beginning of the cropping.  */
     268          172 :   string_scan nested_scan
     269          172 :     = m_uncropped_bv.scan_for_first_zero_byte
     270          172 :     (uncropped_bytes_to_use.get_start_offset () + start_byte_offset);
     271              : 
     272              :   /* If the first thing that happened in the nested scan was beyond the
     273              :      cropped region, then simply advance the outer scan to the end of
     274              :      the cropped region.  */
     275          172 :   if (nested_scan.m_byte_offset >= uncropped_bytes_to_use.get_next_offset ())
     276          164 :     return string_scan::beyond_domain
     277          328 :       (std::max (start_byte_offset,
     278          492 :                  uncropped_bytes_to_use.get_size ()));
     279              : 
     280              :   /* Otherwise, report the location of interest relative to the start
     281              :      of this cropped bit_vector.  */
     282            8 :   return string_scan (nested_scan.m_byte_offset
     283           16 :                       - uncropped_bytes_to_use.get_start_offset (),
     284           16 :                       nested_scan.m_state);
     285          344 : }
     286              : 
     287              : #if CHECKING_P
     288              : 
     289              : string_scan
     290           20 : named_test_bit_vector::
     291              : scan_for_first_zero_byte (concrete_byte_offset start_byte_offset) const
     292              : {
     293           20 :   return string_scan::cannot_scan (start_byte_offset);
     294              : }
     295              : 
     296              : #endif /* #if CHECKING_P */
     297              : 
     298              : string_scan
     299            0 : empty_bit_vector::
     300              : scan_for_first_zero_byte (concrete_byte_offset byte_offset) const
     301              : {
     302            0 :   return string_scan::beyond_domain (byte_offset);
     303              : }
     304              : 
     305              : string_scan
     306            0 : poisoned_bit_vector::
     307              : scan_for_first_zero_byte (concrete_byte_offset start_byte_offset) const
     308              : {
     309            0 :   return string_scan::cannot_scan (start_byte_offset);
     310              : }
     311              : 
     312              : /* Scan for null terminator within repeated bit_vector.
     313              :     . +-----+-----+-----+-----+-----+-----+
     314              :     . |     |     |     |     |     |     |
     315              :     . +-----+-----+-----+-----+-----+-----+
     316              :     .                ^
     317              :     .                start_byte_offset
     318              :    Do a partial scan within the first repetition we're at;
     319              :    failing that, try the next (if any).  */
     320              : 
     321              : string_scan
     322          110 : repeated_bit_vector::
     323              : scan_for_first_zero_byte (concrete_byte_offset start_byte_offset) const
     324              : {
     325          110 :   concrete_byte_size inner_byte_size;
     326          110 :   if (!m_inner_bv.maybe_get_concrete_size_in_bytes (&inner_byte_size))
     327            0 :     return string_scan::cannot_scan (start_byte_offset);
     328              : 
     329          110 :   concrete_integer conc_repeat_count;
     330          110 :   if (!concrete_from_symbolic (m_repeat_count, &conc_repeat_count))
     331            0 :     return string_scan::cannot_scan (start_byte_offset);
     332              : 
     333              :   /* If we're starting within a repetition, perform a partial scan of the
     334              :      repetition of the inner bit_vector that we hit at START_BYTE_OFFSET, e.g.:
     335              :     . +0----+1----+2----+3----+4----+5----+
     336              :     . |     |     |     |     |     |     |
     337              :     . +-----+-----+-----+-----+-----+-----+
     338              :     .                ^
     339              :     .                start_byte_offset
     340              :     .                V
     341              :     .             +-----+
     342              :     .             |     |
     343              :     .             +-----+
     344              :   */
     345          110 :   concrete_byte_offset byte_offset_within_initial
     346          110 :     = (start_byte_offset.floor_mod (inner_byte_size));
     347          110 :   if (byte_offset_within_initial > 0_bytes)
     348              :     {
     349           44 :       auto partial_instance_index
     350           44 :         = start_byte_offset.floor_div (inner_byte_size);
     351           44 :       const string_scan partial_nested_scan
     352           44 :         = m_inner_bv.scan_for_first_zero_byte (byte_offset_within_initial);
     353           44 :       if (partial_nested_scan.m_state != string_scan::state::beyond_domain)
     354              :         {
     355           32 :           concrete_byte_offset start_byte_offset_of_partial_instance
     356           32 :             = partial_instance_index * inner_byte_size;
     357           32 :           return string_scan ((start_byte_offset_of_partial_instance
     358           64 :                                + partial_nested_scan.m_byte_offset),
     359           64 :                               partial_nested_scan.m_state);
     360           32 :         }
     361           44 :     }
     362              : 
     363              :   /* Otherwise, we haven't found anything yet; look from the start of
     364              :      the next full repetition of the inner bit_vector, if there is one.
     365              :     . +0----+1----+2----+3----+4----+5----+
     366              :     . |     |     |     |     |     |     |
     367              :     . +-----+-----+-----+-----+-----+-----+
     368              :     .                   ^
     369              :     .                   V
     370              :     .                   +-----+
     371              :     .                   |     |
     372              :     .                   +-----+
     373              : 
     374              :   */
     375           78 :   auto full_instance_index = start_byte_offset.floor_div (inner_byte_size);
     376           78 :   if (full_instance_index < conc_repeat_count)
     377              :     {
     378           78 :       const string_scan full_nested_scan
     379           78 :         = m_inner_bv.scan_for_first_zero_byte (0_bytes);
     380           78 :       if (full_nested_scan.m_state != string_scan::state::beyond_domain)
     381              :         {
     382           13 :           concrete_byte_offset start_byte_offset_of_full_instance
     383           13 :             = (full_instance_index * inner_byte_size);
     384           13 :           return string_scan ((start_byte_offset_of_full_instance
     385           26 :                                + full_nested_scan.m_byte_offset),
     386           26 :                               full_nested_scan.m_state);
     387           13 :         }
     388           78 :     }
     389              : 
     390              :   /* Otherwise, we've run out of repetitions.  */
     391           65 :   return string_scan::beyond_domain (inner_byte_size * conc_repeat_count);
     392          298 : }
     393              : 
     394              : string_scan
     395         4000 : bit_vector_from_svalue::
     396              : scan_for_first_zero_byte (concrete_byte_offset start_byte_offset) const
     397              : {
     398         4000 :   switch (m_sval.get_kind ())
     399              :     {
     400              :     default:
     401              :       break;
     402              : 
     403         1017 :     case SK_CONSTANT:
     404         1017 :       {
     405         1017 :         concrete_bit_size conc_size;
     406         1017 :         if (!concrete_from_symbolic (get_size (), &conc_size))
     407            0 :           gcc_unreachable ();
     408         1017 :         tree cst = as_a<const constant_svalue *> (&m_sval)->get_constant ();
     409         2034 :         return scan_tree_cst_for_null_terminator (cst,
     410              :                                                   conc_size,
     411         1017 :                                                   start_byte_offset);
     412         1017 :       }
     413              : 
     414         2982 :     case SK_INITIAL:
     415         2982 :       {
     416         2982 :         auto *initial_sval = as_a<const initial_svalue *> (&m_sval);
     417         2982 :         auto &reg = *initial_sval->get_region ();
     418         2982 :         switch (reg.get_kind ())
     419              :           {
     420              :           default:
     421              :             break;
     422              : 
     423         2973 :           case RK_STRING:
     424         2973 :             {
     425         2973 :               auto *string_reg = as_a<const string_region *> (&reg);
     426         2973 :               return scan_string_cst_for_null_terminator
     427         5946 :                 (string_reg->get_string_cst (),
     428         2973 :                  start_byte_offset);
     429              :             }
     430              :           }
     431              :       }
     432              :       break;
     433              :     }
     434              : 
     435           10 :   return string_scan::cannot_scan (start_byte_offset);
     436              : }
     437              : 
     438              : string_scan
     439            1 : bit_vector_from_ubc::
     440              : scan_for_first_zero_byte (concrete_byte_offset start_byte_offset) const
     441              : {
     442            1 :   return string_scan::cannot_scan (start_byte_offset);
     443              : }
     444              : 
     445              : string_scan
     446          269 : tristate_bit_vector::
     447              : scan_for_first_zero_byte (concrete_byte_offset start_byte_offset) const
     448              : {
     449          269 :   concrete_byte_size size_in_bytes;
     450          269 :   if (!maybe_get_concrete_size_in_bytes (&size_in_bytes))
     451            0 :     return string_scan::cannot_scan (start_byte_offset);
     452              : 
     453          269 :   gcc_assert (size_in_bytes >= start_byte_offset);
     454              : 
     455          269 :   switch (m_bit_value.get_value ())
     456              :     {
     457            0 :     default:
     458            0 :       gcc_unreachable ();
     459              : 
     460           10 :     case tristate::TS_UNKNOWN:
     461           10 :       return string_scan::cannot_scan (start_byte_offset);
     462              : 
     463           68 :     case tristate::TS_TRUE:
     464              :       // All ones: NUL not found.
     465           68 :       return string_scan::beyond_domain (size_in_bytes);
     466              : 
     467          191 :     case tristate::TS_FALSE:
     468              :       // All zeros: NUL immediately found.
     469          191 :       return string_scan::found_nul (start_byte_offset);
     470              :     }
     471          269 : }
     472              : 
     473              : } // namespace ana::symbolic
     474              : 
     475              : #if CHECKING_P
     476              : 
     477              : namespace selftest {
     478              : 
     479              : static void
     480            4 : test_string_scan_dump ()
     481              : {
     482            4 :   {
     483            4 :     pretty_printer pp;
     484            4 :     string_scan::found_nul (5_bytes).dump_to_pp (&pp);
     485            4 :     ASSERT_STREQ (pp_formatted_text (&pp),
     486              :                   "string_scan::found_nul(5_bytes)");
     487            4 :   }
     488            4 :   {
     489            4 :     pretty_printer pp;
     490            4 :     string_scan::found_poison (100_bytes, poison_kind::uninit).dump_to_pp (&pp);
     491            4 :     ASSERT_STREQ (pp_formatted_text (&pp),
     492              :                   "string_scan::found_poison(100_bytes, uninit)");
     493            4 :   }
     494            4 :   {
     495            4 :     pretty_printer pp;
     496            4 :     string_scan::beyond_domain (1024_bytes).dump_to_pp (&pp);
     497            4 :     ASSERT_STREQ (pp_formatted_text (&pp),
     498              :                   "string_scan::beyond_domain(1024_bytes)");
     499            4 :   }
     500            4 :   {
     501            4 :     pretty_printer pp;
     502            4 :     string_scan::cannot_scan (42_bytes).dump_to_pp (&pp);
     503            4 :     ASSERT_STREQ (pp_formatted_text (&pp),
     504              :                   "string_scan::cannot_scan(42_bytes)");
     505            4 :   }
     506            4 : }
     507              : 
     508              : // Test scanning a string constant for NUL.
     509              : 
     510              : static void
     511            4 : test_scan_string_constant ()
     512              : {
     513            4 :   tree string_cst_ptr = make_string_constant ("abc");
     514              : 
     515            4 :   symbolic::node_manager node_mgr;
     516            4 :   auto &mgr = node_mgr.get_bit_vector_manager ();
     517              : 
     518            4 :   auto &abc0 = mgr.get_bits_for_constant (string_cst_ptr);
     519              : 
     520           24 :   for (concrete_byte_offset i = 0_bytes; i < 4_bytes; ++i)
     521           16 :     ASSERT_EQ (abc0.scan_for_first_zero_byte (i),
     522            4 :                string_scan::found_nul (3_bytes));
     523            4 : }
     524              : 
     525              : static void
     526            4 : test_scan_integer_constants ()
     527              : {
     528            4 :   symbolic::node_manager node_mgr;
     529            4 :   auto &mgr = node_mgr.get_bit_vector_manager ();
     530              : 
     531              :   /* sizeof() > 1, all zero: NUL at byte 0.  */
     532            4 :   {
     533            4 :     auto &int_0
     534            4 :       = mgr.get_bits_for_constant (build_int_cst (integer_type_node, 0));
     535            4 :     ASSERT_EQ (int_0.scan_for_first_zero_byte (0_bytes),
     536              :                string_scan::found_nul (0_bytes));
     537              :   }
     538              : 
     539              :   /* sizeof() > 1, not all zero.  */
     540            4 :   {
     541            4 :     auto &int_42
     542            4 :       = mgr.get_bits_for_constant (build_int_cst (uint32_type_node, 42));
     543            4 :     if (BYTES_BIG_ENDIAN)
     544              :       {
     545              :         /* Big-endian.  */
     546              :         /* First 3 bytes are all zero.  */
     547              :         for (auto i = 0_bytes; i < 3_bytes; ++i)
     548              :           ASSERT_EQ (int_42.scan_for_first_zero_byte (i),
     549              :                      string_scan::found_nul (i));
     550              :         /* Then non-zero.  */
     551              :         ASSERT_EQ (int_42.scan_for_first_zero_byte (3_bytes),
     552              :                    string_scan::beyond_domain (4_bytes));
     553              :       }
     554              :     else
     555              :       {
     556              :         /* Little-endian.  */
     557            4 :         if (!WORDS_BIG_ENDIAN)
     558              :           {
     559              :             /* First byte is non-zero (42).  */
     560            4 :             ASSERT_EQ (int_42.scan_for_first_zero_byte (0_bytes),
     561              :                        string_scan::found_nul (1_byte));
     562              :             /* Then 3 zero bytes.  */
     563           20 :             for (auto i = 1_byte; i < 4_bytes; ++i)
     564           12 :               ASSERT_EQ (int_42.scan_for_first_zero_byte (i),
     565            4 :                          string_scan::found_nul (i));
     566              :           }
     567              :       }
     568              :   }
     569              : 
     570              :   /* sizeof() == 1.  */
     571            4 :   auto &char_00
     572            4 :     = mgr.get_bits_for_constant (build_int_cst (char_type_node, 0));
     573            4 :   ASSERT_EQ (char_00.scan_for_first_zero_byte (0_bytes),
     574              :              string_scan::found_nul (0_bytes));
     575              : 
     576            4 :   auto &char_a
     577            4 :     = mgr.get_bits_for_constant (build_int_cst (char_type_node, 'a'));
     578            4 :   ASSERT_EQ (char_a.scan_for_first_zero_byte (0_bytes),
     579              :              string_scan::beyond_domain (1_byte));
     580              : 
     581            4 :   auto &char_b
     582            4 :     = mgr.get_bits_for_constant (build_int_cst (char_type_node, 'b'));
     583            4 :   ASSERT_EQ (char_b.scan_for_first_zero_byte (0_bytes),
     584              :              string_scan::beyond_domain (1_byte));
     585            4 : }
     586              : 
     587              : static void
     588            4 : test_scan_tristates ()
     589              : {
     590            4 :   symbolic::node_manager node_mgr;
     591            4 :   auto &mgr = node_mgr.get_bit_vector_manager ();
     592              : 
     593            4 :   {
     594            4 :     auto &byte_00 = mgr.get_all_zeros (1_byte);
     595            4 :     ASSERT_EQ (byte_00.scan_for_first_zero_byte (0_bytes),
     596              :                string_scan::found_nul (0_bytes));
     597              :   }
     598            4 :   {
     599            4 :     auto &byte_ff = mgr.get_all_ones (1_byte);
     600            4 :     ASSERT_EQ (byte_ff.scan_for_first_zero_byte (0_bytes),
     601              :                string_scan::beyond_domain (1_byte));
     602              :   }
     603            4 :   {
     604            4 :     auto &byte_unknown = mgr.get_tristate_bits (8_bits, tristate::unknown ());
     605            4 :     ASSERT_EQ (byte_unknown.scan_for_first_zero_byte (0_bytes),
     606              :                string_scan::cannot_scan (0_bytes));
     607              :   }
     608            4 : }
     609              : 
     610              : /* Return true iff default_tree_printer prints "char" for
     611              :    %T with char_type_node.
     612              : 
     613              :    2026-11-05(dmalcolm): crab1 seems to print char_type_node as
     614              :    "unsigned char", whereas cc1 and cc1plus print it as "char".  */
     615              : 
     616              : static bool
     617            8 : char_type_node_printed_as_char_p ()
     618              : {
     619            8 :   pretty_printer pp;
     620            8 :   pp_format_decoder (&pp) = default_tree_printer;
     621            8 :   pp_printf (&pp, "%T", char_type_node);
     622            8 :   return strcmp (pp_formatted_text (&pp), "char") == 0;
     623            8 : }
     624              : 
     625              : static void
     626            4 : test_scan_concat ()
     627              : {
     628            4 :   symbolic::node_manager node_mgr;
     629            4 :   auto &mgr = node_mgr.get_bit_vector_manager ();
     630              : 
     631            4 :   auto &abc0 = mgr.get_bits_for_constant (make_string_constant ("abc"));
     632            4 :   auto &def0 = mgr.get_bits_for_constant (make_string_constant ("def"));
     633              : 
     634            4 :   {
     635            4 :     auto &abc0def0 = mgr.get_concat_bits ({&abc0, &def0});
     636              : 
     637            4 :     if (char_type_node_printed_as_char_p ())
     638            3 :       ASSERT_SYMBOLIC_NODE_DUMP_EQ
     639              :         (abc0def0,
     640              :          ("concat_bit_vector[size: 8 bytes]\n"
     641              :           "+- bit_vector_from_svalue[size: 4 bytes]\n"
     642              :           "|  `- m_sval: 'const char[4]': constant_svalue ('\"abc\"')\n"
     643              :           "`- bit_vector_from_svalue[size: 4 bytes]\n"
     644              :           "   `- m_sval: 'const char[4]': constant_svalue ('\"def\"')\n"));
     645              : 
     646           24 :     for (concrete_byte_offset i = 0_bytes; i < 4_bytes; ++i)
     647           16 :       ASSERT_EQ (abc0def0.scan_for_first_zero_byte (i),
     648            4 :                  string_scan::found_nul (3_bytes));
     649           24 :     for (concrete_byte_offset i = 4_bytes; i < 8_bytes; ++i)
     650           16 :       ASSERT_EQ (abc0def0.scan_for_first_zero_byte (i),
     651            4 :                  string_scan::found_nul (7_bytes));
     652              :   }
     653              : 
     654              :   // Concat, cropping the terminator from "abc"
     655            4 :   {
     656            4 :     auto &abcdef0
     657           12 :       = mgr.get_concat_bits ({&abc0.get_cropped_bytes (concrete_byte_range (0_bytes, 3_bytes)),
     658              :                               &def0});
     659              : 
     660            4 :     if (char_type_node_printed_as_char_p ())
     661            3 :       ASSERT_SYMBOLIC_NODE_DUMP_EQ
     662              :         (abcdef0,
     663              :          ("concat_bit_vector[size: 7 bytes]\n"
     664              :           "+- cropped_bit_vector[size: 3 bytes]: [bytes 0..3)\n"
     665              :           "|  `- uncropped: bit_vector_from_svalue[size: 4 bytes]\n"
     666              :           "|     `- m_sval: 'const char[4]': constant_svalue ('\"abc\"')\n"
     667              :           "`- bit_vector_from_svalue[size: 4 bytes]\n"
     668              :           "   `- m_sval: 'const char[4]': constant_svalue ('\"def\"')\n"));
     669              : 
     670           36 :     for (concrete_byte_offset i = 0_bytes; i < 7_bytes; ++i)
     671           28 :       ASSERT_EQ (abcdef0.scan_for_first_zero_byte (i),
     672            4 :                  string_scan::found_nul (6_bytes));
     673              :   }
     674            4 : }
     675              : 
     676              : static void
     677            4 : test_scan_cropped ()
     678              : {
     679            4 :   symbolic::node_manager node_mgr;
     680            4 :   auto &mgr = node_mgr.get_bit_vector_manager ();
     681              : 
     682            4 :   auto &abc0 = mgr.get_bits_for_constant (make_string_constant ("abc"));
     683              : 
     684              :   // Crop the terminator from "abc"
     685            4 :   {
     686           12 :     auto &abc = abc0.get_cropped_bytes( concrete_byte_range (0_bytes, 3_bytes));
     687           20 :     for (concrete_byte_offset i = 0_bytes; i < 3_bytes; ++i)
     688           12 :       ASSERT_EQ (abc.scan_for_first_zero_byte (i),
     689            4 :                  string_scan::beyond_domain (3_bytes));
     690              :   }
     691            4 : }
     692              : 
     693              : static void
     694            4 : test_scan_repeated ()
     695              : {
     696            4 :   symbolic::node_manager node_mgr;
     697            4 :   auto &mgr = node_mgr.get_bit_vector_manager ();
     698              : 
     699            4 :   auto &char_a
     700            4 :     = mgr.get_bits_for_constant (build_int_cst (char_type_node, 'a'));
     701            4 :   auto &char_b
     702            4 :     = mgr.get_bits_for_constant (build_int_cst (char_type_node, 'b'));
     703              : 
     704            4 :   {
     705            4 :     auto &aaa = mgr.get_repeated_bits (char_a, 3);
     706           20 :     for (concrete_byte_offset i = 0_bytes; i < 3_bytes; ++i)
     707           12 :       ASSERT_EQ (aaa.scan_for_first_zero_byte (i),
     708            4 :                  string_scan::beyond_domain (3_bytes));
     709              :   }
     710              : 
     711            4 :   {
     712            4 :     auto &ab = mgr.get_concat_bits ({&char_a, &char_b});
     713            4 :     auto &ababab = mgr.get_repeated_bits (ab, 3);
     714           32 :     for (concrete_byte_offset i = 0_bytes; i < 6_bytes; ++i)
     715           24 :       ASSERT_EQ (ababab.scan_for_first_zero_byte (i),
     716            4 :                  string_scan::beyond_domain (6_bytes));
     717              :   }
     718              : 
     719            4 :   {
     720            4 :     tree string_cst_ptr = make_string_constant ("abc");
     721            4 :     auto &abc0 = mgr.get_bits_for_constant (string_cst_ptr);
     722            4 :     auto &abc0abc0abc0 = mgr.get_repeated_bits (abc0, 3);
     723           24 :     for (concrete_byte_offset i = 0_bytes; i < 4_bytes; ++i)
     724           16 :       ASSERT_EQ (abc0abc0abc0.scan_for_first_zero_byte (i),
     725            4 :                  string_scan::found_nul (3_bytes));
     726           20 :     for (concrete_byte_offset i = 4_bytes; i < 7_bytes; ++i)
     727           12 :       ASSERT_EQ (abc0abc0abc0.scan_for_first_zero_byte (i),
     728            4 :                  string_scan::found_nul (7_bytes));
     729           24 :     for (concrete_byte_offset i = 8_bytes; i < 12_bytes; ++i)
     730           16 :       ASSERT_EQ (abc0abc0abc0.scan_for_first_zero_byte (i),
     731            4 :                  string_scan::found_nul (11_bytes));
     732              :   }
     733            4 : }
     734              : 
     735              : /* Run all of the selftests within this file.  */
     736              : 
     737              : void
     738            4 : analyzer_string_scan_cc_tests ()
     739              : {
     740            4 :   test_string_scan_dump ();
     741            4 :   test_scan_string_constant ();
     742            4 :   test_scan_integer_constants ();
     743            4 :   test_scan_tristates ();
     744            4 :   test_scan_concat ();
     745            4 :   test_scan_cropped ();
     746            4 :   test_scan_repeated ();
     747            4 : }
     748              : 
     749              : } // namespace selftest
     750              : 
     751              : #endif /* CHECKING_P */
     752              : 
     753              : } // namespace ana
     754              : 
     755              : #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.