Skip to content

Fix clang thread-safety-pointer warnings #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 29, 2025

Conversation

theuni
Copy link

@theuni theuni commented May 28, 2025

This prevents a warning in an upcoming version of clang which will turn this on for us.

Submitted upstream: google#1270

Clang's -Wthread-safety-pointer warnings will become part of -Wthread-safety in some future release. Add missing annotations for functions that pass the address of guarded members. No functional change. Fixes the following warnings:

util/cache.cc:220:17: warning: passing pointer to variable 'in_use_' requires holding mutex 'mutex_' [-Wthread-safety-pointer]
  220 |     LRU_Append(&in_use_, e);
      |                 ^
util/cache.cc:235:17: warning: passing pointer to variable 'lru_' requires holding mutex 'mutex_' [-Wthread-safety-pointer]
  235 |     LRU_Append(&lru_, e);

Clang's -Wthread-safety-pointer warnings will become part of -Wthread-safety in
some future release. Add missing annotations for functions that pass the
address of guarded members. No functional change. Fixes the following warnings:

util/cache.cc:220:17: warning: passing pointer to variable 'in_use_' requires holding mutex 'mutex_' [-Wthread-safety-pointer]
  220 |     LRU_Append(&in_use_, e);
      |                 ^
util/cache.cc:235:17: warning: passing pointer to variable 'lru_' requires holding mutex 'mutex_' [-Wthread-safety-pointer]
  235 |     LRU_Append(&lru_, e);
@davidgumberg
Copy link

Tested and review ACK 7daf4ed

in_use and lru_ are guarded by mutex_:

// Dummy head of LRU list.
// lru.prev is newest entry, lru.next is oldest entry.
// Entries have refs==1 and in_cache==true.
LRUHandle lru_ GUARDED_BY(mutex_);
// Dummy head of in-use list.
// Entries are in use by clients, and have refs >= 2 and in_cache==true.
LRUHandle in_use_ GUARDED_BY(mutex_);

and are accessed by LRUCache::Ref() and LRUCache::Unref() respectively:

void LRUCache::Ref(LRUHandle* e) {
if (e->refs == 1 && e->in_cache) { // If on lru_ list, move to in_use_ list.
LRU_Remove(e);
LRU_Append(&in_use_, e);
}
e->refs++;
}
void LRUCache::Unref(LRUHandle* e) {
assert(e->refs > 0);
e->refs--;
if (e->refs == 0) { // Deallocate.
assert(!e->in_cache);
(*e->deleter)(e->key(), e->value);
free(e);
} else if (e->in_cache && e->refs == 1) {
// No longer in use; move to lru_ list.
LRU_Remove(e);
LRU_Append(&lru_, e);
}
}

The annotations added here are correct.


Attempting to build with -Wthread-safety-pointer on master fails for me with errors identical to those in the PR description

$ clang --version
clang version 21.0.0git (git@github.com:llvm/llvm-project.git 1f1c725b68b9d72b5cd3fc95c57ce4244bc85c8e)
$ CC=clang CXX=clang++ cmake -B build -DCMAKE_CXX_FLAGS=-Wthread-safety-pointer
/leveldb-subtree/util/cache.cc:220:17: error: passing pointer to variable 'in_use_' requires holding mutex 'mutex_' [-Werror,-Wthread-safety-pointer]
  220 |     LRU_Append(&in_use_, e);
      |                 ^
/leveldb-subtree/util/cache.cc:235:17: error: passing pointer to variable 'lru_' requires holding mutex 'mutex_' [-Werror,-Wthread-safety-pointer]
  235 |     LRU_Append(&lru_, e);
      | 

And this branch fixes those thread safety warnings.

Copy link
Member

@fanquake fanquake left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 7daf4ed

Clang's -Wthread-safety-pointer warnings will become part of -Wthread-safety in some future release.

For anyone else wondering, Wthread-safety-pointer is first appearing in Clang 21.x.

@fanquake fanquake merged commit aba469a into bitcoin-core:bitcoin-fork May 29, 2025
fanquake added a commit to fanquake/bitcoin that referenced this pull request May 30, 2025
aba469ad6a Merge bitcoin-core/leveldb-subtree#54: Fix clang thread-safety-pointer warnings
7daf4ed575 Fix clang thread-safety-pointer warnings

git-subtree-dir: src/leveldb
git-subtree-split: aba469ad6a808da8381edf83a99e5ece18958b31
fanquake added a commit to bitcoin/bitcoin that referenced this pull request Jun 4, 2025
83bfe14 build: add -Wthread-safety-pointer (fanquake)
240a4fb Squashed 'src/leveldb/' changes from 113db4962b..aba469ad6a (fanquake)

Pull request description:

  This will become available in Clang 21:

  > ThreadSafetyAnalysis now supports -Wthread-safety-pointer, which
  > enables warning on passing or returning pointers to guarded variables
  > as function arguments or return value respectively. Note that
  > ThreadSafetyAnalysis still does not perform alias analysis. The
  > feature will be default-enabled with -Wthread-safety in a future release.

  See https://github.com/llvm/llvm-project/blob/main/clang/docs/ReleaseNotes.rst.

  Also updates the leveldb subtree to pull: bitcoin-core/leveldb-subtree#54.

ACKs for top commit:
  davidgumberg:
    Tested ACK  83bfe14
  maflcko:
    lgtm ACK 83bfe14
  theuni:
    utACK 83bfe14

Tree-SHA512: 9bc80bd04a9cebed8aca20bc23a17e52a6a89a1fb042993322f43dbf7bd93de509c091ebb69255063833b098ab11a64285eccf61e17b9f94f974c734a20ad8da
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants