From 9967e5862488c5e3a962d84a3a46c8bdae2d0d56 Mon Sep 17 00:00:00 2001 From: Steven Wu Date: Wed, 31 Jul 2024 13:13:52 -0700 Subject: [PATCH] [DependencyScanner] Use mutex to protect all accesses to contextCacheMap Rather than only protecting the insertion and non-const access to `ContextSpecificCacheMap` in ScanningService, extend the mutex protection to all accesses. Even a 'const' lookup in the cache map is not thread safe because the `StringMap` could be in the process of being rehashed. rdar://127205953 --- include/swift/AST/ModuleDependencies.h | 2 +- lib/AST/ModuleDependencies.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/swift/AST/ModuleDependencies.h b/include/swift/AST/ModuleDependencies.h index 74b85fc02d183..6ad80e69ccffa 100644 --- a/include/swift/AST/ModuleDependencies.h +++ b/include/swift/AST/ModuleDependencies.h @@ -998,7 +998,7 @@ class SwiftDependencyScanningService { std::vector AllContextHashes; /// Shared state mutual-exclusivity lock - llvm::sys::SmartMutex ScanningServiceGlobalLock; + mutable llvm::sys::SmartMutex ScanningServiceGlobalLock; /// Retrieve the dependencies map that corresponds to the given dependency /// kind. diff --git a/lib/AST/ModuleDependencies.cpp b/lib/AST/ModuleDependencies.cpp index ef9f75ec46c7a..097e50840f388 100644 --- a/lib/AST/ModuleDependencies.cpp +++ b/lib/AST/ModuleDependencies.cpp @@ -736,6 +736,7 @@ bool SwiftDependencyScanningService::setupCachingDependencyScanningService( SwiftDependencyScanningService::ContextSpecificGlobalCacheState * SwiftDependencyScanningService::getCacheForScanningContextHash(StringRef scanningContextHash) const { + llvm::sys::SmartScopedLock Lock(ScanningServiceGlobalLock); auto contextSpecificCache = ContextSpecificCacheMap.find(scanningContextHash); assert(contextSpecificCache != ContextSpecificCacheMap.end() && "Global Module Dependencies Cache not configured with context-specific " @@ -756,7 +757,6 @@ SwiftDependencyScanningService::getDependenciesMap( ModuleNameToDependencyMap & SwiftDependencyScanningService::getDependenciesMap( ModuleDependencyKind kind, StringRef scanContextHash) { - llvm::sys::SmartScopedLock Lock(ScanningServiceGlobalLock); auto contextSpecificCache = getCacheForScanningContextHash(scanContextHash); auto it = contextSpecificCache->ModuleDependenciesMap.find(kind); assert(it != contextSpecificCache->ModuleDependenciesMap.end() &&