Branch data Line data Source code
1 : : // Copyright (c) 2019-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 <script/script.h>
6 : :
7 : : #include <chainparams.h>
8 : : #include <compressor.h>
9 : : #include <core_io.h>
10 : : #include <core_memusage.h>
11 : : #include <key_io.h>
12 : : #include <policy/policy.h>
13 : : #include <pubkey.h>
14 : : #include <rpc/util.h>
15 : : #include <script/descriptor.h>
16 : : #include <script/interpreter.h>
17 : : #include <script/script_error.h>
18 : : #include <script/sign.h>
19 : : #include <script/signingprovider.h>
20 : : #include <script/solver.h>
21 : : #include <streams.h>
22 : : #include <test/fuzz/FuzzedDataProvider.h>
23 : : #include <test/fuzz/fuzz.h>
24 : : #include <test/fuzz/util.h>
25 : : #include <univalue.h>
26 : : #include <util/chaintype.h>
27 : : #include <util/check.h>
28 : :
29 : : #include <algorithm>
30 : : #include <cstdint>
31 : : #include <optional>
32 : : #include <string>
33 : : #include <vector>
34 : :
35 : 1 : void initialize_script()
36 : : {
37 : 1 : SelectParams(ChainType::REGTEST);
38 : 1 : }
39 : :
40 [ + - ]: 2905 : FUZZ_TARGET(script, .init = initialize_script)
41 : : {
42 : 2429 : FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
43 : 2429 : const CScript script{ConsumeScript(fuzzed_data_provider)};
44 : :
45 : 2429 : CompressedScript compressed;
46 [ + - + + ]: 2429 : if (CompressScript(script, compressed)) {
47 [ - + ]: 20 : const unsigned int size = compressed[0];
48 : 20 : compressed.erase(compressed.begin());
49 [ - + ]: 20 : assert(size <= 5);
50 : 20 : CScript decompressed_script;
51 [ + - ]: 20 : const bool ok = DecompressScript(decompressed_script, size, compressed);
52 [ - + ]: 20 : assert(ok);
53 [ - + ]: 20 : assert(script == decompressed_script);
54 : 20 : }
55 : :
56 : 2429 : TxoutType which_type;
57 [ + - ]: 2429 : bool is_standard_ret = IsStandard(script, which_type);
58 [ + + ]: 2429 : if (!is_standard_ret) {
59 [ + + + - : 2297 : assert(which_type == TxoutType::NONSTANDARD ||
- + ]
60 : : which_type == TxoutType::NULL_DATA ||
61 : : which_type == TxoutType::MULTISIG);
62 : : }
63 [ + + ]: 2429 : if (which_type == TxoutType::NONSTANDARD) {
64 [ - + ]: 921 : assert(!is_standard_ret);
65 : : }
66 [ + + ]: 2429 : if (which_type == TxoutType::NULL_DATA) {
67 [ - + ]: 16 : assert(script.IsUnspendable());
68 : : }
69 [ + + ]: 2429 : if (script.IsUnspendable()) {
70 [ + + - + ]: 128 : assert(which_type == TxoutType::NULL_DATA ||
71 : : which_type == TxoutType::NONSTANDARD);
72 : : }
73 : :
74 : 2429 : CTxDestination address;
75 [ + - ]: 2429 : bool extract_destination_ret = ExtractDestination(script, address);
76 [ + + ]: 2429 : if (!extract_destination_ret) {
77 [ + + + + : 2349 : assert(which_type == TxoutType::PUBKEY ||
+ + - + ]
78 : : which_type == TxoutType::NONSTANDARD ||
79 : : which_type == TxoutType::NULL_DATA ||
80 : : which_type == TxoutType::MULTISIG);
81 : : }
82 [ + + + + ]: 2429 : if (which_type == TxoutType::NONSTANDARD ||
83 [ + + ]: 1492 : which_type == TxoutType::NULL_DATA ||
84 : : which_type == TxoutType::MULTISIG) {
85 [ - + ]: 2320 : assert(!extract_destination_ret);
86 : : }
87 : :
88 : 2429 : const FlatSigningProvider signing_provider;
89 [ + - ]: 2429 : (void)InferDescriptor(script, signing_provider);
90 [ + - ]: 2429 : (void)IsSegWitOutput(signing_provider, script);
91 : :
92 [ + + ]: 2429 : (void)RecursiveDynamicUsage(script);
93 : :
94 : 2429 : std::vector<std::vector<unsigned char>> solutions;
95 [ + - ]: 2429 : (void)Solver(script, solutions);
96 : :
97 : 2429 : {
98 : 2429 : const std::vector<uint8_t> bytes = ConsumeRandomLengthByteVector(fuzzed_data_provider);
99 : 2429 : CompressedScript compressed_script;
100 : 2429 : compressed_script.assign(bytes.begin(), bytes.end());
101 : : // DecompressScript(..., ..., bytes) is not guaranteed to be defined if the bytes vector is too short
102 [ + + + + ]: 2972 : if (compressed_script.size() >= 32) {
103 : 573 : CScript decompressed_script;
104 [ + - ]: 573 : DecompressScript(decompressed_script, fuzzed_data_provider.ConsumeIntegral<unsigned int>(), compressed_script);
105 : 573 : }
106 : 2429 : }
107 : :
108 : 2429 : const std::optional<CScript> other_script = ConsumeDeserializable<CScript>(fuzzed_data_provider);
109 [ + + ]: 2429 : if (other_script) {
110 : 450 : {
111 : 450 : CScript script_mut{script};
112 [ + - ]: 450 : (void)FindAndDelete(script_mut, *other_script);
113 : 0 : }
114 : 450 : const std::vector<std::string> random_string_vector = ConsumeRandomLengthStringVector(fuzzed_data_provider);
115 : 450 : const auto flags_rand{fuzzed_data_provider.ConsumeIntegral<script_verify_flags::value_type>()};
116 : 450 : const auto flags = script_verify_flags::from_int(flags_rand) | SCRIPT_VERIFY_P2SH;
117 : 450 : {
118 : 450 : CScriptWitness wit;
119 [ + + ]: 1868 : for (const auto& s : random_string_vector) {
120 [ - + + - ]: 1418 : wit.stack.emplace_back(s.begin(), s.end());
121 : : }
122 [ + - ]: 450 : (void)CountWitnessSigOps(script, *other_script, wit, flags);
123 : 450 : wit.SetNull();
124 : 0 : }
125 : 450 : }
126 : :
127 [ + - ]: 2429 : (void)GetOpName(ConsumeOpcodeType(fuzzed_data_provider));
128 [ + - ]: 2429 : (void)ScriptErrorString(static_cast<ScriptError>(fuzzed_data_provider.ConsumeIntegralInRange<int>(0, SCRIPT_ERR_ERROR_COUNT)));
129 : :
130 : 2429 : {
131 : 2429 : const std::vector<uint8_t> bytes = ConsumeRandomLengthByteVector(fuzzed_data_provider);
132 : 2429 : CScript append_script{bytes.begin(), bytes.end()};
133 [ + - ]: 2429 : append_script << fuzzed_data_provider.ConsumeIntegral<int64_t>();
134 [ + - ]: 2429 : append_script << ConsumeOpcodeType(fuzzed_data_provider);
135 [ + - ]: 2429 : append_script << CScriptNum{fuzzed_data_provider.ConsumeIntegral<int64_t>()};
136 [ - + ]: 2429 : append_script << ConsumeRandomLengthByteVector(fuzzed_data_provider);
137 : 2429 : }
138 : :
139 : 2429 : {
140 : 2429 : const CTxDestination tx_destination_1{
141 [ + + ]: 2429 : fuzzed_data_provider.ConsumeBool() ?
142 : 428 : DecodeDestination(fuzzed_data_provider.ConsumeRandomLengthString()) :
143 [ + - ]: 2429 : ConsumeTxDestination(fuzzed_data_provider)};
144 : 2429 : const CTxDestination tx_destination_2{ConsumeTxDestination(fuzzed_data_provider)};
145 [ + - ]: 2429 : const std::string encoded_dest{EncodeDestination(tx_destination_1)};
146 [ + - ]: 2429 : const UniValue json_dest{DescribeAddress(tx_destination_1)};
147 [ + - ]: 2429 : (void)GetKeyForDestination(/*store=*/{}, tx_destination_1);
148 [ + - ]: 2429 : const CScript dest{GetScriptForDestination(tx_destination_1)};
149 [ + - ]: 2429 : const bool valid{IsValidDestination(tx_destination_1)};
150 : :
151 [ + + ]: 2429 : if (!std::get_if<PubKeyDestination>(&tx_destination_1)) {
152 : : // Only try to round trip non-pubkey destinations since PubKeyDestination has no encoding
153 [ + + + - ]: 2377 : Assert(dest.empty() != valid);
154 [ + - + - ]: 2364 : Assert(tx_destination_1 == DecodeDestination(encoded_dest));
155 [ + - + - ]: 2364 : Assert(valid == IsValidDestinationString(encoded_dest));
156 : : }
157 : :
158 : 2429 : (void)(tx_destination_1 < tx_destination_2);
159 [ + + ]: 2429 : if (tx_destination_1 == tx_destination_2) {
160 [ + - + - ]: 2027 : Assert(encoded_dest == EncodeDestination(tx_destination_2));
161 [ + - + - : 4054 : Assert(json_dest.write() == DescribeAddress(tx_destination_2).write());
+ - + - ]
162 [ + - + - ]: 4054 : Assert(dest == GetScriptForDestination(tx_destination_2));
163 : : }
164 : 2429 : }
165 : 2429 : }
|