diff --git a/src/random.cpp b/src/random.cpp index 9b5131023f7..4265c48022d 100644 --- a/src/random.cpp +++ b/src/random.cpp @@ -671,9 +671,11 @@ void MakeRandDeterministicDANGEROUS(const uint256& seed) noexcept { GetRNGState().MakeDeterministic(seed); } +std::atomic g_used_g_prng{false}; // Only accessed from tests void GetRandBytes(Span bytes) noexcept { + g_used_g_prng = true; ProcRand(bytes.data(), bytes.size(), RNGLevel::FAST, /*always_use_real_rng=*/false); } diff --git a/src/test/fuzz/addrman.cpp b/src/test/fuzz/addrman.cpp index 53dd7215d2e..dedea850803 100644 --- a/src/test/fuzz/addrman.cpp +++ b/src/test/fuzz/addrman.cpp @@ -41,6 +41,7 @@ void initialize_addrman() FUZZ_TARGET(data_stream_addr_man, .init = initialize_addrman) { + SeedRandomStateForTest(SeedRand::ZEROS); FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()}; DataStream data_stream = ConsumeDataStream(fuzzed_data_provider); NetGroupManager netgroupman{ConsumeNetGroupManager(fuzzed_data_provider)}; @@ -113,6 +114,7 @@ void FillAddrman(AddrMan& addrman, FuzzedDataProvider& fuzzed_data_provider) FUZZ_TARGET(addrman, .init = initialize_addrman) { + SeedRandomStateForTest(SeedRand::ZEROS); FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size()); SetMockTime(ConsumeTime(fuzzed_data_provider)); NetGroupManager netgroupman{ConsumeNetGroupManager(fuzzed_data_provider)}; @@ -197,6 +199,7 @@ FUZZ_TARGET(addrman, .init = initialize_addrman) // Check that serialize followed by unserialize produces the same addrman. FUZZ_TARGET(addrman_serdeser, .init = initialize_addrman) { + SeedRandomStateForTest(SeedRand::ZEROS); FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size()); SetMockTime(ConsumeTime(fuzzed_data_provider)); diff --git a/src/test/fuzz/banman.cpp b/src/test/fuzz/banman.cpp index 4165cc6b2cd..bbe8e12b2f9 100644 --- a/src/test/fuzz/banman.cpp +++ b/src/test/fuzz/banman.cpp @@ -42,6 +42,7 @@ static bool operator==(const CBanEntry& lhs, const CBanEntry& rhs) FUZZ_TARGET(banman, .init = initialize_banman) { + SeedRandomStateForTest(SeedRand::ZEROS); FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()}; SetMockTime(ConsumeTime(fuzzed_data_provider)); fs::path banlist_file = gArgs.GetDataDirNet() / "fuzzed_banlist"; diff --git a/src/test/fuzz/blockfilter.cpp b/src/test/fuzz/blockfilter.cpp index 3adc1145151..17e99ac94d0 100644 --- a/src/test/fuzz/blockfilter.cpp +++ b/src/test/fuzz/blockfilter.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -14,6 +15,7 @@ FUZZ_TARGET(blockfilter) { + SeedRandomStateForTest(SeedRand::ZEROS); FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size()); const std::optional block_filter = ConsumeDeserializable(fuzzed_data_provider); if (!block_filter) { diff --git a/src/test/fuzz/connman.cpp b/src/test/fuzz/connman.cpp index 20a60b098f5..a62d227da8e 100644 --- a/src/test/fuzz/connman.cpp +++ b/src/test/fuzz/connman.cpp @@ -36,6 +36,7 @@ void initialize_connman() FUZZ_TARGET(connman, .init = initialize_connman) { + SeedRandomStateForTest(SeedRand::ZEROS); FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()}; SetMockTime(ConsumeTime(fuzzed_data_provider)); auto netgroupman{ConsumeNetGroupManager(fuzzed_data_provider)}; diff --git a/src/test/fuzz/fuzz.cpp b/src/test/fuzz/fuzz.cpp index 3c81e0f3fe8..e4e4723c740 100644 --- a/src/test/fuzz/fuzz.cpp +++ b/src/test/fuzz/fuzz.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -78,6 +79,12 @@ void FuzzFrameworkRegisterTarget(std::string_view name, TypeTestOneInput target, static std::string_view g_fuzz_target; static const TypeTestOneInput* g_test_one_input{nullptr}; +inline void test_one_input(FuzzBufferType buffer) +{ + CheckGlobals check{}; + (*Assert(g_test_one_input))(buffer); +} + const std::function G_TEST_GET_FULL_NAME{[]{ return std::string{g_fuzz_target}; }}; @@ -210,7 +217,6 @@ void signal_handler(int signal) // This function is used by libFuzzer extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { - static const auto& test_one_input = *Assert(g_test_one_input); test_one_input({data, size}); return 0; } @@ -227,7 +233,6 @@ extern "C" int LLVMFuzzerInitialize(int* argc, char*** argv) int main(int argc, char** argv) { initialize(); - static const auto& test_one_input = *Assert(g_test_one_input); #ifdef __AFL_LOOP // Enable AFL persistent mode. Requires compilation using afl-clang-fast++. // See fuzzing.md for details. diff --git a/src/test/fuzz/golomb_rice.cpp b/src/test/fuzz/golomb_rice.cpp index 92e49445ba7..066c168652b 100644 --- a/src/test/fuzz/golomb_rice.cpp +++ b/src/test/fuzz/golomb_rice.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -42,6 +43,7 @@ std::vector BuildHashedSet(const std::unordered_set golomb_rice_data; std::vector encoded_deltas; diff --git a/src/test/fuzz/headerssync.cpp b/src/test/fuzz/headerssync.cpp index 1aa878bd6d8..8135b0d4eab 100644 --- a/src/test/fuzz/headerssync.cpp +++ b/src/test/fuzz/headerssync.cpp @@ -48,6 +48,7 @@ class FuzzedHeadersSyncState : public HeadersSyncState FUZZ_TARGET(headers_sync_state, .init = initialize_headers_sync_state_fuzz) { + SeedRandomStateForTest(SeedRand::ZEROS); FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size()); auto mock_time{ConsumeTime(fuzzed_data_provider)}; diff --git a/src/test/fuzz/i2p.cpp b/src/test/fuzz/i2p.cpp index 51517187a0a..b8024f7b1c4 100644 --- a/src/test/fuzz/i2p.cpp +++ b/src/test/fuzz/i2p.cpp @@ -21,6 +21,7 @@ void initialize_i2p() FUZZ_TARGET(i2p, .init = initialize_i2p) { + SeedRandomStateForTest(SeedRand::ZEROS); FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()}; SetMockTime(ConsumeTime(fuzzed_data_provider)); diff --git a/src/test/fuzz/key.cpp b/src/test/fuzz/key.cpp index 82973803f84..c7ff2f3a28e 100644 --- a/src/test/fuzz/key.cpp +++ b/src/test/fuzz/key.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -38,6 +39,7 @@ void initialize_key() FUZZ_TARGET(key, .init = initialize_key) { + SeedRandomStateForTest(SeedRand::ZEROS); const CKey key = [&] { CKey k; k.Set(buffer.begin(), buffer.end(), true); diff --git a/src/test/fuzz/mini_miner.cpp b/src/test/fuzz/mini_miner.cpp index 139fdcdab81..a5bccd103d2 100644 --- a/src/test/fuzz/mini_miner.cpp +++ b/src/test/fuzz/mini_miner.cpp @@ -34,6 +34,7 @@ void initialize_miner() // Test that the MiniMiner can run with various outpoints and feerates. FUZZ_TARGET(mini_miner, .init = initialize_miner) { + SeedRandomStateForTest(SeedRand::ZEROS); FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()}; bilingual_str error; CTxMemPool pool{CTxMemPool::Options{}, error}; @@ -112,6 +113,7 @@ FUZZ_TARGET(mini_miner, .init = initialize_miner) // Test that MiniMiner and BlockAssembler build the same block given the same transactions and constraints. FUZZ_TARGET(mini_miner_selection, .init = initialize_miner) { + SeedRandomStateForTest(SeedRand::ZEROS); FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()}; bilingual_str error; CTxMemPool pool{CTxMemPool::Options{}, error}; diff --git a/src/test/fuzz/netaddress.cpp b/src/test/fuzz/netaddress.cpp index 4803cdccad5..28a2026a358 100644 --- a/src/test/fuzz/netaddress.cpp +++ b/src/test/fuzz/netaddress.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -13,6 +14,7 @@ FUZZ_TARGET(netaddress) { + SeedRandomStateForTest(SeedRand::ZEROS); FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size()); const CNetAddr net_addr = ConsumeNetAddr(fuzzed_data_provider); diff --git a/src/test/fuzz/p2p_handshake.cpp b/src/test/fuzz/p2p_handshake.cpp index 6c1ed11d456..d608efd87ac 100644 --- a/src/test/fuzz/p2p_handshake.cpp +++ b/src/test/fuzz/p2p_handshake.cpp @@ -39,6 +39,7 @@ void initialize() FUZZ_TARGET(p2p_handshake, .init = ::initialize) { + SeedRandomStateForTest(SeedRand::ZEROS); FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size()); ConnmanTestMsg& connman = static_cast(*g_setup->m_node.connman); diff --git a/src/test/fuzz/p2p_headers_presync.cpp b/src/test/fuzz/p2p_headers_presync.cpp index 935794057d4..873eb2b1cc9 100644 --- a/src/test/fuzz/p2p_headers_presync.cpp +++ b/src/test/fuzz/p2p_headers_presync.cpp @@ -153,6 +153,7 @@ void initialize() FUZZ_TARGET(p2p_headers_presync, .init = initialize) { + SeedRandomStateForTest(SeedRand::ZEROS); ChainstateManager& chainman = *g_testing_setup->m_node.chainman; LOCK(NetEventsInterface::g_msgproc_mutex); diff --git a/src/test/fuzz/package_eval.cpp b/src/test/fuzz/package_eval.cpp index 2016e3e0522..8e3d84a9e63 100644 --- a/src/test/fuzz/package_eval.cpp +++ b/src/test/fuzz/package_eval.cpp @@ -192,6 +192,7 @@ std::optional GetChildEvictingPrevout(const CTxMemPool& tx_pool) FUZZ_TARGET(ephemeral_package_eval, .init = initialize_tx_pool) { + SeedRandomStateForTest(SeedRand::ZEROS); FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size()); const auto& node = g_setup->m_node; auto& chainstate{static_cast(node.chainman->ActiveChainstate())}; @@ -346,6 +347,7 @@ FUZZ_TARGET(ephemeral_package_eval, .init = initialize_tx_pool) FUZZ_TARGET(tx_package_eval, .init = initialize_tx_pool) { + SeedRandomStateForTest(SeedRand::ZEROS); FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size()); const auto& node = g_setup->m_node; auto& chainstate{static_cast(node.chainman->ActiveChainstate())}; diff --git a/src/test/fuzz/partially_downloaded_block.cpp b/src/test/fuzz/partially_downloaded_block.cpp index c41005eb738..8a42807be81 100644 --- a/src/test/fuzz/partially_downloaded_block.cpp +++ b/src/test/fuzz/partially_downloaded_block.cpp @@ -44,6 +44,7 @@ PartiallyDownloadedBlock::CheckBlockFn FuzzedCheckBlock(std::optional(fuzzed_data_provider, TX_WITH_WITNESS)}; diff --git a/src/test/fuzz/process_message.cpp b/src/test/fuzz/process_message.cpp index a95c6e54dfb..4bd38a1ac68 100644 --- a/src/test/fuzz/process_message.cpp +++ b/src/test/fuzz/process_message.cpp @@ -52,6 +52,7 @@ void initialize_process_message() FUZZ_TARGET(process_message, .init = initialize_process_message) { + SeedRandomStateForTest(SeedRand::ZEROS); FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size()); ConnmanTestMsg& connman = *static_cast(g_setup->m_node.connman.get()); diff --git a/src/test/fuzz/process_messages.cpp b/src/test/fuzz/process_messages.cpp index 9b8ce5b23a8..0688868c02b 100644 --- a/src/test/fuzz/process_messages.cpp +++ b/src/test/fuzz/process_messages.cpp @@ -42,6 +42,7 @@ void initialize_process_messages() FUZZ_TARGET(process_messages, .init = initialize_process_messages) { + SeedRandomStateForTest(SeedRand::ZEROS); FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size()); ConnmanTestMsg& connman = *static_cast(g_setup->m_node.connman.get()); diff --git a/src/test/fuzz/psbt.cpp b/src/test/fuzz/psbt.cpp index e0692600bb2..37fa159c33c 100644 --- a/src/test/fuzz/psbt.cpp +++ b/src/test/fuzz/psbt.cpp @@ -2,14 +2,14 @@ // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. -#include -#include - #include #include #include #include