2021-12-30 19:36:57 +02:00
|
|
|
// Copyright (c) 2019-2021 The Bitcoin Core developers
|
2019-12-18 16:25:20 +00:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#include <chainparams.h>
|
2021-05-24 20:11:44 -07:00
|
|
|
#include <hash.h>
|
2019-12-18 16:25:20 +00:00
|
|
|
#include <net.h>
|
2021-05-23 08:10:25 -07:00
|
|
|
#include <netmessagemaker.h>
|
2019-12-18 16:25:20 +00:00
|
|
|
#include <protocol.h>
|
2021-05-24 20:11:44 -07:00
|
|
|
#include <test/fuzz/FuzzedDataProvider.h>
|
2019-12-18 16:25:20 +00:00
|
|
|
#include <test/fuzz/fuzz.h>
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <limits>
|
2021-03-15 11:59:05 +08:00
|
|
|
#include <optional>
|
2019-12-18 16:25:20 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2021-05-23 08:10:25 -07:00
|
|
|
void initialize_p2p_transport_serialization()
|
2019-12-18 16:25:20 +00:00
|
|
|
{
|
|
|
|
SelectParams(CBaseChainParams::REGTEST);
|
|
|
|
}
|
|
|
|
|
2021-05-23 08:10:25 -07:00
|
|
|
FUZZ_TARGET_INIT(p2p_transport_serialization, initialize_p2p_transport_serialization)
|
2019-12-18 16:25:20 +00:00
|
|
|
{
|
2020-06-29 14:09:42 -04:00
|
|
|
// Construct deserializer, with a dummy NodeId
|
2020-06-08 22:26:22 -04:00
|
|
|
V1TransportDeserializer deserializer{Params(), (NodeId)0, SER_NETWORK, INIT_PROTO_VERSION};
|
2021-05-23 08:10:25 -07:00
|
|
|
V1TransportSerializer serializer{};
|
2021-05-24 20:11:44 -07:00
|
|
|
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
|
|
|
|
|
|
|
|
auto checksum_assist = fuzzed_data_provider.ConsumeBool();
|
2021-05-24 22:14:23 -07:00
|
|
|
auto magic_bytes_assist = fuzzed_data_provider.ConsumeBool();
|
|
|
|
std::vector<uint8_t> mutable_msg_bytes;
|
|
|
|
|
|
|
|
auto header_bytes_remaining = CMessageHeader::HEADER_SIZE;
|
|
|
|
if (magic_bytes_assist) {
|
|
|
|
auto msg_start = Params().MessageStart();
|
|
|
|
for (size_t i = 0; i < CMessageHeader::MESSAGE_SIZE_SIZE; ++i) {
|
|
|
|
mutable_msg_bytes.push_back(msg_start[i]);
|
|
|
|
}
|
|
|
|
header_bytes_remaining -= CMessageHeader::MESSAGE_SIZE_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (checksum_assist) {
|
|
|
|
header_bytes_remaining -= CMessageHeader::CHECKSUM_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto header_random_bytes = fuzzed_data_provider.ConsumeBytes<uint8_t>(header_bytes_remaining);
|
|
|
|
mutable_msg_bytes.insert(mutable_msg_bytes.end(), header_random_bytes.begin(), header_random_bytes.end());
|
2021-05-24 20:11:44 -07:00
|
|
|
auto payload_bytes = fuzzed_data_provider.ConsumeRemainingBytes<uint8_t>();
|
|
|
|
|
|
|
|
if (checksum_assist && mutable_msg_bytes.size() == CMessageHeader::CHECKSUM_OFFSET) {
|
|
|
|
CHash256 hasher;
|
|
|
|
unsigned char hsh[32];
|
|
|
|
hasher.Write(payload_bytes);
|
|
|
|
hasher.Finalize(hsh);
|
|
|
|
for (size_t i = 0; i < CMessageHeader::CHECKSUM_SIZE; ++i) {
|
|
|
|
mutable_msg_bytes.push_back(hsh[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mutable_msg_bytes.insert(mutable_msg_bytes.end(), payload_bytes.begin(), payload_bytes.end());
|
|
|
|
Span<const uint8_t> msg_bytes{mutable_msg_bytes};
|
2020-09-30 17:08:26 +02:00
|
|
|
while (msg_bytes.size() > 0) {
|
|
|
|
const int handled = deserializer.Read(msg_bytes);
|
2019-12-18 16:25:20 +00:00
|
|
|
if (handled < 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (deserializer.Complete()) {
|
2020-04-14 13:24:18 -04:00
|
|
|
const std::chrono::microseconds m_time{std::numeric_limits<int64_t>::max()};
|
2020-11-05 05:05:32 -05:00
|
|
|
bool reject_message{false};
|
|
|
|
CNetMessage msg = deserializer.GetMessage(m_time, reject_message);
|
2022-01-15 20:59:19 +02:00
|
|
|
assert(msg.m_type.size() <= CMessageHeader::COMMAND_SIZE);
|
2020-11-05 05:05:32 -05:00
|
|
|
assert(msg.m_raw_message_size <= mutable_msg_bytes.size());
|
|
|
|
assert(msg.m_raw_message_size == CMessageHeader::HEADER_SIZE + msg.m_message_size);
|
|
|
|
assert(msg.m_time == m_time);
|
2021-05-23 08:10:25 -07:00
|
|
|
|
2020-11-05 05:05:32 -05:00
|
|
|
std::vector<unsigned char> header;
|
2022-01-15 20:59:19 +02:00
|
|
|
auto msg2 = CNetMsgMaker{msg.m_recv.GetVersion()}.Make(msg.m_type, MakeUCharSpan(msg.m_recv));
|
2020-11-05 05:05:32 -05:00
|
|
|
serializer.prepareForTransport(msg2, header);
|
2019-12-18 16:25:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|