|
| 1 | +// Copyright (c) 2009-2019 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 <script/interpreter.h> |
| 6 | +#include <script/script.h> |
| 7 | +#include <streams.h> |
| 8 | +#include <version.h> |
| 9 | + |
| 10 | +#include <test/fuzz/fuzz.h> |
| 11 | + |
| 12 | +/** Flags that are not forbidden by an assert */ |
| 13 | +static bool IsValidFlagCombination(unsigned flags); |
| 14 | + |
| 15 | +void test_one_input(std::vector<uint8_t> buffer) |
| 16 | +{ |
| 17 | + CDataStream ds(buffer, SER_NETWORK, INIT_PROTO_VERSION); |
| 18 | + try { |
| 19 | + int nVersion; |
| 20 | + ds >> nVersion; |
| 21 | + ds.SetVersion(nVersion); |
| 22 | + } catch (const std::ios_base::failure&) { |
| 23 | + return; |
| 24 | + } |
| 25 | + |
| 26 | + try { |
| 27 | + const CTransaction tx(deserialize, ds); |
| 28 | + const PrecomputedTransactionData txdata(tx); |
| 29 | + |
| 30 | + unsigned int verify_flags; |
| 31 | + ds >> verify_flags; |
| 32 | + |
| 33 | + if (!IsValidFlagCombination(verify_flags)) return; |
| 34 | + |
| 35 | + unsigned int fuzzed_flags; |
| 36 | + ds >> fuzzed_flags; |
| 37 | + |
| 38 | + for (unsigned i = 0; i < tx.vin.size(); ++i) { |
| 39 | + CTxOut prevout; |
| 40 | + ds >> prevout; |
| 41 | + |
| 42 | + const TransactionSignatureChecker checker{&tx, i, prevout.nValue, txdata}; |
| 43 | + |
| 44 | + ScriptError serror; |
| 45 | + const bool ret = VerifyScript(tx.vin.at(i).scriptSig, prevout.scriptPubKey, &tx.vin.at(i).scriptWitness, verify_flags, checker, &serror); |
| 46 | + assert(ret == (serror == SCRIPT_ERR_OK)); |
| 47 | + |
| 48 | + // Verify that removing flags from a passing test or adding flags to a failing test does not change the result |
| 49 | + if (ret) { |
| 50 | + verify_flags &= ~fuzzed_flags; |
| 51 | + } else { |
| 52 | + verify_flags |= fuzzed_flags; |
| 53 | + } |
| 54 | + if (!IsValidFlagCombination(verify_flags)) return; |
| 55 | + |
| 56 | + ScriptError serror_fuzzed; |
| 57 | + const bool ret_fuzzed = VerifyScript(tx.vin.at(i).scriptSig, prevout.scriptPubKey, &tx.vin.at(i).scriptWitness, verify_flags, checker, &serror_fuzzed); |
| 58 | + assert(ret_fuzzed == (serror_fuzzed == SCRIPT_ERR_OK)); |
| 59 | + |
| 60 | + assert(ret_fuzzed == ret); |
| 61 | + } |
| 62 | + } catch (const std::ios_base::failure&) { |
| 63 | + return; |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +static bool IsValidFlagCombination(unsigned flags) |
| 68 | +{ |
| 69 | + if (flags & SCRIPT_VERIFY_CLEANSTACK && ~flags & (SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS)) return false; |
| 70 | + if (flags & SCRIPT_VERIFY_WITNESS && ~flags & SCRIPT_VERIFY_P2SH) return false; |
| 71 | + return true; |
| 72 | +} |
0 commit comments