Bitcoin Core  29.1.0
P2P Digital Currency
net.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020-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 #include <chainparams.h>
6 #include <net.h>
7 #include <net_permissions.h>
8 #include <netaddress.h>
9 #include <protocol.h>
10 #include <random.h>
12 #include <test/fuzz/fuzz.h>
13 #include <test/fuzz/util.h>
14 #include <test/fuzz/util/net.h>
15 #include <test/util/net.h>
16 #include <test/util/setup_common.h>
17 #include <util/asmap.h>
18 #include <util/chaintype.h>
19 #include <util/time.h>
20 
21 #include <cstdint>
22 #include <optional>
23 #include <string>
24 #include <vector>
25 
27 {
28  static const auto testing_setup = MakeNoLogFileContext<>(ChainType::MAIN);
29 }
30 
32 {
33  FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
34  SetMockTime(ConsumeTime(fuzzed_data_provider));
35  CNode node{ConsumeNode(fuzzed_data_provider)};
36  node.SetCommonVersion(fuzzed_data_provider.ConsumeIntegral<int>());
37  if (const auto service_opt =
38  ConsumeDeserializable<CService>(fuzzed_data_provider, ConsumeDeserializationParams<CNetAddr::SerParams>(fuzzed_data_provider)))
39  {
40  node.SetAddrLocal(*service_opt);
41  }
42  LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
43  CallOneOf(
44  fuzzed_data_provider,
45  [&] {
46  node.CloseSocketDisconnect();
47  },
48  [&] {
49  CNodeStats stats;
50  node.CopyStats(stats);
51  },
52  [&] {
53  const CNode* add_ref_node = node.AddRef();
54  assert(add_ref_node == &node);
55  },
56  [&] {
57  if (node.GetRefCount() > 0) {
58  node.Release();
59  }
60  },
61  [&] {
62  const std::vector<uint8_t> b = ConsumeRandomLengthByteVector(fuzzed_data_provider);
63  bool complete;
64  node.ReceiveMsgBytes(b, complete);
65  });
66  }
67 
68  (void)node.GetAddrLocal();
69  (void)node.GetId();
70  (void)node.GetLocalNonce();
71  const int ref_count = node.GetRefCount();
72  assert(ref_count >= 0);
73  (void)node.GetCommonVersion();
74 
75  const NetPermissionFlags net_permission_flags = ConsumeWeakEnum(fuzzed_data_provider, ALL_NET_PERMISSION_FLAGS);
76  (void)node.HasPermission(net_permission_flags);
77  (void)node.ConnectedThroughNetwork();
78 }
79 
80 FUZZ_TARGET(local_address, .init = initialize_net)
81 {
82  FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
83  SetMockTime(ConsumeTime(fuzzed_data_provider));
84  CService service{ConsumeService(fuzzed_data_provider)};
85  CNode node{ConsumeNode(fuzzed_data_provider)};
86  {
88  mapLocalHost.clear();
89  }
90  LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
91  CallOneOf(
92  fuzzed_data_provider,
93  [&] {
94  service = ConsumeService(fuzzed_data_provider);
95  },
96  [&] {
97  const bool added{AddLocal(service, fuzzed_data_provider.ConsumeIntegralInRange<int>(0, LOCAL_MAX - 1))};
98  if (!added) return;
99  assert(service.IsRoutable());
100  assert(IsLocal(service));
101  assert(SeenLocal(service));
102  },
103  [&] {
104  (void)RemoveLocal(service);
105  },
106  [&] {
107  (void)SeenLocal(service);
108  },
109  [&] {
110  (void)IsLocal(service);
111  },
112  [&] {
113  (void)GetLocalAddress(node);
114  });
115  }
116 }
assert(!tx.IsCoinBase())
CService GetLocalAddress(const CNode &peer)
Definition: net.cpp:226
WeakEnumType ConsumeWeakEnum(FuzzedDataProvider &fuzzed_data_provider, const WeakEnumType(&all_types)[size]) noexcept
Definition: util.h:133
constexpr NetPermissionFlags ALL_NET_PERMISSION_FLAGS[]
Definition: net.h:105
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
Definition: fuzz.h:22
auto ConsumeNode(FuzzedDataProvider &fuzzed_data_provider, const std::optional< NodeId > &node_id_in=std::nullopt) noexcept
Definition: net.h:231
bool SeenLocal(const CService &addr)
vote for a local address
Definition: net.cpp:318
NetPermissionFlags
Definition: net.h:154
std::vector< B > ConsumeRandomLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition: util.h:57
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition: time.cpp:40
void initialize_net()
Definition: net.cpp:26
FUZZ_TARGET(net,.init=initialize_net)
Definition: net.cpp:31
#define LOCK(cs)
Definition: sync.h:257
bool AddLocal(const CService &addr_, int nScore)
Definition: net.cpp:277
CService ConsumeService(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: net.h:223
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:530
Definition: messages.h:20
int64_t ConsumeTime(FuzzedDataProvider &fuzzed_data_provider, const std::optional< int64_t > &min, const std::optional< int64_t > &max) noexcept
Definition: util.cpp:34
GlobalMutex g_maplocalhost_mutex
Definition: net.cpp:124
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
Definition: util.h:35
Information about a peer.
Definition: net.h:672
void RemoveLocal(const CService &addr)
Definition: net.cpp:310
bool IsLocal(const CService &addr)
check whether a given address is potentially local
Definition: net.cpp:329