Branch data Line data Source code
1 : : // Copyright (c) 2020-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 <coins.h>
6 : : #include <consensus/amount.h>
7 : : #include <consensus/tx_check.h>
8 : : #include <consensus/tx_verify.h>
9 : : #include <consensus/validation.h>
10 : : #include <policy/policy.h>
11 : : #include <primitives/transaction.h>
12 : : #include <script/interpreter.h>
13 : : #include <test/fuzz/FuzzedDataProvider.h>
14 : : #include <test/fuzz/fuzz.h>
15 : : #include <test/fuzz/util.h>
16 : : #include <test/util/setup_common.h>
17 : : #include <util/hasher.h>
18 : :
19 : : #include <cassert>
20 : : #include <cstdint>
21 : : #include <limits>
22 : : #include <memory>
23 : : #include <optional>
24 : : #include <stdexcept>
25 : : #include <string>
26 : : #include <utility>
27 : : #include <vector>
28 : :
29 : : namespace {
30 : : const Coin EMPTY_COIN{};
31 : :
32 : 3618 : bool operator==(const Coin& a, const Coin& b)
33 : : {
34 [ + + - + ]: 3618 : if (a.IsSpent() && b.IsSpent()) return true;
35 [ + + + + : 2968 : return a.fCoinBase == b.fCoinBase && a.nHeight == b.nHeight && a.out == b.out;
+ + ]
36 : : }
37 : : } // namespace
38 : :
39 : 1 : void initialize_coins_view()
40 : : {
41 [ + - + - ]: 2 : static const auto testing_setup = MakeNoLogFileContext<>();
42 [ + - ]: 2 : }
43 : :
44 [ + - ]: 2576 : FUZZ_TARGET(coins_view, .init = initialize_coins_view)
45 : : {
46 [ + - ]: 2134 : FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
47 : 2134 : bool good_data{true};
48 : :
49 : 2134 : CCoinsView backend_coins_view;
50 [ + - ]: 2134 : CCoinsViewCache coins_view_cache{&backend_coins_view, /*deterministic=*/true};
51 : 2134 : COutPoint random_out_point;
52 : 2134 : Coin random_coin;
53 [ + - ]: 2134 : CMutableTransaction random_mutable_transaction;
54 [ + + + + : 284136 : LIMITED_WHILE(good_data && fuzzed_data_provider.ConsumeBool(), 10'000)
+ + ]
55 : : {
56 [ + - ]: 140067 : CallOneOf(
57 : : fuzzed_data_provider,
58 : 56201 : [&] {
59 [ + + ]: 56201 : if (random_coin.IsSpent()) {
60 : : return;
61 : : }
62 : 26193 : Coin coin = random_coin;
63 : 26193 : bool expected_code_path = false;
64 : 26193 : const bool possible_overwrite = fuzzed_data_provider.ConsumeBool();
65 : 26193 : try {
66 [ + + ]: 26193 : coins_view_cache.AddCoin(random_out_point, std::move(coin), possible_overwrite);
67 : : expected_code_path = true;
68 [ - + ]: 10677 : } catch (const std::logic_error& e) {
69 [ + - + - ]: 10677 : if (e.what() == std::string{"Attempted to overwrite an unspent coin (when possible_overwrite is false)"}) {
70 [ - + ]: 10677 : assert(!possible_overwrite);
71 : : expected_code_path = true;
72 : : }
73 : 0 : }
74 : 10677 : assert(expected_code_path);
75 : 26193 : },
76 : 3735 : [&] {
77 : 3735 : (void)coins_view_cache.Flush();
78 : 3735 : },
79 : 29000 : [&] {
80 : 29000 : (void)coins_view_cache.Sync();
81 : 29000 : },
82 : 2677 : [&] {
83 : 2677 : coins_view_cache.SetBestBlock(ConsumeUInt256(fuzzed_data_provider));
84 : 2677 : },
85 : 7974 : [&] {
86 : 7974 : Coin move_to;
87 [ + + + - ]: 10596 : (void)coins_view_cache.SpendCoin(random_out_point, fuzzed_data_provider.ConsumeBool() ? &move_to : nullptr);
88 : 7974 : },
89 : 8687 : [&] {
90 : 8687 : coins_view_cache.Uncache(random_out_point);
91 : 8687 : },
92 : 2131 : [&] {
93 [ + + ]: 2131 : if (fuzzed_data_provider.ConsumeBool()) {
94 : 1640 : backend_coins_view = CCoinsView{};
95 : : }
96 : 2131 : coins_view_cache.SetBackend(backend_coins_view);
97 : 2131 : },
98 : 2359 : [&] {
99 : 2359 : const std::optional<COutPoint> opt_out_point = ConsumeDeserializable<COutPoint>(fuzzed_data_provider);
100 [ + + ]: 2359 : if (!opt_out_point) {
101 : 26 : good_data = false;
102 : 26 : return;
103 : : }
104 : 2333 : random_out_point = *opt_out_point;
105 : : },
106 : 9045 : [&] {
107 : 9045 : const std::optional<Coin> opt_coin = ConsumeDeserializable<Coin>(fuzzed_data_provider);
108 [ + + ]: 9045 : if (!opt_coin) {
109 : 79 : good_data = false;
110 : 79 : return;
111 : : }
112 : 8966 : random_coin = *opt_coin;
113 : 9045 : },
114 : 7568 : [&] {
115 : 7568 : const std::optional<CMutableTransaction> opt_mutable_transaction = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
116 [ + + ]: 7568 : if (!opt_mutable_transaction) {
117 : 134 : good_data = false;
118 [ - + ]: 134 : return;
119 : : }
120 [ + - ]: 7434 : random_mutable_transaction = *opt_mutable_transaction;
121 : 7568 : },
122 : 10690 : [&] {
123 : 10690 : CoinsCachePair sentinel{};
124 : 10690 : sentinel.second.SelfRef(sentinel);
125 : 10690 : size_t usage{0};
126 [ + - ]: 10690 : CCoinsMapMemoryResource resource;
127 [ + - + - ]: 10690 : CCoinsMap coins_map{0, SaltedOutpointHasher{/*deterministic=*/true}, CCoinsMap::key_equal{}, &resource};
128 [ + - + + : 361888 : LIMITED_WHILE(good_data && fuzzed_data_provider.ConsumeBool(), 10'000)
+ + ]
129 : : {
130 : 351225 : CCoinsCacheEntry coins_cache_entry;
131 : 351225 : const auto dirty{fuzzed_data_provider.ConsumeBool()};
132 : 351225 : const auto fresh{fuzzed_data_provider.ConsumeBool()};
133 [ + + ]: 351225 : if (fuzzed_data_provider.ConsumeBool()) {
134 : 348962 : coins_cache_entry.coin = random_coin;
135 : : } else {
136 : 2263 : const std::optional<Coin> opt_coin = ConsumeDeserializable<Coin>(fuzzed_data_provider);
137 [ + + ]: 2263 : if (!opt_coin) {
138 : 27 : good_data = false;
139 : 27 : return;
140 : : }
141 : 2236 : coins_cache_entry.coin = *opt_coin;
142 : 2236 : }
143 [ + - ]: 351198 : auto it{coins_map.emplace(random_out_point, std::move(coins_cache_entry)).first};
144 [ + + ]: 351198 : if (dirty) CCoinsCacheEntry::SetDirty(*it, sentinel);
145 [ + + ]: 351198 : if (fresh) CCoinsCacheEntry::SetFresh(*it, sentinel);
146 [ + + ]: 351198 : usage += it->second.coin.DynamicMemoryUsage();
147 : 351225 : }
148 : 10663 : bool expected_code_path = false;
149 : 10663 : try {
150 : 10663 : auto cursor{CoinsViewCacheCursor(usage, sentinel, coins_map, /*will_erase=*/true)};
151 [ + + + - : 10663 : coins_view_cache.BatchWrite(cursor, fuzzed_data_provider.ConsumeBool() ? ConsumeUInt256(fuzzed_data_provider) : coins_view_cache.GetBestBlock());
+ + ]
152 : : expected_code_path = true;
153 [ - + ]: 2021 : } catch (const std::logic_error& e) {
154 [ + - + - ]: 2021 : if (e.what() == std::string{"FRESH flag misapplied to coin that exists in parent cache"}) {
155 : 2021 : expected_code_path = true;
156 : : }
157 : 0 : }
158 : 2021 : assert(expected_code_path);
159 : 10690 : });
160 : : }
161 : :
162 : 2134 : {
163 [ + - ]: 2134 : const Coin& coin_using_access_coin = coins_view_cache.AccessCoin(random_out_point);
164 : 2134 : const bool exists_using_access_coin = !(coin_using_access_coin == EMPTY_COIN);
165 [ + - ]: 2134 : const bool exists_using_have_coin = coins_view_cache.HaveCoin(random_out_point);
166 [ + - ]: 2134 : const bool exists_using_have_coin_in_cache = coins_view_cache.HaveCoinInCache(random_out_point);
167 [ + - + + ]: 2134 : if (auto coin{coins_view_cache.GetCoin(random_out_point)}) {
168 [ - + ]: 1484 : assert(*coin == coin_using_access_coin);
169 [ + - - + ]: 1484 : assert(exists_using_access_coin && exists_using_have_coin_in_cache && exists_using_have_coin);
170 : : } else {
171 [ + - - + ]: 650 : assert(!exists_using_access_coin && !exists_using_have_coin_in_cache && !exists_using_have_coin);
172 : 2134 : }
173 : : // If HaveCoin on the backend is true, it must also be on the cache if the coin wasn't spent.
174 [ + - ]: 2134 : const bool exists_using_have_coin_in_backend = backend_coins_view.HaveCoin(random_out_point);
175 [ + + - + ]: 2134 : if (!coin_using_access_coin.IsSpent() && exists_using_have_coin_in_backend) {
176 [ # # ]: 0 : assert(exists_using_have_coin);
177 : : }
178 [ + - - + ]: 2134 : if (auto coin{backend_coins_view.GetCoin(random_out_point)}) {
179 [ # # ]: 0 : assert(exists_using_have_coin_in_backend);
180 : : // Note we can't assert that `coin_using_get_coin == *coin` because the coin in
181 : : // the cache may have been modified but not yet flushed.
182 : : } else {
183 [ - + ]: 2134 : assert(!exists_using_have_coin_in_backend);
184 : 2134 : }
185 : : }
186 : :
187 : 2134 : {
188 : 2134 : bool expected_code_path = false;
189 : 2134 : try {
190 : 2134 : (void)coins_view_cache.Cursor();
191 [ - + ]: 2134 : } catch (const std::logic_error&) {
192 : 2134 : expected_code_path = true;
193 : 2134 : }
194 : 2134 : assert(expected_code_path);
195 [ + - ]: 2134 : (void)coins_view_cache.DynamicMemoryUsage();
196 [ + - ]: 2134 : (void)coins_view_cache.EstimateSize();
197 [ + - ]: 2134 : (void)coins_view_cache.GetBestBlock();
198 [ + - ]: 2134 : (void)coins_view_cache.GetCacheSize();
199 [ + - ]: 2134 : (void)coins_view_cache.GetHeadBlocks();
200 [ + - + - ]: 2134 : (void)coins_view_cache.HaveInputs(CTransaction{random_mutable_transaction});
201 : : }
202 : :
203 : 2134 : {
204 [ + - ]: 2134 : std::unique_ptr<CCoinsViewCursor> coins_view_cursor = backend_coins_view.Cursor();
205 [ - + ]: 2134 : assert(!coins_view_cursor);
206 : 2134 : (void)backend_coins_view.EstimateSize();
207 [ + - ]: 2134 : (void)backend_coins_view.GetBestBlock();
208 [ + - ]: 2134 : (void)backend_coins_view.GetHeadBlocks();
209 : 2134 : }
210 : :
211 [ + + ]: 2134 : if (fuzzed_data_provider.ConsumeBool()) {
212 [ + - ]: 1643 : CallOneOf(
213 : : fuzzed_data_provider,
214 : 156 : [&] {
215 : 156 : const CTransaction transaction{random_mutable_transaction};
216 : 156 : bool is_spent = false;
217 [ + + ]: 1646696 : for (const CTxOut& tx_out : transaction.vout) {
218 [ + + ]: 1646540 : if (Coin{tx_out, 0, transaction.IsCoinBase()}.IsSpent()) {
219 : 10 : is_spent = true;
220 : : }
221 : : }
222 [ + + ]: 156 : if (is_spent) {
223 : : // Avoid:
224 : : // coins.cpp:69: void CCoinsViewCache::AddCoin(const COutPoint &, Coin &&, bool): Assertion `!coin.IsSpent()' failed.
225 : 4 : return;
226 : : }
227 : 152 : bool expected_code_path = false;
228 : 152 : const int height{int(fuzzed_data_provider.ConsumeIntegral<uint32_t>() >> 1)};
229 : 152 : const bool possible_overwrite = fuzzed_data_provider.ConsumeBool();
230 : 152 : try {
231 [ + - ]: 152 : AddCoins(coins_view_cache, transaction, height, possible_overwrite);
232 : : expected_code_path = true;
233 [ - - ]: 0 : } catch (const std::logic_error& e) {
234 [ - - - - ]: 0 : if (e.what() == std::string{"Attempted to overwrite an unspent coin (when possible_overwrite is false)"}) {
235 [ - - ]: 0 : assert(!possible_overwrite);
236 : : expected_code_path = true;
237 : : }
238 : 0 : }
239 : 0 : assert(expected_code_path);
240 : 156 : },
241 : 960 : [&] {
242 [ + - ]: 960 : (void)AreInputsStandard(CTransaction{random_mutable_transaction}, coins_view_cache);
243 : 960 : },
244 : 98 : [&] {
245 [ + - ]: 98 : TxValidationState state;
246 : 98 : CAmount tx_fee_out;
247 [ + - ]: 98 : const CTransaction transaction{random_mutable_transaction};
248 [ + + ]: 98 : if (ContainsSpentInput(transaction, coins_view_cache)) {
249 : : // Avoid:
250 : : // consensus/tx_verify.cpp:171: bool Consensus::CheckTxInputs(const CTransaction &, TxValidationState &, const CCoinsViewCache &, int, CAmount &): Assertion `!coin.IsSpent()' failed.
251 : : return;
252 : : }
253 [ + - ]: 85 : TxValidationState dummy;
254 [ + - + + ]: 85 : if (!CheckTransaction(transaction, dummy)) {
255 : : // It is not allowed to call CheckTxInputs if CheckTransaction failed
256 : 54 : return;
257 : : }
258 [ + - + + ]: 31 : if (Consensus::CheckTxInputs(transaction, state, coins_view_cache, fuzzed_data_provider.ConsumeIntegralInRange<int>(0, std::numeric_limits<int>::max()), tx_fee_out)) {
259 [ - + ]: 2 : assert(MoneyRange(tx_fee_out));
260 : : }
261 : 281 : },
262 : 42 : [&] {
263 : 42 : const CTransaction transaction{random_mutable_transaction};
264 [ + + ]: 42 : if (ContainsSpentInput(transaction, coins_view_cache)) {
265 : : // Avoid:
266 : : // consensus/tx_verify.cpp:130: unsigned int GetP2SHSigOpCount(const CTransaction &, const CCoinsViewCache &): Assertion `!coin.IsSpent()' failed.
267 : 30 : return;
268 : : }
269 [ + - ]: 12 : (void)GetP2SHSigOpCount(transaction, coins_view_cache);
270 : 42 : },
271 : 215 : [&] {
272 : 215 : const CTransaction transaction{random_mutable_transaction};
273 [ + + ]: 215 : if (ContainsSpentInput(transaction, coins_view_cache)) {
274 : : // Avoid:
275 : : // consensus/tx_verify.cpp:130: unsigned int GetP2SHSigOpCount(const CTransaction &, const CCoinsViewCache &): Assertion `!coin.IsSpent()' failed.
276 : : return;
277 : : }
278 : 208 : const auto flags{fuzzed_data_provider.ConsumeIntegral<uint32_t>()};
279 [ + + + + ]: 208 : if (!transaction.vin.empty() && (flags & SCRIPT_VERIFY_WITNESS) != 0 && (flags & SCRIPT_VERIFY_P2SH) == 0) {
280 : : // Avoid:
281 : : // script/interpreter.cpp:1705: size_t CountWitnessSigOps(const CScript &, const CScript &, const CScriptWitness *, unsigned int): Assertion `(flags & SCRIPT_VERIFY_P2SH) != 0' failed.
282 : : return;
283 : : }
284 [ + - ]: 207 : (void)GetTransactionSigOpCost(transaction, coins_view_cache, flags);
285 : 215 : },
286 : 172 : [&] {
287 [ + - ]: 172 : (void)IsWitnessStandard(CTransaction{random_mutable_transaction}, coins_view_cache);
288 : 172 : });
289 : : }
290 : 2134 : }
|