aboutsummaryrefslogtreecommitdiff
path: root/gcc/analyzer
diff options
context:
space:
mode:
authorMartin Liska <mliska@suse.cz>2022-10-28 10:02:34 +0200
committerMartin Liska <mliska@suse.cz>2022-10-28 10:02:34 +0200
commit1eb021edb27e26f95cda63df121f6bc951647599 (patch)
tree7f132fded85bd7d05d81cd4c1227da2fd0c3c2d5 /gcc/analyzer
parent62e475bad0d668c432bb97113cbf73fa281b8b55 (diff)
parent0607307768b66a90e27c5bc91a247acc938f070e (diff)
downloadgcc-1eb021edb27e26f95cda63df121f6bc951647599.zip
gcc-1eb021edb27e26f95cda63df121f6bc951647599.tar.gz
gcc-1eb021edb27e26f95cda63df121f6bc951647599.tar.bz2
Merge branch 'master' into devel/sphinx
Diffstat (limited to 'gcc/analyzer')
-rw-r--r--gcc/analyzer/ChangeLog11
-rw-r--r--gcc/analyzer/sm-fd.cc34
-rw-r--r--gcc/analyzer/sm-fd.dot109
3 files changed, 137 insertions, 17 deletions
diff --git a/gcc/analyzer/ChangeLog b/gcc/analyzer/ChangeLog
index 733580a..6e6b056 100644
--- a/gcc/analyzer/ChangeLog
+++ b/gcc/analyzer/ChangeLog
@@ -1,3 +1,14 @@
+2022-10-26 David Malcolm <dmalcolm@redhat.com>
+
+ * sm-fd.cc (fd_state_machine::on_open): Transition to "unchecked"
+ when the mode is symbolic, rather than just on integer constants.
+ (fd_state_machine::check_for_open_fd): Don't complain about
+ unchecked values in the start state.
+
+2022-10-26 David Malcolm <dmalcolm@redhat.com>
+
+ * sm-fd.dot: New file.
+
2022-10-24 David Malcolm <dmalcolm@redhat.com>
PR analyzer/107349
diff --git a/gcc/analyzer/sm-fd.cc b/gcc/analyzer/sm-fd.cc
index 8a4c208..ae846cd 100644
--- a/gcc/analyzer/sm-fd.cc
+++ b/gcc/analyzer/sm-fd.cc
@@ -940,25 +940,25 @@ fd_state_machine::on_open (sm_context *sm_ctxt, const supernode *node,
if (lhs)
{
tree arg = gimple_call_arg (call, 1);
+ enum access_mode mode = READ_WRITE;
if (TREE_CODE (arg) == INTEGER_CST)
{
int flag = TREE_INT_CST_LOW (arg);
- enum access_mode mode = get_access_mode_from_flag (flag);
-
- switch (mode)
- {
- case READ_ONLY:
- sm_ctxt->on_transition (node, stmt, lhs, m_start,
- m_unchecked_read_only);
- break;
- case WRITE_ONLY:
- sm_ctxt->on_transition (node, stmt, lhs, m_start,
- m_unchecked_write_only);
- break;
- default:
- sm_ctxt->on_transition (node, stmt, lhs, m_start,
- m_unchecked_read_write);
- }
+ mode = get_access_mode_from_flag (flag);
+ }
+ switch (mode)
+ {
+ case READ_ONLY:
+ sm_ctxt->on_transition (node, stmt, lhs, m_start,
+ m_unchecked_read_only);
+ break;
+ case WRITE_ONLY:
+ sm_ctxt->on_transition (node, stmt, lhs, m_start,
+ m_unchecked_write_only);
+ break;
+ default:
+ sm_ctxt->on_transition (node, stmt, lhs, m_start,
+ m_unchecked_read_write);
}
}
else
@@ -1096,7 +1096,7 @@ fd_state_machine::check_for_open_fd (
else
{
- if (!(is_valid_fd_p (state) || (state == m_stop)))
+ if (!(is_valid_fd_p (state) || state == m_start || state == m_stop))
{
if (!is_constant_fd_p (state))
sm_ctxt->warn (
diff --git a/gcc/analyzer/sm-fd.dot b/gcc/analyzer/sm-fd.dot
new file mode 100644
index 0000000..175daae
--- /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 <dmalcolm@redhat.com>.
+
+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
+<http://www.gnu.org/licenses/>. */
+
+/* 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')"];
+}