Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Subject: [PATCH 2/2] Add locking when reading BFD sections
Date: Thu, 17 Oct 2024 18:02:38 -0600	[thread overview]
Message-ID: <20241017-fix-race-pr-symtab-31626-v1-2-3a53459e9651@tromey.com> (raw)
In-Reply-To: <20241017-fix-race-pr-symtab-31626-v1-0-3a53459e9651@tromey.com>

This adds some per-BFD locking to gdb_bfd_map_section and
gdb_bfd_get_full_section_contents.

It turned out that the background DWARF reader could race with the
auto-load code, because the reader might try to mmap a section when
the main thread was trying to read in .debug_gdb_scripts.

The current BFD threading model is that only BFD globals will be
locked, so any multi-threaded use of a BFD has to be handled specially
by the application.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31626
---
 gdb/gdb_bfd.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/gdb/gdb_bfd.c b/gdb/gdb_bfd.c
index b142f985dcd99e6d29f4a6b295ebff50ca851293..fff71d0c3bdb900e180ec136429bf66c030fd1bc 100644
--- a/gdb/gdb_bfd.c
+++ b/gdb/gdb_bfd.c
@@ -144,6 +144,19 @@ struct gdb_bfd_data
 
   /* The registry.  */
   registry<bfd> registry_fields;
+
+#if CXX_STD_THREAD
+  /* Most of the locking needed for multi-threaded operation is
+     handled by BFD itself.  However, the current BFD model is that
+     locking is only needed for global operations -- but it turned out
+     that the background DWARF reader could race with the auto-load
+     code reading the .debug_gdb_scripts section from the same BFD.
+
+     This lock is the fix: wrappers for important BFD functions will
+     acquire this lock before performing operations that might modify
+     the state of this BFD.  */
+  std::mutex per_bfd_mutex;
+#endif
 };
 
 registry<bfd> *
@@ -777,6 +790,11 @@ gdb_bfd_map_section (asection *sectp, bfd_size_type *size)
 
   abfd = sectp->owner;
 
+#if CXX_STD_THREAD
+  gdb_bfd_data *gdata = (gdb_bfd_data *) bfd_usrdata (abfd);
+  std::lock_guard<std::mutex> guard (gdata->per_bfd_mutex);
+#endif
+
   descriptor = get_section_descriptor (sectp);
 
   /* If the data was already read for this BFD, just reuse it.  */
@@ -1108,6 +1126,11 @@ bool
 gdb_bfd_get_full_section_contents (bfd *abfd, asection *section,
 				   gdb::byte_vector *contents)
 {
+#if CXX_STD_THREAD
+  gdb_bfd_data *gdata = (gdb_bfd_data *) bfd_usrdata (abfd);
+  std::lock_guard<std::mutex> guard (gdata->per_bfd_mutex);
+#endif
+
   bfd_size_type section_size = bfd_section_size (section);
 
   contents->resize (section_size);

-- 
2.46.1


  parent reply	other threads:[~2024-10-18  0:03 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-18  0:02 [PATCH 0/2] Fix a data race with the background DWARF reader Tom Tromey
2024-10-18  0:02 ` [PATCH 1/2] Use gdb_bfd_get_full_section_contents in auto-load.c Tom Tromey
2024-10-18  0:02 ` Tom Tromey [this message]
2024-10-23 22:30 ` [PATCH 0/2] Fix a data race with the background DWARF reader Kevin Buettner

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=20241017-fix-race-pr-symtab-31626-v1-2-3a53459e9651@tromey.com \
    --to=tom@tromey.com \
    --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