Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[lldb][TypeSystem] ForEach: Don't hold the TypeSystemMap lock across …
…callback The `TypeSystemMap::m_mutex` guards against concurrent modifications of members of `TypeSystemMap`. In particular, `m_map`. `TypeSystemMap::ForEach` iterates through the entire `m_map` calling a user-specified callback for each entry. This is all done while `m_mutex` is locked. However, there's nothing that guarantees that the callback itself won't call back into `TypeSystemMap` APIs on the same thread. This lead to double-locking `m_mutex`, which is undefined behaviour. We've seen this cause a deadlock in the swift plugin with following backtrace: ``` int main() { std::unique_ptr<int> up = std::make_unique<int>(5); volatile int val = *up; return val; } clang++ -std=c++2a -g -O1 main.cpp ./bin/lldb -o “br se -p return” -o run -o “v *up” -o “expr *up” -b ``` ``` frame #4: std::lock_guard<std::mutex>::lock_guard frame #5: lldb_private::TypeSystemMap::GetTypeSystemForLanguage <<<< Lock #2 frame #6: lldb_private::TypeSystemMap::GetTypeSystemForLanguage frame #7: lldb_private::Target::GetScratchTypeSystemForLanguage ... frame intel#26: lldb_private::SwiftASTContext::LoadLibraryUsingPaths frame intel#27: lldb_private::SwiftASTContext::LoadModule frame intel#30: swift::ModuleDecl::collectLinkLibraries frame intel#31: lldb_private::SwiftASTContext::LoadModule frame intel#34: lldb_private::SwiftASTContext::GetCompileUnitImportsImpl frame intel#35: lldb_private::SwiftASTContext::PerformCompileUnitImports frame intel#36: lldb_private::TypeSystemSwiftTypeRefForExpressions::GetSwiftASTContext frame intel#37: lldb_private::TypeSystemSwiftTypeRefForExpressions::GetPersistentExpressionState frame intel#38: lldb_private::Target::GetPersistentSymbol frame intel#41: lldb_private::TypeSystemMap::ForEach <<<< Lock #1 frame intel#42: lldb_private::Target::GetPersistentSymbol frame intel#43: lldb_private::IRExecutionUnit::FindInUserDefinedSymbols frame intel#44: lldb_private::IRExecutionUnit::FindSymbol frame intel#45: lldb_private::IRExecutionUnit::MemoryManager::GetSymbolAddressAndPresence frame intel#46: lldb_private::IRExecutionUnit::MemoryManager::findSymbol frame intel#47: non-virtual thunk to lldb_private::IRExecutionUnit::MemoryManager::findSymbol frame intel#48: llvm::LinkingSymbolResolver::findSymbol frame intel#49: llvm::LegacyJITSymbolResolver::lookup frame intel#50: llvm::RuntimeDyldImpl::resolveExternalSymbols frame intel#51: llvm::RuntimeDyldImpl::resolveRelocations frame intel#52: llvm::MCJIT::finalizeLoadedModules frame intel#53: llvm::MCJIT::finalizeObject frame intel#54: lldb_private::IRExecutionUnit::ReportAllocations frame intel#55: lldb_private::IRExecutionUnit::GetRunnableInfo frame intel#56: lldb_private::ClangExpressionParser::PrepareForExecution frame intel#57: lldb_private::ClangUserExpression::TryParse frame intel#58: lldb_private::ClangUserExpression::Parse ``` Our solution is to simply iterate over a local copy of `m_map`. **Testing** * Confirmed on manual reproducer (would reproduce 100% of the time before the patch) Differential Revision: https://reviews.llvm.org/D149949
- Loading branch information