Skip to content

Commit

Permalink
Change thread safety annotations in TLS code
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
krinkinmu committed Jan 21, 2025
1 parent 9b4e465 commit 985574c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
2 changes: 2 additions & 0 deletions source/common/common/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class MutexBasicLockable : public BasicLockable {
bool tryLock() ABSL_EXCLUSIVE_TRYLOCK_FUNCTION(true) override { return mutex_.TryLock(); }
void unlock() ABSL_UNLOCK_FUNCTION() override { mutex_.Unlock(); }

void assertHeld() ABSL_ASSERT_EXCLUSIVE_LOCK(this) { mutex_.AssertHeld(); }

private:
friend class CondVar;
absl::Mutex mutex_;
Expand Down
3 changes: 3 additions & 0 deletions source/common/stats/thread_local_store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ ThreadLocalStoreImpl::~ThreadLocalStoreImpl() {

void ThreadLocalStoreImpl::setHistogramSettings(HistogramSettingsConstPtr&& histogram_settings) {
iterateScopes([](const ScopeImplSharedPtr& scope) -> bool {
scope->parent_.lock_.assertHeld();
ASSERT(scope->centralCacheLockHeld()->histograms_.empty());
return true;
});
Expand All @@ -74,6 +75,7 @@ void ThreadLocalStoreImpl::setStatsMatcher(StatsMatcherPtr&& stats_matcher) {
const uint32_t first_histogram_index = deleted_histograms_.size();
iterateScopesLockHeld([this](const ScopeImplSharedPtr& scope) ABSL_EXCLUSIVE_LOCKS_REQUIRED(
lock_) -> bool {
scope->parent_.lock_.assertHeld();
const CentralCacheEntrySharedPtr& central_cache = scope->centralCacheLockHeld();
removeRejectedStats<CounterSharedPtr>(central_cache->counters_,
[this](const CounterSharedPtr& counter) mutable {
Expand Down Expand Up @@ -293,6 +295,7 @@ void ThreadLocalStoreImpl::releaseScopeCrossThread(ScopeImpl* scope) {
// VirtualHosts.
bool need_post = scopes_to_cleanup_.empty();
scopes_to_cleanup_.push_back(scope->scope_id_);
scope->parent_.lock_.assertHeld();
central_cache_entries_to_cleanup_.push_back(scope->centralCacheLockHeld());
lock.release();

Expand Down
15 changes: 9 additions & 6 deletions source/common/stats/thread_local_store.h
Original file line number Diff line number Diff line change
Expand Up @@ -339,16 +339,16 @@ class ThreadLocalStoreImpl : Logger::Loggable<Logger::Id::stats>, public StoreRo
return iterateLockHeld(fn);
}

bool iterateLockHeld(const IterateFn<Counter>& fn) const {
bool iterateLockHeld(const IterateFn<Counter>& fn) const ABSL_EXCLUSIVE_LOCKS_REQUIRED(parent_.lock_) {
return iterHelper(fn, centralCacheLockHeld()->counters_);
}
bool iterateLockHeld(const IterateFn<Gauge>& fn) const {
bool iterateLockHeld(const IterateFn<Gauge>& fn) const ABSL_EXCLUSIVE_LOCKS_REQUIRED(parent_.lock_) {
return iterHelper(fn, centralCacheLockHeld()->gauges_);
}
bool iterateLockHeld(const IterateFn<Histogram>& fn) const {
bool iterateLockHeld(const IterateFn<Histogram>& fn) const ABSL_EXCLUSIVE_LOCKS_REQUIRED(parent_.lock_) {
return iterHelper(fn, centralCacheLockHeld()->histograms_);
}
bool iterateLockHeld(const IterateFn<TextReadout>& fn) const {
bool iterateLockHeld(const IterateFn<TextReadout>& fn) const ABSL_EXCLUSIVE_LOCKS_REQUIRED(parent_.lock_) {
return iterHelper(fn, centralCacheLockHeld()->text_readouts_);
}
ThreadLocalStoreImpl& store() override { return parent_; }
Expand Down Expand Up @@ -421,7 +421,7 @@ class ThreadLocalStoreImpl : Logger::Loggable<Logger::Id::stats>, public StoreRo
// scope->central_cache_, the analysis system cannot understand that the
// scope's parent_.lock_ is held, so we assert that here.
const CentralCacheEntrySharedPtr& centralCacheLockHeld() const
ABSL_ASSERT_EXCLUSIVE_LOCK(parent_.lock_) {
ABSL_EXCLUSIVE_LOCKS_REQUIRED(parent_.lock_) {
return central_cache_;
}

Expand Down Expand Up @@ -482,7 +482,10 @@ class ThreadLocalStoreImpl : Logger::Loggable<Logger::Id::stats>, public StoreRo
// The Store versions of iterate cover all the scopes in the store.
template <class StatFn> bool iterHelper(StatFn fn) const {
return iterateScopes(
[fn](const ScopeImplSharedPtr& scope) -> bool { return scope->iterateLockHeld(fn); });
[fn](const ScopeImplSharedPtr& scope) -> bool {
scope->parent_.lock_.assertHeld();
return scope->iterateLockHeld(fn);
});
}

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

0 comments on commit 985574c

Please sign in to comment.