public inbox for gdb-cvs@sourceware.org
help / color / mirror / Atom feed
From: Tom de Vries <vries@sourceware.org>
To: gdb-cvs@sourceware.org
Subject: [binutils-gdb] [gdb/symtab] Make per_cu->m_lang atomic
Date: Thu, 14 Jul 2022 06:19:14 +0000 (GMT)	[thread overview]
Message-ID: <20220714061919.BDBB43858C83@sourceware.org> (raw)

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

commit 14dd1080c6127e7ad7566598860a885aa244ff8d
Author: Tom de Vries <tdevries@suse.de>
Date:   Thu Jul 14 08:19:00 2022 +0200

    [gdb/symtab] Make per_cu->m_lang atomic
    
    When building gdb with -fsanitize=thread and running test-case
    gdb.dwarf2/inlined_subroutine-inheritance.exp, we run into a data race
    between:
    ...
      Read of size 1 at 0x7b2000003010 by thread T4:
        #0 packed<language, 1ul>::operator language() const packed.h:54
        #1 dwarf2_per_cu_data::set_lang(language) read.h:363
    ...
    and:
    ...
      Previous write of size 1 at 0x7b2000003010 by main thread:
        #0 dwarf2_per_cu_data::set_lang(language) read.h:365
    ...
    
    Fix this by making per_cu->m_lang atomic.
    
    Tested on x86_64-linux.
    
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=29286

Diff:
---
 gdb/dwarf2/read.h | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)

diff --git a/gdb/dwarf2/read.h b/gdb/dwarf2/read.h
index acbe298b310..c2f86a9d367 100644
--- a/gdb/dwarf2/read.h
+++ b/gdb/dwarf2/read.h
@@ -181,7 +181,7 @@ private:
   std::atomic<packed<dwarf_unit_type, 1>> m_unit_type {(dwarf_unit_type)0};
 
   /* The language of this CU.  */
-  packed<language, LANGUAGE_BYTES> m_lang = language_unknown;
+  std::atomic<packed<language, LANGUAGE_BYTES>> m_lang {language_unknown};
 
 public:
   /* True if this CU has been scanned by the indexer; false if
@@ -351,21 +351,25 @@ public:
 
   enum language lang (bool strict_p = true) const
   {
+    enum language l = m_lang.load ();
     if (strict_p)
-      gdb_assert (m_lang != language_unknown);
-    return m_lang;
+      gdb_assert (l != language_unknown);
+    return l;
   }
 
   void set_lang (enum language lang)
   {
     if (unit_type () == DW_UT_partial)
       return;
-    if (m_lang == language_unknown)
-      /* Set if not set already.  */
-      m_lang = lang;
-    else
-      /* If already set, verify that it's the same value.  */
-      gdb_assert (m_lang == lang);
+    /* Set if not set already.  */
+    packed<language, LANGUAGE_BYTES> nope = language_unknown;
+    if (m_lang.compare_exchange_strong (nope, lang))
+      return;
+    /* If already set, verify that it's the same value.  */
+    nope = lang;
+    if (m_lang.compare_exchange_strong (nope, lang))
+      return;
+    gdb_assert_not_reached ();
   }
 
   /* Free any cached file names.  */


                 reply	other threads:[~2022-07-14  6:19 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=20220714061919.BDBB43858C83@sourceware.org \
    --to=vries@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).