Bitcoin Core  22.99.0
P2P Digital Currency
script_interpreter.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020 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 
6 #include <script/interpreter.h>
8 #include <test/fuzz/fuzz.h>
9 #include <test/fuzz/util.h>
10 
11 #include <cstdint>
12 #include <optional>
13 #include <string>
14 #include <vector>
15 
16 bool CastToBool(const std::vector<unsigned char>& vch);
17 
18 FUZZ_TARGET(script_interpreter)
19 {
20  FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
21  {
22  const CScript script_code = ConsumeScript(fuzzed_data_provider);
23  const std::optional<CMutableTransaction> mtx = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider);
24  if (mtx) {
25  const CTransaction tx_to{*mtx};
26  const unsigned int in = fuzzed_data_provider.ConsumeIntegral<unsigned int>();
27  if (in < tx_to.vin.size()) {
28  (void)SignatureHash(script_code, tx_to, in, fuzzed_data_provider.ConsumeIntegral<int>(), ConsumeMoney(fuzzed_data_provider), fuzzed_data_provider.PickValueInArray({SigVersion::BASE, SigVersion::WITNESS_V0}), nullptr);
29  const std::optional<CMutableTransaction> mtx_precomputed = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider);
30  if (mtx_precomputed) {
31  const CTransaction tx_precomputed{*mtx_precomputed};
32  const PrecomputedTransactionData precomputed_transaction_data{tx_precomputed};
33  (void)SignatureHash(script_code, tx_to, in, fuzzed_data_provider.ConsumeIntegral<int>(), ConsumeMoney(fuzzed_data_provider), fuzzed_data_provider.PickValueInArray({SigVersion::BASE, SigVersion::WITNESS_V0}), &precomputed_transaction_data);
34  }
35  }
36  }
37  }
38  {
39  (void)CastToBool(ConsumeRandomLengthByteVector(fuzzed_data_provider));
40  }
41 }
FUZZ_TARGET
FUZZ_TARGET(script_interpreter)
Definition: script_interpreter.cpp:18
SigVersion::BASE
@ BASE
Bare scripts and BIP16 P2SH-wrapped redeemscripts.
transaction.h
util.h
FuzzedDataProvider::PickValueInArray
T PickValueInArray(const T(&array)[size])
Definition: FuzzedDataProvider.h:303
interpreter.h
CTransaction
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:259
ConsumeScript
CScript ConsumeScript(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length, const bool maybe_p2wsh) noexcept
Definition: util.cpp:285
FuzzedDataProvider.h
CastToBool
bool CastToBool(const std::vector< unsigned char > &vch)
Definition: interpreter.cpp:35
ConsumeRandomLengthByteVector
std::vector< uint8_t > ConsumeRandomLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition: util.h:61
SigVersion::WITNESS_V0
@ WITNESS_V0
Witness v0 (P2WPKH and P2WSH); see BIP 141.
PrecomputedTransactionData
Definition: interpreter.h:150
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:405
SignatureHash
uint256 SignatureHash(const CScript &scriptCode, const T &txTo, unsigned int nIn, int nHashType, const CAmount &amount, SigVersion sigversion, const PrecomputedTransactionData *cache)
Definition: interpreter.cpp:1590
fuzz.h
FuzzedDataProvider
Definition: FuzzedDataProvider.h:31
FuzzedDataProvider::ConsumeIntegral
T ConsumeIntegral()
Definition: FuzzedDataProvider.h:194
ConsumeMoney
CAmount ConsumeMoney(FuzzedDataProvider &fuzzed_data_provider, const std::optional< CAmount > &max) noexcept
Definition: util.cpp:221