LCOV - code coverage report
Current view: top level - gcc/analyzer - kf.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 96.0 % 1400 1344
Test Date: 2026-11-14 16:39:50 Functions: 96.6 % 148 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         1529 : pure_known_function_with_default_return::
      71              : impl_call_pre (const call_details &cd) const
      72              : {
      73         1529 :   cd.set_any_lhs_with_defaults ();
      74         1529 : }
      75              : 
      76              : /* Implementations of specific functions.  */
      77              : 
      78              : /* Handler for "alloca".  */
      79              : 
      80        10719 : class kf_alloca : public builtin_known_function
      81              : {
      82              : public:
      83         1304 :   bool matches_call_types_p (const call_details &cd) const final override
      84              :   {
      85         1304 :     return cd.num_args () == 1;
      86              :   }
      87         1574 :   enum built_in_function builtin_code () const final override
      88              :   {
      89         1574 :     return BUILT_IN_ALLOCA;
      90              :   }
      91              :   void impl_call_pre (const call_details &cd) const final override;
      92              : };
      93              : 
      94              : void
      95          516 : kf_alloca::impl_call_pre (const call_details &cd) const
      96              : {
      97          516 :   const svalue *size_sval = cd.get_arg_svalue (0);
      98              : 
      99          516 :   region_model *model = cd.get_model ();
     100          516 :   region_model_manager *mgr = cd.get_manager ();
     101              : 
     102          516 :   const region *new_reg
     103          516 :     = model->create_region_for_alloca (size_sval, cd.get_ctxt ());
     104          516 :   const svalue *ptr_sval
     105          516 :     = mgr->get_ptr_svalue (cd.get_lhs_type (), new_reg);
     106          516 :   cd.maybe_set_lhs (ptr_sval);
     107          516 : }
     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         3573 : 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        21438 : 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        89325 :   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        89325 :   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         3573 : 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         3573 : 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        21438 : class kf_atomic_load_n : public internal_known_function
     346              : {
     347              : public:
     348              :   /* This is effectively:
     349              :        RET = *PTR;
     350              :        return RET;
     351              :   */
     352           41 :   void impl_call_pre (const call_details &cd) const final override
     353              :   {
     354           41 :     const svalue *ptr_ptr_sval = cd.get_arg_svalue (0);
     355           41 :     tree ptr_ptr_tree = cd.get_arg_tree (0);
     356              :     /* Ignore the memorder param.  */
     357              : 
     358           41 :     region_model *model = cd.get_model ();
     359           41 :     region_model_context *ctxt = cd.get_ctxt ();
     360              : 
     361           41 :     const region *ptr_region
     362           41 :       = model->deref_rvalue (ptr_ptr_sval, ptr_ptr_tree, ctxt);
     363           41 :     const svalue *star_ptr_sval = model->get_store_value (ptr_region, ctxt);
     364           41 :     cd.maybe_set_lhs (star_ptr_sval);
     365           41 :   }
     366              : };
     367              : 
     368              : /* Handler for:
     369              :    void __atomic_store_n (type *ptr, type val, int memorder) */
     370              : 
     371        21438 : 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        10719 : 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        10719 : class kf_calloc : public builtin_known_function
     409              : {
     410              : public:
     411         1586 :   bool matches_call_types_p (const call_details &cd) const final override
     412              :   {
     413         1586 :     return (cd.num_args () == 2
     414         1586 :             && cd.arg_is_size_p (0)
     415         3172 :             && 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          443 : kf_calloc::impl_call_pre (const call_details &cd) const
     427              : {
     428          443 :   region_model *model = cd.get_model ();
     429          443 :   region_model_manager *mgr = cd.get_manager ();
     430          443 :   const svalue *nmemb_sval = cd.get_arg_svalue (0);
     431          443 :   const svalue *size_sval = cd.get_arg_svalue (1);
     432              :   /* TODO: check for overflow here?  */
     433          443 :   const svalue *prod_sval
     434          443 :     = mgr->get_or_create_binop (size_type_node, MULT_EXPR,
     435              :                                 nmemb_sval, size_sval);
     436          443 :   const region *new_reg
     437          443 :     = model->get_or_create_region_for_heap_alloc (prod_sval, cd.get_ctxt ());
     438          443 :   const region *sized_reg
     439          443 :     = mgr->get_sized_region (new_reg, NULL_TREE, prod_sval);
     440          443 :   model->zero_fill_region (sized_reg, cd.get_ctxt ());
     441          443 :   if (cd.get_lhs_type ())
     442              :     {
     443          443 :       const svalue *ptr_sval
     444          443 :         = mgr->get_ptr_svalue (cd.get_lhs_type (), new_reg);
     445          443 :       cd.maybe_set_lhs (ptr_sval);
     446              :     }
     447          443 : }
     448              : 
     449              : /* Handler for glibc's "__errno_location".  */
     450              : 
     451        17865 : class kf_errno_location : public known_function
     452              : {
     453              : public:
     454         2650 :   bool matches_call_types_p (const call_details &cd) const final override
     455              :   {
     456         2650 :     return cd.num_args () == 0;
     457              :   }
     458              : 
     459          405 :   void impl_call_pre (const call_details &cd) const final override
     460              :   {
     461          405 :     if (cd.get_lhs_region ())
     462              :       {
     463          405 :         region_model_manager *mgr = cd.get_manager ();
     464          405 :         const region *errno_reg = mgr->get_errno_region ();
     465          405 :         const svalue *errno_ptr = mgr->get_ptr_svalue (cd.get_lhs_type (),
     466              :                                                        errno_reg);
     467          405 :         cd.maybe_set_lhs (errno_ptr);
     468              :       }
     469          405 :   }
     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        14292 :   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         3573 : 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        10719 : class kf_free : public builtin_known_function
     554              : {
     555              : public:
     556        69217 :   bool matches_call_types_p (const call_details &cd) const final override
     557              :   {
     558        69217 :     return (cd.num_args () == 1 && cd.arg_is_pointer_p (0));
     559              :   }
     560        66252 :   enum built_in_function builtin_code () const final override
     561              :   {
     562        66252 :     return BUILT_IN_FREE;
     563              :   }
     564              :   void impl_call_post (const call_details &cd) const final override;
     565              : };
     566              : 
     567              : void
     568        12999 : kf_free::impl_call_post (const call_details &cd) const
     569              : {
     570        12999 :   const svalue *ptr_sval = cd.get_arg_svalue (0);
     571        12999 :   if (auto ptr = ptr_sval->maybe_get_symbolic_pointer ())
     572        10337 :     if (ptr->zero_offset_p ())
     573              :       {
     574              :         /* If the ptr points to an underlying heap region, delete it,
     575              :            poisoning pointers.  */
     576        10329 :         const region *freed_reg = &ptr->get_base_region ();
     577        10329 :         region_model *model = cd.get_model ();
     578        10329 :         model->unbind_region_and_descendents (freed_reg, poison_kind::freed);
     579        10329 :         model->unset_dynamic_extents (freed_reg);
     580              :       }
     581        12999 : }
     582              : 
     583              : /* Handle the on_call_pre part of "malloc".  */
     584              : 
     585        10719 : class kf_malloc : public builtin_known_function
     586              : {
     587              : public:
     588        45191 :   bool matches_call_types_p (const call_details &cd) const final override
     589              :   {
     590        45191 :     return (cd.num_args () == 1
     591        45191 :             && cd.arg_is_size_p (0));
     592              :   }
     593        36996 :   enum built_in_function builtin_code () const final override
     594              :   {
     595        36996 :     return BUILT_IN_MALLOC;
     596              :   }
     597              :   void impl_call_pre (const call_details &cd) const final override;
     598              : };
     599              : 
     600              : void
     601        10255 : kf_malloc::impl_call_pre (const call_details &cd) const
     602              : {
     603        10255 :   region_model *model = cd.get_model ();
     604        10255 :   region_model_manager *mgr = cd.get_manager ();
     605        10255 :   const svalue *size_sval = cd.get_arg_svalue (0);
     606        10255 :   const region *new_reg
     607        10255 :     = model->get_or_create_region_for_heap_alloc (size_sval, cd.get_ctxt ());
     608        10255 :   if (cd.get_lhs_type ())
     609              :     {
     610        10255 :       const svalue *ptr_sval
     611        10255 :         = mgr->get_ptr_svalue (cd.get_lhs_type (), new_reg);
     612        10255 :       cd.maybe_set_lhs (ptr_sval);
     613              :     }
     614        10255 : }
     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        35730 :   kf_memcpy_memmove (enum kf_memcpy_memmove_variant variant)
     630        35730 :     : m_variant (variant) {};
     631         3684 :   bool matches_call_types_p (const call_details &cd) const final override
     632              :   {
     633         3684 :     return (cd.num_args () == 3
     634         3684 :             && cd.arg_is_pointer_p (0)
     635         3684 :             && cd.arg_is_pointer_p (1)
     636         7368 :             && cd.arg_is_size_p (2));
     637              :   }
     638         3760 :   enum built_in_function builtin_code () const final override
     639              :   {
     640         3760 :     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          670 : kf_memcpy_memmove::impl_call_pre (const call_details &cd) const
     661              : {
     662          670 :   auto *dest_ptr_sval = cd.get_arg_svalue (0);
     663          670 :   symbolic::pointer dest_sym_ptr = cd.get_arg_as_sym_ptr (0);
     664          670 :   symbolic::pointer src_sym_ptr = cd.get_arg_as_sym_ptr (1);
     665          670 :   auto &num_bytes = cd.get_arg_as_num_bytes (2);
     666          670 :   const svalue *num_bytes_sval = cd.get_arg_svalue (2);
     667              : 
     668          670 :   region_model *model = cd.get_model ();
     669              : 
     670          670 :   cd.maybe_set_lhs (dest_ptr_sval);
     671              :   /* Check for overlap.  */
     672          670 :   switch (m_variant)
     673              :     {
     674          649 :     case KF_MEMCPY:
     675          649 :     case KF_MEMCPY_CHK:
     676          649 :       cd.complain_about_overlap (0, 1, num_bytes_sval);
     677          649 :       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          670 :   model->copy_bytes (dest_sym_ptr,
     688              :                      src_sym_ptr, cd.get_arg_tree (1),
     689              :                      num_bytes,
     690              :                      cd.get_ctxt ());
     691          670 : }
     692              : 
     693              : /* Handler for "memset" and "__builtin_memset".  */
     694              : 
     695              : class kf_memset : public builtin_known_function
     696              : {
     697              : public:
     698        17865 :   kf_memset (bool chk_variant) : m_chk_variant (chk_variant) {}
     699         3624 :   bool matches_call_types_p (const call_details &cd) const final override
     700              :   {
     701         3624 :     return (cd.num_args () == 3 && cd.arg_is_pointer_p (0));
     702              :   }
     703         3472 :   enum built_in_function builtin_code () const final override
     704              :   {
     705         3472 :     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          699 : kf_memset::impl_call_pre (const call_details &cd) const
     714              : {
     715          699 :   const svalue *dest_sval = cd.get_arg_svalue (0);
     716          699 :   const svalue *fill_value_sval = cd.get_arg_svalue (1);
     717          699 :   const svalue *num_bytes_sval = cd.get_arg_svalue (2);
     718              : 
     719          699 :   region_model *model = cd.get_model ();
     720          699 :   region_model_manager *mgr = cd.get_manager ();
     721              : 
     722          699 :   const region *dest_reg
     723          699 :     = model->deref_rvalue (dest_sval, cd.get_arg_tree (0), cd.get_ctxt ());
     724              : 
     725          699 :   const svalue *fill_value_u8
     726          699 :     = mgr->get_or_create_cast (unsigned_char_type_node, fill_value_sval);
     727              : 
     728          699 :   auto &bv_mgr = mgr->get_node_manager ().get_bit_vector_manager ();
     729          699 :   auto &fill_byte = bv_mgr.get_bit_vector_from_svalue (*fill_value_u8);
     730              : 
     731          699 :   auto &unitless_mgr = mgr->get_node_manager ().get_scalars ().get_unitless ();
     732          699 :   const symbolic::scalar<integer> &repeat_count
     733          699 :     = unitless_mgr.get_scalar_from_svalue (*num_bytes_sval);
     734              : 
     735          699 :   if (!repeat_count.is_zero_p ())
     736              :     {
     737          687 :       auto &fill_bv = bv_mgr.get_repeated_bits (fill_byte, repeat_count);
     738              : 
     739          687 :       auto mem_offset = dest_reg->get_memory_offset (*mgr);
     740          687 :       model->write_to_memory_offset (mem_offset,
     741              :                                      fill_bv,
     742              :                                      cd.get_ctxt ());
     743              :     }
     744              : 
     745          699 :   cd.maybe_set_lhs (dest_sval);
     746          699 : }
     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        21438 :   kf_mktemp_family (outcome oc, int suffixlen_arg_idx)
     937        21438 :     : 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              :   bool
    1051          103 :   update_model (region_model *model, const exploded_edge *,
    1052              :                 region_model_context *ctxt) const final override
    1053              :   {
    1054          103 :     const call_details cd (get_call_details (model, ctxt));
    1055              : 
    1056          103 :     switch (m_outcome)
    1057              :       {
    1058           73 :       case outcome::fd:
    1059           73 :         model->update_for_int_cst_return (cd, -1, true);
    1060           73 :         break;
    1061           15 :       case outcome::null_ptr:
    1062           15 :         model->update_for_null_return (cd, true);
    1063           15 :         break;
    1064           15 :       case outcome::modif_tmpl:
    1065           15 :         {
    1066           15 :           const svalue *first_arg_svalue = cd.get_arg_svalue (0);
    1067              : 
    1068              :           /* Return the same pointer that was passed in.  */
    1069           15 :           cd.maybe_set_lhs (first_arg_svalue);
    1070              : 
    1071           15 :           region_model_manager *mgr = cd.get_manager ();
    1072           15 :           const region *template_reg = model->deref_rvalue (
    1073              :             first_arg_svalue, cd.get_arg_tree (0), ctxt);
    1074              : 
    1075              :           /* mktemp may have modified the X positions before failing;
    1076              :              invalidate the old buffer contents.  */
    1077           15 :           model->mark_region_as_unknown (template_reg, nullptr);
    1078              : 
    1079              :           /* Then: template[0] = '\0'.  */
    1080           15 :           const svalue *nul = mgr->get_or_create_int_cst (char_type_node, 0);
    1081           15 :           model->set_value (template_reg, nul, ctxt);
    1082           15 :           break;
    1083              :         }
    1084            0 :       default:
    1085            0 :         gcc_unreachable ();
    1086              :       }
    1087              : 
    1088          103 :     model->set_errno (cd);
    1089          103 :     return true;
    1090              :   }
    1091              : 
    1092              : private:
    1093              :   outcome m_outcome;
    1094              : };
    1095              : 
    1096              : /* Model the success outcome of a mktemp-family function call.
    1097              : 
    1098              :    For fd-returning functions, conjures a non-negative fd and marks it valid.
    1099              :    For pointer-returning functions, returns the template pointer and marks the
    1100              :    template region as modified.  */
    1101              : 
    1102              : class kf_mktemp_family::success : public success_call_info
    1103              : {
    1104              : public:
    1105              :   success (const call_details &cd, outcome oc)
    1106           69 :     : success_call_info (cd), m_outcome (oc)
    1107              :   {
    1108              :   }
    1109              : 
    1110              :   bool
    1111          163 :   update_model (region_model *model, const exploded_edge *,
    1112              :                 region_model_context *ctxt) const final override
    1113              :   {
    1114          163 :     const call_details cd (get_call_details (model, ctxt));
    1115              : 
    1116          163 :     switch (m_outcome)
    1117              :       {
    1118          144 :       case outcome::fd:
    1119          144 :         {
    1120          144 :           region_model_manager *mgr = cd.get_manager ();
    1121          144 :           const region *lhs_reg = cd.get_lhs_region ();
    1122              :           /* conjured_svalue needs a non-null id_reg.  When there is
    1123              :              no LHS, use an unknown symbolic region; we still conjure
    1124              :              and mark the fd so the leak checker can see it.  */
    1125          144 :           const region *id_reg
    1126          144 :             = lhs_reg ? lhs_reg
    1127          116 :                       : mgr->get_unknown_symbolic_region (integer_type_node);
    1128              : 
    1129          144 :           tree lhs_type = cd.get_lhs_type ();
    1130          144 :           if (!lhs_type)
    1131          116 :             lhs_type = integer_type_node;
    1132              : 
    1133          144 :           conjured_purge p (model, ctxt);
    1134          144 :           const svalue *fd_sval = mgr->get_or_create_conjured_svalue (
    1135          144 :             lhs_type, &cd.get_call_stmt (), id_reg, p);
    1136          144 :           const svalue *zero = mgr->get_or_create_int_cst (lhs_type, 0);
    1137              : 
    1138          144 :           if (!model->add_constraint (fd_sval, GE_EXPR, zero, ctxt))
    1139            0 :             return false;
    1140              : 
    1141          144 :           if (lhs_reg)
    1142           28 :             model->set_value (lhs_reg, fd_sval, ctxt);
    1143              : 
    1144              :           /* TODO: transition to an unchecked state so that
    1145              :              use-without-check can be detected.  */
    1146          144 :           model->mark_as_valid_fd (fd_sval, ctxt);
    1147          144 :           break;
    1148              :         }
    1149           12 :       case outcome::null_ptr:
    1150           12 :         {
    1151           12 :           region_model_manager *mgr = cd.get_manager ();
    1152           12 :           const svalue *first_arg_svalue = cd.get_arg_svalue (0);
    1153           12 :           cd.maybe_set_lhs (first_arg_svalue);
    1154              : 
    1155              :           /* On success, mkdtemp returns non-NULL.  */
    1156           12 :           const svalue *null_ptr
    1157           12 :             = mgr->get_or_create_int_cst (first_arg_svalue->get_type (), 0);
    1158           12 :           if (!model->add_constraint (first_arg_svalue, NE_EXPR, null_ptr,
    1159              :                                       ctxt))
    1160              :             return false;
    1161              : 
    1162           11 :           const region *template_reg = model->deref_rvalue (
    1163              :             first_arg_svalue, cd.get_arg_tree (0), ctxt);
    1164           11 :           model->mark_region_as_unknown (template_reg, nullptr);
    1165           11 :           break;
    1166              :         }
    1167            7 :       case outcome::modif_tmpl:
    1168            7 :         {
    1169            7 :           const svalue *first_arg_svalue = cd.get_arg_svalue (0);
    1170            7 :           cd.maybe_set_lhs (first_arg_svalue);
    1171            7 :           const region *template_reg = model->deref_rvalue (
    1172              :             first_arg_svalue, cd.get_arg_tree (0), ctxt);
    1173            7 :           model->mark_region_as_unknown (template_reg, nullptr);
    1174            7 :           break;
    1175              :         }
    1176            0 :       default:
    1177            0 :         gcc_unreachable ();
    1178              :       }
    1179              : 
    1180              :     return true;
    1181              :   }
    1182              : 
    1183              : private:
    1184              :   outcome m_outcome;
    1185              : };
    1186              : 
    1187              : /* Bifurcate into success and failure paths.  The template placeholder is
    1188              :    validated when possible.  If definitely invalid, only the failure path is
    1189              :    explored.  */
    1190              : 
    1191              : void
    1192          103 : kf_mktemp_family::impl_call_post (const call_details &cd) const
    1193              : {
    1194          103 :   if (!cd.get_ctxt ())
    1195            0 :     return;
    1196              : 
    1197          103 :   tristate valid = tristate::TS_UNKNOWN;
    1198          103 :   HOST_WIDE_INT trailing_len = get_trailing_len (cd, valid);
    1199              : 
    1200              :   /* Determine whether the template placeholder is valid.  */
    1201          103 :   const svalue *strlen_sval = nullptr;
    1202          103 :   const svalue *ptr_sval = nullptr;
    1203          103 :   if (trailing_len >= 0 && !valid.is_false ())
    1204              :     {
    1205           93 :       ptr_sval = cd.get_arg_svalue (0);
    1206           93 :       strlen_sval = cd.get_model ()->check_for_null_terminated_string_arg (
    1207              :         cd, 0, false, nullptr);
    1208              :     }
    1209              : 
    1210           93 :   if (strlen_sval)
    1211              :     {
    1212           93 :       valid = check_placeholder (cd, trailing_len, ptr_sval, strlen_sval);
    1213           93 :       if (valid.is_false ())
    1214           68 :         cd.get_ctxt ()->warn (
    1215           34 :           std::make_unique<mktemp_missing_placeholder> (cd, trailing_len));
    1216              :     }
    1217              : 
    1218              :   /* Failure is always possible (bad template or runtime failure).  */
    1219          103 :   cd.get_ctxt ()->bifurcate (std::make_unique<failure> (cd, m_outcome));
    1220              :   /* Success is only possible if the template is not definitely invalid.  */
    1221          103 :   if (!valid.is_false ())
    1222           69 :     cd.get_ctxt ()->bifurcate (std::make_unique<success> (cd, m_outcome));
    1223          103 :   cd.get_ctxt ()->terminate_path ();
    1224              : }
    1225              : 
    1226              : tristate
    1227           93 : kf_mktemp_family::check_placeholder (const call_details &cd,
    1228              :                                      size_t trailing_len,
    1229              :                                      const svalue *ptr_sval,
    1230              :                                      const svalue *strlen_sval)
    1231              : {
    1232           93 :   region_model *model = cd.get_model ();
    1233              : 
    1234           93 :   const constant_svalue *len_cst = strlen_sval->dyn_cast_constant_svalue ();
    1235           93 :   if (!len_cst)
    1236           26 :     return tristate::TS_UNKNOWN;
    1237              : 
    1238           67 :   byte_offset_t len = TREE_INT_CST_LOW (len_cst->get_constant ());
    1239           67 :   if (len < PLACEHOLDER_LEN + trailing_len)
    1240           16 :     return tristate::TS_FALSE;
    1241              : 
    1242           51 :   tree arg_tree = cd.get_arg_tree (0);
    1243           51 :   const region *reg = model->deref_rvalue (ptr_sval, arg_tree, cd.get_ctxt ());
    1244              : 
    1245              :   /* Find the byte offset of the pointed-to region. */
    1246           51 :   auto sym_ptr = reg->get_pointer (*model->get_manager ());
    1247              : 
    1248           51 :   auto &byte_offset = sym_ptr.get_byte_offset ();
    1249           51 :   concrete_byte_offset ptr_byte_offset;
    1250           51 :   if (!concrete_from_symbolic (byte_offset, &ptr_byte_offset))
    1251            0 :     return tristate::TS_UNKNOWN;
    1252              : 
    1253           51 :   const region *base_reg = &sym_ptr.get_base_region ();
    1254           51 :   const svalue *base_sval = model->get_store_value (base_reg, cd.get_ctxt ());
    1255              : 
    1256           51 :   const constant_svalue *cst_sval = base_sval->dyn_cast_constant_svalue ();
    1257           51 :   if (!cst_sval)
    1258            0 :     return tristate::TS_UNKNOWN;
    1259              : 
    1260           51 :   tree cst = cst_sval->get_constant ();
    1261           51 :   if (TREE_CODE (cst) != STRING_CST)
    1262            0 :     return tristate::TS_UNKNOWN;
    1263              : 
    1264           51 :   HOST_WIDE_INT str_len = len.to_shwi ();
    1265           51 :   HOST_WIDE_INT start = ptr_byte_offset.get_integer (). as_long ();
    1266              : 
    1267              :   /* Ensure we can read up to and including the NUL terminator at position
    1268              :      [start + str_len - trailing_len] within the STRING_CST.  */
    1269           51 :   HOST_WIDE_INT range = start + str_len - trailing_len + 1;
    1270           51 :   if (range > TREE_STRING_LENGTH (cst))
    1271            0 :     return tristate::TS_UNKNOWN;
    1272              : 
    1273           51 :   if (memcmp (TREE_STRING_POINTER (cst) + start + str_len - trailing_len
    1274           51 :                 - PLACEHOLDER_LEN,
    1275              :               "XXXXXX", PLACEHOLDER_LEN)
    1276              :       != 0)
    1277           18 :     return tristate::TS_FALSE;
    1278              : 
    1279           33 :   return tristate::TS_TRUE;
    1280           51 : }
    1281              : 
    1282              : /* Handler for calls to "mkdtemp", "mkstemp", and "mktemp", which all
    1283              :    take a single char * template argument.
    1284              : 
    1285              :    The template must not be a string constant, and its last six
    1286              :    characters must be "XXXXXX".  */
    1287              : 
    1288              : class kf_mktemp_simple : public kf_mktemp_family
    1289              : {
    1290              : public:
    1291        10719 :   kf_mktemp_simple (outcome oc) : kf_mktemp_family (oc, -1) {}
    1292              : 
    1293              :   bool
    1294          376 :   matches_call_types_p (const call_details &cd) const final override
    1295              :   {
    1296          376 :     return (cd.num_args () == 1 && cd.arg_is_pointer_p (0));
    1297              :   }
    1298              : 
    1299              :   void
    1300           55 :   impl_call_pre (const call_details &cd) const final override
    1301              :   {
    1302           55 :     if (cd.get_ctxt ())
    1303           55 :       check_for_string_literal_arg (cd);
    1304           55 :   }
    1305              : };
    1306              : 
    1307              : /* Handler for calls to "mkostemp":
    1308              : 
    1309              :      int mkostemp(char *template, int flags);
    1310              : 
    1311              :    The template must not be a string constant, and its last six
    1312              :    characters must be "XXXXXX".  Warns when flags contains O_RDWR,
    1313              :    O_CREAT, or O_EXCL, which are already included internally.  */
    1314              : 
    1315              : class kf_mkostemp : public kf_mktemp_family
    1316              : {
    1317              : public:
    1318         3573 :   kf_mkostemp () : kf_mktemp_family (outcome::fd, -1) {}
    1319              : 
    1320              :   bool
    1321          165 :   matches_call_types_p (const call_details &cd) const final override
    1322              :   {
    1323          330 :     return (cd.num_args () == 2 && cd.arg_is_pointer_p (0)
    1324          330 :             && cd.arg_is_integral_p (1));
    1325              :   }
    1326              : 
    1327              :   void
    1328           24 :   impl_call_pre (const call_details &cd) const final override
    1329              :   {
    1330           24 :     if (cd.get_ctxt ())
    1331              :       {
    1332           24 :         check_for_string_literal_arg (cd);
    1333           24 :         check_flags (cd, 1);
    1334              :       }
    1335           24 :   }
    1336              : };
    1337              : 
    1338              : /* Handler for calls to "mkostemps":
    1339              : 
    1340              :      int mkostemps(char *template, int suffixlen, int flags);
    1341              : 
    1342              :    The template must not be a string constant, and must contain
    1343              :    "XXXXXX" before a suffixlen-character suffix.  Warns when flags
    1344              :    contains O_RDWR, O_CREAT, or O_EXCL, which are already included
    1345              :    internally.  */
    1346              : 
    1347              : class kf_mkostemps : public kf_mktemp_family
    1348              : {
    1349              : public:
    1350         3573 :   kf_mkostemps () : kf_mktemp_family (outcome::fd, 1) {}
    1351              : 
    1352              :   bool
    1353          171 :   matches_call_types_p (const call_details &cd) const final override
    1354              :   {
    1355          342 :     return (cd.num_args () == 3 && cd.arg_is_pointer_p (0)
    1356          342 :             && cd.arg_is_integral_p (1) && cd.arg_is_integral_p (2));
    1357              :   }
    1358              : 
    1359              :   void
    1360           25 :   impl_call_pre (const call_details &cd) const final override
    1361              :   {
    1362           25 :     if (cd.get_ctxt ())
    1363              :       {
    1364           25 :         check_for_string_literal_arg (cd);
    1365           25 :         check_flags (cd, 2);
    1366              :       }
    1367           25 :   }
    1368              : };
    1369              : 
    1370              : /* Handler for calls to "mkstemps":
    1371              : 
    1372              :      int mkstemps(char *template, int suffixlen);
    1373              : 
    1374              :    The template must not be a string constant, and must contain
    1375              :    "XXXXXX" before a suffixlen-character suffix.  */
    1376              : 
    1377              : class kf_mkstemps : public kf_mktemp_family
    1378              : {
    1379              : public:
    1380         3573 :   kf_mkstemps () : kf_mktemp_family (outcome::fd, 1) {}
    1381              : 
    1382              :   bool
    1383          129 :   matches_call_types_p (const call_details &cd) const final override
    1384              :   {
    1385          258 :     return (cd.num_args () == 2 && cd.arg_is_pointer_p (0)
    1386          258 :             && cd.arg_is_integral_p (1));
    1387              :   }
    1388              : 
    1389              :   void
    1390           19 :   impl_call_pre (const call_details &cd) const final override
    1391              :   {
    1392           19 :     if (cd.get_ctxt ())
    1393           19 :       check_for_string_literal_arg (cd);
    1394           19 :   }
    1395              : };
    1396              : 
    1397              : /* A subclass of pending_diagnostic for complaining about 'putenv'
    1398              :    called on an auto var.  */
    1399              : 
    1400              : class putenv_of_auto_var
    1401              : : public pending_diagnostic_subclass<putenv_of_auto_var>
    1402              : {
    1403              : public:
    1404            7 :   putenv_of_auto_var (tree fndecl, const region *reg)
    1405            7 :   : m_fndecl (fndecl), m_reg (reg),
    1406            7 :     m_var_decl (reg->get_base_region ()->maybe_get_decl ())
    1407              :   {
    1408            7 :   }
    1409              : 
    1410          106 :   const char *get_kind () const final override
    1411              :   {
    1412          106 :     return "putenv_of_auto_var";
    1413              :   }
    1414              : 
    1415            7 :   bool operator== (const putenv_of_auto_var &other) const
    1416              :   {
    1417            7 :     return (m_fndecl == other.m_fndecl
    1418            7 :             && m_reg == other.m_reg
    1419           14 :             && same_tree_p (m_var_decl, other.m_var_decl));
    1420              :   }
    1421              : 
    1422           14 :   int get_controlling_option () const final override
    1423              :   {
    1424           14 :     return OPT_Wanalyzer_putenv_of_auto_var;
    1425              :   }
    1426              : 
    1427            7 :   bool emit (diagnostic_emission_context &ctxt) final override
    1428              :   {
    1429            7 :     auto_diagnostic_group d;
    1430              : 
    1431              :     /* SEI CERT C Coding Standard: "POS34-C. Do not call putenv() with a
    1432              :        pointer to an automatic variable as the argument".  */
    1433            7 :     diagnostics::metadata::precanned_rule
    1434            7 :       rule ("POS34-C", "https://wiki.sei.cmu.edu/confluence/x/6NYxBQ");
    1435            7 :     ctxt.add_rule (rule);
    1436              : 
    1437            7 :     bool warned;
    1438            7 :     if (m_var_decl)
    1439            6 :       warned = ctxt.warn ("%qE on a pointer to automatic variable %qE",
    1440              :                           m_fndecl, m_var_decl);
    1441              :     else
    1442            1 :       warned = ctxt.warn ("%qE on a pointer to an on-stack buffer",
    1443              :                           m_fndecl);
    1444            7 :     if (warned)
    1445              :       {
    1446            7 :         if (m_var_decl)
    1447            6 :           inform (DECL_SOURCE_LOCATION (m_var_decl),
    1448              :                   "%qE declared on stack here", m_var_decl);
    1449            7 :         inform (ctxt.get_location (), "perhaps use %qs rather than %qE",
    1450              :                 "setenv", m_fndecl);
    1451              :       }
    1452              : 
    1453           14 :     return warned;
    1454            7 :   }
    1455              : 
    1456              :   bool
    1457           14 :   describe_final_event (pretty_printer &pp,
    1458              :                         const evdesc::final_event &) final override
    1459              :   {
    1460           14 :     if (m_var_decl)
    1461           12 :       pp_printf  (&pp,
    1462              :                   "%qE on a pointer to automatic variable %qE",
    1463              :                   m_fndecl, m_var_decl);
    1464              :     else
    1465            2 :       pp_printf  (&pp,
    1466              :                   "%qE on a pointer to an on-stack buffer",
    1467              :                   m_fndecl);
    1468           14 :     return true;
    1469              :   }
    1470              : 
    1471           14 :   void mark_interesting_stuff (interesting_t *interest) final override
    1472              :   {
    1473           14 :     if (!m_var_decl)
    1474            2 :       interest->add_region_creation (m_reg->get_base_region ());
    1475           14 :   }
    1476              : 
    1477              : private:
    1478              :   tree m_fndecl; // non-NULL
    1479              :   const region *m_reg; // non-NULL
    1480              :   tree m_var_decl; // could be NULL
    1481              : };
    1482              : 
    1483        21438 : class kf_atoi_family: public known_function
    1484              : {
    1485              : public:
    1486           41 :   bool matches_call_types_p (const call_details &cd) const final override
    1487              :   {
    1488           41 :     return (cd.num_args () == 1 && cd.arg_is_pointer_p (0));
    1489              :   }
    1490              : 
    1491            6 :   void impl_call_pre (const call_details &cd) const final override
    1492              :   {
    1493              :     /* atoi expects a valid, null-terminated string. */
    1494            6 :     cd.check_for_null_terminated_string_arg (0, false, nullptr);
    1495              :     
    1496              :     /* atoi returns an integer, but we don't know what it is statically. 
    1497              :        Tell the analyzer to assume it returns a generic, unknown value. */
    1498            6 :     cd.set_any_lhs_with_defaults ();
    1499            6 :   }
    1500              : };
    1501              : 
    1502              : /* Handler for calls to "getenv".
    1503              :      char *getenv (const char *name);
    1504              : 
    1505              :    Returns either NULL (if the environment variable is not found),
    1506              :    or a pointer to the value string.  */
    1507              : 
    1508         3573 : class kf_getenv : public known_function
    1509              : {
    1510              : public:
    1511           63 :   bool matches_call_types_p (const call_details &cd) const final override
    1512              :   {
    1513           63 :     return (cd.num_args () == 1 && cd.arg_is_pointer_p (0));
    1514              :   }
    1515              : 
    1516            9 :   void impl_call_pre (const call_details &cd) const final override
    1517              :   {
    1518            9 :     cd.check_for_null_terminated_string_arg (0);
    1519            9 :   }
    1520              : 
    1521              :   void impl_call_post (const call_details &cd) const final override;
    1522              : };
    1523              : 
    1524              : void
    1525            9 : kf_getenv::impl_call_post (const call_details &cd) const
    1526              : {
    1527            0 :   class getenv_call_info : public call_info
    1528              :   {
    1529              :   public:
    1530           18 :     getenv_call_info (const call_details &cd, bool found)
    1531           18 :     : call_info (cd), m_found (found)
    1532              :     {
    1533              :     }
    1534              : 
    1535          158 :     void print_desc (pretty_printer &pp) const final override
    1536              :     {
    1537          158 :       if (m_found)
    1538          154 :         pp_printf (&pp,
    1539              :                    "when %qE returns non-NULL",
    1540              :                    get_fndecl ());
    1541              :       else
    1542            4 :         pp_printf (&pp,
    1543              :                    "when %qE returns NULL",
    1544              :                    get_fndecl ());
    1545          158 :     }
    1546              : 
    1547          453 :     bool update_model (region_model *model,
    1548              :                        const exploded_edge *,
    1549              :                        region_model_context *ctxt) const final override
    1550              :     {
    1551          453 :       const call_details cd (get_call_details (model, ctxt));
    1552          453 :       if (tree lhs_type = cd.get_lhs_type ())
    1553              :         {
    1554          451 :           region_model_manager *mgr = model->get_manager ();
    1555          451 :           const svalue *result;
    1556          451 :           if (m_found)
    1557              :             {
    1558              :               /* Return a conjured non-NULL pointer.  */
    1559          262 :               result
    1560          262 :                 = mgr->get_or_create_conjured_svalue (lhs_type,
    1561          262 :                                                        &cd.get_call_stmt (),
    1562              :                                                        cd.get_lhs_region (),
    1563          262 :                                                        conjured_purge (model,
    1564          262 :                                                                        ctxt));
    1565          262 :               const svalue *null_ptr
    1566          262 :                 = mgr->get_or_create_int_cst (lhs_type, 0);
    1567          262 :               model->add_constraint (result, NE_EXPR, null_ptr, ctxt);
    1568              :             }
    1569              :           else
    1570          189 :             result = mgr->get_or_create_int_cst (lhs_type, 0);
    1571          451 :           cd.maybe_set_lhs (result);
    1572              :         }
    1573          453 :       return true;
    1574              :     }
    1575              :   private:
    1576              :     bool m_found;
    1577              :   };
    1578              : 
    1579              :   /* Body of kf_getenv::impl_call_post.  */
    1580            9 :   if (cd.get_ctxt ())
    1581              :     {
    1582            9 :       cd.get_ctxt ()->bifurcate
    1583            9 :         (std::make_unique<getenv_call_info> (cd, false));
    1584            9 :       cd.get_ctxt ()->bifurcate
    1585            9 :         (std::make_unique<getenv_call_info> (cd, true));
    1586            9 :       cd.get_ctxt ()->terminate_path ();
    1587              :     }
    1588            9 : }
    1589              : 
    1590              : /* Handler for calls to "putenv".
    1591              : 
    1592              :    In theory we could try to model the state of the environment variables
    1593              :    for the process; for now we merely complain about putenv of regions
    1594              :    on the stack.  */
    1595              : 
    1596         3573 : class kf_putenv : public known_function
    1597              : {
    1598              : public:
    1599          118 :   bool matches_call_types_p (const call_details &cd) const final override
    1600              :   {
    1601          118 :     return (cd.num_args () == 1 && cd.arg_is_pointer_p (0));
    1602              :   }
    1603              : 
    1604           17 :   void impl_call_pre (const call_details &cd) const final override
    1605              :   {
    1606           17 :     tree fndecl = cd.get_fndecl_for_call ();
    1607           17 :     gcc_assert (fndecl);
    1608           17 :     region_model_context *ctxt = cd.get_ctxt ();
    1609           17 :     region_model *model = cd.get_model ();
    1610           17 :     model->check_for_null_terminated_string_arg (cd, 0);
    1611           17 :     const svalue *ptr_sval = cd.get_arg_svalue (0);
    1612           17 :     const region *reg
    1613           17 :       = model->deref_rvalue (ptr_sval, cd.get_arg_tree (0), ctxt);
    1614           17 :     model->get_store ()->mark_as_escaped (reg->get_base_region (),
    1615              :                                           model->get_baseline_state ());
    1616           17 :     enum memory_space mem_space = reg->get_memory_space ();
    1617           17 :     switch (mem_space)
    1618              :       {
    1619            0 :       default:
    1620            0 :         gcc_unreachable ();
    1621              :       case MEMSPACE_UNKNOWN:
    1622              :       case MEMSPACE_CODE:
    1623              :       case MEMSPACE_GLOBALS:
    1624              :       case MEMSPACE_HEAP:
    1625              :       case MEMSPACE_READONLY_DATA:
    1626              :         break;
    1627            7 :       case MEMSPACE_STACK:
    1628            7 :         if (ctxt)
    1629            7 :           ctxt->warn (std::make_unique<putenv_of_auto_var> (fndecl, reg));
    1630              :         break;
    1631              :       }
    1632           17 :     cd.set_any_lhs_with_defaults ();
    1633           17 :   }
    1634              : };
    1635              : 
    1636              : /* Handler for "realloc":
    1637              : 
    1638              :      void *realloc(void *ptr, size_t size);
    1639              : 
    1640              :    realloc(3) is awkward, since it has various different outcomes
    1641              :    that are best modelled as separate exploded nodes/edges.
    1642              : 
    1643              :    We first check for sm-state, in
    1644              :    malloc_state_machine::on_realloc_call, so that we
    1645              :    can complain about issues such as realloc of a non-heap
    1646              :    pointer, and terminate the path for such cases (and issue
    1647              :    the complaints at the call's exploded node).
    1648              : 
    1649              :    Assuming that these checks pass, we split the path here into
    1650              :    three special cases (and terminate the "standard" path):
    1651              :    (A) failure, returning NULL
    1652              :    (B) success, growing the buffer in-place without moving it
    1653              :    (C) success, allocating a new buffer, copying the content
    1654              :    of the old buffer to it, and freeing the old buffer.
    1655              : 
    1656              :    Each of these has a custom_edge_info subclass, which updates
    1657              :    the region_model and sm-state of the destination state.  */
    1658              : 
    1659        10719 : class kf_realloc : public builtin_known_function
    1660              : {
    1661              : public:
    1662         1667 :   bool matches_call_types_p (const call_details &cd) const final override
    1663              :   {
    1664         1667 :     return (cd.num_args () == 2
    1665         1667 :             && cd.arg_is_pointer_p (0)
    1666         3334 :             && cd.arg_is_size_p (1));
    1667              :   }
    1668              : 
    1669         1692 :   enum built_in_function builtin_code () const final override
    1670              :   {
    1671         1692 :     return BUILT_IN_REALLOC;
    1672              :   }
    1673              : 
    1674              :   void impl_call_post (const call_details &cd) const final override;
    1675              : };
    1676              : 
    1677              : void
    1678          257 : kf_realloc::impl_call_post (const call_details &cd) const
    1679              : {
    1680              :   /* Three custom subclasses of custom_edge_info, for handling the various
    1681              :      outcomes of "realloc".  */
    1682              : 
    1683              :   /* Concrete custom_edge_info: a realloc call that fails, returning NULL.  */
    1684            0 :   class failure : public failed_call_info
    1685              :   {
    1686              :   public:
    1687          257 :     failure (const call_details &cd)
    1688          257 :     : failed_call_info (cd)
    1689              :     {
    1690              :     }
    1691              : 
    1692          311 :     bool update_model (region_model *model,
    1693              :                        const exploded_edge *,
    1694              :                        region_model_context *ctxt) const final override
    1695              :     {
    1696              :       /* Return NULL; everything else is unchanged.  */
    1697          311 :       const call_details cd (get_call_details (model, ctxt));
    1698          311 :       region_model_manager *mgr = cd.get_manager ();
    1699          311 :       if (cd.get_lhs_type ())
    1700              :         {
    1701          302 :           const svalue *zero
    1702          302 :             = mgr->get_or_create_int_cst (cd.get_lhs_type (), 0);
    1703          302 :           model->set_value (cd.get_lhs_region (),
    1704              :                             zero,
    1705              :                             cd.get_ctxt ());
    1706              :         }
    1707          311 :       return true;
    1708              :     }
    1709              :   };
    1710              : 
    1711              :   /* Concrete custom_edge_info: a realloc call that succeeds, growing
    1712              :      the existing buffer without moving it.  */
    1713            0 :   class success_no_move : public call_info
    1714              :   {
    1715              :   public:
    1716          257 :     success_no_move (const call_details &cd)
    1717          257 :     : call_info (cd)
    1718              :     {
    1719              :     }
    1720              : 
    1721            8 :     void print_desc (pretty_printer &pp) const final override
    1722              :     {
    1723            8 :       pp_printf (&pp,
    1724              :                  "when %qE succeeds, without moving buffer",
    1725              :                  get_fndecl ());
    1726            8 :     }
    1727              : 
    1728          279 :     bool update_model (region_model *model,
    1729              :                        const exploded_edge *,
    1730              :                        region_model_context *ctxt) const final override
    1731              :     {
    1732              :       /* Update size of buffer and return the ptr unchanged.  */
    1733          279 :       const call_details cd (get_call_details (model, ctxt));
    1734          279 :       region_model_manager *mgr = cd.get_manager ();
    1735          279 :       const svalue *ptr_sval = cd.get_arg_svalue (0);
    1736          279 :       const svalue *size_sval = cd.get_arg_svalue (1);
    1737              : 
    1738              :       /* We can only grow in place with a non-NULL pointer.  */
    1739          279 :       {
    1740          279 :         const svalue *null_ptr
    1741          279 :           = mgr->get_or_create_int_cst (ptr_sval->get_type (), 0);
    1742          279 :         if (!model->add_constraint (ptr_sval, NE_EXPR, null_ptr,
    1743              :                                     cd.get_ctxt ()))
    1744              :           return false;
    1745              :       }
    1746              : 
    1747          273 :       if (const region *buffer_reg = model->deref_rvalue (ptr_sval, NULL_TREE,
    1748              :                                                           ctxt))
    1749          273 :         if (compat_types_p (size_sval->get_type (), size_type_node))
    1750          273 :           model->set_dynamic_extents (buffer_reg, size_sval, ctxt);
    1751          273 :       if (cd.get_lhs_region ())
    1752              :         {
    1753          264 :           model->set_value (cd.get_lhs_region (), ptr_sval, cd.get_ctxt ());
    1754          264 :           const svalue *zero
    1755          264 :             = mgr->get_or_create_int_cst (cd.get_lhs_type (), 0);
    1756          264 :           return model->add_constraint (ptr_sval, NE_EXPR, zero, ctxt);
    1757              :         }
    1758              :       else
    1759              :         return true;
    1760              :     }
    1761              :   };
    1762              : 
    1763              :   /* Concrete custom_edge_info: a realloc call that succeeds, freeing
    1764              :      the existing buffer and moving the content to a freshly allocated
    1765              :      buffer.  */
    1766            0 :   class success_with_move : public call_info
    1767              :   {
    1768              :   public:
    1769          257 :     success_with_move (const call_details &cd)
    1770          257 :     : call_info (cd)
    1771              :     {
    1772              :     }
    1773              : 
    1774           60 :     void print_desc (pretty_printer &pp) const final override
    1775              :     {
    1776           60 :       pp_printf (&pp,
    1777              :                  "when %qE succeeds, moving buffer",
    1778              :                  get_fndecl ());
    1779           60 :     }
    1780          356 :     bool update_model (region_model *model,
    1781              :                        const exploded_edge *,
    1782              :                        region_model_context *ctxt) const final override
    1783              :     {
    1784          356 :       const call_details cd (get_call_details (model, ctxt));
    1785          356 :       region_model_manager *mgr = cd.get_manager ();
    1786          356 :       const svalue *old_ptr_sval = cd.get_arg_svalue (0);
    1787          356 :       const svalue *new_size_sval = cd.get_arg_svalue (1);
    1788              : 
    1789              :       /* Create the new region.  */
    1790          356 :       const region *new_reg
    1791          356 :         = model->get_or_create_region_for_heap_alloc (new_size_sval, ctxt);
    1792          356 :       const svalue *new_ptr_sval
    1793          356 :         = mgr->get_ptr_svalue (ptr_type_node, new_reg);
    1794          356 :       if (!model->add_constraint (new_ptr_sval, NE_EXPR, old_ptr_sval,
    1795              :                                   cd.get_ctxt ()))
    1796              :         return false;
    1797              : 
    1798          356 :       if (cd.get_lhs_type ())
    1799          347 :         cd.maybe_set_lhs (new_ptr_sval);
    1800              : 
    1801          356 :       if (const region *freed_reg = model->deref_rvalue (old_ptr_sval,
    1802              :                                                          NULL_TREE, ctxt))
    1803              :         {
    1804              :           /* Copy the data.  */
    1805          356 :           const svalue *old_size_sval = model->get_dynamic_extents (freed_reg);
    1806          356 :           if (old_size_sval)
    1807              :             {
    1808          172 :               const svalue *copied_size_sval
    1809          172 :                 = get_copied_size (model, old_size_sval, new_size_sval);
    1810          172 :               const region *copied_old_reg
    1811          172 :                 = mgr->get_sized_region (freed_reg, nullptr, copied_size_sval);
    1812          172 :               const svalue *buffer_content_sval
    1813          172 :                 = model->get_store_value (copied_old_reg, cd.get_ctxt ());
    1814          172 :               const region *copied_new_reg
    1815          172 :                 = mgr->get_sized_region (new_reg, nullptr, copied_size_sval);
    1816          172 :               model->set_value (copied_new_reg, buffer_content_sval,
    1817              :                                 cd.get_ctxt ());
    1818              :             }
    1819              :           else
    1820              :             {
    1821              :               /* We don't know how big the old region was;
    1822              :                  mark the new region as having been touched to avoid uninit
    1823              :                  issues.  */
    1824          184 :               model->mark_region_as_unknown (new_reg, cd.get_uncertainty ());
    1825              :             }
    1826              : 
    1827              :           /* Free the old region, so that pointers to the old buffer become
    1828              :              invalid.  */
    1829              : 
    1830              :           /* If the ptr points to an underlying heap region, delete it,
    1831              :              poisoning pointers.  */
    1832          356 :           model->unbind_region_and_descendents (freed_reg, poison_kind::freed);
    1833          356 :           model->unset_dynamic_extents (freed_reg);
    1834              :         }
    1835              : 
    1836              :       /* Update the sm-state: mark the old_ptr_sval as "freed",
    1837              :          and the new_ptr_sval as "nonnull".  */
    1838          356 :       model->on_realloc_with_move (cd, old_ptr_sval, new_ptr_sval);
    1839              : 
    1840          356 :       if (cd.get_lhs_type ())
    1841              :         {
    1842          347 :           const svalue *zero
    1843          347 :             = mgr->get_or_create_int_cst (cd.get_lhs_type (), 0);
    1844          347 :           return model->add_constraint (new_ptr_sval, NE_EXPR, zero,
    1845          347 :                                         cd.get_ctxt ());
    1846              :         }
    1847              :       else
    1848              :         return true;
    1849              :     }
    1850              : 
    1851              :   private:
    1852              :     /* Return the lesser of OLD_SIZE_SVAL and NEW_SIZE_SVAL.
    1853              :        If unknown, OLD_SIZE_SVAL is returned.  */
    1854          172 :     const svalue *get_copied_size (region_model *model,
    1855              :                                    const svalue *old_size_sval,
    1856              :                                    const svalue *new_size_sval) const
    1857              :     {
    1858          172 :       tristate res
    1859          172 :         = model->eval_condition (old_size_sval, GT_EXPR, new_size_sval);
    1860          172 :       switch (res.get_value ())
    1861              :         {
    1862              :         case tristate::TS_TRUE:
    1863              :           return new_size_sval;
    1864          131 :         case tristate::TS_FALSE:
    1865          131 :         case tristate::TS_UNKNOWN:
    1866          131 :           return old_size_sval;
    1867            0 :         default:
    1868            0 :           gcc_unreachable ();
    1869              :         }
    1870              :     }
    1871              :   };
    1872              : 
    1873              :   /* Body of kf_realloc::impl_call_post.  */
    1874              : 
    1875          257 :   if (cd.get_ctxt ())
    1876              :     {
    1877          257 :       cd.get_ctxt ()->bifurcate (std::make_unique<failure> (cd));
    1878          257 :       cd.get_ctxt ()->bifurcate (std::make_unique<success_no_move> (cd));
    1879          257 :       cd.get_ctxt ()->bifurcate (std::make_unique<success_with_move> (cd));
    1880          257 :       cd.get_ctxt ()->terminate_path ();
    1881              :     }
    1882          257 : }
    1883              : 
    1884              : /* Handler for "strchr" and "__builtin_strchr".  */
    1885              : 
    1886         7146 : class kf_strchr : public builtin_known_function
    1887              : {
    1888              : public:
    1889          153 :   bool matches_call_types_p (const call_details &cd) const final override
    1890              :   {
    1891          153 :     return (cd.num_args () == 2 && cd.arg_is_pointer_p (0));
    1892              :   }
    1893           22 :   void impl_call_pre (const call_details &cd) const final override
    1894              :   {
    1895           22 :     cd.check_for_null_terminated_string_arg (0);
    1896           22 :   }
    1897              : 
    1898          176 :   enum built_in_function builtin_code () const final override
    1899              :   {
    1900          176 :     return BUILT_IN_STRCHR;
    1901              :   }
    1902              :   void impl_call_post (const call_details &cd) const final override;
    1903              : };
    1904              : 
    1905              : void
    1906           21 : kf_strchr::impl_call_post (const call_details &cd) const
    1907              : {
    1908            0 :   class strchr_call_info : public call_info
    1909              :   {
    1910              :   public:
    1911           42 :     strchr_call_info (const call_details &cd, bool found)
    1912           42 :     : call_info (cd), m_found (found)
    1913              :     {
    1914              :     }
    1915              : 
    1916           38 :     void print_desc (pretty_printer &pp) const final override
    1917              :     {
    1918           38 :       if (m_found)
    1919           18 :         pp_printf (&pp,
    1920              :                    "when %qE returns non-NULL",
    1921              :                    get_fndecl ());
    1922              :       else
    1923           20 :         pp_printf (&pp,
    1924              :                    "when %qE returns NULL",
    1925              :                    get_fndecl ());
    1926           38 :     }
    1927              : 
    1928           86 :     bool update_model (region_model *model,
    1929              :                        const exploded_edge *,
    1930              :                        region_model_context *ctxt) const final override
    1931              :     {
    1932           86 :       const call_details cd (get_call_details (model, ctxt));
    1933           86 :       if (tree lhs_type = cd.get_lhs_type ())
    1934              :         {
    1935           84 :           region_model_manager *mgr = model->get_manager ();
    1936           84 :           const svalue *result;
    1937           84 :           if (m_found)
    1938              :             {
    1939           41 :               const svalue *str_sval = cd.get_arg_svalue (0);
    1940           41 :               const region *str_reg
    1941           41 :                 = model->deref_rvalue (str_sval, cd.get_arg_tree (0),
    1942              :                                        cd.get_ctxt ());
    1943              :               /* We want str_sval + OFFSET for some unknown OFFSET.
    1944              :                  Use a conjured_svalue to represent the offset,
    1945              :                  using the str_reg as the id of the conjured_svalue.  */
    1946           41 :               const svalue *offset
    1947           41 :                 = mgr->get_or_create_conjured_svalue (size_type_node,
    1948           41 :                                                       &cd.get_call_stmt (),
    1949              :                                                       str_reg,
    1950           41 :                                                       conjured_purge (model,
    1951           41 :                                                                       ctxt));
    1952           41 :               result = mgr->get_or_create_binop (lhs_type, POINTER_PLUS_EXPR,
    1953              :                                                  str_sval, offset);
    1954              :             }
    1955              :           else
    1956           43 :             result = mgr->get_or_create_int_cst (lhs_type, 0);
    1957           84 :           cd.maybe_set_lhs (result);
    1958              :         }
    1959           86 :       return true;
    1960              :     }
    1961              :   private:
    1962              :     bool m_found;
    1963              :   };
    1964              : 
    1965              :   /* Body of kf_strchr::impl_call_post.  */
    1966           21 :   if (cd.get_ctxt ())
    1967              :     {
    1968           21 :       cd.get_ctxt ()->bifurcate (std::make_unique<strchr_call_info> (cd, false));
    1969           21 :       cd.get_ctxt ()->bifurcate (std::make_unique<strchr_call_info> (cd, true));
    1970           21 :       cd.get_ctxt ()->terminate_path ();
    1971              :     }
    1972           21 : }
    1973              : 
    1974              : /* Handler for "sprintf".
    1975              :      int sprintf(char *str, const char *format, ...);
    1976              : */
    1977              : 
    1978         7146 : class kf_sprintf : public builtin_known_function
    1979              : {
    1980              : public:
    1981         2572 :   bool matches_call_types_p (const call_details &cd) const final override
    1982              :   {
    1983         2572 :     return (cd.num_args () >= 2
    1984         2572 :             && cd.arg_is_pointer_p (0)
    1985         5144 :             && cd.arg_is_pointer_p (1));
    1986              :   }
    1987              : 
    1988          632 :   enum built_in_function builtin_code () const final override
    1989              :   {
    1990          632 :     return BUILT_IN_SPRINTF;
    1991              :   }
    1992              : 
    1993         1093 :   void impl_call_pre (const call_details &cd) const final override
    1994              :   {
    1995              :     /* For now, merely assume that the destination buffer gets set to a
    1996              :        new svalue.  */
    1997         1093 :     region_model *model = cd.get_model ();
    1998         1093 :     region_model_context *ctxt = cd.get_ctxt ();
    1999         1093 :     const svalue *dst_ptr = cd.get_arg_svalue (0);
    2000         1093 :     const region *dst_reg
    2001         1093 :       = model->deref_rvalue (dst_ptr, cd.get_arg_tree (0), ctxt);
    2002              :     /* Restrict the region we consider to be affected to the valid capacity
    2003              :        so that we don't trigger buffer overflow false positives.  */
    2004         1093 :     const svalue *capacity = model->get_capacity (dst_reg);
    2005         1093 :     dst_reg = model->get_manager ()->get_sized_region (dst_reg,
    2006              :                                                        NULL_TREE,
    2007              :                                                        capacity);
    2008         1093 :     const svalue *content = cd.get_or_create_conjured_svalue (dst_reg);
    2009         1093 :     model->set_value (dst_reg, content, ctxt);
    2010         1093 :     cd.set_any_lhs_with_defaults ();
    2011         1093 :   }
    2012              : };
    2013              : 
    2014              : /* Handler for "__builtin_stack_restore".  */
    2015              : 
    2016         3573 : class kf_stack_restore : public pure_known_function_with_default_return
    2017              : {
    2018              : public:
    2019            0 :   bool matches_call_types_p (const call_details &) const final override
    2020              :   {
    2021            0 :     return true;
    2022              :   }
    2023              : 
    2024              :   /* Currently a no-op.  */
    2025              : };
    2026              : 
    2027              : /* Handler for "__builtin_stack_save".  */
    2028              : 
    2029         3573 : class kf_stack_save : public pure_known_function_with_default_return
    2030              : {
    2031              : public:
    2032            0 :   bool matches_call_types_p (const call_details &) const final override
    2033              :   {
    2034            0 :     return true;
    2035              :   }
    2036              : 
    2037              :   /* Currently a no-op.  */
    2038              : };
    2039              : 
    2040              : /* Handler for "__builtin_eh_pointer".  */
    2041              : 
    2042         3573 : class kf_eh_pointer : public builtin_known_function
    2043              : {
    2044              : public:
    2045            0 :   bool matches_call_types_p (const call_details &) const final override
    2046              :   {
    2047            0 :     return true;
    2048              :   }
    2049              : 
    2050         1184 :   enum built_in_function builtin_code () const final override
    2051              :   {
    2052         1184 :     return BUILT_IN_EH_POINTER;
    2053              :   }
    2054              : 
    2055          355 :   void impl_call_pre (const call_details &cd) const final override
    2056              :   {
    2057          355 :     cd.set_any_lhs_with_defaults ();
    2058          355 :   }
    2059              : };
    2060              : 
    2061              : /* Handler for "strcat" and "__builtin_strcat_chk".  */
    2062              : 
    2063              : class kf_strcat : public builtin_known_function
    2064              : {
    2065              : public:
    2066        17865 :   kf_strcat (unsigned int num_args, bool chk_variant)
    2067        17865 :     : m_num_args (num_args),
    2068        17865 :       m_chk_variant (chk_variant) {}
    2069          397 :   bool matches_call_types_p (const call_details &cd) const final override
    2070              :   {
    2071          397 :     return (cd.num_args () == m_num_args
    2072          397 :             && cd.arg_is_pointer_p (0)
    2073          794 :             && cd.arg_is_pointer_p (1));
    2074              :   }
    2075              : 
    2076          456 :   enum built_in_function builtin_code () const final override
    2077              :   {
    2078          456 :     return m_chk_variant ? BUILT_IN_STRCAT_CHK : BUILT_IN_STRCAT;
    2079              :   }
    2080              : 
    2081           57 :   void impl_call_pre (const call_details &cd) const final override
    2082              :   {
    2083           57 :     region_model *model = cd.get_model ();
    2084           57 :     region_model_manager *mgr = cd.get_manager ();
    2085              : 
    2086           57 :     const svalue *dest_sval = cd.get_arg_svalue (0);
    2087           57 :     const region *dest_reg = model->deref_rvalue (dest_sval, cd.get_arg_tree (0),
    2088              :                                                   cd.get_ctxt ());
    2089              : 
    2090           57 :     const svalue *dst_strlen_sval
    2091           57 :       = cd.check_for_null_terminated_string_arg (0, false, nullptr);
    2092           57 :     if (!dst_strlen_sval)
    2093              :       {
    2094            0 :         if (cd.get_ctxt ())
    2095            0 :           cd.get_ctxt ()->terminate_path ();
    2096            0 :         return;
    2097              :       }
    2098              : 
    2099           57 :     const symbolic::bit_vector *bv_to_copy;
    2100           57 :     const svalue *num_src_bytes_read_sval
    2101           57 :       = cd.check_for_null_terminated_string_arg (1, true, &bv_to_copy);
    2102           57 :     if (!num_src_bytes_read_sval)
    2103              :       {
    2104            0 :         if (cd.get_ctxt ())
    2105            0 :           cd.get_ctxt ()->terminate_path ();
    2106              :         return;
    2107              :       }
    2108              : 
    2109           57 :     cd.maybe_set_lhs (dest_sval);
    2110           57 :     cd.complain_about_overlap (0, 1, num_src_bytes_read_sval);
    2111              : 
    2112           57 :     const region *offset_reg
    2113           57 :       = mgr->get_offset_region (dest_reg, NULL_TREE, dst_strlen_sval);
    2114           57 :     model->write_bytes (offset_reg->get_pointer (*mgr),
    2115              :                         *bv_to_copy,
    2116              :                         cd.get_ctxt ());
    2117              :   }
    2118              : 
    2119              : private:
    2120              :   unsigned int m_num_args;
    2121              :   const bool m_chk_variant;
    2122              : };
    2123              : 
    2124              : /* Handler for "strcpy" and "__builtin_strcpy_chk".  */
    2125              : 
    2126              : class kf_strcpy : public builtin_known_function
    2127              : {
    2128              : public:
    2129        17865 :   kf_strcpy (unsigned int num_args, bool chk_variant)
    2130        17865 :     : m_num_args (num_args),
    2131        17865 :       m_chk_variant (chk_variant) {}
    2132         1094 :   bool matches_call_types_p (const call_details &cd) const final override
    2133              :   {
    2134         1094 :     return (cd.num_args () == m_num_args
    2135         1094 :             && cd.arg_is_pointer_p (0)
    2136         2188 :             && cd.arg_is_pointer_p (1));
    2137              :   }
    2138          656 :   enum built_in_function builtin_code () const final override
    2139              :   {
    2140          656 :     return m_chk_variant ? BUILT_IN_STRCPY_CHK : BUILT_IN_STRCPY;
    2141              :   }
    2142              :   void impl_call_pre (const call_details &cd) const final override;
    2143              : 
    2144              : private:
    2145              :   unsigned int m_num_args;
    2146              :   const bool m_chk_variant;
    2147              : };
    2148              : 
    2149              : void
    2150          343 : kf_strcpy::impl_call_pre (const call_details &cd) const
    2151              : {
    2152          343 :   region_model *model = cd.get_model ();
    2153          343 :   region_model_context *ctxt = cd.get_ctxt ();
    2154              : 
    2155          343 :   const svalue *dest_sval = cd.get_arg_svalue (0);
    2156          343 :   const region *dest_reg = model->deref_rvalue (dest_sval, cd.get_arg_tree (0),
    2157              :                                                     ctxt);
    2158              :   /* strcpy returns the initial param.  */
    2159          343 :   cd.maybe_set_lhs (dest_sval);
    2160              : 
    2161          343 :   const symbolic::bit_vector *bv_to_copy;
    2162          686 :   if (const svalue *num_bytes_read_sval
    2163          343 :       = cd.check_for_null_terminated_string_arg (1, true, &bv_to_copy))
    2164              :     {
    2165          343 :       gcc_assert (bv_to_copy);
    2166          343 :       cd.complain_about_overlap (0, 1, num_bytes_read_sval);
    2167          343 :       model->write_bytes (dest_reg->get_pointer (*model->get_manager ()),
    2168              :                           *bv_to_copy,
    2169              :                           ctxt);
    2170              :     }
    2171              :   else
    2172              :     {
    2173            0 :       if (cd.get_ctxt ())
    2174            0 :         cd.get_ctxt ()->terminate_path ();
    2175              :     }
    2176          343 : }
    2177              : 
    2178              : /* Handler for "strdup" and "__builtin_strdup".  */
    2179              : 
    2180         7146 : class kf_strdup : public builtin_known_function
    2181              : {
    2182              : public:
    2183         1451 :   bool matches_call_types_p (const call_details &cd) const final override
    2184              :   {
    2185         1451 :     return (cd.num_args () == 1 && cd.arg_is_pointer_p (0));
    2186              :   }
    2187         1320 :   enum built_in_function builtin_code () const final override
    2188              :   {
    2189         1320 :     return BUILT_IN_STRDUP;
    2190              :   }
    2191          286 :   void impl_call_pre (const call_details &cd) const final override
    2192              :   {
    2193          286 :     region_model *model = cd.get_model ();
    2194          286 :     region_model_context *ctxt = cd.get_ctxt ();
    2195          286 :     region_model_manager *mgr = cd.get_manager ();
    2196          286 :     const symbolic::bit_vector *bv_to_copy;
    2197          572 :     if (const svalue *num_bytes_read_sval
    2198          286 :         = cd.check_for_null_terminated_string_arg (0, true, &bv_to_copy))
    2199              :       {
    2200          286 :         gcc_assert (bv_to_copy);
    2201          286 :         const region *new_reg
    2202          286 :           = model->get_or_create_region_for_heap_alloc (num_bytes_read_sval,
    2203              :                                                         ctxt);
    2204          286 :         model->write_bytes (new_reg->get_pointer (*mgr),
    2205              :                             *bv_to_copy,
    2206              :                             ctxt);
    2207          286 :         if (cd.get_lhs_type ())
    2208              :           {
    2209          286 :             const svalue *ptr_sval
    2210          286 :               = mgr->get_ptr_svalue (cd.get_lhs_type (), new_reg);
    2211          286 :             cd.maybe_set_lhs (ptr_sval);
    2212              :           }
    2213              :       }
    2214              :     else
    2215              :       {
    2216            0 :         if (ctxt)
    2217            0 :           ctxt->terminate_path ();
    2218              :       }
    2219          286 :   }
    2220              : };
    2221              : 
    2222              : /* Handler for "strlen" and for "__analyzer_get_strlen".  */
    2223              : 
    2224        14292 : class kf_strlen : public builtin_known_function
    2225              : {
    2226              : public:
    2227         7518 :   bool matches_call_types_p (const call_details &cd) const final override
    2228              :   {
    2229         7518 :     return (cd.num_args () == 1 && cd.arg_is_pointer_p (0));
    2230              :   }
    2231         5264 :   enum built_in_function builtin_code () const final override
    2232              :   {
    2233         5264 :     return BUILT_IN_STRLEN;
    2234              :   }
    2235              : 
    2236         2119 :   void impl_call_pre (const call_details &cd) const final override
    2237              :   {
    2238         4238 :     if (const svalue *strlen_sval
    2239         2119 :           = cd.check_for_null_terminated_string_arg (0, false, nullptr))
    2240         2119 :       if (strlen_sval->get_kind () != SK_UNKNOWN)
    2241              :         {
    2242         1304 :           cd.maybe_set_lhs (strlen_sval);
    2243         1304 :           return;
    2244              :         }
    2245              : 
    2246              :     /* Use a conjured svalue.  */
    2247          815 :     cd.set_any_lhs_with_defaults ();
    2248              :   }
    2249              : };
    2250              : 
    2251              : /* Factory function, so that kf-analyzer.cc can use this class.  */
    2252              : 
    2253              : std::unique_ptr<known_function>
    2254         3573 : make_kf_strlen ()
    2255              : {
    2256         3573 :   return std::make_unique<kf_strlen> ();
    2257              : }
    2258              : 
    2259              : /* Handler for "strncpy" and "__builtin_strncpy".
    2260              :    See e.g. https://en.cppreference.com/w/c/string/byte/strncpy
    2261              : 
    2262              :      extern char *strncpy (char *dst, const char *src, size_t count);
    2263              : 
    2264              :    Handle this by splitting into two outcomes:
    2265              :    (a) truncated read from "src" of "count" bytes,
    2266              :        writing "count" bytes to "dst"
    2267              :    (b) read from "src" of up to (and including) the null terminator,
    2268              :        where the number of bytes read < "count" bytes,
    2269              :        writing those bytes to "dst", and zero-filling the rest,
    2270              :        up to "count".  */
    2271              : 
    2272        10719 : class kf_strncpy : public builtin_known_function
    2273              : {
    2274              : public:
    2275         1169 :   bool matches_call_types_p (const call_details &cd) const final override
    2276              :   {
    2277         1169 :     return (cd.num_args () == 3
    2278         1169 :             && cd.arg_is_pointer_p (0)
    2279         1169 :             && cd.arg_is_pointer_p (1)
    2280         2338 :             && cd.arg_is_integral_p (2));
    2281              :   }
    2282         1336 :   enum built_in_function builtin_code () const final override
    2283              :   {
    2284         1336 :     return BUILT_IN_STRNCPY;
    2285              :   }
    2286              :   void impl_call_post (const call_details &cd) const final override;
    2287              : };
    2288              : 
    2289              : void
    2290          167 : kf_strncpy::impl_call_post (const call_details &cd) const
    2291              : {
    2292            0 :   class strncpy_call_info : public call_info
    2293              :   {
    2294              :   public:
    2295          334 :     strncpy_call_info (const call_details &cd,
    2296              :                        const svalue *num_bytes_with_terminator_sval,
    2297              :                        bool truncated_read)
    2298              :     : call_info (cd),
    2299          334 :       m_num_bytes_with_terminator_sval (num_bytes_with_terminator_sval),
    2300          334 :       m_truncated_read (truncated_read)
    2301              :     {
    2302              :     }
    2303              : 
    2304          112 :     void print_desc (pretty_printer &pp) const final override
    2305              :     {
    2306          112 :       if (m_truncated_read)
    2307           12 :         pp_printf (&pp,
    2308              :                    "when %qE truncates the source string",
    2309              :                    get_fndecl ());
    2310              :       else
    2311          100 :         pp_printf (&pp,
    2312              :                    "when %qE copies the full source string",
    2313              :                    get_fndecl ());
    2314          112 :     }
    2315              : 
    2316          562 :     bool update_model (region_model *model,
    2317              :                        const exploded_edge *,
    2318              :                        region_model_context *ctxt) const final override
    2319              :     {
    2320          562 :       const call_details cd (get_call_details (model, ctxt));
    2321              : 
    2322          562 :       const svalue *dest_sval = cd.get_arg_svalue (0);
    2323          562 :       const region *dest_reg
    2324          562 :         = model->deref_rvalue (dest_sval, cd.get_arg_tree (0), ctxt);
    2325          562 :       auto dest_sym_ptr = cd.get_arg_as_sym_ptr (0);
    2326              : 
    2327          562 :       auto src_sym_ptr = cd.get_arg_as_sym_ptr (1);
    2328              : 
    2329          562 :       const svalue *count_sval = cd.get_arg_svalue (2);
    2330              : 
    2331              :       /* strncpy returns the initial param.  */
    2332          562 :       cd.maybe_set_lhs (dest_sval);
    2333              : 
    2334          562 :       const svalue *num_bytes_read_sval;
    2335          562 :       if (m_truncated_read)
    2336              :         {
    2337              :           /* Truncated read.  */
    2338          259 :           num_bytes_read_sval = count_sval;
    2339              : 
    2340          259 :           if (m_num_bytes_with_terminator_sval)
    2341              :             {
    2342              :               /* The terminator is after the limit.  */
    2343          259 :               if (!model->add_constraint (m_num_bytes_with_terminator_sval,
    2344              :                                           GT_EXPR,
    2345              :                                           count_sval,
    2346              :                                           ctxt))
    2347              :                 return false;
    2348              :             }
    2349              :           else
    2350              :             {
    2351              :               /* We don't know where the terminator is, or if there is one.
    2352              :                  In theory we know that the first COUNT bytes are non-zero,
    2353              :                  but we don't have a way to record that constraint.  */
    2354              :             }
    2355              :         }
    2356              :       else
    2357              :         {
    2358              :           /* Full read of the src string before reaching the limit,
    2359              :              so there must be a terminator and it must be at or before
    2360              :              the limit.  */
    2361          303 :           if (m_num_bytes_with_terminator_sval)
    2362              :             {
    2363          303 :               if (!model->add_constraint (m_num_bytes_with_terminator_sval,
    2364              :                                           LE_EXPR,
    2365              :                                           count_sval,
    2366              :                                           ctxt))
    2367              :                 return false;
    2368          299 :               num_bytes_read_sval = m_num_bytes_with_terminator_sval;
    2369              : 
    2370              :               /* First, zero-fill the dest buffer.
    2371              :                  We don't need to do this for the truncation case, as
    2372              :                  this fully populates the dest buffer.  */
    2373          299 :               const region *sized_dest_reg
    2374          299 :                 = model->get_manager ()->get_sized_region (dest_reg,
    2375              :                                                            NULL_TREE,
    2376              :                                                            count_sval);
    2377          299 :               model->zero_fill_region (sized_dest_reg, ctxt);
    2378              :             }
    2379              :           else
    2380              :             {
    2381              :               /* Don't analyze this case; the other case will
    2382              :                  assume a "truncated" read up to the limit.  */
    2383              :               return false;
    2384              :             }
    2385              :         }
    2386              : 
    2387          544 :       gcc_assert (num_bytes_read_sval);
    2388              : 
    2389          544 :       auto &node_mgr = model->get_manager ()->get_node_manager ();
    2390          544 :       auto &bytes_mgr = node_mgr.get_scalars ().get_bytes ();
    2391          544 :       auto &num_bytes = bytes_mgr.get_scalar_from_svalue (*num_bytes_read_sval);
    2392              : 
    2393          544 :       const symbolic::bit_vector &bytes_to_copy
    2394          544 :         = model->read_bytes (src_sym_ptr,
    2395              :                              cd.get_arg_tree (1),
    2396              :                              num_bytes,
    2397              :                              ctxt);
    2398          544 :       cd.complain_about_overlap (0, 1, num_bytes_read_sval);
    2399          544 :       model->write_bytes (dest_sym_ptr,
    2400              :                           bytes_to_copy,
    2401              :                           ctxt);
    2402              : 
    2403          544 :       return true;
    2404              :     }
    2405              :   private:
    2406              :     /* (strlen + 1) of the source string if it has a terminator,
    2407              :        or nullptr for the case where UB would happen before
    2408              :        finding any terminator.  */
    2409              :     const svalue *m_num_bytes_with_terminator_sval;
    2410              : 
    2411              :     /* true: if this is the outcome where the limit was reached before
    2412              :        the null terminator
    2413              :        false: if the null terminator was reached before the limit.  */
    2414              :     bool m_truncated_read;
    2415              :   };
    2416              : 
    2417              :   /* Body of kf_strncpy::impl_call_post.  */
    2418          167 :   if (cd.get_ctxt ())
    2419              :     {
    2420              :       /* First, scan for a null terminator as if there were no limit,
    2421              :          with a null ctxt so no errors are reported.  */
    2422          167 :       const region_model *model = cd.get_model ();
    2423          167 :       const svalue *ptr_arg_sval = cd.get_arg_svalue (1);
    2424          167 :       const region *buf_reg
    2425          167 :         = model->deref_rvalue (ptr_arg_sval, cd.get_arg_tree (1), nullptr);
    2426          167 :       const svalue *num_bytes_with_terminator_sval
    2427          167 :         = model->scan_for_null_terminator (buf_reg,
    2428              :                                            cd.get_arg_tree (1),
    2429              :                                            nullptr,
    2430          167 :                                            nullptr);
    2431          167 :       cd.get_ctxt ()->bifurcate
    2432          167 :         (std::make_unique<strncpy_call_info>
    2433          167 :            (cd, num_bytes_with_terminator_sval,
    2434          167 :             false));
    2435          167 :       cd.get_ctxt ()->bifurcate
    2436          167 :         (std::make_unique<strncpy_call_info>
    2437          167 :            (cd, num_bytes_with_terminator_sval,
    2438          167 :             true));
    2439          167 :       cd.get_ctxt ()->terminate_path ();
    2440              :     }
    2441          167 : };
    2442              : 
    2443              : /* Handler for "strndup" and "__builtin_strndup".  */
    2444              : 
    2445         7146 : class kf_strndup : public builtin_known_function
    2446              : {
    2447              : public:
    2448          118 :   bool matches_call_types_p (const call_details &cd) const final override
    2449              :   {
    2450          118 :     return (cd.num_args () == 2 && cd.arg_is_pointer_p (0));
    2451              :   }
    2452           78 :   enum built_in_function builtin_code () const final override
    2453              :   {
    2454           78 :     return BUILT_IN_STRNDUP;
    2455              :   }
    2456           33 :   void impl_call_pre (const call_details &cd) const final override
    2457              :   {
    2458           33 :     region_model *model = cd.get_model ();
    2459           33 :     region_model_manager *mgr = cd.get_manager ();
    2460              :     /* Ideally we'd get the size here, and simulate copying the bytes.  */
    2461           33 :     const region *new_reg
    2462           33 :       = model->get_or_create_region_for_heap_alloc (nullptr, cd.get_ctxt ());
    2463           33 :     model->mark_region_as_unknown (new_reg, nullptr);
    2464           33 :     if (cd.get_lhs_type ())
    2465              :       {
    2466           33 :         const svalue *ptr_sval
    2467           33 :           = mgr->get_ptr_svalue (cd.get_lhs_type (), new_reg);
    2468           33 :         cd.maybe_set_lhs (ptr_sval);
    2469              :       }
    2470           33 :   }
    2471              : };
    2472              : 
    2473              : /* Handler for "strstr" and "__builtin_strstr".
    2474              :      extern char *strstr (const char* str, const char* substr);
    2475              :    See e.g. https://en.cppreference.com/w/c/string/byte/strstr  */
    2476              : 
    2477         7146 : class kf_strstr : public builtin_known_function
    2478              : {
    2479              : public:
    2480          279 :   bool matches_call_types_p (const call_details &cd) const final override
    2481              :   {
    2482          279 :     return (cd.num_args () == 2
    2483          279 :             && cd.arg_is_pointer_p (0)
    2484          558 :             && cd.arg_is_pointer_p (1));
    2485              :   }
    2486          328 :   enum built_in_function builtin_code () const final override
    2487              :   {
    2488          328 :     return BUILT_IN_STRSTR;
    2489              :   }
    2490           41 :   void impl_call_pre (const call_details &cd) const final override
    2491              :   {
    2492           41 :     cd.check_for_null_terminated_string_arg (0);
    2493           41 :     cd.check_for_null_terminated_string_arg (1);
    2494           41 :   }
    2495              :   void impl_call_post (const call_details &cd) const final override;
    2496              : };
    2497              : 
    2498              : void
    2499           33 : kf_strstr::impl_call_post (const call_details &cd) const
    2500              : {
    2501            0 :   class strstr_call_info : public call_info
    2502              :   {
    2503              :   public:
    2504           66 :     strstr_call_info (const call_details &cd, bool found)
    2505           66 :     : call_info (cd), m_found (found)
    2506              :     {
    2507              :     }
    2508              : 
    2509            8 :     void print_desc (pretty_printer &pp) const final override
    2510              :     {
    2511            8 :       if (m_found)
    2512            0 :         pp_printf (&pp,
    2513              :                    "when %qE returns non-NULL",
    2514              :                    get_fndecl ());
    2515              :       else
    2516            8 :         pp_printf (&pp,
    2517              :                    "when %qE returns NULL",
    2518              :                    get_fndecl ());
    2519            8 :     }
    2520              : 
    2521          106 :     bool update_model (region_model *model,
    2522              :                        const exploded_edge *,
    2523              :                        region_model_context *ctxt) const final override
    2524              :     {
    2525          106 :       const call_details cd (get_call_details (model, ctxt));
    2526          106 :       if (tree lhs_type = cd.get_lhs_type ())
    2527              :         {
    2528          106 :           region_model_manager *mgr = model->get_manager ();
    2529          106 :           const svalue *result;
    2530          106 :           if (m_found)
    2531              :             {
    2532           33 :               const svalue *str_sval = cd.get_arg_svalue (0);
    2533           33 :               const region *str_reg
    2534           33 :                 = model->deref_rvalue (str_sval, cd.get_arg_tree (0),
    2535              :                                        cd.get_ctxt ());
    2536              :               /* We want str_sval + OFFSET for some unknown OFFSET.
    2537              :                  Use a conjured_svalue to represent the offset,
    2538              :                  using the str_reg as the id of the conjured_svalue.  */
    2539           33 :               const svalue *offset
    2540           33 :                 = mgr->get_or_create_conjured_svalue (size_type_node,
    2541           33 :                                                       &cd.get_call_stmt (),
    2542              :                                                       str_reg,
    2543           33 :                                                       conjured_purge (model,
    2544           33 :                                                                       ctxt));
    2545           33 :               result = mgr->get_or_create_binop (lhs_type, POINTER_PLUS_EXPR,
    2546              :                                                  str_sval, offset);
    2547              :             }
    2548              :           else
    2549           73 :             result = mgr->get_or_create_int_cst (lhs_type, 0);
    2550          106 :           cd.maybe_set_lhs (result);
    2551              :         }
    2552          106 :       return true;
    2553              :     }
    2554              :   private:
    2555              :     bool m_found;
    2556              :   };
    2557              : 
    2558              :   /* Body of kf_strstr::impl_call_post.  */
    2559           33 :   if (cd.get_ctxt ())
    2560              :     {
    2561           33 :       cd.get_ctxt ()->bifurcate (std::make_unique<strstr_call_info> (cd, false));
    2562           33 :       cd.get_ctxt ()->bifurcate (std::make_unique<strstr_call_info> (cd, true));
    2563           33 :       cd.get_ctxt ()->terminate_path ();
    2564              :     }
    2565           33 : }
    2566              : 
    2567              : /* Handle calls to "strtok".
    2568              :    See e.g.
    2569              :      https://en.cppreference.com/w/c/string/byte/strtok
    2570              :      https://man7.org/linux/man-pages/man3/strtok.3.html  */
    2571              : 
    2572              : class kf_strtok : public known_function
    2573              : {
    2574              : public:
    2575              :   class undefined_behavior : public undefined_function_behavior
    2576              :   {
    2577              :   public:
    2578            8 :     undefined_behavior (const call_details &cd)
    2579            8 :     : undefined_function_behavior (cd)
    2580              :     {
    2581              :     }
    2582           12 :     int get_controlling_option () const final override
    2583              :     {
    2584           12 :       return OPT_Wanalyzer_undefined_behavior_strtok;
    2585              :     }
    2586              : 
    2587            4 :     bool emit (diagnostic_emission_context &ctxt) final override
    2588              :     {
    2589              :       /* CWE-476: NULL Pointer Dereference.  */
    2590            4 :       ctxt.add_cwe (476);
    2591            4 :       if (ctxt.warn ("calling %qD for first time with NULL as argument 1"
    2592              :                      " has undefined behavior",
    2593              :                      get_callee_fndecl ()))
    2594              :         {
    2595            4 :           inform (ctxt.get_location (),
    2596              :                   "some implementations of %qD may crash on such input",
    2597              :                   get_callee_fndecl ());
    2598            4 :           return true;
    2599              :         }
    2600              :       return false;
    2601              :     }
    2602              : 
    2603              :     bool
    2604            8 :     describe_final_event (pretty_printer &pp,
    2605              :                           const evdesc::final_event &) final override
    2606              :     {
    2607            8 :       pp_printf (&pp,
    2608              :                  "calling %qD for first time with NULL as argument 1"
    2609              :                  " has undefined behavior",
    2610              :                  get_callee_fndecl ());
    2611            8 :       return true;
    2612              :     }
    2613              :   };
    2614              : 
    2615              :   /* An outcome of a "strtok" call.
    2616              :      We have a four-way bifurcation of the analysis via the
    2617              :      4 combinations of two flags:
    2618              :      - m_nonnull_str covers whether the "str" param was null or non-null
    2619              :      - m_found covers whether the result is null or non-null
    2620              :    */
    2621              :   class strtok_call_info : public call_info
    2622              :   {
    2623              :   public:
    2624          304 :     strtok_call_info (const call_details &cd,
    2625              :                       const private_region &private_reg,
    2626              :                       bool nonnull_str,
    2627              :                       bool found)
    2628              :     : call_info (cd),
    2629          304 :       m_private_reg (private_reg),
    2630          304 :       m_nonnull_str (nonnull_str),
    2631          304 :       m_found (found)
    2632              :     {
    2633              :     }
    2634              : 
    2635            0 :     void print_desc (pretty_printer &pp) const final override
    2636              :     {
    2637            0 :       if (m_nonnull_str)
    2638              :         {
    2639            0 :           if (m_found)
    2640            0 :             pp_printf (&pp,
    2641              :                        "when %qE on non-NULL string returns non-NULL",
    2642              :                        get_fndecl ());
    2643              :           else
    2644            0 :             pp_printf (&pp,
    2645              :                        "when %qE on non-NULL string returns NULL",
    2646              :                        get_fndecl ());
    2647              :         }
    2648              :       else
    2649              :         {
    2650            0 :           if (m_found)
    2651            0 :             pp_printf (&pp,
    2652              :                        "when %qE with NULL string (using prior) returns"
    2653              :                        " non-NULL",
    2654              :                        get_fndecl ());
    2655              :           else
    2656            0 :             pp_printf (&pp,
    2657              :                        "when %qE with NULL string (using prior) returns NULL",
    2658              :                        get_fndecl ());
    2659              :         }
    2660            0 :     }
    2661              : 
    2662          320 :     bool update_model (region_model *model,
    2663              :                        const exploded_edge *,
    2664              :                        region_model_context *ctxt) const final override
    2665              :     {
    2666          320 :       region_model_manager *mgr = model->get_manager ();
    2667          320 :       const call_details cd (get_call_details (model, ctxt));
    2668          320 :       const svalue *str_sval = cd.get_arg_svalue (0);
    2669              :       /* const svalue *delim_sval = cd.get_arg_svalue (1); */
    2670              : 
    2671          320 :       cd.check_for_null_terminated_string_arg (1);
    2672              :       /* We check that either arg 0 or the private region is null
    2673              :          terminated below.  */
    2674              : 
    2675          320 :       const svalue *null_ptr_sval
    2676          320 :         = mgr->get_or_create_null_ptr (cd.get_arg_type (0));;
    2677          488 :       if (!model->add_constraint (str_sval,
    2678          320 :                                   m_nonnull_str ? NE_EXPR : EQ_EXPR,
    2679              :                                   null_ptr_sval,
    2680              :                                   cd.get_ctxt ()))
    2681              :         return false;
    2682              : 
    2683          208 :       if (m_nonnull_str)
    2684              :         {
    2685              :           /* Update internal buffer.  */
    2686           72 :           model->set_value (&m_private_reg,
    2687              :                             mgr->get_or_create_unmergeable (str_sval),
    2688              :                             ctxt);
    2689              :         }
    2690              :       else
    2691              :         {
    2692              :           /* Read from internal buffer.  */
    2693          136 :           str_sval = model->get_store_value (&m_private_reg, ctxt);
    2694              : 
    2695              :           /* The initial value of the private region is NULL when we're
    2696              :              on a path from main.  */
    2697          272 :           if (const initial_svalue *initial_sval
    2698          136 :                 = str_sval->dyn_cast_initial_svalue ())
    2699           56 :             if (initial_sval->get_region () == &m_private_reg
    2700           56 :                 && model->called_from_main_p ())
    2701              :               {
    2702              :                 /* Implementations of strtok do not necessarily check for NULL
    2703              :                    here, and may crash; see PR analyzer/107573.
    2704              :                    Warn for this, if we were definitely passed NULL.  */
    2705           16 :                 if (cd.get_arg_svalue (0)->all_zeroes_p ())
    2706              :                   {
    2707            8 :                     if (ctxt)
    2708            8 :                       ctxt->warn (::std::make_unique<undefined_behavior> (cd));
    2709              :                   }
    2710              : 
    2711              :                 /* Assume that "str" was actually non-null; terminate
    2712              :                    this path.  */
    2713              :                 return false;
    2714              :               }
    2715              : 
    2716              :           /* Now assume str_sval is non-null.  */
    2717          120 :           if (!model->add_constraint (str_sval,
    2718              :                                       NE_EXPR,
    2719              :                                       null_ptr_sval,
    2720              :                                       cd.get_ctxt ()))
    2721              :             return false;
    2722              :         }
    2723              : 
    2724          192 :       const region *buf_reg = model->deref_rvalue (str_sval, NULL_TREE, ctxt);
    2725          192 :       model->scan_for_null_terminator (buf_reg,
    2726              :                                        NULL_TREE,
    2727              :                                        nullptr,
    2728              :                                        ctxt);
    2729              : 
    2730          192 :       if (m_found)
    2731              :         {
    2732           96 :           const region *str_reg
    2733           96 :             = model->deref_rvalue (str_sval, cd.get_arg_tree (0),
    2734              :                                    cd.get_ctxt ());
    2735              :           /* We want to figure out the start and nul terminator
    2736              :              for the token.
    2737              :              For each, we want str_sval + OFFSET for some unknown OFFSET.
    2738              :              Use a conjured_svalue to represent the offset,
    2739              :              using the str_reg as the id of the conjured_svalue.  */
    2740           96 :           const svalue *start_offset
    2741           96 :             = mgr->get_or_create_conjured_svalue (size_type_node,
    2742           96 :                                                   &cd.get_call_stmt (),
    2743              :                                                   str_reg,
    2744           96 :                                                   conjured_purge (model,
    2745           96 :                                                                   ctxt),
    2746              :                                                   0);
    2747           96 :           const svalue *nul_offset
    2748           96 :             = mgr->get_or_create_conjured_svalue (size_type_node,
    2749           96 :                                                   &cd.get_call_stmt (),
    2750              :                                                   str_reg,
    2751           96 :                                                   conjured_purge (model,
    2752           96 :                                                                   ctxt),
    2753              :                                                   1);
    2754              : 
    2755           96 :           tree char_ptr_type = build_pointer_type (char_type_node);
    2756           96 :           const svalue *result
    2757           96 :             = mgr->get_or_create_binop (char_ptr_type, POINTER_PLUS_EXPR,
    2758              :                                         str_sval, start_offset);
    2759           96 :           cd.maybe_set_lhs (result);
    2760              : 
    2761              :           /* nul_offset + 1; the offset to use for the next call.  */
    2762           96 :           const svalue *next_offset
    2763           96 :             = mgr->get_or_create_binop (size_type_node, PLUS_EXPR,
    2764              :                                         nul_offset,
    2765              :                                         mgr->get_or_create_int_cst
    2766           96 :                                         (char_type_node, 1));
    2767              : 
    2768              :           /* Write '\0' to str_sval[nul_offset].  */
    2769           96 :           const svalue *ptr_to_term
    2770           96 :             = mgr->get_or_create_binop (char_ptr_type, POINTER_PLUS_EXPR,
    2771              :                                         str_sval, nul_offset);
    2772           96 :           const region *terminator_reg
    2773           96 :             = model->deref_rvalue (ptr_to_term, NULL_TREE, cd.get_ctxt ());
    2774           96 :           model->set_value (terminator_reg,
    2775              :                             mgr->get_or_create_unmergeable
    2776              :                             (mgr->get_or_create_int_cst (char_type_node,
    2777           96 :                                                          0)),
    2778              :                             cd.get_ctxt ());
    2779              : 
    2780              :           /* Update saved ptr to be at [nul_offset + 1].  */
    2781           96 :           const svalue *ptr_to_next
    2782           96 :             = mgr->get_or_create_binop (char_ptr_type, POINTER_PLUS_EXPR,
    2783              :                                         str_sval, next_offset);
    2784           96 :           model->set_value (&m_private_reg, ptr_to_next, ctxt);
    2785              :         }
    2786              :       else
    2787           96 :         if (tree lhs_type = cd.get_lhs_type ())
    2788              :           {
    2789           88 :             const svalue *result
    2790           88 :               = mgr->get_or_create_int_cst (lhs_type, 0);
    2791           88 :             cd.maybe_set_lhs (result);
    2792              :           }
    2793              :       return true;
    2794              :     }
    2795              :   private:
    2796              :     const private_region &m_private_reg;
    2797              :     bool m_nonnull_str;
    2798              :     bool m_found;
    2799              :   }; // class strtok_call_info
    2800              : 
    2801         7146 :   kf_strtok (region_model_manager &mgr)
    2802         7146 :   : m_private_reg (mgr.alloc_node_id (),
    2803         7146 :                    mgr.get_root_region (),
    2804              :                    get_region_type (),
    2805         7146 :                    "strtok buffer")
    2806              :   {
    2807         7146 :   }
    2808              : 
    2809          532 :   bool matches_call_types_p (const call_details &cd) const final override
    2810              :   {
    2811          532 :     return (cd.num_args () == 2
    2812          532 :             && POINTER_TYPE_P (cd.get_arg_type (0))
    2813         1064 :             && POINTER_TYPE_P (cd.get_arg_type (1)));
    2814              :   }
    2815              : 
    2816           76 :   void impl_call_post (const call_details &cd) const final override
    2817              :   {
    2818           76 :     if (cd.get_ctxt ())
    2819              :       {
    2820              :         /* Four-way bifurcation, based on whether:
    2821              :            - the str is non-null
    2822              :            - the result is non-null
    2823              :            Typically the str is either null or non-null at a particular site,
    2824              :            so hopefully this will generally just lead to two out-edges.  */
    2825           76 :         cd.get_ctxt ()->bifurcate
    2826           76 :           (std::make_unique<strtok_call_info> (cd, m_private_reg, false, false));
    2827           76 :         cd.get_ctxt ()->bifurcate
    2828           76 :           (std::make_unique<strtok_call_info> (cd, m_private_reg, false, true));
    2829           76 :         cd.get_ctxt ()->bifurcate
    2830           76 :           (std::make_unique<strtok_call_info> (cd, m_private_reg, true, false));
    2831           76 :         cd.get_ctxt ()->bifurcate
    2832           76 :           (std::make_unique<strtok_call_info> (cd, m_private_reg, true, true));
    2833           76 :         cd.get_ctxt ()->terminate_path ();
    2834              :       }
    2835           76 :   }
    2836              : 
    2837              : private:
    2838         7146 :   static tree get_region_type ()
    2839              :   {
    2840         7146 :     return build_pointer_type (char_type_node);
    2841              :   }
    2842              :   const private_region m_private_reg;
    2843              : };
    2844              : 
    2845              : /* Handle calls to functions referenced by
    2846              :    __attribute__((malloc(FOO))).  */
    2847              : 
    2848              : void
    2849          482 : region_model::impl_deallocation_call (const call_details &cd)
    2850              : {
    2851          482 :   kf_free kf;
    2852          482 :   kf.impl_call_post (cd);
    2853          482 : }
    2854              : 
    2855              : /* Handler for "strcasecmp"   */
    2856              : 
    2857        10719 : class kf_strcasecmp : public builtin_known_function
    2858              : {
    2859              : public:
    2860              :   bool
    2861            0 :   matches_call_types_p (const call_details &cd) const final override
    2862              :   {
    2863            0 :     return (cd.num_args () == 2
    2864            0 :       && cd.arg_is_pointer_p (0)
    2865            0 :       && cd.arg_is_pointer_p (1));
    2866              :   }
    2867              :   void
    2868            7 :   impl_call_pre (const call_details &cd) const final override
    2869              :   {
    2870            7 :     cd.check_for_null_terminated_string_arg (0);
    2871            7 :     cd.check_for_null_terminated_string_arg (1);
    2872            7 :   }
    2873              : 
    2874              :   enum built_in_function
    2875           56 :   builtin_code () const final override
    2876              :   {
    2877           56 :     return BUILT_IN_STRCASECMP;
    2878              :   }
    2879              : 
    2880              :   void impl_call_post (const call_details &cd) const final override;
    2881              : };
    2882              : 
    2883              : void
    2884            7 : kf_strcasecmp::impl_call_post (const call_details &cd) const
    2885              : {
    2886            7 :   if (cd.get_lhs_type ())
    2887              :     {
    2888            2 :       const svalue *result_val
    2889            2 :         = cd.get_or_create_conjured_svalue (cd.get_lhs_region ());
    2890            2 :       cd.maybe_set_lhs (result_val);
    2891              :     }
    2892            7 : }
    2893              : 
    2894              : static void
    2895         3573 : register_atomic_builtins (known_function_manager &kfm)
    2896              : {
    2897         3573 :   kfm.add (BUILT_IN_ATOMIC_EXCHANGE, std::make_unique<kf_atomic_exchange> ());
    2898         3573 :   kfm.add (BUILT_IN_ATOMIC_EXCHANGE_N, std::make_unique<kf_atomic_exchange_n> ());
    2899         3573 :   kfm.add (BUILT_IN_ATOMIC_EXCHANGE_1, std::make_unique<kf_atomic_exchange_n> ());
    2900         3573 :   kfm.add (BUILT_IN_ATOMIC_EXCHANGE_2, std::make_unique<kf_atomic_exchange_n> ());
    2901         3573 :   kfm.add (BUILT_IN_ATOMIC_EXCHANGE_4, std::make_unique<kf_atomic_exchange_n> ());
    2902         3573 :   kfm.add (BUILT_IN_ATOMIC_EXCHANGE_8, std::make_unique<kf_atomic_exchange_n> ());
    2903         3573 :   kfm.add (BUILT_IN_ATOMIC_EXCHANGE_16, std::make_unique<kf_atomic_exchange_n> ());
    2904         3573 :   kfm.add (BUILT_IN_ATOMIC_LOAD, std::make_unique<kf_atomic_load> ());
    2905         3573 :   kfm.add (BUILT_IN_ATOMIC_LOAD_N, std::make_unique<kf_atomic_load_n> ());
    2906         3573 :   kfm.add (BUILT_IN_ATOMIC_LOAD_1, std::make_unique<kf_atomic_load_n> ());
    2907         3573 :   kfm.add (BUILT_IN_ATOMIC_LOAD_2, std::make_unique<kf_atomic_load_n> ());
    2908         3573 :   kfm.add (BUILT_IN_ATOMIC_LOAD_4, std::make_unique<kf_atomic_load_n> ());
    2909         3573 :   kfm.add (BUILT_IN_ATOMIC_LOAD_8, std::make_unique<kf_atomic_load_n> ());
    2910         3573 :   kfm.add (BUILT_IN_ATOMIC_LOAD_16, std::make_unique<kf_atomic_load_n> ());
    2911         3573 :   kfm.add (BUILT_IN_ATOMIC_STORE, std::make_unique<kf_atomic_store> ());
    2912         3573 :   kfm.add (BUILT_IN_ATOMIC_STORE_N, std::make_unique<kf_atomic_store_n> ());
    2913         3573 :   kfm.add (BUILT_IN_ATOMIC_STORE_1, std::make_unique<kf_atomic_store_n> ());
    2914         3573 :   kfm.add (BUILT_IN_ATOMIC_STORE_2, std::make_unique<kf_atomic_store_n> ());
    2915         3573 :   kfm.add (BUILT_IN_ATOMIC_STORE_4, std::make_unique<kf_atomic_store_n> ());
    2916         3573 :   kfm.add (BUILT_IN_ATOMIC_STORE_8, std::make_unique<kf_atomic_store_n> ());
    2917         3573 :   kfm.add (BUILT_IN_ATOMIC_STORE_16, std::make_unique<kf_atomic_store_n> ());
    2918         3573 :   kfm.add (BUILT_IN_ATOMIC_ADD_FETCH_1,
    2919         3573 :            std::make_unique<kf_atomic_op_fetch> (PLUS_EXPR));
    2920         3573 :   kfm.add (BUILT_IN_ATOMIC_ADD_FETCH_2,
    2921         3573 :            std::make_unique<kf_atomic_op_fetch> (PLUS_EXPR));
    2922         3573 :   kfm.add (BUILT_IN_ATOMIC_ADD_FETCH_4,
    2923         3573 :            std::make_unique<kf_atomic_op_fetch> (PLUS_EXPR));
    2924         3573 :   kfm.add (BUILT_IN_ATOMIC_ADD_FETCH_8,
    2925         3573 :            std::make_unique<kf_atomic_op_fetch> (PLUS_EXPR));
    2926         3573 :   kfm.add (BUILT_IN_ATOMIC_ADD_FETCH_16,
    2927         3573 :            std::make_unique<kf_atomic_op_fetch> (PLUS_EXPR));
    2928         3573 :   kfm.add (BUILT_IN_ATOMIC_SUB_FETCH_1,
    2929         3573 :            std::make_unique<kf_atomic_op_fetch> (MINUS_EXPR));
    2930         3573 :   kfm.add (BUILT_IN_ATOMIC_SUB_FETCH_2,
    2931         3573 :            std::make_unique<kf_atomic_op_fetch> (MINUS_EXPR));
    2932         3573 :   kfm.add (BUILT_IN_ATOMIC_SUB_FETCH_4,
    2933         3573 :            std::make_unique<kf_atomic_op_fetch> (MINUS_EXPR));
    2934         3573 :   kfm.add (BUILT_IN_ATOMIC_SUB_FETCH_8,
    2935         3573 :            std::make_unique<kf_atomic_op_fetch> (MINUS_EXPR));
    2936         3573 :   kfm.add (BUILT_IN_ATOMIC_SUB_FETCH_16,
    2937         3573 :            std::make_unique<kf_atomic_op_fetch> (MINUS_EXPR));
    2938         3573 :   kfm.add (BUILT_IN_ATOMIC_AND_FETCH_1,
    2939         3573 :            std::make_unique<kf_atomic_op_fetch> (BIT_AND_EXPR));
    2940         3573 :   kfm.add (BUILT_IN_ATOMIC_AND_FETCH_2,
    2941         3573 :            std::make_unique<kf_atomic_op_fetch> (BIT_AND_EXPR));
    2942         3573 :   kfm.add (BUILT_IN_ATOMIC_AND_FETCH_4,
    2943         3573 :            std::make_unique<kf_atomic_op_fetch> (BIT_AND_EXPR));
    2944         3573 :   kfm.add (BUILT_IN_ATOMIC_AND_FETCH_8,
    2945         3573 :            std::make_unique<kf_atomic_op_fetch> (BIT_AND_EXPR));
    2946         3573 :   kfm.add (BUILT_IN_ATOMIC_AND_FETCH_16,
    2947         3573 :            std::make_unique<kf_atomic_op_fetch> (BIT_AND_EXPR));
    2948         3573 :   kfm.add (BUILT_IN_ATOMIC_XOR_FETCH_1,
    2949         3573 :            std::make_unique<kf_atomic_op_fetch> (BIT_XOR_EXPR));
    2950         3573 :   kfm.add (BUILT_IN_ATOMIC_XOR_FETCH_2,
    2951         3573 :            std::make_unique<kf_atomic_op_fetch> (BIT_XOR_EXPR));
    2952         3573 :   kfm.add (BUILT_IN_ATOMIC_XOR_FETCH_4,
    2953         3573 :            std::make_unique<kf_atomic_op_fetch> (BIT_XOR_EXPR));
    2954         3573 :   kfm.add (BUILT_IN_ATOMIC_XOR_FETCH_8,
    2955         3573 :            std::make_unique<kf_atomic_op_fetch> (BIT_XOR_EXPR));
    2956         3573 :   kfm.add (BUILT_IN_ATOMIC_XOR_FETCH_16,
    2957         3573 :            std::make_unique<kf_atomic_op_fetch> (BIT_XOR_EXPR));
    2958         3573 :   kfm.add (BUILT_IN_ATOMIC_OR_FETCH_1,
    2959         3573 :            std::make_unique<kf_atomic_op_fetch> (BIT_IOR_EXPR));
    2960         3573 :   kfm.add (BUILT_IN_ATOMIC_OR_FETCH_2,
    2961         3573 :            std::make_unique<kf_atomic_op_fetch> (BIT_IOR_EXPR));
    2962         3573 :   kfm.add (BUILT_IN_ATOMIC_OR_FETCH_4,
    2963         3573 :            std::make_unique<kf_atomic_op_fetch> (BIT_IOR_EXPR));
    2964         3573 :   kfm.add (BUILT_IN_ATOMIC_OR_FETCH_8,
    2965         3573 :            std::make_unique<kf_atomic_op_fetch> (BIT_IOR_EXPR));
    2966         3573 :   kfm.add (BUILT_IN_ATOMIC_OR_FETCH_16,
    2967         3573 :            std::make_unique<kf_atomic_op_fetch> (BIT_IOR_EXPR));
    2968         3573 :   kfm.add (BUILT_IN_ATOMIC_FETCH_ADD_1,
    2969         3573 :            std::make_unique<kf_atomic_fetch_op> (PLUS_EXPR));
    2970         3573 :   kfm.add (BUILT_IN_ATOMIC_FETCH_ADD_2,
    2971         3573 :            std::make_unique<kf_atomic_fetch_op> (PLUS_EXPR));
    2972         3573 :   kfm.add (BUILT_IN_ATOMIC_FETCH_ADD_4,
    2973         3573 :            std::make_unique<kf_atomic_fetch_op> (PLUS_EXPR));
    2974         3573 :   kfm.add (BUILT_IN_ATOMIC_FETCH_ADD_8,
    2975         3573 :            std::make_unique<kf_atomic_fetch_op> (PLUS_EXPR));
    2976         3573 :   kfm.add (BUILT_IN_ATOMIC_FETCH_ADD_16,
    2977         3573 :            std::make_unique<kf_atomic_fetch_op> (PLUS_EXPR));
    2978         3573 :   kfm.add (BUILT_IN_ATOMIC_FETCH_SUB_1,
    2979         3573 :            std::make_unique<kf_atomic_fetch_op> (MINUS_EXPR));
    2980         3573 :   kfm.add (BUILT_IN_ATOMIC_FETCH_SUB_2,
    2981         3573 :            std::make_unique<kf_atomic_fetch_op> (MINUS_EXPR));
    2982         3573 :   kfm.add (BUILT_IN_ATOMIC_FETCH_SUB_4,
    2983         3573 :            std::make_unique<kf_atomic_fetch_op> (MINUS_EXPR));
    2984         3573 :   kfm.add (BUILT_IN_ATOMIC_FETCH_SUB_8,
    2985         3573 :            std::make_unique<kf_atomic_fetch_op> (MINUS_EXPR));
    2986         3573 :   kfm.add (BUILT_IN_ATOMIC_FETCH_SUB_16,
    2987         3573 :            std::make_unique<kf_atomic_fetch_op> (MINUS_EXPR));
    2988         3573 :   kfm.add (BUILT_IN_ATOMIC_FETCH_AND_1,
    2989         3573 :            std::make_unique<kf_atomic_fetch_op> (BIT_AND_EXPR));
    2990         3573 :   kfm.add (BUILT_IN_ATOMIC_FETCH_AND_2,
    2991         3573 :            std::make_unique<kf_atomic_fetch_op> (BIT_AND_EXPR));
    2992         3573 :   kfm.add (BUILT_IN_ATOMIC_FETCH_AND_4,
    2993         3573 :            std::make_unique<kf_atomic_fetch_op> (BIT_AND_EXPR));
    2994         3573 :   kfm.add (BUILT_IN_ATOMIC_FETCH_AND_8,
    2995         3573 :            std::make_unique<kf_atomic_fetch_op> (BIT_AND_EXPR));
    2996         3573 :   kfm.add (BUILT_IN_ATOMIC_FETCH_AND_16,
    2997         3573 :            std::make_unique<kf_atomic_fetch_op> (BIT_AND_EXPR));
    2998         3573 :   kfm.add (BUILT_IN_ATOMIC_FETCH_XOR_1,
    2999         3573 :            std::make_unique<kf_atomic_fetch_op> (BIT_XOR_EXPR));
    3000         3573 :   kfm.add (BUILT_IN_ATOMIC_FETCH_XOR_2,
    3001         3573 :            std::make_unique<kf_atomic_fetch_op> (BIT_XOR_EXPR));
    3002         3573 :   kfm.add (BUILT_IN_ATOMIC_FETCH_XOR_4,
    3003         3573 :            std::make_unique<kf_atomic_fetch_op> (BIT_XOR_EXPR));
    3004         3573 :   kfm.add (BUILT_IN_ATOMIC_FETCH_XOR_8,
    3005         3573 :            std::make_unique<kf_atomic_fetch_op> (BIT_XOR_EXPR));
    3006         3573 :   kfm.add (BUILT_IN_ATOMIC_FETCH_XOR_16,
    3007         3573 :            std::make_unique<kf_atomic_fetch_op> (BIT_XOR_EXPR));
    3008         3573 :   kfm.add (BUILT_IN_ATOMIC_FETCH_OR_1,
    3009         3573 :            std::make_unique<kf_atomic_fetch_op> (BIT_IOR_EXPR));
    3010         3573 :   kfm.add (BUILT_IN_ATOMIC_FETCH_OR_2,
    3011         3573 :            std::make_unique<kf_atomic_fetch_op> (BIT_IOR_EXPR));
    3012         3573 :   kfm.add (BUILT_IN_ATOMIC_FETCH_OR_4,
    3013         3573 :            std::make_unique<kf_atomic_fetch_op> (BIT_IOR_EXPR));
    3014         3573 :   kfm.add (BUILT_IN_ATOMIC_FETCH_OR_8,
    3015         3573 :            std::make_unique<kf_atomic_fetch_op> (BIT_IOR_EXPR));
    3016         3573 :   kfm.add (BUILT_IN_ATOMIC_FETCH_OR_16,
    3017         3573 :            std::make_unique<kf_atomic_fetch_op> (BIT_IOR_EXPR));
    3018         3573 : }
    3019              : 
    3020              : /* Handle calls to the various IFN_UBSAN_* with no return value.
    3021              :    For now, treat these as no-ops.  */
    3022              : 
    3023        10719 : class kf_ubsan_noop : public internal_known_function
    3024              : {
    3025              : };
    3026              : 
    3027              : /* Handle calls to the various __builtin___ubsan_handle_*.
    3028              :    These can return, but continuing after such a return
    3029              :    isn't likely to be interesting to the user of the analyzer.
    3030              :    Hence we terminate the analysis path at one of these calls.  */
    3031              : 
    3032         3573 : class kf_ubsan_handler : public internal_known_function
    3033              : {
    3034           12 :   void impl_call_post (const call_details &cd) const final override
    3035              :   {
    3036           12 :     if (cd.get_ctxt ())
    3037           12 :       cd.get_ctxt ()->terminate_path ();
    3038           12 :   }
    3039              : };
    3040              : 
    3041              : static void
    3042         3573 : register_sanitizer_builtins (known_function_manager &kfm)
    3043              : {
    3044              :   /* Handle calls to the various IFN_UBSAN_* with no return value.
    3045              :      For now, treat these as no-ops.  */
    3046         3573 :   kfm.add (IFN_UBSAN_NULL,
    3047         3573 :            std::make_unique<kf_ubsan_noop> ());
    3048         3573 :   kfm.add (IFN_UBSAN_BOUNDS,
    3049         3573 :            std::make_unique<kf_ubsan_noop> ());
    3050         3573 :   kfm.add (IFN_UBSAN_PTR,
    3051         3573 :            std::make_unique<kf_ubsan_noop> ());
    3052              : 
    3053         3573 :   kfm.add (BUILT_IN_UBSAN_HANDLE_NONNULL_ARG,
    3054         3573 :            std::make_unique<kf_ubsan_handler> ());
    3055         3573 : }
    3056              : 
    3057              : /* Populate KFM with instances of known functions supported by the core of the
    3058              :    analyzer (as opposed to plugins).  */
    3059              : 
    3060              : void
    3061         3573 : register_known_functions (known_function_manager &kfm,
    3062              :                           region_model_manager &rmm)
    3063              : {
    3064              :   /* Debugging/test support functions, all  with a "__analyzer_" prefix.  */
    3065         3573 :   register_known_analyzer_functions (kfm);
    3066              : 
    3067              :   /* Internal fns the analyzer has known_functions for.  */
    3068         3573 :   {
    3069         3573 :     kfm.add (IFN_BUILTIN_EXPECT, std::make_unique<kf_expect> ());
    3070              :   }
    3071              : 
    3072              :   /* GCC built-ins that do not correspond to a function
    3073              :      in the standard library.  */
    3074         3573 :   {
    3075         3573 :     kfm.add (BUILT_IN_EXPECT, std::make_unique<kf_expect> ());
    3076         3573 :     kfm.add (BUILT_IN_EXPECT_WITH_PROBABILITY, std::make_unique<kf_expect> ());
    3077         3573 :     kfm.add (BUILT_IN_ALLOCA_WITH_ALIGN, std::make_unique<kf_alloca> ());
    3078         3573 :     kfm.add (BUILT_IN_STACK_RESTORE, std::make_unique<kf_stack_restore> ());
    3079         3573 :     kfm.add (BUILT_IN_STACK_SAVE, std::make_unique<kf_stack_save> ());
    3080              : 
    3081         3573 :     kfm.add (BUILT_IN_EH_POINTER, std::make_unique<kf_eh_pointer> ());
    3082              : 
    3083         3573 :     kfm.add(BUILT_IN_STRCASECMP, std::make_unique<kf_strcasecmp>());
    3084              : 
    3085         3573 :     register_atomic_builtins (kfm);
    3086         3573 :     register_sanitizer_builtins (kfm);
    3087         3573 :     register_varargs_builtins (kfm);
    3088              :   }
    3089              : 
    3090              :   /* Known builtins and C standard library functions
    3091              :      the analyzer has known functions for.  */
    3092         3573 :   {
    3093         3573 :     kfm.add ("atoi", std::make_unique<kf_atoi_family> ());
    3094         3573 :     kfm.add ("atol", std::make_unique<kf_atoi_family> ());
    3095         3573 :     kfm.add ("atoll", std::make_unique<kf_atoi_family> ());
    3096              : 
    3097         3573 :     kfm.add ("alloca", std::make_unique<kf_alloca> ());
    3098         3573 :     kfm.add ("__builtin_alloca", std::make_unique<kf_alloca> ());
    3099         3573 :     kfm.add ("calloc", std::make_unique<kf_calloc> ());
    3100         3573 :     kfm.add ("__builtin_calloc", std::make_unique<kf_calloc> ());
    3101         3573 :     kfm.add ("free", std::make_unique<kf_free> ());
    3102         3573 :     kfm.add ("__builtin_free", std::make_unique<kf_free> ());
    3103         3573 :     kfm.add ("malloc", std::make_unique<kf_malloc> ());
    3104         3573 :     kfm.add ("__builtin_malloc", std::make_unique<kf_malloc> ());
    3105         3573 :     kfm.add ("memcpy",
    3106         3573 :               std::make_unique<kf_memcpy_memmove> (kf_memcpy_memmove::KF_MEMCPY));
    3107         3573 :     kfm.add ("__builtin_memcpy",
    3108         3573 :               std::make_unique<kf_memcpy_memmove> (kf_memcpy_memmove::KF_MEMCPY));
    3109         3573 :     kfm.add ("__memcpy_chk", std::make_unique<kf_memcpy_memmove>
    3110         3573 :                               (kf_memcpy_memmove::KF_MEMCPY_CHK));
    3111         3573 :     kfm.add ("__builtin___memcpy_chk", std::make_unique<kf_memcpy_memmove>
    3112         3573 :                               (kf_memcpy_memmove::KF_MEMCPY_CHK));
    3113         3573 :     kfm.add ("memmove",
    3114         3573 :               std::make_unique<kf_memcpy_memmove> (kf_memcpy_memmove::KF_MEMMOVE));
    3115         3573 :     kfm.add ("__builtin_memmove",
    3116         3573 :               std::make_unique<kf_memcpy_memmove> (kf_memcpy_memmove::KF_MEMMOVE));
    3117         3573 :     kfm.add ("__memmove_chk", std::make_unique<kf_memcpy_memmove>
    3118         3573 :                               (kf_memcpy_memmove::KF_MEMMOVE_CHK));
    3119         3573 :     kfm.add ("__builtin___memmove_chk", std::make_unique<kf_memcpy_memmove>
    3120         3573 :                               (kf_memcpy_memmove::KF_MEMMOVE_CHK));
    3121         3573 :     kfm.add ("memset", std::make_unique<kf_memset> (false));
    3122         3573 :     kfm.add ("__builtin_memset", std::make_unique<kf_memset> (false));
    3123         3573 :     kfm.add ("__memset_chk", std::make_unique<kf_memset> (true));
    3124         3573 :     kfm.add ("__builtin___memset_chk", std::make_unique<kf_memset> (true));
    3125         3573 :     kfm.add ("realloc", std::make_unique<kf_realloc> ());
    3126         3573 :     kfm.add ("__builtin_realloc", std::make_unique<kf_realloc> ());
    3127         3573 :     kfm.add ("sprintf", std::make_unique<kf_sprintf> ());
    3128         3573 :     kfm.add ("__builtin_sprintf", std::make_unique<kf_sprintf> ());
    3129         3573 :     kfm.add ("strchr", std::make_unique<kf_strchr> ());
    3130         3573 :     kfm.add ("__builtin_strchr", std::make_unique<kf_strchr> ());
    3131         3573 :     kfm.add ("strcpy", std::make_unique<kf_strcpy> (2, false));
    3132         3573 :     kfm.add ("__builtin_strcpy", std::make_unique<kf_strcpy> (2, false));
    3133         3573 :     kfm.add ("__strcpy_chk", std::make_unique<kf_strcpy> (3, true));
    3134         3573 :     kfm.add ("__builtin___strcpy_chk", std::make_unique<kf_strcpy> (3, true));
    3135         3573 :     kfm.add ("strcat", std::make_unique<kf_strcat> (2, false));
    3136         3573 :     kfm.add ("__builtin_strcat", std::make_unique<kf_strcat> (2, false));
    3137         3573 :     kfm.add ("__strcat_chk", std::make_unique<kf_strcat> (3, true));
    3138         3573 :     kfm.add ("__builtin___strcat_chk", std::make_unique<kf_strcat> (3, true));
    3139         3573 :     kfm.add ("strdup", std::make_unique<kf_strdup> ());
    3140         3573 :     kfm.add ("__builtin_strdup", std::make_unique<kf_strdup> ());
    3141         3573 :     kfm.add ("strncpy", std::make_unique<kf_strncpy> ());
    3142         3573 :     kfm.add ("__builtin_strncpy", std::make_unique<kf_strncpy> ());
    3143         3573 :     kfm.add ("strndup", std::make_unique<kf_strndup> ());
    3144         3573 :     kfm.add ("__builtin_strndup", std::make_unique<kf_strndup> ());
    3145         3573 :     kfm.add ("strlen", std::make_unique<kf_strlen> ());
    3146         3573 :     kfm.add ("__builtin_strlen", std::make_unique<kf_strlen> ());
    3147         3573 :     kfm.add ("strstr", std::make_unique<kf_strstr> ());
    3148         3573 :     kfm.add ("__builtin_strstr", std::make_unique<kf_strstr> ());
    3149         3573 :     kfm.add("strcasecmp", std::make_unique<kf_strcasecmp>());
    3150         3573 :     kfm.add("__builtin_strcasecmp", std::make_unique<kf_strcasecmp>());
    3151              :   }
    3152              : 
    3153              :   /* Known POSIX functions, and some non-standard extensions.  */
    3154         3573 :   {
    3155         3573 :     kfm.add ("fopen", std::make_unique<kf_fopen> ());
    3156         3573 :     kfm.add ("getenv", std::make_unique<kf_getenv> ());
    3157         3573 :     kfm.add ("mkdtemp", std::make_unique<kf_mktemp_simple> (
    3158         3573 :                           kf_mktemp_family::outcome::null_ptr));
    3159         3573 :     kfm.add ("mkostemp", std::make_unique<kf_mkostemp> ());
    3160         3573 :     kfm.add ("mkostemps", std::make_unique<kf_mkostemps> ());
    3161         3573 :     kfm.add ("mkstemps", std::make_unique<kf_mkstemps> ());
    3162         3573 :     kfm.add ("mkstemp", std::make_unique<kf_mktemp_simple> (
    3163         3573 :                           kf_mktemp_family::outcome::fd));
    3164              :     /* TODO: Report mktemp as deprecated per MSC24-C
    3165              :        (https://wiki.sei.cmu.edu/confluence/x/hNYxBQ).  */
    3166         3573 :     kfm.add ("mktemp", std::make_unique<kf_mktemp_simple> (
    3167         3573 :                          kf_mktemp_family::outcome::modif_tmpl));
    3168         3573 :     kfm.add ("putenv", std::make_unique<kf_putenv> ());
    3169         3573 :     kfm.add ("strtok", std::make_unique<kf_strtok> (rmm));
    3170              : 
    3171         3573 :     register_known_fd_functions (kfm);
    3172         3573 :     register_known_file_functions (kfm);
    3173              :   }
    3174              : 
    3175              :   /* glibc functions.  */
    3176         3573 :   {
    3177         3573 :     kfm.add ("__errno_location", std::make_unique<kf_errno_location> ());
    3178         3573 :     kfm.add ("error", std::make_unique<kf_error> (3));
    3179         3573 :     kfm.add ("error_at_line", std::make_unique<kf_error> (5));
    3180              :     /* Variants of "error" and "error_at_line" seen by the
    3181              :        analyzer at -O0 (PR analyzer/115724).  */
    3182         3573 :     kfm.add ("__error_alias", std::make_unique<kf_error> (3));
    3183         3573 :     kfm.add ("__error_at_line_alias", std::make_unique<kf_error> (5));
    3184              :   }
    3185              : 
    3186              :   /* Other implementations of C standard library.  */
    3187         3573 :   {
    3188              :     /* According to PR 107807 comment #2, Solaris implements "errno"
    3189              :        like this:
    3190              :          extern int *___errno(void) __attribute__((__const__));
    3191              :          #define errno (*(___errno()))
    3192              :        and macOS like this:
    3193              :          extern int * __error(void);
    3194              :          #define errno (*__error())
    3195              :        and similarly __errno for newlib.
    3196              :        Add these as synonyms for "__errno_location".  */
    3197         3573 :     kfm.add ("___errno", std::make_unique<kf_errno_location> ());
    3198         3573 :     kfm.add ("__error", std::make_unique<kf_errno_location> ());
    3199         3573 :     kfm.add ("__errno", std::make_unique<kf_errno_location> ());
    3200         3573 :     kfm.add ("__get_errno_ptr", std::make_unique<kf_errno_location> ());
    3201              :   }
    3202              : 
    3203              :   /* Language-specific support functions.  */
    3204         3573 :   register_known_functions_lang_cp (kfm);
    3205              : 
    3206              :   /* Some C++ implementations use the std:: copies of these functions
    3207              :      from <cstdlib> etc for the C spellings of these headers (e.g. <stdlib.h>),
    3208              :      so we must match against these too.  */
    3209         3573 :   {
    3210         3573 :     kfm.add_std_ns ("atoi", std::make_unique<kf_atoi_family> ());
    3211         3573 :     kfm.add_std_ns ("atol", std::make_unique<kf_atoi_family> ());
    3212         3573 :     kfm.add_std_ns ("atoll", std::make_unique<kf_atoi_family> ());
    3213              : 
    3214         3573 :     kfm.add_std_ns ("malloc", std::make_unique<kf_malloc> ());
    3215         3573 :     kfm.add_std_ns ("free", std::make_unique<kf_free> ());
    3216         3573 :     kfm.add_std_ns ("realloc", std::make_unique<kf_realloc> ());
    3217         3573 :     kfm.add_std_ns ("calloc", std::make_unique<kf_calloc> ());
    3218         3573 :     kfm.add_std_ns
    3219         3573 :       ("memcpy",
    3220         3573 :        std::make_unique<kf_memcpy_memmove> (kf_memcpy_memmove::KF_MEMCPY));
    3221         3573 :     kfm.add_std_ns
    3222         3573 :       ("memmove",
    3223         3573 :        std::make_unique<kf_memcpy_memmove> (kf_memcpy_memmove::KF_MEMMOVE));
    3224         3573 :     kfm.add_std_ns ("memset", std::make_unique<kf_memset> (false));
    3225         3573 :     kfm.add_std_ns ("strcat", std::make_unique<kf_strcat> (2, false));
    3226         3573 :     kfm.add_std_ns ("strcpy", std::make_unique<kf_strcpy> (2, false));
    3227         3573 :     kfm.add_std_ns ("strlen", std::make_unique<kf_strlen> ());
    3228         3573 :     kfm.add_std_ns ("strncpy", std::make_unique<kf_strncpy> ());
    3229         3573 :     kfm.add_std_ns ("strtok", std::make_unique<kf_strtok> (rmm));
    3230              :   }
    3231         3573 : }
    3232              : 
    3233              : } // namespace ana
    3234              : 
    3235              : #endif /* #if ENABLE_ANALYZER */
        

Generated by: LCOV version 2.5.0-beta

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