Branch data Line data Source code
1 : : // Copyright (c) 2009-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 <chainparams.h>
6 : : #include <key_io.h>
7 : : #include <pubkey.h>
8 : : #include <script/descriptor.h>
9 : : #include <test/fuzz/fuzz.h>
10 : : #include <test/fuzz/util/descriptor.h>
11 : : #include <util/chaintype.h>
12 : : #include <util/check.h>
13 : : #include <util/strencodings.h>
14 : :
15 : : //! The converter of mocked descriptors, needs to be initialized when the target is.
16 : : MockedDescriptorConverter MOCKED_DESC_CONVERTER;
17 : :
18 : : /** Test a successfully parsed descriptor. */
19 : 31224 : static void TestDescriptor(const Descriptor& desc, FlatSigningProvider& sig_provider, std::string& dummy, std::optional<bool>& is_ranged, std::optional<bool>& is_solvable)
20 : : {
21 : : // Trivial helpers.
22 : 31224 : (void)desc.IsRange();
23 : 31224 : (void)desc.IsSingleType();
24 : 31224 : (void)desc.GetOutputType();
25 : :
26 [ + + ]: 31224 : if (is_ranged.has_value()) {
27 [ - + ]: 26777 : assert(desc.IsRange() == *is_ranged);
28 : : } else {
29 : 4447 : is_ranged = desc.IsRange();
30 : : }
31 [ + + ]: 31224 : if (is_solvable.has_value()) {
32 [ - + ]: 26777 : assert(desc.IsSolvable() == *is_solvable);
33 : : } else {
34 : 4447 : is_solvable = desc.IsSolvable();
35 : : }
36 : :
37 : : // Serialization to string representation.
38 : 31224 : (void)desc.ToString();
39 : 31224 : (void)desc.ToPrivateString(sig_provider, dummy);
40 : 31224 : (void)desc.ToNormalizedString(sig_provider, dummy);
41 : :
42 : : // Serialization to Script.
43 : 31224 : DescriptorCache cache;
44 : 31224 : std::vector<CScript> out_scripts;
45 [ + - ]: 31224 : (void)desc.Expand(0, sig_provider, out_scripts, sig_provider, &cache);
46 [ + - ]: 31224 : (void)desc.ExpandPrivate(0, sig_provider, sig_provider);
47 [ + - ]: 31224 : (void)desc.ExpandFromCache(0, cache, out_scripts, sig_provider);
48 : :
49 : : // If we could serialize to script we must be able to infer using the same provider.
50 [ + + ]: 31224 : if (!out_scripts.empty()) {
51 [ + - - + ]: 29808 : assert(InferDescriptor(out_scripts.back(), sig_provider));
52 : :
53 : : // The ScriptSize() must match the size of the serialized Script. (ScriptSize() is set for all descs but 'combo()'.)
54 [ + - ]: 29808 : const bool is_combo{!desc.IsSingleType()};
55 [ + + + + : 30053 : assert(is_combo || desc.ScriptSize() == out_scripts.back().size());
+ - ]
56 : : }
57 : :
58 [ + - ]: 31224 : const auto max_sat_maxsig{desc.MaxSatisfactionWeight(true)};
59 [ + - ]: 31224 : const auto max_sat_nonmaxsig{desc.MaxSatisfactionWeight(false)};
60 : : // Whether an estimate is available must not depend on the signature-size
61 : : // assumption, and assuming non-max-size signatures must never increase it.
62 [ - + ]: 31224 : assert(max_sat_maxsig.has_value() == max_sat_nonmaxsig.has_value());
63 [ - + ]: 31224 : assert(max_sat_nonmaxsig <= max_sat_maxsig);
64 [ + - ]: 31224 : const auto max_elems{desc.MaxSatisfactionElems()};
65 : : // We must be able to estimate the max satisfaction size for any solvable descriptor (but combo).
66 [ + + + - : 31224 : const bool is_nontop_or_nonsolvable{!*is_solvable || !desc.GetOutputType()};
+ + ]
67 [ + + + - : 31224 : const bool is_input_size_info_set{max_sat_maxsig && max_sat_nonmaxsig && max_elems};
- + ]
68 [ - + ]: 1079 : assert(is_input_size_info_set || is_nontop_or_nonsolvable);
69 : :
70 [ + - ]: 31224 : auto max_key_expr = desc.GetMaxKeyExpr();
71 [ + - ]: 31224 : auto key_count = desc.GetKeyCount();
72 [ + + - + ]: 31224 : assert((max_key_expr == 0 && key_count == 0) || max_key_expr + 1 == key_count);
73 : 31224 : }
74 : :
75 : 2 : void initialize_descriptor_parse()
76 : : {
77 [ + - + - : 2 : static ECC_Context ecc_context{};
+ - ]
78 : 2 : SelectParams(ChainType::MAIN);
79 : 2 : }
80 : :
81 : 1 : void initialize_mocked_descriptor_parse()
82 : : {
83 : 1 : initialize_descriptor_parse();
84 : 1 : MOCKED_DESC_CONVERTER.Init();
85 : 1 : }
86 : :
87 [ + - ]: 4470 : FUZZ_TARGET(mocked_descriptor_parse, .init = initialize_mocked_descriptor_parse)
88 : : {
89 [ - + ]: 3994 : const std::string mocked_descriptor{buffer.begin(), buffer.end()};
90 [ - + + - : 3994 : if (const auto descriptor = MOCKED_DESC_CONVERTER.GetDescriptor(mocked_descriptor)) {
+ + ]
91 [ + - + + ]: 3975 : if (IsTooExpensive(MakeUCharSpan(*descriptor))) return;
92 : 3963 : FlatSigningProvider signing_provider;
93 [ - + ]: 3963 : std::string error;
94 [ - + + - ]: 3963 : const auto desc = Parse(*descriptor, signing_provider, error);
95 : 3963 : std::optional<bool> is_ranged;
96 : 3963 : std::optional<bool> is_solvable;
97 [ + + ]: 22701 : for (const auto& d : desc) {
98 [ - + ]: 18738 : assert(d);
99 [ + - ]: 18738 : TestDescriptor(*d, signing_provider, error, is_ranged, is_solvable);
100 : : }
101 : 7957 : }
102 : 3994 : }
103 : :
104 [ + - ]: 4175 : FUZZ_TARGET(descriptor_parse, .init = initialize_descriptor_parse)
105 : : {
106 [ + + ]: 3699 : if (IsTooExpensive(buffer)) return;
107 : :
108 : 3690 : const std::string descriptor(buffer.begin(), buffer.end());
109 : 3690 : FlatSigningProvider signing_provider;
110 : 3690 : std::string error;
111 [ + + ]: 11070 : for (const bool require_checksum : {true, false}) {
112 [ - + + - ]: 7380 : const auto desc = Parse(descriptor, signing_provider, error, require_checksum);
113 : 7380 : std::optional<bool> is_ranged;
114 : 7380 : std::optional<bool> is_solvable;
115 [ + + ]: 19866 : for (const auto& d : desc) {
116 [ - + ]: 12486 : assert(d);
117 [ + - ]: 12486 : TestDescriptor(*d, signing_provider, error, is_ranged, is_solvable);
118 : : }
119 : 7380 : }
120 : 3690 : }
|