public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] unshare expressions in attribute arguments
@ 2020-11-20 19:00 Martin Sebor
  2020-11-20 19:29 ` Marek Polacek
  2020-11-22  4:01 ` Jeff Law
  0 siblings, 2 replies; 17+ messages in thread
From: Martin Sebor @ 2020-11-20 19:00 UTC (permalink / raw)
  To: gcc-patches

To detect a subset of VLA misuses, the C front associates the bounds
of VLAs in function argument lists with the corresponding variables
by implicitly adding an instance of attribute access to each function
declared to take VLAs with the bound expressions chained on the list
of attribute arguments.

Some of these expressions end up modified by the middle end, which
results in references to nonlocal variables (and perhaps other nodes)
used in these expression getting garbage collected.  A simple example
of this is described in pr97172.

By unsharing the bound expressions the patch below prevents this from
happening (it's not a fix for pr97172).

My understanding of the details of node sharing and garbage collection
in GCC is very limited (I didn't expect a tree to be garbage-collected
if it's still referenced by something).  Is this the right approach
to solving this problem?

Thanks
Martin

diff --git a/gcc/c/c-decl.c b/gcc/c/c-decl.c
index d348e39c27a..4aea4dcafb9 100644
--- a/gcc/c/c-decl.c
+++ b/gcc/c/c-decl.c
@@ -58,7 +58,7 @@ along with GCC; see the file COPYING3.  If not see
  #include "c-family/name-hint.h"
  #include "c-family/known-headers.h"
  #include "c-family/c-spellcheck.h"
-
+#include "gimplify.h"
  #include "tree-pretty-print.h"

  /* In grokdeclarator, distinguish syntactic contexts of declarators.  */
@@ -5780,6 +5780,7 @@ get_parm_array_spec (const struct c_parm *parm, 
tree attrs)
                   /* Each variable VLA bound is represented by the dollar
                      sign.  */
                   spec += "$";
+                 nelts = unshare_expr (nelts);
                   tpbnds = tree_cons (NULL_TREE, nelts, tpbnds);
                 }
             }
@@ -5834,6 +5835,7 @@ get_parm_array_spec (const struct c_parm *parm, 
tree attrs)

        /* Each variable VLA bound is represented by a dollar sign.  */
        spec += "$";
+      nelts = unshare_expr (nelts);
        vbchain = tree_cons (NULL_TREE, nelts, vbchain);
      }


^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2020-11-23 23:51 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-20 19:00 [PATCH] unshare expressions in attribute arguments Martin Sebor
2020-11-20 19:29 ` Marek Polacek
2020-11-20 20:28   ` Martin Sebor
2020-11-20 20:37     ` Jakub Jelinek
2020-11-20 21:30       ` Martin Sebor
2020-11-20 21:41         ` Jakub Jelinek
2020-11-20 21:54           ` Martin Sebor
2020-11-20 21:57             ` Jakub Jelinek
2020-11-20 22:44               ` Martin Sebor
2020-11-21  8:01                 ` Jakub Jelinek
2020-11-23 17:03                   ` Martin Sebor
2020-11-23 17:30                     ` Jakub Jelinek
2020-11-23 18:08                       ` Martin Sebor
2020-11-23 18:21                         ` Jakub Jelinek
2020-11-23 18:51                           ` Martin Sebor
2020-11-23 23:51                 ` Joseph Myers
2020-11-22  4:01 ` Jeff Law

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).