Branch data Line data Source code
1 : : // Copyright (c) 2020-present The Bitcoin Core developers
2 : : // Distributed under the MIT software license, see the accompanying
3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 : :
5 : : #include <addrdb.h>
6 : : #include <addrman.h>
7 : : #include <addrman_impl.h>
8 : : #include <chainparams.h>
9 : : #include <common/args.h>
10 : : #include <merkleblock.h>
11 : : #include <random.h>
12 : : #include <test/fuzz/FuzzedDataProvider.h>
13 : : #include <test/fuzz/fuzz.h>
14 : : #include <test/fuzz/util.h>
15 : : #include <test/fuzz/util/net.h>
16 : : #include <test/util/setup_common.h>
17 : : #include <test/util/time.h>
18 : : #include <util/asmap.h>
19 : : #include <util/chaintype.h>
20 : :
21 : : #include <cassert>
22 : : #include <cstdint>
23 : : #include <ctime>
24 : : #include <optional>
25 : : #include <string>
26 : : #include <vector>
27 : :
28 : : namespace {
29 : : const BasicTestingSetup* g_setup;
30 : :
31 : 6309 : int32_t GetCheckRatio()
32 : : {
33 [ + - ]: 18927 : return std::clamp<int32_t>(g_setup->m_node.args->GetIntArg("-checkaddrman", 0), 0, 1000000);
34 : : }
35 : : } // namespace
36 : :
37 : 3 : void initialize_addrman()
38 : : {
39 [ + - + - : 3 : static const auto testing_setup = MakeNoLogFileContext<>(ChainType::REGTEST);
+ - ]
40 : 3 : g_setup = testing_setup.get();
41 : 3 : }
42 : :
43 [ + - ]: 1672 : FUZZ_TARGET(data_stream_addr_man, .init = initialize_addrman)
44 : : {
45 : 1196 : SeedRandomStateForTest(SeedRand::ZEROS);
46 : 1196 : FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
47 : 1196 : DataStream data_stream = ConsumeDataStream(fuzzed_data_provider);
48 : 1196 : NetGroupManager netgroupman{ConsumeNetGroupManager(fuzzed_data_provider)};
49 [ + - + - ]: 1196 : AddrMan addr_man(netgroupman, /*deterministic=*/false, GetCheckRatio());
50 : 1196 : try {
51 [ + + ]: 1196 : ReadFromStream(addr_man, data_stream);
52 [ - + ]: 732 : } catch (const std::exception&) {
53 : 732 : }
54 : 1196 : }
55 : :
56 : : /**
57 : : * Generate a random address. Always returns a valid address.
58 : : */
59 : 8817407 : CNetAddr RandAddr(FuzzedDataProvider& fuzzed_data_provider, FastRandomContext& fast_random_context)
60 : : {
61 : 8817407 : CNetAddr addr;
62 [ + - + - ]: 8817407 : assert(!addr.IsValid());
63 [ + + + - : 19309050 : for (size_t i = 0; i < 8 && !addr.IsValid(); ++i) {
+ + ]
64 [ + + + + ]: 11854228 : if (fuzzed_data_provider.remaining_bytes() > 1 && fuzzed_data_provider.ConsumeBool()) {
65 : 1056665 : addr = ConsumeNetAddr(fuzzed_data_provider);
66 : : } else {
67 : 9434978 : addr = ConsumeNetAddr(fuzzed_data_provider, &fast_random_context);
68 : : }
69 : : }
70 : :
71 : : // Return a dummy IPv4 5.5.5.5 if we generated an invalid address.
72 [ + - + + ]: 8817407 : if (!addr.IsValid()) {
73 : 5582 : in_addr v4_addr = {};
74 : 5582 : v4_addr.s_addr = 0x05050505;
75 [ + - ]: 11164 : addr = CNetAddr{v4_addr};
76 : : }
77 : :
78 : 8817407 : return addr;
79 : 0 : }
80 : :
81 : : /** Fill addrman with lots of addresses from lots of sources. */
82 : 1272 : void FillAddrman(AddrMan& addrman, FuzzedDataProvider& fuzzed_data_provider)
83 : : {
84 : : // Add a fraction of the addresses to the "tried" table.
85 : : // 0, 1, 2, 3 corresponding to 0%, 100%, 50%, 33%
86 : 1272 : const size_t n = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 3);
87 : :
88 : 1272 : const size_t num_sources = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, 50);
89 : 1272 : CNetAddr prev_source;
90 : : // Generate a FastRandomContext seed to use inside the loops instead of
91 : : // fuzzed_data_provider. When fuzzed_data_provider is exhausted it
92 : : // just returns 0.
93 : 1272 : FastRandomContext fast_random_context{ConsumeUInt256(fuzzed_data_provider)};
94 [ + + ]: 36345 : for (size_t i = 0; i < num_sources; ++i) {
95 [ + - ]: 35073 : const auto source = RandAddr(fuzzed_data_provider, fast_random_context);
96 : 35073 : const size_t num_addresses = fast_random_context.randrange(500) + 1; // [1..500]
97 : :
98 [ + + ]: 8817407 : for (size_t j = 0; j < num_addresses; ++j) {
99 [ + - + - ]: 17564668 : const auto addr = CAddress{CService{RandAddr(fuzzed_data_provider, fast_random_context), 8333}, NODE_NETWORK};
100 : 8782334 : const std::chrono::seconds time_penalty{fast_random_context.randrange(100000001)};
101 [ + - + - : 17564668 : addrman.Add({addr}, source, time_penalty);
+ + - - ]
102 : :
103 [ + + + - : 8782334 : if (n > 0 && addrman.Size() % n == 0) {
+ + ]
104 [ + - ]: 5143001 : addrman.Good(addr, Now<NodeSeconds>());
105 : : }
106 : :
107 : : // Add 10% of the addresses from more than one source.
108 [ + + + - : 8782334 : if (fast_random_context.randrange(10) == 0 && prev_source.IsValid()) {
+ + ]
109 [ + - + - : 1710742 : addrman.Add({addr}, prev_source, time_penalty);
+ + - - ]
110 : : }
111 : 8782334 : }
112 : 70146 : prev_source = source;
113 : 35073 : }
114 : 9638977 : }
115 : :
116 [ + - ]: 2260 : FUZZ_TARGET(addrman, .init = initialize_addrman)
117 : : {
118 : 1784 : SeedRandomStateForTest(SeedRand::ZEROS);
119 : 1784 : FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
120 : 1784 : FakeNodeClock clock{ConsumeTime(fuzzed_data_provider)};
121 : 1784 : NetGroupManager netgroupman{ConsumeNetGroupManager(fuzzed_data_provider)};
122 [ + - + - ]: 1784 : auto addr_man_ptr = std::make_unique<AddrManDeterministic>(netgroupman, fuzzed_data_provider, GetCheckRatio());
123 [ + + ]: 1784 : if (fuzzed_data_provider.ConsumeBool()) {
124 : 1076 : const std::vector<uint8_t> serialized_data{ConsumeRandomLengthByteVector(fuzzed_data_provider)};
125 [ - + + - ]: 1076 : DataStream ds{serialized_data};
126 : 1076 : try {
127 [ + + ]: 2152 : ds >> *addr_man_ptr;
128 [ - + ]: 785 : } catch (const std::ios_base::failure&) {
129 [ + - + - ]: 1570 : addr_man_ptr = std::make_unique<AddrManDeterministic>(netgroupman, fuzzed_data_provider, GetCheckRatio());
130 : 785 : }
131 : 1076 : }
132 : 1784 : AddrManDeterministic& addr_man = *addr_man_ptr;
133 [ + + + + ]: 493803 : LIMITED_WHILE (fuzzed_data_provider.ConsumeBool(), 10000) {
134 [ + - ]: 492019 : CallOneOf(
135 : : fuzzed_data_provider,
136 : 8652 : [&] {
137 : 8652 : addr_man.ResolveCollisions();
138 : 8652 : },
139 : 70661 : [&] {
140 : 70661 : (void)addr_man.SelectTriedCollision();
141 : 70661 : },
142 : 10051 : [&] {
143 : 10051 : std::vector<CAddress> addresses;
144 [ + + + + ]: 2677610 : LIMITED_WHILE (fuzzed_data_provider.ConsumeBool(), 10000) {
145 [ + - ]: 2667559 : addresses.push_back(ConsumeAddress(fuzzed_data_provider));
146 : : }
147 : 10051 : auto net_addr = ConsumeNetAddr(fuzzed_data_provider);
148 : 10051 : auto time_penalty = ConsumeDuration<std::chrono::seconds>(fuzzed_data_provider, /*min=*/0s, /*max=*/100000000s);
149 [ + - ]: 10051 : addr_man.Add(addresses, net_addr, time_penalty);
150 : 10051 : },
151 : 335466 : [&] {
152 : 335466 : auto addr = ConsumeService(fuzzed_data_provider);
153 : 335466 : auto time = ConsumeTime(fuzzed_data_provider);
154 [ + - ]: 335466 : addr_man.Good(addr, time);
155 : 335466 : },
156 : 7585 : [&] {
157 : 7585 : auto addr = ConsumeService(fuzzed_data_provider);
158 : 7585 : auto count_failure = fuzzed_data_provider.ConsumeBool();
159 : 7585 : auto time = ConsumeTime(fuzzed_data_provider);
160 [ + - ]: 7585 : addr_man.Attempt(addr, count_failure, time);
161 : 7585 : },
162 : 9462 : [&] {
163 : 9462 : auto addr = ConsumeService(fuzzed_data_provider);
164 : 9462 : auto time = ConsumeTime(fuzzed_data_provider);
165 [ + - ]: 9462 : addr_man.Connected(addr, time);
166 : 9462 : },
167 : 50142 : [&] {
168 : 50142 : auto addr = ConsumeService(fuzzed_data_provider);
169 : 50142 : auto n_services = ConsumeWeakEnum(fuzzed_data_provider, ALL_SERVICE_FLAGS);
170 [ + - ]: 50142 : addr_man.SetServices(addr, n_services);
171 : 50142 : });
172 : : }
173 : 1784 : const AddrMan& const_addr_man{addr_man};
174 : 1784 : std::optional<Network> network;
175 [ + + ]: 1784 : if (fuzzed_data_provider.ConsumeBool()) {
176 : 164 : network = fuzzed_data_provider.PickValueInArray(ALL_NETWORKS);
177 : : }
178 : 1784 : auto max_addresses = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096);
179 : 1784 : auto max_pct = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 100);
180 : 1784 : auto filtered = fuzzed_data_provider.ConsumeBool();
181 [ + - ]: 1784 : (void)const_addr_man.GetAddr(max_addresses, max_pct, network, filtered);
182 : :
183 : 1784 : std::unordered_set<Network> nets;
184 [ + + ]: 14272 : for (const auto& net : ALL_NETWORKS) {
185 [ + + ]: 12488 : if (fuzzed_data_provider.ConsumeBool()) {
186 [ + - ]: 1128 : nets.insert(net);
187 : : }
188 : : }
189 : 1784 : const bool new_only{fuzzed_data_provider.ConsumeBool()};
190 [ + - ]: 1784 : const CAddress selected{const_addr_man.Select(new_only, nets).first};
191 : : // Select() must return a (valid) address if and only if there are entries matching the
192 : : // filters, and if a network filter was provided, the selected address must match it.
193 [ + + ]: 1784 : const std::optional<bool> select_in_new{new_only ? std::optional<bool>{true} : std::nullopt};
194 : 1784 : size_t num_matching{0};
195 [ + + ]: 1784 : if (nets.empty()) {
196 [ + - ]: 1526 : num_matching = const_addr_man.Size(/*net=*/std::nullopt, select_in_new);
197 : : } else {
198 [ + + + - ]: 1386 : for (const auto net : nets) num_matching += const_addr_man.Size(net, select_in_new);
199 : : }
200 [ + - - + ]: 1784 : assert(selected.IsValid() == (num_matching > 0));
201 [ + + + - : 1784 : if (!nets.empty() && selected.IsValid()) assert(nets.contains(selected.GetNetwork()));
+ + + - -
+ ]
202 : :
203 : 1784 : std::optional<bool> in_new;
204 [ + + ]: 1784 : if (fuzzed_data_provider.ConsumeBool()) {
205 : 150 : in_new = fuzzed_data_provider.ConsumeBool();
206 : : }
207 [ + - ]: 1784 : (void)const_addr_man.Size(network, in_new);
208 : 1784 : DataStream data_stream{};
209 [ + - ]: 3568 : data_stream << const_addr_man;
210 : 1784 : }
211 : :
212 : : // Check that serialize followed by unserialize produces the same addrman.
213 [ + - ]: 1748 : FUZZ_TARGET(addrman_serdeser, .init = initialize_addrman)
214 : : {
215 : 1272 : SeedRandomStateForTest(SeedRand::ZEROS);
216 : 1272 : FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
217 : 1272 : FakeNodeClock clock{ConsumeTime(fuzzed_data_provider)};
218 : :
219 : 1272 : NetGroupManager netgroupman{ConsumeNetGroupManager(fuzzed_data_provider)};
220 [ + - + - ]: 1272 : AddrManDeterministic addr_man1{netgroupman, fuzzed_data_provider, GetCheckRatio()};
221 [ + - + - ]: 1272 : AddrManDeterministic addr_man2{netgroupman, fuzzed_data_provider, GetCheckRatio()};
222 : :
223 : 1272 : DataStream data_stream{};
224 : :
225 [ + - ]: 1272 : FillAddrman(addr_man1, fuzzed_data_provider);
226 [ + - ]: 1272 : data_stream << addr_man1;
227 [ + - ]: 1272 : data_stream >> addr_man2;
228 [ + - - + ]: 1272 : assert(addr_man1 == addr_man2);
229 : 1272 : }
|