aboutsummaryrefslogtreecommitdiff
path: root/gcc/analyzer/sm-fd.dot
blob: d7676b1f77927ebe7dd781e36dc6c9c91fac111c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* 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-fd.cc  */

digraph "fd" {

  /* STATES. */

  /* Start state.  */
  start;

  /* State for a constant file descriptor (>= 0).  */
  constant_fd;

  /* 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;

  /* States for FDs relating to socket APIs.  */

  /* Result of successful "socket" with SOCK_DGRAM.  */
  new_datagram_socket;
  /* Result of successful "socket" with SOCK_STREAM.  */
  new_stream_socket;
  /* Result of successful "socket" with unknown type.  */
  new_unknown_socket;

  /* The above after a successful call to "bind".  */
  bound_datagram_socket;
  bound_stream_socket;
  bound_unknown_socket;

  /* A bound socket after a successful call to "listen" (stream or unknown).  */
  listening_stream_socket;

  /* (i) the new FD as a result of a succesful call to "accept" on a
      listening socket (via a passive open), or
     (ii) an active socket after a successful call to "connect"
     (via an active open).  */
  connected_stream_socket;

  /* 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);'"];
  new_datagram_socket -> closed [label="on 'close(X);'"];
  new_stream_socket -> closed [label="on 'close(X);'"];
  new_unknown_socket -> closed [label="on 'close(X);'"];
  bound_datagram_socket -> closed [label="on 'close(X);'"];
  bound_stream_socket -> closed [label="on 'close(X);'"];
  bound_unknown_socket -> closed [label="on 'close(X);'"];
  listening_stream_socket -> closed [label="on 'close(X);'"];
  connected_stream_socket -> 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 "socket".  */
  start -> new_datagram_socket [label="when 'socket(..., SOCK_DGRAM, ...)' succeeds"];
  start -> new_stream_socket [label="when 'socket(..., SOCK_STREAM, ...)' succeeds"];
  start -> new_unknown_socket [label="when 'socket(..., ..., ...)' succeeds"];

  /* On "bind".  */
  start -> bound_unknown_socket [label="when 'bind(X, ...)' succeeds"];
  constant_fd -> bound_unknown_socket [label="when 'bind(X, ...)' succeeds"];
  new_stream_socket -> bound_stream_socket [label="when 'bind(X, ...)' succeeds"];
  new_datagram_socket -> bound_datagram_socket [label="when 'bind(X, ...)' succeeds"];
  new_unknown_socket -> bound_unknown_socket [label="when 'bind(X, ...)' succeeds"];

  /* On "listen".  */
  start -> listening_stream_socket [label="when 'listen(X, ...)' succeeds"];
  bound_stream_socket -> listening_stream_socket [label="when 'listen(X, ...)' succeeds"];
  bound_unknown_socket -> listening_stream_socket [label="when 'listen(X, ...)' succeeds"];

  /* On "accept".  */
  start -> connected_stream_socket [label="when 'accept(OTHER, ...)' succeeds on a listening_stream_socket"];
  constant_fd -> connected_stream_socket [label="when 'accept(OTHER, ...)' succeeds on a listening_stream_socket"];

  /* On "connect".  */
  new_stream_socket -> connected_stream_socket [label="when 'connect(X, ...)' succeeds"];
  new_datagram_socket -> new_datagram_socket [label="when 'connect(X, ...)' succeeds"];
  new_unknown_socket -> stop [label="when 'connect(X, ...)' succeeds"];
  start -> stop [label="when 'connect(X, ...)' succeeds"];
  constant_fd -> stop [label="when 'connect(X, ...)' 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')"];
  new_datagram_socket -> stop [label="on leak:\nWarn('leak')"];
  new_stream_socket -> stop [label="on leak:\nWarn('leak')"];
  new_unknown_socket -> stop [label="on leak:\nWarn('leak')"];
  bound_datagram_socket -> stop [label="on leak:\nWarn('leak')"];
  bound_stream_socket -> stop [label="on leak:\nWarn('leak')"];
  bound_unknown_socket -> stop [label="on leak:\nWarn('leak')"];
  listening_stream_socket -> stop [label="on leak:\nWarn('leak')"];
  connected_stream_socket -> stop [label="on leak:\nWarn('leak')"];
}