]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
analyzer: detect zero-assignment in phis (PR 93544)
authorDavid Malcolm <dmalcolm@redhat.com>
Mon, 3 Feb 2020 16:23:09 +0000 (11:23 -0500)
committerDavid Malcolm <dmalcolm@redhat.com>
Mon, 3 Feb 2020 19:29:08 +0000 (14:29 -0500)
PR analyzer/93544 reports an ICE when attempting to report a double-free
within diagnostic_manager::prune_for_sm_diagnostic, in which the
variable of interest has become an INTEGER_CST.  Additionally, it picks
a nonsensical path through the function in which the pointer being
double-freed is known to be NULL, which we shouldn't complain about.

The dump shows that it picks the INTEGER_CST when updating var at a phi
node:
    considering event 4, with var: ‘iftmp.0_2’, state: ‘start’
    updating from ‘iftmp.0_2’ to ‘0B’ based on phi node
      phi: iftmp.0_2 = PHI <iftmp.0_6(3), 0B(2)>
    considering event 3, with var: ‘0B’, state: ‘start’
and that it has picked the shortest path through the exploded graph,
and on this path the pointer has been assigned NULL.

The root cause is that the state machine's on_stmt isn't called for phi
nodes (and wouldn't make much sense, as we wouldn't know which arg to
choose).  malloc state machine::on_stmt "sees" a GIMPLE_ASSIGN to NULL
and handles it by transitioning the lhs to the "null" state, but never
"sees" GIMPLE_PHI nodes.

This patch fixes the ICE by wiring up phi-handling with state machines,
so that state machines have an on_phi vfunc.  It updates the only current
user of "is_zero_assignment" (the malloc sm) to implement equivalent
logic for phi nodes.  Doing so ensures that the pointer is in a separate
sm-state for the NULL vs non-NULL cases, and so gets separate exploded
nodes, and hence the path-finding logic chooses the correct path, and
the correct non-NULL phi argument.

The patch also adds some bulletproofing to prune_for_sm_diagnostic to
avoid crashing in the event of a bad path.

gcc/analyzer/ChangeLog:
PR analyzer/93544
* diagnostic-manager.cc
(diagnostic_manager::prune_for_sm_diagnostic): Bulletproof
against bad choices due to bad paths.
* engine.cc (impl_region_model_context::on_phi): New.
* exploded-graph.h (impl_region_model_context::on_phi): New decl.
* region-model.cc (region_model::on_longjmp): Likewise.
(region_model::handle_phi): Add phi param.  Call the ctxt's on_phi
vfunc.
(region_model::update_for_phis): Pass phi to handle_phi.
* region-model.h (region_model::handle_phi): Add phi param.
(region_model_context::on_phi): New vfunc.
(test_region_model_context::on_phi): New.
* sm-malloc.cc (malloc_state_machine::on_phi): New.
(malloc_state_machine::on_zero_assignment): New.
* sm.h (state_machine::on_phi): New vfunc.

gcc/testsuite/ChangeLog:
PR analyzer/93544
* gcc.dg/analyzer/torture/pr93544.c: New test.

gcc/analyzer/ChangeLog
gcc/analyzer/diagnostic-manager.cc
gcc/analyzer/engine.cc
gcc/analyzer/exploded-graph.h
gcc/analyzer/region-model.cc
gcc/analyzer/region-model.h
gcc/analyzer/sm-malloc.cc
gcc/analyzer/sm.h
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/analyzer/torture/pr93544.c [new file with mode: 0644]

