Hi!

Apparently the cp_free_lang_data fix wasn't sufficient, the
DECL_OMP_PRIVATIZED_MEMBER vars can be copied to child functions
but without the lang specific data so it doesn't trigger any longer,
and even trying to free it in cp_free_lang_data if it satisfies
omp_member_access_dummy_var doesn't work.  So, this patch instead
looks for these variables at the end of the omp lowering phase
in methods, and removes them from GIMPLE_BIND bind vars as well as
BLOCK_VARS in the block tree.

Bootstrapped/regtested on x86_64-linux and i686-linux, committed to trunk.

2018-02-10  Jakub Jelinek  <ja...@redhat.com>

        PR sanitizer/83987
        * omp-low.c (maybe_remove_omp_member_access_dummy_vars,
        remove_member_access_dummy_vars): New functions.
        (lower_omp_for, lower_omp_taskreg, lower_omp_target,
        lower_omp_1, execute_lower_omp): Use them.

        * tree.c (cp_free_lang_data): Revert 2018-01-23 change.

        * g++.dg/ubsan/pr83987-2.C: New test.

--- gcc/omp-low.c.jj    2018-01-24 17:26:25.216357997 +0100
+++ gcc/omp-low.c       2018-02-09 15:26:25.653403123 +0100
@@ -3208,6 +3208,43 @@ scan_omp (gimple_seq *body_p, omp_contex
 
 /* Re-gimplification and code generation routines.  */
 
+/* Remove omp_member_access_dummy_var variables from gimple_bind_vars
+   of BIND if in a method.  */
+
+static void
+maybe_remove_omp_member_access_dummy_vars (gbind *bind)
+{
+  if (DECL_ARGUMENTS (current_function_decl)
+      && DECL_ARTIFICIAL (DECL_ARGUMENTS (current_function_decl))
+      && (TREE_CODE (TREE_TYPE (DECL_ARGUMENTS (current_function_decl)))
+         == POINTER_TYPE))
+    {
+      tree vars = gimple_bind_vars (bind);
+      for (tree *pvar = &vars; *pvar; )
+       if (omp_member_access_dummy_var (*pvar))
+         *pvar = DECL_CHAIN (*pvar);
+       else
+         pvar = &DECL_CHAIN (*pvar);
+      gimple_bind_set_vars (bind, vars);
+    }
+}
+
+/* Remove omp_member_access_dummy_var variables from BLOCK_VARS of
+   block and its subblocks.  */
+
+static void
+remove_member_access_dummy_vars (tree block)
+{
+  for (tree *pvar = &BLOCK_VARS (block); *pvar; )
+    if (omp_member_access_dummy_var (*pvar))
+      *pvar = DECL_CHAIN (*pvar);
+    else
+      pvar = &DECL_CHAIN (*pvar);
+
+  for (block = BLOCK_SUBBLOCKS (block); block; block = BLOCK_CHAIN (block))
+    remove_member_access_dummy_vars (block);
+}
+
 /* If a context was created for STMT when it was scanned, return it.  */
 
 static omp_context *
@@ -6961,6 +6998,7 @@ lower_omp_for (gimple_stmt_iterator *gsi
   pop_gimplify_context (new_stmt);
 
   gimple_bind_append_vars (new_stmt, ctx->block_vars);
+  maybe_remove_omp_member_access_dummy_vars (new_stmt);
   BLOCK_VARS (block) = gimple_bind_vars (new_stmt);
   if (BLOCK_VARS (block))
     TREE_USED (block) = 1;
@@ -7413,6 +7451,7 @@ lower_omp_taskreg (gimple_stmt_iterator
   /* Declare all the variables created by mapping and the variables
      declared in the scope of the parallel body.  */
   record_vars_into (ctx->block_vars, child_fn);
+  maybe_remove_omp_member_access_dummy_vars (par_bind);
   record_vars_into (gimple_bind_vars (par_bind), child_fn);
 
   if (ctx->record_type)
@@ -7781,6 +7820,7 @@ lower_omp_target (gimple_stmt_iterator *
       /* Declare all the variables created by mapping and the variables
         declared in the scope of the target body.  */
       record_vars_into (ctx->block_vars, child_fn);
+      maybe_remove_omp_member_access_dummy_vars (tgt_bind);
       record_vars_into (gimple_bind_vars (tgt_bind), child_fn);
     }
 
@@ -8772,6 +8812,7 @@ lower_omp_1 (gimple_stmt_iterator *gsi_p
       break;
     case GIMPLE_BIND:
       lower_omp (gimple_bind_body_ptr (as_a <gbind *> (stmt)), ctx);
+      maybe_remove_omp_member_access_dummy_vars (as_a <gbind *> (stmt));
       break;
     case GIMPLE_OMP_PARALLEL:
     case GIMPLE_OMP_TASK:
@@ -8976,6 +9017,16 @@ execute_lower_omp (void)
       all_contexts = NULL;
     }
   BITMAP_FREE (task_shared_vars);
+
+  /* If current function is a method, remove artificial dummy VAR_DECL created
+     for non-static data member privatization, they aren't needed for
+     debuginfo nor anything else, have been already replaced everywhere in the
+     IL and cause problems with LTO.  */
+  if (DECL_ARGUMENTS (current_function_decl)
+      && DECL_ARTIFICIAL (DECL_ARGUMENTS (current_function_decl))
+      && (TREE_CODE (TREE_TYPE (DECL_ARGUMENTS (current_function_decl)))
+         == POINTER_TYPE))
+    remove_member_access_dummy_vars (DECL_INITIAL (current_function_decl));
   return 0;
 }
 
--- gcc/cp/tree.c.jj    2018-02-09 06:44:24.855812073 +0100
+++ gcc/cp/tree.c       2018-02-09 13:44:22.938670431 +0100
@@ -5273,16 +5273,6 @@ cp_free_lang_data (tree t)
     /* We do not need the leftover chaining of namespaces from the
        binding level.  */
     DECL_CHAIN (t) = NULL_TREE;
-  /* Set DECL_VALUE_EXPRs of OpenMP privatized member artificial
-     decls to error_mark_node.  These are DECL_IGNORED_P and after
-     OpenMP lowering they aren't useful anymore.  Clearing DECL_VALUE_EXPR
-     doesn't work, as expansion could then consider them as something
-     to be expanded.  */
-  if (VAR_P (t)
-      && DECL_LANG_SPECIFIC (t)
-      && DECL_OMP_PRIVATIZED_MEMBER (t)
-      && DECL_IGNORED_P (t))
-    SET_DECL_VALUE_EXPR (t, error_mark_node);
 }
 
 /* Stub for c-common.  Please keep in sync with c-decl.c.
--- gcc/testsuite/g++.dg/ubsan/pr83987-2.C.jj   2018-02-09 12:36:23.124496958 
+0100
+++ gcc/testsuite/g++.dg/ubsan/pr83987-2.C      2018-02-09 12:20:52.567160966 
+0100
@@ -0,0 +1,24 @@
+// PR sanitizer/83987
+// { dg-do compile { target fopenmp } }
+// { dg-options "-fopenmp -fsanitize=vptr" }
+
+struct A
+{
+  int i;
+};
+
+struct B : virtual A
+{
+  void foo();
+};
+
+void B::foo()
+{
+#pragma omp parallel
+  {
+  #pragma omp sections lastprivate (i)
+    {
+      i = 0;
+    }
+  }
+}

        Jakub

Reply via email to