Skip to content

Conversation

hebasto
Copy link
Member

@hebasto hebasto commented Apr 14, 2020

This PR gets rid of -Wthread-safety-attributes and -Wthread-safety-precise warnings, and replaces -Wthread-safety-analysis compiler option with the broader -Wthread-safety one.

@hebasto hebasto marked this pull request as draft April 14, 2020 19:58
@hebasto hebasto changed the title Revert "Annotate AssertLockHeld() with ASSERT_CAPABILITY() for thread safety analysis" [WIP] Revert "Annotate AssertLockHeld() with ASSERT_CAPABILITY() for thread safety analysis" Apr 14, 2020
@hebasto hebasto changed the title [WIP] Revert "Annotate AssertLockHeld() with ASSERT_CAPABILITY() for thread safety analysis" Suppress -Wthread-safety-attributes warning spam Apr 14, 2020
@hebasto hebasto marked this pull request as ready for review April 14, 2020 22:36
@fanquake fanquake requested a review from ajtowns May 7, 2020 00:45
Copy link
Contributor

@ajtowns ajtowns left a comment

Choose a reason for hiding this comment

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

It's not clear to me what the value of adding -Wthread-safety-attributes is, but better typing makes sense in general to me.

@hebasto
Copy link
Member Author

hebasto commented May 7, 2020

@ajtowns

It's not clear to me what the value of adding -Wthread-safety-attributes is, but better typing makes sense in general to me.

From the Thread Safety Analysis docs:

-Wthread-safety-attributes: Sanity checks on attribute syntax.

@vasild
Copy link
Contributor

vasild commented May 7, 2020

Concept ACK

I confirm that if master (3b1e289) is compiled with -Wthread-safety-attributes then some warnings are shown. This patch fixes all such warnings (Clang 11, FreeBSD 12).

I agree with @ajtowns that it would be better to use a template than duplicating the function body. Further, instead of template<typename PARENT> / AnnotatedMixin<PARENT>* cs we can use template <typename MutexType> / MutexType* cs - that is simpler and removes the need to move the Mutex and RecursiveMutex definitions higher in sync.h. Just to be explicit, this is what I mean:

patch to use MutexType* argument, on top of this PR (b6e44c3)
diff --git i/src/sync.cpp w/src/sync.cpp
index 67779b33a..8fc7f5d97 100644
--- i/src/sync.cpp
+++ w/src/sync.cpp
@@ -182,29 +182,25 @@ std::string LocksHeld()
     std::string result;
     for (const std::pair<void*, CLockLocation>& i : g_lockstack)
         result += i.second.ToString() + std::string("\n");
     return result;
 }
 
-void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, RecursiveMutex* cs)
+template <typename MutexType>
+void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, MutexType* cs)
 {
     for (const std::pair<void*, CLockLocation>& i : g_lockstack)
         if (i.first == cs)
             return;
     tfm::format(std::cerr, "Assertion failed: lock %s not held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld());
     abort();
 }
 
-void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, Mutex* cs)
-{
-    for (const std::pair<void*, CLockLocation>& i : g_lockstack)
-        if (i.first == cs)
-            return;
-    tfm::format(std::cerr, "Assertion failed: lock %s not held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld());
-    abort();
-}
+// Explicitly instantiate for Mutex and RecursiveMutex.
+template void AssertLockHeldInternal(const char*, const char*, int, Mutex*);
+template void AssertLockHeldInternal(const char*, const char*, int, RecursiveMutex*);
 
 void AssertLockNotHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs)
 {
     for (const std::pair<void*, CLockLocation>& i : g_lockstack) {
         if (i.first == cs) {
             tfm::format(std::cerr, "Assertion failed: lock %s held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld());
diff --git i/src/sync.h w/src/sync.h
index 78adf29c0..60e5a87ae 100644
--- i/src/sync.h
+++ w/src/sync.h
@@ -11,17 +11,12 @@
 
 #include <condition_variable>
 #include <mutex>
 #include <string>
 #include <thread>
 
-template <typename PARENT>
-class AnnotatedMixin;
-using RecursiveMutex = AnnotatedMixin<std::recursive_mutex>;
-using Mutex = AnnotatedMixin<std::mutex>;
-
 ////////////////////////////////////////////////
 //                                            //
 // THE SIMPLE DEFINITION, EXCLUDING DEBUG CODE //
 //                                            //
 ////////////////////////////////////////////////
 
@@ -54,14 +49,14 @@ LEAVE_CRITICAL_SECTION(mutex); // no RAII
 
 #ifdef DEBUG_LOCKORDER
 void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false);
 void LeaveCritical();
 void CheckLastCritical(void* cs, std::string& lockname, const char* guardname, const char* file, int line);
 std::string LocksHeld();
-void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, RecursiveMutex* cs) ASSERT_EXCLUSIVE_LOCK(cs);
-void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, Mutex* cs) ASSERT_EXCLUSIVE_LOCK(cs);
+template <typename MutexType>
+void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, MutexType* cs) ASSERT_EXCLUSIVE_LOCK(cs);
 void AssertLockNotHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs);
 void DeleteLock(void* cs);
 
 /**
  * Call abort() if a potential lock order deadlock bug is detected, instead of
  * just logging information and throwing a logic_error. Defaults to true, and
@@ -69,14 +64,14 @@ void DeleteLock(void* cs);
  */
 extern bool g_debug_lockorder_abort;
 #else
 void static inline EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false) {}
 void static inline LeaveCritical() {}
 void static inline CheckLastCritical(void* cs, std::string& lockname, const char* guardname, const char* file, int line) {}
