Skip to content

Commit faa5fa9

Browse files
author
MarcoFalke
committed
fuzz: Use LIMITED_WHILE instead of limit_max_ops
1 parent f5a406f commit faa5fa9

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
@@ -41,10 +41,6 @@ static bool operator==(const CBanEntry& lhs, const CBanEntry& rhs)
4141

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

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

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
@@ -16,16 +16,13 @@
1616

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

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

src/test/fuzz/tx_pool.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,6 @@ void MockTime(FuzzedDataProvider& fuzzed_data_provider, const CChainState& chain
112112

113113
FUZZ_TARGET_INIT(tx_pool_standard, initialize_tx_pool)
114114
{
115-
// Pick an arbitrary upper bound to limit the runtime and avoid timeouts on
116-
// inputs.
117-
int limit_max_ops{300};
118-
119115
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
120116
const auto& node = g_setup->m_node;
121117
auto& chainstate = node.chainman->ActiveChainstate();
@@ -146,7 +142,8 @@ FUZZ_TARGET_INIT(tx_pool_standard, initialize_tx_pool)
146142
return c.out.nValue;
147143
};
148144

149-
while (--limit_max_ops >= 0 && fuzzed_data_provider.ConsumeBool()) {
145+
LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 300)
146+
{
150147
{
151148
// Total supply is the mempool fee + all outpoints
152149
CAmount supply_now{WITH_LOCK(tx_pool.cs, return tx_pool.GetTotalFee())};
@@ -289,10 +286,6 @@ FUZZ_TARGET_INIT(tx_pool_standard, initialize_tx_pool)
289286

290287
FUZZ_TARGET_INIT(tx_pool, initialize_tx_pool)
291288
{
292-
// Pick an arbitrary upper bound to limit the runtime and avoid timeouts on
293-
// inputs.
294-
int limit_max_ops{300};
295-
296289
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
297290
const auto& node = g_setup->m_node;
298291
auto& chainstate = node.chainman->ActiveChainstate();
@@ -313,7 +306,8 @@ FUZZ_TARGET_INIT(tx_pool, initialize_tx_pool)
313306
CTxMemPool tx_pool_{/* estimator */ nullptr, /* check_ratio */ 1};
314307
MockedTxPool& tx_pool = *static_cast<MockedTxPool*>(&tx_pool_);
315308

316-
while (--limit_max_ops >= 0 && fuzzed_data_provider.ConsumeBool()) {
309+
LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 300)
310+
{
317311
const auto mut_tx = ConsumeTransaction(fuzzed_data_provider, txids);
318312

319313
if (fuzzed_data_provider.ConsumeBool()) {

0 commit comments

Comments
 (0)