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