Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH] [gdb/symtab] Add assert in free_cached_comp_units constructor
@ 2026-05-27  9:57 Tom de Vries
  2026-05-28 16:43 ` Tom Tromey
  0 siblings, 1 reply; 6+ messages in thread
From: Tom de Vries @ 2026-05-27  9:57 UTC (permalink / raw)
  To: gdb-patches

I wrote a patch containing:
...
      dw2_instantiate_symtab (cu->per_cu, ...);
...
and ran into a use-after-free at a following use of cu.

The problem is that dw2_instantiate_symtab contains:
...
      free_cached_comp_units freer (per_objfile);
...
and that the destructor does:
...
  ~free_cached_comp_units ()
  {
    m_per_objfile->remove_all_cus ();
  }
...
which also frees the cu we used in the cu->per_cu argument to
dw2_instantiate_symtab.

Detect this situation using an assert in the free_cached_comp_units
constructor.

Tested on aarch64-linux.
---
 gdb/dwarf2/read.c | 3 +++
 gdb/dwarf2/read.h | 6 ++++++
 2 files changed, 9 insertions(+)

diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index c761b732819..0dd0e944597 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -902,6 +902,9 @@ class free_cached_comp_units
   explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
     : m_per_objfile (per_objfile)
   {
+    /* The destructor frees all cached comp units, including ones currently
+       cached, so check that there are no currently cached comp units.  */
+    gdb_assert (m_per_objfile->nr_of_cus () == 0);
   }
 
   ~free_cached_comp_units ()
diff --git a/gdb/dwarf2/read.h b/gdb/dwarf2/read.h
index 15dd2abf3a1..603fe089d6a 100644
--- a/gdb/dwarf2/read.h
+++ b/gdb/dwarf2/read.h
@@ -969,6 +969,12 @@ struct dwarf2_per_objfile
   /* Free all cached compilation units.  */
   void remove_all_cus ();
 
+  /* Return the number of cached compilation units.  */
+  size_t nr_of_cus () const
+  {
+    return m_dwarf2_cus.size ();
+  }
+
   /* Increase the age counter on each CU compilation unit and free
      any that are too old.  */
   void age_comp_units ();

base-commit: a8740b7533b4df34d4d7bc5be9bb2108d019fce5
-- 
2.51.0


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

end of thread, other threads:[~2026-06-08 14:21 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-05-27  9:57 [PATCH] [gdb/symtab] Add assert in free_cached_comp_units constructor Tom de Vries
2026-05-28 16:43 ` Tom Tromey
2026-05-30 12:52   ` Tom de Vries
2026-06-01  8:02     ` Tom de Vries
2026-06-01 15:18       ` Simon Marchi
2026-06-08 14:20         ` 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