Skip to content

Commit 7d31288

Browse files
committed
Change thread safety annotations in TLS code
When I tried to build Envoy with Clang-18 I hit an issue that Clang thread safety analizer does not like the fact that we are returning a reference to a protected member (central_cache_) from centralCacheLockHeld method. While I do think that the code is correct, when looking at the thread safety annotations I think we could do a little bit better. Currently, centralCacheLockHeld is annotated with ABLS_ASSERT_EXCLUSIVE_LOCK. My understanding is that this annotation should be used on functions that during runtime check that the right lock is held and fail if it's not the case. centralCacheLockHeld currently does not actually check that the lock is held - this seems somewhat misleading and I don't think that thread safety analysis should derive anything from this annotation TBH, as there is no runtime check present there. We could add a runtime check to the function, but unfortunately it will not be enough to address Clang's warning (see llvm/llvm-project#123512). Besides I think that we can do slightly better. This change replaces ABLS_ASSERT_EXCLUSIVE_LOCK with ABSL_EXCLUSIVE_LOCKS_REQUIRED, to let Clang thread safety analysis know during compilation time that this function should be called under a lock. That change triggered a few more warnings in various places that call into centralCacheLockHeld. In simple cases just adding ABSL_EXCLUSIVE_LOCKS_REQUIRED to the functions that call centralCacheLockHeld was enough. There were a couple of more complicated cases that Clang could not figure out because it does not support aliasing (i.e., when the same mutex is known under different names, Clang cannot always figure out that different names refer to the same lock). To deal with those cases I added assertHeld method with ABLS_ASSERT_EXCLUSIVE_LOCK annotation to the lock implementation and used it to help Clang to figure out what locks are held. All-in-all, I think relying on ABSL_EXCLUSIVE_LOCKS_REQUIRED is slightly better and it addresses the warning for me as well. Signed-off-by: Mikhail Krinkin <mkrinkin@microsoft.com>
1 parent 9b4e465 commit 7d31288

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

source/common/common/thread.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ class MutexBasicLockable : public BasicLockable {
2525
bool tryLock() ABSL_EXCLUSIVE_TRYLOCK_FUNCTION(true) override { return mutex_.TryLock(); }
2626
void unlock() ABSL_UNLOCK_FUNCTION() override { mutex_.Unlock(); }
2727

28+
void assertHeld() ABSL_ASSERT_EXCLUSIVE_LOCK(this) { mutex_.AssertHeld(); }
29+
2830
private:
2931
friend class CondVar;
3032
absl::Mutex mutex_;

source/common/stats/thread_local_store.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ ThreadLocalStoreImpl::~ThreadLocalStoreImpl() {
5454

5555
void ThreadLocalStoreImpl::setHistogramSettings(HistogramSettingsConstPtr&& histogram_settings) {
5656
iterateScopes([](const ScopeImplSharedPtr& scope) -> bool {
57+
scope->parent_.lock_.assertHeld();
5758
ASSERT(scope->centralCacheLockHeld()->histograms_.empty());
5859
return true;
5960
});
@@ -74,6 +75,7 @@ void ThreadLocalStoreImpl::setStatsMatcher(StatsMatcherPtr&& stats_matcher) {
7475
const uint32_t first_histogram_index = deleted_histograms_.size();
7576
iterateScopesLockHeld([this](const ScopeImplSharedPtr& scope) ABSL_EXCLUSIVE_LOCKS_REQUIRED(
7677
lock_) -> bool {
78+
scope->parent_.lock_.assertHeld();
7779
const CentralCacheEntrySharedPtr& central_cache = scope->centralCacheLockHeld();
7880
removeRejectedStats<CounterSharedPtr>(central_cache->counters_,
7981
[this](const CounterSharedPtr& counter) mutable {
@@ -293,6 +295,7 @@ void ThreadLocalStoreImpl::releaseScopeCrossThread(ScopeImpl* scope) {
293295
// VirtualHosts.
294296
bool need_post = scopes_to_cleanup_.empty();
295297
scopes_to_cleanup_.push_back(scope->scope_id_);
298+
scope->parent_.lock_.assertHeld();
296299
central_cache_entries_to_cleanup_.push_back(scope->centralCacheLockHeld());
297300
lock.release();
298301

source/common/stats/thread_local_store.h

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -339,16 +339,20 @@ class ThreadLocalStoreImpl : Logger::Loggable<Logger::Id::stats>, public StoreRo
339339
return iterateLockHeld(fn);
340340
}
341341

342-
bool iterateLockHeld(const IterateFn<Counter>& fn) const {
342+
bool iterateLockHeld(const IterateFn<Counter>& fn) const
343+
ABSL_EXCLUSIVE_LOCKS_REQUIRED(parent_.lock_) {
343344
return iterHelper(fn, centralCacheLockHeld()->counters_);
344345
}
345-
bool iterateLockHeld(const IterateFn<Gauge>& fn) const {
346+
bool iterateLockHeld(const IterateFn<Gauge>& fn) const
347+
ABSL_EXCLUSIVE_LOCKS_REQUIRED(parent_.lock_) {
346348
return iterHelper(fn, centralCacheLockHeld()->gauges_);
347349
}
348-
bool iterateLockHeld(const IterateFn<Histogram>& fn) const {
350+
bool iterateLockHeld(const IterateFn<Histogram>& fn) const
351+
ABSL_EXCLUSIVE_LOCKS_REQUIRED(parent_.lock_) {
349352
return iterHelper(fn, centralCacheLockHeld()->histograms_);
350353
}
351-
bool iterateLockHeld(const IterateFn<TextReadout>& fn) const {
354+
bool iterateLockHeld(const IterateFn<TextReadout>& fn) const
355+
ABSL_EXCLUSIVE_LOCKS_REQUIRED(parent_.lock_) {
352356
return iterHelper(fn, centralCacheLockHeld()->text_readouts_);
353357
}
354358
ThreadLocalStoreImpl& store() override { return parent_; }
@@ -421,7 +425,7 @@ class ThreadLocalStoreImpl : Logger::Loggable<Logger::Id::stats>, public StoreRo
421425
// scope->central_cache_, the analysis system cannot understand that the
422426
// scope's parent_.lock_ is held, so we assert that here.
423427
const CentralCacheEntrySharedPtr& centralCacheLockHeld() const
424-
ABSL_ASSERT_EXCLUSIVE_LOCK(parent_.lock_) {
428+
ABSL_EXCLUSIVE_LOCKS_REQUIRED(parent_.lock_) {
425429
return central_cache_;
426430
}
427431

@@ -481,8 +485,10 @@ class ThreadLocalStoreImpl : Logger::Loggable<Logger::Id::stats>, public StoreRo
481485

482486
// The Store versions of iterate cover all the scopes in the store.
483487
template <class StatFn> bool iterHelper(StatFn fn) const {
484-
return iterateScopes(
485-
[fn](const ScopeImplSharedPtr& scope) -> bool { return scope->iterateLockHeld(fn); });
488+
return iterateScopes([fn](const ScopeImplSharedPtr& scope) -> bool {
489+
scope->parent_.lock_.assertHeld();
490+
return scope->iterateLockHeld(fn);
491+
});
486492
}
487493

488494
std::string getTagsForName(const std::string& name, TagVector& tags) const;

0 commit comments

Comments
 (0)