Branch data Line data Source code
1 : : // Copyright (c) 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 : : #include <serialize.h>
7 : : #include <streams.h>
8 : : #include <test/fuzz/FuzzedDataProvider.h>
9 : : #include <test/fuzz/fuzz.h>
10 : :
11 : : #include <algorithm>
12 : : #include <cassert>
13 : : #include <cstddef>
14 : : #include <ios>
15 : : #include <optional>
16 : : #include <span>
17 : : #include <utility>
18 : : #include <vector>
19 : :
20 : : /** Deserialization of a CScriptWitness behaves exactly like that of a
21 : : * std::vector<std::vector<unsigned char>>, and its accessors and serialization agree with that
22 : : * vector. */
23 [ + - ]: 478 : FUZZ_TARGET(script_witness_deserialize)
24 : : {
25 : : // Deserialize the input as a vector of vector of unsigned chars.
26 : 0 : size_t stack_left{0};
27 : 0 : std::optional<std::vector<std::vector<unsigned char>>> stack;
28 : 0 : try {
29 : 0 : SpanReader reader{buffer};
30 : 0 : std::vector<std::vector<unsigned char>> tmp;
31 [ # # ]: 0 : reader >> tmp;
32 : 0 : stack = std::move(tmp);
33 : 0 : stack_left = reader.size();
34 [ # # ]: 0 : } catch (const std::ios_base::failure&) {
35 : 0 : }
36 : :
37 : : // Start over, and deserialize as a CScriptWitness.
38 : 0 : std::optional<CScriptWitness> witness;
39 : 0 : size_t witness_left{0};
40 : 0 : try {
41 : 0 : SpanReader reader{buffer};
42 : 0 : CScriptWitness tmp;
43 [ # # ]: 0 : reader >> tmp;
44 : 0 : witness = std::move(tmp);
45 [ # # ]: 0 : witness_left = reader.size();
46 [ # # ]: 0 : } catch (const std::ios_base::failure&) {
47 : 0 : }
48 : :
49 : : // Check whether deserialization success matches between them.
50 [ # # ]: 0 : assert(stack.has_value() == witness.has_value());
51 : :
52 : : // If deserialization succeeded (in both), compare equivalence in behavior.
53 [ # # # # ]: 0 : if (!stack) return;
54 : : // Make sure the number of consumed bytes matches.
55 [ # # ]: 0 : assert(stack_left == witness_left);
56 : : // Check equivalence after converting CScriptWitness to vector of vectors.
57 [ # # # # ]: 0 : assert(witness->ToVectors() == *stack);
58 : : // Check size/empty.
59 [ # # # # ]: 0 : assert(witness->size() == stack->size());
60 [ # # ]: 0 : assert(witness->empty() == stack->empty());
61 : : // Check equivalence after converting vector of vectors to CScriptWitness.
62 [ # # # # ]: 0 : assert(CScriptWitness{*stack} == *witness);
63 [ # # ]: 0 : if (!stack->empty()) {
64 : : // Compare first and last element.
65 [ # # ]: 0 : assert(std::ranges::equal(witness->front(), stack->front()));
66 [ # # ]: 0 : assert(std::ranges::equal(witness->back(), stack->back()));
67 : : // A copy has the same last element.
68 [ # # # # ]: 0 : assert(std::ranges::equal(CScriptWitness{*witness}.back(), stack->back()));
69 : : // Compare elements accessed by index. As indexing may take time linear in the index, at most MAX_INDEXED_READS
70 : : // of them: all if there are that few, and otherwise those at positions read from the rest of the input, after
71 : : // the serialized witness.
72 : 0 : constexpr size_t MAX_INDEXED_READS{64};
73 [ # # # # ]: 0 : if (stack->size() <= MAX_INDEXED_READS) {
74 [ # # # # ]: 0 : for (size_t i = 0; i < stack->size(); ++i) {
75 [ # # ]: 0 : assert(std::ranges::equal((*witness)[i], (*stack)[i]));
76 : : }
77 : : } else {
78 : 0 : FuzzedDataProvider provider{buffer.last(witness_left).data(), witness_left};
79 [ # # # # ]: 0 : LIMITED_WHILE(provider.remaining_bytes() > 0, MAX_INDEXED_READS) {
80 [ # # ]: 0 : const auto i = provider.ConsumeIntegralInRange<size_t>(0, stack->size() - 1);
81 [ # # ]: 0 : assert(std::ranges::equal((*witness)[i], (*stack)[i]));
82 : : }
83 : : }
84 : : }
85 : :
86 : : // Check reserialization.
87 : 0 : std::vector<unsigned char> ser_stack, ser_witness;
88 [ # # ]: 0 : VectorWriter{ser_stack, 0, *stack};
89 [ # # ]: 0 : VectorWriter{ser_witness, 0, *witness};
90 [ # # ]: 0 : assert(ser_stack == ser_witness);
91 : : // Deserialization only accepts canonically encoded sizes, so what was read is the stack's only serialization.
92 [ # # ]: 0 : assert(std::ranges::equal(ser_witness, buffer.first(buffer.size() - witness_left)));
93 [ # # # # ]: 0 : assert(::GetSerializeSize(*witness) == ser_stack.size());
94 : : // Sanity check memory usage.
95 [ # # ]: 0 : if (stack->empty()) {
96 [ # # ]: 0 : assert(witness->DynamicMemoryUsage() == 0);
97 : : } else {
98 [ # # # # ]: 0 : assert(witness->DynamicMemoryUsage() >= ser_stack.size());
99 : : }
100 [ # # ]: 0 : }
|