From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam02olkn2066.outbound.protection.outlook.com [40.92.43.66]) by sourceware.org (Postfix) with ESMTPS id 2D1733857BBD for ; Fri, 15 Jul 2022 15:38:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2D1733857BBD ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=IwuXzhYFTEs6+7BkT2pa3M1PSRiNREd1l2v6AquI3ZsycJVkBnCbTILC0M6h5d4kUasIGJ7ySpaMUNmNUPxTN1/EqqYNJ4KCz4laqY+37BdshwGQUV6iXvqpPM0CEDNeZj0v6BkIze1cheB0yV3yc2xTTgslznw8fpzVx1BjO/QKR/da+Y0Z4gj8UNsFGv0UBxzuBhbFvkxEcZX9Xbizt/k6p1AwqaNxzoFVX0Z8A0RkwYip9XhRf4lo57hemMDpnngNX0vCZo7qP3VyD0ycR1X+k8Fy2WydSPsU/viviWLtEzxNoH7xB90K2ffrGePCNOxM8TCamb8Nah+KGKtEBQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=1Q8Wu/gtsn0Uy0vo9FmiFWwg4wFEpDK+DL4yTkbMjEs=; b=E1Q1XiJrVYXi7AiC27g1BXetZXW5vuU8t/8fSpj9sFSuMh+r7LhkscqqF5sy8qi2Ize3SMJNPCjXSnyxzSSuuM5FjxrMRiXehBdLlYgHGsQvu9IvaoEZHsMJ7RIF9Me/cDroqnX+v9I7vi8dVeIJ6e19S5TKAPo31as0aXvuE/7qyVu1Innvz5B4cLPliobq3LKFNmbRl/yN8iMavCMjZ8PdetTendpvKg2gdea83RFonibLap6ACfkxjYsp5w+HIcOV57zFvH6gvueebPCIYxbISJWeeWnckbBkRESVadg8QXn5hpJ/Rk9u4z1KNNxhGneuE1rDXAAYx5HBxJF6zQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none Received: from MWHPR1801MB1919.namprd18.prod.outlook.com (2603:10b6:301:64::30) by BY5PR18MB3425.namprd18.prod.outlook.com (2603:10b6:a03:1af::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5438.19; Fri, 15 Jul 2022 15:38:42 +0000 Received: from MWHPR1801MB1919.namprd18.prod.outlook.com ([fe80::2011:bbe1:6867:3eee]) by MWHPR1801MB1919.namprd18.prod.outlook.com ([fe80::2011:bbe1:6867:3eee%7]) with mapi id 15.20.5438.014; Fri, 15 Jul 2022 15:38:42 +0000 From: Immad Mir To: gcc-patches@gcc.gnu.org Cc: dmalcolm@redhat.com, mirimnan017@gmail.com, Immad Mir Subject: [PATCH] Adding three new function attributes for static analysis of file descriptors Date: Fri, 15 Jul 2022 21:08:16 +0530 Message-ID: X-Mailer: git-send-email 2.25.1 Reply-To: mirimnan017@gmail.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain X-TMN: [IvmNhWOkneK3V6E/EdZI81slf/NmpxIvLe7p3t1APqsIH3LEv2UGIVnCg/Dr9tHe] X-ClientProxiedBy: BMXPR01CA0075.INDPRD01.PROD.OUTLOOK.COM (2603:1096:b00:54::15) To MWHPR1801MB1919.namprd18.prod.outlook.com (2603:10b6:301:64::30) X-Microsoft-Original-Message-ID: <20220715153816.10741-1-mirimmad@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 0808a290-36f8-4c49-9a17-08da66781852 X-MS-Exchange-SLBlob-MailProps: uIjBWfsAVx9c52Rras6kbyMilq/6aclYWVqM8CR4WaCoemeD3VbBk5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OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 0808a290-36f8-4c49-9a17-08da66781852 X-MS-Exchange-CrossTenant-AuthSource: MWHPR1801MB1919.namprd18.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Jul 2022 15:38:42.1365 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR18MB3425 X-Spam-Status: No, score=-11.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, FREEMAIL_REPLYTO, FREEMAIL_REPLYTO_END_DIGIT, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS, SPF_PASS, TXREP, T_FILL_THIS_FORM_SHORT autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 15 Jul 2022 15:38:47 -0000 --- gcc/analyzer/sm-fd.cc | 257 ++++++++++++++++++++++++--- gcc/c-family/c-attribs.cc | 115 ++++++++++++ gcc/doc/extend.texi | 19 ++ gcc/testsuite/gcc.dg/analyzer/fd-5.c | 53 ++++++ gcc/testsuite/gcc.dg/analyzer/fd-6.c | 14 ++ 5 files changed, 431 insertions(+), 27 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/analyzer/fd-5.c create mode 100644 gcc/testsuite/gcc.dg/analyzer/fd-6.c diff --git a/gcc/analyzer/sm-fd.cc b/gcc/analyzer/sm-fd.cc index 8e4300b06e2..20018dfd12b 100644 --- a/gcc/analyzer/sm-fd.cc +++ b/gcc/analyzer/sm-fd.cc @@ -39,10 +39,13 @@ along with GCC; see the file COPYING3. If not see #include "analyzer/analyzer-selftests.h"=0D #include "tristate.h"=0D #include "selftest.h"=0D +#include "stringpool.h"=0D +#include "attribs.h"=0D #include "analyzer/call-string.h"=0D #include "analyzer/program-point.h"=0D #include "analyzer/store.h"=0D #include "analyzer/region-model.h"=0D +#include "bitmap.h"=0D =0D #if ENABLE_ANALYZER=0D =0D @@ -59,6 +62,13 @@ enum access_mode WRITE_ONLY=0D };=0D =0D +enum fd_access_direction=0D +{=0D + DIR_READ_WRITE,=0D + DIR_READ,=0D + DIR_WRITE=0D +};=0D +=0D class fd_state_machine : public state_machine=0D {=0D public:=0D @@ -104,6 +114,8 @@ public: bool is_readonly_fd_p (state_t s) const;=0D bool is_writeonly_fd_p (state_t s) const;=0D enum access_mode get_access_mode_from_flag (int flag) const;=0D + void inform_filedescriptor_attribute (tree callee_fndecl, int arg,=0D + fd_access_direction fd_dir) const;= =0D =0D /* State for a constant file descriptor (>=3D 0) */=0D state_t m_constant_fd;=0D @@ -146,7 +158,7 @@ private: void check_for_open_fd (sm_context *sm_ctxt, const supernode *node,=0D const gimple *stmt, const gcall *call,=0D const tree callee_fndecl,=0D - enum access_direction access_fn) const;=0D + enum fd_access_direction access_fn) const;=0D =0D void make_valid_transitions_on_condition (sm_context *sm_ctxt,=0D const supernode *node,=0D @@ -156,6 +168,12 @@ private: const supernode *node,=0D const gimple *stmt,=0D const svalue *lhs) const;=0D + bitmap get_fd_attrs (const char *attr_name, tree callee_fndecl) const;=0D + void check_for_fd_attrs (sm_context *sm_ctxt, const supernode *node,=0D + const gimple *stmt, const gcall *call,=0D + const tree callee_fndecl, bitmap argmap,=0D + fd_access_direction fd_attr_access_dir) const;=0D +=0D };=0D =0D /* Base diagnostic class relative to fd_state_machine. */=0D @@ -294,10 +312,10 @@ class fd_access_mode_mismatch : public fd_diagnostic {=0D public:=0D fd_access_mode_mismatch (const fd_state_machine &sm, tree arg,=0D - enum access_direction fd_dir,=0D - const tree callee_fndecl)=0D + enum fd_access_direction fd_dir,=0D + const tree callee_fndecl, bool attr, int arg_id= x)=0D : fd_diagnostic (sm, arg), m_fd_dir (fd_dir),=0D - m_callee_fndecl (callee_fndecl)=0D + m_callee_fndecl (callee_fndecl), m_attr (attr), m_arg_idx (arg_idx= )=0D =0D {=0D }=0D @@ -317,19 +335,27 @@ public: bool=0D emit (rich_location *rich_loc) final override=0D {=0D + bool warned;=0D switch (m_fd_dir)=0D {=0D case DIR_READ:=0D - return warning_at (rich_loc, get_controlling_option (),=0D + warned =3D warning_at (rich_loc, get_controlling_option (),=0D "%qE on % file descriptor %qE",=0D m_callee_fndecl, m_arg);=0D + break;=0D case DIR_WRITE:=0D - return warning_at (rich_loc, get_controlling_option (),=0D + warned =3D warning_at (rich_loc, get_controlling_option (),=0D "%qE on % file descriptor %qE",=0D m_callee_fndecl, m_arg);=0D + break;=0D default:=0D gcc_unreachable ();=0D }=0D + if (warned && m_attr)=0D + {=0D + m_sm.inform_filedescriptor_attribute (m_callee_fndecl, m_arg_idx, = m_fd_dir);=0D + }=0D + return warned;=0D }=0D =0D bool=0D @@ -359,8 +385,10 @@ public: }=0D =0D private:=0D - enum access_direction m_fd_dir;=0D + enum fd_access_direction m_fd_dir;=0D const tree m_callee_fndecl;=0D + bool m_attr;=0D + int m_arg_idx;=0D };=0D =0D class double_close : public fd_diagnostic=0D @@ -422,8 +450,9 @@ class fd_use_after_close : public fd_diagnostic {=0D public:=0D fd_use_after_close (const fd_state_machine &sm, tree arg,=0D - const tree callee_fndecl)=0D - : fd_diagnostic (sm, arg), m_callee_fndecl (callee_fndecl)=0D + const tree callee_fndecl, bool attr, int arg_idx)=0D + : fd_diagnostic (sm, arg), m_callee_fndecl (callee_fndecl), m_attr (= attr),=0D + m_arg_idx (arg_idx)=0D {=0D }=0D =0D @@ -439,12 +468,27 @@ public: return OPT_Wanalyzer_fd_use_after_close;=0D }=0D =0D + bool=0D + subclass_equal_p (const pending_diagnostic &base_other) const override=0D + {=0D + const fd_use_after_close &sub_other=0D + =3D (const fd_use_after_close &)base_other;=0D + return (same_tree_p (m_arg, sub_other.m_arg)=0D + && m_callee_fndecl =3D=3D sub_other.m_callee_fndecl=0D + && m_attr =3D=3D sub_other.m_attr=0D + && m_arg_idx =3D=3D sub_other.m_arg_idx);=0D + }=0D +=0D bool=0D emit (rich_location *rich_loc) final override=0D {=0D - return warning_at (rich_loc, get_controlling_option (),=0D + bool warned;=0D + warned =3D warning_at (rich_loc, get_controlling_option (),=0D "%qE on closed file descriptor %qE", m_callee_fndec= l,=0D m_arg);=0D + if (warned && m_attr)=0D + m_sm.inform_filedescriptor_attribute (m_callee_fndecl, m_arg_idx, DI= R_READ_WRITE);=0D + return warned;=0D }=0D =0D label_text=0D @@ -466,25 +510,29 @@ public: describe_final_event (const evdesc::final_event &ev) final override=0D {=0D if (m_first_close_event.known_p ())=0D - return ev.formatted_print (=0D - "%qE on closed file descriptor %qE; %qs was at %@", m_callee_fnd= ecl,=0D - m_arg, "close", &m_first_close_event);=0D - else=0D - return ev.formatted_print ("%qE on closed file descriptor %qE",=0D - m_callee_fndecl, m_arg);=0D + return ev.formatted_print (=0D + "%qE on closed file descriptor %qE; %qs was at %@", m_callee_f= ndecl,=0D + m_arg, "close", &m_first_close_event);=0D + else=0D + return ev.formatted_print ("%qE on closed file descriptor %qE",=0D + m_callee_fndecl, m_arg);=0D }=0D =0D private:=0D diagnostic_event_id_t m_first_close_event;=0D const tree m_callee_fndecl;=0D + bool m_attr;=0D + int m_arg_idx;=0D };=0D =0D class unchecked_use_of_fd : public fd_diagnostic=0D {=0D public:=0D unchecked_use_of_fd (const fd_state_machine &sm, tree arg,=0D - const tree callee_fndecl)=0D - : fd_diagnostic (sm, arg), m_callee_fndecl (callee_fndecl)=0D + const tree callee_fndecl, bool attr,=0D + int arg_idx)=0D + : fd_diagnostic (sm, arg), m_callee_fndecl (callee_fndecl),=0D + m_attr (attr), m_arg_idx (arg_idx)=0D {=0D }=0D =0D @@ -503,9 +551,13 @@ public: bool=0D emit (rich_location *rich_loc) final override=0D {=0D - return warning_at (rich_loc, get_controlling_option (),=0D - "%qE on possibly invalid file descriptor %qE",=0D - m_callee_fndecl, m_arg);=0D + bool warned;=0D + warned =3D warning_at (rich_loc, get_controlling_option (),=0D + "%qE on possibly invalid file descriptor %qE",=0D + m_callee_fndecl, m_arg);=0D + if (warned && m_attr)=0D + m_sm.inform_filedescriptor_attribute (m_callee_fndecl, m_arg_idx,=0D + DIR_READ_WRITE);=0D }=0D =0D bool=0D @@ -514,7 +566,9 @@ public: const unchecked_use_of_fd &sub_other=0D =3D (const unchecked_use_of_fd &)base_other;=0D return (same_tree_p (m_arg, sub_other.m_arg)=0D - && m_callee_fndecl =3D=3D sub_other.m_callee_fndecl);=0D + && m_callee_fndecl =3D=3D sub_other.m_callee_fndecl=0D + && m_attr =3D=3D sub_other.m_attr=0D + && m_arg_idx =3D=3D sub_other.m_arg_idx);=0D }=0D =0D label_text=0D @@ -543,8 +597,40 @@ public: private:=0D diagnostic_event_id_t m_first_open_event;=0D const tree m_callee_fndecl;=0D + bool m_attr;=0D + int m_arg_idx;=0D + =0D };=0D =0D +/* Issue a note regarding misuse of a file descriptor.=0D + ARG_IDX here is 0-based.=0D + */=0D +void=0D +fd_state_machine::inform_filedescriptor_attribute (=0D + tree callee_fndecl, int arg_idx, fd_access_direction fd_dir) const=0D +{=0D + switch (fd_dir)=0D + {=0D + case READ_WRITE:=0D + inform (DECL_SOURCE_LOCATION (callee_fndecl),=0D + "argument %d of %qD must be an open file descriptor", arg_id= x + 1,=0D + callee_fndecl);=0D + break;=0D + case DIR_WRITE:=0D + inform (DECL_SOURCE_LOCATION (callee_fndecl),=0D + "argument %d of %qD must be a read-only file descriptor",=0D + arg_idx + 1, callee_fndecl);=0D + break;=0D + case DIR_READ:=0D + inform (DECL_SOURCE_LOCATION (callee_fndecl),=0D + "argument %d of %qD must be a write-only file descriptor",=0D + arg_idx + 1, callee_fndecl);=0D + break;=0D + }=0D +}=0D +=0D +=0D +=0D fd_state_machine::fd_state_machine (logger *logger)=0D : state_machine ("file-descriptor", logger),=0D m_constant_fd (add_state ("fd-constant")),=0D @@ -647,11 +733,126 @@ fd_state_machine::on_stmt (sm_context *sm_ctxt, cons= t supernode *node, on_read (sm_ctxt, node, stmt, call, callee_fndecl);=0D return true;=0D } // "read"=0D +=0D + =0D + {=0D + // Handle __attribute__((fd_arg))=0D + bitmap argmap =3D get_fd_attrs ("fd_arg", callee_fndecl);=0D + if (argmap)=0D + check_for_fd_attrs (sm_ctxt, node, stmt, call, callee_fndecl= , argmap, DIR_READ_WRITE);=0D + =0D + // Handle __attribute__((fd_arg_read))=0D + bitmap read_argmap =3D get_fd_attrs ("fd_arg_read", callee_fnd= ecl);=0D + if(read_argmap)=0D + check_for_fd_attrs (sm_ctxt, node, stmt, call, callee_fndecl= , read_argmap, DIR_READ);=0D + =0D + // Handle __attribute__((fd_arg_write))=0D + bitmap write_argmap =3D get_fd_attrs ("fd_arg_write", callee_f= ndecl);=0D + if (write_argmap)=0D + check_for_fd_attrs (sm_ctxt, node, stmt, call, callee_fndecl= , write_argmap, DIR_WRITE);=0D + =0D + return true;=0D + }=0D + =0D }=0D =0D return false;=0D }=0D =0D +void=0D +fd_state_machine::check_for_fd_attrs (sm_context *sm_ctxt,=0D + const supernode *node, const gimple = *stmt,=0D + const gcall *call,=0D + const tree callee_fndecl, bitmap arg= map,=0D + fd_access_direction fd_attr_access_d= ir) const=0D +{=0D + for (unsigned i =3D 0; i < gimple_call_num_args (stmt); i++)=0D + {=0D + unsigned int arg_idx =3D i;=0D + tree arg =3D gimple_call_arg (stmt, i);=0D + tree diag_arg =3D sm_ctxt->get_diagnostic_tree (arg);=0D + state_t state =3D sm_ctxt->get_state (stmt, arg);=0D + if (TREE_CODE (TREE_TYPE (arg)) !=3D INTEGER_TYPE)=0D + continue;=0D + if (bitmap_bit_p (argmap, arg_idx)) // check if the arg is any of th= e file=0D + // descriptor attributes=0D + {=0D +=0D + if (is_closed_fd_p (state))=0D + {=0D +=0D + sm_ctxt->warn (node, stmt, arg,=0D + new fd_use_after_close (*this, diag_arg,=0D + callee_fndecl, true,= =0D + arg_idx));=0D + continue;=0D + }=0D + else=0D + {=0D + if (!(is_valid_fd_p (state) || (state =3D=3D m_stop)))=0D + {=0D + if (!is_constant_fd_p (state))=0D + sm_ctxt->warn (node, stmt, arg,=0D + new unchecked_use_of_fd (*this, diag_ar= g,=0D + callee_fndecl,= true,=0D + arg_idx));=0D + }=0D + }=0D + }=0D + switch (fd_attr_access_dir)=0D + {=0D + case DIR_READ:=0D + if (bitmap_bit_p (argmap, arg_idx)) // check if the arg is marke= d by=0D + // reads_from_fd attribute=0D + {=0D + if (is_writeonly_fd_p (state))=0D + {=0D + sm_ctxt->warn (node, stmt, arg,=0D + new fd_access_mode_mismatch (*this, diag_= arg,=0D + DIR_WRITE,=0D + callee_fndec= l, true, i));=0D + }=0D + }=0D + break;=0D + case DIR_WRITE:=0D + if (bitmap_bit_p (argmap, arg_idx)) // check if the arg is marke= d by=0D + // writes_to_fd attribute=0D + {=0D + if (is_readonly_fd_p (state))=0D + {=0D + sm_ctxt->warn (node, stmt, arg,=0D + new fd_access_mode_mismatch (=0D + *this, diag_arg, DIR_READ, callee_fnd= ecl, true, i));=0D + }=0D + }=0D + break;=0D + }=0D + }=0D +}=0D +=0D +bitmap=0D +fd_state_machine::get_fd_attrs (const char *attr_name, tree callee_fndecl)= const=0D +{=0D + bitmap argmap =3D NULL;=0D + tree attrs =3D TYPE_ATTRIBUTES (TREE_TYPE (callee_fndecl));=0D + attrs =3D lookup_attribute (attr_name, attrs);=0D + if (!attrs)=0D + return argmap;=0D +=0D + if (!TREE_VALUE (attrs))=0D + return argmap;=0D +=0D + argmap =3D BITMAP_ALLOC (NULL);=0D +=0D + for (tree idx =3D TREE_VALUE (attrs); idx; idx =3D TREE_CHAIN (idx))=0D + {=0D + unsigned int val =3D TREE_INT_CST_LOW (TREE_VALUE (idx)) - 1;=0D + bitmap_set_bit (argmap, val);=0D + }=0D + return argmap;=0D +}=0D +=0D +=0D void=0D fd_state_machine::on_open (sm_context *sm_ctxt, const supernode *node,=0D const gimple *stmt, const gcall *call) const=0D @@ -729,7 +930,7 @@ void fd_state_machine::check_for_open_fd (=0D sm_context *sm_ctxt, const supernode *node, const gimple *stmt,=0D const gcall *call, const tree callee_fndecl,=0D - enum access_direction callee_fndecl_dir) const=0D + enum fd_access_direction callee_fndecl_dir) const=0D {=0D tree arg =3D gimple_call_arg (call, 0);=0D tree diag_arg =3D sm_ctxt->get_diagnostic_tree (arg);=0D @@ -738,7 +939,7 @@ fd_state_machine::check_for_open_fd ( if (is_closed_fd_p (state))=0D {=0D sm_ctxt->warn (node, stmt, arg,=0D - new fd_use_after_close (*this, diag_arg, callee_fndec= l));=0D + new fd_use_after_close (*this, diag_arg, callee_fndec= l, false, -1));=0D }=0D =0D else=0D @@ -748,7 +949,7 @@ fd_state_machine::check_for_open_fd ( if (!is_constant_fd_p (state))=0D sm_ctxt->warn (=0D node, stmt, arg,=0D - new unchecked_use_of_fd (*this, diag_arg, callee_fndecl));= =0D + new unchecked_use_of_fd (*this, diag_arg, callee_fndecl, f= alse, -1));=0D }=0D switch (callee_fndecl_dir)=0D {=0D @@ -758,7 +959,7 @@ fd_state_machine::check_for_open_fd ( tree diag_arg =3D sm_ctxt->get_diagnostic_tree (arg);=0D sm_ctxt->warn (node, stmt, arg,=0D new fd_access_mode_mismatch (=0D - *this, diag_arg, DIR_WRITE, callee_fndecl= ));=0D + *this, diag_arg, DIR_WRITE, callee_fndecl= , false, -1));=0D }=0D =0D break;=0D @@ -769,9 +970,11 @@ fd_state_machine::check_for_open_fd ( tree diag_arg =3D sm_ctxt->get_diagnostic_tree (arg);=0D sm_ctxt->warn (node, stmt, arg,=0D new fd_access_mode_mismatch (=0D - *this, diag_arg, DIR_READ, callee_fndecl)= );=0D + *this, diag_arg, DIR_READ, callee_fndecl,= false, -1));=0D }=0D break;=0D + default:=0D + gcc_unreachable ();=0D }=0D }=0D }=0D diff --git a/gcc/c-family/c-attribs.cc b/gcc/c-family/c-attribs.cc index c8d96723f4c..1a8f91d6222 100644 --- a/gcc/c-family/c-attribs.cc +++ b/gcc/c-family/c-attribs.cc @@ -173,6 +173,10 @@ static tree handle_objc_nullability_attribute (tree *,= tree, tree, int, bool *); static tree handle_signed_bool_precision_attribute (tree *, tree, tree, in= t, bool *); static tree handle_retain_attribute (tree *, tree, tree, int, bool *); +static tree handle_fd_arg_attribute (tree *, tree, tree, int, bool *); +static tree handle_fd_arg_read_attribute (tree *, tree, tree, int, bool *); +static tree handle_fd_arg_write_attribute (tree *, tree, tree, int, bool *= ); + =20 /* Helper to define attribute exclusions. */ #define ATTR_EXCL(name, function, type, variable) \ @@ -427,6 +431,12 @@ const struct attribute_spec c_common_attribute_table[]= =3D handle_tls_model_attribute, NULL }, { "nonnull", 0, -1, false, true, true, false, handle_nonnull_attribute, NULL }, + { "fd_arg", 0, -1, false, true, true, false, + handle_fd_arg_attribute, NULL},=20=20=20=20=20=20 + { "fd_arg_read", 0, -1, false, true, true, false, + handle_fd_arg_read_attribute, NULL}, + { "fd_arg_write", 0, -1, false, true, true, false, + handle_fd_arg_write_attribute, NULL},=20=20=20=20=20=20=20=20= =20 { "nonstring", 0, 0, true, false, false, false, handle_nonstring_attribute, NULL }, { "nothrow", 0, 0, true, false, false, false, @@ -4521,6 +4531,111 @@ handle_nonnull_attribute (tree *node, tree name, return NULL_TREE; } =20 +/* Handle the "fd_arg" attribute */ + +static tree +handle_fd_arg_attribute (tree *node, tree name, tree args, + int ARG_UNUSED (flags), bool *no_add_attrs) +{ + tree type =3D *node; + if (!args) + { + if (!prototype_p (type)) + { + error ("%qE attribute without arguments on a non-prototype", nam= e); + *no_add_attrs =3D true; + } + return NULL_TREE; + } + + for (int i =3D 1; args; ++i) + { + tree pos =3D TREE_VALUE (args); + tree next =3D TREE_CHAIN (args); + if (tree val =3D positional_argument (type, name, pos, INTEGER_TYPE, + next || i > 1 ? i : 0)) + TREE_VALUE (args) =3D val; + else + { + *no_add_attrs =3D true; + break; + } + args =3D next; + } + return NULL_TREE; +} +=20=20 +/* Handle the "fd_arg_read" attribute. */ + +static tree +handle_fd_arg_read_attribute (tree *node, tree name, tree ARG_UNUSED (args= ), + int ARG_UNUSED (flags), bool *no_add_attrs) +{ + tree type =3D *node; + if (!args) + { + if (!prototype_p (type)) + { + error ("%qE attribute without arguments on a non-prototype", nam= e); + *no_add_attrs =3D true; + } + return NULL_TREE; + } + + for (int i =3D 1; args; ++i) + { + tree pos =3D TREE_VALUE (args); + tree next =3D TREE_CHAIN (args); + if (tree val =3D positional_argument (type, name, pos, INTEGER_TYPE, + next || i > 1 ? i : 0)) + TREE_VALUE (args) =3D val; + else + { + *no_add_attrs =3D true; + break; + } + args =3D next; + } + return NULL_TREE; +} + + +/* Handle the "fd_arg_write" attribute. */ +static tree +handle_fd_arg_write_attribute (tree *node, tree name, tree ARG_UNUSED (arg= s), + int ARG_UNUSED (flags), bool *no_add_attrs) +{ + tree type =3D *node; + if (!args) + { + if (!prototype_p (type)) + { + error ("%qE attribute without arguments on a non-prototype", nam= e); + *no_add_attrs =3D true; + } + return NULL_TREE; + } + + for (int i =3D 1; args; ++i) + { + tree pos =3D TREE_VALUE (args); + tree next =3D TREE_CHAIN (args); + if (tree val =3D positional_argument (type, name, pos, INTEGER_TYPE, + next || i > 1 ? i : 0)) + TREE_VALUE (args) =3D val; + else + { + *no_add_attrs =3D true; + break; + } + args =3D next; + } + return NULL_TREE; +} + + + + /* Handle the "nonstring" variable attribute. */ =20 static tree diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index dfbe33ac652..eba86e9f7ef 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -3007,6 +3007,25 @@ produced by @command{gold}. For other linkers that cannot generate resolution file, explicit @code{externally_visible} attributes are still necessary. =20 +@item fd_arg (@Var{N}, @Var{...}) + +The @Var{fd_arg} attribute may be applied to a function that takes an open= file +descriptor at referenced argument @Var{N}. It indicates that the passed fi= le +descriptor must not have been closed. + +@item fd_arg_read (@Var{N}, @Var{...}) + +The @Var{fd_arg_read} attribute may be applied to function that takes an o= pen file +descriptor at referenced argument @Var{N} that it might read from. It=20 +indicates that the passed file descriptor must not have been closed or +not opened as write-only. + +@item fd_arg_write (@Var{N}, @Var{...}) + +The @Var{fd_arg_write} attribute may be applied to a function that takes a= n open +file descriptor at referenced argument @Var{N} that it might write to. It = indicates +that the passed file descriptor must not have been closed or not opened as= read-only. + @item flatten @cindex @code{flatten} function attribute Generally, inlining into a function is limited. For a function marked with diff --git a/gcc/testsuite/gcc.dg/analyzer/fd-5.c b/gcc/testsuite/gcc.dg/an= alyzer/fd-5.c new file mode 100644 index 00000000000..6287ddd38fc --- /dev/null +++ b/gcc/testsuite/gcc.dg/analyzer/fd-5.c @@ -0,0 +1,53 @@ +int open(const char *, int mode);=0D +void close(int fd);=0D +int write (int fd, void *buf, int nbytes);=0D +int read (int fd, void *buf, int nbytes);=0D +=0D +#define O_RDONLY 0=0D +#define O_WRONLY 1=0D +#define O_RDWR 2=0D +=0D +void f (int fd) __attribute__((fd_arg(1))); /* { dg-message "argument 1 of= 'f' must be an open file descriptor" } */=0D +=0D +void=0D +test_1 (const char *path)=0D +{=0D + int fd =3D open (path, O_RDWR);=0D + close(fd);=0D + f(fd); /* { dg-warning "'f' on closed file descriptor 'fd'" } */=0D + /* { dg-message "\\(3\\) 'f' on closed file descriptor 'fd'; 'close'= was at \\(2\\)" "" { target *-*-* } .-1 } */=0D +}=0D +=0D +void g (int fd) __attribute__((fd_arg_read(1))); /* { dg-message "argument= 1 of 'g' must be a read-only file descriptor" } */=0D +=0D +void=0D +test_2 (const char *path)=0D +{=0D + int fd =3D open (path, O_WRONLY);=0D + if (fd !=3D -1)=0D + {=0D + g (fd); /* { dg-warning "'g' on 'write-only' file descriptor 'fd'" } *= /=0D + }=0D + close (fd);=0D +}=0D +=0D +void h (int fd) __attribute__((fd_arg_write(1))); /* { dg-message "argumen= t 1 of 'h' must be a write-only file descriptor" } */=0D +void=0D +test_3 (const char *path)=0D +{=0D + int fd =3D open (path, O_RDONLY);=0D + if (fd !=3D -1)=0D + {=0D + h (fd); /* { dg-warning "'h' on 'read-only' file descriptor 'fd'" } */= =0D + }=0D + close(fd);=0D +}=0D +=0D +void ff (int fd) __attribute__((fd_arg(1))); /* { dg-message "argument 1 o= f 'ff' must be an open file descriptor" } */=0D +=0D +void test_4 (const char *path)=0D +{=0D + int fd =3D open (path, O_RDWR);=0D + ff (fd); /* { dg-warning "'ff' on possibly invalid file descriptor 'fd'"= } */=0D + close(fd);=0D +} \ No newline at end of file diff --git a/gcc/testsuite/gcc.dg/analyzer/fd-6.c b/gcc/testsuite/gcc.dg/an= alyzer/fd-6.c new file mode 100644 index 00000000000..7efb9e69b58 --- /dev/null +++ b/gcc/testsuite/gcc.dg/analyzer/fd-6.c @@ -0,0 +1,14 @@ +=0D +int not_a_fn __attribute__ ((fd_arg(1))); /* { dg-warning "'fd_arg' attrib= ute only applies to function types" } */=0D +=0D +void f (char *p) __attribute__ ((fd_arg(1))); /* { dg-warning "'fd_arg' at= tribute argument value '1' refers to parameter type 'char \\\*'" } */=0D +=0D +=0D +int not_a_fn_b __attribute__ ((fd_arg_read(1))); /* { dg-warning "'fd_arg_= read' attribute only applies to function types" } */=0D +=0D +void g (char *p) __attribute__ ((fd_arg_read(1))); /* { dg-warning "'fd_ar= g_read' attribute argument value '1' refers to parameter type 'char \\\*'" = } */=0D +=0D +=0D +int not_a_fn_c __attribute__ ((fd_arg_write(1))); /* { dg-warning "'fd_arg= _write' attribute only applies to function types" } */=0D +=0D +void f (char *p) __attribute__ ((fd_arg_write(1))); /* { dg-warning "'fd_a= rg_write' attribute argument value '1' refers to parameter type 'char \\\*'= " } */ \ No newline at end of file --=20 2.25.1