Skip to content

Commit 3e9f6c8

Browse files
Add missing locks and locking annotations for CAddrMan
1 parent 3315007 commit 3e9f6c8

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
@@ -187,36 +187,37 @@ class CAddrInfo : public CAddress
187187
*/
188188
class CAddrMan
189189
{
190-
private:
190+
protected:
191191
//! critical section to protect the inner data structures
192192
mutable CCriticalSection cs;
193193

194+
private:
194195
//! last used nId
195-
int nIdCount;
196+
int nIdCount GUARDED_BY(cs);
196197

197198
//! table with information about all nIds
198-
std::map<int, CAddrInfo> mapInfo;
199+
std::map<int, CAddrInfo> mapInfo GUARDED_BY(cs);
199200

200201
//! find an nId based on its network address
201-
std::map<CNetAddr, int> mapAddr;
202+
std::map<CNetAddr, int> mapAddr GUARDED_BY(cs);
202203

203204
//! randomly-ordered vector of all nIds
204-
std::vector<int> vRandom;
205+
std::vector<int> vRandom GUARDED_BY(cs);
205206

206207
// number of "tried" entries
207-
int nTried;
208+
int nTried GUARDED_BY(cs);
208209

209210
//! list of "tried" buckets
210-
int vvTried[ADDRMAN_TRIED_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE];
211+
int vvTried[ADDRMAN_TRIED_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE] GUARDED_BY(cs);
211212

212213
//! number of (unique) "new" entries
213-
int nNew;
214+
int nNew GUARDED_BY(cs);
214215

215216
//! list of "new" buckets
216-
int vvNew[ADDRMAN_NEW_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE];
217+
int vvNew[ADDRMAN_NEW_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE] GUARDED_BY(cs);
217218

218219
//! last time Good was called (memory only)
219-
int64_t nLastGood;
220+
int64_t nLastGood GUARDED_BY(cs);
220221

221222
//! Holds addrs inserted into tried table that collide with existing entries. Test-before-evict discipline used to resolve these collisions.
222223
std::set<int> m_tried_collisions;
@@ -229,58 +230,58 @@ class CAddrMan
229230
FastRandomContext insecure_rand;
230231

231232
//! Find an entry.
232-
CAddrInfo* Find(const CNetAddr& addr, int *pnId = nullptr);
233+
CAddrInfo* Find(const CNetAddr& addr, int *pnId = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs);
233234

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

238239
//! Swap two elements in vRandom.
239-
void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2);
240+
void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2) EXCLUSIVE_LOCKS_REQUIRED(cs);
240241

241242
//! Move an entry from the "new" table(s) to the "tried" table
242-
void MakeTried(CAddrInfo& info, int nId);
243+
void MakeTried(CAddrInfo& info, int nId) EXCLUSIVE_LOCKS_REQUIRED(cs);
243244

244245
//! Delete an entry. It must not be in tried, and have refcount 0.
245-
void Delete(int nId);
246+
void Delete(int nId) EXCLUSIVE_LOCKS_REQUIRED(cs);
246247

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

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

253254
//! Add an entry to the "new" table.
254-
bool Add_(const CAddress &addr, const CNetAddr& source, int64_t nTimePenalty);
255+
bool Add_(const CAddress &addr, const CNetAddr& source, int64_t nTimePenalty) EXCLUSIVE_LOCKS_REQUIRED(cs);
255256

256257
//! Mark an entry as attempted to connect.
257-
void Attempt_(const CService &addr, bool fCountFailure, int64_t nTime);
258+
void Attempt_(const CService &addr, bool fCountFailure, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(cs);
258259

259260
//! Select an address to connect to, if newOnly is set to true, only the new table is selected from.
260-
CAddrInfo Select_(bool newOnly);
261+
CAddrInfo Select_(bool newOnly) EXCLUSIVE_LOCKS_REQUIRED(cs);
261262

262263
//! See if any to-be-evicted tried table entries have been tested and if so resolve the collisions.
263-
void ResolveCollisions_();
264+
void ResolveCollisions_() EXCLUSIVE_LOCKS_REQUIRED(cs);
264265

265266
//! Return a random to-be-evicted tried table address.
266-
CAddrInfo SelectTriedCollision_();
267+
CAddrInfo SelectTriedCollision_() EXCLUSIVE_LOCKS_REQUIRED(cs);
267268

268269
//! Wraps GetRandInt to allow tests to override RandomInt and make it determinismistic.
269270
virtual int RandomInt(int nMax);
270271

271272
#ifdef DEBUG_ADDRMAN
272273
//! Perform consistency check. Returns an error code or zero.
273-
int Check_();
274+
int Check_() EXCLUSIVE_LOCKS_REQUIRED(cs);
274275
#endif
275276

276277
//! Select several addresses at once.
277-
void GetAddr_(std::vector<CAddress> &vAddr);
278+
void GetAddr_(std::vector<CAddress> &vAddr) EXCLUSIVE_LOCKS_REQUIRED(cs);
278279

279280
//! Mark an entry as currently-connected-to.
280-
void Connected_(const CService &addr, int64_t nTime);
281+
void Connected_(const CService &addr, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(cs);
281282

282283
//! Update an entry's service bits.
283-
void SetServices_(const CService &addr, ServiceFlags nServices);
284+
void SetServices_(const CService &addr, ServiceFlags nServices) EXCLUSIVE_LOCKS_REQUIRED(cs);
284285

285286
public:
286287
/**

src/test/addrman_tests.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,26 @@ class CAddrManTest : public CAddrMan
4040

4141
CAddrInfo* Find(const CNetAddr& addr, int* pnId = nullptr)
4242
{
43+
LOCK(cs);
4344
return CAddrMan::Find(addr, pnId);
4445
}
4546

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

5153
void Delete(int nId)
5254
{
55+
LOCK(cs);
5356
CAddrMan::Delete(nId);
5457
}
5558

5659
// Simulates connection failure so that we can test eviction of offline nodes
5760
void SimConnFail(CService& addr)
5861
{
62+
LOCK(cs);
5963
int64_t nLastSuccess = 1;
6064
Good_(addr, true, nLastSuccess); // Set last good connection in the deep past.
6165

0 commit comments

Comments
 (0)