[PATCH] unshare expressions in attribute arguments

Martin Sebor msebor@gmail.com
Fri Nov 20 19:00:58 GMT 2020


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);
      }



More information about the Gcc-patches mailing list