index 9b8820ede033515939bef8e43870872e114c2fb4..5e444895101e25259bc26e2908f5f0db3662015d 100644 (file)
@@ -1,3 +1,22 @@
+2020-02-03  David Malcolm  <dmalcolm@redhat.com>
+
+       PR analyzer/93544
+       * diagnostic-manager.cc
+       (diagnostic_manager::prune_for_sm_diagnostic): Bulletproof
+       against bad choices due to bad paths.
+       * engine.cc (impl_region_model_context::on_phi): New.
+       * exploded-graph.h (impl_region_model_context::on_phi): New decl.
+       * region-model.cc (region_model::on_longjmp): Likewise.
+       (region_model::handle_phi): Add phi param.  Call the ctxt's on_phi
+       vfunc.
+       (region_model::update_for_phis): Pass phi to handle_phi.
+       * region-model.h (region_model::handle_phi): Add phi param.
+       (region_model_context::on_phi): New vfunc.
+       (test_region_model_context::on_phi): New.
+       * sm-malloc.cc (malloc_state_machine::on_phi): New.
+       (malloc_state_machine::on_zero_assignment): New.
+       * sm.h (state_machine::on_phi): New vfunc.
+
 2020-02-03  David Malcolm  <dmalcolm@redhat.com>
 
        * engine.cc (supernode_cluster::dump_dot): Show BB index as
index 023fda9fa7cb7989646f84ea2384f4e2f587baef..1a82d5f22ec0bc97d20e4b5c69601a3dd2b89d14 100644 (file)
@@ -1087,6 +1087,15 @@ diagnostic_manager::prune_for_sm_diagnostic (checker_path *path,
                          pp_gimple_stmt_1 (&pp, phi, 0, (dump_flags_t)0);
                          log ("  phi: %s", pp_formatted_text (&pp));
                        }
+                     /* If we've chosen a bad exploded_path, then the
+                        phi arg might be a constant.  Fail gracefully for
+                        this case.  */
+                     if (CONSTANT_CLASS_P (var))
+                       {
+                         log ("new var is a constant (bad path?);"
+                              " setting var to NULL");
+                         var = NULL;
+                       }
                    }
                }
 
index 66ca37ea33b4211fd5759ae9f0ba0d75276643cb..90f7067dec115dcc0dd9fb43ceb21720d2490cbe 100644 (file)
@@ -663,6 +663,27 @@ impl_region_model_context::on_condition (tree lhs, enum tree_code op, tree rhs)
     }
 }
 
+/* Implementation of region_model_context::on_phi vfunc.
+   Notify all state machines about the phi, which could lead to
+   state transitions.  */
+
+void
+impl_region_model_context::on_phi (const gphi *phi, tree rhs)
+{
+  int sm_idx;
+  sm_state_map *smap;
+  FOR_EACH_VEC_ELT (m_new_state->m_checker_states, sm_idx, smap)
+    {
+      const state_machine &sm = m_ext_state.get_sm (sm_idx);
+      impl_sm_context sm_ctxt (*m_eg, sm_idx, sm, m_enode_for_diag,
+                              m_old_state, m_new_state,
+                              m_change,
+                              m_old_state->m_checker_states[sm_idx],
+                              m_new_state->m_checker_states[sm_idx]);
+      sm.on_phi (&sm_ctxt, m_enode_for_diag->get_supernode (), phi, rhs);
+    }
+}
+
 /* struct point_and_state.  */
 
 /* Assert that this object is sane.  */
index c72319cb5c198bb8cb1a24cf5e4f0b26ce4d7308..6a1b9b2027744357e4317471f9257f1770a275d7 100644 (file)
@@ -74,6 +74,8 @@ class impl_region_model_context : public region_model_context
 
   void on_unknown_change (svalue_id sid ATTRIBUTE_UNUSED) FINAL OVERRIDE;
 
+  void on_phi (const gphi *phi, tree rhs) FINAL OVERRIDE;
+
   exploded_graph *m_eg;
   log_user m_logger;
   const exploded_node *m_enode_for_diag;
