From patchwork Wed Oct 26 21:18:06 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 11443 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp488017wru; Wed, 26 Oct 2022 14:18:55 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7baND5nDdR0ocV2KgNhS/Uu38XcpP0+4+Fp6AIEDgkIjxm6FTMgh0CrJ9iWIFXHNQAfulz X-Received: by 2002:a17:906:5dda:b0:78d:e7d2:7499 with SMTP id p26-20020a1709065dda00b0078de7d27499mr39794233ejv.588.1666819135590; Wed, 26 Oct 2022 14:18:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666819135; cv=none; d=google.com; s=arc-20160816; b=QL/KzNP1jY41nL806xzgCXiLLyULt2X0WX1OJButEaw3E3Bm3QwDgupnJQRM/i5+7Q 4F9bQvwRtROT4dXfOCeJSLftfasGBuqnniqjkN5UiHqBE9A++fKLyyMQAzBOrTXtSDXw yo3RdmZpeTEfAimY3E71lOzGY9mWkTLv6RpbvUbiKNY3FaKmV0j9gRHcsE0neEmy6XKQ xGfMVlKmHCMKpHgDfY2jTSRF2RAG93mmQtmxuydDR6KOAPFdhMsLwr07OL1V1iQev1TX EP3nh5SqidIp4vYIjswk+rfX3lE4whrm+lefKq0eJ8+TNuQmFOUfif/h16kT6ggaNssT txww== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=CVLvSQHDXkhjpRSu2aUK+hd87nc8CQ6i72zz0olfurY=; b=tQV/ZgW2Py4EayIJ+9Z0XdmasPr8R7KWECRy2ELcN+D/ndoMS7fU0YpYc4eVuvZDo5 hayAe+DKiAhGnxGc7MAugolHOfKqyzf9eHZKdPVxPiuz5ixq2uQd903OY0iRK3rRGUPH E9cyuXwBsoFGLIFWVY/lAEqkOJtdRTcT7Q5vj42DFoMF52tz+GTQFaWRCDVCRYDmkoIe WK3kL70BOxRUvK4I/qUmfybCkN7MNdEzKf148TYR00PeUi1wIzgP4+pV1X4n/AUrRzGa ZhV/PLSnbyxsU1zqu9jmh90NPUTgJlUm7t3xuB/cqhROXevf82fDw7dkLmY8hY7qA5Bx jxKQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=DoL3e3Rc; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id sz15-20020a1709078b0f00b0078d4962a32asi5690055ejc.961.2022.10.26.14.18.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Oct 2022 14:18:55 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=DoL3e3Rc; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 9F9F2382C151 for ; Wed, 26 Oct 2022 21:18:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9F9F2382C151 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1666819134; bh=CVLvSQHDXkhjpRSu2aUK+hd87nc8CQ6i72zz0olfurY=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=DoL3e3RcIOq7ZPPd8X7dZTcQpuArEgEFw24PRBX11LNAb8jQTy0D0m2GnBeKH6RyD 7s92eFV0R3rza690PR3FmtfiDfyzjaIwPaQKv/jhzK+cSy0xbDp+6TkFxqDGCYKv4h 9TEhcRNBLZAtIYin2v/HH1JqhMoHr3/yNkjtAafY= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 40B17382C151 for ; Wed, 26 Oct 2022 21:18:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 40B17382C151 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-319-u4P7q17VO2qYLqm9aF778g-1; Wed, 26 Oct 2022 17:18:09 -0400 X-MC-Unique: u4P7q17VO2qYLqm9aF778g-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E7E7B1C06EC0 for ; Wed, 26 Oct 2022 21:18:08 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.2.17.189]) by smtp.corp.redhat.com (Postfix) with ESMTP id C2D672166B2B; Wed, 26 Oct 2022 21:18:08 +0000 (UTC) To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [committed] analyzer: add sm-fd.dot Date: Wed, 26 Oct 2022 17:18:06 -0400 Message-Id: <20221026211806.1866873-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, 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: David Malcolm via Gcc-patches From: David Malcolm Reply-To: David Malcolm 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?1747786541917982990?= X-GMAIL-MSGID: =?utf-8?q?1747786541917982990?= Add a .dot file to document the file descriptor state machine. gcc/analyzer/ChangeLog: * sm-fd.dot: New file. Signed-off-by: David Malcolm --- gcc/analyzer/sm-fd.dot | 109 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 gcc/analyzer/sm-fd.dot diff --git a/gcc/analyzer/sm-fd.dot b/gcc/analyzer/sm-fd.dot new file mode 100644 index 00000000000..175daae44ae --- /dev/null +++ b/gcc/analyzer/sm-fd.dot @@ -0,0 +1,109 @@ +/* An overview of the state machine from sm-fd.cc. + Copyright (C) 2022 Free Software Foundation, Inc. + Contributed by David Malcolm . + +This file is part of GCC. + +GCC is free software; you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 3, or (at your option) +any later version. + +GCC is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GCC; see the file COPYING3. If not see +. */ + +/* Keep this in-sync with sm-dot.cc */ + +digraph "fd" { + + /* STATES. */ + + /* Start state. */ + start; + + /* States representing a file descriptor that hasn't yet been + checked for validity after opening, for three different + access modes. */ + unchecked_read_write; + unchecked_read_only; + unchecked_write_only; + + /* States for representing a file descriptor that is known to be valid (>= + 0), for three different access modes. */ + valid_read_write; + valid_read_only; + valid_write_only; + + /* State for a file descriptor that is known to be invalid (< 0). */ + invalid; + + /* State for a file descriptor that has been closed. */ + closed; + + /* State for a file descriptor that we do not want to track anymore . */ + stop; + + /* TRANSITIONS. */ + + /* On "open". */ + start -> unchecked_read_only [label="on 'X = open(..., O_RDONLY);'"]; + start -> unchecked_write_only [label="on 'X = open(..., O_WRONLY);'"]; + start -> unchecked_read_write [label="on 'X = open(..., ...);'"]; + + /* On "creat". */ + start -> unchecked_write_only [label="on 'X = create(...);'"]; + + /* On "close". */ + start -> closed [label="on 'close(X);'"]; + unchecked_read_write -> closed [label="on 'close(X);'"]; + unchecked_read_only -> closed [label="on 'close(X);'"]; + unchecked_write_only -> closed [label="on 'close(X);'"]; + valid_read_write -> closed [label="on 'close(X);'"]; + valid_read_only -> closed [label="on 'close(X);'"]; + valid_write_only -> closed [label="on 'close(X);'"]; + constant_fd -> closed [label="on 'close(X);'"]; + closed -> stop [label="on 'close(X);':\nWarn('double close')"]; + + /* On "read". */ + closed -> closed [label="on 'read(X);':\nWarn('use after close')"]; + unchecked_read_write -> unchecked_read_write [label="on 'read(X);:\nWarn('use without check')'"]; + unchecked_read_only -> unchecked_read_only [label="on 'read(X);:\nWarn('use without check')'"]; + unchecked_write_only -> unchecked_write_only [label="on 'read(X);:\nWarn('use without check')'"]; + valid_write_only -> valid_write_only [label="on 'read(X);:\nWarn('access mode mismatch')'"]; + + /* On "write". */ + closed -> closed [label="on 'write(X);':\nWarn('use after close')"]; + unchecked_read_write -> unchecked_read_write [label="on 'write(X);:\nWarn('use without check')'"]; + unchecked_read_only -> unchecked_read_only [label="on 'write(X);:\nWarn('use without check')'"]; + unchecked_write_only -> unchecked_write_only [label="on 'write(X);:\nWarn('use without check')'"]; + valid_read_only -> valid_read_only [label="on 'write(X);:\nWarn('access mode mismatch')'"]; + + /* On "dup". */ + closed -> closed [label="on 'dup(X);':\nWarn('use after close')"]; + /* plus stuff for the new fd. */ + + /* On "pipe". */ + start -> valid_read_write [label="when 'pipe()' succeeds"]; + + /* on_condition. */ + unchecked_read_write -> valid_read_write [label="on 'X >= 0'"]; + unchecked_read_only -> valid_read_only [label="on 'X >= 0'"]; + unchecked_write_only -> valid_write_only [label="on 'X >= 0'"]; + unchecked_read_write -> invalid [label="on 'X < 0'"]; + unchecked_read_only -> invalid [label="on 'X < 0'"]; + unchecked_write_only -> invalid [label="on 'X < 0'"]; + + /* Leaks. */ + unchecked_read_write -> stop [label="on leak:\nWarn('leak')"]; + unchecked_read_only -> stop [label="on leak:\nWarn('leak')"]; + unchecked_write_only -> stop [label="on leak:\nWarn('leak')"]; + valid_read_write -> stop [label="on leak:\nWarn('leak')"]; + valid_read_only -> stop [label="on leak:\nWarn('leak')"]; + valid_write_only -> stop [label="on leak:\nWarn('leak')"]; +}