Branch data Line data Source code
1 : : // Copyright (c) 2009-2022 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 : : #ifndef BITCOIN_TEST_FUZZ_UTIL_NET_H
6 : : #define BITCOIN_TEST_FUZZ_UTIL_NET_H
7 : :
8 : : #include <addrman.h>
9 : : #include <addrman_impl.h>
10 : : #include <net.h>
11 : : #include <net_permissions.h>
12 : : #include <netaddress.h>
13 : : #include <node/connection_types.h>
14 : : #include <node/eviction.h>
15 : : #include <protocol.h>
16 : : #include <test/fuzz/FuzzedDataProvider.h>
17 : : #include <test/fuzz/util.h>
18 : : #include <test/util/net.h>
19 : : #include <threadsafety.h>
20 : : #include <util/asmap.h>
21 : : #include <util/sock.h>
22 : :
23 : : #include <chrono>
24 : : #include <cstdint>
25 : : #include <limits>
26 : : #include <memory>
27 : : #include <optional>
28 : : #include <string>
29 : :
30 : : /**
31 : : * Create a CNetAddr. It may have `addr.IsValid() == false`.
32 : : * @param[in,out] fuzzed_data_provider Take data for the address from this, if `rand` is `nullptr`.
33 : : * @param[in,out] rand If not nullptr, take data from it instead of from `fuzzed_data_provider`.
34 : : * Prefer generating addresses using `fuzzed_data_provider` because it is not uniform. Only use
35 : : * `rand` if `fuzzed_data_provider` is exhausted or its data is needed for other things.
36 : : * @return a "random" network address.
37 : : */
38 : : CNetAddr ConsumeNetAddr(FuzzedDataProvider& fuzzed_data_provider, FastRandomContext* rand = nullptr) noexcept;
39 : :
40 : 5244 : class AddrManDeterministic : public AddrMan
41 : : {
42 : : public:
43 : 6231 : explicit AddrManDeterministic(const NetGroupManager& netgroupman, FuzzedDataProvider& fuzzed_data_provider, int32_t check_ratio)
44 : 6231 : : AddrMan(netgroupman, /*deterministic=*/true, check_ratio)
45 : : {
46 [ + - + - ]: 12462 : WITH_LOCK(m_impl->cs, m_impl->insecure_rand.Reseed(ConsumeUInt256(fuzzed_data_provider)));
47 : 6231 : }
48 : :
49 : : /**
50 : : * Compare with another AddrMan.
51 : : * This compares:
52 : : * - the values in `mapInfo` (the keys aka ids are ignored)
53 : : * - vvNew entries refer to the same addresses
54 : : * - vvTried entries refer to the same addresses
55 : : */
56 : 987 : bool operator==(const AddrManDeterministic& other) const
57 : : {
58 [ + - ]: 987 : LOCK2(m_impl->cs, other.m_impl->cs);
59 : :
60 [ + - + - : 987 : if (m_impl->mapInfo.size() != other.m_impl->mapInfo.size() || m_impl->nNew != other.m_impl->nNew ||
+ - ]
61 [ + - ]: 987 : m_impl->nTried != other.m_impl->nTried) {
62 : : return false;
63 : : }
64 : :
65 : : // Check that all values in `mapInfo` are equal to all values in `other.mapInfo`.
66 : : // Keys may be different.
67 : :
68 : 10569239 : auto addrinfo_hasher = [](const AddrInfo& a) {
69 : 10568252 : CSipHasher hasher(0, 0);
70 : 10568252 : auto addr_key = a.GetKey();
71 [ + - ]: 10568252 : auto source_key = a.source.GetAddrBytes();
72 [ + - ]: 10568252 : hasher.Write(TicksSinceEpoch<std::chrono::seconds>(a.m_last_success));
73 [ + - ]: 10568252 : hasher.Write(a.nAttempts);
74 [ + - ]: 10568252 : hasher.Write(a.nRefCount);
75 [ + - ]: 10568252 : hasher.Write(a.fInTried);
76 [ + - + - ]: 10568252 : hasher.Write(a.GetNetwork());
77 [ + - + - ]: 10568252 : hasher.Write(a.source.GetNetwork());
78 [ - + + - ]: 10568252 : hasher.Write(addr_key.size());
79 [ - + + - ]: 10568252 : hasher.Write(source_key.size());
80 [ - + + - ]: 10568252 : hasher.Write(addr_key);
81 [ - + + - ]: 10568252 : hasher.Write(source_key);
82 [ + - ]: 10568252 : return (size_t)hasher.Finalize();
83 : 10568252 : };
84 : :
85 : 987 : auto addrinfo_eq = [](const AddrInfo& lhs, const AddrInfo& rhs) {
86 : 0 : return std::tie(static_cast<const CService&>(lhs), lhs.source, lhs.m_last_success, lhs.nAttempts, lhs.nRefCount, lhs.fInTried) ==
87 : 0 : std::tie(static_cast<const CService&>(rhs), rhs.source, rhs.m_last_success, rhs.nAttempts, rhs.nRefCount, rhs.fInTried);
88 : : };
89 : :
90 : 987 : using Addresses = std::unordered_set<AddrInfo, decltype(addrinfo_hasher), decltype(addrinfo_eq)>;
91 : :
92 [ + - ]: 987 : const size_t num_addresses{m_impl->mapInfo.size()};
93 : :
94 [ + - ]: 1974 : Addresses addresses{num_addresses, addrinfo_hasher, addrinfo_eq};
95 [ + + + - ]: 5285113 : for (const auto& [id, addr] : m_impl->mapInfo) {
96 [ + - ]: 5284126 : addresses.insert(addr);
97 : : }
98 : :
99 [ + - ]: 1974 : Addresses other_addresses{num_addresses, addrinfo_hasher, addrinfo_eq};
100 [ + + + - ]: 5285113 : for (const auto& [id, addr] : other.m_impl->mapInfo) {
101 [ + - ]: 5284126 : other_addresses.insert(addr);
102 : : }
103 : :
104 [ + - + - ]: 987 : if (addresses != other_addresses) {
105 : : return false;
106 : : }
107 : :
108 : 80856027 : auto IdsReferToSameAddress = [&](nid_type id, nid_type other_id) EXCLUSIVE_LOCKS_REQUIRED(m_impl->cs, other.m_impl->cs) {
109 [ + + - + ]: 80855040 : if (id == -1 && other_id == -1) {
110 : : return true;
111 : : }
112 [ - + - - : 5395557 : if ((id == -1 && other_id != -1) || (id != -1 && other_id == -1)) {
+ - + - ]
113 : : return false;
114 : : }
115 : 5395557 : return m_impl->mapInfo.at(id) == other.m_impl->mapInfo.at(other_id);
116 : 987 : };
117 : :
118 : : // Check that `vvNew` contains the same addresses as `other.vvNew`. Notice - `vvNew[i][j]`
119 : : // contains just an id and the address is to be found in `mapInfo.at(id)`. The ids
120 : : // themselves may differ between `vvNew` and `other.vvNew`.
121 [ + + ]: 1011675 : for (size_t i = 0; i < ADDRMAN_NEW_BUCKET_COUNT; ++i) {
122 [ + + ]: 65694720 : for (size_t j = 0; j < ADDRMAN_BUCKET_SIZE; ++j) {
123 [ + - + - ]: 64684032 : if (!IdsReferToSameAddress(m_impl->vvNew[i][j], other.m_impl->vvNew[i][j])) {
124 : : return false;
125 : : }
126 : : }
127 : : }
128 : :
129 : : // Same for `vvTried`.
130 [ + + ]: 253659 : for (size_t i = 0; i < ADDRMAN_TRIED_BUCKET_COUNT; ++i) {
131 [ + + ]: 16423680 : for (size_t j = 0; j < ADDRMAN_BUCKET_SIZE; ++j) {
132 [ + - + - ]: 16171008 : if (!IdsReferToSameAddress(m_impl->vvTried[i][j], other.m_impl->vvTried[i][j])) {
133 : : return false;
134 : : }
135 : : }
136 : : }
137 : :
138 : : return true;
139 [ + - ]: 2961 : }
140 : : };
141 : :
142 : : class FuzzedNetEvents : public NetEventsInterface
143 : : {
144 : : public:
145 : 1713 : FuzzedNetEvents(FuzzedDataProvider& fdp) : m_fdp(fdp) {}
146 : :
147 : 0 : virtual void InitializeNode(const CNode&, ServiceFlags) override {}
148 : :
149 : 0 : virtual void FinalizeNode(const CNode&) override {}
150 : :
151 : 0 : virtual bool HasAllDesirableServiceFlags(ServiceFlags) const override { return m_fdp.ConsumeBool(); }
152 : :
153 : 0 : virtual bool ProcessMessages(CNode*, std::atomic<bool>&) override { return m_fdp.ConsumeBool(); }
154 : :
155 : 0 : virtual bool SendMessages(CNode*) override { return m_fdp.ConsumeBool(); }
156 : :
157 : : private:
158 : : FuzzedDataProvider& m_fdp;
159 : : };
160 : :
161 : : class FuzzedSock : public Sock
162 : : {
163 : : FuzzedDataProvider& m_fuzzed_data_provider;
164 : :
165 : : /**
166 : : * Data to return when `MSG_PEEK` is used as a `Recv()` flag.
167 : : * If `MSG_PEEK` is used, then our `Recv()` returns some random data as usual, but on the next
168 : : * `Recv()` call we must return the same data, thus we remember it here.
169 : : */
170 : : mutable std::vector<uint8_t> m_peek_data;
171 : :
172 : : /**
173 : : * Whether to pretend that the socket is select(2)-able. This is randomly set in the
174 : : * constructor. It should remain constant so that repeated calls to `IsSelectable()`
175 : : * return the same value.
176 : : */
177 : : const bool m_selectable;
178 : :
179 : : /**
180 : : * Used to mock the steady clock in methods waiting for a given duration.
181 : : */
182 : : mutable std::chrono::milliseconds m_time;
183 : :
184 : : /**
185 : : * Set the value of the mocked steady clock such as that many ms have passed.
186 : : */
187 : : void ElapseTime(std::chrono::milliseconds duration) const;
188 : :
189 : : public:
190 : : explicit FuzzedSock(FuzzedDataProvider& fuzzed_data_provider);
191 : :
192 : : ~FuzzedSock() override;
193 : :
194 : : FuzzedSock& operator=(Sock&& other) override;
195 : :
196 : : ssize_t Send(const void* data, size_t len, int flags) const override;
197 : :
198 : : ssize_t Recv(void* buf, size_t len, int flags) const override;
199 : :
200 : : int Connect(const sockaddr*, socklen_t) const override;
201 : :
202 : : int Bind(const sockaddr*, socklen_t) const override;
203 : :
204 : : int Listen(int backlog) const override;
205 : :
206 : : std::unique_ptr<Sock> Accept(sockaddr* addr, socklen_t* addr_len) const override;
207 : :
208 : : int GetSockOpt(int level, int opt_name, void* opt_val, socklen_t* opt_len) const override;
209 : :
210 : : int SetSockOpt(int level, int opt_name, const void* opt_val, socklen_t opt_len) const override;
211 : :
212 : : int GetSockName(sockaddr* name, socklen_t* name_len) const override;
213 : :
214 : : bool SetNonBlocking() const override;
215 : :
216 : : bool IsSelectable() const override;
217 : :
218 : : bool Wait(std::chrono::milliseconds timeout, Event requested, Event* occurred = nullptr) const override;
219 : :
220 : : bool WaitMany(std::chrono::milliseconds timeout, EventsPerSock& events_per_sock) const override;
221 : :
222 : : bool IsConnected(std::string& errmsg) const override;
223 : : };
224 : :
225 : 1713 : [[nodiscard]] inline FuzzedNetEvents ConsumeNetEvents(FuzzedDataProvider& fdp) noexcept
226 : : {
227 [ + - ]: 1713 : return FuzzedNetEvents{fdp};
228 : : }
229 : :
230 : 42 : [[nodiscard]] inline FuzzedSock ConsumeSock(FuzzedDataProvider& fuzzed_data_provider)
231 : : {
232 [ + - ]: 42 : return FuzzedSock{fuzzed_data_provider};
233 : : }
234 : :
235 : 5052 : [[nodiscard]] inline NetGroupManager ConsumeNetGroupManager(FuzzedDataProvider& fuzzed_data_provider) noexcept
236 : : {
237 : 5052 : std::vector<bool> asmap = ConsumeRandomLengthBitVector(fuzzed_data_provider);
238 [ + + ]: 5052 : if (!SanityCheckASMap(asmap, 128)) asmap.clear();
239 : 5052 : return NetGroupManager(asmap);
240 : 5052 : }
241 : :
242 : 8017 : inline CSubNet ConsumeSubNet(FuzzedDataProvider& fuzzed_data_provider) noexcept
243 : : {
244 : 8017 : return {ConsumeNetAddr(fuzzed_data_provider), fuzzed_data_provider.ConsumeIntegral<uint8_t>()};
245 : : }
246 : :
247 : 2063552 : inline CService ConsumeService(FuzzedDataProvider& fuzzed_data_provider) noexcept
248 : : {
249 : 2063552 : return {ConsumeNetAddr(fuzzed_data_provider), fuzzed_data_provider.ConsumeIntegral<uint16_t>()};
250 : : }
251 : :
252 : 28 : inline std::vector<CService> ConsumeServiceVector(FuzzedDataProvider& fuzzed_data_provider,
253 : : size_t max_vector_size = 5) noexcept
254 : : {
255 : 28 : std::vector<CService> ret;
256 : 28 : const size_t size = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, max_vector_size);
257 : 28 : ret.reserve(size);
258 [ + + ]: 66 : for (size_t i = 0; i < size; ++i) {
259 : 38 : ret.emplace_back(ConsumeService(fuzzed_data_provider));
260 : : }
261 : 28 : return ret;
262 : : }
263 : :
264 : : CAddress ConsumeAddress(FuzzedDataProvider& fuzzed_data_provider) noexcept;
265 : :
266 : : template <bool ReturnUniquePtr = false>
267 : 24113 : auto ConsumeNode(FuzzedDataProvider& fuzzed_data_provider, const std::optional<NodeId>& node_id_in = std::nullopt) noexcept
268 : : {
269 [ + + ]: 30626 : const NodeId node_id = node_id_in.value_or(fuzzed_data_provider.ConsumeIntegralInRange<NodeId>(0, std::numeric_limits<NodeId>::max()));
270 : 24113 : const auto sock = std::make_shared<FuzzedSock>(fuzzed_data_provider);
271 : 24113 : const CAddress address = ConsumeAddress(fuzzed_data_provider);
272 : 24113 : const uint64_t keyed_net_group = fuzzed_data_provider.ConsumeIntegral<uint64_t>();
273 : 24113 : const uint64_t local_host_nonce = fuzzed_data_provider.ConsumeIntegral<uint64_t>();
274 : 24113 : const CAddress addr_bind = ConsumeAddress(fuzzed_data_provider);
275 : 24113 : const std::string addr_name = fuzzed_data_provider.ConsumeRandomLengthString(64);
276 : 24113 : const ConnectionType conn_type = fuzzed_data_provider.PickValueInArray(ALL_CONNECTION_TYPES);
277 [ + + + + ]: 29653 : const bool inbound_onion{conn_type == ConnectionType::INBOUND ? fuzzed_data_provider.ConsumeBool() : false};
278 : 24113 : const uint64_t network_id = fuzzed_data_provider.ConsumeIntegral<uint64_t>();
279 : :
280 : 24113 : NetPermissionFlags permission_flags = ConsumeWeakEnum(fuzzed_data_provider, ALL_NET_PERMISSION_FLAGS);
281 : : if constexpr (ReturnUniquePtr) {
282 : : return std::make_unique<CNode>(node_id,
283 : : sock,
284 : : address,
285 : : keyed_net_group,
286 : : local_host_nonce,
287 : : addr_bind,
288 : : addr_name,
289 : : conn_type,
290 : : inbound_onion,
291 : : network_id,
292 : 20589 : CNodeOptions{ .permission_flags = permission_flags });
293 : : } else {
294 : : return CNode{node_id,
295 : 3524 : sock,
296 : : address,
297 : : keyed_net_group,
298 : : local_host_nonce,
299 : : addr_bind,
300 : : addr_name,
301 : : conn_type,
302 : : inbound_onion,
303 : : network_id,
304 [ + - + - ]: 7048 : CNodeOptions{ .permission_flags = permission_flags }};
305 : : }
306 [ + - ]: 48226 : }
307 : 20589 : inline std::unique_ptr<CNode> ConsumeNodeAsUniquePtr(FuzzedDataProvider& fdp, const std::optional<NodeId>& node_id_in = std::nullopt) { return ConsumeNode<true>(fdp, node_id_in); }
308 : :
309 : : void FillNode(FuzzedDataProvider& fuzzed_data_provider, ConnmanTestMsg& connman, CNode& node) noexcept EXCLUSIVE_LOCKS_REQUIRED(NetEventsInterface::g_msgproc_mutex);
310 : :
311 : : #endif // BITCOIN_TEST_FUZZ_UTIL_NET_H
|