Skip to content

Commit dbbd0a2

Browse files
MarcoFalkegades
authored andcommitted
Merge bitcoin#13115: addrman: Add Clang thread safety annotations for variables guarded by CAddrMan.cs
3e9f6c8 Add missing locks and locking annotations for CAddrMan (practicalswift) Pull request description: * Add Clang thread safety annotations for variables guarded by `CAddrMan.cs ` * Add missing `CAddrMan.cs ` locks Tree-SHA512: c78d56d56eb63a4469333c04c95317545a8f97d5e3a36ff2699ee4a91a6433d416221eed6c5ff168e1e31f6936c2ae101a4c60b635f2b2309f40e3d66a727322
1 parent 010ec27 commit dbbd0a2

File tree

2 files changed

+31
-26
lines changed

2 files changed

+31
-26
lines changed

src/addrman.h

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -177,39 +177,40 @@ static const int64_t ADDRMAN_TEST_WINDOW = 40*60; // 40 minutes
177177
*/
178178
class CAddrMan
179179
{
180-
private:
180+
protected:
181181
friend class CAddrManTest;
182182

183183
protected:
184184
//! critical section to protect the inner data structures
185185
mutable CCriticalSection cs;
186186

187+
private:
187188
//! last used nId
188-
int nIdCount;
189+
int nIdCount GUARDED_BY(cs);
189190

190191
//! table with information about all nIds
191-
std::map<int, CAddrInfo> mapInfo;
192+
std::map<int, CAddrInfo> mapInfo GUARDED_BY(cs);
192193

193194
//! find an nId based on its network address
194-
std::map<CService, int> mapAddr;
195+
std::map<CService, int> mapAddr GUARDED_BY(cs);
195196

196197
//! randomly-ordered vector of all nIds
197-
std::vector<int> vRandom;
198+
std::vector<int> vRandom GUARDED_BY(cs);
198199

199200
// number of "tried" entries
200-
int nTried;
201+
int nTried GUARDED_BY(cs);
201202

202203
//! list of "tried" buckets
203-
int vvTried[ADDRMAN_TRIED_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE];
204+
int vvTried[ADDRMAN_TRIED_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE] GUARDED_BY(cs);
204205

205206
//! number of (unique) "new" entries
206-
int nNew;
207+
int nNew GUARDED_BY(cs);
207208

208209
//! list of "new" buckets
209-
int vvNew[ADDRMAN_NEW_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE];
210+
int vvNew[ADDRMAN_NEW_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE] GUARDED_BY(cs);
210211

211212
//! last time Good was called (memory only)
212-
int64_t nLastGood;
213+
int64_t nLastGood GUARDED_BY(cs);
213214

214215
// discriminate entries based on port. Should be false on mainnet/testnet and can be true on devnet/regtest
215216
bool discriminatePorts;
@@ -225,55 +226,55 @@ class CAddrMan
225226
FastRandomContext insecure_rand;
226227

227228
//! Find an entry.
228-
CAddrInfo* Find(const CService& addr, int *pnId = nullptr);
229+
CAddrInfo* Find(const CService& addr, int *pnId = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs);
229230

230231
//! find an entry, creating it if necessary.
231232
//! nTime and nServices of the found node are updated, if necessary.
232-
CAddrInfo* Create(const CAddress &addr, const CNetAddr &addrSource, int *pnId = nullptr);
233+
CAddrInfo* Create(const CAddress &addr, const CNetAddr &addrSource, int *pnId = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs);
233234

234235
//! Swap two elements in vRandom.
235-
void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2);
236+
void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2) EXCLUSIVE_LOCKS_REQUIRED(cs);
236237

237238
//! Move an entry from the "new" table(s) to the "tried" table
238-
void MakeTried(CAddrInfo& info, int nId);
239+
void MakeTried(CAddrInfo& info, int nId) EXCLUSIVE_LOCKS_REQUIRED(cs);
239240

240241
//! Delete an entry. It must not be in tried, and have refcount 0.
241-
void Delete(int nId);
242+
void Delete(int nId) EXCLUSIVE_LOCKS_REQUIRED(cs);
242243

