From b35bd7d552f80518ad90e81f592b73ee2ef736d7 Mon Sep 17 00:00:00 2001 From: Tom de Vries Date: Thu, 14 Jul 2022 08:19:00 +0200 Subject: [gdb/symtab] Make per_cu->unit_type atomic With gdb with -fsanitize=thread and test-case gdb.ada/array_bounds.exp, I run into a data race between: ... Read of size 1 at 0x7b2000025f0f by main thread: #0 packed::operator dwarf_unit_type() const packed.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 ... Fix this by making per_cu->unit_type atomic. Tested on x86_64-linux. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=29286 --- 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 fcf0cd7..acbe298 100644 --- a/gdb/dwarf2/read.h +++ b/gdb/dwarf2/read.h @@ -178,7 +178,7 @@ public: private: /* The unit type of this CU. */ - packed m_unit_type = (dwarf_unit_type) 0; + std::atomic> m_unit_type {(dwarf_unit_type)0}; /* The language of this CU. */ packed m_lang = language_unknown; @@ -329,19 +329,24 @@ public: dwarf_unit_type unit_type (bool strict_p = true) const { + dwarf_unit_type ut = m_unit_type.load (); if (strict_p) - gdb_assert (m_unit_type != 0); - return m_unit_type; + gdb_assert (ut != 0); + return ut; } void set_unit_type (dwarf_unit_type unit_type) { - if (m_unit_type == 0) - /* Set if not set already. */ - m_unit_type = unit_type; - else - /* If already set, verify that it's the same value. */ - gdb_assert (m_unit_type == unit_type); + /* Set if not set already. */ + packed nope = (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 = unit_type; + if (m_unit_type.compare_exchange_strong (nope, unit_type)) + return; + gdb_assert_not_reached (); } enum language lang (bool strict_p = true) const -- cgit v1.1