-void static inline AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, RecursiveMutex* cs) ASSERT_EXCLUSIVE_LOCK(cs) {}
-void static inline AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, Mutex* cs) ASSERT_EXCLUSIVE_LOCK(cs) {}
+template <typename MutexType>
+void static inline AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, MutexType* cs) ASSERT_EXCLUSIVE_LOCK(cs) {}
 void static inline AssertLockNotHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs) {}
 void static inline DeleteLock(void* cs) {}
 #endif
 #define AssertLockHeld(cs) AssertLockHeldInternal(#cs, __FILE__, __LINE__, &cs)
 #define AssertLockNotHeld(cs) AssertLockNotHeldInternal(#cs, __FILE__, __LINE__, &cs)
 

Maybe add thread-safety-attributes to the warnings flags in configure.ac to make sure future changes do not introduce such warnings:

patch to extend warnings in configure.ac
diff --git i/configure.ac w/configure.ac
index 4c9902efc..e19346e59 100644
--- i/configure.ac
+++ w/configure.ac
@@ -326,12 +326,13 @@ if test "x$enable_werror" = "xyes"; then
   if test "x$CXXFLAG_WERROR" = "x"; then
     AC_MSG_ERROR("enable-werror set but -Werror is not usable")
   fi
   AX_CHECK_COMPILE_FLAG([-Werror=vla],[ERROR_CXXFLAGS="$ERROR_CXXFLAGS -Werror=vla"],,[[$CXXFLAG_WERROR]])
   AX_CHECK_COMPILE_FLAG([-Werror=switch],[ERROR_CXXFLAGS="$ERROR_CXXFLAGS -Werror=switch"],,[[$CXXFLAG_WERROR]])
   AX_CHECK_COMPILE_FLAG([-Werror=thread-safety-analysis],[ERROR_CXXFLAGS="$ERROR_CXXFLAGS -Werror=thread-safety-analysis"],,[[$CXXFLAG_WERROR]])
+  AX_CHECK_COMPILE_FLAG([-Werror=thread-safety-attributes],[ERROR_CXXFLAGS="$ERROR_CXXFLAGS -Werror=thread-safety-attributes"],,[[$CXXFLAG_WERROR]])
   AX_CHECK_COMPILE_FLAG([-Werror=unused-variable],[ERROR_CXXFLAGS="$ERROR_CXXFLAGS -Werror=unused-variable"],,[[$CXXFLAG_WERROR]])
   AX_CHECK_COMPILE_FLAG([-Werror=date-time],[ERROR_CXXFLAGS="$ERROR_CXXFLAGS -Werror=date-time"],,[[$CXXFLAG_WERROR]])
   AX_CHECK_COMPILE_FLAG([-Werror=return-type],[ERROR_CXXFLAGS="$ERROR_CXXFLAGS -Werror=return-type"],,[[$CXXFLAG_WERROR]])
 fi

 if test "x$CXXFLAGS_overridden" = "xno"; then
@@ -339,12 +340,13 @@ if test "x$CXXFLAGS_overridden" = "xno"; then
   AX_CHECK_COMPILE_FLAG([-Wextra],[WARN_CXXFLAGS="$WARN_CXXFLAGS -Wextra"],,[[$CXXFLAG_WERROR]])
   AX_CHECK_COMPILE_FLAG([-Wformat],[WARN_CXXFLAGS="$WARN_CXXFLAGS -Wformat"],,[[$CXXFLAG_WERROR]])
   AX_CHECK_COMPILE_FLAG([-Wvla],[WARN_CXXFLAGS="$WARN_CXXFLAGS -Wvla"],,[[$CXXFLAG_WERROR]])
   AX_CHECK_COMPILE_FLAG([-Wswitch],[WARN_CXXFLAGS="$WARN_CXXFLAGS -Wswitch"],,[[$CXXFLAG_WERROR]])
   AX_CHECK_COMPILE_FLAG([-Wformat-security],[WARN_CXXFLAGS="$WARN_CXXFLAGS -Wformat-security"],,[[$CXXFLAG_WERROR]])
   AX_CHECK_COMPILE_FLAG([-Wthread-safety-analysis],[WARN_CXXFLAGS="$WARN_CXXFLAGS -Wthread-safety-analysis"],,[[$CXXFLAG_WERROR]])
+  AX_CHECK_COMPILE_FLAG([-Wthread-safety-attributes],[WARN_CXXFLAGS="$WARN_CXXFLAGS -Wthread-safety-attributes"],,[[$CXXFLAG_WERROR]])
   AX_CHECK_COMPILE_FLAG([-Wrange-loop-analysis],[WARN_CXXFLAGS="$WARN_CXXFLAGS -Wrange-loop-analysis"],,[[$CXXFLAG_WERROR]])
   AX_CHECK_COMPILE_FLAG([-Wredundant-decls],[WARN_CXXFLAGS="$WARN_CXXFLAGS -Wredundant-decls"],,[[$CXXFLAG_WERROR]])
   AX_CHECK_COMPILE_FLAG([-Wunused-variable],[WARN_CXXFLAGS="$WARN_CXXFLAGS -Wunused-variable"],,[[$CXXFLAG_WERROR]])
   AX_CHECK_COMPILE_FLAG([-Wdate-time],[WARN_CXXFLAGS="$WARN_CXXFLAGS -Wdate-time"],,[[$CXXFLAG_WERROR]])

   dnl Some compilers (gcc) ignore unknown -Wno-* options, but warn about all

@hebasto
Copy link
Member Author

hebasto commented May 9, 2020

Updated:

  • addressed @ajtowns's and @vasild's comments
  • rebased
  • added two commits that allow to replace -Wthread-safety-analysis with broader -Wthread-safety

The OP has been updated.

@hebasto hebasto changed the title Suppress -Wthread-safety-attributes warning spam Replace -Wthread-safety-analysis with broader -Wthread-safety May 9, 2020
Copy link
Member

@maflcko maflcko left a comment

Choose a reason for hiding this comment

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

Concept ACK

@DrahtBot
Copy link
Contributor

DrahtBot commented May 9, 2020

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

Reviewers, this pull request conflicts with the following ones:

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@hebasto
Copy link
Member Author

hebasto commented May 10, 2020

Updated 6bc97d0 -> 7a54927 (pr18635.03 -> pr18635.04, diff):

Since you are touching all the lines where pool is used anyway, mind to make it const, since the access is read-only?

Copy link
Contributor

@vasild vasild left a comment

Choose a reason for hiding this comment

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

ACK 7a54927

It compiles locally with Clang 11 using the new flags without thread-safety related warnings.

@ajtowns
Copy link
Contributor

ajtowns commented May 12, 2020

@hebasto Yeah, I saw the "sanity check" description, but it's still not clear to me what that actually means. Changes for precise look good to me.

@ajtowns
Copy link
Contributor

ajtowns commented May 19, 2020

Note that this conflicts with #16127 in that -Wthread-safety-attributes means you need to add a wrapper around any plain std::mutex instances in order to do thread safety analysis on them. Adding class LOCKABLE StdMutex : public std::mutex { }; and having #16127's LockGuard refer to that instead of std::mutex seems to be sufficient to resolve the conflict, I think.

@hebasto
Copy link
Member Author

hebasto commented May 19, 2020

Note that this conflicts with #16127 in that -Wthread-safety-attributes means you need to add a wrapper around any plain std::mutex instances in order to do thread safety analysis on them. Adding class LOCKABLE StdMutex : public std::mutex { }; and having #16127's LockGuard refer to that instead of std::mutex seems to be sufficient to resolve the conflict, I think.

Agree. Hope #16127 will be merged soon.

@hebasto
Copy link
Member Author

hebasto commented May 28, 2020

Rebased 908c6c3 -> 87766b3 (pr18635.05 -> pr18635.06) due to the conflict with #16127:

Note that this conflicts with #16127 in that -Wthread-safety-attributes means you need to add a wrapper around any plain std::mutex instances in order to do thread safety analysis on them. Adding class LOCKABLE StdMutex : public std::mutex { }; and having #16127's LockGuard refer to that instead of std::mutex seems to be sufficient to resolve the conflict, I think.

Copy link
Contributor

@vasild vasild left a comment

Choose a reason for hiding this comment

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

ACK 87766b3

@practicalswift
Copy link
Contributor

ACK 87766b3 -- patch looks correct

@ajtowns
Copy link
Contributor

ajtowns commented May 28, 2020

ACK 87766b3

@maflcko
Copy link
Member

maflcko commented May 28, 2020

ACK 87766b3 👍

Show signature and timestamp

Signature:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

