Author: Augusto Noronha Date: 2023-08-04T15:33:46-07:00 New Revision: 318f600722e3cc6745317bb68309f82656c97b27
URL: https://github.com/llvm/llvm-project/commit/318f600722e3cc6745317bb68309f82656c97b27 DIFF: https://github.com/llvm/llvm-project/commit/318f600722e3cc6745317bb68309f82656c97b27.diff LOG: [lldb] Protect OptionValue accesses from data races Thread sanitizer is catching data races in OptionValue, protect accesses to OptionValue with a mutex. Differential Revision: https://reviews.llvm.org/D157041 Added: Modified: lldb/include/lldb/Interpreter/OptionValue.h lldb/source/Interpreter/OptionValue.cpp Removed: ################################################################################ diff --git a/lldb/include/lldb/Interpreter/OptionValue.h b/lldb/include/lldb/Interpreter/OptionValue.h index 4fa0b23042669d..bfbdbead72c66d 100644 --- a/lldb/include/lldb/Interpreter/OptionValue.h +++ b/lldb/include/lldb/Interpreter/OptionValue.h @@ -23,6 +23,7 @@ #include "lldb/lldb-private-enumerations.h" #include "lldb/lldb-private-interfaces.h" #include "llvm/Support/JSON.h" +#include <mutex> namespace lldb_private { @@ -70,6 +71,10 @@ class OptionValue { virtual ~OptionValue() = default; + OptionValue(const OptionValue &other); + + OptionValue& operator=(const OptionValue &other); + // Subclasses should override these functions virtual Type GetType() const = 0; @@ -382,6 +387,8 @@ class OptionValue { const FormatEntity::Entry *GetFormatEntity() const; const RegularExpression *GetRegexValue() const; + + mutable std::mutex m_mutex; }; } // namespace lldb_private diff --git a/lldb/source/Interpreter/OptionValue.cpp b/lldb/source/Interpreter/OptionValue.cpp index 8d429b6bc9cf21..5ff7e3c3208980 100644 --- a/lldb/source/Interpreter/OptionValue.cpp +++ b/lldb/source/Interpreter/OptionValue.cpp @@ -15,6 +15,25 @@ using namespace lldb; using namespace lldb_private; +OptionValue::OptionValue(const OptionValue &other) { + std::lock_guard<std::mutex> lock(other.m_mutex); + + m_parent_wp = other.m_parent_wp; + m_callback = other.m_callback; + m_value_was_set = other.m_value_was_set; + +} + +OptionValue& OptionValue::operator=(const OptionValue &other) { + std::scoped_lock lock(m_mutex, other.m_mutex); + + m_parent_wp = other.m_parent_wp; + m_callback = other.m_callback; + m_value_was_set = other.m_value_was_set; + + return *this; +} + Status OptionValue::SetSubValue(const ExecutionContext *exe_ctx, VarSetOperationType op, llvm::StringRef name, llvm::StringRef value) { @@ -252,12 +271,14 @@ const OptionValueUUID *OptionValue::GetAsUUID() const { } std::optional<bool> OptionValue::GetBooleanValue() const { + std::lock_guard<std::mutex> lock(m_mutex); if (const OptionValueBoolean *option_value = GetAsBoolean()) return option_value->GetCurrentValue(); return {}; } bool OptionValue::SetBooleanValue(bool new_value) { + std::lock_guard<std::mutex> lock(m_mutex); if (OptionValueBoolean *option_value = GetAsBoolean()) { option_value->SetCurrentValue(new_value); return true; @@ -266,12 +287,14 @@ bool OptionValue::SetBooleanValue(bool new_value) { } std::optional<char> OptionValue::GetCharValue() const { + std::lock_guard<std::mutex> lock(m_mutex); if (const OptionValueChar *option_value = GetAsChar()) return option_value->GetCurrentValue(); return {}; } bool OptionValue::SetCharValue(char new_value) { + std::lock_guard<std::mutex> lock(m_mutex); if (OptionValueChar *option_value = GetAsChar()) { option_value->SetCurrentValue(new_value); return true; @@ -280,12 +303,14 @@ bool OptionValue::SetCharValue(char new_value) { } std::optional<int64_t> OptionValue::GetEnumerationValue() const { + std::lock_guard<std::mutex> lock(m_mutex); if (const OptionValueEnumeration *option_value = GetAsEnumeration()) return option_value->GetCurrentValue(); return {}; } bool OptionValue::SetEnumerationValue(int64_t value) { + std::lock_guard<std::mutex> lock(m_mutex); if (OptionValueEnumeration *option_value = GetAsEnumeration()) { option_value->SetCurrentValue(value); return true; @@ -294,12 +319,14 @@ bool OptionValue::SetEnumerationValue(int64_t value) { } std::optional<FileSpec> OptionValue::GetFileSpecValue() const { + std::lock_guard<std::mutex> lock(m_mutex); if (const OptionValueFileSpec *option_value = GetAsFileSpec()) return option_value->GetCurrentValue(); return {}; } bool OptionValue::SetFileSpecValue(FileSpec file_spec) { + std::lock_guard<std::mutex> lock(m_mutex); if (OptionValueFileSpec *option_value = GetAsFileSpec()) { option_value->SetCurrentValue(file_spec, false); return true; @@ -308,6 +335,7 @@ bool OptionValue::SetFileSpecValue(FileSpec file_spec) { } bool OptionValue::AppendFileSpecValue(FileSpec file_spec) { + std::lock_guard<std::mutex> lock(m_mutex); if (OptionValueFileSpecList *option_value = GetAsFileSpecList()) { option_value->AppendCurrentValue(file_spec); return true; @@ -316,18 +344,21 @@ bool OptionValue::AppendFileSpecValue(FileSpec file_spec) { } std::optional<FileSpecList> OptionValue::GetFileSpecListValue() const { + std::lock_guard<std::mutex> lock(m_mutex); if (const OptionValueFileSpecList *option_value = GetAsFileSpecList()) return option_value->GetCurrentValue(); return {}; } std::optional<lldb::Format> OptionValue::GetFormatValue() const { + std::lock_guard<std::mutex> lock(m_mutex); if (const OptionValueFormat *option_value = GetAsFormat()) return option_value->GetCurrentValue(); return {}; } bool OptionValue::SetFormatValue(lldb::Format new_value) { + std::lock_guard<std::mutex> lock(m_mutex); if (OptionValueFormat *option_value = GetAsFormat()) { option_value->SetCurrentValue(new_value); return true; @@ -336,12 +367,14 @@ bool OptionValue::SetFormatValue(lldb::Format new_value) { } std::optional<lldb::LanguageType> OptionValue::GetLanguageValue() const { + std::lock_guard<std::mutex> lock(m_mutex); if (const OptionValueLanguage *option_value = GetAsLanguage()) return option_value->GetCurrentValue(); return {}; } bool OptionValue::SetLanguageValue(lldb::LanguageType new_language) { + std::lock_guard<std::mutex> lock(m_mutex); if (OptionValueLanguage *option_value = GetAsLanguage()) { option_value->SetCurrentValue(new_language); return true; @@ -350,24 +383,28 @@ bool OptionValue::SetLanguageValue(lldb::LanguageType new_language) { } const FormatEntity::Entry *OptionValue::GetFormatEntity() const { + std::lock_guard<std::mutex> lock(m_mutex); if (const OptionValueFormatEntity *option_value = GetAsFormatEntity()) return &option_value->GetCurrentValue(); return nullptr; } const RegularExpression *OptionValue::GetRegexValue() const { + std::lock_guard<std::mutex> lock(m_mutex); if (const OptionValueRegex *option_value = GetAsRegex()) return option_value->GetCurrentValue(); return nullptr; } std::optional<int64_t> OptionValue::GetSInt64Value() const { + std::lock_guard<std::mutex> lock(m_mutex); if (const OptionValueSInt64 *option_value = GetAsSInt64()) return option_value->GetCurrentValue(); return {}; } bool OptionValue::SetSInt64Value(int64_t new_value) { + std::lock_guard<std::mutex> lock(m_mutex); if (OptionValueSInt64 *option_value = GetAsSInt64()) { option_value->SetCurrentValue(new_value); return true; @@ -376,12 +413,14 @@ bool OptionValue::SetSInt64Value(int64_t new_value) { } std::optional<llvm::StringRef> OptionValue::GetStringValue() const { + std::lock_guard<std::mutex> lock(m_mutex); if (const OptionValueString *option_value = GetAsString()) return option_value->GetCurrentValueAsRef(); return {}; } bool OptionValue::SetStringValue(llvm::StringRef new_value) { + std::lock_guard<std::mutex> lock(m_mutex); if (OptionValueString *option_value = GetAsString()) { option_value->SetCurrentValue(new_value); return true; @@ -390,12 +429,14 @@ bool OptionValue::SetStringValue(llvm::StringRef new_value) { } std::optional<uint64_t> OptionValue::GetUInt64Value() const { + std::lock_guard<std::mutex> lock(m_mutex); if (const OptionValueUInt64 *option_value = GetAsUInt64()) return option_value->GetCurrentValue(); return {}; } bool OptionValue::SetUInt64Value(uint64_t new_value) { + std::lock_guard<std::mutex> lock(m_mutex); if (OptionValueUInt64 *option_value = GetAsUInt64()) { option_value->SetCurrentValue(new_value); return true; @@ -404,12 +445,14 @@ bool OptionValue::SetUInt64Value(uint64_t new_value) { } std::optional<UUID> OptionValue::GetUUIDValue() const { + std::lock_guard<std::mutex> lock(m_mutex); if (const OptionValueUUID *option_value = GetAsUUID()) return option_value->GetCurrentValue(); return {}; } bool OptionValue::SetUUIDValue(const UUID &uuid) { + std::lock_guard<std::mutex> lock(m_mutex); if (OptionValueUUID *option_value = GetAsUUID()) { option_value->SetCurrentValue(uuid); return true; @@ -418,12 +461,14 @@ bool OptionValue::SetUUIDValue(const UUID &uuid) { } std::optional<ArchSpec> OptionValue::GetArchSpecValue() const { + std::lock_guard<std::mutex> lock(m_mutex); if (const OptionValueArch *option_value = GetAsArch()) return option_value->GetCurrentValue(); return {}; } bool OptionValue::SetArchSpecValue(ArchSpec arch_spec) { + std::lock_guard<std::mutex> lock(m_mutex); if (OptionValueArch *option_value = GetAsArch()) { option_value->SetCurrentValue(arch_spec, false); return true; _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits