diff --git a/llvm/include/llvm/DebugInfo/DWARF/DWARFContext.h b/llvm/include/llvm/DebugInfo/DWARF/DWARFContext.h --- a/llvm/include/llvm/DebugInfo/DWARF/DWARFContext.h +++ b/llvm/include/llvm/DebugInfo/DWARF/DWARFContext.h @@ -25,6 +25,7 @@ #include "llvm/TargetParser/Host.h" #include #include +#include namespace llvm { @@ -46,6 +47,8 @@ /// information parsing. The actual data is supplied through DWARFObj. class DWARFContext : public DIContext { DWARFUnitVector NormalUnits; + // Mutex protecting multi-threaded access to the DWARFContext. + std::recursive_mutex Mutex; std::optional> NormalTypeUnits; std::unique_ptr CUIndex; std::unique_ptr GdbIndex; diff --git a/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp b/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp --- a/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp +++ b/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp @@ -898,6 +898,7 @@ } const DWARFUnitIndex &DWARFContext::getCUIndex() { + std::unique_lock LockGuard(Mutex); if (CUIndex) return *CUIndex; @@ -910,6 +911,7 @@ } const DWARFUnitIndex &DWARFContext::getTUIndex() { + std::unique_lock LockGuard(Mutex); if (TUIndex) return *TUIndex; @@ -924,6 +926,7 @@ } DWARFGdbIndex &DWARFContext::getGdbIndex() { + std::unique_lock LockGuard(Mutex); if (GdbIndex) return *GdbIndex; @@ -934,6 +937,7 @@ } const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() { + std::unique_lock LockGuard(Mutex); if (Abbrev) return Abbrev.get(); @@ -943,6 +947,7 @@ } const DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() { + std::unique_lock LockGuard(Mutex); if (AbbrevDWO) return AbbrevDWO.get(); @@ -952,6 +957,7 @@ } const DWARFDebugLoc *DWARFContext::getDebugLoc() { + std::unique_lock LockGuard(Mutex); if (Loc) return Loc.get(); @@ -966,6 +972,7 @@ } const DWARFDebugAranges *DWARFContext::getDebugAranges() { + std::unique_lock LockGuard(Mutex); if (Aranges) return Aranges.get(); @@ -975,6 +982,7 @@ } Expected DWARFContext::getDebugFrame() { + std::unique_lock LockGuard(Mutex); if (DebugFrame) return DebugFrame.get(); @@ -1001,6 +1009,7 @@ } Expected DWARFContext::getEHFrame() { + std::unique_lock LockGuard(Mutex); if (EHFrame) return EHFrame.get(); @@ -1017,24 +1026,28 @@ } const DWARFDebugMacro *DWARFContext::getDebugMacro() { + std::unique_lock LockGuard(Mutex); if (!Macro) Macro = parseMacroOrMacinfo(MacroSection); return Macro.get(); } const DWARFDebugMacro *DWARFContext::getDebugMacroDWO() { + std::unique_lock LockGuard(Mutex); if (!MacroDWO) MacroDWO = parseMacroOrMacinfo(MacroDwoSection); return MacroDWO.get(); } const DWARFDebugMacro *DWARFContext::getDebugMacinfo() { + std::unique_lock LockGuard(Mutex); if (!Macinfo) Macinfo = parseMacroOrMacinfo(MacinfoSection); return Macinfo.get(); } const DWARFDebugMacro *DWARFContext::getDebugMacinfoDWO() { + std::unique_lock LockGuard(Mutex); if (!MacinfoDWO) MacinfoDWO = parseMacroOrMacinfo(MacinfoDwoSection); return MacinfoDWO.get(); @@ -1093,6 +1106,7 @@ Expected DWARFContext::getLineTableForUnit( DWARFUnit *U, function_ref RecoverableErrorHandler) { + std::unique_lock LockGuard(Mutex); if (!Line) Line.reset(new DWARFDebugLine); @@ -1121,6 +1135,7 @@ } void DWARFContext::clearLineTableForUnit(DWARFUnit *U) { + std::unique_lock LockGuard(Mutex); if (!Line) return; @@ -1137,6 +1152,7 @@ } void DWARFContext::parseNormalUnits() { + std::unique_lock LockGuard(Mutex); if (!NormalUnits.empty()) return; DObj->forEachInfoSections([&](const DWARFSection &S) { @@ -1149,6 +1165,7 @@ } void DWARFContext::parseDWOUnits(bool Lazy) { + std::unique_lock LockGuard(Mutex); if (!DWOUnits.empty()) return; DObj->forEachInfoDWOSections([&](const DWARFSection &S) { @@ -1519,6 +1536,7 @@ std::shared_ptr DWARFContext::getDWOContext(StringRef AbsolutePath) { + std::unique_lock LockGuard(Mutex); if (auto S = DWP.lock()) { DWARFContext *Ctxt = S->Context.get(); return std::shared_ptr(std::move(S), Ctxt);