From d58e54bd277b90d847be09ae4b18bfdbc0dc2066 Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Wed, 4 Aug 2021 12:44:10 -0600 Subject: [PATCH] Fix two regressions caused by CU / TU merging PR symtab/28160 and PR symtab/27893 concern GDB crashes in the test suite when using the "fission" target board. They are both caused by the patches that merge the list of CUs with the list of TUs (and to a lesser degree by the patches to share DWARF data across objfiles), and the underlying issue is the same: it turns out that reading a DWO can cause new type units to be created. This means that the list of dwarf2_per_cu_data objects depends on precisely which CUs have been expanded. However, because the type units can be created while expanding a CU means that the vector of CUs can expand while it is being iterated over -- a classic mistake. Also, because a TU can be added later, it means the resize_symtabs approach is incorrect. This patch fixes resize_symtabs by removing it, and having set_symtab resize the vector on demand. It fixes the iteration problem by introducing a safe (index-based) iterator and changing the relevant spots to use it. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=28160 Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=27893 --- gdb/dwarf2/read.c | 114 ++++++++++++++++++++++++++++++++++------------ gdb/dwarf2/read.h | 10 ---- 2 files changed, 85 insertions(+), 39 deletions(-) diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c index 2e85deeaa39..5385a3aff53 100644 --- a/gdb/dwarf2/read.c +++ b/gdb/dwarf2/read.c @@ -1643,6 +1643,73 @@ line_header_eq_voidp (const void *item_lhs, const void *item_rhs) +/* An iterator for all_comp_units that is based on index. This + approach makes it possible to iterate over all_comp_units safely, + when some caller in the loop may add new units. */ + +class all_comp_units_iterator +{ +public: + + all_comp_units_iterator (dwarf2_per_bfd *per_bfd, bool start) + : m_per_bfd (per_bfd), + m_index (start ? 0 : per_bfd->all_comp_units.size ()) + { + } + + all_comp_units_iterator &operator++ () + { + ++m_index; + return *this; + } + + dwarf2_per_cu_data *operator* () const + { + return m_per_bfd->get_cu (m_index); + } + + bool operator== (const all_comp_units_iterator &other) const + { + return m_index == other.m_index; + } + + + bool operator!= (const all_comp_units_iterator &other) const + { + return m_index != other.m_index; + } + +private: + + dwarf2_per_bfd *m_per_bfd; + size_t m_index; +}; + +/* A range adapter for the all_comp_units_iterator. */ +class all_comp_units_range +{ +public: + + all_comp_units_range (dwarf2_per_bfd *per_bfd) + : m_per_bfd (per_bfd) + { + } + + all_comp_units_iterator begin () + { + return all_comp_units_iterator (m_per_bfd, true); + } + + all_comp_units_iterator end () + { + return all_comp_units_iterator (m_per_bfd, false); + } + +private: + + dwarf2_per_bfd *m_per_bfd; +}; + /* See declaration. */ dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names, @@ -1710,9 +1777,9 @@ private: bool dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const { - gdb_assert (per_cu->index < this->m_symtabs.size ()); - - return this->m_symtabs[per_cu->index] != nullptr; + if (per_cu->index < this->m_symtabs.size ()) + return this->m_symtabs[per_cu->index] != nullptr; + return false; } /* See read.h. */ @@ -1720,9 +1787,9 @@ dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const compunit_symtab * dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const { - gdb_assert (per_cu->index < this->m_symtabs.size ()); - - return this->m_symtabs[per_cu->index]; + if (per_cu->index < this->m_symtabs.size ()) + return this->m_symtabs[per_cu->index]; + return nullptr; } /* See read.h. */ @@ -1731,9 +1798,9 @@ void dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu, compunit_symtab *symtab) { - gdb_assert (per_cu->index < this->m_symtabs.size ()); + if (per_cu->index >= this->m_symtabs.size ()) + this->m_symtabs.resize (per_cu->index + 1); gdb_assert (this->m_symtabs[per_cu->index] == nullptr); - this->m_symtabs[per_cu->index] = symtab; } @@ -4348,11 +4415,12 @@ dwarf2_gdb_index::expand_symtabs_matching gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr); if (lookup_name == nullptr) { - for (const auto &per_cu : per_objfile->per_bfd->all_comp_units) + for (dwarf2_per_cu_data *per_cu + : all_comp_units_range (per_objfile->per_bfd)) { QUIT; - if (!dw2_expand_symtabs_matching_one (per_cu.get (), per_objfile, + if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher, expansion_notify)) return false; @@ -4465,15 +4533,14 @@ dwarf2_base_index_functions::map_symbol_filenames } } - for (const auto &per_cu : per_objfile->per_bfd->all_comp_units) + for (dwarf2_per_cu_data *per_cu + : all_comp_units_range (per_objfile->per_bfd)) { /* We only need to look at symtabs not already expanded. */ - if (per_cu->is_debug_types - || per_objfile->symtab_set_p (per_cu.get ())) + if (per_cu->is_debug_types || per_objfile->symtab_set_p (per_cu)) continue; - quick_file_names *file_data = dw2_get_file_names (per_cu.get (), - per_objfile); + quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile); if (file_data == nullptr || qfn_cache.find (file_data) != qfn_cache.end ()) continue; @@ -5341,11 +5408,12 @@ dwarf2_debug_names_index::expand_symtabs_matching gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr); if (lookup_name == nullptr) { - for (const auto &per_cu : per_objfile->per_bfd->all_comp_units) + for (dwarf2_per_cu_data *per_cu + : all_comp_units_range (per_objfile->per_bfd)) { QUIT; - if (!dw2_expand_symtabs_matching_one (per_cu.get (), per_objfile, + if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher, expansion_notify)) return false; @@ -5453,7 +5521,6 @@ dwarf2_initialize_objfile (struct objfile *objfile) if (per_bfd->using_index) { dwarf_read_debug_printf ("using_index already set"); - per_objfile->resize_symtabs (); objfile->qf.push_front (make_dwarf_gdb_index ()); return; } @@ -5462,7 +5529,6 @@ dwarf2_initialize_objfile (struct objfile *objfile) create_all_comp_units (per_objfile); per_bfd->quick_file_names_table = create_quick_file_names_table (per_bfd->all_comp_units.size ()); - per_objfile->resize_symtabs (); for (int i = 0; i < per_bfd->all_comp_units.size (); ++i) { @@ -5484,7 +5550,6 @@ dwarf2_initialize_objfile (struct objfile *objfile) if (per_bfd->debug_names_table != nullptr) { dwarf_read_debug_printf ("re-using shared debug names table"); - per_objfile->resize_symtabs (); objfile->qf.push_front (make_dwarf_debug_names ()); return; } @@ -5494,7 +5559,6 @@ dwarf2_initialize_objfile (struct objfile *objfile) if (per_bfd->index_table != nullptr) { dwarf_read_debug_printf ("re-using shared index table"); - per_objfile->resize_symtabs (); objfile->qf.push_front (make_dwarf_gdb_index ()); return; } @@ -5514,7 +5578,6 @@ dwarf2_initialize_objfile (struct objfile *objfile) if (dwarf2_read_debug_names (per_objfile)) { dwarf_read_debug_printf ("found debug names"); - per_objfile->resize_symtabs (); objfile->qf.push_front (make_dwarf_debug_names ()); return; } @@ -5524,7 +5587,6 @@ dwarf2_initialize_objfile (struct objfile *objfile) get_gdb_index_contents_from_section)) { dwarf_read_debug_printf ("found gdb index from file"); - per_objfile->resize_symtabs (); objfile->qf.push_front (make_dwarf_gdb_index ()); return; } @@ -5536,7 +5598,6 @@ dwarf2_initialize_objfile (struct objfile *objfile) { dwarf_read_debug_printf ("found gdb index from cache"); global_index_cache.hit (); - per_objfile->resize_symtabs (); objfile->qf.push_front (make_dwarf_gdb_index ()); return; } @@ -5566,7 +5627,6 @@ dwarf2_build_psymtabs (struct objfile *objfile, psymbol_functions *psf) } else psf->set_partial_symtabs (per_bfd->partial_symtabs); - per_objfile->resize_symtabs (); return; } @@ -5593,8 +5653,6 @@ dwarf2_build_psymtabs (struct objfile *objfile, psymbol_functions *psf) dwarf2_build_psymtabs_hard (per_objfile); psymtabs.keep (); - per_objfile->resize_symtabs (); - /* (maybe) store an index in the cache. */ global_index_cache.store (per_objfile); } @@ -5937,8 +5995,6 @@ add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot) = per_objfile->per_bfd->allocate_signatured_type (sig); signatured_type *sig_type = sig_type_holder.get (); - per_objfile->resize_symtabs (); - per_objfile->per_bfd->all_comp_units.emplace_back (sig_type_holder.release ()); if (per_objfile->per_bfd->using_index) diff --git a/gdb/dwarf2/read.h b/gdb/dwarf2/read.h index 08ef179112c..b57e1f95c45 100644 --- a/gdb/dwarf2/read.h +++ b/gdb/dwarf2/read.h @@ -517,16 +517,6 @@ struct dwarf2_per_objfile const struct comp_unit_head *cu_header, unsigned int *bytes_read_ptr); - /* Resize the M_SYMTABS vector to the needed size (the number of partial - symtabs allocated by the per-bfd). */ - void resize_symtabs () - { - /* The symtabs vector should only grow, not shrink. */ - gdb_assert (per_bfd->all_comp_units.size () >= m_symtabs.size ()); - - m_symtabs.resize (per_bfd->all_comp_units.size ()); - } - /* Return true if the symtab corresponding to PER_CU has been set, false otherwise. */ bool symtab_set_p (const dwarf2_per_cu_data *per_cu) const; -- 2.30.2