https://gcc.gnu.org/g:1ed04a18ef6bb051a4f681963b4ff6f7b156d051

commit r17-6609-g1ed04a18ef6bb051a4f681963b4ff6f7b156d051
Author: Oleg Tolmatcev <[email protected]>
Date:   Mon Nov 23 11:39:53 2026 -0700

    [PATCH] codeview: Avoid retaining released function objects
    
    cgraph releases each function object after expansion.  CodeView retained
    cfun and dereferenced it from cv_func_hasher while compiling later
    functions, causing an ICE.  Store the DIE and function definition number
    instead, and hash stored entries by DIE consistently with lookups.
    
    gcc/ChangeLog:
    
            * dwarf2codeview.cc (codeview_function): Replace func with die and
            funcdef_no.
            (cv_func_hasher::hash): New function.
            (cv_func_hasher::equal): Compare the stored DIE directly.
            (new_codeview_function): Store the DIE and function definition 
number.
            (codeview_source_line): Compare function definition numbers.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/debug/codeview/func-lifetime-1.C: New test.
    
    Signed-off-by: Oleg Tolmatcev <[email protected]>

Diff:
---
 gcc/dwarf2codeview.cc                              | 22 +++++++++++++---------
 .../g++.dg/debug/codeview/func-lifetime-1.C        | 12 ++++++++++++
 2 files changed, 25 insertions(+), 9 deletions(-)

diff --git a/gcc/dwarf2codeview.cc b/gcc/dwarf2codeview.cc
index 2089ea8a4cf2..8d5d1dce48eb 100644
--- a/gcc/dwarf2codeview.cc
+++ b/gcc/dwarf2codeview.cc
@@ -1177,7 +1177,8 @@ struct codeview_function
 {
   codeview_function *next;
   codeview_function *htab_next;
-  function *func;
+  dw_die_ref die;
+  int funcdef_no;
   unsigned int end_label;
   codeview_line_block *blocks, *last_block;
   codeview_function *parent;
@@ -1470,9 +1471,14 @@ struct cv_func_hasher : nofree_ptr_hash <struct 
codeview_function>
 {
   typedef dw_die_ref compare_type;
 
+  static hashval_t hash (const codeview_function *f)
+  {
+    return htab_hash_pointer (f->die);
+  }
+
   static bool equal (const codeview_function *f, dw_die_ref die)
   {
-    return lookup_decl_die (f->func->decl) == die;
+    return f->die == die;
   }
 };
 
@@ -1544,13 +1550,13 @@ static codeview_function *
 new_codeview_function (void)
 {
   codeview_function **slot;
-  dw_die_ref die;
   codeview_function *f = (codeview_function *)
                            xmalloc (sizeof (codeview_function));
 
   f->next = NULL;
   f->htab_next = NULL;
-  f->func = cfun;
+  f->die = lookup_decl_die (cfun->decl);
+  f->funcdef_no = current_function_funcdef_no;
   f->end_label = 0;
   f->blocks = f->last_block = NULL;
   f->inline_block = 0;
@@ -1566,10 +1572,8 @@ new_codeview_function (void)
   if (!cv_func_htab)
     cv_func_htab = new hash_table<cv_func_hasher> (10);
 
-  die = lookup_decl_die (cfun->decl);
-
-  slot = cv_func_htab->find_slot_with_hash (die, htab_hash_pointer (die),
-                                           INSERT);
+  slot = cv_func_htab->find_slot_with_hash (f->die,
+                                           htab_hash_pointer (f->die), INSERT);
   if (*slot)
     f->htab_next = *slot;
 
@@ -1589,7 +1593,7 @@ codeview_source_line (unsigned int line_no, const char 
*filename)
 
   targetm.asm_out.internal_label (asm_out_file, LINE_LABEL, label_num);
 
-  if (!cur_func || cur_func->func != cfun)
+  if (!cur_func || cur_func->funcdef_no != current_function_funcdef_no)
     {
       codeview_function *f = new_codeview_function ();
 
diff --git a/gcc/testsuite/g++.dg/debug/codeview/func-lifetime-1.C 
b/gcc/testsuite/g++.dg/debug/codeview/func-lifetime-1.C
new file mode 100644
index 000000000000..bd7bf72dd190
--- /dev/null
+++ b/gcc/testsuite/g++.dg/debug/codeview/func-lifetime-1.C
@@ -0,0 +1,12 @@
+// { dg-do compile }
+// { dg-options "-O0 -gcodeview" }
+
+// cv_func_htab must not dereference function objects after their bodies have
+// been released.
+
+#define F(N) int f##N () { return N; }
+
+F (0)
+F (1)
+F (2)
+F (3)

Reply via email to