LCOV - code coverage report
Current view: top level - src/test/fuzz - coins_view.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 99.0 % 208 206
Test Date: 2025-10-19 04:10:16 Functions: 100.0 % 21 21
Branches: 69.3 % 238 165

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2020-present 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 <coins.h>
       6                 :             : #include <consensus/amount.h>
       7                 :             : #include <consensus/tx_check.h>
       8                 :             : #include <consensus/tx_verify.h>
       9                 :             : #include <consensus/validation.h>
      10                 :             : #include <policy/policy.h>
      11                 :             : #include <primitives/transaction.h>
      12                 :             : #include <script/interpreter.h>
      13                 :             : #include <test/fuzz/FuzzedDataProvider.h>
      14                 :             : #include <test/fuzz/fuzz.h>
      15                 :             : #include <test/fuzz/util.h>
      16                 :             : #include <test/util/setup_common.h>
      17                 :             : #include <txdb.h>
      18                 :             : #include <util/hasher.h>
      19                 :             : 
      20                 :             : #include <cassert>
      21                 :             : #include <cstdint>
      22                 :             : #include <limits>
      23                 :             : #include <memory>
      24                 :             : #include <optional>
      25                 :             : #include <stdexcept>
      26                 :             : #include <string>
      27                 :             : #include <utility>
      28                 :             : #include <vector>
      29                 :             : 
      30                 :             : namespace {
      31                 :             : const Coin EMPTY_COIN{};
      32                 :             : 
      33                 :       10119 : bool operator==(const Coin& a, const Coin& b)
      34                 :             : {
      35   [ +  +  -  + ]:       10119 :     if (a.IsSpent() && b.IsSpent()) return true;
      36   [ +  +  +  +  :        8710 :     return a.fCoinBase == b.fCoinBase && a.nHeight == b.nHeight && a.out == b.out;
                   +  + ]
      37                 :             : }
      38                 :             : } // namespace
      39                 :             : 
      40                 :           2 : void initialize_coins_view()
      41                 :             : {
      42   [ +  -  +  -  :           2 :     static const auto testing_setup = MakeNoLogFileContext<>();
                   +  - ]
      43                 :           2 : }
      44                 :             : 
      45                 :        5764 : void TestCoinsView(FuzzedDataProvider& fuzzed_data_provider, CCoinsView& backend_coins_view, bool is_db)
      46                 :             : {
      47                 :        5764 :     bool good_data{true};
      48                 :             : 
      49                 :        5764 :     CCoinsViewCache coins_view_cache{&backend_coins_view, /*deterministic=*/true};
      50   [ +  +  +  - ]:        5764 :     if (is_db) coins_view_cache.SetBestBlock(uint256::ONE);
      51                 :        5764 :     COutPoint random_out_point;
      52                 :        5764 :     Coin random_coin;
      53         [ +  - ]:        5764 :     CMutableTransaction random_mutable_transaction;
      54   [ +  +  +  +  :     1110646 :     LIMITED_WHILE(good_data && fuzzed_data_provider.ConsumeBool(), 10'000)
                   +  + ]
      55                 :             :     {
      56         [ +  - ]:      550040 :         CallOneOf(
      57                 :             :             fuzzed_data_provider,
      58                 :       60904 :             [&] {
      59         [ +  + ]:       60904 :                 if (random_coin.IsSpent()) {
      60                 :             :                     return;
      61                 :             :                 }
      62                 :       14893 :                 COutPoint outpoint{random_out_point};
      63                 :       14893 :                 Coin coin{random_coin};
      64         [ +  + ]:       14893 :                 if (fuzzed_data_provider.ConsumeBool()) {
      65                 :       10061 :                     const bool possible_overwrite{fuzzed_data_provider.ConsumeBool()};
      66                 :       10061 :                     try {
      67         [ +  + ]:       10061 :                         coins_view_cache.AddCoin(outpoint, std::move(coin), possible_overwrite);
      68         [ -  + ]:         122 :                     } catch (const std::logic_error& e) {
      69   [ +  -  -  + ]:         122 :                         assert(e.what() == std::string{"Attempted to overwrite an unspent coin (when possible_overwrite is false)"});
      70         [ -  + ]:         122 :                         assert(!possible_overwrite);
      71                 :         122 :                     }
      72                 :             :                 } else {
      73         [ +  - ]:        4832 :                     coins_view_cache.EmplaceCoinInternalDANGER(std::move(outpoint), std::move(coin));
      74                 :             :                 }
      75                 :       14893 :             },
      76                 :      140787 :             [&] {
      77                 :      140787 :                 (void)coins_view_cache.Flush();
      78                 :      140787 :             },
      79                 :      223471 :             [&] {
      80                 :      223471 :                 (void)coins_view_cache.Sync();
      81                 :      223471 :             },
      82                 :        8866 :             [&] {
      83                 :        8866 :                 uint256 best_block{ConsumeUInt256(fuzzed_data_provider)};
      84                 :             :                 // Set best block hash to non-null to satisfy the assertion in CCoinsViewDB::BatchWrite().
      85   [ +  +  +  + ]:       13246 :                 if (is_db && best_block.IsNull()) best_block = uint256::ONE;
      86                 :        8866 :                 coins_view_cache.SetBestBlock(best_block);
      87                 :        8866 :             },
      88                 :       28634 :             [&] {
      89                 :       28634 :                 Coin move_to;
      90   [ +  +  +  - ]:       30848 :                 (void)coins_view_cache.SpendCoin(random_out_point, fuzzed_data_provider.ConsumeBool() ? &move_to : nullptr);
      91                 :       28634 :             },
      92                 :       13559 :             [&] {
      93                 :       13559 :                 coins_view_cache.Uncache(random_out_point);
      94                 :       13559 :             },
      95                 :        2488 :             [&] {
      96         [ +  + ]:        2488 :                 if (fuzzed_data_provider.ConsumeBool()) {
      97                 :        1386 :                     backend_coins_view = CCoinsView{};
      98                 :             :                 }
      99                 :        2488 :                 coins_view_cache.SetBackend(backend_coins_view);
     100                 :        2488 :             },
     101                 :        3303 :             [&] {
     102                 :        3303 :                 const std::optional<COutPoint> opt_out_point = ConsumeDeserializable<COutPoint>(fuzzed_data_provider);
     103         [ +  + ]:        3303 :                 if (!opt_out_point) {
     104                 :         148 :                     good_data = false;
     105                 :         148 :                     return;
     106                 :             :                 }
     107                 :        3155 :                 random_out_point = *opt_out_point;
     108                 :             :             },
     109                 :       19583 :             [&] {
     110                 :       19583 :                 const std::optional<Coin> opt_coin = ConsumeDeserializable<Coin>(fuzzed_data_provider);
     111         [ +  + ]:       19583 :                 if (!opt_coin) {
     112                 :         229 :                     good_data = false;
     113                 :         229 :                     return;
     114                 :             :                 }
     115                 :       19354 :                 random_coin = *opt_coin;
     116                 :       19583 :             },
     117                 :       12887 :             [&] {
     118                 :       12887 :                 const std::optional<CMutableTransaction> opt_mutable_transaction = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
     119         [ +  + ]:       12887 :                 if (!opt_mutable_transaction) {
     120                 :         467 :                     good_data = false;
     121         [ -  + ]:         467 :                     return;
     122                 :             :                 }
     123         [ +  - ]:       12420 :                 random_mutable_transaction = *opt_mutable_transaction;
     124                 :       12887 :             },
     125                 :       35558 :             [&] {
     126                 :       35558 :                 CoinsCachePair sentinel{};
     127                 :       35558 :                 sentinel.second.SelfRef(sentinel);
     128         [ +  - ]:       35558 :                 CCoinsMapMemoryResource resource;
     129   [ +  -  +  - ]:       35558 :                 CCoinsMap coins_map{0, SaltedOutpointHasher{/*deterministic=*/true}, CCoinsMap::key_equal{}, &resource};
     130   [ +  -  +  +  :      522095 :                 LIMITED_WHILE(good_data && fuzzed_data_provider.ConsumeBool(), 10'000)
                   +  + ]
     131                 :             :                 {
     132                 :      486655 :                     CCoinsCacheEntry coins_cache_entry;
     133                 :      486655 :                     const auto dirty{fuzzed_data_provider.ConsumeBool()};
     134                 :      486655 :                     const auto fresh{fuzzed_data_provider.ConsumeBool()};
     135         [ +  + ]:      486655 :                     if (fuzzed_data_provider.ConsumeBool()) {
     136                 :      478410 :                         coins_cache_entry.coin = random_coin;
     137                 :             :                     } else {
     138                 :        8245 :                         const std::optional<Coin> opt_coin = ConsumeDeserializable<Coin>(fuzzed_data_provider);
     139         [ +  + ]:        8245 :                         if (!opt_coin) {
     140                 :         118 :                             good_data = false;
     141                 :         118 :                             return;
     142                 :             :                         }
     143                 :        8127 :                         coins_cache_entry.coin = *opt_coin;
     144                 :        8127 :                     }
     145         [ +  - ]:      486537 :                     auto it{coins_map.emplace(random_out_point, std::move(coins_cache_entry)).first};
     146         [ +  + ]:      486537 :                     if (dirty) CCoinsCacheEntry::SetDirty(*it, sentinel);
     147         [ +  + ]:      486537 :                     if (fresh) CCoinsCacheEntry::SetFresh(*it, sentinel);
     148                 :      486655 :                 }
     149                 :       35440 :                 bool expected_code_path = false;
     150                 :       35440 :                 try {
     151         [ +  - ]:       35440 :                     auto cursor{CoinsViewCacheCursor(sentinel, coins_map, /*will_erase=*/true)};
     152         [ +  - ]:       35440 :                     uint256 best_block{coins_view_cache.GetBestBlock()};
     153         [ +  + ]:       35440 :                     if (fuzzed_data_provider.ConsumeBool()) best_block = ConsumeUInt256(fuzzed_data_provider);
     154                 :             :                     // Set best block hash to non-null to satisfy the assertion in CCoinsViewDB::BatchWrite().
     155   [ +  +  +  + ]:       46339 :                     if (is_db && best_block.IsNull()) best_block = uint256::ONE;
     156         [ +  + ]:       35440 :                     coins_view_cache.BatchWrite(cursor, best_block);
     157                 :             :                     expected_code_path = true;
     158         [ -  + ]:        2301 :                 } catch (const std::logic_error& e) {
     159   [ +  -  +  - ]:        2301 :                     if (e.what() == std::string{"FRESH flag misapplied to coin that exists in parent cache"}) {
     160                 :        2301 :                         expected_code_path = true;
     161                 :             :                     }
     162                 :           0 :                 }
     163                 :        2301 :                 assert(expected_code_path);
     164                 :       35558 :             });
     165                 :             :     }
     166                 :             : 
     167                 :        5764 :     {
     168         [ +  - ]:        5764 :         const Coin& coin_using_access_coin = coins_view_cache.AccessCoin(random_out_point);
     169                 :        5764 :         const bool exists_using_access_coin = !(coin_using_access_coin == EMPTY_COIN);
     170         [ +  - ]:        5764 :         const bool exists_using_have_coin = coins_view_cache.HaveCoin(random_out_point);
     171         [ +  - ]:        5764 :         const bool exists_using_have_coin_in_cache = coins_view_cache.HaveCoinInCache(random_out_point);
     172   [ +  -  +  + ]:        5764 :         if (auto coin{coins_view_cache.GetCoin(random_out_point)}) {
     173         [ -  + ]:        4355 :             assert(*coin == coin_using_access_coin);
     174   [ +  -  -  + ]:        4355 :             assert(exists_using_access_coin && exists_using_have_coin_in_cache && exists_using_have_coin);
     175                 :             :         } else {
     176   [ +  -  -  + ]:        1409 :             assert(!exists_using_access_coin && !exists_using_have_coin_in_cache && !exists_using_have_coin);
     177                 :        5764 :         }
     178                 :             :         // If HaveCoin on the backend is true, it must also be on the cache if the coin wasn't spent.
     179         [ +  - ]:        5764 :         const bool exists_using_have_coin_in_backend = backend_coins_view.HaveCoin(random_out_point);
     180   [ +  +  +  + ]:        5764 :         if (!coin_using_access_coin.IsSpent() && exists_using_have_coin_in_backend) {
     181         [ -  + ]:        1686 :             assert(exists_using_have_coin);
     182                 :             :         }
     183   [ +  -  +  + ]:        5764 :         if (auto coin{backend_coins_view.GetCoin(random_out_point)}) {
     184         [ -  + ]:        1722 :             assert(exists_using_have_coin_in_backend);
     185                 :             :             // Note we can't assert that `coin_using_get_coin == *coin` because the coin in
     186                 :             :             // the cache may have been modified but not yet flushed.
     187                 :             :         } else {
     188         [ -  + ]:        4042 :             assert(!exists_using_have_coin_in_backend);
     189                 :        5764 :         }
     190                 :             :     }
     191                 :             : 
     192                 :        5764 :     {
     193                 :        5764 :         bool expected_code_path = false;
     194                 :        5764 :         try {
     195                 :        5764 :             (void)coins_view_cache.Cursor();
     196         [ -  + ]:        5764 :         } catch (const std::logic_error&) {
     197                 :        5764 :             expected_code_path = true;
     198                 :        5764 :         }
     199                 :        5764 :         assert(expected_code_path);
     200         [ +  - ]:        5764 :         (void)coins_view_cache.DynamicMemoryUsage();
     201         [ +  - ]:        5764 :         (void)coins_view_cache.EstimateSize();
     202         [ +  - ]:        5764 :         (void)coins_view_cache.GetBestBlock();
     203         [ +  - ]:        5764 :         (void)coins_view_cache.GetCacheSize();
     204         [ +  - ]:        5764 :         (void)coins_view_cache.GetHeadBlocks();
     205   [ +  -  +  - ]:        5764 :         (void)coins_view_cache.HaveInputs(CTransaction{random_mutable_transaction});
     206                 :             :     }
     207                 :             : 
     208                 :        5764 :     {
     209         [ +  - ]:        5764 :         std::unique_ptr<CCoinsViewCursor> coins_view_cursor = backend_coins_view.Cursor();
     210         [ -  + ]:        5764 :         assert(is_db == !!coins_view_cursor);
     211         [ +  - ]:        5764 :         (void)backend_coins_view.EstimateSize();
     212         [ +  - ]:        5764 :         (void)backend_coins_view.GetBestBlock();
     213         [ +  - ]:        5764 :         (void)backend_coins_view.GetHeadBlocks();
     214                 :        5764 :     }
     215                 :             : 
     216         [ +  + ]:        5764 :     if (fuzzed_data_provider.ConsumeBool()) {
     217         [ +  - ]:        3047 :         CallOneOf(
     218                 :             :             fuzzed_data_provider,
     219                 :         299 :             [&] {
     220                 :         299 :                 const CTransaction transaction{random_mutable_transaction};
     221                 :         299 :                 bool is_spent = false;
     222         [ +  + ]:      645434 :                 for (const CTxOut& tx_out : transaction.vout) {
     223         [ +  + ]:      645135 :                     if (Coin{tx_out, 0, transaction.IsCoinBase()}.IsSpent()) {
     224                 :          47 :                         is_spent = true;
     225                 :             :                     }
     226                 :             :                 }
     227         [ +  + ]:         299 :                 if (is_spent) {
     228                 :             :                     // Avoid:
     229                 :             :                     // coins.cpp:69: void CCoinsViewCache::AddCoin(const COutPoint &, Coin &&, bool): Assertion `!coin.IsSpent()' failed.
     230                 :          15 :                     return;
     231                 :             :                 }
     232                 :         284 :                 bool expected_code_path = false;
     233                 :         284 :                 const int height{int(fuzzed_data_provider.ConsumeIntegral<uint32_t>() >> 1)};
     234                 :         284 :                 const bool possible_overwrite = fuzzed_data_provider.ConsumeBool();
     235                 :         284 :                 try {
     236         [ +  + ]:         284 :                     AddCoins(coins_view_cache, transaction, height, possible_overwrite);
     237                 :             :                     expected_code_path = true;
     238         [ -  + ]:           3 :                 } catch (const std::logic_error& e) {
     239   [ +  -  +  - ]:           3 :                     if (e.what() == std::string{"Attempted to overwrite an unspent coin (when possible_overwrite is false)"}) {
     240         [ -  + ]:           3 :                         assert(!possible_overwrite);
     241                 :             :                         expected_code_path = true;
     242                 :             :                     }
     243                 :           0 :                 }
     244                 :           3 :                 assert(expected_code_path);
     245                 :         299 :             },
     246                 :        1235 :             [&] {
     247         [ +  - ]:        1235 :                 (void)AreInputsStandard(CTransaction{random_mutable_transaction}, coins_view_cache);
     248                 :        1235 :             },
     249                 :         100 :             [&] {
     250         [ +  - ]:         100 :                 TxValidationState state;
     251                 :         100 :                 CAmount tx_fee_out;
     252         [ +  - ]:         100 :                 const CTransaction transaction{random_mutable_transaction};
     253         [ +  + ]:         100 :                 if (ContainsSpentInput(transaction, coins_view_cache)) {
     254                 :             :                     // Avoid:
     255                 :             :                     // consensus/tx_verify.cpp:171: bool Consensus::CheckTxInputs(const CTransaction &, TxValidationState &, const CCoinsViewCache &, int, CAmount &): Assertion `!coin.IsSpent()' failed.
     256                 :             :                     return;
     257                 :             :                 }
     258         [ +  - ]:          81 :                 TxValidationState dummy;
     259   [ +  -  +  + ]:          81 :                 if (!CheckTransaction(transaction, dummy)) {
     260                 :             :                     // It is not allowed to call CheckTxInputs if CheckTransaction failed
     261                 :          65 :                     return;
     262                 :             :                 }
     263   [ +  -  +  + ]:          16 :                 if (Consensus::CheckTxInputs(transaction, state, coins_view_cache, fuzzed_data_provider.ConsumeIntegralInRange<int>(0, std::numeric_limits<int>::max()), tx_fee_out)) {
     264         [ -  + ]:           1 :                     assert(MoneyRange(tx_fee_out));
     265                 :             :                 }
     266                 :         281 :             },
     267                 :         306 :             [&] {
     268                 :         306 :                 const CTransaction transaction{random_mutable_transaction};
     269         [ +  + ]:         306 :                 if (ContainsSpentInput(transaction, coins_view_cache)) {
     270                 :             :                     // Avoid:
     271                 :             :                     // consensus/tx_verify.cpp:130: unsigned int GetP2SHSigOpCount(const CTransaction &, const CCoinsViewCache &): Assertion `!coin.IsSpent()' failed.
     272                 :          80 :                     return;
     273                 :             :                 }
     274         [ +  - ]:         226 :                 (void)GetP2SHSigOpCount(transaction, coins_view_cache);
     275                 :         306 :             },
     276                 :         306 :             [&] {
     277                 :         306 :                 const CTransaction transaction{random_mutable_transaction};
     278         [ +  + ]:         306 :                 if (ContainsSpentInput(transaction, coins_view_cache)) {
     279                 :             :                     // Avoid:
     280                 :             :                     // consensus/tx_verify.cpp:130: unsigned int GetP2SHSigOpCount(const CTransaction &, const CCoinsViewCache &): Assertion `!coin.IsSpent()' failed.
     281                 :             :                     return;
     282                 :             :                 }
     283         [ +  + ]:         291 :                 const auto flags = script_verify_flags::from_int(fuzzed_data_provider.ConsumeIntegral<script_verify_flags::value_type>());
     284   [ +  +  +  +  :         291 :                 if (!transaction.vin.empty() && (flags & SCRIPT_VERIFY_WITNESS) != 0 && (flags & SCRIPT_VERIFY_P2SH) == 0) {
                   +  + ]
     285                 :             :                     // Avoid:
     286                 :             :                     // script/interpreter.cpp:1705: size_t CountWitnessSigOps(const CScript &, const CScript &, const CScriptWitness *, unsigned int): Assertion `(flags & SCRIPT_VERIFY_P2SH) != 0' failed.
     287                 :             :                     return;
     288                 :             :                 }
     289         [ +  - ]:         277 :                 (void)GetTransactionSigOpCost(transaction, coins_view_cache, flags);
     290                 :         306 :             },
     291                 :         801 :             [&] {
     292         [ +  - ]:         801 :                 (void)IsWitnessStandard(CTransaction{random_mutable_transaction}, coins_view_cache);
     293                 :         801 :             });
     294                 :             :     }
     295                 :        5764 : }
     296                 :             : 
     297         [ +  - ]:        3310 : FUZZ_TARGET(coins_view, .init = initialize_coins_view)
     298                 :             : {
     299         [ +  - ]:        2854 :     FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
     300                 :        2854 :     CCoinsView backend_coins_view;
     301         [ +  - ]:        2854 :     TestCoinsView(fuzzed_data_provider, backend_coins_view, /*is_db=*/false);
     302                 :        2854 : }
     303                 :             : 
     304         [ +  - ]:        3366 : FUZZ_TARGET(coins_view_db, .init = initialize_coins_view)
     305                 :             : {
     306                 :        2910 :     FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
     307                 :        2910 :     auto db_params = DBParams{
     308                 :             :         .path = "",
     309                 :             :         .cache_bytes = 1_MiB,
     310                 :             :         .memory_only = true,
     311                 :        2910 :     };
     312         [ +  - ]:        2910 :     CCoinsViewDB coins_db{std::move(db_params), CoinsViewOptions{}};
     313         [ +  - ]:        2910 :     TestCoinsView(fuzzed_data_provider, coins_db, /*is_db=*/true);
     314                 :        5820 : }
        

Generated by: LCOV version 2.0-1