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