Skip to content

Commit f1239b7

Browse files
author
MarcoFalke
committed
Merge #21025: validation: Guard chainman chainstates with cs_main
20677ff validation: Guard all chainstates with cs_main (Carl Dong) Pull request description: ``` This avoids a potential race-condition where a thread is reading the ChainstateManager::m_active_chainstate pointer while another one is writing to it. There is no portable guarantee that reading/writing the pointer is thread-safe. This is also done in way that mimics ::ChainstateActive(), so the transition from that function to this method is easy. More discussion: 1. bitcoin/bitcoin#20749 (comment) 2. bitcoin/bitcoin#19806 (comment) 3. bitcoin/bitcoin#19806 (comment) 4. bitcoin/bitcoin#19806 (comment) ``` Basically this PR removes the loaded-but-unfired footgun, which: - Is multiplied (but still unshot) in the chainman deglobalization PRs (#20158) - Is shot in the test framework in the au.activate PR (#19806) ACKs for top commit: jnewbery: code review ACK 20677ff. I've verified by eye that neither of these members are accessed without cs_main. ryanofsky: Code review ACK 20677ff. It is safer to have these new `GUARDED_BY` annotations and locks than not to have them, but in the longer run I think every `LOCK(cs_main)` added here and added earlier in f92dc65 from #20749 should be removed and replaced with `EXCLUSIVE_LOCKS_REQUIRED(cs_main)` on the accessor methods instead. `cs_main` is a high level lock that should be explicitly acquired at a high level to prevent the chain state from changing. It shouldn't be acquired recursively in low-level methods just to read pointer values atomically. Tree-SHA512: 68a3a46d79a407b774eab77e1d682a97e95f1672db0a5fcb877572e188bec09f3a7b47c5d0cc1f2769ea276896dcbe97cb35c861acf7d8e3e513e955dc773f89
2 parents 4e946eb + 20677ff commit f1239b7

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

src/validation.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5159,7 +5159,7 @@ double GuessVerificationProgress(const ChainTxData& data, const CBlockIndex *pin
51595159
}
51605160

51615161
Optional<uint256> ChainstateManager::SnapshotBlockhash() const {
5162-
LOCK(::cs_main); // for m_active_chainstate access
5162+
LOCK(::cs_main);
51635163
if (m_active_chainstate != nullptr) {
51645164
// If a snapshot chainstate exists, it will always be our active.
51655165
return m_active_chainstate->m_from_snapshot_blockhash;
@@ -5169,6 +5169,7 @@ Optional<uint256> ChainstateManager::SnapshotBlockhash() const {
51695169

51705170
std::vector<CChainState*> ChainstateManager::GetAll()
51715171
{
5172+
LOCK(::cs_main);
51725173
std::vector<CChainState*> out;
51735174

51745175
if (!IsSnapshotValidated() && m_ibd_chainstate) {
@@ -5213,11 +5214,13 @@ CChainState& ChainstateManager::ActiveChainstate() const
52135214

52145215
bool ChainstateManager::IsSnapshotActive() const
52155216
{
5216-
return m_snapshot_chainstate && WITH_LOCK(::cs_main, return m_active_chainstate) == m_snapshot_chainstate.get();
5217+
LOCK(::cs_main);
5218+
return m_snapshot_chainstate && m_active_chainstate == m_snapshot_chainstate.get();
52175219
}
52185220

52195221
CChainState& ChainstateManager::ValidatedChainstate() const
52205222
{
5223+
LOCK(::cs_main);
52215224
if (m_snapshot_chainstate && IsSnapshotValidated()) {
52225225
return *m_snapshot_chainstate.get();
52235226
}
@@ -5227,6 +5230,7 @@ CChainState& ChainstateManager::ValidatedChainstate() const
52275230

52285231
bool ChainstateManager::IsBackgroundIBD(CChainState* chainstate) const
52295232
{
5233+
LOCK(::cs_main);
52305234
return (m_snapshot_chainstate && chainstate == m_ibd_chainstate.get());
52315235
}
52325236

@@ -5242,12 +5246,10 @@ void ChainstateManager::Unload()
52425246

52435247
void ChainstateManager::Reset()
52445248
{
5249+
LOCK(::cs_main);
52455250
m_ibd_chainstate.reset();
52465251
m_snapshot_chainstate.reset();
5247-
{
5248-
LOCK(::cs_main);
5249-
m_active_chainstate = nullptr;
5250-
}
5252+
m_active_chainstate = nullptr;
52515253
m_snapshot_validated = false;
52525254
}
52535255

src/validation.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ class ChainstateManager
802802
//! This is especially important when, e.g., calling ActivateBestChain()
803803
//! on all chainstates because we are not able to hold ::cs_main going into
804804
//! that call.
805-
std::unique_ptr<CChainState> m_ibd_chainstate;
805+
std::unique_ptr<CChainState> m_ibd_chainstate GUARDED_BY(::cs_main);
806806

807807
//! A chainstate initialized on the basis of a UTXO snapshot. If this is
808808
//! non-null, it is always our active chainstate.
@@ -815,7 +815,7 @@ class ChainstateManager
815815
//! This is especially important when, e.g., calling ActivateBestChain()
816816
//! on all chainstates because we are not able to hold ::cs_main going into
817817
//! that call.
818-
std::unique_ptr<CChainState> m_snapshot_chainstate;
818+
std::unique_ptr<CChainState> m_snapshot_chainstate GUARDED_BY(::cs_main);
819819

820820
//! Points to either the ibd or snapshot chainstate; indicates our
821821
//! most-work chain.

0 commit comments

Comments
 (0)