analyzer: Handle strdup builtins
[official-gcc.git] / gcc / analyzer / sm-malloc.cc
blob4f07d1f9257c345f4cc1c1524073c986d1fabbd6
1 /* A state machine for detecting misuses of the malloc/free API.
2 Copyright (C) 2019-2021 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)
10 any later version.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tree.h"
25 #include "function.h"
26 #include "basic-block.h"
27 #include "gimple.h"
28 #include "options.h"
29 #include "bitmap.h"
30 #include "diagnostic-path.h"
31 #include "diagnostic-metadata.h"
32 #include "function.h"
33 #include "json.h"
34 #include "analyzer/analyzer.h"
35 #include "diagnostic-event-id.h"
36 #include "analyzer/analyzer-logging.h"
37 #include "analyzer/sm.h"
38 #include "analyzer/pending-diagnostic.h"
39 #include "tristate.h"
40 #include "selftest.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 "stringpool.h"
46 #include "attribs.h"
47 #include "analyzer/function-set.h"
49 #if ENABLE_ANALYZER
51 namespace ana {
53 namespace {
55 /* This state machine and its various support classes track allocations
56 and deallocations.
58 It has a few standard allocation/deallocation pairs (e.g. new/delete),
59 and also supports user-defined ones via
60 __attribute__ ((malloc(DEALLOCATOR))).
62 There can be more than one valid deallocator for a given allocator,
63 for example:
64 __attribute__ ((malloc (fclose)))
65 __attribute__ ((malloc (freopen, 3)))
66 FILE* fopen (const char*, const char*);
67 A deallocator_set represents a particular set of valid deallocators.
69 We track the expected deallocator_set for a value, but not the allocation
70 function - there could be more than one allocator per deallocator_set.
71 For example, there could be dozens of allocators for "free" beyond just
72 malloc e.g. calloc, xstrdup, etc. We don't want to explode the number
73 of states by tracking individual allocators in the exploded graph;
74 we merely want to track "this value expects to have 'free' called on it".
75 Perhaps we can reconstruct which allocator was used later, when emitting
76 the path, if it's necessary for precision of wording of diagnostics. */
78 class deallocator;
79 class deallocator_set;
80 class malloc_state_machine;
82 /* An enum for discriminating between different kinds of allocation_state. */
84 enum resource_state
86 /* States that are independent of allocator/deallocator. */
88 /* The start state. */
89 RS_START,
91 /* State for a pointer that's known to be NULL. */
92 RS_NULL,
94 /* State for a pointer that's known to not be on the heap (e.g. to a local
95 or global). */
96 RS_NON_HEAP,
98 /* Stop state, for pointers we don't want to track any more. */
99 RS_STOP,
101 /* States that relate to a specific deallocator_set. */
103 /* State for a pointer returned from an allocator that hasn't
104 been checked for NULL.
105 It could be a pointer to heap-allocated memory, or could be NULL. */
106 RS_UNCHECKED,
108 /* State for a pointer returned from an allocator,
109 known to be non-NULL. */
110 RS_NONNULL,
112 /* State for a pointer passed to a deallocator. */
113 RS_FREED
116 /* Custom state subclass, which can optionally refer to an a
117 deallocator_set. */
119 struct allocation_state : public state_machine::state
121 allocation_state (const char *name, unsigned id,
122 enum resource_state rs,
123 const deallocator_set *deallocators,
124 const deallocator *deallocator)
125 : state (name, id), m_rs (rs),
126 m_deallocators (deallocators),
127 m_deallocator (deallocator)
130 void dump_to_pp (pretty_printer *pp) const FINAL OVERRIDE;
132 const allocation_state *get_nonnull () const;
134 enum resource_state m_rs;
135 const deallocator_set *m_deallocators;
136 const deallocator *m_deallocator;
139 /* An enum for choosing which wording to use in various diagnostics
140 when describing deallocations. */
142 enum wording
144 WORDING_FREED,
145 WORDING_DELETED,
146 WORDING_DEALLOCATED,
147 WORDING_REALLOCATED
150 /* Base class representing a deallocation function,
151 either a built-in one we know about, or one exposed via
152 __attribute__((malloc(DEALLOCATOR))). */
154 struct deallocator
156 hashval_t hash () const;
157 void dump_to_pp (pretty_printer *pp) const;
158 static int cmp (const deallocator *a, const deallocator *b);
159 static int cmp_ptr_ptr (const void *, const void *);
161 /* Name to use in diagnostics. */
162 const char *m_name;
164 /* Which wording to use in diagnostics. */
165 enum wording m_wording;
167 /* State for a value passed to one of the deallocators. */
168 state_machine::state_t m_freed;
170 protected:
171 deallocator (malloc_state_machine *sm,
172 const char *name,
173 enum wording wording);
176 /* Subclass representing a predefined deallocator.
177 e.g. "delete []", without needing a specific FUNCTION_DECL
178 ahead of time. */
180 struct standard_deallocator : public deallocator
182 standard_deallocator (malloc_state_machine *sm,
183 const char *name,
184 enum wording wording);
187 /* Subclass representing a user-defined deallocator
188 via __attribute__((malloc(DEALLOCATOR))) given
189 a specific FUNCTION_DECL. */
191 struct custom_deallocator : public deallocator
193 custom_deallocator (malloc_state_machine *sm,
194 tree deallocator_fndecl,
195 enum wording wording)
196 : deallocator (sm, IDENTIFIER_POINTER (DECL_NAME (deallocator_fndecl)),
197 wording)
202 /* Base class representing a set of possible deallocators.
203 Often this will be just a single deallocator, but some
204 allocators have multiple valid deallocators (e.g. the result of
205 "fopen" can be closed by either "fclose" or "freopen"). */
207 struct deallocator_set
209 deallocator_set (malloc_state_machine *sm,
210 enum wording wording);
211 virtual ~deallocator_set () {}
213 virtual bool contains_p (const deallocator *d) const = 0;
214 virtual const deallocator *maybe_get_single () const = 0;
215 virtual void dump_to_pp (pretty_printer *pp) const = 0;
216 void dump () const;
218 /* Which wording to use in diagnostics. */
219 enum wording m_wording;
221 /* Pointers to states.
222 These states are owned by the state_machine base class. */
224 /* State for an unchecked result from an allocator using this set. */
225 state_machine::state_t m_unchecked;
227 /* State for a known non-NULL result from such an allocator. */
228 state_machine::state_t m_nonnull;
231 /* Subclass of deallocator_set representing a set of deallocators
232 defined by one or more __attribute__((malloc(DEALLOCATOR))). */
234 struct custom_deallocator_set : public deallocator_set
236 typedef const auto_vec <const deallocator *> *key_t;
238 custom_deallocator_set (malloc_state_machine *sm,
239 const auto_vec <const deallocator *> *vec,
240 //const char *name,
241 //const char *dealloc_funcname,
242 //unsigned arg_idx,
243 enum wording wording);
245 bool contains_p (const deallocator *d) const FINAL OVERRIDE;
246 const deallocator *maybe_get_single () const FINAL OVERRIDE;
247 void dump_to_pp (pretty_printer *pp) const FINAL OVERRIDE;
249 auto_vec <const deallocator *> m_deallocator_vec;
252 /* Subclass of deallocator_set representing a set of deallocators
253 with a single standard_deallocator, e.g. "delete []". */
255 struct standard_deallocator_set : public deallocator_set
257 standard_deallocator_set (malloc_state_machine *sm,
258 const char *name,
259 enum wording wording);
261 bool contains_p (const deallocator *d) const FINAL OVERRIDE;
262 const deallocator *maybe_get_single () const FINAL OVERRIDE;
263 void dump_to_pp (pretty_printer *pp) const FINAL OVERRIDE;
265 standard_deallocator m_deallocator;
268 /* Traits class for ensuring uniqueness of deallocator_sets within
269 malloc_state_machine. */
271 struct deallocator_set_map_traits
273 typedef custom_deallocator_set::key_t key_type;
274 typedef custom_deallocator_set *value_type;
275 typedef custom_deallocator_set *compare_type;
277 static inline hashval_t hash (const key_type &k)
279 gcc_assert (k != NULL);
280 gcc_assert (k != reinterpret_cast<key_type> (1));
282 hashval_t result = 0;
283 unsigned i;
284 const deallocator *d;
285 FOR_EACH_VEC_ELT (*k, i, d)
286 result ^= d->hash ();
287 return result;
289 static inline bool equal_keys (const key_type &k1, const key_type &k2)
291 if (k1->length () != k2->length ())
292 return false;
294 for (unsigned i = 0; i < k1->length (); i++)
295 if ((*k1)[i] != (*k2)[i])
296 return false;
298 return true;
300 template <typename T>
301 static inline void remove (T &)
303 /* empty; the nodes are handled elsewhere. */
305 template <typename T>
306 static inline void mark_deleted (T &entry)
308 entry.m_key = reinterpret_cast<key_type> (1);
310 template <typename T>
311 static inline void mark_empty (T &entry)
313 entry.m_key = NULL;
315 template <typename T>
316 static inline bool is_deleted (const T &entry)
318 return entry.m_key == reinterpret_cast<key_type> (1);
320 template <typename T>
321 static inline bool is_empty (const T &entry)
323 return entry.m_key == NULL;
325 static const bool empty_zero_p = false;
328 /* A state machine for detecting misuses of the malloc/free API.
330 See sm-malloc.dot for an overview (keep this in-sync with that file). */
332 class malloc_state_machine : public state_machine
334 public:
335 typedef allocation_state custom_data_t;
337 malloc_state_machine (logger *logger);
338 ~malloc_state_machine ();
340 state_t
341 add_state (const char *name, enum resource_state rs,
342 const deallocator_set *deallocators,
343 const deallocator *deallocator);
345 bool inherited_state_p () const FINAL OVERRIDE { return false; }
347 state_machine::state_t
348 get_default_state (const svalue *sval) const FINAL OVERRIDE
350 if (tree cst = sval->maybe_get_constant ())
352 if (zerop (cst))
353 return m_null;
355 if (const region_svalue *ptr = sval->dyn_cast_region_svalue ())
357 const region *reg = ptr->get_pointee ();
358 const region *base_reg = reg->get_base_region ();
359 if (base_reg->get_kind () == RK_DECL
360 || base_reg->get_kind () == RK_STRING)
361 return m_non_heap;
363 return m_start;
366 bool on_stmt (sm_context *sm_ctxt,
367 const supernode *node,
368 const gimple *stmt) const FINAL OVERRIDE;
370 void on_phi (sm_context *sm_ctxt,
371 const supernode *node,
372 const gphi *phi,
373 tree rhs) const FINAL OVERRIDE;
375 void on_condition (sm_context *sm_ctxt,
376 const supernode *node,
377 const gimple *stmt,
378 const svalue *lhs,
379 enum tree_code op,
380 const svalue *rhs) const FINAL OVERRIDE;
382 bool can_purge_p (state_t s) const FINAL OVERRIDE;
383 pending_diagnostic *on_leak (tree var) const FINAL OVERRIDE;
385 bool reset_when_passed_to_unknown_fn_p (state_t s,
386 bool is_mutable) const FINAL OVERRIDE;
388 static bool unaffected_by_call_p (tree fndecl);
390 standard_deallocator_set m_free;
391 standard_deallocator_set m_scalar_delete;
392 standard_deallocator_set m_vector_delete;
394 standard_deallocator m_realloc;
396 /* States that are independent of api. */
398 /* State for a pointer that's known to be NULL. */
399 state_t m_null;
401 /* State for a pointer that's known to not be on the heap (e.g. to a local
402 or global). */
403 state_t m_non_heap; // TODO: or should this be a different state machine?
404 // or do we need child values etc?
406 /* Stop state, for pointers we don't want to track any more. */
407 state_t m_stop;
409 private:
410 const custom_deallocator_set *
411 get_or_create_custom_deallocator_set (tree allocator_fndecl);
412 custom_deallocator_set *
413 maybe_create_custom_deallocator_set (tree allocator_fndecl);
414 const deallocator *
415 get_or_create_deallocator (tree deallocator_fndecl);
417 void on_allocator_call (sm_context *sm_ctxt,
418 const gcall *call,
419 const deallocator_set *deallocators,
420 bool returns_nonnull = false) const;
421 void on_deallocator_call (sm_context *sm_ctxt,
422 const supernode *node,
423 const gcall *call,
424 const deallocator *d,
425 unsigned argno) const;
426 void on_realloc_call (sm_context *sm_ctxt,
427 const supernode *node,
428 const gcall *call) const;
429 void on_zero_assignment (sm_context *sm_ctxt,
430 const gimple *stmt,
431 tree lhs) const;
433 /* A map for consolidating deallocators so that they are
434 unique per deallocator FUNCTION_DECL. */
435 typedef hash_map<tree, deallocator *> deallocator_map_t;
436 deallocator_map_t m_deallocator_map;
438 /* Memoized lookups from FUNCTION_DECL to custom_deallocator_set *. */
439 typedef hash_map<tree, custom_deallocator_set *> deallocator_set_cache_t;
440 deallocator_set_cache_t m_custom_deallocator_set_cache;
442 /* A map for consolidating custom_deallocator_set instances. */
443 typedef hash_map<custom_deallocator_set::key_t,
444 custom_deallocator_set *,
445 deallocator_set_map_traits> custom_deallocator_set_map_t;
446 custom_deallocator_set_map_t m_custom_deallocator_set_map;
448 /* Record of dynamically-allocated objects, for cleanup. */
449 auto_vec <custom_deallocator_set *> m_dynamic_sets;
450 auto_vec <custom_deallocator *> m_dynamic_deallocators;
453 /* struct deallocator. */
455 deallocator::deallocator (malloc_state_machine *sm,
456 const char *name,
457 enum wording wording)
458 : m_name (name),
459 m_wording (wording),
460 m_freed (sm->add_state ("freed", RS_FREED, NULL, this))
464 hashval_t
465 deallocator::hash () const
467 return (hashval_t)m_freed->get_id ();
470 void
471 deallocator::dump_to_pp (pretty_printer *pp) const
473 pp_printf (pp, "%qs", m_name);
477 deallocator::cmp (const deallocator *a, const deallocator *b)
479 return (int)a->m_freed->get_id () - (int)b->m_freed->get_id ();
483 deallocator::cmp_ptr_ptr (const void *a, const void *b)
485 return cmp (*(const deallocator * const *)a,
486 *(const deallocator * const *)b);
490 /* struct standard_deallocator : public deallocator. */
492 standard_deallocator::standard_deallocator (malloc_state_machine *sm,
493 const char *name,
494 enum wording wording)
495 : deallocator (sm, name, wording)
499 /* struct deallocator_set. */
501 deallocator_set::deallocator_set (malloc_state_machine *sm,
502 enum wording wording)
503 : m_wording (wording),
504 m_unchecked (sm->add_state ("unchecked", RS_UNCHECKED, this, NULL)),
505 m_nonnull (sm->add_state ("nonnull", RS_NONNULL, this, NULL))
509 /* Dump a description of this deallocator_set to stderr. */
511 DEBUG_FUNCTION void
512 deallocator_set::dump () const
514 pretty_printer pp;
515 pp_show_color (&pp) = pp_show_color (global_dc->printer);
516 pp.buffer->stream = stderr;
517 dump_to_pp (&pp);
518 pp_newline (&pp);
519 pp_flush (&pp);
522 /* struct custom_deallocator_set : public deallocator_set. */
524 custom_deallocator_set::
525 custom_deallocator_set (malloc_state_machine *sm,
526 const auto_vec <const deallocator *> *vec,
527 enum wording wording)
528 : deallocator_set (sm, wording),
529 m_deallocator_vec (vec->length ())
531 unsigned i;
532 const deallocator *d;
533 FOR_EACH_VEC_ELT (*vec, i, d)
534 m_deallocator_vec.safe_push (d);
537 bool
538 custom_deallocator_set::contains_p (const deallocator *d) const
540 unsigned i;
541 const deallocator *cd;
542 FOR_EACH_VEC_ELT (m_deallocator_vec, i, cd)
543 if (cd == d)
544 return true;
545 return false;
548 const deallocator *
549 custom_deallocator_set::maybe_get_single () const
551 if (m_deallocator_vec.length () == 1)
552 return m_deallocator_vec[0];
553 return NULL;
556 void
557 custom_deallocator_set::dump_to_pp (pretty_printer *pp) const
559 pp_character (pp, '{');
560 unsigned i;
561 const deallocator *d;
562 FOR_EACH_VEC_ELT (m_deallocator_vec, i, d)
564 if (i > 0)
565 pp_string (pp, ", ");
566 d->dump_to_pp (pp);
568 pp_character (pp, '}');
571 /* struct standard_deallocator_set : public deallocator_set. */
573 standard_deallocator_set::standard_deallocator_set (malloc_state_machine *sm,
574 const char *name,
575 enum wording wording)
576 : deallocator_set (sm, wording),
577 m_deallocator (sm, name, wording)
581 bool
582 standard_deallocator_set::contains_p (const deallocator *d) const
584 return d == &m_deallocator;
587 const deallocator *
588 standard_deallocator_set::maybe_get_single () const
590 return &m_deallocator;
593 void
594 standard_deallocator_set::dump_to_pp (pretty_printer *pp) const
596 pp_character (pp, '{');
597 pp_string (pp, m_deallocator.m_name);
598 pp_character (pp, '}');
601 /* Return STATE cast to the custom state subclass, or NULL for the start state.
602 Everything should be an allocation_state apart from the start state. */
604 static const allocation_state *
605 dyn_cast_allocation_state (state_machine::state_t state)
607 if (state->get_id () == 0)
608 return NULL;
609 return static_cast <const allocation_state *> (state);
612 /* Return STATE cast to the custom state subclass, for a state that is
613 already known to not be the start state . */
615 static const allocation_state *
616 as_a_allocation_state (state_machine::state_t state)
618 gcc_assert (state->get_id () != 0);
619 return static_cast <const allocation_state *> (state);
622 /* Get the resource_state for STATE. */
624 static enum resource_state
625 get_rs (state_machine::state_t state)
627 if (const allocation_state *astate = dyn_cast_allocation_state (state))
628 return astate->m_rs;
629 else
630 return RS_START;
633 /* Return true if STATE is the start state. */
635 static bool
636 start_p (state_machine::state_t state)
638 return get_rs (state) == RS_START;
641 /* Return true if STATE is an unchecked result from an allocator. */
643 static bool
644 unchecked_p (state_machine::state_t state)
646 return get_rs (state) == RS_UNCHECKED;
649 /* Return true if STATE is a non-null result from an allocator. */
651 static bool
652 nonnull_p (state_machine::state_t state)
654 return get_rs (state) == RS_NONNULL;
657 /* Return true if STATE is a value that has been passed to a deallocator. */
659 static bool
660 freed_p (state_machine::state_t state)
662 return get_rs (state) == RS_FREED;
665 /* Class for diagnostics relating to malloc_state_machine. */
667 class malloc_diagnostic : public pending_diagnostic
669 public:
670 malloc_diagnostic (const malloc_state_machine &sm, tree arg)
671 : m_sm (sm), m_arg (arg)
674 bool subclass_equal_p (const pending_diagnostic &base_other) const OVERRIDE
676 return same_tree_p (m_arg, ((const malloc_diagnostic &)base_other).m_arg);
679 label_text describe_state_change (const evdesc::state_change &change)
680 OVERRIDE
682 if (change.m_old_state == m_sm.get_start_state ()
683 && unchecked_p (change.m_new_state))
684 // TODO: verify that it's the allocation stmt, not a copy
685 return label_text::borrow ("allocated here");
686 if (unchecked_p (change.m_old_state)
687 && nonnull_p (change.m_new_state))
689 if (change.m_expr)
690 return change.formatted_print ("assuming %qE is non-NULL",
691 change.m_expr);
692 else
693 return change.formatted_print ("assuming %qs is non-NULL",
694 "<unknown>");
696 if (change.m_new_state == m_sm.m_null)
698 if (unchecked_p (change.m_old_state))
700 if (change.m_expr)
701 return change.formatted_print ("assuming %qE is NULL",
702 change.m_expr);
703 else
704 return change.formatted_print ("assuming %qs is NULL",
705 "<unknown>");
707 else
709 if (change.m_expr)
710 return change.formatted_print ("%qE is NULL",
711 change.m_expr);
712 else
713 return change.formatted_print ("%qs is NULL",
714 "<unknown>");
718 return label_text ();
721 protected:
722 const malloc_state_machine &m_sm;
723 tree m_arg;
726 /* Concrete subclass for reporting mismatching allocator/deallocator
727 diagnostics. */
729 class mismatching_deallocation : public malloc_diagnostic
731 public:
732 mismatching_deallocation (const malloc_state_machine &sm, tree arg,
733 const deallocator_set *expected_deallocators,
734 const deallocator *actual_dealloc)
735 : malloc_diagnostic (sm, arg),
736 m_expected_deallocators (expected_deallocators),
737 m_actual_dealloc (actual_dealloc)
740 const char *get_kind () const FINAL OVERRIDE
742 return "mismatching_deallocation";
745 bool emit (rich_location *rich_loc) FINAL OVERRIDE
747 auto_diagnostic_group d;
748 diagnostic_metadata m;
749 m.add_cwe (762); /* CWE-762: Mismatched Memory Management Routines. */
750 if (const deallocator *expected_dealloc
751 = m_expected_deallocators->maybe_get_single ())
752 return warning_meta (rich_loc, m, OPT_Wanalyzer_mismatching_deallocation,
753 "%qE should have been deallocated with %qs"
754 " but was deallocated with %qs",
755 m_arg, expected_dealloc->m_name,
756 m_actual_dealloc->m_name);
757 else
758 return warning_meta (rich_loc, m, OPT_Wanalyzer_mismatching_deallocation,
759 "%qs called on %qE returned from a mismatched"
760 " allocation function",
761 m_actual_dealloc->m_name, m_arg);
764 label_text describe_state_change (const evdesc::state_change &change)
765 FINAL OVERRIDE
767 if (unchecked_p (change.m_new_state))
769 m_alloc_event = change.m_event_id;
770 if (const deallocator *expected_dealloc
771 = m_expected_deallocators->maybe_get_single ())
772 return change.formatted_print ("allocated here"
773 " (expects deallocation with %qs)",
774 expected_dealloc->m_name);
775 else
776 return change.formatted_print ("allocated here");
778 return malloc_diagnostic::describe_state_change (change);
781 label_text describe_final_event (const evdesc::final_event &ev) FINAL OVERRIDE
783 if (m_alloc_event.known_p ())
785 if (const deallocator *expected_dealloc
786 = m_expected_deallocators->maybe_get_single ())
787 return ev.formatted_print
788 ("deallocated with %qs here;"
789 " allocation at %@ expects deallocation with %qs",
790 m_actual_dealloc->m_name, &m_alloc_event,
791 expected_dealloc->m_name);
792 else
793 return ev.formatted_print
794 ("deallocated with %qs here;"
795 " allocated at %@",
796 m_actual_dealloc->m_name, &m_alloc_event);
798 return ev.formatted_print ("deallocated with %qs here",
799 m_actual_dealloc->m_name);
802 private:
803 diagnostic_event_id_t m_alloc_event;
804 const deallocator_set *m_expected_deallocators;
805 const deallocator *m_actual_dealloc;
808 /* Concrete subclass for reporting double-free diagnostics. */
810 class double_free : public malloc_diagnostic
812 public:
813 double_free (const malloc_state_machine &sm, tree arg, const char *funcname)
814 : malloc_diagnostic (sm, arg), m_funcname (funcname)
817 const char *get_kind () const FINAL OVERRIDE { return "double_free"; }
819 bool emit (rich_location *rich_loc) FINAL OVERRIDE
821 auto_diagnostic_group d;
822 diagnostic_metadata m;
823 m.add_cwe (415); /* CWE-415: Double Free. */
824 return warning_meta (rich_loc, m, OPT_Wanalyzer_double_free,
825 "double-%qs of %qE", m_funcname, m_arg);
828 label_text describe_state_change (const evdesc::state_change &change)
829 FINAL OVERRIDE
831 if (freed_p (change.m_new_state))
833 m_first_free_event = change.m_event_id;
834 return change.formatted_print ("first %qs here", m_funcname);
836 return malloc_diagnostic::describe_state_change (change);
839 label_text describe_call_with_state (const evdesc::call_with_state &info)
840 FINAL OVERRIDE
842 if (freed_p (info.m_state))
843 return info.formatted_print
844 ("passing freed pointer %qE in call to %qE from %qE",
845 info.m_expr, info.m_callee_fndecl, info.m_caller_fndecl);
846 return label_text ();
849 label_text describe_final_event (const evdesc::final_event &ev) FINAL OVERRIDE
851 if (m_first_free_event.known_p ())
852 return ev.formatted_print ("second %qs here; first %qs was at %@",
853 m_funcname, m_funcname,
854 &m_first_free_event);
855 return ev.formatted_print ("second %qs here", m_funcname);
858 private:
859 diagnostic_event_id_t m_first_free_event;
860 const char *m_funcname;
863 /* Abstract subclass for describing possible bad uses of NULL.
864 Responsible for describing the call that could return NULL. */
866 class possible_null : public malloc_diagnostic
868 public:
869 possible_null (const malloc_state_machine &sm, tree arg)
870 : malloc_diagnostic (sm, arg)
873 label_text describe_state_change (const evdesc::state_change &change)
874 FINAL OVERRIDE
876 if (change.m_old_state == m_sm.get_start_state ()
877 && unchecked_p (change.m_new_state))
879 m_origin_of_unchecked_event = change.m_event_id;
880 return label_text::borrow ("this call could return NULL");
882 return malloc_diagnostic::describe_state_change (change);
885 label_text describe_return_of_state (const evdesc::return_of_state &info)
886 FINAL OVERRIDE
888 if (unchecked_p (info.m_state))
889 return info.formatted_print ("possible return of NULL to %qE from %qE",
890 info.m_caller_fndecl, info.m_callee_fndecl);
891 return label_text ();
894 protected:
895 diagnostic_event_id_t m_origin_of_unchecked_event;
898 /* Concrete subclass for describing dereference of a possible NULL
899 value. */
901 class possible_null_deref : public possible_null
903 public:
904 possible_null_deref (const malloc_state_machine &sm, tree arg)
905 : possible_null (sm, arg)
908 const char *get_kind () const FINAL OVERRIDE { return "possible_null_deref"; }
910 bool emit (rich_location *rich_loc) FINAL OVERRIDE
912 /* CWE-690: Unchecked Return Value to NULL Pointer Dereference. */
913 diagnostic_metadata m;
914 m.add_cwe (690);
915 return warning_meta (rich_loc, m,
916 OPT_Wanalyzer_possible_null_dereference,
917 "dereference of possibly-NULL %qE", m_arg);
920 label_text describe_final_event (const evdesc::final_event &ev) FINAL OVERRIDE
922 if (m_origin_of_unchecked_event.known_p ())
923 return ev.formatted_print ("%qE could be NULL: unchecked value from %@",
924 ev.m_expr,
925 &m_origin_of_unchecked_event);
926 else
927 return ev.formatted_print ("%qE could be NULL", ev.m_expr);
932 /* Return true if FNDECL is a C++ method. */
934 static bool
935 method_p (tree fndecl)
937 return TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE;
940 /* Return a 1-based description of ARG_IDX (0-based) of FNDECL.
941 Compare with %P in the C++ FE (implemented in cp/error.c: parm_to_string
942 as called from cp_printer). */
944 static label_text
945 describe_argument_index (tree fndecl, int arg_idx)
947 if (method_p (fndecl))
948 if (arg_idx == 0)
949 return label_text::borrow ("'this'");
950 pretty_printer pp;
951 pp_printf (&pp, "%u", arg_idx + 1 - method_p (fndecl));
952 return label_text::take (xstrdup (pp_formatted_text (&pp)));
955 /* Subroutine for use by possible_null_arg::emit and null_arg::emit.
956 Issue a note informing that the pertinent argument must be non-NULL. */
958 static void
959 inform_nonnull_attribute (tree fndecl, int arg_idx)
961 label_text arg_desc = describe_argument_index (fndecl, arg_idx);
962 inform (DECL_SOURCE_LOCATION (fndecl),
963 "argument %s of %qD must be non-null",
964 arg_desc.m_buffer, fndecl);
965 arg_desc.maybe_free ();
966 /* Ideally we would use the location of the parm and underline the
967 attribute also - but we don't have the location_t values at this point
968 in the middle-end.
969 For reference, the C and C++ FEs have get_fndecl_argument_location. */
972 /* Concrete subclass for describing passing a possibly-NULL value to a
973 function marked with __attribute__((nonnull)). */
975 class possible_null_arg : public possible_null
977 public:
978 possible_null_arg (const malloc_state_machine &sm, tree arg,
979 tree fndecl, int arg_idx)
980 : possible_null (sm, arg),
981 m_fndecl (fndecl), m_arg_idx (arg_idx)
984 const char *get_kind () const FINAL OVERRIDE { return "possible_null_arg"; }
986 bool subclass_equal_p (const pending_diagnostic &base_other) const
988 const possible_null_arg &sub_other
989 = (const possible_null_arg &)base_other;
990 return (same_tree_p (m_arg, sub_other.m_arg)
991 && m_fndecl == sub_other.m_fndecl
992 && m_arg_idx == sub_other.m_arg_idx);
996 bool emit (rich_location *rich_loc) FINAL OVERRIDE
998 /* CWE-690: Unchecked Return Value to NULL Pointer Dereference. */
999 auto_diagnostic_group d;
1000 diagnostic_metadata m;
1001 m.add_cwe (690);
1002 bool warned
1003 = warning_meta (rich_loc, m, OPT_Wanalyzer_possible_null_argument,
1004 "use of possibly-NULL %qE where non-null expected",
1005 m_arg);
1006 if (warned)
1007 inform_nonnull_attribute (m_fndecl, m_arg_idx);
1008 return warned;
1011 label_text describe_final_event (const evdesc::final_event &ev) FINAL OVERRIDE
1013 label_text arg_desc = describe_argument_index (m_fndecl, m_arg_idx);
1014 label_text result;
1015 if (m_origin_of_unchecked_event.known_p ())
1016 result = ev.formatted_print ("argument %s (%qE) from %@ could be NULL"
1017 " where non-null expected",
1018 arg_desc.m_buffer, ev.m_expr,
1019 &m_origin_of_unchecked_event);
1020 else
1021 result = ev.formatted_print ("argument %s (%qE) could be NULL"
1022 " where non-null expected",
1023 arg_desc.m_buffer, ev.m_expr);
1024 arg_desc.maybe_free ();
1025 return result;
1028 private:
1029 tree m_fndecl;
1030 int m_arg_idx;
1033 /* Concrete subclass for describing a dereference of a NULL value. */
1035 class null_deref : public malloc_diagnostic
1037 public:
1038 null_deref (const malloc_state_machine &sm, tree arg)
1039 : malloc_diagnostic (sm, arg) {}
1041 const char *get_kind () const FINAL OVERRIDE { return "null_deref"; }
1043 bool emit (rich_location *rich_loc) FINAL OVERRIDE
1045 /* CWE-476: NULL Pointer Dereference. */
1046 diagnostic_metadata m;
1047 m.add_cwe (476);
1048 return warning_meta (rich_loc, m,
1049 OPT_Wanalyzer_null_dereference,
1050 "dereference of NULL %qE", m_arg);
1053 label_text describe_return_of_state (const evdesc::return_of_state &info)
1054 FINAL OVERRIDE
1056 if (info.m_state == m_sm.m_null)
1057 return info.formatted_print ("return of NULL to %qE from %qE",
1058 info.m_caller_fndecl, info.m_callee_fndecl);
1059 return label_text ();
1062 label_text describe_final_event (const evdesc::final_event &ev) FINAL OVERRIDE
1064 return ev.formatted_print ("dereference of NULL %qE", ev.m_expr);
1068 /* Concrete subclass for describing passing a NULL value to a
1069 function marked with __attribute__((nonnull)). */
1071 class null_arg : public malloc_diagnostic
1073 public:
1074 null_arg (const malloc_state_machine &sm, tree arg,
1075 tree fndecl, int arg_idx)
1076 : malloc_diagnostic (sm, arg),
1077 m_fndecl (fndecl), m_arg_idx (arg_idx)
1080 const char *get_kind () const FINAL OVERRIDE { return "null_arg"; }
1082 bool subclass_equal_p (const pending_diagnostic &base_other) const
1084 const null_arg &sub_other
1085 = (const null_arg &)base_other;
1086 return (same_tree_p (m_arg, sub_other.m_arg)
1087 && m_fndecl == sub_other.m_fndecl
1088 && m_arg_idx == sub_other.m_arg_idx);
1091 bool emit (rich_location *rich_loc) FINAL OVERRIDE
1093 /* CWE-476: NULL Pointer Dereference. */
1094 auto_diagnostic_group d;
1095 diagnostic_metadata m;
1096 m.add_cwe (476);
1098 bool warned;
1099 if (zerop (m_arg))
1100 warned = warning_meta (rich_loc, m, OPT_Wanalyzer_null_argument,
1101 "use of NULL where non-null expected");
1102 else
1103 warned = warning_meta (rich_loc, m, OPT_Wanalyzer_null_argument,
1104 "use of NULL %qE where non-null expected",
1105 m_arg);
1106 if (warned)
1107 inform_nonnull_attribute (m_fndecl, m_arg_idx);
1108 return warned;
1111 label_text describe_final_event (const evdesc::final_event &ev) FINAL OVERRIDE
1113 label_text arg_desc = describe_argument_index (m_fndecl, m_arg_idx);
1114 label_text result;
1115 if (zerop (ev.m_expr))
1116 result = ev.formatted_print ("argument %s NULL where non-null expected",
1117 arg_desc.m_buffer);
1118 else
1119 result = ev.formatted_print ("argument %s (%qE) NULL"
1120 " where non-null expected",
1121 arg_desc.m_buffer, ev.m_expr);
1122 arg_desc.maybe_free ();
1123 return result;
1126 private:
1127 tree m_fndecl;
1128 int m_arg_idx;
1131 class use_after_free : public malloc_diagnostic
1133 public:
1134 use_after_free (const malloc_state_machine &sm, tree arg,
1135 const deallocator *deallocator)
1136 : malloc_diagnostic (sm, arg),
1137 m_deallocator (deallocator)
1139 gcc_assert (deallocator);
1142 const char *get_kind () const FINAL OVERRIDE { return "use_after_free"; }
1144 bool emit (rich_location *rich_loc) FINAL OVERRIDE
1146 /* CWE-416: Use After Free. */
1147 diagnostic_metadata m;
1148 m.add_cwe (416);
1149 return warning_meta (rich_loc, m, OPT_Wanalyzer_use_after_free,
1150 "use after %<%s%> of %qE",
1151 m_deallocator->m_name, m_arg);
1154 label_text describe_state_change (const evdesc::state_change &change)
1155 FINAL OVERRIDE
1157 if (freed_p (change.m_new_state))
1159 m_free_event = change.m_event_id;
1160 switch (m_deallocator->m_wording)
1162 default:
1163 case WORDING_REALLOCATED:
1164 gcc_unreachable ();
1165 case WORDING_FREED:
1166 return label_text::borrow ("freed here");
1167 case WORDING_DELETED:
1168 return label_text::borrow ("deleted here");
1169 case WORDING_DEALLOCATED:
1170 return label_text::borrow ("deallocated here");
1173 return malloc_diagnostic::describe_state_change (change);
1176 label_text describe_final_event (const evdesc::final_event &ev) FINAL OVERRIDE
1178 const char *funcname = m_deallocator->m_name;
1179 if (m_free_event.known_p ())
1180 switch (m_deallocator->m_wording)
1182 default:
1183 case WORDING_REALLOCATED:
1184 gcc_unreachable ();
1185 case WORDING_FREED:
1186 return ev.formatted_print ("use after %<%s%> of %qE; freed at %@",
1187 funcname, ev.m_expr, &m_free_event);
1188 case WORDING_DELETED:
1189 return ev.formatted_print ("use after %<%s%> of %qE; deleted at %@",
1190 funcname, ev.m_expr, &m_free_event);
1191 case WORDING_DEALLOCATED:
1192 return ev.formatted_print ("use after %<%s%> of %qE;"
1193 " deallocated at %@",
1194 funcname, ev.m_expr, &m_free_event);
1196 else
1197 return ev.formatted_print ("use after %<%s%> of %qE",
1198 funcname, ev.m_expr);
1201 /* Implementation of pending_diagnostic::supercedes_p for
1202 use_after_free.
1204 We want use-after-free to supercede use-of-unitialized-value,
1205 so that if we have these at the same stmt, we don't emit
1206 a use-of-uninitialized, just the use-after-free.
1207 (this is because we fully purge information about freed
1208 buffers when we free them to avoid state explosions, so
1209 that if they are accessed after the free, it looks like
1210 they are uninitialized). */
1212 bool supercedes_p (const pending_diagnostic &other) const FINAL OVERRIDE
1214 if (other.use_of_uninit_p ())
1215 return true;
1217 return false;
1220 private:
1221 diagnostic_event_id_t m_free_event;
1222 const deallocator *m_deallocator;
1225 class malloc_leak : public malloc_diagnostic
1227 public:
1228 malloc_leak (const malloc_state_machine &sm, tree arg)
1229 : malloc_diagnostic (sm, arg) {}
1231 const char *get_kind () const FINAL OVERRIDE { return "malloc_leak"; }
1233 bool emit (rich_location *rich_loc) FINAL OVERRIDE
1235 diagnostic_metadata m;
1236 m.add_cwe (401);
1237 if (m_arg)
1238 return warning_meta (rich_loc, m, OPT_Wanalyzer_malloc_leak,
1239 "leak of %qE", m_arg);
1240 else
1241 return warning_meta (rich_loc, m, OPT_Wanalyzer_malloc_leak,
1242 "leak of %qs", "<unknown>");
1245 label_text describe_state_change (const evdesc::state_change &change)
1246 FINAL OVERRIDE
1248 if (unchecked_p (change.m_new_state)
1249 || (start_p (change.m_old_state) && nonnull_p (change.m_new_state)))
1251 m_alloc_event = change.m_event_id;
1252 return label_text::borrow ("allocated here");
1254 return malloc_diagnostic::describe_state_change (change);
1257 label_text describe_final_event (const evdesc::final_event &ev) FINAL OVERRIDE
1259 if (ev.m_expr)
1261 if (m_alloc_event.known_p ())
1262 return ev.formatted_print ("%qE leaks here; was allocated at %@",
1263 ev.m_expr, &m_alloc_event);
1264 else
1265 return ev.formatted_print ("%qE leaks here", ev.m_expr);
1267 else
1269 if (m_alloc_event.known_p ())
1270 return ev.formatted_print ("%qs leaks here; was allocated at %@",
1271 "<unknown>", &m_alloc_event);
1272 else
1273 return ev.formatted_print ("%qs leaks here", "<unknown>");
1277 private:
1278 diagnostic_event_id_t m_alloc_event;
1281 class free_of_non_heap : public malloc_diagnostic
1283 public:
1284 free_of_non_heap (const malloc_state_machine &sm, tree arg,
1285 const char *funcname)
1286 : malloc_diagnostic (sm, arg), m_funcname (funcname), m_kind (KIND_UNKNOWN)
1290 const char *get_kind () const FINAL OVERRIDE { return "free_of_non_heap"; }
1292 bool subclass_equal_p (const pending_diagnostic &base_other) const
1293 FINAL OVERRIDE
1295 const free_of_non_heap &other = (const free_of_non_heap &)base_other;
1296 return (same_tree_p (m_arg, other.m_arg) && m_kind == other.m_kind);
1299 bool emit (rich_location *rich_loc) FINAL OVERRIDE
1301 auto_diagnostic_group d;
1302 diagnostic_metadata m;
1303 m.add_cwe (590); /* CWE-590: Free of Memory not on the Heap. */
1304 switch (m_kind)
1306 default:
1307 gcc_unreachable ();
1308 case KIND_UNKNOWN:
1309 return warning_meta (rich_loc, m, OPT_Wanalyzer_free_of_non_heap,
1310 "%<%s%> of %qE which points to memory"
1311 " not on the heap",
1312 m_funcname, m_arg);
1313 break;
1314 case KIND_ALLOCA:
1315 return warning_meta (rich_loc, m, OPT_Wanalyzer_free_of_non_heap,
1316 "%<%s%> of memory allocated on the stack by"
1317 " %qs (%qE) will corrupt the heap",
1318 m_funcname, "alloca", m_arg);
1319 break;
1323 label_text describe_state_change (const evdesc::state_change &change)
1324 FINAL OVERRIDE
1326 /* Attempt to reconstruct what kind of pointer it is.
1327 (It seems neater for this to be a part of the state, though). */
1328 if (change.m_expr && TREE_CODE (change.m_expr) == SSA_NAME)
1330 gimple *def_stmt = SSA_NAME_DEF_STMT (change.m_expr);
1331 if (gcall *call = dyn_cast <gcall *> (def_stmt))
1333 if (is_special_named_call_p (call, "alloca", 1)
1334 || is_special_named_call_p (call, "__builtin_alloca", 1))
1336 m_kind = KIND_ALLOCA;
1337 return label_text::borrow
1338 ("memory is allocated on the stack here");
1342 return label_text::borrow ("pointer is from here");
1345 label_text describe_final_event (const evdesc::final_event &ev) FINAL OVERRIDE
1347 return ev.formatted_print ("call to %qs here", m_funcname);
1350 private:
1351 enum kind
1353 KIND_UNKNOWN,
1354 KIND_ALLOCA
1356 const char *m_funcname;
1357 enum kind m_kind;
1360 /* struct allocation_state : public state_machine::state. */
1362 /* Implementation of state_machine::state::dump_to_pp vfunc
1363 for allocation_state: append the API that this allocation is
1364 associated with. */
1366 void
1367 allocation_state::dump_to_pp (pretty_printer *pp) const
1369 state_machine::state::dump_to_pp (pp);
1370 if (m_deallocators)
1372 pp_string (pp, " (");
1373 m_deallocators->dump_to_pp (pp);
1374 pp_character (pp, ')');
1378 /* Given a allocation_state for a deallocator_set, get the "nonnull" state
1379 for the corresponding allocator(s). */
1381 const allocation_state *
1382 allocation_state::get_nonnull () const
1384 gcc_assert (m_deallocators);
1385 return as_a_allocation_state (m_deallocators->m_nonnull);
1388 /* malloc_state_machine's ctor. */
1390 malloc_state_machine::malloc_state_machine (logger *logger)
1391 : state_machine ("malloc", logger),
1392 m_free (this, "free", WORDING_FREED),
1393 m_scalar_delete (this, "delete", WORDING_DELETED),
1394 m_vector_delete (this, "delete[]", WORDING_DELETED),
1395 m_realloc (this, "realloc", WORDING_REALLOCATED)
1397 gcc_assert (m_start->get_id () == 0);
1398 m_null = add_state ("null", RS_FREED, NULL, NULL);
1399 m_non_heap = add_state ("non-heap", RS_NON_HEAP, NULL, NULL);
1400 m_stop = add_state ("stop", RS_STOP, NULL, NULL);
1403 malloc_state_machine::~malloc_state_machine ()
1405 unsigned i;
1406 custom_deallocator_set *set;
1407 FOR_EACH_VEC_ELT (m_dynamic_sets, i, set)
1408 delete set;
1409 custom_deallocator *d;
1410 FOR_EACH_VEC_ELT (m_dynamic_deallocators, i, d)
1411 delete d;
1414 state_machine::state_t
1415 malloc_state_machine::add_state (const char *name, enum resource_state rs,
1416 const deallocator_set *deallocators,
1417 const deallocator *deallocator)
1419 return add_custom_state (new allocation_state (name, alloc_state_id (),
1420 rs, deallocators,
1421 deallocator));
1424 /* If ALLOCATOR_FNDECL has any "__attribute__((malloc(FOO)))",
1425 return a custom_deallocator_set for them, consolidating them
1426 to ensure uniqueness of the sets.
1428 Return NULL if it has no such attributes. */
1430 const custom_deallocator_set *
1431 malloc_state_machine::
1432 get_or_create_custom_deallocator_set (tree allocator_fndecl)
1434 /* Early rejection of decls without attributes. */
1435 tree attrs = DECL_ATTRIBUTES (allocator_fndecl);
1436 if (!attrs)
1437 return NULL;
1439 /* Otherwise, call maybe_create_custom_deallocator_set,
1440 memoizing the result. */
1441 if (custom_deallocator_set **slot
1442 = m_custom_deallocator_set_cache.get (allocator_fndecl))
1443 return *slot;
1444 custom_deallocator_set *set
1445 = maybe_create_custom_deallocator_set (allocator_fndecl);
1446 m_custom_deallocator_set_cache.put (allocator_fndecl, set);
1447 return set;
1450 /* Given ALLOCATOR_FNDECL, a FUNCTION_DECL with attributes,
1451 look for any "__attribute__((malloc(FOO)))" and return a
1452 custom_deallocator_set for them, consolidating them
1453 to ensure uniqueness of the sets.
1455 Return NULL if it has no such attributes.
1457 Subroutine of get_or_create_custom_deallocator_set which
1458 memoizes the result. */
1460 custom_deallocator_set *
1461 malloc_state_machine::
1462 maybe_create_custom_deallocator_set (tree allocator_fndecl)
1464 tree attrs = DECL_ATTRIBUTES (allocator_fndecl);
1465 gcc_assert (attrs);
1467 /* Look for instances of __attribute__((malloc(FOO))). */
1468 auto_vec<const deallocator *> deallocator_vec;
1469 for (tree allocs = attrs;
1470 (allocs = lookup_attribute ("malloc", allocs));
1471 allocs = TREE_CHAIN (allocs))
1473 tree args = TREE_VALUE (allocs);
1474 if (!args)
1475 continue;
1476 if (TREE_VALUE (args))
1478 const deallocator *d
1479 = get_or_create_deallocator (TREE_VALUE (args));
1480 deallocator_vec.safe_push (d);
1484 /* If there weren't any deallocators, bail. */
1485 if (deallocator_vec.length () == 0)
1486 return NULL;
1488 /* Consolidate, so that we reuse existing deallocator_set
1489 instances. */
1490 deallocator_vec.qsort (deallocator::cmp_ptr_ptr);
1491 custom_deallocator_set **slot
1492 = m_custom_deallocator_set_map.get (&deallocator_vec);
1493 if (slot)
1494 return *slot;
1495 custom_deallocator_set *set
1496 = new custom_deallocator_set (this, &deallocator_vec, WORDING_DEALLOCATED);
1497 m_custom_deallocator_set_map.put (&set->m_deallocator_vec, set);
1498 m_dynamic_sets.safe_push (set);
1499 return set;
1502 /* Get the deallocator for DEALLOCATOR_FNDECL, creating it if necessary. */
1504 const deallocator *
1505 malloc_state_machine::get_or_create_deallocator (tree deallocator_fndecl)
1507 deallocator **slot = m_deallocator_map.get (deallocator_fndecl);
1508 if (slot)
1509 return *slot;
1511 /* Reuse "free". */
1512 deallocator *d;
1513 if (is_named_call_p (deallocator_fndecl, "free")
1514 || is_std_named_call_p (deallocator_fndecl, "free")
1515 || is_named_call_p (deallocator_fndecl, "__builtin_free"))
1516 d = &m_free.m_deallocator;
1517 else
1519 custom_deallocator *cd
1520 = new custom_deallocator (this, deallocator_fndecl,
1521 WORDING_DEALLOCATED);
1522 m_dynamic_deallocators.safe_push (cd);
1523 d = cd;
1525 m_deallocator_map.put (deallocator_fndecl, d);
1526 return d;
1529 /* Try to identify the function declaration either by name or as a known malloc
1530 builtin. */
1532 static bool
1533 known_allocator_p (const_tree fndecl, const gcall *call)
1535 /* Either it is a function we know by name and number of arguments... */
1536 if (is_named_call_p (fndecl, "malloc", call, 1)
1537 || is_named_call_p (fndecl, "calloc", call, 2)
1538 || is_std_named_call_p (fndecl, "malloc", call, 1)
1539 || is_std_named_call_p (fndecl, "calloc", call, 2)
1540 || is_named_call_p (fndecl, "strdup", call, 1)
1541 || is_named_call_p (fndecl, "strndup", call, 2))
1542 return true;
1544 /* ... or it is a builtin allocator that allocates objects freed with
1545 __builtin_free. */
1546 if (fndecl_built_in_p (fndecl))
1547 switch (DECL_FUNCTION_CODE (fndecl))
1549 case BUILT_IN_MALLOC:
1550 case BUILT_IN_CALLOC:
1551 case BUILT_IN_STRDUP:
1552 case BUILT_IN_STRNDUP:
1553 return true;
1554 default:
1555 break;
1558 return false;
1561 /* Implementation of state_machine::on_stmt vfunc for malloc_state_machine. */
1563 bool
1564 malloc_state_machine::on_stmt (sm_context *sm_ctxt,
1565 const supernode *node,
1566 const gimple *stmt) const
1568 if (const gcall *call = dyn_cast <const gcall *> (stmt))
1569 if (tree callee_fndecl = sm_ctxt->get_fndecl_for_call (call))
1571 if (known_allocator_p (callee_fndecl, call))
1573 on_allocator_call (sm_ctxt, call, &m_free);
1574 return true;
1577 if (is_named_call_p (callee_fndecl, "operator new", call, 1))
1578 on_allocator_call (sm_ctxt, call, &m_scalar_delete);
1579 else if (is_named_call_p (callee_fndecl, "operator new []", call, 1))
1580 on_allocator_call (sm_ctxt, call, &m_vector_delete);
1581 else if (is_named_call_p (callee_fndecl, "operator delete", call, 1)
1582 || is_named_call_p (callee_fndecl, "operator delete", call, 2))
1584 on_deallocator_call (sm_ctxt, node, call,
1585 &m_scalar_delete.m_deallocator, 0);
1586 return true;
1588 else if (is_named_call_p (callee_fndecl, "operator delete []", call, 1))
1590 on_deallocator_call (sm_ctxt, node, call,
1591 &m_vector_delete.m_deallocator, 0);
1592 return true;
1595 if (is_named_call_p (callee_fndecl, "alloca", call, 1)
1596 || is_named_call_p (callee_fndecl, "__builtin_alloca", call, 1))
1598 tree lhs = gimple_call_lhs (call);
1599 if (lhs)
1600 sm_ctxt->on_transition (node, stmt, lhs, m_start, m_non_heap);
1601 return true;
1604 if (is_named_call_p (callee_fndecl, "free", call, 1)
1605 || is_std_named_call_p (callee_fndecl, "free", call, 1)
1606 || is_named_call_p (callee_fndecl, "__builtin_free", call, 1))
1608 on_deallocator_call (sm_ctxt, node, call,
1609 &m_free.m_deallocator, 0);
1610 return true;
1613 if (is_named_call_p (callee_fndecl, "realloc", call, 2)
1614 || is_named_call_p (callee_fndecl, "__builtin_realloc", call, 2))
1616 on_realloc_call (sm_ctxt, node, call);
1617 return true;
1620 if (unaffected_by_call_p (callee_fndecl))
1621 return true;
1623 /* Cast away const-ness for cache-like operations. */
1624 malloc_state_machine *mutable_this
1625 = const_cast <malloc_state_machine *> (this);
1627 /* Handle "__attribute__((malloc(FOO)))". */
1628 if (const deallocator_set *deallocators
1629 = mutable_this->get_or_create_custom_deallocator_set
1630 (callee_fndecl))
1632 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (callee_fndecl));
1633 bool returns_nonnull
1634 = lookup_attribute ("returns_nonnull", attrs);
1635 on_allocator_call (sm_ctxt, call, deallocators, returns_nonnull);
1638 /* Handle "__attribute__((nonnull))". */
1640 tree fntype = TREE_TYPE (callee_fndecl);
1641 bitmap nonnull_args = get_nonnull_args (fntype);
1642 if (nonnull_args)
1644 for (unsigned i = 0; i < gimple_call_num_args (stmt); i++)
1646 tree arg = gimple_call_arg (stmt, i);
1647 if (TREE_CODE (TREE_TYPE (arg)) != POINTER_TYPE)
1648 continue;
1649 /* If we have a nonnull-args, and either all pointers, or just
1650 the specified pointers. */
1651 if (bitmap_empty_p (nonnull_args)
1652 || bitmap_bit_p (nonnull_args, i))
1654 state_t state = sm_ctxt->get_state (stmt, arg);
1655 /* Can't use a switch as the states are non-const. */
1656 if (unchecked_p (state))
1658 tree diag_arg = sm_ctxt->get_diagnostic_tree (arg);
1659 sm_ctxt->warn (node, stmt, arg,
1660 new possible_null_arg (*this, diag_arg,
1661 callee_fndecl,
1662 i));
1663 const allocation_state *astate
1664 = as_a_allocation_state (state);
1665 sm_ctxt->set_next_state (stmt, arg,
1666 astate->get_nonnull ());
1668 else if (state == m_null)
1670 tree diag_arg = sm_ctxt->get_diagnostic_tree (arg);
1671 sm_ctxt->warn (node, stmt, arg,
1672 new null_arg (*this, diag_arg,
1673 callee_fndecl, i));
1674 sm_ctxt->set_next_state (stmt, arg, m_stop);
1678 BITMAP_FREE (nonnull_args);
1682 /* Check for this after nonnull, so that if we have both
1683 then we transition to "freed", rather than "checked". */
1684 unsigned dealloc_argno = fndecl_dealloc_argno (callee_fndecl);
1685 if (dealloc_argno != UINT_MAX)
1687 const deallocator *d
1688 = mutable_this->get_or_create_deallocator (callee_fndecl);
1689 on_deallocator_call (sm_ctxt, node, call, d, dealloc_argno);
1693 if (tree lhs = sm_ctxt->is_zero_assignment (stmt))
1694 if (any_pointer_p (lhs))
1695 on_zero_assignment (sm_ctxt, stmt,lhs);
1697 /* If we have "LHS = &EXPR;" and EXPR is something other than a MEM_REF,
1698 transition LHS from start to non_heap.
1699 Doing it for ADDR_EXPR(MEM_REF()) is likely wrong, and can lead to
1700 unbounded chains of unmergeable sm-state on pointer arithmetic in loops
1701 when optimization is enabled. */
1702 if (const gassign *assign_stmt = dyn_cast <const gassign *> (stmt))
1704 enum tree_code op = gimple_assign_rhs_code (assign_stmt);
1705 if (op == ADDR_EXPR)
1707 tree lhs = gimple_assign_lhs (assign_stmt);
1708 if (lhs)
1710 tree addr_expr = gimple_assign_rhs1 (assign_stmt);
1711 if (TREE_CODE (TREE_OPERAND (addr_expr, 0)) != MEM_REF)
1712 sm_ctxt->on_transition (node, stmt, lhs, m_start, m_non_heap);
1717 /* Handle dereferences. */
1718 for (unsigned i = 0; i < gimple_num_ops (stmt); i++)
1720 tree op = gimple_op (stmt, i);
1721 if (!op)
1722 continue;
1723 if (TREE_CODE (op) == COMPONENT_REF)
1724 op = TREE_OPERAND (op, 0);
1726 if (TREE_CODE (op) == MEM_REF)
1728 tree arg = TREE_OPERAND (op, 0);
1730 state_t state = sm_ctxt->get_state (stmt, arg);
1731 if (unchecked_p (state))
1733 tree diag_arg = sm_ctxt->get_diagnostic_tree (arg);
1734 sm_ctxt->warn (node, stmt, arg,
1735 new possible_null_deref (*this, diag_arg));
1736 const allocation_state *astate = as_a_allocation_state (state);
1737 sm_ctxt->set_next_state (stmt, arg, astate->get_nonnull ());
1739 else if (state == m_null)
1741 tree diag_arg = sm_ctxt->get_diagnostic_tree (arg);
1742 sm_ctxt->warn (node, stmt, arg,
1743 new null_deref (*this, diag_arg));
1744 sm_ctxt->set_next_state (stmt, arg, m_stop);
1746 else if (freed_p (state))
1748 tree diag_arg = sm_ctxt->get_diagnostic_tree (arg);
1749 const allocation_state *astate = as_a_allocation_state (state);
1750 sm_ctxt->warn (node, stmt, arg,
1751 new use_after_free (*this, diag_arg,
1752 astate->m_deallocator));
1753 sm_ctxt->set_next_state (stmt, arg, m_stop);
1757 return false;
1760 /* Handle a call to an allocator.
1761 RETURNS_NONNULL is true if CALL is to a fndecl known to have
1762 __attribute__((returns_nonnull)). */
1764 void
1765 malloc_state_machine::on_allocator_call (sm_context *sm_ctxt,
1766 const gcall *call,
1767 const deallocator_set *deallocators,
1768 bool returns_nonnull) const
1770 tree lhs = gimple_call_lhs (call);
1771 if (lhs)
1773 if (sm_ctxt->get_state (call, lhs) == m_start)
1774 sm_ctxt->set_next_state (call, lhs,
1775 (returns_nonnull
1776 ? deallocators->m_nonnull
1777 : deallocators->m_unchecked));
1779 else
1781 /* TODO: report leak. */
1785 void
1786 malloc_state_machine::on_deallocator_call (sm_context *sm_ctxt,
1787 const supernode *node,
1788 const gcall *call,
1789 const deallocator *d,
1790 unsigned argno) const
1792 if (argno >= gimple_call_num_args (call))
1793 return;
1794 tree arg = gimple_call_arg (call, argno);
1796 state_t state = sm_ctxt->get_state (call, arg);
1798 /* start/unchecked/nonnull -> freed. */
1799 if (state == m_start)
1800 sm_ctxt->set_next_state (call, arg, d->m_freed);
1801 else if (unchecked_p (state) || nonnull_p (state))
1803 const allocation_state *astate = as_a_allocation_state (state);
1804 gcc_assert (astate->m_deallocators);
1805 if (!astate->m_deallocators->contains_p (d))
1807 /* Wrong allocator. */
1808 tree diag_arg = sm_ctxt->get_diagnostic_tree (arg);
1809 pending_diagnostic *pd
1810 = new mismatching_deallocation (*this, diag_arg,
1811 astate->m_deallocators,
1813 sm_ctxt->warn (node, call, arg, pd);
1815 sm_ctxt->set_next_state (call, arg, d->m_freed);
1818 /* Keep state "null" as-is, rather than transitioning to "freed";
1819 we don't want to complain about double-free of NULL. */
1820 else if (state == d->m_freed)
1822 /* freed -> stop, with warning. */
1823 tree diag_arg = sm_ctxt->get_diagnostic_tree (arg);
1824 sm_ctxt->warn (node, call, arg,
1825 new double_free (*this, diag_arg, d->m_name));
1826 sm_ctxt->set_next_state (call, arg, m_stop);
1828 else if (state == m_non_heap)
1830 /* non-heap -> stop, with warning. */
1831 tree diag_arg = sm_ctxt->get_diagnostic_tree (arg);
1832 sm_ctxt->warn (node, call, arg,
1833 new free_of_non_heap (*this, diag_arg,
1834 d->m_name));
1835 sm_ctxt->set_next_state (call, arg, m_stop);
1839 /* Implementation of realloc(3):
1841 void *realloc(void *ptr, size_t size);
1843 realloc(3) is awkward.
1845 We currently don't have a way to express multiple possible outcomes
1846 from a function call, "bifurcating" the state such as:
1847 - success: non-NULL is returned
1848 - failure: NULL is returned, existing buffer is not freed.
1849 or even an N-way state split e.g.:
1850 - buffer grew successfully in-place
1851 - buffer was successfully moved to a larger allocation
1852 - buffer was successfully contracted
1853 - realloc failed, returning NULL, without freeing existing buffer.
1854 (PR analyzer/99260 tracks this)
1856 Given that we can currently only express one outcome, eliminate
1857 false positives by dropping state from the buffer. */
1859 void
1860 malloc_state_machine::on_realloc_call (sm_context *sm_ctxt,
1861 const supernode *node ATTRIBUTE_UNUSED,
1862 const gcall *call) const
1864 tree ptr = gimple_call_arg (call, 0);
1866 state_t state = sm_ctxt->get_state (call, ptr);
1868 /* Detect mismatches. */
1869 if (unchecked_p (state) || nonnull_p (state))
1871 const allocation_state *astate = as_a_allocation_state (state);
1872 gcc_assert (astate->m_deallocators);
1873 if (astate->m_deallocators != &m_free)
1875 /* Wrong allocator. */
1876 tree diag_ptr = sm_ctxt->get_diagnostic_tree (ptr);
1877 pending_diagnostic *pd
1878 = new mismatching_deallocation (*this, diag_ptr,
1879 astate->m_deallocators,
1880 &m_realloc);
1881 sm_ctxt->warn (node, call, ptr, pd);
1885 /* Transition ptr to "stop" state. */
1886 sm_ctxt->set_next_state (call, ptr, m_stop);
1889 /* Implementation of state_machine::on_phi vfunc for malloc_state_machine. */
1891 void
1892 malloc_state_machine::on_phi (sm_context *sm_ctxt,
1893 const supernode *node ATTRIBUTE_UNUSED,
1894 const gphi *phi,
1895 tree rhs) const
1897 if (zerop (rhs))
1899 tree lhs = gimple_phi_result (phi);
1900 on_zero_assignment (sm_ctxt, phi, lhs);
1904 /* Implementation of state_machine::on_condition vfunc for malloc_state_machine.
1905 Potentially transition state 'unchecked' to 'nonnull' or to 'null'. */
1907 void
1908 malloc_state_machine::on_condition (sm_context *sm_ctxt,
1909 const supernode *node ATTRIBUTE_UNUSED,
1910 const gimple *stmt,
1911 const svalue *lhs,
1912 enum tree_code op,
1913 const svalue *rhs) const
1915 if (!rhs->all_zeroes_p ())
1916 return;
1918 if (!any_pointer_p (lhs))
1919 return;
1920 if (!any_pointer_p (rhs))
1921 return;
1923 if (op == NE_EXPR)
1925 log ("got 'ARG != 0' match");
1926 state_t s = sm_ctxt->get_state (stmt, lhs);
1927 if (unchecked_p (s))
1929 const allocation_state *astate = as_a_allocation_state (s);
1930 sm_ctxt->set_next_state (stmt, lhs, astate->get_nonnull ());
1933 else if (op == EQ_EXPR)
1935 log ("got 'ARG == 0' match");
1936 state_t s = sm_ctxt->get_state (stmt, lhs);
1937 if (unchecked_p (s))
1938 sm_ctxt->set_next_state (stmt, lhs, m_null);
1942 /* Implementation of state_machine::can_purge_p vfunc for malloc_state_machine.
1943 Don't allow purging of pointers in state 'unchecked' or 'nonnull'
1944 (to avoid false leak reports). */
1946 bool
1947 malloc_state_machine::can_purge_p (state_t s) const
1949 enum resource_state rs = get_rs (s);
1950 return rs != RS_UNCHECKED && rs != RS_NONNULL;
1953 /* Implementation of state_machine::on_leak vfunc for malloc_state_machine
1954 (for complaining about leaks of pointers in state 'unchecked' and
1955 'nonnull'). */
1957 pending_diagnostic *
1958 malloc_state_machine::on_leak (tree var) const
1960 return new malloc_leak (*this, var);
1963 /* Implementation of state_machine::reset_when_passed_to_unknown_fn_p vfunc
1964 for malloc_state_machine. */
1966 bool
1967 malloc_state_machine::reset_when_passed_to_unknown_fn_p (state_t s,
1968 bool is_mutable) const
1970 /* An on-stack ptr doesn't stop being stack-allocated when passed to an
1971 unknown fn. */
1972 if (s == m_non_heap)
1973 return false;
1975 /* Otherwise, pointers passed as non-const can be freed. */
1976 return is_mutable;
1979 /* Return true if calls to FNDECL are known to not affect this sm-state. */
1981 bool
1982 malloc_state_machine::unaffected_by_call_p (tree fndecl)
1984 /* A set of functions that are known to not affect allocation
1985 status, even if we haven't fully modelled the rest of their
1986 behavior yet. */
1987 static const char * const funcnames[] = {
1988 /* This array must be kept sorted. */
1989 "strsep",
1991 const size_t count
1992 = sizeof(funcnames) / sizeof (funcnames[0]);
1993 function_set fs (funcnames, count);
1995 if (fs.contains_decl_p (fndecl))
1996 return true;
1998 return false;
2001 /* Shared logic for handling GIMPLE_ASSIGNs and GIMPLE_PHIs that
2002 assign zero to LHS. */
2004 void
2005 malloc_state_machine::on_zero_assignment (sm_context *sm_ctxt,
2006 const gimple *stmt,
2007 tree lhs) const
2009 state_t s = sm_ctxt->get_state (stmt, lhs);
2010 enum resource_state rs = get_rs (s);
2011 if (rs == RS_START
2012 || rs == RS_UNCHECKED
2013 || rs == RS_NONNULL
2014 || rs == RS_FREED)
2015 sm_ctxt->set_next_state (stmt, lhs, m_null);
2018 } // anonymous namespace
2020 /* Internal interface to this file. */
2022 state_machine *
2023 make_malloc_state_machine (logger *logger)
2025 return new malloc_state_machine (logger);
2028 } // namespace ana
2030 #endif /* #if ENABLE_ANALYZER */