From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7879) id 6B90A3858296; Wed, 15 Feb 2023 10:22:38 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6B90A3858296 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1676456558; bh=7qu4ufFMIXYXdhNPolfQ/jMoQywkARL9mGSxXBUJfs8=; h=From:To:Subject:Date:From; b=Suvya5eFMNshzdTHa+THpDwucUdpRn/liqHxJClcf5Bu4Ec3DXpuPttTzjRWyViY/ /sXfZd8OOtNLDyD97tXP30QnijpA1fEJu9x7vWaLYFLdryGo2G9YVM+ditn1IBY0CA MRarMw6ExGDD2NswvXIQzatAMh1MXBlqXqJFQnOg= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Filip Kastl To: gcc-cvs@gcc.gnu.org Subject: [gcc(refs/users/pheeck/heads/sccp)] analyzer: move known funs for fds to sm-fd.cc X-Act-Checkin: gcc X-Git-Author: David Malcolm X-Git-Refname: refs/users/pheeck/heads/sccp X-Git-Oldrev: aba7278d1001a95fe9677bfa14155f7437a4aac0 X-Git-Newrev: 8bab582402e5e31b3e6784bb045c4b334e983e91 Message-Id: <20230215102238.6B90A3858296@sourceware.org> Date: Wed, 15 Feb 2023 10:22:38 +0000 (GMT) List-Id: https://gcc.gnu.org/g:8bab582402e5e31b3e6784bb045c4b334e983e91 commit 8bab582402e5e31b3e6784bb045c4b334e983e91 Author: David Malcolm Date: Wed Nov 23 20:43:32 2022 -0500 analyzer: move known funs for fds to sm-fd.cc This mostly mechanical change enables a simplification in the followup patch. No functional change intended. gcc/analyzer/ChangeLog: * analyzer.h (register_known_fd_functions): New decl. * region-model-impl-calls.cc (class kf_accept): Move to sm-fd.cc. (class kf_bind): Likewise. (class kf_connect): Likewise. (class kf_listen): Likewise. (class kf_pipe): Likewise. (class kf_socket): Likewise. (register_known_functions): Remove registration of the above functions, instead calling register_known_fd_functions. * sm-fd.cc: Include "analyzer/call-info.h". (class kf_socket): Move here from region-model-impl-calls.cc. (class kf_bind): Likewise. (class kf_listen): Likewise. (class kf_accept): Likewise. (class kf_connect): Likewise. (class kf_pipe): Likewise. (register_known_fd_functions): New. Signed-off-by: David Malcolm Diff: --- gcc/analyzer/analyzer.h | 1 + gcc/analyzer/region-model-impl-calls.cc | 286 +------------------------------ gcc/analyzer/sm-fd.cc | 293 ++++++++++++++++++++++++++++++++ 3 files changed, 296 insertions(+), 284 deletions(-) diff --git a/gcc/analyzer/analyzer.h b/gcc/analyzer/analyzer.h index d424b43f2de..4fbe092199f 100644 --- a/gcc/analyzer/analyzer.h +++ b/gcc/analyzer/analyzer.h @@ -258,6 +258,7 @@ public: }; extern void register_known_functions (known_function_manager &mgr); +extern void register_known_fd_functions (known_function_manager &kfm); extern void register_varargs_builtins (known_function_manager &kfm); /* Passed by pointer to PLUGIN_ANALYZER_INIT callbacks. */ diff --git a/gcc/analyzer/region-model-impl-calls.cc b/gcc/analyzer/region-model-impl-calls.cc index 23a21d752cf..d3f2bf8240b 100644 --- a/gcc/analyzer/region-model-impl-calls.cc +++ b/gcc/analyzer/region-model-impl-calls.cc @@ -595,83 +595,6 @@ public: } }; -/* Handle calls to "accept". - See e.g. https://man7.org/linux/man-pages/man3/accept.3p.html */ - -class kf_accept : public known_function -{ - class outcome_of_accept : public succeed_or_fail_call_info - { - public: - outcome_of_accept (const call_details &cd, bool success) - : succeed_or_fail_call_info (cd, success) - {} - - bool update_model (region_model *model, - const exploded_edge *, - region_model_context *ctxt) const final override - { - const call_details cd (get_call_details (model, ctxt)); - return cd.get_model ()->on_accept (cd, m_success); - } - }; - - bool matches_call_types_p (const call_details &cd) const final override - { - return (cd.num_args () == 3 - && cd.arg_is_pointer_p (1) - && cd.arg_is_pointer_p (2)); - } - - void impl_call_post (const call_details &cd) const final override - { - if (cd.get_ctxt ()) - { - cd.get_ctxt ()->bifurcate (make_unique (cd, false)); - cd.get_ctxt ()->bifurcate (make_unique (cd, true)); - cd.get_ctxt ()->terminate_path (); - } - } -}; - -/* Handle calls to "bind". - See e.g. https://man7.org/linux/man-pages/man3/bind.3p.html */ - -class kf_bind : public known_function -{ -public: - class outcome_of_bind : public succeed_or_fail_call_info - { - public: - outcome_of_bind (const call_details &cd, bool success) - : succeed_or_fail_call_info (cd, success) - {} - - bool update_model (region_model *model, - const exploded_edge *, - region_model_context *ctxt) const final override - { - const call_details cd (get_call_details (model, ctxt)); - return cd.get_model ()->on_bind (cd, m_success); - } - }; - - bool matches_call_types_p (const call_details &cd) const final override - { - return (cd.num_args () == 3 && cd.arg_is_pointer_p (1)); - } - - void impl_call_post (const call_details &cd) const final override - { - if (cd.get_ctxt ()) - { - cd.get_ctxt ()->bifurcate (make_unique (cd, false)); - cd.get_ctxt ()->bifurcate (make_unique (cd, true)); - cd.get_ctxt ()->terminate_path (); - } - } -}; - /* Handler for "__builtin_expect" etc. */ class kf_expect : public internal_known_function @@ -723,45 +646,6 @@ kf_calloc::impl_call_pre (const call_details &cd) const } } -/* Handle calls to "connect". - See e.g. https://man7.org/linux/man-pages/man3/connect.3p.html */ - -class kf_connect : public known_function -{ -public: - class outcome_of_connect : public succeed_or_fail_call_info - { - public: - outcome_of_connect (const call_details &cd, bool success) - : succeed_or_fail_call_info (cd, success) - {} - - bool update_model (region_model *model, - const exploded_edge *, - region_model_context *ctxt) const final override - { - const call_details cd (get_call_details (model, ctxt)); - return cd.get_model ()->on_connect (cd, m_success); - } - }; - - bool matches_call_types_p (const call_details &cd) const final override - { - return (cd.num_args () == 3 - && cd.arg_is_pointer_p (1)); - } - - void impl_call_post (const call_details &cd) const final override - { - if (cd.get_ctxt ()) - { - cd.get_ctxt ()->bifurcate (make_unique (cd, false)); - cd.get_ctxt ()->bifurcate (make_unique (cd, true)); - cd.get_ctxt ()->terminate_path (); - } - } -}; - /* Handler for glibc's "__errno_location". */ class kf_errno_location : public known_function @@ -933,43 +817,6 @@ public: for the analyzer). */ }; -/* Handle calls to "listen". - See e.g. https://man7.org/linux/man-pages/man3/listen.3p.html */ - -class kf_listen : public known_function -{ - class outcome_of_listen : public succeed_or_fail_call_info - { - public: - outcome_of_listen (const call_details &cd, bool success) - : succeed_or_fail_call_info (cd, success) - {} - - bool update_model (region_model *model, - const exploded_edge *, - region_model_context *ctxt) const final override - { - const call_details cd (get_call_details (model, ctxt)); - return cd.get_model ()->on_listen (cd, m_success); - } - }; - - bool matches_call_types_p (const call_details &cd) const final override - { - return cd.num_args () == 2; - } - - void impl_call_post (const call_details &cd) const final override - { - if (cd.get_ctxt ()) - { - cd.get_ctxt ()->bifurcate (make_unique (cd, false)); - cd.get_ctxt ()->bifurcate (make_unique (cd, true)); - cd.get_ctxt ()->terminate_path (); - } - } -}; - /* Handle the on_call_pre part of "malloc". */ class kf_malloc : public known_function @@ -1077,92 +924,6 @@ kf_memset::impl_call_pre (const call_details &cd) const model->fill_region (sized_dest_reg, fill_value_u8); } -/* Handler for calls to "pipe" and "pipe2". - See e.g. https://www.man7.org/linux/man-pages/man2/pipe.2.html */ - -class kf_pipe : public known_function -{ - class failure : public failed_call_info - { - public: - failure (const call_details &cd) : failed_call_info (cd) {} - - bool update_model (region_model *model, - const exploded_edge *, - region_model_context *ctxt) const final override - { - /* Return -1; everything else is unchanged. */ - const call_details cd (get_call_details (model, ctxt)); - model->update_for_int_cst_return (cd, -1, true); - return true; - } - }; - - class success : public success_call_info - { - public: - success (const call_details &cd) : success_call_info (cd) {} - - bool update_model (region_model *model, - const exploded_edge *, - region_model_context *ctxt) const final override - { - const call_details cd (get_call_details (model, ctxt)); - - /* Return 0. */ - model->update_for_zero_return (cd, true); - - /* Update fd array. */ - region_model_manager *mgr = cd.get_manager (); - tree arr_tree = cd.get_arg_tree (0); - const svalue *arr_sval = cd.get_arg_svalue (0); - for (int idx = 0; idx < 2; idx++) - { - const region *arr_reg - = model->deref_rvalue (arr_sval, arr_tree, cd.get_ctxt ()); - const svalue *idx_sval - = mgr->get_or_create_int_cst (integer_type_node, idx); - const region *element_reg - = mgr->get_element_region (arr_reg, integer_type_node, idx_sval); - conjured_purge p (model, cd.get_ctxt ()); - const svalue *fd_sval - = mgr->get_or_create_conjured_svalue (integer_type_node, - cd.get_call_stmt (), - element_reg, - p); - model->set_value (element_reg, fd_sval, cd.get_ctxt ()); - model->mark_as_valid_fd (fd_sval, cd.get_ctxt ()); - } - return true; - } - }; - -public: - kf_pipe (unsigned num_args) - : m_num_args (num_args) - { - gcc_assert (num_args > 0); - } - - bool matches_call_types_p (const call_details &cd) const final override - { - return (cd.num_args () == m_num_args && cd.arg_is_pointer_p (0)); - } - - void impl_call_post (const call_details &cd) const final override - { - if (cd.get_ctxt ()) - { - cd.get_ctxt ()->bifurcate (make_unique (cd)); - cd.get_ctxt ()->bifurcate (make_unique (cd)); - cd.get_ctxt ()->terminate_path (); - } - } - -private: - unsigned m_num_args; -}; - /* A subclass of pending_diagnostic for complaining about 'putenv' called on an auto var. */ @@ -1587,44 +1348,6 @@ kf_realloc::impl_call_post (const call_details &cd) const } } -/* Handle calls to "socket". - See e.g. https://man7.org/linux/man-pages/man3/socket.3p.html */ - -class kf_socket : public known_function -{ -public: - class outcome_of_socket : public succeed_or_fail_call_info - { - public: - outcome_of_socket (const call_details &cd, bool success) - : succeed_or_fail_call_info (cd, success) - {} - - bool update_model (region_model *model, - const exploded_edge *, - region_model_context *ctxt) const final override - { - const call_details cd (get_call_details (model, ctxt)); - return cd.get_model ()->on_socket (cd, m_success); - } - }; - - bool matches_call_types_p (const call_details &cd) const final override - { - return cd.num_args () == 3; - } - - void impl_call_post (const call_details &cd) const final override - { - if (cd.get_ctxt ()) - { - cd.get_ctxt ()->bifurcate (make_unique (cd, false)); - cd.get_ctxt ()->bifurcate (make_unique (cd, true)); - cd.get_ctxt ()->terminate_path (); - } - } -}; - /* Handler for "strchr" and "__builtin_strchr". */ class kf_strchr : public known_function @@ -1933,17 +1656,12 @@ register_known_functions (known_function_manager &kfm) /* Known POSIX functions, and some non-standard extensions. */ { - kfm.add ("accept", make_unique ()); - kfm.add ("bind", make_unique ()); - kfm.add ("connect", make_unique ()); kfm.add ("fgets", make_unique ()); kfm.add ("fgets_unlocked", make_unique ()); // non-standard kfm.add ("fread", make_unique ()); - kfm.add ("listen", make_unique ()); - kfm.add ("pipe", make_unique (1)); - kfm.add ("pipe2", make_unique (2)); kfm.add ("putenv", make_unique ()); - kfm.add ("socket", make_unique ()); + + register_known_fd_functions (kfm); } /* glibc functions. */ diff --git a/gcc/analyzer/sm-fd.cc b/gcc/analyzer/sm-fd.cc index f7779be7d26..af59aef401d 100644 --- a/gcc/analyzer/sm-fd.cc +++ b/gcc/analyzer/sm-fd.cc @@ -47,6 +47,7 @@ along with GCC; see the file COPYING3. If not see #include "analyzer/program-state.h" #include "analyzer/supergraph.h" #include "analyzer/analyzer-language.h" +#include "analyzer/call-info.h" #if ENABLE_ANALYZER @@ -2251,6 +2252,44 @@ region_model::mark_as_valid_fd (const svalue *sval, region_model_context *ctxt) fd_sm->mark_as_valid_fd (this, smap, sval, *ext_state); } +/* Handle calls to "socket". + See e.g. https://man7.org/linux/man-pages/man3/socket.3p.html */ + +class kf_socket : public known_function +{ +public: + class outcome_of_socket : public succeed_or_fail_call_info + { + public: + outcome_of_socket (const call_details &cd, bool success) + : succeed_or_fail_call_info (cd, success) + {} + + bool update_model (region_model *model, + const exploded_edge *, + region_model_context *ctxt) const final override + { + const call_details cd (get_call_details (model, ctxt)); + return cd.get_model ()->on_socket (cd, m_success); + } + }; + + bool matches_call_types_p (const call_details &cd) const final override + { + return cd.num_args () == 3; + } + + void impl_call_post (const call_details &cd) const final override + { + if (cd.get_ctxt ()) + { + cd.get_ctxt ()->bifurcate (make_unique (cd, false)); + cd.get_ctxt ()->bifurcate (make_unique (cd, true)); + cd.get_ctxt ()->terminate_path (); + } + } +}; + /* Specialcase hook for handling "socket", for use by kf_socket::outcome_of_socket::update_model. */ @@ -2269,6 +2308,44 @@ region_model::on_socket (const call_details &cd, bool successful) return fd_sm->on_socket (cd, successful, sm_ctxt.get (), *ext_state); } +/* Handle calls to "bind". + See e.g. https://man7.org/linux/man-pages/man3/bind.3p.html */ + +class kf_bind : public known_function +{ +public: + class outcome_of_bind : public succeed_or_fail_call_info + { + public: + outcome_of_bind (const call_details &cd, bool success) + : succeed_or_fail_call_info (cd, success) + {} + + bool update_model (region_model *model, + const exploded_edge *, + region_model_context *ctxt) const final override + { + const call_details cd (get_call_details (model, ctxt)); + return cd.get_model ()->on_bind (cd, m_success); + } + }; + + bool matches_call_types_p (const call_details &cd) const final override + { + return (cd.num_args () == 3 && cd.arg_is_pointer_p (1)); + } + + void impl_call_post (const call_details &cd) const final override + { + if (cd.get_ctxt ()) + { + cd.get_ctxt ()->bifurcate (make_unique (cd, false)); + cd.get_ctxt ()->bifurcate (make_unique (cd, true)); + cd.get_ctxt ()->terminate_path (); + } + } +}; + /* Specialcase hook for handling "bind", for use by kf_bind::outcome_of_bind::update_model. */ @@ -2287,6 +2364,43 @@ region_model::on_bind (const call_details &cd, bool successful) return fd_sm->on_bind (cd, successful, sm_ctxt.get (), *ext_state); } +/* Handle calls to "listen". + See e.g. https://man7.org/linux/man-pages/man3/listen.3p.html */ + +class kf_listen : public known_function +{ + class outcome_of_listen : public succeed_or_fail_call_info + { + public: + outcome_of_listen (const call_details &cd, bool success) + : succeed_or_fail_call_info (cd, success) + {} + + bool update_model (region_model *model, + const exploded_edge *, + region_model_context *ctxt) const final override + { + const call_details cd (get_call_details (model, ctxt)); + return cd.get_model ()->on_listen (cd, m_success); + } + }; + + bool matches_call_types_p (const call_details &cd) const final override + { + return cd.num_args () == 2; + } + + void impl_call_post (const call_details &cd) const final override + { + if (cd.get_ctxt ()) + { + cd.get_ctxt ()->bifurcate (make_unique (cd, false)); + cd.get_ctxt ()->bifurcate (make_unique (cd, true)); + cd.get_ctxt ()->terminate_path (); + } + } +}; + /* Specialcase hook for handling "listen", for use by kf_listen::outcome_of_listen::update_model. */ @@ -2305,6 +2419,45 @@ region_model::on_listen (const call_details &cd, bool successful) return fd_sm->on_listen (cd, successful, sm_ctxt.get (), *ext_state); } +/* Handle calls to "accept". + See e.g. https://man7.org/linux/man-pages/man3/accept.3p.html */ + +class kf_accept : public known_function +{ + class outcome_of_accept : public succeed_or_fail_call_info + { + public: + outcome_of_accept (const call_details &cd, bool success) + : succeed_or_fail_call_info (cd, success) + {} + + bool update_model (region_model *model, + const exploded_edge *, + region_model_context *ctxt) const final override + { + const call_details cd (get_call_details (model, ctxt)); + return cd.get_model ()->on_accept (cd, m_success); + } + }; + + bool matches_call_types_p (const call_details &cd) const final override + { + return (cd.num_args () == 3 + && cd.arg_is_pointer_p (1) + && cd.arg_is_pointer_p (2)); + } + + void impl_call_post (const call_details &cd) const final override + { + if (cd.get_ctxt ()) + { + cd.get_ctxt ()->bifurcate (make_unique (cd, false)); + cd.get_ctxt ()->bifurcate (make_unique (cd, true)); + cd.get_ctxt ()->terminate_path (); + } + } +}; + /* Specialcase hook for handling "accept", for use by kf_accept::outcome_of_accept::update_model. */ @@ -2323,6 +2476,45 @@ region_model::on_accept (const call_details &cd, bool successful) return fd_sm->on_accept (cd, successful, sm_ctxt.get (), *ext_state); } +/* Handle calls to "connect". + See e.g. https://man7.org/linux/man-pages/man3/connect.3p.html */ + +class kf_connect : public known_function +{ +public: + class outcome_of_connect : public succeed_or_fail_call_info + { + public: + outcome_of_connect (const call_details &cd, bool success) + : succeed_or_fail_call_info (cd, success) + {} + + bool update_model (region_model *model, + const exploded_edge *, + region_model_context *ctxt) const final override + { + const call_details cd (get_call_details (model, ctxt)); + return cd.get_model ()->on_connect (cd, m_success); + } + }; + + bool matches_call_types_p (const call_details &cd) const final override + { + return (cd.num_args () == 3 + && cd.arg_is_pointer_p (1)); + } + + void impl_call_post (const call_details &cd) const final override + { + if (cd.get_ctxt ()) + { + cd.get_ctxt ()->bifurcate (make_unique (cd, false)); + cd.get_ctxt ()->bifurcate (make_unique (cd, true)); + cd.get_ctxt ()->terminate_path (); + } + } +}; + /* Specialcase hook for handling "connect", for use by kf_connect::outcome_of_connect::update_model. */ @@ -2341,6 +2533,107 @@ region_model::on_connect (const call_details &cd, bool successful) return fd_sm->on_connect (cd, successful, sm_ctxt.get (), *ext_state); } +/* Handler for calls to "pipe" and "pipe2". + See e.g. https://www.man7.org/linux/man-pages/man2/pipe.2.html */ + +class kf_pipe : public known_function +{ + class failure : public failed_call_info + { + public: + failure (const call_details &cd) : failed_call_info (cd) {} + + bool update_model (region_model *model, + const exploded_edge *, + region_model_context *ctxt) const final override + { + /* Return -1; everything else is unchanged. */ + const call_details cd (get_call_details (model, ctxt)); + model->update_for_int_cst_return (cd, -1, true); + return true; + } + }; + + class success : public success_call_info + { + public: + success (const call_details &cd) : success_call_info (cd) {} + + bool update_model (region_model *model, + const exploded_edge *, + region_model_context *ctxt) const final override + { + const call_details cd (get_call_details (model, ctxt)); + + /* Return 0. */ + model->update_for_zero_return (cd, true); + + /* Update fd array. */ + region_model_manager *mgr = cd.get_manager (); + tree arr_tree = cd.get_arg_tree (0); + const svalue *arr_sval = cd.get_arg_svalue (0); + for (int idx = 0; idx < 2; idx++) + { + const region *arr_reg + = model->deref_rvalue (arr_sval, arr_tree, cd.get_ctxt ()); + const svalue *idx_sval + = mgr->get_or_create_int_cst (integer_type_node, idx); + const region *element_reg + = mgr->get_element_region (arr_reg, integer_type_node, idx_sval); + conjured_purge p (model, cd.get_ctxt ()); + const svalue *fd_sval + = mgr->get_or_create_conjured_svalue (integer_type_node, + cd.get_call_stmt (), + element_reg, + p); + model->set_value (element_reg, fd_sval, cd.get_ctxt ()); + model->mark_as_valid_fd (fd_sval, cd.get_ctxt ()); + } + return true; + } + }; + +public: + kf_pipe (unsigned num_args) + : m_num_args (num_args) + { + gcc_assert (num_args > 0); + } + + bool matches_call_types_p (const call_details &cd) const final override + { + return (cd.num_args () == m_num_args && cd.arg_is_pointer_p (0)); + } + + void impl_call_post (const call_details &cd) const final override + { + if (cd.get_ctxt ()) + { + cd.get_ctxt ()->bifurcate (make_unique (cd)); + cd.get_ctxt ()->bifurcate (make_unique (cd)); + cd.get_ctxt ()->terminate_path (); + } + } + +private: + unsigned m_num_args; +}; + +/* Populate KFM with instances of known functions relating to + file descriptors. */ + +void +register_known_fd_functions (known_function_manager &kfm) +{ + kfm.add ("accept", make_unique ()); + kfm.add ("bind", make_unique ()); + kfm.add ("connect", make_unique ()); + kfm.add ("listen", make_unique ()); + kfm.add ("pipe", make_unique (1)); + kfm.add ("pipe2", make_unique (2)); + kfm.add ("socket", make_unique ()); +} + } // namespace ana #endif // ENABLE_ANALYZER