]> gcc.gnu.org Git - gcc.git/commitdiff
c++/modules: Fix dangling pointer with imported_temploid_friends
authorNathaniel Shead <nathanieloshead@gmail.com>
Fri, 3 May 2024 09:36:17 +0000 (19:36 +1000)
committerNathaniel Shead <nathanieloshead@gmail.com>
Tue, 7 May 2024 01:37:47 +0000 (11:37 +1000)
I got notified by Linaro CI and by checking testresults that there seems
to be some occasional failures in tpl-friend-4_b.C on some architectures
and standards modes since r15-59-gb5f6a56940e708.  I haven't been able
to reproduce but looking at the backtrace I suspect the issue is that
we're adding to the 'imported_temploid_friend' map a decl that is
ultimately discarded, which then has its address reused by a later decl
causing a failure in the assert in 'set_originating_module'.

This patch fixes the problem by ensuring 'imported_temploid_friends' is
correctly marked as a GTY root, and that 'duplicate_decls' properly
removes entries from the map for declarations that it frees.

PR c++/114275

gcc/cp/ChangeLog:

* cp-tree.h (remove_defining_module): Declare.
* decl.cc (duplicate_decls): Call remove_defining_module on
to-be-freed newdecl.
* module.cc (imported_temploid_friends): Mark as GTY root...
(init_modules): ...and allocate from ggc.
(trees_in::decl_value): Only track for declarations that won't
be discarded.
(remove_defining_module): New function.

Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
Reviewed-by: Jason Merrill <jason@redhat.com>
Reviewed-by: Patrick Palka <ppalka@redhat.com>
gcc/cp/cp-tree.h
gcc/cp/decl.cc
gcc/cp/module.cc

index 52d6841559ca47b2d78587fafc868a99d3f78fdf..4fadc9aaf48865130b098977c88fc832cebf1698 100644 (file)
@@ -7420,6 +7420,7 @@ extern void set_instantiating_module (tree);
 extern void set_defining_module (tree);
 extern void maybe_key_decl (tree ctx, tree decl);
 extern void propagate_defining_module (tree decl, tree orig);
+extern void remove_defining_module (tree decl);
 
 extern void mangle_module (int m, bool include_partition);
 extern void mangle_module_fini ();
index 04a151c341ce9d14819d05a582c782f14866b319..b112b70659f8f52a02cfa0c81e57035fad865161 100644 (file)
@@ -3340,6 +3340,10 @@ duplicate_decls (tree newdecl, tree olddecl, bool hiding, bool was_hidden)
   if (flag_concepts)
     remove_constraints (newdecl);
 
+  /* And similarly for any module tracking data.  */
+  if (modules_p ())
+    remove_defining_module (newdecl);
+
   ggc_free (newdecl);
 
   return olddecl;
index 44dc81eed3e5167400341565771773ce652d9887..520dd710549ed735923b678b15f22428257ca85f 100644 (file)
@@ -2731,7 +2731,7 @@ static keyed_map_t *keyed_table;
    need to be attached to the same module as the temploid.  This maps
    these decls to the temploid they are instantiated them, as there is
    no other easy way to get this information.  */
-static hash_map<tree, tree> *imported_temploid_friends;
+static GTY((cache)) decl_tree_cache_map *imported_temploid_friends;
 
 /********************************************************************/
 /* Tree streaming.   The tree streaming is very specific to the tree
@@ -8327,7 +8327,8 @@ trees_in::decl_value ()
   if (TREE_CODE (inner) == FUNCTION_DECL
       || TREE_CODE (inner) == TYPE_DECL)
     if (tree owner = tree_node ())
-      imported_temploid_friends->put (decl, owner);
+      if (is_new)
+       imported_temploid_friends->put (decl, owner);
 
   /* Regular typedefs will have a NULL TREE_TYPE at this point.  */
   unsigned tdef_flags = 0;
@@ -19336,6 +19337,18 @@ propagate_defining_module (tree decl, tree orig)
     }
 }
 
+/* DECL is being freed, clear data we don't need anymore.  */
+
+void
+remove_defining_module (tree decl)
+{
+  if (!modules_p ())
+    return;
+
+  if (imported_temploid_friends)
+    imported_temploid_friends->remove (decl);
+}
+
 /* Create the flat name string.  It is simplest to have it handy.  */
 
 void
@@ -20550,7 +20563,7 @@ init_modules (cpp_reader *reader)
       entity_map = new entity_map_t (EXPERIMENT (1, 400));
       vec_safe_reserve (entity_ary, EXPERIMENT (1, 400));
       imported_temploid_friends
-       = new hash_map<tree,tree> (EXPERIMENT (1, 400));
+       = decl_tree_cache_map::create_ggc (EXPERIMENT (1, 400));
     }
 
 #if CHECKING_P
This page took 0.095099 seconds and 5 git commands to generate.