|
| 1 | +// Copyright (c) 2019-2020 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 <hash.h> |
| 7 | +#include <net.h> |
| 8 | +#include <netmessagemaker.h> |
| 9 | +#include <protocol.h> |
| 10 | +#include <test/fuzz/FuzzedDataProvider.h> |
| 11 | +#include <test/fuzz/fuzz.h> |
| 12 | + |
| 13 | +#include <cassert> |
| 14 | +#include <cstdint> |
| 15 | +#include <limits> |
| 16 | +#include <optional> |
| 17 | +#include <vector> |
| 18 | + |
| 19 | +void initialize_p2p_transport_serialization() |
| 20 | +{ |
| 21 | + SelectParams(CBaseChainParams::REGTEST); |
| 22 | +} |
| 23 | + |
| 24 | +FUZZ_TARGET_INIT(p2p_transport_serialization, initialize_p2p_transport_serialization) |
| 25 | +{ |
| 26 | + // Construct deserializer, with a dummy NodeId |
| 27 | + V1TransportDeserializer deserializer{Params(), (NodeId)0, SER_NETWORK, INIT_PROTO_VERSION}; |
| 28 | + V1TransportSerializer serializer{}; |
| 29 | + FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()}; |
| 30 | + |
| 31 | + auto checksum_assist = fuzzed_data_provider.ConsumeBool(); |
| 32 | + auto magic_bytes_assist = fuzzed_data_provider.ConsumeBool(); |
| 33 | + std::vector<uint8_t> mutable_msg_bytes; |
| 34 | + |
| 35 | + auto header_bytes_remaining = CMessageHeader::HEADER_SIZE; |
| 36 | + if (magic_bytes_assist) { |
| 37 | + auto msg_start = Params().MessageStart(); |
| 38 | + for (size_t i = 0; i < CMessageHeader::MESSAGE_SIZE_SIZE; ++i) { |
| 39 | + mutable_msg_bytes.push_back(msg_start[i]); |
| 40 | + } |
| 41 | + header_bytes_remaining -= CMessageHeader::MESSAGE_SIZE_SIZE; |
| 42 | + } |
| 43 | + |
| 44 | + if (checksum_assist) { |
| 45 | + header_bytes_remaining -= CMessageHeader::CHECKSUM_SIZE; |
| 46 | + } |
| 47 | + |
| 48 | + auto header_random_bytes = fuzzed_data_provider.ConsumeBytes<uint8_t>(header_bytes_remaining); |
| 49 | + mutable_msg_bytes.insert(mutable_msg_bytes.end(), header_random_bytes.begin(), header_random_bytes.end()); |
| 50 | + auto payload_bytes = fuzzed_data_provider.ConsumeRemainingBytes<uint8_t>(); |
| 51 | + |
| 52 | + if (checksum_assist && mutable_msg_bytes.size() == CMessageHeader::CHECKSUM_OFFSET) { |
| 53 | + CHash256 hasher; |
| 54 | + unsigned char hsh[32]; |
| 55 | + hasher.Write(payload_bytes); |
| 56 | + hasher.Finalize(hsh); |
| 57 | + for (size_t i = 0; i < CMessageHeader::CHECKSUM_SIZE; ++i) { |
| 58 | + mutable_msg_bytes.push_back(hsh[i]); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + mutable_msg_bytes.insert(mutable_msg_bytes.end(), payload_bytes.begin(), payload_bytes.end()); |
| 63 | + Span<const uint8_t> msg_bytes{mutable_msg_bytes}; |
| 64 | + while (msg_bytes.size() > 0) { |
| 65 | + const int handled = deserializer.Read(msg_bytes); |
| 66 | + if (handled < 0) { |
| 67 | + break; |
| 68 | + } |
| 69 | + if (deserializer.Complete()) { |
| 70 | + const std::chrono::microseconds m_time{std::numeric_limits<int64_t>::max()}; |
| 71 | + uint32_t out_err_raw_size{0}; |
| 72 | + std::optional<CNetMessage> result{deserializer.GetMessage(m_time, out_err_raw_size)}; |
| 73 | + if (result) { |
| 74 | + assert(result->m_command.size() <= CMessageHeader::COMMAND_SIZE); |
| 75 | + assert(result->m_raw_message_size <= mutable_msg_bytes.size()); |
| 76 | + assert(result->m_raw_message_size == CMessageHeader::HEADER_SIZE + result->m_message_size); |
| 77 | + assert(result->m_time == m_time); |
| 78 | + |
| 79 | + std::vector<unsigned char> header; |
| 80 | + auto msg = CNetMsgMaker{result->m_recv.GetVersion()}.Make(result->m_command, MakeUCharSpan(result->m_recv)); |
| 81 | + serializer.prepareForTransport(msg, header); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments