Branch data Line data Source code
1 : : /* Classes for managing a directed graph of <point, state> pairs.
2 : : Copyright (C) 2019-2025 Free Software Foundation, Inc.
3 : : Contributed by David Malcolm <dmalcolm@redhat.com>.
4 : :
5 : : This file is part of GCC.
6 : :
7 : : GCC is free software; you can redistribute it and/or modify it
8 : : under the terms of the GNU General Public License as published by
9 : : the Free Software Foundation; either version 3, or (at your option)
10 : : any later version.
11 : :
12 : : GCC is distributed in the hope that it will be useful, but
13 : : WITHOUT ANY WARRANTY; without even the implied warranty of
14 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 : : General Public License for more details.
16 : :
17 : : You should have received a copy of the GNU General Public License
18 : : along with GCC; see the file COPYING3. If not see
19 : : <http://www.gnu.org/licenses/>. */
20 : :
21 : : #ifndef GCC_ANALYZER_EXPLODED_GRAPH_H
22 : : #define GCC_ANALYZER_EXPLODED_GRAPH_H
23 : :
24 : : #include "alloc-pool.h"
25 : : #include "fibonacci_heap.h"
26 : : #include "supergraph.h"
27 : : #include "sbitmap.h"
28 : : #include "shortest-paths.h"
29 : : #include "analyzer/sm.h"
30 : : #include "analyzer/program-state.h"
31 : : #include "analyzer/diagnostic-manager.h"
32 : :
33 : : namespace ana {
34 : :
35 : : /* Concrete implementation of region_model_context, wiring it up to the
36 : : rest of the analysis engine. */
37 : :
38 : 1417957 : class impl_region_model_context : public region_model_context
39 : : {
40 : : public:
41 : : impl_region_model_context (exploded_graph &eg,
42 : : exploded_node *enode_for_diag,
43 : :
44 : : /* TODO: should we be getting the ECs from the
45 : : old state, rather than the new? */
46 : : const program_state *old_state,
47 : : program_state *new_state,
48 : : uncertainty_t *uncertainty,
49 : : path_context *path_ctxt,
50 : :
51 : : const gimple *stmt,
52 : : stmt_finder *stmt_finder = NULL,
53 : :
54 : : bool *out_could_have_done_work = nullptr);
55 : :
56 : : impl_region_model_context (program_state *state,
57 : : const extrinsic_state &ext_state,
58 : : uncertainty_t *uncertainty,
59 : : logger *logger = NULL);
60 : :
61 : : bool warn (std::unique_ptr<pending_diagnostic> d,
62 : : const stmt_finder *custom_finder = NULL) final override;
63 : : void add_note (std::unique_ptr<pending_note> pn) final override;
64 : : void add_event (std::unique_ptr<checker_event> event) final override;
65 : : void on_svalue_leak (const svalue *) override;
66 : : void on_liveness_change (const svalue_set &live_svalues,
67 : : const region_model *model) final override;
68 : 695941 : logger *get_logger () final override
69 : : {
70 : 5973 : return m_logger.get_logger ();
71 : : }
72 : :
73 : : void on_state_leak (const state_machine &sm,
74 : : const svalue *sval,
75 : : state_machine::state_t state);
76 : :
77 : : void on_condition (const svalue *lhs,
78 : : enum tree_code op,
79 : : const svalue *rhs) final override;
80 : :
81 : : void on_bounded_ranges (const svalue &sval,
82 : : const bounded_ranges &ranges) final override;
83 : :
84 : : void on_pop_frame (const frame_region *frame_reg) final override;
85 : :
86 : : void on_unknown_change (const svalue *sval, bool is_mutable) final override;
87 : :
88 : : void on_phi (const gphi *phi, tree rhs) final override;
89 : :
90 : : void on_unexpected_tree_code (tree t,
91 : : const dump_location_t &loc) final override;
92 : :
93 : : void on_escaped_function (tree fndecl) final override;
94 : :
95 : : uncertainty_t *get_uncertainty () final override;
96 : :
97 : : void purge_state_involving (const svalue *sval) final override;
98 : :
99 : : void bifurcate (std::unique_ptr<custom_edge_info> info) final override;
100 : : void terminate_path () final override;
101 : 999381 : const extrinsic_state *get_ext_state () const final override
102 : : {
103 : 999381 : return &m_ext_state;
104 : : }
105 : : bool
106 : : get_state_map_by_name (const char *name,
107 : : sm_state_map **out_smap,
108 : : const state_machine **out_sm,
109 : : unsigned *out_sm_idx,
110 : : std::unique_ptr<sm_context> *out_sm_context) override;
111 : :
112 : 361479 : const gimple *get_stmt () const override { return m_stmt; }
113 : 0 : const exploded_graph *get_eg () const override { return m_eg; }
114 : :
115 : : void maybe_did_work () override;
116 : 44241 : bool checking_for_infinite_loop_p () const override { return false; }
117 : 0 : void on_unusable_in_infinite_loop () override {}
118 : :
119 : : exploded_graph *m_eg;
120 : : log_user m_logger;
121 : : exploded_node *m_enode_for_diag;
122 : : const program_state *m_old_state;
123 : : program_state *m_new_state;
124 : : const gimple *m_stmt;
125 : : stmt_finder *m_stmt_finder;
126 : : const extrinsic_state &m_ext_state;
127 : : uncertainty_t *m_uncertainty;
128 : : path_context *m_path_ctxt;
129 : : bool *m_out_could_have_done_work;
130 : : };
131 : :
132 : : /* A <program_point, program_state> pair, used internally by
133 : : exploded_node as its immutable data, and as a key for identifying
134 : : exploded_nodes we've already seen in the graph. */
135 : :
136 : 764586 : class point_and_state
137 : : {
138 : : public:
139 : 385557 : point_and_state (const program_point &point,
140 : : const program_state &state)
141 : 385557 : : m_point (point),
142 : 385557 : m_state (state),
143 : 385557 : m_hash (m_point.hash () ^ m_state.hash ())
144 : : {
145 : : /* We shouldn't be building point_and_states and thus exploded_nodes
146 : : for states that aren't valid. */
147 : 385557 : gcc_assert (state.m_valid);
148 : 385557 : }
149 : :
150 : 5672785 : hashval_t hash () const
151 : : {
152 : 5672785 : return m_hash;
153 : : }
154 : 5395997 : bool operator== (const point_and_state &other) const
155 : : {
156 : 5395997 : return m_point == other.m_point && m_state == other.m_state;
157 : : }
158 : :
159 : 428608 : const program_point &get_point () const { return m_point; }
160 : 564428 : const program_state &get_state () const { return m_state; }
161 : :
162 : 46271 : void set_state (const program_state &state)
163 : : {
164 : 46271 : m_state = state;
165 : 46271 : m_hash = m_point.hash () ^ m_state.hash ();
166 : 46271 : }
167 : :
168 : : void validate (const extrinsic_state &ext_state) const;
169 : :
170 : : private:
171 : : program_point m_point;
172 : : program_state m_state;
173 : : hashval_t m_hash;
174 : : };
175 : :
176 : : /* A traits class for exploded graphs and their nodes and edges. */
177 : :
178 : : struct eg_traits
179 : : {
180 : : typedef exploded_node node_t;
181 : : typedef exploded_edge edge_t;
182 : : typedef exploded_graph graph_t;
183 : : struct dump_args_t
184 : : {
185 : 20 : dump_args_t (const exploded_graph &eg) : m_eg (eg) {}
186 : :
187 : : bool show_enode_details_p (const exploded_node &enode) const;
188 : :
189 : : virtual void
190 : 477 : dump_extra_info (const exploded_node *, pretty_printer *) const {}
191 : :
192 : : const exploded_graph &m_eg;
193 : : };
194 : : typedef exploded_cluster cluster_t;
195 : : };
196 : :
197 : : /* An exploded_node is a unique, immutable <point, state> pair within the
198 : : exploded_graph.
199 : : Each exploded_node has a unique index within the graph
200 : : (for ease of debugging). */
201 : :
202 : : class exploded_node : public dnode<eg_traits>
203 : : {
204 : : public:
205 : : /* Has this enode had exploded_graph::process_node called on it?
206 : : This allows us to distinguish enodes that were merged during
207 : : worklist-handling, and thus never had process_node called on them
208 : : (in favor of processing the merged node). */
209 : : enum class status
210 : : {
211 : : /* Node is in the worklist. */
212 : : worklist,
213 : :
214 : : /* Node has had exploded_graph::process_node called on it. */
215 : : processed,
216 : :
217 : : /* Node was excluded from worklist on creation.
218 : : e.g. for handling exception-unwinding. */
219 : : special,
220 : :
221 : : /* Node was left unprocessed due to merger; it won't have had
222 : : exploded_graph::process_node called on it. */
223 : : merger,
224 : :
225 : : /* Node was processed by maybe_process_run_of_before_supernode_enodes. */
226 : : bulk_merged
227 : : };
228 : : static const char * status_to_str (enum status s);
229 : :
230 : : exploded_node (const point_and_state &ps, int index);
231 : :
232 : : hashval_t hash () const { return m_ps.hash (); }
233 : :
234 : : const char * get_dot_fillcolor () const;
235 : : void dump_dot (graphviz_out *gv, const dump_args_t &args)
236 : : const final override;
237 : : void dump_dot_id (pretty_printer *pp) const;
238 : :
239 : : void dump_to_pp (pretty_printer *pp, const extrinsic_state &ext_state) const;
240 : : void dump (FILE *fp, const extrinsic_state &ext_state) const;
241 : : void dump (const extrinsic_state &ext_state) const;
242 : :
243 : : void dump_processed_stmts (pretty_printer *pp) const;
244 : : void dump_saved_diagnostics (pretty_printer *pp) const;
245 : :
246 : : std::unique_ptr<json::object> to_json (const extrinsic_state &ext_state) const;
247 : :
248 : : /* The result of on_stmt. */
249 : : struct on_stmt_flags
250 : : {
251 : 241236 : on_stmt_flags () : m_terminate_path (false)
252 : : {}
253 : :
254 : 1672 : static on_stmt_flags terminate_path ()
255 : : {
256 : 1672 : return on_stmt_flags (true);
257 : : }
258 : :
259 : : /* Should we stop analyzing this path (on_stmt may have already
260 : : added nodes/edges, e.g. when handling longjmp). */
261 : : bool m_terminate_path : 1;
262 : :
263 : : private:
264 : 1672 : on_stmt_flags (bool terminate_path)
265 : : : m_terminate_path (terminate_path)
266 : : {}
267 : : };
268 : :
269 : : on_stmt_flags on_stmt (exploded_graph &eg,
270 : : const supernode *snode,
271 : : const gimple *stmt,
272 : : program_state *state,
273 : : uncertainty_t *uncertainty,
274 : : bool *out_could_have_done_work,
275 : : path_context *path_ctxt);
276 : : void on_stmt_pre (exploded_graph &eg,
277 : : const gimple *stmt,
278 : : program_state *state,
279 : : bool *out_terminate_path,
280 : : bool *out_unknown_side_effects,
281 : : region_model_context *ctxt);
282 : : void on_stmt_post (const gimple *stmt,
283 : : program_state *state,
284 : : bool unknown_side_effects,
285 : : region_model_context *ctxt);
286 : :
287 : : on_stmt_flags replay_call_summaries (exploded_graph &eg,
288 : : const supernode *snode,
289 : : const gcall &call_stmt,
290 : : program_state *state,
291 : : path_context *path_ctxt,
292 : : const function &called_fn,
293 : : per_function_data &called_fn_data,
294 : : region_model_context *ctxt);
295 : : void replay_call_summary (exploded_graph &eg,
296 : : const supernode *snode,
297 : : const gcall &call_stmt,
298 : : program_state *state,
299 : : path_context *path_ctxt,
300 : : const function &called_fn,
301 : : call_summary &summary,
302 : : region_model_context *ctxt);
303 : :
304 : : bool on_edge (exploded_graph &eg,
305 : : const superedge *succ,
306 : : program_point *next_point,
307 : : program_state *next_state,
308 : : uncertainty_t *uncertainty);
309 : : void on_longjmp (exploded_graph &eg,
310 : : const gcall &call,
311 : : program_state *new_state,
312 : : region_model_context *ctxt);
313 : : void on_throw (exploded_graph &eg,
314 : : const gcall &call,
315 : : program_state *new_state,
316 : : bool is_rethrow,
317 : : region_model_context *ctxt);
318 : : void on_resx (exploded_graph &eg,
319 : : const gresx &resx,
320 : : program_state *new_state,
321 : : region_model_context *ctxt);
322 : :
323 : : void detect_leaks (exploded_graph &eg);
324 : :
325 : 4239536 : const program_point &get_point () const { return m_ps.get_point (); }
326 : 2711796 : const supernode *get_supernode () const
327 : : {
328 : 1114225 : return get_point ().get_supernode ();
329 : : }
330 : 351939 : function *get_function () const
331 : : {
332 : 351921 : return get_point ().get_function ();
333 : : }
334 : 4304 : int get_stack_depth () const
335 : : {
336 : 19734 : return get_point ().get_stack_depth ();
337 : : }
338 : 179211 : const gimple *get_stmt () const { return get_point ().get_stmt (); }
339 : : const gimple *get_processed_stmt (unsigned idx) const;
340 : :
341 : 1073401 : const program_state &get_state () const { return m_ps.get_state (); }
342 : :
343 : 379029 : const point_and_state *get_ps_key () const { return &m_ps; }
344 : : const program_point *get_point_key () const { return &m_ps.get_point (); }
345 : :
346 : : void dump_succs_and_preds (FILE *outf) const;
347 : :
348 : 1450267 : enum status get_status () const { return m_status; }
349 : 378315 : void set_status (enum status s)
350 : : {
351 : 378315 : gcc_assert (m_status == status::worklist);
352 : 378315 : m_status = s;
353 : 378315 : }
354 : :
355 : 6741 : void add_diagnostic (const saved_diagnostic *sd)
356 : : {
357 : 6741 : m_saved_diagnostics.safe_push (sd);
358 : : }
359 : 489 : unsigned get_num_diagnostics () const
360 : : {
361 : 489 : return m_saved_diagnostics.length ();
362 : : }
363 : 8 : const saved_diagnostic *get_saved_diagnostic (unsigned idx) const
364 : : {
365 : 8 : return m_saved_diagnostics[idx];
366 : : }
367 : :
368 : : private:
369 : : DISABLE_COPY_AND_ASSIGN (exploded_node);
370 : :
371 : : /* The <program_point, program_state> pair. This is const, as it
372 : : is immutable once the exploded_node has been created. */
373 : : const point_and_state m_ps;
374 : :
375 : : enum status m_status;
376 : :
377 : : /* The saved_diagnostics at this enode, borrowed from the
378 : : diagnostic_manager. */
379 : : auto_vec <const saved_diagnostic *> m_saved_diagnostics;
380 : :
381 : : public:
382 : : /* The index of this exploded_node. */
383 : : const int m_index;
384 : :
385 : : /* The number of stmts that were processed when process_node was
386 : : called on this enode. */
387 : : unsigned m_num_processed_stmts;
388 : : };
389 : :
390 : : /* An edge within the exploded graph.
391 : : Some exploded_edges have an underlying superedge; others don't. */
392 : :
393 : : class exploded_edge : public dedge<eg_traits>
394 : : {
395 : : public:
396 : : exploded_edge (exploded_node *src, exploded_node *dest,
397 : : const superedge *sedge, bool could_do_work,
398 : : std::unique_ptr<custom_edge_info> custom_info);
399 : : void dump_dot (graphviz_out *gv, const dump_args_t &args)
400 : : const final override;
401 : : void dump_dot_label (pretty_printer *pp) const;
402 : :
403 : : std::unique_ptr<json::object> to_json () const;
404 : :
405 : : //private:
406 : : const superedge *const m_sedge;
407 : :
408 : : /* NULL for most edges; will be non-NULL for special cases
409 : : such as an unwind from a longjmp to a setjmp, or when
410 : : a signal is delivered to a signal-handler. */
411 : : std::unique_ptr<custom_edge_info> m_custom_info;
412 : :
413 : 18712 : bool could_do_work_p () const { return m_could_do_work_p; }
414 : :
415 : : private:
416 : : DISABLE_COPY_AND_ASSIGN (exploded_edge);
417 : :
418 : : /* For -Wanalyzer-infinite-loop.
419 : : Set to true during processing if any of the activity along
420 : : this edge is "externally-visible work" (and thus ruling this
421 : : out as being part of an infinite loop.
422 : :
423 : : For example, given:
424 : :
425 : : while (1)
426 : : do_something ();
427 : :
428 : : although it is an infinite loop, presumably the point of the
429 : : program is the loop body, and thus reporting this as an infinite
430 : : loop is likely to be unhelpful to the user. */
431 : : bool m_could_do_work_p;
432 : : };
433 : :
434 : : /* Extra data for an exploded_edge that represents dynamic call info ( calls
435 : : that doesn't have an underlying superedge representing the call ). */
436 : :
437 : : class dynamic_call_info_t : public custom_edge_info
438 : : {
439 : : public:
440 : 144 : dynamic_call_info_t (const gcall &dynamic_call,
441 : : const bool is_returning_call = false)
442 : 144 : : m_dynamic_call (dynamic_call),
443 : 67 : m_is_returning_call (is_returning_call)
444 : : {}
445 : :
446 : 0 : void print (pretty_printer *pp) const final override
447 : : {
448 : 0 : if (m_is_returning_call)
449 : 0 : pp_string (pp, "dynamic_return");
450 : : else
451 : 0 : pp_string (pp, "dynamic_call");
452 : 0 : }
453 : :
454 : : bool update_model (region_model *model,
455 : : const exploded_edge *eedge,
456 : : region_model_context *ctxt) const final override;
457 : :
458 : : void add_events_to_path (checker_path *emission_path,
459 : : const exploded_edge &eedge) const final override;
460 : : private:
461 : : const gcall &m_dynamic_call;
462 : : const bool m_is_returning_call;
463 : : };
464 : :
465 : :
466 : : /* Extra data for an exploded_edge that represents a rewind from a
467 : : longjmp to a setjmp (or from a siglongjmp to a sigsetjmp). */
468 : :
469 : 25 : class rewind_info_t : public custom_edge_info
470 : : {
471 : : public:
472 : 45 : rewind_info_t (const setjmp_record &setjmp_record,
473 : : const gcall &longjmp_call)
474 : 45 : : m_setjmp_record (setjmp_record),
475 : 25 : m_longjmp_call (longjmp_call)
476 : : {}
477 : :
478 : 0 : void print (pretty_printer *pp) const final override
479 : : {
480 : 0 : pp_string (pp, "rewind");
481 : 0 : }
482 : :
483 : : bool update_model (region_model *model,
484 : : const exploded_edge *eedge,
485 : : region_model_context *ctxt) const final override;
486 : :
487 : : void add_events_to_path (checker_path *emission_path,
488 : : const exploded_edge &eedge) const final override;
489 : :
490 : 25 : const program_point &get_setjmp_point () const
491 : : {
492 : 25 : const program_point &origin_point = get_enode_origin ()->get_point ();
493 : :
494 : : /* "origin_point" ought to be before the call to "setjmp". */
495 : 0 : gcc_assert (origin_point.get_kind () == PK_BEFORE_STMT);
496 : :
497 : : /* TODO: assert that it's the final stmt in its supernode. */
498 : :
499 : 25 : return origin_point;
500 : : }
501 : :
502 : 81 : const gcall &get_setjmp_call () const
503 : : {
504 : 70 : return *m_setjmp_record.m_setjmp_call;
505 : : }
506 : :
507 : 56 : const gcall &get_longjmp_call () const
508 : : {
509 : 56 : return m_longjmp_call;
510 : : }
511 : :
512 : 40 : const exploded_node *get_enode_origin () const
513 : : {
514 : 40 : return m_setjmp_record.m_enode;
515 : : }
516 : :
517 : : private:
518 : : setjmp_record m_setjmp_record;
519 : : const gcall &m_longjmp_call;
520 : : };
521 : :
522 : : /* Statistics about aspects of an exploded_graph. */
523 : :
524 : : struct stats
525 : : {
526 : : stats (int num_supernodes);
527 : : void log (logger *logger) const;
528 : : void dump (FILE *out) const;
529 : :
530 : : int get_total_enodes () const;
531 : :
532 : : int m_num_nodes[NUM_POINT_KINDS];
533 : : int m_node_reuse_count;
534 : : int m_node_reuse_after_merge_count;
535 : : int m_num_supernodes;
536 : : };
537 : :
538 : : /* Traits class for ensuring uniqueness of point_and_state data within
539 : : an exploded_graph. */
540 : :
541 : : struct eg_hash_map_traits
542 : : {
543 : : typedef const point_and_state *key_type;
544 : : typedef exploded_node *value_type;
545 : : typedef exploded_node *compare_type;
546 : :
547 : 5672785 : static inline hashval_t hash (const key_type &k)
548 : : {
549 : 5672785 : gcc_assert (k != NULL);
550 : 5672785 : gcc_assert (k != reinterpret_cast<key_type> (1));
551 : 5672785 : return k->hash ();
552 : : }
553 : 5395997 : static inline bool equal_keys (const key_type &k1, const key_type &k2)
554 : : {
555 : 5395997 : gcc_assert (k1 != NULL);
556 : 5395997 : gcc_assert (k2 != NULL);
557 : 5395997 : gcc_assert (k1 != reinterpret_cast<key_type> (1));
558 : 5395997 : gcc_assert (k2 != reinterpret_cast<key_type> (1));
559 : 5395997 : if (k1 && k2)
560 : 5395997 : return *k1 == *k2;
561 : : else
562 : : /* Otherwise they must both be non-NULL. */
563 : : return k1 == k2;
564 : : }
565 : : template <typename T>
566 : : static inline void remove (T &)
567 : : {
568 : : /* empty; the nodes are handled elsewhere. */
569 : : }
570 : : template <typename T>
571 : : static inline void mark_deleted (T &entry)
572 : : {
573 : : entry.m_key = reinterpret_cast<key_type> (1);
574 : : }
575 : : template <typename T>
576 : 1410126 : static inline void mark_empty (T &entry)
577 : : {
578 : 1410126 : entry.m_key = NULL;
579 : : }
580 : : template <typename T>
581 : 6411389 : static inline bool is_deleted (const T &entry)
582 : : {
583 : 6411389 : return entry.m_key == reinterpret_cast<key_type> (1);
584 : : }
585 : : template <typename T>
586 : 19984625 : static inline bool is_empty (const T &entry)
587 : : {
588 : 19984625 : return entry.m_key == NULL;
589 : : }
590 : : static const bool empty_zero_p = false;
591 : : };
592 : :
593 : : /* Per-program_point data for an exploded_graph. */
594 : :
595 : 271236 : struct per_program_point_data
596 : : {
597 : 271236 : per_program_point_data (const program_point &key)
598 : 271236 : : m_key (key), m_excess_enodes (0)
599 : : {}
600 : :
601 : : const program_point m_key;
602 : : auto_vec<exploded_node *> m_enodes;
603 : : /* The number of attempts to create an enode for this point
604 : : after exceeding --param=analyzer-max-enodes-per-program-point. */
605 : : int m_excess_enodes;
606 : : };
607 : :
608 : : /* Traits class for storing per-program_point data within
609 : : an exploded_graph. */
610 : :
611 : : struct eg_point_hash_map_traits
612 : : {
613 : : typedef const program_point *key_type;
614 : : typedef per_program_point_data *value_type;
615 : : typedef per_program_point_data *compare_type;
616 : :
617 : 4444851 : static inline hashval_t hash (const key_type &k)
618 : : {
619 : 4444851 : gcc_assert (k != NULL);
620 : 4444851 : gcc_assert (k != reinterpret_cast<key_type> (1));
621 : 4444851 : return k->hash ();
622 : : }
623 : 4335774 : static inline bool equal_keys (const key_type &k1, const key_type &k2)
624 : : {
625 : 4335774 : gcc_assert (k1 != NULL);
626 : 4335774 : gcc_assert (k2 != NULL);
627 : 4335774 : gcc_assert (k1 != reinterpret_cast<key_type> (1));
628 : 4335774 : gcc_assert (k2 != reinterpret_cast<key_type> (1));
629 : 4335774 : if (k1 && k2)
630 : 4335774 : return *k1 == *k2;
631 : : else
632 : : /* Otherwise they must both be non-NULL. */
633 : : return k1 == k2;
634 : : }
635 : : template <typename T>
636 : : static inline void remove (T &)
637 : : {
638 : : /* empty; the nodes are handled elsewhere. */
639 : : }
640 : : template <typename T>
641 : : static inline void mark_deleted (T &entry)
642 : : {
643 : : entry.m_key = reinterpret_cast<key_type> (1);
644 : : }
645 : : template <typename T>
646 : 972856 : static inline void mark_empty (T &entry)
647 : : {
648 : 972856 : entry.m_key = NULL;
649 : : }
650 : : template <typename T>
651 : 5314293 : static inline bool is_deleted (const T &entry)
652 : : {
653 : 5314293 : return entry.m_key == reinterpret_cast<key_type> (1);
654 : : }
655 : : template <typename T>
656 : 16412584 : static inline bool is_empty (const T &entry)
657 : : {
658 : 16412584 : return entry.m_key == NULL;
659 : : }
660 : : static const bool empty_zero_p = false;
661 : : };
662 : :
663 : : /* Data about a particular call_string within an exploded_graph. */
664 : :
665 : : struct per_call_string_data
666 : : {
667 : 8363 : per_call_string_data (const call_string &key, int num_supernodes)
668 : 8363 : : m_key (key), m_stats (num_supernodes)
669 : : {}
670 : :
671 : : const call_string &m_key;
672 : : stats m_stats;
673 : : };
674 : :
675 : : /* Data about a particular function within an exploded_graph. */
676 : :
677 : : struct per_function_data
678 : : {
679 : 8317 : per_function_data () {}
680 : : ~per_function_data ();
681 : :
682 : : void add_call_summary (exploded_node *node);
683 : :
684 : : auto_vec<call_summary *> m_summaries;
685 : : };
686 : :
687 : :
688 : : /* The strongly connected components of a supergraph.
689 : : In particular, this allows us to compute a partial ordering
690 : : of supernodes. */
691 : :
692 : : class strongly_connected_components
693 : : {
694 : : public:
695 : : strongly_connected_components (const supergraph &sg, logger *logger);
696 : :
697 : 1587693 : int get_scc_id (int node_index) const
698 : : {
699 : 0 : return m_per_node[node_index].m_lowlink;
700 : : }
701 : :
702 : : void dump () const;
703 : :
704 : : std::unique_ptr<json::array> to_json () const;
705 : :
706 : : private:
707 : : struct per_node_data
708 : : {
709 : 61734 : per_node_data ()
710 : 61734 : : m_index (-1), m_lowlink (-1), m_on_stack (false)
711 : : {}
712 : :
713 : : int m_index;
714 : : int m_lowlink;
715 : : bool m_on_stack;
716 : : };
717 : :
718 : : void strong_connect (unsigned index);
719 : :
720 : : const supergraph &m_sg;
721 : : auto_vec<unsigned> m_stack;
722 : : auto_vec<per_node_data> m_per_node;
723 : : };
724 : :
725 : : /* The worklist of exploded_node instances that have been added to
726 : : an exploded_graph, but that haven't yet been processed to find
727 : : their successors (or warnings).
728 : :
729 : : The enodes are stored in a priority queue, ordered by a topological
730 : : sort of the SCCs in the supergraph, so that enodes for the same
731 : : program_point should appear at the front of the queue together.
732 : : This allows for state-merging at CFG join-points, so that
733 : : sufficiently-similar enodes can be merged into one. */
734 : :
735 : : class worklist
736 : : {
737 : : public:
738 : : worklist (const exploded_graph &eg, const analysis_plan &plan);
739 : : unsigned length () const;
740 : : exploded_node *take_next ();
741 : : exploded_node *peek_next ();
742 : : void add_node (exploded_node *enode);
743 : 549 : int get_scc_id (const supernode &snode) const
744 : : {
745 : 1098 : return m_scc.get_scc_id (snode.m_index);
746 : : }
747 : :
748 : : std::unique_ptr<json::object> to_json () const;
749 : :
750 : : private:
751 : : class key_t
752 : : {
753 : : public:
754 : 375919 : key_t (const worklist &w, exploded_node *enode)
755 : 375919 : : m_worklist (w), m_enode (enode)
756 : : {}
757 : :
758 : 1050228 : bool operator< (const key_t &other) const
759 : : {
760 : 1050228 : return cmp (*this, other) < 0;
761 : : }
762 : :
763 : 282234 : bool operator== (const key_t &other) const
764 : : {
765 : 282234 : return cmp (*this, other) == 0;
766 : : }
767 : :
768 : 282234 : bool operator> (const key_t &other) const
769 : : {
770 : 564468 : return !(*this == other || *this < other);
771 : : }
772 : :
773 : : private:
774 : : static int cmp (const key_t &ka, const key_t &kb);
775 : :
776 : 1597098 : int get_scc_id (const exploded_node *enode) const
777 : : {
778 : 1597098 : const supernode *snode = enode->get_supernode ();
779 : 1597098 : if (snode == NULL)
780 : : return 0;
781 : 1587144 : return m_worklist.m_scc.get_scc_id (snode->m_index);
782 : : }
783 : :
784 : : const worklist &m_worklist;
785 : : exploded_node *m_enode;
786 : : };
787 : :
788 : : /* The order is important here: m_scc needs to stick around
789 : : until after m_queue has finished being cleaned up (the dtor
790 : : calls the ordering fns). */
791 : : strongly_connected_components m_scc;
792 : : const analysis_plan &m_plan;
793 : :
794 : : /* Priority queue, backed by a fibonacci_heap. */
795 : : typedef fibonacci_heap<key_t, exploded_node> queue_t;
796 : : queue_t m_queue;
797 : : };
798 : :
799 : : /* An exploded_graph is a directed graph of unique <point, state> pairs.
800 : : It also has a worklist of nodes that are waiting for their successors
801 : : to be added to the graph. */
802 : :
803 : : class exploded_graph : public digraph<eg_traits>
804 : : {
805 : : public:
806 : : typedef hash_map <const call_string *,
807 : : per_call_string_data *> call_string_data_map_t;
808 : :
809 : : exploded_graph (const supergraph &sg, logger *logger,
810 : : const extrinsic_state &ext_state,
811 : : const state_purge_map *purge_map,
812 : : const analysis_plan &plan,
813 : : int verbosity);
814 : : ~exploded_graph ();
815 : :
816 : 7008142 : logger *get_logger () const { return m_logger.get_logger (); }
817 : :
818 : 22163 : const supergraph &get_supergraph () const { return m_sg; }
819 : 3791540 : const extrinsic_state &get_ext_state () const { return m_ext_state; }
820 : 6741 : engine *get_engine () const { return m_ext_state.get_engine (); }
821 : 623480 : const state_purge_map *get_purge_map () const { return m_purge_map; }
822 : 24403 : const analysis_plan &get_analysis_plan () const { return m_plan; }
823 : :
824 : 6751 : exploded_node *get_origin () const { return m_origin; }
825 : :
826 : : exploded_node *add_function_entry (const function &fun);
827 : :
828 : : void build_initial_worklist ();
829 : : void process_worklist ();
830 : : bool maybe_process_run_of_before_supernode_enodes (exploded_node *node);
831 : : void process_node (exploded_node *node);
832 : :
833 : : bool maybe_create_dynamic_call (const gcall &call,
834 : : tree fn_decl,
835 : : exploded_node *node,
836 : : program_state next_state,
837 : : program_point &next_point,
838 : : uncertainty_t *uncertainty,
839 : : logger *logger);
840 : :
841 : : exploded_node *get_or_create_node (const program_point &point,
842 : : const program_state &state,
843 : : exploded_node *enode_for_diag,
844 : : bool add_to_worklist = true);
845 : : exploded_edge *add_edge (exploded_node *src, exploded_node *dest,
846 : : const superedge *sedge, bool could_do_work,
847 : : std::unique_ptr<custom_edge_info> custom = NULL);
848 : :
849 : : per_program_point_data *
850 : : get_or_create_per_program_point_data (const program_point &);
851 : : per_program_point_data *
852 : : get_per_program_point_data (const program_point &) const;
853 : :
854 : : per_call_string_data *
855 : : get_or_create_per_call_string_data (const call_string &);
856 : :
857 : : per_function_data *
858 : : get_or_create_per_function_data (function *);
859 : : per_function_data *get_per_function_data (function *) const;
860 : :
861 : : void save_diagnostic (const state_machine &sm,
862 : : const exploded_node *enode,
863 : : const supernode *node, const gimple *stmt,
864 : : stmt_finder *finder,
865 : : tree var, state_machine::state_t state,
866 : : pending_diagnostic *d);
867 : :
868 : 14538 : diagnostic_manager &get_diagnostic_manager ()
869 : : {
870 : 14558 : return m_diagnostic_manager;
871 : : }
872 : : const diagnostic_manager &get_diagnostic_manager () const
873 : : {
874 : : return m_diagnostic_manager;
875 : : }
876 : :
877 : : stats *get_global_stats () { return &m_global_stats; }
878 : : stats *get_or_create_function_stats (function *fn);
879 : : void log_stats () const;
880 : : void dump_stats (FILE *) const;
881 : : void dump_states_for_supernode (FILE *, const supernode *snode) const;
882 : : void dump_exploded_nodes () const;
883 : :
884 : : std::unique_ptr<json::object> to_json () const;
885 : :
886 : : exploded_node *get_node_by_index (int idx) const;
887 : :
888 : 12 : const call_string_data_map_t *get_per_call_string_data () const
889 : 12 : { return &m_per_call_string_data; }
890 : :
891 : 549 : int get_scc_id (const supernode &node) const
892 : : {
893 : 549 : return m_worklist.get_scc_id (node);
894 : : }
895 : :
896 : : void on_escaped_function (tree fndecl);
897 : :
898 : : void unwind_from_exception (exploded_node &enode,
899 : : const gimple *stmt,
900 : : region_model_context *ctxt);
901 : :
902 : : /* In infinite-loop.cc */
903 : : void detect_infinite_loops ();
904 : :
905 : : /* In infinite-recursion.cc */
906 : : void detect_infinite_recursion (exploded_node *enode);
907 : : exploded_node *find_previous_entry_to (function *top_of_stack_fun,
908 : : exploded_node *enode) const;
909 : :
910 : : private:
911 : : void print_bar_charts (pretty_printer *pp) const;
912 : :
913 : : DISABLE_COPY_AND_ASSIGN (exploded_graph);
914 : :
915 : : const supergraph &m_sg;
916 : :
917 : : log_user m_logger;
918 : :
919 : : /* Map from point/state to exploded node.
920 : : To avoid duplication we store point_and_state
921 : : *pointers* as keys, rather than point_and_state, using the
922 : : instance from within the exploded_node, with a custom hasher. */
923 : : typedef hash_map <const point_and_state *, exploded_node *,
924 : : eg_hash_map_traits> map_t;
925 : : map_t m_point_and_state_to_node;
926 : :
927 : : /* Map from program_point to per-program_point data. */
928 : : typedef hash_map <const program_point *, per_program_point_data *,
929 : : eg_point_hash_map_traits> point_map_t;
930 : : point_map_t m_per_point_data;
931 : :
932 : : worklist m_worklist;
933 : :
934 : : exploded_node *m_origin;
935 : :
936 : : const extrinsic_state &m_ext_state;
937 : :
938 : : const state_purge_map *const m_purge_map;
939 : :
940 : : const analysis_plan &m_plan;
941 : :
942 : : typedef hash_map<function *, per_function_data *> per_function_data_t;
943 : : per_function_data_t m_per_function_data;
944 : :
945 : : diagnostic_manager m_diagnostic_manager;
946 : :
947 : : /* Stats. */
948 : : stats m_global_stats;
949 : : typedef ordered_hash_map<function *, stats *> function_stat_map_t;
950 : : function_stat_map_t m_per_function_stats;
951 : : stats m_functionless_stats;
952 : :
953 : : call_string_data_map_t m_per_call_string_data;
954 : :
955 : : auto_vec<int> m_PK_AFTER_SUPERNODE_per_snode;
956 : :
957 : : /* Functions with a top-level enode, to make add_function_entry
958 : : be idempotent, for use in handling callbacks. */
959 : : hash_set<function *> m_functions_with_enodes;
960 : : };
961 : :
962 : : /* A path within an exploded_graph: a sequence of edges. */
963 : :
964 : 6596 : class exploded_path
965 : : {
966 : : public:
967 : 6592 : exploded_path () : m_edges () {}
968 : : exploded_path (const exploded_path &other);
969 : :
970 : 7922 : unsigned length () const { return m_edges.length (); }
971 : :
972 : : bool find_stmt_backwards (const gimple *search_stmt,
973 : : int *out_idx) const;
974 : :
975 : : exploded_node *get_final_enode () const;
976 : :
977 : : void dump_to_pp (pretty_printer *pp,
978 : : const extrinsic_state *ext_state) const;
979 : : void dump (FILE *fp, const extrinsic_state *ext_state) const;
980 : : void dump (const extrinsic_state *ext_state = NULL) const;
981 : : void dump_to_file (const char *filename,
982 : : const extrinsic_state &ext_state) const;
983 : :
984 : : bool feasible_p (logger *logger, std::unique_ptr<feasibility_problem> *out,
985 : : engine *eng, const exploded_graph *eg) const;
986 : :
987 : : auto_vec<const exploded_edge *> m_edges;
988 : : };
989 : :
990 : : /* A reason why a particular exploded_path is infeasible. */
991 : :
992 : 4 : class feasibility_problem
993 : : {
994 : : public:
995 : 4 : feasibility_problem (unsigned eedge_idx,
996 : : const exploded_edge &eedge,
997 : : const gimple *last_stmt,
998 : : std::unique_ptr<rejected_constraint> rc)
999 : 4 : : m_eedge_idx (eedge_idx), m_eedge (eedge),
1000 : 4 : m_last_stmt (last_stmt), m_rc (std::move (rc))
1001 : : {}
1002 : :
1003 : : void dump_to_pp (pretty_printer *pp) const;
1004 : :
1005 : : unsigned m_eedge_idx;
1006 : : const exploded_edge &m_eedge;
1007 : : const gimple *m_last_stmt;
1008 : : std::unique_ptr<rejected_constraint> m_rc;
1009 : : };
1010 : :
1011 : : /* A class for capturing the state of a node when checking a path
1012 : : through the exploded_graph for feasibility. */
1013 : :
1014 : : class feasibility_state
1015 : : {
1016 : : public:
1017 : : feasibility_state (region_model_manager *manager,
1018 : : const supergraph &sg);
1019 : : feasibility_state (const region_model &model,
1020 : : const supergraph &sg);
1021 : : feasibility_state (const feasibility_state &other);
1022 : :
1023 : : feasibility_state &operator= (const feasibility_state &other);
1024 : :
1025 : : bool maybe_update_for_edge (logger *logger,
1026 : : const exploded_edge *eedge,
1027 : : region_model_context *ctxt,
1028 : : std::unique_ptr<rejected_constraint> *out_rc);
1029 : : void update_for_stmt (const gimple *stmt);
1030 : :
1031 : 1639 : const region_model &get_model () const { return m_model; }
1032 : : const auto_sbitmap &get_snodes_visited () const { return m_snodes_visited; }
1033 : :
1034 : : void dump_to_pp (pretty_printer *pp, bool simple, bool multiline) const;
1035 : :
1036 : : private:
1037 : : region_model m_model;
1038 : : auto_sbitmap m_snodes_visited;
1039 : : };
1040 : :
1041 : : /* Finding the shortest exploded_path within an exploded_graph. */
1042 : :
1043 : : typedef shortest_paths<eg_traits, exploded_path> shortest_exploded_paths;
1044 : :
1045 : : /* Abstract base class for use when passing NULL as the stmt for
1046 : : a possible warning, allowing the choice of stmt to be deferred
1047 : : until after we have an emission path (and know we're emitting a
1048 : : warning). */
1049 : :
1050 : 3269 : class stmt_finder
1051 : : {
1052 : : public:
1053 : 1892 : virtual ~stmt_finder () {}
1054 : : virtual std::unique_ptr<stmt_finder> clone () const = 0;
1055 : : virtual const gimple *find_stmt (const exploded_path &epath) = 0;
1056 : : virtual void update_event_loc_info (event_loc_info &) = 0;
1057 : : };
1058 : :
1059 : : // TODO: split the above up?
1060 : :
1061 : : } // namespace ana
1062 : :
1063 : : #endif /* GCC_ANALYZER_EXPLODED_GRAPH_H */
|