243244
//! Clear a position in a "new" table. This is the only place where entries are actually deleted.
244-
void ClearNew(int nUBucket, int nUBucketPos);
245+
void ClearNew(int nUBucket, int nUBucketPos) EXCLUSIVE_LOCKS_REQUIRED(cs);
245246

246247
//! Mark an entry "good", possibly moving it from "new" to "tried".
247-
void Good_(const CService &addr, bool test_before_evict, int64_t time);
248+
void Good_(const CService &addr, bool test_before_evict, int64_t time) EXCLUSIVE_LOCKS_REQUIRED(cs);
248249

249250
//! Add an entry to the "new" table.
250-
bool Add_(const CAddress &addr, const CNetAddr& source, int64_t nTimePenalty);
251+
bool Add_(const CAddress &addr, const CNetAddr& source, int64_t nTimePenalty) EXCLUSIVE_LOCKS_REQUIRED(cs);
251252

252253
//! Mark an entry as attempted to connect.
253-
void Attempt_(const CService &addr, bool fCountFailure, int64_t nTime);
254+
void Attempt_(const CService &addr, bool fCountFailure, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(cs);
254255

255256
//! Select an address to connect to, if newOnly is set to true, only the new table is selected from.
256-
CAddrInfo Select_(bool newOnly);
257+
CAddrInfo Select_(bool newOnly) EXCLUSIVE_LOCKS_REQUIRED(cs);
257258

258259
//! See if any to-be-evicted tried table entries have been tested and if so resolve the collisions.
259-
void ResolveCollisions_();
260+
void ResolveCollisions_() EXCLUSIVE_LOCKS_REQUIRED(cs);
260261

261262
//! Return a random to-be-evicted tried table address.
262-
CAddrInfo SelectTriedCollision_();
263+
CAddrInfo SelectTriedCollision_() EXCLUSIVE_LOCKS_REQUIRED(cs);
263264

264265
#ifdef DEBUG_ADDRMAN
265266
//! Perform consistency check. Returns an error code or zero.
266-
int Check_();
267+
int Check_() EXCLUSIVE_LOCKS_REQUIRED(cs);
267268
#endif
268269

269270
//! Select several addresses at once.
270-
void GetAddr_(std::vector<CAddress> &vAddr);
271+
void GetAddr_(std::vector<CAddress> &vAddr) EXCLUSIVE_LOCKS_REQUIRED(cs);
271272

272273
//! Mark an entry as currently-connected-to.
273-
void Connected_(const CService &addr, int64_t nTime);
274+
void Connected_(const CService &addr, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(cs);
274275

275276
//! Update an entry's service bits.
276-
void SetServices_(const CService &addr, ServiceFlags nServices);
277+
void SetServices_(const CService &addr, ServiceFlags nServices) EXCLUSIVE_LOCKS_REQUIRED(cs);
277278

278279
//! Get address info for address
279280
CAddrInfo GetAddressInfo_(const CService& addr);

src/test/addrman_tests.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,19 @@ class CAddrManTest : public CAddrMan
3939

4040
CAddrInfo* Find(const CService& addr, int* pnId = nullptr)
4141
{
42+
LOCK(cs);
4243
return CAddrMan::Find(addr, pnId);
4344
}
4445

4546
CAddrInfo* Create(const CAddress& addr, const CNetAddr& addrSource, int* pnId = nullptr)
4647
{
48+
LOCK(cs);
4749
return CAddrMan::Create(addr, addrSource, pnId);
4850
}
4951

5052
void Delete(int nId)
5153
{
54+
LOCK(cs);
5255
CAddrMan::Delete(nId);
5356
}
5457

@@ -70,6 +73,7 @@ class CAddrManTest : public CAddrMan
7073
// Simulates connection failure so that we can test eviction of offline nodes
7174
void SimConnFail(CService& addr)
7275
{
76+
LOCK(cs);
7377
int64_t nLastSuccess = 1;
7478
Good_(addr, true, nLastSuccess); // Set last good connection in the deep past.
7579

0 commit comments

Comments
 (0)