LCOV - code coverage report
Current view: top level - src/test/fuzz - util.h (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 0.0 % 126 0
Test Date: 2024-08-28 04:44:32 Functions: 0.0 % 140 0
Branches: 0.0 % 392 0

             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_H
       6                 :             : #define BITCOIN_TEST_FUZZ_UTIL_H
       7                 :             : 
       8                 :             : #include <addresstype.h>
       9                 :             : #include <arith_uint256.h>
      10                 :             : #include <coins.h>
      11                 :             : #include <compat/compat.h>
      12                 :             : #include <consensus/amount.h>
      13                 :             : #include <consensus/consensus.h>
      14                 :             : #include <key.h>
      15                 :             : #include <merkleblock.h>
      16                 :             : #include <primitives/transaction.h>
      17                 :             : #include <script/script.h>
      18                 :             : #include <serialize.h>
      19                 :             : #include <streams.h>
      20                 :             : #include <test/fuzz/FuzzedDataProvider.h>
      21                 :             : #include <test/fuzz/fuzz.h>
      22                 :             : #include <uint256.h>
      23                 :             : 
      24                 :             : #include <algorithm>
      25                 :             : #include <array>
      26                 :             : #include <cstdint>
      27                 :             : #include <cstdio>
      28                 :             : #include <optional>
      29                 :             : #include <string>
      30                 :             : #include <vector>
      31                 :             : 
      32                 :             : class PeerManager;
      33                 :             : 
      34                 :             : template <typename... Callables>
      35                 :           0 : size_t CallOneOf(FuzzedDataProvider& fuzzed_data_provider, Callables... callables)
      36                 :             : {
      37                 :           0 :     constexpr size_t call_size{sizeof...(callables)};
      38                 :             :     static_assert(call_size >= 1);
      39                 :           0 :     const size_t call_index{fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, call_size - 1)};
      40                 :             : 
      41                 :             :     size_t i{0};
      42   [ #  #  #  #  :           0 :     ((i++ == call_index ? callables() : void()), ...);
          #  #  #  #  #  
           #  #  # ][ #  
             #  #  #  #  
           # ][ #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           # ][ #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           # ][ #  #  #  
          #  #  #  #  #  
           #  # ][ #  #  
          #  #  #  #  #  
           # ][ #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
           [ #  #  #  # ]
           [ #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           # ][ #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
           [ #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           #  #  # ][ #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           #  # ][ #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           #  #  # ][ #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
      43                 :           0 :     return call_size;
      44                 :             : }
      45                 :             : 
      46                 :             : template <typename Collection>
      47         [ #  # ]:           0 : auto& PickValue(FuzzedDataProvider& fuzzed_data_provider, Collection& col)
      48                 :             : {
      49                 :           0 :     auto sz{col.size()};
      50         [ #  # ]:           0 :     assert(sz >= 1);
      51                 :           0 :     auto it = col.begin();
      52                 :           0 :     std::advance(it, fuzzed_data_provider.ConsumeIntegralInRange<decltype(sz)>(0, sz - 1));
      53                 :           0 :     return *it;
      54                 :             : }
      55                 :             : 
      56                 :             : template<typename B = uint8_t>
      57         [ #  # ]:           0 : [[nodiscard]] inline std::vector<B> ConsumeRandomLengthByteVector(FuzzedDataProvider& fuzzed_data_provider, const std::optional<size_t>& max_length = std::nullopt) noexcept
      58                 :             : {
      59                 :             :     static_assert(sizeof(B) == 1);
      60         [ #  # ]:           0 :     const std::string s = max_length ?
      61                 :           0 :                               fuzzed_data_provider.ConsumeRandomLengthString(*max_length) :
      62                 :             :                               fuzzed_data_provider.ConsumeRandomLengthString();
      63                 :           0 :     std::vector<B> ret(s.size());
      64                 :           0 :     std::copy(s.begin(), s.end(), reinterpret_cast<char*>(ret.data()));
      65                 :           0 :     return ret;
      66                 :           0 : }
      67                 :             : 
      68                 :           0 : [[nodiscard]] inline std::vector<bool> ConsumeRandomLengthBitVector(FuzzedDataProvider& fuzzed_data_provider, const std::optional<size_t>& max_length = std::nullopt) noexcept
      69                 :             : {
      70                 :           0 :     return BytesToBits(ConsumeRandomLengthByteVector(fuzzed_data_provider, max_length));
      71                 :             : }
      72                 :             : 
      73                 :           0 : [[nodiscard]] inline DataStream ConsumeDataStream(FuzzedDataProvider& fuzzed_data_provider, const std::optional<size_t>& max_length = std::nullopt) noexcept
      74                 :             : {
      75                 :           0 :     return DataStream{ConsumeRandomLengthByteVector(fuzzed_data_provider, max_length)};
      76                 :             : }
      77                 :             : 
      78                 :           0 : [[nodiscard]] inline std::vector<std::string> ConsumeRandomLengthStringVector(FuzzedDataProvider& fuzzed_data_provider, const size_t max_vector_size = 16, const size_t max_string_length = 16) noexcept
      79                 :             : {
      80                 :           0 :     const size_t n_elements = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, max_vector_size);
      81                 :           0 :     std::vector<std::string> r;
      82         [ #  # ]:           0 :     for (size_t i = 0; i < n_elements; ++i) {
      83                 :           0 :         r.push_back(fuzzed_data_provider.ConsumeRandomLengthString(max_string_length));
      84                 :             :     }
      85                 :           0 :     return r;
      86                 :             : }
      87                 :             : 
      88                 :             : template <typename T>
      89                 :           0 : [[nodiscard]] inline std::vector<T> ConsumeRandomLengthIntegralVector(FuzzedDataProvider& fuzzed_data_provider, const size_t max_vector_size = 16) noexcept
      90                 :             : {
      91                 :           0 :     const size_t n_elements = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, max_vector_size);
      92                 :           0 :     std::vector<T> r;
      93         [ #  # ]:           0 :     for (size_t i = 0; i < n_elements; ++i) {
      94                 :           0 :         r.push_back(fuzzed_data_provider.ConsumeIntegral<T>());
      95                 :             :     }
      96                 :           0 :     return r;
      97                 :             : }
      98                 :             : 
      99                 :             : template <typename P>
     100                 :             : [[nodiscard]] P ConsumeDeserializationParams(FuzzedDataProvider& fuzzed_data_provider) noexcept;
     101                 :             : 
     102                 :             : template <typename T, typename P>
     103                 :           0 : [[nodiscard]] std::optional<T> ConsumeDeserializable(FuzzedDataProvider& fuzzed_data_provider, const P& params, const std::optional<size_t>& max_length = std::nullopt) noexcept
     104                 :             : {
     105                 :           0 :     const std::vector<uint8_t> buffer{ConsumeRandomLengthByteVector(fuzzed_data_provider, max_length)};
     106                 :           0 :     DataStream ds{buffer};
     107         [ #  # ]:           0 :     T obj;
     108                 :             :     try {
     109         [ #  # ]:           0 :         ds >> params(obj);
     110         [ -  - ]:           0 :     } catch (const std::ios_base::failure&) {
     111                 :           0 :         return std::nullopt;
     112                 :             :     }
     113                 :           0 :     return obj;
     114                 :           0 : }
     115                 :             : 
     116                 :             : template <typename T>
     117                 :           0 : [[nodiscard]] inline std::optional<T> ConsumeDeserializable(FuzzedDataProvider& fuzzed_data_provider, const std::optional<size_t>& max_length = std::nullopt) noexcept
     118                 :             : {
     119                 :           0 :     const std::vector<uint8_t> buffer = ConsumeRandomLengthByteVector(fuzzed_data_provider, max_length);
     120         [ #  # ]:           0 :     DataStream ds{buffer};
     121         [ #  # ]:           0 :     T obj;
     122                 :             :     try {
     123                 :           0 :         ds >> obj;
     124         [ -  - ]:           0 :     } catch (const std::ios_base::failure&) {
     125                 :           0 :         return std::nullopt;
     126                 :             :     }
     127                 :           0 :     return obj;
     128                 :           0 : }
     129                 :             : 
     130                 :             : template <typename WeakEnumType, size_t size>
     131                 :           0 : [[nodiscard]] WeakEnumType ConsumeWeakEnum(FuzzedDataProvider& fuzzed_data_provider, const WeakEnumType (&all_types)[size]) noexcept
     132                 :             : {
     133         [ #  # ]:           0 :     return fuzzed_data_provider.ConsumeBool() ?
     134                 :           0 :                fuzzed_data_provider.PickValueInArray<WeakEnumType>(all_types) :
     135                 :           0 :                WeakEnumType(fuzzed_data_provider.ConsumeIntegral<typename std::underlying_type<WeakEnumType>::type>());
     136                 :             : }
     137                 :             : 
     138                 :           0 : [[nodiscard]] inline opcodetype ConsumeOpcodeType(FuzzedDataProvider& fuzzed_data_provider) noexcept
     139                 :             : {
     140                 :           0 :     return static_cast<opcodetype>(fuzzed_data_provider.ConsumeIntegralInRange<uint32_t>(0, MAX_OPCODE));
     141                 :             : }
     142                 :             : 
     143                 :             : [[nodiscard]] CAmount ConsumeMoney(FuzzedDataProvider& fuzzed_data_provider, const std::optional<CAmount>& max = std::nullopt) noexcept;
     144                 :             : 
     145                 :             : [[nodiscard]] int64_t ConsumeTime(FuzzedDataProvider& fuzzed_data_provider, const std::optional<int64_t>& min = std::nullopt, const std::optional<int64_t>& max = std::nullopt) noexcept;
     146                 :             : 
     147                 :             : [[nodiscard]] CMutableTransaction ConsumeTransaction(FuzzedDataProvider& fuzzed_data_provider, const std::optional<std::vector<Txid>>& prevout_txids, const int max_num_in = 10, const int max_num_out = 10) noexcept;
     148                 :             : 
     149                 :             : [[nodiscard]] CScriptWitness ConsumeScriptWitness(FuzzedDataProvider& fuzzed_data_provider, const size_t max_stack_elem_size = 32) noexcept;
     150                 :             : 
     151                 :             : [[nodiscard]] CScript ConsumeScript(FuzzedDataProvider& fuzzed_data_provider, const bool maybe_p2wsh = false) noexcept;
     152                 :             : 
     153                 :             : [[nodiscard]] uint32_t ConsumeSequence(FuzzedDataProvider& fuzzed_data_provider) noexcept;
     154                 :             : 
     155                 :           0 : [[nodiscard]] inline CScriptNum ConsumeScriptNum(FuzzedDataProvider& fuzzed_data_provider) noexcept
     156                 :             : {
     157   [ #  #  #  #  :           0 :     return CScriptNum{fuzzed_data_provider.ConsumeIntegral<int64_t>()};
                   #  # ]
     158                 :             : }
     159                 :             : 
     160                 :           0 : [[nodiscard]] inline uint160 ConsumeUInt160(FuzzedDataProvider& fuzzed_data_provider) noexcept
     161                 :             : {
     162                 :           0 :     const std::vector<uint8_t> v160 = fuzzed_data_provider.ConsumeBytes<uint8_t>(160 / 8);
     163         [ #  # ]:           0 :     if (v160.size() != 160 / 8) {
     164                 :           0 :         return {};
     165                 :             :     }
     166                 :           0 :     return uint160{v160};
     167                 :           0 : }
     168                 :             : 
     169                 :           0 : [[nodiscard]] inline uint256 ConsumeUInt256(FuzzedDataProvider& fuzzed_data_provider) noexcept
     170                 :             : {
     171                 :           0 :     const std::vector<uint8_t> v256 = fuzzed_data_provider.ConsumeBytes<uint8_t>(256 / 8);
     172         [ #  # ]:           0 :     if (v256.size() != 256 / 8) {
     173                 :           0 :         return {};
     174                 :             :     }
     175                 :           0 :     return uint256{v256};
     176                 :           0 : }
     177                 :             : 
     178                 :           0 : [[nodiscard]] inline arith_uint256 ConsumeArithUInt256(FuzzedDataProvider& fuzzed_data_provider) noexcept
     179                 :             : {
     180                 :           0 :     return UintToArith256(ConsumeUInt256(fuzzed_data_provider));
     181                 :             : }
     182                 :             : 
     183                 :             : [[nodiscard]] std::map<COutPoint, Coin> ConsumeCoins(FuzzedDataProvider& fuzzed_data_provider) noexcept;
     184                 :             : 
     185                 :             : [[nodiscard]] CTxDestination ConsumeTxDestination(FuzzedDataProvider& fuzzed_data_provider) noexcept;
     186                 :             : 
     187                 :             : [[nodiscard]] CKey ConsumePrivateKey(FuzzedDataProvider& fuzzed_data_provider, std::optional<bool> compressed = std::nullopt) noexcept;
     188                 :             : 
     189                 :             : template <typename T>
     190                 :           0 : [[nodiscard]] bool MultiplicationOverflow(const T i, const T j) noexcept
     191                 :             : {
     192                 :             :     static_assert(std::is_integral<T>::value, "Integral required.");
     193                 :             :     if (std::numeric_limits<T>::is_signed) {
     194         [ #  # ]:           0 :         if (i > 0) {
     195         [ #  # ]:           0 :             if (j > 0) {
     196                 :           0 :                 return i > (std::numeric_limits<T>::max() / j);
     197                 :             :             } else {
     198                 :           0 :                 return j < (std::numeric_limits<T>::min() / i);
     199                 :             :             }
     200                 :             :         } else {
     201         [ #  # ]:           0 :             if (j > 0) {
     202                 :           0 :                 return i < (std::numeric_limits<T>::min() / j);
     203                 :             :             } else {
     204   [ #  #  #  # ]:           0 :                 return i != 0 && (j < (std::numeric_limits<T>::max() / i));
     205                 :             :             }
     206                 :             :         }
     207                 :             :     } else {
     208   [ #  #  #  # ]:           0 :         return j != 0 && i > std::numeric_limits<T>::max() / j;
     209                 :             :     }
     210                 :             : }
     211                 :             : 
     212                 :             : [[nodiscard]] bool ContainsSpentInput(const CTransaction& tx, const CCoinsViewCache& inputs) noexcept;
     213                 :             : 
     214                 :             : /**
     215                 :             :  * Sets errno to a value selected from the given std::array `errnos`.
     216                 :             :  */
     217                 :             : template <typename T, size_t size>
     218                 :           0 : void SetFuzzedErrNo(FuzzedDataProvider& fuzzed_data_provider, const std::array<T, size>& errnos)
     219                 :             : {
     220                 :           0 :     errno = fuzzed_data_provider.PickValueInArray(errnos);
     221                 :           0 : }
     222                 :             : 
     223                 :             : /*
     224                 :             :  * Sets a fuzzed errno in the range [0, 133 (EHWPOISON)]. Can be used from functions emulating
     225                 :             :  * standard library functions that set errno, or in other contexts where the value of errno
     226                 :             :  * might be relevant for the execution path that will be taken.
     227                 :             :  */
     228                 :           0 : inline void SetFuzzedErrNo(FuzzedDataProvider& fuzzed_data_provider) noexcept
     229                 :             : {
     230                 :           0 :     errno = fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 133);
     231                 :           0 : }
     232                 :             : 
     233                 :             : /**
     234                 :             :  * Returns a byte vector of specified size regardless of the number of remaining bytes available
     235                 :             :  * from the fuzzer. Pads with zero value bytes if needed to achieve the specified size.
     236                 :             :  */
     237                 :             : template<typename B = uint8_t>
     238                 :           0 : [[nodiscard]] inline std::vector<B> ConsumeFixedLengthByteVector(FuzzedDataProvider& fuzzed_data_provider, const size_t length) noexcept
     239                 :             : {
     240                 :             :     static_assert(sizeof(B) == 1);
     241                 :           0 :     auto random_bytes = fuzzed_data_provider.ConsumeBytes<B>(length);
     242                 :           0 :     random_bytes.resize(length);
     243                 :           0 :     return random_bytes;
     244                 :             : }
     245                 :             : 
     246                 :             : class FuzzedFileProvider
     247                 :             : {
     248                 :             :     FuzzedDataProvider& m_fuzzed_data_provider;
     249                 :             :     int64_t m_offset = 0;
     250                 :             : 
     251                 :             : public:
     252         [ #  # ]:           0 :     FuzzedFileProvider(FuzzedDataProvider& fuzzed_data_provider) : m_fuzzed_data_provider{fuzzed_data_provider}
     253                 :             :     {
     254                 :             :     }
     255                 :             : 
     256                 :             :     FILE* open();
     257                 :             : 
     258                 :             :     static ssize_t read(void* cookie, char* buf, size_t size);
     259                 :             : 
     260                 :             :     static ssize_t write(void* cookie, const char* buf, size_t size);
     261                 :             : 
     262                 :             :     static int seek(void* cookie, int64_t* offset, int whence);
     263                 :             : 
     264                 :             :     static int close(void* cookie);
     265                 :             : };
     266                 :             : 
     267                 :             : #define WRITE_TO_STREAM_CASE(type, consume) \
     268                 :             :     [&] {                                   \
     269                 :             :         type o = consume;                   \
     270                 :             :         stream << o;                        \
     271                 :             :     }
     272                 :             : template <typename Stream>
     273                 :           0 : void WriteToStream(FuzzedDataProvider& fuzzed_data_provider, Stream& stream) noexcept
     274                 :             : {
     275         [ #  # ]:           0 :     while (fuzzed_data_provider.ConsumeBool()) {
     276                 :             :         try {
     277         [ #  # ]:           0 :             CallOneOf(
     278                 :             :                 fuzzed_data_provider,
     279                 :           0 :                 WRITE_TO_STREAM_CASE(bool, fuzzed_data_provider.ConsumeBool()),
     280                 :           0 :                 WRITE_TO_STREAM_CASE(int8_t, fuzzed_data_provider.ConsumeIntegral<int8_t>()),
     281                 :           0 :                 WRITE_TO_STREAM_CASE(uint8_t, fuzzed_data_provider.ConsumeIntegral<uint8_t>()),
     282                 :           0 :                 WRITE_TO_STREAM_CASE(int16_t, fuzzed_data_provider.ConsumeIntegral<int16_t>()),
     283                 :           0 :                 WRITE_TO_STREAM_CASE(uint16_t, fuzzed_data_provider.ConsumeIntegral<uint16_t>()),
     284                 :           0 :                 WRITE_TO_STREAM_CASE(int32_t, fuzzed_data_provider.ConsumeIntegral<int32_t>()),
     285                 :           0 :                 WRITE_TO_STREAM_CASE(uint32_t, fuzzed_data_provider.ConsumeIntegral<uint32_t>()),
     286                 :           0 :                 WRITE_TO_STREAM_CASE(int64_t, fuzzed_data_provider.ConsumeIntegral<int64_t>()),
     287                 :           0 :                 WRITE_TO_STREAM_CASE(uint64_t, fuzzed_data_provider.ConsumeIntegral<uint64_t>()),
     288         [ #  # ]:           0 :                 WRITE_TO_STREAM_CASE(std::string, fuzzed_data_provider.ConsumeRandomLengthString(32)),
     289         [ #  # ]:           0 :                 WRITE_TO_STREAM_CASE(std::vector<uint8_t>, ConsumeRandomLengthIntegralVector<uint8_t>(fuzzed_data_provider)));
     290         [ -  - ]:           0 :         } catch (const std::ios_base::failure&) {
     291                 :             :             break;
     292                 :             :         }
     293                 :             :     }
     294                 :           0 : }
     295                 :             : 
     296                 :             : #define READ_FROM_STREAM_CASE(type) \
     297                 :             :     [&] {                           \
     298                 :             :         type o;                     \
     299                 :             :         stream >> o;                \
     300                 :             :     }
     301                 :             : template <typename Stream>
     302                 :           0 : void ReadFromStream(FuzzedDataProvider& fuzzed_data_provider, Stream& stream) noexcept
     303                 :             : {
     304         [ #  # ]:           0 :     while (fuzzed_data_provider.ConsumeBool()) {
     305                 :             :         try {
     306         [ #  # ]:           0 :             CallOneOf(
     307                 :             :                 fuzzed_data_provider,
     308                 :           0 :                 READ_FROM_STREAM_CASE(bool),
     309                 :           0 :                 READ_FROM_STREAM_CASE(int8_t),
     310                 :           0 :                 READ_FROM_STREAM_CASE(uint8_t),
     311                 :           0 :                 READ_FROM_STREAM_CASE(int16_t),
     312                 :           0 :                 READ_FROM_STREAM_CASE(uint16_t),
     313                 :           0 :                 READ_FROM_STREAM_CASE(int32_t),
     314                 :           0 :                 READ_FROM_STREAM_CASE(uint32_t),
     315                 :           0 :                 READ_FROM_STREAM_CASE(int64_t),
     316                 :           0 :                 READ_FROM_STREAM_CASE(uint64_t),
     317         [ #  # ]:           0 :                 READ_FROM_STREAM_CASE(std::string),
     318         [ #  # ]:           0 :                 READ_FROM_STREAM_CASE(std::vector<uint8_t>));
     319         [ -  - ]:           0 :         } catch (const std::ios_base::failure&) {
     320                 :             :             break;
     321                 :             :         }
     322                 :             :     }
     323                 :           0 : }
     324                 :             : 
     325                 :             : #endif // BITCOIN_TEST_FUZZ_UTIL_H
        

Generated by: LCOV version 2.0-1