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.
Replace -Wthread-safety-analysis with broader -Wthread-safety #18635
pull hebasto wants to merge 5 commits into bitcoin:master from hebasto:200414-threads changing 9 files +34 −24-
hebasto commented at 5:17 PM on April 14, 2020: member
- hebasto marked this as a draft on Apr 14, 2020
- hebasto renamed this:
Revert "Annotate AssertLockHeld() with ASSERT_CAPABILITY() for thread safety analysis"
[WIP] Revert "Annotate AssertLockHeld() with ASSERT_CAPABILITY() for thread safety analysis"
on Apr 14, 2020 - hebasto force-pushed on Apr 14, 2020
- hebasto renamed this:
[WIP] Revert "Annotate AssertLockHeld() with ASSERT_CAPABILITY() for thread safety analysis"
Suppress -Wthread-safety-attributes warning spam
on Apr 14, 2020 - hebasto marked this as ready for review on Apr 14, 2020
- fanquake requested review from ajtowns on May 7, 2020
-
in src/sync.cpp:197 in b6e44c3fd4 outdated
193 | + return; 194 | + tfm::format(std::cerr, "Assertion failed: lock %s not held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld()); 195 | + abort(); 196 | +} 197 | + 198 | +void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, Mutex* cs)
ajtowns commented at 3:30 AM on May 7, 2020:Rather than duplicating the function, you can use a template:
template<typename PARENT> void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, AnnotatedMixin<PARENT>* cs) { ... } template void AssertLockHeldInternal(const char*, const char*, int, Mutex*); template void AssertLockHeldInternal(const char*, const char*, int, RecursiveMutex*);That means changing the header to:
template<typename PARENT> void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, AnnotatedMixin<PARENT>* cs) ASSERT_EXCLUSIVE_LOCK(cs);to match.
Or alternatively, don't touch sync.cpp file at all, drop the
ASSERT_EXCLUSIVE_LOCK(cs)fromAssertLockHeldInternaland changeAssertLockHeldto:template<typename PARENT> void static inline AssertLockHeldInternalTyped(const char* pszName, const char* pszFile, int nLine, AnnotatedMixin<PARENT>* cs) ASSERT_EXCLUSIVE_LOCK(cs) { AssertLockHeldInternal(pszName, pszFile, nLine, (void*)cs); }; #define AssertLockHeld(cs) AssertLockHeldInternalTyped(#cs, __FILE__, __LINE__, &cs)
vasild commented at 10:16 AM on May 7, 2020:Right, in
Cthe "any type" isvoid*, whereas inC++we have templates.That
void*typecast at the bottom of the comment above is like sweeping it under the carpet and it still leaves the possibility to useAssertLockHeldInternal()in a type unsafe way (because it takesvoid*argument).
in src/sync.h:120 in b6e44c3fd4 outdated
116 | @@ -110,7 +117,7 @@ class LOCKABLE AnnotatedMixin : public PARENT 117 | using RecursiveMutex = AnnotatedMixin<std::recursive_mutex>; 118 | 119 | /** Wrapped mutex: supports waiting but not recursive locking */ 120 | -typedef AnnotatedMixin<std::mutex> Mutex; 121 | +using Mutex = AnnotatedMixin<std::mutex>;
ajtowns commented at 3:31 AM on May 7, 2020:No need to repeat the definitions of
MutexandRecursiveMutexafter you added the definitions above.
ajtowns commented at 4:37 AM on May 7, 2020: memberIt's not clear to me what the value of adding
-Wthread-safety-attributesis, but better typing makes sense in general to me.vasild commented at 10:09 AM on May 7, 2020: memberConcept ACK
I confirm that if
master(3b1e28924) is compiled with-Wthread-safety-attributesthen 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>* cswe can usetemplate <typename MutexType>/MutexType* cs- that is simpler and removes the need to move theMutexandRecursiveMutexdefinitions higher insync.h. Just to be explicit, this is what I mean:<details> <summary>patch to use MutexType* argument, on top of this PR (b6e44c3fd)</summary>
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)</details>
Maybe add
thread-safety-attributesto the warnings flags inconfigure.acto make sure future changes do not introduce such warnings:<details> <summary>patch to extend warnings in configure.ac</summary>
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</details>
hebasto force-pushed on May 9, 2020hebasto renamed this:Suppress -Wthread-safety-attributes warning spam
Replace -Wthread-safety-analysis with broader -Wthread-safety
on May 9, 2020DrahtBot added the label Build system on May 9, 2020DrahtBot added the label RPC/REST/ZMQ on May 9, 2020DrahtBot added the label Wallet on May 9, 2020in src/blockencodings.cpp:113 in 6bc97d0f86 outdated
112 | + uint64_t shortid = cmpctblock.GetShortID(pool->vTxHashes[i].first); 113 | std::unordered_map<uint64_t, uint16_t>::iterator idit = shorttxids.find(shortid); 114 | if (idit != shorttxids.end()) { 115 | if (!have_txn[idit->second]) { 116 | - txn_available[idit->second] = vTxHashes[i].second->GetSharedTx(); 117 | + txn_available[idit->second] = pool->vTxHashes[i].second->GetSharedTx();
MarcoFalke commented at 1:54 PM on May 9, 2020:Since you are touching all the lines where
poolis used anyway, mind to make it const, since the access is read-only?diff --git a/src/blockencodings.h b/src/blockencodings.h index 377ac3a1a6..6efcc8c9c9 100644 --- a/src/blockencodings.h +++ b/src/blockencodings.h @@ -125,7 +125,8 @@ class PartiallyDownloadedBlock { protected: std::vector<CTransactionRef> txn_available; size_t prefilled_count = 0, mempool_count = 0, extra_count = 0; - CTxMemPool* pool; + const CTxMemPool* pool; + public: CBlockHeader header; explicit PartiallyDownloadedBlock(CTxMemPool* poolIn) : pool(poolIn) {}
MarcoFalke commented at 1:54 PM on May 9, 2020: memberConcept ACK
DrahtBot commented at 7:21 PM on May 9, 2020: member<!--e57a25ab6845829454e8d69fc972939a-->
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.
<!--174a7506f384e20aa4161008e828411d-->
Conflicts
Reviewers, this pull request conflicts with the following ones:
- #18882 (build: fix -Wformat-security check when compiling with GCC by fanquake)
- #18857 (build: avoid repetitions when enabling warnings in configure.ac by vasild)
- #18354 (Use shared pointers only in validation interface by bvbfan)
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 force-pushed on May 10, 2020hebasto commented at 5:29 AM on May 10, 2020: memberUpdated 6bc97d0f860b52933b5664386576876d61ae228c -> 7a54927d2f068fef1638b3f6e3f5d7b52be22edf (pr18635.03 -> pr18635.04, diff):
- addressed @MarcoFalke's comment:
Since you are touching all the lines where
poolis used anyway, mind to make it const, since the access is read-only?vasild approvedvasild commented at 9:05 AM on May 11, 2020: memberACK 7a54927d2
It compiles locally with Clang 11 using the new flags without
thread-safetyrelated warnings.ajtowns commented at 7:37 AM on May 19, 2020: memberNote that this conflicts with #16127 in that
-Wthread-safety-attributesmeans you need to add a wrapper around any plainstd::mutexinstances in order to do thread safety analysis on them. Addingclass LOCKABLE StdMutex : public std::mutex { };and having #16127'sLockGuardrefer to that instead ofstd::mutexseems to be sufficient to resolve the conflict, I think.hebasto commented at 7:50 AM on May 19, 2020: memberNote that this conflicts with #16127 in that
-Wthread-safety-attributesmeans you need to add a wrapper around any plainstd::mutexinstances in order to do thread safety analysis on them. Addingclass LOCKABLE StdMutex : public std::mutex { };and having #16127'sLockGuardrefer to that instead ofstd::mutexseems to be sufficient to resolve the conflict, I think.Agree. Hope #16127 will be merged soon.
DrahtBot added the label Needs rebase on May 26, 2020hebasto force-pushed on May 26, 2020hebasto commented at 2:26 PM on May 26, 2020: memberRebased 7a54927d2f068fef1638b3f6e3f5d7b52be22edf -> 908c6c3cea5ffe3b9a9612f2327227cf3f54e5fc (pr18635.04 -> pr18635.05) due to the conflict with #18881.
DrahtBot removed the label Needs rebase on May 26, 2020ryanofsky approvedryanofsky commented at 7:11 PM on May 27, 2020: memberCode review ACK 908c6c3cea5ffe3b9a9612f2327227cf3f54e5fc. It seems good to enable more static checking here, and the code changes are simple and straightforward
practicalswift commented at 7:30 PM on May 27, 2020: contributorACK 908c6c3cea5ffe3b9a9612f2327227cf3f54e5fc: patch looks correct, and more compile-time checking is better! :)
79be487420Add thread safety annotated wrapper for std::mutex
Co-authored-by: Anthony Towns <aj@erisian.com.au>
refactor: Rename LockGuard to StdLockGuard for consistency with StdMutex dfb75ae49d971a468ccfUse template function instead of void* parameter
This change gets rid of -Wthread-safety-attributes warning spam.
Get rid of -Wthread-safety-precise warnings 9cc6eb3c9ebuild: Replace -Wthread-safety-analysis with broader -Wthread-safety 87766b355chebasto force-pushed on May 28, 2020hebasto commented at 7:05 AM on May 28, 2020: memberRebased 908c6c3cea5ffe3b9a9612f2327227cf3f54e5fc -> 87766b355c47fcb0f0dcf3f6fe359eb00227d50c (pr18635.05 -> pr18635.06) due to the conflict with #16127:
Note that this conflicts with #16127 in that
-Wthread-safety-attributesmeans you need to add a wrapper around any plainstd::mutexinstances in order to do thread safety analysis on them. Addingclass LOCKABLE StdMutex : public std::mutex { };and having #16127'sLockGuardrefer to that instead ofstd::mutexseems to be sufficient to resolve the conflict, I think.vasild approvedvasild commented at 8:22 AM on May 28, 2020: memberACK 87766b3
practicalswift commented at 10:04 AM on May 28, 2020: contributorACK 87766b355c47fcb0f0dcf3f6fe359eb00227d50c -- patch looks correct
ajtowns commented at 11:26 AM on May 28, 2020: memberACK 87766b355c47fcb0f0dcf3f6fe359eb00227d50c
MarcoFalke commented at 2:47 PM on May 28, 2020: memberACK 87766b355c47fcb0f0dcf3f6fe359eb00227d50c 👍
<details><summary>Show signature and timestamp</summary>
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 -</details>
MarcoFalke merged this on May 28, 2020MarcoFalke closed this on May 28, 2020hebasto deleted the branch on May 28, 2020jasonbcox referenced this in commit 902d495d4e on Oct 11, 2020jasonbcox referenced this in commit 08508a1417 on Oct 11, 2020jasonbcox referenced this in commit de6770e8a3 on Oct 11, 2020jasonbcox referenced this in commit 8465ae5d5a on Oct 11, 2020deadalnix referenced this in commit 3c9a54910e on Oct 12, 2020kittywhiskers referenced this in commit c12ed3633a on Jun 5, 2021kittywhiskers referenced this in commit 7c44302d0d on Jun 5, 2021kittywhiskers referenced this in commit 231e885590 on Jun 6, 2021kittywhiskers referenced this in commit c578b6444d on Jun 6, 2021kittywhiskers referenced this in commit fbc248c5ff on Jun 6, 2021UdjinM6 referenced this in commit 878d370b3e on Jun 6, 2021UdjinM6 referenced this in commit eb652da8a7 on Jun 6, 2021kittywhiskers referenced this in commit 5b70904229 on Jun 7, 2021kittywhiskers referenced this in commit 5d9e69fc57 on Jun 8, 2021kittywhiskers referenced this in commit d532622ecd on Jun 9, 2021kittywhiskers referenced this in commit 51fababb9c on Jun 10, 2021UdjinM6 referenced this in commit a8aee57447 on Jun 11, 2021DrahtBot locked this on Feb 15, 2022
This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-05-02 15:14 UTC
More mirrored repositories can be found on mirror.b10c.me