LCOV - code coverage report
Current view: top level - src/test/fuzz - script_flags.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 100.0 % 46 46
Test Date: 2025-10-10 04:04:55 Functions: 100.0 % 3 3
Branches: 66.7 % 66 44

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2009-2021 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 <consensus/amount.h>
       6                 :             : #include <primitives/transaction.h>
       7                 :             : #include <script/interpreter.h>
       8                 :             : #include <serialize.h>
       9                 :             : #include <streams.h>
      10                 :             : #include <test/fuzz/fuzz.h>
      11                 :             : #include <test/util/script.h>
      12                 :             : 
      13                 :             : #include <cassert>
      14                 :             : #include <ios>
      15                 :             : #include <utility>
      16                 :             : #include <vector>
      17                 :             : 
      18                 :        4082 : static DataStream& operator>>(DataStream& ds, script_verify_flags& f)
      19                 :             : {
      20                 :        4082 :     script_verify_flags::value_type n{0};
      21                 :        4082 :     ds >> n;
      22         [ -  + ]:        4075 :     f = script_verify_flags::from_int(n);
      23         [ -  + ]:        4075 :     assert(n == f.as_int());
      24                 :        4075 :     return ds;
      25                 :             : }
      26                 :             : 
      27         [ +  - ]:        2523 : FUZZ_TARGET(script_flags)
      28                 :             : {
      29         [ +  + ]:        2067 :     if (buffer.size() > 100'000) return;
      30                 :        2066 :     DataStream ds{buffer};
      31                 :        2066 :     try {
      32         [ +  + ]:        2066 :         const CTransaction tx(deserialize, TX_WITH_WITNESS, ds);
      33                 :             : 
      34                 :        2045 :         script_verify_flags verify_flags;
      35         [ +  + ]:        2045 :         ds >> verify_flags;
      36                 :             : 
      37         [ +  - ]:        2039 :         assert(verify_flags == script_verify_flags::from_int(verify_flags.as_int()));
      38                 :             : 
      39   [ +  -  +  + ]:        2039 :         if (!IsValidFlagCombination(verify_flags)) return;
      40                 :             : 
      41                 :        2037 :         script_verify_flags fuzzed_flags;
      42         [ +  + ]:        2037 :         ds >> fuzzed_flags;
      43                 :             : 
      44                 :        2036 :         std::vector<CTxOut> spent_outputs;
      45   [ -  +  +  + ]:       83356 :         for (unsigned i = 0; i < tx.vin.size(); ++i) {
      46                 :       82630 :             CTxOut prevout;
      47         [ +  + ]:       82630 :             ds >> prevout;
      48         [ +  + ]:       81320 :             if (!MoneyRange(prevout.nValue)) {
      49                 :             :                 // prevouts should be consensus-valid
      50                 :       34297 :                 prevout.nValue = 1;
      51                 :             :             }
      52         [ +  - ]:       81320 :             spent_outputs.push_back(prevout);
      53                 :       82630 :         }
      54                 :         726 :         PrecomputedTransactionData txdata;
      55         [ +  - ]:         726 :         txdata.Init(tx, std::move(spent_outputs));
      56                 :             : 
      57   [ -  +  +  + ]:       39775 :         for (unsigned i = 0; i < tx.vin.size(); ++i) {
      58         [ +  - ]:       39196 :             const CTxOut& prevout = txdata.m_spent_outputs.at(i);
      59         [ +  - ]:       39196 :             const TransactionSignatureChecker checker{&tx, i, prevout.nValue, txdata, MissingDataBehavior::ASSERT_FAIL};
      60                 :             : 
      61                 :       39196 :             ScriptError serror;
      62   [ +  -  +  -  :       39196 :             const bool ret = VerifyScript(tx.vin.at(i).scriptSig, prevout.scriptPubKey, &tx.vin.at(i).scriptWitness, verify_flags, checker, &serror);
                   +  - ]
      63         [ -  + ]:       39196 :             assert(ret == (serror == SCRIPT_ERR_OK));
      64                 :             : 
      65                 :             :             // Verify that removing flags from a passing test or adding flags to a failing test does not change the result
      66         [ +  + ]:       39196 :             if (ret) {
      67                 :        4982 :                 verify_flags &= ~fuzzed_flags;
      68                 :             :             } else {
      69                 :       34214 :                 verify_flags |= fuzzed_flags;
      70                 :             :             }
      71   [ +  -  +  + ]:       39196 :             if (!IsValidFlagCombination(verify_flags)) return;
      72                 :             : 
      73                 :       39049 :             ScriptError serror_fuzzed;
      74   [ +  -  +  -  :       39049 :             const bool ret_fuzzed = VerifyScript(tx.vin.at(i).scriptSig, prevout.scriptPubKey, &tx.vin.at(i).scriptWitness, verify_flags, checker, &serror_fuzzed);
                   +  - ]
      75         [ -  + ]:       39049 :             assert(ret_fuzzed == (serror_fuzzed == SCRIPT_ERR_OK));
      76                 :             : 
      77         [ -  + ]:       39049 :             assert(ret_fuzzed == ret);
      78                 :       39196 :         }
      79         [ -  + ]:        4102 :     } catch (const std::ios_base::failure&) {
      80                 :        1338 :         return;
      81                 :        1338 :     }
      82                 :        2066 : }
        

Generated by: LCOV version 2.0-1