public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Fix crash when creating index from index
@ 2022-04-21 17:57 Tom Tromey
  2022-05-04 14:32 ` Tom Tromey
  0 siblings, 1 reply; 2+ messages in thread
From: Tom Tromey @ 2022-04-21 17:57 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

My patches yesterday to unify the DWARF index base classes had a bug
-- namely, I did the wholesale dynamic_cast-to-static_cast too hastily
and introduced a crash.  This can be seen by trying to add an index to
a file that has an index, or by running a test like gdb-index-cxx.exp
using the cc-with-debug-names.exp target board.

This patch fixes the crash by introducing a new virtual method and
removing some of the static casts.
---
 gdb/dwarf2/cooked-index.h |  5 +++++
 gdb/dwarf2/index-write.c  | 27 +++++++++------------------
 gdb/dwarf2/mapped-index.h | 14 +++++++++++++-
 3 files changed, 27 insertions(+), 19 deletions(-)

diff --git a/gdb/dwarf2/cooked-index.h b/gdb/dwarf2/cooked-index.h
index e1ff05645c5..fbee7999f56 100644
--- a/gdb/dwarf2/cooked-index.h
+++ b/gdb/dwarf2/cooked-index.h
@@ -292,6 +292,11 @@ class cooked_index_vector : public dwarf_scanner_base
      "main".  This will return NULL if no such entry is available.  */
   const cooked_index_entry *get_main () const;
 
+  cooked_index_vector *index_for_writing () override
+  {
+    return this;
+  }
+
   quick_symbol_functions_up make_quick_functions () const override;
 
 private:
diff --git a/gdb/dwarf2/index-write.c b/gdb/dwarf2/index-write.c
index b7a2e214f6b..c228dc85f61 100644
--- a/gdb/dwarf2/index-write.c
+++ b/gdb/dwarf2/index-write.c
@@ -1122,8 +1122,9 @@ write_cooked_index (cooked_index_vector *table,
    associated dwz file, DWZ_OUT_FILE must be NULL.  */
 
 static void
-write_gdbindex (dwarf2_per_objfile *per_objfile, FILE *out_file,
-		FILE *dwz_out_file)
+write_gdbindex (dwarf2_per_objfile *per_objfile,
+		cooked_index_vector *table,
+		FILE *out_file, FILE *dwz_out_file)
 {
   mapped_symtab symtab;
   data_buf objfile_cu_list;
@@ -1177,9 +1178,6 @@ write_gdbindex (dwarf2_per_objfile *per_objfile, FILE *out_file,
       ++this_counter;
     }
 
-  cooked_index_vector *table
-    = (static_cast<cooked_index_vector *>
-       (per_objfile->per_bfd->index_table.get ()));
   write_cooked_index (table, cu_index_htab, &symtab);
 
   /* Dump the address map.  */
@@ -1215,6 +1213,7 @@ static const gdb_byte dwarf5_gdb_augmentation[] = { 'G', 'D', 'B', 0 };
 
 static void
 write_debug_names (dwarf2_per_objfile *per_objfile,
+		   cooked_index_vector *table,
 		   FILE *out_file, FILE *out_file_str)
 {
   const bool dwarf5_is_dwarf64 = check_dwarf64_offsets (per_objfile);
@@ -1250,9 +1249,6 @@ write_debug_names (dwarf2_per_objfile *per_objfile,
 			  - per_objfile->per_bfd->tu_stats.nr_tus));
   gdb_assert (types_counter == per_objfile->per_bfd->tu_stats.nr_tus);
 
-  cooked_index_vector *table
-    = (static_cast<cooked_index_vector *>
-       (per_objfile->per_bfd->index_table.get ()));
   for (const cooked_index_entry *entry : table->all_entries ())
     nametable.insert (entry);
 
@@ -1390,15 +1386,10 @@ write_dwarf_index (dwarf2_per_objfile *per_objfile, const char *dir,
 {
   struct objfile *objfile = per_objfile->objfile;
 
+  if (per_objfile->per_bfd->index_table == nullptr)
+    error (_("No debugging symbols"));
   cooked_index_vector *table
-    = (static_cast<cooked_index_vector *>
-       (per_objfile->per_bfd->index_table.get ()));
-  if (table == nullptr)
-    {
-      if (per_objfile->per_bfd->index_table != nullptr)
-	error (_("Cannot use an index to create the index"));
-      error (_("No debugging symbols"));
-    }
+    = per_objfile->per_bfd->index_table->index_for_writing ();
 
   if (per_objfile->per_bfd->types.size () > 1)
     error (_("Cannot make an index when the file has multiple .debug_types sections"));
@@ -1420,13 +1411,13 @@ write_dwarf_index (dwarf2_per_objfile *per_objfile, const char *dir,
     {
       index_wip_file str_wip_file (dir, basename, DEBUG_STR_SUFFIX);
 
-      write_debug_names (per_objfile, objfile_index_wip.out_file.get (),
+      write_debug_names (per_objfile, table, objfile_index_wip.out_file.get (),
 			 str_wip_file.out_file.get ());
 
       str_wip_file.finalize ();
     }
   else
-    write_gdbindex (per_objfile, objfile_index_wip.out_file.get (),
+    write_gdbindex (per_objfile, table, objfile_index_wip.out_file.get (),
 		    (dwz_index_wip.has_value ()
 		     ? dwz_index_wip->out_file.get () : NULL));
 
diff --git a/gdb/dwarf2/mapped-index.h b/gdb/dwarf2/mapped-index.h
index e5d77469925..7d71347f9f4 100644
--- a/gdb/dwarf2/mapped-index.h
+++ b/gdb/dwarf2/mapped-index.h
@@ -1,6 +1,6 @@
 /* Base class for mapped indices
 
-   Copyright (C) 2021 Free Software Foundation, Inc.
+   Copyright (C) 2021, 2022 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -47,6 +47,8 @@ struct name_component
   offset_type idx;
 };
 
+class cooked_index_vector;
+
 /* Base class of all DWARF scanner types.  */
 
 struct dwarf_scanner_base
@@ -66,6 +68,11 @@ struct dwarf_scanner_base
   {
     return true;
   }
+
+  /* This is called when writing an index.  For a cooked index, it
+     will return 'this' as a cooked index.  For other forms, it will
+     throw an exception with an appropriate error message.  */
+  virtual cooked_index_vector *index_for_writing () = 0;
 };
 
 /* Base class containing bits shared by both .gdb_index and
@@ -109,6 +116,11 @@ struct mapped_index_base : public dwarf_scanner_base
     find_name_components_bounds (const lookup_name_info &ln_no_params,
 				 enum language lang,
 				 dwarf2_per_objfile *per_objfile) const;
+
+  cooked_index_vector *index_for_writing () override
+  {
+    error (_("Cannot use an index to create the index"));
+  }
 };
 
 #endif /* GDB_DWARF2_MAPPED_INDEX_H */
-- 
2.34.1


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

* Re: [PATCH] Fix crash when creating index from index
  2022-04-21 17:57 [PATCH] Fix crash when creating index from index Tom Tromey
@ 2022-05-04 14:32 ` Tom Tromey
  0 siblings, 0 replies; 2+ messages in thread
From: Tom Tromey @ 2022-05-04 14:32 UTC (permalink / raw)
  To: Tom Tromey via Gdb-patches; +Cc: Tom Tromey

>>>>> "Tom" == Tom Tromey via Gdb-patches <gdb-patches@sourceware.org> writes:

Tom> My patches yesterday to unify the DWARF index base classes had a bug
Tom> -- namely, I did the wholesale dynamic_cast-to-static_cast too hastily
Tom> and introduced a crash.  This can be seen by trying to add an index to
Tom> a file that has an index, or by running a test like gdb-index-cxx.exp
Tom> using the cc-with-debug-names.exp target board.

Tom> This patch fixes the crash by introducing a new virtual method and
Tom> removing some of the static casts.

I'm checking this in.

Tom

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

end of thread, other threads:[~2022-05-04 14:32 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-21 17:57 [PATCH] Fix crash when creating index from index Tom Tromey
2022-05-04 14:32 ` Tom Tromey

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).