Branch data Line data Source code
1 : : /* Classes for saving, deduplicating, and emitting analyzer diagnostics.
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 : : #include "analyzer/common.h"
22 : :
23 : : #include "cfg.h"
24 : : #include "basic-block.h"
25 : : #include "gimple.h"
26 : : #include "gimple-pretty-print.h"
27 : : #include "gimple-iterator.h"
28 : : #include "inlining-iterator.h"
29 : : #include "cgraph.h"
30 : : #include "digraph.h"
31 : : #include "gcc-rich-location.h"
32 : : #include "diagnostic-format-sarif.h"
33 : :
34 : : #include "analyzer/analyzer-logging.h"
35 : : #include "analyzer/sm.h"
36 : : #include "analyzer/pending-diagnostic.h"
37 : : #include "analyzer/diagnostic-manager.h"
38 : : #include "analyzer/call-string.h"
39 : : #include "analyzer/program-point.h"
40 : : #include "analyzer/store.h"
41 : : #include "analyzer/region-model.h"
42 : : #include "analyzer/constraint-manager.h"
43 : : #include "analyzer/supergraph.h"
44 : : #include "analyzer/program-state.h"
45 : : #include "analyzer/exploded-graph.h"
46 : : #include "analyzer/trimmed-graph.h"
47 : : #include "analyzer/feasible-graph.h"
48 : : #include "analyzer/checker-path.h"
49 : : #include "analyzer/reachability.h"
50 : :
51 : : #if ENABLE_ANALYZER
52 : :
53 : : namespace ana {
54 : :
55 : : class feasible_worklist;
56 : :
57 : : /* State for finding the shortest feasible exploded_path for a
58 : : saved_diagnostic.
59 : : This is shared between all diagnostics, so that we avoid repeating work. */
60 : :
61 : : class epath_finder
62 : : {
63 : : public:
64 : 1552 : epath_finder (const exploded_graph &eg)
65 : 1552 : : m_eg (eg),
66 : 1552 : m_sep (nullptr)
67 : : {
68 : : /* This is shared by all diagnostics, but only needed if
69 : : !flag_analyzer_feasibility. */
70 : 1552 : if (!flag_analyzer_feasibility)
71 : 4 : m_sep = new shortest_exploded_paths (eg, eg.get_origin (),
72 : 4 : SPS_FROM_GIVEN_ORIGIN);
73 : 1552 : }
74 : :
75 : 1556 : ~epath_finder () { delete m_sep; }
76 : :
77 : 173862 : logger *get_logger () const { return m_eg.get_logger (); }
78 : :
79 : : std::unique_ptr<exploded_path>
80 : : get_best_epath (const exploded_node *target_enode,
81 : : const gimple *target_stmt,
82 : : const pending_diagnostic &pd,
83 : : const char *desc, unsigned diag_idx,
84 : : std::unique_ptr<feasibility_problem> *out_problem);
85 : :
86 : : private:
87 : : DISABLE_COPY_AND_ASSIGN(epath_finder);
88 : :
89 : : std::unique_ptr<exploded_path>
90 : : explore_feasible_paths (const exploded_node *target_enode,
91 : : const gimple *target_stmt,
92 : : const pending_diagnostic &pd,
93 : : const char *desc, unsigned diag_idx);
94 : : bool
95 : : process_worklist_item (feasible_worklist *worklist,
96 : : const trimmed_graph &tg,
97 : : feasible_graph *fg,
98 : : const exploded_node *target_enode,
99 : : const gimple *target_stmt,
100 : : const pending_diagnostic &pd,
101 : : unsigned diag_idx,
102 : : std::unique_ptr<exploded_path> *out_best_path) const;
103 : : void dump_trimmed_graph (const exploded_node *target_enode,
104 : : const char *desc, unsigned diag_idx,
105 : : const trimmed_graph &tg,
106 : : const shortest_paths<eg_traits, exploded_path> &sep);
107 : : void dump_feasible_graph (const exploded_node *target_enode,
108 : : const char *desc, unsigned diag_idx,
109 : : const feasible_graph &fg);
110 : : void dump_feasible_path (const exploded_node *target_enode,
111 : : unsigned diag_idx,
112 : : const feasible_graph &fg,
113 : : const feasible_node &fnode) const;
114 : :
115 : : const exploded_graph &m_eg;
116 : : shortest_exploded_paths *m_sep;
117 : : };
118 : :
119 : : /* class epath_finder. */
120 : :
121 : : /* Get the "best" exploded_path for reaching ENODE from the origin,
122 : : returning ownership of it to the caller.
123 : :
124 : : If TARGET_STMT is non-NULL, then check for reaching that stmt
125 : : within ENODE.
126 : :
127 : : Ideally we want to report the shortest feasible path.
128 : : Return nullptr if we could not find a feasible path
129 : : (when flag_analyzer_feasibility is true).
130 : :
131 : : If flag_analyzer_feasibility is false, then simply return the
132 : : shortest path.
133 : :
134 : : Use DESC and DIAG_IDX when logging.
135 : :
136 : : Write any feasibility_problem to *OUT_PROBLEM. */
137 : :
138 : : std::unique_ptr<exploded_path>
139 : 6750 : epath_finder::get_best_epath (const exploded_node *enode,
140 : : const gimple *target_stmt,
141 : : const pending_diagnostic &pd,
142 : : const char *desc, unsigned diag_idx,
143 : : std::unique_ptr<feasibility_problem> *out_problem)
144 : : {
145 : 6750 : logger *logger = get_logger ();
146 : 6750 : LOG_SCOPE (logger);
147 : :
148 : 6750 : unsigned snode_idx = enode->get_supernode ()->m_index;
149 : 6750 : if (logger)
150 : 0 : logger->log ("considering %qs at EN: %i, SN: %i (sd: %i)",
151 : 0 : desc, enode->m_index, snode_idx, diag_idx);
152 : :
153 : : /* State-merging means that not every path in the egraph corresponds
154 : : to a feasible one w.r.t. states.
155 : :
156 : : We want to find the shortest feasible path from the origin to ENODE
157 : : in the egraph. */
158 : :
159 : 6750 : if (flag_analyzer_feasibility)
160 : : {
161 : : /* Attempt to find the shortest feasible path using feasible_graph. */
162 : 6746 : if (logger)
163 : 0 : logger->log ("trying to find shortest feasible path");
164 : 6746 : if (std::unique_ptr<exploded_path> epath
165 : 6746 : = explore_feasible_paths (enode, target_stmt, pd, desc, diag_idx))
166 : : {
167 : 6475 : if (logger)
168 : 0 : logger->log ("accepting %qs at EN: %i, SN: %i (sd: %i)"
169 : : " with feasible path (length: %i)",
170 : 0 : desc, enode->m_index, snode_idx, diag_idx,
171 : : epath->length ());
172 : 6475 : return epath;
173 : : }
174 : : else
175 : : {
176 : 271 : if (logger)
177 : 0 : logger->log ("rejecting %qs at EN: %i, SN: %i (sd: %i)"
178 : : " due to not finding feasible path",
179 : 0 : desc, enode->m_index, snode_idx, diag_idx);
180 : 271 : return nullptr;
181 : 6746 : }
182 : : }
183 : : else
184 : : {
185 : : /* As a crude approximation to shortest feasible path, simply find
186 : : the shortest path, and note whether it is feasible.
187 : : There could be longer feasible paths within the egraph, so this
188 : : approach would lead to diagnostics being falsely rejected
189 : : (PR analyzer/96374). */
190 : 4 : if (logger)
191 : 0 : logger->log ("trying to find shortest path ignoring feasibility");
192 : 4 : gcc_assert (m_sep);
193 : 4 : std::unique_ptr<exploded_path> epath
194 : 4 : = std::make_unique<exploded_path> (m_sep->get_shortest_path (enode));
195 : 4 : if (epath->feasible_p (logger, out_problem, m_eg.get_engine (), &m_eg))
196 : : {
197 : 0 : if (logger)
198 : 0 : logger->log ("accepting %qs at EN: %i, SN: %i (sn: %i)"
199 : : " with feasible path (length: %i)",
200 : 0 : desc, enode->m_index, snode_idx, diag_idx,
201 : : epath->length ());
202 : : }
203 : : else
204 : : {
205 : 4 : if (logger)
206 : 0 : logger->log ("accepting %qs at EN: %i, SN: %i (sn: %i) (length: %i)"
207 : : " despite infeasible path (due to %qs)",
208 : 0 : desc, enode->m_index, snode_idx, diag_idx,
209 : : epath->length (),
210 : : "-fno-analyzer-feasibility");
211 : : }
212 : 4 : return epath;
213 : 4 : }
214 : 6750 : }
215 : :
216 : : /* A class for managing the worklist of feasible_nodes in
217 : : epath_finder::explore_feasible_paths, prioritizing them
218 : : so that shorter paths appear earlier in the queue. */
219 : :
220 : 13492 : class feasible_worklist
221 : : {
222 : : public:
223 : 6746 : feasible_worklist (const shortest_paths<eg_traits, exploded_path> &sep)
224 : 13492 : : m_queue (key_t (*this, nullptr)),
225 : 6746 : m_sep (sep)
226 : : {
227 : 6746 : }
228 : :
229 : 153616 : feasible_node *take_next () { return m_queue.extract_min (); }
230 : :
231 : 156974 : void add_node (feasible_node *fnode)
232 : : {
233 : 313948 : m_queue.insert (key_t (*this, fnode), fnode);
234 : 150228 : }
235 : :
236 : : private:
237 : : struct key_t
238 : : {
239 : 163720 : key_t (const feasible_worklist &w, feasible_node *fnode)
240 : 6746 : : m_worklist (w), m_fnode (fnode)
241 : : {}
242 : :
243 : 140604 : bool operator< (const key_t &other) const
244 : : {
245 : 140604 : return cmp (*this, other) < 0;
246 : : }
247 : :
248 : 49074 : bool operator== (const key_t &other) const
249 : : {
250 : 49074 : return cmp (*this, other) == 0;
251 : : }
252 : :
253 : 49074 : bool operator> (const key_t &other) const
254 : : {
255 : 49074 : return !(*this == other || *this < other);
256 : : }
257 : :
258 : : private:
259 : 189678 : static int cmp (const key_t &ka, const key_t &kb)
260 : : {
261 : : /* Choose the node for which if the remaining path were feasible,
262 : : it would be the shortest path (summing the length of the
263 : : known-feasible path so far with that of the remaining
264 : : possibly-feasible path). */
265 : 189678 : int ca = ka.m_worklist.get_estimated_cost (ka.m_fnode);
266 : 189678 : int cb = kb.m_worklist.get_estimated_cost (kb.m_fnode);
267 : 189678 : return ca - cb;
268 : : }
269 : :
270 : : const feasible_worklist &m_worklist;
271 : : feasible_node *m_fnode;
272 : : };
273 : :
274 : : /* Get the estimated length of a path involving FNODE from
275 : : the origin to the target enode.
276 : : Sum the length of the known-feasible path so far with
277 : : that of the remaining possibly-feasible path. */
278 : :
279 : 379356 : int get_estimated_cost (const feasible_node *fnode) const
280 : : {
281 : 379356 : unsigned length_so_far = fnode->get_path_length ();
282 : 379356 : int shortest_remaining_path
283 : 379356 : = m_sep.get_shortest_distance (fnode->get_inner_node ());
284 : :
285 : 379356 : gcc_assert (shortest_remaining_path >= 0);
286 : : /* This should be true since we're only exploring nodes within
287 : : the trimmed graph (and we anticipate it being much smaller
288 : : than this, and thus not overflowing the sum). */
289 : 379356 : gcc_assert (shortest_remaining_path < INT_MAX);
290 : :
291 : 379356 : return length_so_far + shortest_remaining_path;
292 : : }
293 : :
294 : : /* Priority queue, backed by a fibonacci_heap. */
295 : : typedef fibonacci_heap<key_t, feasible_node> queue_t;
296 : : queue_t m_queue;
297 : : const shortest_paths<eg_traits, exploded_path> &m_sep;
298 : : };
299 : :
300 : : /* When we're building the exploded graph we want to simplify
301 : : overly-complicated symbolic values down to "UNKNOWN" to try to avoid
302 : : state explosions and unbounded chains of exploration.
303 : :
304 : : However, when we're building the feasibility graph for a diagnostic
305 : : (actually a tree), we don't want UNKNOWN values, as conditions on them
306 : : are also unknown: we don't want to have a contradiction such as a path
307 : : where (VAL != 0) and then (VAL == 0) along the same path.
308 : :
309 : : Hence this is an RAII class for temporarily disabling complexity-checking
310 : : in the region_model_manager, for use within
311 : : epath_finder::explore_feasible_paths.
312 : :
313 : : We also disable the creation of unknown_svalue instances during feasibility
314 : : checking, instead creating unique svalues, to avoid paradoxes in paths. */
315 : :
316 : : class auto_checking_feasibility
317 : : {
318 : : public:
319 : 6746 : auto_checking_feasibility (region_model_manager *mgr) : m_mgr (mgr)
320 : : {
321 : 6746 : m_mgr->begin_checking_feasibility ();
322 : 6746 : }
323 : 6746 : ~auto_checking_feasibility ()
324 : : {
325 : 6746 : m_mgr->end_checking_feasibility ();
326 : : }
327 : : private:
328 : : region_model_manager *m_mgr;
329 : : };
330 : :
331 : : /* Attempt to find the shortest feasible path from the origin to
332 : : TARGET_ENODE by iteratively building a feasible_graph, in which
333 : : every path to a feasible_node is feasible by construction.
334 : :
335 : : If TARGET_STMT is non-NULL, then check for reaching that stmt
336 : : within TARGET_ENODE.
337 : :
338 : : We effectively explore the tree of feasible paths in order of shortest
339 : : path until we either find a feasible path to TARGET_ENODE, or hit
340 : : a limit and give up.
341 : :
342 : : Preliminaries:
343 : : - Find the shortest path from each node to the TARGET_ENODE (without
344 : : checking feasibility), so that we can prioritize our worklist.
345 : : - Construct a trimmed_graph: the subset of nodes/edges that
346 : : are on a path that eventually reaches TARGET_ENODE. We will only need
347 : : to consider these when considering the shortest feasible path.
348 : :
349 : : Build a feasible_graph, in which every path to a feasible_node
350 : : is feasible by construction.
351 : : We use a worklist to flatten the exploration into an iteration.
352 : : Starting at the origin, find feasible out-edges within the trimmed graph.
353 : : At each stage, choose the node for which if the remaining path were feasible,
354 : : it would be the shortest path (summing the length of the known-feasible path
355 : : so far with that of the remaining possibly-feasible path).
356 : : This way, the first feasible path we find to TARGET_ENODE is the shortest.
357 : : We start by trying the shortest possible path, but if that fails,
358 : : we explore progressively longer paths, eventually trying iterations through
359 : : loops. The exploration is captured in the feasible_graph, which can be
360 : : dumped as a .dot file to visualize the exploration. The indices of the
361 : : feasible_nodes show the order in which they were created.
362 : :
363 : : This is something of a brute-force approach, but the trimmed_graph
364 : : hopefully keeps the complexity manageable.
365 : :
366 : : Terminate with failure when the number of infeasible edges exceeds
367 : : a threshold (--param=analyzer-max-infeasible-edges=).
368 : : This is guaranteed to eventually lead to terminatation, as
369 : : we can't keep creating feasible nodes without eventually
370 : : either reaching an infeasible edge, or reaching the
371 : : TARGET_ENODE. Specifically, there can't be a cycle of
372 : : feasible edges that doesn't reach the target_enode without
373 : : an out-edge that either fails feasibility or gets closer
374 : : to the TARGET_ENODE: on each iteration we are either:
375 : : - effectively getting closer to the TARGET_ENODE (which can't
376 : : continue forever without reaching the target), or
377 : : - getting monotonically closer to the termination threshold. */
378 : :
379 : : std::unique_ptr<exploded_path>
380 : 6746 : epath_finder::explore_feasible_paths (const exploded_node *target_enode,
381 : : const gimple *target_stmt,
382 : : const pending_diagnostic &pd,
383 : : const char *desc, unsigned diag_idx)
384 : : {
385 : 6746 : logger *logger = get_logger ();
386 : 6746 : LOG_SCOPE (logger);
387 : :
388 : 6746 : region_model_manager *mgr = m_eg.get_engine ()->get_model_manager ();
389 : :
390 : : /* Determine the shortest path to TARGET_ENODE from each node in
391 : : the exploded graph. */
392 : 6746 : shortest_paths<eg_traits, exploded_path> sep
393 : 6746 : (m_eg, target_enode, SPS_TO_GIVEN_TARGET);
394 : :
395 : : /* Construct a trimmed_graph: the subset of nodes/edges that
396 : : are on a path that eventually reaches TARGET_ENODE.
397 : : We only need to consider these when considering the shortest
398 : : feasible path. */
399 : 6746 : trimmed_graph tg (m_eg, target_enode);
400 : :
401 : 6746 : if (flag_dump_analyzer_feasibility)
402 : 8 : dump_trimmed_graph (target_enode, desc, diag_idx, tg, sep);
403 : :
404 : 6746 : feasible_graph fg;
405 : 6746 : feasible_worklist worklist (sep);
406 : :
407 : : /* Populate the worklist with the origin node. */
408 : 6746 : {
409 : 6746 : feasibility_state init_state (mgr, m_eg.get_supergraph ());
410 : 6746 : feasible_node *origin = fg.add_node (m_eg.get_origin (), init_state, 0);
411 : 6746 : worklist.add_node (origin);
412 : 6746 : }
413 : :
414 : : /* Iteratively explore the tree of feasible paths in order of shortest
415 : : path until we either find a feasible path to TARGET_ENODE, or hit
416 : : a limit. */
417 : :
418 : : /* Set this if we find a feasible path to TARGET_ENODE. */
419 : 6746 : std::unique_ptr<exploded_path> best_path = nullptr;
420 : :
421 : 6746 : {
422 : 6746 : auto_checking_feasibility sentinel (mgr);
423 : :
424 : 153616 : while (process_worklist_item (&worklist, tg, &fg, target_enode, target_stmt,
425 : : pd, diag_idx, &best_path))
426 : : {
427 : : /* Empty; the work is done within process_worklist_item. */
428 : : }
429 : 6746 : }
430 : :
431 : 6746 : if (logger)
432 : : {
433 : 0 : logger->log ("tg for sd: %i:", diag_idx);
434 : 0 : logger->inc_indent ();
435 : 0 : tg.log_stats (logger);
436 : 0 : logger->dec_indent ();
437 : :
438 : 0 : logger->log ("fg for sd: %i:", diag_idx);
439 : 0 : logger->inc_indent ();
440 : 0 : fg.log_stats (logger);
441 : 0 : logger->dec_indent ();
442 : : }
443 : :
444 : : /* Dump the feasible_graph. */
445 : 6746 : if (flag_dump_analyzer_feasibility)
446 : 8 : dump_feasible_graph (target_enode, desc, diag_idx, fg);
447 : :
448 : 6746 : return best_path;
449 : 13492 : }
450 : :
451 : : /* Process the next item in WORKLIST, potentially adding new items
452 : : based on feasible out-edges, and extending FG accordingly.
453 : : Use TG to ignore out-edges that don't lead to TARGET_ENODE.
454 : : Return true if the worklist processing should continue.
455 : : Return false if the processing of the worklist should stop
456 : : (either due to reaching TARGET_ENODE, or hitting a limit).
457 : : Write to *OUT_BEST_PATH if stopping due to finding a feasible path
458 : : to TARGET_ENODE.
459 : : Use PD to provide additional restrictions on feasibility of
460 : : the final path in the feasible_graph before converting to
461 : : an exploded_path. */
462 : :
463 : : bool
464 : 153616 : epath_finder::
465 : : process_worklist_item (feasible_worklist *worklist,
466 : : const trimmed_graph &tg,
467 : : feasible_graph *fg,
468 : : const exploded_node *target_enode,
469 : : const gimple *target_stmt,
470 : : const pending_diagnostic &pd,
471 : : unsigned diag_idx,
472 : : std::unique_ptr<exploded_path> *out_best_path) const
473 : : {
474 : 153616 : logger *logger = get_logger ();
475 : :
476 : 153616 : feasible_node *fnode = worklist->take_next ();
477 : 153616 : if (!fnode)
478 : : {
479 : 50 : if (logger)
480 : 0 : logger->log ("drained worklist for sd: %i"
481 : : " without finding feasible path",
482 : : diag_idx);
483 : 50 : return false;
484 : : }
485 : :
486 : 153566 : log_scope s (logger, "fg worklist item",
487 : : "considering FN: %i (EN: %i) for sd: %i",
488 : 153566 : fnode->get_index (), fnode->get_inner_node ()->m_index,
489 : 153566 : diag_idx);
490 : :
491 : : /* Iterate through all out-edges from this item. */
492 : 153566 : unsigned i;
493 : 153566 : exploded_edge *succ_eedge;
494 : 362538 : FOR_EACH_VEC_ELT (fnode->get_inner_node ()->m_succs, i, succ_eedge)
495 : : {
496 : 215668 : log_scope s (logger, "edge", "considering edge: EN:%i -> EN:%i",
497 : 215668 : succ_eedge->m_src->m_index,
498 : 215668 : succ_eedge->m_dest->m_index);
499 : : /* Reject edges that aren't in the trimmed graph. */
500 : 215668 : if (!tg.contains_p (succ_eedge))
501 : : {
502 : 56898 : if (logger)
503 : 0 : logger->log ("rejecting: not in trimmed graph");
504 : 56898 : continue;
505 : : }
506 : :
507 : 158770 : feasibility_state succ_state (fnode->get_state ());
508 : 158770 : std::unique_ptr<rejected_constraint> rc;
509 : 158770 : if (succ_state.maybe_update_for_edge (logger, succ_eedge, nullptr, &rc))
510 : : {
511 : 156807 : gcc_assert (rc == nullptr);
512 : 156807 : feasible_node *succ_fnode
513 : 156807 : = fg->add_node (succ_eedge->m_dest,
514 : : succ_state,
515 : 156807 : fnode->get_path_length () + 1);
516 : 156807 : if (logger)
517 : 0 : logger->log ("accepting as FN: %i", succ_fnode->get_index ());
518 : 156807 : fg->add_edge (new feasible_edge (fnode, succ_fnode, succ_eedge));
519 : :
520 : : /* Have we reached TARGET_ENODE? */
521 : 156807 : if (succ_fnode->get_inner_node () == target_enode)
522 : : {
523 : 6579 : if (logger)
524 : 0 : logger->log ("success: got feasible path to EN: %i (sd: %i)"
525 : : " (length: %i)",
526 : 0 : target_enode->m_index, diag_idx,
527 : : succ_fnode->get_path_length ());
528 : 6579 : if (!pd.check_valid_fpath_p (*succ_fnode, target_stmt))
529 : : {
530 : 104 : if (logger)
531 : 0 : logger->log ("rejecting feasible path due to"
532 : : " pending_diagnostic");
533 : 104 : return false;
534 : : }
535 : 6475 : *out_best_path = fg->make_epath (succ_fnode);
536 : 6475 : if (flag_dump_analyzer_feasibility)
537 : 8 : dump_feasible_path (target_enode, diag_idx, *fg, *succ_fnode);
538 : :
539 : : /* Success: stop the worklist iteration. */
540 : 6475 : return false;
541 : : }
542 : : else
543 : 150228 : worklist->add_node (succ_fnode);
544 : : }
545 : : else
546 : : {
547 : 1963 : if (logger)
548 : 0 : logger->log ("infeasible");
549 : 1963 : gcc_assert (rc);
550 : 1963 : fg->add_feasibility_problem (fnode,
551 : : succ_eedge,
552 : : std::move (rc));
553 : :
554 : : /* Give up if there have been too many infeasible edges. */
555 : 1963 : if (fg->get_num_infeasible ()
556 : 1963 : > (unsigned)param_analyzer_max_infeasible_edges)
557 : : {
558 : 117 : if (logger)
559 : 0 : logger->log ("too many infeasible edges (%i); giving up",
560 : : fg->get_num_infeasible ());
561 : 117 : return false;
562 : : }
563 : : }
564 : 215668 : }
565 : :
566 : : /* Continue the worklist iteration. */
567 : : return true;
568 : 153566 : }
569 : :
570 : : /* Helper class for epath_finder::dump_trimmed_graph
571 : : to dump extra per-node information.
572 : : Use SEP to add the length of the shortest path from each
573 : : node to the target node to each node's dump. */
574 : :
575 : : class dump_eg_with_shortest_path : public eg_traits::dump_args_t
576 : : {
577 : : public:
578 : 8 : dump_eg_with_shortest_path
579 : : (const exploded_graph &eg,
580 : : const shortest_paths<eg_traits, exploded_path> &sep)
581 : 8 : : dump_args_t (eg),
582 : 8 : m_sep (sep)
583 : : {
584 : : }
585 : :
586 : 124 : void dump_extra_info (const exploded_node *enode,
587 : : pretty_printer *pp) const final override
588 : : {
589 : 240 : pp_printf (pp, "sp: %i", m_sep.get_shortest_path (enode).length ());
590 : 124 : pp_newline (pp);
591 : 124 : }
592 : :
593 : : private:
594 : : const shortest_paths<eg_traits, exploded_path> &m_sep;
595 : : };
596 : :
597 : : /* Dump TG to "BASE_NAME.DESC.DIAG_IDX.to-enN.tg.dot",
598 : : annotating each node with the length of the shortest path
599 : : from that node to TARGET_ENODE (using SEP). */
600 : :
601 : : void
602 : 8 : epath_finder::
603 : : dump_trimmed_graph (const exploded_node *target_enode,
604 : : const char *desc, unsigned diag_idx,
605 : : const trimmed_graph &tg,
606 : : const shortest_paths<eg_traits, exploded_path> &sep)
607 : : {
608 : 8 : auto_timevar tv (TV_ANALYZER_DUMP);
609 : 8 : dump_eg_with_shortest_path inner_args (m_eg, sep);
610 : 8 : trimmed_graph::dump_args_t args (inner_args);
611 : 8 : pretty_printer pp;
612 : 8 : pp_printf (&pp, "%s.%s.%i.to-en%i.tg.dot",
613 : 8 : dump_base_name, desc, diag_idx, target_enode->m_index);
614 : 8 : char *filename = xstrdup (pp_formatted_text (&pp));
615 : 8 : tg.dump_dot (filename, nullptr, args);
616 : 8 : free (filename);
617 : 8 : }
618 : :
619 : : /* Dump FG to "BASE_NAME.DESC.DIAG_IDX.to-enN.fg.dot". */
620 : :
621 : : void
622 : 8 : epath_finder::dump_feasible_graph (const exploded_node *target_enode,
623 : : const char *desc, unsigned diag_idx,
624 : : const feasible_graph &fg)
625 : : {
626 : 8 : auto_timevar tv (TV_ANALYZER_DUMP);
627 : 8 : exploded_graph::dump_args_t inner_args (m_eg);
628 : 8 : feasible_graph::dump_args_t args (inner_args);
629 : 8 : pretty_printer pp;
630 : 8 : pp_printf (&pp, "%s.%s.%i.to-en%i.fg.dot",
631 : 8 : dump_base_name, desc, diag_idx, target_enode->m_index);
632 : 8 : char *filename = xstrdup (pp_formatted_text (&pp));
633 : 8 : fg.dump_dot (filename, nullptr, args);
634 : 8 : free (filename);
635 : 8 : }
636 : :
637 : : /* Dump the path to FNODE to "BASE_NAME.DIAG_IDX.to-enN.fpath.txt". */
638 : :
639 : : void
640 : 8 : epath_finder::dump_feasible_path (const exploded_node *target_enode,
641 : : unsigned diag_idx,
642 : : const feasible_graph &fg,
643 : : const feasible_node &fnode) const
644 : : {
645 : 8 : auto_timevar tv (TV_ANALYZER_DUMP);
646 : 8 : pretty_printer pp;
647 : 8 : pp_printf (&pp, "%s.%i.to-en%i.fpath.txt",
648 : 8 : dump_base_name, diag_idx, target_enode->m_index);
649 : 8 : char *filename = xstrdup (pp_formatted_text (&pp));
650 : 8 : fg.dump_feasible_path (fnode, filename);
651 : 8 : free (filename);
652 : 8 : }
653 : :
654 : : /* class saved_diagnostic. */
655 : :
656 : : /* saved_diagnostic's ctor. */
657 : :
658 : 6750 : saved_diagnostic::saved_diagnostic (const state_machine *sm,
659 : : const pending_location &ploc,
660 : : tree var,
661 : : const svalue *sval,
662 : : state_machine::state_t state,
663 : : std::unique_ptr<pending_diagnostic> d,
664 : 6750 : unsigned idx)
665 : 6750 : : m_sm (sm), m_enode (ploc.m_enode), m_snode (ploc.m_snode),
666 : 6750 : m_stmt (ploc.m_stmt),
667 : : /* stmt_finder could be on-stack; we want our own copy that can
668 : : outlive that. */
669 : 6750 : m_stmt_finder (ploc.m_finder ? ploc.m_finder->clone () : nullptr),
670 : 6750 : m_loc (ploc.m_loc),
671 : 6750 : m_var (var), m_sval (sval), m_state (state),
672 : 6750 : m_d (std::move (d)), m_trailing_eedge (nullptr),
673 : 6750 : m_idx (idx),
674 : 6750 : m_best_epath (nullptr), m_problem (nullptr),
675 : 6750 : m_notes ()
676 : : {
677 : : /* We must have an enode in order to be able to look for paths
678 : : through the exploded_graph to this diagnostic. */
679 : 6750 : gcc_assert (m_enode);
680 : 6750 : }
681 : :
682 : : bool
683 : 40458 : saved_diagnostic::operator== (const saved_diagnostic &other) const
684 : : {
685 : 42939 : if (m_notes.length () != other.m_notes.length ())
686 : : return false;
687 : 39885 : for (unsigned i = 0; i < m_notes.length (); i++)
688 : 824 : if (!m_notes[i]->equal_p (*other.m_notes[i]))
689 : : return false;
690 : :
691 : : // Don't deduplicate dump_path_diagnostic instances
692 : 39061 : if (!strcmp (m_d->get_kind (), "dump_path_diagnostic"))
693 : 1790 : return this == &other;
694 : :
695 : 37271 : return (m_sm == other.m_sm
696 : : /* We don't compare m_enode. */
697 : 33143 : && m_snode == other.m_snode
698 : 9548 : && m_stmt == other.m_stmt
699 : : /* We don't compare m_stmt_finder. */
700 : 7357 : && m_loc == other.m_loc
701 : 7357 : && pending_diagnostic::same_tree_p (m_var, other.m_var)
702 : 6449 : && m_state == other.m_state
703 : 6386 : && m_d->equal_p (*other.m_d)
704 : 43496 : && m_trailing_eedge == other.m_trailing_eedge);
705 : : }
706 : :
707 : : /* Add PN to this diagnostic, taking ownership of it. */
708 : :
709 : : void
710 : 224 : saved_diagnostic::add_note (std::unique_ptr<pending_note> pn)
711 : : {
712 : 224 : gcc_assert (pn);
713 : 224 : m_notes.safe_push (pn.release ());
714 : 224 : }
715 : :
716 : : /* Add EVENT to this diagnostic. */
717 : :
718 : : void
719 : 188 : saved_diagnostic::add_event (std::unique_ptr<checker_event> event)
720 : : {
721 : 188 : gcc_assert (event);
722 : 188 : m_saved_events.safe_push (event.release ());
723 : 188 : }
724 : :
725 : : /* Return a new json::object of the form
726 : : {"sm": optional str,
727 : : "enode": int,
728 : : "snode": int,
729 : : "sval": optional str,
730 : : "state": optional str,
731 : : "path_length": optional int,
732 : : "pending_diagnostic": str,
733 : : "idx": int}. */
734 : :
735 : : std::unique_ptr<json::object>
736 : 0 : saved_diagnostic::to_json () const
737 : : {
738 : 0 : auto sd_obj = std::make_unique<json::object> ();
739 : :
740 : 0 : if (m_sm)
741 : 0 : sd_obj->set_string ("sm", m_sm->get_name ());
742 : 0 : sd_obj->set_integer ("enode", m_enode->m_index);
743 : 0 : sd_obj->set_integer ("snode", m_snode->m_index);
744 : 0 : if (m_sval)
745 : 0 : sd_obj->set ("sval", m_sval->to_json ());
746 : 0 : if (m_state)
747 : 0 : sd_obj->set ("state", m_state->to_json ());
748 : 0 : if (m_best_epath)
749 : 0 : sd_obj->set_integer ("path_length", get_epath_length ());
750 : 0 : sd_obj->set_string ("pending_diagnostic", m_d->get_kind ());
751 : 0 : sd_obj->set_integer ("idx", m_idx);
752 : :
753 : : /* We're not yet JSONifying the following fields:
754 : : const gimple *m_stmt;
755 : : stmt_finder *m_stmt_finder;
756 : : tree m_var;
757 : : exploded_edge *m_trailing_eedge;
758 : : enum status m_status;
759 : : feasibility_problem *m_problem;
760 : : auto_delete_vec <pending_note> m_notes;
761 : : */
762 : :
763 : 0 : return sd_obj;
764 : : }
765 : :
766 : : /* Dump this to PP in a form suitable for use as an id in .dot output. */
767 : :
768 : : void
769 : 40 : saved_diagnostic::dump_dot_id (pretty_printer *pp) const
770 : : {
771 : 40 : pp_printf (pp, "sd_%i", m_idx);
772 : 40 : }
773 : :
774 : : /* Dump this to PP in a form suitable for use as a node in .dot output. */
775 : :
776 : : void
777 : 20 : saved_diagnostic::dump_as_dot_node (pretty_printer *pp) const
778 : : {
779 : 20 : dump_dot_id (pp);
780 : 20 : pp_printf (pp,
781 : : " [shape=none,margin=0,style=filled,fillcolor=\"red\",label=\"");
782 : 20 : pp_write_text_to_stream (pp);
783 : :
784 : : /* Node label. */
785 : 20 : pp_printf (pp, "DIAGNOSTIC: %s (sd: %i)\n",
786 : 20 : m_d->get_kind (), m_idx);
787 : 20 : if (m_sm)
788 : : {
789 : 20 : pp_printf (pp, "sm: %s", m_sm->get_name ());
790 : 20 : if (m_state)
791 : : {
792 : 20 : pp_printf (pp, "; state: ");
793 : 20 : m_state->dump_to_pp (pp);
794 : : }
795 : 20 : pp_newline (pp);
796 : : }
797 : 20 : if (m_stmt)
798 : : {
799 : 20 : pp_string (pp, "stmt: ");
800 : 20 : pp_gimple_stmt_1 (pp, m_stmt, 0, (dump_flags_t)0);
801 : 20 : pp_newline (pp);
802 : : }
803 : 20 : if (m_var)
804 : 20 : pp_printf (pp, "var: %qE\n", m_var);
805 : 20 : if (m_sval)
806 : : {
807 : 20 : pp_string (pp, "sval: ");
808 : 20 : m_sval->dump_to_pp (pp, true);
809 : 20 : pp_newline (pp);
810 : : }
811 : 20 : if (m_best_epath)
812 : 4 : pp_printf (pp, "path length: %i\n", get_epath_length ());
813 : :
814 : 20 : pp_write_text_as_dot_label_to_stream (pp, /*for_record=*/true);
815 : 20 : pp_string (pp, "\"];\n\n");
816 : :
817 : : /* Show links to duplicates. */
818 : 20 : for (auto iter : m_duplicates)
819 : : {
820 : 0 : dump_dot_id (pp);
821 : 0 : pp_string (pp, " -> ");
822 : 0 : iter->dump_dot_id (pp);
823 : 0 : pp_string (pp, " [style=\"dotted\" arrowhead=\"none\"];");
824 : 0 : pp_newline (pp);
825 : : }
826 : 20 : }
827 : :
828 : : /* Use PF to find the best exploded_path for this saved_diagnostic,
829 : : and store it in m_best_epath.
830 : : If we don't have a specific location in m_loc and m_stmt is still nullptr,
831 : : use m_stmt_finder on the epath to populate m_stmt.
832 : : Return true if a best path was found. */
833 : :
834 : : bool
835 : 6750 : saved_diagnostic::calc_best_epath (epath_finder *pf)
836 : : {
837 : 6750 : logger *logger = pf->get_logger ();
838 : 6750 : LOG_SCOPE (logger);
839 : 6750 : m_problem = nullptr;
840 : :
841 : 6750 : m_best_epath = pf->get_best_epath (m_enode, m_stmt,
842 : 6750 : *m_d, m_d->get_kind (), m_idx,
843 : 6750 : &m_problem);
844 : :
845 : : /* Handle failure to find a feasible path. */
846 : 6750 : if (m_best_epath == nullptr)
847 : : return false;
848 : :
849 : 6479 : gcc_assert (m_best_epath);
850 : 6479 : if (m_loc == UNKNOWN_LOCATION)
851 : : {
852 : 6392 : if (m_stmt == nullptr)
853 : : {
854 : 854 : gcc_assert (m_stmt_finder);
855 : 854 : m_stmt = m_stmt_finder->find_stmt (*m_best_epath);
856 : : }
857 : 6392 : gcc_assert (m_stmt);
858 : : }
859 : :
860 : : return true;
861 : 6750 : }
862 : :
863 : : unsigned
864 : 7862 : saved_diagnostic::get_epath_length () const
865 : : {
866 : 7862 : gcc_assert (m_best_epath);
867 : 7862 : return m_best_epath->length ();
868 : : }
869 : :
870 : : /* Record that OTHER (and its duplicates) are duplicates
871 : : of this saved_diagnostic. */
872 : :
873 : : void
874 : 2458 : saved_diagnostic::add_duplicate (saved_diagnostic *other)
875 : : {
876 : 2458 : gcc_assert (other);
877 : 2458 : m_duplicates.reserve (m_duplicates.length ()
878 : 2482 : + other->m_duplicates.length ()
879 : : + 1);
880 : 2458 : m_duplicates.splice (other->m_duplicates);
881 : 2458 : other->m_duplicates.truncate (0);
882 : 2458 : m_duplicates.safe_push (other);
883 : 2458 : }
884 : :
885 : : /* Walk up the sedges of each of the two paths.
886 : : If the two sequences of sedges do not perfectly correspond,
887 : : then paths are incompatible.
888 : : If there is at least one sedge that either cannot be paired up
889 : : or its counterpart is not equal, then the paths are incompatible
890 : : and this function returns FALSE.
891 : : Otherwise return TRUE.
892 : :
893 : : Incompatible paths:
894 : :
895 : : <cond Y>
896 : : / \
897 : : / \
898 : : true false
899 : : | |
900 : : ... ...
901 : : | |
902 : : ... stmt x
903 : : |
904 : : stmt x
905 : :
906 : : Both LHS_PATH and RHS_PATH final enodes should be
907 : : over the same gimple statement. */
908 : :
909 : : static bool
910 : 58 : compatible_epath_p (const exploded_path *lhs_path,
911 : : const exploded_path *rhs_path)
912 : : {
913 : 58 : gcc_assert (lhs_path);
914 : 58 : gcc_assert (rhs_path);
915 : 58 : gcc_assert (rhs_path->length () > 0);
916 : 58 : gcc_assert (rhs_path->length () > 0);
917 : 58 : int lhs_eedge_idx = lhs_path->length () - 1;
918 : 58 : int rhs_eedge_idx = rhs_path->length () - 1;
919 : 58 : const exploded_edge *lhs_eedge;
920 : 58 : const exploded_edge *rhs_eedge;
921 : :
922 : 277 : while (lhs_eedge_idx >= 0 && rhs_eedge_idx >= 0)
923 : : {
924 : 853 : while (lhs_eedge_idx >= 0)
925 : : {
926 : : /* Find LHS_PATH's next superedge. */
927 : 815 : lhs_eedge = lhs_path->m_edges[lhs_eedge_idx];
928 : 815 : if (lhs_eedge->m_sedge)
929 : : break;
930 : : else
931 : 576 : lhs_eedge_idx--;
932 : : }
933 : 853 : while (rhs_eedge_idx >= 0)
934 : : {
935 : : /* Find RHS_PATH's next superedge. */
936 : 815 : rhs_eedge = rhs_path->m_edges[rhs_eedge_idx];
937 : 815 : if (rhs_eedge->m_sedge)
938 : : break;
939 : : else
940 : 576 : rhs_eedge_idx--;
941 : : }
942 : :
943 : 277 : if (lhs_eedge->m_sedge && rhs_eedge->m_sedge)
944 : : {
945 : 239 : if (lhs_eedge->m_sedge != rhs_eedge->m_sedge)
946 : : /* Both superedges do not match.
947 : : Superedges are not dependent on the exploded path, so even
948 : : different epaths will have similar sedges if they follow
949 : : the same outcome of a conditional node. */
950 : : return false;
951 : :
952 : 219 : lhs_eedge_idx--;
953 : 219 : rhs_eedge_idx--;
954 : 219 : continue;
955 : : }
956 : 38 : else if (lhs_eedge->m_sedge == nullptr && rhs_eedge->m_sedge == nullptr)
957 : : /* Both paths were drained up entirely.
958 : : No discriminant was found. */
959 : : return true;
960 : :
961 : : /* A superedge was found for only one of the two paths. */
962 : : return false;
963 : : }
964 : :
965 : : /* A superedge was found for only one of the two paths. */
966 : 0 : if (lhs_eedge_idx >= 0 || rhs_eedge_idx >= 0)
967 : : return false;
968 : :
969 : : /* Both paths were drained up entirely.
970 : : No discriminant was found. */
971 : : return true;
972 : : }
973 : :
974 : :
975 : : /* Return true if this diagnostic supercedes OTHER, and that OTHER should
976 : : therefore not be emitted. */
977 : :
978 : : bool
979 : 29186 : saved_diagnostic::supercedes_p (const saved_diagnostic &other) const
980 : : {
981 : : /* They should be at the same stmt. */
982 : 29186 : if (m_stmt != other.m_stmt)
983 : : return false;
984 : : /* return early if OTHER won't be superseded anyway. */
985 : 5314 : if (!m_d->supercedes_p (*other.m_d))
986 : : return false;
987 : :
988 : : /* If the two saved_diagnostics' path are not compatible
989 : : then they cannot supersede one another. */
990 : 58 : return compatible_epath_p (m_best_epath.get (), other.m_best_epath.get ());
991 : : }
992 : :
993 : : /* Move any saved checker_events from this saved_diagnostic to
994 : : the end of DST_PATH. */
995 : :
996 : : void
997 : 3983 : saved_diagnostic::add_any_saved_events (checker_path &dst_path)
998 : : {
999 : 4421 : for (auto &event : m_saved_events)
1000 : : {
1001 : 146 : dst_path.add_event (std::unique_ptr<checker_event> (event));
1002 : 146 : event = nullptr;
1003 : : }
1004 : 3983 : }
1005 : :
1006 : : /* Emit any pending notes owned by this diagnostic. */
1007 : :
1008 : : void
1009 : 3915 : saved_diagnostic::emit_any_notes () const
1010 : : {
1011 : 4430 : for (auto pn : m_notes)
1012 : 177 : pn->emit ();
1013 : 3915 : }
1014 : :
1015 : : /* For SARIF output, add additional properties to the "result" object
1016 : : for this diagnostic.
1017 : : This extra data is intended for use when debugging the analyzer. */
1018 : :
1019 : : void
1020 : 23 : saved_diagnostic::maybe_add_sarif_properties (sarif_object &result_obj) const
1021 : : {
1022 : 23 : sarif_property_bag &props = result_obj.get_or_create_properties ();
1023 : : #define PROPERTY_PREFIX "gcc/analyzer/saved_diagnostic/"
1024 : 23 : if (m_sm)
1025 : 10 : props.set_string (PROPERTY_PREFIX "sm", m_sm->get_name ());
1026 : 23 : props.set_integer (PROPERTY_PREFIX "enode", m_enode->m_index);
1027 : 23 : props.set_integer (PROPERTY_PREFIX "snode", m_snode->m_index);
1028 : 23 : if (m_stmt)
1029 : : {
1030 : 23 : pretty_printer pp;
1031 : 23 : pp_gimple_stmt_1 (&pp, m_stmt, 0, (dump_flags_t)0);
1032 : 23 : props.set_string (PROPERTY_PREFIX "stmt", pp_formatted_text (&pp));
1033 : 23 : }
1034 : 23 : if (m_var)
1035 : 10 : props.set (PROPERTY_PREFIX "var", tree_to_json (m_var));
1036 : 23 : if (m_sval)
1037 : 10 : props.set (PROPERTY_PREFIX "sval", m_sval->to_json ());
1038 : 23 : if (m_state)
1039 : 10 : props.set (PROPERTY_PREFIX "state", m_state->to_json ());
1040 : : // TODO: m_best_epath
1041 : 23 : props.set_integer (PROPERTY_PREFIX "idx", m_idx);
1042 : 23 : if (m_duplicates.length () > 0)
1043 : : {
1044 : 4 : auto duplicates_arr = std::make_unique<json::array> ();
1045 : 16 : for (auto iter : m_duplicates)
1046 : : {
1047 : 4 : auto sd_obj = std::make_unique<sarif_object> ();
1048 : 4 : iter->maybe_add_sarif_properties (*sd_obj);
1049 : 4 : duplicates_arr->append (std::move (sd_obj));
1050 : 4 : }
1051 : 4 : props.set<json::array> (PROPERTY_PREFIX "duplicates",
1052 : : std::move (duplicates_arr));
1053 : 4 : }
1054 : : #undef PROPERTY_PREFIX
1055 : :
1056 : : #define PROPERTY_PREFIX "gcc/analyzer/pending_diagnostic/"
1057 : 23 : props.set_string (PROPERTY_PREFIX "kind", m_d->get_kind ());
1058 : : #undef PROPERTY_PREFIX
1059 : :
1060 : : /* Potentially add pending_diagnostic-specific properties. */
1061 : 23 : m_d->maybe_add_sarif_properties (result_obj);
1062 : 23 : }
1063 : :
1064 : : /* State for building a checker_path from a particular exploded_path.
1065 : : In particular, this precomputes reachability information: the set of
1066 : : source enodes for which a path be found to the diagnostic enode. */
1067 : :
1068 : 3983 : class path_builder
1069 : : {
1070 : : public:
1071 : 3983 : path_builder (const exploded_graph &eg,
1072 : : const exploded_path &epath,
1073 : : const feasibility_problem *problem,
1074 : : const saved_diagnostic &sd)
1075 : 3983 : : m_eg (eg),
1076 : 3983 : m_diag_enode (epath.get_final_enode ()),
1077 : 3983 : m_sd (sd),
1078 : 3983 : m_reachability (eg, m_diag_enode),
1079 : 3983 : m_feasibility_problem (problem)
1080 : 3983 : {}
1081 : :
1082 : 0 : const exploded_node *get_diag_node () const { return m_diag_enode; }
1083 : :
1084 : 24860 : pending_diagnostic *get_pending_diagnostic () const
1085 : : {
1086 : 207 : return m_sd.m_d.get ();
1087 : : }
1088 : :
1089 : 3127 : bool reachable_from_p (const exploded_node *src_enode) const
1090 : : {
1091 : 6254 : return m_reachability.reachable_from_p (src_enode);
1092 : : }
1093 : :
1094 : 278559 : const extrinsic_state &get_ext_state () const { return m_eg.get_ext_state (); }
1095 : :
1096 : 52294 : const feasibility_problem *get_feasibility_problem () const
1097 : : {
1098 : 52294 : return m_feasibility_problem;
1099 : : }
1100 : :
1101 : 5217 : const state_machine *get_sm () const { return m_sd.m_sm; }
1102 : :
1103 : : private:
1104 : : typedef reachability<eg_traits> enode_reachability;
1105 : :
1106 : : const exploded_graph &m_eg;
1107 : :
1108 : : /* The enode where the diagnostic occurs. */
1109 : : const exploded_node *m_diag_enode;
1110 : :
1111 : : const saved_diagnostic &m_sd;
1112 : :
1113 : : /* Precompute all enodes from which the diagnostic is reachable. */
1114 : : enode_reachability m_reachability;
1115 : :
1116 : : const feasibility_problem *m_feasibility_problem;
1117 : : };
1118 : :
1119 : : /* Determine the emission location for PD at STMT in FUN. */
1120 : :
1121 : : static location_t
1122 : 13752 : get_emission_location (const gimple *stmt, function *fun,
1123 : : const pending_diagnostic &pd)
1124 : : {
1125 : 13752 : location_t loc = get_stmt_location (stmt, fun);
1126 : :
1127 : : /* Allow the pending_diagnostic to fix up the location. */
1128 : 13752 : loc = pd.fixup_location (loc, true);
1129 : :
1130 : 13752 : return loc;
1131 : : }
1132 : :
1133 : : /* class diagnostic_manager. */
1134 : :
1135 : : /* diagnostic_manager's ctor. */
1136 : :
1137 : 3308 : diagnostic_manager::diagnostic_manager (logger *logger, engine *eng,
1138 : 3308 : int verbosity)
1139 : 3308 : : log_user (logger), m_eng (eng), m_verbosity (verbosity),
1140 : 3308 : m_num_disabled_diagnostics (0)
1141 : : {
1142 : 3308 : }
1143 : :
1144 : : /* Queue pending_diagnostic D at ENODE for later emission.
1145 : : Return true/false signifying if the diagnostic was actually added. */
1146 : :
1147 : : bool
1148 : 10822 : diagnostic_manager::add_diagnostic (const state_machine *sm,
1149 : : const pending_location &ploc,
1150 : : tree var,
1151 : : const svalue *sval,
1152 : : state_machine::state_t state,
1153 : : std::unique_ptr<pending_diagnostic> d)
1154 : : {
1155 : 10822 : LOG_FUNC (get_logger ());
1156 : :
1157 : : /* We must have an enode in order to be able to look for paths
1158 : : through the exploded_graph to the diagnostic. */
1159 : 10822 : gcc_assert (ploc.m_enode);
1160 : :
1161 : : /* If this warning is ultimately going to be rejected by a -Wno-analyzer-*
1162 : : flag, reject it now.
1163 : : We can only do this for diagnostics where we already know the stmt,
1164 : : and thus can determine the emission location. */
1165 : 10822 : if (ploc.m_stmt)
1166 : : {
1167 : 9848 : location_t loc
1168 : 9848 : = get_emission_location (ploc.m_stmt, ploc.m_snode->m_fun, *d);
1169 : 9848 : int option = d->get_controlling_option ();
1170 : 9848 : if (!warning_enabled_at (loc, option))
1171 : : {
1172 : 4072 : if (get_logger ())
1173 : 0 : get_logger ()->log ("rejecting disabled warning %qs",
1174 : 0 : d->get_kind ());
1175 : 4072 : m_num_disabled_diagnostics++;
1176 : 4072 : return false;
1177 : : }
1178 : : }
1179 : :
1180 : 6750 : saved_diagnostic *sd
1181 : : = new saved_diagnostic (sm, ploc, var, sval, state, std::move (d),
1182 : 11948 : m_saved_diagnostics.length ());
1183 : 6750 : m_saved_diagnostics.safe_push (sd);
1184 : 6750 : ploc.m_enode->add_diagnostic (sd);
1185 : 6750 : if (get_logger ())
1186 : 0 : log ("adding saved diagnostic %i at SN %i to EN %i: %qs",
1187 : : sd->get_index (),
1188 : 0 : ploc.m_snode->m_index, ploc.m_enode->m_index, sd->m_d->get_kind ());
1189 : : return true;
1190 : 10822 : }
1191 : :
1192 : : /* Queue pending_diagnostic D at ENODE for later emission.
1193 : : Return true/false signifying if the diagnostic was actually added.
1194 : : Take ownership of D (or delete it). */
1195 : :
1196 : : bool
1197 : 3567 : diagnostic_manager::add_diagnostic (const pending_location &ploc,
1198 : : std::unique_ptr<pending_diagnostic> d)
1199 : : {
1200 : 3567 : gcc_assert (ploc.m_enode);
1201 : 3567 : return add_diagnostic (nullptr, ploc, NULL_TREE, nullptr, 0, std::move (d));
1202 : : }
1203 : :
1204 : : /* Add PN to the most recent saved_diagnostic. */
1205 : :
1206 : : void
1207 : 224 : diagnostic_manager::add_note (std::unique_ptr<pending_note> pn)
1208 : : {
1209 : 224 : LOG_FUNC (get_logger ());
1210 : 224 : gcc_assert (pn);
1211 : :
1212 : : /* Get most recent saved_diagnostic. */
1213 : 224 : gcc_assert (m_saved_diagnostics.length () > 0);
1214 : 224 : saved_diagnostic *sd = m_saved_diagnostics[m_saved_diagnostics.length () - 1];
1215 : 224 : sd->add_note (std::move (pn));
1216 : 224 : }
1217 : :
1218 : : /* Add EVENT to the most recent saved_diagnostic. */
1219 : :
1220 : : void
1221 : 188 : diagnostic_manager::add_event (std::unique_ptr<checker_event> event)
1222 : : {
1223 : 188 : LOG_FUNC (get_logger ());
1224 : 188 : gcc_assert (event);
1225 : :
1226 : : /* Get most recent saved_diagnostic. */
1227 : 188 : gcc_assert (m_saved_diagnostics.length () > 0);
1228 : 188 : saved_diagnostic *sd = m_saved_diagnostics[m_saved_diagnostics.length () - 1];
1229 : 188 : sd->add_event (std::move (event));
1230 : 188 : }
1231 : :
1232 : : /* Return a new json::object of the form
1233 : : {"diagnostics" : [obj for saved_diagnostic]}. */
1234 : :
1235 : : std::unique_ptr<json::object>
1236 : 0 : diagnostic_manager::to_json () const
1237 : : {
1238 : 0 : auto dm_obj = std::make_unique<json::object> ();
1239 : :
1240 : 0 : {
1241 : 0 : auto sd_arr = std::make_unique<json::array> ();
1242 : 0 : int i;
1243 : 0 : saved_diagnostic *sd;
1244 : 0 : FOR_EACH_VEC_ELT (m_saved_diagnostics, i, sd)
1245 : 0 : sd_arr->append (sd->to_json ());
1246 : 0 : dm_obj->set ("diagnostics", std::move (sd_arr));
1247 : 0 : }
1248 : :
1249 : 0 : return dm_obj;
1250 : : }
1251 : :
1252 : : /* A class for identifying sets of duplicated pending_diagnostic.
1253 : :
1254 : : We want to find the simplest saved_diagnostic amongst those that share a
1255 : : dedupe_key. */
1256 : :
1257 : : class dedupe_key
1258 : : {
1259 : : public:
1260 : 6479 : dedupe_key (const saved_diagnostic &sd)
1261 : 6479 : : m_sd (sd), m_stmt (sd.m_stmt), m_loc (sd.m_loc)
1262 : : {
1263 : 6479 : gcc_assert (m_stmt || m_loc != UNKNOWN_LOCATION);
1264 : 6479 : }
1265 : :
1266 : 48615 : hashval_t hash () const
1267 : : {
1268 : 48615 : inchash::hash hstate;
1269 : 48615 : hstate.add_ptr (m_stmt);
1270 : : // TODO: m_sd
1271 : 48615 : return hstate.end ();
1272 : : }
1273 : 40458 : bool operator== (const dedupe_key &other) const
1274 : : {
1275 : 40458 : return (m_sd == other.m_sd
1276 : 6479 : && m_stmt == other.m_stmt
1277 : 46937 : && m_loc == other.m_loc);
1278 : : }
1279 : :
1280 : 60070 : location_t get_location () const
1281 : : {
1282 : 60070 : if (m_loc != UNKNOWN_LOCATION)
1283 : : return m_loc;
1284 : 58591 : gcc_assert (m_stmt);
1285 : 58591 : return m_stmt->location;
1286 : : }
1287 : :
1288 : : /* A qsort comparator for use by dedupe_winners::emit_best
1289 : : to sort them into location_t order. */
1290 : :
1291 : : static int
1292 : 30035 : comparator (const void *p1, const void *p2)
1293 : : {
1294 : 30035 : const dedupe_key *pk1 = *(const dedupe_key * const *)p1;
1295 : 30035 : const dedupe_key *pk2 = *(const dedupe_key * const *)p2;
1296 : :
1297 : 30035 : location_t loc1 = pk1->get_location ();
1298 : 30035 : location_t loc2 = pk2->get_location ();
1299 : :
1300 : 30035 : if (int cmp = linemap_compare_locations (line_table, loc2, loc1))
1301 : : return cmp;
1302 : 1467 : if (int cmp = ((int)pk1->m_sd.get_epath_length ()
1303 : 1467 : - (int)pk2->m_sd.get_epath_length ()))
1304 : : return cmp;
1305 : 979 : if (int cmp = strcmp (pk1->m_sd.m_d->get_kind (),
1306 : 979 : pk2->m_sd.m_d->get_kind ()))
1307 : : return cmp;
1308 : : return 0;
1309 : : }
1310 : :
1311 : : const saved_diagnostic &m_sd;
1312 : : const gimple *m_stmt;
1313 : : location_t m_loc;
1314 : : };
1315 : :
1316 : : /* Traits for use by dedupe_winners. */
1317 : :
1318 : : class dedupe_hash_map_traits
1319 : : {
1320 : : public:
1321 : : typedef const dedupe_key *key_type;
1322 : : typedef saved_diagnostic *value_type;
1323 : : typedef saved_diagnostic *compare_type;
1324 : :
1325 : 48615 : static inline hashval_t hash (const key_type &v)
1326 : : {
1327 : 48615 : return v->hash ();
1328 : : }
1329 : 40458 : static inline bool equal_keys (const key_type &k1, const key_type &k2)
1330 : : {
1331 : 40458 : return *k1 == *k2;
1332 : : }
1333 : : template <typename T>
1334 : : static inline void remove (T &)
1335 : : {
1336 : : // TODO
1337 : : }
1338 : : template <typename T>
1339 : 38 : static inline void mark_deleted (T &entry)
1340 : : {
1341 : 38 : entry.m_key = reinterpret_cast<key_type> (1);
1342 : : }
1343 : : template <typename T>
1344 : 0 : static inline void mark_empty (T &entry)
1345 : : {
1346 : 0 : entry.m_key = nullptr;
1347 : : }
1348 : : template <typename T>
1349 : 92856 : static inline bool is_deleted (const T &entry)
1350 : : {
1351 : 92856 : return entry.m_key == reinterpret_cast<key_type> (1);
1352 : : }
1353 : : template <typename T>
1354 : 439883 : static inline bool is_empty (const T &entry)
1355 : : {
1356 : 439883 : return entry.m_key == nullptr;
1357 : : }
1358 : : static const bool empty_zero_p = true;
1359 : : };
1360 : :
1361 : : /* A class for deduplicating diagnostics and finding (and emitting) the
1362 : : best saved_diagnostic within each partition. */
1363 : :
1364 : : class dedupe_winners
1365 : : {
1366 : : public:
1367 : 1552 : ~dedupe_winners ()
1368 : : {
1369 : : /* Delete all keys, but not the saved_diagnostics. */
1370 : 5535 : for (map_t::iterator iter = m_map.begin ();
1371 : 5535 : iter != m_map.end ();
1372 : 3983 : ++iter)
1373 : 3983 : delete (*iter).first;
1374 : 1552 : }
1375 : :
1376 : : /* Determine an exploded_path for SD using PF and, if it's feasible,
1377 : : determine if SD is the best seen so far for its dedupe_key.
1378 : : Record the winning SD for each dedupe_key. */
1379 : :
1380 : 6750 : void add (logger *logger,
1381 : : epath_finder *pf,
1382 : : saved_diagnostic *sd)
1383 : : {
1384 : : /* Determine best epath for SD. */
1385 : 6750 : if (!sd->calc_best_epath (pf))
1386 : 271 : return;
1387 : :
1388 : 6479 : dedupe_key *key = new dedupe_key (*sd);
1389 : 6479 : if (saved_diagnostic **slot = m_map.get (key))
1390 : : {
1391 : 2458 : if (logger)
1392 : 0 : logger->log ("already have this dedupe_key");
1393 : :
1394 : 2458 : saved_diagnostic *cur_best_sd = *slot;
1395 : :
1396 : 2458 : if (sd->get_epath_length () < cur_best_sd->get_epath_length ())
1397 : : {
1398 : : /* We've got a shorter path for the key; replace
1399 : : the current candidate, marking it as a duplicate of SD. */
1400 : 47 : if (logger)
1401 : 0 : logger->log ("length %i is better than existing length %i;"
1402 : : " taking over this dedupe_key",
1403 : : sd->get_epath_length (),
1404 : : cur_best_sd->get_epath_length ());
1405 : 47 : sd->add_duplicate (cur_best_sd);
1406 : 47 : *slot = sd;
1407 : : }
1408 : : else
1409 : : /* We haven't beaten the current best candidate; add SD
1410 : : as a duplicate of it. */
1411 : : {
1412 : 2411 : if (logger)
1413 : 0 : logger->log ("length %i isn't better than existing length %i;"
1414 : : " dropping this candidate",
1415 : : sd->get_epath_length (),
1416 : : cur_best_sd->get_epath_length ());
1417 : 2411 : cur_best_sd->add_duplicate (sd);
1418 : : }
1419 : 2458 : delete key;
1420 : : }
1421 : : else
1422 : : {
1423 : : /* This is the first candidate for this key. */
1424 : 4021 : m_map.put (key, sd);
1425 : 4021 : if (logger)
1426 : 0 : logger->log ("first candidate for this dedupe_key");
1427 : : }
1428 : : }
1429 : :
1430 : : /* Handle interactions between the dedupe winners, so that some
1431 : : diagnostics can supercede others (of different kinds).
1432 : :
1433 : : We want use-after-free to supercede use-of-unitialized-value,
1434 : : so that if we have these at the same stmt, we don't emit
1435 : : a use-of-uninitialized, just the use-after-free. */
1436 : :
1437 : 1552 : void handle_interactions (diagnostic_manager *dm)
1438 : : {
1439 : 1552 : LOG_SCOPE (dm->get_logger ());
1440 : 1552 : auto_vec<const dedupe_key *> superceded;
1441 : 5573 : for (auto outer : m_map)
1442 : : {
1443 : 4021 : const saved_diagnostic *outer_sd = outer.second;
1444 : 66338 : for (auto inner : m_map)
1445 : : {
1446 : 29186 : const saved_diagnostic *inner_sd = inner.second;
1447 : 29186 : if (inner_sd->supercedes_p (*outer_sd))
1448 : : {
1449 : 38 : superceded.safe_push (outer.first);
1450 : 38 : if (dm->get_logger ())
1451 : 0 : dm->log ("sd[%i] \"%s\" superceded by sd[%i] \"%s\"",
1452 : 0 : outer_sd->get_index (), outer_sd->m_d->get_kind (),
1453 : 0 : inner_sd->get_index (), inner_sd->m_d->get_kind ());
1454 : : break;
1455 : : }
1456 : : }
1457 : : }
1458 : 1666 : for (auto iter : superceded)
1459 : 38 : m_map.remove (iter);
1460 : 1552 : }
1461 : :
1462 : : /* Emit the simplest diagnostic within each set. */
1463 : :
1464 : 1552 : void emit_best (diagnostic_manager *dm,
1465 : : const exploded_graph &eg)
1466 : : {
1467 : 1552 : LOG_SCOPE (dm->get_logger ());
1468 : :
1469 : : /* Get keys into a vec for sorting. */
1470 : 1552 : auto_vec<const dedupe_key *> keys (m_map.elements ());
1471 : 1552 : for (map_t::iterator iter = m_map.begin ();
1472 : 5535 : iter != m_map.end ();
1473 : 3983 : ++iter)
1474 : 3983 : keys.quick_push ((*iter).first);
1475 : :
1476 : 3050 : dm->log ("# keys after de-duplication: %i", keys.length ());
1477 : :
1478 : : /* Sort into a good emission order. */
1479 : 1552 : keys.qsort (dedupe_key::comparator);
1480 : :
1481 : : /* Emit the best saved_diagnostics for each key. */
1482 : : int i;
1483 : : const dedupe_key *key;
1484 : 7033 : FOR_EACH_VEC_ELT (keys, i, key)
1485 : : {
1486 : 3983 : saved_diagnostic **slot = m_map.get (key);
1487 : 3983 : gcc_assert (*slot);
1488 : 3983 : saved_diagnostic *sd = *slot;
1489 : 3983 : dm->emit_saved_diagnostic (eg, *sd);
1490 : : }
1491 : 1552 : }
1492 : :
1493 : : private:
1494 : : /* This maps from each dedupe_key to a current best saved_diagnostic. */
1495 : :
1496 : : typedef hash_map<const dedupe_key *, saved_diagnostic *,
1497 : : dedupe_hash_map_traits> map_t;
1498 : : map_t m_map;
1499 : : };
1500 : :
1501 : : /* Emit all saved diagnostics. */
1502 : :
1503 : : void
1504 : 3308 : diagnostic_manager::emit_saved_diagnostics (const exploded_graph &eg)
1505 : : {
1506 : 3308 : LOG_SCOPE (get_logger ());
1507 : 3308 : auto_timevar tv (TV_ANALYZER_DIAGNOSTICS);
1508 : 4860 : log ("# saved diagnostics: %i", m_saved_diagnostics.length ());
1509 : 3308 : log ("# disabled diagnostics: %i", m_num_disabled_diagnostics);
1510 : 3308 : if (get_logger ())
1511 : : {
1512 : : unsigned i;
1513 : : saved_diagnostic *sd;
1514 : 2 : FOR_EACH_VEC_ELT (m_saved_diagnostics, i, sd)
1515 : 0 : log ("[%i] sd: %qs at EN: %i, SN: %i",
1516 : 0 : i, sd->m_d->get_kind (), sd->m_enode->m_index,
1517 : 0 : sd->m_snode->m_index);
1518 : : }
1519 : :
1520 : 3308 : if (m_saved_diagnostics.length () == 0)
1521 : 1756 : return;
1522 : :
1523 : : /* Compute the shortest_paths once, sharing it between all diagnostics. */
1524 : 1552 : epath_finder pf (eg);
1525 : :
1526 : : /* Iterate through all saved diagnostics, adding them to a dedupe_winners
1527 : : instance. This partitions the saved diagnostics by dedupe_key,
1528 : : generating exploded_paths for them, and retaining the best one in each
1529 : : partition. */
1530 : 1552 : dedupe_winners best_candidates;
1531 : :
1532 : 1552 : int i;
1533 : 1552 : saved_diagnostic *sd;
1534 : 9854 : FOR_EACH_VEC_ELT (m_saved_diagnostics, i, sd)
1535 : 6750 : best_candidates.add (get_logger (), &pf, sd);
1536 : :
1537 : 1552 : best_candidates.handle_interactions (this);
1538 : :
1539 : : /* For each dedupe-key, call emit_saved_diagnostic on the "best"
1540 : : saved_diagnostic. */
1541 : 1552 : best_candidates.emit_best (this, eg);
1542 : 3308 : }
1543 : :
1544 : : /* Custom subclass of diagnostic_metadata which, for SARIF output,
1545 : : populates the property bag of the diagnostic's "result" object
1546 : : with information from the saved_diagnostic and the
1547 : : pending_diagnostic. */
1548 : :
1549 : 3983 : class pending_diagnostic_metadata : public diagnostic_metadata
1550 : : {
1551 : : public:
1552 : 3983 : pending_diagnostic_metadata (const saved_diagnostic &sd)
1553 : 3983 : : m_sd (sd)
1554 : : {
1555 : : }
1556 : :
1557 : : void
1558 : 19 : maybe_add_sarif_properties (sarif_object &result_obj) const override
1559 : : {
1560 : 19 : m_sd.maybe_add_sarif_properties (result_obj);
1561 : 19 : }
1562 : :
1563 : : private:
1564 : : const saved_diagnostic &m_sd;
1565 : : };
1566 : :
1567 : : /* Given a saved_diagnostic SD with m_best_epath through EG,
1568 : : create an checker_path of suitable events and use it to call
1569 : : SD's underlying pending_diagnostic "emit" vfunc to emit a diagnostic. */
1570 : :
1571 : : void
1572 : 3983 : diagnostic_manager::emit_saved_diagnostic (const exploded_graph &eg,
1573 : : saved_diagnostic &sd)
1574 : : {
1575 : 3983 : LOG_SCOPE (get_logger ());
1576 : 3983 : log ("sd[%i]: %qs at SN: %i",
1577 : 3983 : sd.get_index (), sd.m_d->get_kind (), sd.m_snode->m_index);
1578 : 5706 : log ("num dupes: %i", sd.get_num_dupes ());
1579 : :
1580 : 3983 : const exploded_path *epath = sd.get_best_epath ();
1581 : 3983 : gcc_assert (epath);
1582 : :
1583 : : /* Precompute all enodes from which the diagnostic is reachable. */
1584 : 3983 : path_builder pb (eg, *epath, sd.get_feasibility_problem (), sd);
1585 : :
1586 : : /* This is the diagnostic_path subclass that will be built for
1587 : : the diagnostic. */
1588 : 3983 : checker_path emission_path (get_logical_location_manager (),
1589 : : eg.get_ext_state (),
1590 : 3983 : get_logger ());
1591 : :
1592 : : /* Populate emission_path with a full description of EPATH. */
1593 : 3983 : build_emission_path (pb, *epath, &emission_path);
1594 : :
1595 : : /* Now prune it to just cover the most pertinent events. */
1596 : 3983 : prune_path (&emission_path, sd.m_sm, sd.m_sval, sd.m_state);
1597 : :
1598 : : /* Add any saved events to the path, giving contextual information
1599 : : about what the analyzer was simulating as the diagnostic was
1600 : : generated. These don't get pruned, as they are probably pertinent. */
1601 : 3983 : sd.add_any_saved_events (emission_path);
1602 : :
1603 : : /* Add a final event to the path, covering the diagnostic itself.
1604 : : We use the final enode from the epath, which might be different from
1605 : : the sd.m_enode, as the dedupe code doesn't care about enodes, just
1606 : : snodes. */
1607 : 3983 : {
1608 : 3983 : const exploded_node *const enode = epath->get_final_enode ();
1609 : 3983 : const gimple *stmt = sd.m_stmt;
1610 : 3983 : event_loc_info loc_info (get_stmt_location (stmt, enode->get_function ()),
1611 : 3983 : enode->get_function ()->decl,
1612 : 3983 : enode->get_stack_depth ());
1613 : 3983 : if (sd.m_stmt_finder)
1614 : 590 : sd.m_stmt_finder->update_event_loc_info (loc_info);
1615 : 3983 : sd.m_d->add_final_event (sd.m_sm, enode, loc_info,
1616 : : sd.m_var, sd.m_state, &emission_path);
1617 : : }
1618 : :
1619 : : /* The "final" event might not be final; if the saved_diagnostic has a
1620 : : trailing eedge stashed, add any events for it. This is for use
1621 : : in handling longjmp, to show where a longjmp is rewinding to. */
1622 : 3983 : if (sd.m_trailing_eedge)
1623 : 4 : add_events_for_eedge (pb, *sd.m_trailing_eedge, &emission_path, nullptr);
1624 : :
1625 : 3983 : emission_path.inject_any_inlined_call_events (get_logger ());
1626 : :
1627 : 3983 : emission_path.prepare_for_emission (sd.m_d.get ());
1628 : :
1629 : 3983 : location_t loc = sd.m_loc;
1630 : 3983 : if (loc == UNKNOWN_LOCATION)
1631 : 3904 : loc = get_emission_location (sd.m_stmt, sd.m_snode->m_fun, *sd.m_d);
1632 : :
1633 : : /* Allow the pending_diagnostic to fix up the locations of events. */
1634 : 3983 : emission_path.fixup_locations (sd.m_d.get ());
1635 : :
1636 : 3983 : gcc_rich_location rich_loc (loc);
1637 : 3983 : rich_loc.set_path (&emission_path);
1638 : :
1639 : 3983 : auto_diagnostic_group d;
1640 : 3983 : auto_cfun sentinel (sd.m_snode->m_fun);
1641 : 3983 : pending_diagnostic_metadata m (sd);
1642 : 3983 : diagnostic_emission_context diag_ctxt (sd, rich_loc, m, get_logger ());
1643 : 3983 : if (sd.m_d->emit (diag_ctxt))
1644 : : {
1645 : 3915 : sd.emit_any_notes ();
1646 : :
1647 : 3915 : unsigned num_dupes = sd.get_num_dupes ();
1648 : 3915 : if (flag_analyzer_show_duplicate_count && num_dupes > 0)
1649 : 10 : inform_n (loc, num_dupes,
1650 : : "%i duplicate", "%i duplicates",
1651 : : num_dupes);
1652 : 3915 : if (flag_dump_analyzer_exploded_paths)
1653 : : {
1654 : 0 : auto_timevar tv (TV_ANALYZER_DUMP);
1655 : 0 : pretty_printer pp;
1656 : 0 : pp_printf (&pp, "%s.%i.%s.epath.txt",
1657 : 0 : dump_base_name, sd.get_index (), sd.m_d->get_kind ());
1658 : 0 : char *filename = xstrdup (pp_formatted_text (&pp));
1659 : 0 : epath->dump_to_file (filename, eg.get_ext_state ());
1660 : 0 : inform (loc, "exploded path written to %qs", filename);
1661 : 0 : free (filename);
1662 : 0 : }
1663 : : }
1664 : 3983 : }
1665 : :
1666 : : const logical_location_manager &
1667 : 3983 : diagnostic_manager::get_logical_location_manager () const
1668 : : {
1669 : 3983 : gcc_assert (global_dc);
1670 : 3983 : auto mgr = global_dc->get_logical_location_manager ();
1671 : 3983 : gcc_assert (mgr);
1672 : 3983 : return *mgr;
1673 : : }
1674 : :
1675 : : /* Emit a "path" of events to EMISSION_PATH describing the exploded path
1676 : : EPATH within EG. */
1677 : :
1678 : : void
1679 : 3983 : diagnostic_manager::build_emission_path (const path_builder &pb,
1680 : : const exploded_path &epath,
1681 : : checker_path *emission_path) const
1682 : : {
1683 : 3983 : LOG_SCOPE (get_logger ());
1684 : :
1685 : 3983 : interesting_t interest;
1686 : 3983 : pb.get_pending_diagnostic ()->mark_interesting_stuff (&interest);
1687 : :
1688 : : /* Add region creation events for any globals of interest, at the
1689 : : beginning of the path. */
1690 : 3983 : {
1691 : 7902 : for (auto reg : interest.m_region_creation)
1692 : 1325 : switch (reg->get_memory_space ())
1693 : : {
1694 : 1109 : default:
1695 : 1109 : continue;
1696 : 216 : case MEMSPACE_CODE:
1697 : 216 : case MEMSPACE_GLOBALS:
1698 : 216 : case MEMSPACE_READONLY_DATA:
1699 : 216 : {
1700 : 216 : const region *base_reg = reg->get_base_region ();
1701 : 216 : if (tree decl = base_reg->maybe_get_decl ())
1702 : 207 : if (DECL_P (decl)
1703 : 207 : && DECL_SOURCE_LOCATION (decl) != UNKNOWN_LOCATION)
1704 : : {
1705 : 207 : emission_path->add_region_creation_events
1706 : 207 : (pb.get_pending_diagnostic (),
1707 : : reg, nullptr,
1708 : 207 : event_loc_info (DECL_SOURCE_LOCATION (decl),
1709 : : NULL_TREE,
1710 : 207 : 0),
1711 : 207 : m_verbosity > 3);
1712 : : }
1713 : : }
1714 : 1109 : }
1715 : : }
1716 : :
1717 : : /* Walk EPATH, adding events as appropriate. */
1718 : 56269 : for (unsigned i = 0; i < epath.m_edges.length (); i++)
1719 : : {
1720 : 52286 : const exploded_edge *eedge = epath.m_edges[i];
1721 : 52286 : add_events_for_eedge (pb, *eedge, emission_path, &interest);
1722 : : }
1723 : 3983 : add_event_on_final_node (pb, epath.get_final_enode (),
1724 : : emission_path, &interest);
1725 : 3983 : }
1726 : :
1727 : : /* Emit a region_creation_event when requested on the last statement in
1728 : : the path.
1729 : :
1730 : : If a region_creation_event should be emitted on the last statement of the
1731 : : path, we need to peek to the successors to get whether the final enode
1732 : : created a region.
1733 : : */
1734 : :
1735 : : void
1736 : 3983 : diagnostic_manager::add_event_on_final_node (const path_builder &pb,
1737 : : const exploded_node *final_enode,
1738 : : checker_path *emission_path,
1739 : : interesting_t *interest) const
1740 : : {
1741 : 3983 : const program_point &src_point = final_enode->get_point ();
1742 : 3983 : const int src_stack_depth = src_point.get_stack_depth ();
1743 : 3983 : const program_state &src_state = final_enode->get_state ();
1744 : 3983 : const region_model *src_model = src_state.m_region_model;
1745 : :
1746 : 3983 : unsigned j;
1747 : 3983 : exploded_edge *e;
1748 : 6948 : FOR_EACH_VEC_ELT (final_enode->m_succs, j, e)
1749 : : {
1750 : 3026 : exploded_node *dst = e->m_dest;
1751 : 3026 : const program_state &dst_state = dst->get_state ();
1752 : 3026 : const region_model *dst_model = dst_state.m_region_model;
1753 : 3026 : if (src_model->get_dynamic_extents ()
1754 : 3026 : != dst_model->get_dynamic_extents ())
1755 : : {
1756 : : unsigned i;
1757 : : const region *reg;
1758 : : bool emitted = false;
1759 : 270 : FOR_EACH_VEC_ELT (interest->m_region_creation, i, reg)
1760 : : {
1761 : 68 : const region *base_reg = reg->get_base_region ();
1762 : 68 : const svalue *old_extents
1763 : 68 : = src_model->get_dynamic_extents (base_reg);
1764 : 68 : const svalue *new_extents
1765 : 68 : = dst_model->get_dynamic_extents (base_reg);
1766 : 68 : if (old_extents == nullptr && new_extents != nullptr)
1767 : 61 : switch (base_reg->get_kind ())
1768 : : {
1769 : : default:
1770 : : break;
1771 : 61 : case RK_HEAP_ALLOCATED:
1772 : 61 : case RK_ALLOCA:
1773 : 61 : emission_path->add_region_creation_events
1774 : 61 : (pb.get_pending_diagnostic (),
1775 : : reg,
1776 : : dst_model,
1777 : 61 : event_loc_info (src_point.get_location (),
1778 : : src_point.get_fndecl (),
1779 : 61 : src_stack_depth),
1780 : : false);
1781 : 61 : emitted = true;
1782 : 61 : break;
1783 : : }
1784 : : }
1785 : 202 : if (emitted)
1786 : : break;
1787 : : }
1788 : : }
1789 : 3983 : }
1790 : :
1791 : : /* Subclass of state_change_visitor that creates state_change_event
1792 : : instances. */
1793 : :
1794 : 52290 : class state_change_event_creator : public state_change_visitor
1795 : : {
1796 : : public:
1797 : 52290 : state_change_event_creator (const path_builder &pb,
1798 : : const exploded_edge &eedge,
1799 : : checker_path *emission_path)
1800 : 52290 : : m_pb (pb),
1801 : 52290 : m_eedge (eedge),
1802 : 52290 : m_emission_path (emission_path)
1803 : : {}
1804 : :
1805 : 97 : bool on_global_state_change (const state_machine &sm,
1806 : : state_machine::state_t src_sm_val,
1807 : : state_machine::state_t dst_sm_val)
1808 : : final override
1809 : : {
1810 : 97 : if (&sm != m_pb.get_sm ())
1811 : : return false;
1812 : 55 : const exploded_node *src_node = m_eedge.m_src;
1813 : 55 : const program_point &src_point = src_node->get_point ();
1814 : 55 : const int src_stack_depth = src_point.get_stack_depth ();
1815 : 55 : const exploded_node *dst_node = m_eedge.m_dest;
1816 : 55 : const gimple *stmt = src_point.get_stmt ();
1817 : 55 : const supernode *supernode = src_point.get_supernode ();
1818 : 55 : const program_state &dst_state = dst_node->get_state ();
1819 : :
1820 : 55 : int stack_depth = src_stack_depth;
1821 : :
1822 : 55 : m_emission_path->add_event
1823 : 55 : (std::make_unique<state_change_event> (supernode,
1824 : : stmt,
1825 : : stack_depth,
1826 : : sm,
1827 : 110 : nullptr,
1828 : : src_sm_val,
1829 : : dst_sm_val,
1830 : 55 : nullptr,
1831 : : dst_state,
1832 : : src_node));
1833 : 55 : return false;
1834 : : }
1835 : :
1836 : 5120 : bool on_state_change (const state_machine &sm,
1837 : : state_machine::state_t src_sm_val,
1838 : : state_machine::state_t dst_sm_val,
1839 : : const svalue *sval,
1840 : : const svalue *dst_origin_sval) final override
1841 : : {
1842 : 5120 : if (&sm != m_pb.get_sm ())
1843 : : return false;
1844 : 4162 : const exploded_node *src_node = m_eedge.m_src;
1845 : 4162 : const program_point &src_point = src_node->get_point ();
1846 : 4162 : const int src_stack_depth = src_point.get_stack_depth ();
1847 : 4162 : const exploded_node *dst_node = m_eedge.m_dest;
1848 : 4162 : const gimple *stmt = src_point.get_stmt ();
1849 : 4162 : const supernode *supernode = src_point.get_supernode ();
1850 : 4162 : const program_state &dst_state = dst_node->get_state ();
1851 : :
1852 : 4162 : int stack_depth = src_stack_depth;
1853 : :
1854 : 4162 : if (m_eedge.m_sedge
1855 : 315 : && m_eedge.m_sedge->m_kind == SUPEREDGE_CFG_EDGE)
1856 : : {
1857 : 315 : supernode = src_point.get_supernode ();
1858 : 315 : stmt = supernode->get_last_stmt ();
1859 : 315 : stack_depth = src_stack_depth;
1860 : : }
1861 : :
1862 : : /* Bulletproofing for state changes at calls/returns;
1863 : : TODO: is there a better way? */
1864 : 4162 : if (!stmt)
1865 : : return false;
1866 : :
1867 : 4099 : m_emission_path->add_event
1868 : 4099 : (std::make_unique<state_change_event> (supernode,
1869 : : stmt,
1870 : : stack_depth,
1871 : : sm,
1872 : : sval,
1873 : : src_sm_val,
1874 : : dst_sm_val,
1875 : : dst_origin_sval,
1876 : : dst_state,
1877 : : src_node));
1878 : 4099 : return false;
1879 : : }
1880 : :
1881 : : const path_builder &m_pb;
1882 : : const exploded_edge &m_eedge;
1883 : : checker_path *m_emission_path;
1884 : : };
1885 : :
1886 : : /* Compare SRC_STATE and DST_STATE (which use EXT_STATE), and call
1887 : : VISITOR's on_state_change for every sm-state change that occurs
1888 : : to a tree, and on_global_state_change for every global state change
1889 : : that occurs.
1890 : :
1891 : : This determines the state changes that ought to be reported to
1892 : : the user: a combination of the effects of changes to sm_state_map
1893 : : (which maps svalues to sm-states), and of region_model changes
1894 : : (which map trees to svalues).
1895 : :
1896 : : Bail out early and return true if any call to on_global_state_change
1897 : : or on_state_change returns true, otherwise return false.
1898 : :
1899 : : This is split out to make it easier to experiment with changes to
1900 : : exploded_node granularity (so that we can observe what state changes
1901 : : lead to state_change_events being emitted). */
1902 : :
1903 : : bool
1904 : 52290 : for_each_state_change (const program_state &src_state,
1905 : : const program_state &dst_state,
1906 : : const extrinsic_state &ext_state,
1907 : : state_change_visitor *visitor)
1908 : : {
1909 : 104580 : gcc_assert (src_state.m_checker_states.length ()
1910 : : == ext_state.get_num_checkers ());
1911 : 104580 : gcc_assert (dst_state.m_checker_states.length ()
1912 : : == ext_state.get_num_checkers ());
1913 : 415792 : for (unsigned i = 0; i < ext_state.get_num_checkers (); i++)
1914 : : {
1915 : 363502 : const state_machine &sm = ext_state.get_sm (i);
1916 : 363502 : const sm_state_map &src_smap = *src_state.m_checker_states[i];
1917 : 363502 : const sm_state_map &dst_smap = *dst_state.m_checker_states[i];
1918 : :
1919 : : /* Add events for any global state changes. */
1920 : 363502 : if (src_smap.get_global_state () != dst_smap.get_global_state ())
1921 : 97 : if (visitor->on_global_state_change (sm,
1922 : : src_smap.get_global_state (),
1923 : : dst_smap.get_global_state ()))
1924 : : return true;
1925 : :
1926 : : /* Add events for per-svalue state changes. */
1927 : 422462 : for (sm_state_map::iterator_t iter = dst_smap.begin ();
1928 : 785964 : iter != dst_smap.end ();
1929 : 58960 : ++iter)
1930 : : {
1931 : 58960 : const svalue *sval = (*iter).first;
1932 : 58960 : state_machine::state_t dst_sm_val = (*iter).second.m_state;
1933 : 58960 : state_machine::state_t src_sm_val
1934 : 58960 : = src_smap.get_state (sval, ext_state);
1935 : 58960 : if (dst_sm_val != src_sm_val)
1936 : : {
1937 : 5120 : const svalue *origin_sval = (*iter).second.m_origin;
1938 : 5120 : if (visitor->on_state_change (sm, src_sm_val, dst_sm_val,
1939 : : sval, origin_sval))
1940 : 0 : return true;
1941 : : }
1942 : : }
1943 : : }
1944 : : return false;
1945 : : }
1946 : :
1947 : : /* An sm_context for adding state_change_event on assignments to NULL,
1948 : : where the default state isn't m_start. Storing such state in the
1949 : : sm_state_map would lead to bloat of the exploded_graph, so we want
1950 : : to leave it as a default state, and inject state change events here
1951 : : when we have a diagnostic.
1952 : : Find transitions of constants, for handling on_zero_assignment. */
1953 : :
1954 : 197931 : struct null_assignment_sm_context : public sm_context
1955 : : {
1956 : 197931 : null_assignment_sm_context (int sm_idx,
1957 : : const state_machine &sm,
1958 : : const program_state *old_state,
1959 : : const program_state *new_state,
1960 : : const gimple *stmt,
1961 : : const program_point *point,
1962 : : checker_path *emission_path,
1963 : : const extrinsic_state &ext_state)
1964 : 197931 : : sm_context (sm_idx, sm), m_old_state (old_state), m_new_state (new_state),
1965 : 197931 : m_stmt (stmt), m_point (point), m_emission_path (emission_path),
1966 : 197931 : m_ext_state (ext_state)
1967 : : {
1968 : : }
1969 : :
1970 : 0 : tree get_fndecl_for_call (const gcall &/*call*/) final override
1971 : : {
1972 : 0 : return NULL_TREE;
1973 : : }
1974 : :
1975 : 10716 : state_machine::state_t get_state (const gimple *stmt ATTRIBUTE_UNUSED,
1976 : : tree var) final override
1977 : : {
1978 : 10716 : const svalue *var_old_sval
1979 : 10716 : = m_old_state->m_region_model->get_rvalue (var, nullptr);
1980 : 10716 : const sm_state_map *old_smap = m_old_state->m_checker_states[m_sm_idx];
1981 : :
1982 : 10716 : state_machine::state_t current
1983 : 10716 : = old_smap->get_state (var_old_sval, m_ext_state);
1984 : :
1985 : 10716 : return current;
1986 : : }
1987 : :
1988 : 57 : state_machine::state_t get_state (const gimple *stmt ATTRIBUTE_UNUSED,
1989 : : const svalue *sval) final override
1990 : : {
1991 : 57 : const sm_state_map *old_smap = m_old_state->m_checker_states[m_sm_idx];
1992 : 57 : state_machine::state_t current = old_smap->get_state (sval, m_ext_state);
1993 : 57 : return current;
1994 : : }
1995 : :
1996 : 3984 : void set_next_state (const gimple *stmt,
1997 : : tree var,
1998 : : state_machine::state_t to,
1999 : : tree origin ATTRIBUTE_UNUSED) final override
2000 : : {
2001 : 3984 : state_machine::state_t from = get_state (stmt, var);
2002 : 3984 : if (from != m_sm.get_start_state ())
2003 : 3410 : return;
2004 : 2966 : if (!is_transition_to_null (to))
2005 : : return;
2006 : :
2007 : 574 : const svalue *var_new_sval
2008 : 574 : = m_new_state->m_region_model->get_rvalue (var, nullptr);
2009 : :
2010 : 574 : const supernode *supernode = m_point->get_supernode ();
2011 : 574 : int stack_depth = m_point->get_stack_depth ();
2012 : :
2013 : 574 : m_emission_path->add_event
2014 : 574 : (std::make_unique<state_change_event> (supernode,
2015 : 574 : m_stmt,
2016 : : stack_depth,
2017 : : m_sm,
2018 : : var_new_sval,
2019 : : from, to,
2020 : 1148 : nullptr,
2021 : 574 : *m_new_state,
2022 : 1148 : nullptr));
2023 : : }
2024 : :
2025 : 12 : void set_next_state (const gimple *stmt,
2026 : : const svalue *sval,
2027 : : state_machine::state_t to,
2028 : : tree origin ATTRIBUTE_UNUSED) final override
2029 : : {
2030 : 12 : state_machine::state_t from = get_state (stmt, sval);
2031 : 12 : if (from != m_sm.get_start_state ())
2032 : 12 : return;
2033 : 0 : if (!is_transition_to_null (to))
2034 : : return;
2035 : :
2036 : 0 : const supernode *supernode = m_point->get_supernode ();
2037 : 0 : int stack_depth = m_point->get_stack_depth ();
2038 : :
2039 : 0 : m_emission_path->add_event
2040 : 0 : (std::make_unique<state_change_event> (supernode,
2041 : 0 : m_stmt,
2042 : : stack_depth,
2043 : : m_sm,
2044 : : sval,
2045 : : from, to,
2046 : 0 : nullptr,
2047 : 0 : *m_new_state,
2048 : 0 : nullptr));
2049 : : }
2050 : :
2051 : 1015 : void warn (const supernode *, const gimple *,
2052 : : tree, std::unique_ptr<pending_diagnostic>) final override
2053 : : {
2054 : 1015 : }
2055 : 0 : void warn (const supernode *, const gimple *,
2056 : : const svalue *, std::unique_ptr<pending_diagnostic>) final override
2057 : : {
2058 : 0 : }
2059 : :
2060 : 1015 : tree get_diagnostic_tree (tree expr) final override
2061 : : {
2062 : 1015 : return expr;
2063 : : }
2064 : :
2065 : 0 : tree get_diagnostic_tree (const svalue *sval) final override
2066 : : {
2067 : 0 : return m_new_state->m_region_model->get_representative_tree (sval);
2068 : : }
2069 : :
2070 : 28283 : state_machine::state_t get_global_state () const final override
2071 : : {
2072 : 28283 : return 0;
2073 : : }
2074 : :
2075 : 0 : void set_global_state (state_machine::state_t) final override
2076 : : {
2077 : : /* No-op. */
2078 : 0 : }
2079 : :
2080 : 0 : void clear_all_per_svalue_state () final override
2081 : : {
2082 : : /* No-op. */
2083 : 0 : }
2084 : :
2085 : 0 : void on_custom_transition (custom_transition *) final override
2086 : : {
2087 : 0 : }
2088 : :
2089 : 28320 : tree is_zero_assignment (const gimple *stmt) final override
2090 : : {
2091 : 53986 : const gassign *assign_stmt = dyn_cast <const gassign *> (stmt);
2092 : 28320 : if (!assign_stmt)
2093 : : return NULL_TREE;
2094 : 56640 : if (const svalue *sval
2095 : 28320 : = m_new_state->m_region_model->get_gassign_result (assign_stmt, nullptr))
2096 : 27438 : if (tree cst = sval->maybe_get_constant ())
2097 : 6010 : if (::zerop(cst))
2098 : 2654 : return gimple_assign_lhs (assign_stmt);
2099 : : return NULL_TREE;
2100 : : }
2101 : :
2102 : 3491 : const program_state *get_old_program_state () const final override
2103 : : {
2104 : 3491 : return m_old_state;
2105 : : }
2106 : 0 : const program_state *get_new_program_state () const final override
2107 : : {
2108 : 0 : return m_new_state;
2109 : : }
2110 : :
2111 : : /* We only care about transitions to the "null" state
2112 : : within sm-malloc. Special-case this. */
2113 : 2966 : static bool is_transition_to_null (state_machine::state_t s)
2114 : : {
2115 : 2966 : return !strcmp (s->get_name (), "null");
2116 : : }
2117 : :
2118 : : const program_state *m_old_state;
2119 : : const program_state *m_new_state;
2120 : : const gimple *m_stmt;
2121 : : const program_point *m_point;
2122 : : checker_path *m_emission_path;
2123 : : const extrinsic_state &m_ext_state;
2124 : : };
2125 : :
2126 : : /* Subroutine of diagnostic_manager::build_emission_path.
2127 : : Add any events for EEDGE to EMISSION_PATH. */
2128 : :
2129 : : void
2130 : 52290 : diagnostic_manager::add_events_for_eedge (const path_builder &pb,
2131 : : const exploded_edge &eedge,
2132 : : checker_path *emission_path,
2133 : : interesting_t *interest) const
2134 : : {
2135 : 52290 : const exploded_node *src_node = eedge.m_src;
2136 : 52290 : const program_point &src_point = src_node->get_point ();
2137 : 52290 : const int src_stack_depth = src_point.get_stack_depth ();
2138 : 52290 : const exploded_node *dst_node = eedge.m_dest;
2139 : 52290 : const program_point &dst_point = dst_node->get_point ();
2140 : 52290 : const int dst_stack_depth = dst_point.get_stack_depth ();
2141 : 52290 : if (get_logger ())
2142 : : {
2143 : 0 : get_logger ()->start_log_line ();
2144 : 0 : pretty_printer *pp = get_logger ()->get_printer ();
2145 : 0 : pp_printf (pp, "EN %i -> EN %i: ",
2146 : 0 : eedge.m_src->m_index,
2147 : 0 : eedge.m_dest->m_index);
2148 : 0 : src_point.print (pp, format (false));
2149 : 0 : pp_string (pp, "-> ");
2150 : 0 : dst_point.print (pp, format (false));
2151 : 0 : get_logger ()->end_log_line ();
2152 : : }
2153 : 52290 : const program_state &src_state = src_node->get_state ();
2154 : 52290 : const program_state &dst_state = dst_node->get_state ();
2155 : :
2156 : : /* Add state change events for the states that have changed.
2157 : : We add these before events for superedges, so that if we have a
2158 : : state_change_event due to following an edge, we'll get this sequence
2159 : : of events:
2160 : :
2161 : : | if (!ptr)
2162 : : | ~
2163 : : | |
2164 : : | (1) assuming 'ptr' is non-NULL (state_change_event)
2165 : : | (2) following 'false' branch... (start_cfg_edge_event)
2166 : : ...
2167 : : | do_something (ptr);
2168 : : | ~~~~~~~~~~~~~^~~~~
2169 : : | |
2170 : : | (3) ...to here (end_cfg_edge_event). */
2171 : 52290 : state_change_event_creator visitor (pb, eedge, emission_path);
2172 : 52290 : for_each_state_change (src_state, dst_state, pb.get_ext_state (),
2173 : : &visitor);
2174 : :
2175 : : /* Allow non-standard edges to add events, e.g. when rewinding from
2176 : : longjmp to a setjmp. */
2177 : 52290 : if (eedge.m_custom_info)
2178 : 585 : eedge.m_custom_info->add_events_to_path (emission_path, eedge);
2179 : :
2180 : : /* Add events for superedges, function entries, and for statements. */
2181 : 52290 : switch (dst_point.get_kind ())
2182 : : {
2183 : : default:
2184 : : break;
2185 : 18512 : case PK_BEFORE_SUPERNODE:
2186 : 18512 : if (src_point.get_kind () == PK_AFTER_SUPERNODE)
2187 : : {
2188 : 14459 : if (eedge.m_sedge)
2189 : 14383 : add_events_for_superedge (pb, eedge, emission_path);
2190 : : }
2191 : : /* Add function entry events. */
2192 : 18512 : if (dst_point.get_supernode ()->entry_p ())
2193 : : {
2194 : 5238 : pb.get_pending_diagnostic ()->add_function_entry_event
2195 : 5238 : (eedge, emission_path);
2196 : : /* Create region_creation_events for on-stack regions within
2197 : : this frame. */
2198 : 5238 : if (interest)
2199 : : {
2200 : : unsigned i;
2201 : : const region *reg;
2202 : 6737 : FOR_EACH_VEC_ELT (interest->m_region_creation, i, reg)
2203 : 1499 : if (const frame_region *frame = reg->maybe_get_frame_region ())
2204 : 1005 : if (frame->get_fndecl () == dst_point.get_fndecl ())
2205 : : {
2206 : 852 : const region *base_reg = reg->get_base_region ();
2207 : 852 : if (tree decl = base_reg->maybe_get_decl ())
2208 : 751 : if (DECL_P (decl)
2209 : 751 : && DECL_SOURCE_LOCATION (decl) != UNKNOWN_LOCATION)
2210 : : {
2211 : 747 : emission_path->add_region_creation_events
2212 : 1494 : (pb.get_pending_diagnostic (),
2213 : 747 : reg, dst_state.m_region_model,
2214 : 1494 : event_loc_info (DECL_SOURCE_LOCATION (decl),
2215 : : dst_point.get_fndecl (),
2216 : 747 : dst_stack_depth),
2217 : 747 : m_verbosity > 3);
2218 : : }
2219 : : }
2220 : : }
2221 : : }
2222 : : break;
2223 : 18808 : case PK_BEFORE_STMT:
2224 : 18808 : {
2225 : 18808 : const gimple *stmt = dst_point.get_stmt ();
2226 : 18808 : const gcall *call = dyn_cast <const gcall *> (stmt);
2227 : 8553 : if (call && is_setjmp_call_p (*call))
2228 : 20 : emission_path->add_event
2229 : 20 : (std::make_unique<setjmp_event>
2230 : 20 : (event_loc_info (stmt->location,
2231 : : dst_point.get_fndecl (),
2232 : 20 : dst_stack_depth),
2233 : : dst_node,
2234 : : *call));
2235 : : else
2236 : 18788 : emission_path->add_event
2237 : 18788 : (std::make_unique<statement_event> (stmt,
2238 : 37576 : dst_point.get_fndecl (),
2239 : : dst_stack_depth, dst_state));
2240 : :
2241 : : /* Create state change events for assignment to NULL.
2242 : : Iterate through the stmts in dst_enode, adding state change
2243 : : events for them. */
2244 : 18808 : if (dst_state.m_region_model)
2245 : : {
2246 : 18808 : log_scope s (get_logger (), "processing run of stmts");
2247 : 18808 : program_state iter_state (dst_state);
2248 : 18808 : program_point iter_point (dst_point);
2249 : 73136 : while (1)
2250 : : {
2251 : 73136 : const gimple *stmt = iter_point.get_stmt ();
2252 : 73136 : if (const gassign *assign = dyn_cast<const gassign *> (stmt))
2253 : : {
2254 : 28338 : const extrinsic_state &ext_state = pb.get_ext_state ();
2255 : 28338 : program_state old_state (iter_state);
2256 : 28338 : iter_state.m_region_model->on_assignment (assign, nullptr);
2257 : 226269 : for (unsigned i = 0; i < ext_state.get_num_checkers (); i++)
2258 : : {
2259 : 197931 : const state_machine &sm = ext_state.get_sm (i);
2260 : 197931 : null_assignment_sm_context sm_ctxt (i, sm,
2261 : : &old_state,
2262 : : &iter_state,
2263 : : stmt,
2264 : : &iter_point,
2265 : : emission_path,
2266 : 197931 : pb.get_ext_state ());
2267 : 197931 : sm.on_stmt (sm_ctxt, dst_point.get_supernode (), stmt);
2268 : : // TODO: what about phi nodes?
2269 : 197931 : }
2270 : 28338 : }
2271 : 73136 : iter_point.next_stmt ();
2272 : 73136 : if (iter_point.get_kind () == PK_AFTER_SUPERNODE
2273 : 73592 : || (dst_node->m_succs.length () > 1
2274 : : && (iter_point
2275 : 456 : == dst_node->m_succs[0]->m_dest->get_point ())))
2276 : : break;
2277 : : }
2278 : :
2279 : 18808 : }
2280 : : }
2281 : 18808 : break;
2282 : : }
2283 : :
2284 : : /* Look for changes in dynamic extents, which will identify
2285 : : the creation of heap-based regions and alloca regions. */
2286 : 52290 : if (interest)
2287 : : {
2288 : 52286 : const region_model *src_model = src_state.m_region_model;
2289 : 52286 : const region_model *dst_model = dst_state.m_region_model;
2290 : 52286 : if (src_model->get_dynamic_extents ()
2291 : 52286 : != dst_model->get_dynamic_extents ())
2292 : : {
2293 : : unsigned i;
2294 : : const region *reg;
2295 : 2742 : FOR_EACH_VEC_ELT (interest->m_region_creation, i, reg)
2296 : : {
2297 : 382 : const region *base_reg = reg->get_base_region ();
2298 : 382 : const svalue *old_extents
2299 : 382 : = src_model->get_dynamic_extents (base_reg);
2300 : 382 : const svalue *new_extents
2301 : 382 : = dst_model->get_dynamic_extents (base_reg);
2302 : 382 : if (old_extents == nullptr && new_extents != nullptr)
2303 : 277 : switch (base_reg->get_kind ())
2304 : : {
2305 : : default:
2306 : : break;
2307 : 241 : case RK_HEAP_ALLOCATED:
2308 : 241 : case RK_ALLOCA:
2309 : 241 : emission_path->add_region_creation_events
2310 : 241 : (pb.get_pending_diagnostic (),
2311 : : reg, dst_model,
2312 : 241 : event_loc_info (src_point.get_location (),
2313 : : src_point.get_fndecl (),
2314 : 241 : src_stack_depth),
2315 : 241 : m_verbosity > 3);
2316 : 241 : break;
2317 : : }
2318 : : }
2319 : : }
2320 : : }
2321 : :
2322 : 52290 : if (pb.get_feasibility_problem ()
2323 : 52290 : && &pb.get_feasibility_problem ()->m_eedge == &eedge)
2324 : : {
2325 : 4 : pretty_printer pp;
2326 : 4 : pp_format_decoder (&pp) = default_tree_printer;
2327 : 4 : pp_string (&pp,
2328 : : "this path would have been rejected as infeasible"
2329 : : " at this edge: ");
2330 : 4 : pb.get_feasibility_problem ()->dump_to_pp (&pp);
2331 : 4 : emission_path->add_event
2332 : 4 : (std::make_unique<precanned_custom_event>
2333 : 4 : (event_loc_info (dst_point.get_location (),
2334 : : dst_point.get_fndecl (),
2335 : 4 : dst_stack_depth),
2336 : 4 : pp_formatted_text (&pp)));
2337 : 4 : }
2338 : 52290 : }
2339 : :
2340 : : /* Return true if EEDGE is a significant edge in the path to the diagnostic
2341 : : for PB.
2342 : :
2343 : : Consider all of the sibling out-eedges from the same source enode
2344 : : as EEDGE.
2345 : : If there's no path from the destinations of those eedges to the
2346 : : diagnostic enode, then we have to take this eedge and thus it's
2347 : : significant.
2348 : :
2349 : : Conversely if there is a path from the destination of any other sibling
2350 : : eedge to the diagnostic enode, then this edge is insignificant.
2351 : :
2352 : : Example 1: redundant if-else:
2353 : :
2354 : : (A) if (...) A
2355 : : (B) ... / \
2356 : : else B C
2357 : : (C) ... \ /
2358 : : (D) [DIAGNOSTIC] D
2359 : :
2360 : : D is reachable by either B or C, so neither of these edges
2361 : : are significant.
2362 : :
2363 : : Example 2: pertinent if-else:
2364 : :
2365 : : (A) if (...) A
2366 : : (B) ... / \
2367 : : else B C
2368 : : (C) [NECESSARY CONDITION] | |
2369 : : (D) [POSSIBLE DIAGNOSTIC] D1 D2
2370 : :
2371 : : D becomes D1 and D2 in the exploded graph, where the diagnostic occurs
2372 : : at D2. D2 is only reachable via C, so the A -> C edge is significant.
2373 : :
2374 : : Example 3: redundant loop:
2375 : :
2376 : : (A) while (...) +-->A
2377 : : (B) ... | / \
2378 : : (C) ... +-B C
2379 : : (D) [DIAGNOSTIC] |
2380 : : D
2381 : :
2382 : : D is reachable from both B and C, so the A->C edge is not significant. */
2383 : :
2384 : : bool
2385 : 13870 : diagnostic_manager::significant_edge_p (const path_builder &pb,
2386 : : const exploded_edge &eedge) const
2387 : : {
2388 : 13870 : int i;
2389 : 13870 : exploded_edge *sibling;
2390 : 29426 : FOR_EACH_VEC_ELT (eedge.m_src->m_succs, i, sibling)
2391 : : {
2392 : 16436 : if (sibling == &eedge)
2393 : 13309 : continue;
2394 : 3127 : if (pb.reachable_from_p (sibling->m_dest))
2395 : : {
2396 : 880 : if (get_logger ())
2397 : 0 : get_logger ()->log (" edge EN: %i -> EN: %i is insignificant as"
2398 : : " EN: %i is also reachable via"
2399 : : " EN: %i -> EN: %i",
2400 : 0 : eedge.m_src->m_index, eedge.m_dest->m_index,
2401 : 0 : pb.get_diag_node ()->m_index,
2402 : 0 : sibling->m_src->m_index,
2403 : : sibling->m_dest->m_index);
2404 : 880 : return false;
2405 : : }
2406 : : }
2407 : :
2408 : : return true;
2409 : : }
2410 : :
2411 : : /* Subroutine of diagnostic_manager::add_events_for_eedge
2412 : : where EEDGE has an underlying superedge i.e. a CFG edge,
2413 : : or an interprocedural call/return.
2414 : : Add any events for the superedge to EMISSION_PATH. */
2415 : :
2416 : : void
2417 : 14383 : diagnostic_manager::add_events_for_superedge (const path_builder &pb,
2418 : : const exploded_edge &eedge,
2419 : : checker_path *emission_path)
2420 : : const
2421 : : {
2422 : 14383 : gcc_assert (eedge.m_sedge);
2423 : :
2424 : : /* Give diagnostics an opportunity to override this function. */
2425 : 14383 : pending_diagnostic *pd = pb.get_pending_diagnostic ();
2426 : 14383 : if (pd->maybe_add_custom_events_for_superedge (eedge, emission_path))
2427 : : return;
2428 : :
2429 : : /* Don't add events for insignificant edges at verbosity levels below 3. */
2430 : 13997 : if (m_verbosity < 3)
2431 : 13870 : if (!significant_edge_p (pb, eedge))
2432 : : return;
2433 : :
2434 : 13117 : const exploded_node *src_node = eedge.m_src;
2435 : 13117 : const program_point &src_point = src_node->get_point ();
2436 : 13117 : const exploded_node *dst_node = eedge.m_dest;
2437 : 13117 : const program_point &dst_point = dst_node->get_point ();
2438 : 13117 : const int src_stack_depth = src_point.get_stack_depth ();
2439 : 13117 : const int dst_stack_depth = dst_point.get_stack_depth ();
2440 : 13117 : const gimple *last_stmt = src_point.get_supernode ()->get_last_stmt ();
2441 : :
2442 : 13117 : switch (eedge.m_sedge->m_kind)
2443 : : {
2444 : 11094 : case SUPEREDGE_CFG_EDGE:
2445 : 11094 : {
2446 : 22188 : if (auto eh_dispatch_try_sedge
2447 : 11094 : = eedge.m_sedge->dyn_cast_eh_dispatch_try_cfg_superedge ())
2448 : : {
2449 : 66 : if (eh_dispatch_try_sedge->get_eh_catch ())
2450 : : {
2451 : 63 : const region_model *model = src_node->get_state ().m_region_model;
2452 : 63 : auto curr_thrown_exception_node
2453 : 63 : = model->get_current_thrown_exception ();
2454 : 0 : gcc_assert (curr_thrown_exception_node);
2455 : 63 : tree type = curr_thrown_exception_node->maybe_get_type ();
2456 : 63 : emission_path->add_event
2457 : 63 : (std::make_unique<catch_cfg_edge_event>
2458 : 63 : (eedge,
2459 : 63 : event_loc_info (dst_point.get_supernode ()->get_start_location (),
2460 : : dst_point.get_fndecl (),
2461 : 63 : dst_stack_depth),
2462 : : type));
2463 : 63 : return;
2464 : : }
2465 : : else
2466 : : {
2467 : : /* We have the "uncaught exception" sedge, from eh_dispatch
2468 : : to a block containing resx.
2469 : : Don't add any events for this, so that we can consolidate
2470 : : adjacent stack unwinding events. */
2471 : : return;
2472 : : }
2473 : : }
2474 : :
2475 : 11028 : emission_path->add_event
2476 : 11028 : (std::make_unique<start_cfg_edge_event>
2477 : 11028 : (eedge,
2478 : 11028 : event_loc_info
2479 : : (last_stmt ? last_stmt->location : UNKNOWN_LOCATION,
2480 : : src_point.get_fndecl (),
2481 : 11028 : src_stack_depth)));
2482 : 11028 : emission_path->add_event
2483 : 11028 : (std::make_unique<end_cfg_edge_event>
2484 : 11028 : (eedge,
2485 : 11028 : event_loc_info (dst_point.get_supernode ()->get_start_location (),
2486 : : dst_point.get_fndecl (),
2487 : 11028 : dst_stack_depth)));
2488 : : }
2489 : 11028 : break;
2490 : :
2491 : 1208 : case SUPEREDGE_CALL:
2492 : 1208 : pd->add_call_event (eedge, emission_path);
2493 : 1208 : break;
2494 : :
2495 : 160 : case SUPEREDGE_INTRAPROCEDURAL_CALL:
2496 : 160 : {
2497 : : /* TODO: add a subclass for this, or generate events for the
2498 : : summary. */
2499 : 160 : emission_path->add_event
2500 : 160 : (std::make_unique<debug_event>
2501 : 160 : (event_loc_info (last_stmt
2502 : : ? last_stmt->location
2503 : : : UNKNOWN_LOCATION,
2504 : : src_point.get_fndecl (),
2505 : 160 : src_stack_depth),
2506 : : "call summary"));
2507 : : }
2508 : 160 : break;
2509 : :
2510 : 655 : case SUPEREDGE_RETURN:
2511 : 655 : {
2512 : 655 : const return_superedge *return_edge
2513 : 655 : = as_a <const return_superedge *> (eedge.m_sedge);
2514 : :
2515 : 655 : const gcall &call_stmt = return_edge->get_call_stmt ();
2516 : 655 : emission_path->add_event
2517 : 655 : (std::make_unique<return_event>
2518 : 655 : (eedge,
2519 : 655 : event_loc_info (call_stmt.location,
2520 : : dst_point.get_fndecl (),
2521 : 655 : dst_stack_depth)));
2522 : : }
2523 : 655 : break;
2524 : : }
2525 : : }
2526 : :
2527 : : /* Prune PATH, based on the verbosity level, to the most pertinent
2528 : : events for a diagnostic that involves VAR ending in state STATE
2529 : : (for state machine SM).
2530 : :
2531 : : PATH is updated in place, and the redundant checker_events are deleted.
2532 : :
2533 : : As well as deleting events, call record_critical_state on events in
2534 : : which state critical to the pending_diagnostic is being handled; see
2535 : : the comment for diagnostic_manager::prune_for_sm_diagnostic. */
2536 : :
2537 : : void
2538 : 3983 : diagnostic_manager::prune_path (checker_path *path,
2539 : : const state_machine *sm,
2540 : : const svalue *sval,
2541 : : state_machine::state_t state) const
2542 : : {
2543 : 3983 : LOG_FUNC (get_logger ());
2544 : 3983 : path->maybe_log (get_logger (), "path");
2545 : 3983 : prune_for_sm_diagnostic (path, sm, sval, state);
2546 : 3983 : prune_interproc_events (path);
2547 : 3983 : if (! flag_analyzer_show_events_in_system_headers)
2548 : 3981 : prune_system_headers (path);
2549 : 3983 : consolidate_conditions (path);
2550 : 3983 : consolidate_unwind_events (path);
2551 : 3983 : finish_pruning (path);
2552 : 3983 : path->maybe_log (get_logger (), "pruned");
2553 : 3983 : }
2554 : :
2555 : : /* A cheap test to determine if EXPR can be the expression of interest in
2556 : : an sm-diagnostic, so that we can reject cases where we have a non-lvalue.
2557 : : We don't have always have a model when calling this, so we can't use
2558 : : tentative_region_model_context, so there can be false positives. */
2559 : :
2560 : : static bool
2561 : 0 : can_be_expr_of_interest_p (tree expr)
2562 : : {
2563 : 0 : if (!expr)
2564 : : return false;
2565 : :
2566 : : /* Reject constants. */
2567 : 0 : if (CONSTANT_CLASS_P (expr))
2568 : 0 : return false;
2569 : :
2570 : : /* Otherwise assume that it can be an lvalue. */
2571 : : return true;
2572 : : }
2573 : :
2574 : : /* First pass of diagnostic_manager::prune_path: apply verbosity level,
2575 : : pruning unrelated state change events.
2576 : :
2577 : : Iterate backwards through PATH, skipping state change events that aren't
2578 : : VAR but update the pertinent VAR when state-copying occurs.
2579 : :
2580 : : As well as deleting events, call record_critical_state on events in
2581 : : which state critical to the pending_diagnostic is being handled, so
2582 : : that the event's get_desc vfunc can potentially supply a more precise
2583 : : description of the event to the user.
2584 : : e.g. improving
2585 : : "calling 'foo' from 'bar'"
2586 : : to
2587 : : "passing possibly-NULL pointer 'ptr' to 'foo' from 'bar' as param 1"
2588 : : when the diagnostic relates to later dereferencing 'ptr'. */
2589 : :
2590 : : void
2591 : 3983 : diagnostic_manager::prune_for_sm_diagnostic (checker_path *path,
2592 : : const state_machine *sm,
2593 : : const svalue *sval,
2594 : : state_machine::state_t state) const
2595 : : {
2596 : 3983 : int idx = path->num_events () - 1;
2597 : 114109 : while (idx >= 0 && idx < (signed)path->num_events ())
2598 : : {
2599 : 55063 : checker_event *base_event = path->get_checker_event (idx);
2600 : 55063 : if (get_logger ())
2601 : : {
2602 : 0 : if (sm)
2603 : : {
2604 : 0 : if (sval)
2605 : : {
2606 : 0 : label_text sval_desc = sval->get_desc ();
2607 : 0 : log ("considering event %i (%s), with sval: %qs, state: %qs",
2608 : : idx, event_kind_to_string (base_event->get_kind ()),
2609 : : sval_desc.get (), state->get_name ());
2610 : 0 : }
2611 : : else
2612 : 0 : log ("considering event %i (%s), with global state: %qs",
2613 : : idx, event_kind_to_string (base_event->get_kind ()),
2614 : : state->get_name ());
2615 : : }
2616 : : else
2617 : 0 : log ("considering event %i", idx);
2618 : : }
2619 : :
2620 : 55063 : switch (base_event->get_kind ())
2621 : : {
2622 : 0 : default:
2623 : 0 : gcc_unreachable ();
2624 : :
2625 : 160 : case event_kind::debug:
2626 : 160 : if (m_verbosity < 4)
2627 : : {
2628 : 160 : log ("filtering event %i: debug event", idx);
2629 : 160 : path->delete_event (idx);
2630 : : }
2631 : : break;
2632 : :
2633 : : case event_kind::custom:
2634 : : /* Don't filter custom events. */
2635 : : break;
2636 : :
2637 : 18788 : case event_kind::stmt:
2638 : 18788 : {
2639 : 18788 : if (m_verbosity < 4)
2640 : : {
2641 : 18788 : log ("filtering event %i: statement event", idx);
2642 : 18788 : path->delete_event (idx);
2643 : : }
2644 : : }
2645 : : break;
2646 : :
2647 : : case event_kind::region_creation:
2648 : : /* Don't filter these. */
2649 : : break;
2650 : :
2651 : 5238 : case event_kind::function_entry:
2652 : 5238 : if (m_verbosity < 1)
2653 : : {
2654 : 33 : log ("filtering event %i: function entry", idx);
2655 : 33 : path->delete_event (idx);
2656 : : }
2657 : : break;
2658 : :
2659 : 4728 : case event_kind::state_change:
2660 : 4728 : {
2661 : 4728 : state_change_event *state_change = (state_change_event *)base_event;
2662 : 4728 : gcc_assert (state_change->m_dst_state.m_region_model);
2663 : :
2664 : 4728 : if (state_change->m_sval == sval)
2665 : : {
2666 : 2308 : if (state_change->m_origin)
2667 : : {
2668 : 0 : if (get_logger ())
2669 : : {
2670 : 0 : label_text sval_desc = sval->get_desc ();
2671 : 0 : label_text origin_sval_desc
2672 : 0 : = state_change->m_origin->get_desc ();
2673 : 0 : log ("event %i:"
2674 : : " switching var of interest from %qs to %qs",
2675 : : idx, sval_desc.get (),
2676 : : origin_sval_desc.get ());
2677 : 0 : }
2678 : 0 : sval = state_change->m_origin;
2679 : : }
2680 : 2308 : log ("event %i: switching state of interest from %qs to %qs",
2681 : 2308 : idx, state_change->m_to->get_name (),
2682 : 2308 : state_change->m_from->get_name ());
2683 : 2308 : state = state_change->m_from;
2684 : : }
2685 : 2420 : else if (m_verbosity < 4)
2686 : : {
2687 : 2420 : if (get_logger ())
2688 : : {
2689 : 0 : if (state_change->m_sval)
2690 : : {
2691 : 0 : label_text change_sval_desc
2692 : 0 : = state_change->m_sval->get_desc ();
2693 : 0 : if (sval)
2694 : : {
2695 : 0 : label_text sval_desc = sval->get_desc ();
2696 : 0 : log ("filtering event %i:"
2697 : : " state change to %qs unrelated to %qs",
2698 : : idx, change_sval_desc.get (),
2699 : : sval_desc.get ());
2700 : 0 : }
2701 : : else
2702 : 0 : log ("filtering event %i: state change to %qs",
2703 : : idx, change_sval_desc.get ());
2704 : 0 : }
2705 : : else
2706 : 0 : log ("filtering event %i: global state change", idx);
2707 : : }
2708 : 2420 : path->delete_event (idx);
2709 : : }
2710 : : }
2711 : : break;
2712 : :
2713 : 11028 : case event_kind::start_cfg_edge:
2714 : 11028 : {
2715 : 11028 : cfg_edge_event *event = (cfg_edge_event *)base_event;
2716 : :
2717 : : /* TODO: is this edge significant to var?
2718 : : See if var can be in other states in the dest, but not
2719 : : in other states in the src?
2720 : : Must have multiple sibling edges. */
2721 : :
2722 : 11028 : if (event->should_filter_p (m_verbosity))
2723 : : {
2724 : 8364 : log ("filtering events %i and %i: CFG edge", idx, idx + 1);
2725 : 8364 : path->delete_event (idx);
2726 : : /* Also delete the corresponding event_kind::end_cfg_edge. */
2727 : 8364 : gcc_assert (path->get_checker_event (idx)->get_kind ()
2728 : : == event_kind::end_cfg_edge);
2729 : 8364 : path->delete_event (idx);
2730 : : }
2731 : : }
2732 : : break;
2733 : :
2734 : : case event_kind::end_cfg_edge:
2735 : : /* These come in pairs with event_kind::start_cfg_edge events and are
2736 : : filtered when their start event is filtered. */
2737 : : break;
2738 : :
2739 : : case event_kind::catch_:
2740 : : case event_kind::throw_:
2741 : : case event_kind::unwind:
2742 : : /* Don't filter these. */
2743 : : break;
2744 : :
2745 : 1241 : case event_kind::call_edge:
2746 : 1241 : {
2747 : 1241 : call_event *event = (call_event *)base_event;
2748 : 1241 : const region_model *callee_model
2749 : 1241 : = event->m_eedge.m_dest->get_state ().m_region_model;
2750 : 1241 : const region_model *caller_model
2751 : 1241 : = event->m_eedge.m_src->get_state ().m_region_model;
2752 : 1241 : tree callee_var = callee_model->get_representative_tree (sval);
2753 : 1241 : callsite_expr expr;
2754 : :
2755 : 1241 : tree caller_var;
2756 : 1241 : if(event->m_sedge)
2757 : : {
2758 : 1208 : const callgraph_superedge& cg_superedge
2759 : 1208 : = event->get_callgraph_superedge ();
2760 : 1208 : if (cg_superedge.m_cedge)
2761 : 1208 : caller_var
2762 : 1208 : = cg_superedge.map_expr_from_callee_to_caller (callee_var,
2763 : : &expr);
2764 : : else
2765 : 0 : caller_var = caller_model->get_representative_tree (sval);
2766 : : }
2767 : : else
2768 : 33 : caller_var = caller_model->get_representative_tree (sval);
2769 : :
2770 : 1241 : if (caller_var)
2771 : : {
2772 : 233 : if (get_logger ())
2773 : : {
2774 : 0 : label_text sval_desc = sval->get_desc ();
2775 : 0 : log ("event %i:"
2776 : : " recording critical state for %qs at call"
2777 : : " from %qE in callee to %qE in caller",
2778 : : idx, sval_desc.get (), callee_var, caller_var);
2779 : 0 : }
2780 : 233 : if (expr.param_p ())
2781 : 200 : event->record_critical_state (caller_var, state);
2782 : : }
2783 : : }
2784 : 1241 : break;
2785 : :
2786 : 673 : case event_kind::return_edge:
2787 : 673 : {
2788 : 673 : if (sval)
2789 : : {
2790 : 493 : return_event *event = (return_event *)base_event;
2791 : 493 : const region_model *caller_model
2792 : 493 : = event->m_eedge.m_dest->get_state ().m_region_model;
2793 : 493 : tree caller_var = caller_model->get_representative_tree (sval);
2794 : 493 : const region_model *callee_model
2795 : 493 : = event->m_eedge.m_src->get_state ().m_region_model;
2796 : 493 : callsite_expr expr;
2797 : :
2798 : 493 : tree callee_var;
2799 : 493 : if (event->m_sedge)
2800 : : {
2801 : 475 : const callgraph_superedge& cg_superedge
2802 : 475 : = event->get_callgraph_superedge ();
2803 : 475 : if (cg_superedge.m_cedge)
2804 : 475 : callee_var
2805 : 475 : = cg_superedge.map_expr_from_caller_to_callee (caller_var,
2806 : : &expr);
2807 : : else
2808 : 0 : callee_var = callee_model->get_representative_tree (sval);
2809 : : }
2810 : : else
2811 : 18 : callee_var = callee_model->get_representative_tree (sval);
2812 : :
2813 : 493 : if (callee_var)
2814 : : {
2815 : 166 : if (get_logger ())
2816 : : {
2817 : 0 : label_text sval_desc = sval->get_desc ();
2818 : 0 : log ("event %i:"
2819 : : " recording critical state for %qs at return"
2820 : : " from %qE in caller to %qE in callee",
2821 : : idx, sval_desc.get (), callee_var, callee_var);
2822 : 0 : }
2823 : 166 : if (expr.return_value_p ())
2824 : 69 : event->record_critical_state (callee_var, state);
2825 : : }
2826 : : }
2827 : : }
2828 : : break;
2829 : :
2830 : : case event_kind::inlined_call:
2831 : : /* We don't expect to see these yet, as they're added later.
2832 : : We'd want to keep them around. */
2833 : : break;
2834 : :
2835 : : case event_kind::setjmp_:
2836 : : /* TODO: only show setjmp_events that matter i.e. those for which
2837 : : there is a later rewind event using them. */
2838 : : case event_kind::rewind_from_longjmp:
2839 : : case event_kind::rewind_to_setjmp:
2840 : : break;
2841 : :
2842 : : case event_kind::warning:
2843 : : /* Always show the final "warning" event in the path. */
2844 : : break;
2845 : : }
2846 : 55063 : idx--;
2847 : : }
2848 : 3983 : }
2849 : :
2850 : : /* Subroutine of diagnostic_manager::prune_for_sm_diagnostic.
2851 : : If *EXPR is not suitable to be the expression of interest in
2852 : : an sm-diagnostic, set *EXPR to NULL and log. */
2853 : :
2854 : : void
2855 : 0 : diagnostic_manager::update_for_unsuitable_sm_exprs (tree *expr) const
2856 : : {
2857 : 0 : gcc_assert (expr);
2858 : 0 : if (*expr && !can_be_expr_of_interest_p (*expr))
2859 : : {
2860 : 0 : log ("new var %qE is unsuitable; setting var to NULL", *expr);
2861 : 0 : *expr = NULL_TREE;
2862 : : }
2863 : 0 : }
2864 : :
2865 : : /* Second pass of diagnostic_manager::prune_path: remove redundant
2866 : : interprocedural information.
2867 : :
2868 : : For example, given:
2869 : : (1)- calling "f2" from "f1"
2870 : : (2)--- entry to "f2"
2871 : : (3)--- calling "f3" from "f2"
2872 : : (4)----- entry to "f3"
2873 : : (5)--- returning to "f2" to "f3"
2874 : : (6)- returning to "f1" to "f2"
2875 : : with no other intervening events, then none of these events are
2876 : : likely to be interesting to the user.
2877 : :
2878 : : Prune [..., call, function-entry, return, ...] triples repeatedly
2879 : : until nothing has changed. For the example above, this would
2880 : : remove events (3, 4, 5), and then remove events (1, 2, 6). */
2881 : :
2882 : : void
2883 : 3983 : diagnostic_manager::prune_interproc_events (checker_path *path) const
2884 : : {
2885 : 3983 : bool changed = false;
2886 : 4219 : do
2887 : : {
2888 : 4219 : changed = false;
2889 : 4219 : int idx = (signed)path->num_events () - 1;
2890 : 23033 : while (idx >= 0)
2891 : : {
2892 : : /* Prune [..., call, function-entry, return, ...] triples. */
2893 : 18814 : if (idx + 2 < (signed)path->num_events ()
2894 : 10636 : && path->get_checker_event (idx)->is_call_p ()
2895 : 1027 : && path->get_checker_event (idx + 1)->is_function_entry_p ()
2896 : 19833 : && path->get_checker_event (idx + 2)->is_return_p ())
2897 : : {
2898 : 302 : if (get_logger ())
2899 : : {
2900 : 0 : label_text desc
2901 : 0 : (path->get_checker_event (idx)->get_desc
2902 : 0 : (*global_dc->get_reference_printer ()));
2903 : 0 : log ("filtering events %i-%i:"
2904 : : " irrelevant call/entry/return: %s",
2905 : : idx, idx + 2, desc.get ());
2906 : 0 : }
2907 : 302 : path->delete_event (idx + 2);
2908 : 302 : path->delete_event (idx + 1);
2909 : 302 : path->delete_event (idx);
2910 : 302 : changed = true;
2911 : 302 : idx--;
2912 : 302 : continue;
2913 : 302 : }
2914 : :
2915 : : /* Prune [..., call, return, ...] pairs
2916 : : (for -fanalyzer-verbosity=0). */
2917 : 18512 : if (idx + 1 < (signed)path->num_events ()
2918 : 14154 : && path->get_checker_event (idx)->is_call_p ()
2919 : 19588 : && path->get_checker_event (idx + 1)->is_return_p ())
2920 : : {
2921 : 4 : if (get_logger ())
2922 : : {
2923 : 0 : label_text desc
2924 : 0 : (path->get_checker_event (idx)->get_desc
2925 : 0 : (*global_dc->get_reference_printer ()));
2926 : 0 : log ("filtering events %i-%i:"
2927 : : " irrelevant call/return: %s",
2928 : : idx, idx + 1, desc.get ());
2929 : 0 : }
2930 : 4 : path->delete_event (idx + 1);
2931 : 4 : path->delete_event (idx);
2932 : 4 : changed = true;
2933 : 4 : idx--;
2934 : 4 : continue;
2935 : 4 : }
2936 : :
2937 : 18508 : idx--;
2938 : : }
2939 : :
2940 : : }
2941 : : while (changed);
2942 : 3983 : }
2943 : :
2944 : : /* Remove everything within [call point, IDX]. For consistency,
2945 : : IDX should represent the return event of the frame to delete,
2946 : : or if there is none it should be the last event of the frame.
2947 : : After this function, IDX designates the event prior to calling
2948 : : this frame. */
2949 : :
2950 : : static void
2951 : 4 : prune_frame (checker_path *path, int &idx)
2952 : : {
2953 : 4 : gcc_assert (idx >= 0);
2954 : 4 : int nesting = 1;
2955 : 4 : if (path->get_checker_event (idx)->is_return_p ())
2956 : 4 : nesting = 0;
2957 : 28 : do
2958 : : {
2959 : 28 : if (path->get_checker_event (idx)->is_call_p ())
2960 : 8 : nesting--;
2961 : 20 : else if (path->get_checker_event (idx)->is_return_p ())
2962 : 8 : nesting++;
2963 : :
2964 : 28 : path->delete_event (idx--);
2965 : 28 : } while (idx >= 0 && nesting != 0);
2966 : 4 : }
2967 : :
2968 : : /* This function is called when fanalyzer-show-events-in-system-headers
2969 : : is disabled and will prune the diagnostic of all events within a
2970 : : system header, only keeping the entry and exit events to the header.
2971 : : This should be called after diagnostic_manager::prune_interproc_events
2972 : : so that sucessive events [system header call, system header return]
2973 : : are preserved thereafter.
2974 : :
2975 : : Given a diagnostics path diving into a system header in the form
2976 : : [
2977 : : prefix events...,
2978 : : system header call,
2979 : : system header entry,
2980 : : events within system headers...,
2981 : : system header return,
2982 : : suffix events...
2983 : : ]
2984 : :
2985 : : then transforms it into
2986 : : [
2987 : : prefix events...,
2988 : : system header call,
2989 : : system header return,
2990 : : suffix events...
2991 : : ]. */
2992 : :
2993 : : void
2994 : 3981 : diagnostic_manager::prune_system_headers (checker_path *path) const
2995 : : {
2996 : 3981 : int idx = (signed)path->num_events () - 1;
2997 : 19951 : while (idx >= 0)
2998 : : {
2999 : 15970 : const checker_event *event = path->get_checker_event (idx);
3000 : : /* Prune everything between
3001 : : [..., system entry, (...), system return, ...]. */
3002 : 15970 : if (event->is_return_p ()
3003 : 15970 : && in_system_header_at (event->get_location ()))
3004 : : {
3005 : 4 : int ret_idx = idx;
3006 : 4 : prune_frame (path, idx);
3007 : :
3008 : 4 : if (get_logger ())
3009 : : {
3010 : 0 : log ("filtering system headers events %i-%i:",
3011 : : idx, ret_idx);
3012 : : }
3013 : : // Delete function entry within system headers.
3014 : 4 : if (idx >= 0)
3015 : : {
3016 : 4 : event = path->get_checker_event (idx);
3017 : 4 : if (event->is_function_entry_p ()
3018 : 4 : && in_system_header_at (event->get_location ()))
3019 : : {
3020 : 4 : if (get_logger ())
3021 : : {
3022 : 0 : label_text desc
3023 : 0 : (event->get_desc (*global_dc->get_reference_printer ()));
3024 : 0 : log ("filtering event %i:"
3025 : : "system header entry event: %s",
3026 : : idx, desc.get ());
3027 : 0 : }
3028 : :
3029 : 4 : path->delete_event (idx);
3030 : : }
3031 : : }
3032 : : }
3033 : :
3034 : 15970 : idx--;
3035 : : }
3036 : 3981 : }
3037 : :
3038 : : /* Return true iff event IDX within PATH is on the same line as REF_EXP_LOC. */
3039 : :
3040 : : static bool
3041 : 2727 : same_line_as_p (const expanded_location &ref_exp_loc,
3042 : : checker_path *path, unsigned idx)
3043 : : {
3044 : 2727 : const checker_event *ev = path->get_checker_event (idx);
3045 : 2727 : expanded_location idx_exp_loc = expand_location (ev->get_location ());
3046 : 2727 : gcc_assert (ref_exp_loc.file);
3047 : 2727 : if (idx_exp_loc.file == nullptr)
3048 : : return false;
3049 : 2709 : if (strcmp (ref_exp_loc.file, idx_exp_loc.file))
3050 : : return false;
3051 : 2709 : return ref_exp_loc.line == idx_exp_loc.line;
3052 : : }
3053 : :
3054 : : /* This path-readability optimization reduces the verbosity of compound
3055 : : conditional statements (without needing to reconstruct the AST, which
3056 : : has already been lost).
3057 : :
3058 : : For example, it converts:
3059 : :
3060 : : | 61 | if (cp[0] != '\0' && cp[0] != '#')
3061 : : | | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3062 : : | | | | |
3063 : : | | | | (6) ...to here
3064 : : | | | (7) following ‘true’ branch...
3065 : : | | (5) following ‘true’ branch...
3066 : : | 62 | {
3067 : : | 63 | alias = cp++;
3068 : : | | ~~~~
3069 : : | | |
3070 : : | | (8) ...to here
3071 : :
3072 : : into:
3073 : :
3074 : : | 61 | if (cp[0] != '\0' && cp[0] != '#')
3075 : : | | ~
3076 : : | | |
3077 : : | | (5) following ‘true’ branch...
3078 : : | 62 | {
3079 : : | 63 | alias = cp++;
3080 : : | | ~~~~
3081 : : | | |
3082 : : | | (6) ...to here
3083 : :
3084 : : by combining events 5-8 into new events 5-6.
3085 : :
3086 : : Find runs of consecutive (start_cfg_edge_event, end_cfg_edge_event) pairs
3087 : : in which all events apart from the final end_cfg_edge_event are on the same
3088 : : line, and for which either all the CFG edges are TRUE edges, or all are
3089 : : FALSE edges.
3090 : :
3091 : : Consolidate each such run into a
3092 : : (start_consolidated_cfg_edges_event, end_consolidated_cfg_edges_event)
3093 : : pair. */
3094 : :
3095 : : void
3096 : 3983 : diagnostic_manager::consolidate_conditions (checker_path *path) const
3097 : : {
3098 : : /* Don't simplify edges if we're debugging them. */
3099 : 3983 : if (flag_analyzer_verbose_edges)
3100 : : return;
3101 : :
3102 : 11814 : for (int start_idx = 0;
3103 : 31594 : start_idx < (signed)path->num_events () - 1;
3104 : : start_idx++)
3105 : : {
3106 : 11814 : if (path->cfg_edge_pair_at_p (start_idx))
3107 : : {
3108 : 2568 : const checker_event *old_start_ev
3109 : 2568 : = path->get_checker_event (start_idx);
3110 : 2568 : expanded_location start_exp_loc
3111 : 2568 : = expand_location (old_start_ev->get_location ());
3112 : 2568 : if (start_exp_loc.file == nullptr)
3113 : 2303 : continue;
3114 : 2568 : if (!same_line_as_p (start_exp_loc, path, start_idx + 1))
3115 : 2303 : continue;
3116 : :
3117 : : /* Are we looking for a run of all TRUE edges, or all FALSE edges? */
3118 : 265 : gcc_assert (old_start_ev->get_kind () == event_kind::start_cfg_edge);
3119 : 265 : const start_cfg_edge_event *old_start_cfg_ev
3120 : : = (const start_cfg_edge_event *)old_start_ev;
3121 : 265 : const cfg_superedge& first_cfg_sedge
3122 : 265 : = old_start_cfg_ev->get_cfg_superedge ();
3123 : 265 : bool edge_sense;
3124 : 265 : if (first_cfg_sedge.true_value_p ())
3125 : : edge_sense = true;
3126 : 81 : else if (first_cfg_sedge.false_value_p ())
3127 : : edge_sense = false;
3128 : : else
3129 : 0 : continue;
3130 : :
3131 : : /* Find a run of CFG start/end event pairs from
3132 : : [start_idx, next_idx)
3133 : : where all apart from the final event are on the same line,
3134 : : and all are either TRUE or FALSE edges, matching the initial. */
3135 : 265 : int next_idx = start_idx + 2;
3136 : 265 : while (path->cfg_edge_pair_at_p (next_idx)
3137 : 361 : && same_line_as_p (start_exp_loc, path, next_idx))
3138 : : {
3139 : 109 : const checker_event *iter_ev
3140 : 109 : = path->get_checker_event (next_idx);
3141 : 109 : gcc_assert (iter_ev->get_kind () == event_kind::start_cfg_edge);
3142 : 109 : const start_cfg_edge_event *iter_cfg_ev
3143 : : = (const start_cfg_edge_event *)iter_ev;
3144 : 109 : const cfg_superedge& iter_cfg_sedge
3145 : 109 : = iter_cfg_ev->get_cfg_superedge ();
3146 : 109 : if (edge_sense)
3147 : : {
3148 : 55 : if (!iter_cfg_sedge.true_value_p ())
3149 : : break;
3150 : : }
3151 : : else
3152 : : {
3153 : 54 : if (!iter_cfg_sedge.false_value_p ())
3154 : : break;
3155 : : }
3156 : 96 : next_idx += 2;
3157 : : }
3158 : :
3159 : : /* If we have more than one pair in the run, consolidate. */
3160 : 265 : if (next_idx > start_idx + 2)
3161 : : {
3162 : 91 : const checker_event *old_end_ev
3163 : 91 : = path->get_checker_event (next_idx - 1);
3164 : 91 : log ("consolidating CFG edge events %i-%i into %i-%i",
3165 : : start_idx, next_idx - 1, start_idx, start_idx +1);
3166 : 91 : start_consolidated_cfg_edges_event *new_start_ev
3167 : : = new start_consolidated_cfg_edges_event
3168 : 91 : (event_loc_info (old_start_ev->get_location (),
3169 : : old_start_ev->get_fndecl (),
3170 : 91 : old_start_ev->get_stack_depth ()),
3171 : 91 : edge_sense);
3172 : 91 : checker_event *new_end_ev
3173 : : = new end_consolidated_cfg_edges_event
3174 : 91 : (event_loc_info (old_end_ev->get_location (),
3175 : : old_end_ev->get_fndecl (),
3176 : 91 : old_end_ev->get_stack_depth ()));
3177 : 91 : path->replace_event (start_idx, new_start_ev);
3178 : 91 : path->replace_event (start_idx + 1, new_end_ev);
3179 : 91 : path->delete_events (start_idx + 2, next_idx - (start_idx + 2));
3180 : : }
3181 : : }
3182 : : }
3183 : : }
3184 : :
3185 : : /* Consolidate runs of consecutive unwind_event. */
3186 : :
3187 : : void
3188 : 3983 : diagnostic_manager::consolidate_unwind_events (checker_path *path) const
3189 : : {
3190 : : /* Don't simplify edges if we're debugging them. */
3191 : 3983 : if (flag_analyzer_verbose_edges)
3192 : : return;
3193 : :
3194 : 11808 : for (int start_idx = 0;
3195 : 31582 : start_idx < (signed)path->num_events () - 1;
3196 : : start_idx++)
3197 : : {
3198 : : /* Find a run of consecutive unwind_event instances. */
3199 : 11808 : if (path->get_checker_event (start_idx)->get_kind ()
3200 : : != event_kind::unwind)
3201 : 11799 : continue;
3202 : 9 : int iter_idx = start_idx + 1;
3203 : 15 : while (iter_idx < (int)path->num_events ())
3204 : 15 : if (path->get_checker_event (iter_idx)->get_kind ()
3205 : : == event_kind::unwind)
3206 : 6 : ++iter_idx;
3207 : : else
3208 : : break;
3209 : :
3210 : : /* iter_idx should now be one after the last unwind_event in the run. */
3211 : 9 : const int last_idx = iter_idx - 1;
3212 : 9 : if (last_idx == start_idx)
3213 : 3 : continue;
3214 : :
3215 : 6 : gcc_assert (last_idx > start_idx);
3216 : :
3217 : 6 : log ("consolidating unwind events %i-%i into %i",
3218 : : start_idx, last_idx, start_idx);
3219 : :
3220 : 6 : unwind_event *first_event
3221 : 6 : = (unwind_event *)path->get_checker_event (start_idx);
3222 : 6 : const unwind_event *last_event
3223 : 6 : = (const unwind_event *)path->get_checker_event (last_idx);
3224 : 6 : first_event->m_num_frames += last_event->m_num_frames;
3225 : 6 : path->delete_events (start_idx + 1, last_idx - start_idx);
3226 : : }
3227 : : }
3228 : :
3229 : : /* Final pass of diagnostic_manager::prune_path.
3230 : :
3231 : : If all we're left with is in one function, then filter function entry
3232 : : events. */
3233 : :
3234 : : void
3235 : 3983 : diagnostic_manager::finish_pruning (checker_path *path) const
3236 : : {
3237 : 3983 : if (!path->interprocedural_p ())
3238 : : {
3239 : 3187 : int idx = path->num_events () - 1;
3240 : 24263 : while (idx >= 0 && idx < (signed)path->num_events ())
3241 : : {
3242 : 10538 : checker_event *base_event = path->get_checker_event (idx);
3243 : 10538 : if (base_event->get_kind () == event_kind::function_entry)
3244 : : {
3245 : 3182 : log ("filtering event %i:"
3246 : : " function entry for purely intraprocedural path", idx);
3247 : 3182 : path->delete_event (idx);
3248 : : }
3249 : 10538 : idx--;
3250 : : }
3251 : : }
3252 : 3983 : }
3253 : :
3254 : : } // namespace ana
3255 : :
3256 : : #endif /* #if ENABLE_ANALYZER */
|