Skip to content

Commit

Permalink
[DependencyScanner] Use mutex to protect all accesses to contextCacheMap
Browse files Browse the repository at this point in the history
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
  • Loading branch information
cachemeifyoucan committed Jul 31, 2024
1 parent f89db53 commit 9967e58
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion include/swift/AST/ModuleDependencies.h
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,7 @@ class SwiftDependencyScanningService {
std::vector<std::string> AllContextHashes;

/// Shared state mutual-exclusivity lock
llvm::sys::SmartMutex<true> ScanningServiceGlobalLock;
mutable llvm::sys::SmartMutex<true> ScanningServiceGlobalLock;

/// Retrieve the dependencies map that corresponds to the given dependency
/// kind.
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/ModuleDependencies.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,7 @@ bool SwiftDependencyScanningService::setupCachingDependencyScanningService(

SwiftDependencyScanningService::ContextSpecificGlobalCacheState *
SwiftDependencyScanningService::getCacheForScanningContextHash(StringRef scanningContextHash) const {
llvm::sys::SmartScopedLock<true> Lock(ScanningServiceGlobalLock);
auto contextSpecificCache = ContextSpecificCacheMap.find(scanningContextHash);
assert(contextSpecificCache != ContextSpecificCacheMap.end() &&
"Global Module Dependencies Cache not configured with context-specific "
Expand All @@ -756,7 +757,6 @@ SwiftDependencyScanningService::getDependenciesMap(
ModuleNameToDependencyMap &
SwiftDependencyScanningService::getDependenciesMap(
ModuleDependencyKind kind, StringRef scanContextHash) {
llvm::sys::SmartScopedLock<true> Lock(ScanningServiceGlobalLock);
auto contextSpecificCache = getCacheForScanningContextHash(scanContextHash);
auto it = contextSpecificCache->ModuleDependenciesMap.find(kind);
assert(it != contextSpecificCache->ModuleDependenciesMap.end() &&
Expand Down

0 comments on commit 9967e58

Please sign in to comment.