merge #18635: Replace -Wthread-safety-analysis with -Wthread-safety

This commit is contained in:
Kittywhiskers Van Gogh 2021-06-09 17:32:42 +05:30
parent d559983eeb
commit 51fababb9c
9 changed files with 36 additions and 26 deletions

View File

@ -362,7 +362,7 @@ if test "x$enable_werror" = "xyes"; then
AC_MSG_ERROR("enable-werror set but -Werror is not usable") AC_MSG_ERROR("enable-werror set but -Werror is not usable")
fi fi
AX_CHECK_COMPILE_FLAG([-Werror=vla],[ERROR_CXXFLAGS="$ERROR_CXXFLAGS -Werror=vla"],,[[$CXXFLAG_WERROR]]) AX_CHECK_COMPILE_FLAG([-Werror=vla],[ERROR_CXXFLAGS="$ERROR_CXXFLAGS -Werror=vla"],,[[$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],[ERROR_CXXFLAGS="$ERROR_CXXFLAGS -Werror=thread-safety"],,[[$CXXFLAG_WERROR]])
fi fi
if test "x$CXXFLAGS_overridden" = "xno"; then if test "x$CXXFLAGS_overridden" = "xno"; then
@ -371,7 +371,7 @@ if test "x$CXXFLAGS_overridden" = "xno"; then
AX_CHECK_COMPILE_FLAG([-Wformat],[CXXFLAGS="$CXXFLAGS -Wformat"],,[[$CXXFLAG_WERROR]]) AX_CHECK_COMPILE_FLAG([-Wformat],[CXXFLAGS="$CXXFLAGS -Wformat"],,[[$CXXFLAG_WERROR]])
AX_CHECK_COMPILE_FLAG([-Wvla],[CXXFLAGS="$CXXFLAGS -Wvla"],,[[$CXXFLAG_WERROR]]) AX_CHECK_COMPILE_FLAG([-Wvla],[CXXFLAGS="$CXXFLAGS -Wvla"],,[[$CXXFLAG_WERROR]])
AX_CHECK_COMPILE_FLAG([-Wformat-security],[CXXFLAGS="$CXXFLAGS -Wformat-security"],,[[$CXXFLAG_WERROR]]) AX_CHECK_COMPILE_FLAG([-Wformat-security],[CXXFLAGS="$CXXFLAGS -Wformat-security"],,[[$CXXFLAG_WERROR]])
AX_CHECK_COMPILE_FLAG([-Wthread-safety-analysis],[CXXFLAGS="$CXXFLAGS -Wthread-safety-analysis"],,[[$CXXFLAG_WERROR]]) AX_CHECK_COMPILE_FLAG([-Wthread-safety],[CXXFLAGS="$CXXFLAGS -Wthread-safety"],,[[$CXXFLAG_WERROR]])
AX_CHECK_COMPILE_FLAG([-Wrange-loop-analysis],[WARN_CXXFLAGS="$WARN_CXXFLAGS -Wrange-loop-analysis"],,[[$CXXFLAG_WERROR]]) AX_CHECK_COMPILE_FLAG([-Wrange-loop-analysis],[WARN_CXXFLAGS="$WARN_CXXFLAGS -Wrange-loop-analysis"],,[[$CXXFLAG_WERROR]])
## Some compilers (gcc) ignore unknown -Wno-* options, but warn about all ## Some compilers (gcc) ignore unknown -Wno-* options, but warn about all

View File

@ -106,13 +106,12 @@ ReadStatus PartiallyDownloadedBlock::InitData(const CBlockHeaderAndShortTxIDs& c
std::vector<bool> have_txn(txn_available.size()); std::vector<bool> have_txn(txn_available.size());
{ {
LOCK(pool->cs); LOCK(pool->cs);
const std::vector<std::pair<uint256, CTxMemPool::txiter> >& vTxHashes = pool->vTxHashes; for (size_t i = 0; i < pool->vTxHashes.size(); i++) {
for (size_t i = 0; i < vTxHashes.size(); i++) { uint64_t shortid = cmpctblock.GetShortID(pool->vTxHashes[i].first);
uint64_t shortid = cmpctblock.GetShortID(vTxHashes[i].first);
std::unordered_map<uint64_t, uint16_t>::iterator idit = shorttxids.find(shortid); std::unordered_map<uint64_t, uint16_t>::iterator idit = shorttxids.find(shortid);
if (idit != shorttxids.end()) { if (idit != shorttxids.end()) {
if (!have_txn[idit->second]) { if (!have_txn[idit->second]) {
txn_available[idit->second] = vTxHashes[i].second->GetSharedTx(); txn_available[idit->second] = pool->vTxHashes[i].second->GetSharedTx();
have_txn[idit->second] = true; have_txn[idit->second] = true;
mempool_count++; mempool_count++;
} else { } else {

View File

@ -127,7 +127,7 @@ class PartiallyDownloadedBlock {
protected: protected:
std::vector<CTransactionRef> txn_available; std::vector<CTransactionRef> txn_available;
size_t prefilled_count = 0, mempool_count = 0, extra_count = 0; size_t prefilled_count = 0, mempool_count = 0, extra_count = 0;
CTxMemPool* pool; const CTxMemPool* pool;
public: public:
CBlockHeader header; CBlockHeader header;
explicit PartiallyDownloadedBlock(CTxMemPool* poolIn) : pool(poolIn) {} explicit PartiallyDownloadedBlock(CTxMemPool* poolIn) : pool(poolIn) {}

View File

@ -33,7 +33,7 @@ static int FileWriteStr(const std::string &str, FILE *fp)
bool BCLog::Logger::StartLogging() bool BCLog::Logger::StartLogging()
{ {
LockGuard scoped_lock(m_cs); StdLockGuard scoped_lock(m_cs);
assert(m_buffering); assert(m_buffering);
assert(m_fileout == nullptr); assert(m_fileout == nullptr);
@ -263,7 +263,7 @@ std::string BCLog::Logger::LogThreadNameStr(const std::string &str)
void BCLog::Logger::LogPrintStr(const std::string& str) void BCLog::Logger::LogPrintStr(const std::string& str)
{ {
LockGuard scoped_lock(m_cs); StdLockGuard scoped_lock(m_cs);
std::string strThreadLogged = LogThreadNameStr(str); std::string strThreadLogged = LogThreadNameStr(str);
std::string strTimestamped = LogTimestampStr(strThreadLogged); std::string strTimestamped = LogTimestampStr(strThreadLogged);

View File

@ -83,7 +83,7 @@ namespace BCLog {
class Logger class Logger
{ {
private: private:
mutable std::mutex m_cs; // Can not use Mutex from sync.h because in debug mode it would cause a deadlock when a potential deadlock was detected mutable StdMutex m_cs; // Can not use Mutex from sync.h because in debug mode it would cause a deadlock when a potential deadlock was detected
FILE* m_fileout GUARDED_BY(m_cs) = nullptr; FILE* m_fileout GUARDED_BY(m_cs) = nullptr;
std::list<std::string> m_msgs_before_open GUARDED_BY(m_cs); std::list<std::string> m_msgs_before_open GUARDED_BY(m_cs);
@ -119,7 +119,7 @@ namespace BCLog {
/** Returns whether logs will be written to any output */ /** Returns whether logs will be written to any output */
bool Enabled() const bool Enabled() const
{ {
LockGuard scoped_lock(m_cs); StdLockGuard scoped_lock(m_cs);
return m_buffering || m_print_to_console || m_print_to_file; return m_buffering || m_print_to_console || m_print_to_file;
} }

View File

@ -10,9 +10,9 @@
#include <fs.h> #include <fs.h>
#include <logging.h> #include <logging.h>
#include <streams.h> #include <streams.h>
#include <threadsafety.h>
#include <utilstrencodings.h> #include <utilstrencodings.h>
#include <mutex>
#include <map> #include <map>
#include <vector> #include <vector>
#include <memory> #include <memory>
@ -169,8 +169,8 @@ static __attribute__((noinline)) std::vector<uint64_t> GetStackFrames(size_t ski
static BOOL symInitialized = SymInitialize(GetCurrentProcess(), nullptr, TRUE); static BOOL symInitialized = SymInitialize(GetCurrentProcess(), nullptr, TRUE);
// dbghelp is not thread safe // dbghelp is not thread safe
static std::mutex m; static StdMutex m;
LockGuard l(m); StdLockGuard l(m);
HANDLE process = GetCurrentProcess(); HANDLE process = GetCurrentProcess();
HANDLE thread = GetCurrentThread(); HANDLE thread = GetCurrentThread();
@ -531,7 +531,7 @@ static void PrintCrashInfo(const crash_info& ci)
} }
#ifdef ENABLE_CRASH_HOOKS #ifdef ENABLE_CRASH_HOOKS
static std::mutex g_stacktraces_mutex; static StdMutex g_stacktraces_mutex;
static std::map<void*, std::shared_ptr<std::vector<uint64_t>>> g_stacktraces; static std::map<void*, std::shared_ptr<std::vector<uint64_t>>> g_stacktraces;
#if CRASH_HOOKS_WRAPPED_CXX_ABI #if CRASH_HOOKS_WRAPPED_CXX_ABI
@ -594,7 +594,7 @@ extern "C" void* __attribute__((noinline)) WRAPPED_NAME(__cxa_allocate_exception
void* p = __real___cxa_allocate_exception(thrown_size); void* p = __real___cxa_allocate_exception(thrown_size);
LockGuard l(g_stacktraces_mutex); StdLockGuard l(g_stacktraces_mutex);
g_stacktraces.emplace(p, st); g_stacktraces.emplace(p, st);
return p; return p;
} }
@ -603,7 +603,7 @@ extern "C" void __attribute__((noinline)) WRAPPED_NAME(__cxa_free_exception)(voi
{ {
__real___cxa_free_exception(thrown_exception); __real___cxa_free_exception(thrown_exception);
LockGuard l(g_stacktraces_mutex); StdLockGuard l(g_stacktraces_mutex);
g_stacktraces.erase(thrown_exception); g_stacktraces.erase(thrown_exception);
} }
@ -667,7 +667,7 @@ static std::shared_ptr<std::vector<uint64_t>> GetExceptionStacktrace(const std::
#ifdef ENABLE_CRASH_HOOKS #ifdef ENABLE_CRASH_HOOKS
void* p = *(void**)&e; void* p = *(void**)&e;
LockGuard l(g_stacktraces_mutex); StdLockGuard l(g_stacktraces_mutex);
auto it = g_stacktraces.find(p); auto it = g_stacktraces.find(p);
if (it == g_stacktraces.end()) { if (it == g_stacktraces.end()) {
return nullptr; return nullptr;

View File

@ -156,7 +156,8 @@ std::string LocksHeld()
return result; return result;
} }
void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, void* 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) for (const std::pair<void*, CLockLocation>& i : g_lockstack)
if (i.first == cs) if (i.first == cs)
@ -164,6 +165,8 @@ void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine,
fprintf(stderr, "Assertion failed: lock %s not held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld().c_str()); fprintf(stderr, "Assertion failed: lock %s not held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld().c_str());
abort(); abort();
} }
template void AssertLockHeldInternal(const char*, const char*, int, Mutex*);
template void AssertLockHeldInternal(const char*, const char*, int, CCriticalSection*);
void AssertLockNotHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs) void AssertLockNotHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs)
{ {

View File

@ -50,7 +50,8 @@ LEAVE_CRITICAL_SECTION(mutex); // no RAII
void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false); void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false);
void LeaveCritical(); void LeaveCritical();
std::string LocksHeld(); std::string LocksHeld();
void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, void* 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 AssertLockNotHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs);
void DeleteLock(void* cs); void DeleteLock(void* cs);
@ -63,7 +64,8 @@ extern bool g_debug_lockorder_abort;
#else #else
void static inline EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false) {} void static inline EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false) {}
void static inline LeaveCritical() {} void static inline LeaveCritical() {}
void static inline AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, void* 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 AssertLockNotHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs) {}
void static inline DeleteLock(void* cs) {} void static inline DeleteLock(void* cs) {}
#endif #endif

View File

@ -56,13 +56,19 @@
#define ASSERT_EXCLUSIVE_LOCK(...) #define ASSERT_EXCLUSIVE_LOCK(...)
#endif // __GNUC__ #endif // __GNUC__
// LockGuard provides an annotated version of lock_guard for us // StdMutex provides an annotated version of std::mutex for us,
// should only be used when sync.h Mutex/LOCK/etc aren't usable // and should only be used when sync.h Mutex/LOCK/etc are not usable.
class SCOPED_LOCKABLE LockGuard : public std::lock_guard<std::mutex> class LOCKABLE StdMutex : public std::mutex
{
};
// StdLockGuard provides an annotated version of std::lock_guard for us,
// and should only be used when sync.h Mutex/LOCK/etc are not usable.
class SCOPED_LOCKABLE StdLockGuard : public std::lock_guard<StdMutex>
{ {
public: public:
explicit LockGuard(std::mutex& cs) EXCLUSIVE_LOCK_FUNCTION(cs) : std::lock_guard<std::mutex>(cs) { } explicit StdLockGuard(StdMutex& cs) EXCLUSIVE_LOCK_FUNCTION(cs) : std::lock_guard<StdMutex>(cs) {}
~LockGuard() UNLOCK_FUNCTION() {}; ~StdLockGuard() UNLOCK_FUNCTION() {}
}; };
#endif // BITCOIN_THREADSAFETY_H #endif // BITCOIN_THREADSAFETY_H