public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 1/2] [gdb/symtab] Fix segfault in search_one_symtab
@ 2021-11-12 17:16 Tom de Vries
  2021-11-12 17:16 ` [PATCH 2/2] [gdb/symtab] Add maint expand-symtabs -verbose Tom de Vries
  2021-11-13 14:51 ` [PATCH 1/2] [gdb/symtab] Fix segfault in search_one_symtab Simon Marchi
  0 siblings, 2 replies; 10+ messages in thread
From: Tom de Vries @ 2021-11-12 17:16 UTC (permalink / raw)
  To: gdb-patches

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


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2022-01-10  2:34 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-12 17:16 [PATCH 1/2] [gdb/symtab] Fix segfault in search_one_symtab Tom de Vries
2021-11-12 17:16 ` [PATCH 2/2] [gdb/symtab] Add maint expand-symtabs -verbose Tom de Vries
2022-01-03 14:53   ` [PING][PATCH " Tom de Vries
2022-01-08 10:55     ` Joel Brobecker
2022-01-10  2:34   ` [PATCH " Simon Marchi
2021-11-13 14:51 ` [PATCH 1/2] [gdb/symtab] Fix segfault in search_one_symtab Simon Marchi
2021-11-15 12:58   ` Tom de Vries
2021-11-22 19:29     ` [PING][PATCH " Tom de Vries
2021-11-29 13:51     ` [PATCH " Simon Marchi
2021-11-29 15:22       ` Tom de Vries

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).