// Copyright (c) 2020-2021 The Bitcoin Core developers // 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 #include #include #include #include #include #include namespace { int64_t ConsumeBanTimeOffset(FuzzedDataProvider& fuzzed_data_provider) noexcept { // Avoid signed integer overflow by capping to int32_t max: // banman.cpp:137:73: runtime error: signed integer overflow: 1591700817 + 9223372036854775807 cannot be represented in type 'long' return fuzzed_data_provider.ConsumeIntegralInRange(std::numeric_limits::min(), std::numeric_limits::max()); } } // namespace void initialize_banman() { static const auto testing_setup = MakeNoLogFileContext<>(); } FUZZ_TARGET_INIT(banman, initialize_banman) { FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()}; SetMockTime(ConsumeTime(fuzzed_data_provider)); const fs::path banlist_file = gArgs.GetDataDirNet() / "fuzzed_banlist.dat"; fs::remove(banlist_file); { BanMan ban_man{banlist_file, nullptr, ConsumeBanTimeOffset(fuzzed_data_provider)}; while (fuzzed_data_provider.ConsumeBool()) { CallOneOf( fuzzed_data_provider, [&] { ban_man.Ban(ConsumeNetAddr(fuzzed_data_provider), ConsumeBanTimeOffset(fuzzed_data_provider), fuzzed_data_provider.ConsumeBool()); }, [&] { ban_man.Ban(ConsumeSubNet(fuzzed_data_provider), ConsumeBanTimeOffset(fuzzed_data_provider), fuzzed_data_provider.ConsumeBool()); }, [&] { ban_man.ClearBanned(); }, [] {}, [&] { ban_man.IsBanned(ConsumeNetAddr(fuzzed_data_provider)); }, [&] { ban_man.IsBanned(ConsumeSubNet(fuzzed_data_provider)); }, [&] { ban_man.Unban(ConsumeNetAddr(fuzzed_data_provider)); }, [&] { ban_man.Unban(ConsumeSubNet(fuzzed_data_provider)); }, [&] { banmap_t banmap; ban_man.GetBanned(banmap); }, [&] { ban_man.DumpBanlist(); }, [] {}, [&] { ban_man.Discourage(ConsumeNetAddr(fuzzed_data_provider)); }); } } fs::remove(banlist_file); }