ACK 87766b355c47fcb0f0dcf3f6fe359eb00227d50c 👍
-----BEGIN PGP SIGNATURE-----

iQGzBAEBCgAdFiEE+rVPoUahrI9sLGYTzit1aX5ppUgFAlwqrYAACgkQzit1aX5p
pUi4iAwAg0uiwOcah5mJaVzZrOWF2rtKNZTQjr+KEZxWCBRuq1Q+GLJ7Dsf0cqwl
CWtPoyfI524dIxAEANrMOqlitZ0fS7PYfgWAHDKVgS4mXaYRUEH7YKs1aShz/0u8
ES1GiLCtAM7WCcvjeVTwsvQ7D8hXtRDyN86oXJnJg+WWJPtp0yd2dMuWhZNvLTiF
hRbv03wBr632i8dSlBG5y0libBVyPBozVMYAnVKmK3ZMlrLlUwYPtFPBwOmN+4cf
4MrST70Ks90f+6Tlo6VbSoFbIvpNLYfM1g8saQDseA0y04q0h8y0og6WnUMzNYX5
QwJVr3hR9t50wbiKmAsdIPmFiOTlVu1pqbckAkgNCNmrHaASgNIuRfPR9WyP8fMq
CWYLw0727ZsUNKxCwEp18vIWkq9wZYVbXTM8GnfpfiA8FX8OH9FH+q7uroNH784N
Shf9kU2DPmOEHu8MG+7htpWRjHuEN/jVWCrxsoU66g1G7jREddIKNfoLDAzmB0p9
PlykKQ8f
=ufrW
-----END PGP SIGNATURE-----

Timestamp of file with hash 55f3dcd9f7f76a0aa36c4f8d7721a03cda9bfb278201a4ebc4c064d229aba0a4 -

@maflcko maflcko merged commit 082a417 into bitcoin:master May 28, 2020
@hebasto hebasto deleted the 200414-threads branch May 28, 2020 15:53
jasonbcox pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Oct 11, 2020
Summary:
Co-authored-by: Anthony Towns <aj@erisian.com.au>

This is part [1/5] of Core [[bitcoin/bitcoin#18635 | PR18635]] : bitcoin/bitcoin@79be487

Test Plan:
  ninja all check

Using clang. Check that the thread safety analysis is happy with it.

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D7877
jasonbcox pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Oct 11, 2020
Summary:
This is part [2/5] of Core [[bitcoin/bitcoin#18635 | PR18635]] : bitcoin/bitcoin@dfb75ae

Depends on D7877

Test Plan:
  ninja all check-all

Check that clang's thread safety analysis is happy.

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D7878
jasonbcox pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Oct 11, 2020
Summary:
This change gets rid of -Wthread-safety-attributes warning spam.

This is part [3/5] of Core [[bitcoin/bitcoin#18635 | PR18635]] : bitcoin/bitcoin@971a468

Depends on D7878

Test Plan:
  ninja all check

Checks that clang thread safety analysis is happy.

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D7879
jasonbcox pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Oct 11, 2020
Summary:
This is part [4/5] of Core [[bitcoin/bitcoin#18635 | PR18635]] : bitcoin/bitcoin@9cc6eb3

Depends on D7879, D7876 and D7875

Test Plan:
  ninja all check

Check that clang thread safety analysis is happy.

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D7880
deadalnix pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Oct 12, 2020
Summary:
This is part [5/5] of Core [[bitcoin/bitcoin#18635 | PR18635]] : bitcoin/bitcoin@87766b3

Depends on D7882

Test Plan:
  ninja all check

Make sure that clang is happy.

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D7883
kwvg added a commit to kwvg/dash that referenced this pull request Jun 5, 2021
…afety

Wthread-safety-analysis with -Wthread-safety
kwvg added a commit to kwvg/dash that referenced this pull request Jun 5, 2021
kwvg added a commit to kwvg/dash that referenced this pull request Jun 6, 2021
kwvg added a commit to kwvg/dash that referenced this pull request Jun 6, 2021
kwvg added a commit to kwvg/dash that referenced this pull request Jun 6, 2021
UdjinM6 pushed a commit to UdjinM6/dash that referenced this pull request Jun 6, 2021
UdjinM6 pushed a commit to UdjinM6/dash that referenced this pull request Jun 6, 2021
kwvg added a commit to kwvg/dash that referenced this pull request Jun 7, 2021
kwvg added a commit to kwvg/dash that referenced this pull request Jun 8, 2021
kwvg added a commit to kwvg/dash that referenced this pull request Jun 9, 2021
kwvg added a commit to kwvg/dash that referenced this pull request Jun 10, 2021
UdjinM6 added a commit to dashpay/dash that referenced this pull request Jun 11, 2021
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Feb 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants