Author: spyffe Date: Tue Jul 25 10:33:37 2017 New Revision: 308993 URL: http://llvm.org/viewvc/llvm-project?rev=308993&view=rev Log: [TypeSystem] Guard the global `ASTSourceMap` with a mutex
s_source_map in ClangExternalASTSourceCommon.cpp is unguarded and therefore can break in multithreaded conditions. This can cause crashes in particular if multiple targets are being set up at once. This patch wraps s_source_map in a function that ensures exclusivity, and makes every user of it use that function instead. <rdar://problem/33429774> lldb crashes after "resume_off" Differential Revision: https://reviews.llvm.org/D35083 Modified: lldb/trunk/source/Symbol/ClangExternalASTSourceCommon.cpp Modified: lldb/trunk/source/Symbol/ClangExternalASTSourceCommon.cpp URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Symbol/ClangExternalASTSourceCommon.cpp?rev=308993&r1=308992&r2=308993&view=diff ============================================================================== --- lldb/trunk/source/Symbol/ClangExternalASTSourceCommon.cpp (original) +++ lldb/trunk/source/Symbol/ClangExternalASTSourceCommon.cpp Tue Jul 25 10:33:37 2017 @@ -10,6 +10,8 @@ #include "lldb/Symbol/ClangExternalASTSourceCommon.h" #include "lldb/Utility/Stream.h" +#include <mutex> + using namespace lldb_private; uint64_t g_TotalSizeOfMetadata = 0; @@ -18,15 +20,19 @@ typedef llvm::DenseMap<clang::ExternalAS ClangExternalASTSourceCommon *> ASTSourceMap; -static ASTSourceMap &GetSourceMap() { +static ASTSourceMap &GetSourceMap(std::unique_lock<std::mutex> &guard) { // Intentionally leaked to avoid problems with global destructors. static ASTSourceMap *s_source_map = new ASTSourceMap; + static std::mutex s_mutex; + std::unique_lock<std::mutex> locked_guard(s_mutex); + guard.swap(locked_guard); return *s_source_map; } ClangExternalASTSourceCommon * ClangExternalASTSourceCommon::Lookup(clang::ExternalASTSource *source) { - ASTSourceMap &source_map = GetSourceMap(); + std::unique_lock<std::mutex> guard; + ASTSourceMap &source_map = GetSourceMap(guard); ASTSourceMap::iterator iter = source_map.find(source); @@ -40,11 +46,13 @@ ClangExternalASTSourceCommon::Lookup(cla ClangExternalASTSourceCommon::ClangExternalASTSourceCommon() : clang::ExternalASTSource() { g_TotalSizeOfMetadata += m_metadata.size(); - GetSourceMap()[this] = this; + std::unique_lock<std::mutex> guard; + GetSourceMap(guard)[this] = this; } ClangExternalASTSourceCommon::~ClangExternalASTSourceCommon() { - GetSourceMap().erase(this); + std::unique_lock<std::mutex> guard; + GetSourceMap(guard).erase(this); g_TotalSizeOfMetadata -= m_metadata.size(); } _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits