1 /* A state machine for detecting misuses of <stdio.h>'s FILE * API.
2 Copyright (C) 2019-2024 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
5 This file is part of GCC.
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)
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.
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/>. */
22 #define INCLUDE_MEMORY
23 #define INCLUDE_VECTOR
25 #include "coretypes.h"
26 #include "make-unique.h"
29 #include "basic-block.h"
32 #include "diagnostic-path.h"
33 #include "analyzer/analyzer.h"
34 #include "diagnostic-event-id.h"
35 #include "analyzer/analyzer-logging.h"
36 #include "analyzer/sm.h"
37 #include "analyzer/pending-diagnostic.h"
38 #include "analyzer/function-set.h"
39 #include "analyzer/analyzer-selftests.h"
41 #include "analyzer/call-string.h"
42 #include "analyzer/program-point.h"
43 #include "analyzer/store.h"
44 #include "analyzer/region-model.h"
45 #include "analyzer/call-details.h"
53 /* A state machine for detecting misuses of <stdio.h>'s FILE * API. */
55 class fileptr_state_machine
: public state_machine
58 fileptr_state_machine (logger
*logger
);
60 bool inherited_state_p () const final override
{ return false; }
62 state_machine::state_t
63 get_default_state (const svalue
*sval
) const final override
65 if (tree cst
= sval
->maybe_get_constant ())
73 bool on_stmt (sm_context
&sm_ctxt
,
74 const supernode
*node
,
75 const gimple
*stmt
) const final override
;
77 void on_condition (sm_context
&sm_ctxt
,
78 const supernode
*node
,
82 const svalue
*rhs
) const final override
;
84 bool can_purge_p (state_t s
) const final override
;
85 std::unique_ptr
<pending_diagnostic
> on_leak (tree var
) const final override
;
87 /* State for a FILE * returned from fopen that hasn't been checked for
89 It could be an open stream, or could be NULL. */
92 /* State for a FILE * that's known to be NULL. */
95 /* State for a FILE * that's known to be a non-NULL open stream. */
98 /* State for a FILE * that's had fclose called on it. */
101 /* Stop state, for a FILE * we don't want to track any more. */
105 /* Base class for diagnostics relative to fileptr_state_machine. */
107 class file_diagnostic
: public pending_diagnostic
110 file_diagnostic (const fileptr_state_machine
&sm
, tree arg
)
111 : m_sm (sm
), m_arg (arg
)
114 bool subclass_equal_p (const pending_diagnostic
&base_other
) const override
116 return same_tree_p (m_arg
, ((const file_diagnostic
&)base_other
).m_arg
);
119 label_text
describe_state_change (const evdesc::state_change
&change
)
122 if (change
.m_old_state
== m_sm
.get_start_state ()
123 && change
.m_new_state
== m_sm
.m_unchecked
)
124 // TODO: verify that it's the fopen stmt, not a copy
125 return label_text::borrow ("opened here");
126 if (change
.m_old_state
== m_sm
.m_unchecked
127 && change
.m_new_state
== m_sm
.m_nonnull
)
130 return change
.formatted_print ("assuming %qE is non-NULL",
133 return change
.formatted_print ("assuming FILE * is non-NULL");
135 if (change
.m_new_state
== m_sm
.m_null
)
138 return change
.formatted_print ("assuming %qE is NULL",
141 return change
.formatted_print ("assuming FILE * is NULL");
143 return label_text ();
146 diagnostic_event::meaning
147 get_meaning_for_state_change (const evdesc::state_change
&change
)
150 if (change
.m_old_state
== m_sm
.get_start_state ()
151 && change
.m_new_state
== m_sm
.m_unchecked
)
152 return diagnostic_event::meaning (diagnostic_event::VERB_acquire
,
153 diagnostic_event::NOUN_resource
);
154 if (change
.m_new_state
== m_sm
.m_closed
)
155 return diagnostic_event::meaning (diagnostic_event::VERB_release
,
156 diagnostic_event::NOUN_resource
);
157 return diagnostic_event::meaning ();
161 const fileptr_state_machine
&m_sm
;
165 class double_fclose
: public file_diagnostic
168 double_fclose (const fileptr_state_machine
&sm
, tree arg
)
169 : file_diagnostic (sm
, arg
)
172 const char *get_kind () const final override
{ return "double_fclose"; }
174 int get_controlling_option () const final override
176 return OPT_Wanalyzer_double_fclose
;
179 bool emit (diagnostic_emission_context
&ctxt
) final override
181 /* CWE-1341: Multiple Releases of Same Resource or Handle. */
183 return ctxt
.warn ("double %<fclose%> of FILE %qE",
187 label_text
describe_state_change (const evdesc::state_change
&change
)
190 if (change
.m_new_state
== m_sm
.m_closed
)
192 m_first_fclose_event
= change
.m_event_id
;
193 return change
.formatted_print ("first %qs here", "fclose");
195 return file_diagnostic::describe_state_change (change
);
198 label_text
describe_final_event (const evdesc::final_event
&ev
) final override
200 if (m_first_fclose_event
.known_p ())
201 return ev
.formatted_print ("second %qs here; first %qs was at %@",
203 &m_first_fclose_event
);
204 return ev
.formatted_print ("second %qs here", "fclose");
208 diagnostic_event_id_t m_first_fclose_event
;
211 class file_leak
: public file_diagnostic
214 file_leak (const fileptr_state_machine
&sm
, tree arg
)
215 : file_diagnostic (sm
, arg
)
218 const char *get_kind () const final override
{ return "file_leak"; }
220 int get_controlling_option () const final override
222 return OPT_Wanalyzer_file_leak
;
225 bool emit (diagnostic_emission_context
&ctxt
) final override
227 /* CWE-775: "Missing Release of File Descriptor or Handle after
228 Effective Lifetime". */
231 return ctxt
.warn ("leak of FILE %qE", m_arg
);
233 return ctxt
.warn ("leak of FILE");
236 label_text
describe_state_change (const evdesc::state_change
&change
)
239 if (change
.m_new_state
== m_sm
.m_unchecked
)
241 m_fopen_event
= change
.m_event_id
;
242 return label_text::borrow ("opened here");
244 return file_diagnostic::describe_state_change (change
);
247 label_text
describe_final_event (const evdesc::final_event
&ev
) final override
249 if (m_fopen_event
.known_p ())
252 return ev
.formatted_print ("%qE leaks here; was opened at %@",
253 ev
.m_expr
, &m_fopen_event
);
255 return ev
.formatted_print ("leaks here; was opened at %@",
261 return ev
.formatted_print ("%qE leaks here", ev
.m_expr
);
263 return ev
.formatted_print ("leaks here");
268 diagnostic_event_id_t m_fopen_event
;
271 /* fileptr_state_machine's ctor. */
273 fileptr_state_machine::fileptr_state_machine (logger
*logger
)
274 : state_machine ("file", logger
),
275 m_unchecked (add_state ("unchecked")),
276 m_null (add_state ("null")),
277 m_nonnull (add_state ("nonnull")),
278 m_closed (add_state ("closed")),
279 m_stop (add_state ("stop"))
283 /* Get a set of functions that are known to take a FILE * that must be open,
284 and are known to not close it. */
287 get_file_using_fns ()
289 // TODO: populate this list more fully
290 static const char * const funcnames
[] = {
291 /* This array must be kept sorted. */
307 "fflush", // safe to call with NULL
308 "fflush_unlocked", // safe to call with NULL
343 const size_t count
= ARRAY_SIZE (funcnames
);
344 function_set
fs (funcnames
, count
);
348 /* Return true if FNDECL is known to require an open FILE *, and is known
352 is_file_using_fn_p (tree fndecl
)
354 function_set fs
= get_file_using_fns ();
355 if (fs
.contains_decl_p (fndecl
))
358 /* Also support variants of these names prefixed with "_IO_". */
359 const char *name
= IDENTIFIER_POINTER (DECL_NAME (fndecl
));
360 if (startswith (name
, "_IO_") && fs
.contains_name_p (name
+ 4))
366 /* Implementation of state_machine::on_stmt vfunc for fileptr_state_machine. */
369 fileptr_state_machine::on_stmt (sm_context
&sm_ctxt
,
370 const supernode
*node
,
371 const gimple
*stmt
) const
373 if (const gcall
*call
= dyn_cast
<const gcall
*> (stmt
))
374 if (tree callee_fndecl
= sm_ctxt
.get_fndecl_for_call (call
))
376 if (is_named_call_p (callee_fndecl
, "fopen", call
, 2))
378 tree lhs
= gimple_call_lhs (call
);
380 sm_ctxt
.on_transition (node
, stmt
, lhs
, m_start
, m_unchecked
);
383 /* TODO: report leak. */
388 if (is_named_call_p (callee_fndecl
, "fclose", call
, 1))
390 tree arg
= gimple_call_arg (call
, 0);
392 sm_ctxt
.on_transition (node
, stmt
, arg
, m_start
, m_closed
);
394 // TODO: is it safe to call fclose (NULL) ?
395 sm_ctxt
.on_transition (node
, stmt
, arg
, m_unchecked
, m_closed
);
396 sm_ctxt
.on_transition (node
, stmt
, arg
, m_null
, m_closed
);
398 sm_ctxt
.on_transition (node
, stmt
, arg
, m_nonnull
, m_closed
);
400 if (sm_ctxt
.get_state (stmt
, arg
) == m_closed
)
402 tree diag_arg
= sm_ctxt
.get_diagnostic_tree (arg
);
403 sm_ctxt
.warn (node
, stmt
, arg
,
404 make_unique
<double_fclose
> (*this, diag_arg
));
405 sm_ctxt
.set_next_state (stmt
, arg
, m_stop
);
410 if (is_file_using_fn_p (callee_fndecl
))
412 // TODO: operations on unchecked file
421 /* Implementation of state_machine::on_condition vfunc for
422 fileptr_state_machine.
423 Potentially transition state 'unchecked' to 'nonnull' or to 'null'. */
426 fileptr_state_machine::on_condition (sm_context
&sm_ctxt
,
427 const supernode
*node
,
431 const svalue
*rhs
) const
433 if (!rhs
->all_zeroes_p ())
436 // TODO: has to be a FILE *, specifically
437 if (!any_pointer_p (lhs
))
439 // TODO: has to be a FILE *, specifically
440 if (!any_pointer_p (rhs
))
445 log ("got 'ARG != 0' match");
446 sm_ctxt
.on_transition (node
, stmt
,
447 lhs
, m_unchecked
, m_nonnull
);
449 else if (op
== EQ_EXPR
)
451 log ("got 'ARG == 0' match");
452 sm_ctxt
.on_transition (node
, stmt
,
453 lhs
, m_unchecked
, m_null
);
457 /* Implementation of state_machine::can_purge_p vfunc for fileptr_state_machine.
458 Don't allow purging of pointers in state 'unchecked' or 'nonnull'
459 (to avoid false leak reports). */
462 fileptr_state_machine::can_purge_p (state_t s
) const
464 return s
!= m_unchecked
&& s
!= m_nonnull
;
467 /* Implementation of state_machine::on_leak vfunc for
468 fileptr_state_machine, for complaining about leaks of FILE * in
469 state 'unchecked' and 'nonnull'. */
471 std::unique_ptr
<pending_diagnostic
>
472 fileptr_state_machine::on_leak (tree var
) const
474 return make_unique
<file_leak
> (*this, var
);
477 } // anonymous namespace
479 /* Internal interface to this file. */
482 make_fileptr_state_machine (logger
*logger
)
484 return new fileptr_state_machine (logger
);
487 /* Handler for various stdio-related builtins that merely have external
488 effects that are out of scope for the analyzer: we only want to model
489 the effects on the return value. */
491 class kf_stdio_output_fn
: public pure_known_function_with_default_return
494 bool matches_call_types_p (const call_details
&) const final override
499 /* A no-op; we just want the conjured return value. */
502 /* Handler for "ferror"". */
504 class kf_ferror
: public pure_known_function_with_default_return
507 bool matches_call_types_p (const call_details
&cd
) const final override
509 return (cd
.num_args () == 1
510 && cd
.arg_is_pointer_p (0));
513 /* No side effects. */
516 /* Handler for "fileno"". */
518 class kf_fileno
: public pure_known_function_with_default_return
521 bool matches_call_types_p (const call_details
&cd
) const final override
523 return (cd
.num_args () == 1
524 && cd
.arg_is_pointer_p (0));
527 /* No side effects. */
530 /* Handler for "fgets" and "fgets_unlocked". */
532 class kf_fgets
: public known_function
535 bool matches_call_types_p (const call_details
&cd
) const final override
537 return (cd
.num_args () == 3
538 && cd
.arg_is_pointer_p (0)
539 && cd
.arg_is_pointer_p (2));
542 void impl_call_pre (const call_details
&cd
) const final override
544 /* Ideally we would bifurcate state here between the
545 error vs no error cases. */
546 region_model
*model
= cd
.get_model ();
547 const svalue
*ptr_sval
= cd
.get_arg_svalue (0);
548 if (const region
*reg
= ptr_sval
->maybe_get_region ())
550 const region
*base_reg
= reg
->get_base_region ();
551 const svalue
*new_sval
= cd
.get_or_create_conjured_svalue (base_reg
);
552 model
->set_value (base_reg
, new_sval
, cd
.get_ctxt ());
554 cd
.set_any_lhs_with_defaults ();
558 /* Handler for "fread".
559 size_t fread(void *restrict buffer, size_t size, size_t count,
560 FILE *restrict stream);
561 See e.g. https://en.cppreference.com/w/c/io/fread
562 and https://www.man7.org/linux/man-pages/man3/fread.3.html */
564 class kf_fread
: public known_function
567 bool matches_call_types_p (const call_details
&cd
) const final override
569 return (cd
.num_args () == 4
570 && cd
.arg_is_pointer_p (0)
571 && cd
.arg_is_size_p (1)
572 && cd
.arg_is_size_p (2)
573 && cd
.arg_is_pointer_p (3));
576 /* For now, assume that any call to "fread" fully clobbers the buffer
577 passed in. This isn't quite correct (e.g. errors, partial reads;
578 see PR analyzer/108689), but at least stops us falsely complaining
579 about the buffer being uninitialized. */
580 void impl_call_pre (const call_details
&cd
) const final override
582 region_model
*model
= cd
.get_model ();
583 const svalue
*ptr_sval
= cd
.get_arg_svalue (0);
584 if (const region
*reg
= ptr_sval
->maybe_get_region ())
586 const region
*base_reg
= reg
->get_base_region ();
587 const svalue
*new_sval
= cd
.get_or_create_conjured_svalue (base_reg
);
588 model
->set_value (base_reg
, new_sval
, cd
.get_ctxt ());
590 cd
.set_any_lhs_with_defaults ();
594 /* Handler for "getc"". */
596 class kf_getc
: public pure_known_function_with_default_return
599 bool matches_call_types_p (const call_details
&cd
) const final override
601 return (cd
.num_args () == 1
602 && cd
.arg_is_pointer_p (0));
606 /* Handler for "getchar"". */
608 class kf_getchar
: public pure_known_function_with_default_return
611 bool matches_call_types_p (const call_details
&cd
) const final override
613 return cd
.num_args () == 0;
616 /* Empty. No side-effects (tracking stream state is out-of-scope
617 for the analyzer). */
620 /* Populate KFM with instances of known functions relating to
624 register_known_file_functions (known_function_manager
&kfm
)
626 kfm
.add (BUILT_IN_FPRINTF
, make_unique
<kf_stdio_output_fn
> ());
627 kfm
.add (BUILT_IN_FPRINTF_UNLOCKED
, make_unique
<kf_stdio_output_fn
> ());
628 kfm
.add (BUILT_IN_FPUTC
, make_unique
<kf_stdio_output_fn
> ());
629 kfm
.add (BUILT_IN_FPUTC_UNLOCKED
, make_unique
<kf_stdio_output_fn
> ());
630 kfm
.add (BUILT_IN_FPUTS
, make_unique
<kf_stdio_output_fn
> ());
631 kfm
.add (BUILT_IN_FPUTS_UNLOCKED
, make_unique
<kf_stdio_output_fn
> ());
632 kfm
.add (BUILT_IN_FWRITE
, make_unique
<kf_stdio_output_fn
> ());
633 kfm
.add (BUILT_IN_FWRITE_UNLOCKED
, make_unique
<kf_stdio_output_fn
> ());
634 kfm
.add (BUILT_IN_PRINTF
, make_unique
<kf_stdio_output_fn
> ());
635 kfm
.add (BUILT_IN_PRINTF_UNLOCKED
, make_unique
<kf_stdio_output_fn
> ());
636 kfm
.add (BUILT_IN_PUTC
, make_unique
<kf_stdio_output_fn
> ());
637 kfm
.add (BUILT_IN_PUTCHAR
, make_unique
<kf_stdio_output_fn
> ());
638 kfm
.add (BUILT_IN_PUTCHAR_UNLOCKED
, make_unique
<kf_stdio_output_fn
> ());
639 kfm
.add (BUILT_IN_PUTC_UNLOCKED
, make_unique
<kf_stdio_output_fn
> ());
640 kfm
.add (BUILT_IN_PUTS
, make_unique
<kf_stdio_output_fn
> ());
641 kfm
.add (BUILT_IN_PUTS_UNLOCKED
, make_unique
<kf_stdio_output_fn
> ());
642 kfm
.add (BUILT_IN_VFPRINTF
, make_unique
<kf_stdio_output_fn
> ());
643 kfm
.add (BUILT_IN_VPRINTF
, make_unique
<kf_stdio_output_fn
> ());
645 kfm
.add ("ferror", make_unique
<kf_ferror
> ());
646 kfm
.add ("fgets", make_unique
<kf_fgets
> ());
647 kfm
.add ("fgets_unlocked", make_unique
<kf_fgets
> ()); // non-standard
648 kfm
.add ("fileno", make_unique
<kf_fileno
> ());
649 kfm
.add ("fread", make_unique
<kf_fread
> ());
650 kfm
.add ("getc", make_unique
<kf_getc
> ());
651 kfm
.add ("getchar", make_unique
<kf_getchar
> ());
653 /* Some C++ implementations use the std:: copies of these functions
654 from <cstdio> for <stdio.h>, so we must match against these too. */
655 kfm
.add_std_ns ("ferror", make_unique
<kf_ferror
> ());
656 kfm
.add_std_ns ("fgets", make_unique
<kf_fgets
> ());
657 kfm
.add_std_ns ("fread", make_unique
<kf_fread
> ());
658 kfm
.add_std_ns ("getc", make_unique
<kf_getc
> ());
659 kfm
.add_std_ns ("getchar", make_unique
<kf_getchar
> ());
666 /* Run all of the selftests within this file. */
669 analyzer_sm_file_cc_tests ()
671 function_set fs
= get_file_using_fns ();
676 } // namespace selftest
678 #endif /* CHECKING_P */
682 #endif /* #if ENABLE_ANALYZER */