From patchwork Tue Jul 26 16:37:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Immad Mir X-Patchwork-Id: 190 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:6a10:b5d6:b0:2b9:3548:2db5 with SMTP id v22csp2369368pxt; Tue, 26 Jul 2022 09:38:44 -0700 (PDT) X-Google-Smtp-Source: AGRyM1ukW+9EBOyf4MzdCQ7FdJZp1oIWdkMILIQUw4ryh5V6cIkV5LXZ/wHHscKqOIBMLvJJWlD6 X-Received: by 2002:aa7:de08:0:b0:43c:84bc:fbaa with SMTP id h8-20020aa7de08000000b0043c84bcfbaamr2686663edv.238.1658853524269; Tue, 26 Jul 2022 09:38:44 -0700 (PDT) Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id i19-20020a1709064fd300b00726b8e79284si16393300ejw.763.2022.07.26.09.38.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Jul 2022 09:38:44 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=HEH5qXNz; arc=fail (signature failed); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E7175385843D for ; Tue, 26 Jul 2022 16:38:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E7175385843D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1658853522; bh=faKyog7GfXKUE4wYvQq/S035bGhy6quVT3ah3d/uxNY=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=HEH5qXNz3xd2kxAtZdRL4KzG2D5ITxRLNgO+hJLK3dXKPw9VaDetAjwezpVRpin95 MuRWLRsevbiFyv6Rxo5i6rxnLnFfH+F9jfngUhQiPy38tNQ/iAG37Q4iJNQNvNDD4z 0FWpsj1mEB+RDsGWm99b3H4/ceCXsorCeQo9vrLM= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10olkn2072.outbound.protection.outlook.com [40.92.40.72]) by sourceware.org (Postfix) with ESMTPS id 67D583858D28 for ; Tue, 26 Jul 2022 16:37:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 67D583858D28 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=C8GM6fw1vEXWsbHeyJyshFxnJa1lQVNnJcWB4CDCwJdFJvzn+TGmYbasbMRpVVjRLadn9TyLLRFnKFKQRsre74AphD/8S4mKJGAPh0I6KP8hJV4KxAeeRBVFx0FbfMzVdltclMrVypERKu4IQ+WH2ab+PHpSo1u+fVl+tFna8/5IdhaKVSztOWROOq8u57Xn+mykmu9ANYuKH0Q0uIvTbEvU3L7O8Md8fukuI68rntVAAonShNaucANE/J8nzPx4sgDqau851TwYudGs96FTPHG8dZg4gzta/Q3qRXTBdYvzQHfqAfv6D9xmnx4XlPD54L7ZH59caUXQyeCoFax31Q== 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=faKyog7GfXKUE4wYvQq/S035bGhy6quVT3ah3d/uxNY=; b=Oyql34KLZ01sbjL/TXvqQEOwyP0yG5LlrrNz0huKuCV4DEcKpg1YkYyUji5g8/jWvKmZZnlv1dhL3uqrOXQLjs7Jey72qsGzQtAdIcrGsXcGVmclR5CAbKMynlZHGg7g/4GVtQXL1njp/6s9ID7nsM1lEoCIFe6HWtR8FkICnWy0snx7l22uHaHg1ZIZW1LiYwRrA3dOzGNiJl/5xVjI24N85UB/xsML3wjfr1RY5ftePJfbHgUVOIWGO7w1SegQGRVeWhsZNaxMGsJl5rJJmdH2cQR51TEj91o+36wAU9jzRSRUm2jtcpYIUyQEFxKh828xLnDMcday44FixzdGUA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none Received: from CY4PR1801MB1910.namprd18.prod.outlook.com (2603:10b6:910:79::14) by MN2PR18MB2909.namprd18.prod.outlook.com (2603:10b6:208:39::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5458.25; Tue, 26 Jul 2022 16:37:57 +0000 Received: from CY4PR1801MB1910.namprd18.prod.outlook.com ([fe80::c5c4:ae52:8e5f:e5c5]) by CY4PR1801MB1910.namprd18.prod.outlook.com ([fe80::c5c4:ae52:8e5f:e5c5%6]) with mapi id 15.20.5458.024; Tue, 26 Jul 2022 16:37:57 +0000 To: gcc-patches@gcc.gnu.org Subject: [PATCH] analyzer: add get_meaning_for_state_change vfunc to fd_diagnostic in sm-fd.cc [PR106286] Date: Tue, 26 Jul 2022 22:07:38 +0530 Message-ID: X-Mailer: git-send-email 2.25.1 X-TMN: [p8SajXhoiKLK7CE2bjSSH5dGrA/11scRF7XcnGuNCZ/UJolrM8f/QQWY4D00VBxO] X-ClientProxiedBy: BMXPR01CA0074.INDPRD01.PROD.OUTLOOK.COM (2603:1096:b00:54::14) To CY4PR1801MB1910.namprd18.prod.outlook.com (2603:10b6:910:79::14) X-Microsoft-Original-Message-ID: <20220726163738.8757-1-mirimmad@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 6b0908db-bcbe-4908-c383-08da6f2531a8 X-MS-Exchange-SLBlob-MailProps: aa/WAlXx4eSinjOTp7Mq2Y7qJigwaX1uZBj9vMmcX+5tRVcBNSq3alOzNaTOafU6UwwkJDRqlQvNqO/pSBnbNX/alghqeEWJZi6z3t0c6Fkx59bFkIm7oD89rQntvcs5hhNB+5T8ES8yZ42hym+j+Zxuw8WydMvN2RIjZI/dVcZ/kRq7vuVoYX9Rw2aEmjcxoW0QCJ9pdOPN7jJihnEkW1MLDfIo4taoqUxMnwOUZy6WKETLz3wepVmC+rB80bUrgMK8bZDKbVoIG9qHBwU8MJ5SKqVdvBcwPLWQyJ/4xGbol+f7pjYB42qNz8Ywjrx+g4y5AcmUVA8FE/1JaanRLozyVkrhfrYg9+GmF5WiNf2L32E2LmEm+Hu5/JEFOlbRLaLAFhM7MrEMUNUr4BM4q3s+JDYwgS9LmxjCE8evUq/daIFDNyB5J6xM68eNPWAKajAx2FLlJnTG/f1c4Kq8EGHPRqxl3E+U77yHtIqJmePg+IYKF1GnQKtgJhixrsOJ2mdGDkYryu/eafKZ1jdlZroJ16qVXmSCS67LVa6g7coMyycDf19bIQfVL+1LyAgRVHf5mtUc+eulS3VBGPk13Lk+9I2IfDzMw2p0yDnh/MQhsIm6JJ5UIQ4DdppB8e+FA8t90V2wTA47jwAx8GlZRMBzGfAFKlPPtI9iqCSNVI9qF8gDTW8TBQMAjiWlXxF46vdzKenfdH59hX3QIUArELlu5KASE0RI5FyMWF7dILsH0yASao74d8KF7/Y+XVcuX4h1TxndMoGr5paYaOV/1O1xY1VCdFsV X-MS-TrafficTypeDiagnostic: MN2PR18MB2909:EE_ X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: LE4MsYlFuBCu/Bj9rIUxfVuC0h4PQrAtIugCherblSThPUEqvWR9WG0Ku5ZdnDtsV4FuOA2eQHfTqDMis06h7Q/yMRVvV+2XvfLI1BfIs1PBS43+KAPvz2u5aWrqKGXveI7lD+2AVXj1XG9ZeXKgkHQ96eX3llT0Zw+9MFsxfCZFrWs1VfWlGslMSVR27T4QVi1YsfMG69Bm9zXjPfr975/yOkGDWFdWSCAYSe+IFs8Oy8K6P7/BfO2EuBN+3dtBOkWIpLgdP3R0vC/yvLyu8h6bdfgxUOwU5mo+wQMcxpVUir3CwV/UotJXgn4YGpwW8zT/hfqqKxR+HS1qNtKTyyj/IZsYLslAghqA1coY/LtfjtzlU6HoD+PIG2wUzPjJcg+WBGLMiSorxvQh0QGtu+h/8fSOkJxV9V5hLoF344jMns7g0GkDTllYeiIf5T9Eeni5a6USpoSuQqbzy9YOMAhRYr6RpFfQBfUQQQ+ZNSGt3ZVJy0MqviJP5yaqsje4Oll36n3GRYhvdC69dMYNpkrWNnpFX61HFzh79Nv38G+za3OzSwQdF0/Lq6+nxURJnIvvLjL+Dir3XuaylyHFH9qvKXglApQORFTBKiWgBdBwP0pgCHPS8+UWE4kcZHLO X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: rNqIDCeL7elVQwMNqILYRVmsYTuC9djHwiBQrsarPKpdQTN67de3jtN5yWjafIE9fVJcAnoNxEfwZBMqaG+fCftBfertflfA7hCPiv2GQ8naUAn03IUXWfWUK66InneAtHDmRAMtO+vJ3rjTgXdDU7ahErAANV6bmABD8yaP2q7ZWQZ19cEzgxuJSn6qxFUDr1n011FzncUlIEe+UU1NQYKMwgpvd19nuLhQTQzBn6nTJZtjlcj1p2zYvjDigvubzlP8pdrPNm7gMW0aTQOhZU8+nicJrhbwGdWW0jtcmljICBsJcTKgTmi/EvbnuP31TFYJsU1j4x0c4t7JW0vYQRmjFdx1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6f2531a8 X-MS-Exchange-CrossTenant-AuthSource: CY4PR1801MB1910.namprd18.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jul 2022 16:37:56.9275 (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: MN2PR18MB2909 X-Spam-Status: No, score=-11.9 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, RCVD_IN_MSPIKE_H2, SPF_HELO_PASS, SPF_PASS, TXREP 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-Patchwork-Original-From: Immad Mir via Gcc-patches From: Immad Mir Reply-To: mirimnan017@gmail.com Cc: Immad Mir , mirimnan017@gmail.com Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1739433993135333409?= X-GMAIL-MSGID: =?utf-8?q?1739433993135333409?= This patch adds get_meaning_for_state_change vfunc to fd_diagnostic in sm-fd.cc which could be used by SARIF output. Lightly tested on x86_64 Linux. gcc/analyzer/ChangeLog: PR analyzer/106286 * sm-fd.cc: (fd_diagnostic::get_meaning_for_state_change): New. gcc/testsuite/ChangeLog: PR analyzer/106286 * gcc.dg/analyzer/fd-meaning.c: New test. Signed-off-by: Immad Mir --- gcc/analyzer/sm-fd.cc | 14 ++++++++ gcc/testsuite/gcc.dg/analyzer/fd-meaning.c | 37 ++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/analyzer/fd-meaning.c diff --git a/gcc/analyzer/sm-fd.cc b/gcc/analyzer/sm-fd.cc index 56b0063ba42..ed923ade100 100644 --- a/gcc/analyzer/sm-fd.cc +++ b/gcc/analyzer/sm-fd.cc @@ -229,6 +229,20 @@ public: return label_text (); } + diagnostic_event::meaning + get_meaning_for_state_change ( + const evdesc::state_change &change) const final override + { + if (change.m_old_state == m_sm.get_start_state () + && (m_sm.is_unchecked_fd_p (change.m_new_state))) + return diagnostic_event::meaning (diagnostic_event::VERB_acquire, + diagnostic_event::NOUN_resource); + if (change.m_new_state == m_sm.m_closed) + return diagnostic_event::meaning (diagnostic_event::VERB_release, + diagnostic_event::NOUN_resource); + return diagnostic_event::meaning (); + } + protected: const fd_state_machine &m_sm; tree m_arg; diff --git a/gcc/testsuite/gcc.dg/analyzer/fd-meaning.c b/gcc/testsuite/gcc.dg/analyzer/fd-meaning.c new file mode 100644 index 00000000000..6a9ec921fd3 --- /dev/null +++ b/gcc/testsuite/gcc.dg/analyzer/fd-meaning.c @@ -0,0 +1,37 @@ + /* { dg-additional-options "-fanalyzer-verbose-state-changes" } */ +int open(const char *, int mode); +void close(int fd); + +#define O_RDONLY 0 +#define O_WRONLY 1 +#define O_RDWR 2 + +void test_1 (const char* path) +{ + int fd = open (path, O_RDWR); /* { dg-message "meaning: \\{verb: 'acquire', noun: 'resource'\\}" } */ + if (fd != -1) + { + close(fd); /* { dg-message "meaning: \\{verb: 'release', noun: 'resource'\\}" } */ + close(fd); /* { dg-warning "double 'close' of file descriptor 'fd' \\\[CWE-1341\\\]" } */ + } +} + +void test_2 (const char* path) +{ + int fd = open (path, O_RDONLY); /* { dg-message "meaning: \\{verb: 'acquire', noun: 'resource'\\}" } */ + if (fd != -1) + { + close(fd); /* { dg-message "meaning: \\{verb: 'release', noun: 'resource'\\}" } */ + close(fd); /* { dg-warning "double 'close' of file descriptor 'fd' \\\[CWE-1341\\\]" } */ + } +} + +void test_3 (const char* path) +{ + int fd = open (path, O_WRONLY); /* { dg-message "meaning: \\{verb: 'acquire', noun: 'resource'\\}" } */ + if (fd != -1) + { + close(fd); /* { dg-message "meaning: \\{verb: 'release', noun: 'resource'\\}" } */ + close(fd); /* { dg-warning "double 'close' of file descriptor 'fd' \\\[CWE-1341\\\]" } */ + } +} \ No newline at end of file