Skip to content

Commit a3aa840

Browse files
authored
Merge pull request #2292 from jamescowens/fix_gcc10_compile_warnings
refactor: Fix advanced compiler warnings
2 parents 988da21 + a3289ff commit a3aa840

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+613
-412
lines changed

configure.ac

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,12 @@ AC_ARG_ENABLE([glibc-back-compat],
179179
[use_glibc_compat=$enableval],
180180
[use_glibc_compat=no])
181181

182+
AC_ARG_ENABLE([threadlocal],
183+
[AS_HELP_STRING([--enable-threadlocal],
184+
[enable features that depend on the c++ thread_local keyword (currently just thread names in debug logs). (default is to enabled if there is platform support and glibc-back-compat is not enabled)])],
185+
[use_thread_local=$enableval],
186+
[use_thread_local=auto])
187+
182188
AC_ARG_ENABLE([asm],
183189
[AS_HELP_STRING([--disable-asm],
184190
[disable assembly routines (enabled by default)])],
@@ -229,7 +235,6 @@ if test "x$enable_werror" = "xyes"; then
229235
if test "x$CXXFLAG_WERROR" = "x"; then
230236
AC_MSG_ERROR("enable-werror set but -Werror is not usable")
231237
fi
232-
AX_CHECK_COMPILE_FLAG([-Werror=gnu],[ERROR_CXXFLAGS="$ERROR_CXXFLAGS -Werror=gnu"],,[[$CXXFLAG_WERROR]])
233238
AX_CHECK_COMPILE_FLAG([-Werror=vla],[ERROR_CXXFLAGS="$ERROR_CXXFLAGS -Werror=vla"],,[[$CXXFLAG_WERROR]])
234239
AX_CHECK_COMPILE_FLAG([-Werror=shadow-field],[ERROR_CXXFLAGS="$ERROR_CXXFLAGS -Werror=shadow-field"],,[[$CXXFLAG_WERROR]])
235240
AX_CHECK_COMPILE_FLAG([-Werror=switch],[ERROR_CXXFLAGS="$ERROR_CXXFLAGS -Werror=switch"],,[[$CXXFLAG_WERROR]])
@@ -246,7 +251,6 @@ fi
246251
if test "x$CXXFLAGS_overridden" = "xno"; then
247252
AX_CHECK_COMPILE_FLAG([-Wall],[CXXFLAGS="$CXXFLAGS -Wall"],,[[$CXXFLAG_WERROR]])
248253
AX_CHECK_COMPILE_FLAG([-Wextra],[CXXFLAGS="$CXXFLAGS -Wextra"],,[[$CXXFLAG_WERROR]])
249-
AX_CHECK_COMPILE_FLAG([-Wgnu],[CXXFLAGS="$CXXFLAGS -Wgnu"],,[[$CXXFLAG_WERROR]])
250254
dnl some compilers will ignore -Wformat-security without -Wformat, so just combine the two here.
251255
AX_CHECK_COMPILE_FLAG([-Wformat -Wformat-security],[CXXFLAGS="$CXXFLAGS -Wformat -Wformat-security"],,[[$CXXFLAG_WERROR]])
252256
AX_CHECK_COMPILE_FLAG([-Wvla],[CXXFLAGS="$CXXFLAGS -Wvla"],,[[$CXXFLAG_WERROR]])
@@ -782,6 +786,48 @@ AC_LINK_IFELSE([AC_LANG_SOURCE([
782786
]
783787
)
784788

789+
dnl thread_local is currently disabled when building with glibc back compat.
790+
dnl Our minimum supported glibc is 2.17, however support for thread_local
791+
dnl did not arrive in glibc until 2.18.
792+
if test "x$use_thread_local" = xyes || { test "x$use_thread_local" = xauto && test "x$use_glibc_compat" = xno; }; then
793+
TEMP_LDFLAGS="$LDFLAGS"
794+
LDFLAGS="$TEMP_LDFLAGS $PTHREAD_CFLAGS"
795+
AC_MSG_CHECKING([for thread_local support])
796+
AC_LINK_IFELSE([AC_LANG_SOURCE([
797+
#include <thread>
798+
static thread_local int foo = 0;
799+
static void run_thread() { foo++;}
800+
int main(){
801+
for(int i = 0; i < 10; i++) { std::thread(run_thread).detach();}
802+
return foo;
803+
}
804+
])],
805+
[
806+
case $host in
807+
*mingw*)
808+
dnl mingw32's implementation of thread_local has also been shown to behave
809+
dnl erroneously under concurrent usage; see:
810+
dnl https://gist.github.com/jamesob/fe9a872051a88b2025b1aa37bfa98605
811+
AC_MSG_RESULT(no)
812+
;;
813+
*freebsd*)
814+
dnl FreeBSD's implementation of thread_local is also buggy (per
815+
dnl https://groups.google.com/d/msg/bsdmailinglist/22ncTZAbDp4/Dii_pII5AwAJ)
816+
AC_MSG_RESULT(no)
817+
;;
818+
*)
819+
AC_DEFINE(HAVE_THREAD_LOCAL,1,[Define if thread_local is supported.])
820+
AC_MSG_RESULT(yes)
821+
;;
822+
esac
823+
],
824+
[
825+
AC_MSG_RESULT(no)
826+
]
827+
)
828+
LDFLAGS="$TEMP_LDFLAGS"
829+
fi
830+
785831
dnl check for gmtime_r(), fallback to gmtime_s() if that is unavailable
786832
dnl fail if neither are available.
787833
AC_MSG_CHECKING(for gmtime_r)

