Skip to content

Commit f4db464

Browse files
fanquakevijaydasmp
authored andcommitted
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
1 parent bb37b19 commit f4db464

5 files changed

Lines changed: 9 additions & 8 deletions

File tree

src/net_processing.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4762,7 +4762,7 @@ void PeerManagerImpl::ProcessMessage(
47624762

47634763
// DoS prevention: do not allow m_orphans to grow unbounded (see CVE-2012-3789)
47644764
unsigned int nMaxOrphanTxSize = (unsigned int)std::max((int64_t)0, gArgs.GetIntArg("-maxorphantxsize", DEFAULT_MAX_ORPHAN_TRANSACTIONS_SIZE)) * 1000000;
4765-
m_orphanage.LimitOrphans(nMaxOrphanTxSize);
4765+
m_orphanage.LimitOrphans(nMaxOrphanTxSize, m_rng);
47664766
} else {
47674767
LogPrint(BCLog::MEMPOOL, "not keeping orphan with rejected parents %s\n",tx.GetHash().ToString());
47684768
// We will continue to reject this tx since it has rejected

src/test/fuzz/txorphan.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ void initialize_orphanage()
3636
FUZZ_TARGET(txorphan, .init = initialize_orphanage)
3737
{
3838
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
39+
FastRandomContext limit_orphans_rng{/*fDeterministic=*/true};
3940
SetMockTime(ConsumeTime(fuzzed_data_provider));
4041

4142
TxOrphanage orphanage;
@@ -140,7 +141,7 @@ FUZZ_TARGET(txorphan, .init = initialize_orphanage)
140141
// test mocktime and expiry
141142
SetMockTime(ConsumeTime(fuzzed_data_provider));
142143
auto limit = fuzzed_data_provider.ConsumeIntegral<unsigned int>();
143-
orphanage.LimitOrphans(limit);
144+
orphanage.LimitOrphans(limit, limit_orphans_rng);
144145
Assert(orphanage.Size() <= limit);
145146
});
146147
}

src/test/orphanage_tests.cpp

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

128128
// Test LimitOrphanTxSize() function:
129-
orphanage.LimitOrphans(40);
129+
FastRandomContext rng{/*fDeterministic=*/true};
130+
orphanage.LimitOrphans(40, rng);
130131
BOOST_CHECK(orphanage.CountOrphans() <= 40);
131-
orphanage.LimitOrphans(10);
132+
orphanage.LimitOrphans(10, rng);
132133
BOOST_CHECK(orphanage.CountOrphans() <= 10);
133-
orphanage.LimitOrphans(0);
134+
orphanage.LimitOrphans(0, rng);
134135
BOOST_CHECK(orphanage.CountOrphans() == 0);
135136
}
136137

src/txorphanage.cpp

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

119-
void TxOrphanage::LimitOrphans(unsigned int max_orphans_size)
119+
void TxOrphanage::LimitOrphans(unsigned int max_orphans_size, FastRandomContext& rng)
120120
{
121121
LOCK(m_mutex);
122122

@@ -141,7 +141,6 @@ void TxOrphanage::LimitOrphans(unsigned int max_orphans_size)
141141
nNextSweep = nMinExpTime + ORPHAN_TX_EXPIRE_INTERVAL;
142142
if (nErased > 0) LogPrint(BCLog::MEMPOOL, "Erased %d orphan tx due to expiration\n", nErased);
143143
}
144-
FastRandomContext rng;
145144
while (!m_orphans.empty() && m_orphan_tx_size > max_orphans_size)
146145
{
147146
// Evict a random orphan:

src/txorphanage.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class TxOrphanage {
5151
bool HaveMoreWork(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
5252

5353
/** Limit the orphanage to the given maximum */
54-
void LimitOrphans(unsigned int max_orphans_size) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
54+
void LimitOrphans(unsigned int max_orphans_size, FastRandomContext& rng) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
5555

5656
/** Add any orphans that list a particular tx as a parent into a peer's work set */
5757
void AddChildrenToWorkSet(const CTransaction& tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);

0 commit comments

Comments
 (0)