Skip to content

Commit 40bc501

Browse files
committed
Merge bitcoin#29031: fuzz: Improve fuzzing stability for txorphan harness
15f5a0d fuzz: Improve fuzzing stability for txorphan harness (dergoegge) Pull request description: The `txorphan` harness has low stability as eviction of orphan txs is entirely random at the moment. Fix this by passing the rng to `LimitOrphans`, which can be deterministic in tests. Also see bitcoin#29018. ACKs for top commit: maflcko: lgtm ACK 15f5a0d brunoerg: utACK 15f5a0d Tree-SHA512: 854ec34b3a0f16f26db6dc419096c6e7a380e8400119534aa278d6b1d54c253b572aa2fad13c383c796c431d8ff4263956e6f60326e99f8bf6abd16d9a280e97
2 parents ba5f16e + 15f5a0d commit 40bc501

File tree

5 files changed

+9
-8
lines changed

5 files changed

+9
-8
lines changed

src/net_processing.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4298,7 +4298,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
42984298
m_txrequest.ForgetTxHash(tx.GetWitnessHash());
42994299

43004300
// DoS prevention: do not allow m_orphanage to grow unbounded (see CVE-2012-3789)
4301-
m_orphanage.LimitOrphans(m_opts.max_orphan_txs);
4301+
m_orphanage.LimitOrphans(m_opts.max_orphan_txs, m_rng);
43024302
} else {
43034303
LogPrint(BCLog::MEMPOOL, "not keeping orphan with rejected parents %s (wtxid=%s)\n",
43044304
tx.GetHash().ToString(),

src/test/fuzz/txorphan.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ void initialize_orphanage()
3333
FUZZ_TARGET(txorphan, .init = initialize_orphanage)
3434
{
3535
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
36+
FastRandomContext limit_orphans_rng{/*fDeterministic=*/true};
3637
SetMockTime(ConsumeTime(fuzzed_data_provider));
3738

3839
TxOrphanage orphanage;
@@ -132,7 +133,7 @@ FUZZ_TARGET(txorphan, .init = initialize_orphanage)
132133
// test mocktime and expiry
133134
SetMockTime(ConsumeTime(fuzzed_data_provider));
134135
auto limit = fuzzed_data_provider.ConsumeIntegral<unsigned int>();
135-
orphanage.LimitOrphans(limit);
136+
orphanage.LimitOrphans(limit, limit_orphans_rng);
136137
Assert(orphanage.Size() <= limit);
137138
});
138139
}

src/test/orphanage_tests.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,12 @@ BOOST_AUTO_TEST_CASE(DoS_mapOrphans)
129129
}
130130

131131
// Test LimitOrphanTxSize() function:
132-
orphanage.LimitOrphans(40);
132+
FastRandomContext rng{/*fDeterministic=*/true};
133+
orphanage.LimitOrphans(40, rng);
133134
BOOST_CHECK(orphanage.CountOrphans() <= 40);
134-
orphanage.LimitOrphans(10);
135+
orphanage.LimitOrphans(10, rng);
135136
BOOST_CHECK(orphanage.CountOrphans() <= 10);
136-
orphanage.LimitOrphans(0);
137+
orphanage.LimitOrphans(0, rng);
137138
BOOST_CHECK(orphanage.CountOrphans() == 0);
138139
}
139140

src/txorphanage.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ void TxOrphanage::EraseForPeer(NodeId peer)
113113
if (nErased > 0) LogPrint(BCLog::TXPACKAGES, "Erased %d orphan tx from peer=%d\n", nErased, peer);
114114
}
115115

116-
void TxOrphanage::LimitOrphans(unsigned int max_orphans)
116+
void TxOrphanage::LimitOrphans(unsigned int max_orphans, FastRandomContext& rng)
117117
{
118118
LOCK(m_mutex);
119119

@@ -138,7 +138,6 @@ void TxOrphanage::LimitOrphans(unsigned int max_orphans)
138138
nNextSweep = nMinExpTime + ORPHAN_TX_EXPIRE_INTERVAL;
139139
if (nErased > 0) LogPrint(BCLog::TXPACKAGES, "Erased %d orphan tx due to expiration\n", nErased);
140140
}
141-
FastRandomContext rng;
142141
while (m_orphans.size() > max_orphans)
143142
{
144143
// Evict a random orphan:

src/txorphanage.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class TxOrphanage {
4343
void EraseForBlock(const CBlock& block) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
4444

4545
/** Limit the orphanage to the given maximum */
46-
void LimitOrphans(unsigned int max_orphans) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
46+
void LimitOrphans(unsigned int max_orphans, FastRandomContext& rng) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
4747

4848
/** Add any orphans that list a particular tx as a parent into the from peer's work set */
4949
void AddChildrenToWorkSet(const CTransaction& tx) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);;

0 commit comments

Comments
 (0)