LCOV - code coverage report
Current view: top level - src/test/fuzz - coins_view.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 4 201 2.0 %
Date: 2023-11-12 01:39:15 Functions: 1 25 4.0 %
Branches: 3 300 1.0 %

           Branch data     Line data    Source code
       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 <coins.h>
       7                 :            : #include <consensus/amount.h>
       8                 :            : #include <consensus/tx_check.h>
       9                 :            : #include <consensus/tx_verify.h>
      10                 :            : #include <consensus/validation.h>
      11                 :            : #include <key.h>
      12                 :            : #include <policy/policy.h>
      13                 :            : #include <primitives/transaction.h>
      14                 :            : #include <pubkey.h>
      15                 :            : #include <test/fuzz/FuzzedDataProvider.h>
      16                 :            : #include <test/fuzz/fuzz.h>
      17         [ +  - ]:          2 : #include <test/fuzz/util.h>
      18         [ +  - ]:          2 : #include <test/util/setup_common.h>
      19                 :            : #include <validation.h>
      20                 :            : 
      21                 :            : #include <cstdint>
      22                 :            : #include <limits>
      23                 :            : #include <optional>
      24                 :            : #include <string>
      25                 :            : #include <vector>
      26                 :            : 
      27                 :            : namespace {
      28                 :            : const TestingSetup* g_setup;
      29                 :          2 : const Coin EMPTY_COIN{};
      30                 :            : 
      31                 :          0 : bool operator==(const Coin& a, const Coin& b)
      32                 :            : {
      33 [ #  # ][ #  # ]:          0 :     if (a.IsSpent() && b.IsSpent()) return true;
      34 [ #  # ][ #  # ]:          0 :     return a.fCoinBase == b.fCoinBase && a.nHeight == b.nHeight && a.out == b.out;
      35                 :          0 : }
      36                 :            : } // namespace
      37                 :            : 
      38                 :          0 : void initialize_coins_view()
      39                 :            : {
      40 [ #  # ][ #  # ]:          0 :     static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
                 [ #  # ]
      41                 :          0 :     g_setup = testing_setup.get();
      42                 :          0 : }
      43                 :            : 
      44         [ +  - ]:          4 : FUZZ_TARGET(coins_view, .init = initialize_coins_view)
      45                 :            : {
      46                 :          0 :     FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
      47                 :          0 :     CCoinsView backend_coins_view;
      48         [ #  # ]:          0 :     CCoinsViewCache coins_view_cache{&backend_coins_view, /*deterministic=*/true};
      49         [ #  # ]:          0 :     COutPoint random_out_point;
      50         [ #  # ]:          0 :     Coin random_coin;
      51         [ #  # ]:          0 :     CMutableTransaction random_mutable_transaction;
      52 [ #  # ][ #  # ]:          0 :     LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
                 [ #  # ]
      53         [ #  # ]:          0 :         CallOneOf(
      54                 :            :             fuzzed_data_provider,
      55                 :          0 :             [&] {
      56         [ #  # ]:          0 :                 if (random_coin.IsSpent()) {
      57                 :          0 :                     return;
      58                 :            :                 }
      59                 :          0 :                 Coin coin = random_coin;
      60                 :          0 :                 bool expected_code_path = false;
      61         [ #  # ]:          0 :                 const bool possible_overwrite = fuzzed_data_provider.ConsumeBool();
      62                 :            :                 try {
      63         [ #  # ]:          0 :                     coins_view_cache.AddCoin(random_out_point, std::move(coin), possible_overwrite);
      64                 :          0 :                     expected_code_path = true;
      65         [ #  # ]:          0 :                 } catch (const std::logic_error& e) {
      66 [ #  # ][ #  # ]:          0 :                     if (e.what() == std::string{"Attempted to overwrite an unspent coin (when possible_overwrite is false)"}) {
                 [ #  # ]
      67         [ #  # ]:          0 :                         assert(!possible_overwrite);
      68                 :          0 :                         expected_code_path = true;
      69                 :          0 :                     }
      70 [ #  # ][ #  # ]:          0 :                 }
      71         [ #  # ]:          0 :                 assert(expected_code_path);
      72                 :          0 :             },
      73                 :          0 :             [&] {
      74                 :          0 :                 (void)coins_view_cache.Flush();
      75                 :          0 :             },
      76                 :          0 :             [&] {
      77                 :          0 :                 (void)coins_view_cache.Sync();
      78                 :          0 :             },
      79                 :          0 :             [&] {
      80                 :          0 :                 coins_view_cache.SetBestBlock(ConsumeUInt256(fuzzed_data_provider));
      81                 :          0 :             },
      82                 :          0 :             [&] {
      83                 :          0 :                 Coin move_to;
      84 [ #  # ][ #  # ]:          0 :                 (void)coins_view_cache.SpendCoin(random_out_point, fuzzed_data_provider.ConsumeBool() ? &move_to : nullptr);
                 [ #  # ]
      85                 :          0 :             },
      86                 :          0 :             [&] {
      87                 :          0 :                 coins_view_cache.Uncache(random_out_point);
      88                 :          0 :             },
      89                 :          0 :             [&] {
      90         [ #  # ]:          0 :                 if (fuzzed_data_provider.ConsumeBool()) {
      91                 :          0 :                     backend_coins_view = CCoinsView{};
      92                 :          0 :                 }
      93                 :          0 :                 coins_view_cache.SetBackend(backend_coins_view);
      94                 :          0 :             },
      95                 :          0 :             [&] {
      96                 :          0 :                 const std::optional<COutPoint> opt_out_point = ConsumeDeserializable<COutPoint>(fuzzed_data_provider);
      97         [ #  # ]:          0 :                 if (!opt_out_point) {
      98                 :          0 :                     return;
      99                 :            :                 }
     100                 :          0 :                 random_out_point = *opt_out_point;
     101                 :          0 :             },
     102                 :          0 :             [&] {
     103                 :          0 :                 const std::optional<Coin> opt_coin = ConsumeDeserializable<Coin>(fuzzed_data_provider);
     104         [ #  # ]:          0 :                 if (!opt_coin) {
     105                 :          0 :                     return;
     106                 :            :                 }
     107         [ #  # ]:          0 :                 random_coin = *opt_coin;
     108         [ #  # ]:          0 :             },
     109                 :          0 :             [&] {
     110                 :          0 :                 const std::optional<CMutableTransaction> opt_mutable_transaction = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider);
     111         [ #  # ]:          0 :                 if (!opt_mutable_transaction) {
     112                 :          0 :                     return;
     113                 :            :                 }
     114         [ #  # ]:          0 :                 random_mutable_transaction = *opt_mutable_transaction;
     115         [ #  # ]:          0 :             },
     116                 :          0 :             [&] {
     117                 :          0 :                 CCoinsMapMemoryResource resource;
     118 [ #  # ][ #  # ]:          0 :                 CCoinsMap coins_map{0, SaltedOutpointHasher{/*deterministic=*/true}, CCoinsMap::key_equal{}, &resource};
     119 [ #  # ][ #  # ]:          0 :                 LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
     120         [ #  # ]:          0 :                     CCoinsCacheEntry coins_cache_entry;
     121         [ #  # ]:          0 :                     coins_cache_entry.flags = fuzzed_data_provider.ConsumeIntegral<unsigned char>();
     122 [ #  # ][ #  # ]:          0 :                     if (fuzzed_data_provider.ConsumeBool()) {
     123         [ #  # ]:          0 :                         coins_cache_entry.coin = random_coin;
     124                 :          0 :                     } else {
     125                 :          0 :                         const std::optional<Coin> opt_coin = ConsumeDeserializable<Coin>(fuzzed_data_provider);
     126         [ #  # ]:          0 :                         if (!opt_coin) {
     127                 :          0 :                             return;
     128                 :            :                         }
     129         [ #  # ]:          0 :                         coins_cache_entry.coin = *opt_coin;
     130         [ #  # ]:          0 :                     }
     131         [ #  # ]:          0 :                     coins_map.emplace(random_out_point, std::move(coins_cache_entry));
     132                 :          0 :                 }
     133                 :          0 :                 bool expected_code_path = false;
     134                 :            :                 try {
     135 [ #  # ][ #  # ]:          0 :                     coins_view_cache.BatchWrite(coins_map, fuzzed_data_provider.ConsumeBool() ? ConsumeUInt256(fuzzed_data_provider) : coins_view_cache.GetBestBlock());
         [ #  # ][ #  # ]
     136                 :          0 :                     expected_code_path = true;
     137         [ #  # ]:          0 :                 } catch (const std::logic_error& e) {
     138 [ #  # ][ #  # ]:          0 :                     if (e.what() == std::string{"FRESH flag misapplied to coin that exists in parent cache"}) {
                 [ #  # ]
     139                 :          0 :                         expected_code_path = true;
     140                 :          0 :                     }
     141 [ #  # ][ #  # ]:          0 :                 }
     142         [ #  # ]:          0 :                 assert(expected_code_path);
     143         [ #  # ]:          0 :             });
     144                 :          0 :     }
     145                 :            : 
     146                 :            :     {
     147         [ #  # ]:          0 :         const Coin& coin_using_access_coin = coins_view_cache.AccessCoin(random_out_point);
     148         [ #  # ]:          0 :         const bool exists_using_access_coin = !(coin_using_access_coin == EMPTY_COIN);
     149         [ #  # ]:          0 :         const bool exists_using_have_coin = coins_view_cache.HaveCoin(random_out_point);
     150         [ #  # ]:          0 :         const bool exists_using_have_coin_in_cache = coins_view_cache.HaveCoinInCache(random_out_point);
     151         [ #  # ]:          0 :         Coin coin_using_get_coin;
     152         [ #  # ]:          0 :         const bool exists_using_get_coin = coins_view_cache.GetCoin(random_out_point, coin_using_get_coin);
     153         [ #  # ]:          0 :         if (exists_using_get_coin) {
     154 [ #  # ][ #  # ]:          0 :             assert(coin_using_get_coin == coin_using_access_coin);
     155                 :          0 :         }
     156 [ #  # ][ #  # ]:          0 :         assert((exists_using_access_coin && exists_using_have_coin_in_cache && exists_using_have_coin && exists_using_get_coin) ||
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     157                 :            :                (!exists_using_access_coin && !exists_using_have_coin_in_cache && !exists_using_have_coin && !exists_using_get_coin));
     158                 :            :         // If HaveCoin on the backend is true, it must also be on the cache if the coin wasn't spent.
     159         [ #  # ]:          0 :         const bool exists_using_have_coin_in_backend = backend_coins_view.HaveCoin(random_out_point);
     160 [ #  # ][ #  # ]:          0 :         if (!coin_using_access_coin.IsSpent() && exists_using_have_coin_in_backend) {
                 [ #  # ]
     161         [ #  # ]:          0 :             assert(exists_using_have_coin);
     162                 :          0 :         }
     163         [ #  # ]:          0 :         Coin coin_using_backend_get_coin;
     164 [ #  # ][ #  # ]:          0 :         if (backend_coins_view.GetCoin(random_out_point, coin_using_backend_get_coin)) {
     165         [ #  # ]:          0 :             assert(exists_using_have_coin_in_backend);
     166                 :            :             // Note we can't assert that `coin_using_get_coin == coin_using_backend_get_coin` because the coin in
     167                 :            :             // the cache may have been modified but not yet flushed.
     168                 :          0 :         } else {
     169         [ #  # ]:          0 :             assert(!exists_using_have_coin_in_backend);
     170                 :            :         }
     171                 :          0 :     }
     172                 :            : 
     173                 :            :     {
     174                 :          0 :         bool expected_code_path = false;
     175                 :            :         try {
     176         [ #  # ]:          0 :             (void)coins_view_cache.Cursor();
     177         [ #  # ]:          0 :         } catch (const std::logic_error&) {
     178                 :          0 :             expected_code_path = true;
     179         [ #  # ]:          0 :         }
     180         [ #  # ]:          0 :         assert(expected_code_path);
     181         [ #  # ]:          0 :         (void)coins_view_cache.DynamicMemoryUsage();
     182         [ #  # ]:          0 :         (void)coins_view_cache.EstimateSize();
     183         [ #  # ]:          0 :         (void)coins_view_cache.GetBestBlock();
     184         [ #  # ]:          0 :         (void)coins_view_cache.GetCacheSize();
     185         [ #  # ]:          0 :         (void)coins_view_cache.GetHeadBlocks();
     186 [ #  # ][ #  # ]:          0 :         (void)coins_view_cache.HaveInputs(CTransaction{random_mutable_transaction});
     187                 :            :     }
     188                 :            : 
     189                 :            :     {
     190         [ #  # ]:          0 :         std::unique_ptr<CCoinsViewCursor> coins_view_cursor = backend_coins_view.Cursor();
     191         [ #  # ]:          0 :         assert(!coins_view_cursor);
     192         [ #  # ]:          0 :         (void)backend_coins_view.EstimateSize();
     193         [ #  # ]:          0 :         (void)backend_coins_view.GetBestBlock();
     194         [ #  # ]:          0 :         (void)backend_coins_view.GetHeadBlocks();
     195                 :          0 :     }
     196                 :            : 
     197 [ #  # ][ #  # ]:          0 :     if (fuzzed_data_provider.ConsumeBool()) {
     198         [ #  # ]:          0 :         CallOneOf(
     199                 :            :             fuzzed_data_provider,
     200                 :          0 :             [&] {
     201                 :          0 :                 const CTransaction transaction{random_mutable_transaction};
     202                 :          0 :                 bool is_spent = false;
     203         [ #  # ]:          0 :                 for (const CTxOut& tx_out : transaction.vout) {
     204 [ #  # ][ #  # ]:          0 :                     if (Coin{tx_out, 0, transaction.IsCoinBase()}.IsSpent()) {
                 [ #  # ]
     205                 :          0 :                         is_spent = true;
     206                 :          0 :                     }
     207                 :            :                 }
     208         [ #  # ]:          0 :                 if (is_spent) {
     209                 :            :                     // Avoid:
     210                 :            :                     // coins.cpp:69: void CCoinsViewCache::AddCoin(const COutPoint &, Coin &&, bool): Assertion `!coin.IsSpent()' failed.
     211                 :          0 :                     return;
     212                 :            :                 }
     213                 :          0 :                 bool expected_code_path = false;
     214         [ #  # ]:          0 :                 const int height{int(fuzzed_data_provider.ConsumeIntegral<uint32_t>() >> 1)};
     215         [ #  # ]:          0 :                 const bool possible_overwrite = fuzzed_data_provider.ConsumeBool();
     216                 :            :                 try {
     217         [ #  # ]:          0 :                     AddCoins(coins_view_cache, transaction, height, possible_overwrite);
     218                 :          0 :                     expected_code_path = true;
     219         [ #  # ]:          0 :                 } catch (const std::logic_error& e) {
     220 [ #  # ][ #  # ]:          0 :                     if (e.what() == std::string{"Attempted to overwrite an unspent coin (when possible_overwrite is false)"}) {
                 [ #  # ]
     221         [ #  # ]:          0 :                         assert(!possible_overwrite);
     222                 :          0 :                         expected_code_path = true;
     223                 :          0 :                     }
     224 [ #  # ][ #  # ]:          0 :                 }
     225         [ #  # ]:          0 :                 assert(expected_code_path);
     226         [ #  # ]:          0 :             },
     227                 :          0 :             [&] {
     228         [ #  # ]:          0 :                 (void)AreInputsStandard(CTransaction{random_mutable_transaction}, coins_view_cache);
     229                 :          0 :             },
     230                 :          0 :             [&] {
     231                 :          0 :                 TxValidationState state;
     232                 :            :                 CAmount tx_fee_out;
     233         [ #  # ]:          0 :                 const CTransaction transaction{random_mutable_transaction};
     234         [ #  # ]:          0 :                 if (ContainsSpentInput(transaction, coins_view_cache)) {
     235                 :            :                     // Avoid:
     236                 :            :                     // consensus/tx_verify.cpp:171: bool Consensus::CheckTxInputs(const CTransaction &, TxValidationState &, const CCoinsViewCache &, int, CAmount &): Assertion `!coin.IsSpent()' failed.
     237                 :          0 :                     return;
     238                 :            :                 }
     239                 :          0 :                 TxValidationState dummy;
     240 [ #  # ][ #  # ]:          0 :                 if (!CheckTransaction(transaction, dummy)) {
     241                 :            :                     // It is not allowed to call CheckTxInputs if CheckTransaction failed
     242                 :          0 :                     return;
     243                 :            :                 }
     244 [ #  # ][ #  # ]:          0 :                 if (Consensus::CheckTxInputs(transaction, state, coins_view_cache, fuzzed_data_provider.ConsumeIntegralInRange<int>(0, std::numeric_limits<int>::max()), tx_fee_out)) {
                 [ #  # ]
     245 [ #  # ][ #  # ]:          0 :                     assert(MoneyRange(tx_fee_out));
     246                 :          0 :                 }
     247         [ #  # ]:          0 :             },
     248                 :          0 :             [&] {
     249                 :          0 :                 const CTransaction transaction{random_mutable_transaction};
     250         [ #  # ]:          0 :                 if (ContainsSpentInput(transaction, coins_view_cache)) {
     251                 :            :                     // Avoid:
     252                 :            :                     // consensus/tx_verify.cpp:130: unsigned int GetP2SHSigOpCount(const CTransaction &, const CCoinsViewCache &): Assertion `!coin.IsSpent()' failed.
     253                 :          0 :                     return;
     254                 :            :                 }
     255         [ #  # ]:          0 :                 (void)GetP2SHSigOpCount(transaction, coins_view_cache);
     256         [ #  # ]:          0 :             },
     257                 :          0 :             [&] {
     258                 :          0 :                 const CTransaction transaction{random_mutable_transaction};
     259         [ #  # ]:          0 :                 if (ContainsSpentInput(transaction, coins_view_cache)) {
     260                 :            :                     // Avoid:
     261                 :            :                     // consensus/tx_verify.cpp:130: unsigned int GetP2SHSigOpCount(const CTransaction &, const CCoinsViewCache &): Assertion `!coin.IsSpent()' failed.
     262                 :          0 :                     return;
     263                 :            :                 }
     264         [ #  # ]:          0 :                 const auto flags{fuzzed_data_provider.ConsumeIntegral<uint32_t>()};
     265 [ #  # ][ #  # ]:          0 :                 if (!transaction.vin.empty() && (flags & SCRIPT_VERIFY_WITNESS) != 0 && (flags & SCRIPT_VERIFY_P2SH) == 0) {
                 [ #  # ]
     266                 :            :                     // Avoid:
     267                 :            :                     // script/interpreter.cpp:1705: size_t CountWitnessSigOps(const CScript &, const CScript &, const CScriptWitness *, unsigned int): Assertion `(flags & SCRIPT_VERIFY_P2SH) != 0' failed.
     268                 :          0 :                     return;
     269                 :            :                 }
     270         [ #  # ]:          0 :                 (void)GetTransactionSigOpCost(transaction, coins_view_cache, flags);
     271         [ #  # ]:          0 :             },
     272                 :          0 :             [&] {
     273         [ #  # ]:          0 :                 (void)IsWitnessStandard(CTransaction{random_mutable_transaction}, coins_view_cache);
     274                 :          0 :             });
     275                 :          0 :     }
     276                 :          0 : }

Generated by: LCOV version 1.14