From: Tom de Vries <tdevries@suse.de>
To: gdb-patches@sourceware.org
Subject: [PATCH] [gdb/symtab] Add assert in free_cached_comp_units constructor
Date: Wed, 27 May 2026 11:57:15 +0200 [thread overview]
Message-ID: <20260527095715.2481440-1-tdevries@suse.de> (raw)
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
next reply other threads:[~2026-05-27 9:57 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-27 9:57 Tom de Vries [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260527095715.2481440-1-tdevries@suse.de \
--to=tdevries@suse.de \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox