Branch data Line data Source code
1 : : /* A state machine for detecting misuses of POSIX file descriptor APIs.
2 : : Copyright (C) 2019-2025 Free Software Foundation, Inc.
3 : : Contributed by Immad Mir <mir@sourceware.org>.
4 : :
5 : : This file is part of GCC.
6 : :
7 : : GCC is free software; you can redistribute it and/or modify it
8 : : under the terms of the GNU General Public License as published by
9 : : the Free Software Foundation; either version 3, or (at your option)
10 : : any later version.
11 : :
12 : : GCC is distributed in the hope that it will be useful, but
13 : : WITHOUT ANY WARRANTY; without even the implied warranty of
14 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 : : General Public License for more details.
16 : :
17 : : You should have received a copy of the GNU General Public License
18 : : along with GCC; see the file COPYING3. If not see
19 : : <http://www.gnu.org/licenses/>. */
20 : :
21 : : #include "analyzer/common.h"
22 : :
23 : : #include "diagnostics/event-id.h"
24 : : #include "stringpool.h"
25 : : #include "attribs.h"
26 : :
27 : : #include "analyzer/analyzer-logging.h"
28 : : #include "analyzer/sm.h"
29 : : #include "analyzer/pending-diagnostic.h"
30 : : #include "analyzer/function-set.h"
31 : : #include "analyzer/analyzer-selftests.h"
32 : : #include "analyzer/call-string.h"
33 : : #include "analyzer/program-point.h"
34 : : #include "analyzer/store.h"
35 : : #include "analyzer/region-model.h"
36 : : #include "analyzer/program-state.h"
37 : : #include "analyzer/supergraph.h"
38 : : #include "analyzer/analyzer-language.h"
39 : : #include "analyzer/call-details.h"
40 : : #include "analyzer/call-info.h"
41 : :
42 : : #if ENABLE_ANALYZER
43 : :
44 : : namespace ana {
45 : :
46 : : namespace {
47 : :
48 : : /* An enum for distinguishing between three different access modes. */
49 : :
50 : : enum access_mode
51 : : {
52 : : READ_WRITE,
53 : : READ_ONLY,
54 : : WRITE_ONLY
55 : : };
56 : :
57 : : enum access_directions
58 : : {
59 : : DIRS_READ_WRITE,
60 : : DIRS_READ,
61 : : DIRS_WRITE
62 : : };
63 : :
64 : : /* An enum for distinguishing between dup, dup2 and dup3. */
65 : : enum dup
66 : : {
67 : : DUP_1,
68 : : DUP_2,
69 : : DUP_3
70 : : };
71 : :
72 : : /* Enum for use by -Wanalyzer-fd-phase-mismatch. */
73 : :
74 : : enum expected_phase
75 : : {
76 : : EXPECTED_PHASE_CAN_TRANSFER, /* can "read"/"write". */
77 : : EXPECTED_PHASE_CAN_BIND,
78 : : EXPECTED_PHASE_CAN_LISTEN,
79 : : EXPECTED_PHASE_CAN_ACCEPT,
80 : : EXPECTED_PHASE_CAN_CONNECT
81 : : };
82 : :
83 : : class fd_state_machine : public state_machine
84 : : {
85 : : public:
86 : : fd_state_machine (logger *logger);
87 : :
88 : : bool
89 : 1061505 : inherited_state_p () const final override
90 : : {
91 : 1061505 : return false;
92 : : }
93 : :
94 : : state_machine::state_t
95 : 1060052 : get_default_state (const svalue *sval) const final override
96 : : {
97 : 1060052 : if (tree cst = sval->maybe_get_constant ())
98 : : {
99 : 160211 : if (TREE_CODE (cst) == INTEGER_CST)
100 : : {
101 : 157415 : int val = TREE_INT_CST_LOW (cst);
102 : 157415 : if (val >= 0)
103 : 153321 : return m_constant_fd;
104 : : else
105 : 4094 : return m_invalid;
106 : : }
107 : : }
108 : 902637 : return m_start;
109 : : }
110 : :
111 : : bool on_stmt (sm_context &sm_ctxt, const supernode *node,
112 : : const gimple *stmt) const final override;
113 : :
114 : : void on_condition (sm_context &sm_ctxt, const supernode *node,
115 : : const gimple *stmt, const svalue *lhs, const tree_code op,
116 : : const svalue *rhs) const final override;
117 : :
118 : : bool can_purge_p (state_t s) const final override;
119 : :
120 : : std::unique_ptr<pending_diagnostic>
121 : : on_leak (tree var,
122 : : const program_state *old_state,
123 : : const program_state *new_state) const final override;
124 : :
125 : : bool is_unchecked_fd_p (state_t s) const;
126 : : bool is_valid_fd_p (state_t s) const;
127 : : bool is_socket_fd_p (state_t s) const;
128 : : bool is_datagram_socket_fd_p (state_t s) const;
129 : : bool is_stream_socket_fd_p (state_t s) const;
130 : : bool is_closed_fd_p (state_t s) const;
131 : : bool is_constant_fd_p (state_t s) const;
132 : : bool is_readonly_fd_p (state_t s) const;
133 : : bool is_writeonly_fd_p (state_t s) const;
134 : : enum access_mode get_access_mode_from_flag (int flag) const;
135 : : /* Function for one-to-one correspondence between valid
136 : : and unchecked states. */
137 : : state_t valid_to_unchecked_state (state_t state) const;
138 : :
139 : : void mark_as_valid_fd (region_model *model,
140 : : sm_state_map *smap,
141 : : const svalue *fd_sval,
142 : : const extrinsic_state &ext_state) const;
143 : :
144 : : bool on_socket (const call_details &cd,
145 : : bool successful,
146 : : sm_context &sm_ctxt,
147 : : const extrinsic_state &ext_state) const;
148 : : bool on_bind (const call_details &cd,
149 : : bool successful,
150 : : sm_context &sm_ctxt,
151 : : const extrinsic_state &ext_state) const;
152 : : bool on_listen (const call_details &cd,
153 : : bool successful,
154 : : sm_context &sm_ctxt,
155 : : const extrinsic_state &ext_state) const;
156 : : bool on_accept (const call_details &cd,
157 : : bool successful,
158 : : sm_context &sm_ctxt,
159 : : const extrinsic_state &ext_state) const;
160 : : bool on_connect (const call_details &cd,
161 : : bool successful,
162 : : sm_context &sm_ctxt,
163 : : const extrinsic_state &ext_state) const;
164 : :
165 : : /* State for a constant file descriptor (>= 0) */
166 : : state_t m_constant_fd;
167 : :
168 : : /* States representing a file descriptor that hasn't yet been
169 : : checked for validity after opening, for three different
170 : : access modes. */
171 : : state_t m_unchecked_read_write;
172 : :
173 : : state_t m_unchecked_read_only;
174 : :
175 : : state_t m_unchecked_write_only;
176 : :
177 : : /* States for representing a file descriptor that is known to be valid (>=
178 : : 0), for three different access modes. */
179 : : state_t m_valid_read_write;
180 : :
181 : : state_t m_valid_read_only;
182 : :
183 : : state_t m_valid_write_only;
184 : :
185 : : /* State for a file descriptor that is known to be invalid (< 0). */
186 : : state_t m_invalid;
187 : :
188 : : /* State for a file descriptor that has been closed. */
189 : : state_t m_closed;
190 : :
191 : : /* States for FDs relating to socket APIs. */
192 : :
193 : : /* Result of successful "socket" with SOCK_DGRAM. */
194 : : state_t m_new_datagram_socket;
195 : : /* Result of successful "socket" with SOCK_STREAM. */
196 : : state_t m_new_stream_socket;
197 : : /* Result of successful "socket" with unknown type. */
198 : : state_t m_new_unknown_socket;
199 : :
200 : : /* The above after a successful call to "bind". */
201 : : state_t m_bound_datagram_socket;
202 : : state_t m_bound_stream_socket;
203 : : state_t m_bound_unknown_socket;
204 : :
205 : : /* A bound socket after a successful call to "listen" (stream or unknown). */
206 : : state_t m_listening_stream_socket;
207 : :
208 : : /* (i) the new FD as a result of a succesful call to "accept" on a
209 : : listening socket (via a passive open), or
210 : : (ii) an active socket after a successful call to "connect"
211 : : (via an active open). */
212 : : state_t m_connected_stream_socket;
213 : :
214 : : /* State for a file descriptor that we do not want to track anymore . */
215 : : state_t m_stop;
216 : :
217 : : /* Stashed constant values from the frontend. These could be NULL_TREE. */
218 : : tree m_O_ACCMODE;
219 : : tree m_O_RDONLY;
220 : : tree m_O_WRONLY;
221 : : tree m_SOCK_STREAM;
222 : : tree m_SOCK_DGRAM;
223 : :
224 : : private:
225 : : void on_open (sm_context &sm_ctxt, const supernode *node, const gimple *stmt,
226 : : const gcall &call) const;
227 : : void on_creat (sm_context &sm_ctxt, const supernode *node, const gimple *stmt,
228 : : const gcall &call) const;
229 : : void on_close (sm_context &sm_ctxt, const supernode *node, const gimple *stmt,
230 : : const gcall &call) const;
231 : : void on_read (sm_context &sm_ctxt, const supernode *node, const gimple *stmt,
232 : : const gcall &call, const tree callee_fndecl) const;
233 : : void on_write (sm_context &sm_ctxt, const supernode *node, const gimple *stmt,
234 : : const gcall &call, const tree callee_fndecl) const;
235 : : void check_for_open_fd (sm_context &sm_ctxt, const supernode *node,
236 : : const gimple *stmt, const gcall &call,
237 : : const tree callee_fndecl,
238 : : enum access_directions access_fn) const;
239 : :
240 : : void make_valid_transitions_on_condition (sm_context &sm_ctxt,
241 : : const supernode *node,
242 : : const gimple *stmt,
243 : : const svalue *lhs) const;
244 : : void make_invalid_transitions_on_condition (sm_context &sm_ctxt,
245 : : const supernode *node,
246 : : const gimple *stmt,
247 : : const svalue *lhs) const;
248 : : void check_for_fd_attrs (sm_context &sm_ctxt, const supernode *node,
249 : : const gimple *stmt, const gcall &call,
250 : : const tree callee_fndecl, const char *attr_name,
251 : : access_directions fd_attr_access_dir) const;
252 : : void check_for_dup (sm_context &sm_ctxt, const supernode *node,
253 : : const gimple *stmt, const gcall &call, const tree callee_fndecl,
254 : : enum dup kind) const;
255 : :
256 : : state_t get_state_for_socket_type (const svalue *socket_type_sval) const;
257 : :
258 : : bool check_for_socket_fd (const call_details &cd,
259 : : bool successful,
260 : : sm_context &sm_ctxt,
261 : : const svalue *fd_sval,
262 : : const supernode *node,
263 : : state_t old_state,
264 : : bool *complained = nullptr) const;
265 : : bool check_for_new_socket_fd (const call_details &cd,
266 : : bool successful,
267 : : sm_context &sm_ctxt,
268 : : const svalue *fd_sval,
269 : : const supernode *node,
270 : : state_t old_state,
271 : : enum expected_phase expected_phase) const;
272 : : };
273 : :
274 : : /* Base diagnostic class relative to fd_state_machine. */
275 : 0 : class fd_diagnostic : public pending_diagnostic
276 : : {
277 : : public:
278 : 364 : fd_diagnostic (const fd_state_machine &sm, tree arg) : m_sm (sm), m_arg (arg)
279 : : {
280 : : }
281 : :
282 : : bool
283 : 159 : subclass_equal_p (const pending_diagnostic &base_other) const override
284 : : {
285 : 159 : return same_tree_p (m_arg, ((const fd_diagnostic &)base_other).m_arg);
286 : : }
287 : :
288 : : bool
289 : 256 : describe_state_change (pretty_printer &pp,
290 : : const evdesc::state_change &change) override
291 : : {
292 : 256 : if (change.m_old_state == m_sm.get_start_state ())
293 : : {
294 : 162 : if (change.m_new_state == m_sm.m_unchecked_read_write
295 : 162 : || change.m_new_state == m_sm.m_valid_read_write)
296 : : {
297 : 8 : pp_string (&pp, "opened here as read-write");
298 : 8 : return true;
299 : : }
300 : :
301 : 154 : if (change.m_new_state == m_sm.m_unchecked_read_only
302 : 142 : || change.m_new_state == m_sm.m_valid_read_only)
303 : : {
304 : 12 : pp_string (&pp, "opened here as read-only");
305 : 12 : return true;
306 : : }
307 : :
308 : 142 : if (change.m_new_state == m_sm.m_unchecked_write_only
309 : 128 : || change.m_new_state == m_sm.m_valid_write_only)
310 : : {
311 : 14 : pp_string (&pp, "opened here as write-only");
312 : 14 : return true;
313 : : }
314 : :
315 : 128 : if (change.m_new_state == m_sm.m_new_datagram_socket)
316 : : {
317 : 18 : pp_string (&pp, "datagram socket created here");
318 : 18 : return true;
319 : : }
320 : :
321 : 110 : if (change.m_new_state == m_sm.m_new_stream_socket)
322 : : {
323 : 22 : pp_string (&pp, "stream socket created here");
324 : 22 : return true;
325 : : }
326 : :
327 : 88 : if (change.m_new_state == m_sm.m_new_unknown_socket
328 : 18 : || change.m_new_state == m_sm.m_connected_stream_socket)
329 : : {
330 : 78 : pp_string (&pp, "socket created here");
331 : 78 : return true;
332 : : }
333 : : }
334 : :
335 : 104 : if (change.m_new_state == m_sm.m_bound_datagram_socket)
336 : : {
337 : 4 : pp_string (&pp, "datagram socket bound here");
338 : 4 : return true;
339 : : }
340 : :
341 : 100 : if (change.m_new_state == m_sm.m_bound_stream_socket)
342 : : {
343 : 4 : pp_string (&pp, "stream socket bound here");
344 : 4 : return true;
345 : : }
346 : :
347 : 96 : if (change.m_new_state == m_sm.m_bound_unknown_socket
348 : 86 : || change.m_new_state == m_sm.m_connected_stream_socket)
349 : : {
350 : 10 : pp_string (&pp, "socket bound here");
351 : 10 : return true;
352 : : }
353 : :
354 : 86 : if (change.m_new_state == m_sm.m_listening_stream_socket)
355 : : {
356 : 8 : pp_printf (&pp,
357 : : "stream socket marked as passive here via %qs",
358 : : "listen");
359 : 8 : return true;
360 : : }
361 : :
362 : 78 : if (change.m_new_state == m_sm.m_closed)
363 : : {
364 : 0 : pp_string (&pp, "closed here");
365 : 0 : return true;
366 : : }
367 : :
368 : 164 : if (m_sm.is_unchecked_fd_p (change.m_old_state)
369 : 156 : && m_sm.is_valid_fd_p (change.m_new_state))
370 : : {
371 : 70 : if (change.m_expr)
372 : 70 : pp_printf (&pp,
373 : : "assuming %qE is a valid file descriptor (>= 0)",
374 : : change.m_expr);
375 : : else
376 : 0 : pp_string (&pp, "assuming a valid file descriptor");
377 : 70 : return true;
378 : : }
379 : :
380 : 272 : if (m_sm.is_unchecked_fd_p (change.m_old_state)
381 : 8 : && change.m_new_state == m_sm.m_invalid)
382 : : {
383 : 8 : if (change.m_expr)
384 : 8 : pp_printf (&pp,
385 : : "assuming %qE is an invalid file descriptor (< 0)",
386 : : change.m_expr);
387 : : else
388 : 0 : pp_string (&pp, "assuming an invalid file descriptor");
389 : 8 : return true;
390 : : }
391 : :
392 : : return false;
393 : : }
394 : :
395 : : diagnostics::paths::event::meaning
396 : 96 : get_meaning_for_state_change (
397 : : const evdesc::state_change &change) const final override
398 : : {
399 : 96 : using event = diagnostics::paths::event;
400 : 96 : if (change.m_old_state == m_sm.get_start_state ()
401 : 96 : && (m_sm.is_unchecked_fd_p (change.m_new_state)
402 : 24 : || change.m_new_state == m_sm.m_new_datagram_socket
403 : 22 : || change.m_new_state == m_sm.m_new_stream_socket
404 : 20 : || change.m_new_state == m_sm.m_new_unknown_socket))
405 : 48 : return event::meaning (event::verb::acquire,
406 : 48 : event::noun::resource);
407 : 48 : if (change.m_new_state == m_sm.m_closed)
408 : 24 : return event::meaning (event::verb::release,
409 : 24 : event::noun::resource);
410 : 24 : return event::meaning ();
411 : : }
412 : :
413 : : protected:
414 : : const fd_state_machine &m_sm;
415 : : tree m_arg;
416 : : };
417 : :
418 : 0 : class fd_param_diagnostic : public fd_diagnostic
419 : : {
420 : : public:
421 : 12 : fd_param_diagnostic (const fd_state_machine &sm, tree arg, tree callee_fndecl,
422 : : const char *attr_name, int arg_idx)
423 : 12 : : fd_diagnostic (sm, arg), m_callee_fndecl (callee_fndecl),
424 : 12 : m_attr_name (attr_name), m_arg_idx (arg_idx)
425 : : {
426 : : }
427 : :
428 : 193 : fd_param_diagnostic (const fd_state_machine &sm, tree arg, tree callee_fndecl)
429 : 193 : : fd_diagnostic (sm, arg), m_callee_fndecl (callee_fndecl),
430 : 193 : m_attr_name (nullptr), m_arg_idx (-1)
431 : : {
432 : : }
433 : :
434 : : bool
435 : 157 : subclass_equal_p (const pending_diagnostic &base_other) const override
436 : : {
437 : 157 : const fd_param_diagnostic &sub_other
438 : : = (const fd_param_diagnostic &)base_other;
439 : 157 : return (same_tree_p (m_arg, sub_other.m_arg)
440 : 157 : && same_tree_p (m_callee_fndecl, sub_other.m_callee_fndecl)
441 : 157 : && m_arg_idx == sub_other.m_arg_idx
442 : 314 : && ((m_attr_name)
443 : 12 : ? (strcmp (m_attr_name, sub_other.m_attr_name) == 0)
444 : 157 : : true));
445 : : }
446 : :
447 : : void
448 : 59 : inform_filedescriptor_attribute (access_directions fd_dir)
449 : : {
450 : :
451 : 59 : if (m_attr_name)
452 : 12 : switch (fd_dir)
453 : : {
454 : 6 : case DIRS_READ_WRITE:
455 : 12 : inform (DECL_SOURCE_LOCATION (m_callee_fndecl),
456 : : "argument %d of %qD must be an open file descriptor, due to "
457 : : "%<__attribute__((%s(%d)))%>",
458 : 6 : m_arg_idx + 1, m_callee_fndecl, m_attr_name, m_arg_idx + 1);
459 : 6 : break;
460 : 3 : case DIRS_WRITE:
461 : 6 : inform (DECL_SOURCE_LOCATION (m_callee_fndecl),
462 : : "argument %d of %qD must be a readable file descriptor, due "
463 : : "to %<__attribute__((%s(%d)))%>",
464 : 3 : m_arg_idx + 1, m_callee_fndecl, m_attr_name, m_arg_idx + 1);
465 : 3 : break;
466 : 3 : case DIRS_READ:
467 : 6 : inform (DECL_SOURCE_LOCATION (m_callee_fndecl),
468 : : "argument %d of %qD must be a writable file descriptor, due "
469 : : "to %<__attribute__((%s(%d)))%>",
470 : 3 : m_arg_idx + 1, m_callee_fndecl, m_attr_name, m_arg_idx + 1);
471 : 3 : break;
472 : : }
473 : 59 : }
474 : :
475 : : protected:
476 : : tree m_callee_fndecl;
477 : : const char *m_attr_name;
478 : : /* ARG_IDX is 0-based. */
479 : : int m_arg_idx;
480 : : };
481 : :
482 : : class fd_leak : public fd_diagnostic
483 : : {
484 : : public:
485 : 131 : fd_leak (const fd_state_machine &sm, tree arg,
486 : : const program_state *final_state)
487 : 131 : : fd_diagnostic (sm, arg),
488 : 116 : m_final_state ()
489 : : {
490 : 116 : if (final_state)
491 : 116 : m_final_state = std::make_unique<program_state> (*final_state);
492 : 116 : }
493 : :
494 : : const char *
495 : 1719 : get_kind () const final override
496 : : {
497 : 1719 : return "fd_leak";
498 : : }
499 : :
500 : : int
501 : 96 : get_controlling_option () const final override
502 : : {
503 : 96 : return OPT_Wanalyzer_fd_leak;
504 : : }
505 : :
506 : : bool
507 : 73 : emit (diagnostic_emission_context &ctxt) final override
508 : : {
509 : : /*CWE-775: Missing Release of File Descriptor or Handle after Effective
510 : : Lifetime
511 : : */
512 : 73 : ctxt.add_cwe (775);
513 : 73 : if (m_arg)
514 : 58 : return ctxt.warn ("leak of file descriptor %qE", m_arg);
515 : : else
516 : 15 : return ctxt.warn ("leak of file descriptor");
517 : : }
518 : :
519 : : bool
520 : 136 : describe_state_change (pretty_printer &pp,
521 : : const evdesc::state_change &change) final override
522 : : {
523 : 272 : if (m_sm.is_unchecked_fd_p (change.m_new_state))
524 : : {
525 : 34 : m_open_event = change.m_event_id;
526 : 34 : pp_string (&pp, "opened here");
527 : 34 : return true;
528 : : }
529 : :
530 : 102 : return fd_diagnostic::describe_state_change (pp, change);
531 : : }
532 : :
533 : : bool
534 : 146 : describe_final_event (pretty_printer &pp,
535 : : const evdesc::final_event &ev) final override
536 : : {
537 : 146 : if (m_open_event.known_p ())
538 : : {
539 : 34 : if (ev.m_expr)
540 : 34 : pp_printf (&pp,
541 : : "%qE leaks here; was opened at %@",
542 : : ev.m_expr, &m_open_event);
543 : : else
544 : 0 : pp_printf (&pp,
545 : : "leaks here; was opened at %@",
546 : : &m_open_event);
547 : : }
548 : : else
549 : : {
550 : 112 : if (ev.m_expr)
551 : 82 : pp_printf (&pp, "%qE leaks here", ev.m_expr);
552 : : else
553 : 30 : pp_string (&pp, "leaks here");
554 : : }
555 : 146 : return true;
556 : : }
557 : :
558 : : const program_state *
559 : 73 : get_final_state () const final override
560 : : {
561 : 73 : return m_final_state.get ();
562 : : }
563 : :
564 : : private:
565 : : diagnostics::paths::event_id_t m_open_event;
566 : : std::unique_ptr<program_state> m_final_state;
567 : : };
568 : :
569 : 0 : class fd_access_mode_mismatch : public fd_param_diagnostic
570 : : {
571 : : public:
572 : 6 : fd_access_mode_mismatch (const fd_state_machine &sm, tree arg,
573 : : enum access_directions fd_dir,
574 : : const tree callee_fndecl, const char *attr_name,
575 : : int arg_idx)
576 : 6 : : fd_param_diagnostic (sm, arg, callee_fndecl, attr_name, arg_idx),
577 : 6 : m_fd_dir (fd_dir)
578 : :
579 : : {
580 : : }
581 : :
582 : 6 : fd_access_mode_mismatch (const fd_state_machine &sm, tree arg,
583 : : enum access_directions fd_dir,
584 : : const tree callee_fndecl)
585 : 6 : : fd_param_diagnostic (sm, arg, callee_fndecl), m_fd_dir (fd_dir)
586 : : {
587 : : }
588 : :
589 : : const char *
590 : 126 : get_kind () const final override
591 : : {
592 : 126 : return "fd_access_mode_mismatch";
593 : : }
594 : :
595 : : int
596 : 24 : get_controlling_option () const final override
597 : : {
598 : 24 : return OPT_Wanalyzer_fd_access_mode_mismatch;
599 : : }
600 : :
601 : : bool
602 : 12 : emit (diagnostic_emission_context &ctxt) final override
603 : : {
604 : 12 : bool warned;
605 : 12 : switch (m_fd_dir)
606 : : {
607 : 5 : case DIRS_READ:
608 : 5 : warned = ctxt.warn ("%qE on read-only file descriptor %qE",
609 : : m_callee_fndecl, m_arg);
610 : 5 : break;
611 : 7 : case DIRS_WRITE:
612 : 7 : warned = ctxt.warn ("%qE on write-only file descriptor %qE",
613 : : m_callee_fndecl, m_arg);
614 : 7 : break;
615 : 0 : default:
616 : 0 : gcc_unreachable ();
617 : : }
618 : 12 : if (warned)
619 : 12 : inform_filedescriptor_attribute (m_fd_dir);
620 : 12 : return warned;
621 : : }
622 : :
623 : : bool
624 : 24 : describe_final_event (pretty_printer &pp,
625 : : const evdesc::final_event &) final override
626 : : {
627 : 24 : switch (m_fd_dir)
628 : : {
629 : 10 : case DIRS_READ:
630 : 10 : pp_printf (&pp,
631 : : "%qE on read-only file descriptor %qE",
632 : : m_callee_fndecl, m_arg);
633 : 10 : return true;
634 : 14 : case DIRS_WRITE:
635 : 14 : pp_printf (&pp,
636 : : "%qE on write-only file descriptor %qE",
637 : : m_callee_fndecl, m_arg);
638 : 14 : return true;
639 : 0 : default:
640 : 0 : gcc_unreachable ();
641 : : }
642 : : }
643 : :
644 : : private:
645 : : enum access_directions m_fd_dir;
646 : : };
647 : :
648 : 0 : class fd_double_close : public fd_diagnostic
649 : : {
650 : : public:
651 : 28 : fd_double_close (const fd_state_machine &sm, tree arg) : fd_diagnostic (sm, arg)
652 : : {
653 : : }
654 : :
655 : : const char *
656 : 264 : get_kind () const final override
657 : : {
658 : 264 : return "fd_double_close";
659 : : }
660 : :
661 : : int
662 : 56 : get_controlling_option () const final override
663 : : {
664 : 56 : return OPT_Wanalyzer_fd_double_close;
665 : : }
666 : : bool
667 : 28 : emit (diagnostic_emission_context &ctxt) final override
668 : : {
669 : : // CWE-1341: Multiple Releases of Same Resource or Handle
670 : 28 : ctxt.add_cwe (1341);
671 : 28 : return ctxt.warn ("double %<close%> of file descriptor %qE", m_arg);
672 : : }
673 : :
674 : : bool
675 : 136 : describe_state_change (pretty_printer &pp,
676 : : const evdesc::state_change &change) override
677 : : {
678 : 272 : if (m_sm.is_unchecked_fd_p (change.m_new_state))
679 : : {
680 : 48 : pp_string (&pp, "opened here");
681 : 48 : return true;
682 : : }
683 : :
684 : 88 : if (change.m_new_state == m_sm.m_closed)
685 : : {
686 : 56 : m_first_close_event = change.m_event_id;
687 : 56 : pp_printf (&pp, "first %qs here", "close");
688 : 56 : return true;
689 : : }
690 : 32 : return fd_diagnostic::describe_state_change (pp, change);
691 : : }
692 : :
693 : : bool
694 : 56 : describe_final_event (pretty_printer &pp,
695 : : const evdesc::final_event &) final override
696 : : {
697 : 56 : if (m_first_close_event.known_p ())
698 : 56 : pp_printf (&pp,
699 : : "second %qs here; first %qs was at %@",
700 : : "close", "close", &m_first_close_event);
701 : : else
702 : 0 : pp_printf (&pp,
703 : : "second %qs here",
704 : : "close");
705 : 56 : return true;
706 : : }
707 : :
708 : : private:
709 : : diagnostics::paths::event_id_t m_first_close_event;
710 : : };
711 : :
712 : 0 : class fd_use_after_close : public fd_param_diagnostic
713 : : {
714 : : public:
715 : 3 : fd_use_after_close (const fd_state_machine &sm, tree arg,
716 : : const tree callee_fndecl, const char *attr_name,
717 : : int arg_idx)
718 : 3 : : fd_param_diagnostic (sm, arg, callee_fndecl, attr_name, arg_idx)
719 : : {
720 : : }
721 : :
722 : 29 : fd_use_after_close (const fd_state_machine &sm, tree arg,
723 : : const tree callee_fndecl)
724 : 29 : : fd_param_diagnostic (sm, arg, callee_fndecl)
725 : : {
726 : : }
727 : :
728 : : const char *
729 : 125 : get_kind () const final override
730 : : {
731 : 125 : return "fd_use_after_close";
732 : : }
733 : :
734 : : int
735 : 39 : get_controlling_option () const final override
736 : : {
737 : 39 : return OPT_Wanalyzer_fd_use_after_close;
738 : : }
739 : :
740 : : bool
741 : 7 : emit (diagnostic_emission_context &ctxt) final override
742 : : {
743 : 7 : bool warned = ctxt.warn ("%qE on closed file descriptor %qE",
744 : : m_callee_fndecl, m_arg);
745 : 7 : if (warned)
746 : 7 : inform_filedescriptor_attribute (DIRS_READ_WRITE);
747 : 7 : return warned;
748 : : }
749 : :
750 : : bool
751 : 32 : describe_state_change (pretty_printer &pp,
752 : : const evdesc::state_change &change) override
753 : : {
754 : 64 : if (m_sm.is_unchecked_fd_p (change.m_new_state))
755 : : {
756 : 14 : pp_string (&pp, "opened here");
757 : 14 : return true;
758 : : }
759 : :
760 : 18 : if (change.m_new_state == m_sm.m_closed)
761 : : {
762 : 14 : m_first_close_event = change.m_event_id;
763 : 14 : pp_string (&pp, "closed here");
764 : 14 : return true;
765 : : }
766 : :
767 : 4 : return fd_diagnostic::describe_state_change (pp, change);
768 : : }
769 : :
770 : : bool
771 : 14 : describe_final_event (pretty_printer &pp,
772 : : const evdesc::final_event &) final override
773 : : {
774 : 14 : if (m_first_close_event.known_p ())
775 : 14 : pp_printf (&pp,
776 : : "%qE on closed file descriptor %qE;"
777 : : " %qs was at %@",
778 : : m_callee_fndecl, m_arg,
779 : : "close", &m_first_close_event);
780 : : else
781 : 0 : pp_printf (&pp,
782 : : "%qE on closed file descriptor %qE",
783 : : m_callee_fndecl, m_arg);
784 : 14 : return true;
785 : : }
786 : :
787 : : private:
788 : : diagnostics::paths::event_id_t m_first_close_event;
789 : : };
790 : :
791 : 0 : class fd_use_without_check : public fd_param_diagnostic
792 : : {
793 : : public:
794 : 3 : fd_use_without_check (const fd_state_machine &sm, tree arg,
795 : : const tree callee_fndecl, const char *attr_name,
796 : : int arg_idx)
797 : 3 : : fd_param_diagnostic (sm, arg, callee_fndecl, attr_name, arg_idx)
798 : : {
799 : : }
800 : :
801 : 80 : fd_use_without_check (const fd_state_machine &sm, tree arg,
802 : : const tree callee_fndecl)
803 : 80 : : fd_param_diagnostic (sm, arg, callee_fndecl)
804 : : {
805 : : }
806 : :
807 : : const char *
808 : 704 : get_kind () const final override
809 : : {
810 : 704 : return "fd_use_without_check";
811 : : }
812 : :
813 : : int
814 : 123 : get_controlling_option () const final override
815 : : {
816 : 123 : return OPT_Wanalyzer_fd_use_without_check;
817 : : }
818 : :
819 : : bool
820 : 40 : emit (diagnostic_emission_context &ctxt) final override
821 : : {
822 : 40 : bool warned = ctxt.warn ("%qE on possibly invalid file descriptor %qE",
823 : : m_callee_fndecl, m_arg);
824 : 40 : if (warned)
825 : 40 : inform_filedescriptor_attribute (DIRS_READ_WRITE);
826 : 40 : return warned;
827 : : }
828 : :
829 : : bool
830 : 64 : describe_state_change (pretty_printer &pp,
831 : : const evdesc::state_change &change) override
832 : : {
833 : 128 : if (m_sm.is_unchecked_fd_p (change.m_new_state))
834 : : {
835 : 56 : m_first_open_event = change.m_event_id;
836 : 56 : pp_string (&pp, "opened here");
837 : 56 : return true;
838 : : }
839 : :
840 : 8 : return fd_diagnostic::describe_state_change (pp, change);
841 : : }
842 : :
843 : : bool
844 : 80 : describe_final_event (pretty_printer &pp,
845 : : const evdesc::final_event &) final override
846 : : {
847 : 80 : if (m_first_open_event.known_p ())
848 : 56 : pp_printf (&pp,
849 : : "%qE could be invalid: unchecked value from %@", m_arg,
850 : : &m_first_open_event);
851 : : else
852 : 24 : pp_printf (&pp,
853 : : "%qE could be invalid", m_arg);
854 : 80 : return true;
855 : : }
856 : :
857 : : private:
858 : : diagnostics::paths::event_id_t m_first_open_event;
859 : : };
860 : :
861 : : /* Concrete pending_diagnostic subclass for -Wanalyzer-fd-phase-mismatch. */
862 : :
863 : 0 : class fd_phase_mismatch : public fd_param_diagnostic
864 : : {
865 : : public:
866 : 64 : fd_phase_mismatch (const fd_state_machine &sm, tree arg,
867 : : const tree callee_fndecl,
868 : : state_machine::state_t actual_state,
869 : : enum expected_phase expected_phase)
870 : 64 : : fd_param_diagnostic (sm, arg, callee_fndecl),
871 : 64 : m_actual_state (actual_state),
872 : 64 : m_expected_phase (expected_phase)
873 : : {
874 : 64 : gcc_assert (m_sm.is_socket_fd_p (actual_state));
875 : 64 : switch (expected_phase)
876 : : {
877 : 4 : case EXPECTED_PHASE_CAN_TRANSFER:
878 : 4 : gcc_assert (actual_state == m_sm.m_new_stream_socket
879 : : || actual_state == m_sm.m_bound_stream_socket
880 : : || actual_state == m_sm.m_listening_stream_socket);
881 : : break;
882 : 16 : case EXPECTED_PHASE_CAN_BIND:
883 : 16 : gcc_assert (actual_state == m_sm.m_bound_datagram_socket
884 : : || actual_state == m_sm.m_bound_stream_socket
885 : : || actual_state == m_sm.m_bound_unknown_socket
886 : : || actual_state == m_sm.m_connected_stream_socket
887 : : || actual_state == m_sm.m_listening_stream_socket);
888 : : break;
889 : 38 : case EXPECTED_PHASE_CAN_LISTEN:
890 : 38 : gcc_assert (actual_state == m_sm.m_new_stream_socket
891 : : || actual_state == m_sm.m_new_unknown_socket
892 : : || actual_state == m_sm.m_connected_stream_socket);
893 : : break;
894 : 4 : case EXPECTED_PHASE_CAN_ACCEPT:
895 : 4 : gcc_assert (actual_state == m_sm.m_new_stream_socket
896 : : || actual_state == m_sm.m_new_unknown_socket
897 : : || actual_state == m_sm.m_bound_stream_socket
898 : : || actual_state == m_sm.m_bound_unknown_socket
899 : : || actual_state == m_sm.m_connected_stream_socket);
900 : : break;
901 : 2 : case EXPECTED_PHASE_CAN_CONNECT:
902 : 2 : gcc_assert (actual_state == m_sm.m_bound_datagram_socket
903 : : || actual_state == m_sm.m_bound_stream_socket
904 : : || actual_state == m_sm.m_bound_unknown_socket
905 : : || actual_state == m_sm.m_listening_stream_socket
906 : : || actual_state == m_sm.m_connected_stream_socket);
907 : : break;
908 : : }
909 : 64 : }
910 : :
911 : : const char *
912 : 477 : get_kind () const final override
913 : : {
914 : 477 : return "fd_phase_mismatch";
915 : : }
916 : :
917 : : bool
918 : 64 : subclass_equal_p (const pending_diagnostic &base_other) const final override
919 : : {
920 : 64 : const fd_phase_mismatch &sub_other = (const fd_phase_mismatch &)base_other;
921 : 64 : if (!fd_param_diagnostic ::subclass_equal_p (sub_other))
922 : : return false;
923 : 64 : return (m_actual_state == sub_other.m_actual_state
924 : 64 : && m_expected_phase == sub_other.m_expected_phase);
925 : : }
926 : :
927 : : int
928 : 87 : get_controlling_option () const final override
929 : : {
930 : 87 : return OPT_Wanalyzer_fd_phase_mismatch;
931 : : }
932 : :
933 : : bool
934 : 23 : emit (diagnostic_emission_context &ctxt) final override
935 : : {
936 : : /* CWE-666: Operation on Resource in Wrong Phase of Lifetime. */
937 : 23 : ctxt.add_cwe (666);
938 : 23 : return ctxt.warn ("%qE on file descriptor %qE in wrong phase",
939 : 23 : m_callee_fndecl, m_arg);
940 : : }
941 : :
942 : : bool
943 : 46 : describe_final_event (pretty_printer &pp,
944 : : const evdesc::final_event &) final override
945 : : {
946 : 46 : switch (m_expected_phase)
947 : : {
948 : 6 : case EXPECTED_PHASE_CAN_TRANSFER:
949 : 6 : {
950 : 6 : if (m_actual_state == m_sm.m_new_stream_socket)
951 : : {
952 : 2 : pp_printf (&pp,
953 : : "%qE expects a stream socket to be connected via %qs"
954 : : " but %qE has not yet been bound",
955 : : m_callee_fndecl, "accept", m_arg);
956 : 2 : return true;
957 : : }
958 : 4 : if (m_actual_state == m_sm.m_bound_stream_socket)
959 : : {
960 : 0 : pp_printf (&pp,
961 : : "%qE expects a stream socket to be connected via %qs"
962 : : " but %qE is not yet listening",
963 : : m_callee_fndecl, "accept", m_arg);
964 : 0 : return true;
965 : : }
966 : 4 : if (m_actual_state == m_sm.m_listening_stream_socket)
967 : : {
968 : 4 : pp_printf (&pp,
969 : : "%qE expects a stream socket to be connected via"
970 : : " the return value of %qs"
971 : : " but %qE is listening; wrong file descriptor?",
972 : : m_callee_fndecl, "accept", m_arg);
973 : 4 : return true;
974 : : }
975 : : }
976 : : break;
977 : 8 : case EXPECTED_PHASE_CAN_BIND:
978 : 8 : {
979 : 8 : if (m_actual_state == m_sm.m_bound_datagram_socket
980 : 8 : || m_actual_state == m_sm.m_bound_stream_socket
981 : 8 : || m_actual_state == m_sm.m_bound_unknown_socket)
982 : : {
983 : 2 : pp_printf (&pp,
984 : : "%qE expects a new socket file descriptor"
985 : : " but %qE has already been bound",
986 : : m_callee_fndecl, m_arg);
987 : 2 : return true;
988 : : }
989 : 6 : if (m_actual_state == m_sm.m_connected_stream_socket)
990 : : {
991 : 2 : pp_printf (&pp,
992 : : "%qE expects a new socket file descriptor"
993 : : " but %qE is already connected",
994 : : m_callee_fndecl, m_arg);
995 : 2 : return true;
996 : : }
997 : 4 : if (m_actual_state == m_sm.m_listening_stream_socket)
998 : : {
999 : 4 : pp_printf (&pp,
1000 : : "%qE expects a new socket file descriptor"
1001 : : " but %qE is already listening",
1002 : : m_callee_fndecl, m_arg);
1003 : 4 : return true;
1004 : : }
1005 : : }
1006 : : break;
1007 : 26 : case EXPECTED_PHASE_CAN_LISTEN:
1008 : 26 : {
1009 : 26 : if (m_actual_state == m_sm.m_new_stream_socket
1010 : 20 : || m_actual_state == m_sm.m_new_unknown_socket)
1011 : : {
1012 : 24 : pp_printf (&pp,
1013 : : "%qE expects a bound stream socket file descriptor"
1014 : : " but %qE has not yet been bound",
1015 : : m_callee_fndecl, m_arg);
1016 : 24 : return true;
1017 : : }
1018 : 2 : if (m_actual_state == m_sm.m_connected_stream_socket)
1019 : : {
1020 : 2 : pp_printf (&pp,
1021 : : "%qE expects a bound stream socket file descriptor"
1022 : : " but %qE is connected",
1023 : : m_callee_fndecl, m_arg);
1024 : 2 : return true;
1025 : : }
1026 : : }
1027 : : break;
1028 : 4 : case EXPECTED_PHASE_CAN_ACCEPT:
1029 : 4 : {
1030 : 4 : if (m_actual_state == m_sm.m_new_stream_socket
1031 : 2 : || m_actual_state == m_sm.m_new_unknown_socket)
1032 : : {
1033 : 2 : pp_printf (&pp,
1034 : : "%qE expects a listening stream socket file descriptor"
1035 : : " but %qE has not yet been bound",
1036 : : m_callee_fndecl, m_arg);
1037 : 2 : return true;
1038 : : }
1039 : 2 : if (m_actual_state == m_sm.m_bound_stream_socket
1040 : 2 : || m_actual_state == m_sm.m_bound_unknown_socket)
1041 : : {
1042 : 0 : pp_printf (&pp,
1043 : : "%qE expects a listening stream socket file descriptor"
1044 : : " whereas %qE is bound but not yet listening",
1045 : : m_callee_fndecl, m_arg);
1046 : 0 : return true;
1047 : : }
1048 : 2 : if (m_actual_state == m_sm.m_connected_stream_socket)
1049 : : {
1050 : 2 : pp_printf (&pp,
1051 : : "%qE expects a listening stream socket file descriptor"
1052 : : " but %qE is connected",
1053 : : m_callee_fndecl, m_arg);
1054 : 2 : return true;
1055 : : }
1056 : : }
1057 : : break;
1058 : 2 : case EXPECTED_PHASE_CAN_CONNECT:
1059 : 2 : {
1060 : 2 : if (m_actual_state == m_sm.m_bound_datagram_socket
1061 : 2 : || m_actual_state == m_sm.m_bound_stream_socket
1062 : 0 : || m_actual_state == m_sm.m_bound_unknown_socket)
1063 : : {
1064 : 2 : pp_printf (&pp,
1065 : : "%qE expects a new socket file descriptor"
1066 : : " but %qE is bound",
1067 : : m_callee_fndecl, m_arg);
1068 : 2 : return true;
1069 : : }
1070 : : else
1071 : : {
1072 : 0 : pp_printf (&pp,
1073 : : "%qE expects a new socket file descriptor",
1074 : : m_callee_fndecl);
1075 : 0 : return true;
1076 : : }
1077 : : }
1078 : 0 : break;
1079 : : }
1080 : 0 : gcc_unreachable ();
1081 : : }
1082 : :
1083 : : private:
1084 : : state_machine::state_t m_actual_state;
1085 : : enum expected_phase m_expected_phase;
1086 : : };
1087 : :
1088 : : /* Enum for use by -Wanalyzer-fd-type-mismatch. */
1089 : :
1090 : : enum expected_type
1091 : : {
1092 : : EXPECTED_TYPE_SOCKET,
1093 : : EXPECTED_TYPE_STREAM_SOCKET
1094 : : };
1095 : :
1096 : : /* Concrete pending_diagnostic subclass for -Wanalyzer-fd-type-mismatch. */
1097 : :
1098 : 0 : class fd_type_mismatch : public fd_param_diagnostic
1099 : : {
1100 : : public:
1101 : 14 : fd_type_mismatch (const fd_state_machine &sm, tree arg,
1102 : : const tree callee_fndecl,
1103 : : state_machine::state_t actual_state,
1104 : : enum expected_type expected_type)
1105 : 14 : : fd_param_diagnostic (sm, arg, callee_fndecl),
1106 : 14 : m_actual_state (actual_state),
1107 : 14 : m_expected_type (expected_type)
1108 : : {
1109 : : }
1110 : :
1111 : : const char *
1112 : 107 : get_kind () const final override
1113 : : {
1114 : 107 : return "fd_type_mismatch";
1115 : : }
1116 : :
1117 : : bool
1118 : 14 : subclass_equal_p (const pending_diagnostic &base_other) const final override
1119 : : {
1120 : 14 : const fd_type_mismatch &sub_other = (const fd_type_mismatch &)base_other;
1121 : 14 : if (!fd_param_diagnostic ::subclass_equal_p (sub_other))
1122 : : return false;
1123 : 14 : return (m_actual_state == sub_other.m_actual_state
1124 : 14 : && m_expected_type == sub_other.m_expected_type);
1125 : : }
1126 : :
1127 : : int
1128 : 19 : get_controlling_option () const final override
1129 : : {
1130 : 19 : return OPT_Wanalyzer_fd_type_mismatch;
1131 : : }
1132 : :
1133 : : bool
1134 : 5 : emit (diagnostic_emission_context &ctxt) final override
1135 : : {
1136 : 5 : switch (m_expected_type)
1137 : : {
1138 : 0 : default:
1139 : 0 : gcc_unreachable ();
1140 : 1 : case EXPECTED_TYPE_SOCKET:
1141 : 1 : return ctxt.warn ("%qE on non-socket file descriptor %qE",
1142 : 1 : m_callee_fndecl, m_arg);
1143 : 4 : case EXPECTED_TYPE_STREAM_SOCKET:
1144 : 8 : if (m_sm.is_datagram_socket_fd_p (m_actual_state))
1145 : 4 : return ctxt.warn ("%qE on datagram socket file descriptor %qE",
1146 : 4 : m_callee_fndecl, m_arg);
1147 : : else
1148 : 0 : return ctxt.warn ("%qE on non-stream-socket file descriptor %qE",
1149 : 0 : m_callee_fndecl, m_arg);
1150 : : }
1151 : : }
1152 : :
1153 : : bool
1154 : 10 : describe_final_event (pretty_printer &pp,
1155 : : const evdesc::final_event &) final override
1156 : : {
1157 : 10 : switch (m_expected_type)
1158 : : {
1159 : : default:
1160 : : break;
1161 : : gcc_unreachable ();
1162 : 10 : case EXPECTED_TYPE_SOCKET:
1163 : 10 : case EXPECTED_TYPE_STREAM_SOCKET:
1164 : 10 : if (!m_sm.is_socket_fd_p (m_actual_state))
1165 : : {
1166 : 2 : pp_printf (&pp,
1167 : : "%qE expects a socket file descriptor"
1168 : : " but %qE is not a socket",
1169 : : m_callee_fndecl, m_arg);
1170 : 2 : return true;
1171 : : }
1172 : : }
1173 : 8 : gcc_assert (m_expected_type == EXPECTED_TYPE_STREAM_SOCKET);
1174 : 0 : gcc_assert (m_sm.is_datagram_socket_fd_p (m_actual_state));
1175 : 8 : pp_printf (&pp,
1176 : : "%qE expects a stream socket file descriptor"
1177 : : " but %qE is a datagram socket",
1178 : : m_callee_fndecl, m_arg);
1179 : 8 : return true;
1180 : : }
1181 : :
1182 : : private:
1183 : : state_machine::state_t m_actual_state;
1184 : : enum expected_type m_expected_type;
1185 : : };
1186 : :
1187 : 3310 : fd_state_machine::fd_state_machine (logger *logger)
1188 : : : state_machine ("file-descriptor", logger),
1189 : 6620 : m_constant_fd (add_state ("fd-constant")),
1190 : 3310 : m_unchecked_read_write (add_state ("fd-unchecked-read-write")),
1191 : 3310 : m_unchecked_read_only (add_state ("fd-unchecked-read-only")),
1192 : 3310 : m_unchecked_write_only (add_state ("fd-unchecked-write-only")),
1193 : 3310 : m_valid_read_write (add_state ("fd-valid-read-write")),
1194 : 3310 : m_valid_read_only (add_state ("fd-valid-read-only")),
1195 : 3310 : m_valid_write_only (add_state ("fd-valid-write-only")),
1196 : 3310 : m_invalid (add_state ("fd-invalid")),
1197 : 3310 : m_closed (add_state ("fd-closed")),
1198 : 3310 : m_new_datagram_socket (add_state ("fd-new-datagram-socket")),
1199 : 3310 : m_new_stream_socket (add_state ("fd-new-stream-socket")),
1200 : 3310 : m_new_unknown_socket (add_state ("fd-new-unknown-socket")),
1201 : 3310 : m_bound_datagram_socket (add_state ("fd-bound-datagram-socket")),
1202 : 3310 : m_bound_stream_socket (add_state ("fd-bound-stream-socket")),
1203 : 3310 : m_bound_unknown_socket (add_state ("fd-bound-unknown-socket")),
1204 : 3310 : m_listening_stream_socket (add_state ("fd-listening-stream-socket")),
1205 : 3310 : m_connected_stream_socket (add_state ("fd-connected-stream-socket")),
1206 : 3310 : m_stop (add_state ("fd-stop")),
1207 : 3310 : m_O_ACCMODE (get_stashed_constant_by_name ("O_ACCMODE")),
1208 : 3310 : m_O_RDONLY (get_stashed_constant_by_name ("O_RDONLY")),
1209 : 3310 : m_O_WRONLY (get_stashed_constant_by_name ("O_WRONLY")),
1210 : 3310 : m_SOCK_STREAM (get_stashed_constant_by_name ("SOCK_STREAM")),
1211 : 6620 : m_SOCK_DGRAM (get_stashed_constant_by_name ("SOCK_DGRAM"))
1212 : : {
1213 : 3310 : }
1214 : :
1215 : : bool
1216 : 1051356 : fd_state_machine::is_unchecked_fd_p (state_t s) const
1217 : : {
1218 : 1051356 : return (s == m_unchecked_read_write
1219 : 1051016 : || s == m_unchecked_read_only
1220 : 1051506 : || s == m_unchecked_write_only);
1221 : : }
1222 : :
1223 : : bool
1224 : 1050961 : fd_state_machine::is_valid_fd_p (state_t s) const
1225 : : {
1226 : 1050961 : return (s == m_valid_read_write
1227 : 1050819 : || s == m_valid_read_only
1228 : 1050905 : || s == m_valid_write_only);
1229 : : }
1230 : :
1231 : : bool
1232 : 1050235 : fd_state_machine::is_socket_fd_p (state_t s) const
1233 : : {
1234 : 1050235 : return (s == m_new_datagram_socket
1235 : 1050136 : || s == m_new_stream_socket
1236 : 1050055 : || s == m_new_unknown_socket
1237 : 1049770 : || s == m_bound_datagram_socket
1238 : 1049696 : || s == m_bound_stream_socket
1239 : 1049632 : || s == m_bound_unknown_socket
1240 : 1049438 : || s == m_listening_stream_socket
1241 : 2099552 : || s == m_connected_stream_socket);
1242 : : }
1243 : :
1244 : : bool
1245 : 12 : fd_state_machine::is_datagram_socket_fd_p (state_t s) const
1246 : : {
1247 : 12 : return (s == m_new_datagram_socket
1248 : 3 : || s == m_new_unknown_socket
1249 : 3 : || s == m_bound_datagram_socket
1250 : 12 : || s == m_bound_unknown_socket);
1251 : : }
1252 : :
1253 : : bool
1254 : 52 : fd_state_machine::is_stream_socket_fd_p (state_t s) const
1255 : : {
1256 : 52 : return (s == m_new_stream_socket
1257 : 42 : || s == m_new_unknown_socket
1258 : 14 : || s == m_bound_stream_socket
1259 : 14 : || s == m_bound_unknown_socket
1260 : 14 : || s == m_listening_stream_socket
1261 : 66 : || s == m_connected_stream_socket);
1262 : : }
1263 : :
1264 : : enum access_mode
1265 : 87 : fd_state_machine::get_access_mode_from_flag (int flag) const
1266 : : {
1267 : 87 : if (m_O_ACCMODE && TREE_CODE (m_O_ACCMODE) == INTEGER_CST)
1268 : : {
1269 : 37 : const unsigned HOST_WIDE_INT mask_val = TREE_INT_CST_LOW (m_O_ACCMODE);
1270 : 37 : const unsigned HOST_WIDE_INT masked_flag = flag & mask_val;
1271 : :
1272 : 37 : if (m_O_RDONLY && TREE_CODE (m_O_RDONLY) == INTEGER_CST)
1273 : 37 : if (masked_flag == TREE_INT_CST_LOW (m_O_RDONLY))
1274 : : return READ_ONLY;
1275 : :
1276 : 31 : if (m_O_WRONLY && TREE_CODE (m_O_WRONLY) == INTEGER_CST)
1277 : 31 : if (masked_flag == TREE_INT_CST_LOW (m_O_WRONLY))
1278 : 6 : return WRITE_ONLY;
1279 : : }
1280 : : return READ_WRITE;
1281 : : }
1282 : :
1283 : : bool
1284 : 72 : fd_state_machine::is_readonly_fd_p (state_t state) const
1285 : : {
1286 : 72 : return (state == m_unchecked_read_only || state == m_valid_read_only);
1287 : : }
1288 : :
1289 : : bool
1290 : 94 : fd_state_machine::is_writeonly_fd_p (state_t state) const
1291 : : {
1292 : 94 : return (state == m_unchecked_write_only || state == m_valid_write_only);
1293 : : }
1294 : :
1295 : : bool
1296 : 1272 : fd_state_machine::is_closed_fd_p (state_t state) const
1297 : : {
1298 : 1272 : return (state == m_closed);
1299 : : }
1300 : :
1301 : : bool
1302 : 104 : fd_state_machine::is_constant_fd_p (state_t state) const
1303 : : {
1304 : 104 : return (state == m_constant_fd);
1305 : : }
1306 : :
1307 : : fd_state_machine::state_t
1308 : 9 : fd_state_machine::valid_to_unchecked_state (state_t state) const
1309 : : {
1310 : 9 : if (state == m_valid_read_write)
1311 : 6 : return m_unchecked_read_write;
1312 : 3 : else if (state == m_valid_write_only)
1313 : 2 : return m_unchecked_write_only;
1314 : 1 : else if (state == m_valid_read_only)
1315 : 1 : return m_unchecked_read_only;
1316 : : else
1317 : 0 : gcc_unreachable ();
1318 : : return nullptr;
1319 : : }
1320 : :
1321 : : void
1322 : 18 : fd_state_machine::mark_as_valid_fd (region_model *model,
1323 : : sm_state_map *smap,
1324 : : const svalue *fd_sval,
1325 : : const extrinsic_state &ext_state) const
1326 : : {
1327 : 0 : smap->set_state (model, fd_sval, m_valid_read_write, nullptr, ext_state);
1328 : 0 : }
1329 : :
1330 : : bool
1331 : 269916 : fd_state_machine::on_stmt (sm_context &sm_ctxt, const supernode *node,
1332 : : const gimple *stmt) const
1333 : : {
1334 : 269916 : if (const gcall *call = dyn_cast<const gcall *> (stmt))
1335 : 57789 : if (tree callee_fndecl = sm_ctxt.get_fndecl_for_call (*call))
1336 : : {
1337 : 56307 : if (is_named_call_p (callee_fndecl, "open", *call, 2))
1338 : : {
1339 : 95 : on_open (sm_ctxt, node, stmt, *call);
1340 : 95 : return true;
1341 : : } // "open"
1342 : :
1343 : 56212 : if (is_named_call_p (callee_fndecl, "creat", *call, 2))
1344 : : {
1345 : 14 : on_creat (sm_ctxt, node, stmt, *call);
1346 : 14 : return true;
1347 : : } // "creat"
1348 : :
1349 : 56198 : if (is_named_call_p (callee_fndecl, "close", *call, 1))
1350 : : {
1351 : 503 : on_close (sm_ctxt, node, stmt, *call);
1352 : 503 : return true;
1353 : : } // "close"
1354 : :
1355 : 55695 : if (is_named_call_p (callee_fndecl, "write", *call, 3))
1356 : : {
1357 : 82 : on_write (sm_ctxt, node, stmt, *call, callee_fndecl);
1358 : 82 : return true;
1359 : : } // "write"
1360 : :
1361 : 55613 : if (is_named_call_p (callee_fndecl, "read", *call, 3))
1362 : : {
1363 : 104 : on_read (sm_ctxt, node, stmt, *call, callee_fndecl);
1364 : 104 : return true;
1365 : : } // "read"
1366 : :
1367 : 55509 : if (is_named_call_p (callee_fndecl, "dup", *call, 1))
1368 : : {
1369 : 16 : check_for_dup (sm_ctxt, node, stmt, *call, callee_fndecl, DUP_1);
1370 : 16 : return true;
1371 : : }
1372 : :
1373 : 55493 : if (is_named_call_p (callee_fndecl, "dup2", *call, 2))
1374 : : {
1375 : 10 : check_for_dup (sm_ctxt, node, stmt, *call, callee_fndecl, DUP_2);
1376 : 10 : return true;
1377 : : }
1378 : :
1379 : 55483 : if (is_named_call_p (callee_fndecl, "dup3", *call, 3))
1380 : : {
1381 : 4 : check_for_dup (sm_ctxt, node, stmt, *call, callee_fndecl, DUP_3);
1382 : 4 : return true;
1383 : : }
1384 : :
1385 : 55479 : {
1386 : : // Handle __attribute__((fd_arg))
1387 : :
1388 : 55479 : check_for_fd_attrs (sm_ctxt, node, stmt, *call, callee_fndecl,
1389 : : "fd_arg", DIRS_READ_WRITE);
1390 : :
1391 : : // Handle __attribute__((fd_arg_read))
1392 : :
1393 : 55479 : check_for_fd_attrs (sm_ctxt, node, stmt, *call, callee_fndecl,
1394 : : "fd_arg_read", DIRS_READ);
1395 : :
1396 : : // Handle __attribute__((fd_arg_write))
1397 : :
1398 : 55479 : check_for_fd_attrs (sm_ctxt, node, stmt, *call, callee_fndecl,
1399 : : "fd_arg_write", DIRS_WRITE);
1400 : : }
1401 : : }
1402 : :
1403 : : return false;
1404 : : }
1405 : :
1406 : : void
1407 : 166437 : fd_state_machine::check_for_fd_attrs (
1408 : : sm_context &sm_ctxt, const supernode *node, const gimple *stmt,
1409 : : const gcall &call, const tree callee_fndecl, const char *attr_name,
1410 : : access_directions fd_attr_access_dir) const
1411 : : {
1412 : : /* Handle interesting fd attributes of the callee_fndecl,
1413 : : or prioritize those of the builtin that callee_fndecl is
1414 : : expected to be.
1415 : : Might want this to be controlled by a flag. */
1416 : 166437 : tree fndecl = callee_fndecl;
1417 : : /* If call is recognized as a builtin known_function,
1418 : : use that builtin's function_decl. */
1419 : 166437 : if (const region_model *old_model = sm_ctxt.get_old_region_model ())
1420 : 332874 : if (const builtin_known_function *builtin_kf
1421 : 166437 : = old_model->get_builtin_kf (call))
1422 : 65496 : fndecl = builtin_kf->builtin_decl ();
1423 : :
1424 : 166437 : tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (fndecl));
1425 : 166437 : attrs = lookup_attribute (attr_name, attrs);
1426 : 166437 : if (!attrs)
1427 : 166425 : return;
1428 : :
1429 : 12 : if (!TREE_VALUE (attrs))
1430 : : return;
1431 : :
1432 : 12 : auto_bitmap argmap;
1433 : :
1434 : 24 : for (tree idx = TREE_VALUE (attrs); idx; idx = TREE_CHAIN (idx))
1435 : : {
1436 : 12 : unsigned int val = TREE_INT_CST_LOW (TREE_VALUE (idx)) - 1;
1437 : 12 : bitmap_set_bit (argmap, val);
1438 : : }
1439 : 12 : if (bitmap_empty_p (argmap))
1440 : 0 : return;
1441 : :
1442 : 24 : for (unsigned arg_idx = 0; arg_idx < gimple_call_num_args (&call); arg_idx++)
1443 : : {
1444 : 12 : tree arg = gimple_call_arg (&call, arg_idx);
1445 : 12 : tree diag_arg = sm_ctxt.get_diagnostic_tree (arg);
1446 : 12 : state_t state = sm_ctxt.get_state (stmt, arg);
1447 : 12 : bool bit_set = bitmap_bit_p (argmap, arg_idx);
1448 : 12 : if (TREE_CODE (TREE_TYPE (arg)) != INTEGER_TYPE)
1449 : 6 : continue;
1450 : 12 : if (bit_set) // Check if arg_idx is marked by any of the file descriptor
1451 : : // attributes
1452 : : {
1453 : :
1454 : : /* Do use the fndecl that caused the warning so that the
1455 : : misused attributes are printed and the user not confused. */
1456 : 12 : if (is_closed_fd_p (state))
1457 : : {
1458 : :
1459 : 3 : sm_ctxt.warn (node, stmt, arg,
1460 : : std::make_unique<fd_use_after_close>
1461 : 3 : (*this, diag_arg,
1462 : : fndecl, attr_name,
1463 : : arg_idx));
1464 : 3 : continue;
1465 : : }
1466 : :
1467 : 12 : if (!(is_valid_fd_p (state) || (state == m_stop)))
1468 : : {
1469 : 3 : if (!is_constant_fd_p (state))
1470 : : {
1471 : 3 : sm_ctxt.warn (node, stmt, arg,
1472 : : std::make_unique<fd_use_without_check>
1473 : 3 : (*this, diag_arg,
1474 : : fndecl, attr_name,
1475 : : arg_idx));
1476 : 3 : continue;
1477 : : }
1478 : : }
1479 : :
1480 : 6 : switch (fd_attr_access_dir)
1481 : : {
1482 : : case DIRS_READ_WRITE:
1483 : : break;
1484 : 3 : case DIRS_READ:
1485 : :
1486 : 3 : if (is_writeonly_fd_p (state))
1487 : : {
1488 : 3 : sm_ctxt.warn
1489 : 3 : (node, stmt, arg,
1490 : 3 : std::make_unique<fd_access_mode_mismatch> (*this, diag_arg,
1491 : 6 : DIRS_WRITE,
1492 : : fndecl,
1493 : : attr_name,
1494 : : arg_idx));
1495 : : }
1496 : :
1497 : : break;
1498 : 3 : case DIRS_WRITE:
1499 : :
1500 : 9 : if (is_readonly_fd_p (state))
1501 : : {
1502 : 3 : sm_ctxt.warn
1503 : 3 : (node, stmt, arg,
1504 : 3 : std::make_unique<fd_access_mode_mismatch> (*this, diag_arg,
1505 : 6 : DIRS_READ,
1506 : : fndecl,
1507 : : attr_name,
1508 : : arg_idx));
1509 : : }
1510 : :
1511 : : break;
1512 : : }
1513 : : }
1514 : : }
1515 : 12 : }
1516 : :
1517 : :
1518 : : void
1519 : 95 : fd_state_machine::on_open (sm_context &sm_ctxt, const supernode *node,
1520 : : const gimple *stmt, const gcall &call) const
1521 : : {
1522 : 95 : tree lhs = gimple_call_lhs (&call);
1523 : 95 : if (lhs)
1524 : : {
1525 : 91 : tree arg = gimple_call_arg (&call, 1);
1526 : 91 : enum access_mode mode = READ_WRITE;
1527 : 91 : if (TREE_CODE (arg) == INTEGER_CST)
1528 : : {
1529 : 87 : int flag = TREE_INT_CST_LOW (arg);
1530 : 87 : mode = get_access_mode_from_flag (flag);
1531 : : }
1532 : 87 : switch (mode)
1533 : : {
1534 : 6 : case READ_ONLY:
1535 : 6 : sm_ctxt.on_transition (node, stmt, lhs, m_start,
1536 : 6 : m_unchecked_read_only);
1537 : 6 : break;
1538 : 6 : case WRITE_ONLY:
1539 : 6 : sm_ctxt.on_transition (node, stmt, lhs, m_start,
1540 : 6 : m_unchecked_write_only);
1541 : 6 : break;
1542 : 79 : default:
1543 : 79 : sm_ctxt.on_transition (node, stmt, lhs, m_start,
1544 : 79 : m_unchecked_read_write);
1545 : : }
1546 : : }
1547 : : else
1548 : : {
1549 : 4 : sm_ctxt.warn (node, stmt, NULL_TREE,
1550 : 4 : std::make_unique<fd_leak> (*this, NULL_TREE, nullptr));
1551 : : }
1552 : 95 : }
1553 : :
1554 : : void
1555 : 14 : fd_state_machine::on_creat (sm_context &sm_ctxt, const supernode *node,
1556 : : const gimple *stmt, const gcall &call) const
1557 : : {
1558 : 14 : tree lhs = gimple_call_lhs (&call);
1559 : 14 : if (lhs)
1560 : 10 : sm_ctxt.on_transition (node, stmt, lhs, m_start, m_unchecked_write_only);
1561 : : else
1562 : 4 : sm_ctxt.warn (node, stmt, NULL_TREE,
1563 : 4 : std::make_unique<fd_leak> (*this, NULL_TREE, nullptr));
1564 : 14 : }
1565 : :
1566 : : void
1567 : 30 : fd_state_machine::check_for_dup (sm_context &sm_ctxt, const supernode *node,
1568 : : const gimple *stmt, const gcall &call,
1569 : : const tree callee_fndecl, enum dup kind) const
1570 : : {
1571 : 30 : tree lhs = gimple_call_lhs (&call);
1572 : 30 : tree arg_1 = gimple_call_arg (&call, 0);
1573 : 30 : state_t state_arg_1 = sm_ctxt.get_state (stmt, arg_1);
1574 : 30 : if (state_arg_1 == m_stop)
1575 : : return;
1576 : 30 : if (!(is_constant_fd_p (state_arg_1) || is_valid_fd_p (state_arg_1)
1577 : 17 : || state_arg_1 == m_start))
1578 : : {
1579 : 7 : check_for_open_fd (sm_ctxt, node, stmt, call, callee_fndecl,
1580 : : DIRS_READ_WRITE);
1581 : 7 : return;
1582 : : }
1583 : 23 : switch (kind)
1584 : : {
1585 : 14 : case DUP_1:
1586 : 14 : if (lhs)
1587 : : {
1588 : 14 : if (is_constant_fd_p (state_arg_1) || state_arg_1 == m_start)
1589 : 8 : sm_ctxt.set_next_state (stmt, lhs, m_unchecked_read_write);
1590 : : else
1591 : 6 : sm_ctxt.set_next_state (stmt, lhs,
1592 : : valid_to_unchecked_state (state_arg_1));
1593 : : }
1594 : 21 : break;
1595 : :
1596 : 9 : case DUP_2:
1597 : 9 : case DUP_3:
1598 : 9 : tree arg_2 = gimple_call_arg (&call, 1);
1599 : 9 : state_t state_arg_2 = sm_ctxt.get_state (stmt, arg_2);
1600 : 9 : tree diag_arg_2 = sm_ctxt.get_diagnostic_tree (arg_2);
1601 : 9 : if (state_arg_2 == m_stop)
1602 : 2 : return;
1603 : : /* Check if -1 was passed as second argument to dup2. */
1604 : 9 : if (!(is_constant_fd_p (state_arg_2) || is_valid_fd_p (state_arg_2)
1605 : 2 : || state_arg_2 == m_start))
1606 : : {
1607 : 2 : sm_ctxt.warn (
1608 : : node, stmt, arg_2,
1609 : 2 : std::make_unique<fd_use_without_check> (*this, diag_arg_2,
1610 : : callee_fndecl));
1611 : 2 : return;
1612 : : }
1613 : : /* dup2 returns value of its second argument on success.But, the
1614 : : access mode of the returned file descriptor depends on the duplicated
1615 : : file descriptor i.e the first argument. */
1616 : 7 : if (lhs)
1617 : : {
1618 : 7 : if (is_constant_fd_p (state_arg_1) || state_arg_1 == m_start)
1619 : 4 : sm_ctxt.set_next_state (stmt, lhs, m_unchecked_read_write);
1620 : : else
1621 : 3 : sm_ctxt.set_next_state (stmt, lhs,
1622 : : valid_to_unchecked_state (state_arg_1));
1623 : : }
1624 : :
1625 : : break;
1626 : : }
1627 : : }
1628 : :
1629 : : void
1630 : 503 : fd_state_machine::on_close (sm_context &sm_ctxt, const supernode *node,
1631 : : const gimple *stmt, const gcall &call) const
1632 : : {
1633 : 503 : tree arg = gimple_call_arg (&call, 0);
1634 : 503 : state_t state = sm_ctxt.get_state (stmt, arg);
1635 : 503 : tree diag_arg = sm_ctxt.get_diagnostic_tree (arg);
1636 : :
1637 : 503 : sm_ctxt.on_transition (node, stmt, arg, m_start, m_closed);
1638 : 503 : sm_ctxt.on_transition (node, stmt, arg, m_unchecked_read_write, m_closed);
1639 : 503 : sm_ctxt.on_transition (node, stmt, arg, m_unchecked_read_only, m_closed);
1640 : 503 : sm_ctxt.on_transition (node, stmt, arg, m_unchecked_write_only, m_closed);
1641 : 503 : sm_ctxt.on_transition (node, stmt, arg, m_valid_read_write, m_closed);
1642 : 503 : sm_ctxt.on_transition (node, stmt, arg, m_valid_read_only, m_closed);
1643 : 503 : sm_ctxt.on_transition (node, stmt, arg, m_valid_write_only, m_closed);
1644 : 503 : sm_ctxt.on_transition (node, stmt, arg, m_constant_fd, m_closed);
1645 : 503 : sm_ctxt.on_transition (node, stmt, arg, m_new_datagram_socket, m_closed);
1646 : 503 : sm_ctxt.on_transition (node, stmt, arg, m_new_stream_socket, m_closed);
1647 : 503 : sm_ctxt.on_transition (node, stmt, arg, m_new_unknown_socket, m_closed);
1648 : 503 : sm_ctxt.on_transition (node, stmt, arg, m_bound_datagram_socket, m_closed);
1649 : 503 : sm_ctxt.on_transition (node, stmt, arg, m_bound_stream_socket, m_closed);
1650 : 503 : sm_ctxt.on_transition (node, stmt, arg, m_bound_unknown_socket, m_closed);
1651 : 503 : sm_ctxt.on_transition (node, stmt, arg, m_listening_stream_socket, m_closed);
1652 : 503 : sm_ctxt.on_transition (node, stmt, arg, m_connected_stream_socket, m_closed);
1653 : :
1654 : 503 : if (is_closed_fd_p (state))
1655 : : {
1656 : 28 : sm_ctxt.warn (node, stmt, arg,
1657 : 28 : std::make_unique<fd_double_close> (*this, diag_arg));
1658 : 28 : sm_ctxt.set_next_state (stmt, arg, m_stop);
1659 : : }
1660 : 503 : }
1661 : : void
1662 : 104 : fd_state_machine::on_read (sm_context &sm_ctxt, const supernode *node,
1663 : : const gimple *stmt, const gcall &call,
1664 : : const tree callee_fndecl) const
1665 : : {
1666 : 104 : check_for_open_fd (sm_ctxt, node, stmt, call, callee_fndecl, DIRS_READ);
1667 : 0 : }
1668 : : void
1669 : 82 : fd_state_machine::on_write (sm_context &sm_ctxt, const supernode *node,
1670 : : const gimple *stmt, const gcall &call,
1671 : : const tree callee_fndecl) const
1672 : : {
1673 : 82 : check_for_open_fd (sm_ctxt, node, stmt, call, callee_fndecl, DIRS_WRITE);
1674 : 0 : }
1675 : :
1676 : : void
1677 : 193 : fd_state_machine::check_for_open_fd (
1678 : : sm_context &sm_ctxt, const supernode *node, const gimple *stmt,
1679 : : const gcall &call, const tree callee_fndecl,
1680 : : enum access_directions callee_fndecl_dir) const
1681 : : {
1682 : 193 : tree arg = gimple_call_arg (&call, 0);
1683 : 193 : tree diag_arg = sm_ctxt.get_diagnostic_tree (arg);
1684 : 193 : state_t state = sm_ctxt.get_state (stmt, arg);
1685 : :
1686 : 193 : if (is_closed_fd_p (state))
1687 : : {
1688 : 29 : sm_ctxt.warn (node, stmt, arg,
1689 : 29 : std::make_unique<fd_use_after_close> (*this, diag_arg,
1690 : : callee_fndecl));
1691 : : }
1692 : :
1693 : : else
1694 : : {
1695 : 164 : if (state == m_new_stream_socket
1696 : 163 : || state == m_bound_stream_socket
1697 : 163 : || state == m_listening_stream_socket)
1698 : : /* Complain about fncall on socket in wrong phase. */
1699 : 4 : sm_ctxt.warn
1700 : 4 : (node, stmt, arg,
1701 : 4 : std::make_unique<fd_phase_mismatch> (*this, diag_arg,
1702 : : callee_fndecl,
1703 : : state,
1704 : 8 : EXPECTED_PHASE_CAN_TRANSFER));
1705 : 160 : else if (!(is_valid_fd_p (state)
1706 : 137 : || state == m_new_datagram_socket
1707 : 137 : || state == m_bound_unknown_socket
1708 : 135 : || state == m_connected_stream_socket
1709 : 112 : || state == m_start
1710 : 89 : || state == m_stop))
1711 : : {
1712 : 62 : if (!is_constant_fd_p (state))
1713 : 52 : sm_ctxt.warn (
1714 : : node, stmt, arg,
1715 : 52 : std::make_unique<fd_use_without_check> (*this, diag_arg,
1716 : : callee_fndecl));
1717 : : }
1718 : 164 : switch (callee_fndecl_dir)
1719 : : {
1720 : : case DIRS_READ_WRITE:
1721 : : break;
1722 : 91 : case DIRS_READ:
1723 : 91 : if (is_writeonly_fd_p (state))
1724 : : {
1725 : 4 : tree diag_arg = sm_ctxt.get_diagnostic_tree (arg);
1726 : 4 : sm_ctxt.warn (node, stmt, arg,
1727 : : std::make_unique<fd_access_mode_mismatch>
1728 : 4 : (*this, diag_arg, DIRS_WRITE, callee_fndecl));
1729 : : }
1730 : :
1731 : : break;
1732 : 69 : case DIRS_WRITE:
1733 : :
1734 : 69 : if (is_readonly_fd_p (state))
1735 : : {
1736 : 2 : tree diag_arg = sm_ctxt.get_diagnostic_tree (arg);
1737 : 2 : sm_ctxt.warn (node, stmt, arg,
1738 : : std::make_unique<fd_access_mode_mismatch>
1739 : 2 : (*this, diag_arg, DIRS_READ, callee_fndecl));
1740 : : }
1741 : : break;
1742 : : }
1743 : : }
1744 : 193 : }
1745 : :
1746 : : static bool
1747 : 333 : add_constraint_ge_zero (region_model *model,
1748 : : const svalue *fd_sval,
1749 : : region_model_context *ctxt)
1750 : : {
1751 : 333 : const svalue *zero
1752 : 333 : = model->get_manager ()->get_or_create_int_cst (integer_type_node, 0);
1753 : 333 : return model->add_constraint (fd_sval, GE_EXPR, zero, ctxt);
1754 : : }
1755 : :
1756 : : /* Get the state for a new socket type based on SOCKET_TYPE_SVAL,
1757 : : a SOCK_* value. */
1758 : :
1759 : : state_machine::state_t
1760 : 101 : fd_state_machine::
1761 : : get_state_for_socket_type (const svalue *socket_type_sval) const
1762 : : {
1763 : 101 : if (tree socket_type_cst = socket_type_sval->maybe_get_constant ())
1764 : : {
1765 : : /* Attempt to use SOCK_* constants stashed from the frontend. */
1766 : 45 : if (tree_int_cst_equal (socket_type_cst, m_SOCK_STREAM))
1767 : 20 : return m_new_stream_socket;
1768 : 25 : if (tree_int_cst_equal (socket_type_cst, m_SOCK_DGRAM))
1769 : 16 : return m_new_datagram_socket;
1770 : : }
1771 : :
1772 : : /* Unrecognized constant, or a symbolic "type" value. */
1773 : 65 : return m_new_unknown_socket;
1774 : : }
1775 : :
1776 : : /* Update the model and fd state for an outcome of a call to "socket",
1777 : : where SUCCESSFUL indicate which of the two outcomes.
1778 : : Return true if the outcome is feasible, or false to reject it. */
1779 : :
1780 : : bool
1781 : 214 : fd_state_machine::on_socket (const call_details &cd,
1782 : : bool successful,
1783 : : sm_context &sm_ctxt,
1784 : : const extrinsic_state &ext_state) const
1785 : : {
1786 : 214 : const gcall &call = cd.get_call_stmt ();
1787 : 214 : engine *eng = ext_state.get_engine ();
1788 : 214 : const supergraph *sg = eng->get_supergraph ();
1789 : 214 : const supernode *node = sg->get_supernode_for_stmt (&call);
1790 : 214 : region_model *model = cd.get_model ();
1791 : :
1792 : 214 : if (successful)
1793 : : {
1794 : 107 : if (gimple_call_lhs (&call))
1795 : : {
1796 : 101 : conjured_purge p (model, cd.get_ctxt ());
1797 : 101 : region_model_manager *mgr = model->get_manager ();
1798 : 101 : const svalue *new_fd
1799 : 101 : = mgr->get_or_create_conjured_svalue (integer_type_node,
1800 : : &call,
1801 : : cd.get_lhs_region (),
1802 : : p);
1803 : 101 : if (!add_constraint_ge_zero (model, new_fd, cd.get_ctxt ()))
1804 : 0 : return false;
1805 : :
1806 : 101 : const svalue *socket_type_sval = cd.get_arg_svalue (1);
1807 : 101 : state_machine::state_t new_state
1808 : 101 : = get_state_for_socket_type (socket_type_sval);
1809 : 101 : sm_ctxt.on_transition (node, &call, new_fd, m_start, new_state);
1810 : 101 : model->set_value (cd.get_lhs_region (), new_fd, cd.get_ctxt ());
1811 : : }
1812 : : else
1813 : 6 : sm_ctxt.warn (node, &call, NULL_TREE,
1814 : 6 : std::make_unique<fd_leak> (*this, NULL_TREE, nullptr));
1815 : : }
1816 : : else
1817 : : {
1818 : : /* Return -1; set errno. */
1819 : 107 : model->update_for_int_cst_return (cd, -1, true);
1820 : 107 : model->set_errno (cd);
1821 : : }
1822 : :
1823 : : return true;
1824 : : }
1825 : :
1826 : : /* Check that FD_SVAL is usable by socket APIs.
1827 : : Complain if it has been closed, if it is a non-socket,
1828 : : or is invalid.
1829 : : If COMPLAINED is non-NULL and a problem is found,
1830 : : write *COMPLAINED = true.
1831 : :
1832 : : If SUCCESSFUL is true, attempt to add the constraint that FD_SVAL >= 0.
1833 : : Return true if this outcome is feasible. */
1834 : :
1835 : : bool
1836 : 448 : fd_state_machine::check_for_socket_fd (const call_details &cd,
1837 : : bool successful,
1838 : : sm_context &sm_ctxt,
1839 : : const svalue *fd_sval,
1840 : : const supernode *node,
1841 : : state_t old_state,
1842 : : bool *complained) const
1843 : : {
1844 : 448 : const gcall &call = cd.get_call_stmt ();
1845 : :
1846 : 448 : if (is_closed_fd_p (old_state))
1847 : : {
1848 : 0 : tree diag_arg = sm_ctxt.get_diagnostic_tree (fd_sval);
1849 : 0 : sm_ctxt.warn
1850 : 0 : (node, &call, fd_sval,
1851 : 0 : std::make_unique<fd_use_after_close> (*this, diag_arg,
1852 : 0 : cd.get_fndecl_for_call ()));
1853 : 0 : if (complained)
1854 : 0 : *complained = true;
1855 : 0 : if (successful)
1856 : 0 : return false;
1857 : : }
1858 : 896 : else if (is_unchecked_fd_p (old_state) || is_valid_fd_p (old_state))
1859 : : {
1860 : : /* Complain about non-socket. */
1861 : 4 : tree diag_arg = sm_ctxt.get_diagnostic_tree (fd_sval);
1862 : 4 : sm_ctxt.warn
1863 : 4 : (node, &call, fd_sval,
1864 : 4 : std::make_unique<fd_type_mismatch> (*this, diag_arg,
1865 : 4 : cd.get_fndecl_for_call (),
1866 : : old_state,
1867 : 4 : EXPECTED_TYPE_SOCKET));
1868 : 4 : if (complained)
1869 : 4 : *complained = true;
1870 : 4 : if (successful)
1871 : 2 : return false;
1872 : : }
1873 : 444 : else if (old_state == m_invalid)
1874 : : {
1875 : 26 : tree diag_arg = sm_ctxt.get_diagnostic_tree (fd_sval);
1876 : 26 : sm_ctxt.warn
1877 : 26 : (node, &call, fd_sval,
1878 : 26 : std::make_unique<fd_use_without_check> (*this, diag_arg,
1879 : 26 : cd.get_fndecl_for_call ()));
1880 : 26 : if (complained)
1881 : 24 : *complained = true;
1882 : 26 : if (successful)
1883 : 13 : return false;
1884 : : }
1885 : :
1886 : 433 : if (successful)
1887 : 209 : if (!add_constraint_ge_zero (cd.get_model (), fd_sval, cd.get_ctxt ()))
1888 : : return false;
1889 : :
1890 : : return true;
1891 : : }
1892 : :
1893 : : /* For use by "bind" and "connect".
1894 : : As per fd_state_machine::check_for_socket_fd above,
1895 : : but also complain if we don't have a new socket, and check that
1896 : : we can read up to the size bytes from the address. */
1897 : :
1898 : : bool
1899 : 286 : fd_state_machine::check_for_new_socket_fd (const call_details &cd,
1900 : : bool successful,
1901 : : sm_context &sm_ctxt,
1902 : : const svalue *fd_sval,
1903 : : const supernode *node,
1904 : : state_t old_state,
1905 : : enum expected_phase expected_phase)
1906 : : const
1907 : : {
1908 : 286 : bool complained = false;
1909 : :
1910 : : /* Check address and len. */
1911 : 286 : const svalue *address_sval = cd.get_arg_svalue (1);
1912 : 286 : const svalue *len_sval = cd.get_arg_svalue (2);
1913 : :
1914 : : /* Check that we can read the given number of bytes from the
1915 : : address. */
1916 : 286 : region_model *model = cd.get_model ();
1917 : 286 : const region *address_reg
1918 : 286 : = model->deref_rvalue (address_sval, cd.get_arg_tree (1),
1919 : : cd.get_ctxt ());
1920 : 286 : const region *sized_address_reg
1921 : 286 : = model->get_manager ()->get_sized_region (address_reg,
1922 : : NULL_TREE,
1923 : : len_sval);
1924 : 286 : model->get_store_value (sized_address_reg, cd.get_ctxt ());
1925 : :
1926 : 286 : if (!check_for_socket_fd (cd, successful, sm_ctxt,
1927 : : fd_sval, node, old_state, &complained))
1928 : : return false;
1929 : 272 : else if (!complained
1930 : 258 : && !(old_state == m_new_stream_socket
1931 : 226 : || old_state == m_new_datagram_socket
1932 : 198 : || old_state == m_new_unknown_socket
1933 : 92 : || old_state == m_start
1934 : 50 : || old_state == m_stop
1935 : 46 : || old_state == m_constant_fd))
1936 : : {
1937 : : /* Complain about "bind" or "connect" in wrong phase. */
1938 : 18 : tree diag_arg = sm_ctxt.get_diagnostic_tree (fd_sval);
1939 : 18 : sm_ctxt.warn
1940 : 18 : (node, &cd.get_call_stmt (), fd_sval,
1941 : 18 : std::make_unique<fd_phase_mismatch> (*this, diag_arg,
1942 : 18 : cd.get_fndecl_for_call (),
1943 : : old_state,
1944 : : expected_phase));
1945 : 18 : if (successful)
1946 : 9 : return false;
1947 : : }
1948 : 254 : else if (!successful)
1949 : : {
1950 : : /* If we were in the start state, assume we had a new socket. */
1951 : 134 : if (old_state == m_start)
1952 : 21 : sm_ctxt.set_next_state (&cd.get_call_stmt (), fd_sval,
1953 : 21 : m_new_unknown_socket);
1954 : : }
1955 : :
1956 : : /* Passing NULL as the address will lead to failure. */
1957 : 9 : if (successful)
1958 : 120 : if (address_sval->all_zeroes_p ())
1959 : : return false;
1960 : :
1961 : : return true;
1962 : : }
1963 : :
1964 : : /* Update the model and fd state for an outcome of a call to "bind",
1965 : : where SUCCESSFUL indicate which of the two outcomes.
1966 : : Return true if the outcome is feasible, or false to reject it. */
1967 : :
1968 : : bool
1969 : 230 : fd_state_machine::on_bind (const call_details &cd,
1970 : : bool successful,
1971 : : sm_context &sm_ctxt,
1972 : : const extrinsic_state &ext_state) const
1973 : : {
1974 : 230 : const gcall &call = cd.get_call_stmt ();
1975 : 230 : engine *eng = ext_state.get_engine ();
1976 : 230 : const supergraph *sg = eng->get_supergraph ();
1977 : 230 : const supernode *node = sg->get_supernode_for_stmt (&call);
1978 : 230 : const svalue *fd_sval = cd.get_arg_svalue (0);
1979 : 230 : region_model *model = cd.get_model ();
1980 : 230 : state_t old_state = sm_ctxt.get_state (&call, fd_sval);
1981 : :
1982 : 230 : if (!check_for_new_socket_fd (cd, successful, sm_ctxt,
1983 : : fd_sval, node, old_state,
1984 : : EXPECTED_PHASE_CAN_BIND))
1985 : : return false;
1986 : :
1987 : 206 : if (successful)
1988 : : {
1989 : 91 : state_t next_state = nullptr;
1990 : 91 : if (old_state == m_new_stream_socket)
1991 : 13 : next_state = m_bound_stream_socket;
1992 : 78 : else if (old_state == m_new_datagram_socket)
1993 : 14 : next_state = m_bound_datagram_socket;
1994 : 64 : else if (old_state == m_new_unknown_socket)
1995 : 42 : next_state = m_bound_unknown_socket;
1996 : 22 : else if (old_state == m_start
1997 : 8 : || old_state == m_constant_fd)
1998 : 20 : next_state = m_bound_unknown_socket;
1999 : 2 : else if (old_state == m_stop)
2000 : : next_state = m_stop;
2001 : : else
2002 : 0 : gcc_unreachable ();
2003 : 91 : sm_ctxt.set_next_state (&cd.get_call_stmt (), fd_sval, next_state);
2004 : 91 : model->update_for_zero_return (cd, true);
2005 : : }
2006 : : else
2007 : : {
2008 : : /* Return -1; set errno. */
2009 : 115 : model->update_for_int_cst_return (cd, -1, true);
2010 : 115 : model->set_errno (cd);
2011 : : }
2012 : :
2013 : : return true;
2014 : : }
2015 : :
2016 : : /* Update the model and fd state for an outcome of a call to "listen",
2017 : : where SUCCESSFUL indicate which of the two outcomes.
2018 : : Return true if the outcome is feasible, or false to reject it. */
2019 : :
2020 : : bool
2021 : 108 : fd_state_machine::on_listen (const call_details &cd,
2022 : : bool successful,
2023 : : sm_context &sm_ctxt,
2024 : : const extrinsic_state &ext_state) const
2025 : : {
2026 : 108 : const gcall &call = cd.get_call_stmt ();
2027 : 108 : engine *eng = ext_state.get_engine ();
2028 : 108 : const supergraph *sg = eng->get_supergraph ();
2029 : 108 : const supernode *node = sg->get_supernode_for_stmt (&cd.get_call_stmt ());
2030 : 108 : const svalue *fd_sval = cd.get_arg_svalue (0);
2031 : 108 : region_model *model = cd.get_model ();
2032 : 108 : state_t old_state = sm_ctxt.get_state (&call, fd_sval);
2033 : :
2034 : : /* We expect a stream socket that's had "bind" called on it. */
2035 : 108 : if (!check_for_socket_fd (cd, successful, sm_ctxt, fd_sval, node, old_state))
2036 : : return false;
2037 : 107 : if (!(old_state == m_start
2038 : 93 : || old_state == m_constant_fd
2039 : 89 : || old_state == m_stop
2040 : 89 : || old_state == m_invalid
2041 : 88 : || old_state == m_bound_stream_socket
2042 : 76 : || old_state == m_bound_unknown_socket
2043 : : /* Assume it's OK to call "listen" more than once. */
2044 : 48 : || old_state == m_listening_stream_socket))
2045 : : {
2046 : : /* Complain about fncall on wrong type or in wrong phase. */
2047 : 46 : tree diag_arg = sm_ctxt.get_diagnostic_tree (fd_sval);
2048 : 46 : if (is_stream_socket_fd_p (old_state))
2049 : 38 : sm_ctxt.warn
2050 : 38 : (node, &call, fd_sval,
2051 : 38 : std::make_unique<fd_phase_mismatch> (*this, diag_arg,
2052 : 38 : cd.get_fndecl_for_call (),
2053 : : old_state,
2054 : 76 : EXPECTED_PHASE_CAN_LISTEN));
2055 : : else
2056 : 8 : sm_ctxt.warn
2057 : 8 : (node, &call, fd_sval,
2058 : 8 : std::make_unique<fd_type_mismatch> (*this, diag_arg,
2059 : 8 : cd.get_fndecl_for_call (),
2060 : : old_state,
2061 : 16 : EXPECTED_TYPE_STREAM_SOCKET));
2062 : 46 : if (successful)
2063 : 23 : return false;
2064 : : }
2065 : :
2066 : 84 : if (successful)
2067 : : {
2068 : 30 : model->update_for_zero_return (cd, true);
2069 : 30 : sm_ctxt.set_next_state (&cd.get_call_stmt (), fd_sval,
2070 : 30 : m_listening_stream_socket);
2071 : : }
2072 : : else
2073 : : {
2074 : : /* Return -1; set errno. */
2075 : 54 : model->update_for_int_cst_return (cd, -1, true);
2076 : 54 : model->set_errno (cd);
2077 : 54 : if (old_state == m_start)
2078 : 7 : sm_ctxt.set_next_state (&cd.get_call_stmt (), fd_sval,
2079 : 7 : m_bound_stream_socket);
2080 : : }
2081 : :
2082 : : return true;
2083 : : }
2084 : :
2085 : : /* Update the model and fd state for an outcome of a call to "accept",
2086 : : where SUCCESSFUL indicate which of the two outcomes.
2087 : : Return true if the outcome is feasible, or false to reject it. */
2088 : :
2089 : : bool
2090 : 54 : fd_state_machine::on_accept (const call_details &cd,
2091 : : bool successful,
2092 : : sm_context &sm_ctxt,
2093 : : const extrinsic_state &ext_state) const
2094 : : {
2095 : 54 : const gcall &call = cd.get_call_stmt ();
2096 : 54 : engine *eng = ext_state.get_engine ();
2097 : 54 : const supergraph *sg = eng->get_supergraph ();
2098 : 54 : const supernode *node = sg->get_supernode_for_stmt (&call);
2099 : 54 : const svalue *fd_sval = cd.get_arg_svalue (0);
2100 : 54 : const svalue *address_sval = cd.get_arg_svalue (1);
2101 : 54 : const svalue *len_ptr_sval = cd.get_arg_svalue (2);
2102 : 54 : region_model *model = cd.get_model ();
2103 : 54 : state_t old_state = sm_ctxt.get_state (&call, fd_sval);
2104 : :
2105 : 54 : if (!address_sval->all_zeroes_p ())
2106 : : {
2107 : 18 : region_model_manager *mgr = model->get_manager ();
2108 : :
2109 : : /* We might have a union of various pointer types, rather than a
2110 : : pointer type; cast to (void *) before dereferencing. */
2111 : 18 : address_sval = mgr->get_or_create_cast (ptr_type_node, address_sval);
2112 : :
2113 : 18 : const region *address_reg
2114 : 18 : = model->deref_rvalue (address_sval, cd.get_arg_tree (1),
2115 : : cd.get_ctxt ());
2116 : 18 : const region *len_reg
2117 : 18 : = model->deref_rvalue (len_ptr_sval, cd.get_arg_tree (2),
2118 : : cd.get_ctxt ());
2119 : 18 : const svalue *old_len_sval
2120 : 18 : = model->get_store_value (len_reg, cd.get_ctxt ());
2121 : 18 : tree len_ptr = cd.get_arg_tree (2);
2122 : 18 : tree star_len_ptr = build2 (MEM_REF, TREE_TYPE (TREE_TYPE (len_ptr)),
2123 : : len_ptr,
2124 : 18 : build_int_cst (TREE_TYPE (len_ptr), 0));
2125 : 18 : old_len_sval = model->check_for_poison (old_len_sval,
2126 : : star_len_ptr,
2127 : : len_reg,
2128 : : cd.get_ctxt ());
2129 : 18 : if (successful)
2130 : : {
2131 : 9 : conjured_purge p (model, cd.get_ctxt ());
2132 : 9 : const region *old_sized_address_reg
2133 : 9 : = mgr->get_sized_region (address_reg,
2134 : : NULL_TREE,
2135 : : old_len_sval);
2136 : 9 : const svalue *new_addr_sval
2137 : 9 : = mgr->get_or_create_conjured_svalue (NULL_TREE,
2138 : : &call,
2139 : : old_sized_address_reg,
2140 : : p);
2141 : 9 : model->set_value (old_sized_address_reg, new_addr_sval,
2142 : : cd.get_ctxt ());
2143 : 9 : const svalue *new_addr_len
2144 : 9 : = mgr->get_or_create_conjured_svalue (NULL_TREE,
2145 : : &call,
2146 : : len_reg,
2147 : : p);
2148 : 9 : model->set_value (len_reg, new_addr_len, cd.get_ctxt ());
2149 : : }
2150 : : }
2151 : :
2152 : : /* We expect a stream socket in the "listening" state. */
2153 : 54 : if (!check_for_socket_fd (cd, successful, sm_ctxt, fd_sval, node, old_state))
2154 : : return false;
2155 : :
2156 : 54 : if (old_state == m_start || old_state == m_constant_fd)
2157 : : /* If we were in the start state (or a constant), assume we had the
2158 : : expected state. */
2159 : 24 : sm_ctxt.set_next_state (&cd.get_call_stmt (), fd_sval,
2160 : 24 : m_listening_stream_socket);
2161 : 30 : else if (old_state == m_stop)
2162 : : {
2163 : : /* No further complaints. */
2164 : : }
2165 : 30 : else if (old_state != m_listening_stream_socket)
2166 : : {
2167 : : /* Complain about fncall on wrong type or in wrong phase. */
2168 : 6 : tree diag_arg = sm_ctxt.get_diagnostic_tree (fd_sval);
2169 : 6 : if (is_stream_socket_fd_p (old_state))
2170 : 4 : sm_ctxt.warn
2171 : 4 : (node, &call, fd_sval,
2172 : 4 : std::make_unique<fd_phase_mismatch> (*this, diag_arg,
2173 : 4 : cd.get_fndecl_for_call (),
2174 : : old_state,
2175 : 8 : EXPECTED_PHASE_CAN_ACCEPT));
2176 : : else
2177 : 2 : sm_ctxt.warn
2178 : 2 : (node, &call, fd_sval,
2179 : 2 : std::make_unique<fd_type_mismatch> (*this, diag_arg,
2180 : 2 : cd.get_fndecl_for_call (),
2181 : : old_state,
2182 : 4 : EXPECTED_TYPE_STREAM_SOCKET));
2183 : 6 : if (successful)
2184 : 3 : return false;
2185 : : }
2186 : :
2187 : 51 : if (successful)
2188 : : {
2189 : : /* Return new conjured FD in "connected" state. */
2190 : 24 : if (gimple_call_lhs (&call))
2191 : : {
2192 : 23 : conjured_purge p (model, cd.get_ctxt ());
2193 : 23 : region_model_manager *mgr = model->get_manager ();
2194 : 23 : const svalue *new_fd
2195 : 23 : = mgr->get_or_create_conjured_svalue (integer_type_node,
2196 : : &call,
2197 : : cd.get_lhs_region (),
2198 : : p);
2199 : 23 : if (!add_constraint_ge_zero (model, new_fd, cd.get_ctxt ()))
2200 : 0 : return false;
2201 : 23 : sm_ctxt.on_transition (node, &call, new_fd,
2202 : 23 : m_start, m_connected_stream_socket);
2203 : 23 : model->set_value (cd.get_lhs_region (), new_fd, cd.get_ctxt ());
2204 : : }
2205 : : else
2206 : 1 : sm_ctxt.warn (node, &call, NULL_TREE,
2207 : 1 : std::make_unique<fd_leak> (*this, NULL_TREE, nullptr));
2208 : : }
2209 : : else
2210 : : {
2211 : : /* Return -1; set errno. */
2212 : 27 : model->update_for_int_cst_return (cd, -1, true);
2213 : 27 : model->set_errno (cd);
2214 : : }
2215 : :
2216 : : return true;
2217 : : }
2218 : :
2219 : : /* Update the model and fd state for an outcome of a call to "connect",
2220 : : where SUCCESSFUL indicate which of the two outcomes.
2221 : : Return true if the outcome is feasible, or false to reject it. */
2222 : :
2223 : : bool
2224 : 56 : fd_state_machine::on_connect (const call_details &cd,
2225 : : bool successful,
2226 : : sm_context &sm_ctxt,
2227 : : const extrinsic_state &ext_state) const
2228 : : {
2229 : 56 : const gcall &call = cd.get_call_stmt ();
2230 : 56 : engine *eng = ext_state.get_engine ();
2231 : 56 : const supergraph *sg = eng->get_supergraph ();
2232 : 56 : const supernode *node = sg->get_supernode_for_stmt (&call);
2233 : 56 : const svalue *fd_sval = cd.get_arg_svalue (0);
2234 : 56 : region_model *model = cd.get_model ();
2235 : 56 : state_t old_state = sm_ctxt.get_state (&call, fd_sval);
2236 : :
2237 : 56 : if (!check_for_new_socket_fd (cd, successful, sm_ctxt,
2238 : : fd_sval, node, old_state,
2239 : : EXPECTED_PHASE_CAN_CONNECT))
2240 : : return false;
2241 : :
2242 : 53 : if (successful)
2243 : : {
2244 : 25 : model->update_for_zero_return (cd, true);
2245 : 25 : state_t next_state = nullptr;
2246 : 25 : if (old_state == m_new_stream_socket)
2247 : 3 : next_state = m_connected_stream_socket;
2248 : 22 : else if (old_state == m_new_datagram_socket)
2249 : : /* It's legal to call connect on a datagram socket, potentially
2250 : : more than once. We don't transition states for this. */
2251 : : next_state = m_new_datagram_socket;
2252 : 22 : else if (old_state == m_new_unknown_socket)
2253 : 11 : next_state = m_stop;
2254 : 11 : else if (old_state == m_start
2255 : 6 : || old_state == m_constant_fd)
2256 : 11 : next_state = m_stop;
2257 : 0 : else if (old_state == m_stop)
2258 : : next_state = m_stop;
2259 : : else
2260 : 0 : gcc_unreachable ();
2261 : 25 : sm_ctxt.set_next_state (&cd.get_call_stmt (), fd_sval, next_state);
2262 : : }
2263 : : else
2264 : : {
2265 : : /* Return -1; set errno. */
2266 : 28 : model->update_for_int_cst_return (cd, -1, true);
2267 : 28 : model->set_errno (cd);
2268 : : /* TODO: perhaps transition to a failed state, since the
2269 : : portable way to handle a failed "connect" is to close
2270 : : the socket and try again with a new socket. */
2271 : : }
2272 : :
2273 : : return true;
2274 : : }
2275 : :
2276 : : void
2277 : 30254 : fd_state_machine::on_condition (sm_context &sm_ctxt, const supernode *node,
2278 : : const gimple *stmt, const svalue *lhs,
2279 : : enum tree_code op, const svalue *rhs) const
2280 : : {
2281 : 30254 : if (tree cst = rhs->maybe_get_constant ())
2282 : : {
2283 : 24582 : if (TREE_CODE (cst) == INTEGER_CST)
2284 : : {
2285 : 24574 : int val = TREE_INT_CST_LOW (cst);
2286 : 24574 : if (val == -1)
2287 : : {
2288 : 626 : if (op == NE_EXPR)
2289 : 289 : make_valid_transitions_on_condition (sm_ctxt, node, stmt, lhs);
2290 : :
2291 : 337 : else if (op == EQ_EXPR)
2292 : 289 : make_invalid_transitions_on_condition (sm_ctxt, node, stmt,
2293 : : lhs);
2294 : : }
2295 : : }
2296 : : }
2297 : :
2298 : 30254 : if (rhs->all_zeroes_p ())
2299 : : {
2300 : 19646 : if (op == GE_EXPR)
2301 : 656 : make_valid_transitions_on_condition (sm_ctxt, node, stmt, lhs);
2302 : 18990 : else if (op == LT_EXPR)
2303 : 481 : make_invalid_transitions_on_condition (sm_ctxt, node, stmt, lhs);
2304 : : }
2305 : 30254 : }
2306 : :
2307 : : void
2308 : 945 : fd_state_machine::make_valid_transitions_on_condition (sm_context &sm_ctxt,
2309 : : const supernode *node,
2310 : : const gimple *stmt,
2311 : : const svalue *lhs) const
2312 : : {
2313 : 945 : sm_ctxt.on_transition (node, stmt, lhs, m_unchecked_read_write,
2314 : 945 : m_valid_read_write);
2315 : 945 : sm_ctxt.on_transition (node, stmt, lhs, m_unchecked_read_only,
2316 : 945 : m_valid_read_only);
2317 : 945 : sm_ctxt.on_transition (node, stmt, lhs, m_unchecked_write_only,
2318 : 945 : m_valid_write_only);
2319 : 945 : }
2320 : :
2321 : : void
2322 : 770 : fd_state_machine::make_invalid_transitions_on_condition (
2323 : : sm_context &sm_ctxt, const supernode *node, const gimple *stmt,
2324 : : const svalue *lhs) const
2325 : : {
2326 : 770 : sm_ctxt.on_transition (node, stmt, lhs, m_unchecked_read_write, m_invalid);
2327 : 770 : sm_ctxt.on_transition (node, stmt, lhs, m_unchecked_read_only, m_invalid);
2328 : 770 : sm_ctxt.on_transition (node, stmt, lhs, m_unchecked_write_only, m_invalid);
2329 : 770 : }
2330 : :
2331 : : bool
2332 : 1050406 : fd_state_machine::can_purge_p (state_t s) const
2333 : : {
2334 : 2100641 : if (is_unchecked_fd_p (s)
2335 : 1051404 : || is_valid_fd_p (s)
2336 : 1050161 : || is_socket_fd_p (s))
2337 : 1243 : return false;
2338 : : else
2339 : : return true;
2340 : : }
2341 : :
2342 : : std::unique_ptr<pending_diagnostic>
2343 : 116 : fd_state_machine::on_leak (tree var,
2344 : : const program_state *,
2345 : : const program_state *new_state) const
2346 : : {
2347 : 116 : return std::make_unique<fd_leak> (*this, var, new_state);
2348 : : }
2349 : : } // namespace
2350 : :
2351 : : std::unique_ptr<state_machine>
2352 : 3310 : make_fd_state_machine (logger *logger)
2353 : : {
2354 : 3310 : return std::make_unique<fd_state_machine> (logger);
2355 : : }
2356 : :
2357 : : static bool
2358 : 2488 : get_fd_state (region_model_context *ctxt,
2359 : : sm_state_map **out_smap,
2360 : : const fd_state_machine **out_sm,
2361 : : unsigned *out_sm_idx,
2362 : : std::unique_ptr<sm_context> *out_sm_context)
2363 : : {
2364 : 2488 : if (!ctxt)
2365 : : return false;
2366 : :
2367 : 796 : const state_machine *sm;
2368 : 796 : if (!ctxt->get_fd_map (out_smap, &sm, out_sm_idx, out_sm_context))
2369 : : return false;
2370 : :
2371 : 796 : gcc_assert (sm);
2372 : :
2373 : 796 : *out_sm = (const fd_state_machine *)sm;
2374 : 796 : return true;
2375 : : }
2376 : :
2377 : : /* Specialcase hook for handling pipe, for use by
2378 : : kf_pipe::success::update_model. */
2379 : :
2380 : : void
2381 : 34 : region_model::mark_as_valid_fd (const svalue *sval, region_model_context *ctxt)
2382 : : {
2383 : 34 : sm_state_map *smap;
2384 : 34 : const fd_state_machine *fd_sm;
2385 : 34 : if (!get_fd_state (ctxt, &smap, &fd_sm, nullptr, nullptr))
2386 : 16 : return;
2387 : 18 : const extrinsic_state *ext_state = ctxt->get_ext_state ();
2388 : 18 : if (!ext_state)
2389 : : return;
2390 : 18 : fd_sm->mark_as_valid_fd (this, smap, sval, *ext_state);
2391 : : }
2392 : :
2393 : : /* Handle calls to "socket".
2394 : : See e.g. https://man7.org/linux/man-pages/man3/socket.3p.html */
2395 : :
2396 : 3310 : class kf_socket : public known_function
2397 : : {
2398 : : public:
2399 : : class outcome_of_socket : public succeed_or_fail_call_info
2400 : : {
2401 : : public:
2402 : 250 : outcome_of_socket (const call_details &cd, bool success)
2403 : 250 : : succeed_or_fail_call_info (cd, success)
2404 : : {}
2405 : :
2406 : 1415 : bool update_model (region_model *model,
2407 : : const exploded_edge *,
2408 : : region_model_context *ctxt) const final override
2409 : : {
2410 : 1415 : const call_details cd (get_call_details (model, ctxt));
2411 : 1415 : sm_state_map *smap;
2412 : 1415 : const fd_state_machine *fd_sm;
2413 : 1415 : std::unique_ptr<sm_context> sm_ctxt;
2414 : 1415 : if (!get_fd_state (ctxt, &smap, &fd_sm, nullptr, &sm_ctxt))
2415 : : {
2416 : 1201 : cd.set_any_lhs_with_defaults ();
2417 : 1201 : return true;
2418 : : }
2419 : 214 : const extrinsic_state *ext_state = ctxt->get_ext_state ();
2420 : 214 : if (!ext_state)
2421 : : {
2422 : 0 : cd.set_any_lhs_with_defaults ();
2423 : 0 : return true;
2424 : : }
2425 : :
2426 : 214 : return fd_sm->on_socket (cd, m_success, *(sm_ctxt.get ()), *ext_state);
2427 : 1415 : }
2428 : : };
2429 : :
2430 : 3152 : bool matches_call_types_p (const call_details &cd) const final override
2431 : : {
2432 : 3152 : return cd.num_args () == 3;
2433 : : }
2434 : :
2435 : 1326 : void impl_call_post (const call_details &cd) const final override
2436 : : {
2437 : 1326 : if (cd.get_ctxt ())
2438 : : {
2439 : 125 : cd.get_ctxt ()->bifurcate
2440 : 125 : (std::make_unique<outcome_of_socket> (cd, false));
2441 : 125 : cd.get_ctxt ()->bifurcate
2442 : 125 : (std::make_unique<outcome_of_socket> (cd, true));
2443 : 125 : cd.get_ctxt ()->terminate_path ();
2444 : : }
2445 : 1326 : }
2446 : : };
2447 : :
2448 : : /* Handle calls to "bind".
2449 : : See e.g. https://man7.org/linux/man-pages/man3/bind.3p.html */
2450 : :
2451 : 3310 : class kf_bind : public known_function
2452 : : {
2453 : : public:
2454 : : class outcome_of_bind : public succeed_or_fail_call_info
2455 : : {
2456 : : public:
2457 : 276 : outcome_of_bind (const call_details &cd, bool success)
2458 : 276 : : succeed_or_fail_call_info (cd, success)
2459 : : {}
2460 : :
2461 : 316 : bool update_model (region_model *model,
2462 : : const exploded_edge *,
2463 : : region_model_context *ctxt) const final override
2464 : : {
2465 : 316 : const call_details cd (get_call_details (model, ctxt));
2466 : 316 : sm_state_map *smap;
2467 : 316 : const fd_state_machine *fd_sm;
2468 : 316 : std::unique_ptr<sm_context> sm_ctxt;
2469 : 316 : if (!get_fd_state (ctxt, &smap, &fd_sm, nullptr, &sm_ctxt))
2470 : : {
2471 : 86 : cd.set_any_lhs_with_defaults ();
2472 : 86 : return true;
2473 : : }
2474 : 230 : const extrinsic_state *ext_state = ctxt->get_ext_state ();
2475 : 230 : if (!ext_state)
2476 : : {
2477 : 0 : cd.set_any_lhs_with_defaults ();
2478 : 0 : return true;
2479 : : }
2480 : 230 : return fd_sm->on_bind (cd, m_success, *sm_ctxt.get (), *ext_state);
2481 : 316 : }
2482 : : };
2483 : :
2484 : 1044 : bool matches_call_types_p (const call_details &cd) const final override
2485 : : {
2486 : 1044 : return (cd.num_args () == 3 && cd.arg_is_pointer_p (1));
2487 : : }
2488 : :
2489 : 224 : void impl_call_post (const call_details &cd) const final override
2490 : : {
2491 : 224 : if (cd.get_ctxt ())
2492 : : {
2493 : 138 : cd.get_ctxt ()->bifurcate
2494 : 138 : (std::make_unique<outcome_of_bind> (cd, false));
2495 : 138 : cd.get_ctxt ()->bifurcate
2496 : 138 : (std::make_unique<outcome_of_bind> (cd, true));
2497 : 138 : cd.get_ctxt ()->terminate_path ();
2498 : : }
2499 : 224 : }
2500 : : };
2501 : :
2502 : : /* Handle calls to "listen".
2503 : : See e.g. https://man7.org/linux/man-pages/man3/listen.3p.html */
2504 : :
2505 : 3310 : class kf_listen : public known_function
2506 : : {
2507 : : class outcome_of_listen : public succeed_or_fail_call_info
2508 : : {
2509 : : public:
2510 : 126 : outcome_of_listen (const call_details &cd, bool success)
2511 : 126 : : succeed_or_fail_call_info (cd, success)
2512 : : {}
2513 : :
2514 : 117 : bool update_model (region_model *model,
2515 : : const exploded_edge *,
2516 : : region_model_context *ctxt) const final override
2517 : : {
2518 : 117 : const call_details cd (get_call_details (model, ctxt));
2519 : 117 : sm_state_map *smap;
2520 : 117 : const fd_state_machine *fd_sm;
2521 : 117 : std::unique_ptr<sm_context> sm_ctxt;
2522 : 117 : if (!get_fd_state (ctxt, &smap, &fd_sm, nullptr, &sm_ctxt))
2523 : : {
2524 : 9 : cd.set_any_lhs_with_defaults ();
2525 : 9 : return true;
2526 : : }
2527 : 108 : const extrinsic_state *ext_state = ctxt->get_ext_state ();
2528 : 108 : if (!ext_state)
2529 : : {
2530 : 0 : cd.set_any_lhs_with_defaults ();
2531 : 0 : return true;
2532 : : }
2533 : :
2534 : 108 : return fd_sm->on_listen (cd, m_success, *sm_ctxt.get (), *ext_state);
2535 : 117 : }
2536 : : };
2537 : :
2538 : 396 : bool matches_call_types_p (const call_details &cd) const final override
2539 : : {
2540 : 396 : return cd.num_args () == 2;
2541 : : }
2542 : :
2543 : 72 : void impl_call_post (const call_details &cd) const final override
2544 : : {
2545 : 72 : if (cd.get_ctxt ())
2546 : : {
2547 : 63 : cd.get_ctxt ()->bifurcate
2548 : 63 : (std::make_unique<outcome_of_listen> (cd, false));
2549 : 63 : cd.get_ctxt ()->bifurcate
2550 : 63 : (std::make_unique<outcome_of_listen> (cd, true));
2551 : 63 : cd.get_ctxt ()->terminate_path ();
2552 : : }
2553 : 72 : }
2554 : : };
2555 : :
2556 : : /* Handle calls to "accept".
2557 : : See e.g. https://man7.org/linux/man-pages/man3/accept.3p.html */
2558 : :
2559 : 3310 : class kf_accept : public known_function
2560 : : {
2561 : : class outcome_of_accept : public succeed_or_fail_call_info
2562 : : {
2563 : : public:
2564 : 82 : outcome_of_accept (const call_details &cd, bool success)
2565 : 82 : : succeed_or_fail_call_info (cd, success)
2566 : : {}
2567 : :
2568 : 66 : bool update_model (region_model *model,
2569 : : const exploded_edge *,
2570 : : region_model_context *ctxt) const final override
2571 : : {
2572 : 66 : const call_details cd (get_call_details (model, ctxt));
2573 : 66 : sm_state_map *smap;
2574 : 66 : const fd_state_machine *fd_sm;
2575 : 66 : std::unique_ptr<sm_context> sm_ctxt;
2576 : 66 : if (!get_fd_state (ctxt, &smap, &fd_sm, nullptr, &sm_ctxt))
2577 : : {
2578 : 12 : cd.set_any_lhs_with_defaults ();
2579 : 12 : return true;
2580 : : }
2581 : 54 : const extrinsic_state *ext_state = ctxt->get_ext_state ();
2582 : 54 : if (!ext_state)
2583 : : {
2584 : 0 : cd.set_any_lhs_with_defaults ();
2585 : 0 : return true;
2586 : : }
2587 : :
2588 : 54 : return fd_sm->on_accept (cd, m_success, *sm_ctxt.get (), *ext_state);
2589 : 66 : }
2590 : : };
2591 : :
2592 : 330 : bool matches_call_types_p (const call_details &cd) const final override
2593 : : {
2594 : 330 : return (cd.num_args () == 3
2595 : 330 : && cd.arg_is_pointer_p (1)
2596 : 600 : && cd.arg_is_pointer_p (2));
2597 : : }
2598 : :
2599 : 53 : void impl_call_post (const call_details &cd) const final override
2600 : : {
2601 : 53 : if (cd.get_ctxt ())
2602 : : {
2603 : 41 : cd.get_ctxt ()->bifurcate
2604 : 41 : (std::make_unique<outcome_of_accept> (cd, false));
2605 : 41 : cd.get_ctxt ()->bifurcate
2606 : 41 : (std::make_unique<outcome_of_accept> (cd, true));
2607 : 41 : cd.get_ctxt ()->terminate_path ();
2608 : : }
2609 : 53 : }
2610 : : };
2611 : :
2612 : : /* Handle calls to "connect".
2613 : : See e.g. https://man7.org/linux/man-pages/man3/connect.3p.html */
2614 : :
2615 : 3310 : class kf_connect : public known_function
2616 : : {
2617 : : public:
2618 : : class outcome_of_connect : public succeed_or_fail_call_info
2619 : : {
2620 : : public:
2621 : 94 : outcome_of_connect (const call_details &cd, bool success)
2622 : 94 : : succeed_or_fail_call_info (cd, success)
2623 : : {}
2624 : :
2625 : 416 : bool update_model (region_model *model,
2626 : : const exploded_edge *,
2627 : : region_model_context *ctxt) const final override
2628 : : {
2629 : 416 : const call_details cd (get_call_details (model, ctxt));
2630 : 416 : sm_state_map *smap;
2631 : 416 : const fd_state_machine *fd_sm;
2632 : 416 : std::unique_ptr<sm_context> sm_ctxt;
2633 : 416 : if (!get_fd_state (ctxt, &smap, &fd_sm, nullptr, &sm_ctxt))
2634 : : {
2635 : 360 : cd.set_any_lhs_with_defaults ();
2636 : 360 : return true;
2637 : : }
2638 : 56 : const extrinsic_state *ext_state = ctxt->get_ext_state ();
2639 : 56 : if (!ext_state)
2640 : : {
2641 : 0 : cd.set_any_lhs_with_defaults ();
2642 : 0 : return true;
2643 : : }
2644 : :
2645 : 56 : return fd_sm->on_connect (cd, m_success, *sm_ctxt.get (), *ext_state);
2646 : 416 : }
2647 : : };
2648 : :
2649 : 1002 : bool matches_call_types_p (const call_details &cd) const final override
2650 : : {
2651 : 1002 : return (cd.num_args () == 3
2652 : 1002 : && cd.arg_is_pointer_p (1));
2653 : : }
2654 : :
2655 : 407 : void impl_call_post (const call_details &cd) const final override
2656 : : {
2657 : 407 : if (cd.get_ctxt ())
2658 : : {
2659 : 47 : cd.get_ctxt ()->bifurcate
2660 : 47 : (std::make_unique<outcome_of_connect> (cd, false));
2661 : 47 : cd.get_ctxt ()->bifurcate
2662 : 47 : (std::make_unique<outcome_of_connect> (cd, true));
2663 : 47 : cd.get_ctxt ()->terminate_path ();
2664 : : }
2665 : 407 : }
2666 : : };
2667 : :
2668 : : /* Handler for "isatty"".
2669 : : See e.g. https://man7.org/linux/man-pages/man3/isatty.3.html */
2670 : :
2671 : 3310 : class kf_isatty : public known_function
2672 : : {
2673 : : class outcome_of_isatty : public succeed_or_fail_call_info
2674 : : {
2675 : : public:
2676 : 448 : outcome_of_isatty (const call_details &cd, bool success)
2677 : 448 : : succeed_or_fail_call_info (cd, success)
2678 : : {}
2679 : :
2680 : 248 : bool update_model (region_model *model,
2681 : : const exploded_edge *,
2682 : : region_model_context *ctxt) const final override
2683 : : {
2684 : 248 : const call_details cd (get_call_details (model, ctxt));
2685 : :
2686 : 248 : if (m_success)
2687 : : {
2688 : : /* Return 1. */
2689 : 124 : model->update_for_int_cst_return (cd, 1, true);
2690 : : }
2691 : : else
2692 : : {
2693 : : /* Return 0; set errno. */
2694 : 124 : model->update_for_int_cst_return (cd, 0, true);
2695 : 124 : model->set_errno (cd);
2696 : : }
2697 : :
2698 : 248 : return feasible_p (cd, ctxt);
2699 : : }
2700 : :
2701 : : private:
2702 : 248 : bool feasible_p (const call_details &cd,
2703 : : region_model_context *ctxt) const
2704 : : {
2705 : 248 : if (m_success)
2706 : : {
2707 : : /* Can't be "success" on a closed/invalid fd. */
2708 : 124 : sm_state_map *smap;
2709 : 124 : const fd_state_machine *fd_sm;
2710 : 124 : std::unique_ptr<sm_context> sm_ctxt;
2711 : 124 : if (!get_fd_state (ctxt, &smap, &fd_sm, nullptr, &sm_ctxt))
2712 : : return true;
2713 : 116 : const extrinsic_state *ext_state = ctxt->get_ext_state ();
2714 : 116 : if (!ext_state)
2715 : : return true;
2716 : :
2717 : 116 : const svalue *fd_sval = cd.get_arg_svalue (0);
2718 : 116 : state_machine::state_t old_state
2719 : 116 : = sm_ctxt->get_state (&cd.get_call_stmt (), fd_sval);
2720 : :
2721 : 116 : if (fd_sm->is_closed_fd_p (old_state)
2722 : 116 : || old_state == fd_sm->m_invalid)
2723 : : return false;
2724 : 124 : }
2725 : : return true;
2726 : : }
2727 : : }; // class outcome_of_isatty
2728 : :
2729 : : public:
2730 : 1376 : bool matches_call_types_p (const call_details &cd) const final override
2731 : : {
2732 : 1376 : return cd.num_args () == 1;
2733 : : }
2734 : :
2735 : 240 : void impl_call_post (const call_details &cd) const final override
2736 : : {
2737 : 240 : if (cd.get_ctxt ())
2738 : : {
2739 : 224 : cd.get_ctxt ()->bifurcate
2740 : 224 : (std::make_unique<outcome_of_isatty> (cd, false));
2741 : 224 : cd.get_ctxt ()->bifurcate
2742 : 224 : (std::make_unique<outcome_of_isatty> (cd, true));
2743 : 224 : cd.get_ctxt ()->terminate_path ();
2744 : : }
2745 : 240 : }
2746 : : };
2747 : :
2748 : : /* Handler for calls to "pipe" and "pipe2".
2749 : : See e.g. https://www.man7.org/linux/man-pages/man2/pipe.2.html */
2750 : :
2751 : : class kf_pipe : public known_function
2752 : : {
2753 : : class failure : public failed_call_info
2754 : : {
2755 : : public:
2756 : 9 : failure (const call_details &cd) : failed_call_info (cd) {}
2757 : :
2758 : 21 : bool update_model (region_model *model,
2759 : : const exploded_edge *,
2760 : : region_model_context *ctxt) const final override
2761 : : {
2762 : : /* Return -1; everything else is unchanged. */
2763 : 21 : const call_details cd (get_call_details (model, ctxt));
2764 : 21 : model->update_for_int_cst_return (cd, -1, true);
2765 : 21 : return true;
2766 : : }
2767 : : };
2768 : :
2769 : : class success : public success_call_info
2770 : : {
2771 : : public:
2772 : 9 : success (const call_details &cd) : success_call_info (cd) {}
2773 : :
2774 : 17 : bool update_model (region_model *model,
2775 : : const exploded_edge *,
2776 : : region_model_context *ctxt) const final override
2777 : : {
2778 : 17 : const call_details cd (get_call_details (model, ctxt));
2779 : :
2780 : : /* Return 0. */
2781 : 17 : model->update_for_zero_return (cd, true);
2782 : :
2783 : : /* Update fd array. */
2784 : 17 : region_model_manager *mgr = cd.get_manager ();
2785 : 17 : tree arr_tree = cd.get_arg_tree (0);
2786 : 17 : const svalue *arr_sval = cd.get_arg_svalue (0);
2787 : 51 : for (int idx = 0; idx < 2; idx++)
2788 : : {
2789 : 34 : const region *arr_reg
2790 : 34 : = model->deref_rvalue (arr_sval, arr_tree, cd.get_ctxt ());
2791 : 34 : const svalue *idx_sval
2792 : 34 : = mgr->get_or_create_int_cst (integer_type_node, idx);
2793 : 34 : const region *element_reg
2794 : 34 : = mgr->get_element_region (arr_reg, integer_type_node, idx_sval);
2795 : 34 : conjured_purge p (model, cd.get_ctxt ());
2796 : 34 : const svalue *fd_sval
2797 : 34 : = mgr->get_or_create_conjured_svalue (integer_type_node,
2798 : 34 : &cd.get_call_stmt (),
2799 : : element_reg,
2800 : : p);
2801 : 34 : model->set_value (element_reg, fd_sval, cd.get_ctxt ());
2802 : 34 : model->mark_as_valid_fd (fd_sval, cd.get_ctxt ());
2803 : : }
2804 : 17 : return true;
2805 : : }
2806 : : };
2807 : :
2808 : : public:
2809 : 6620 : kf_pipe (unsigned num_args)
2810 : 6620 : : m_num_args (num_args)
2811 : : {
2812 : 6620 : gcc_assert (num_args > 0);
2813 : 6620 : }
2814 : :
2815 : 118 : bool matches_call_types_p (const call_details &cd) const final override
2816 : : {
2817 : 118 : return (cd.num_args () == m_num_args && cd.arg_is_pointer_p (0));
2818 : : }
2819 : :
2820 : 29 : void impl_call_post (const call_details &cd) const final override
2821 : : {
2822 : 29 : if (cd.get_ctxt ())
2823 : : {
2824 : 9 : cd.get_ctxt ()->bifurcate
2825 : 9 : (std::make_unique<failure> (cd));
2826 : 9 : cd.get_ctxt ()->bifurcate
2827 : 9 : (std::make_unique<success> (cd));
2828 : 9 : cd.get_ctxt ()->terminate_path ();
2829 : : }
2830 : 29 : }
2831 : :
2832 : : private:
2833 : : unsigned m_num_args;
2834 : : };
2835 : :
2836 : : /* Handler for "read".
2837 : : ssize_t read(int fildes, void *buf, size_t nbyte);
2838 : : See e.g. https://man7.org/linux/man-pages/man2/read.2.html */
2839 : :
2840 : 3310 : class kf_read : public known_function
2841 : : {
2842 : : public:
2843 : 332 : bool matches_call_types_p (const call_details &cd) const final override
2844 : : {
2845 : 332 : return (cd.num_args () == 3
2846 : 332 : && cd.arg_is_pointer_p (1)
2847 : 664 : && cd.arg_is_size_p (2));
2848 : : }
2849 : :
2850 : : /* For now, assume that any call to "read" fully clobbers the buffer
2851 : : passed in. This isn't quite correct (e.g. errors, partial reads;
2852 : : see PR analyzer/108689), but at least stops us falsely complaining
2853 : : about the buffer being uninitialized. */
2854 : 78 : void impl_call_pre (const call_details &cd) const final override
2855 : : {
2856 : 78 : region_model *model = cd.get_model ();
2857 : 78 : const svalue *ptr_sval = cd.get_arg_svalue (1);
2858 : 78 : if (const region *reg = ptr_sval->maybe_get_region ())
2859 : : {
2860 : 59 : const region *base_reg = reg->get_base_region ();
2861 : 59 : const svalue *new_sval = cd.get_or_create_conjured_svalue (base_reg);
2862 : 59 : model->set_value (base_reg, new_sval, cd.get_ctxt ());
2863 : : }
2864 : 78 : cd.set_any_lhs_with_defaults ();
2865 : 78 : }
2866 : : };
2867 : :
2868 : :
2869 : : /* Populate KFM with instances of known functions relating to
2870 : : file descriptors. */
2871 : :
2872 : : void
2873 : 3310 : register_known_fd_functions (known_function_manager &kfm)
2874 : : {
2875 : 3310 : kfm.add ("accept", std::make_unique<kf_accept> ());
2876 : 3310 : kfm.add ("bind", std::make_unique<kf_bind> ());
2877 : 3310 : kfm.add ("connect", std::make_unique<kf_connect> ());
2878 : 3310 : kfm.add ("isatty", std::make_unique<kf_isatty> ());
2879 : 3310 : kfm.add ("listen", std::make_unique<kf_listen> ());
2880 : 3310 : kfm.add ("pipe", std::make_unique<kf_pipe> (1));
2881 : 3310 : kfm.add ("pipe2", std::make_unique<kf_pipe> (2));
2882 : 3310 : kfm.add ("read", std::make_unique<kf_read> ());
2883 : 3310 : kfm.add ("socket", std::make_unique<kf_socket> ());
2884 : 3310 : }
2885 : :
2886 : : } // namespace ana
2887 : :
2888 : : #endif // ENABLE_ANALYZER
|