LCOV - code coverage report
Current view: top level - gcc/analyzer - varargs.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 93.9 % 379 356
Test Date: 2026-12-05 16:10:02 Functions: 94.5 % 55 52
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Implementation of <stdarg.h> within analyzer.
       2              :    Copyright (C) 2022-2026 Free Software Foundation, Inc.
       3              :    Contributed by David Malcolm <dmalcolm@redhat.com>.
       4              : 
       5              : This file is part of GCC.
       6              : 
       7              : GCC is free software; you can redistribute it and/or modify it
       8              : under the terms of the GNU General Public License as published by
       9              : the Free Software Foundation; either version 3, or (at your option)
      10              : any later version.
      11              : 
      12              : GCC is distributed in the hope that it will be useful, but
      13              : WITHOUT ANY WARRANTY; without even the implied warranty of
      14              : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15              : General Public License for more details.
      16              : 
      17              : You should have received a copy of the GNU General Public License
      18              : along with GCC; see the file COPYING3.  If not see
      19              : <http://www.gnu.org/licenses/>.  */
      20              : 
      21              : #include "analyzer/common.h"
      22              : 
      23              : #include "analyzer/analyzer-logging.h"
      24              : #include "analyzer/sm.h"
      25              : #include "analyzer/pending-diagnostic.h"
      26              : #include "analyzer/call-string.h"
      27              : #include "analyzer/program-point.h"
      28              : #include "analyzer/store.h"
      29              : #include "analyzer/region-model.h"
      30              : #include "analyzer/program-state.h"
      31              : #include "analyzer/checker-path.h"
      32              : #include "analyzer/supergraph.h"
      33              : #include "analyzer/diagnostic-manager.h"
      34              : #include "analyzer/exploded-graph.h"
      35              : #include "analyzer/call-details.h"
      36              : 
      37              : #if ENABLE_ANALYZER
      38              : 
      39              : namespace ana {
      40              : 
      41              : /* Implementation of <stdarg.h> within analyzer.
      42              : 
      43              :    Objectives:
      44              :    - detection of interprocedural type errors involving va_arg
      45              :    - tracking of symbolic values interprocedurally from variadic call
      46              :      through to va_arg unpacking
      47              :    - detection of missing va_end
      48              :    - detection of va_arg outside of a va_start/va_end pair
      49              :    - detection of uses of a va_list after the frame in containing the
      50              :      va_start has returned
      51              : 
      52              :    The analyzer runs *before* the "stdarg" and "lower_vaarg" gimple
      53              :    passes, which have target-dependent effects.
      54              : 
      55              :    This file implements a state machine on svalues for tracking when
      56              :    va_start has been called, so that we can detect missing va_end,
      57              :    and misplaced va_arg, etc.
      58              :    To do this requires an svalue that can have state, so we implement va_start
      59              :    by creating a stack-allocated region, and use a pointer to that region
      60              :    as the svalue that has state.
      61              : 
      62              :    We call this stack-allocated region the "impl_reg".  Allocating it on
      63              :    the stack ensures that it is invalidated when the frame containing
      64              :    the va_start returns, leading to
      65              :    -Wanalyzer-use-of-pointer-in-stale-stack-frame on attempts to use such
      66              :    a va_list.
      67              : 
      68              :    To track svalues from variadic calls interprocedurally, we implement
      69              :    variadic arguments via new child regions of the callee's frame_region,
      70              :    var_arg_region, each one representing a storage slot for one of the
      71              :    variadic arguments, accessed by index.
      72              : 
      73              :    We have:
      74              : 
      75              :     stack frame:
      76              :       va_list: &impl_reg at start, then zero fill
      77              :       'impl_reg': pointer to next var_arg_region
      78              :       var_arg_region for arg 0
      79              :       ...
      80              :       var_arg_region for arg N-1
      81              : 
      82              :    Hence given test_1 in stdarg-1.c, at the call to:
      83              : 
      84              :      __analyzer_called_by_test_1 (int placeholder, ...);
      85              : 
      86              :    here:
      87              : 
      88              :      __analyzer_called_by_test_1 (42, "foo", 1066, '@');
      89              : 
      90              :    we push this frame for the called function:
      91              :      clusters within frame: ‘__analyzer_called_by_test_1’@2
      92              :        cluster for: placeholder: (int)42
      93              :        cluster for: VAR_ARG_REG(frame: ‘__analyzer_called_by_test_1’@2, arg_idx: 0): &"foo" (TOUCHED)
      94              :        cluster for: VAR_ARG_REG(frame: ‘__analyzer_called_by_test_1’@2, arg_idx: 1): (int)1066 (TOUCHED)
      95              :        cluster for: VAR_ARG_REG(frame: ‘__analyzer_called_by_test_1’@2, arg_idx: 2): (int)64 (TOUCHED)
      96              :    where the called function's frame has been populated with both the value
      97              :    of the regular argument "placeholder", and with values for 3 variadic
      98              :    arguments.
      99              : 
     100              :    At the call to
     101              :       va_start (ap, placeholder);
     102              :    we allocate a region ALLOCA_REGION for ap to point to, populate that
     103              :    region with the address of variadic argument 0, and set sm-state of
     104              :    &ALLOCA_REGION to "started":
     105              :     clusters within frame: ‘__analyzer_called_by_test_1’@2
     106              :       cluster for: placeholder: (int)42
     107              :       cluster for: VAR_ARG_REG(frame: ‘__analyzer_called_by_test_1’@2, arg_idx: 0): &"foo" (TOUCHED)
     108              :       cluster for: VAR_ARG_REG(frame: ‘__analyzer_called_by_test_1’@2, arg_idx: 1): (int)1066 (TOUCHED)
     109              :       cluster for: VAR_ARG_REG(frame: ‘__analyzer_called_by_test_1’@2, arg_idx: 2): (int)64 (TOUCHED)
     110              :       cluster for: ap: &ALLOCA_REGION
     111              :       cluster for: ALLOCA_REGION: &VAR_ARG_REG(frame: ‘__analyzer_called_by_test_1’@2, arg_idx: 0) (TOUCHED)
     112              :     va_list:
     113              :       0x4c83700: &ALLOCA_REGION: started
     114              : 
     115              :    At each call to
     116              :      va_arg (ap, TYPE);
     117              :    we can look within *ap, locate the region holding the next variadic
     118              :    argument to be extracted, extract the svalue, and advance the index
     119              :    by effectively updating *ap.
     120              : 
     121              :    At the va_end, we can set &ALLOCA_REGION's state to "ended".
     122              : 
     123              :    The various __builtin_va_* accept ap by pointer, so we have e.g.:
     124              : 
     125              :      __builtin_va_start (&ap, [...]);
     126              : 
     127              :    except for the 2nd param of __builtin_va_copy, where the type
     128              :    is already target-dependent (see the discussion of get_va_copy_arg
     129              :    below).  */
     130              : 
     131              : /* Get a tree for diagnostics.
     132              :    Typically we have "&ap", but it will make more sense to
     133              :    the user as just "ap", so strip off the ADDR_EXPR.  */
     134              : 
     135              : static tree
     136         1234 : get_va_list_diag_arg (tree va_list_tree)
     137              : {
     138         1234 :   if (TREE_CODE (va_list_tree) == ADDR_EXPR)
     139          998 :     va_list_tree = TREE_OPERAND (va_list_tree, 0);
     140         1234 :   return va_list_tree;
     141              : }
     142              : 
     143              : /* Extract a pointer from argument ARG_IDX of va_copy.
     144              : 
     145              :    builtin-types.def has:
     146              :      DEF_PRIMITIVE_TYPE (BT_VALIST_ARG, va_list_arg_type_node)
     147              : 
     148              :    and c_common_nodes_and_builtins initializes va_list_arg_type_node
     149              :    based on whether TREE_CODE (va_list_type_node) is of ARRAY_TYPE or
     150              :    not, giving either one or zero levels of indirection.
     151              : 
     152              :    Alternatively we could be dealing with __builtin_ms_va_copy or
     153              :    __builtin_sysv_va_copy.
     154              : 
     155              :    Handle this by looking at the types of the argument in question.  */
     156              : 
     157              : static const svalue *
     158          127 : get_va_copy_arg (const region_model *model,
     159              :                  region_model_context *ctxt,
     160              :                  const gcall &call,
     161              :                  unsigned arg_idx)
     162              : {
     163          127 :   tree arg = gimple_call_arg (&call, arg_idx);
     164          127 :   auto *rmm = model->get_manager ();
     165          127 :   const region *src_reg;
     166          127 :   if (TREE_CODE (TREE_TYPE (arg)) == POINTER_TYPE
     167          127 :       && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == ARRAY_TYPE)
     168              :     {
     169              :       /* va_list_arg_type_node is a pointer to a va_list.  */
     170          107 :       const svalue *arg_sval = model->get_rvalue (arg, ctxt);
     171          107 :       if (const svalue *cast = arg_sval->maybe_undo_cast ())
     172           16 :         if (cast->get_type () && POINTER_TYPE_P (cast->get_type ()))
     173              :           arg_sval = cast;
     174          107 :       src_reg = model->deref_rvalue (arg_sval, arg, ctxt);
     175              :     }
     176              :   else
     177              :     {
     178              :       /* va_list_arg_type_node is a va_list.
     179              :          Read the pointer out of the first pointer-sized chunk of
     180              :          ARG's region.  */
     181           20 :       src_reg = model->get_lvalue (arg, ctxt);
     182              :     }
     183              : 
     184          127 :   const region *cast_reg = rmm->get_cast_region (src_reg, ptr_type_node);
     185          127 :   const svalue *src_reg_sval = model->get_store_value (cast_reg, ctxt);
     186          127 :   if (const svalue *cast = src_reg_sval->maybe_undo_cast ())
     187            0 :     if (cast->get_type () && POINTER_TYPE_P (cast->get_type ()))
     188              :       src_reg_sval = cast;
     189          127 :   return src_reg_sval;
     190              : }
     191              : 
     192              : namespace {
     193              : 
     194              : /* A state machine for tracking the state of a va_list, so that
     195              :    we can enforce that each va_start is paired with a va_end,
     196              :    and va_arg only happens within a va_start/va_end pair.
     197              :    Specifically, this tracks the state of the &ALLOCA_BUFFER
     198              :    that va_start/va_copy allocate.  */
     199              : 
     200              : class va_list_state_machine : public state_machine
     201              : {
     202              : public:
     203              :   va_list_state_machine (logger *logger);
     204              : 
     205       508183 :   bool inherited_state_p () const final override { return false; }
     206              : 
     207              :   bool on_stmt (sm_context &sm_ctxt,
     208              :                 const gimple *stmt) const final override;
     209              : 
     210       506927 :   bool can_purge_p (state_t s) const final override
     211              :   {
     212       506927 :     return s != m_started;
     213              :   }
     214              : 
     215              :   std::unique_ptr<pending_diagnostic>
     216              :   on_leak (tree var,
     217              :            const program_state *old_state,
     218              :            const program_state *new_state) const final override;
     219              : 
     220              :   /* State for a va_list that is the result of a va_start or va_copy.  */
     221              :   state_t m_started;
     222              : 
     223              :   /* State for a va_list that has had va_end called on it.  */
     224              :   state_t m_ended;
     225              : 
     226              : private:
     227              :   void on_va_start (sm_context &sm_ctxt, const gcall &call) const;
     228              :   void on_va_copy (sm_context &sm_ctxt, const gcall &call) const;
     229              :   void on_va_arg (sm_context &sm_ctxt, const gcall &call) const;
     230              :   void on_va_end (sm_context &sm_ctxt, const gcall &call) const;
     231              :   void check_for_ended_va_list (sm_context &sm_ctxt,
     232              :                                 const svalue *arg,
     233              :                                 const char *usage_fnname) const;
     234              : };
     235              : 
     236              : /* va_list_state_machine's ctor.  */
     237              : 
     238         3659 : va_list_state_machine::va_list_state_machine (logger *logger)
     239              : : state_machine ("va_list", logger),
     240         7318 :   m_started (add_state ("started")),
     241         3659 :   m_ended (add_state ("ended"))
     242              : {
     243         3659 : }
     244              : 
     245              : /* Implementation of the various "va_*" functions for
     246              :    va_list_state_machine.  */
     247              : 
     248              : bool
     249       229583 : va_list_state_machine::on_stmt (sm_context &sm_ctxt,
     250              :                                 const gimple *stmt) const
     251              : {
     252       229583 :   if (const gcall *call_stmt = dyn_cast <const gcall *> (stmt))
     253              :     {
     254        49852 :       const gcall &call = *call_stmt;
     255              : 
     256        49852 :       if (gimple_call_internal_p (call_stmt)
     257        49852 :           && gimple_call_internal_fn (call_stmt) == IFN_VA_ARG)
     258              :         {
     259          727 :           on_va_arg (sm_ctxt, call);
     260          727 :           return false;
     261              :         }
     262              : 
     263        49125 :       if (tree callee_fndecl = sm_ctxt.get_fndecl_for_call (call))
     264        45953 :         if (fndecl_built_in_p (callee_fndecl, BUILT_IN_NORMAL)
     265        45953 :             && gimple_builtin_call_types_compatible_p (&call, callee_fndecl))
     266        22557 :           switch (DECL_UNCHECKED_FUNCTION_CODE (callee_fndecl))
     267              :             {
     268              :             default:
     269              :               break;
     270              : 
     271          365 :             case BUILT_IN_VA_START:
     272          365 :               on_va_start (sm_ctxt, call);
     273          365 :               break;
     274              : 
     275           53 :             case BUILT_IN_VA_COPY:
     276           53 :               on_va_copy (sm_ctxt, call);
     277           53 :               break;
     278              : 
     279          460 :             case BUILT_IN_VA_END:
     280          460 :               on_va_end (sm_ctxt, call);
     281          460 :               break;
     282              :             }
     283              :     }
     284              :   return false;
     285              : }
     286              : 
     287              : /* Get the svalue for which va_list_state_machine holds state on argument ARG_
     288              :    IDX to CALL.  */
     289              : 
     290              : static const svalue *
     291         1605 : get_stateful_arg (sm_context &sm_ctxt, const gcall &call, unsigned arg_idx)
     292              : {
     293         1605 :   tree ap = gimple_call_arg (&call, arg_idx);
     294         1605 :   if (ap
     295         1605 :       && POINTER_TYPE_P (TREE_TYPE (ap)))
     296              :     {
     297         1605 :       if (const program_state *new_state = sm_ctxt.get_new_program_state ())
     298              :         {
     299         1605 :           const region_model *new_model = new_state->m_region_model;
     300         1605 :           const svalue *ptr_sval = new_model->get_rvalue (ap, nullptr);
     301         1605 :           const region *reg = new_model->deref_rvalue (ptr_sval, ap, nullptr);
     302         1605 :           auto *rmm = new_model->get_manager ();
     303         1605 :           const region *cast_reg = rmm->get_cast_region (reg, ptr_type_node);
     304         1605 :           const svalue *impl_sval = new_model->get_store_value (cast_reg, nullptr);
     305         1605 :           if (const svalue *cast = impl_sval->maybe_undo_cast ())
     306            0 :             impl_sval = cast;
     307              :           return impl_sval;
     308              :         }
     309              :     }
     310              :   return nullptr;
     311              : }
     312              : 
     313              : /* Abstract class for diagnostics relating to va_list_state_machine.  */
     314              : 
     315            0 : class va_list_sm_diagnostic : public pending_diagnostic
     316              : {
     317              : public:
     318           74 :   bool subclass_equal_p (const pending_diagnostic &base_other) const override
     319              :   {
     320           74 :     const va_list_sm_diagnostic &other
     321              :       = (const va_list_sm_diagnostic &)base_other;
     322           74 :     return (m_ap_sval == other.m_ap_sval
     323           74 :             && same_tree_p (m_ap_tree, other.m_ap_tree));
     324              :   }
     325              : 
     326              :   bool
     327           96 :   describe_state_change (pretty_printer &pp,
     328              :                          const evdesc::state_change &change) override
     329              :   {
     330           96 :     if (const char *fnname = maybe_get_fnname (change))
     331              :       {
     332           96 :         pp_printf (&pp, "%qs called here", fnname);
     333           96 :         return true;
     334              :       }
     335              :     return false;
     336              :   }
     337              : 
     338              :   diagnostics::paths::event::meaning
     339            8 :   get_meaning_for_state_change (const evdesc::state_change &change)
     340              :     const final override
     341              :   {
     342            8 :     if (change.m_new_state == m_sm.m_started)
     343            8 :       return diagnostics::paths::event::meaning (diagnostics::paths::event::verb::acquire,
     344            8 :                                         diagnostics::paths::event::noun::resource);
     345            0 :     if (change.m_new_state == m_sm.m_ended)
     346            0 :       return diagnostics::paths::event::meaning (diagnostics::paths::event::verb::release,
     347            0 :                                         diagnostics::paths::event::noun::resource);
     348            0 :     return diagnostics::paths::event::meaning ();
     349              :   }
     350              : 
     351              : protected:
     352           74 :   va_list_sm_diagnostic (const va_list_state_machine &sm,
     353              :                          const svalue *ap_sval, tree ap_tree)
     354           74 :     : m_sm (sm), m_ap_sval (ap_sval), m_ap_tree (ap_tree)
     355              :   {}
     356              : 
     357          144 :   static const char *maybe_get_fnname (const evdesc::state_change &change)
     358              :   {
     359          144 :     if (change.m_event.m_stmt)
     360          144 :       if (const gcall *call = as_a <const gcall *> (change.m_event.m_stmt))
     361          144 :         if (tree callee_fndecl = gimple_call_fndecl (call))
     362              :           {
     363          144 :             if (fndecl_built_in_p (callee_fndecl, BUILT_IN_NORMAL))
     364          144 :               switch (DECL_UNCHECKED_FUNCTION_CODE (callee_fndecl))
     365              :                 {
     366           92 :                 case BUILT_IN_VA_START:
     367           92 :                   return "va_start";
     368           28 :                 case BUILT_IN_VA_COPY:
     369           28 :                   return "va_copy";
     370           24 :                 case BUILT_IN_VA_END:
     371           24 :                   return "va_end";
     372              :                 }
     373              :           }
     374              :     return nullptr;
     375              :   }
     376              : 
     377              :   const va_list_state_machine &m_sm;
     378              :   const svalue *m_ap_sval;
     379              :   tree m_ap_tree;
     380              : };
     381              : 
     382              : /* Concrete class for -Wanalyzer-va-list-use-after-va-end:
     383              :    complain about use of a va_list after va_end has been called on it.  */
     384              : 
     385            0 : class va_list_use_after_va_end : public va_list_sm_diagnostic
     386              : {
     387              : public:
     388           12 :   va_list_use_after_va_end (const va_list_state_machine &sm,
     389              :                             const svalue *ap_sval, tree ap_tree,
     390              :                             const char *usage_fnname)
     391              :   : va_list_sm_diagnostic (sm, ap_sval, ap_tree),
     392           12 :     m_usage_fnname (usage_fnname)
     393              :   {
     394              :   }
     395              : 
     396           24 :   int get_controlling_option () const final override
     397              :   {
     398           24 :     return OPT_Wanalyzer_va_list_use_after_va_end;
     399              :   }
     400              : 
     401              :   bool operator== (const va_list_use_after_va_end &other) const
     402              :   {
     403              :     return (va_list_sm_diagnostic::subclass_equal_p (other)
     404              :             && 0 == strcmp (m_usage_fnname, other.m_usage_fnname));
     405              :   }
     406              : 
     407           12 :   bool emit (diagnostic_emission_context &ctxt) final override
     408              :   {
     409           12 :     return ctxt.warn ("%qs after %qs", m_usage_fnname, "va_end");
     410              :   }
     411              : 
     412          388 :   const char *get_kind () const final override
     413              :   {
     414          388 :     return "va_list_use_after_va_end";
     415              :   }
     416              : 
     417              :   bool
     418           48 :   describe_state_change (pretty_printer &pp,
     419              :                          const evdesc::state_change &change) final override
     420              :   {
     421           48 :     if (change.m_new_state == m_sm.m_ended)
     422           24 :       m_va_end_event = change.m_event_id;
     423           48 :     return va_list_sm_diagnostic::describe_state_change (pp, change);
     424              :   }
     425              : 
     426              :   bool
     427           24 :   describe_final_event (pretty_printer &pp,
     428              :                         const evdesc::final_event &ev) final override
     429              :   {
     430           24 :     if (ev.m_expr)
     431              :       {
     432            0 :         if (m_va_end_event.known_p ())
     433            0 :           pp_printf (&pp,
     434              :                      "%qs on %qE after %qs at %@",
     435              :                      m_usage_fnname, ev.m_expr, "va_end", &m_va_end_event);
     436              :         else
     437            0 :           pp_printf (&pp,
     438              :                      "%qs on %qE after %qs",
     439              :                      m_usage_fnname, ev.m_expr, "va_end");
     440              :       }
     441              :     else
     442              :       {
     443           24 :         if (m_va_end_event.known_p ())
     444           24 :           pp_printf (&pp,
     445              :                      "%qs after %qs at %@",
     446              :                      m_usage_fnname, "va_end", &m_va_end_event);
     447              :         else
     448            0 :           pp_printf (&pp,
     449              :                      "%qs after %qs",
     450              :                      m_usage_fnname, "va_end");
     451              :       }
     452           24 :     return true;
     453              :   }
     454              : 
     455              : private:
     456              :   diagnostics::paths::event_id_t m_va_end_event;
     457              :   const char *m_usage_fnname;
     458              : };
     459              : 
     460              : /* Concrete class for -Wanalyzer-va-list-leak:
     461              :    complain about a va_list in the "started" state that doesn't get after
     462              :    va_end called on it.  */
     463              : 
     464              : class va_list_leak : public va_list_sm_diagnostic
     465              : {
     466              : public:
     467           62 :   va_list_leak (const va_list_state_machine &sm,
     468              :                 const svalue *ap_sval, tree ap_tree,
     469              :                 const program_state *final_state)
     470              :   : va_list_sm_diagnostic (sm, ap_sval, ap_tree),
     471           62 :     m_start_event_fnname (nullptr),
     472           62 :     m_final_state ()
     473              :   {
     474           62 :     if (final_state)
     475           62 :       m_final_state = std::make_unique<program_state> (*final_state);
     476           62 :   }
     477              : 
     478           86 :   int get_controlling_option () const final override
     479              :   {
     480           86 :     return OPT_Wanalyzer_va_list_leak;
     481              :   }
     482              : 
     483              :   bool operator== (const va_list_leak &other) const
     484              :   {
     485              :     return va_list_sm_diagnostic::subclass_equal_p (other);
     486              :   }
     487              : 
     488           24 :   bool emit (diagnostic_emission_context &ctxt) final override
     489              :   {
     490           24 :     return ctxt.warn ("missing call to %qs", "va_end");
     491              :   }
     492              : 
     493          822 :   const char *get_kind () const final override { return "va_list_leak"; }
     494              : 
     495              :   bool
     496           48 :   describe_state_change (pretty_printer &pp,
     497              :                          const evdesc::state_change &change) final override
     498              :   {
     499           48 :     if (change.m_new_state == m_sm.m_started)
     500              :       {
     501           48 :         m_start_event = change.m_event_id;
     502           48 :         m_start_event_fnname = maybe_get_fnname (change);
     503              :       }
     504           48 :     return va_list_sm_diagnostic::describe_state_change (pp, change);
     505              :   }
     506              : 
     507              :   bool
     508           48 :   describe_final_event (pretty_printer &pp,
     509              :                         const evdesc::final_event &ev) final override
     510              :   {
     511           48 :     if (ev.m_expr)
     512              :       {
     513           10 :         if (m_start_event.known_p () && m_start_event_fnname)
     514           10 :           pp_printf (&pp,
     515              :                      "missing call to %qs on %qE to match %qs at %@",
     516              :                      "va_end", ev.m_expr, m_start_event_fnname, &m_start_event);
     517              :         else
     518            0 :           pp_printf (&pp,
     519              :                      "missing call to %qs on %qE",
     520              :                      "va_end", ev.m_expr);
     521              :       }
     522              :     else
     523              :       {
     524           38 :         if (m_start_event.known_p () && m_start_event_fnname)
     525           38 :           pp_printf (&pp,
     526              :                      "missing call to %qs to match %qs at %@",
     527              :                      "va_end", m_start_event_fnname, &m_start_event);
     528              :         else
     529            0 :           pp_printf (&pp,
     530              :                      "missing call to %qs",
     531              :                      "va_end");
     532              :       }
     533           48 :     return true;
     534              :   }
     535              : 
     536              :   const program_state *
     537           24 :   get_final_state () const final override
     538              :   {
     539           24 :     return m_final_state.get ();
     540              :   }
     541              : 
     542              : private:
     543              :   diagnostics::paths::event_id_t m_start_event;
     544              :   const char *m_start_event_fnname;
     545              :   std::unique_ptr<program_state> m_final_state;
     546              : };
     547              : 
     548              : /* Update state machine for a "va_start" call.  */
     549              : 
     550              : void
     551          365 : va_list_state_machine::on_va_start (sm_context &sm_ctxt,
     552              :                                     const gcall &call) const
     553              : {
     554          365 :   const svalue *arg = get_stateful_arg (sm_ctxt, call, 0);
     555          365 :   if (arg)
     556              :     {
     557              :       /* Transition from start state to "started".  */
     558          365 :       if (sm_ctxt.get_state (arg) == m_start)
     559          365 :         sm_ctxt.set_next_state (arg, m_started);
     560              :     }
     561          365 : }
     562              : 
     563              : /* Complain if ARG is in the "ended" state.  */
     564              : 
     565              : void
     566          784 : va_list_state_machine::check_for_ended_va_list (sm_context &sm_ctxt,
     567              :                                                 const svalue *arg,
     568              :                                                 const char *usage_fnname) const
     569              : {
     570          784 :   if (sm_ctxt.get_state (arg) == m_ended)
     571           12 :     sm_ctxt.warn (arg,
     572              :                   std::make_unique<va_list_use_after_va_end>
     573           12 :                     (*this, arg, NULL_TREE, usage_fnname));
     574          784 : }
     575              : 
     576              : /* Get the svalue with associated va_list_state_machine state for
     577              :    ARG_IDX of CALL to va_copy, if SM_CTXT supports this,
     578              :    or nullptr otherwise.  */
     579              : 
     580              : static const svalue *
     581           53 : get_stateful_va_copy_arg (sm_context &sm_ctxt,
     582              :                           const gcall &call,
     583              :                           unsigned arg_idx)
     584              : {
     585           53 :   if (const program_state *new_state = sm_ctxt.get_new_program_state ())
     586              :     {
     587           53 :       const region_model *new_model = new_state->m_region_model;
     588           53 :       const svalue *arg = get_va_copy_arg (new_model, nullptr, call, arg_idx);
     589           53 :       return arg;
     590              :     }
     591              :   return nullptr;
     592              : }
     593              : 
     594              : /* Update state machine for a "va_copy" call.  */
     595              : 
     596              : void
     597           53 : va_list_state_machine::on_va_copy (sm_context &sm_ctxt,
     598              :                                    const gcall &call) const
     599              : {
     600           53 :   const svalue *src_arg = get_stateful_va_copy_arg (sm_ctxt, call, 1);
     601           53 :   if (src_arg)
     602           53 :     check_for_ended_va_list (sm_ctxt, src_arg, "va_copy");
     603              : 
     604           53 :   const svalue *dst_arg = get_stateful_arg (sm_ctxt, call, 0);
     605           53 :   if (dst_arg)
     606              :     {
     607              :       /* Transition from start state to "started".  */
     608           53 :       if (sm_ctxt.get_state (dst_arg) == m_start)
     609           53 :         sm_ctxt.set_next_state (dst_arg, m_started);
     610              :     }
     611           53 : }
     612              : 
     613              : /* Update state machine for a "va_arg" call.  */
     614              : 
     615              : void
     616          727 : va_list_state_machine::on_va_arg (sm_context &sm_ctxt,
     617              :                                   const gcall &call) const
     618              : {
     619          727 :   const svalue *arg = get_stateful_arg (sm_ctxt, call, 0);
     620          727 :   if (arg)
     621          727 :     check_for_ended_va_list (sm_ctxt, arg, "va_arg");
     622          727 : }
     623              : 
     624              : /* Update state machine for a "va_end" call.  */
     625              : 
     626              : void
     627          460 : va_list_state_machine::on_va_end (sm_context &sm_ctxt,
     628              :                                   const gcall &call) const
     629              : {
     630          460 :   const svalue *arg = get_stateful_arg (sm_ctxt, call, 0);
     631          460 :   if (arg)
     632              :     {
     633          460 :       state_t s = sm_ctxt.get_state (arg);
     634              :       /* Transition from "started" to "ended".  */
     635          460 :       if (s == m_started)
     636          415 :         sm_ctxt.set_next_state (arg, m_ended);
     637           45 :       else if (s == m_ended)
     638            4 :         check_for_ended_va_list (sm_ctxt, arg, "va_end");
     639              :     }
     640          460 : }
     641              : 
     642              : /* Implementation of state_machine::on_leak vfunc for va_list_state_machine
     643              :    (for complaining about leaks of values in state 'started').  */
     644              : 
     645              : std::unique_ptr<pending_diagnostic>
     646           62 : va_list_state_machine::on_leak (tree var,
     647              :                                 const program_state *,
     648              :                                 const program_state *new_state) const
     649              : {
     650           62 :   return std::make_unique<va_list_leak> (*this, nullptr, var, new_state);
     651              : }
     652              : 
     653              : } // anonymous namespace
     654              : 
     655              : /* Internal interface to this file. */
     656              : 
     657              : std::unique_ptr<state_machine>
     658         3659 : make_va_list_state_machine (logger *logger)
     659              : {
     660         3659 :   return std::make_unique<va_list_state_machine> (logger);
     661              : }
     662              : 
     663              : /* Handler for "__builtin_va_start".  */
     664              : 
     665         3659 : class kf_va_start : public known_function
     666              : {
     667              : public:
     668            0 :   bool matches_call_types_p (const call_details &) const final override
     669              :   {
     670            0 :     return true;
     671              :   }
     672              :   void impl_call_pre (const call_details &cd) const final override;
     673              : };
     674              : 
     675              : void
     676          542 : kf_va_start::impl_call_pre (const call_details &cd) const
     677              : {
     678          542 :   region_model *model = cd.get_model ();
     679          542 :   region_model_manager *mgr = cd.get_manager ();
     680          542 :   const svalue *out_ptr = cd.get_arg_svalue (0);
     681          542 :   const region *out_reg
     682          542 :     = model->deref_rvalue (out_ptr, cd.get_arg_tree (0), cd.get_ctxt ());
     683          542 :   const frame_region *frame = model->get_current_frame ();
     684              : 
     685              :   /* "*out_ptr = &IMPL_REGION;".  */
     686          542 :   const region *impl_reg = mgr->create_region_for_alloca (frame);
     687              : 
     688              :   /* We abuse the types here, since va_list_type isn't
     689              :      necessarily anything to do with a pointer.  */
     690          542 :   const svalue *ptr_to_impl_reg = mgr->get_ptr_svalue (ptr_type_node, impl_reg);
     691          542 :   model->zero_fill_region (out_reg, cd.get_ctxt ());
     692          542 :   model->set_value (out_reg, ptr_to_impl_reg, cd.get_ctxt ());
     693              : 
     694          542 :   if (model->get_stack_depth () > 1)
     695              :     {
     696              :       /* The interprocedural case: the frame containing the va_start call
     697              :          will have been populated with any variadic aruguments.
     698              :          Initialize IMPL_REGION with a ptr to var_arg_region 0.  */
     699          278 :       const region *init_var_arg_reg = mgr->get_var_arg_region (frame, 0);
     700          278 :       const svalue *ap_sval
     701          278 :         = mgr->get_ptr_svalue (ptr_type_node, init_var_arg_reg);
     702          278 :       model->set_value (impl_reg, ap_sval, cd.get_ctxt ());
     703              :     }
     704              :   else
     705              :     {
     706              :       /* The frame containing va_start is an entry-point to the analysis,
     707              :          so there won't be any specific var_arg_regions populated within it.  */
     708          264 :       model->mark_region_as_unknown (impl_reg, nullptr);
     709              :     }
     710          542 : }
     711              : 
     712              : /* Handler for "__builtin_va_copy".  */
     713              : 
     714         3659 : class kf_va_copy : public known_function
     715              : {
     716              : public:
     717            0 :   bool matches_call_types_p (const call_details &) const final override
     718              :   {
     719            0 :     return true;
     720              :   }
     721              :   void impl_call_pre (const call_details &cd) const final override;
     722              : };
     723              : 
     724              : void
     725           74 : kf_va_copy::impl_call_pre (const call_details &cd) const
     726              : {
     727           74 :   region_model *model = cd.get_model ();
     728           74 :   region_model_manager *mgr = cd.get_manager ();
     729           74 :   const svalue *out_dst_ptr = cd.get_arg_svalue (0);
     730           74 :   const svalue *in_impl_ptr
     731           74 :     = get_va_copy_arg (model, cd.get_ctxt (), cd.get_call_stmt (), 1);
     732           74 :   in_impl_ptr
     733           74 :     = model->check_for_poison (in_impl_ptr,
     734              :                                get_va_list_diag_arg (cd.get_arg_tree (1)),
     735              :                                nullptr,
     736              :                                cd.get_ctxt ());
     737              : 
     738           74 :   const region *out_dst_reg
     739           74 :     = model->deref_rvalue (out_dst_ptr, cd.get_arg_tree (0), cd.get_ctxt ());
     740              : 
     741              :   /* "*out_dst_ptr = &NEW_IMPL_REGION;".  */
     742           74 :   const region *new_impl_reg
     743           74 :     = mgr->create_region_for_alloca (model->get_current_frame ());
     744           74 :   const svalue *ptr_to_new_impl_reg
     745           74 :     = mgr->get_ptr_svalue (ptr_type_node, new_impl_reg);
     746           74 :   model->set_value (out_dst_reg, ptr_to_new_impl_reg, cd.get_ctxt ());
     747              : 
     748           74 :   const region *old_impl_reg = model->deref_rvalue (in_impl_ptr,
     749              :                                                     NULL_TREE,
     750              :                                                     cd.get_ctxt ());
     751           74 :   old_impl_reg = mgr->get_cast_region (old_impl_reg, ptr_type_node);
     752           74 :   new_impl_reg = mgr->get_cast_region (new_impl_reg, ptr_type_node);
     753              : 
     754              :   /* "(NEW_IMPL_REGION) = (OLD_IMPL_REGION);".  */
     755           74 :   const svalue *existing_sval
     756           74 :     = model->get_store_value (old_impl_reg, cd.get_ctxt ());
     757           74 :   model->set_value (new_impl_reg, existing_sval, cd.get_ctxt ());
     758           74 : }
     759              : 
     760              : /* Get the number of variadic arguments to CALLEE_FNDECL at CALL_STMT.  */
     761              : 
     762              : static int
     763           34 : get_num_variadic_arguments (tree callee_fndecl,
     764              :                             const gcall &call_stmt)
     765              : {
     766           34 :   int num_positional = 0;
     767           68 :   for (tree iter_parm = DECL_ARGUMENTS (callee_fndecl); iter_parm;
     768           34 :        iter_parm = DECL_CHAIN (iter_parm))
     769           34 :     num_positional++;
     770           34 :   return gimple_call_num_args (&call_stmt) - num_positional;
     771              : }
     772              : 
     773              : /* An abstract subclass of pending_diagnostic for diagnostics relating
     774              :    to bad va_arg invocations.
     775              : 
     776              :    This shows the number of variadic arguments at the call of interest.
     777              :    Ideally we'd also be able to highlight individual arguments, but
     778              :    that location information isn't generally available from the middle end.  */
     779              : 
     780              : class va_arg_diagnostic : public pending_diagnostic
     781              : {
     782              : public:
     783              :   /* Override of pending_diagnostic::add_call_event,
     784              :      adding a custom call_event subclass.  */
     785           42 :   void add_call_event (const exploded_edge &eedge,
     786              :                        const gcall &call_stmt,
     787              :                        checker_path &emission_path,
     788              :                        const state_transition_at_call *state_trans) override
     789              :   {
     790              :     /* As per call_event, but show the number of variadic arguments
     791              :        in the call.  */
     792            0 :     class va_arg_call_event : public call_event
     793              :     {
     794              :     public:
     795           34 :       va_arg_call_event (const exploded_edge &eedge,
     796              :                          const event_loc_info &loc_info,
     797              :                          int num_variadic_arguments)
     798              :       : call_event (eedge, loc_info, nullptr),
     799           34 :         m_num_variadic_arguments (num_variadic_arguments)
     800              :       {
     801              :       }
     802              : 
     803           68 :       void print_desc (pretty_printer &pp) const override
     804              :       {
     805           68 :         pp_printf_n (&pp,
     806           68 :                      m_num_variadic_arguments,
     807              :                      "calling %qE from %qE with %i variadic argument",
     808              :                      "calling %qE from %qE with %i variadic arguments",
     809              :                      get_callee_fndecl (),
     810              :                      get_caller_fndecl (),
     811           68 :                      m_num_variadic_arguments);
     812           68 :       }
     813              :     private:
     814              :       int m_num_variadic_arguments;
     815              :     };
     816              : 
     817           42 :     const frame_region *frame_reg = m_var_arg_reg->get_frame_region ();
     818           42 :     const exploded_node *dst_node = eedge.m_dest;
     819           42 :     if (dst_node->get_state ().m_region_model->get_current_frame ()
     820              :         == frame_reg)
     821              :       {
     822           34 :         int num_variadic_arguments
     823           34 :           = get_num_variadic_arguments (dst_node->get_function ()->decl,
     824           34 :                                         call_stmt);
     825           34 :         emission_path.add_event
     826           34 :           (std::make_unique<va_arg_call_event>
     827           34 :            (eedge,
     828           68 :             event_loc_info (eedge.m_src),
     829              :             num_variadic_arguments));
     830              :       }
     831              :     else
     832            8 :       pending_diagnostic::add_call_event (eedge, call_stmt, emission_path,
     833              :                                           state_trans);
     834           42 :   }
     835              : 
     836              : protected:
     837           51 :   va_arg_diagnostic (tree va_list_tree, const var_arg_region *var_arg_reg)
     838           51 :   : m_va_list_tree (va_list_tree), m_var_arg_reg (var_arg_reg)
     839              :   {}
     840              : 
     841           37 :   bool subclass_equal_p (const pending_diagnostic &base_other) const override
     842              :   {
     843           37 :     const va_arg_diagnostic &other = (const va_arg_diagnostic &)base_other;
     844           37 :     return (same_tree_p (m_va_list_tree, other.m_va_list_tree)
     845           37 :             && m_var_arg_reg == other.m_var_arg_reg);
     846              :   }
     847              : 
     848              :   /* Get the number of arguments consumed so far from the va_list
     849              :      (*before* this va_arg call).  */
     850          102 :   unsigned get_num_consumed () const
     851              :   {
     852          102 :     return m_var_arg_reg->get_index ();
     853              :   }
     854              : 
     855              :   /* Get a 1-based index of which variadic argument is being consumed.  */
     856           63 :   unsigned get_variadic_index_for_diagnostic () const
     857              :   {
     858           63 :     return get_num_consumed () + 1;
     859              :   }
     860              : 
     861              :   /* User-readable expr for the va_list argument to va_arg.  */
     862              :   tree m_va_list_tree;
     863              : 
     864              :   /* The region that the va_arg attempted to access.  */
     865              :   const var_arg_region *m_var_arg_reg;
     866              : };
     867              : 
     868              : /* A subclass of pending_diagnostic for complaining about a type mismatch
     869              :    between the result of:
     870              :      va_arg (AP);
     871              :    and the type of the argument that was passed to the variadic call.  */
     872              : 
     873              : class va_arg_type_mismatch : public va_arg_diagnostic
     874              : {
     875              : public:
     876           25 :   va_arg_type_mismatch (tree va_list_tree, const var_arg_region *var_arg_reg,
     877              :                         tree expected_type, tree actual_type)
     878              :   : va_arg_diagnostic (va_list_tree, var_arg_reg),
     879           25 :     m_expected_type (expected_type), m_actual_type (actual_type)
     880              :   {}
     881              : 
     882          498 :   const char *get_kind () const final override
     883              :   {
     884          498 :     return "va_arg_type_mismatch";
     885              :   }
     886              : 
     887           24 :   bool subclass_equal_p (const pending_diagnostic &base_other)
     888              :     const final override
     889              :   {
     890           24 :     if (!va_arg_diagnostic::subclass_equal_p (base_other))
     891              :       return false;
     892           21 :     const va_arg_type_mismatch &other
     893              :       = (const va_arg_type_mismatch &)base_other;
     894           21 :     return (same_tree_p (m_expected_type, other.m_expected_type)
     895           21 :             && same_tree_p (m_actual_type, other.m_actual_type));
     896              :   }
     897              : 
     898           42 :   int get_controlling_option () const final override
     899              :   {
     900           42 :     return OPT_Wanalyzer_va_arg_type_mismatch;
     901              :   }
     902              : 
     903           21 :   bool emit (diagnostic_emission_context &ctxt) final override
     904              :   {
     905              :     /* "CWE-686: Function Call With Incorrect Argument Type".  */
     906           21 :     ctxt.add_cwe (686);
     907           21 :     bool warned
     908           21 :       = ctxt.warn ("%<va_arg%> expected %qT but received %qT"
     909              :                    " for variadic argument %i of %qE",
     910              :                    m_expected_type, m_actual_type,
     911              :                    get_variadic_index_for_diagnostic (), m_va_list_tree);
     912           21 :     return warned;
     913              :   }
     914              : 
     915              :   bool
     916           42 :   describe_final_event (pretty_printer &pp,
     917              :                         const evdesc::final_event &) final override
     918              :   {
     919           42 :     pp_printf (&pp,
     920              :                "%<va_arg%> expected %qT but received %qT"
     921              :                " for variadic argument %i of %qE",
     922              :                m_expected_type, m_actual_type,
     923              :                get_variadic_index_for_diagnostic (),
     924              :                m_va_list_tree);
     925           42 :     return true;
     926              :   }
     927              : 
     928              : private:
     929              :   tree m_expected_type;
     930              :   tree m_actual_type;
     931              : };
     932              : 
     933              : /* A subclass of pending_diagnostic for complaining about a
     934              :      va_arg (AP);
     935              :    after all of the args in AP have been consumed.  */
     936              : 
     937              : class va_list_exhausted : public va_arg_diagnostic
     938              : {
     939              : public:
     940           26 :   va_list_exhausted (tree va_list_tree, const var_arg_region *var_arg_reg)
     941           26 :   : va_arg_diagnostic (va_list_tree, var_arg_reg)
     942              :   {}
     943              : 
     944          134 :   const char *get_kind () const final override
     945              :   {
     946          134 :     return "va_list_exhausted";
     947              :   }
     948              : 
     949           26 :   int get_controlling_option () const final override
     950              :   {
     951           26 :     return OPT_Wanalyzer_va_list_exhausted;
     952              :   }
     953              : 
     954           13 :   bool emit (diagnostic_emission_context &ctxt) final override
     955              :   {
     956              :     /* CWE-685: Function Call With Incorrect Number of Arguments.  */
     957           13 :     ctxt.add_cwe (685);
     958           13 :     bool warned = ctxt.warn ("%qE has no more arguments (%i consumed)",
     959              :                              m_va_list_tree, get_num_consumed ());
     960           13 :     return warned;
     961              :   }
     962              : 
     963              :   bool
     964           26 :   describe_final_event (pretty_printer &pp,
     965              :                         const evdesc::final_event &) final override
     966              :   {
     967           26 :     pp_printf (&pp,
     968              :                "%qE has no more arguments (%i consumed)",
     969              :                m_va_list_tree, get_num_consumed ());
     970           26 :     return true;
     971              :   }
     972              : };
     973              : 
     974              : static bool
     975            8 : representable_in_integral_type_p (const svalue &sval, const_tree type)
     976              : {
     977            8 :   gcc_assert (INTEGRAL_TYPE_P (type));
     978              : 
     979            8 :   if (tree cst = sval.maybe_get_constant ())
     980            8 :     return wi::fits_to_tree_p (wi::to_wide (cst), type);
     981              : 
     982              :   return true;
     983              : }
     984              : 
     985              : /* Return true if it's OK to copy ARG_SVAL from ARG_TYPE to LHS_TYPE via
     986              :    va_arg (where argument promotion has already happened).  */
     987              : 
     988              : static bool
     989          557 : va_arg_compatible_types_p (tree lhs_type, tree arg_type, const svalue &arg_sval)
     990              : {
     991          557 :   if (compat_types_p (arg_type, lhs_type))
     992              :     return true;
     993              : 
     994              :   /* It's OK if both types are integer types, where one is signed and the
     995              :      other type the corresponding unsigned type, when the value is
     996              :      representable in both types.  */
     997           29 :   if (INTEGRAL_TYPE_P (lhs_type)
     998           17 :       && INTEGRAL_TYPE_P (arg_type)
     999           10 :       && TYPE_UNSIGNED (lhs_type) != TYPE_UNSIGNED (arg_type)
    1000            4 :       && TYPE_PRECISION (lhs_type) == TYPE_PRECISION (arg_type)
    1001            4 :       && representable_in_integral_type_p (arg_sval, lhs_type)
    1002           33 :       && representable_in_integral_type_p (arg_sval, arg_type))
    1003              :     return true;
    1004              : 
    1005              :   /* It's OK if one type is a pointer to void and the other is a
    1006              :      pointer to a character type.
    1007              :      This is handled by compat_types_p.  */
    1008              : 
    1009              :   /* Otherwise the types are not compatible.  */
    1010              :   return false;
    1011              : }
    1012              : 
    1013              : /* If AP_SVAL is a pointer to a var_arg_region, return that var_arg_region.
    1014              :    Otherwise return nullptr.  */
    1015              : 
    1016              : static const var_arg_region *
    1017         1160 : maybe_get_var_arg_region (region_model *model,
    1018              :                           const svalue *ap_sval)
    1019              : {
    1020         1160 :   const region *reg = model->deref_rvalue (ap_sval, NULL_TREE, nullptr, false);
    1021         1160 :   return reg->dyn_cast_var_arg_region ();
    1022              : }
    1023              : 
    1024              : /* Attempt to extract a typed svalue from VAR_ARG_REG in MODEL, returning
    1025              :    nullptr if unsuccessful.
    1026              :    Normally the store has no conception of types; this relies on matching
    1027              :    the shape of how interprocedural arguments are recorded in frames so
    1028              :    that we can reconstruct the types of variadic arguments.  */
    1029              : 
    1030              : static const svalue *
    1031          583 : maybe_get_var_arg_svalue (region_model *model,
    1032              :                           const var_arg_region *var_arg_reg)
    1033              : {
    1034              :   /* A var_arg_region has no inherent size.
    1035              :    */
    1036          583 :   auto *cluster = model->get_store ()->get_cluster (var_arg_reg);
    1037          583 :   if (!cluster)
    1038              :     return nullptr;
    1039          557 :   auto &contents = cluster->get_bit_contents ();
    1040          557 :   auto *conc_bit_overlay = contents.dyn_cast_concrete_bit_overlay ();
    1041          557 :   if (!conc_bit_overlay)
    1042              :     return nullptr;
    1043              : 
    1044          557 :   return conc_bit_overlay->maybe_get_var_arg_svalue ();
    1045              : }
    1046              : 
    1047              : /* Handler for "__builtin_va_arg".  */
    1048              : 
    1049         3659 : class kf_va_arg : public internal_known_function
    1050              : {
    1051              : public:
    1052              :   void impl_call_pre (const call_details &cd) const final override;
    1053              : };
    1054              : 
    1055              : void
    1056         1160 : kf_va_arg::impl_call_pre (const call_details &cd) const
    1057              : {
    1058         1160 :   region_model_context *ctxt = cd.get_ctxt ();
    1059         1160 :   region_model *model = cd.get_model ();
    1060         1160 :   region_model_manager *mgr = cd.get_manager ();
    1061              : 
    1062         1160 :   const svalue *in_ptr = cd.get_arg_svalue (0);
    1063         1160 :   const region *ap_reg
    1064         1160 :     = model->deref_rvalue (in_ptr, cd.get_arg_tree (0), ctxt);
    1065              : 
    1066         1160 :   ap_reg = mgr->get_cast_region (ap_reg, ptr_type_node);
    1067              : 
    1068         1160 :   const svalue *ap_sval = model->get_store_value (ap_reg, ctxt);
    1069         1160 :   if (const svalue *cast = ap_sval->maybe_undo_cast ())
    1070            0 :     ap_sval = cast;
    1071              : 
    1072         1160 :   tree va_list_tree = get_va_list_diag_arg (cd.get_arg_tree (0));
    1073         1160 :   ap_sval = model->check_for_poison (ap_sval, va_list_tree, ap_reg, ctxt);
    1074              : 
    1075         1160 :   cd.set_any_lhs_with_defaults ();
    1076              : 
    1077         1160 :   if (!(ap_sval->get_type ()
    1078         1160 :         && POINTER_TYPE_P (ap_sval->get_type ())))
    1079          577 :     return;
    1080              : 
    1081         1160 :   const region *impl_reg
    1082         1160 :     = model->deref_rvalue (ap_sval, NULL_TREE, ctxt, false);
    1083         1160 :   impl_reg = mgr->get_cast_region (impl_reg, ptr_type_node);
    1084         1160 :   const svalue *old_impl_sval = model->get_store_value (impl_reg, ctxt);
    1085         1160 :   const var_arg_region *arg_reg = maybe_get_var_arg_region (model,
    1086         1160 :                                                           old_impl_sval);
    1087         1160 :   if (!arg_reg)
    1088              :     return;
    1089              : 
    1090          583 :   bool saw_problem = false;
    1091              : 
    1092          583 :   const frame_region *frame_reg = arg_reg->get_frame_region ();
    1093          583 :   unsigned next_arg_idx = arg_reg->get_index ();
    1094              : 
    1095          583 :   if (frame_reg->get_stack_depth () > 1)
    1096              :     {
    1097              :       /* The interprocedural case: the called frame will have been
    1098              :          populated with any variadic aruguments.
    1099              :          Attempt to extract arg_reg to cd's return region (which already
    1100              :          has a conjured_svalue), or warn if there's a problem
    1101              :          (incompatible types, or if we've run out of args).  */
    1102          583 :       if (const svalue *arg_sval = maybe_get_var_arg_svalue (model,
    1103              :                                                              arg_reg))
    1104              :         {
    1105          557 :           tree lhs_type = cd.get_lhs_type ();
    1106          557 :           tree arg_type = arg_sval->get_type ();
    1107          557 :           if (va_arg_compatible_types_p (lhs_type, arg_type, *arg_sval))
    1108          532 :             cd.maybe_set_lhs (arg_sval);
    1109              :           else
    1110              :             {
    1111           25 :               if (ctxt)
    1112           25 :                 ctxt->warn (std::make_unique <va_arg_type_mismatch>
    1113           25 :                             (va_list_tree,
    1114              :                              arg_reg,
    1115              :                              lhs_type,
    1116              :                              arg_type));
    1117              :               saw_problem = true;
    1118              :             }
    1119              :         }
    1120              :       else
    1121              :         {
    1122           26 :           if (ctxt)
    1123           26 :             ctxt->warn
    1124           26 :               (std::make_unique <va_list_exhausted> (va_list_tree,
    1125              :                                                      arg_reg));
    1126              :           saw_problem = true;
    1127              :         }
    1128              :     }
    1129              :   else
    1130              :     {
    1131              :       /* This frame is an entry-point to the analysis, so there won't be
    1132              :          any specific var_arg_regions populated within it.
    1133              :          We already have a conjured_svalue for the result, so leave
    1134              :          it untouched.  */
    1135            0 :       gcc_assert (frame_reg->get_stack_depth () == 1);
    1136              :     }
    1137              : 
    1138          583 :   if (saw_problem)
    1139              :     {
    1140              :       /* Set impl_reg to UNKNOWN to suppress further warnings.  */
    1141           51 :       const svalue *new_ap_sval
    1142           51 :         = mgr->get_or_create_unknown_svalue (impl_reg->get_type ());
    1143           51 :       model->set_value (impl_reg, new_ap_sval, ctxt);
    1144              :     }
    1145              :   else
    1146              :     {
    1147              :       /* Update impl_reg to advance to the next arg.  */
    1148          532 :       const region *next_var_arg_region
    1149          532 :         = mgr->get_var_arg_region (frame_reg, next_arg_idx + 1);
    1150          532 :       const svalue *new_ap_sval
    1151          532 :         = mgr->get_ptr_svalue (ptr_type_node, next_var_arg_region);
    1152          532 :       model->set_value (impl_reg, new_ap_sval, ctxt);
    1153              :     }
    1154              : }
    1155              : 
    1156              : /* Handler for "__builtin_va_end".  */
    1157              : 
    1158         3659 : class kf_va_end : public known_function
    1159              : {
    1160              : public:
    1161            0 :   bool matches_call_types_p (const call_details &) const
    1162              :   {
    1163            0 :     return true;
    1164              :   }
    1165              : };
    1166              : 
    1167              : /* Populate KFM with instances of known functions relating to varargs.  */
    1168              : 
    1169              : void
    1170         3659 : register_varargs_builtins (known_function_manager &kfm)
    1171              : {
    1172         3659 :   kfm.add (BUILT_IN_VA_START, std::make_unique<kf_va_start> ());
    1173         3659 :   kfm.add (BUILT_IN_VA_COPY, std::make_unique<kf_va_copy> ());
    1174         3659 :   kfm.add (IFN_VA_ARG, std::make_unique<kf_va_arg> ());
    1175         3659 :   kfm.add (BUILT_IN_VA_END, std::make_unique<kf_va_end> ());
    1176         3659 : }
    1177              : 
    1178              : } // namespace ana
    1179              : 
    1180              : #endif /* #if ENABLE_ANALYZER */
        

Generated by: LCOV version 2.6-beta

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