public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom de Vries <tdevries@suse.de>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [PATCH 5/5] [gdb/symtab] Fix data race on per_cu->lang
Date: Wed, 29 Jun 2022 17:29:14 +0200	[thread overview]
Message-ID: <20220629152914.13149-5-tdevries@suse.de> (raw)
In-Reply-To: <20220629152914.13149-1-tdevries@suse.de>

When building gdb with -fsanitize=thread and gcc 12, and running test-case
gdb.dwarf2/inlined_subroutine-inheritance.exp, we run into a data race
between thread T4 and the main thread:
...
  Write of size 1 at 0x7b200000308d:
    #0 prepare_one_comp_unit gdb/dwarf2/read.c:23586 (gdb+0x86f859)
...
which is here:
...
    cu->per_cu->lang = dwarf_lang_to_enum_language (attr->constant_value (0));
...

Both writes are called from the parallel for in dwarf2_build_psymtabs_hard,
this one directly:
...
    #1 process_psymtab_comp_unit gdb/dwarf2/read.c:6812 (gdb+0x830912)
    #2 operator() gdb/dwarf2/read.c:7102 (gdb+0x831902)
    #3 operator() gdb/../gdbsupport/parallel-for.h:171 (gdb+0x8723a8)
...
and this one when handling cross-CU refs:
...
    #1 cooked_indexer::ensure_cu_exists(cutu_reader*, dwarf2_per_objfile*, \
    sect_offset, bool, bool) gdb/dwarf2/read.c:17973 (gdb+0x85c522)
    #2 cooked_indexer::scan_attributes(dwarf2_per_cu_data*, cutu_reader*, \
    unsigned char const*, unsigned char const*, abbrev_info const*, \
    char const**, char const**, enum_flags<cooked_index_flag_enum>*, \
    sect_offset*, cooked_index_entry const**, unsigned long*, bool) \
    gdb/dwarf2/read.c:18148 (gdb+0x85d079)
    #3 cooked_indexer::index_dies(cutu_reader*, unsigned char const*, \
    cooked_index_entry const*, bool) gdb/dwarf2/read.c:18327 (gdb+0x85df65)
    #4 cooked_indexer::make_index(cutu_reader*) gdb/dwarf2/read.c:18450 \
    (gdb+0x85e72c)
    #5 process_psymtab_comp_unit gdb/dwarf2/read.c:6816 (gdb+0x8309c1)
    #6 operator() gdb/dwarf2/read.c:7102 (gdb+0x831902)
    #7 operator() gdbsupport/parallel-for.h:163 (gdb+0x872346)
...

Fix this by guarding the write with a lock.
---
 gdb/dwarf2/read.c | 31 ++++++++++++++++++++++++++-----
 1 file changed, 26 insertions(+), 5 deletions(-)

diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index 67702d43aff..a36f25f4e62 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -6766,6 +6766,10 @@ class cooked_indexer
   std::vector<deferred_entry> m_deferred_entries;
 };
 
+#if CXX_STD_THREAD
+static std::mutex per_cu_long_lock;
+#endif
+
 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
    Process compilation unit THIS_CU for a psymtab.  */
 
@@ -23567,6 +23571,8 @@ prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
 
   /* Set the language we're debugging.  */
   attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
+
+  enum language lang;
   if (cu->producer != nullptr
       && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
     {
@@ -23574,19 +23580,34 @@ prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
 	 attribute is not standardised yet.  As a workaround for the
 	 language detection we fall back to the DW_AT_producer
 	 string.  */
-      cu->per_cu->lang = language_opencl;
+      lang = language_opencl;
     }
   else if (cu->producer != nullptr
 	   && strstr (cu->producer, "GNU Go ") != NULL)
     {
       /* Similar hack for Go.  */
-      cu->per_cu->lang = language_go;
+      lang = language_go;
     }
   else if (attr != nullptr)
-    cu->per_cu->lang = dwarf_lang_to_enum_language (attr->constant_value (0));
+    lang = dwarf_lang_to_enum_language (attr->constant_value (0));
   else
-    cu->per_cu->lang = pretend_language;
-  cu->language_defn = language_def (cu->per_cu->lang);
+    lang = pretend_language;
+
+  {
+#if CXX_STD_THREAD
+    std::lock_guard<std::mutex> guard (per_cu_long_lock);
+#endif
+    /* Assign cu->per_cu->lang lazily.  Note: we're not doing here:
+         if (cu->per_cu->lang == language_unknown)
+           cu->per_cu->lang = lang;
+         else
+           gdb_assert (cu->per_cu->lang == lang);
+       because language may go from unknown to minimal to c.  */
+    if (cu->per_cu->lang != lang)
+      cu->per_cu->lang = lang;
+  }
+
+  cu->language_defn = language_def (lang);
 }
 
 /* See read.h.  */
-- 
2.35.3


  parent reply	other threads:[~2022-06-29 15:29 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-29 15:29 [PATCH 1/5] [COVER-LETTER, RFC] Fix some fsanitize=thread issues in gdb's cooked index Tom de Vries
2022-06-29 15:29 ` [PATCH 2/5] [gdb/symtab] Fix data race on per_cu->dwarf_version Tom de Vries
2022-07-01 11:16   ` Tom de Vries
2022-07-02 11:07     ` Tom de Vries
2022-07-04 18:51       ` Tom Tromey
2022-07-04 19:43         ` Tom de Vries
2022-07-04 19:53           ` Tom Tromey
2022-06-29 15:29 ` [PATCH 3/5] [gdb/symtab] Work around fsanitize=address false positive for per_cu->lang Tom de Vries
2022-06-29 17:38   ` Pedro Alves
2022-06-29 18:25     ` Pedro Alves
2022-06-29 18:28       ` Pedro Alves
2022-07-04  7:04         ` [PATCH 3/5] [gdb/symtab] Work around fsanitize=address false positive for per_ cu->lang Tom de Vries
2022-07-04 18:32   ` [PATCH 3/5] [gdb/symtab] Work around fsanitize=address false positive for per_cu->lang Tom Tromey
2022-07-04 19:45     ` Tom de Vries
2022-07-06 19:20       ` [PATCH] Introduce struct packed template, fix -fsanitize=thread for per_cu fields Pedro Alves
2022-07-07 10:18         ` Tom de Vries
2022-07-07 15:26           ` Pedro Alves
2022-07-08 14:54             ` Tom de Vries
2022-07-12 10:22               ` Tom de Vries
2022-06-29 15:29 ` [PATCH 4/5] [gdb/symtab] Work around fsanitize=address false positive for per_cu->unit_type Tom de Vries
2022-06-29 15:29 ` Tom de Vries [this message]
2022-07-04 18:30   ` [PATCH 5/5] [gdb/symtab] Fix data race on per_cu->lang Tom Tromey
2022-07-05  8:17     ` Tom de Vries
2022-07-05 15:19     ` Tom de Vries
2022-07-06 15:42       ` 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=20220629152914.13149-5-tdevries@suse.de \
    --to=tdevries@suse.de \
    --cc=gdb-patches@sourceware.org \
    --cc=tom@tromey.com \
    /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).