From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2205) id 46F263857C42; Thu, 14 Jul 2022 06:19:09 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 46F263857C42 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Tom de Vries To: gdb-cvs@sourceware.org Subject: [binutils-gdb] [gdb/symtab] Make per_cu->unit_type atomic X-Act-Checkin: binutils-gdb X-Git-Author: Tom de Vries X-Git-Refname: refs/heads/master X-Git-Oldrev: 4f92e10cda142fd1f213e01a53ca687e38cddf22 X-Git-Newrev: b35bd7d552f80518ad90e81f592b73ee2ef736d7 Message-Id: <20220714061909.46F263857C42@sourceware.org> Date: Thu, 14 Jul 2022 06:19:09 +0000 (GMT) X-BeenThere: gdb-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 14 Jul 2022 06:19:09 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3Db35bd7d552f8= 0518ad90e81f592b73ee2ef736d7 commit b35bd7d552f80518ad90e81f592b73ee2ef736d7 Author: Tom de Vries Date: Thu Jul 14 08:19:00 2022 +0200 [gdb/symtab] Make per_cu->unit_type atomic =20 With gdb with -fsanitize=3Dthread and test-case gdb.ada/array_bounds.ex= p, I run into a data race between: ... Read of size 1 at 0x7b2000025f0f by main thread: #0 packed::operator dwarf_unit_type() const p= acked.h:54 #1 dwarf2_per_cu_data::set_unit_type(dwarf_unit_type) read.h:339 ... and: ... Previous write of size 1 at 0x7b2000025f0f by thread T3: #0 dwarf2_per_cu_data::set_unit_type(dwarf_unit_type) read.h:341 ... =20 Fix this by making per_cu->unit_type atomic. =20 Tested on x86_64-linux. =20 Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=3D29286 Diff: --- gdb/dwarf2/read.h | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/gdb/dwarf2/read.h b/gdb/dwarf2/read.h index fcf0cd70530..acbe298b310 100644 --- a/gdb/dwarf2/read.h +++ b/gdb/dwarf2/read.h @@ -178,7 +178,7 @@ public: =20 private: /* The unit type of this CU. */ - packed m_unit_type =3D (dwarf_unit_type) 0; + std::atomic> m_unit_type {(dwarf_unit_type)0}; =20 /* The language of this CU. */ packed m_lang =3D language_unknown; @@ -329,19 +329,24 @@ public: =20 dwarf_unit_type unit_type (bool strict_p =3D true) const { + dwarf_unit_type ut =3D m_unit_type.load (); if (strict_p) - gdb_assert (m_unit_type !=3D 0); - return m_unit_type; + gdb_assert (ut !=3D 0); + return ut; } =20 void set_unit_type (dwarf_unit_type unit_type) { - if (m_unit_type =3D=3D 0) - /* Set if not set already. */ - m_unit_type =3D unit_type; - else - /* If already set, verify that it's the same value. */ - gdb_assert (m_unit_type =3D=3D unit_type); + /* Set if not set already. */ + packed nope =3D (dwarf_unit_type)0; + if (m_unit_type.compare_exchange_strong (nope, unit_type)) + return; + + /* If already set, verify that it's the same value. */ + nope =3D unit_type; + if (m_unit_type.compare_exchange_strong (nope, unit_type)) + return; + gdb_assert_not_reached (); } =20 enum language lang (bool strict_p =3D true) const