Skip to content

Commit 123888d

Browse files
committed
Merge bitcoin/bitcoin#30447: fuzz: Deglobalize signature cache in sigcache test
fae0db0 fuzz: Deglobalize signature cache in sigcache test (TheCharlatan) Pull request description: The body of the fuzz test should ideally be a pure function. If data is persisted in the cache over many iterations, and there is a crash, reproducing it from the input might be difficult. Solve this by getting rid of the global state. This is a follow-up from #30425. ACKs for top commit: dergoegge: utACK fae0db0 ryanofsky: Code review ACK fae0db0 Tree-SHA512: 93dcbb9f2497f13856970469042d6870f04de10fe206827a8db1aae7fc8f3ac7fd900bee7945b5fe4c9e33883268dabb15be7e7bc91cf353ffc0d118cd60e97d
2 parents 37bd70a + fae0db0 commit 123888d

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

src/test/fuzz/script_sigcache.cpp

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,41 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

5-
#include <chainparams.h>
6-
#include <key.h>
5+
#include <consensus/amount.h>
6+
#include <primitives/transaction.h>
77
#include <pubkey.h>
8+
#include <script/interpreter.h>
89
#include <script/sigcache.h>
10+
#include <span.h>
911
#include <test/fuzz/FuzzedDataProvider.h>
1012
#include <test/fuzz/fuzz.h>
1113
#include <test/fuzz/util.h>
1214
#include <test/util/setup_common.h>
15+
#include <uint256.h>
1316

14-
#include <cstdint>
17+
#include <cstddef>
1518
#include <optional>
16-
#include <string>
1719
#include <vector>
1820

19-
namespace {
20-
const BasicTestingSetup* g_setup;
21-
SignatureCache* g_signature_cache;
22-
} // namespace
23-
2421
void initialize_script_sigcache()
2522
{
2623
static const auto testing_setup = MakeNoLogFileContext<>();
27-
static SignatureCache signature_cache{DEFAULT_SIGNATURE_CACHE_BYTES};
28-
g_setup = testing_setup.get();
29-
g_signature_cache = &signature_cache;
3024
}
3125

3226
FUZZ_TARGET(script_sigcache, .init = initialize_script_sigcache)
3327
{
3428
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
3529

30+
const auto max_sigcache_bytes{fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, DEFAULT_SIGNATURE_CACHE_BYTES)};
31+
SignatureCache signature_cache{max_sigcache_bytes};
32+
3633
const std::optional<CMutableTransaction> mutable_transaction = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
3734
const CTransaction tx{mutable_transaction ? *mutable_transaction : CMutableTransaction{}};
3835
const unsigned int n_in = fuzzed_data_provider.ConsumeIntegral<unsigned int>();
3936
const CAmount amount = ConsumeMoney(fuzzed_data_provider);
4037
const bool store = fuzzed_data_provider.ConsumeBool();
4138
PrecomputedTransactionData tx_data;
42-
CachingTransactionSignatureChecker caching_transaction_signature_checker{mutable_transaction ? &tx : nullptr, n_in, amount, store, *g_signature_cache, tx_data};
39+
CachingTransactionSignatureChecker caching_transaction_signature_checker{mutable_transaction ? &tx : nullptr, n_in, amount, store, signature_cache, tx_data};
4340
if (fuzzed_data_provider.ConsumeBool()) {
4441
const auto random_bytes = fuzzed_data_provider.ConsumeBytes<unsigned char>(64);
4542
const XOnlyPubKey pub_key(ConsumeUInt256(fuzzed_data_provider));

0 commit comments

Comments
 (0)