index 38cf3b93b28c29d06d8535443a38bc0fe95bfbe1..2cfded836b6dff61f4d8a4e4bbef67929dacae3c 100644 (file)
@@ -4568,7 +4568,8 @@ region_model::on_longjmp (const gcall *longjmp_call, const gcall *setjmp_call,
    where RHS is for the appropriate edge.  */
 
 void
-region_model::handle_phi (tree lhs, tree rhs, bool is_back_edge,
+region_model::handle_phi (const gphi *phi,
+                         tree lhs, tree rhs, bool is_back_edge,
                          region_model_context *ctxt)
 {
   /* For now, don't bother tracking the .MEM SSA names.  */
@@ -4593,6 +4594,9 @@ region_model::handle_phi (tree lhs, tree rhs, bool is_back_edge,
     }
   else
     set_value (get_lvalue (lhs, ctxt), rhs_sid, ctxt);
+
+  if (ctxt)
+    ctxt->on_phi (phi, rhs);
 }
 
 /* Implementation of region_model::get_lvalue; the latter adds type-checking.
@@ -5584,7 +5588,7 @@ region_model::update_for_phis (const supernode *snode,
 
       /* Update next_state based on phi.  */
       bool is_back_edge = last_cfg_superedge->back_edge_p ();
-      handle_phi (lhs, src, is_back_edge, ctxt);
+      handle_phi (phi, lhs, src, is_back_edge, ctxt);
     }
 }
 
index 7768e45134f557a694d557b8855ac18cec59a604..9c9a936fae2cd38e133133faf0bb437f3a4d9aa6 100644 (file)
@@ -1691,7 +1691,8 @@ class region_model
                        const cfg_superedge *last_cfg_superedge,
                        region_model_context *ctxt);
 
-  void handle_phi (tree lhs, tree rhs, bool is_back_edge,
+  void handle_phi (const gphi *phi,
+                  tree lhs, tree rhs, bool is_back_edge,
                   region_model_context *ctxt);
 
   bool maybe_update_for_edge (const superedge &edge,
@@ -1932,6 +1933,10 @@ class region_model_context
   /* Hooks for clients to be notified when an unknown change happens
      to SID (in response to a call to an unknown function).  */
   virtual void on_unknown_change (svalue_id sid) = 0;
+
+  /* Hooks for clients to be notified when a phi node is handled,
+     where RHS is the pertinent argument.  */
+  virtual void on_phi (const gphi *phi, tree rhs) = 0;
 };
 
 /* A bundle of data for use when attempting to merge two region_model
@@ -2108,6 +2113,11 @@ public:
   {
   }
 
+  void on_phi (const gphi *phi ATTRIBUTE_UNUSED,
+              tree rhs ATTRIBUTE_UNUSED) FINAL OVERRIDE
+  {
+  }
+
 private:
   /* Implicitly delete any diagnostics in the dtor.  */
   auto_delete_vec<pending_diagnostic> m_diagnostics;
index 9415a0741354036630bf2c98b524f047e0cfaab2..bdd0731b5d1e300550d3674b2ac79df3e93497c2 100644 (file)
@@ -57,6 +57,11 @@ public:
                const supernode *node,
                const gimple *stmt) const FINAL OVERRIDE;
 
+  void on_phi (sm_context *sm_ctxt,
+              const supernode *node,
+              const gphi *phi,
+              tree rhs) const FINAL OVERRIDE;
+
   void on_condition (sm_context *sm_ctxt,
                     const supernode *node,
                     const gimple *stmt,
@@ -91,6 +96,12 @@ public:
 
   /* Stop state, for pointers we don't want to track any more.  */
   state_t m_stop;
+
+private:
+  void on_zero_assignment (sm_context *sm_ctxt,
+                          const supernode *node,
+                          const gimple *stmt,
+                          tree lhs) const;
 };
 
 /* Class for diagnostics relating to malloc_state_machine.  */
@@ -682,15 +693,8 @@ malloc_state_machine::on_stmt (sm_context *sm_ctxt,
       }
 
   if (tree lhs = is_zero_assignment (stmt))
