public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: David Malcolm <dmalcolm@redhat.com>
To: gcc-patches@gcc.gnu.org
Subject: [committed] analyzer: fix false leaks when writing through unknown ptrs [PR97072]
Date: Tue,  5 Jan 2021 20:59:48 -0500	[thread overview]
Message-ID: <20210106015948.3117342-1-dmalcolm@redhat.com> (raw)

Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu.
Pushed to master as r11-6497-gac3966e315ada63eb379d560a012fa77c3909155.

gcc/analyzer/ChangeLog:
	PR analyzer/97072
	* region-model-reachability.cc (reachable_regions::init_cluster):
	Convert symbolic region handling to a switch statement.  Add cases
	to handle SK_UNKNOWN and SK_CONJURED.

gcc/testsuite/ChangeLog:
	PR analyzer/97072
	* gcc.dg/analyzer/pr97072.c: New test.
---
 gcc/analyzer/region-model-reachability.cc | 36 +++++++++++++++++------
 gcc/testsuite/gcc.dg/analyzer/pr97072.c   |  9 ++++++
 2 files changed, 36 insertions(+), 9 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/analyzer/pr97072.c

diff --git a/gcc/analyzer/region-model-reachability.cc b/gcc/analyzer/region-model-reachability.cc
index daf785254ea..a988ffc1439 100644
--- a/gcc/analyzer/region-model-reachability.cc
+++ b/gcc/analyzer/region-model-reachability.cc
@@ -88,20 +88,38 @@ reachable_regions::init_cluster (const region *base_reg)
   if (m_store->escaped_p (base_reg))
     add (base_reg, true);
 
-  /* If BASE_REG is *INIT_VAL(REG) for some other REG, see if REG is
-     unbound and untouched.  If so, then add BASE_REG as a root.  */
   if (const symbolic_region *sym_reg = base_reg->dyn_cast_symbolic_region ())
     {
       const svalue *ptr = sym_reg->get_pointer ();
-      if (const initial_svalue *init_sval = ptr->dyn_cast_initial_svalue ())
+      switch (ptr->get_kind ())
 	{
-	  const region *init_sval_reg = init_sval->get_region ();
-	  const region *other_base_reg = init_sval_reg->get_base_region ();
-	  const binding_cluster *other_cluster
-	    = m_store->get_cluster (other_base_reg);
-	  if (other_cluster == NULL
-	      || !other_cluster->touched_p ())
+	default:
+	  break;
+	case SK_INITIAL:
+	  {
+	    /* If BASE_REG is *INIT_VAL(REG) for some other REG, see if REG is
+	       unbound and untouched.  If so, then add BASE_REG as a root.  */
+	    const initial_svalue *init_sval
+	      = as_a <const initial_svalue *> (ptr);
+	    const region *init_sval_reg = init_sval->get_region ();
+	    const region *other_base_reg = init_sval_reg->get_base_region ();
+	    const binding_cluster *other_cluster
+	      = m_store->get_cluster (other_base_reg);
+	    if (other_cluster == NULL
+		|| !other_cluster->touched_p ())
+	      add (base_reg, true);
+	  }
+	  break;
+
+	case SK_UNKNOWN:
+	case SK_CONJURED:
+	  {
+	    /* If this cluster is due to dereferencing an unknown/conjured
+	       pointer, any values written through the pointer could still
+	       be live.  */
 	    add (base_reg, true);
+	  }
+	  break;
 	}
     }
 }
diff --git a/gcc/testsuite/gcc.dg/analyzer/pr97072.c b/gcc/testsuite/gcc.dg/analyzer/pr97072.c
new file mode 100644
index 00000000000..40241248884
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/analyzer/pr97072.c
@@ -0,0 +1,9 @@
+void unknown_fn_1 (void *);
+
+void test_1 (int co, int y)
+{
+  void *p = __builtin_malloc (1024);
+  void **q;
+  unknown_fn_1 (&q);
+  *q = p;
+}
-- 
2.26.2


                 reply	other threads:[~2021-01-06  1:59 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210106015948.3117342-1-dmalcolm@redhat.com \
    --to=dmalcolm@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).