Branch data Line data Source code
1 : : // Copyright (c) 2020-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 <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 <txdb.h>
18 : : #include <util/hasher.h>
19 : :
20 : : #include <cassert>
21 : : #include <cstdint>
22 : : #include <limits>
23 : : #include <memory>
24 : : #include <optional>
25 : : #include <stdexcept>
26 : : #include <string>
27 : : #include <utility>
28 : : #include <vector>
29 : :
30 : : namespace {
31 : : const Coin EMPTY_COIN{};
32 : :
33 : 14752 : bool operator==(const Coin& a, const Coin& b)
34 : : {
35 [ + + - + ]: 14752 : if (a.IsSpent() && b.IsSpent()) return true;
36 [ + + + + : 4042 : return a.fCoinBase == b.fCoinBase && a.nHeight == b.nHeight && a.out == b.out;
+ + ]
37 : : }
38 : : } // namespace
39 : :
40 : 2 : void initialize_coins_view()
41 : : {
42 [ + - + - : 2 : static const auto testing_setup = MakeNoLogFileContext<>();
+ - ]
43 : 2 : }
44 : :
45 : 12731 : void TestCoinsView(FuzzedDataProvider& fuzzed_data_provider, CCoinsView& backend_coins_view, bool is_db)
46 : : {
47 : 12731 : bool good_data{true};
48 : :
49 : 12731 : CCoinsViewCache coins_view_cache{&backend_coins_view, /*deterministic=*/true};
50 [ + + + - ]: 12731 : if (is_db) coins_view_cache.SetBestBlock(uint256::ONE);
51 : 12731 : COutPoint random_out_point;
52 : 12731 : Coin random_coin;
53 [ + - ]: 12731 : CMutableTransaction random_mutable_transaction;
54 [ + + + + : 1427113 : LIMITED_WHILE(good_data && fuzzed_data_provider.ConsumeBool(), 10'000)
+ + ]
55 : : {
56 [ + - ]: 702092 : CallOneOf(
57 : : fuzzed_data_provider,
58 : 3380 : [&] {
59 [ + + ]: 3380 : if (random_coin.IsSpent()) {
60 : : return;
61 : : }
62 : 1172 : COutPoint outpoint{random_out_point};
63 : 1172 : Coin coin{random_coin};
64 [ + + ]: 1172 : if (fuzzed_data_provider.ConsumeBool()) {
65 : 999 : const bool possible_overwrite{fuzzed_data_provider.ConsumeBool()};
66 : 999 : try {
67 [ + - ]: 999 : coins_view_cache.AddCoin(outpoint, std::move(coin), possible_overwrite);
68 [ - - ]: 0 : } catch (const std::logic_error& e) {
69 [ - - - - ]: 0 : assert(e.what() == std::string{"Attempted to overwrite an unspent coin (when possible_overwrite is false)"});
70 [ - - ]: 0 : assert(!possible_overwrite);
71 : 0 : }
72 : : } else {
73 [ + - ]: 173 : coins_view_cache.EmplaceCoinInternalDANGER(std::move(outpoint), std::move(coin));
74 : : }
75 : 1172 : },
76 : 265071 : [&] {
77 : 265071 : coins_view_cache.Flush(/*reallocate_cache=*/fuzzed_data_provider.ConsumeBool());
78 : 265071 : },
79 : 3446 : [&] {
80 : 3446 : coins_view_cache.Sync();
81 : 3446 : },
82 : 106918 : [&] {
83 : 106918 : uint256 best_block{ConsumeUInt256(fuzzed_data_provider)};
84 : : // Set best block hash to non-null to satisfy the assertion in CCoinsViewDB::BatchWrite().
85 [ + + + + ]: 203037 : if (is_db && best_block.IsNull()) best_block = uint256::ONE;
86 : 106918 : coins_view_cache.SetBestBlock(best_block);
87 : 106918 : },
88 : 381 : [&] {
89 : 381 : {
90 : 381 : const auto reset_guard{coins_view_cache.CreateResetGuard()};
91 : 381 : }
92 : : // Set best block hash to non-null to satisfy the assertion in CCoinsViewDB::BatchWrite().
93 [ + + ]: 381 : if (is_db) {
94 : 249 : const uint256 best_block{ConsumeUInt256(fuzzed_data_provider)};
95 [ + + ]: 498 : if (best_block.IsNull()) {
96 : 26 : good_data = false;
97 : 26 : return;
98 : : }
99 : 223 : coins_view_cache.SetBestBlock(best_block);
100 : : }
101 : : },
102 : 45538 : [&] {
103 : 45538 : Coin move_to;
104 [ + + + - ]: 47516 : (void)coins_view_cache.SpendCoin(random_out_point, fuzzed_data_provider.ConsumeBool() ? &move_to : nullptr);
105 : 45538 : },
106 : 477 : [&] {
107 : 477 : coins_view_cache.Uncache(random_out_point);
108 : 477 : },
109 : 259922 : [&] {
110 [ + + ]: 259922 : if (fuzzed_data_provider.ConsumeBool()) {
111 : 259437 : backend_coins_view = CCoinsView{};
112 : : }
113 : 259922 : coins_view_cache.SetBackend(backend_coins_view);
114 : 259922 : },
115 : 666 : [&] {
116 : 666 : const std::optional<COutPoint> opt_out_point = ConsumeDeserializable<COutPoint>(fuzzed_data_provider);
117 [ + + ]: 666 : if (!opt_out_point) {
118 : 174 : good_data = false;
119 : 174 : return;
120 : : }
121 : 492 : random_out_point = *opt_out_point;
122 : : },
123 : 10131 : [&] {
124 : 10131 : const std::optional<Coin> opt_coin = ConsumeDeserializable<Coin>(fuzzed_data_provider);
125 [ + + ]: 10131 : if (!opt_coin) {
126 : 788 : good_data = false;
127 : 788 : return;
128 : : }
129 : 9343 : random_coin = *opt_coin;
130 : 10131 : },
131 : 976 : [&] {
132 : 976 : const std::optional<CMutableTransaction> opt_mutable_transaction = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
133 [ + + ]: 976 : if (!opt_mutable_transaction) {
134 : 585 : good_data = false;
135 [ - + ]: 585 : return;
136 : : }
137 [ + - ]: 391 : random_mutable_transaction = *opt_mutable_transaction;
138 : 976 : },
139 : 5186 : [&] {
140 : 5186 : CoinsCachePair sentinel{};
141 : 5186 : sentinel.second.SelfRef(sentinel);
142 [ + - ]: 5186 : CCoinsMapMemoryResource resource;
143 [ + - + - ]: 5186 : CCoinsMap coins_map{0, SaltedOutpointHasher{/*deterministic=*/true}, CCoinsMap::key_equal{}, &resource};
144 [ + - + + : 835274 : LIMITED_WHILE(good_data && fuzzed_data_provider.ConsumeBool(), 10'000)
+ + ]
145 : : {
146 : 831048 : CCoinsCacheEntry coins_cache_entry;
147 : 831048 : const auto dirty{fuzzed_data_provider.ConsumeBool()};
148 : 831048 : const auto fresh{fuzzed_data_provider.ConsumeBool()};
149 [ + + ]: 831048 : if (fuzzed_data_provider.ConsumeBool()) {
150 : 825024 : coins_cache_entry.coin = random_coin;
151 : : } else {
152 : 6024 : const std::optional<Coin> opt_coin = ConsumeDeserializable<Coin>(fuzzed_data_provider);
153 [ + + ]: 6024 : if (!opt_coin) {
154 : 960 : good_data = false;
155 : 960 : return;
156 : : }
157 : 5064 : coins_cache_entry.coin = *opt_coin;
158 : 5064 : }
159 [ + - ]: 830088 : auto it{coins_map.emplace(random_out_point, std::move(coins_cache_entry)).first};
160 [ + + ]: 830088 : if (dirty) CCoinsCacheEntry::SetDirty(*it, sentinel);
161 [ + + ]: 830088 : if (fresh) CCoinsCacheEntry::SetFresh(*it, sentinel);
162 : 831048 : }
163 : 4226 : bool expected_code_path = false;
164 : 4226 : try {
165 [ + - ]: 4226 : auto cursor{CoinsViewCacheCursor(sentinel, coins_map, /*will_erase=*/true)};
166 [ + - ]: 4226 : uint256 best_block{coins_view_cache.GetBestBlock()};
167 [ + + ]: 4226 : if (fuzzed_data_provider.ConsumeBool()) best_block = ConsumeUInt256(fuzzed_data_provider);
168 : : // Set best block hash to non-null to satisfy the assertion in CCoinsViewDB::BatchWrite().
169 [ + + + + ]: 5776 : if (is_db && best_block.IsNull()) best_block = uint256::ONE;
170 [ + + ]: 4226 : coins_view_cache.BatchWrite(cursor, best_block);
171 : : expected_code_path = true;
172 [ - + ]: 13 : } catch (const std::logic_error& e) {
173 [ + - + - ]: 13 : if (e.what() == std::string{"FRESH flag misapplied to coin that exists in parent cache"}) {
174 : 13 : expected_code_path = true;
175 : : }
176 : 0 : }
177 : 13 : assert(expected_code_path);
178 : 5186 : });
179 : : }
180 : :
181 : 12731 : {
182 [ + - ]: 12731 : const Coin& coin_using_access_coin = coins_view_cache.AccessCoin(random_out_point);
183 : 12731 : const bool exists_using_access_coin = !(coin_using_access_coin == EMPTY_COIN);
184 [ + - ]: 12731 : const bool exists_using_have_coin = coins_view_cache.HaveCoin(random_out_point);
185 [ + - ]: 12731 : const bool exists_using_have_coin_in_cache = coins_view_cache.HaveCoinInCache(random_out_point);
186 [ + - + + ]: 12731 : if (auto coin{coins_view_cache.GetCoin(random_out_point)}) {
187 [ - + ]: 2021 : assert(*coin == coin_using_access_coin);
188 [ + - - + ]: 2021 : assert(exists_using_access_coin && exists_using_have_coin_in_cache && exists_using_have_coin);
189 : : } else {
190 [ + - - + ]: 10710 : assert(!exists_using_access_coin && !exists_using_have_coin_in_cache && !exists_using_have_coin);
191 : 12731 : }
192 : : // If HaveCoin on the backend is true, it must also be on the cache if the coin wasn't spent.
193 [ + - ]: 12731 : const bool exists_using_have_coin_in_backend = backend_coins_view.HaveCoin(random_out_point);
194 [ + + + + ]: 12731 : if (!coin_using_access_coin.IsSpent() && exists_using_have_coin_in_backend) {
195 [ - + ]: 35 : assert(exists_using_have_coin);
196 : : }
197 [ + - + + ]: 12731 : if (auto coin{backend_coins_view.GetCoin(random_out_point)}) {
198 [ - + ]: 40 : assert(exists_using_have_coin_in_backend);
199 : : // Note we can't assert that `coin_using_get_coin == *coin` because the coin in
200 : : // the cache may have been modified but not yet flushed.
201 : : } else {
202 [ - + ]: 12691 : assert(!exists_using_have_coin_in_backend);
203 : 12731 : }
204 : : }
205 : :
206 : 12731 : {
207 : 12731 : bool expected_code_path = false;
208 : 12731 : try {
209 : 12731 : (void)coins_view_cache.Cursor();
210 [ - + ]: 12731 : } catch (const std::logic_error&) {
211 : 12731 : expected_code_path = true;
212 : 12731 : }
213 : 12731 : assert(expected_code_path);
214 [ + - ]: 12731 : (void)coins_view_cache.DynamicMemoryUsage();
215 [ + - ]: 12731 : (void)coins_view_cache.EstimateSize();
216 [ + - ]: 12731 : (void)coins_view_cache.GetBestBlock();
217 [ + - ]: 12731 : (void)coins_view_cache.GetCacheSize();
218 [ + - ]: 12731 : (void)coins_view_cache.GetHeadBlocks();
219 [ + - + - ]: 12731 : (void)coins_view_cache.HaveInputs(CTransaction{random_mutable_transaction});
220 : : }
221 : :
222 : 12731 : {
223 [ + - ]: 12731 : std::unique_ptr<CCoinsViewCursor> coins_view_cursor = backend_coins_view.Cursor();
224 [ - + ]: 12731 : assert(is_db == !!coins_view_cursor);
225 [ + - ]: 12731 : (void)backend_coins_view.EstimateSize();
226 [ + - ]: 12731 : (void)backend_coins_view.GetBestBlock();
227 [ + - ]: 12731 : (void)backend_coins_view.GetHeadBlocks();
228 : 12731 : }
229 : :
230 [ + + ]: 12731 : if (fuzzed_data_provider.ConsumeBool()) {
231 [ + - ]: 7617 : CallOneOf(
232 : : fuzzed_data_provider,
233 : 1092 : [&] {
234 : 1092 : const CTransaction transaction{random_mutable_transaction};
235 : 1092 : bool is_spent = false;
236 [ + + ]: 1097 : for (const CTxOut& tx_out : transaction.vout) {
237 [ - + ]: 5 : if (Coin{tx_out, 0, transaction.IsCoinBase()}.IsSpent()) {
238 : 0 : is_spent = true;
239 : : }
240 : : }
241 [ - + ]: 1092 : if (is_spent) {
242 : : // Avoid:
243 : : // coins.cpp:69: void CCoinsViewCache::AddCoin(const COutPoint &, Coin &&, bool): Assertion `!coin.IsSpent()' failed.
244 : 0 : return;
245 : : }
246 : 1092 : bool expected_code_path = false;
247 : 1092 : const int height{int(fuzzed_data_provider.ConsumeIntegral<uint32_t>() >> 1)};
248 : 1092 : const bool possible_overwrite = fuzzed_data_provider.ConsumeBool();
249 : 1092 : try {
250 [ + - ]: 1092 : AddCoins(coins_view_cache, transaction, height, possible_overwrite);
251 : : expected_code_path = true;
252 [ - - ]: 0 : } catch (const std::logic_error& e) {
253 [ - - - - ]: 0 : if (e.what() == std::string{"Attempted to overwrite an unspent coin (when possible_overwrite is false)"}) {
254 [ - - ]: 0 : assert(!possible_overwrite);
255 : : expected_code_path = true;
256 : : }
257 : 0 : }
258 : 0 : assert(expected_code_path);
259 : 1092 : },
260 : 1206 : [&] {
261 [ + - ]: 1206 : (void)AreInputsStandard(CTransaction{random_mutable_transaction}, coins_view_cache);
262 : 1206 : },
263 : 628 : [&] {
264 [ + - ]: 628 : TxValidationState state;
265 : 628 : CAmount tx_fee_out;
266 [ + - ]: 628 : const CTransaction transaction{random_mutable_transaction};
267 [ + + ]: 628 : if (ContainsSpentInput(transaction, coins_view_cache)) {
268 : : // Avoid:
269 : : // consensus/tx_verify.cpp:171: bool Consensus::CheckTxInputs(const CTransaction &, TxValidationState &, const CCoinsViewCache &, int, CAmount &): Assertion `!coin.IsSpent()' failed.
270 : : return;
271 : : }
272 [ + - ]: 625 : TxValidationState dummy;
273 [ + - + - ]: 625 : if (!CheckTransaction(transaction, dummy)) {
274 : : // It is not allowed to call CheckTxInputs if CheckTransaction failed
275 : 625 : return;
276 : : }
277 [ # # # # ]: 0 : if (Consensus::CheckTxInputs(transaction, state, coins_view_cache, fuzzed_data_provider.ConsumeIntegralInRange<int>(0, std::numeric_limits<int>::max()), tx_fee_out)) {
278 [ # # ]: 0 : assert(MoneyRange(tx_fee_out));
279 : : }
280 : 1253 : },
281 : 3398 : [&] {
282 : 3398 : const CTransaction transaction{random_mutable_transaction};
283 [ + + ]: 3398 : if (ContainsSpentInput(transaction, coins_view_cache)) {
284 : : // Avoid:
285 : : // consensus/tx_verify.cpp:130: unsigned int GetP2SHSigOpCount(const CTransaction &, const CCoinsViewCache &): Assertion `!coin.IsSpent()' failed.
286 : 14 : return;
287 : : }
288 [ + - ]: 3384 : (void)GetP2SHSigOpCount(transaction, coins_view_cache);
289 : 3398 : },
290 : 280 : [&] {
291 : 280 : const CTransaction transaction{random_mutable_transaction};
292 [ + + ]: 280 : if (ContainsSpentInput(transaction, coins_view_cache)) {
293 : : // Avoid:
294 : : // consensus/tx_verify.cpp:130: unsigned int GetP2SHSigOpCount(const CTransaction &, const CCoinsViewCache &): Assertion `!coin.IsSpent()' failed.
295 : : return;
296 : : }
297 [ - + ]: 278 : const auto flags = script_verify_flags::from_int(fuzzed_data_provider.ConsumeIntegral<script_verify_flags::value_type>());
298 [ - + - - : 278 : if (!transaction.vin.empty() && (flags & SCRIPT_VERIFY_WITNESS) != 0 && (flags & SCRIPT_VERIFY_P2SH) == 0) {
- - ]
299 : : // Avoid:
300 : : // script/interpreter.cpp:1705: size_t CountWitnessSigOps(const CScript &, const CScript &, const CScriptWitness &, unsigned int): Assertion `(flags & SCRIPT_VERIFY_P2SH) != 0' failed.
301 : : return;
302 : : }
303 [ + - ]: 278 : (void)GetTransactionSigOpCost(transaction, coins_view_cache, flags);
304 : 280 : },
305 : 1013 : [&] {
306 [ + - ]: 1013 : (void)IsWitnessStandard(CTransaction{random_mutable_transaction}, coins_view_cache);
307 : 1013 : });
308 : : }
309 : 12731 : }
310 : :
311 [ + - ]: 6124 : FUZZ_TARGET(coins_view, .init = initialize_coins_view)
312 : : {
313 [ + - ]: 5670 : FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
314 : 5670 : CCoinsView backend_coins_view;
315 [ + - ]: 5670 : TestCoinsView(fuzzed_data_provider, backend_coins_view, /*is_db=*/false);
316 : 5670 : }
317 : :
318 [ + - ]: 7515 : FUZZ_TARGET(coins_view_db, .init = initialize_coins_view)
319 : : {
320 : 7061 : FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
321 : 7061 : auto db_params = DBParams{
322 : : .path = "",
323 : : .cache_bytes = 1_MiB,
324 : : .memory_only = true,
325 : 7061 : };
326 [ + - ]: 7061 : CCoinsViewDB coins_db{std::move(db_params), CoinsViewOptions{}};
327 [ + - ]: 7061 : TestCoinsView(fuzzed_data_provider, coins_db, /*is_db=*/true);
328 : 14122 : }
|