LCOV - code coverage report
Current view: top level - gcc/analyzer - kf.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 95.6 % 1408 1346
Test Date: 2026-12-05 16:10:02 Functions: 96.0 % 149 143
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Handling for the known behavior of various specific functions.
       2              :    Copyright (C) 2020-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 "diagnostic.h"
      24              : 
      25              : #include "analyzer/analyzer-logging.h"
      26              : #include "analyzer/region-model.h"
      27              : #include "analyzer/call-details.h"
      28              : #include "analyzer/call-info.h"
      29              : 
      30              : #if ENABLE_ANALYZER
      31              : 
      32              : namespace ana {
      33              : 
      34              : /* Abstract subclass for describing undefined behavior of an API.  */
      35              : 
      36              : class undefined_function_behavior
      37              :   : public pending_diagnostic_subclass<undefined_function_behavior>
      38              : {
      39              : public:
      40           28 :   undefined_function_behavior (const call_details &cd)
      41           28 :   : m_call_stmt (cd.get_call_stmt ()),
      42           28 :     m_callee_fndecl (cd.get_fndecl_for_call ())
      43              :   {
      44           28 :     gcc_assert (m_callee_fndecl);
      45           28 :   }
      46              : 
      47          250 :   const char *get_kind () const final override
      48              :   {
      49          250 :     return "undefined_behavior";
      50              :   }
      51              : 
      52           28 :   bool operator== (const undefined_function_behavior &other) const
      53              :   {
      54           28 :     return (&m_call_stmt == &other.m_call_stmt
      55           28 :             && m_callee_fndecl == other.m_callee_fndecl);
      56              :   }
      57              : 
      58           28 :   bool terminate_path_p () const final override { return true; }
      59              : 
      60           76 :   tree get_callee_fndecl () const { return m_callee_fndecl; }
      61              : 
      62              : private:
      63              :   const gimple &m_call_stmt;
      64              :   tree m_callee_fndecl;
      65              : };
      66              : 
      67              : /* class pure_known_function_with_default_return : public known_function.  */
      68              : 
      69              : void
      70         1542 : pure_known_function_with_default_return::
      71              : impl_call_pre (const call_details &cd) const
      72              : {
      73         1542 :   cd.set_any_lhs_with_defaults ();
      74         1542 : }
      75              : 
      76              : /* Implementations of specific functions.  */
      77              : 
      78              : /* Handler for "alloca".  */
      79              : 
      80        10977 : class kf_alloca : public builtin_known_function
      81              : {
      82              : public:
      83         1314 :   bool matches_call_types_p (const call_details &cd) const final override
      84              :   {
      85         1314 :     return cd.num_args () == 1;
      86              :   }
      87         1612 :   enum built_in_function builtin_code () const final override
      88              :   {
      89         1612 :     return BUILT_IN_ALLOCA;
      90              :   }
      91              :   void impl_call_pre (const call_details &cd) const final override;
      92              : };
      93              : 
      94              : void
      95          531 : kf_alloca::impl_call_pre (const call_details &cd) const
      96              : {
      97          531 :   const svalue *size_sval = cd.get_arg_svalue (0);
      98              : 
      99          531 :   region_model *model = cd.get_model ();
     100          531 :   region_model_manager *mgr = cd.get_manager ();
     101              : 
     102          531 :   const region *new_reg
     103          531 :     = model->create_region_for_alloca (size_sval, cd.get_ctxt ());
     104          531 :   const svalue *ptr_sval
     105          531 :     = mgr->get_ptr_svalue (cd.get_lhs_type (), new_reg);
     106          531 :   cd.maybe_set_lhs (ptr_sval);
     107          531 : }
     108              : 
     109              : /* Handler for __atomic_exchange.
     110              :    Although the user-facing documentation specifies it as having this
     111              :    signature:
     112              :      void __atomic_exchange (type *ptr, type *val, type *ret, int memorder)
     113              : 
     114              :    by the time the C/C++ frontends have acted on it, any calls that
     115              :    can't be mapped to a _N variation end up with this signature:
     116              : 
     117              :      void
     118              :      __atomic_exchange (size_t sz, void *ptr, void *val, void *ret,
     119              :                         int memorder)
     120              : 
     121              :    as seen in the gimple seen by the analyzer, and as specified
     122              :    in sync-builtins.def.  */
     123              : 
     124         3659 : class kf_atomic_exchange : public internal_known_function
     125              : {
     126              : public:
     127              :   /* This is effectively:
     128              :        tmpA = *PTR;
     129              :        tmpB = *VAL;
     130              :        *PTR = tmpB;
     131              :        *RET = tmpA;
     132              :   */
     133            4 :   void impl_call_pre (const call_details &cd) const final override
     134              :   {
     135            4 :     auto &num_bytes = cd.get_arg_as_num_bytes (0);
     136            4 :     symbolic::pointer ptr_sym_ptr = cd.get_arg_as_sym_ptr (1);
     137            4 :     tree ptr_tree = cd.get_arg_tree (1);
     138            4 :     symbolic::pointer val_sym_ptr = cd.get_arg_as_sym_ptr (2);
     139            4 :     tree val_tree = cd.get_arg_tree (2);
     140            4 :     symbolic::pointer ret_sym_ptr = cd.get_arg_as_sym_ptr (3);
     141              :     /* Ignore the memorder param.  */
     142              : 
     143            4 :     region_model *model = cd.get_model ();
     144            4 :     region_model_context *ctxt = cd.get_ctxt ();
     145              : 
     146            4 :     auto &tmp_a_bytes
     147            4 :       = model->read_bytes (ptr_sym_ptr, ptr_tree, num_bytes, ctxt);
     148            4 :     auto &tmp_b_bytes
     149            4 :       = model->read_bytes (val_sym_ptr, val_tree, num_bytes, ctxt);
     150            4 :     model->write_bytes (ptr_sym_ptr, tmp_b_bytes, ctxt);
     151            4 :     model->write_bytes (ret_sym_ptr, tmp_a_bytes, ctxt);
     152            4 :   }
     153              : };
     154              : 
     155              : /* Handler for:
     156              :    __atomic_exchange_n (type *ptr, type val, int memorder).  */
     157              : 
     158        21954 : class kf_atomic_exchange_n : public internal_known_function
     159              : {
     160              : public:
     161              :   /* This is effectively:
     162              :        RET = *PTR;
     163              :        *PTR = VAL;
     164              :        return RET;
     165              :   */
     166           39 :   void impl_call_pre (const call_details &cd) const final override
     167              :   {
     168           39 :     const svalue *ptr_sval = cd.get_arg_svalue (0);
     169           39 :     tree ptr_tree = cd.get_arg_tree (0);
     170           39 :     const svalue *set_sval = cd.get_arg_svalue (1);
     171              :     /* Ignore the memorder param.  */
     172              : 
     173           39 :     region_model *model = cd.get_model ();
     174           39 :     region_model_context *ctxt = cd.get_ctxt ();
     175              : 
     176           39 :     const region *dst_region = model->deref_rvalue (ptr_sval, ptr_tree, ctxt);
     177           39 :     const svalue *ret_sval = model->get_store_value (dst_region, ctxt);
     178           39 :     model->set_value (dst_region, set_sval, ctxt);
     179           39 :     cd.maybe_set_lhs (ret_sval);
     180           39 :   }
     181              : };
     182              : 
     183              : /* Handler for:
     184              :    type __atomic_fetch_add (type *ptr, type val, int memorder);
     185              :    type __atomic_fetch_sub (type *ptr, type val, int memorder);
     186              :    type __atomic_fetch_and (type *ptr, type val, int memorder);
     187              :    type __atomic_fetch_xor (type *ptr, type val, int memorder);
     188              :    type __atomic_fetch_or (type *ptr, type val, int memorder);
     189              : */
     190              : 
     191              : class kf_atomic_fetch_op : public internal_known_function
     192              : {
     193              : public:
     194        91475 :   kf_atomic_fetch_op (enum tree_code op): m_op (op) {}
     195              : 
     196              :   /* This is effectively:
     197              :        RET = *PTR;
     198              :        *PTR = RET OP VAL;
     199              :        return RET;
     200              :   */
     201           80 :   void impl_call_pre (const call_details &cd) const final override
     202              :   {
     203           80 :     const svalue *ptr_sval = cd.get_arg_svalue (0);
     204           80 :     tree ptr_tree = cd.get_arg_tree (0);
     205           80 :     const svalue *val_sval = cd.get_arg_svalue (1);
     206              :     /* Ignore the memorder param.  */
     207              : 
     208           80 :     region_model *model = cd.get_model ();
     209           80 :     region_model_manager *mgr = cd.get_manager ();
     210           80 :     region_model_context *ctxt = cd.get_ctxt ();
     211              : 
     212           80 :     const region *star_ptr_region
     213           80 :       = model->deref_rvalue (ptr_sval, ptr_tree, ctxt);
     214           80 :     const svalue *old_sval = model->get_store_value (star_ptr_region, ctxt);
     215          160 :     const svalue *new_sval = mgr->get_or_create_binop (old_sval->get_type (),
     216           80 :                                                        m_op,
     217              :                                                        old_sval, val_sval);
     218           80 :     model->set_value (star_ptr_region, new_sval, ctxt);
     219           80 :     cd.maybe_set_lhs (old_sval);
     220           80 :   }
     221              : 
     222              : private:
     223              :   enum tree_code m_op;
     224              : };
     225              : 
     226              : /* Handler for:
     227              :    type __atomic_add_fetch (type *ptr, type val, int memorder);
     228              :    type __atomic_sub_fetch (type *ptr, type val, int memorder);
     229              :    type __atomic_and_fetch (type *ptr, type val, int memorder);
     230              :    type __atomic_xor_fetch (type *ptr, type val, int memorder);
     231              :    type __atomic_or_fetch (type *ptr, type val, int memorder);
     232              : */
     233              : 
     234              : class kf_atomic_op_fetch : public internal_known_function
     235              : {
     236              : public:
     237        91475 :   kf_atomic_op_fetch (enum tree_code op): m_op (op) {}
     238              : 
     239              :   /* This is effectively:
     240              :        *PTR = RET OP VAL;
     241              :        return *PTR;
     242              :   */
     243           10 :   void impl_call_pre (const call_details &cd) const final override
     244              :   {
     245           10 :     const svalue *ptr_sval = cd.get_arg_svalue (0);
     246           10 :     tree ptr_tree = cd.get_arg_tree (0);
     247           10 :     const svalue *val_sval = cd.get_arg_svalue (1);
     248              :     /* Ignore the memorder param.  */
     249              : 
     250           10 :     region_model *model = cd.get_model ();
     251           10 :     region_model_manager *mgr = cd.get_manager ();
     252           10 :     region_model_context *ctxt = cd.get_ctxt ();
     253              : 
     254           10 :     const region *star_ptr_region
     255           10 :       = model->deref_rvalue (ptr_sval, ptr_tree, ctxt);
     256           10 :     const svalue *old_sval = model->get_store_value (star_ptr_region, ctxt);
     257           20 :     const svalue *new_sval = mgr->get_or_create_binop (old_sval->get_type (),
     258           10 :                                                        m_op,
     259              :                                                        old_sval, val_sval);
     260           10 :     model->set_value (star_ptr_region, new_sval, ctxt);
     261           10 :     cd.maybe_set_lhs (new_sval);
     262           10 :   }
     263              : 
     264              : private:
     265              :   enum tree_code m_op;
     266              : };
     267              : 
     268              : /* Handler for __atomic_load.
     269              :    Although the user-facing documentation specifies it as having this
     270              :    signature:
     271              : 
     272              :       void __atomic_load (type *ptr, type *ret, int memorder)
     273              : 
     274              :    by the time the C/C++ frontends have acted on it, any calls that
     275              :    can't be mapped to a _N variation end up with this signature:
     276              : 
     277              :       void __atomic_load (size_t sz, const void *src, void *dst, int memorder);
     278              : 
     279              :    as seen in the gimple seen by the analyzer, and as specified
     280              :    in sync-builtins.def.  */
     281              : 
     282         3659 : class kf_atomic_load : public internal_known_function
     283              : {
     284              : public:
     285              :   /* This is effectively:
     286              :        memmove (dst, src, sz);
     287              :   */
     288           12 :   void impl_call_pre (const call_details &cd) const final override
     289              :   {
     290           12 :     auto &num_bytes = cd.get_arg_as_num_bytes (0);
     291           12 :     symbolic::pointer src_sym_ptr = cd.get_arg_as_sym_ptr (1);
     292           12 :     tree src_tree = cd.get_arg_tree (1);
     293           12 :     symbolic::pointer dst_sym_ptr = cd.get_arg_as_sym_ptr (2);
     294              :     /* Ignore the memorder param.  */
     295              : 
     296           12 :     region_model *model = cd.get_model ();
     297           12 :     region_model_context *ctxt = cd.get_ctxt ();
     298              : 
     299           12 :     auto &bytes
     300           12 :       = model->read_bytes (src_sym_ptr, src_tree, num_bytes, ctxt);
     301           12 :     model->write_bytes (dst_sym_ptr, bytes, ctxt);
     302           12 :   }
     303              : };
     304              : 
     305              : /* Handler for __atomic_store.
     306              :    Although the user-facing documentation specifies it as having this
     307              :    signature:
     308              : 
     309              :       void __atomic_store (type *ptr, type *val, int memorder)
     310              : 
     311              :    by the time the C/C++ frontends have acted on it, any calls that
     312              :    can't be mapped to a _N variation end up with this signature:
     313              : 
     314              :       void __atomic_store (size_t sz, type *dst, type *src, int memorder)
     315              : 
     316              :    as seen in the gimple seen by the analyzer, and as specified
     317              :    in sync-builtins.def.  */
     318              : 
     319         3659 : class kf_atomic_store : public internal_known_function
     320              : {
     321              : public:
     322              :   /* This is effectively:
     323              :        memmove (dst, src, sz);
     324              :   */
     325            4 :   void impl_call_pre (const call_details &cd) const final override
     326              :   {
     327            4 :     auto &num_bytes = cd.get_arg_as_num_bytes (0);
     328            4 :     symbolic::pointer dst_sym_ptr = cd.get_arg_as_sym_ptr (1);
     329            4 :     symbolic::pointer src_sym_ptr = cd.get_arg_as_sym_ptr (2);
     330            4 :     tree src_tree = cd.get_arg_tree (2);
     331              :     /* Ignore the memorder param.  */
     332              : 
     333            4 :     region_model *model = cd.get_model ();
     334            4 :     region_model_context *ctxt = cd.get_ctxt ();
     335              : 
     336            4 :     auto &bytes
     337            4 :       = model->read_bytes (src_sym_ptr, src_tree, num_bytes, ctxt);
     338            4 :     model->write_bytes (dst_sym_ptr, bytes, ctxt);
     339            4 :   }
     340              : };
     341              : 
     342              : /* Handler for:
     343              :    type __atomic_load_n (type *ptr, int memorder) */
     344              : 
     345        21954 : class kf_atomic_load_n : public internal_known_function
     346              : {
     347              : public:
     348              :   /* This is effectively:
     349              :        RET = *PTR;
     350              :        return RET;
     351              :   */
     352           42 :   void impl_call_pre (const call_details &cd) const final override
     353              :   {
     354           42 :     const svalue *ptr_ptr_sval = cd.get_arg_svalue (0);
     355           42 :     tree ptr_ptr_tree = cd.get_arg_tree (0);
     356              :     /* Ignore the memorder param.  */
     357              : 
     358           42 :     region_model *model = cd.get_model ();
     359           42 :     region_model_context *ctxt = cd.get_ctxt ();
     360              : 
     361           42 :     const region *ptr_region
     362           42 :       = model->deref_rvalue (ptr_ptr_sval, ptr_ptr_tree, ctxt);
     363           42 :     const svalue *star_ptr_sval = model->get_store_value (ptr_region, ctxt);
     364           42 :     cd.maybe_set_lhs (star_ptr_sval);
     365           42 :   }
     366              : };
     367              : 
     368              : /* Handler for:
     369              :    void __atomic_store_n (type *ptr, type val, int memorder) */
     370              : 
     371        21954 : class kf_atomic_store_n : public internal_known_function
     372              : {
     373              : public:
     374              :   /* This is effectively:
     375              :        *PTR = VAL;
     376              :   */
     377           10 :   void impl_call_pre (const call_details &cd) const final override
     378              :   {
     379           10 :     const svalue *ptr_sval = cd.get_arg_svalue (0);
     380           10 :     tree ptr_tree = cd.get_arg_tree (0);
     381           10 :     const svalue *new_sval = cd.get_arg_svalue (1);
     382              :     /* Ignore the memorder param.  */
     383              : 
     384           10 :     region_model *model = cd.get_model ();
     385           10 :     region_model_context *ctxt = cd.get_ctxt ();
     386              : 
     387           10 :     const region *star_ptr_region
     388           10 :       = model->deref_rvalue (ptr_sval, ptr_tree, ctxt);
     389           10 :     model->set_value (star_ptr_region, new_sval, ctxt);
     390           10 :   }
     391              : };
     392              : 
     393              : /* Handler for "__builtin_expect" etc.  */
     394              : 
     395        10977 : class kf_expect : public internal_known_function
     396              : {
     397              : public:
     398          104 :   void impl_call_pre (const call_details &cd) const final override
     399              :   {
     400              :     /* __builtin_expect's return value is its initial argument.  */
     401          104 :     const svalue *sval = cd.get_arg_svalue (0);
     402          104 :     cd.maybe_set_lhs (sval);
     403          104 :   }
     404              : };
     405              : 
     406              : /* Handler for "calloc".  */
     407              : 
     408        10977 : class kf_calloc : public builtin_known_function
     409              : {
     410              : public:
     411         1590 :   bool matches_call_types_p (const call_details &cd) const final override
     412              :   {
     413         1590 :     return (cd.num_args () == 2
     414         1590 :             && cd.arg_is_size_p (0)
     415         3180 :             && cd.arg_is_size_p (1));
     416              :   }
     417         1050 :   enum built_in_function builtin_code () const final override
     418              :   {
     419         1050 :     return BUILT_IN_CALLOC;
     420              :   }
     421              : 
     422              :   void impl_call_pre (const call_details &cd) const final override;
     423              : };
     424              : 
     425              : void
     426          445 : kf_calloc::impl_call_pre (const call_details &cd) const
     427              : {
     428          445 :   region_model *model = cd.get_model ();
     429          445 :   region_model_manager *mgr = cd.get_manager ();
     430          445 :   const svalue *nmemb_sval = cd.get_arg_svalue (0);
     431          445 :   const svalue *size_sval = cd.get_arg_svalue (1);
     432              :   /* TODO: check for overflow here?  */
     433          445 :   const svalue *prod_sval
     434          445 :     = mgr->get_or_create_binop (size_type_node, MULT_EXPR,
     435              :                                 nmemb_sval, size_sval);
     436          445 :   const region *new_reg
     437          445 :     = model->get_or_create_region_for_heap_alloc (prod_sval, cd.get_ctxt ());
     438          445 :   const region *sized_reg
     439          445 :     = mgr->get_sized_region (new_reg, NULL_TREE, prod_sval);
     440          445 :   model->zero_fill_region (sized_reg, cd.get_ctxt ());
     441          445 :   if (cd.get_lhs_type ())
     442              :     {
     443          445 :       const svalue *ptr_sval
     444          445 :         = mgr->get_ptr_svalue (cd.get_lhs_type (), new_reg);
     445          445 :       cd.maybe_set_lhs (ptr_sval);
     446              :     }
     447          445 : }
     448              : 
     449              : /* Handler for glibc's "__errno_location".  */
     450              : 
     451        18295 : class kf_errno_location : public known_function
     452              : {
     453              : public:
     454         2770 :   bool matches_call_types_p (const call_details &cd) const final override
     455              :   {
     456         2770 :     return cd.num_args () == 0;
     457              :   }
     458              : 
     459          425 :   void impl_call_pre (const call_details &cd) const final override
     460              :   {
     461          425 :     if (cd.get_lhs_region ())
     462              :       {
     463          425 :         region_model_manager *mgr = cd.get_manager ();
     464          425 :         const region *errno_reg = mgr->get_errno_region ();
     465          425 :         const svalue *errno_ptr = mgr->get_ptr_svalue (cd.get_lhs_type (),
     466              :                                                        errno_reg);
     467          425 :         cd.maybe_set_lhs (errno_ptr);
     468              :       }
     469          425 :   }
     470              : };
     471              : 
     472              : /* Handler for "error" and "error_at_line" from GNU's non-standard <error.h>.
     473              :    MIN_ARGS identifies the minimum number of expected arguments
     474              :    to be consistent with such a call (3 and 5 respectively).  */
     475              : 
     476              : class kf_error : public known_function
     477              : {
     478              : public:
     479        14636 :   kf_error (unsigned min_args) : m_min_args (min_args) {}
     480              : 
     481          475 :   bool matches_call_types_p (const call_details &cd) const final override
     482              :   {
     483          475 :     return (cd.num_args () >= m_min_args
     484          475 :             && cd.get_arg_type (0) == integer_type_node);
     485              :   }
     486              : 
     487              :   void impl_call_pre (const call_details &cd) const final override;
     488              : 
     489              : private:
     490              :   unsigned m_min_args;
     491              : };
     492              : 
     493              : void
     494           44 : kf_error::impl_call_pre (const call_details &cd) const
     495              : {
     496              :   /* The process exits if status != 0, so it only continues
     497              :      for the case where status == 0.
     498              :      Add that constraint, or terminate this analysis path.  */
     499           44 :   tree status = cd.get_arg_tree (0);
     500           44 :   region_model_context *ctxt = cd.get_ctxt ();
     501           44 :   region_model *model = cd.get_model ();
     502           44 :   if (!model->add_constraint (status, EQ_EXPR, integer_zero_node, ctxt))
     503           15 :     if (ctxt)
     504           15 :       ctxt->terminate_path ();
     505              : 
     506              :   /* Check "format" arg.  */
     507           44 :   const int fmt_arg_idx = (m_min_args == 3) ? 2 : 4;
     508           44 :   model->check_for_null_terminated_string_arg (cd, fmt_arg_idx);
     509           44 : }
     510              : 
     511              : /* Handler for fopen.
     512              :      FILE *fopen (const char *filename, const char *mode);
     513              :    See e.g. https://en.cppreference.com/w/c/io/fopen
     514              :    https://www.man7.org/linux/man-pages/man3/fopen.3.html
     515              :    https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/fopen-wfopen?view=msvc-170  */
     516              : 
     517         3659 : class kf_fopen : public known_function
     518              : {
     519              : public:
     520         2355 :   bool matches_call_types_p (const call_details &cd) const final override
     521              :   {
     522         2355 :     return (cd.num_args () == 2
     523         2355 :             && cd.arg_is_pointer_p (0)
     524         4710 :             && cd.arg_is_pointer_p (1));
     525              :   }
     526              : 
     527          588 :   void impl_call_pre (const call_details &cd) const final override
     528              :   {
     529          588 :     cd.check_for_null_terminated_string_arg (0);
     530          588 :     cd.check_for_null_terminated_string_arg (1);
     531          588 :     cd.set_any_lhs_with_defaults ();
     532              : 
     533              :     /* fopen's mode param is effectively a mini-DSL, but there are various
     534              :        non-standard extensions, so we don't bother to check it.  */
     535          588 :   }
     536              : };
     537              : 
     538              : /* Handler for "free", after sm-handling.
     539              : 
     540              :    If the ptr points to an underlying heap region, delete the region,
     541              :    poisoning pointers to it and regions within it.
     542              : 
     543              :    We delay this until after sm-state has been updated so that the
     544              :    sm-handling can transition all of the various casts of the pointer
     545              :    to a "freed" state *before* we delete the related region here.
     546              : 
     547              :    This has to be done here so that the sm-handling can use the fact
     548              :    that they point to the same region to establish that they are equal
     549              :    (in region_model::eval_condition), and thus transition
     550              :    all pointers to the region to the "freed" state together, regardless
     551              :    of casts.  */
     552              : 
     553        10977 : class kf_free : public builtin_known_function
     554              : {
     555              : public:
     556        69365 :   bool matches_call_types_p (const call_details &cd) const final override
     557              :   {
     558        69365 :     return (cd.num_args () == 1 && cd.arg_is_pointer_p (0));
     559              :   }
     560        66396 :   enum built_in_function builtin_code () const final override
     561              :   {
     562        66396 :     return BUILT_IN_FREE;
     563              :   }
     564              :   void impl_call_post (const call_details &cd) const final override;
     565              : };
     566              : 
     567              : void
     568        13025 : kf_free::impl_call_post (const call_details &cd) const
     569              : {
     570        13025 :   const svalue *ptr_sval = cd.get_arg_svalue (0);
     571        13025 :   if (auto ptr = ptr_sval->maybe_get_symbolic_pointer ())
     572        10361 :     if (ptr->zero_offset_p ())
     573              :       {
     574              :         /* If the ptr points to an underlying heap region, delete it,
     575              :            poisoning pointers.  */
     576        10353 :         const region *freed_reg = &ptr->get_base_region ();
     577        10353 :         region_model *model = cd.get_model ();
     578        10353 :         model->unbind_region_and_descendents (freed_reg, poison_kind::freed);
     579        10353 :         model->unset_dynamic_extents (freed_reg);
     580              :       }
     581        13025 : }
     582              : 
     583              : /* Handle the on_call_pre part of "malloc".  */
     584              : 
     585        10977 : class kf_malloc : public builtin_known_function
     586              : {
     587              : public:
     588        45329 :   bool matches_call_types_p (const call_details &cd) const final override
     589              :   {
     590        45329 :     return (cd.num_args () == 1
     591        45329 :             && cd.arg_is_size_p (0));
     592              :   }
     593        37098 :   enum built_in_function builtin_code () const final override
     594              :   {
     595        37098 :     return BUILT_IN_MALLOC;
     596              :   }
     597              :   void impl_call_pre (const call_details &cd) const final override;
     598              : };
     599              : 
     600              : void
     601        10290 : kf_malloc::impl_call_pre (const call_details &cd) const
     602              : {
     603        10290 :   region_model *model = cd.get_model ();
     604        10290 :   region_model_manager *mgr = cd.get_manager ();
     605        10290 :   const svalue *size_sval = cd.get_arg_svalue (0);
     606        10290 :   const region *new_reg
     607        10290 :     = model->get_or_create_region_for_heap_alloc (size_sval, cd.get_ctxt ());
     608        10290 :   if (cd.get_lhs_type ())
     609              :     {
     610        10290 :       const svalue *ptr_sval
     611        10290 :         = mgr->get_ptr_svalue (cd.get_lhs_type (), new_reg);
     612        10290 :       cd.maybe_set_lhs (ptr_sval);
     613              :     }
     614        10290 : }
     615              : 
     616              : /* Handler for "memcpy" and "__builtin_memcpy",
     617              :    "memmove", and "__builtin_memmove".  */
     618              : 
     619              : class kf_memcpy_memmove : public builtin_known_function
     620              : {
     621              : public:
     622              :   enum kf_memcpy_memmove_variant
     623              :   {
     624              :     KF_MEMCPY,
     625              :     KF_MEMCPY_CHK,
     626              :     KF_MEMMOVE,
     627              :     KF_MEMMOVE_CHK,
     628              :   };
     629        36590 :   kf_memcpy_memmove (enum kf_memcpy_memmove_variant variant)
     630        36590 :     : m_variant (variant) {};
     631         3728 :   bool matches_call_types_p (const call_details &cd) const final override
     632              :   {
     633         3728 :     return (cd.num_args () == 3
     634         3728 :             && cd.arg_is_pointer_p (0)
     635         3728 :             && cd.arg_is_pointer_p (1)
     636         7456 :             && cd.arg_is_size_p (2));
     637              :   }
     638         3792 :   enum built_in_function builtin_code () const final override
     639              :   {
     640         3792 :     switch (m_variant)
     641              :       {
     642              :       case KF_MEMCPY:
     643              :         return BUILT_IN_MEMCPY;
     644              :       case KF_MEMCPY_CHK:
     645              :         return BUILT_IN_MEMCPY_CHK;
     646              :       case KF_MEMMOVE:
     647              :         return BUILT_IN_MEMMOVE;
     648              :       case KF_MEMMOVE_CHK:
     649              :         return BUILT_IN_MEMMOVE_CHK;
     650            0 :       default:
     651            0 :         gcc_unreachable ();
     652              :       }
     653              :   }
     654              :   void impl_call_pre (const call_details &cd) const final override;
     655              : private:
     656              :   const enum kf_memcpy_memmove_variant m_variant;
     657              : };
     658              : 
     659              : void
     660          682 : kf_memcpy_memmove::impl_call_pre (const call_details &cd) const
     661              : {
     662          682 :   auto *dest_ptr_sval = cd.get_arg_svalue (0);
     663          682 :   symbolic::pointer dest_sym_ptr = cd.get_arg_as_sym_ptr (0);
     664          682 :   symbolic::pointer src_sym_ptr = cd.get_arg_as_sym_ptr (1);
     665          682 :   auto &num_bytes = cd.get_arg_as_num_bytes (2);
     666          682 :   const svalue *num_bytes_sval = cd.get_arg_svalue (2);
     667              : 
     668          682 :   region_model *model = cd.get_model ();
     669              : 
     670          682 :   cd.maybe_set_lhs (dest_ptr_sval);
     671              :   /* Check for overlap.  */
     672          682 :   switch (m_variant)
     673              :     {
     674          661 :     case KF_MEMCPY:
     675          661 :     case KF_MEMCPY_CHK:
     676          661 :       cd.complain_about_overlap (0, 1, num_bytes_sval);
     677          661 :       break;
     678              : 
     679              :     case KF_MEMMOVE:
     680              :     case KF_MEMMOVE_CHK:
     681              :       /* It's OK for memmove's arguments to overlap.  */
     682              :       break;
     683              : 
     684            0 :     default:
     685            0 :         gcc_unreachable ();
     686              :     }
     687          682 :   model->copy_bytes (dest_sym_ptr,
     688              :                      src_sym_ptr, cd.get_arg_tree (1),
     689              :                      num_bytes,
     690              :                      cd.get_ctxt ());
     691          682 : }
     692              : 
     693              : /* Handler for "memset" and "__builtin_memset".  */
     694              : 
     695              : class kf_memset : public builtin_known_function
     696              : {
     697              : public:
     698        18295 :   kf_memset (bool chk_variant) : m_chk_variant (chk_variant) {}
     699         3652 :   bool matches_call_types_p (const call_details &cd) const final override
     700              :   {
     701         3652 :     return (cd.num_args () == 3 && cd.arg_is_pointer_p (0));
     702              :   }
     703         3504 :   enum built_in_function builtin_code () const final override
     704              :   {
     705         3504 :     return m_chk_variant ? BUILT_IN_MEMSET_CHK : BUILT_IN_MEMSET;
     706              :   }
     707              :   void impl_call_pre (const call_details &cd) const final override;
     708              : private:
     709              :   const bool m_chk_variant;
     710              : };
     711              : 
     712              : void
     713          703 : kf_memset::impl_call_pre (const call_details &cd) const
     714              : {
     715          703 :   const svalue *dest_sval = cd.get_arg_svalue (0);
     716          703 :   const svalue *fill_value_sval = cd.get_arg_svalue (1);
     717          703 :   const svalue *num_bytes_sval = cd.get_arg_svalue (2);
     718              : 
     719          703 :   region_model *model = cd.get_model ();
     720          703 :   region_model_manager *mgr = cd.get_manager ();
     721              : 
     722          703 :   const region *dest_reg
     723          703 :     = model->deref_rvalue (dest_sval, cd.get_arg_tree (0), cd.get_ctxt ());
     724              : 
     725          703 :   const svalue *fill_value_u8
     726          703 :     = mgr->get_or_create_cast (unsigned_char_type_node, fill_value_sval);
     727              : 
     728          703 :   auto &bv_mgr = mgr->get_node_manager ().get_bit_vector_manager ();
     729          703 :   auto &fill_byte = bv_mgr.get_bit_vector_from_svalue (*fill_value_u8);
     730              : 
     731          703 :   auto &unitless_mgr = mgr->get_node_manager ().get_scalars ().get_unitless ();
     732          703 :   const symbolic::scalar<integer> &repeat_count
     733          703 :     = unitless_mgr.get_scalar_from_svalue (*num_bytes_sval);
     734              : 
     735          703 :   if (!repeat_count.is_zero_p ())
     736              :     {
     737          691 :       auto &fill_bv = bv_mgr.get_repeated_bits (fill_byte, repeat_count);
     738              : 
     739          691 :       auto mem_offset = dest_reg->get_memory_offset (*mgr);
     740          691 :       model->write_to_memory_offset (mem_offset,
     741              :                                      fill_bv,
     742              :                                      cd.get_ctxt ());
     743              :     }
     744              : 
     745          703 :   cd.maybe_set_lhs (dest_sval);
     746          703 : }
     747              : 
     748              : /* A subclass of pending_diagnostic for complaining about functions on the
     749              :    'mktemp' family called on a string literal.  */
     750              : 
     751              : class mktemp_of_string_literal : public undefined_function_behavior
     752              : {
     753              : public:
     754           20 :   mktemp_of_string_literal (const call_details &cd)
     755           20 :     : undefined_function_behavior (cd)
     756              :   {
     757              :   }
     758              : 
     759              :   int
     760           40 :   get_controlling_option () const final override
     761              :   {
     762           40 :     return OPT_Wanalyzer_mktemp_of_string_literal;
     763              :   }
     764              : 
     765              :   bool
     766           20 :   emit (diagnostic_emission_context &ctxt) final override
     767              :   {
     768           20 :     auto_diagnostic_group d;
     769              : 
     770              :     /* SEI CERT C Coding Standard: "STR30-C. Do not attempt to modify string
     771              :        literals.  */
     772           20 :     diagnostics::metadata::precanned_rule rule (
     773           20 :       "STR30-C", "https://wiki.sei.cmu.edu/confluence/x/VtYxBQ");
     774           20 :     ctxt.add_rule (rule);
     775              : 
     776           20 :     bool warned = ctxt.warn ("%qE on a string literal", get_callee_fndecl ());
     777           20 :     if (warned)
     778           20 :       inform (ctxt.get_location (),
     779              :               "use a writable character array as the template argument,"
     780              :               " e.g. %<char tmpl[] = \"/tmp/fooXXXXXX\"%>");
     781           40 :     return warned;
     782           20 :   }
     783              : 
     784              :   bool
     785           40 :   describe_final_event (pretty_printer &pp,
     786              :                         const evdesc::final_event &) final override
     787              :   {
     788           40 :     pp_printf (&pp, "%qE on a string literal", get_callee_fndecl ());
     789           40 :     return true;
     790              :   }
     791              : };
     792              : 
     793              : /* A subclass of pending_diagnostic for complaining about functions in the
     794              :    'mktemp' family called with a template that does not contain the expected
     795              :    "XXXXXX" placeholder.  */
     796              : 
     797              : class mktemp_missing_placeholder
     798              :   : public pending_diagnostic_subclass<mktemp_missing_placeholder>
     799              : {
     800              : public:
     801           34 :   mktemp_missing_placeholder (const call_details &cd, size_t trailing_len)
     802           34 :     : m_call_stmt (cd.get_call_stmt ()), m_fndecl (cd.get_fndecl_for_call ()),
     803           34 :       m_trailing_len (trailing_len)
     804              :   {
     805           34 :     gcc_assert (m_fndecl);
     806           34 :   }
     807              : 
     808              :   const char *
     809         1007 :   get_kind () const final override
     810              :   {
     811         1007 :     return "mktemp_missing_placeholder";
     812              :   }
     813              : 
     814              :   bool
     815           34 :   operator== (const mktemp_missing_placeholder &other) const
     816              :   {
     817           34 :     return &m_call_stmt == &other.m_call_stmt;
     818              :   }
     819              : 
     820              :   int
     821           68 :   get_controlling_option () const final override
     822              :   {
     823           68 :     return OPT_Wanalyzer_mktemp_missing_placeholder;
     824              :   }
     825              : 
     826              :   bool
     827           34 :   emit (diagnostic_emission_context &ctxt) final override
     828              :   {
     829           34 :     if (m_trailing_len == 0)
     830           24 :       return ctxt.warn ("%qE template string does not end with %qs", m_fndecl,
     831           24 :                         "XXXXXX");
     832              :     else
     833           10 :       return ctxt.warn ("%qE template string does not contain %qs"
     834              :                         " before a %zu-character suffix",
     835           10 :                         m_fndecl, "XXXXXX", m_trailing_len);
     836              :   }
     837              : 
     838              :   bool
     839           68 :   describe_final_event (pretty_printer &pp,
     840              :                         const evdesc::final_event &) final override
     841              :   {
     842           68 :     if (m_trailing_len == 0)
     843           48 :       pp_printf (&pp, "%qE template string does not end with %qs", m_fndecl,
     844              :                  "XXXXXX");
     845              :     else
     846           20 :       pp_printf (&pp,
     847              :                  "%qE template string does not contain %qs"
     848              :                  " before a %zu-character suffix",
     849              :                  m_fndecl, "XXXXXX", m_trailing_len);
     850           68 :     return true;
     851              :   }
     852              : 
     853              : private:
     854              :   const gimple &m_call_stmt;
     855              :   tree m_fndecl; // non-NULL
     856              :   size_t m_trailing_len;
     857              : };
     858              : 
     859              : /* A subclass of pending_diagnostic for complaining about 'mkostemp'
     860              :    or 'mkostemps' called with flags that are already included
     861              :    internally (O_CREAT, O_EXCL, O_RDWR).  */
     862              : 
     863              : class mkostemp_redundant_flags
     864              :   : public pending_diagnostic_subclass<mkostemp_redundant_flags>
     865              : {
     866              : public:
     867            8 :   mkostemp_redundant_flags (const call_details &cd)
     868            8 :     : m_call_stmt (cd.get_call_stmt ()), m_fndecl (cd.get_fndecl_for_call ())
     869              :   {
     870            8 :     gcc_assert (m_fndecl);
     871            8 :   }
     872              : 
     873              :   const char *
     874          484 :   get_kind () const final override
     875              :   {
     876          484 :     return "mkostemp_redundant_flags";
     877              :   }
     878              : 
     879              :   bool
     880            8 :   operator== (const mkostemp_redundant_flags &other) const
     881              :   {
     882            8 :     return &m_call_stmt == &other.m_call_stmt;
     883              :   }
     884              : 
     885              :   int
     886           16 :   get_controlling_option () const final override
     887              :   {
     888           16 :     return OPT_Wanalyzer_mkostemp_redundant_flags;
     889              :   }
     890              : 
     891              :   bool
     892            8 :   emit (diagnostic_emission_context &ctxt) final override
     893              :   {
     894            8 :     return ctxt.warn (
     895              :       "%qE flags argument should not include %<O_RDWR%>, %<O_CREAT%>,"
     896              :       " or %<O_EXCL%> as these are already implied",
     897            8 :       m_fndecl);
     898              :   }
     899              : 
     900              :   bool
     901           16 :   describe_final_event (pretty_printer &pp,
     902              :                         const evdesc::final_event &) final override
     903              :   {
     904           16 :     pp_printf (&pp,
     905              :                "%qE flags argument should not include %<O_RDWR%>, %<O_CREAT%>,"
     906              :                " or %<O_EXCL%> as these are already implied",
     907              :                m_fndecl);
     908           16 :     return true;
     909              :   }
     910              : 
     911              : private:
     912              :   const gimple &m_call_stmt;
     913              :   tree m_fndecl; // non-NULL
     914              : };
     915              : 
     916              : class kf_mktemp_family : public known_function
     917              : {
     918              : public:
     919              :   /* Describes how the mktemp-family function signals success or failure
     920              :      through its return value.  */
     921              :   enum class outcome
     922              :   {
     923              :     /* Returns fd on success, -1 on failure (mkstemp, mkostemp, etc.).  */
     924              :     fd,
     925              : 
     926              :     /* Returns pointer on success, NULL on failure (mkdtemp).  */
     927              :     null_ptr,
     928              : 
     929              :     /* Returns template pointer; first byte \0 on failure (mktemp).  */
     930              :     modif_tmpl
     931              :   };
     932              : 
     933              : protected:
     934              :   /* suffixlen_arg_idx is the index of the suffixlen argument, or -1
     935              :      if there is none (trailing_len is implicitly 0).  */
     936        21954 :   kf_mktemp_family (outcome oc, int suffixlen_arg_idx)
     937        21954 :     : m_outcome (oc), m_suffixlen_arg_idx (suffixlen_arg_idx)
     938              :   {
     939              :   }
     940              : 
     941              :   class failure;
     942              :   class success;
     943              : 
     944              :   static void check_for_string_literal_arg (const call_details &cd);
     945              : 
     946              :   /* Check whether the flags argument at FLAGS_ARG_IDX contains any of
     947              :      O_RDWR, O_CREAT, or O_EXCL, which are already included internally
     948              :      by mkostemp/mkostemps.  */
     949              :   static void check_flags (const call_details &cd, unsigned int flags_arg_idx);
     950              : 
     951              :   HOST_WIDE_INT get_trailing_len (const call_details &cd,
     952              :                                   tristate &valid) const;
     953              : 
     954              :   void impl_call_post (const call_details &cd) const final override;
     955              : 
     956              : private:
     957              :   outcome m_outcome;
     958              :   int m_suffixlen_arg_idx;
     959              : 
     960              :   static const int PLACEHOLDER_LEN = 6;
     961              : 
     962              :   /* Return true if the placeholder is "XXXXXX", false if it definitely isn't,
     963              :    or unknown if we can't determine.  */
     964              :   static tristate check_placeholder (const call_details &cd,
     965              :                                      size_t trailing_len,
     966              :                                      const svalue *ptr_sval,
     967              :                                      const svalue *strlen_sval);
     968              : };
     969              : 
     970              : void
     971          123 : kf_mktemp_family::check_for_string_literal_arg (const call_details &cd)
     972              : {
     973          123 :   region_model_context *ctxt = cd.get_ctxt ();
     974          123 :   gcc_assert (ctxt);
     975          123 :   cd.get_model ()->check_for_null_terminated_string_arg (cd, 0, false,
     976              :                                                          nullptr);
     977          123 :   if (cd.get_arg_string_literal (0))
     978              :     {
     979           20 :       ctxt->warn (std::make_unique<mktemp_of_string_literal> (cd));
     980           20 :       ctxt->terminate_path ();
     981              :     }
     982          123 : }
     983              : 
     984              : void
     985           49 : kf_mktemp_family::check_flags (const call_details &cd,
     986              :                                unsigned int flags_arg_idx)
     987              : {
     988           49 :   region_model_context *ctxt = cd.get_ctxt ();
     989           49 :   gcc_assert (ctxt);
     990              : 
     991           49 :   const svalue *flags_sval = cd.get_arg_svalue (flags_arg_idx);
     992           49 :   const constant_svalue *cst = flags_sval->dyn_cast_constant_svalue ();
     993           49 :   if (!cst)
     994              :     return;
     995              : 
     996           39 :   unsigned HOST_WIDE_INT flags = TREE_INT_CST_LOW (cst->get_constant ());
     997              : 
     998              :   /* Check whether any of the implicit flags are redundantly specified. */
     999           39 :   unsigned HOST_WIDE_INT implicit_flags = 0;
    1000          156 :   for (const char *name : { "O_RDWR", "O_CREAT", "O_EXCL" })
    1001          117 :     if (tree cst_tree = get_stashed_constant_by_name (name))
    1002          117 :       implicit_flags |= TREE_INT_CST_LOW (cst_tree);
    1003              : 
    1004           39 :   if (flags & implicit_flags)
    1005            8 :     ctxt->warn (std::make_unique<mkostemp_redundant_flags> (cd));
    1006              : }
    1007              : 
    1008              : /* Extract the trailing length from the suffixlen argument.
    1009              : 
    1010              :    Returns the trailing length on success, or -1 if the suffixlen is
    1011              :    not a compile-time constant.  Sets VALID to TS_FALSE if the
    1012              :    suffixlen is a negative constant (always invalid).  */
    1013              : 
    1014              : HOST_WIDE_INT
    1015          103 : kf_mktemp_family::get_trailing_len (const call_details &cd,
    1016              :                                     tristate &valid) const
    1017              : {
    1018          103 :   if (m_suffixlen_arg_idx < 0)
    1019              :     return 0;
    1020              : 
    1021           36 :   const svalue *suffixlen_sval = cd.get_arg_svalue (m_suffixlen_arg_idx);
    1022           36 :   const constant_svalue *cst = suffixlen_sval->dyn_cast_constant_svalue ();
    1023           36 :   if (!cst)
    1024              :     return -1;
    1025              : 
    1026              :   /* TODO: Negative suffixlen is always wrong and potentially OOB, maybe add a
    1027              :      warning in the future?  */
    1028           26 :   if (tree_int_cst_sgn (cst->get_constant ()) < 0)
    1029              :     {
    1030            0 :       valid = tristate::TS_FALSE;
    1031            0 :       return -1;
    1032              :     }
    1033              : 
    1034           26 :   return TREE_INT_CST_LOW (cst->get_constant ());
    1035              : }
    1036              : 
    1037              : /* Model the failure outcome of a mktemp-family function call.
    1038              : 
    1039              :    Sets the return value according to the function's convention (fd == -1, NULL
    1040              :    pointer, or '\0' in template[0]) and sets errno.  */
    1041              : 
    1042              : class kf_mktemp_family::failure : public failed_call_info
    1043              : {
    1044              : public:
    1045          103 :   failure (const call_details &cd, outcome oc)
    1046          103 :     : failed_call_info (cd), m_outcome (oc)
    1047              :   {
    1048              :   }
    1049              : 
    1050              :   message_detail
    1051            0 :   get_message_detail () const final override
    1052              :   {
    1053            0 :     switch (m_outcome)
    1054              :       {
    1055              :       case outcome::fd:
    1056              :         return message_detail::returning_minus_one;
    1057              :       case outcome::null_ptr:
    1058              :         return message_detail::returning_null;
    1059              :       case outcome::modif_tmpl:
    1060              :         return message_detail::none;
    1061            0 :       default:
    1062            0 :         gcc_unreachable ();
    1063              :       }
    1064              :   }
    1065              : 
    1066              :   bool
    1067          103 :   update_model (region_model *model, const exploded_edge *,
    1068              :                 region_model_context *ctxt) const final override
    1069              :   {
    1070          103 :     const call_details cd (get_call_details (model, ctxt));
    1071              : 
    1072          103 :     switch (m_outcome)
    1073              :       {
    1074           73 :       case outcome::fd:
    1075           73 :         model->update_for_int_cst_return (cd, -1, true);
    1076           73 :         break;
    1077           15 :       case outcome::null_ptr:
    1078           15 :         model->update_for_null_return (cd, true);
    1079           15 :         break;
    1080           15 :       case outcome::modif_tmpl:
    1081           15 :         {
    1082           15 :           const svalue *first_arg_svalue = cd.get_arg_svalue (0);
    1083              : 
    1084              :           /* Return the same pointer that was passed in.  */
    1085           15 :           cd.maybe_set_lhs (first_arg_svalue);
    1086              : 
    1087           15 :           region_model_manager *mgr = cd.get_manager ();
    1088           15 :           const region *template_reg = model->deref_rvalue (
    1089              :             first_arg_svalue, cd.get_arg_tree (0), ctxt);
    1090              : 
    1091              :           /* mktemp may have modified the X positions before failing;
    1092              :              invalidate the old buffer contents.  */
    1093           15 :           model->mark_region_as_unknown (template_reg, nullptr);
    1094              : 
    1095              :           /* Then: template[0] = '\0'.  */
    1096           15 :           const svalue *nul = mgr->get_or_create_int_cst (char_type_node, 0);
    1097           15 :           model->set_value (template_reg, nul, ctxt);
    1098           15 :           break;
    1099              :         }
    1100            0 :       default:
    1101            0 :         gcc_unreachable ();
    1102              :       }
    1103              : 
    1104          103 :     model->set_errno (cd);
    1105          103 :     return true;
    1106              :   }
    1107              : 
    1108              : private:
    1109              :   outcome m_outcome;
    1110              : };
    1111              : 
    1112              : /* Model the success outcome of a mktemp-family function call.
    1113              : 
    1114              :    For fd-returning functions, conjures a non-negative fd and marks it valid.
    1115              :    For pointer-returning functions, returns the template pointer and marks the
    1116              :    template region as modified.  */
    1117              : 
    1118              : class kf_mktemp_family::success : public success_call_info
    1119              : {
    1120              : public:
    1121           69 :   success (const call_details &cd, outcome oc)
    1122           69 :     : success_call_info (cd), m_outcome (oc)
    1123              :   {
    1124              :   }
    1125              : 
    1126              :   message_detail
    1127           93 :   get_message_detail () const final override
    1128              :   {
    1129           93 :     switch (m_outcome)
    1130              :       {
    1131              :       case outcome::fd:
    1132              :         return message_detail::returning_non_negative;
    1133              :       case outcome::null_ptr:
    1134              :         return message_detail::returning_non_null;
    1135              :       case outcome::modif_tmpl:
    1136              :         return message_detail::none;
    1137            0 :       default:
    1138            0 :         gcc_unreachable ();
    1139              :       }
    1140              :   }
    1141              : 
    1142              :   bool
    1143          163 :   update_model (region_model *model, const exploded_edge *,
    1144              :                 region_model_context *ctxt) const final override
    1145              :   {
    1146          163 :     const call_details cd (get_call_details (model, ctxt));
    1147              : 
    1148          163 :     switch (m_outcome)
    1149              :       {
    1150          144 :       case outcome::fd:
    1151          144 :         {
    1152          144 :           region_model_manager *mgr = cd.get_manager ();
    1153          144 :           const region *lhs_reg = cd.get_lhs_region ();
    1154              :           /* conjured_svalue needs a non-null id_reg.  When there is
    1155              :              no LHS, use an unknown symbolic region; we still conjure
    1156              :              and mark the fd so the leak checker can see it.  */
    1157          144 :           const region *id_reg
    1158          144 :             = lhs_reg ? lhs_reg
    1159          116 :                       : mgr->get_unknown_symbolic_region (integer_type_node);
    1160              : 
    1161          144 :           tree lhs_type = cd.get_lhs_type ();
    1162          144 :           if (!lhs_type)
    1163          116 :             lhs_type = integer_type_node;
    1164              : 
    1165          144 :           conjured_purge p (model, ctxt);
    1166          144 :           const svalue *fd_sval = mgr->get_or_create_conjured_svalue (
    1167          144 :             lhs_type, &cd.get_call_stmt (), id_reg, p);
    1168          144 :           const svalue *zero = mgr->get_or_create_int_cst (lhs_type, 0);
    1169              : 
    1170          144 :           if (!model->add_constraint (fd_sval, GE_EXPR, zero, ctxt))
    1171            0 :             return false;
    1172              : 
    1173          144 :           if (lhs_reg)
    1174           28 :             model->set_value (lhs_reg, fd_sval, ctxt);
    1175              : 
    1176              :           /* TODO: transition to an unchecked state so that
    1177              :              use-without-check can be detected.  */
    1178          144 :           model->mark_as_valid_fd (fd_sval, ctxt);
    1179          144 :           break;
    1180              :         }
    1181           12 :       case outcome::null_ptr:
    1182           12 :         {
    1183           12 :           region_model_manager *mgr = cd.get_manager ();
    1184           12 :           const svalue *first_arg_svalue = cd.get_arg_svalue (0);
    1185           12 :           cd.maybe_set_lhs (first_arg_svalue);
    1186              : 
    1187              :           /* On success, mkdtemp returns non-NULL.  */
    1188           12 :           const svalue *null_ptr
    1189           12 :             = mgr->get_or_create_int_cst (first_arg_svalue->get_type (), 0);
    1190           12 :           if (!model->add_constraint (first_arg_svalue, NE_EXPR, null_ptr,
    1191              :                                       ctxt))
    1192              :             return false;
    1193              : 
    1194           11 :           const region *template_reg = model->deref_rvalue (
    1195              :             first_arg_svalue, cd.get_arg_tree (0), ctxt);
    1196           11 :           model->mark_region_as_unknown (template_reg, nullptr);
    1197           11 :           break;
    1198              :         }
    1199            7 :       case outcome::modif_tmpl:
    1200            7 :         {
    1201            7 :           const svalue *first_arg_svalue = cd.get_arg_svalue (0);
    1202            7 :           cd.maybe_set_lhs (first_arg_svalue);
    1203            7 :           const region *template_reg = model->deref_rvalue (
    1204              :             first_arg_svalue, cd.get_arg_tree (0), ctxt);
    1205            7 :           model->mark_region_as_unknown (template_reg, nullptr);
    1206            7 :           break;
    1207              :         }
    1208            0 :       default:
    1209            0 :         gcc_unreachable ();
    1210              :       }
    1211              : 
    1212              :     return true;
    1213              :   }
    1214              : 
    1215              : private:
    1216              :   outcome m_outcome;
    1217              : };
    1218              : 
    1219              : /* Bifurcate into success and failure paths.  The template placeholder is
    1220              :    validated when possible.  If definitely invalid, only the failure path is
    1221              :    explored.  */
    1222              : 
    1223              : void
    1224          103 : kf_mktemp_family::impl_call_post (const call_details &cd) const
    1225              : {
    1226          103 :   if (!cd.get_ctxt ())
    1227            0 :     return;
    1228              : 
    1229          103 :   tristate valid = tristate::TS_UNKNOWN;
    1230          103 :   HOST_WIDE_INT trailing_len = get_trailing_len (cd, valid);
    1231              : 
    1232              :   /* Determine whether the template placeholder is valid.  */
    1233          103 :   const svalue *strlen_sval = nullptr;
    1234          103 :   const svalue *ptr_sval = nullptr;
    1235          103 :   if (trailing_len >= 0 && !valid.is_false ())
    1236              :     {
    1237           93 :       ptr_sval = cd.get_arg_svalue (0);
    1238           93 :       strlen_sval = cd.get_model ()->check_for_null_terminated_string_arg (
    1239              :         cd, 0, false, nullptr);
    1240              :     }
    1241              : 
    1242           93 :   if (strlen_sval)
    1243              :     {
    1244           93 :       valid = check_placeholder (cd, trailing_len, ptr_sval, strlen_sval);
    1245           93 :       if (valid.is_false ())
    1246           68 :         cd.get_ctxt ()->warn (
    1247           34 :           std::make_unique<mktemp_missing_placeholder> (cd, trailing_len));
    1248              :     }
    1249              : 
    1250              :   /* Failure is always possible (bad template or runtime failure).  */
    1251          103 :   cd.get_ctxt ()->bifurcate (std::make_unique<failure> (cd, m_outcome));
    1252              :   /* Success is only possible if the template is not definitely invalid.  */
    1253          103 :   if (!valid.is_false ())
    1254           69 :     cd.get_ctxt ()->bifurcate (std::make_unique<success> (cd, m_outcome));
    1255          103 :   cd.get_ctxt ()->terminate_path ();
    1256              : }
    1257              : 
    1258              : tristate
    1259           93 : kf_mktemp_family::check_placeholder (const call_details &cd,
    1260              :                                      size_t trailing_len,
    1261              :                                      const svalue *ptr_sval,
    1262              :                                      const svalue *strlen_sval)
    1263              : {
    1264           93 :   region_model *model = cd.get_model ();
    1265              : 
    1266           93 :   const constant_svalue *len_cst = strlen_sval->dyn_cast_constant_svalue ();
    1267           93 :   if (!len_cst)
    1268           26 :     return tristate::TS_UNKNOWN;
    1269              : 
    1270           67 :   byte_offset_t len = TREE_INT_CST_LOW (len_cst->get_constant ());
    1271           67 :   if (len < PLACEHOLDER_LEN + trailing_len)
    1272           16 :     return tristate::TS_FALSE;
    1273              : 
    1274           51 :   tree arg_tree = cd.get_arg_tree (0);
    1275           51 :   const region *reg = model->deref_rvalue (ptr_sval, arg_tree, cd.get_ctxt ());
    1276              : 
    1277              :   /* Find the byte offset of the pointed-to region. */
    1278           51 :   auto sym_ptr = reg->get_pointer (*model->get_manager ());
    1279              : 
    1280           51 :   auto &byte_offset = sym_ptr.get_byte_offset ();
    1281           51 :   concrete_byte_offset ptr_byte_offset;
    1282           51 :   if (!concrete_from_symbolic (byte_offset, &ptr_byte_offset))
    1283            0 :     return tristate::TS_UNKNOWN;
    1284              : 
    1285           51 :   const region *base_reg = &sym_ptr.get_base_region ();
    1286           51 :   const svalue *base_sval = model->get_store_value (base_reg, cd.get_ctxt ());
    1287              : 
    1288           51 :   const constant_svalue *cst_sval = base_sval->dyn_cast_constant_svalue ();
    1289           51 :   if (!cst_sval)
    1290            0 :     return tristate::TS_UNKNOWN;
    1291              : 
    1292           51 :   tree cst = cst_sval->get_constant ();
    1293           51 :   if (TREE_CODE (cst) != STRING_CST)
    1294            0 :     return tristate::TS_UNKNOWN;
    1295              : 
    1296           51 :   HOST_WIDE_INT str_len = len.to_shwi ();
    1297           51 :   HOST_WIDE_INT start = ptr_byte_offset.get_integer (). as_long ();
    1298              : 
    1299              :   /* Ensure we can read up to and including the NUL terminator at position
    1300              :      [start + str_len - trailing_len] within the STRING_CST.  */
    1301           51 :   HOST_WIDE_INT range = start + str_len - trailing_len + 1;
    1302           51 :   if (range > TREE_STRING_LENGTH (cst))
    1303            0 :     return tristate::TS_UNKNOWN;
    1304              : 
    1305           51 :   if (memcmp (TREE_STRING_POINTER (cst) + start + str_len - trailing_len
    1306           51 :                 - PLACEHOLDER_LEN,
    1307              :               "XXXXXX", PLACEHOLDER_LEN)
    1308              :       != 0)
    1309           18 :     return tristate::TS_FALSE;
    1310              : 
    1311           33 :   return tristate::TS_TRUE;
    1312           51 : }
    1313              : 
    1314              : /* Handler for calls to "mkdtemp", "mkstemp", and "mktemp", which all
    1315              :    take a single char * template argument.
    1316              : 
    1317              :    The template must not be a string constant, and its last six
    1318              :    characters must be "XXXXXX".  */
    1319              : 
    1320              : class kf_mktemp_simple : public kf_mktemp_family
    1321              : {
    1322              : public:
    1323        10977 :   kf_mktemp_simple (outcome oc) : kf_mktemp_family (oc, -1) {}
    1324              : 
    1325              :   bool
    1326          376 :   matches_call_types_p (const call_details &cd) const final override
    1327              :   {
    1328          376 :     return (cd.num_args () == 1 && cd.arg_is_pointer_p (0));
    1329              :   }
    1330              : 
    1331              :   void
    1332           55 :   impl_call_pre (const call_details &cd) const final override
    1333              :   {
    1334           55 :     if (cd.get_ctxt ())
    1335           55 :       check_for_string_literal_arg (cd);
    1336           55 :   }
    1337              : };
    1338              : 
    1339              : /* Handler for calls to "mkostemp":
    1340              : 
    1341              :      int mkostemp(char *template, int flags);
    1342              : 
    1343              :    The template must not be a string constant, and its last six
    1344              :    characters must be "XXXXXX".  Warns when flags contains O_RDWR,
    1345              :    O_CREAT, or O_EXCL, which are already included internally.  */
    1346              : 
    1347              : class kf_mkostemp : public kf_mktemp_family
    1348              : {
    1349              : public:
    1350         3659 :   kf_mkostemp () : kf_mktemp_family (outcome::fd, -1) {}
    1351              : 
    1352              :   bool
    1353          165 :   matches_call_types_p (const call_details &cd) const final override
    1354              :   {
    1355          330 :     return (cd.num_args () == 2 && cd.arg_is_pointer_p (0)
    1356          330 :             && cd.arg_is_integral_p (1));
    1357              :   }
    1358              : 
    1359              :   void
    1360           24 :   impl_call_pre (const call_details &cd) const final override
    1361              :   {
    1362           24 :     if (cd.get_ctxt ())
    1363              :       {
    1364           24 :         check_for_string_literal_arg (cd);
    1365           24 :         check_flags (cd, 1);
    1366              :       }
    1367           24 :   }
    1368              : };
    1369              : 
    1370              : /* Handler for calls to "mkostemps":
    1371              : 
    1372              :      int mkostemps(char *template, int suffixlen, int flags);
    1373              : 
    1374              :    The template must not be a string constant, and must contain
    1375              :    "XXXXXX" before a suffixlen-character suffix.  Warns when flags
    1376              :    contains O_RDWR, O_CREAT, or O_EXCL, which are already included
    1377              :    internally.  */
    1378              : 
    1379              : class kf_mkostemps : public kf_mktemp_family
    1380              : {
    1381              : public:
    1382         3659 :   kf_mkostemps () : kf_mktemp_family (outcome::fd, 1) {}
    1383              : 
    1384              :   bool
    1385          171 :   matches_call_types_p (const call_details &cd) const final override
    1386              :   {
    1387          342 :     return (cd.num_args () == 3 && cd.arg_is_pointer_p (0)
    1388          342 :             && cd.arg_is_integral_p (1) && cd.arg_is_integral_p (2));
    1389              :   }
    1390              : 
    1391              :   void
    1392           25 :   impl_call_pre (const call_details &cd) const final override
    1393              :   {
    1394           25 :     if (cd.get_ctxt ())
    1395              :       {
    1396           25 :         check_for_string_literal_arg (cd);
    1397           25 :         check_flags (cd, 2);
    1398              :       }
    1399           25 :   }
    1400              : };
    1401              : 
    1402              : /* Handler for calls to "mkstemps":
    1403              : 
    1404              :      int mkstemps(char *template, int suffixlen);
    1405              : 
    1406              :    The template must not be a string constant, and must contain
    1407              :    "XXXXXX" before a suffixlen-character suffix.  */
    1408              : 
    1409              : class kf_mkstemps : public kf_mktemp_family
    1410              : {
    1411              : public:
    1412         3659 :   kf_mkstemps () : kf_mktemp_family (outcome::fd, 1) {}
    1413              : 
    1414              :   bool
    1415          129 :   matches_call_types_p (const call_details &cd) const final override
    1416              :   {
    1417          258 :     return (cd.num_args () == 2 && cd.arg_is_pointer_p (0)
    1418          258 :             && cd.arg_is_integral_p (1));
    1419              :   }
    1420              : 
    1421              :   void
    1422           19 :   impl_call_pre (const call_details &cd) const final override
    1423              :   {
    1424           19 :     if (cd.get_ctxt ())
    1425           19 :       check_for_string_literal_arg (cd);
    1426           19 :   }
    1427              : };
    1428              : 
    1429              : /* A subclass of pending_diagnostic for complaining about 'putenv'
    1430              :    called on an auto var.  */
    1431              : 
    1432              : class putenv_of_auto_var
    1433              : : public pending_diagnostic_subclass<putenv_of_auto_var>
    1434              : {
    1435              : public:
    1436            7 :   putenv_of_auto_var (tree fndecl, const region *reg)
    1437            7 :   : m_fndecl (fndecl), m_reg (reg),
    1438            7 :     m_var_decl (reg->get_base_region ()->maybe_get_decl ())
    1439              :   {
    1440              :   }
    1441              : 
    1442          106 :   const char *get_kind () const final override
    1443              :   {
    1444          106 :     return "putenv_of_auto_var";
    1445              :   }
    1446              : 
    1447            7 :   bool operator== (const putenv_of_auto_var &other) const
    1448              :   {
    1449            7 :     return (m_fndecl == other.m_fndecl
    1450            7 :             && m_reg == other.m_reg
    1451           14 :             && same_tree_p (m_var_decl, other.m_var_decl));
    1452              :   }
    1453              : 
    1454           14 :   int get_controlling_option () const final override
    1455              :   {
    1456           14 :     return OPT_Wanalyzer_putenv_of_auto_var;
    1457              :   }
    1458              : 
    1459            7 :   bool emit (diagnostic_emission_context &ctxt) final override
    1460              :   {
    1461            7 :     auto_diagnostic_group d;
    1462              : 
    1463              :     /* SEI CERT C Coding Standard: "POS34-C. Do not call putenv() with a
    1464              :        pointer to an automatic variable as the argument".  */
    1465            7 :     diagnostics::metadata::precanned_rule
    1466            7 :       rule ("POS34-C", "https://wiki.sei.cmu.edu/confluence/x/6NYxBQ");
    1467            7 :     ctxt.add_rule (rule);
    1468              : 
    1469            7 :     bool warned;
    1470            7 :     if (m_var_decl)
    1471            6 :       warned = ctxt.warn ("%qE on a pointer to automatic variable %qE",
    1472              :                           m_fndecl, m_var_decl);
    1473              :     else
    1474            1 :       warned = ctxt.warn ("%qE on a pointer to an on-stack buffer",
    1475              :                           m_fndecl);
    1476            7 :     if (warned)
    1477              :       {
    1478            7 :         if (m_var_decl)
    1479            6 :           inform (DECL_SOURCE_LOCATION (m_var_decl),
    1480              :                   "%qE declared on stack here", m_var_decl);
    1481            7 :         inform (ctxt.get_location (), "perhaps use %qs rather than %qE",
    1482              :                 "setenv", m_fndecl);
    1483              :       }
    1484              : 
    1485           14 :     return warned;
    1486            7 :   }
    1487              : 
    1488              :   bool
    1489           14 :   describe_final_event (pretty_printer &pp,
    1490              :                         const evdesc::final_event &) final override
    1491              :   {
    1492           14 :     if (m_var_decl)
    1493           12 :       pp_printf  (&pp,
    1494              :                   "%qE on a pointer to automatic variable %qE",
    1495              :                   m_fndecl, m_var_decl);
    1496              :     else
    1497            2 :       pp_printf  (&pp,
    1498              :                   "%qE on a pointer to an on-stack buffer",
    1499              :                   m_fndecl);
    1500           14 :     return true;
    1501              :   }
    1502              : 
    1503           14 :   void mark_interesting_stuff (interesting_t *interest) final override
    1504              :   {
    1505           14 :     if (!m_var_decl)
    1506            2 :       interest->add_region_creation (m_reg->get_base_region ());
    1507           14 :   }
    1508              : 
    1509              : private:
    1510              :   tree m_fndecl; // non-NULL
    1511              :   const region *m_reg; // non-NULL
    1512              :   tree m_var_decl; // could be NULL
    1513              : };
    1514              : 
    1515        21954 : class kf_atoi_family: public known_function
    1516              : {
    1517              : public:
    1518           41 :   bool matches_call_types_p (const call_details &cd) const final override
    1519              :   {
    1520           41 :     return (cd.num_args () == 1 && cd.arg_is_pointer_p (0));
    1521              :   }
    1522              : 
    1523            6 :   void impl_call_pre (const call_details &cd) const final override
    1524              :   {
    1525              :     /* atoi expects a valid, null-terminated string. */
    1526            6 :     cd.check_for_null_terminated_string_arg (0, false, nullptr);
    1527              :     
    1528              :     /* atoi returns an integer, but we don't know what it is statically. 
    1529              :        Tell the analyzer to assume it returns a generic, unknown value. */
    1530            6 :     cd.set_any_lhs_with_defaults ();
    1531            6 :   }
    1532              : };
    1533              : 
    1534              : /* Handler for calls to "getenv".
    1535              :      char *getenv (const char *name);
    1536              : 
    1537              :    Returns either NULL (if the environment variable is not found),
    1538              :    or a pointer to the value string.  */
    1539              : 
    1540         3659 : class kf_getenv : public known_function
    1541              : {
    1542              : public:
    1543           63 :   bool matches_call_types_p (const call_details &cd) const final override
    1544              :   {
    1545           63 :     return (cd.num_args () == 1 && cd.arg_is_pointer_p (0));
    1546              :   }
    1547              : 
    1548            9 :   void impl_call_pre (const call_details &cd) const final override
    1549              :   {
    1550            9 :     cd.check_for_null_terminated_string_arg (0);
    1551            9 :   }
    1552              : 
    1553              :   void impl_call_post (const call_details &cd) const final override;
    1554              : };
    1555              : 
    1556              : void
    1557            9 : kf_getenv::impl_call_post (const call_details &cd) const
    1558              : {
    1559            0 :   class getenv_call_info : public call_info
    1560              :   {
    1561              :   public:
    1562           18 :     getenv_call_info (const call_details &cd, bool found)
    1563           18 :     : call_info (cd), m_found (found)
    1564              :     {
    1565              :     }
    1566              : 
    1567          158 :     void print_desc (pretty_printer &pp) const final override
    1568              :     {
    1569          158 :       if (m_found)
    1570          154 :         pp_printf (&pp,
    1571              :                    "when %qE returns non-NULL",
    1572              :                    get_fndecl ());
    1573              :       else
    1574            4 :         pp_printf (&pp,
    1575              :                    "when %qE returns NULL",
    1576              :                    get_fndecl ());
    1577          158 :     }
    1578              : 
    1579          453 :     bool update_model (region_model *model,
    1580              :                        const exploded_edge *,
    1581              :                        region_model_context *ctxt) const final override
    1582              :     {
    1583          453 :       const call_details cd (get_call_details (model, ctxt));
    1584          453 :       if (tree lhs_type = cd.get_lhs_type ())
    1585              :         {
    1586          451 :           region_model_manager *mgr = model->get_manager ();
    1587          451 :           const svalue *result;
    1588          451 :           if (m_found)
    1589              :             {
    1590              :               /* Return a conjured non-NULL pointer.  */
    1591          262 :               result
    1592          262 :                 = mgr->get_or_create_conjured_svalue (lhs_type,
    1593          262 :                                                        &cd.get_call_stmt (),
    1594              :                                                        cd.get_lhs_region (),
    1595          262 :                                                        conjured_purge (model,
    1596          262 :                                                                        ctxt));
    1597          262 :               const svalue *null_ptr
    1598          262 :                 = mgr->get_or_create_int_cst (lhs_type, 0);
    1599          262 :               model->add_constraint (result, NE_EXPR, null_ptr, ctxt);
    1600              :             }
    1601              :           else
    1602          189 :             result = mgr->get_or_create_int_cst (lhs_type, 0);
    1603          451 :           cd.maybe_set_lhs (result);
    1604              :         }
    1605          453 :       return true;
    1606              :     }
    1607              :   private:
    1608              :     bool m_found;
    1609              :   };
    1610              : 
    1611              :   /* Body of kf_getenv::impl_call_post.  */
    1612            9 :   if (cd.get_ctxt ())
    1613              :     {
    1614            9 :       cd.get_ctxt ()->bifurcate
    1615            9 :         (std::make_unique<getenv_call_info> (cd, false));
    1616            9 :       cd.get_ctxt ()->bifurcate
    1617            9 :         (std::make_unique<getenv_call_info> (cd, true));
    1618            9 :       cd.get_ctxt ()->terminate_path ();
    1619              :     }
    1620            9 : }
    1621              : 
    1622              : /* Handler for calls to "putenv".
    1623              : 
    1624              :    In theory we could try to model the state of the environment variables
    1625              :    for the process; for now we merely complain about putenv of regions
    1626              :    on the stack.  */
    1627              : 
    1628         3659 : class kf_putenv : public known_function
    1629              : {
    1630              : public:
    1631          118 :   bool matches_call_types_p (const call_details &cd) const final override
    1632              :   {
    1633          118 :     return (cd.num_args () == 1 && cd.arg_is_pointer_p (0));
    1634              :   }
    1635              : 
    1636           17 :   void impl_call_pre (const call_details &cd) const final override
    1637              :   {
    1638           17 :     tree fndecl = cd.get_fndecl_for_call ();
    1639           17 :     gcc_assert (fndecl);
    1640           17 :     region_model_context *ctxt = cd.get_ctxt ();
    1641           17 :     region_model *model = cd.get_model ();
    1642           17 :     model->check_for_null_terminated_string_arg (cd, 0);
    1643           17 :     const svalue *ptr_sval = cd.get_arg_svalue (0);
    1644           17 :     const region *reg
    1645           17 :       = model->deref_rvalue (ptr_sval, cd.get_arg_tree (0), ctxt);
    1646           17 :     model->get_store ()->mark_as_escaped (reg->get_base_region (),
    1647              :                                           model->get_baseline_state ());
    1648           17 :     memory_space mem_space = reg->get_memory_space ();
    1649           17 :     switch (mem_space)
    1650              :       {
    1651            0 :       default:
    1652            0 :         gcc_unreachable ();
    1653              :       case memory_space::unknown:
    1654              :       case memory_space::code:
    1655              :       case memory_space::globals:
    1656              :       case memory_space::heap:
    1657              :       case memory_space::read_only_data:
    1658              :         break;
    1659            7 :       case memory_space::stack:
    1660            7 :         if (ctxt)
    1661            7 :           ctxt->warn (std::make_unique<putenv_of_auto_var> (fndecl, reg));
    1662              :         break;
    1663              :       }
    1664           17 :     cd.set_any_lhs_with_defaults ();
    1665           17 :   }
    1666              : };
    1667              : 
    1668              : /* Handler for "realloc":
    1669              : 
    1670              :      void *realloc(void *ptr, size_t size);
    1671              : 
    1672              :    realloc(3) is awkward, since it has various different outcomes
    1673              :    that are best modelled as separate exploded nodes/edges.
    1674              : 
    1675              :    We first check for sm-state, in
    1676              :    malloc_state_machine::on_realloc_call, so that we
    1677              :    can complain about issues such as realloc of a non-heap
    1678              :    pointer, and terminate the path for such cases (and issue
    1679              :    the complaints at the call's exploded node).
    1680              : 
    1681              :    Assuming that these checks pass, we split the path here into
    1682              :    three special cases (and terminate the "standard" path):
    1683              :    (A) failure, returning NULL
    1684              :    (B) success, growing the buffer in-place without moving it
    1685              :    (C) success, allocating a new buffer, copying the content
    1686              :    of the old buffer to it, and freeing the old buffer.
    1687              : 
    1688              :    Each of these has a custom_edge_info subclass, which updates
    1689              :    the region_model and sm-state of the destination state.  */
    1690              : 
    1691        10977 : class kf_realloc : public builtin_known_function
    1692              : {
    1693              : public:
    1694         1805 :   bool matches_call_types_p (const call_details &cd) const final override
    1695              :   {
    1696         1805 :     return (cd.num_args () == 2
    1697         1805 :             && cd.arg_is_pointer_p (0)
    1698         3610 :             && cd.arg_is_size_p (1));
    1699              :   }
    1700              : 
    1701         1830 :   enum built_in_function builtin_code () const final override
    1702              :   {
    1703         1830 :     return BUILT_IN_REALLOC;
    1704              :   }
    1705              : 
    1706              :   void impl_call_post (const call_details &cd) const final override;
    1707              : };
    1708              : 
    1709              : void
    1710          280 : kf_realloc::impl_call_post (const call_details &cd) const
    1711              : {
    1712              :   /* Three custom subclasses of custom_edge_info, for handling the various
    1713              :      outcomes of "realloc".  */
    1714              : 
    1715              :   /* Concrete custom_edge_info: a realloc call that fails, returning NULL.  */
    1716            0 :   class failure : public failed_call_info
    1717              :   {
    1718              :   public:
    1719          280 :     failure (const call_details &cd)
    1720          280 :     : failed_call_info (cd)
    1721              :     {
    1722              :     }
    1723              : 
    1724          334 :     bool update_model (region_model *model,
    1725              :                        const exploded_edge *,
    1726              :                        region_model_context *ctxt) const final override
    1727              :     {
    1728              :       /* Return NULL; everything else is unchanged.  */
    1729          334 :       const call_details cd (get_call_details (model, ctxt));
    1730          334 :       region_model_manager *mgr = cd.get_manager ();
    1731          334 :       if (cd.get_lhs_type ())
    1732              :         {
    1733          325 :           const svalue *zero
    1734          325 :             = mgr->get_or_create_int_cst (cd.get_lhs_type (), 0);
    1735          325 :           model->set_value (cd.get_lhs_region (),
    1736              :                             zero,
    1737              :                             cd.get_ctxt ());
    1738              :         }
    1739          334 :       return true;
    1740              :     }
    1741              :   };
    1742              : 
    1743              :   /* Concrete custom_edge_info: a realloc call that succeeds, growing
    1744              :      the existing buffer without moving it.  */
    1745            0 :   class success_no_move : public call_info
    1746              :   {
    1747              :   public:
    1748          280 :     success_no_move (const call_details &cd)
    1749          280 :     : call_info (cd)
    1750              :     {
    1751              :     }
    1752              : 
    1753            8 :     void print_desc (pretty_printer &pp) const final override
    1754              :     {
    1755            8 :       pp_printf (&pp,
    1756              :                  "when %qE succeeds, without moving buffer",
    1757              :                  get_fndecl ());
    1758            8 :     }
    1759              : 
    1760          302 :     bool update_model (region_model *model,
    1761              :                        const exploded_edge *,
    1762              :                        region_model_context *ctxt) const final override
    1763              :     {
    1764              :       /* Update size of buffer and return the ptr unchanged.  */
    1765          302 :       const call_details cd (get_call_details (model, ctxt));
    1766          302 :       region_model_manager *mgr = cd.get_manager ();
    1767          302 :       const svalue *ptr_sval = cd.get_arg_svalue (0);
    1768          302 :       const svalue *size_sval = cd.get_arg_svalue (1);
    1769              : 
    1770              :       /* We can only grow in place with a non-NULL pointer.  */
    1771          302 :       {
    1772          302 :         const svalue *null_ptr
    1773          302 :           = mgr->get_or_create_int_cst (ptr_sval->get_type (), 0);
    1774          302 :         if (!model->add_constraint (ptr_sval, NE_EXPR, null_ptr,
    1775              :                                     cd.get_ctxt ()))
    1776              :           return false;
    1777              :       }
    1778              : 
    1779          296 :       if (const region *buffer_reg = model->deref_rvalue (ptr_sval, NULL_TREE,
    1780              :                                                           ctxt))
    1781          296 :         if (compat_types_p (size_sval->get_type (), size_type_node))
    1782          296 :           model->set_dynamic_extents (buffer_reg, size_sval, ctxt);
    1783          296 :       if (cd.get_lhs_region ())
    1784              :         {
    1785          287 :           model->set_value (cd.get_lhs_region (), ptr_sval, cd.get_ctxt ());
    1786          287 :           const svalue *zero
    1787          287 :             = mgr->get_or_create_int_cst (cd.get_lhs_type (), 0);
    1788          287 :           return model->add_constraint (ptr_sval, NE_EXPR, zero, ctxt);
    1789              :         }
    1790              :       else
    1791              :         return true;
    1792              :     }
    1793              :   };
    1794              : 
    1795              :   /* Concrete custom_edge_info: a realloc call that succeeds, freeing
    1796              :      the existing buffer and moving the content to a freshly allocated
    1797              :      buffer.  */
    1798            0 :   class success_with_move : public call_info
    1799              :   {
    1800              :   public:
    1801          280 :     success_with_move (const call_details &cd)
    1802          280 :     : call_info (cd)
    1803              :     {
    1804              :     }
    1805              : 
    1806           64 :     void print_desc (pretty_printer &pp) const final override
    1807              :     {
    1808           64 :       pp_printf (&pp,
    1809              :                  "when %qE succeeds, moving buffer",
    1810              :                  get_fndecl ());
    1811           64 :     }
    1812          381 :     bool update_model (region_model *model,
    1813              :                        const exploded_edge *,
    1814              :                        region_model_context *ctxt) const final override
    1815              :     {
    1816          381 :       const call_details cd (get_call_details (model, ctxt));
    1817          381 :       region_model_manager *mgr = cd.get_manager ();
    1818          381 :       const svalue *old_ptr_sval = cd.get_arg_svalue (0);
    1819          381 :       const svalue *new_size_sval = cd.get_arg_svalue (1);
    1820              : 
    1821              :       /* Create the new region.  */
    1822          381 :       const region *new_reg
    1823          381 :         = model->get_or_create_region_for_heap_alloc (new_size_sval, ctxt);
    1824          381 :       const svalue *new_ptr_sval
    1825          381 :         = mgr->get_ptr_svalue (ptr_type_node, new_reg);
    1826          381 :       if (!model->add_constraint (new_ptr_sval, NE_EXPR, old_ptr_sval,
    1827              :                                   cd.get_ctxt ()))
    1828              :         return false;
    1829              : 
    1830          381 :       if (cd.get_lhs_type ())
    1831          372 :         cd.maybe_set_lhs (new_ptr_sval);
    1832              : 
    1833          381 :       if (const region *freed_reg = model->deref_rvalue (old_ptr_sval,
    1834              :                                                          NULL_TREE, ctxt))
    1835              :         {
    1836              :           /* Copy the data.  */
    1837          381 :           const svalue *old_size_sval = model->get_dynamic_extents (freed_reg);
    1838          381 :           if (old_size_sval)
    1839              :             {
    1840          203 :               const svalue *copied_size_sval
    1841          203 :                 = get_copied_size (model, old_size_sval, new_size_sval);
    1842          203 :               const region *copied_old_reg
    1843          203 :                 = mgr->get_sized_region (freed_reg, nullptr, copied_size_sval);
    1844          203 :               const svalue *buffer_content_sval
    1845          203 :                 = model->get_store_value (copied_old_reg, cd.get_ctxt ());
    1846          203 :               const region *copied_new_reg
    1847          203 :                 = mgr->get_sized_region (new_reg, nullptr, copied_size_sval);
    1848          203 :               model->set_value (copied_new_reg, buffer_content_sval,
    1849              :                                 cd.get_ctxt ());
    1850              :             }
    1851              :           else
    1852              :             {
    1853              :               /* We don't know how big the old region was;
    1854              :                  mark the new region as having been touched to avoid uninit
    1855              :                  issues.  */
    1856          178 :               model->mark_region_as_unknown (new_reg, cd.get_uncertainty ());
    1857              :             }
    1858              : 
    1859              :           /* Free the old region, so that pointers to the old buffer become
    1860              :              invalid.  */
    1861              : 
    1862              :           /* If the ptr points to an underlying heap region, delete it,
    1863              :              poisoning pointers.  */
    1864          381 :           model->unbind_region_and_descendents (freed_reg, poison_kind::freed);
    1865          381 :           model->unset_dynamic_extents (freed_reg);
    1866              :         }
    1867              : 
    1868              :       /* Update the sm-state: mark the old_ptr_sval as "freed",
    1869              :          and the new_ptr_sval as "nonnull".  */
    1870          381 :       model->on_realloc_with_move (cd, old_ptr_sval, new_ptr_sval);
    1871              : 
    1872          381 :       if (cd.get_lhs_type ())
    1873              :         {
    1874          372 :           const svalue *zero
    1875          372 :             = mgr->get_or_create_int_cst (cd.get_lhs_type (), 0);
    1876          372 :           return model->add_constraint (new_ptr_sval, NE_EXPR, zero,
    1877          372 :                                         cd.get_ctxt ());
    1878              :         }
    1879              :       else
    1880              :         return true;
    1881              :     }
    1882              : 
    1883              :   private:
    1884              :     /* Return the lesser of OLD_SIZE_SVAL and NEW_SIZE_SVAL.
    1885              :        If unknown, OLD_SIZE_SVAL is returned.  */
    1886          203 :     const svalue *get_copied_size (region_model *model,
    1887              :                                    const svalue *old_size_sval,
    1888              :                                    const svalue *new_size_sval) const
    1889              :     {
    1890          203 :       tristate res
    1891          203 :         = model->eval_condition (old_size_sval, GT_EXPR, new_size_sval);
    1892          203 :       switch (res.get_value ())
    1893              :         {
    1894              :         case tristate::TS_TRUE:
    1895              :           return new_size_sval;
    1896          160 :         case tristate::TS_FALSE:
    1897          160 :         case tristate::TS_UNKNOWN:
    1898          160 :           return old_size_sval;
    1899            0 :         default:
    1900            0 :           gcc_unreachable ();
    1901              :         }
    1902              :     }
    1903              :   };
    1904              : 
    1905              :   /* Body of kf_realloc::impl_call_post.  */
    1906              : 
    1907          280 :   if (cd.get_ctxt ())
    1908              :     {
    1909          280 :       cd.get_ctxt ()->bifurcate (std::make_unique<failure> (cd));
    1910          280 :       cd.get_ctxt ()->bifurcate (std::make_unique<success_no_move> (cd));
    1911          280 :       cd.get_ctxt ()->bifurcate (std::make_unique<success_with_move> (cd));
    1912          280 :       cd.get_ctxt ()->terminate_path ();
    1913              :     }
    1914          280 : }
    1915              : 
    1916              : /* Handler for "strchr" and "__builtin_strchr".  */
    1917              : 
    1918         7318 : class kf_strchr : public builtin_known_function
    1919              : {
    1920              : public:
    1921          153 :   bool matches_call_types_p (const call_details &cd) const final override
    1922              :   {
    1923          153 :     return (cd.num_args () == 2 && cd.arg_is_pointer_p (0));
    1924              :   }
    1925           22 :   void impl_call_pre (const call_details &cd) const final override
    1926              :   {
    1927           22 :     cd.check_for_null_terminated_string_arg (0);
    1928           22 :   }
    1929              : 
    1930          176 :   enum built_in_function builtin_code () const final override
    1931              :   {
    1932          176 :     return BUILT_IN_STRCHR;
    1933              :   }
    1934              :   void impl_call_post (const call_details &cd) const final override;
    1935              : };
    1936              : 
    1937              : void
    1938           21 : kf_strchr::impl_call_post (const call_details &cd) const
    1939              : {
    1940            0 :   class strchr_call_info : public call_info
    1941              :   {
    1942              :   public:
    1943           42 :     strchr_call_info (const call_details &cd, bool found)
    1944           42 :     : call_info (cd), m_found (found)
    1945              :     {
    1946              :     }
    1947              : 
    1948           38 :     void print_desc (pretty_printer &pp) const final override
    1949              :     {
    1950           38 :       if (m_found)
    1951           18 :         pp_printf (&pp,
    1952              :                    "when %qE returns non-NULL",
    1953              :                    get_fndecl ());
    1954              :       else
    1955           20 :         pp_printf (&pp,
    1956              :                    "when %qE returns NULL",
    1957              :                    get_fndecl ());
    1958           38 :     }
    1959              : 
    1960           86 :     bool update_model (region_model *model,
    1961              :                        const exploded_edge *,
    1962              :                        region_model_context *ctxt) const final override
    1963              :     {
    1964           86 :       const call_details cd (get_call_details (model, ctxt));
    1965           86 :       if (tree lhs_type = cd.get_lhs_type ())
    1966              :         {
    1967           84 :           region_model_manager *mgr = model->get_manager ();
    1968           84 :           const svalue *result;
    1969           84 :           if (m_found)
    1970              :             {
    1971           41 :               const svalue *str_sval = cd.get_arg_svalue (0);
    1972           41 :               const region *str_reg
    1973           41 :                 = model->deref_rvalue (str_sval, cd.get_arg_tree (0),
    1974              :                                        cd.get_ctxt ());
    1975              :               /* We want str_sval + OFFSET for some unknown OFFSET.
    1976              :                  Use a conjured_svalue to represent the offset,
    1977              :                  using the str_reg as the id of the conjured_svalue.  */
    1978           41 :               const svalue *offset
    1979           41 :                 = mgr->get_or_create_conjured_svalue (size_type_node,
    1980           41 :                                                       &cd.get_call_stmt (),
    1981              :                                                       str_reg,
    1982           41 :                                                       conjured_purge (model,
    1983           41 :                                                                       ctxt));
    1984           41 :               result = mgr->get_or_create_binop (lhs_type, POINTER_PLUS_EXPR,
    1985              :                                                  str_sval, offset);
    1986              :             }
    1987              :           else
    1988           43 :             result = mgr->get_or_create_int_cst (lhs_type, 0);
    1989           84 :           cd.maybe_set_lhs (result);
    1990              :         }
    1991           86 :       return true;
    1992              :     }
    1993              :   private:
    1994              :     bool m_found;
    1995              :   };
    1996              : 
    1997              :   /* Body of kf_strchr::impl_call_post.  */
    1998           21 :   if (cd.get_ctxt ())
    1999              :     {
    2000           21 :       cd.get_ctxt ()->bifurcate (std::make_unique<strchr_call_info> (cd, false));
    2001           21 :       cd.get_ctxt ()->bifurcate (std::make_unique<strchr_call_info> (cd, true));
    2002           21 :       cd.get_ctxt ()->terminate_path ();
    2003              :     }
    2004           21 : }
    2005              : 
    2006              : /* Handler for "sprintf".
    2007              :      int sprintf(char *str, const char *format, ...);
    2008              : */
    2009              : 
    2010         7318 : class kf_sprintf : public builtin_known_function
    2011              : {
    2012              : public:
    2013         2572 :   bool matches_call_types_p (const call_details &cd) const final override
    2014              :   {
    2015         2572 :     return (cd.num_args () >= 2
    2016         2572 :             && cd.arg_is_pointer_p (0)
    2017         5144 :             && cd.arg_is_pointer_p (1));
    2018              :   }
    2019              : 
    2020          632 :   enum built_in_function builtin_code () const final override
    2021              :   {
    2022          632 :     return BUILT_IN_SPRINTF;
    2023              :   }
    2024              : 
    2025         1093 :   void impl_call_pre (const call_details &cd) const final override
    2026              :   {
    2027              :     /* For now, merely assume that the destination buffer gets set to a
    2028              :        new svalue.  */
    2029         1093 :     region_model *model = cd.get_model ();
    2030         1093 :     region_model_context *ctxt = cd.get_ctxt ();
    2031         1093 :     const svalue *dst_ptr = cd.get_arg_svalue (0);
    2032         1093 :     const region *dst_reg
    2033         1093 :       = model->deref_rvalue (dst_ptr, cd.get_arg_tree (0), ctxt);
    2034              :     /* Restrict the region we consider to be affected to the valid capacity
    2035              :        so that we don't trigger buffer overflow false positives.  */
    2036         1093 :     const svalue *capacity = model->get_capacity (dst_reg);
    2037         1093 :     dst_reg = model->get_manager ()->get_sized_region (dst_reg,
    2038              :                                                        NULL_TREE,
    2039              :                                                        capacity);
    2040         1093 :     const svalue *content = cd.get_or_create_conjured_svalue (dst_reg);
    2041         1093 :     model->set_value (dst_reg, content, ctxt);
    2042         1093 :     cd.set_any_lhs_with_defaults ();
    2043         1093 :   }
    2044              : };
    2045              : 
    2046              : /* Handler for "__builtin_stack_restore".  */
    2047              : 
    2048         3659 : class kf_stack_restore : public pure_known_function_with_default_return
    2049              : {
    2050              : public:
    2051            0 :   bool matches_call_types_p (const call_details &) const final override
    2052              :   {
    2053            0 :     return true;
    2054              :   }
    2055              : 
    2056              :   /* Currently a no-op.  */
    2057              : };
    2058              : 
    2059              : /* Handler for "__builtin_stack_save".  */
    2060              : 
    2061         3659 : class kf_stack_save : public pure_known_function_with_default_return
    2062              : {
    2063              : public:
    2064            0 :   bool matches_call_types_p (const call_details &) const final override
    2065              :   {
    2066            0 :     return true;
    2067              :   }
    2068              : 
    2069              :   /* Currently a no-op.  */
    2070              : };
    2071              : 
    2072              : /* Handler for "__builtin_eh_pointer".  */
    2073              : 
    2074         3659 : class kf_eh_pointer : public builtin_known_function
    2075              : {
    2076              : public:
    2077            0 :   bool matches_call_types_p (const call_details &) const final override
    2078              :   {
    2079            0 :     return true;
    2080              :   }
    2081              : 
    2082         1184 :   enum built_in_function builtin_code () const final override
    2083              :   {
    2084         1184 :     return BUILT_IN_EH_POINTER;
    2085              :   }
    2086              : 
    2087          355 :   void impl_call_pre (const call_details &cd) const final override
    2088              :   {
    2089          355 :     cd.set_any_lhs_with_defaults ();
    2090          355 :   }
    2091              : };
    2092              : 
    2093              : /* Handler for "strcat" and "__builtin_strcat_chk".  */
    2094              : 
    2095              : class kf_strcat : public builtin_known_function
    2096              : {
    2097              : public:
    2098        18295 :   kf_strcat (unsigned int num_args, bool chk_variant)
    2099        18295 :     : m_num_args (num_args),
    2100        18295 :       m_chk_variant (chk_variant) {}
    2101          397 :   bool matches_call_types_p (const call_details &cd) const final override
    2102              :   {
    2103          397 :     return (cd.num_args () == m_num_args
    2104          397 :             && cd.arg_is_pointer_p (0)
    2105          794 :             && cd.arg_is_pointer_p (1));
    2106              :   }
    2107              : 
    2108          456 :   enum built_in_function builtin_code () const final override
    2109              :   {
    2110          456 :     return m_chk_variant ? BUILT_IN_STRCAT_CHK : BUILT_IN_STRCAT;
    2111              :   }
    2112              : 
    2113           57 :   void impl_call_pre (const call_details &cd) const final override
    2114              :   {
    2115           57 :     region_model *model = cd.get_model ();
    2116           57 :     region_model_manager *mgr = cd.get_manager ();
    2117              : 
    2118           57 :     const svalue *dest_sval = cd.get_arg_svalue (0);
    2119           57 :     const region *dest_reg = model->deref_rvalue (dest_sval, cd.get_arg_tree (0),
    2120              :                                                   cd.get_ctxt ());
    2121              : 
    2122           57 :     const svalue *dst_strlen_sval
    2123           57 :       = cd.check_for_null_terminated_string_arg (0, false, nullptr);
    2124           57 :     if (!dst_strlen_sval)
    2125              :       {
    2126            0 :         if (cd.get_ctxt ())
    2127            0 :           cd.get_ctxt ()->terminate_path ();
    2128            0 :         return;
    2129              :       }
    2130              : 
    2131           57 :     const symbolic::bit_vector *bv_to_copy;
    2132           57 :     const svalue *num_src_bytes_read_sval
    2133           57 :       = cd.check_for_null_terminated_string_arg (1, true, &bv_to_copy);
    2134           57 :     if (!num_src_bytes_read_sval)
    2135              :       {
    2136            0 :         if (cd.get_ctxt ())
    2137            0 :           cd.get_ctxt ()->terminate_path ();
    2138              :         return;
    2139              :       }
    2140              : 
    2141           57 :     cd.maybe_set_lhs (dest_sval);
    2142           57 :     cd.complain_about_overlap (0, 1, num_src_bytes_read_sval);
    2143              : 
    2144           57 :     const region *offset_reg
    2145           57 :       = mgr->get_offset_region (dest_reg, NULL_TREE, dst_strlen_sval);
    2146           57 :     model->write_bytes (offset_reg->get_pointer (*mgr),
    2147              :                         *bv_to_copy,
    2148              :                         cd.get_ctxt ());
    2149              :   }
    2150              : 
    2151              : private:
    2152              :   unsigned int m_num_args;
    2153              :   const bool m_chk_variant;
    2154              : };
    2155              : 
    2156              : /* Handler for "strcpy" and "__builtin_strcpy_chk".  */
    2157              : 
    2158              : class kf_strcpy : public builtin_known_function
    2159              : {
    2160              : public:
    2161        18295 :   kf_strcpy (unsigned int num_args, bool chk_variant)
    2162        18295 :     : m_num_args (num_args),
    2163        18295 :       m_chk_variant (chk_variant) {}
    2164         1094 :   bool matches_call_types_p (const call_details &cd) const final override
    2165              :   {
    2166         1094 :     return (cd.num_args () == m_num_args
    2167         1094 :             && cd.arg_is_pointer_p (0)
    2168         2188 :             && cd.arg_is_pointer_p (1));
    2169              :   }
    2170          656 :   enum built_in_function builtin_code () const final override
    2171              :   {
    2172          656 :     return m_chk_variant ? BUILT_IN_STRCPY_CHK : BUILT_IN_STRCPY;
    2173              :   }
    2174              :   void impl_call_pre (const call_details &cd) const final override;
    2175              : 
    2176              : private:
    2177              :   unsigned int m_num_args;
    2178              :   const bool m_chk_variant;
    2179              : };
    2180              : 
    2181              : void
    2182          343 : kf_strcpy::impl_call_pre (const call_details &cd) const
    2183              : {
    2184          343 :   region_model *model = cd.get_model ();
    2185          343 :   region_model_context *ctxt = cd.get_ctxt ();
    2186              : 
    2187          343 :   const svalue *dest_sval = cd.get_arg_svalue (0);
    2188          343 :   const region *dest_reg = model->deref_rvalue (dest_sval, cd.get_arg_tree (0),
    2189              :                                                     ctxt);
    2190              :   /* strcpy returns the initial param.  */
    2191          343 :   cd.maybe_set_lhs (dest_sval);
    2192              : 
    2193          343 :   const symbolic::bit_vector *bv_to_copy;
    2194          686 :   if (const svalue *num_bytes_read_sval
    2195          343 :       = cd.check_for_null_terminated_string_arg (1, true, &bv_to_copy))
    2196              :     {
    2197          343 :       gcc_assert (bv_to_copy);
    2198          343 :       cd.complain_about_overlap (0, 1, num_bytes_read_sval);
    2199          343 :       model->write_bytes (dest_reg->get_pointer (*model->get_manager ()),
    2200              :                           *bv_to_copy,
    2201              :                           ctxt);
    2202              :     }
    2203              :   else
    2204              :     {
    2205            0 :       if (cd.get_ctxt ())
    2206            0 :         cd.get_ctxt ()->terminate_path ();
    2207              :     }
    2208          343 : }
    2209              : 
    2210              : /* Handler for "strdup" and "__builtin_strdup".  */
    2211              : 
    2212         7318 : class kf_strdup : public builtin_known_function
    2213              : {
    2214              : public:
    2215         1451 :   bool matches_call_types_p (const call_details &cd) const final override
    2216              :   {
    2217         1451 :     return (cd.num_args () == 1 && cd.arg_is_pointer_p (0));
    2218              :   }
    2219         1320 :   enum built_in_function builtin_code () const final override
    2220              :   {
    2221         1320 :     return BUILT_IN_STRDUP;
    2222              :   }
    2223          286 :   void impl_call_pre (const call_details &cd) const final override
    2224              :   {
    2225          286 :     region_model *model = cd.get_model ();
    2226          286 :     region_model_context *ctxt = cd.get_ctxt ();
    2227          286 :     region_model_manager *mgr = cd.get_manager ();
    2228          286 :     const symbolic::bit_vector *bv_to_copy;
    2229          572 :     if (const svalue *num_bytes_read_sval
    2230          286 :         = cd.check_for_null_terminated_string_arg (0, true, &bv_to_copy))
    2231              :       {
    2232          286 :         gcc_assert (bv_to_copy);
    2233          286 :         const region *new_reg
    2234          286 :           = model->get_or_create_region_for_heap_alloc (num_bytes_read_sval,
    2235              :                                                         ctxt);
    2236          286 :         model->write_bytes (new_reg->get_pointer (*mgr),
    2237              :                             *bv_to_copy,
    2238              :                             ctxt);
    2239          286 :         if (cd.get_lhs_type ())
    2240              :           {
    2241          286 :             const svalue *ptr_sval
    2242          286 :               = mgr->get_ptr_svalue (cd.get_lhs_type (), new_reg);
    2243          286 :             cd.maybe_set_lhs (ptr_sval);
    2244              :           }
    2245              :       }
    2246              :     else
    2247              :       {
    2248            0 :         if (ctxt)
    2249            0 :           ctxt->terminate_path ();
    2250              :       }
    2251          286 :   }
    2252              : };
    2253              : 
    2254              : /* Handler for "strlen" and for "__analyzer_get_strlen".  */
    2255              : 
    2256        14636 : class kf_strlen : public builtin_known_function
    2257              : {
    2258              : public:
    2259         7574 :   bool matches_call_types_p (const call_details &cd) const final override
    2260              :   {
    2261         7574 :     return (cd.num_args () == 1 && cd.arg_is_pointer_p (0));
    2262              :   }
    2263         5328 :   enum built_in_function builtin_code () const final override
    2264              :   {
    2265         5328 :     return BUILT_IN_STRLEN;
    2266              :   }
    2267              : 
    2268         2127 :   void impl_call_pre (const call_details &cd) const final override
    2269              :   {
    2270         4254 :     if (const svalue *strlen_sval
    2271         2127 :           = cd.check_for_null_terminated_string_arg (0, false, nullptr))
    2272         2127 :       if (strlen_sval->get_kind () != SK_UNKNOWN)
    2273              :         {
    2274         1304 :           cd.maybe_set_lhs (strlen_sval);
    2275         1304 :           return;
    2276              :         }
    2277              : 
    2278              :     /* Use a conjured svalue.  */
    2279          823 :     cd.set_any_lhs_with_defaults ();
    2280              :   }
    2281              : };
    2282              : 
    2283              : /* Factory function, so that kf-analyzer.cc can use this class.  */
    2284              : 
    2285              : std::unique_ptr<known_function>
    2286         3659 : make_kf_strlen ()
    2287              : {
    2288         3659 :   return std::make_unique<kf_strlen> ();
    2289              : }
    2290              : 
    2291              : /* Handler for "strncpy" and "__builtin_strncpy".
    2292              :    See e.g. https://en.cppreference.com/w/c/string/byte/strncpy
    2293              : 
    2294              :      extern char *strncpy (char *dst, const char *src, size_t count);
    2295              : 
    2296              :    Handle this by splitting into two outcomes:
    2297              :    (a) truncated read from "src" of "count" bytes,
    2298              :        writing "count" bytes to "dst"
    2299              :    (b) read from "src" of up to (and including) the null terminator,
    2300              :        where the number of bytes read < "count" bytes,
    2301              :        writing those bytes to "dst", and zero-filling the rest,
    2302              :        up to "count".  */
    2303              : 
    2304        10977 : class kf_strncpy : public builtin_known_function
    2305              : {
    2306              : public:
    2307         1169 :   bool matches_call_types_p (const call_details &cd) const final override
    2308              :   {
    2309         1169 :     return (cd.num_args () == 3
    2310         1169 :             && cd.arg_is_pointer_p (0)
    2311         1169 :             && cd.arg_is_pointer_p (1)
    2312         2338 :             && cd.arg_is_integral_p (2));
    2313              :   }
    2314         1336 :   enum built_in_function builtin_code () const final override
    2315              :   {
    2316         1336 :     return BUILT_IN_STRNCPY;
    2317              :   }
    2318              :   void impl_call_post (const call_details &cd) const final override;
    2319              : };
    2320              : 
    2321              : void
    2322          167 : kf_strncpy::impl_call_post (const call_details &cd) const
    2323              : {
    2324            0 :   class strncpy_call_info : public call_info
    2325              :   {
    2326              :   public:
    2327          334 :     strncpy_call_info (const call_details &cd,
    2328              :                        const svalue *num_bytes_with_terminator_sval,
    2329              :                        bool truncated_read)
    2330              :     : call_info (cd),
    2331          334 :       m_num_bytes_with_terminator_sval (num_bytes_with_terminator_sval),
    2332          334 :       m_truncated_read (truncated_read)
    2333              :     {
    2334              :     }
    2335              : 
    2336          112 :     void print_desc (pretty_printer &pp) const final override
    2337              :     {
    2338          112 :       if (m_truncated_read)
    2339           12 :         pp_printf (&pp,
    2340              :                    "when %qE truncates the source string",
    2341              :                    get_fndecl ());
    2342              :       else
    2343          100 :         pp_printf (&pp,
    2344              :                    "when %qE copies the full source string",
    2345              :                    get_fndecl ());
    2346          112 :     }
    2347              : 
    2348          562 :     bool update_model (region_model *model,
    2349              :                        const exploded_edge *,
    2350              :                        region_model_context *ctxt) const final override
    2351              :     {
    2352          562 :       const call_details cd (get_call_details (model, ctxt));
    2353              : 
    2354          562 :       const svalue *dest_sval = cd.get_arg_svalue (0);
    2355          562 :       const region *dest_reg
    2356          562 :         = model->deref_rvalue (dest_sval, cd.get_arg_tree (0), ctxt);
    2357          562 :       auto dest_sym_ptr = cd.get_arg_as_sym_ptr (0);
    2358              : 
    2359          562 :       auto src_sym_ptr = cd.get_arg_as_sym_ptr (1);
    2360              : 
    2361          562 :       const svalue *count_sval = cd.get_arg_svalue (2);
    2362              : 
    2363              :       /* strncpy returns the initial param.  */
    2364          562 :       cd.maybe_set_lhs (dest_sval);
    2365              : 
    2366          562 :       const svalue *num_bytes_read_sval;
    2367          562 :       if (m_truncated_read)
    2368              :         {
    2369              :           /* Truncated read.  */
    2370          259 :           num_bytes_read_sval = count_sval;
    2371              : 
    2372          259 :           if (m_num_bytes_with_terminator_sval)
    2373              :             {
    2374              :               /* The terminator is after the limit.  */
    2375          259 :               if (!model->add_constraint (m_num_bytes_with_terminator_sval,
    2376              :                                           GT_EXPR,
    2377              :                                           count_sval,
    2378              :                                           ctxt))
    2379              :                 return false;
    2380              :             }
    2381              :           else
    2382              :             {
    2383              :               /* We don't know where the terminator is, or if there is one.
    2384              :                  In theory we know that the first COUNT bytes are non-zero,
    2385              :                  but we don't have a way to record that constraint.  */
    2386              :             }
    2387              :         }
    2388              :       else
    2389              :         {
    2390              :           /* Full read of the src string before reaching the limit,
    2391              :              so there must be a terminator and it must be at or before
    2392              :              the limit.  */
    2393          303 :           if (m_num_bytes_with_terminator_sval)
    2394              :             {
    2395          303 :               if (!model->add_constraint (m_num_bytes_with_terminator_sval,
    2396              :                                           LE_EXPR,
    2397              :                                           count_sval,
    2398              :                                           ctxt))
    2399              :                 return false;
    2400          299 :               num_bytes_read_sval = m_num_bytes_with_terminator_sval;
    2401              : 
    2402              :               /* First, zero-fill the dest buffer.
    2403              :                  We don't need to do this for the truncation case, as
    2404              :                  this fully populates the dest buffer.  */
    2405          299 :               const region *sized_dest_reg
    2406          299 :                 = model->get_manager ()->get_sized_region (dest_reg,
    2407              :                                                            NULL_TREE,
    2408              :                                                            count_sval);
    2409          299 :               model->zero_fill_region (sized_dest_reg, ctxt);
    2410              :             }
    2411              :           else
    2412              :             {
    2413              :               /* Don't analyze this case; the other case will
    2414              :                  assume a "truncated" read up to the limit.  */
    2415              :               return false;
    2416              :             }
    2417              :         }
    2418              : 
    2419          544 :       gcc_assert (num_bytes_read_sval);
    2420              : 
    2421          544 :       auto &node_mgr = model->get_manager ()->get_node_manager ();
    2422          544 :       auto &bytes_mgr = node_mgr.get_scalars ().get_bytes ();
    2423          544 :       auto &num_bytes = bytes_mgr.get_scalar_from_svalue (*num_bytes_read_sval);
    2424              : 
    2425          544 :       const symbolic::bit_vector &bytes_to_copy
    2426          544 :         = model->read_bytes (src_sym_ptr,
    2427              :                              cd.get_arg_tree (1),
    2428              :                              num_bytes,
    2429              :                              ctxt);
    2430          544 :       cd.complain_about_overlap (0, 1, num_bytes_read_sval);
    2431          544 :       model->write_bytes (dest_sym_ptr,
    2432              :                           bytes_to_copy,
    2433              :                           ctxt);
    2434              : 
    2435          544 :       return true;
    2436              :     }
    2437              :   private:
    2438              :     /* (strlen + 1) of the source string if it has a terminator,
    2439              :        or nullptr for the case where UB would happen before
    2440              :        finding any terminator.  */
    2441              :     const svalue *m_num_bytes_with_terminator_sval;
    2442              : 
    2443              :     /* true: if this is the outcome where the limit was reached before
    2444              :        the null terminator
    2445              :        false: if the null terminator was reached before the limit.  */
    2446              :     bool m_truncated_read;
    2447              :   };
    2448              : 
    2449              :   /* Body of kf_strncpy::impl_call_post.  */
    2450          167 :   if (cd.get_ctxt ())
    2451              :     {
    2452              :       /* First, scan for a null terminator as if there were no limit,
    2453              :          with a null ctxt so no errors are reported.  */
    2454          167 :       const region_model *model = cd.get_model ();
    2455          167 :       const svalue *ptr_arg_sval = cd.get_arg_svalue (1);
    2456          167 :       const region *buf_reg
    2457          167 :         = model->deref_rvalue (ptr_arg_sval, cd.get_arg_tree (1), nullptr);
    2458          167 :       const svalue *num_bytes_with_terminator_sval
    2459          167 :         = model->scan_for_null_terminator (buf_reg,
    2460              :                                            cd.get_arg_tree (1),
    2461              :                                            nullptr,
    2462          167 :                                            nullptr);
    2463          167 :       cd.get_ctxt ()->bifurcate
    2464          167 :         (std::make_unique<strncpy_call_info>
    2465          167 :            (cd, num_bytes_with_terminator_sval,
    2466          167 :             false));
    2467          167 :       cd.get_ctxt ()->bifurcate
    2468          167 :         (std::make_unique<strncpy_call_info>
    2469          167 :            (cd, num_bytes_with_terminator_sval,
    2470          167 :             true));
    2471          167 :       cd.get_ctxt ()->terminate_path ();
    2472              :     }
    2473          167 : };
    2474              : 
    2475              : /* Handler for "strndup" and "__builtin_strndup".  */
    2476              : 
    2477         7318 : class kf_strndup : public builtin_known_function
    2478              : {
    2479              : public:
    2480          118 :   bool matches_call_types_p (const call_details &cd) const final override
    2481              :   {
    2482          118 :     return (cd.num_args () == 2 && cd.arg_is_pointer_p (0));
    2483              :   }
    2484           78 :   enum built_in_function builtin_code () const final override
    2485              :   {
    2486           78 :     return BUILT_IN_STRNDUP;
    2487              :   }
    2488           33 :   void impl_call_pre (const call_details &cd) const final override
    2489              :   {
    2490           33 :     region_model *model = cd.get_model ();
    2491           33 :     region_model_manager *mgr = cd.get_manager ();
    2492              :     /* Ideally we'd get the size here, and simulate copying the bytes.  */
    2493           33 :     const region *new_reg
    2494           33 :       = model->get_or_create_region_for_heap_alloc (nullptr, cd.get_ctxt ());
    2495           33 :     model->mark_region_as_unknown (new_reg, nullptr);
    2496           33 :     if (cd.get_lhs_type ())
    2497              :       {
    2498           33 :         const svalue *ptr_sval
    2499           33 :           = mgr->get_ptr_svalue (cd.get_lhs_type (), new_reg);
    2500           33 :         cd.maybe_set_lhs (ptr_sval);
    2501              :       }
    2502           33 :   }
    2503              : };
    2504              : 
    2505              : /* Handler for "strstr" and "__builtin_strstr".
    2506              :      extern char *strstr (const char* str, const char* substr);
    2507              :    See e.g. https://en.cppreference.com/w/c/string/byte/strstr  */
    2508              : 
    2509         7318 : class kf_strstr : public builtin_known_function
    2510              : {
    2511              : public:
    2512          279 :   bool matches_call_types_p (const call_details &cd) const final override
    2513              :   {
    2514          279 :     return (cd.num_args () == 2
    2515          279 :             && cd.arg_is_pointer_p (0)
    2516          558 :             && cd.arg_is_pointer_p (1));
    2517              :   }
    2518          328 :   enum built_in_function builtin_code () const final override
    2519              :   {
    2520          328 :     return BUILT_IN_STRSTR;
    2521              :   }
    2522           41 :   void impl_call_pre (const call_details &cd) const final override
    2523              :   {
    2524           41 :     cd.check_for_null_terminated_string_arg (0);
    2525           41 :     cd.check_for_null_terminated_string_arg (1);
    2526           41 :   }
    2527              :   void impl_call_post (const call_details &cd) const final override;
    2528              : };
    2529              : 
    2530              : void
    2531           33 : kf_strstr::impl_call_post (const call_details &cd) const
    2532              : {
    2533            0 :   class strstr_call_info : public call_info
    2534              :   {
    2535              :   public:
    2536           66 :     strstr_call_info (const call_details &cd, bool found)
    2537           66 :     : call_info (cd), m_found (found)
    2538              :     {
    2539              :     }
    2540              : 
    2541            8 :     void print_desc (pretty_printer &pp) const final override
    2542              :     {
    2543            8 :       if (m_found)
    2544            0 :         pp_printf (&pp,
    2545              :                    "when %qE returns non-NULL",
    2546              :                    get_fndecl ());
    2547              :       else
    2548            8 :         pp_printf (&pp,
    2549              :                    "when %qE returns NULL",
    2550              :                    get_fndecl ());
    2551            8 :     }
    2552              : 
    2553          106 :     bool update_model (region_model *model,
    2554              :                        const exploded_edge *,
    2555              :                        region_model_context *ctxt) const final override
    2556              :     {
    2557          106 :       const call_details cd (get_call_details (model, ctxt));
    2558          106 :       if (tree lhs_type = cd.get_lhs_type ())
    2559              :         {
    2560          106 :           region_model_manager *mgr = model->get_manager ();
    2561          106 :           const svalue *result;
    2562          106 :           if (m_found)
    2563              :             {
    2564           33 :               const svalue *str_sval = cd.get_arg_svalue (0);
    2565           33 :               const region *str_reg
    2566           33 :                 = model->deref_rvalue (str_sval, cd.get_arg_tree (0),
    2567              :                                        cd.get_ctxt ());
    2568              :               /* We want str_sval + OFFSET for some unknown OFFSET.
    2569              :                  Use a conjured_svalue to represent the offset,
    2570              :                  using the str_reg as the id of the conjured_svalue.  */
    2571           33 :               const svalue *offset
    2572           33 :                 = mgr->get_or_create_conjured_svalue (size_type_node,
    2573           33 :                                                       &cd.get_call_stmt (),
    2574              :                                                       str_reg,
    2575           33 :                                                       conjured_purge (model,
    2576           33 :                                                                       ctxt));
    2577           33 :               result = mgr->get_or_create_binop (lhs_type, POINTER_PLUS_EXPR,
    2578              :                                                  str_sval, offset);
    2579              :             }
    2580              :           else
    2581           73 :             result = mgr->get_or_create_int_cst (lhs_type, 0);
    2582          106 :           cd.maybe_set_lhs (result);
    2583              :         }
    2584          106 :       return true;
    2585              :     }
    2586              :   private:
    2587              :     bool m_found;
    2588              :   };
    2589              : 
    2590              :   /* Body of kf_strstr::impl_call_post.  */
    2591           33 :   if (cd.get_ctxt ())
    2592              :     {
    2593           33 :       cd.get_ctxt ()->bifurcate (std::make_unique<strstr_call_info> (cd, false));
    2594           33 :       cd.get_ctxt ()->bifurcate (std::make_unique<strstr_call_info> (cd, true));
    2595           33 :       cd.get_ctxt ()->terminate_path ();
    2596              :     }
    2597           33 : }
    2598              : 
    2599              : /* Handle calls to "strtok".
    2600              :    See e.g.
    2601              :      https://en.cppreference.com/w/c/string/byte/strtok
    2602              :      https://man7.org/linux/man-pages/man3/strtok.3.html  */
    2603              : 
    2604              : class kf_strtok : public known_function
    2605              : {
    2606              : public:
    2607              :   class undefined_behavior : public undefined_function_behavior
    2608              :   {
    2609              :   public:
    2610            8 :     undefined_behavior (const call_details &cd)
    2611            8 :     : undefined_function_behavior (cd)
    2612              :     {
    2613              :     }
    2614           12 :     int get_controlling_option () const final override
    2615              :     {
    2616           12 :       return OPT_Wanalyzer_undefined_behavior_strtok;
    2617              :     }
    2618              : 
    2619            4 :     bool emit (diagnostic_emission_context &ctxt) final override
    2620              :     {
    2621              :       /* CWE-476: NULL Pointer Dereference.  */
    2622            4 :       ctxt.add_cwe (476);
    2623            4 :       if (ctxt.warn ("calling %qD for first time with NULL as argument 1"
    2624              :                      " has undefined behavior",
    2625              :                      get_callee_fndecl ()))
    2626              :         {
    2627            4 :           inform (ctxt.get_location (),
    2628              :                   "some implementations of %qD may crash on such input",
    2629              :                   get_callee_fndecl ());
    2630            4 :           return true;
    2631              :         }
    2632              :       return false;
    2633              :     }
    2634              : 
    2635              :     bool
    2636            8 :     describe_final_event (pretty_printer &pp,
    2637              :                           const evdesc::final_event &) final override
    2638              :     {
    2639            8 :       pp_printf (&pp,
    2640              :                  "calling %qD for first time with NULL as argument 1"
    2641              :                  " has undefined behavior",
    2642              :                  get_callee_fndecl ());
    2643            8 :       return true;
    2644              :     }
    2645              :   };
    2646              : 
    2647              :   /* An outcome of a "strtok" call.
    2648              :      We have a four-way bifurcation of the analysis via the
    2649              :      4 combinations of two flags:
    2650              :      - m_nonnull_str covers whether the "str" param was null or non-null
    2651              :      - m_found covers whether the result is null or non-null
    2652              :    */
    2653              :   class strtok_call_info : public call_info
    2654              :   {
    2655              :   public:
    2656          304 :     strtok_call_info (const call_details &cd,
    2657              :                       const private_region &private_reg,
    2658              :                       bool nonnull_str,
    2659              :                       bool found)
    2660              :     : call_info (cd),
    2661          304 :       m_private_reg (private_reg),
    2662          304 :       m_nonnull_str (nonnull_str),
    2663          304 :       m_found (found)
    2664              :     {
    2665              :     }
    2666              : 
    2667            0 :     void print_desc (pretty_printer &pp) const final override
    2668              :     {
    2669            0 :       if (m_nonnull_str)
    2670              :         {
    2671            0 :           if (m_found)
    2672            0 :             pp_printf (&pp,
    2673              :                        "when %qE on non-NULL string returns non-NULL",
    2674              :                        get_fndecl ());
    2675              :           else
    2676            0 :             pp_printf (&pp,
    2677              :                        "when %qE on non-NULL string returns NULL",
    2678              :                        get_fndecl ());
    2679              :         }
    2680              :       else
    2681              :         {
    2682            0 :           if (m_found)
    2683            0 :             pp_printf (&pp,
    2684              :                        "when %qE with NULL string (using prior) returns"
    2685              :                        " non-NULL",
    2686              :                        get_fndecl ());
    2687              :           else
    2688            0 :             pp_printf (&pp,
    2689              :                        "when %qE with NULL string (using prior) returns NULL",
    2690              :                        get_fndecl ());
    2691              :         }
    2692            0 :     }
    2693              : 
    2694          320 :     bool update_model (region_model *model,
    2695              :                        const exploded_edge *,
    2696              :                        region_model_context *ctxt) const final override
    2697              :     {
    2698          320 :       region_model_manager *mgr = model->get_manager ();
    2699          320 :       const call_details cd (get_call_details (model, ctxt));
    2700          320 :       const svalue *str_sval = cd.get_arg_svalue (0);
    2701              :       /* const svalue *delim_sval = cd.get_arg_svalue (1); */
    2702              : 
    2703          320 :       cd.check_for_null_terminated_string_arg (1);
    2704              :       /* We check that either arg 0 or the private region is null
    2705              :          terminated below.  */
    2706              : 
    2707          320 :       const svalue *null_ptr_sval
    2708          320 :         = mgr->get_or_create_null_ptr (cd.get_arg_type (0));;
    2709          488 :       if (!model->add_constraint (str_sval,
    2710          320 :                                   m_nonnull_str ? NE_EXPR : EQ_EXPR,
    2711              :                                   null_ptr_sval,
    2712              :                                   cd.get_ctxt ()))
    2713              :         return false;
    2714              : 
    2715          208 :       if (m_nonnull_str)
    2716              :         {
    2717              :           /* Update internal buffer.  */
    2718           72 :           model->set_value (&m_private_reg,
    2719              :                             mgr->get_or_create_unmergeable (str_sval),
    2720              :                             ctxt);
    2721              :         }
    2722              :       else
    2723              :         {
    2724              :           /* Read from internal buffer.  */
    2725          136 :           str_sval = model->get_store_value (&m_private_reg, ctxt);
    2726              : 
    2727              :           /* The initial value of the private region is NULL when we're
    2728              :              on a path from main.  */
    2729          272 :           if (const initial_svalue *initial_sval
    2730          136 :                 = str_sval->dyn_cast_initial_svalue ())
    2731           56 :             if (initial_sval->get_region () == &m_private_reg
    2732           56 :                 && model->called_from_main_p ())
    2733              :               {
    2734              :                 /* Implementations of strtok do not necessarily check for NULL
    2735              :                    here, and may crash; see PR analyzer/107573.
    2736              :                    Warn for this, if we were definitely passed NULL.  */
    2737           16 :                 if (cd.get_arg_svalue (0)->all_zeroes_p ())
    2738              :                   {
    2739            8 :                     if (ctxt)
    2740            8 :                       ctxt->warn (::std::make_unique<undefined_behavior> (cd));
    2741              :                   }
    2742              : 
    2743              :                 /* Assume that "str" was actually non-null; terminate
    2744              :                    this path.  */
    2745              :                 return false;
    2746              :               }
    2747              : 
    2748              :           /* Now assume str_sval is non-null.  */
    2749          120 :           if (!model->add_constraint (str_sval,
    2750              :                                       NE_EXPR,
    2751              :                                       null_ptr_sval,
    2752              :                                       cd.get_ctxt ()))
    2753              :             return false;
    2754              :         }
    2755              : 
    2756          192 :       const region *buf_reg = model->deref_rvalue (str_sval, NULL_TREE, ctxt);
    2757          192 :       model->scan_for_null_terminator (buf_reg,
    2758              :                                        NULL_TREE,
    2759              :                                        nullptr,
    2760              :                                        ctxt);
    2761              : 
    2762          192 :       if (m_found)
    2763              :         {
    2764           96 :           const region *str_reg
    2765           96 :             = model->deref_rvalue (str_sval, cd.get_arg_tree (0),
    2766              :                                    cd.get_ctxt ());
    2767              :           /* We want to figure out the start and nul terminator
    2768              :              for the token.
    2769              :              For each, we want str_sval + OFFSET for some unknown OFFSET.
    2770              :              Use a conjured_svalue to represent the offset,
    2771              :              using the str_reg as the id of the conjured_svalue.  */
    2772           96 :           const svalue *start_offset
    2773           96 :             = mgr->get_or_create_conjured_svalue (size_type_node,
    2774           96 :                                                   &cd.get_call_stmt (),
    2775              :                                                   str_reg,
    2776           96 :                                                   conjured_purge (model,
    2777           96 :                                                                   ctxt),
    2778              :                                                   0);
    2779           96 :           const svalue *nul_offset
    2780           96 :             = mgr->get_or_create_conjured_svalue (size_type_node,
    2781           96 :                                                   &cd.get_call_stmt (),
    2782              :                                                   str_reg,
    2783           96 :                                                   conjured_purge (model,
    2784           96 :                                                                   ctxt),
    2785              :                                                   1);
    2786              : 
    2787           96 :           tree char_ptr_type = build_pointer_type (char_type_node);
    2788           96 :           const svalue *result
    2789           96 :             = mgr->get_or_create_binop (char_ptr_type, POINTER_PLUS_EXPR,
    2790              :                                         str_sval, start_offset);
    2791           96 :           cd.maybe_set_lhs (result);
    2792              : 
    2793              :           /* nul_offset + 1; the offset to use for the next call.  */
    2794           96 :           const svalue *next_offset
    2795           96 :             = mgr->get_or_create_binop (size_type_node, PLUS_EXPR,
    2796              :                                         nul_offset,
    2797              :                                         mgr->get_or_create_int_cst
    2798           96 :                                         (char_type_node, 1));
    2799              : 
    2800              :           /* Write '\0' to str_sval[nul_offset].  */
    2801           96 :           const svalue *ptr_to_term
    2802           96 :             = mgr->get_or_create_binop (char_ptr_type, POINTER_PLUS_EXPR,
    2803              :                                         str_sval, nul_offset);
    2804           96 :           const region *terminator_reg
    2805           96 :             = model->deref_rvalue (ptr_to_term, NULL_TREE, cd.get_ctxt ());
    2806           96 :           model->set_value (terminator_reg,
    2807              :                             mgr->get_or_create_unmergeable
    2808              :                             (mgr->get_or_create_int_cst (char_type_node,
    2809           96 :                                                          0)),
    2810              :                             cd.get_ctxt ());
    2811              : 
    2812              :           /* Update saved ptr to be at [nul_offset + 1].  */
    2813           96 :           const svalue *ptr_to_next
    2814           96 :             = mgr->get_or_create_binop (char_ptr_type, POINTER_PLUS_EXPR,
    2815              :                                         str_sval, next_offset);
    2816           96 :           model->set_value (&m_private_reg, ptr_to_next, ctxt);
    2817              :         }
    2818              :       else
    2819           96 :         if (tree lhs_type = cd.get_lhs_type ())
    2820              :           {
    2821           88 :             const svalue *result
    2822           88 :               = mgr->get_or_create_int_cst (lhs_type, 0);
    2823           88 :             cd.maybe_set_lhs (result);
    2824              :           }
    2825              :       return true;
    2826              :     }
    2827              :   private:
    2828              :     const private_region &m_private_reg;
    2829              :     bool m_nonnull_str;
    2830              :     bool m_found;
    2831              :   }; // class strtok_call_info
    2832              : 
    2833         7318 :   kf_strtok (region_model_manager &mgr)
    2834         7318 :   : m_private_reg (mgr.alloc_node_id (),
    2835         7318 :                    *mgr.get_private_region (),
    2836              :                    get_region_type (),
    2837         7318 :                    "strtok buffer")
    2838              :   {
    2839         7318 :   }
    2840              : 
    2841          532 :   bool matches_call_types_p (const call_details &cd) const final override
    2842              :   {
    2843          532 :     return (cd.num_args () == 2
    2844          532 :             && POINTER_TYPE_P (cd.get_arg_type (0))
    2845         1064 :             && POINTER_TYPE_P (cd.get_arg_type (1)));
    2846              :   }
    2847              : 
    2848           76 :   void impl_call_post (const call_details &cd) const final override
    2849              :   {
    2850           76 :     if (cd.get_ctxt ())
    2851              :       {
    2852              :         /* Four-way bifurcation, based on whether:
    2853              :            - the str is non-null
    2854              :            - the result is non-null
    2855              :            Typically the str is either null or non-null at a particular site,
    2856              :            so hopefully this will generally just lead to two out-edges.  */
    2857           76 :         cd.get_ctxt ()->bifurcate
    2858           76 :           (std::make_unique<strtok_call_info> (cd, m_private_reg, false, false));
    2859           76 :         cd.get_ctxt ()->bifurcate
    2860           76 :           (std::make_unique<strtok_call_info> (cd, m_private_reg, false, true));
    2861           76 :         cd.get_ctxt ()->bifurcate
    2862           76 :           (std::make_unique<strtok_call_info> (cd, m_private_reg, true, false));
    2863           76 :         cd.get_ctxt ()->bifurcate
    2864           76 :           (std::make_unique<strtok_call_info> (cd, m_private_reg, true, true));
    2865           76 :         cd.get_ctxt ()->terminate_path ();
    2866              :       }
    2867           76 :   }
    2868              : 
    2869              : private:
    2870         7318 :   static tree get_region_type ()
    2871              :   {
    2872         7318 :     return build_pointer_type (char_type_node);
    2873              :   }
    2874              :   const private_region m_private_reg;
    2875              : };
    2876              : 
    2877              : /* Handle calls to functions referenced by
    2878              :    __attribute__((malloc(FOO))).  */
    2879              : 
    2880              : void
    2881          482 : region_model::impl_deallocation_call (const call_details &cd)
    2882              : {
    2883          482 :   kf_free kf;
    2884          482 :   kf.impl_call_post (cd);
    2885          482 : }
    2886              : 
    2887              : /* Handler for "strcasecmp"   */
    2888              : 
    2889        10977 : class kf_strcasecmp : public builtin_known_function
    2890              : {
    2891              : public:
    2892              :   bool
    2893            0 :   matches_call_types_p (const call_details &cd) const final override
    2894              :   {
    2895            0 :     return (cd.num_args () == 2
    2896            0 :       && cd.arg_is_pointer_p (0)
    2897            0 :       && cd.arg_is_pointer_p (1));
    2898              :   }
    2899              :   void
    2900            7 :   impl_call_pre (const call_details &cd) const final override
    2901              :   {
    2902            7 :     cd.check_for_null_terminated_string_arg (0);
    2903            7 :     cd.check_for_null_terminated_string_arg (1);
    2904            7 :   }
    2905              : 
    2906              :   enum built_in_function
    2907           56 :   builtin_code () const final override
    2908              :   {
    2909           56 :     return BUILT_IN_STRCASECMP;
    2910              :   }
    2911              : 
    2912              :   void impl_call_post (const call_details &cd) const final override;
    2913              : };
    2914              : 
    2915              : void
    2916            7 : kf_strcasecmp::impl_call_post (const call_details &cd) const
    2917              : {
    2918            7 :   if (cd.get_lhs_type ())
    2919              :     {
    2920            2 :       const svalue *result_val
    2921            2 :         = cd.get_or_create_conjured_svalue (cd.get_lhs_region ());
    2922            2 :       cd.maybe_set_lhs (result_val);
    2923              :     }
    2924            7 : }
    2925              : 
    2926              : static void
    2927         3659 : register_atomic_builtins (known_function_manager &kfm)
    2928              : {
    2929         3659 :   kfm.add (BUILT_IN_ATOMIC_EXCHANGE, std::make_unique<kf_atomic_exchange> ());
    2930         3659 :   kfm.add (BUILT_IN_ATOMIC_EXCHANGE_N, std::make_unique<kf_atomic_exchange_n> ());
    2931         3659 :   kfm.add (BUILT_IN_ATOMIC_EXCHANGE_1, std::make_unique<kf_atomic_exchange_n> ());
    2932         3659 :   kfm.add (BUILT_IN_ATOMIC_EXCHANGE_2, std::make_unique<kf_atomic_exchange_n> ());
    2933         3659 :   kfm.add (BUILT_IN_ATOMIC_EXCHANGE_4, std::make_unique<kf_atomic_exchange_n> ());
    2934         3659 :   kfm.add (BUILT_IN_ATOMIC_EXCHANGE_8, std::make_unique<kf_atomic_exchange_n> ());
    2935         3659 :   kfm.add (BUILT_IN_ATOMIC_EXCHANGE_16, std::make_unique<kf_atomic_exchange_n> ());
    2936         3659 :   kfm.add (BUILT_IN_ATOMIC_LOAD, std::make_unique<kf_atomic_load> ());
    2937         3659 :   kfm.add (BUILT_IN_ATOMIC_LOAD_N, std::make_unique<kf_atomic_load_n> ());
    2938         3659 :   kfm.add (BUILT_IN_ATOMIC_LOAD_1, std::make_unique<kf_atomic_load_n> ());
    2939         3659 :   kfm.add (BUILT_IN_ATOMIC_LOAD_2, std::make_unique<kf_atomic_load_n> ());
    2940         3659 :   kfm.add (BUILT_IN_ATOMIC_LOAD_4, std::make_unique<kf_atomic_load_n> ());
    2941         3659 :   kfm.add (BUILT_IN_ATOMIC_LOAD_8, std::make_unique<kf_atomic_load_n> ());
    2942         3659 :   kfm.add (BUILT_IN_ATOMIC_LOAD_16, std::make_unique<kf_atomic_load_n> ());
    2943         3659 :   kfm.add (BUILT_IN_ATOMIC_STORE, std::make_unique<kf_atomic_store> ());
    2944         3659 :   kfm.add (BUILT_IN_ATOMIC_STORE_N, std::make_unique<kf_atomic_store_n> ());
    2945         3659 :   kfm.add (BUILT_IN_ATOMIC_STORE_1, std::make_unique<kf_atomic_store_n> ());
    2946         3659 :   kfm.add (BUILT_IN_ATOMIC_STORE_2, std::make_unique<kf_atomic_store_n> ());
    2947         3659 :   kfm.add (BUILT_IN_ATOMIC_STORE_4, std::make_unique<kf_atomic_store_n> ());
    2948         3659 :   kfm.add (BUILT_IN_ATOMIC_STORE_8, std::make_unique<kf_atomic_store_n> ());
    2949         3659 :   kfm.add (BUILT_IN_ATOMIC_STORE_16, std::make_unique<kf_atomic_store_n> ());
    2950         3659 :   kfm.add (BUILT_IN_ATOMIC_ADD_FETCH_1,
    2951         3659 :            std::make_unique<kf_atomic_op_fetch> (PLUS_EXPR));
    2952         3659 :   kfm.add (BUILT_IN_ATOMIC_ADD_FETCH_2,
    2953         3659 :            std::make_unique<kf_atomic_op_fetch> (PLUS_EXPR));
    2954         3659 :   kfm.add (BUILT_IN_ATOMIC_ADD_FETCH_4,
    2955         3659 :            std::make_unique<kf_atomic_op_fetch> (PLUS_EXPR));
    2956         3659 :   kfm.add (BUILT_IN_ATOMIC_ADD_FETCH_8,
    2957         3659 :            std::make_unique<kf_atomic_op_fetch> (PLUS_EXPR));
    2958         3659 :   kfm.add (BUILT_IN_ATOMIC_ADD_FETCH_16,
    2959         3659 :            std::make_unique<kf_atomic_op_fetch> (PLUS_EXPR));
    2960         3659 :   kfm.add (BUILT_IN_ATOMIC_SUB_FETCH_1,
    2961         3659 :            std::make_unique<kf_atomic_op_fetch> (MINUS_EXPR));
    2962         3659 :   kfm.add (BUILT_IN_ATOMIC_SUB_FETCH_2,
    2963         3659 :            std::make_unique<kf_atomic_op_fetch> (MINUS_EXPR));
    2964         3659 :   kfm.add (BUILT_IN_ATOMIC_SUB_FETCH_4,
    2965         3659 :            std::make_unique<kf_atomic_op_fetch> (MINUS_EXPR));
    2966         3659 :   kfm.add (BUILT_IN_ATOMIC_SUB_FETCH_8,
    2967         3659 :            std::make_unique<kf_atomic_op_fetch> (MINUS_EXPR));
    2968         3659 :   kfm.add (BUILT_IN_ATOMIC_SUB_FETCH_16,
    2969         3659 :            std::make_unique<kf_atomic_op_fetch> (MINUS_EXPR));
    2970         3659 :   kfm.add (BUILT_IN_ATOMIC_AND_FETCH_1,
    2971         3659 :            std::make_unique<kf_atomic_op_fetch> (BIT_AND_EXPR));
    2972         3659 :   kfm.add (BUILT_IN_ATOMIC_AND_FETCH_2,
    2973         3659 :            std::make_unique<kf_atomic_op_fetch> (BIT_AND_EXPR));
    2974         3659 :   kfm.add (BUILT_IN_ATOMIC_AND_FETCH_4,
    2975         3659 :            std::make_unique<kf_atomic_op_fetch> (BIT_AND_EXPR));
    2976         3659 :   kfm.add (BUILT_IN_ATOMIC_AND_FETCH_8,
    2977         3659 :            std::make_unique<kf_atomic_op_fetch> (BIT_AND_EXPR));
    2978         3659 :   kfm.add (BUILT_IN_ATOMIC_AND_FETCH_16,
    2979         3659 :            std::make_unique<kf_atomic_op_fetch> (BIT_AND_EXPR));
    2980         3659 :   kfm.add (BUILT_IN_ATOMIC_XOR_FETCH_1,
    2981         3659 :            std::make_unique<kf_atomic_op_fetch> (BIT_XOR_EXPR));
    2982         3659 :   kfm.add (BUILT_IN_ATOMIC_XOR_FETCH_2,
    2983         3659 :            std::make_unique<kf_atomic_op_fetch> (BIT_XOR_EXPR));
    2984         3659 :   kfm.add (BUILT_IN_ATOMIC_XOR_FETCH_4,
    2985         3659 :            std::make_unique<kf_atomic_op_fetch> (BIT_XOR_EXPR));
    2986         3659 :   kfm.add (BUILT_IN_ATOMIC_XOR_FETCH_8,
    2987         3659 :            std::make_unique<kf_atomic_op_fetch> (BIT_XOR_EXPR));
    2988         3659 :   kfm.add (BUILT_IN_ATOMIC_XOR_FETCH_16,
    2989         3659 :            std::make_unique<kf_atomic_op_fetch> (BIT_XOR_EXPR));
    2990         3659 :   kfm.add (BUILT_IN_ATOMIC_OR_FETCH_1,
    2991         3659 :            std::make_unique<kf_atomic_op_fetch> (BIT_IOR_EXPR));
    2992         3659 :   kfm.add (BUILT_IN_ATOMIC_OR_FETCH_2,
    2993         3659 :            std::make_unique<kf_atomic_op_fetch> (BIT_IOR_EXPR));
    2994         3659 :   kfm.add (BUILT_IN_ATOMIC_OR_FETCH_4,
    2995         3659 :            std::make_unique<kf_atomic_op_fetch> (BIT_IOR_EXPR));
    2996         3659 :   kfm.add (BUILT_IN_ATOMIC_OR_FETCH_8,
    2997         3659 :            std::make_unique<kf_atomic_op_fetch> (BIT_IOR_EXPR));
    2998         3659 :   kfm.add (BUILT_IN_ATOMIC_OR_FETCH_16,
    2999         3659 :            std::make_unique<kf_atomic_op_fetch> (BIT_IOR_EXPR));
    3000         3659 :   kfm.add (BUILT_IN_ATOMIC_FETCH_ADD_1,
    3001         3659 :            std::make_unique<kf_atomic_fetch_op> (PLUS_EXPR));
    3002         3659 :   kfm.add (BUILT_IN_ATOMIC_FETCH_ADD_2,
    3003         3659 :            std::make_unique<kf_atomic_fetch_op> (PLUS_EXPR));
    3004         3659 :   kfm.add (BUILT_IN_ATOMIC_FETCH_ADD_4,
    3005         3659 :            std::make_unique<kf_atomic_fetch_op> (PLUS_EXPR));
    3006         3659 :   kfm.add (BUILT_IN_ATOMIC_FETCH_ADD_8,
    3007         3659 :            std::make_unique<kf_atomic_fetch_op> (PLUS_EXPR));
    3008         3659 :   kfm.add (BUILT_IN_ATOMIC_FETCH_ADD_16,
    3009         3659 :            std::make_unique<kf_atomic_fetch_op> (PLUS_EXPR));
    3010         3659 :   kfm.add (BUILT_IN_ATOMIC_FETCH_SUB_1,
    3011         3659 :            std::make_unique<kf_atomic_fetch_op> (MINUS_EXPR));
    3012         3659 :   kfm.add (BUILT_IN_ATOMIC_FETCH_SUB_2,
    3013         3659 :            std::make_unique<kf_atomic_fetch_op> (MINUS_EXPR));
    3014         3659 :   kfm.add (BUILT_IN_ATOMIC_FETCH_SUB_4,
    3015         3659 :            std::make_unique<kf_atomic_fetch_op> (MINUS_EXPR));
    3016         3659 :   kfm.add (BUILT_IN_ATOMIC_FETCH_SUB_8,
    3017         3659 :            std::make_unique<kf_atomic_fetch_op> (MINUS_EXPR));
    3018         3659 :   kfm.add (BUILT_IN_ATOMIC_FETCH_SUB_16,
    3019         3659 :            std::make_unique<kf_atomic_fetch_op> (MINUS_EXPR));
    3020         3659 :   kfm.add (BUILT_IN_ATOMIC_FETCH_AND_1,
    3021         3659 :            std::make_unique<kf_atomic_fetch_op> (BIT_AND_EXPR));
    3022         3659 :   kfm.add (BUILT_IN_ATOMIC_FETCH_AND_2,
    3023         3659 :            std::make_unique<kf_atomic_fetch_op> (BIT_AND_EXPR));
    3024         3659 :   kfm.add (BUILT_IN_ATOMIC_FETCH_AND_4,
    3025         3659 :            std::make_unique<kf_atomic_fetch_op> (BIT_AND_EXPR));
    3026         3659 :   kfm.add (BUILT_IN_ATOMIC_FETCH_AND_8,
    3027         3659 :            std::make_unique<kf_atomic_fetch_op> (BIT_AND_EXPR));
    3028         3659 :   kfm.add (BUILT_IN_ATOMIC_FETCH_AND_16,
    3029         3659 :            std::make_unique<kf_atomic_fetch_op> (BIT_AND_EXPR));
    3030         3659 :   kfm.add (BUILT_IN_ATOMIC_FETCH_XOR_1,
    3031         3659 :            std::make_unique<kf_atomic_fetch_op> (BIT_XOR_EXPR));
    3032         3659 :   kfm.add (BUILT_IN_ATOMIC_FETCH_XOR_2,
    3033         3659 :            std::make_unique<kf_atomic_fetch_op> (BIT_XOR_EXPR));
    3034         3659 :   kfm.add (BUILT_IN_ATOMIC_FETCH_XOR_4,
    3035         3659 :            std::make_unique<kf_atomic_fetch_op> (BIT_XOR_EXPR));
    3036         3659 :   kfm.add (BUILT_IN_ATOMIC_FETCH_XOR_8,
    3037         3659 :            std::make_unique<kf_atomic_fetch_op> (BIT_XOR_EXPR));
    3038         3659 :   kfm.add (BUILT_IN_ATOMIC_FETCH_XOR_16,
    3039         3659 :            std::make_unique<kf_atomic_fetch_op> (BIT_XOR_EXPR));
    3040         3659 :   kfm.add (BUILT_IN_ATOMIC_FETCH_OR_1,
    3041         3659 :            std::make_unique<kf_atomic_fetch_op> (BIT_IOR_EXPR));
    3042         3659 :   kfm.add (BUILT_IN_ATOMIC_FETCH_OR_2,
    3043         3659 :            std::make_unique<kf_atomic_fetch_op> (BIT_IOR_EXPR));
    3044         3659 :   kfm.add (BUILT_IN_ATOMIC_FETCH_OR_4,
    3045         3659 :            std::make_unique<kf_atomic_fetch_op> (BIT_IOR_EXPR));
    3046         3659 :   kfm.add (BUILT_IN_ATOMIC_FETCH_OR_8,
    3047         3659 :            std::make_unique<kf_atomic_fetch_op> (BIT_IOR_EXPR));
    3048         3659 :   kfm.add (BUILT_IN_ATOMIC_FETCH_OR_16,
    3049         3659 :            std::make_unique<kf_atomic_fetch_op> (BIT_IOR_EXPR));
    3050         3659 : }
    3051              : 
    3052              : /* Handle calls to the various IFN_UBSAN_* with no return value.
    3053              :    For now, treat these as no-ops.  */
    3054              : 
    3055        10977 : class kf_ubsan_noop : public internal_known_function
    3056              : {
    3057              : };
    3058              : 
    3059              : /* Handle calls to the various __builtin___ubsan_handle_*.
    3060              :    These can return, but continuing after such a return
    3061              :    isn't likely to be interesting to the user of the analyzer.
    3062              :    Hence we terminate the analysis path at one of these calls.  */
    3063              : 
    3064         3659 : class kf_ubsan_handler : public internal_known_function
    3065              : {
    3066           12 :   void impl_call_post (const call_details &cd) const final override
    3067              :   {
    3068           12 :     if (cd.get_ctxt ())
    3069           12 :       cd.get_ctxt ()->terminate_path ();
    3070           12 :   }
    3071              : };
    3072              : 
    3073              : static void
    3074         3659 : register_sanitizer_builtins (known_function_manager &kfm)
    3075              : {
    3076              :   /* Handle calls to the various IFN_UBSAN_* with no return value.
    3077              :      For now, treat these as no-ops.  */
    3078         3659 :   kfm.add (IFN_UBSAN_NULL,
    3079         3659 :            std::make_unique<kf_ubsan_noop> ());
    3080         3659 :   kfm.add (IFN_UBSAN_BOUNDS,
    3081         3659 :            std::make_unique<kf_ubsan_noop> ());
    3082         3659 :   kfm.add (IFN_UBSAN_PTR,
    3083         3659 :            std::make_unique<kf_ubsan_noop> ());
    3084              : 
    3085         3659 :   kfm.add (BUILT_IN_UBSAN_HANDLE_NONNULL_ARG,
    3086         3659 :            std::make_unique<kf_ubsan_handler> ());
    3087         3659 : }
    3088              : 
    3089              : /* Populate KFM with instances of known functions supported by the core of the
    3090              :    analyzer (as opposed to plugins).  */
    3091              : 
    3092              : void
    3093         3659 : register_known_functions (known_function_manager &kfm,
    3094              :                           region_model_manager &rmm)
    3095              : {
    3096              :   /* Debugging/test support functions, all  with a "__analyzer_" prefix.  */
    3097         3659 :   register_known_analyzer_functions (kfm);
    3098              : 
    3099              :   /* Internal fns the analyzer has known_functions for.  */
    3100         3659 :   {
    3101         3659 :     kfm.add (IFN_BUILTIN_EXPECT, std::make_unique<kf_expect> ());
    3102              :   }
    3103              : 
    3104              :   /* GCC built-ins that do not correspond to a function
    3105              :      in the standard library.  */
    3106         3659 :   {
    3107         3659 :     kfm.add (BUILT_IN_EXPECT, std::make_unique<kf_expect> ());
    3108         3659 :     kfm.add (BUILT_IN_EXPECT_WITH_PROBABILITY, std::make_unique<kf_expect> ());
    3109         3659 :     kfm.add (BUILT_IN_ALLOCA_WITH_ALIGN, std::make_unique<kf_alloca> ());
    3110         3659 :     kfm.add (BUILT_IN_STACK_RESTORE, std::make_unique<kf_stack_restore> ());
    3111         3659 :     kfm.add (BUILT_IN_STACK_SAVE, std::make_unique<kf_stack_save> ());
    3112              : 
    3113         3659 :     kfm.add (BUILT_IN_EH_POINTER, std::make_unique<kf_eh_pointer> ());
    3114              : 
    3115         3659 :     kfm.add(BUILT_IN_STRCASECMP, std::make_unique<kf_strcasecmp>());
    3116              : 
    3117         3659 :     register_atomic_builtins (kfm);
    3118         3659 :     register_sanitizer_builtins (kfm);
    3119         3659 :     register_varargs_builtins (kfm);
    3120              :   }
    3121              : 
    3122              :   /* Known builtins and C standard library functions
    3123              :      the analyzer has known functions for.  */
    3124         3659 :   {
    3125         3659 :     kfm.add ("atoi", std::make_unique<kf_atoi_family> ());
    3126         3659 :     kfm.add ("atol", std::make_unique<kf_atoi_family> ());
    3127         3659 :     kfm.add ("atoll", std::make_unique<kf_atoi_family> ());
    3128              : 
    3129         3659 :     kfm.add ("alloca", std::make_unique<kf_alloca> ());
    3130         3659 :     kfm.add ("__builtin_alloca", std::make_unique<kf_alloca> ());
    3131         3659 :     kfm.add ("calloc", std::make_unique<kf_calloc> ());
    3132         3659 :     kfm.add ("__builtin_calloc", std::make_unique<kf_calloc> ());
    3133         3659 :     kfm.add ("free", std::make_unique<kf_free> ());
    3134         3659 :     kfm.add ("__builtin_free", std::make_unique<kf_free> ());
    3135         3659 :     kfm.add ("malloc", std::make_unique<kf_malloc> ());
    3136         3659 :     kfm.add ("__builtin_malloc", std::make_unique<kf_malloc> ());
    3137         3659 :     kfm.add ("memcpy",
    3138         3659 :               std::make_unique<kf_memcpy_memmove> (kf_memcpy_memmove::KF_MEMCPY));
    3139         3659 :     kfm.add ("__builtin_memcpy",
    3140         3659 :               std::make_unique<kf_memcpy_memmove> (kf_memcpy_memmove::KF_MEMCPY));
    3141         3659 :     kfm.add ("__memcpy_chk", std::make_unique<kf_memcpy_memmove>
    3142         3659 :                               (kf_memcpy_memmove::KF_MEMCPY_CHK));
    3143         3659 :     kfm.add ("__builtin___memcpy_chk", std::make_unique<kf_memcpy_memmove>
    3144         3659 :                               (kf_memcpy_memmove::KF_MEMCPY_CHK));
    3145         3659 :     kfm.add ("memmove",
    3146         3659 :               std::make_unique<kf_memcpy_memmove> (kf_memcpy_memmove::KF_MEMMOVE));
    3147         3659 :     kfm.add ("__builtin_memmove",
    3148         3659 :               std::make_unique<kf_memcpy_memmove> (kf_memcpy_memmove::KF_MEMMOVE));
    3149         3659 :     kfm.add ("__memmove_chk", std::make_unique<kf_memcpy_memmove>
    3150         3659 :                               (kf_memcpy_memmove::KF_MEMMOVE_CHK));
    3151         3659 :     kfm.add ("__builtin___memmove_chk", std::make_unique<kf_memcpy_memmove>
    3152         3659 :                               (kf_memcpy_memmove::KF_MEMMOVE_CHK));
    3153         3659 :     kfm.add ("memset", std::make_unique<kf_memset> (false));
    3154         3659 :     kfm.add ("__builtin_memset", std::make_unique<kf_memset> (false));
    3155         3659 :     kfm.add ("__memset_chk", std::make_unique<kf_memset> (true));
    3156         3659 :     kfm.add ("__builtin___memset_chk", std::make_unique<kf_memset> (true));
    3157         3659 :     kfm.add ("realloc", std::make_unique<kf_realloc> ());
    3158         3659 :     kfm.add ("__builtin_realloc", std::make_unique<kf_realloc> ());
    3159         3659 :     kfm.add ("sprintf", std::make_unique<kf_sprintf> ());
    3160         3659 :     kfm.add ("__builtin_sprintf", std::make_unique<kf_sprintf> ());
    3161         3659 :     kfm.add ("strchr", std::make_unique<kf_strchr> ());
    3162         3659 :     kfm.add ("__builtin_strchr", std::make_unique<kf_strchr> ());
    3163         3659 :     kfm.add ("strcpy", std::make_unique<kf_strcpy> (2, false));
    3164         3659 :     kfm.add ("__builtin_strcpy", std::make_unique<kf_strcpy> (2, false));
    3165         3659 :     kfm.add ("__strcpy_chk", std::make_unique<kf_strcpy> (3, true));
    3166         3659 :     kfm.add ("__builtin___strcpy_chk", std::make_unique<kf_strcpy> (3, true));
    3167         3659 :     kfm.add ("strcat", std::make_unique<kf_strcat> (2, false));
    3168         3659 :     kfm.add ("__builtin_strcat", std::make_unique<kf_strcat> (2, false));
    3169         3659 :     kfm.add ("__strcat_chk", std::make_unique<kf_strcat> (3, true));
    3170         3659 :     kfm.add ("__builtin___strcat_chk", std::make_unique<kf_strcat> (3, true));
    3171         3659 :     kfm.add ("strdup", std::make_unique<kf_strdup> ());
    3172         3659 :     kfm.add ("__builtin_strdup", std::make_unique<kf_strdup> ());
    3173         3659 :     kfm.add ("strncpy", std::make_unique<kf_strncpy> ());
    3174         3659 :     kfm.add ("__builtin_strncpy", std::make_unique<kf_strncpy> ());
    3175         3659 :     kfm.add ("strndup", std::make_unique<kf_strndup> ());
    3176         3659 :     kfm.add ("__builtin_strndup", std::make_unique<kf_strndup> ());
    3177         3659 :     kfm.add ("strlen", std::make_unique<kf_strlen> ());
    3178         3659 :     kfm.add ("__builtin_strlen", std::make_unique<kf_strlen> ());
    3179         3659 :     kfm.add ("strstr", std::make_unique<kf_strstr> ());
    3180         3659 :     kfm.add ("__builtin_strstr", std::make_unique<kf_strstr> ());
    3181         3659 :     kfm.add("strcasecmp", std::make_unique<kf_strcasecmp>());
    3182         3659 :     kfm.add("__builtin_strcasecmp", std::make_unique<kf_strcasecmp>());
    3183              :   }
    3184              : 
    3185              :   /* Known POSIX functions, and some non-standard extensions.  */
    3186         3659 :   {
    3187         3659 :     kfm.add ("fopen", std::make_unique<kf_fopen> ());
    3188         3659 :     kfm.add ("getenv", std::make_unique<kf_getenv> ());
    3189         3659 :     kfm.add ("mkdtemp", std::make_unique<kf_mktemp_simple> (
    3190         3659 :                           kf_mktemp_family::outcome::null_ptr));
    3191         3659 :     kfm.add ("mkostemp", std::make_unique<kf_mkostemp> ());
    3192         3659 :     kfm.add ("mkostemps", std::make_unique<kf_mkostemps> ());
    3193         3659 :     kfm.add ("mkstemps", std::make_unique<kf_mkstemps> ());
    3194         3659 :     kfm.add ("mkstemp", std::make_unique<kf_mktemp_simple> (
    3195         3659 :                           kf_mktemp_family::outcome::fd));
    3196              :     /* TODO: Report mktemp as deprecated per MSC24-C
    3197              :        (https://wiki.sei.cmu.edu/confluence/x/hNYxBQ).  */
    3198         3659 :     kfm.add ("mktemp", std::make_unique<kf_mktemp_simple> (
    3199         3659 :                          kf_mktemp_family::outcome::modif_tmpl));
    3200         3659 :     kfm.add ("putenv", std::make_unique<kf_putenv> ());
    3201         3659 :     kfm.add ("strtok", std::make_unique<kf_strtok> (rmm));
    3202              : 
    3203         3659 :     register_known_fd_functions (kfm);
    3204         3659 :     register_known_file_functions (kfm);
    3205              :   }
    3206              : 
    3207              :   /* glibc functions.  */
    3208         3659 :   {
    3209         3659 :     kfm.add ("__errno_location", std::make_unique<kf_errno_location> ());
    3210         3659 :     kfm.add ("error", std::make_unique<kf_error> (3));
    3211         3659 :     kfm.add ("error_at_line", std::make_unique<kf_error> (5));
    3212              :     /* Variants of "error" and "error_at_line" seen by the
    3213              :        analyzer at -O0 (PR analyzer/115724).  */
    3214         3659 :     kfm.add ("__error_alias", std::make_unique<kf_error> (3));
    3215         3659 :     kfm.add ("__error_at_line_alias", std::make_unique<kf_error> (5));
    3216              :   }
    3217              : 
    3218              :   /* Other implementations of C standard library.  */
    3219         3659 :   {
    3220              :     /* According to PR 107807 comment #2, Solaris implements "errno"
    3221              :        like this:
    3222              :          extern int *___errno(void) __attribute__((__const__));
    3223              :          #define errno (*(___errno()))
    3224              :        and macOS like this:
    3225              :          extern int * __error(void);
    3226              :          #define errno (*__error())
    3227              :        and similarly __errno for newlib.
    3228              :        Add these as synonyms for "__errno_location".  */
    3229         3659 :     kfm.add ("___errno", std::make_unique<kf_errno_location> ());
    3230         3659 :     kfm.add ("__error", std::make_unique<kf_errno_location> ());
    3231         3659 :     kfm.add ("__errno", std::make_unique<kf_errno_location> ());
    3232         3659 :     kfm.add ("__get_errno_ptr", std::make_unique<kf_errno_location> ());
    3233              :   }
    3234              : 
    3235              :   /* Language-specific support functions.  */
    3236         3659 :   register_known_functions_lang_cp (kfm);
    3237              : 
    3238              :   /* Some C++ implementations use the std:: copies of these functions
    3239              :      from <cstdlib> etc for the C spellings of these headers (e.g. <stdlib.h>),
    3240              :      so we must match against these too.  */
    3241         3659 :   {
    3242         3659 :     kfm.add_std_ns ("atoi", std::make_unique<kf_atoi_family> ());
    3243         3659 :     kfm.add_std_ns ("atol", std::make_unique<kf_atoi_family> ());
    3244         3659 :     kfm.add_std_ns ("atoll", std::make_unique<kf_atoi_family> ());
    3245              : 
    3246         3659 :     kfm.add_std_ns ("malloc", std::make_unique<kf_malloc> ());
    3247         3659 :     kfm.add_std_ns ("free", std::make_unique<kf_free> ());
    3248         3659 :     kfm.add_std_ns ("realloc", std::make_unique<kf_realloc> ());
    3249         3659 :     kfm.add_std_ns ("calloc", std::make_unique<kf_calloc> ());
    3250         3659 :     kfm.add_std_ns
    3251         3659 :       ("memcpy",
    3252         3659 :        std::make_unique<kf_memcpy_memmove> (kf_memcpy_memmove::KF_MEMCPY));
    3253         3659 :     kfm.add_std_ns
    3254         3659 :       ("memmove",
    3255         3659 :        std::make_unique<kf_memcpy_memmove> (kf_memcpy_memmove::KF_MEMMOVE));
    3256         3659 :     kfm.add_std_ns ("memset", std::make_unique<kf_memset> (false));
    3257         3659 :     kfm.add_std_ns ("strcat", std::make_unique<kf_strcat> (2, false));
    3258         3659 :     kfm.add_std_ns ("strcpy", std::make_unique<kf_strcpy> (2, false));
    3259         3659 :     kfm.add_std_ns ("strlen", std::make_unique<kf_strlen> ());
    3260         3659 :     kfm.add_std_ns ("strncpy", std::make_unique<kf_strncpy> ());
    3261         3659 :     kfm.add_std_ns ("strtok", std::make_unique<kf_strtok> (rmm));
    3262              :   }
    3263         3659 : }
    3264              : 
    3265              : } // namespace ana
    3266              : 
    3267              : #endif /* #if ENABLE_ANALYZER */
        

Generated by: LCOV version 2.6-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.