Bug 30799

Summary: [gdb/symtab] hang in inherit_abstract_dies
Product: gdb Reporter: Tom de Vries <vries>
Component: symtabAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED FIXED    
Severity: normal    
Priority: P2    
Version: HEAD   
Target Milestone: 14.1   
Host: Target:
Build: Last reconfirmed:

Description Tom de Vries 2023-08-28 04:24:08 UTC
I build gdb with gcc 7.5.0 and -O2 -flto -flto-partition=one, and ran into timeouts in gdb.gdb/*.exp.

The hang seems to be related to a self-referencing DIE:
...
 <2><91dace>: Abbrev Number: 405 (DW_TAG_label)
    <91dad0>   DW_AT_abstract_origin: <0x91dace>
...

We're stuck in this loop in inherit_abstract_dies:
...
      /* For each CHILD_DIE, find the corresponding child of                                                          
         ORIGIN_DIE.  If there is more than one layer of                                                              
         DW_AT_abstract_origin, follow them all; there shouldn't be,                                                  
         but GCC versions at least through 4.4 generate this (GCC PR                                                  
         40573).  */
      die_info *child_origin_die = child_die;
      dwarf2_cu *child_origin_cu = cu;
      while (true)
        {
          attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
                              child_origin_cu);
          if (attr == nullptr)
            break;


          child_origin_die = follow_die_ref (child_origin_die, attr,
                                             &child_origin_cu);
        }
...
Comment 1 Tom de Vries 2023-08-28 04:25:43 UTC
Tentative patch:
...
diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index eb4cb9ba72e..d68882ae657 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -9935,8 +9935,12 @@ inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
 	  if (attr == nullptr)
 	    break;
 
+	  die_info *prev_child_origin_die = child_origin_die;
 	  child_origin_die = follow_die_ref (child_origin_die, attr,
 					     &child_origin_cu);
+	  if (prev_child_origin_die == child_origin_die)
+	    break;
 	}
 
       /* If missing DW_AT_abstract_origin, try the corresponding child
...
Comment 3 Sourceware Commits 2023-08-28 14:27:39 UTC
The master branch has been updated by Tom de Vries <vries@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=8d83f51b91072bc3d79061eba7e564ba9be6afd7

commit 8d83f51b91072bc3d79061eba7e564ba9be6afd7
Author: Tom de Vries <tdevries@suse.de>
Date:   Mon Aug 28 16:27:58 2023 +0200

    [gdb/symtab] Handle self-reference in inherit_abstract_dies
    
    Building gdb with gcc 7.5.0 and -flto -O2 -flto-partition=one generates a
    self-referencing DIE:
    ...
     <2><91dace>: Abbrev Number: 405 (DW_TAG_label)
        <91dad0>   DW_AT_abstract_origin: <0x91dace>
    ...
    
    When encountering the self-reference DIE in inherit_abstract_dies we loop
    following the abstract origin, effectively hanging gdb.
    
    Fix this by handling self-referencing DIEs in the loop in
    inherit_abstract_dies.
    
    Tested on x86_64-linux.
    
    Approved-By: Tom Tromey <tom@tromey.com>
    
    PR symtab/30799
    https://sourceware.org/bugzilla/show_bug.cgi?id=30799
Comment 4 Tom de Vries 2023-08-28 14:28:02 UTC
Fixed.