-    {
-      if (any_pointer_p (lhs))
-       {
-         sm_ctxt->on_transition (node, stmt, lhs, m_start, m_null);
-         sm_ctxt->on_transition (node, stmt, lhs, m_unchecked, m_null);
-         sm_ctxt->on_transition (node, stmt, lhs, m_nonnull, m_null);
-         sm_ctxt->on_transition (node, stmt, lhs, m_freed, m_null);
-       }
-    }
+    if (any_pointer_p (lhs))
+      on_zero_assignment (sm_ctxt, node, stmt,lhs);
 
   if (const gassign *assign_stmt = dyn_cast <const gassign *> (stmt))
     {
@@ -736,6 +740,21 @@ malloc_state_machine::on_stmt (sm_context *sm_ctxt,
   return false;
 }
 
+/* Implementation of state_machine::on_phi vfunc for malloc_state_machine.  */
+
+void
+malloc_state_machine::on_phi (sm_context *sm_ctxt,
+                             const supernode *node,
+                             const gphi *phi,
+                             tree rhs) const
+{
+  if (zerop (rhs))
+    {
+      tree lhs = gimple_phi_result (phi);
+      on_zero_assignment (sm_ctxt, node, phi, lhs);
+    }
+}
+
 /* Implementation of state_machine::on_condition vfunc for malloc_state_machine.
    Potentially transition state 'unchecked' to 'nonnull' or to 'null'.  */
 
@@ -789,6 +808,21 @@ malloc_state_machine::on_leak (tree var) const
   return new malloc_leak (*this, var);
 }
 
+/* Shared logic for handling GIMPLE_ASSIGNs and GIMPLE_PHIs that
+   assign zero to LHS.  */
+
+void
+malloc_state_machine::on_zero_assignment (sm_context *sm_ctxt,
+                                         const supernode *node,
+                                         const gimple *stmt,
+                                         tree lhs) const
+{
+  sm_ctxt->on_transition (node, stmt, lhs, m_start, m_null);
+  sm_ctxt->on_transition (node, stmt, lhs, m_unchecked, m_null);
+  sm_ctxt->on_transition (node, stmt, lhs, m_nonnull, m_null);
+  sm_ctxt->on_transition (node, stmt, lhs, m_freed, m_null);
+}
+
 } // anonymous namespace
 
 /* Internal interface to this file. */
index 3e8f4b6891d56b32a653309d4cd4f4858c5fbe04..2f00aaec7cc28e47baa8b356f016d9a1aec7d192 100644 (file)
@@ -62,6 +62,13 @@ public:
                        const supernode *node,
                        const gimple *stmt) const = 0;
 
+  virtual void on_phi (sm_context *sm_ctxt ATTRIBUTE_UNUSED,
+                      const supernode *node ATTRIBUTE_UNUSED,
+                      const gphi *phi ATTRIBUTE_UNUSED,
+                      tree rhs ATTRIBUTE_UNUSED) const
+  {
+  }
+
   virtual void on_condition (sm_context *sm_ctxt,
                             const supernode *node,
                             const gimple *stmt,
index ca5dda58e7b04d7a2778deba2b02e8f0a73ec8e5..9e2bbda7fdf8d36ff30760693d1e97935a4322d2 100644 (file)
@@ -1,3 +1,8 @@
+2020-02-03  David Malcolm  <dmalcolm@redhat.com>
+
+       PR analyzer/93544
+       * gcc.dg/analyzer/torture/pr93544.c: New test.
+
 2020-02-03  David Malcolm  <dmalcolm@redhat.com>
 
        PR analyzer/93546
diff --git a/gcc/testsuite/gcc.dg/analyzer/torture/pr93544.c b/gcc/testsuite/gcc.dg/analyzer/torture/pr93544.c
new file mode 100644 (file)
index 0000000..43edc97
--- /dev/null
@@ -0,0 +1,17 @@
+/* { dg-skip-if "" { *-*-* } { "-fno-fat-lto-objects" } { "" } } */
+
+int ja;
+
+int *
+qd (void);
+
+void
+lk (void)
+{
+  int *bs, *dx;
+
+  bs = dx = !!ja ? qd () : 0; /* { dg-message "following 'true' branch" } */
+
+  __builtin_free (dx);
+  __builtin_free (bs); /* { dg-warning "double-'free'" } */
+}