aboutsummaryrefslogtreecommitdiff
path: root/gcc/analyzer/trimmed-graph.cc
diff options
context:
space:
mode:
authorDavid Malcolm <dmalcolm@redhat.com>2021-03-11 17:46:37 -0500
committerDavid Malcolm <dmalcolm@redhat.com>2021-03-11 17:46:37 -0500
commit3857edb5d32dcdc11d9a2fe3ad7c156c52a1ec7f (patch)
treedee3fd5ec072bc0fa3f1b00586b8ab0db4e95aa5 /gcc/analyzer/trimmed-graph.cc
parent5e33e5b042a6a830c40cee3d0a925bc49dcfe069 (diff)
downloadgcc-3857edb5d32dcdc11d9a2fe3ad7c156c52a1ec7f.zip
gcc-3857edb5d32dcdc11d9a2fe3ad7c156c52a1ec7f.tar.gz
gcc-3857edb5d32dcdc11d9a2fe3ad7c156c52a1ec7f.tar.bz2
analyzer: new implementation of shortest feasible path [PR96374]
The analyzer builds an exploded graph of (point,state) pairs and when it finds a problem, records a diagnostic at the relevant exploded node. Once it has finished exploring the graph, the analyzer needs to generate the shortest feasible path through the graph to each diagnostic's node. This is used: - for rejecting diagnostics that are infeasible (due to impossible sets of constraints), - for use in determining which diagnostic to use in each deduplication set (the one with the shortest path), and - for building checker_paths for the "winning" diagnostics, giving a list of events Prior to this patch the analyzer simply found the shortest path to the node, and then checked it for feasibility, which could lead to falsely rejecting diagnostics: "the shortest path, if feasible" is not the same as "the shortest feasible path" (PR analyzer/96374). An example is PR analyzer/93355, where this issue causes the analyzer to fail to emit a leak warning for a missing fclose on an error-handling path in intl/localealias.c. This patch implements a new algorithm for finding the shortest feasible path to an exploded node: instead of simply finding the shortest path, the new algorithm uses a worklist to iteratively build a tree of path prefixes, which are feasible paths by construction, until a path to the target node is found. The worklist is prioritized, so that the first feasible path discovered is the shortest possible feasible path. The algorithm continues trying paths until the target node is reached or a limit is exceeded, in which case the diagnostic is treated as being infeasible (which could still be a false negative, but is much less likely to happen than before). Iteratively building a tree of paths allows for work to be reused, and the tree can be dumped in .dot form (via a new -fdump-analyzer-feasibility option), making it much easier to debug compared to other approaches I tried. Doing so fixes the missing leak warning for PR analyzer/93355 and various other test cases. Testing: - I manually verified that the behavior is determistic using 50 builds of pr93355-localealias.c. All dumps were identical. - I manually verified that it still builds with --disable-analyzer. - Lightly tested with valgrind; no additional issues. - Lightly performance tested, showing a slight speed regression to the analyzer relative to before the patch, but correctness for this issue is more important than the slight performance hit for the analyzer. gcc/ChangeLog: PR analyzer/96374 * Makefile.in (ANALYZER_OBJS): Add analyzer/feasible-graph.o and analyzer/trimmed-graph.o. * doc/analyzer.texi (Analyzer Paths): Rewrite description of feasibility checking to reflect new implementation. * doc/invoke.texi (-fdump-analyzer-feasibility): Document new option. * shortest-paths.h (shortest_paths::get_shortest_distance): New. gcc/analyzer/ChangeLog: PR analyzer/96374 * analyzer.opt (-param=analyzer-max-infeasible-edges=): New param. (fdump-analyzer-feasibility): New flag. * diagnostic-manager.cc: Include "analyzer/trimmed-graph.h" and "analyzer/feasible-graph.h". (epath_finder::epath_finder): Convert m_sep to a pointer and only create it if !flag_analyzer_feasibility. (epath_finder::~epath_finder): New. (epath_finder::m_sep): Convert to a pointer. (epath_finder::get_best_epath): Add param "diag_idx" and use it when logging. Rather than finding the shortest path and then checking feasibility, instead use explore_feasible_paths unless !flag_analyzer_feasibility, in which case simply use the shortest path, and note if it is infeasible. Update for m_sep becoming a pointer. (class feasible_worklist): New. (epath_finder::explore_feasible_paths): New. (epath_finder::process_worklist_item): New. (class dump_eg_with_shortest_path): New. (epath_finder::dump_trimmed_graph): New. (epath_finder::dump_feasible_graph): New. (saved_diagnostic::saved_diagnostic): Add "idx" param, using it on new field m_idx. (saved_diagnostic::to_json): Dump m_idx. (saved_diagnostic::calc_best_epath): Pass m_idx to get_best_epath. Remove assertion that m_problem was set when m_best_epath is NULL. (diagnostic_manager::add_diagnostic): Pass an index when created saved_diagnostic instances. * diagnostic-manager.h (saved_diagnostic::saved_diagnostic): Add "idx" param. (saved_diagnostic::get_index): New accessor. (saved_diagnostic::m_idx): New field. * engine.cc (exploded_node::dump_dot): Call args.dump_extra_info. Move code to... (exploded_node::dump_processed_stmts): ...this new function and... (exploded_node::dump_saved_diagnostics): ...this new function. Add index of each diagnostic. (exploded_edge::dump_dot): Move bulk of code to... (exploded_edge::dump_dot_label): ...this new function. * exploded-graph.h (eg_traits::dump_args_t::dump_extra_info): New vfunc. (exploded_node::dump_processed_stmts): New decl. (exploded_node::dump_saved_diagnostics): New decl. (exploded_edge::dump_dot_label): New decl. * feasible-graph.cc: New file. * feasible-graph.h: New file. * trimmed-graph.cc: New file. * trimmed-graph.h: New file. gcc/testsuite/ChangeLog: PR analyzer/96374 * gcc.dg/analyzer/dot-output.c: Add -fdump-analyzer-feasibility to options. * gcc.dg/analyzer/feasibility-1.c (test_6): Remove xfail. (test_7): New. * gcc.dg/analyzer/pr93355-localealias-feasibility-2.c: Remove xfail. * gcc.dg/analyzer/pr93355-localealias-feasibility-3.c: Remove xfails. * gcc.dg/analyzer/pr93355-localealias-feasibility.c: Remove -fno-analyzer-feasibility from options. * gcc.dg/analyzer/pr93355-localealias.c: Likewise. * gcc.dg/analyzer/unknown-fns-4.c: Remove xfail.
Diffstat (limited to 'gcc/analyzer/trimmed-graph.cc')
-rw-r--r--gcc/analyzer/trimmed-graph.cc172
1 files changed, 172 insertions, 0 deletions
diff --git a/gcc/analyzer/trimmed-graph.cc b/gcc/analyzer/trimmed-graph.cc
new file mode 100644
index 0000000..2e23a09
--- /dev/null
+++ b/gcc/analyzer/trimmed-graph.cc
@@ -0,0 +1,172 @@
+/* Trimming an exploded graph to a subset of nodes and edges.
+ Copyright (C) 2021 Free Software Foundation, Inc.
+ Contributed by David Malcolm <dmalcolm@redhat.com>.
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify it
+under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 3, or (at your option)
+any later version.
+
+GCC is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3. If not see
+<http://www.gnu.org/licenses/>. */
+
+#include "config.h"
+#include "system.h"
+#include "coretypes.h"
+#include "tree.h"
+#include "pretty-print.h"
+#include "gcc-rich-location.h"
+#include "gimple-pretty-print.h"
+#include "function.h"
+#include "diagnostic-core.h"
+#include "diagnostic-event-id.h"
+#include "diagnostic-path.h"
+#include "alloc-pool.h"
+#include "fibonacci_heap.h"
+#include "shortest-paths.h"
+#include "sbitmap.h"
+#include "bitmap.h"
+#include "tristate.h"
+#include "selftest.h"
+#include "ordered-hash-map.h"
+#include "json.h"
+#include "analyzer/analyzer.h"
+#include "analyzer/analyzer-logging.h"
+#include "analyzer/sm.h"
+#include "analyzer/pending-diagnostic.h"
+#include "analyzer/diagnostic-manager.h"
+#include "analyzer/call-string.h"
+#include "analyzer/program-point.h"
+#include "analyzer/store.h"
+#include "analyzer/region-model.h"
+#include "analyzer/constraint-manager.h"
+#include "cfg.h"
+#include "basic-block.h"
+#include "gimple.h"
+#include "gimple-iterator.h"
+#include "cgraph.h"
+#include "digraph.h"
+#include "analyzer/supergraph.h"
+#include "analyzer/program-state.h"
+#include "analyzer/exploded-graph.h"
+#include "analyzer/trimmed-graph.h"
+
+#if ENABLE_ANALYZER
+
+namespace ana {
+
+/* class trimmed_node : public dnode<tg_traits>. */
+
+/* Implementation of dump_dot vfunc, delegating to the inner node. */
+
+void
+trimmed_node::dump_dot (graphviz_out *gv,
+ const dump_args_t &args) const
+{
+ m_inner_node->dump_dot (gv, args.m_inner_args);
+}
+
+/* class trimmed_edge : public dedge<tg_traits>. */
+
+/* trimmed_edge's ctor. */
+
+trimmed_edge::trimmed_edge (trimmed_node *src, trimmed_node *dest,
+ const exploded_edge *inner_edge)
+: dedge<tg_traits> (src, dest), m_inner_edge (inner_edge)
+{
+}
+
+/* Implementation of dump_dot vfunc, delegating to the inner edge. */
+
+void
+trimmed_edge::dump_dot (graphviz_out *gv, const dump_args_t &args) const
+{
+ m_inner_edge->dump_dot (gv, args.m_inner_args);
+}
+
+/* class trimmed_graph : public digraph <tg_traits>. */
+
+/* Ctor for trimmed_graph: construct a graph equivalent to trimming
+ INNER_GRAPH to all nodes that can reach INNER_DST_NODE. */
+
+trimmed_graph::trimmed_graph (const exploded_graph &inner_graph,
+ const exploded_node *inner_dst_node)
+: m_enodes (), m_eedges ()
+{
+ /* Determine what subset of nodes and edges to include in the
+ trimmed graph.
+ Walk backwards from INNER_DST_NODE, finding nodes that reach it,
+ iteratively building the set of nodes and edges. */
+ auto_vec <const exploded_node *> worklist;
+ worklist.safe_push (inner_dst_node);
+ m_enodes.add (inner_dst_node);
+ while (worklist.length () > 0)
+ {
+ const exploded_node *inner_node = worklist.pop ();
+ exploded_edge *inner_pred;
+ unsigned i;
+ FOR_EACH_VEC_ELT (inner_node->m_preds, i, inner_pred)
+ {
+ if (!m_enodes.contains (inner_pred->m_src))
+ {
+ worklist.safe_push (inner_pred->m_src);
+ m_enodes.add (inner_pred->m_src);
+ }
+ m_eedges.add (inner_pred);
+ }
+ }
+
+ /* Create trimmed nodes for all enodes in the set. */
+ {
+ /* Iterate over the vec rather than the hash_set
+ to ensure deterministic order. */
+ exploded_node *inner_node;
+ unsigned i;
+ FOR_EACH_VEC_ELT (inner_graph.m_nodes, i, inner_node)
+ if (m_enodes.contains (inner_node))
+ {
+ trimmed_node *tnode = new trimmed_node (inner_node);
+ add_node (tnode);
+ m_map_from_enode_to_tnode.put (inner_node, tnode);
+ }
+ }
+
+ /* Create trimmed edges for all edges in the set. */
+ {
+ /* Iterate over the vec rather than the hash_set
+ to ensure deterministic order. */
+ exploded_edge *inner_edge;
+ unsigned i;
+ FOR_EACH_VEC_ELT (inner_graph.m_edges, i, inner_edge)
+ if (m_eedges.contains (inner_edge))
+ {
+ const exploded_node *inner_src = inner_edge->m_src;
+ const exploded_node *inner_dest = inner_edge->m_dest;
+ trimmed_node *tsrc = *m_map_from_enode_to_tnode.get (inner_src);
+ trimmed_node *tdest = *m_map_from_enode_to_tnode.get (inner_dest);
+ trimmed_edge *tedge = new trimmed_edge (tsrc, tdest, inner_edge);
+ add_edge (tedge);
+ }
+ }
+}
+
+/* Dump stats about this graph to LOGGER. */
+
+void
+trimmed_graph::log_stats (logger *logger) const
+{
+ logger->log ("#nodes: %i", m_nodes.length ());
+ logger->log ("#edges: %i", m_edges.length ());
+}
+
+} // namespace ana
+
+#endif /* #if ENABLE_ANALYZER */