[PATCH 1/2] [gdb/symtab] Fix segfault in search_one_symtab

Tom de Vries tdevries@suse.de
Fri Nov 12 17:16:56 GMT 2021


PR28539 describes a segfault in lambda function search_one_symtab due to
psymbol_functions::expand_symtabs_matching calling expansion_notify with a
nullptr symtab:
...
          struct compunit_symtab *symtab =
            psymtab_to_symtab (objfile, ps);

          if (expansion_notify != NULL)
            if (!expansion_notify (symtab))
              return false;
...

This happens as follows.  The partial symtab ps is a dwarf2_include_psymtab
for some header file:
...
(gdb) p ps.filename
$5 = 0x64fcf80 "/usr/include/c++/11/bits/stl_construct.h"
...

The includer of ps is a shared symtab for a partial unit, with as user:
...
(gdb) p ps.includer().user.filename
$11 = 0x64fc9f0 \
  "/usr/src/debug/llvm13-13.0.0-1.2.x86_64/tools/clang/lib/AST/Decl.cpp"
...

The call to psymtab_to_symtab expands the Decl.cpp symtab (and consequently
the shared symtab), but returns nullptr because:
...
struct dwarf2_include_psymtab : public partial_symtab
{
  ...
  compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
  {
    return nullptr;
  }
...

Fix this by returning the Decl.cpp symtab instead, which fixes the segfault
in the PR.

While trying to write a reproducer for this, I realized that this is difficult
because not all callers of psymbol_functions::expand_symtabs_matching have an
expansion_notify.  Consequently, I decided to add this assert:
...
          struct compunit_symtab *symtab =
            psymtab_to_symtab (objfile, ps);

+         gdb_assert (symtab != nullptr);
+
          if (expansion_notify != NULL)
            if (!expansion_notify (symtab))
              return false;
...
which without the fix triggers in a few test-cases, f.i.:
...
(gdb) maint expand-symtab dw2-symtab-includes.h^M
psymtab.c:1155: internal-error: virtual bool \
  psymbol_functions::expand_symtabs_matching(...): \
  Assertion `symtab != nullptr' failed.^M
A problem internal to GDB has been detected,^M
further debugging may prove unreliable.^M
FAIL: gdb.dwarf2/dw2-symtab-includes.exp: \
  maint expand-symtab dw2-symtab-includes.h (GDB internal error)
...

I also realized that with the assert fixed, it becomes possible to implement
a "maint expand-symtabs -verbose".

Tested on x86_64-linux.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=28539
---
 gdb/dwarf2/read.c | 5 ++++-
 gdb/psymtab.c     | 2 ++
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index ed101237587..b59c638b2eb 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -5787,7 +5787,10 @@ struct dwarf2_include_psymtab : public partial_symtab
 
   compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
   {
-    return nullptr;
+    compunit_symtab *cust = includer ()->get_compunit_symtab (objfile);
+    while (cust != nullptr && cust->user != nullptr)
+      cust = cust->user;
+    return cust;
   }
 
 private:
diff --git a/gdb/psymtab.c b/gdb/psymtab.c
index 7ffb7437785..e09537d8f5e 100644
--- a/gdb/psymtab.c
+++ b/gdb/psymtab.c
@@ -1152,6 +1152,8 @@ psymbol_functions::expand_symtabs_matching
 	  struct compunit_symtab *symtab =
 	    psymtab_to_symtab (objfile, ps);
 
+	  gdb_assert (symtab != nullptr);
+
 	  if (expansion_notify != NULL)
 	    if (!expansion_notify (symtab))
 	      return false;

base-commit: 1f28b70def1bea937fb9227c8346657016168456
-- 
2.26.2



More information about the Gdb-patches mailing list