public inbox for gdb-cvs@sourceware.org
help / color / mirror / Atom feed
From: Tom Tromey <tromey@sourceware.org>
To: gdb-cvs@sourceware.org
Subject: [binutils-gdb] Avoid race when reading dwz file
Date: Fri,  8 Mar 2024 14:32:23 +0000 (GMT)	[thread overview]
Message-ID: <20240308143223.550CD3858D33@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=e9b738dfbdc504589e1a365ce32981f4b23c22c3

commit e9b738dfbdc504589e1a365ce32981f4b23c22c3
Author: Tom Tromey <tom@tromey.com>
Date:   Thu Jan 25 06:48:22 2024 -0700

    Avoid race when reading dwz file
    
    PR gdb/31260 points out a race introduced by the background reading
    changes.  If a given objfile is re-opened when it is already being
    read, dwarf2_initialize_objfile will call dwarf2_read_dwz_file again,
    causing the 'dwz_file' to be reset.
    
    This patch fixes the problem by arranging to open the dwz just once:
    when the dwarf2_per_bfd object is created.
    
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31260

Diff:
---
 gdb/dwarf2/dwz.c  | 15 ++++++---------
 gdb/dwarf2/read.c | 29 ++++++++++++++++++-----------
 2 files changed, 24 insertions(+), 20 deletions(-)

diff --git a/gdb/dwarf2/dwz.c b/gdb/dwarf2/dwz.c
index 5c855f4c265..f53a5b64c4a 100644
--- a/gdb/dwarf2/dwz.c
+++ b/gdb/dwarf2/dwz.c
@@ -188,6 +188,8 @@ dwarf2_read_dwz_file (dwarf2_per_objfile *per_objfile)
      only be run in the main thread.  */
   gdb_assert (is_main_thread ());
 
+  /* This should only be called once.  */
+  gdb_assert (!per_bfd->dwz_file.has_value ());
   /* Set this early, so that on error it remains NULL.  */
   per_bfd->dwz_file.emplace (nullptr);
 
@@ -281,14 +283,9 @@ dwarf2_read_dwz_file (dwarf2_per_objfile *per_objfile)
 struct dwz_file *
 dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd, bool require)
 {
-  gdb_assert (!require || per_bfd->dwz_file.has_value ());
-
-  dwz_file *result = nullptr;
-  if (per_bfd->dwz_file.has_value ())
-    {
-      result = per_bfd->dwz_file->get ();
-      if (require && result == nullptr)
-	error (_("could not read '.gnu_debugaltlink' section"));
-    }
+  gdb_assert (per_bfd->dwz_file.has_value ());
+  dwz_file *result = per_bfd->dwz_file->get ();
+  if (require && result == nullptr)
+    error (_("could not read '.gnu_debugaltlink' section"));
   return result;
 }
diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index 486be7e4921..4afb026b8ce 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -1366,6 +1366,7 @@ dwarf2_has_info (struct objfile *objfile,
   if (per_objfile == NULL)
     {
       dwarf2_per_bfd *per_bfd;
+      bool just_created = false;
 
       /* We can share a "dwarf2_per_bfd" with other objfiles if the
 	 BFD doesn't require relocations.
@@ -1385,6 +1386,7 @@ dwarf2_has_info (struct objfile *objfile,
 	      per_bfd = new dwarf2_per_bfd (objfile->obfd.get (), names,
 					    can_copy);
 	      dwarf2_per_bfd_bfd_data_key.set (objfile->obfd.get (), per_bfd);
+	      just_created = true;
 	    }
 	}
       else
@@ -1392,9 +1394,25 @@ dwarf2_has_info (struct objfile *objfile,
 	  /* No sharing possible, create one specifically for this objfile.  */
 	  per_bfd = new dwarf2_per_bfd (objfile->obfd.get (), names, can_copy);
 	  dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
+	  just_created = true;
 	}
 
       per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
+
+      if (just_created)
+	{
+	  /* Try to fetch any potential dwz file early, while still on
+	     the main thread.  Also, be sure to do it just once per
+	     BFD, to avoid races.  */
+	  try
+	    {
+	      dwarf2_read_dwz_file (per_objfile);
+	    }
+	  catch (const gdb_exception_error &err)
+	    {
+	      warning (_("%s"), err.what ());
+	    }
+	}
     }
 
   return (!per_objfile->per_bfd->info.is_virtual
@@ -3202,17 +3220,6 @@ dwarf2_initialize_objfile (struct objfile *objfile,
 
   dwarf_read_debug_printf ("called");
 
-  /* Try to fetch any potential dwz file early, while still on the
-     main thread.  */
-  try
-    {
-      dwarf2_read_dwz_file (per_objfile);
-    }
-  catch (const gdb_exception_error &err)
-    {
-      warning (_("%s"), err.what ());
-    }
-
   /* If we're about to read full symbols, don't bother with the
      indices.  In this case we also don't care if some other debug
      format is making psymtabs, because they are all about to be

                 reply	other threads:[~2024-03-08 14:32 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20240308143223.550CD3858D33@sourceware.org \
    --to=tromey@sourceware.org \
    --cc=gdb-cvs@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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).