1 /* Classes for managing a directed graph of <point, state> pairs.
2 Copyright (C) 2019-2020 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/>. */
21 #ifndef GCC_ANALYZER_EXPLODED_GRAPH_H
22 #define GCC_ANALYZER_EXPLODED_GRAPH_H
26 /* Concrete implementation of region_model_context, wiring it up to the
27 rest of the analysis engine. */
29 class impl_region_model_context
: public region_model_context
32 impl_region_model_context (exploded_graph
&eg
,
33 const exploded_node
*enode_for_diag
,
35 /* TODO: should we be getting the ECs from the
36 old state, rather than the new? */
37 const program_state
*old_state
,
38 program_state
*new_state
,
42 stmt_finder
*stmt_finder
= NULL
);
44 impl_region_model_context (program_state
*state
,
46 const extrinsic_state
&ext_state
);
48 void warn (pending_diagnostic
*d
) FINAL OVERRIDE
;
50 void remap_svalue_ids (const svalue_id_map
&map
) FINAL OVERRIDE
;
52 int on_svalue_purge (svalue_id first_unused_sid
,
53 const svalue_id_map
&map
) FINAL OVERRIDE
;
55 logger
*get_logger () FINAL OVERRIDE
57 return m_logger
.get_logger ();
60 void on_state_leak (const state_machine
&sm
,
63 svalue_id first_unused_sid
,
64 const svalue_id_map
&map
,
65 state_machine::state_t state
);
67 void on_inherited_svalue (svalue_id parent_sid
,
68 svalue_id child_sid
) FINAL OVERRIDE
;
70 void on_cast (svalue_id src_sid
,
71 svalue_id dst_sid
) FINAL OVERRIDE
;
73 void on_condition (tree lhs
, enum tree_code op
, tree rhs
) FINAL OVERRIDE
;
75 void on_unknown_change (svalue_id sid ATTRIBUTE_UNUSED
) FINAL OVERRIDE
;
77 void on_phi (const gphi
*phi
, tree rhs
) FINAL OVERRIDE
;
79 void on_unknown_tree_code (path_var pv
,
80 const dump_location_t
&loc
) FINAL OVERRIDE
;
84 const exploded_node
*m_enode_for_diag
;
85 const program_state
*m_old_state
;
86 program_state
*m_new_state
;
87 state_change
*m_change
;
89 stmt_finder
*m_stmt_finder
;
90 const extrinsic_state
&m_ext_state
;
93 /* A <program_point, program_state> pair, used internally by
94 exploded_node as its immutable data, and as a key for identifying
95 exploded_nodes we've already seen in the graph. */
100 point_and_state (const program_point
&point
,
101 const program_state
&state
)
104 m_hash (m_point
.hash () ^ m_state
.hash ())
106 /* We shouldn't be building point_and_states and thus exploded_nodes
107 for states that aren't valid. */
108 gcc_assert (state
.m_valid
);
111 hashval_t
hash () const
115 bool operator== (const point_and_state
&other
) const
117 return m_point
== other
.m_point
&& m_state
== other
.m_state
;
120 const program_point
&get_point () const { return m_point
; }
121 const program_state
&get_state () const { return m_state
; }
123 void set_state (const program_state
&state
)
126 m_hash
= m_point
.hash () ^ m_state
.hash ();
129 void validate (const extrinsic_state
&ext_state
) const;
132 program_point m_point
;
133 program_state m_state
;
137 /* A traits class for exploded graphs and their nodes and edges. */
141 typedef exploded_node node_t
;
142 typedef exploded_edge edge_t
;
143 typedef exploded_graph graph_t
;
146 dump_args_t (const exploded_graph
&eg
) : m_eg (eg
) {}
147 const exploded_graph
&m_eg
;
149 typedef exploded_cluster cluster_t
;
152 /* An exploded_node is a unique, immutable <point, state> pair within the
154 Each exploded_node has a unique index within the graph
155 (for ease of debugging). */
157 class exploded_node
: public dnode
<eg_traits
>
160 /* Has this enode had exploded_graph::process_node called on it?
161 This allows us to distinguish enodes that were merged during
162 worklist-handling, and thus never had process_node called on them
163 (in favor of processing the merged node). */
166 /* Node is in the worklist. */
169 /* Node has had exploded_graph::process_node called on it. */
172 /* Node was left unprocessed due to merger; it won't have had
173 exploded_graph::process_node called on it. */
177 exploded_node (point_and_state ps
,
179 : m_ps (ps
), m_status (STATUS_WORKLIST
), m_index (index
)
181 gcc_checking_assert (ps
.get_state ().m_region_model
->canonicalized_p ());
184 hashval_t
hash () const { return m_ps
.hash (); }
186 void dump_dot (graphviz_out
*gv
, const dump_args_t
&args
)
187 const FINAL OVERRIDE
;
188 void dump_dot_id (pretty_printer
*pp
) const;
190 void dump_to_pp (pretty_printer
*pp
, const extrinsic_state
&ext_state
) const;
191 void dump (FILE *fp
, const extrinsic_state
&ext_state
) const;
192 void dump (const extrinsic_state
&ext_state
) const;
194 /* The result of on_stmt. */
197 on_stmt_flags (bool sm_changes
)
198 : m_sm_changes (sm_changes
),
199 m_terminate_path (false)
202 static on_stmt_flags
terminate_path ()
204 return on_stmt_flags (true, true);
207 static on_stmt_flags
state_change (bool any_sm_changes
)
209 return on_stmt_flags (any_sm_changes
, false);
212 /* Did any sm-changes occur handling the stmt. */
213 bool m_sm_changes
: 1;
215 /* Should we stop analyzing this path (on_stmt may have already
216 added nodes/edges, e.g. when handling longjmp). */
217 bool m_terminate_path
: 1;
220 on_stmt_flags (bool sm_changes
,
222 : m_sm_changes (sm_changes
),
223 m_terminate_path (terminate_path
)
227 on_stmt_flags
on_stmt (exploded_graph
&eg
,
228 const supernode
*snode
,
230 program_state
*state
,
231 state_change
*change
) const;
232 bool on_edge (exploded_graph
&eg
,
233 const superedge
*succ
,
234 program_point
*next_point
,
235 program_state
*next_state
,
236 state_change
*change
) const;
237 void on_longjmp (exploded_graph
&eg
,
239 program_state
*new_state
,
240 region_model_context
*ctxt
) const;
242 void detect_leaks (exploded_graph
&eg
) const;
244 const program_point
&get_point () const { return m_ps
.get_point (); }
245 const supernode
*get_supernode () const
247 return get_point ().get_supernode ();
249 function
*get_function () const
251 return get_point ().get_function ();
253 int get_stack_depth () const
255 return get_point ().get_stack_depth ();
257 const gimple
*get_stmt () const { return get_point ().get_stmt (); }
259 const program_state
&get_state () const { return m_ps
.get_state (); }
261 const point_and_state
*get_ps_key () const { return &m_ps
; }
262 const program_point
*get_point_key () const { return &m_ps
.get_point (); }
264 void dump_succs_and_preds (FILE *outf
) const;
266 enum status
get_status () const { return m_status
; }
267 void set_status (enum status status
)
269 gcc_assert (m_status
== STATUS_WORKLIST
);
274 DISABLE_COPY_AND_ASSIGN (exploded_node
);
276 const char * get_dot_fillcolor () const;
278 /* The <program_point, program_state> pair. This is const, as it
279 is immutable once the exploded_node has been created. */
280 const point_and_state m_ps
;
282 enum status m_status
;
285 /* The index of this exploded_node. */
289 /* An edge within the exploded graph.
290 Some exploded_edges have an underlying superedge; others don't. */
292 class exploded_edge
: public dedge
<eg_traits
>
295 /* Abstract base class for associating custom data with an
296 exploded_edge, for handling non-standard edges such as
297 rewinding from a longjmp, signal handlers, etc. */
301 virtual ~custom_info_t () {}
303 /* Hook for making .dot label more readable . */
304 virtual void print (pretty_printer
*pp
) = 0;
306 /* Hook for updating MODEL within exploded_path::feasible_p. */
307 virtual void update_model (region_model
*model
,
308 const exploded_edge
&eedge
) = 0;
310 virtual void add_events_to_path (checker_path
*emission_path
,
311 const exploded_edge
&eedge
) = 0;
314 exploded_edge (exploded_node
*src
, exploded_node
*dest
,
315 const extrinsic_state
&ext_state
,
316 const superedge
*sedge
,
317 const state_change
&change
,
318 custom_info_t
*custom_info
);
320 void dump_dot (graphviz_out
*gv
, const dump_args_t
&args
)
321 const FINAL OVERRIDE
;
324 const superedge
*const m_sedge
;
326 const state_change m_change
;
328 /* NULL for most edges; will be non-NULL for special cases
329 such as an unwind from a longjmp to a setjmp, or when
330 a signal is delivered to a signal-handler.
332 Owned by this class. */
333 custom_info_t
*m_custom_info
;
336 DISABLE_COPY_AND_ASSIGN (exploded_edge
);
339 /* Extra data for an exploded_edge that represents a rewind from a
340 longjmp to a setjmp (or from a siglongjmp to a sigsetjmp). */
342 class rewind_info_t
: public exploded_edge::custom_info_t
345 rewind_info_t (const setjmp_record
&setjmp_record
,
346 const gcall
*longjmp_call
)
347 : m_setjmp_record (setjmp_record
),
348 m_longjmp_call (longjmp_call
)
351 void print (pretty_printer
*pp
) FINAL OVERRIDE
353 pp_string (pp
, "rewind");
356 void update_model (region_model
*model
,
357 const exploded_edge
&eedge
) FINAL OVERRIDE
;
359 void add_events_to_path (checker_path
*emission_path
,
360 const exploded_edge
&eedge
) FINAL OVERRIDE
;
362 const program_point
&get_setjmp_point () const
364 const program_point
&origin_point
= get_enode_origin ()->get_point ();
366 /* "origin_point" ought to be before the call to "setjmp". */
367 gcc_assert (origin_point
.get_kind () == PK_BEFORE_STMT
);
369 /* TODO: assert that it's the final stmt in its supernode. */
374 const gcall
*get_setjmp_call () const
376 return m_setjmp_record
.m_setjmp_call
;
379 const gcall
*get_longjmp_call () const
381 return m_longjmp_call
;
384 const exploded_node
*get_enode_origin () const
386 return m_setjmp_record
.m_enode
;
390 setjmp_record m_setjmp_record
;
391 const gcall
*m_longjmp_call
;
394 /* Statistics about aspects of an exploded_graph. */
398 stats (int num_supernodes
);
399 void log (logger
*logger
) const;
400 void dump (FILE *out
) const;
402 int m_num_nodes
[NUM_POINT_KINDS
];
403 int m_node_reuse_count
;
404 int m_node_reuse_after_merge_count
;
405 int m_num_supernodes
;
408 /* Traits class for ensuring uniqueness of point_and_state data within
409 an exploded_graph. */
411 struct eg_hash_map_traits
413 typedef const point_and_state
*key_type
;
414 typedef exploded_node
*value_type
;
415 typedef exploded_node
*compare_type
;
417 static inline hashval_t
hash (const key_type
&k
)
419 gcc_assert (k
!= NULL
);
420 gcc_assert (k
!= reinterpret_cast<key_type
> (1));
423 static inline bool equal_keys (const key_type
&k1
, const key_type
&k2
)
425 gcc_assert (k1
!= NULL
);
426 gcc_assert (k2
!= NULL
);
427 gcc_assert (k1
!= reinterpret_cast<key_type
> (1));
428 gcc_assert (k2
!= reinterpret_cast<key_type
> (1));
432 /* Otherwise they must both be non-NULL. */
435 template <typename T
>
436 static inline void remove (T
&)
438 /* empty; the nodes are handled elsewhere. */
440 template <typename T
>
441 static inline void mark_deleted (T
&entry
)
443 entry
.m_key
= reinterpret_cast<key_type
> (1);
445 template <typename T
>
446 static inline void mark_empty (T
&entry
)
450 template <typename T
>
451 static inline bool is_deleted (const T
&entry
)
453 return entry
.m_key
== reinterpret_cast<key_type
> (1);
455 template <typename T
>
456 static inline bool is_empty (const T
&entry
)
458 return entry
.m_key
== NULL
;
460 static const bool empty_zero_p
= false;
463 /* Per-program_point data for an exploded_graph. */
465 struct per_program_point_data
467 per_program_point_data (const program_point
&key
)
471 const program_point m_key
;
472 auto_vec
<exploded_node
*> m_enodes
;
475 /* Traits class for storing per-program_point data within
476 an exploded_graph. */
478 struct eg_point_hash_map_traits
480 typedef const program_point
*key_type
;
481 typedef per_program_point_data
*value_type
;
482 typedef per_program_point_data
*compare_type
;
484 static inline hashval_t
hash (const key_type
&k
)
486 gcc_assert (k
!= NULL
);
487 gcc_assert (k
!= reinterpret_cast<key_type
> (1));
490 static inline bool equal_keys (const key_type
&k1
, const key_type
&k2
)
492 gcc_assert (k1
!= NULL
);
493 gcc_assert (k2
!= NULL
);
494 gcc_assert (k1
!= reinterpret_cast<key_type
> (1));
495 gcc_assert (k2
!= reinterpret_cast<key_type
> (1));
499 /* Otherwise they must both be non-NULL. */
502 template <typename T
>
503 static inline void remove (T
&)
505 /* empty; the nodes are handled elsewhere. */
507 template <typename T
>
508 static inline void mark_deleted (T
&entry
)
510 entry
.m_key
= reinterpret_cast<key_type
> (1);
512 template <typename T
>
513 static inline void mark_empty (T
&entry
)
517 template <typename T
>
518 static inline bool is_deleted (const T
&entry
)
520 return entry
.m_key
== reinterpret_cast<key_type
> (1);
522 template <typename T
>
523 static inline bool is_empty (const T
&entry
)
525 return entry
.m_key
== NULL
;
527 static const bool empty_zero_p
= false;
530 /* Data about a particular call_string within an exploded_graph. */
532 struct per_call_string_data
534 per_call_string_data (const call_string
&key
, int num_supernodes
)
535 : m_key (key
), m_stats (num_supernodes
)
538 const call_string m_key
;
542 /* Traits class for storing per-call_string data within
543 an exploded_graph. */
545 struct eg_call_string_hash_map_traits
547 typedef const call_string
*key_type
;
548 typedef per_call_string_data
*value_type
;
549 typedef per_call_string_data
*compare_type
;
551 static inline hashval_t
hash (const key_type
&k
)
553 gcc_assert (k
!= NULL
);
554 gcc_assert (k
!= reinterpret_cast<key_type
> (1));
557 static inline bool equal_keys (const key_type
&k1
, const key_type
&k2
)
559 gcc_assert (k1
!= NULL
);
560 gcc_assert (k2
!= NULL
);
561 gcc_assert (k1
!= reinterpret_cast<key_type
> (1));
562 gcc_assert (k2
!= reinterpret_cast<key_type
> (1));
566 /* Otherwise they must both be non-NULL. */
569 template <typename T
>
570 static inline void remove (T
&)
572 /* empty; the nodes are handled elsewhere. */
574 template <typename T
>
575 static inline void mark_deleted (T
&entry
)
577 entry
.m_key
= reinterpret_cast<key_type
> (1);
579 template <typename T
>
580 static inline void mark_empty (T
&entry
)
584 template <typename T
>
585 static inline bool is_deleted (const T
&entry
)
587 return entry
.m_key
== reinterpret_cast<key_type
> (1);
589 template <typename T
>
590 static inline bool is_empty (const T
&entry
)
592 return entry
.m_key
== NULL
;
594 static const bool empty_zero_p
= false;
597 /* Data about a particular function within an exploded_graph. */
599 struct per_function_data
601 per_function_data () {}
603 void add_call_summary (exploded_node
*node
)
605 m_summaries
.safe_push (node
);
608 auto_vec
<exploded_node
*> m_summaries
;
612 /* The strongly connected components of a supergraph.
613 In particular, this allows us to compute a partial ordering
616 class strongly_connected_components
619 strongly_connected_components (const supergraph
&sg
, logger
*logger
);
621 int get_scc_id (int node_index
) const
623 return m_per_node
[node_index
].m_lowlink
;
632 : m_index (-1), m_lowlink (-1), m_on_stack (false)
640 void strong_connect (unsigned index
);
642 const supergraph
&m_sg
;
643 auto_vec
<unsigned> m_stack
;
644 auto_vec
<per_node_data
> m_per_node
;
647 /* The worklist of exploded_node instances that have been added to
648 an exploded_graph, but that haven't yet been processed to find
649 their successors (or warnings).
651 The enodes are stored in a priority queue, ordered by a topological
652 sort of the SCCs in the supergraph, so that enodes for the same
653 program_point should appear at the front of the queue together.
654 This allows for state-merging at CFG join-points, so that
655 sufficiently-similar enodes can be merged into one. */
660 worklist (const exploded_graph
&eg
, const analysis_plan
&plan
);
661 unsigned length () const;
662 exploded_node
*take_next ();
663 exploded_node
*peek_next ();
664 void add_node (exploded_node
*enode
);
670 key_t (const worklist
&w
, exploded_node
*enode
)
671 : m_worklist (w
), m_enode (enode
)
674 bool operator< (const key_t
&other
) const
676 return cmp (*this, other
) < 0;
679 bool operator== (const key_t
&other
) const
681 return cmp (*this, other
) == 0;
684 bool operator> (const key_t
&other
) const
686 return !(*this == other
|| *this < other
);
690 static int cmp (const key_t
&ka
, const key_t
&kb
);
692 int get_scc_id (const exploded_node
*enode
) const
694 const supernode
*snode
= enode
->get_supernode ();
697 return m_worklist
.m_scc
.get_scc_id (snode
->m_index
);
700 const worklist
&m_worklist
;
701 exploded_node
*m_enode
;
704 /* The order is important here: m_scc needs to stick around
705 until after m_queue has finished being cleaned up (the dtor
706 calls the ordering fns). */
707 const exploded_graph
&m_eg
;
708 strongly_connected_components m_scc
;
709 const analysis_plan
&m_plan
;
711 /* Priority queue, backed by a fibonacci_heap. */
712 typedef fibonacci_heap
<key_t
, exploded_node
> queue_t
;
716 /* An exploded_graph is a directed graph of unique <point, state> pairs.
717 It also has a worklist of nodes that are waiting for their successors
718 to be added to the graph. */
720 class exploded_graph
: public digraph
<eg_traits
>
723 typedef hash_map
<const call_string
*, per_call_string_data
*,
724 eg_call_string_hash_map_traits
> call_string_data_map_t
;
726 exploded_graph (const supergraph
&sg
, logger
*logger
,
727 const extrinsic_state
&ext_state
,
728 const state_purge_map
*purge_map
,
729 const analysis_plan
&plan
,
733 logger
*get_logger () const { return m_logger
.get_logger (); }
735 const supergraph
&get_supergraph () const { return m_sg
; }
736 const extrinsic_state
&get_ext_state () const { return m_ext_state
; }
737 const state_purge_map
*get_purge_map () const { return m_purge_map
; }
738 const analysis_plan
&get_analysis_plan () const { return m_plan
; }
740 exploded_node
*get_origin () const { return m_origin
; }
742 exploded_node
*add_function_entry (function
*fun
);
744 void build_initial_worklist ();
745 void process_worklist ();
746 void process_node (exploded_node
*node
);
748 exploded_node
*get_or_create_node (const program_point
&point
,
749 const program_state
&state
,
750 state_change
*change
);
751 exploded_edge
*add_edge (exploded_node
*src
, exploded_node
*dest
,
752 const superedge
*sedge
,
753 const state_change
&change
,
754 exploded_edge::custom_info_t
*custom
= NULL
);
756 per_program_point_data
*
757 get_or_create_per_program_point_data (const program_point
&);
759 per_call_string_data
*
760 get_or_create_per_call_string_data (const call_string
&);
763 get_or_create_per_function_data (function
*);
764 per_function_data
*get_per_function_data (function
*) const;
766 void save_diagnostic (const state_machine
&sm
,
767 const exploded_node
*enode
,
768 const supernode
*node
, const gimple
*stmt
,
770 tree var
, state_machine::state_t state
,
771 pending_diagnostic
*d
);
773 diagnostic_manager
&get_diagnostic_manager ()
775 return m_diagnostic_manager
;
777 const diagnostic_manager
&get_diagnostic_manager () const
779 return m_diagnostic_manager
;
782 stats
*get_global_stats () { return &m_global_stats
; }
783 stats
*get_or_create_function_stats (function
*fn
);
784 void log_stats () const;
785 void dump_stats (FILE *) const;
786 void dump_states_for_supernode (FILE *, const supernode
*snode
) const;
787 void dump_exploded_nodes () const;
789 const call_string_data_map_t
*get_per_call_string_data () const
790 { return &m_per_call_string_data
; }
793 DISABLE_COPY_AND_ASSIGN (exploded_graph
);
795 const supergraph
&m_sg
;
799 /* Map from point/state to exploded node.
800 To avoid duplication we store point_and_state
801 *pointers* as keys, rather than point_and_state, using the
802 instance from within the exploded_node, with a custom hasher. */
803 typedef hash_map
<const point_and_state
*, exploded_node
*,
804 eg_hash_map_traits
> map_t
;
805 map_t m_point_and_state_to_node
;
807 /* Map from program_point to per-program_point data. */
808 typedef hash_map
<const program_point
*, per_program_point_data
*,
809 eg_point_hash_map_traits
> point_map_t
;
810 point_map_t m_per_point_data
;
814 exploded_node
*m_origin
;
816 const extrinsic_state
&m_ext_state
;
818 const state_purge_map
*const m_purge_map
;
820 const analysis_plan
&m_plan
;
822 typedef hash_map
<function
*, per_function_data
*> per_function_data_t
;
823 per_function_data_t m_per_function_data
;
825 diagnostic_manager m_diagnostic_manager
;
828 stats m_global_stats
;
829 typedef ordered_hash_map
<function
*, stats
*> function_stat_map_t
;
830 function_stat_map_t m_per_function_stats
;
831 stats m_functionless_stats
;
833 call_string_data_map_t m_per_call_string_data
;
835 auto_vec
<int> m_PK_AFTER_SUPERNODE_per_snode
;
838 /* A path within an exploded_graph: a sequence of edges. */
843 exploded_path () : m_edges () {}
844 exploded_path (const exploded_path
&other
);
845 exploded_path
& operator= (const exploded_path
&other
);
847 unsigned length () const { return m_edges
.length (); }
849 bool find_stmt_backwards (const gimple
*search_stmt
,
852 exploded_node
*get_final_enode () const;
854 void dump_to_pp (pretty_printer
*pp
) const;
855 void dump (FILE *fp
) const;
858 bool feasible_p (logger
*logger
) const;
860 auto_vec
<const exploded_edge
*> m_edges
;
863 /* Finding the shortest exploded_path within an exploded_graph. */
865 typedef shortest_paths
<eg_traits
, exploded_path
> shortest_exploded_paths
;
867 /* Abstract base class for use when passing NULL as the stmt for
868 a possible warning, allowing the choice of stmt to be deferred
869 until after we have an emission path (and know we're emitting a
875 virtual ~stmt_finder () {}
876 virtual stmt_finder
*clone () const = 0;
877 virtual const gimple
*find_stmt (const exploded_path
&epath
) = 0;
880 // TODO: split the above up?
884 #endif /* GCC_ANALYZER_EXPLODED_GRAPH_H */