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