Bitcoin Core 28.99.0
P2P Digital Currency
script.cpp
Go to the documentation of this file.
1// Copyright (c) 2019-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 <compressor.h>
7#include <core_io.h>
8#include <core_memusage.h>
9#include <key_io.h>
10#include <policy/policy.h>
11#include <pubkey.h>
12#include <rpc/util.h>
13#include <script/descriptor.h>
14#include <script/interpreter.h>
15#include <script/script.h>
16#include <script/script_error.h>
17#include <script/sign.h>
19#include <script/solver.h>
20#include <streams.h>
22#include <test/fuzz/fuzz.h>
23#include <test/fuzz/util.h>
24#include <univalue.h>
25#include <util/chaintype.h>
26
27#include <algorithm>
28#include <cassert>
29#include <cstdint>
30#include <optional>
31#include <string>
32#include <vector>
33
35{
37}
38
40{
41 FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
42 const CScript script{ConsumeScript(fuzzed_data_provider)};
43
44 CompressedScript compressed;
45 if (CompressScript(script, compressed)) {
46 const unsigned int size = compressed[0];
47 compressed.erase(compressed.begin());
48 assert(size <= 5);
49 CScript decompressed_script;
50 const bool ok = DecompressScript(decompressed_script, size, compressed);
51 assert(ok);
52 assert(script == decompressed_script);
53 }
54
55 TxoutType which_type;
56 bool is_standard_ret = IsStandard(script, std::nullopt, which_type);
57 if (!is_standard_ret) {
58 assert(which_type == TxoutType::NONSTANDARD ||
59 which_type == TxoutType::NULL_DATA ||
60 which_type == TxoutType::MULTISIG);
61 }
62 if (which_type == TxoutType::NONSTANDARD) {
63 assert(!is_standard_ret);
64 }
65 if (which_type == TxoutType::NULL_DATA) {
66 assert(script.IsUnspendable());
67 }
68 if (script.IsUnspendable()) {
69 assert(which_type == TxoutType::NULL_DATA ||
70 which_type == TxoutType::NONSTANDARD);
71 }
72
73 CTxDestination address;
74 bool extract_destination_ret = ExtractDestination(script, address);
75 if (!extract_destination_ret) {
76 assert(which_type == TxoutType::PUBKEY ||
77 which_type == TxoutType::NONSTANDARD ||
78 which_type == TxoutType::NULL_DATA ||
79 which_type == TxoutType::MULTISIG);
80 }
81 if (which_type == TxoutType::NONSTANDARD ||
82 which_type == TxoutType::NULL_DATA ||
83 which_type == TxoutType::MULTISIG) {
84 assert(!extract_destination_ret);
85 }
86
87 const FlatSigningProvider signing_provider;
88 (void)InferDescriptor(script, signing_provider);
89 (void)IsSegWitOutput(signing_provider, script);
90
92
93 std::vector<std::vector<unsigned char>> solutions;
94 (void)Solver(script, solutions);
95
96 (void)script.HasValidOps();
97 (void)script.IsPayToAnchor();
98 (void)script.IsPayToScriptHash();
99 (void)script.IsPayToWitnessScriptHash();
100 (void)script.IsPushOnly();
101 (void)script.GetSigOpCount(/* fAccurate= */ false);
102
103 {
104 const std::vector<uint8_t> bytes = ConsumeRandomLengthByteVector(fuzzed_data_provider);
105 CompressedScript compressed_script;
106 compressed_script.assign(bytes.begin(), bytes.end());
107 // DecompressScript(..., ..., bytes) is not guaranteed to be defined if the bytes vector is too short
108 if (compressed_script.size() >= 32) {
109 CScript decompressed_script;
110 DecompressScript(decompressed_script, fuzzed_data_provider.ConsumeIntegral<unsigned int>(), compressed_script);
111 }
112 }
113
114 const std::optional<CScript> other_script = ConsumeDeserializable<CScript>(fuzzed_data_provider);
115 if (other_script) {
116 {
117 CScript script_mut{script};
118 (void)FindAndDelete(script_mut, *other_script);
119 }
120 const std::vector<std::string> random_string_vector = ConsumeRandomLengthStringVector(fuzzed_data_provider);
121 const uint32_t u32{fuzzed_data_provider.ConsumeIntegral<uint32_t>()};
122 const uint32_t flags{u32 | SCRIPT_VERIFY_P2SH};
123 {
124 CScriptWitness wit;
125 for (const auto& s : random_string_vector) {
126 wit.stack.emplace_back(s.begin(), s.end());
127 }
128 (void)CountWitnessSigOps(script, *other_script, &wit, flags);
129 wit.SetNull();
130 }
131 }
132
133 (void)GetOpName(ConsumeOpcodeType(fuzzed_data_provider));
134 (void)ScriptErrorString(static_cast<ScriptError>(fuzzed_data_provider.ConsumeIntegralInRange<int>(0, SCRIPT_ERR_ERROR_COUNT)));
135
136 {
137 const std::vector<uint8_t> bytes = ConsumeRandomLengthByteVector(fuzzed_data_provider);
138 CScript append_script{bytes.begin(), bytes.end()};
139 append_script << fuzzed_data_provider.ConsumeIntegral<int64_t>();
140 append_script << ConsumeOpcodeType(fuzzed_data_provider);
141 append_script << CScriptNum{fuzzed_data_provider.ConsumeIntegral<int64_t>()};
142 append_script << ConsumeRandomLengthByteVector(fuzzed_data_provider);
143 }
144
145 {
146 const CTxDestination tx_destination_1{
147 fuzzed_data_provider.ConsumeBool() ?
148 DecodeDestination(fuzzed_data_provider.ConsumeRandomLengthString()) :
149 ConsumeTxDestination(fuzzed_data_provider)};
150 const CTxDestination tx_destination_2{ConsumeTxDestination(fuzzed_data_provider)};
151 const std::string encoded_dest{EncodeDestination(tx_destination_1)};
152 const UniValue json_dest{DescribeAddress(tx_destination_1)};
153 (void)GetKeyForDestination(/*store=*/{}, tx_destination_1);
154 const CScript dest{GetScriptForDestination(tx_destination_1)};
155 const bool valid{IsValidDestination(tx_destination_1)};
156
157 if (!std::get_if<PubKeyDestination>(&tx_destination_1)) {
158 // Only try to round trip non-pubkey destinations since PubKeyDestination has no encoding
159 Assert(dest.empty() != valid);
160 Assert(tx_destination_1 == DecodeDestination(encoded_dest));
161 Assert(valid == IsValidDestinationString(encoded_dest));
162 }
163
164 (void)(tx_destination_1 < tx_destination_2);
165 if (tx_destination_1 == tx_destination_2) {
166 Assert(encoded_dest == EncodeDestination(tx_destination_2));
167 Assert(json_dest.write() == DescribeAddress(tx_destination_2).write());
168 Assert(dest == GetScriptForDestination(tx_destination_2));
169 }
170 }
171}
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet)
Parse a scriptPubKey for the destination.
Definition: addresstype.cpp:49
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination corresponds to one with an address.
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
std::variant< CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown > CTxDestination
A txout script categorized into standard templates.
Definition: addresstype.h:140
int flags
Definition: bitcoin-tx.cpp:536
void SelectParams(const ChainType chain)
Sets the params returned by Params() to those for the given chain type.
#define Assert(val)
Identity function.
Definition: check.h:85
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:415
std::string ConsumeRandomLengthString(size_t max_length)
T ConsumeIntegralInRange(T min, T max)
Implements a drop-in replacement for std::vector<T> which stores up to N elements directly (without h...
Definition: prevector.h:37
size_type size() const
Definition: prevector.h:294
iterator erase(iterator pos)
Definition: prevector.h:416
iterator begin()
Definition: prevector.h:302
void assign(size_type n, const T &val)
Definition: prevector.h:223
bool DecompressScript(CScript &script, unsigned int nSize, const CompressedScript &in)
Definition: compressor.cpp:95
bool CompressScript(const CScript &script, CompressedScript &out)
Definition: compressor.cpp:55
static size_t RecursiveDynamicUsage(const CScript &script)
Definition: core_memusage.h:12
unsigned int u32
int FindAndDelete(CScript &script, const CScript &b)
size_t CountWitnessSigOps(const CScript &scriptSig, const CScript &scriptPubKey, const CScriptWitness *witness, unsigned int flags)
@ SCRIPT_VERIFY_P2SH
Definition: interpreter.h:49
bool IsValidDestinationString(const std::string &str, const CChainParams &params)
Definition: key_io.cpp:310
CTxDestination DecodeDestination(const std::string &str, std::string &error_msg, std::vector< int > *error_locations)
Definition: key_io.cpp:299
std::string EncodeDestination(const CTxDestination &dest)
Definition: key_io.cpp:294
bool IsStandard(const CScript &scriptPubKey, const std::optional< unsigned > &max_datacarrier_bytes, TxoutType &whichType)
Definition: policy.cpp:79
UniValue DescribeAddress(const CTxDestination &dest)
Definition: util.cpp:369
std::unique_ptr< Descriptor > InferDescriptor(const CScript &script, const SigningProvider &provider)
Find a descriptor for the specified script, using information from provider where possible.
std::string GetOpName(opcodetype opcode)
Definition: script.cpp:18
std::string ScriptErrorString(const ScriptError serror)
enum ScriptError_t ScriptError
@ SCRIPT_ERR_ERROR_COUNT
Definition: script_error.h:85
bool IsSegWitOutput(const SigningProvider &provider, const CScript &script)
Check whether a scriptPubKey is known to be segwit.
Definition: sign.cpp:747
CKeyID GetKeyForDestination(const SigningProvider &store, const CTxDestination &dest)
Return the CKeyID of the key involved in a script (if there is a unique one).
TxoutType Solver(const CScript &scriptPubKey, std::vector< std::vector< unsigned char > > &vSolutionsRet)
Parse a scriptPubKey and identify script type for standard scripts.
Definition: solver.cpp:141
TxoutType
Definition: solver.h:22
@ NULL_DATA
unspendable OP_RETURN script that carries data
std::vector< std::vector< unsigned char > > stack
Definition: script.h:588
void SetNull()
Definition: script.h:595
FUZZ_TARGET(script,.init=initialize_script)
Definition: script.cpp:39
void initialize_script()
Definition: script.cpp:34
CScript ConsumeScript(FuzzedDataProvider &fuzzed_data_provider, const bool maybe_p2wsh) noexcept
Definition: util.cpp:93
CTxDestination ConsumeTxDestination(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.cpp:184
std::vector< B > ConsumeRandomLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition: util.h:57
std::vector< std::string > ConsumeRandomLengthStringVector(FuzzedDataProvider &fuzzed_data_provider, const size_t max_vector_size=16, const size_t max_string_length=16) noexcept
Definition: util.h:78
opcodetype ConsumeOpcodeType(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.h:140
assert(!tx.IsCoinBase())