Skip to content

Commit e05c215

Browse files
MarcoFalkePastaPastaPasta
authored andcommitted
Merge bitcoin#22769: fuzz: Use LIMITED_WHILE instead of limit_max_ops
faa5fa9 fuzz: Use LIMITED_WHILE instead of limit_max_ops (MarcoFalke) Pull request description: This avoids the local stack variable `limit_max_ops` and makes it easier to grep for limited loops. Also, it is less code. ACKs for top commit: theStack: Code-review ACK faa5fa9 🍷 Zero-1729: crACK faa5fa9 🥤 Tree-SHA512: b10d89f4ce57bac0d6e9de9db7d4db85bae81bc02536d46a910be8c0e77333f2d9a547c7fe03df57f5ff9fd90b6994b09996d8e148573fb7ecd840d08b5c0c7d
1 parent 81b53a4 commit e05c215

File tree

6 files changed

+19
-30
lines changed

6 files changed

+19
-30
lines changed

src/test/fuzz/banman.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,6 @@ static bool operator==(const CBanEntry& lhs, const CBanEntry& rhs)
4040

4141
FUZZ_TARGET_INIT(banman, initialize_banman)
4242
{
43-
// The complexity is O(N^2), where N is the input size, because each call
44-
// might call DumpBanlist (or other methods that are at least linear
45-
// complexity of the input size).
46-
int limit_max_ops{300};
4743
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
4844
SetMockTime(ConsumeTime(fuzzed_data_provider));
4945
fs::path banlist_file = GetDataDir() / "fuzzed_banlist";
@@ -62,7 +58,11 @@ FUZZ_TARGET_INIT(banman, initialize_banman)
6258

6359
{
6460
BanMan ban_man{banlist_file, /* client_interface */ nullptr, /* default_ban_time */ ConsumeBanTimeOffset(fuzzed_data_provider)};
65-
while (--limit_max_ops >= 0 && fuzzed_data_provider.ConsumeBool()) {
61+
// The complexity is O(N^2), where N is the input size, because each call
62+
// might call DumpBanlist (or other methods that are at least linear
63+
// complexity of the input size).
64+
LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 300)
65+
{
6666
CallOneOf(
6767
fuzzed_data_provider,
6868
[&] {

src/test/fuzz/crypto.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@
1919

2020
FUZZ_TARGET(crypto)
2121
{
22-
// Hashing is expensive with sanitizers enabled, so limit the number of
23-
// calls
24-
int limit_max_ops{30};
25-
2622
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
2723
std::vector<uint8_t> data = ConsumeRandomLengthByteVector(fuzzed_data_provider);
2824
if (data.empty()) {
@@ -40,7 +36,8 @@ FUZZ_TARGET(crypto)
4036
SHA3_256 sha3;
4137
CSipHasher sip_hasher{fuzzed_data_provider.ConsumeIntegral<uint64_t>(), fuzzed_data_provider.ConsumeIntegral<uint64_t>()};
4238

43-
while (--limit_max_ops >= 0 && fuzzed_data_provider.ConsumeBool()) {
39+
LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 30)
40+
{
4441
CallOneOf(
4542
fuzzed_data_provider,
4643
[&] {

src/test/fuzz/fuzz.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
#include <functional>
1212
#include <string_view>
1313

14+
/**
15+
* Can be used to limit a theoretically unbounded loop. This caps the runtime
16+
* to avoid timeouts or OOMs.
17+
*/
1418
#define LIMITED_WHILE(condition, limit) \
1519
for (unsigned _count{limit}; (condition) && _count; --_count)
1620

src/test/fuzz/prevector.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,14 +206,11 @@ class prevector_tester
206206

207207
FUZZ_TARGET(prevector)
208208
{
209-
// Pick an arbitrary upper bound to limit the runtime and avoid timeouts on
210-
// inputs.
211-
int limit_max_ops{3000};
212-
213209
FuzzedDataProvider prov(buffer.data(), buffer.size());
214210
prevector_tester<8, int> test;
215211

216-
while (--limit_max_ops >= 0 && prov.remaining_bytes()) {
212+
LIMITED_WHILE(prov.remaining_bytes(), 3000)
213+
{
217214
switch (prov.ConsumeIntegralInRange<int>(0, 13 + 3 * (test.size() > 0))) {
218215
case 0:
219216
test.insert(prov.ConsumeIntegralInRange<size_t>(0, test.size()), prov.ConsumeIntegral<int>());

src/test/fuzz/rolling_bloom_filter.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,13 @@
1515

1616
FUZZ_TARGET(rolling_bloom_filter)
1717
{
18-
// Pick an arbitrary upper bound to limit the runtime and avoid timeouts on
19-
// inputs.
20-
int limit_max_ops{3000};
21-
2218
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
2319

2420
CRollingBloomFilter rolling_bloom_filter{
2521
fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(1, 1000),
2622
0.999 / fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(1, std::numeric_limits<unsigned int>::max())};
27-
while (--limit_max_ops >= 0 && fuzzed_data_provider.remaining_bytes() > 0) {
23+
LIMITED_WHILE(fuzzed_data_provider.remaining_bytes() > 0, 3000)
24+
{
2825
CallOneOf(
2926
fuzzed_data_provider,
3027
[&] {

src/test/fuzz/tx_pool.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,6 @@ void SetMempoolConstraints(ArgsManager& args, FuzzedDataProvider& fuzzed_data_pr
7878

7979
FUZZ_TARGET_INIT(tx_pool_standard, initialize_tx_pool)
8080
{
81-
// Pick an arbitrary upper bound to limit the runtime and avoid timeouts on
82-
// inputs.
83-
int limit_max_ops{300};
84-
8581
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
8682
const auto& node = g_setup->m_node;
8783
auto& chainstate = node.chainman->ActiveChainstate();
@@ -112,7 +108,8 @@ FUZZ_TARGET_INIT(tx_pool_standard, initialize_tx_pool)
112108
return c.out.nValue;
113109
};
114110

115-
while (--limit_max_ops >= 0 && fuzzed_data_provider.ConsumeBool()) {
111+
LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 300)
112+
{
116113
{
117114
// Total supply is all outpoints
118115
CAmount supply_now{0};
@@ -263,10 +260,6 @@ FUZZ_TARGET_INIT(tx_pool_standard, initialize_tx_pool)
263260

264261
FUZZ_TARGET_INIT(tx_pool, initialize_tx_pool)
265262
{
266-
// Pick an arbitrary upper bound to limit the runtime and avoid timeouts on
267-
// inputs.
268-
int limit_max_ops{300};
269-
270263
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
271264
const auto& node = g_setup->m_node;
272265

@@ -282,7 +275,8 @@ FUZZ_TARGET_INIT(tx_pool, initialize_tx_pool)
282275

283276
CTxMemPool tx_pool{/* estimator */ nullptr, /* check_ratio */ 1};
284277

285-
while (--limit_max_ops >= 0 && fuzzed_data_provider.ConsumeBool()) {
278+
LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 300)
279+
{
286280
const auto mut_tx = ConsumeTransaction(fuzzed_data_provider, txids);
287281

288282
const auto tx = MakeTransactionRef(mut_tx);

0 commit comments

Comments
 (0)