LCOV - code coverage report
Current view: top level - gcc/analyzer - sm-fd.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 94.9 % 1304 1237
Test Date: 2026-12-05 16:10:02 Functions: 99.2 % 122 121
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* A state machine for detecting misuses of POSIX file descriptor APIs.
       2              :    Copyright (C) 2019-2026 Free Software Foundation, Inc.
       3              :    Contributed by Immad Mir <mir@sourceware.org>.
       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 "diagnostics/event-id.h"
      24              : #include "stringpool.h"
      25              : #include "attribs.h"
      26              : 
      27              : #include "analyzer/analyzer-logging.h"
      28              : #include "analyzer/sm.h"
      29              : #include "analyzer/pending-diagnostic.h"
      30              : #include "analyzer/function-set.h"
      31              : #include "analyzer/analyzer-selftests.h"
      32              : #include "analyzer/call-string.h"
      33              : #include "analyzer/program-point.h"
      34              : #include "analyzer/store.h"
      35              : #include "analyzer/region-model.h"
      36              : #include "analyzer/program-state.h"
      37              : #include "analyzer/supergraph.h"
      38              : #include "analyzer/analyzer-language.h"
      39              : #include "analyzer/call-details.h"
      40              : #include "analyzer/call-info.h"
      41              : 
      42              : #if ENABLE_ANALYZER
      43              : 
      44              : namespace ana {
      45              : 
      46              : namespace {
      47              : 
      48              : /* An enum for distinguishing between three different access modes.  */
      49              : 
      50              : enum access_mode
      51              : {
      52              :   READ_WRITE,
      53              :   READ_ONLY,
      54              :   WRITE_ONLY
      55              : };
      56              : 
      57              : enum access_directions
      58              : {
      59              :   DIRS_READ_WRITE,
      60              :   DIRS_READ,
      61              :   DIRS_WRITE
      62              : };
      63              : 
      64              : /* An enum for distinguishing between dup, dup2 and dup3.  */
      65              : enum dup
      66              : {
      67              :   DUP_1,
      68              :   DUP_2,
      69              :   DUP_3
      70              : };
      71              : 
      72              : /* Enum for use by -Wanalyzer-fd-phase-mismatch.  */
      73              : 
      74              : enum expected_phase
      75              : {
      76              :   EXPECTED_PHASE_CAN_TRANSFER, /* can "read"/"write".  */
      77              :   EXPECTED_PHASE_CAN_BIND,
      78              :   EXPECTED_PHASE_CAN_LISTEN,
      79              :   EXPECTED_PHASE_CAN_ACCEPT,
      80              :   EXPECTED_PHASE_CAN_CONNECT
      81              : };
      82              : 
      83              : class fd_state_machine : public state_machine
      84              : {
      85              : public:
      86              :   fd_state_machine (logger *logger);
      87              : 
      88              :   bool
      89       517309 :   inherited_state_p () const final override
      90              :   {
      91       517309 :     return false;
      92              :   }
      93              : 
      94              :   state_machine::state_t
      95       516282 :   get_default_state (const svalue *sval) const final override
      96              :   {
      97       516282 :     if (tree cst = sval->maybe_get_constant ())
      98              :       {
      99       106158 :         if (TREE_CODE (cst) == INTEGER_CST)
     100              :           {
     101       104485 :             int val = TREE_INT_CST_LOW (cst);
     102       104485 :             if (val >= 0)
     103        98270 :               return m_constant_fd;
     104              :             else
     105         6215 :               return m_invalid;
     106              :           }
     107              :       }
     108       411797 :     return m_start;
     109              :   }
     110              : 
     111              :   bool on_stmt (sm_context &sm_ctxt,
     112              :                 const gimple *stmt) const final override;
     113              : 
     114              :   void on_condition (sm_context &sm_ctxt,
     115              :                      const svalue *lhs, const tree_code op,
     116              :                      const svalue *rhs) const final override;
     117              : 
     118              :   bool can_purge_p (state_t s) const final override;
     119              : 
     120              :   std::unique_ptr<pending_diagnostic>
     121              :   on_leak (tree var,
     122              :            const program_state *old_state,
     123              :            const program_state *new_state) const final override;
     124              : 
     125              :   bool is_unchecked_fd_p (state_t s) const;
     126              :   bool is_valid_fd_p (state_t s) const;
     127              :   bool is_socket_fd_p (state_t s) const;
     128              :   bool is_datagram_socket_fd_p (state_t s) const;
     129              :   bool is_stream_socket_fd_p (state_t s) const;
     130              :   bool is_closed_fd_p (state_t s) const;
     131              :   bool is_constant_fd_p (state_t s) const;
     132              :   bool is_readonly_fd_p (state_t s) const;
     133              :   bool is_writeonly_fd_p (state_t s) const;
     134              :   enum access_mode get_access_mode_from_flag (int flag) const;
     135              :   /* Function for one-to-one correspondence between valid
     136              :      and unchecked states.  */
     137              :   state_t valid_to_unchecked_state (state_t state) const;
     138              : 
     139              :   void mark_as_valid_fd (region_model *model,
     140              :                          sm_state_map *smap,
     141              :                          const svalue *fd_sval,
     142              :                          const extrinsic_state &ext_state) const;
     143              : 
     144              :   bool on_socket (const call_details &cd,
     145              :                   bool successful,
     146              :                   sm_context &sm_ctxt,
     147              :                   const extrinsic_state &ext_state) const;
     148              :   bool on_bind (const call_details &cd,
     149              :                 bool successful,
     150              :                 sm_context &sm_ctxt,
     151              :                 const extrinsic_state &ext_state) const;
     152              :   bool on_listen (const call_details &cd,
     153              :                   bool successful,
     154              :                   sm_context &sm_ctxt,
     155              :                   const extrinsic_state &ext_state) const;
     156              :   bool on_accept (const call_details &cd,
     157              :                   bool successful,
     158              :                   sm_context &sm_ctxt,
     159              :                   const extrinsic_state &ext_state) const;
     160              :   bool on_connect (const call_details &cd,
     161              :                    bool successful,
     162              :                    sm_context &sm_ctxt,
     163              :                    const extrinsic_state &ext_state) const;
     164              : 
     165              :   /* State for a constant file descriptor (>= 0) */
     166              :   state_t m_constant_fd;
     167              : 
     168              :   /* States representing a file descriptor that hasn't yet been
     169              :     checked for validity after opening, for three different
     170              :     access modes.  */
     171              :   state_t m_unchecked_read_write;
     172              : 
     173              :   state_t m_unchecked_read_only;
     174              : 
     175              :   state_t m_unchecked_write_only;
     176              : 
     177              :   /* States for representing a file descriptor that is known to be valid (>=
     178              :     0), for three different access modes.  */
     179              :   state_t m_valid_read_write;
     180              : 
     181              :   state_t m_valid_read_only;
     182              : 
     183              :   state_t m_valid_write_only;
     184              : 
     185              :   /* State for a file descriptor that is known to be invalid (< 0). */
     186              :   state_t m_invalid;
     187              : 
     188              :   /* State for a file descriptor that has been closed.  */
     189              :   state_t m_closed;
     190              : 
     191              :   /* States for FDs relating to socket APIs.  */
     192              : 
     193              :   /* Result of successful "socket" with SOCK_DGRAM.  */
     194              :   state_t m_new_datagram_socket;
     195              :   /* Result of successful "socket" with SOCK_STREAM.  */
     196              :   state_t m_new_stream_socket;
     197              :   /* Result of successful "socket" with unknown type.  */
     198              :   state_t m_new_unknown_socket;
     199              : 
     200              :   /* The above after a successful call to "bind".  */
     201              :   state_t m_bound_datagram_socket;
     202              :   state_t m_bound_stream_socket;
     203              :   state_t m_bound_unknown_socket;
     204              : 
     205              :   /* A bound socket after a successful call to "listen" (stream or unknown).  */
     206              :   state_t m_listening_stream_socket;
     207              : 
     208              :   /* (i) the new FD as a result of a successful call to "accept" on a
     209              :      listening socket (via a passive open), or
     210              :      (ii) an active socket after a successful call to "connect"
     211              :      (via an active open).  */
     212              :   state_t m_connected_stream_socket;
     213              : 
     214              :   /* State for a file descriptor that we do not want to track anymore . */
     215              :   state_t m_stop;
     216              : 
     217              :   /* Stashed constant values from the frontend.  These could be NULL_TREE.  */
     218              :   tree m_O_ACCMODE;
     219              :   tree m_O_RDONLY;
     220              :   tree m_O_WRONLY;
     221              :   tree m_SOCK_STREAM;
     222              :   tree m_SOCK_DGRAM;
     223              : 
     224              : private:
     225              :   void on_open (sm_context &sm_ctxt,
     226              :                 const gcall &call) const;
     227              :   void on_creat (sm_context &sm_ctxt,
     228              :                 const gcall &call) const;
     229              :   void on_close (sm_context &sm_ctxt,
     230              :                  const gcall &call) const;
     231              :   void on_read (sm_context &sm_ctxt,
     232              :                 const gcall &call, const tree callee_fndecl) const;
     233              :   void on_write (sm_context &sm_ctxt,
     234              :                  const gcall &call, const tree callee_fndecl) const;
     235              :   void check_for_open_fd (sm_context &sm_ctxt,
     236              :                           const gcall &call,
     237              :                           const tree callee_fndecl,
     238              :                           enum access_directions access_fn) const;
     239              : 
     240              :   void make_valid_transitions_on_condition (sm_context &sm_ctxt,
     241              :                                             const svalue *lhs) const;
     242              :   void make_invalid_transitions_on_condition (sm_context &sm_ctxt,
     243              :                                               const svalue *lhs) const;
     244              :   void check_for_fd_attrs (sm_context &sm_ctxt,
     245              :                            const gcall &call,
     246              :                            const tree callee_fndecl,
     247              :                            const char *attr_name,
     248              :                            access_directions fd_attr_access_dir) const;
     249              :   void check_for_dup (sm_context &sm_ctxt,
     250              :                       const gcall &call,
     251              :                       const tree callee_fndecl,
     252              :                       enum dup kind) const;
     253              : 
     254              :   state_t get_state_for_socket_type (const svalue *socket_type_sval) const;
     255              : 
     256              :   bool check_for_socket_fd (const call_details &cd,
     257              :                             bool successful,
     258              :                             sm_context &sm_ctxt,
     259              :                             const svalue *fd_sval,
     260              :                             state_t old_state,
     261              :                             bool *complained = nullptr) const;
     262              :   bool check_for_new_socket_fd (const call_details &cd,
     263              :                                 bool successful,
     264              :                                 sm_context &sm_ctxt,
     265              :                                 const svalue *fd_sval,
     266              :                                 state_t old_state,
     267              :                                 enum expected_phase expected_phase) const;
     268              : };
     269              : 
     270              : /* Base diagnostic class relative to fd_state_machine.  */
     271            0 : class fd_diagnostic : public pending_diagnostic
     272              : {
     273              : public:
     274          303 :   fd_diagnostic (const fd_state_machine &sm, tree arg) : m_sm (sm), m_arg (arg)
     275              :   {
     276              :   }
     277              : 
     278              :   bool
     279          165 :   subclass_equal_p (const pending_diagnostic &base_other) const override
     280              :   {
     281          165 :     return same_tree_p (m_arg, ((const fd_diagnostic &)base_other).m_arg);
     282              :   }
     283              : 
     284              :   bool
     285          380 :   describe_state_change (pretty_printer &pp,
     286              :                          const evdesc::state_change &change) override
     287              :   {
     288          380 :     if (change.m_old_state == m_sm.get_start_state ())
     289              :       {
     290          264 :         if (change.m_new_state == m_sm.m_unchecked_read_write
     291          264 :             || change.m_new_state == m_sm.m_valid_read_write)
     292              :           {
     293          104 :             pp_string (&pp, "opened here as read-write");
     294          104 :             return true;
     295              :           }
     296              : 
     297          160 :         if (change.m_new_state == m_sm.m_unchecked_read_only
     298          148 :             || change.m_new_state == m_sm.m_valid_read_only)
     299              :           {
     300           12 :             pp_string (&pp, "opened here as read-only");
     301           12 :             return true;
     302              :           }
     303              : 
     304          148 :         if (change.m_new_state == m_sm.m_unchecked_write_only
     305          134 :             || change.m_new_state == m_sm.m_valid_write_only)
     306              :           {
     307           14 :             pp_string (&pp, "opened here as write-only");
     308           14 :             return true;
     309              :           }
     310              : 
     311          134 :         if (change.m_new_state == m_sm.m_new_datagram_socket)
     312              :           {
     313           18 :             pp_string (&pp, "datagram socket created here");
     314           18 :             return true;
     315              :           }
     316              : 
     317          116 :         if (change.m_new_state == m_sm.m_new_stream_socket)
     318              :           {
     319           26 :             pp_string (&pp, "stream socket created here");
     320           26 :             return true;
     321              :           }
     322              : 
     323           90 :         if (change.m_new_state == m_sm.m_new_unknown_socket
     324           20 :             || change.m_new_state == m_sm.m_connected_stream_socket)
     325              :           {
     326           80 :             pp_string (&pp, "socket created here");
     327           80 :             return true;
     328              :           }
     329              :       }
     330              : 
     331          126 :     if (change.m_new_state == m_sm.m_bound_datagram_socket)
     332              :       {
     333            4 :         pp_string (&pp, "datagram socket bound here");
     334            4 :         return true;
     335              :       }
     336              : 
     337          122 :     if (change.m_new_state == m_sm.m_bound_stream_socket)
     338              :       {
     339            4 :         pp_string (&pp, "stream socket bound here");
     340            4 :         return true;
     341              :       }
     342              : 
     343          118 :     if (change.m_new_state == m_sm.m_bound_unknown_socket
     344          108 :         || change.m_new_state == m_sm.m_connected_stream_socket)
     345              :       {
     346           10 :         pp_string (&pp, "socket bound here");
     347           10 :         return true;
     348              :       }
     349              : 
     350          108 :     if (change.m_new_state == m_sm.m_listening_stream_socket)
     351              :       {
     352            8 :         pp_printf (&pp,
     353              :                    "stream socket marked as passive here via %qs",
     354              :                    "listen");
     355            8 :         return true;
     356              :       }
     357              : 
     358          100 :     if (change.m_new_state == m_sm.m_closed)
     359              :       {
     360            0 :         pp_string (&pp, "closed here");
     361            0 :         return true;
     362              :       }
     363              : 
     364          208 :     if (m_sm.is_unchecked_fd_p (change.m_old_state)
     365          200 :         && m_sm.is_valid_fd_p (change.m_new_state))
     366              :       {
     367           92 :         if (change.m_expr)
     368           92 :           pp_printf (&pp,
     369              :                      "assuming %qE is a valid file descriptor (>= 0)",
     370              :                      change.m_expr);
     371              :         else
     372            0 :           pp_string (&pp, "assuming a valid file descriptor");
     373              :         return true;
     374              :       }
     375              : 
     376          396 :     if (m_sm.is_unchecked_fd_p (change.m_old_state)
     377            8 :         && change.m_new_state == m_sm.m_invalid)
     378              :       {
     379            8 :         if (change.m_expr)
     380            8 :           pp_printf (&pp,
     381              :                      "assuming %qE is an invalid file descriptor (< 0)",
     382              :                      change.m_expr);
     383              :         else
     384            0 :           pp_string (&pp, "assuming an invalid file descriptor");
     385              :         return true;
     386              :       }
     387              : 
     388              :     return false;
     389              :   }
     390              : 
     391              :   diagnostics::paths::event::meaning
     392           96 :   get_meaning_for_state_change (
     393              :       const evdesc::state_change &change) const final override
     394              :   {
     395           96 :     using event = diagnostics::paths::event;
     396           96 :     if (change.m_old_state == m_sm.get_start_state ()
     397           96 :         && (m_sm.is_unchecked_fd_p (change.m_new_state)
     398           24 :             || change.m_new_state == m_sm.m_new_datagram_socket
     399           22 :             || change.m_new_state == m_sm.m_new_stream_socket
     400           20 :             || change.m_new_state == m_sm.m_new_unknown_socket))
     401           48 :       return event::meaning (event::verb::acquire,
     402           48 :                              event::noun::resource);
     403           48 :     if (change.m_new_state == m_sm.m_closed)
     404           24 :       return event::meaning (event::verb::release,
     405           24 :                              event::noun::resource);
     406           24 :     return event::meaning ();
     407              :   }
     408              : 
     409              : protected:
     410              :   const fd_state_machine &m_sm;
     411              :   tree m_arg;
     412              : };
     413              : 
     414            0 : class fd_param_diagnostic : public fd_diagnostic
     415              : {
     416              : public:
     417           12 :   fd_param_diagnostic (const fd_state_machine &sm, tree arg, tree callee_fndecl,
     418              :                        const char *attr_name, int arg_idx)
     419           12 :       : fd_diagnostic (sm, arg), m_callee_fndecl (callee_fndecl),
     420           12 :         m_attr_name (attr_name), m_arg_idx (arg_idx)
     421              :   {
     422              :   }
     423              : 
     424          126 :   fd_param_diagnostic (const fd_state_machine &sm, tree arg, tree callee_fndecl)
     425          126 :       : fd_diagnostic (sm, arg), m_callee_fndecl (callee_fndecl),
     426          126 :         m_attr_name (nullptr), m_arg_idx (-1)
     427              :   {
     428              :   }
     429              : 
     430              :   bool
     431          122 :   subclass_equal_p (const pending_diagnostic &base_other) const override
     432              :   {
     433          122 :     const fd_param_diagnostic &sub_other
     434              :         = (const fd_param_diagnostic &)base_other;
     435          122 :     return (same_tree_p (m_arg, sub_other.m_arg)
     436          122 :             && same_tree_p (m_callee_fndecl, sub_other.m_callee_fndecl)
     437          122 :             && m_arg_idx == sub_other.m_arg_idx
     438          244 :             && ((m_attr_name)
     439           12 :                     ? (strcmp (m_attr_name, sub_other.m_attr_name) == 0)
     440          122 :                     : true));
     441              :   }
     442              : 
     443              :   void
     444           60 :   inform_filedescriptor_attribute (access_directions fd_dir)
     445              :   {
     446              : 
     447           60 :     if (m_attr_name)
     448           12 :       switch (fd_dir)
     449              :         {
     450            6 :         case DIRS_READ_WRITE:
     451           12 :           inform (DECL_SOURCE_LOCATION (m_callee_fndecl),
     452              :                   "argument %d of %qD must be an open file descriptor, due to "
     453              :                   "%<__attribute__((%s(%d)))%>",
     454            6 :                   m_arg_idx + 1, m_callee_fndecl, m_attr_name, m_arg_idx + 1);
     455            6 :           break;
     456            3 :         case DIRS_WRITE:
     457            6 :           inform (DECL_SOURCE_LOCATION (m_callee_fndecl),
     458              :                   "argument %d of %qD must be a readable file descriptor, due "
     459              :                   "to %<__attribute__((%s(%d)))%>",
     460            3 :                   m_arg_idx + 1, m_callee_fndecl, m_attr_name, m_arg_idx + 1);
     461            3 :           break;
     462            3 :         case DIRS_READ:
     463            6 :           inform (DECL_SOURCE_LOCATION (m_callee_fndecl),
     464              :                   "argument %d of %qD must be a writable file descriptor, due "
     465              :                   "to %<__attribute__((%s(%d)))%>",
     466            3 :                   m_arg_idx + 1, m_callee_fndecl, m_attr_name, m_arg_idx + 1);
     467            3 :           break;
     468              :         }
     469           60 :   }
     470              : 
     471              : protected:
     472              :   tree m_callee_fndecl;
     473              :   const char *m_attr_name;
     474              :   /* ARG_IDX is 0-based.  */
     475              :   int m_arg_idx;
     476              : };
     477              : 
     478              : class fd_leak : public fd_diagnostic
     479              : {
     480              : public:
     481          126 :   fd_leak (const fd_state_machine &sm, tree arg,
     482              :            const program_state *final_state)
     483              :   : fd_diagnostic (sm, arg),
     484          111 :     m_final_state ()
     485              :   {
     486          111 :     if (final_state)
     487          111 :       m_final_state = std::make_unique<program_state> (*final_state);
     488          111 :   }
     489              : 
     490              :   const char *
     491         2559 :   get_kind () const final override
     492              :   {
     493         2559 :     return "fd_leak";
     494              :   }
     495              : 
     496              :   int
     497          252 :   get_controlling_option () const final override
     498              :   {
     499          252 :     return OPT_Wanalyzer_fd_leak;
     500              :   }
     501              : 
     502              :   bool
     503          126 :   emit (diagnostic_emission_context &ctxt) final override
     504              :   {
     505              :     /*CWE-775: Missing Release of File Descriptor or Handle after Effective
     506              :       Lifetime
     507              :      */
     508          126 :     ctxt.add_cwe (775);
     509          126 :     if (m_arg)
     510           70 :       return ctxt.warn ("leak of file descriptor %qE", m_arg);
     511              :     else
     512           56 :       return ctxt.warn ("leak of file descriptor");
     513              :   }
     514              : 
     515              :   bool
     516          244 :   describe_state_change (pretty_printer &pp,
     517              :                          const evdesc::state_change &change) final override
     518              :   {
     519          488 :     if (m_sm.is_unchecked_fd_p (change.m_new_state))
     520              :       {
     521           40 :         m_open_event = change.m_event_id;
     522           40 :         pp_string (&pp, "opened here");
     523           40 :         return true;
     524              :       }
     525              : 
     526          204 :     return fd_diagnostic::describe_state_change (pp, change);
     527              :   }
     528              : 
     529              :   bool
     530          252 :   describe_final_event (pretty_printer &pp,
     531              :                         const evdesc::final_event &ev) final override
     532              :   {
     533          252 :     if (m_open_event.known_p ())
     534              :       {
     535           40 :         if (ev.m_expr)
     536           40 :           pp_printf (&pp,
     537              :                      "%qE leaks here; was opened at %@",
     538              :                      ev.m_expr, &m_open_event);
     539              :         else
     540            0 :           pp_printf (&pp,
     541              :                      "leaks here; was opened at %@",
     542              :                      &m_open_event);
     543              :       }
     544              :     else
     545              :       {
     546          212 :         if (ev.m_expr)
     547          100 :           pp_printf (&pp, "%qE leaks here", ev.m_expr);
     548              :         else
     549          112 :           pp_string (&pp, "leaks here");
     550              :       }
     551          252 :     return true;
     552              :   }
     553              : 
     554              :   const program_state *
     555          126 :   get_final_state () const final override
     556              :   {
     557          126 :     return m_final_state.get ();
     558              :   }
     559              : 
     560              : private:
     561              :   diagnostics::paths::event_id_t m_open_event;
     562              :   std::unique_ptr<program_state> m_final_state;
     563              : };
     564              : 
     565            0 : class fd_access_mode_mismatch : public fd_param_diagnostic
     566              : {
     567              : public:
     568            6 :   fd_access_mode_mismatch (const fd_state_machine &sm, tree arg,
     569              :                            enum access_directions fd_dir,
     570              :                            const tree callee_fndecl, const char *attr_name,
     571              :                            int arg_idx)
     572              :       : fd_param_diagnostic (sm, arg, callee_fndecl, attr_name, arg_idx),
     573            6 :         m_fd_dir (fd_dir)
     574              : 
     575              :   {
     576              :   }
     577              : 
     578            6 :   fd_access_mode_mismatch (const fd_state_machine &sm, tree arg,
     579              :                            enum access_directions fd_dir,
     580              :                            const tree callee_fndecl)
     581            6 :       : fd_param_diagnostic (sm, arg, callee_fndecl), m_fd_dir (fd_dir)
     582              :   {
     583              :   }
     584              : 
     585              :   const char *
     586          195 :   get_kind () const final override
     587              :   {
     588          195 :     return "fd_access_mode_mismatch";
     589              :   }
     590              : 
     591              :   int
     592           24 :   get_controlling_option () const final override
     593              :   {
     594           24 :     return OPT_Wanalyzer_fd_access_mode_mismatch;
     595              :   }
     596              : 
     597              :   bool
     598           12 :   emit (diagnostic_emission_context &ctxt) final override
     599              :   {
     600           12 :     bool warned;
     601           12 :     switch (m_fd_dir)
     602              :       {
     603            5 :       case DIRS_READ:
     604            5 :         warned =  ctxt.warn ("%qE on read-only file descriptor %qE",
     605              :                              m_callee_fndecl, m_arg);
     606            5 :         break;
     607            7 :       case DIRS_WRITE:
     608            7 :         warned = ctxt.warn ("%qE on write-only file descriptor %qE",
     609              :                             m_callee_fndecl, m_arg);
     610            7 :         break;
     611            0 :       default:
     612            0 :         gcc_unreachable ();
     613              :       }
     614           12 :       if (warned)
     615           12 :         inform_filedescriptor_attribute (m_fd_dir);
     616           12 :       return warned;
     617              :   }
     618              : 
     619              :   bool
     620           24 :   describe_final_event (pretty_printer &pp,
     621              :                         const evdesc::final_event &) final override
     622              :   {
     623           24 :     switch (m_fd_dir)
     624              :       {
     625           10 :       case DIRS_READ:
     626           10 :         pp_printf (&pp,
     627              :                    "%qE on read-only file descriptor %qE",
     628              :                    m_callee_fndecl, m_arg);
     629           10 :         return true;
     630           14 :       case DIRS_WRITE:
     631           14 :         pp_printf (&pp,
     632              :                    "%qE on write-only file descriptor %qE",
     633              :                    m_callee_fndecl, m_arg);
     634           14 :         return true;
     635            0 :       default:
     636            0 :         gcc_unreachable ();
     637              :       }
     638              :   }
     639              : 
     640              : private:
     641              :   enum access_directions m_fd_dir;
     642              : };
     643              : 
     644            0 : class fd_double_close : public fd_diagnostic
     645              : {
     646              : public:
     647           39 :   fd_double_close (const fd_state_machine &sm, tree arg) : fd_diagnostic (sm, arg)
     648              :   {
     649              :   }
     650              : 
     651              :   const char *
     652          429 :   get_kind () const final override
     653              :   {
     654          429 :     return "fd_double_close";
     655              :   }
     656              : 
     657              :   int
     658           78 :   get_controlling_option () const final override
     659              :   {
     660           78 :     return OPT_Wanalyzer_fd_double_close;
     661              :   }
     662              :   bool
     663           39 :   emit (diagnostic_emission_context &ctxt) final override
     664              :   {
     665              :     // CWE-1341: Multiple Releases of Same Resource or Handle
     666           39 :     ctxt.add_cwe (1341);
     667           39 :     return ctxt.warn ("double %<close%> of file descriptor %qE", m_arg);
     668              :   }
     669              : 
     670              :   bool
     671          196 :   describe_state_change (pretty_printer &pp,
     672              :                          const evdesc::state_change &change) override
     673              :   {
     674          392 :     if (m_sm.is_unchecked_fd_p (change.m_new_state))
     675              :       {
     676           64 :         pp_string (&pp, "opened here");
     677           64 :         return true;
     678              :       }
     679              : 
     680          132 :     if (change.m_new_state == m_sm.m_closed)
     681              :       {
     682           78 :         m_first_close_event = change.m_event_id;
     683           78 :         pp_printf (&pp, "first %qs here", "close");
     684           78 :         return true;
     685              :       }
     686           54 :     return fd_diagnostic::describe_state_change (pp, change);
     687              :   }
     688              : 
     689              :   bool
     690           78 :   describe_final_event (pretty_printer &pp,
     691              :                         const evdesc::final_event &) final override
     692              :   {
     693           78 :     if (m_first_close_event.known_p ())
     694           78 :       pp_printf (&pp,
     695              :                  "second %qs here; first %qs was at %@",
     696              :                  "close", "close", &m_first_close_event);
     697              :     else
     698            0 :       pp_printf (&pp,
     699              :                  "second %qs here",
     700              :                  "close");
     701           78 :     return true;
     702              :   }
     703              : 
     704              : private:
     705              :   diagnostics::paths::event_id_t m_first_close_event;
     706              : };
     707              : 
     708            0 : class fd_use_after_close : public fd_param_diagnostic
     709              : {
     710              : public:
     711            3 :   fd_use_after_close (const fd_state_machine &sm, tree arg,
     712              :                       const tree callee_fndecl, const char *attr_name,
     713              :                       int arg_idx)
     714            3 :       : fd_param_diagnostic (sm, arg, callee_fndecl, attr_name, arg_idx)
     715              :   {
     716              :   }
     717              : 
     718           12 :   fd_use_after_close (const fd_state_machine &sm, tree arg,
     719              :                       const tree callee_fndecl)
     720           12 :       : fd_param_diagnostic (sm, arg, callee_fndecl)
     721              :   {
     722              :   }
     723              : 
     724              :   const char *
     725          127 :   get_kind () const final override
     726              :   {
     727          127 :     return "fd_use_after_close";
     728              :   }
     729              : 
     730              :   int
     731           22 :   get_controlling_option () const final override
     732              :   {
     733           22 :     return OPT_Wanalyzer_fd_use_after_close;
     734              :   }
     735              : 
     736              :   bool
     737            7 :   emit (diagnostic_emission_context &ctxt) final override
     738              :   {
     739            7 :     bool warned = ctxt.warn ("%qE on closed file descriptor %qE",
     740              :                              m_callee_fndecl, m_arg);
     741            7 :     if (warned)
     742            7 :       inform_filedescriptor_attribute (DIRS_READ_WRITE);
     743            7 :     return warned;
     744              :   }
     745              : 
     746              :   bool
     747           32 :   describe_state_change (pretty_printer &pp,
     748              :                          const evdesc::state_change &change) override
     749              :   {
     750           64 :     if (m_sm.is_unchecked_fd_p (change.m_new_state))
     751              :       {
     752           14 :         pp_string (&pp, "opened here");
     753           14 :         return true;
     754              :       }
     755              : 
     756           18 :     if (change.m_new_state == m_sm.m_closed)
     757              :       {
     758           14 :         m_first_close_event = change.m_event_id;
     759           14 :         pp_string (&pp, "closed here");
     760           14 :         return true;
     761              :       }
     762              : 
     763            4 :     return fd_diagnostic::describe_state_change (pp, change);
     764              :   }
     765              : 
     766              :   bool
     767           14 :   describe_final_event (pretty_printer &pp,
     768              :                         const evdesc::final_event &) final override
     769              :   {
     770           14 :     if (m_first_close_event.known_p ())
     771           14 :       pp_printf (&pp,
     772              :                  "%qE on closed file descriptor %qE;"
     773              :                  " %qs was at %@",
     774              :                  m_callee_fndecl, m_arg,
     775              :                  "close", &m_first_close_event);
     776              :     else
     777            0 :       pp_printf (&pp,
     778              :                  "%qE on closed file descriptor %qE",
     779              :                  m_callee_fndecl, m_arg);
     780           14 :     return true;
     781              :   }
     782              : 
     783              : private:
     784              :   diagnostics::paths::event_id_t m_first_close_event;
     785              : };
     786              : 
     787            0 : class fd_use_without_check : public fd_param_diagnostic
     788              : {
     789              : public:
     790            3 :   fd_use_without_check (const fd_state_machine &sm, tree arg,
     791              :                         const tree callee_fndecl, const char *attr_name,
     792              :                         int arg_idx)
     793            3 :       : fd_param_diagnostic (sm, arg, callee_fndecl, attr_name, arg_idx)
     794              :   {
     795              :   }
     796              : 
     797           54 :   fd_use_without_check (const fd_state_machine &sm, tree arg,
     798              :                         const tree callee_fndecl)
     799           54 :       : fd_param_diagnostic (sm, arg, callee_fndecl)
     800              :   {
     801              :   }
     802              : 
     803              :   const char *
     804          644 :   get_kind () const final override
     805              :   {
     806          644 :     return "fd_use_without_check";
     807              :   }
     808              : 
     809              :   int
     810           98 :   get_controlling_option () const final override
     811              :   {
     812           98 :     return OPT_Wanalyzer_fd_use_without_check;
     813              :   }
     814              : 
     815              :   bool
     816           41 :   emit (diagnostic_emission_context &ctxt) final override
     817              :   {
     818           41 :     bool warned = ctxt.warn ("%qE on possibly invalid file descriptor %qE",
     819              :                              m_callee_fndecl, m_arg);
     820           41 :     if (warned)
     821           41 :      inform_filedescriptor_attribute (DIRS_READ_WRITE);
     822           41 :     return warned;
     823              :   }
     824              : 
     825              :   bool
     826           64 :   describe_state_change (pretty_printer &pp,
     827              :                          const evdesc::state_change &change) override
     828              :   {
     829          128 :     if (m_sm.is_unchecked_fd_p (change.m_new_state))
     830              :       {
     831           56 :         m_first_open_event = change.m_event_id;
     832           56 :         pp_string (&pp, "opened here");
     833           56 :         return true;
     834              :       }
     835              : 
     836            8 :     return fd_diagnostic::describe_state_change (pp, change);
     837              :   }
     838              : 
     839              :   bool
     840           82 :   describe_final_event (pretty_printer &pp,
     841              :                         const evdesc::final_event &) final override
     842              :   {
     843           82 :     if (m_first_open_event.known_p ())
     844           56 :       pp_printf (&pp,
     845              :                  "%qE could be invalid: unchecked value from %@", m_arg,
     846              :                  &m_first_open_event);
     847              :     else
     848           26 :       pp_printf (&pp,
     849              :                  "%qE could be invalid", m_arg);
     850           82 :     return true;
     851              :   }
     852              : 
     853              : private:
     854              :   diagnostics::paths::event_id_t m_first_open_event;
     855              : };
     856              : 
     857              : /* Concrete pending_diagnostic subclass for -Wanalyzer-fd-phase-mismatch.  */
     858              : 
     859            0 : class fd_phase_mismatch : public fd_param_diagnostic
     860              : {
     861              : public:
     862           44 :   fd_phase_mismatch (const fd_state_machine &sm, tree arg,
     863              :                      const tree callee_fndecl,
     864              :                      state_machine::state_t actual_state,
     865              :                      enum expected_phase expected_phase)
     866              :   : fd_param_diagnostic (sm, arg, callee_fndecl),
     867           44 :     m_actual_state (actual_state),
     868           44 :     m_expected_phase (expected_phase)
     869              :   {
     870           44 :     gcc_assert (m_sm.is_socket_fd_p (actual_state));
     871           44 :     switch (expected_phase)
     872              :       {
     873            4 :       case EXPECTED_PHASE_CAN_TRANSFER:
     874            4 :         gcc_assert (actual_state == m_sm.m_new_stream_socket
     875              :                     || actual_state == m_sm.m_bound_stream_socket
     876              :                     || actual_state == m_sm.m_listening_stream_socket);
     877              :         break;
     878            8 :       case EXPECTED_PHASE_CAN_BIND:
     879            8 :         gcc_assert (actual_state == m_sm.m_bound_datagram_socket
     880              :                     || actual_state == m_sm.m_bound_stream_socket
     881              :                     || actual_state == m_sm.m_bound_unknown_socket
     882              :                     || actual_state == m_sm.m_connected_stream_socket
     883              :                     || actual_state == m_sm.m_listening_stream_socket);
     884              :         break;
     885           26 :       case EXPECTED_PHASE_CAN_LISTEN:
     886           26 :         gcc_assert (actual_state == m_sm.m_new_stream_socket
     887              :                     || actual_state == m_sm.m_new_unknown_socket
     888              :                     || actual_state == m_sm.m_connected_stream_socket);
     889              :         break;
     890            4 :       case EXPECTED_PHASE_CAN_ACCEPT:
     891            4 :         gcc_assert (actual_state == m_sm.m_new_stream_socket
     892              :                     || actual_state == m_sm.m_new_unknown_socket
     893              :                     || actual_state == m_sm.m_bound_stream_socket
     894              :                     || actual_state == m_sm.m_bound_unknown_socket
     895              :                     || actual_state == m_sm.m_connected_stream_socket);
     896              :         break;
     897            2 :       case EXPECTED_PHASE_CAN_CONNECT:
     898            2 :         gcc_assert (actual_state == m_sm.m_bound_datagram_socket
     899              :                     || actual_state == m_sm.m_bound_stream_socket
     900              :                     || actual_state == m_sm.m_bound_unknown_socket
     901              :                     || actual_state == m_sm.m_listening_stream_socket
     902              :                     || actual_state == m_sm.m_connected_stream_socket);
     903              :         break;
     904              :       }
     905           44 :   }
     906              : 
     907              :   const char *
     908          524 :   get_kind () const final override
     909              :   {
     910          524 :     return "fd_phase_mismatch";
     911              :   }
     912              : 
     913              :   bool
     914           44 :   subclass_equal_p (const pending_diagnostic &base_other) const final override
     915              :   {
     916           44 :     const fd_phase_mismatch &sub_other = (const fd_phase_mismatch &)base_other;
     917           44 :     if (!fd_param_diagnostic ::subclass_equal_p (sub_other))
     918              :       return false;
     919           44 :     return (m_actual_state == sub_other.m_actual_state
     920           44 :             && m_expected_phase == sub_other.m_expected_phase);
     921              :   }
     922              : 
     923              :   int
     924           67 :   get_controlling_option () const final override
     925              :   {
     926           67 :     return OPT_Wanalyzer_fd_phase_mismatch;
     927              :   }
     928              : 
     929              :   bool
     930           23 :   emit (diagnostic_emission_context &ctxt) final override
     931              :   {
     932              :     /* CWE-666: Operation on Resource in Wrong Phase of Lifetime.  */
     933           23 :     ctxt.add_cwe (666);
     934           23 :     return ctxt.warn ("%qE on file descriptor %qE in wrong phase",
     935           23 :                       m_callee_fndecl, m_arg);
     936              :   }
     937              : 
     938              :   bool
     939           46 :   describe_final_event (pretty_printer &pp,
     940              :                         const evdesc::final_event &) final override
     941              :   {
     942           46 :     switch (m_expected_phase)
     943              :       {
     944            6 :       case EXPECTED_PHASE_CAN_TRANSFER:
     945            6 :         {
     946            6 :           if (m_actual_state == m_sm.m_new_stream_socket)
     947              :             {
     948            2 :               pp_printf (&pp,
     949              :                          "%qE expects a stream socket to be connected via %qs"
     950              :                          " but %qE has not yet been bound",
     951              :                          m_callee_fndecl, "accept", m_arg);
     952            2 :               return true;
     953              :             }
     954            4 :           if (m_actual_state == m_sm.m_bound_stream_socket)
     955              :             {
     956            0 :               pp_printf (&pp,
     957              :                          "%qE expects a stream socket to be connected via %qs"
     958              :                          " but %qE is not yet listening",
     959              :                          m_callee_fndecl, "accept", m_arg);
     960            0 :               return true;
     961              :             }
     962            4 :           if (m_actual_state == m_sm.m_listening_stream_socket)
     963              :             {
     964            4 :               pp_printf (&pp,
     965              :                          "%qE expects a stream socket to be connected via"
     966              :                          " the return value of %qs"
     967              :                          " but %qE is listening; wrong file descriptor?",
     968              :                          m_callee_fndecl, "accept", m_arg);
     969            4 :               return true;
     970              :             }
     971              :         }
     972              :         break;
     973            8 :       case EXPECTED_PHASE_CAN_BIND:
     974            8 :         {
     975            8 :           if (m_actual_state == m_sm.m_bound_datagram_socket
     976            8 :               || m_actual_state == m_sm.m_bound_stream_socket
     977            8 :               || m_actual_state == m_sm.m_bound_unknown_socket)
     978              :             {
     979            2 :               pp_printf (&pp,
     980              :                          "%qE expects a new socket file descriptor"
     981              :                          " but %qE has already been bound",
     982              :                          m_callee_fndecl, m_arg);
     983            2 :               return true;
     984              :             }
     985            6 :           if (m_actual_state == m_sm.m_connected_stream_socket)
     986              :             {
     987            2 :               pp_printf (&pp,
     988              :                          "%qE expects a new socket file descriptor"
     989              :                          " but %qE is already connected",
     990              :                          m_callee_fndecl, m_arg);
     991            2 :               return true;
     992              :             }
     993            4 :           if (m_actual_state == m_sm.m_listening_stream_socket)
     994              :             {
     995            4 :               pp_printf (&pp,
     996              :                          "%qE expects a new socket file descriptor"
     997              :                          " but %qE is already listening",
     998              :                          m_callee_fndecl, m_arg);
     999            4 :               return true;
    1000              :             }
    1001              :         }
    1002              :         break;
    1003           26 :       case EXPECTED_PHASE_CAN_LISTEN:
    1004           26 :         {
    1005           26 :           if (m_actual_state == m_sm.m_new_stream_socket
    1006           20 :               || m_actual_state == m_sm.m_new_unknown_socket)
    1007              :             {
    1008           24 :               pp_printf (&pp,
    1009              :                          "%qE expects a bound stream socket file descriptor"
    1010              :                          " but %qE has not yet been bound",
    1011              :                          m_callee_fndecl, m_arg);
    1012           24 :               return true;
    1013              :             }
    1014            2 :           if (m_actual_state == m_sm.m_connected_stream_socket)
    1015              :             {
    1016            2 :               pp_printf (&pp,
    1017              :                          "%qE expects a bound stream socket file descriptor"
    1018              :                          " but %qE is connected",
    1019              :                          m_callee_fndecl, m_arg);
    1020            2 :               return true;
    1021              :             }
    1022              :         }
    1023              :         break;
    1024            4 :       case EXPECTED_PHASE_CAN_ACCEPT:
    1025            4 :         {
    1026            4 :           if (m_actual_state == m_sm.m_new_stream_socket
    1027            2 :               || m_actual_state == m_sm.m_new_unknown_socket)
    1028              :             {
    1029            2 :               pp_printf (&pp,
    1030              :                          "%qE expects a listening stream socket file descriptor"
    1031              :                          " but %qE has not yet been bound",
    1032              :                          m_callee_fndecl, m_arg);
    1033            2 :               return true;
    1034              :             }
    1035            2 :           if (m_actual_state == m_sm.m_bound_stream_socket
    1036            2 :               || m_actual_state == m_sm.m_bound_unknown_socket)
    1037              :             {
    1038            0 :               pp_printf (&pp,
    1039              :                          "%qE expects a listening stream socket file descriptor"
    1040              :                          " whereas %qE is bound but not yet listening",
    1041              :                          m_callee_fndecl, m_arg);
    1042            0 :               return true;
    1043              :             }
    1044            2 :           if (m_actual_state == m_sm.m_connected_stream_socket)
    1045              :             {
    1046            2 :               pp_printf (&pp,
    1047              :                          "%qE expects a listening stream socket file descriptor"
    1048              :                          " but %qE is connected",
    1049              :                          m_callee_fndecl, m_arg);
    1050            2 :               return true;
    1051              :             }
    1052              :         }
    1053              :         break;
    1054            2 :       case EXPECTED_PHASE_CAN_CONNECT:
    1055            2 :         {
    1056            2 :           if (m_actual_state == m_sm.m_bound_datagram_socket
    1057            2 :               || m_actual_state == m_sm.m_bound_stream_socket
    1058            0 :               || m_actual_state == m_sm.m_bound_unknown_socket)
    1059              :             {
    1060            2 :               pp_printf (&pp,
    1061              :                          "%qE expects a new socket file descriptor"
    1062              :                          " but %qE is bound",
    1063              :                          m_callee_fndecl, m_arg);
    1064            2 :               return true;
    1065              :             }
    1066              :           else
    1067              :             {
    1068            0 :               pp_printf (&pp,
    1069              :                          "%qE expects a new socket file descriptor",
    1070              :                          m_callee_fndecl);
    1071            0 :               return true;
    1072              :             }
    1073              :         }
    1074            0 :         break;
    1075              :       }
    1076            0 :     gcc_unreachable ();
    1077              :   }
    1078              : 
    1079              : private:
    1080              :   state_machine::state_t m_actual_state;
    1081              :   enum expected_phase m_expected_phase;
    1082              : };
    1083              : 
    1084              : /* Enum for use by -Wanalyzer-fd-type-mismatch.  */
    1085              : 
    1086              : enum expected_type
    1087              : {
    1088              :  EXPECTED_TYPE_SOCKET,
    1089              :  EXPECTED_TYPE_STREAM_SOCKET
    1090              : };
    1091              : 
    1092              : /* Concrete pending_diagnostic subclass for -Wanalyzer-fd-type-mismatch.  */
    1093              : 
    1094            0 : class fd_type_mismatch : public fd_param_diagnostic
    1095              : {
    1096              : public:
    1097           10 :   fd_type_mismatch (const fd_state_machine &sm, tree arg,
    1098              :                     const tree callee_fndecl,
    1099              :                     state_machine::state_t actual_state,
    1100              :                     enum expected_type expected_type)
    1101              :   : fd_param_diagnostic (sm, arg, callee_fndecl),
    1102           10 :     m_actual_state (actual_state),
    1103           10 :     m_expected_type (expected_type)
    1104              :   {
    1105              :   }
    1106              : 
    1107              :   const char *
    1108          113 :   get_kind () const final override
    1109              :   {
    1110          113 :     return "fd_type_mismatch";
    1111              :   }
    1112              : 
    1113              :   bool
    1114           10 :   subclass_equal_p (const pending_diagnostic &base_other) const final override
    1115              :   {
    1116           10 :     const fd_type_mismatch &sub_other = (const fd_type_mismatch &)base_other;
    1117           10 :     if (!fd_param_diagnostic ::subclass_equal_p (sub_other))
    1118              :       return false;
    1119           10 :     return (m_actual_state == sub_other.m_actual_state
    1120           10 :             && m_expected_type == sub_other.m_expected_type);
    1121              :   }
    1122              : 
    1123              :   int
    1124           15 :   get_controlling_option () const final override
    1125              :   {
    1126           15 :     return OPT_Wanalyzer_fd_type_mismatch;
    1127              :   }
    1128              : 
    1129              :   bool
    1130            5 :   emit (diagnostic_emission_context &ctxt) final override
    1131              :   {
    1132            5 :     switch (m_expected_type)
    1133              :       {
    1134            0 :       default:
    1135            0 :         gcc_unreachable ();
    1136            1 :       case EXPECTED_TYPE_SOCKET:
    1137            1 :         return ctxt.warn ("%qE on non-socket file descriptor %qE",
    1138            1 :                           m_callee_fndecl, m_arg);
    1139            4 :       case EXPECTED_TYPE_STREAM_SOCKET:
    1140            8 :         if (m_sm.is_datagram_socket_fd_p (m_actual_state))
    1141            4 :           return ctxt.warn ("%qE on datagram socket file descriptor %qE",
    1142            4 :                             m_callee_fndecl, m_arg);
    1143              :         else
    1144            0 :           return ctxt.warn ("%qE on non-stream-socket file descriptor %qE",
    1145            0 :                             m_callee_fndecl, m_arg);
    1146              :       }
    1147              :   }
    1148              : 
    1149              :   bool
    1150           10 :   describe_final_event (pretty_printer &pp,
    1151              :                         const evdesc::final_event &) final override
    1152              :   {
    1153           10 :     switch (m_expected_type)
    1154              :       {
    1155              :       default:
    1156              :         break;
    1157              :         gcc_unreachable ();
    1158           10 :       case EXPECTED_TYPE_SOCKET:
    1159           10 :       case EXPECTED_TYPE_STREAM_SOCKET:
    1160           10 :         if (!m_sm.is_socket_fd_p (m_actual_state))
    1161              :           {
    1162            2 :             pp_printf (&pp,
    1163              :                        "%qE expects a socket file descriptor"
    1164              :                        " but %qE is not a socket",
    1165              :                        m_callee_fndecl, m_arg);
    1166            2 :             return true;
    1167              :           }
    1168              :       }
    1169            8 :     gcc_assert (m_expected_type == EXPECTED_TYPE_STREAM_SOCKET);
    1170            0 :     gcc_assert (m_sm.is_datagram_socket_fd_p (m_actual_state));
    1171            8 :     pp_printf (&pp,
    1172              :                "%qE expects a stream socket file descriptor"
    1173              :                " but %qE is a datagram socket",
    1174              :                m_callee_fndecl, m_arg);
    1175            8 :     return true;
    1176              :   }
    1177              : 
    1178              : private:
    1179              :   state_machine::state_t m_actual_state;
    1180              :   enum expected_type m_expected_type;
    1181              : };
    1182              : 
    1183         3659 : fd_state_machine::fd_state_machine (logger *logger)
    1184              :     : state_machine ("file-descriptor", logger),
    1185         7318 :       m_constant_fd (add_state ("fd-constant")),
    1186         3659 :       m_unchecked_read_write (add_state ("fd-unchecked-read-write")),
    1187         3659 :       m_unchecked_read_only (add_state ("fd-unchecked-read-only")),
    1188         3659 :       m_unchecked_write_only (add_state ("fd-unchecked-write-only")),
    1189         3659 :       m_valid_read_write (add_state ("fd-valid-read-write")),
    1190         3659 :       m_valid_read_only (add_state ("fd-valid-read-only")),
    1191         3659 :       m_valid_write_only (add_state ("fd-valid-write-only")),
    1192         3659 :       m_invalid (add_state ("fd-invalid")),
    1193         3659 :       m_closed (add_state ("fd-closed")),
    1194         3659 :       m_new_datagram_socket (add_state ("fd-new-datagram-socket")),
    1195         3659 :       m_new_stream_socket (add_state ("fd-new-stream-socket")),
    1196         3659 :       m_new_unknown_socket (add_state ("fd-new-unknown-socket")),
    1197         3659 :       m_bound_datagram_socket (add_state ("fd-bound-datagram-socket")),
    1198         3659 :       m_bound_stream_socket (add_state ("fd-bound-stream-socket")),
    1199         3659 :       m_bound_unknown_socket (add_state ("fd-bound-unknown-socket")),
    1200         3659 :       m_listening_stream_socket (add_state ("fd-listening-stream-socket")),
    1201         3659 :       m_connected_stream_socket (add_state ("fd-connected-stream-socket")),
    1202         3659 :       m_stop (add_state ("fd-stop")),
    1203         3659 :       m_O_ACCMODE (get_stashed_constant_by_name ("O_ACCMODE")),
    1204         3659 :       m_O_RDONLY (get_stashed_constant_by_name ("O_RDONLY")),
    1205         3659 :       m_O_WRONLY (get_stashed_constant_by_name ("O_WRONLY")),
    1206         3659 :       m_SOCK_STREAM (get_stashed_constant_by_name ("SOCK_STREAM")),
    1207         7318 :       m_SOCK_DGRAM (get_stashed_constant_by_name ("SOCK_DGRAM"))
    1208              : {
    1209         3659 : }
    1210              : 
    1211              : bool
    1212       509094 : fd_state_machine::is_unchecked_fd_p (state_t s) const
    1213              : {
    1214       509094 :   return (s == m_unchecked_read_write
    1215       508770 :        || s == m_unchecked_read_only
    1216       509450 :        || s == m_unchecked_write_only);
    1217              : }
    1218              : 
    1219              : bool
    1220       508635 : fd_state_machine::is_valid_fd_p (state_t s) const
    1221              : {
    1222       508635 :   return (s == m_valid_read_write
    1223       508387 :        || s == m_valid_read_only
    1224       508494 :        || s == m_valid_write_only);
    1225              : }
    1226              : 
    1227              : bool
    1228       507944 : fd_state_machine::is_socket_fd_p (state_t s) const
    1229              : {
    1230       507944 :   return (s == m_new_datagram_socket
    1231       507902 :           || s == m_new_stream_socket
    1232       507847 :           || s == m_new_unknown_socket
    1233       507720 :           || s == m_bound_datagram_socket
    1234       507689 :           || s == m_bound_stream_socket
    1235       507656 :           || s == m_bound_unknown_socket
    1236       507536 :           || s == m_listening_stream_socket
    1237      1015394 :           || s == m_connected_stream_socket);
    1238              : }
    1239              : 
    1240              : bool
    1241           12 : fd_state_machine::is_datagram_socket_fd_p (state_t s) const
    1242              : {
    1243           12 :   return (s == m_new_datagram_socket
    1244            3 :           || s == m_new_unknown_socket
    1245            3 :           || s == m_bound_datagram_socket
    1246           12 :           || s == m_bound_unknown_socket);
    1247              : }
    1248              : 
    1249              : bool
    1250           38 : fd_state_machine::is_stream_socket_fd_p (state_t s) const
    1251              : {
    1252           38 :   return (s == m_new_stream_socket
    1253           30 :           || s == m_new_unknown_socket
    1254           12 :           || s == m_bound_stream_socket
    1255           12 :           || s == m_bound_unknown_socket
    1256           12 :           || s == m_listening_stream_socket
    1257           50 :           || s == m_connected_stream_socket);
    1258              : }
    1259              : 
    1260              : enum access_mode
    1261          111 : fd_state_machine::get_access_mode_from_flag (int flag) const
    1262              : {
    1263          111 :   if (m_O_ACCMODE && TREE_CODE (m_O_ACCMODE) == INTEGER_CST)
    1264              :     {
    1265           37 :       const unsigned HOST_WIDE_INT mask_val = TREE_INT_CST_LOW (m_O_ACCMODE);
    1266           37 :       const unsigned HOST_WIDE_INT masked_flag = flag & mask_val;
    1267              : 
    1268           37 :       if (m_O_RDONLY && TREE_CODE (m_O_RDONLY) == INTEGER_CST)
    1269           37 :         if (masked_flag == TREE_INT_CST_LOW (m_O_RDONLY))
    1270              :           return READ_ONLY;
    1271              : 
    1272           31 :       if (m_O_WRONLY && TREE_CODE (m_O_WRONLY) == INTEGER_CST)
    1273           31 :         if (masked_flag == TREE_INT_CST_LOW (m_O_WRONLY))
    1274            6 :           return WRITE_ONLY;
    1275              :     }
    1276              :   return READ_WRITE;
    1277              : }
    1278              : 
    1279              : bool
    1280           58 : fd_state_machine::is_readonly_fd_p (state_t state) const
    1281              : {
    1282           58 :   return (state == m_unchecked_read_only || state == m_valid_read_only);
    1283              : }
    1284              : 
    1285              : bool
    1286           76 : fd_state_machine::is_writeonly_fd_p (state_t state) const
    1287              : {
    1288           76 :   return (state == m_unchecked_write_only || state == m_valid_write_only);
    1289              : }
    1290              : 
    1291              : bool
    1292          912 : fd_state_machine::is_closed_fd_p (state_t state) const
    1293              : {
    1294          912 :   return (state == m_closed);
    1295              : }
    1296              : 
    1297              : bool
    1298           83 : fd_state_machine::is_constant_fd_p (state_t state) const
    1299              : {
    1300           83 :   return (state == m_constant_fd);
    1301              : }
    1302              : 
    1303              : fd_state_machine::state_t
    1304            9 : fd_state_machine::valid_to_unchecked_state (state_t state) const
    1305              : {
    1306            9 :   if (state == m_valid_read_write)
    1307            6 :     return m_unchecked_read_write;
    1308            3 :   else if (state == m_valid_write_only)
    1309            2 :     return m_unchecked_write_only;
    1310            1 :   else if (state == m_valid_read_only)
    1311            1 :     return m_unchecked_read_only;
    1312              :   else
    1313            0 :     gcc_unreachable ();
    1314              :   return nullptr;
    1315              : }
    1316              : 
    1317              : void
    1318           72 : fd_state_machine::mark_as_valid_fd (region_model *model,
    1319              :                                     sm_state_map *smap,
    1320              :                                     const svalue *fd_sval,
    1321              :                                     const extrinsic_state &ext_state) const
    1322              : {
    1323            0 :   smap->set_state (model, fd_sval, m_valid_read_write, nullptr, ext_state);
    1324            0 : }
    1325              : 
    1326              : bool
    1327       229583 : fd_state_machine::on_stmt (sm_context &sm_ctxt,
    1328              :                            const gimple *stmt) const
    1329              : {
    1330       229583 :   if (const gcall *call = dyn_cast<const gcall *> (stmt))
    1331        49852 :     if (tree callee_fndecl = sm_ctxt.get_fndecl_for_call (*call))
    1332              :       {
    1333        45953 :         if (is_named_call_p (callee_fndecl, "open", *call, 2))
    1334              :           {
    1335          119 :             on_open (sm_ctxt, *call);
    1336          119 :             return true;
    1337              :           } //  "open"
    1338              : 
    1339        45834 :         if (is_named_call_p (callee_fndecl, "creat", *call, 2))
    1340              :           {
    1341           14 :             on_creat (sm_ctxt, *call);
    1342           14 :             return true;
    1343              :           } // "creat"
    1344              : 
    1345        45820 :         if (is_named_call_p (callee_fndecl, "close", *call, 1))
    1346              :           {
    1347          375 :             on_close (sm_ctxt, *call);
    1348          375 :             return true;
    1349              :           } //  "close"
    1350              : 
    1351        45445 :         if (is_named_call_p (callee_fndecl, "write", *call, 3))
    1352              :           {
    1353           60 :             on_write (sm_ctxt, *call, callee_fndecl);
    1354           60 :             return true;
    1355              :           } // "write"
    1356              : 
    1357        45385 :         if (is_named_call_p (callee_fndecl, "read", *call, 3))
    1358              :           {
    1359           78 :             on_read (sm_ctxt, *call, callee_fndecl);
    1360           78 :             return true;
    1361              :           } // "read"
    1362              : 
    1363        45307 :         if (is_named_call_p (callee_fndecl, "dup", *call, 1))
    1364              :           {
    1365           15 :             check_for_dup (sm_ctxt, *call, callee_fndecl, DUP_1);
    1366           15 :             return true;
    1367              :           }
    1368              : 
    1369        45292 :         if (is_named_call_p (callee_fndecl, "dup2", *call, 2))
    1370              :           {
    1371            8 :             check_for_dup (sm_ctxt, *call, callee_fndecl, DUP_2);
    1372            8 :             return true;
    1373              :           }
    1374              : 
    1375        45284 :         if (is_named_call_p (callee_fndecl, "dup3", *call, 3))
    1376              :           {
    1377            3 :             check_for_dup (sm_ctxt, *call, callee_fndecl, DUP_3);
    1378            3 :             return true;
    1379              :           }
    1380              : 
    1381        45281 :         {
    1382              :           // Handle __attribute__((fd_arg))
    1383              : 
    1384        45281 :           check_for_fd_attrs (sm_ctxt, *call, callee_fndecl,
    1385              :                               "fd_arg", DIRS_READ_WRITE);
    1386              : 
    1387              :           // Handle __attribute__((fd_arg_read))
    1388              : 
    1389        45281 :           check_for_fd_attrs (sm_ctxt, *call, callee_fndecl,
    1390              :                               "fd_arg_read", DIRS_READ);
    1391              : 
    1392              :           // Handle __attribute__((fd_arg_write))
    1393              : 
    1394        45281 :           check_for_fd_attrs (sm_ctxt, *call, callee_fndecl,
    1395              :                               "fd_arg_write", DIRS_WRITE);
    1396              :         }
    1397              :       }
    1398              : 
    1399              :   return false;
    1400              : }
    1401              : 
    1402              : void
    1403       135843 : fd_state_machine::check_for_fd_attrs (
    1404              :     sm_context &sm_ctxt,
    1405              :     const gcall &call, const tree callee_fndecl, const char *attr_name,
    1406              :     access_directions fd_attr_access_dir) const
    1407              : {
    1408              :   /* Handle interesting fd attributes of the callee_fndecl,
    1409              :      or prioritize those of the builtin that callee_fndecl is
    1410              :      expected to be.
    1411              :      Might want this to be controlled by a flag.  */
    1412       135843 :   tree fndecl = callee_fndecl;
    1413              :   /* If call is recognized as a builtin known_function,
    1414              :      use that builtin's function_decl.  */
    1415       135843 :   if (const region_model *old_model = sm_ctxt.get_old_region_model ())
    1416       271686 :     if (const builtin_known_function *builtin_kf
    1417       135843 :          = old_model->get_builtin_kf (call))
    1418        61152 :       fndecl = builtin_kf->builtin_decl ();
    1419              : 
    1420       135843 :   tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (fndecl));
    1421       135843 :   attrs = lookup_attribute (attr_name, attrs);
    1422       135843 :   if (!attrs)
    1423       135831 :     return;
    1424              : 
    1425           12 :   if (!TREE_VALUE (attrs))
    1426              :     return;
    1427              : 
    1428           12 :   auto_bitmap argmap;
    1429              : 
    1430           24 :   for (tree idx = TREE_VALUE (attrs); idx; idx = TREE_CHAIN (idx))
    1431              :     {
    1432           12 :       unsigned int val = TREE_INT_CST_LOW (TREE_VALUE (idx)) - 1;
    1433           12 :       bitmap_set_bit (argmap, val);
    1434              :     }
    1435           12 :   if (bitmap_empty_p (argmap))
    1436            0 :     return;
    1437              : 
    1438           24 :   for (unsigned arg_idx = 0; arg_idx < gimple_call_num_args (&call); arg_idx++)
    1439              :     {
    1440           12 :       tree arg = gimple_call_arg (&call, arg_idx);
    1441           12 :       tree diag_arg = sm_ctxt.get_diagnostic_tree (arg);
    1442           12 :       state_t state = sm_ctxt.get_state (arg);
    1443           12 :       bool bit_set = bitmap_bit_p (argmap, arg_idx);
    1444           12 :       if (TREE_CODE (TREE_TYPE (arg)) != INTEGER_TYPE)
    1445            6 :         continue;
    1446           12 :       if (bit_set) // Check if arg_idx is marked by any of the file descriptor
    1447              :                    // attributes
    1448              :         {
    1449              : 
    1450              :           /* Do use the fndecl that caused the warning so that the
    1451              :              misused attributes are printed and the user not confused.  */
    1452           12 :           if (is_closed_fd_p (state))
    1453              :             {
    1454              : 
    1455            3 :               sm_ctxt.warn (arg,
    1456              :                             std::make_unique<fd_use_after_close>
    1457            3 :                             (*this, diag_arg,
    1458              :                              fndecl, attr_name,
    1459              :                              arg_idx));
    1460            3 :               continue;
    1461              :             }
    1462              : 
    1463           12 :           if (!(is_valid_fd_p (state) || (state == m_stop)))
    1464              :             {
    1465            3 :               if (!is_constant_fd_p (state))
    1466              :                 {
    1467            3 :                   sm_ctxt.warn (arg,
    1468              :                                 std::make_unique<fd_use_without_check>
    1469            3 :                                 (*this, diag_arg,
    1470              :                                  fndecl, attr_name,
    1471              :                                  arg_idx));
    1472            3 :                   continue;
    1473              :                 }
    1474              :             }
    1475              : 
    1476            6 :           switch (fd_attr_access_dir)
    1477              :             {
    1478              :             case DIRS_READ_WRITE:
    1479              :               break;
    1480            3 :             case DIRS_READ:
    1481              : 
    1482            3 :               if (is_writeonly_fd_p (state))
    1483              :                 {
    1484            3 :                   sm_ctxt.warn
    1485            3 :                     (arg,
    1486            3 :                      std::make_unique<fd_access_mode_mismatch> (*this, diag_arg,
    1487            6 :                                                                 DIRS_WRITE,
    1488              :                                                                 fndecl,
    1489              :                                                                 attr_name,
    1490              :                                                                 arg_idx));
    1491              :                 }
    1492              : 
    1493              :               break;
    1494            3 :             case DIRS_WRITE:
    1495              : 
    1496            9 :               if (is_readonly_fd_p (state))
    1497              :                 {
    1498            3 :                   sm_ctxt.warn
    1499            3 :                     (arg,
    1500            3 :                      std::make_unique<fd_access_mode_mismatch> (*this, diag_arg,
    1501            6 :                                                                 DIRS_READ,
    1502              :                                                                 fndecl,
    1503              :                                                                 attr_name,
    1504              :                                                                 arg_idx));
    1505              :                 }
    1506              : 
    1507              :               break;
    1508              :             }
    1509              :         }
    1510              :     }
    1511           12 : }
    1512              : 
    1513              : 
    1514              : void
    1515          119 : fd_state_machine::on_open (sm_context &sm_ctxt, const gcall &call) const
    1516              : {
    1517          119 :   tree lhs = gimple_call_lhs (&call);
    1518          119 :   if (lhs)
    1519              :     {
    1520          115 :       tree arg = gimple_call_arg (&call, 1);
    1521          115 :       enum access_mode mode = READ_WRITE;
    1522          115 :       if (TREE_CODE (arg) == INTEGER_CST)
    1523              :         {
    1524          111 :           int flag = TREE_INT_CST_LOW (arg);
    1525          111 :           mode = get_access_mode_from_flag (flag);
    1526              :         }
    1527          111 :       switch (mode)
    1528              :         {
    1529            6 :         case READ_ONLY:
    1530            6 :           sm_ctxt.on_transition (lhs, m_start,
    1531            6 :                                  m_unchecked_read_only);
    1532            6 :           break;
    1533            6 :         case WRITE_ONLY:
    1534            6 :           sm_ctxt.on_transition (lhs, m_start,
    1535            6 :                                  m_unchecked_write_only);
    1536            6 :           break;
    1537          103 :         default:
    1538          103 :           sm_ctxt.on_transition (lhs, m_start,
    1539          103 :                                  m_unchecked_read_write);
    1540              :         }
    1541              :     }
    1542              :   else
    1543              :     {
    1544            4 :       sm_ctxt.warn (NULL_TREE,
    1545            4 :                     std::make_unique<fd_leak> (*this, NULL_TREE, nullptr));
    1546              :     }
    1547          119 : }
    1548              : 
    1549              : void
    1550           14 : fd_state_machine::on_creat (sm_context &sm_ctxt, const gcall &call) const
    1551              : {
    1552           14 :   tree lhs = gimple_call_lhs (&call);
    1553           14 :   if (lhs)
    1554           10 :     sm_ctxt.on_transition (lhs, m_start, m_unchecked_write_only);
    1555              :   else
    1556            4 :     sm_ctxt.warn (NULL_TREE,
    1557            4 :                   std::make_unique<fd_leak> (*this, NULL_TREE, nullptr));
    1558           14 : }
    1559              : 
    1560              : void
    1561           26 : fd_state_machine::check_for_dup (sm_context &sm_ctxt, const gcall &call,
    1562              :                                  const tree callee_fndecl, enum dup kind) const
    1563              : {
    1564           26 :   tree lhs = gimple_call_lhs (&call);
    1565           26 :   tree arg_1 = gimple_call_arg (&call, 0);
    1566           26 :   state_t state_arg_1 = sm_ctxt.get_state (arg_1);
    1567           26 :   if (state_arg_1 == m_stop)
    1568              :     return;
    1569           26 :   if (!(is_constant_fd_p (state_arg_1) || is_valid_fd_p (state_arg_1)
    1570           13 :         || state_arg_1 == m_start))
    1571              :     {
    1572            6 :       check_for_open_fd (sm_ctxt, call, callee_fndecl, DIRS_READ_WRITE);
    1573            6 :       return;
    1574              :     }
    1575           20 :   switch (kind)
    1576              :     {
    1577           13 :     case DUP_1:
    1578           13 :       if (lhs)
    1579              :         {
    1580           13 :           if (is_constant_fd_p (state_arg_1) || state_arg_1 == m_start)
    1581            7 :             sm_ctxt.set_next_state (lhs, m_unchecked_read_write);
    1582              :           else
    1583            6 :             sm_ctxt.set_next_state (lhs,
    1584              :                                     valid_to_unchecked_state (state_arg_1));
    1585              :         }
    1586           18 :       break;
    1587              : 
    1588            7 :     case DUP_2:
    1589            7 :     case DUP_3:
    1590            7 :       tree arg_2 = gimple_call_arg (&call, 1);
    1591            7 :       state_t state_arg_2 = sm_ctxt.get_state (arg_2);
    1592            7 :       tree diag_arg_2 = sm_ctxt.get_diagnostic_tree (arg_2);
    1593            7 :       if (state_arg_2 == m_stop)
    1594            2 :         return;
    1595              :       /* Check if -1 was passed as second argument to dup2.  */
    1596            7 :       if (!(is_constant_fd_p (state_arg_2) || is_valid_fd_p (state_arg_2)
    1597            2 :             || state_arg_2 == m_start))
    1598              :         {
    1599            2 :           sm_ctxt.warn
    1600            2 :             (arg_2,
    1601            2 :              std::make_unique<fd_use_without_check> (*this, diag_arg_2,
    1602              :                                                      callee_fndecl));
    1603            2 :           return;
    1604              :         }
    1605              :       /* dup2 returns value of its second argument on success.But, the
    1606              :       access mode of the returned file descriptor depends on the duplicated
    1607              :       file descriptor i.e the first argument.  */
    1608            5 :       if (lhs)
    1609              :         {
    1610            5 :           if (is_constant_fd_p (state_arg_1) || state_arg_1 == m_start)
    1611            2 :             sm_ctxt.set_next_state (lhs, m_unchecked_read_write);
    1612              :           else
    1613            3 :             sm_ctxt.set_next_state (lhs,
    1614              :                                     valid_to_unchecked_state (state_arg_1));
    1615              :         }
    1616              : 
    1617              :       break;
    1618              :     }
    1619              : }
    1620              : 
    1621              : void
    1622          375 : fd_state_machine::on_close (sm_context &sm_ctxt, const gcall &call) const
    1623              : {
    1624          375 :   tree arg = gimple_call_arg (&call, 0);
    1625          375 :   state_t state = sm_ctxt.get_state (arg);
    1626          375 :   tree diag_arg = sm_ctxt.get_diagnostic_tree (arg);
    1627              : 
    1628          375 :   sm_ctxt.on_transition (arg, m_start, m_closed);
    1629          375 :   sm_ctxt.on_transition (arg, m_unchecked_read_write, m_closed);
    1630          375 :   sm_ctxt.on_transition (arg, m_unchecked_read_only, m_closed);
    1631          375 :   sm_ctxt.on_transition (arg, m_unchecked_write_only, m_closed);
    1632          375 :   sm_ctxt.on_transition (arg, m_valid_read_write, m_closed);
    1633          375 :   sm_ctxt.on_transition (arg, m_valid_read_only, m_closed);
    1634          375 :   sm_ctxt.on_transition (arg, m_valid_write_only, m_closed);
    1635          375 :   sm_ctxt.on_transition (arg, m_constant_fd, m_closed);
    1636          375 :   sm_ctxt.on_transition (arg, m_new_datagram_socket, m_closed);
    1637          375 :   sm_ctxt.on_transition (arg, m_new_stream_socket, m_closed);
    1638          375 :   sm_ctxt.on_transition (arg, m_new_unknown_socket, m_closed);
    1639          375 :   sm_ctxt.on_transition (arg, m_bound_datagram_socket, m_closed);
    1640          375 :   sm_ctxt.on_transition (arg, m_bound_stream_socket, m_closed);
    1641          375 :   sm_ctxt.on_transition (arg, m_bound_unknown_socket, m_closed);
    1642          375 :   sm_ctxt.on_transition (arg, m_listening_stream_socket, m_closed);
    1643          375 :   sm_ctxt.on_transition (arg, m_connected_stream_socket, m_closed);
    1644              : 
    1645          375 :   if (is_closed_fd_p (state))
    1646              :     {
    1647           39 :       sm_ctxt.warn (arg,
    1648           39 :                     std::make_unique<fd_double_close> (*this, diag_arg));
    1649           39 :       sm_ctxt.set_next_state (arg, m_stop);
    1650              :     }
    1651          375 : }
    1652              : void
    1653           78 : fd_state_machine::on_read (sm_context &sm_ctxt, const gcall &call,
    1654              :                            const tree callee_fndecl) const
    1655              : {
    1656           78 :   check_for_open_fd (sm_ctxt,call, callee_fndecl, DIRS_READ);
    1657            0 : }
    1658              : void
    1659           60 : fd_state_machine::on_write (sm_context &sm_ctxt, const gcall &call,
    1660              :                             const tree callee_fndecl) const
    1661              : {
    1662           60 :   check_for_open_fd (sm_ctxt,call, callee_fndecl, DIRS_WRITE);
    1663            0 : }
    1664              : 
    1665              : void
    1666          144 : fd_state_machine::check_for_open_fd (
    1667              :     sm_context &sm_ctxt,
    1668              :     const gcall &call, const tree callee_fndecl,
    1669              :     enum access_directions callee_fndecl_dir) const
    1670              : {
    1671          144 :   tree arg = gimple_call_arg (&call, 0);
    1672          144 :   tree diag_arg = sm_ctxt.get_diagnostic_tree (arg);
    1673          144 :   state_t state = sm_ctxt.get_state (arg);
    1674              : 
    1675          144 :   if (is_closed_fd_p (state))
    1676              :     {
    1677           12 :       sm_ctxt.warn (arg,
    1678           12 :                     std::make_unique<fd_use_after_close> (*this, diag_arg,
    1679              :                                                           callee_fndecl));
    1680              :     }
    1681              : 
    1682              :   else
    1683              :     {
    1684          132 :       if (state == m_new_stream_socket
    1685          131 :           || state == m_bound_stream_socket
    1686          131 :           || state == m_listening_stream_socket)
    1687              :         /* Complain about fncall on socket in wrong phase.  */
    1688            4 :         sm_ctxt.warn
    1689            4 :           (arg,
    1690            4 :            std::make_unique<fd_phase_mismatch> (*this, diag_arg,
    1691              :                                                 callee_fndecl,
    1692              :                                                 state,
    1693            8 :                                                 EXPECTED_PHASE_CAN_TRANSFER));
    1694          128 :       else if (!(is_valid_fd_p (state)
    1695          105 :                  || state == m_new_datagram_socket
    1696          105 :                  || state == m_bound_unknown_socket
    1697          104 :                  || state == m_connected_stream_socket
    1698           89 :                  || state == m_start
    1699           66 :                  || state == m_stop))
    1700              :         {
    1701           47 :           if (!is_constant_fd_p (state))
    1702           36 :             sm_ctxt.warn
    1703           36 :               (arg,
    1704           36 :                std::make_unique<fd_use_without_check> (*this, diag_arg,
    1705              :                                                        callee_fndecl));
    1706              :         }
    1707          132 :       switch (callee_fndecl_dir)
    1708              :         {
    1709              :         case DIRS_READ_WRITE:
    1710              :           break;
    1711           73 :         case DIRS_READ:
    1712           73 :           if (is_writeonly_fd_p (state))
    1713              :             {
    1714            4 :               tree diag_arg = sm_ctxt.get_diagnostic_tree (arg);
    1715            4 :               sm_ctxt.warn (arg,
    1716              :                             std::make_unique<fd_access_mode_mismatch>
    1717            4 :                               (*this, diag_arg, DIRS_WRITE, callee_fndecl));
    1718              :             }
    1719              : 
    1720              :           break;
    1721           55 :         case DIRS_WRITE:
    1722              : 
    1723           55 :           if (is_readonly_fd_p (state))
    1724              :             {
    1725            2 :               tree diag_arg = sm_ctxt.get_diagnostic_tree (arg);
    1726            2 :               sm_ctxt.warn (arg,
    1727              :                             std::make_unique<fd_access_mode_mismatch>
    1728            2 :                               (*this, diag_arg, DIRS_READ, callee_fndecl));
    1729              :             }
    1730              :           break;
    1731              :         }
    1732              :     }
    1733          144 : }
    1734              : 
    1735              : static bool
    1736          271 : add_constraint_ge_zero (region_model *model,
    1737              :                         const svalue *fd_sval,
    1738              :                         region_model_context *ctxt)
    1739              : {
    1740          271 :   const svalue *zero
    1741          271 :     = model->get_manager ()->get_or_create_int_cst (integer_type_node, 0);
    1742          271 :   return model->add_constraint (fd_sval, GE_EXPR, zero, ctxt);
    1743              : }
    1744              : 
    1745              : /* Get the state for a new socket type based on SOCKET_TYPE_SVAL,
    1746              :    a SOCK_* value.  */
    1747              : 
    1748              : state_machine::state_t
    1749          104 : fd_state_machine::
    1750              : get_state_for_socket_type (const svalue *socket_type_sval) const
    1751              : {
    1752          104 :   if (tree socket_type_cst = socket_type_sval->maybe_get_constant ())
    1753              :     {
    1754              :       /* Attempt to use SOCK_* constants stashed from the frontend.  */
    1755           48 :       if (tree_int_cst_equal (socket_type_cst, m_SOCK_STREAM))
    1756           23 :         return m_new_stream_socket;
    1757           25 :       if (tree_int_cst_equal (socket_type_cst, m_SOCK_DGRAM))
    1758           16 :         return m_new_datagram_socket;
    1759              :     }
    1760              : 
    1761              :   /* Unrecognized constant, or a symbolic "type" value.  */
    1762           65 :   return m_new_unknown_socket;
    1763              : }
    1764              : 
    1765              : /* Update the model and fd state for an outcome of a call to "socket",
    1766              :    where SUCCESSFUL indicate which of the two outcomes.
    1767              :    Return true if the outcome is feasible, or false to reject it.  */
    1768              : 
    1769              : bool
    1770          220 : fd_state_machine::on_socket (const call_details &cd,
    1771              :                              bool successful,
    1772              :                              sm_context &sm_ctxt,
    1773              :                              const extrinsic_state &) const
    1774              : {
    1775          220 :   const gcall &call = cd.get_call_stmt ();
    1776          220 :   region_model *model = cd.get_model ();
    1777              : 
    1778          220 :   if (successful)
    1779              :     {
    1780          110 :       if (gimple_call_lhs (&call))
    1781              :         {
    1782          104 :           conjured_purge p (model, cd.get_ctxt ());
    1783          104 :           region_model_manager *mgr = model->get_manager ();
    1784          104 :           const svalue *new_fd
    1785          104 :             = mgr->get_or_create_conjured_svalue (integer_type_node,
    1786              :                                                   &call,
    1787              :                                                   cd.get_lhs_region (),
    1788              :                                                   p);
    1789          104 :           if (!add_constraint_ge_zero (model, new_fd, cd.get_ctxt ()))
    1790            0 :             return false;
    1791              : 
    1792          104 :           const svalue *socket_type_sval = cd.get_arg_svalue (1);
    1793          104 :           state_machine::state_t new_state
    1794          104 :             = get_state_for_socket_type (socket_type_sval);
    1795          104 :           sm_ctxt.on_transition (new_fd, m_start, new_state);
    1796          104 :           model->set_value (cd.get_lhs_region (), new_fd, cd.get_ctxt ());
    1797              :         }
    1798              :       else
    1799            6 :         sm_ctxt.warn (NULL_TREE,
    1800            6 :                       std::make_unique<fd_leak> (*this, NULL_TREE, nullptr));
    1801              :     }
    1802              :   else
    1803              :     {
    1804              :       /* Return -1; set errno.  */
    1805          110 :       model->update_for_int_cst_return (cd, -1, true);
    1806          110 :       model->set_errno (cd);
    1807              :     }
    1808              : 
    1809              :   return true;
    1810              : }
    1811              : 
    1812              : /* Check that FD_SVAL is usable by socket APIs.
    1813              :    Complain if it has been closed, if it is a non-socket,
    1814              :    or is invalid.
    1815              :    If COMPLAINED is non-NULL and a problem is found,
    1816              :    write *COMPLAINED = true.
    1817              : 
    1818              :    If SUCCESSFUL is true, attempt to add the constraint that FD_SVAL >= 0.
    1819              :    Return true if this outcome is feasible.  */
    1820              : 
    1821              : bool
    1822          308 : fd_state_machine::check_for_socket_fd (const call_details &cd,
    1823              :                                        bool successful,
    1824              :                                        sm_context &sm_ctxt,
    1825              :                                        const svalue *fd_sval,
    1826              :                                        state_t old_state,
    1827              :                                        bool *complained) const
    1828              : {
    1829          308 :   if (is_closed_fd_p (old_state))
    1830              :     {
    1831            0 :       tree diag_arg = sm_ctxt.get_diagnostic_tree (fd_sval);
    1832            0 :       sm_ctxt.warn
    1833            0 :         (fd_sval,
    1834            0 :          std::make_unique<fd_use_after_close> (*this, diag_arg,
    1835            0 :                                                cd.get_fndecl_for_call ()));
    1836            0 :       if (complained)
    1837            0 :         *complained = true;
    1838            0 :       if (successful)
    1839            0 :         return false;
    1840              :     }
    1841          616 :   else if (is_unchecked_fd_p (old_state) || is_valid_fd_p (old_state))
    1842              :     {
    1843              :       /* Complain about non-socket.  */
    1844            2 :       tree diag_arg = sm_ctxt.get_diagnostic_tree (fd_sval);
    1845            2 :       sm_ctxt.warn
    1846            2 :         (fd_sval,
    1847            2 :          std::make_unique<fd_type_mismatch> (*this, diag_arg,
    1848            2 :                                              cd.get_fndecl_for_call (),
    1849              :                                              old_state,
    1850            2 :                                              EXPECTED_TYPE_SOCKET));
    1851            2 :       if (complained)
    1852            2 :         *complained = true;
    1853            2 :       if (successful)
    1854            1 :         return false;
    1855              :     }
    1856          306 :   else if (old_state == m_invalid)
    1857              :     {
    1858           16 :       tree diag_arg = sm_ctxt.get_diagnostic_tree (fd_sval);
    1859           16 :       sm_ctxt.warn
    1860           16 :         (fd_sval,
    1861           16 :          std::make_unique<fd_use_without_check> (*this, diag_arg,
    1862           16 :                                                  cd.get_fndecl_for_call ()));
    1863           16 :       if (complained)
    1864           14 :         *complained = true;
    1865           16 :       if (successful)
    1866            8 :         return false;
    1867              :     }
    1868              : 
    1869          299 :   if (successful)
    1870          145 :     if (!add_constraint_ge_zero (cd.get_model (), fd_sval, cd.get_ctxt ()))
    1871              :       return false;
    1872              : 
    1873              :   return true;
    1874              : }
    1875              : 
    1876              : /* For use by "bind" and "connect".
    1877              :    As per fd_state_machine::check_for_socket_fd above,
    1878              :    but also complain if we don't have a new socket, and check that
    1879              :    we can read up to the size bytes from the address.  */
    1880              : 
    1881              : bool
    1882          178 : fd_state_machine::check_for_new_socket_fd (const call_details &cd,
    1883              :                                            bool successful,
    1884              :                                            sm_context &sm_ctxt,
    1885              :                                            const svalue *fd_sval,
    1886              :                                            state_t old_state,
    1887              :                                            enum expected_phase expected_phase)
    1888              :   const
    1889              : {
    1890          178 :   bool complained = false;
    1891              : 
    1892              :   /* Check address and len.  */
    1893          178 :   const svalue *address_sval = cd.get_arg_svalue (1);
    1894          178 :   const svalue *len_sval = cd.get_arg_svalue (2);
    1895              : 
    1896              :   /* Check that we can read the given number of bytes from the
    1897              :      address.  */
    1898          178 :   region_model *model = cd.get_model ();
    1899          178 :   const region *address_reg
    1900          178 :     = model->deref_rvalue (address_sval, cd.get_arg_tree (1),
    1901              :                            cd.get_ctxt ());
    1902          178 :   const region *sized_address_reg
    1903          178 :     = model->get_manager ()->get_sized_region (address_reg,
    1904              :                                                NULL_TREE,
    1905              :                                                len_sval);
    1906          178 :   model->get_store_value (sized_address_reg, cd.get_ctxt ());
    1907              : 
    1908          178 :   if (!check_for_socket_fd (cd, successful, sm_ctxt,
    1909              :                             fd_sval, old_state, &complained))
    1910              :     return false;
    1911          170 :   else if (!complained
    1912          162 :            && !(old_state == m_new_stream_socket
    1913          140 :                 || old_state == m_new_datagram_socket
    1914          124 :                 || old_state == m_new_unknown_socket
    1915           68 :                 || old_state == m_start
    1916           36 :                 || old_state == m_stop
    1917           34 :                 || old_state == m_constant_fd))
    1918              :     {
    1919              :       /* Complain about "bind" or "connect" in wrong phase.  */
    1920           10 :       tree diag_arg = sm_ctxt.get_diagnostic_tree (fd_sval);
    1921           10 :       sm_ctxt.warn
    1922           10 :         (fd_sval,
    1923           10 :          std::make_unique<fd_phase_mismatch> (*this, diag_arg,
    1924           10 :                                               cd.get_fndecl_for_call (),
    1925              :                                               old_state,
    1926              :                                               expected_phase));
    1927           10 :       if (successful)
    1928            5 :         return false;
    1929              :     }
    1930          160 :   else if (!successful)
    1931              :     {
    1932              :       /* If we were in the start state, assume we had a new socket.  */
    1933           84 :       if (old_state == m_start)
    1934           16 :         sm_ctxt.set_next_state (fd_sval, m_new_unknown_socket);
    1935              :     }
    1936              : 
    1937              :   /* Passing NULL as the address will lead to failure.  */
    1938            5 :   if (successful)
    1939           76 :     if (address_sval->all_zeroes_p ())
    1940            4 :       return false;
    1941              : 
    1942              :   return true;
    1943              : }
    1944              : 
    1945              : /* Update the model and fd state for an outcome of a call to "bind",
    1946              :    where SUCCESSFUL indicate which of the two outcomes.
    1947              :    Return true if the outcome is feasible, or false to reject it.  */
    1948              : 
    1949              : bool
    1950          136 : fd_state_machine::on_bind (const call_details &cd,
    1951              :                            bool successful,
    1952              :                            sm_context &sm_ctxt,
    1953              :                            const extrinsic_state &) const
    1954              : {
    1955          136 :   const svalue *fd_sval = cd.get_arg_svalue (0);
    1956          136 :   region_model *model = cd.get_model ();
    1957          136 :   state_t old_state = sm_ctxt.get_state (fd_sval);
    1958              : 
    1959          136 :   if (!check_for_new_socket_fd (cd, successful, sm_ctxt,
    1960              :                                 fd_sval, old_state,
    1961              :                                 EXPECTED_PHASE_CAN_BIND))
    1962              :     return false;
    1963              : 
    1964          122 :   if (successful)
    1965              :     {
    1966           54 :       state_t next_state = nullptr;
    1967           54 :       if (old_state == m_new_stream_socket)
    1968            9 :         next_state = m_bound_stream_socket;
    1969           45 :       else if (old_state == m_new_datagram_socket)
    1970            8 :         next_state = m_bound_datagram_socket;
    1971           37 :       else if (old_state == m_new_unknown_socket)
    1972           21 :         next_state = m_bound_unknown_socket;
    1973           16 :       else if (old_state == m_start
    1974            6 :                || old_state == m_constant_fd)
    1975           15 :         next_state = m_bound_unknown_socket;
    1976            1 :       else if (old_state == m_stop)
    1977              :         next_state = m_stop;
    1978              :       else
    1979            0 :         gcc_unreachable ();
    1980           54 :       sm_ctxt.set_next_state (fd_sval, next_state);
    1981           54 :       model->update_for_zero_return (cd, true);
    1982              :     }
    1983              :   else
    1984              :     {
    1985              :       /* Return -1; set errno.  */
    1986           68 :       model->update_for_int_cst_return (cd, -1, true);
    1987           68 :       model->set_errno (cd);
    1988              :     }
    1989              : 
    1990              :   return true;
    1991              : }
    1992              : 
    1993              : /* Update the model and fd state for an outcome of a call to "listen",
    1994              :    where SUCCESSFUL indicate which of the two outcomes.
    1995              :    Return true if the outcome is feasible, or false to reject it.  */
    1996              : 
    1997              : bool
    1998           78 : fd_state_machine::on_listen (const call_details &cd,
    1999              :                              bool successful,
    2000              :                              sm_context &sm_ctxt,
    2001              :                              const extrinsic_state &) const
    2002              : {
    2003           78 :   const svalue *fd_sval = cd.get_arg_svalue (0);
    2004           78 :   region_model *model = cd.get_model ();
    2005           78 :   state_t old_state = sm_ctxt.get_state (fd_sval);
    2006              : 
    2007              :   /* We expect a stream socket that's had "bind" called on it.  */
    2008           78 :   if (!check_for_socket_fd (cd, successful, sm_ctxt, fd_sval, old_state))
    2009              :     return false;
    2010           77 :   if (!(old_state == m_start
    2011           61 :         || old_state == m_constant_fd
    2012           57 :         || old_state == m_stop
    2013           57 :         || old_state == m_invalid
    2014           56 :         || old_state == m_bound_stream_socket
    2015           48 :         || old_state == m_bound_unknown_socket
    2016              :         /* Assume it's OK to call "listen" more than once.  */
    2017           34 :         || old_state == m_listening_stream_socket))
    2018              :     {
    2019              :       /* Complain about fncall on wrong type or in wrong phase.  */
    2020           32 :       tree diag_arg = sm_ctxt.get_diagnostic_tree (fd_sval);
    2021           32 :       if (is_stream_socket_fd_p (old_state))
    2022           26 :         sm_ctxt.warn
    2023           26 :           (fd_sval,
    2024           26 :            std::make_unique<fd_phase_mismatch> (*this, diag_arg,
    2025           26 :                                                 cd.get_fndecl_for_call (),
    2026              :                                                 old_state,
    2027           52 :                                                 EXPECTED_PHASE_CAN_LISTEN));
    2028              :       else
    2029            6 :         sm_ctxt.warn
    2030            6 :           (fd_sval,
    2031            6 :            std::make_unique<fd_type_mismatch> (*this, diag_arg,
    2032            6 :                                                cd.get_fndecl_for_call (),
    2033              :                                                old_state,
    2034           12 :                                                EXPECTED_TYPE_STREAM_SOCKET));
    2035           32 :       if (successful)
    2036           16 :         return false;
    2037              :     }
    2038              : 
    2039           61 :   if (successful)
    2040              :     {
    2041           22 :       model->update_for_zero_return (cd, true);
    2042           22 :       sm_ctxt.set_next_state (fd_sval, m_listening_stream_socket);
    2043              :     }
    2044              :   else
    2045              :     {
    2046              :       /* Return -1; set errno.  */
    2047           39 :       model->update_for_int_cst_return (cd, -1, true);
    2048           39 :       model->set_errno (cd);
    2049           39 :       if (old_state == m_start)
    2050            8 :         sm_ctxt.set_next_state (fd_sval, m_bound_stream_socket);
    2051              :     }
    2052              : 
    2053              :   return true;
    2054              : }
    2055              : 
    2056              : /* Update the model and fd state for an outcome of a call to "accept",
    2057              :    where SUCCESSFUL indicate which of the two outcomes.
    2058              :    Return true if the outcome is feasible, or false to reject it.  */
    2059              : 
    2060              : bool
    2061           52 : fd_state_machine::on_accept (const call_details &cd,
    2062              :                              bool successful,
    2063              :                              sm_context &sm_ctxt,
    2064              :                              const extrinsic_state &) const
    2065              : {
    2066           52 :   const gcall &call = cd.get_call_stmt ();
    2067           52 :   const svalue *fd_sval = cd.get_arg_svalue (0);
    2068           52 :   const svalue *address_sval = cd.get_arg_svalue (1);
    2069           52 :   const svalue *len_ptr_sval = cd.get_arg_svalue (2);
    2070           52 :   region_model *model = cd.get_model ();
    2071           52 :   state_t old_state = sm_ctxt.get_state (fd_sval);
    2072              : 
    2073           52 :   if (!address_sval->all_zeroes_p ())
    2074              :     {
    2075           18 :       region_model_manager *mgr = model->get_manager ();
    2076              : 
    2077              :       /* We might have a union of various pointer types, rather than a
    2078              :          pointer type; cast to (void *) before dereferencing.  */
    2079           18 :       address_sval = mgr->get_or_create_cast (ptr_type_node, address_sval);
    2080              : 
    2081           18 :       const region *address_reg
    2082           18 :         = model->deref_rvalue (address_sval, cd.get_arg_tree (1),
    2083              :                                cd.get_ctxt ());
    2084           18 :       const region *len_reg
    2085           18 :         = model->deref_rvalue (len_ptr_sval, cd.get_arg_tree (2),
    2086              :                                cd.get_ctxt ());
    2087           18 :       const svalue *old_len_sval
    2088           18 :         = model->get_store_value (len_reg, cd.get_ctxt ());
    2089           18 :       tree len_ptr = cd.get_arg_tree (2);
    2090           18 :       tree star_len_ptr = build2 (MEM_REF, TREE_TYPE (TREE_TYPE (len_ptr)),
    2091              :                                   len_ptr,
    2092           18 :                                   build_int_cst (TREE_TYPE (len_ptr), 0));
    2093           18 :       old_len_sval = model->check_for_poison (old_len_sval,
    2094              :                                               star_len_ptr,
    2095              :                                               len_reg,
    2096              :                                               cd.get_ctxt ());
    2097           18 :       if (successful)
    2098              :         {
    2099            9 :           conjured_purge p (model, cd.get_ctxt ());
    2100            9 :           const region *old_sized_address_reg
    2101            9 :             = mgr->get_sized_region (address_reg,
    2102              :                                      NULL_TREE,
    2103              :                                      old_len_sval);
    2104            9 :           const svalue *new_addr_sval
    2105            9 :             = mgr->get_or_create_conjured_svalue (NULL_TREE,
    2106              :                                                   &call,
    2107              :                                                   old_sized_address_reg,
    2108              :                                                   p);
    2109            9 :           model->set_value (old_sized_address_reg, new_addr_sval,
    2110              :                             cd.get_ctxt ());
    2111            9 :           const svalue *new_addr_len
    2112            9 :             = mgr->get_or_create_conjured_svalue (NULL_TREE,
    2113              :                                                   &call,
    2114              :                                                   len_reg,
    2115              :                                                   p);
    2116            9 :           model->set_value (len_reg, new_addr_len, cd.get_ctxt ());
    2117              :         }
    2118              :     }
    2119              : 
    2120              :   /* We expect a stream socket in the "listening" state.  */
    2121           52 :   if (!check_for_socket_fd (cd, successful, sm_ctxt, fd_sval, old_state))
    2122              :     return false;
    2123              : 
    2124           52 :   if (old_state == m_start || old_state == m_constant_fd)
    2125              :     /* If we were in the start state (or a constant), assume we had the
    2126              :        expected state.  */
    2127           28 :     sm_ctxt.set_next_state (fd_sval, m_listening_stream_socket);
    2128           24 :   else if (old_state == m_stop)
    2129              :     {
    2130              :       /* No further complaints.  */
    2131              :     }
    2132           24 :   else if (old_state != m_listening_stream_socket)
    2133              :     {
    2134              :       /* Complain about fncall on wrong type or in wrong phase.  */
    2135            6 :       tree diag_arg = sm_ctxt.get_diagnostic_tree (fd_sval);
    2136            6 :       if (is_stream_socket_fd_p (old_state))
    2137            4 :         sm_ctxt.warn
    2138            4 :           (fd_sval,
    2139            4 :            std::make_unique<fd_phase_mismatch> (*this, diag_arg,
    2140            4 :                                                 cd.get_fndecl_for_call (),
    2141              :                                                 old_state,
    2142            8 :                                                 EXPECTED_PHASE_CAN_ACCEPT));
    2143              :       else
    2144            2 :         sm_ctxt.warn
    2145            2 :           (fd_sval,
    2146            2 :            std::make_unique<fd_type_mismatch> (*this, diag_arg,
    2147            2 :                                                cd.get_fndecl_for_call (),
    2148              :                                                old_state,
    2149            4 :                                                EXPECTED_TYPE_STREAM_SOCKET));
    2150            6 :       if (successful)
    2151            3 :         return false;
    2152              :     }
    2153              : 
    2154           49 :   if (successful)
    2155              :     {
    2156              :       /* Return new conjured FD in "connected" state.  */
    2157           23 :       if (gimple_call_lhs (&call))
    2158              :         {
    2159           22 :           conjured_purge p (model, cd.get_ctxt ());
    2160           22 :           region_model_manager *mgr = model->get_manager ();
    2161           22 :           const svalue *new_fd
    2162           22 :             = mgr->get_or_create_conjured_svalue (integer_type_node,
    2163              :                                                   &call,
    2164              :                                                   cd.get_lhs_region (),
    2165              :                                                   p);
    2166           22 :           if (!add_constraint_ge_zero (model, new_fd, cd.get_ctxt ()))
    2167            0 :             return false;
    2168           22 :           sm_ctxt.on_transition (new_fd, m_start, m_connected_stream_socket);
    2169           22 :           model->set_value (cd.get_lhs_region (), new_fd, cd.get_ctxt ());
    2170              :         }
    2171              :       else
    2172            1 :         sm_ctxt.warn (NULL_TREE,
    2173            1 :                       std::make_unique<fd_leak> (*this, NULL_TREE, nullptr));
    2174              :     }
    2175              :   else
    2176              :     {
    2177              :       /* Return -1; set errno.  */
    2178           26 :       model->update_for_int_cst_return (cd, -1, true);
    2179           26 :       model->set_errno (cd);
    2180              :     }
    2181              : 
    2182              :   return true;
    2183              : }
    2184              : 
    2185              : /* Update the model and fd state for an outcome of a call to "connect",
    2186              :    where SUCCESSFUL indicate which of the two outcomes.
    2187              :    Return true if the outcome is feasible, or false to reject it.  */
    2188              : 
    2189              : bool
    2190           42 : fd_state_machine::on_connect (const call_details &cd,
    2191              :                               bool successful,
    2192              :                               sm_context &sm_ctxt,
    2193              :                               const extrinsic_state &) const
    2194              : {
    2195           42 :   const svalue *fd_sval = cd.get_arg_svalue (0);
    2196           42 :   region_model *model = cd.get_model ();
    2197           42 :   state_t old_state = sm_ctxt.get_state (fd_sval);
    2198              : 
    2199           42 :   if (!check_for_new_socket_fd (cd, successful, sm_ctxt,
    2200              :                                 fd_sval, old_state,
    2201              :                                 EXPECTED_PHASE_CAN_CONNECT))
    2202              :     return false;
    2203              : 
    2204           39 :   if (successful)
    2205              :     {
    2206           18 :       model->update_for_zero_return (cd, true);
    2207           18 :       state_t next_state = nullptr;
    2208           18 :       if (old_state == m_new_stream_socket)
    2209            2 :         next_state = m_connected_stream_socket;
    2210           16 :       else if (old_state == m_new_datagram_socket)
    2211              :         /* It's legal to call connect on a datagram socket, potentially
    2212              :            more than once.  We don't transition states for this.  */
    2213              :         next_state = m_new_datagram_socket;
    2214           16 :       else if (old_state == m_new_unknown_socket)
    2215            7 :         next_state = m_stop;
    2216            9 :       else if (old_state == m_start
    2217            5 :                || old_state == m_constant_fd)
    2218            9 :         next_state = m_stop;
    2219            0 :       else if (old_state == m_stop)
    2220              :         next_state = m_stop;
    2221              :       else
    2222            0 :         gcc_unreachable ();
    2223           18 :       sm_ctxt.set_next_state (fd_sval, next_state);
    2224              :     }
    2225              :   else
    2226              :     {
    2227              :       /* Return -1; set errno.  */
    2228           21 :       model->update_for_int_cst_return (cd, -1, true);
    2229           21 :       model->set_errno (cd);
    2230              :       /* TODO: perhaps transition to a failed state, since the
    2231              :          portable way to handle a failed "connect" is to close
    2232              :          the socket and try again with a new socket.  */
    2233              :     }
    2234              : 
    2235              :   return true;
    2236              : }
    2237              : 
    2238              : void
    2239        30776 : fd_state_machine::on_condition (sm_context &sm_ctxt,
    2240              :                                 const svalue *lhs,
    2241              :                                 enum tree_code op,
    2242              :                                 const svalue *rhs) const
    2243              : {
    2244        30776 :   if (tree cst = rhs->maybe_get_constant ())
    2245              :     {
    2246        24919 :       if (TREE_CODE (cst) == INTEGER_CST)
    2247              :         {
    2248        24911 :           int val = TREE_INT_CST_LOW (cst);
    2249        24911 :           if (val == -1)
    2250              :             {
    2251          556 :               if (op == NE_EXPR)
    2252          262 :                 make_valid_transitions_on_condition (sm_ctxt, lhs);
    2253              : 
    2254          294 :               else if (op == EQ_EXPR)
    2255          262 :                 make_invalid_transitions_on_condition (sm_ctxt, lhs);
    2256              :             }
    2257              :         }
    2258              :     }
    2259              : 
    2260        30776 :   if (rhs->all_zeroes_p ())
    2261              :     {
    2262        19388 :       if (op == GE_EXPR)
    2263          753 :         make_valid_transitions_on_condition (sm_ctxt, lhs);
    2264        18635 :       else if (op == LT_EXPR)
    2265          527 :         make_invalid_transitions_on_condition (sm_ctxt, lhs);
    2266              :     }
    2267        30776 : }
    2268              : 
    2269              : void
    2270         1015 : fd_state_machine::make_valid_transitions_on_condition (sm_context &sm_ctxt,
    2271              :                                                        const svalue *lhs) const
    2272              : {
    2273         1015 :   sm_ctxt.on_transition (lhs, m_unchecked_read_write,
    2274         1015 :                          m_valid_read_write);
    2275         1015 :   sm_ctxt.on_transition (lhs, m_unchecked_read_only,
    2276         1015 :                          m_valid_read_only);
    2277         1015 :   sm_ctxt.on_transition (lhs, m_unchecked_write_only,
    2278         1015 :                          m_valid_write_only);
    2279         1015 : }
    2280              : 
    2281              : void
    2282          789 : fd_state_machine::
    2283              : make_invalid_transitions_on_condition (sm_context &sm_ctxt,
    2284              :                                        const svalue *lhs) const
    2285              : {
    2286          789 :   sm_ctxt.on_transition (lhs, m_unchecked_read_write, m_invalid);
    2287          789 :   sm_ctxt.on_transition (lhs, m_unchecked_read_only, m_invalid);
    2288          789 :   sm_ctxt.on_transition (lhs, m_unchecked_write_only, m_invalid);
    2289          789 : }
    2290              : 
    2291              : bool
    2292       508080 : fd_state_machine::can_purge_p (state_t s) const
    2293              : {
    2294      1016080 :   if (is_unchecked_fd_p (s)
    2295      1015959 :       || is_valid_fd_p (s)
    2296       507879 :       || is_socket_fd_p (s))
    2297              :     return false;
    2298              :   else
    2299       507239 :     return true;
    2300              : }
    2301              : 
    2302              : std::unique_ptr<pending_diagnostic>
    2303          111 : fd_state_machine::on_leak (tree var,
    2304              :                            const program_state *,
    2305              :                            const program_state *new_state) const
    2306              : {
    2307          111 :   return std::make_unique<fd_leak> (*this, var, new_state);
    2308              : }
    2309              : } // namespace
    2310              : 
    2311              : std::unique_ptr<state_machine>
    2312         3659 : make_fd_state_machine (logger *logger)
    2313              : {
    2314         3659 :   return std::make_unique<fd_state_machine> (logger);
    2315              : }
    2316              : 
    2317              : static bool
    2318         1591 : get_fd_state (region_model_context *ctxt,
    2319              :               sm_state_map **out_smap,
    2320              :               const fd_state_machine **out_sm,
    2321              :               unsigned *out_sm_idx,
    2322              :               std::unique_ptr<sm_context> *out_sm_context)
    2323              : {
    2324         1591 :   if (!ctxt)
    2325              :     return false;
    2326              : 
    2327          708 :   const state_machine *sm;
    2328          708 :   if (!ctxt->get_fd_map (out_smap, &sm, out_sm_idx, out_sm_context))
    2329              :     return false;
    2330              : 
    2331          708 :   gcc_assert (sm);
    2332              : 
    2333          708 :   *out_sm = (const fd_state_machine *)sm;
    2334          708 :   return true;
    2335              : }
    2336              : 
    2337              : /* Specialcase hook for handling pipe, for use by
    2338              :    kf_pipe::success::update_model.  */
    2339              : 
    2340              : void
    2341          188 : region_model::mark_as_valid_fd (const svalue *sval, region_model_context *ctxt)
    2342              : {
    2343          188 :   sm_state_map *smap;
    2344          188 :   const fd_state_machine *fd_sm;
    2345          188 :   if (!get_fd_state (ctxt, &smap, &fd_sm, nullptr, nullptr))
    2346          116 :     return;
    2347           72 :   const extrinsic_state *ext_state = ctxt->get_ext_state ();
    2348           72 :   if (!ext_state)
    2349              :     return;
    2350           72 :   fd_sm->mark_as_valid_fd (this, smap, sval, *ext_state);
    2351              : }
    2352              : 
    2353              : /* Handle calls to "socket".
    2354              :    See e.g. https://man7.org/linux/man-pages/man3/socket.3p.html  */
    2355              : 
    2356         3659 : class kf_socket : public known_function
    2357              : {
    2358              : public:
    2359              :   class outcome_of_socket : public succeed_or_fail_call_info
    2360              :   {
    2361              :   public:
    2362          220 :     outcome_of_socket (const call_details &cd, bool success)
    2363          220 :     : succeed_or_fail_call_info (cd, success)
    2364              :     {}
    2365              : 
    2366              :     message_detail
    2367          130 :     get_message_detail () const final override
    2368              :     {
    2369          130 :       if (m_success)
    2370              :         return message_detail::returning_non_negative;
    2371              :       else
    2372           16 :         return message_detail::returning_minus_one;
    2373              :     }
    2374              : 
    2375          727 :     bool update_model (region_model *model,
    2376              :                        const exploded_edge *,
    2377              :                        region_model_context *ctxt) const final override
    2378              :     {
    2379          727 :       const call_details cd (get_call_details (model, ctxt));
    2380          727 :       sm_state_map *smap;
    2381          727 :       const fd_state_machine *fd_sm;
    2382          727 :       std::unique_ptr<sm_context> sm_ctxt;
    2383          727 :       if (!get_fd_state (ctxt, &smap, &fd_sm, nullptr, &sm_ctxt))
    2384              :         {
    2385          507 :           cd.set_any_lhs_with_defaults ();
    2386          507 :           return true;
    2387              :         }
    2388          220 :       const extrinsic_state *ext_state = ctxt->get_ext_state ();
    2389          220 :       if (!ext_state)
    2390              :         {
    2391            0 :           cd.set_any_lhs_with_defaults ();
    2392            0 :           return true;
    2393              :         }
    2394              : 
    2395          220 :       return fd_sm->on_socket (cd, m_success, *(sm_ctxt.get ()), *ext_state);
    2396          727 :     }
    2397              :   };
    2398              : 
    2399          770 :   bool matches_call_types_p (const call_details &cd) const final override
    2400              :   {
    2401          770 :     return cd.num_args () == 3;
    2402              :   }
    2403              : 
    2404          110 :   void impl_call_post (const call_details &cd) const final override
    2405              :   {
    2406          110 :     if (cd.get_ctxt ())
    2407              :       {
    2408          110 :         cd.get_ctxt ()->bifurcate
    2409          110 :           (std::make_unique<outcome_of_socket> (cd, false));
    2410          110 :         cd.get_ctxt ()->bifurcate
    2411          110 :           (std::make_unique<outcome_of_socket> (cd, true));
    2412          110 :         cd.get_ctxt ()->terminate_path ();
    2413              :       }
    2414          110 :   }
    2415              : };
    2416              : 
    2417              : /* Handle calls to "bind".
    2418              :    See e.g. https://man7.org/linux/man-pages/man3/bind.3p.html  */
    2419              : 
    2420         3659 : class kf_bind : public known_function
    2421              : {
    2422              : public:
    2423              :   class outcome_of_bind : public succeed_or_fail_call_info
    2424              :   {
    2425              :   public:
    2426          136 :     outcome_of_bind (const call_details &cd, bool success)
    2427          136 :     : succeed_or_fail_call_info (cd, success)
    2428              :     {}
    2429              : 
    2430              :     message_detail
    2431           50 :     get_message_detail () const final override
    2432              :     {
    2433           50 :       if (m_success)
    2434              :         return message_detail::returning_zero;
    2435              :       else
    2436           26 :         return message_detail::returning_minus_one;
    2437              :     }
    2438              : 
    2439          211 :     bool update_model (region_model *model,
    2440              :                        const exploded_edge *,
    2441              :                        region_model_context *ctxt) const final override
    2442              :     {
    2443          211 :       const call_details cd (get_call_details (model, ctxt));
    2444          211 :       sm_state_map *smap;
    2445          211 :       const fd_state_machine *fd_sm;
    2446          211 :       std::unique_ptr<sm_context> sm_ctxt;
    2447          211 :       if (!get_fd_state (ctxt, &smap, &fd_sm, nullptr, &sm_ctxt))
    2448              :         {
    2449           75 :           cd.set_any_lhs_with_defaults ();
    2450           75 :           return true;
    2451              :         }
    2452          136 :       const extrinsic_state *ext_state = ctxt->get_ext_state ();
    2453          136 :       if (!ext_state)
    2454              :         {
    2455            0 :           cd.set_any_lhs_with_defaults ();
    2456            0 :           return true;
    2457              :         }
    2458          136 :       return fd_sm->on_bind (cd, m_success, *sm_ctxt.get (), *ext_state);
    2459          211 :     }
    2460              :   };
    2461              : 
    2462          544 :   bool matches_call_types_p (const call_details &cd) const final override
    2463              :   {
    2464          544 :     return (cd.num_args () == 3
    2465          544 :             && cd.arg_is_integral_p (0)
    2466          523 :             && cd.arg_is_pointer_p (1)
    2467         1020 :             && cd.arg_is_integral_p (2));
    2468              :   }
    2469              : 
    2470           68 :   void impl_call_post (const call_details &cd) const final override
    2471              :   {
    2472           68 :     if (cd.get_ctxt ())
    2473              :       {
    2474           68 :         cd.get_ctxt ()->bifurcate
    2475           68 :           (std::make_unique<outcome_of_bind> (cd, false));
    2476           68 :         cd.get_ctxt ()->bifurcate
    2477           68 :           (std::make_unique<outcome_of_bind> (cd, true));
    2478           68 :         cd.get_ctxt ()->terminate_path ();
    2479              :       }
    2480           68 :   }
    2481              : };
    2482              : 
    2483              : /* Handle calls to "listen".
    2484              :    See e.g. https://man7.org/linux/man-pages/man3/listen.3p.html  */
    2485              : 
    2486         3659 : class kf_listen : public known_function
    2487              : {
    2488              :   class outcome_of_listen : public succeed_or_fail_call_info
    2489              :   {
    2490              :   public:
    2491           78 :     outcome_of_listen (const call_details &cd, bool success)
    2492           78 :     : succeed_or_fail_call_info (cd, success)
    2493              :     {}
    2494              : 
    2495              :     message_detail
    2496            8 :     get_message_detail () const final override
    2497              :     {
    2498            8 :       if (m_success)
    2499              :         return message_detail::returning_zero;
    2500              :       else
    2501            0 :         return message_detail::returning_minus_one;
    2502              :     }
    2503              : 
    2504           88 :     bool update_model (region_model *model,
    2505              :                        const exploded_edge *,
    2506              :                        region_model_context *ctxt) const final override
    2507              :     {
    2508           88 :       const call_details cd (get_call_details (model, ctxt));
    2509           88 :       sm_state_map *smap;
    2510           88 :       const fd_state_machine *fd_sm;
    2511           88 :       std::unique_ptr<sm_context> sm_ctxt;
    2512           88 :       if (!get_fd_state (ctxt, &smap, &fd_sm, nullptr, &sm_ctxt))
    2513              :         {
    2514           10 :           cd.set_any_lhs_with_defaults ();
    2515           10 :           return true;
    2516              :         }
    2517           78 :       const extrinsic_state *ext_state = ctxt->get_ext_state ();
    2518           78 :       if (!ext_state)
    2519              :         {
    2520            0 :           cd.set_any_lhs_with_defaults ();
    2521            0 :           return true;
    2522              :         }
    2523              : 
    2524           78 :       return fd_sm->on_listen (cd, m_success, *sm_ctxt.get (), *ext_state);
    2525           88 :     }
    2526              :   };
    2527              : 
    2528          273 :   bool matches_call_types_p (const call_details &cd) const final override
    2529              :   {
    2530          273 :     return cd.num_args () == 2;
    2531              :   }
    2532              : 
    2533           39 :   void impl_call_post (const call_details &cd) const final override
    2534              :   {
    2535           39 :     if (cd.get_ctxt ())
    2536              :       {
    2537           39 :         cd.get_ctxt ()->bifurcate
    2538           39 :           (std::make_unique<outcome_of_listen> (cd, false));
    2539           39 :         cd.get_ctxt ()->bifurcate
    2540           39 :           (std::make_unique<outcome_of_listen> (cd, true));
    2541           39 :         cd.get_ctxt ()->terminate_path ();
    2542              :       }
    2543           39 :   }
    2544              : };
    2545              : 
    2546              : /* Handle calls to "accept".
    2547              :    See e.g. https://man7.org/linux/man-pages/man3/accept.3p.html  */
    2548              : 
    2549         3659 : class kf_accept : public known_function
    2550              : {
    2551              :   class outcome_of_accept : public succeed_or_fail_call_info
    2552              :   {
    2553              :   public:
    2554           52 :     outcome_of_accept (const call_details &cd, bool success)
    2555           52 :     : succeed_or_fail_call_info (cd, success)
    2556              :     {}
    2557              : 
    2558              :     message_detail
    2559           14 :     get_message_detail () const final override
    2560              :     {
    2561           14 :       if (m_success)
    2562              :         return message_detail::returning_non_negative;
    2563              :       else
    2564            4 :         return message_detail::returning_minus_one;
    2565              :     }
    2566              : 
    2567           70 :     bool update_model (region_model *model,
    2568              :                        const exploded_edge *,
    2569              :                        region_model_context *ctxt) const final override
    2570              :     {
    2571           70 :       const call_details cd (get_call_details (model, ctxt));
    2572           70 :       sm_state_map *smap;
    2573           70 :       const fd_state_machine *fd_sm;
    2574           70 :       std::unique_ptr<sm_context> sm_ctxt;
    2575           70 :       if (!get_fd_state (ctxt, &smap, &fd_sm, nullptr, &sm_ctxt))
    2576              :         {
    2577           18 :           cd.set_any_lhs_with_defaults ();
    2578           18 :           return true;
    2579              :         }
    2580           52 :       const extrinsic_state *ext_state = ctxt->get_ext_state ();
    2581           52 :       if (!ext_state)
    2582              :         {
    2583            0 :           cd.set_any_lhs_with_defaults ();
    2584            0 :           return true;
    2585              :         }
    2586              : 
    2587           52 :       return fd_sm->on_accept (cd, m_success, *sm_ctxt.get (), *ext_state);
    2588           70 :     }
    2589              :   };
    2590              : 
    2591          231 :   bool matches_call_types_p (const call_details &cd) const final override
    2592              :   {
    2593          231 :     return (cd.num_args () == 3
    2594          231 :             && cd.arg_is_pointer_p (1)
    2595          413 :             && cd.arg_is_pointer_p (2));
    2596              :   }
    2597              : 
    2598           26 :   void impl_call_post (const call_details &cd) const final override
    2599              :   {
    2600           26 :     if (cd.get_ctxt ())
    2601              :       {
    2602           26 :         cd.get_ctxt ()->bifurcate
    2603           26 :           (std::make_unique<outcome_of_accept> (cd, false));
    2604           26 :         cd.get_ctxt ()->bifurcate
    2605           26 :           (std::make_unique<outcome_of_accept> (cd, true));
    2606           26 :         cd.get_ctxt ()->terminate_path ();
    2607              :       }
    2608           26 :   }
    2609              : };
    2610              : 
    2611              : /* Handle calls to "connect".
    2612              :    See e.g. https://man7.org/linux/man-pages/man3/connect.3p.html  */
    2613              : 
    2614         3659 : class kf_connect : public known_function
    2615              : {
    2616              : public:
    2617              :   class outcome_of_connect : public succeed_or_fail_call_info
    2618              :   {
    2619              :   public:
    2620           42 :     outcome_of_connect (const call_details &cd, bool success)
    2621           42 :     : succeed_or_fail_call_info (cd, success)
    2622              :     {}
    2623              : 
    2624              :     message_detail
    2625            0 :     get_message_detail () const final override
    2626              :     {
    2627            0 :       if (m_success)
    2628              :         return message_detail::returning_zero;
    2629              :       else
    2630            0 :         return message_detail::returning_minus_one;
    2631              :     }
    2632              : 
    2633          162 :     bool update_model (region_model *model,
    2634              :                        const exploded_edge *,
    2635              :                        region_model_context *ctxt) const final override
    2636              :     {
    2637          162 :       const call_details cd (get_call_details (model, ctxt));
    2638          162 :       sm_state_map *smap;
    2639          162 :       const fd_state_machine *fd_sm;
    2640          162 :       std::unique_ptr<sm_context> sm_ctxt;
    2641          162 :       if (!get_fd_state (ctxt, &smap, &fd_sm, nullptr, &sm_ctxt))
    2642              :         {
    2643          120 :           cd.set_any_lhs_with_defaults ();
    2644          120 :           return true;
    2645              :         }
    2646           42 :       const extrinsic_state *ext_state = ctxt->get_ext_state ();
    2647           42 :       if (!ext_state)
    2648              :         {
    2649            0 :           cd.set_any_lhs_with_defaults ();
    2650            0 :           return true;
    2651              :         }
    2652              : 
    2653           42 :       return fd_sm->on_connect (cd, m_success, *sm_ctxt.get (), *ext_state);
    2654          162 :     }
    2655              :   };
    2656              : 
    2657          147 :   bool matches_call_types_p (const call_details &cd) const final override
    2658              :   {
    2659          147 :     return (cd.num_args () == 3
    2660          147 :             && cd.arg_is_pointer_p (1));
    2661              :   }
    2662              : 
    2663           21 :   void impl_call_post (const call_details &cd) const final override
    2664              :   {
    2665           21 :     if (cd.get_ctxt ())
    2666              :       {
    2667           21 :         cd.get_ctxt ()->bifurcate
    2668           21 :           (std::make_unique<outcome_of_connect> (cd, false));
    2669           21 :         cd.get_ctxt ()->bifurcate
    2670           21 :           (std::make_unique<outcome_of_connect> (cd, true));
    2671           21 :         cd.get_ctxt ()->terminate_path ();
    2672              :       }
    2673           21 :   }
    2674              : };
    2675              : 
    2676              : /* Handler for "isatty"".
    2677              :    See e.g. https://man7.org/linux/man-pages/man3/isatty.3.html  */
    2678              : 
    2679         3659 : class kf_isatty : public known_function
    2680              : {
    2681              :   class outcome_of_isatty : public succeed_or_fail_call_info
    2682              :   {
    2683              :   public:
    2684          146 :     outcome_of_isatty (const call_details &cd, bool success)
    2685          146 :     : succeed_or_fail_call_info (cd, success)
    2686              :     {}
    2687              : 
    2688              :     message_detail
    2689           16 :     get_message_detail () const final override
    2690              :     {
    2691           16 :       if (m_success)
    2692              :         return message_detail::returning_one;
    2693              :       else
    2694            8 :         return message_detail::returning_zero;
    2695              :     }
    2696              : 
    2697          168 :     bool update_model (region_model *model,
    2698              :                        const exploded_edge *,
    2699              :                        region_model_context *ctxt) const final override
    2700              :     {
    2701          168 :       const call_details cd (get_call_details (model, ctxt));
    2702              : 
    2703          168 :       if (m_success)
    2704              :         {
    2705              :           /* Return 1.  */
    2706           84 :           model->update_for_int_cst_return (cd, 1, true);
    2707              :         }
    2708              :       else
    2709              :         {
    2710              :           /* Return 0; set errno.  */
    2711           84 :           model->update_for_int_cst_return (cd, 0, true);
    2712           84 :           model->set_errno (cd);
    2713              :         }
    2714              : 
    2715          168 :       return feasible_p (cd, ctxt);
    2716              :     }
    2717              : 
    2718              :   private:
    2719          168 :     bool feasible_p (const call_details &cd,
    2720              :                      region_model_context *ctxt) const
    2721              :     {
    2722          168 :       if (m_success)
    2723              :         {
    2724              :           /* Can't be "success" on a closed/invalid fd.  */
    2725           84 :           sm_state_map *smap;
    2726           84 :           const fd_state_machine *fd_sm;
    2727           84 :           std::unique_ptr<sm_context> sm_ctxt;
    2728           84 :           if (!get_fd_state (ctxt, &smap, &fd_sm, nullptr, &sm_ctxt))
    2729              :             return true;
    2730           73 :           const extrinsic_state *ext_state = ctxt->get_ext_state ();
    2731           73 :           if (!ext_state)
    2732              :             return true;
    2733              : 
    2734           73 :           const svalue *fd_sval = cd.get_arg_svalue (0);
    2735           73 :           state_machine::state_t old_state = sm_ctxt->get_state (fd_sval);
    2736              : 
    2737           73 :           if (fd_sm->is_closed_fd_p (old_state)
    2738           73 :               || old_state == fd_sm->m_invalid)
    2739              :             return false;
    2740           84 :         }
    2741              :       return true;
    2742              :     }
    2743              :   }; // class outcome_of_isatty
    2744              : 
    2745              : public:
    2746          511 :   bool matches_call_types_p (const call_details &cd) const final override
    2747              :   {
    2748          511 :     return cd.num_args () == 1;
    2749              :   }
    2750              : 
    2751           73 :   void impl_call_post (const call_details &cd) const final override
    2752              :   {
    2753           73 :     if (cd.get_ctxt ())
    2754              :       {
    2755           73 :         cd.get_ctxt ()->bifurcate
    2756           73 :           (std::make_unique<outcome_of_isatty> (cd, false));
    2757           73 :         cd.get_ctxt ()->bifurcate
    2758           73 :           (std::make_unique<outcome_of_isatty> (cd, true));
    2759           73 :         cd.get_ctxt ()->terminate_path ();
    2760              :       }
    2761           73 :   }
    2762              : };
    2763              : 
    2764              : /* Handler for calls to "pipe" and "pipe2".
    2765              :    See e.g. https://www.man7.org/linux/man-pages/man2/pipe.2.html  */
    2766              : 
    2767              : class kf_pipe : public known_function
    2768              : {
    2769              :   class failure : public failed_call_info
    2770              :   {
    2771              :   public:
    2772           10 :     failure (const call_details &cd) : failed_call_info (cd) {}
    2773              : 
    2774              :     message_detail
    2775           12 :     get_message_detail () const final override
    2776              :     {
    2777           12 :       return message_detail::returning_minus_one;
    2778              :     }
    2779              : 
    2780           28 :     bool update_model (region_model *model,
    2781              :                        const exploded_edge *,
    2782              :                        region_model_context *ctxt) const final override
    2783              :     {
    2784              :       /* Return -1; everything else is unchanged.  */
    2785           28 :       const call_details cd (get_call_details (model, ctxt));
    2786           28 :       model->update_for_int_cst_return (cd, -1, true);
    2787           28 :       return true;
    2788              :     }
    2789              :   };
    2790              : 
    2791              :   class success : public success_call_info
    2792              :   {
    2793              :   public:
    2794           10 :     success (const call_details &cd) : success_call_info (cd) {}
    2795              : 
    2796              :     message_detail
    2797           12 :     get_message_detail () const final override
    2798              :     {
    2799           12 :       return message_detail::returning_zero;
    2800              :     }
    2801              : 
    2802           22 :     bool update_model (region_model *model,
    2803              :                        const exploded_edge *,
    2804              :                        region_model_context *ctxt) const final override
    2805              :     {
    2806           22 :       const call_details cd (get_call_details (model, ctxt));
    2807              : 
    2808              :       /* Return 0.  */
    2809           22 :       model->update_for_zero_return (cd, true);
    2810              : 
    2811              :       /* Update fd array.  */
    2812           22 :       region_model_manager *mgr = cd.get_manager ();
    2813           22 :       tree arr_tree = cd.get_arg_tree (0);
    2814           22 :       const svalue *arr_sval = cd.get_arg_svalue (0);
    2815           88 :       for (int idx = 0; idx < 2; idx++)
    2816              :         {
    2817           44 :           const region *arr_reg
    2818           44 :             = model->deref_rvalue (arr_sval, arr_tree, cd.get_ctxt ());
    2819           44 :           const svalue *idx_sval
    2820           44 :             = mgr->get_or_create_int_cst (integer_type_node, idx);
    2821           44 :           const region *element_reg
    2822           44 :             = mgr->get_element_region (arr_reg, integer_type_node, idx_sval);
    2823           44 :           conjured_purge p (model, cd.get_ctxt ());
    2824           44 :           const svalue *fd_sval
    2825           44 :             = mgr->get_or_create_conjured_svalue (integer_type_node,
    2826           44 :                                                   &cd.get_call_stmt (),
    2827              :                                                   element_reg,
    2828              :                                                   p);
    2829           44 :           model->set_value (element_reg, fd_sval, cd.get_ctxt ());
    2830           44 :           model->mark_as_valid_fd (fd_sval, cd.get_ctxt ());
    2831              :         }
    2832           22 :       return true;
    2833              :     }
    2834              :   };
    2835              : 
    2836              : public:
    2837         7318 :   kf_pipe (unsigned num_args)
    2838         7318 :   : m_num_args (num_args)
    2839              :   {
    2840         7318 :     gcc_assert (num_args > 0);
    2841         7318 :   }
    2842              : 
    2843           98 :   bool matches_call_types_p (const call_details &cd) const final override
    2844              :   {
    2845           98 :     return (cd.num_args () == m_num_args && cd.arg_is_pointer_p (0));
    2846              :   }
    2847              : 
    2848           10 :   void impl_call_post (const call_details &cd) const final override
    2849              :   {
    2850           10 :     if (cd.get_ctxt ())
    2851              :       {
    2852           10 :         cd.get_ctxt ()->bifurcate
    2853           10 :           (std::make_unique<failure> (cd));
    2854           10 :         cd.get_ctxt ()->bifurcate
    2855           10 :           (std::make_unique<success> (cd));
    2856           10 :         cd.get_ctxt ()->terminate_path ();
    2857              :       }
    2858           10 :   }
    2859              : 
    2860              : private:
    2861              :   unsigned m_num_args;
    2862              : };
    2863              : 
    2864              : /* Handler for "read".
    2865              :      ssize_t read(int fildes, void *buf, size_t nbyte);
    2866              :    See e.g. https://man7.org/linux/man-pages/man2/read.2.html   */
    2867              : 
    2868         3659 : class kf_read : public known_function
    2869              : {
    2870              : public:
    2871          340 :   bool matches_call_types_p (const call_details &cd) const final override
    2872              :   {
    2873          340 :     return (cd.num_args () == 3
    2874          340 :             && cd.arg_is_pointer_p (1)
    2875          680 :             && cd.arg_is_size_p (2));
    2876              :   }
    2877              : 
    2878              :   /* For now, assume that any call to "read" fully clobbers the buffer
    2879              :      passed in.  This isn't quite correct (e.g. errors, partial reads;
    2880              :      see PR analyzer/108689), but at least stops us falsely complaining
    2881              :      about the buffer being uninitialized.  */
    2882           56 :   void impl_call_pre (const call_details &cd) const final override
    2883              :   {
    2884           56 :     region_model *model = cd.get_model ();
    2885           56 :     const svalue *ptr_sval = cd.get_arg_svalue (1);
    2886           56 :     if (auto *sym_ptr = ptr_sval->maybe_get_symbolic_pointer ())
    2887              :       {
    2888           44 :         const region *base_reg = &sym_ptr->get_base_region ();
    2889           44 :         model->mark_region_as_unknown (base_reg, nullptr);
    2890              :       }
    2891           56 :     cd.set_any_lhs_with_defaults ();
    2892           56 :   }
    2893              : };
    2894              : 
    2895              : /* Handler for "closefrom".
    2896              :      void closefrom (int lowfd);  (glibc / FreeBSD)
    2897              :      int closefrom (int lowfd);   (NetBSD / OpenBSD)
    2898              :    Negative lowfd handling varies across C libraries (glibc clamps to 0,
    2899              :    whereas NetBSD fails with EBADF).  */
    2900              : 
    2901         3659 : class kf_closefrom : public known_function
    2902              : {
    2903              : public:
    2904          333 :   bool matches_call_types_p (const call_details &cd) const final override
    2905              :   {
    2906          333 :     if (cd.num_args () != 1)
    2907              :       return false;
    2908          333 :     if (!types_compatible_p (cd.get_arg_type (0), integer_type_node))
    2909              :       return false;
    2910              : 
    2911          315 :     const gcall &call = cd.get_call_stmt ();
    2912          315 :     if (!gimple_call_fntype (&call))
    2913              :       return false;
    2914          315 :     tree ret_type = gimple_call_return_type (&call);
    2915          315 :     return (VOID_TYPE_P (ret_type)
    2916          315 :             || types_compatible_p (ret_type, integer_type_node));
    2917              :   }
    2918              : 
    2919           61 :   void impl_call_pre (const call_details &cd) const final override
    2920              :   {
    2921           61 :     cd.set_any_lhs_with_defaults ();
    2922              : 
    2923           61 :     region_model *model = cd.get_model ();
    2924           61 :     const region *errno_reg = cd.get_manager ()->get_errno_region ();
    2925           61 :     const svalue *new_errno_sval
    2926           61 :       = cd.get_or_create_conjured_svalue (errno_reg);
    2927           61 :     model->set_value (errno_reg, new_errno_sval, cd.get_ctxt ());
    2928           61 :   }
    2929              : 
    2930           61 :   void impl_call_post (const call_details &cd) const final override
    2931              :   {
    2932           61 :     sm_state_map *smap;
    2933           61 :     const fd_state_machine *fd_sm;
    2934           61 :     if (!get_fd_state (cd.get_ctxt (), &smap, &fd_sm, nullptr, nullptr))
    2935           26 :       return;
    2936              : 
    2937           35 :     const extrinsic_state *ext_state = cd.get_ctxt ()->get_ext_state ();
    2938           35 :     if (!ext_state)
    2939              :       return;
    2940              : 
    2941           35 :     region_model *model = cd.get_model ();
    2942           35 :     region_model_manager *mgr = cd.get_manager ();
    2943           35 :     const svalue *lowfd = cd.get_arg_svalue (0);
    2944           35 :     const svalue *zero = mgr->get_or_create_int_cst (integer_type_node, 0);
    2945           35 :     const bool lowfd_known_nonnegative
    2946           35 :       = model->eval_condition (lowfd, GE_EXPR, zero).is_true ();
    2947           35 :     const bool lowfd_known_eq_zero
    2948           35 :       = model->eval_condition (lowfd, EQ_EXPR, zero).is_true ();
    2949              : 
    2950           35 :     symbolic::node_set<svalue> fds_to_close;
    2951           35 :     symbolic::node_set<svalue> fds_to_stop_tracking;
    2952              : 
    2953           35 :     auto should_close_fd
    2954           31 :       = [&] (const svalue *fd_sval, bool valid,
    2955              :              bool speculative_socket) -> tristate
    2956              :       {
    2957              :         /* Valid fds are guaranteed >= 0 by state-machine semantics even when
    2958              :            constraint_manager lacks fd >= 0 (such as fd != -1 or pipe ()).  */
    2959           31 :         if (valid && lowfd_known_eq_zero)
    2960            7 :           return tristate::TS_TRUE;
    2961              : 
    2962           24 :         tristate ts_in_range = model->eval_condition (fd_sval, GE_EXPR, lowfd);
    2963           24 :         if (ts_in_range.is_false ())
    2964            2 :           return tristate::TS_FALSE;
    2965              : 
    2966              :         /* These socket states are shared by successful calls and speculative
    2967              :            inference from m_start on failed bind, connect, listen, or accept.
    2968              :            Because their origin is not distinguished, stop tracking them here,
    2969              :            accepting some precision loss on double-close diagnostics.  */
    2970           22 :         if (speculative_socket)
    2971            6 :           return tristate::TS_UNKNOWN;
    2972              : 
    2973              :         /* Close fds proven >= lowfd >= 0.  Valid fds are guaranteed
    2974              :            nonnegative; for other states (such as unchecked fds or sockets),
    2975              :            also require fd >= 0 so negative values are not treated as
    2976              :            closed.  */
    2977           16 :         if (ts_in_range.is_true () && lowfd_known_nonnegative)
    2978              :           {
    2979            6 :             if (valid)
    2980            3 :               return tristate::TS_TRUE;
    2981            3 :             return model->eval_condition (fd_sval, GE_EXPR, zero);
    2982              :           }
    2983              : 
    2984           10 :         return tristate::TS_UNKNOWN;
    2985           35 :       };
    2986              : 
    2987           35 :     symbolic::node_set<svalue> fd_svals;
    2988           67 :     for (auto kv : *smap)
    2989              :       {
    2990           32 :         const auto state = kv.second.m_state;
    2991           32 :         if (fd_sm->is_valid_fd_p (state)
    2992           14 :             || fd_sm->is_unchecked_fd_p (state)
    2993           11 :             || fd_sm->is_socket_fd_p (state))
    2994           31 :           fd_svals.add (kv.first);
    2995              :       }
    2996              : 
    2997           66 :     for (const svalue *fd_sval : fd_svals)
    2998              :       {
    2999           31 :         const auto state = smap->get_state (fd_sval, *ext_state);
    3000           31 :         const bool valid = fd_sm->is_valid_fd_p (state);
    3001           62 :         const bool speculative_socket
    3002           31 :           = (state == fd_sm->m_new_unknown_socket
    3003           28 :              || state == fd_sm->m_bound_stream_socket
    3004           58 :              || state == fd_sm->m_listening_stream_socket);
    3005              : 
    3006           31 :         const svalue *fd_for_comparison = fd_sval;
    3007              : 
    3008              :         /* sm_state_map::canonicalize_svalue strips the type from
    3009              :            svalue_from_bit_vector; restore integer_type_node for comparison
    3010              :            when the bit-vector width matches int.  */
    3011           31 :         if (auto *from_bv = fd_sval->dyn_cast_svalue_from_bit_vector ())
    3012              :           {
    3013           23 :             concrete_bit_size type_size;
    3014           23 :             concrete_bit_size bv_size;
    3015           23 :             const auto &bv = from_bv->get_bit_vector ();
    3016           23 :             if (get_type_size (integer_type_node, &type_size)
    3017           23 :                 && bv.maybe_get_concrete_size_in_bits (&bv_size)
    3018           46 :                 && type_size == bv_size)
    3019           23 :               fd_for_comparison
    3020           23 :                 = mgr->get_svalue_from_bit_vector (integer_type_node, bv);
    3021           23 :           }
    3022              : 
    3023           31 :         tristate ts_should_close
    3024           31 :           = should_close_fd (fd_for_comparison, valid, speculative_socket);
    3025              : 
    3026           31 :         if (ts_should_close.is_true ())
    3027           13 :           fds_to_close.add (fd_for_comparison);
    3028           18 :         else if (ts_should_close.is_unknown ())
    3029           16 :           fds_to_stop_tracking.add (fd_for_comparison);
    3030              :       }
    3031              : 
    3032              :     /* Only conclude a 0 return when we have proved at least one genuinely
    3033              :        open fd is closed (ensuring lowfd >= 0 and a non-empty range).
    3034              :        This single-path model ignores EINTR, pruning failure-cleanup paths
    3035              :        for such calls.  */
    3036           35 :     tree lhs_type = cd.get_lhs_type ();
    3037           35 :     if (!fds_to_close.empty ()
    3038           11 :         && cd.get_lhs_region ()
    3039            1 :         && lhs_type
    3040           36 :         && INTEGRAL_TYPE_P (lhs_type))
    3041            1 :       cd.maybe_set_lhs (mgr->get_or_create_int_cst (lhs_type, 0));
    3042              : 
    3043              :     /* set_state updates all equivalent svalues.  Apply m_closed last so
    3044              :        that a proven closure takes precedence over m_stop.  */
    3045           51 :     for (const svalue *fd_sval : fds_to_stop_tracking)
    3046           16 :       smap->set_state (model, fd_sval, fd_sm->m_stop, nullptr, *ext_state);
    3047           48 :     for (const svalue *fd_sval : fds_to_close)
    3048           13 :       smap->set_state (model, fd_sval, fd_sm->m_closed, nullptr, *ext_state);
    3049           35 :   }
    3050              : };
    3051              : 
    3052              : /* Populate KFM with instances of known functions relating to
    3053              :    file descriptors.  */
    3054              : 
    3055              : void
    3056         3659 : register_known_fd_functions (known_function_manager &kfm)
    3057              : {
    3058         3659 :   kfm.add ("accept", std::make_unique<kf_accept> ());
    3059         3659 :   kfm.add ("bind", std::make_unique<kf_bind> ());
    3060         3659 :   kfm.add ("closefrom", std::make_unique<kf_closefrom> ());
    3061         3659 :   kfm.add ("connect", std::make_unique<kf_connect> ());
    3062         3659 :   kfm.add ("isatty", std::make_unique<kf_isatty> ());
    3063         3659 :   kfm.add ("listen", std::make_unique<kf_listen> ());
    3064         3659 :   kfm.add ("pipe", std::make_unique<kf_pipe> (1));
    3065         3659 :   kfm.add ("pipe2", std::make_unique<kf_pipe> (2));
    3066         3659 :   kfm.add ("read", std::make_unique<kf_read> ());
    3067         3659 :   kfm.add ("socket", std::make_unique<kf_socket> ());
    3068         3659 : }
    3069              : 
    3070              : } // namespace ana
    3071              : 
    3072              : #endif // 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.