Bitcoin Core  27.99.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 
20 #include <cstdint>
21 #include <optional>
22 #include <string>
23 #include <vector>
24 
26 {
27  static const auto testing_setup = MakeNoLogFileContext<>(ChainType::MAIN);
28 }
29 
31 {
32  FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
33  SetMockTime(ConsumeTime(fuzzed_data_provider));
34  CNode node{ConsumeNode(fuzzed_data_provider)};
35  node.SetCommonVersion(fuzzed_data_provider.ConsumeIntegral<int>());
36  LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
37  CallOneOf(
38  fuzzed_data_provider,
39  [&] {
40  node.CloseSocketDisconnect();
41  },
42  [&] {
43  CNodeStats stats;
44  node.CopyStats(stats);
45  },
46  [&] {
47  const CNode* add_ref_node = node.AddRef();
48  assert(add_ref_node == &node);
49  },
50  [&] {
51  if (node.GetRefCount() > 0) {
52  node.Release();
53  }
54  },
55  [&] {
56  const std::optional<CService> service_opt = ConsumeDeserializable<CService>(fuzzed_data_provider, ConsumeDeserializationParams<CNetAddr::SerParams>(fuzzed_data_provider));
57  if (!service_opt) {
58  return;
59  }
60  node.SetAddrLocal(*service_opt);
61  },
62  [&] {
63  const std::vector<uint8_t> b = ConsumeRandomLengthByteVector(fuzzed_data_provider);
64  bool complete;
65  node.ReceiveMsgBytes(b, complete);
66  });
67  }
68 
69  (void)node.GetAddrLocal();
70  (void)node.GetId();
71  (void)node.GetLocalNonce();
72  const int ref_count = node.GetRefCount();
73  assert(ref_count >= 0);
74  (void)node.GetCommonVersion();
75 
76  const NetPermissionFlags net_permission_flags = ConsumeWeakEnum(fuzzed_data_provider, ALL_NET_PERMISSION_FLAGS);
77  (void)node.HasPermission(net_permission_flags);
78  (void)node.ConnectedThroughNetwork();
79 }
80 
81 FUZZ_TARGET(local_address, .init = initialize_net)
82 {
83  FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
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 }
Information about a peer.
Definition: net.h:672
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:531
T ConsumeIntegralInRange(T min, T max)
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
Definition: fuzz.h:23
Definition: init.h:25
bool IsLocal(const CService &addr)
check whether a given address is potentially local
Definition: net.cpp:321
void RemoveLocal(const CService &addr)
Definition: net.cpp:302
bool AddLocal(const CService &addr_, int nScore)
Definition: net.cpp:269
CService GetLocalAddress(const CNode &peer)
Definition: net.cpp:218
GlobalMutex g_maplocalhost_mutex
Definition: net.cpp:117
bool SeenLocal(const CService &addr)
vote for a local address
Definition: net.cpp:310
@ LOCAL_MAX
Definition: net.h:155
NetPermissionFlags
#define LOCK(cs)
Definition: sync.h:257
FUZZ_TARGET(net,.init=initialize_net)
Definition: net.cpp:30
void initialize_net()
Definition: net.cpp:25
CService ConsumeService(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: net.h:101
auto ConsumeNode(FuzzedDataProvider &fuzzed_data_provider, const std::optional< NodeId > &node_id_in=std::nullopt) noexcept
Definition: net.h:109
int64_t ConsumeTime(FuzzedDataProvider &fuzzed_data_provider, const std::optional< int64_t > &min, const std::optional< int64_t > &max) noexcept
Definition: util.cpp:34
WeakEnumType ConsumeWeakEnum(FuzzedDataProvider &fuzzed_data_provider, const WeakEnumType(&all_types)[size]) noexcept
Definition: util.h:131
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
Definition: util.h:35
std::vector< B > ConsumeRandomLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition: util.h:57
constexpr NetPermissionFlags ALL_NET_PERMISSION_FLAGS[]
Definition: net.h:99
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition: time.cpp:32
assert(!tx.IsCoinBase())