src/consensus/tx_verify.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#include <primitives/transaction.h>
88

9-
bool IsFinalTx(const CTransaction &tx, int nBlockHeight, int64_t nBlockTime)
9+
bool IsFinalTx(const CTransaction &tx, int nBlockHeight, int64_t nBlockTime) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
1010
{
1111
AssertLockHeld(cs_main);
1212
// Time based nLockTime implemented in 0.1.6

src/crypter.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,17 @@ bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned
132132
return false;
133133
return cKeyCrypter.Decrypt(vchCiphertext, *((CKeyingMaterial*)&vchPlaintext));
134134
}
135+
136+
bool DecryptKey(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCryptedSecret, const CPubKey& vchPubKey, CKey& key)
137+
{
138+
CKeyingMaterial vchSecret;
139+
if(!DecryptSecret(vMasterKey, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
140+
return false;
141+
142+
if (vchSecret.size() != 32)
143+
return false;
144+
145+
key.SetPubKey(vchPubKey);
146+
key.SetSecret(vchSecret);
147+
return (key.GetPubKey() == vchPubKey);
148+
}

src/crypter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,5 @@ class CCrypter
121121

122122
bool EncryptSecret(CKeyingMaterial& vMasterKey, const CSecret &vchPlaintext, const uint256& nIV, std::vector<unsigned char> &vchCiphertext);
123123
bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char> &vchCiphertext, const uint256& nIV, CSecret &vchPlaintext);
124+
bool DecryptKey(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCryptedSecret, const CPubKey& vchPubKey, CKey& key);
124125
#endif

src/gridcoin/gridcoin.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "chainparams.h"
66
#include "main.h"
7+
#include "util/threadnames.h"
78
#include "gridcoin/backup.h"
89
#include "gridcoin/contract/contract.h"
910
#include "gridcoin/gridcoin.h"
@@ -218,6 +219,9 @@ void InitializeResearcherContext()
218219
//!
219220
void ThreadScraper(void* parg)
220221
{
222+
RenameThread("grc-scraper");
223+
util::ThreadSetInternalName("grc-scraper");
224+
221225
LogPrint(BCLog::LogFlags::NOISY, "ThreadSraper starting");
222226

223227
try {
@@ -246,6 +250,9 @@ void ThreadScraper(void* parg)
246250
//!
247251
void ThreadScraperSubscriber(void* parg)
248252
{
253+
RenameThread("grc-scrapersubscriber");
254+
util::ThreadSetInternalName("grc-scrapersubscriber");
255+
249256
LogPrint(BCLog::LogFlags::NOISY, "ThreadScraperSubscriber starting");
250257

251258
try {

src/gridcoin/gridcoin.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
#pragma once
66

7-
class CBlockIndex;
7+
#include "fwd.h"
8+
89
class CScheduler;
910

1011
namespace GRC {

src/gridcoin/magnitude.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ class Magnitude
150150

151151
bool operator==(const int64_t other) const
152152
{
153-
return static_cast<int64_t>(m_scaled) == other * SCALE_FACTOR;
153+
return static_cast<int64_t>(m_scaled) == other * static_cast<int64_t>(SCALE_FACTOR);
154154
}
155155

156156
bool operator!=(const int64_t other) const

src/gridcoin/researcher.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ class RecentBeacons
475475
//!
476476
//! \param beacons Contains the set of pending beacons to import from.
477477
//!
478-
void ImportRegistry(const BeaconRegistry& beacons)
478+
void ImportRegistry(const BeaconRegistry& beacons) EXCLUSIVE_LOCKS_REQUIRED(cs_main, pwalletMain->cs_wallet)
479479
{
480480
AssertLockHeld(cs_main);
481481
AssertLockHeld(pwalletMain->cs_wallet);
@@ -498,7 +498,7 @@ class RecentBeacons
498498
//!
499499
//! \return A pointer to the pending beacon if one exists for the CPID.
500500
//!
501-
const PendingBeacon* Try(const Cpid cpid)
501+
const PendingBeacon* Try(const Cpid cpid) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
502502
{
503503
AssertLockHeld(cs_main);
504504

@@ -531,7 +531,7 @@ class RecentBeacons
531531
//! \param cpid CPID that the beacon was advertised for.
532532
//! \param result Contains the public key if the transaction succeeded.
533533
//!
534-
void Remember(const Cpid cpid, const AdvertiseBeaconResult& result)
534+
void Remember(const Cpid cpid, const AdvertiseBeaconResult& result) EXCLUSIVE_LOCKS_REQUIRED(cs_main, pwalletMain->cs_wallet)
535535
{
536536
AssertLockHeld(cs_main);
537537

src/gridcoin/scraper/scraper.cpp

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ fs::path pathScraper = {};
4444

4545
extern bool fShutdown;
4646
extern CWallet* pwalletMain;
47+
48+
CCriticalSection cs_Scraper;
49+
CCriticalSection cs_StructScraperFileManifest;
50+
CCriticalSection cs_ConvergedScraperStatsCache;
51+
CCriticalSection cs_TeamIDMap;
52+
CCriticalSection cs_VerifiedBeacons;
53+
4754
bool fScraperActive = false;
4855
std::vector<std::pair<std::string, std::string>> vuserpass;
4956
std::vector<std::pair<std::string, int64_t>> vprojectteamids;
@@ -84,20 +91,20 @@ struct ScraperFileManifest
8491
// Both TeamIDMap and ProjTeamETags are protected by cs_TeamIDMap.
8592
// --------------- project -------------team name -- teamID
8693
typedef std::map<std::string, std::map<std::string, int64_t>> mTeamIDs;
87-
mTeamIDs TeamIDMap;
94+
mTeamIDs TeamIDMap GUARDED_BY(cs_TeamIDMap);
8895

8996
// ProjTeamETags is not persisted to disk. There would be little to be gained by doing so. The scrapers are restarted very
9097
// rarely, and on restart, this would only save downloading team files for those projects that have one or TeamIDs missing AND
9198
// an ETag had NOT changed since the last pull. Not worth the complexity.
9299
// --------------- project ---- eTag
93100
typedef std::map<std::string, std::string> mProjectTeamETags;
94-
mProjectTeamETags ProjTeamETags;
101+
mProjectTeamETags ProjTeamETags GUARDED_BY(cs_TeamIDMap);
95102

96103

97104
std::vector<std::string> GetTeamWhiteList();
98105

99106
std::string urlsanity(const std::string& s, const std::string& type);
100-
ScraperFileManifest StructScraperFileManifest = {};
107+
ScraperFileManifest StructScraperFileManifest GUARDED_BY(cs_StructScraperFileManifest) = {};
101108

102109
// Although scraper_net.h declares these maps, we define them here instead of
103110
// in scraper_net.cpp to ensure that the executable destroys these objects in
@@ -109,13 +116,7 @@ std::map<uint256, CSplitBlob::CPart> CSplitBlob::mapParts;
109116
std::map<uint256, std::shared_ptr<CScraperManifest>> CScraperManifest::mapManifest;
110117

111118
// Global cache for converged scraper stats. Access must be with the lock cs_ConvergedScraperStatsCache taken.
112-
ConvergedScraperStats ConvergedScraperStatsCache = {};
113-
114-
CCriticalSection cs_Scraper;
115-
CCriticalSection cs_StructScraperFileManifest;
116-
CCriticalSection cs_ConvergedScraperStatsCache;
117-
CCriticalSection cs_TeamIDMap;
118-
CCriticalSection cs_VerifiedBeacons;
119+
ConvergedScraperStats ConvergedScraperStatsCache GUARDED_BY(cs_ConvergedScraperStatsCache) = {};
119120

120121
void _log(logattribute eType, const std::string& sCall, const std::string& sMessage);
121122

@@ -176,17 +177,17 @@ extern UniValue SuperblockToJson(const Superblock& superblock);
176177
namespace {
177178
//!
178179
//! \brief Get the block index for the height to consider beacons eligible for
179-
//! rewards.
180+
//! rewards. A lock must be held on cs_main before calling this function.
180181
//!
181182
//! \return Block index for the height of a block about 1 hour below the chain
182183
//! tip.
183184
//!
184-
const CBlockIndex* GetBeaconConsensusHeight()
185+
const CBlockIndex* GetBeaconConsensusHeight() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
185186
{
186-
static BlockFinder block_finder;
187-
188187
AssertLockHeld(cs_main);
189188

189+
static BlockFinder block_finder;
190+
190191
// Use 4 times the BLOCK_GRANULARITY which moves the consensus block every hour.
191192
// TODO: Make the mod a function of SCRAPER_CMANIFEST_RETENTION_TIME in scraper.h.
192193
return block_finder.FindByHeight(
@@ -280,7 +281,7 @@ BeaconConsensus GetConsensusBeaconList()
280281
// the only one to survive, which is fine. We only need one.
281282

282283
// This map has to be global because the scraper function is reentrant.
283-
ScraperVerifiedBeacons g_verified_beacons;
284+
ScraperVerifiedBeacons g_verified_beacons GUARDED_BY(cs_VerifiedBeacons);
284285

285286
// Use of this global should be protected by a lock on cs_VerifiedBeacons
286287
ScraperVerifiedBeacons& GetVerifiedBeacons()
@@ -1863,7 +1864,7 @@ bool DownloadProjectTeamFiles(const WhitelistSnapshot& projectWhitelist)
18631864

18641865
// Note this should be called with a lock held on cs_TeamIDMap, which is intended to protect both
18651866
// TeamIDMap and ProjTeamETags.
1866-
bool ProcessProjectTeamFile(const std::string& project, const fs::path& file, const std::string& etag)
1867+
bool ProcessProjectTeamFile(const std::string& project, const fs::path& file, const std::string& etag) EXCLUSIVE_LOCKS_REQUIRED(cs_TeamIDMap)
18671868
{
18681869
std::map<std::string, int64_t> mTeamIdsForProject;
18691870

@@ -2448,7 +2449,7 @@ uint256 GetFileHash(const fs::path& inputfile)
24482449

24492450

24502451
// Note that cs_StructScraperFileManifest needs to be taken before calling.
2451-
uint256 GetmScraperFileManifestHash()
2452+
uint256 GetmScraperFileManifestHash() EXCLUSIVE_LOCKS_REQUIRED(cs_StructScraperFileManifest)
24522453
{
24532454
uint256 nHash;
24542455
CDataStream ss(SER_NETWORK, 1);
@@ -2752,7 +2753,7 @@ bool StoreTeamIDList(const fs::path& file)
27522753

27532754

27542755
// Insert entry into Manifest. Note that cs_StructScraperFileManifest needs to be taken before calling.
2755-
bool InsertScraperFileManifestEntry(ScraperFileManifestEntry& entry)
2756+
bool InsertScraperFileManifestEntry(ScraperFileManifestEntry& entry) EXCLUSIVE_LOCKS_REQUIRED(cs_StructScraperFileManifest)
27562757
{
27572758
// This less readable form is so we know whether the element already existed or not.
27582759
std::pair<ScraperFileManifestMap::iterator, bool> ret;
@@ -2773,7 +2774,7 @@ bool InsertScraperFileManifestEntry(ScraperFileManifestEntry& entry)
27732774
}
27742775

27752776
// Delete entry from Manifest and corresponding file if it exists. Note that cs_StructScraperFileManifest needs to be taken before calling.
2776-
unsigned int DeleteScraperFileManifestEntry(ScraperFileManifestEntry& entry)
2777+
unsigned int DeleteScraperFileManifestEntry(ScraperFileManifestEntry& entry) EXCLUSIVE_LOCKS_REQUIRED(cs_StructScraperFileManifest)
27772778
{
27782779
unsigned int ret;
27792780

@@ -2800,7 +2801,7 @@ unsigned int DeleteScraperFileManifestEntry(ScraperFileManifestEntry& entry)
28002801
// Mark manifest entry non-current. The reason this is encapsulated in a function is
28012802
// to ensure the rehash is done. Note that cs_StructScraperFileManifest needs to be
28022803
// taken before calling.
2803-
bool MarkScraperFileManifestEntryNonCurrent(ScraperFileManifestEntry& entry)
2804+
bool MarkScraperFileManifestEntryNonCurrent(ScraperFileManifestEntry& entry) EXCLUSIVE_LOCKS_REQUIRED(cs_StructScraperFileManifest)
28042805
{
28052806
entry.current = false;
28062807

@@ -4063,7 +4064,7 @@ unsigned int ScraperDeleteUnauthorizedCScraperManifests()
40634064

40644065
// A lock needs to be taken on cs_StructScraperFileManifest for this function.
40654066
// The sCManifestName is the public key of the scraper in address form.
4066-
bool ScraperSendFileManifestContents(CBitcoinAddress& Address, CKey& Key)
4067+
bool ScraperSendFileManifestContents(CBitcoinAddress& Address, CKey& Key) EXCLUSIVE_LOCKS_REQUIRED(cs_StructScraperFileManifest)
40674068
{
40684069
// This "broadcasts" the current ScraperFileManifest contents to the network.
40694070

@@ -5303,8 +5304,13 @@ UniValue sendscraperfilemanifest(const UniValue& params, bool fHelp)
53035304
bool ret;
53045305
if (IsScraperAuthorizedToBroadcastManifests(AddressOut, KeyOut))
53055306
{
5307+
LOCK(cs_StructScraperFileManifest);
5308+
_log(logattribute::INFO, "LOCK", "cs_StructScraperFileManifest");
5309+
53065310
ret = ScraperSendFileManifestContents(AddressOut, KeyOut);
53075311
uiInterface.NotifyScraperEvent(scrapereventtypes::Manifest, CT_NEW, {});
5312+
5313+
_log(logattribute::INFO, "ENDLOCK", "cs_StructScraperFileManifest");
53085314
}
53095315
else
53105316
ret = false;
@@ -5525,6 +5531,8 @@ UniValue testnewsb(const UniValue& params, bool fHelp)
55255531

55265532
if (params.size() == 1) nReducedCacheBits = params[0].get_int();
55275533

5534+
UniValue res(UniValue::VOBJ);
5535+
55285536
{
55295537
LOCK(cs_ConvergedScraperStatsCache);
55305538

@@ -5535,12 +5543,10 @@ UniValue testnewsb(const UniValue& params, bool fHelp)
55355543

55365544
return error;
55375545
}
5538-
}
55395546

5540-
UniValue res(UniValue::VOBJ);
5541-
5542-
_log(logattribute::INFO, "testnewsb", "Size of the PastConvergences map = " + ToString(ConvergedScraperStatsCache.PastConvergences.size()));
5543-
res.pushKV("Size of the PastConvergences map", ToString(ConvergedScraperStatsCache.PastConvergences.size()));
5547+
_log(logattribute::INFO, "testnewsb", "Size of the PastConvergences map = " + ToString(ConvergedScraperStatsCache.PastConvergences.size()));
5548+
res.pushKV("Size of the PastConvergences map", ToString(ConvergedScraperStatsCache.PastConvergences.size()));
5549+
}
55445550

55455551
// Contract binary pack/unpack check...
55465552
_log(logattribute::INFO, "testnewsb", "Checking compatibility with binary SB pack/unpack by packing then unpacking, then comparing to the original");

src/gridcoin/superblock.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ extern std::vector<uint160> GetVerifiedBeaconIDs(const ScraperPendingBeaconMap&
2222
extern ScraperStatsAndVerifiedBeacons GetScraperStatsByConvergedManifest(const ConvergedManifest& StructConvergedManifest);
2323

2424
class CBlockIndex;
25-
class ConvergedScraperStats; // Forward for Superblock
25+
struct ConvergedScraperStats; // Forward for Superblock
2626

2727
namespace GRC {
2828
class Superblock; // Forward for QuorumHash

0 commit comments

Comments
 (0)