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 <random.h>
6 : : #include <span.h>
7 : : #include <test/fuzz/util.h>
8 : : #include <util/bitset.h>
9 : :
10 : : #include <bitset>
11 : : #include <vector>
12 : :
13 : : namespace {
14 : :
15 : : /** Pop the first byte from a byte-span, and return it. */
16 : 2000586 : uint8_t ReadByte(FuzzBufferType& buffer)
17 : : {
18 [ + + ]: 2000586 : if (buffer.empty()) return 0;
19 : 1998250 : uint8_t ret = buffer.front();
20 : 1998250 : buffer = buffer.subspan(1);
21 : 1998250 : return ret;
22 : : }
23 : :
24 : : /** Perform a simulation fuzz test on BitSet type S. */
25 : : template<typename S>
26 : 2928 : void TestType(FuzzBufferType buffer)
27 : : {
28 : : /** This fuzz test's design is based on the assumption that the actual bits stored in the
29 : : * bitsets and their simulations do not matter for the purpose of detecting edge cases, thus
30 : : * these are taken from a deterministically-seeded RNG instead. To provide some level of
31 : : * variation however, pick the seed based on the buffer size and size of the chosen bitset. */
32 : 2928 : InsecureRandomContext rng(buffer.size() + 0x10000 * S::Size());
33 : :
34 : : using Sim = std::bitset<S::Size()>;
35 : : // Up to 4 real BitSets (initially 2).
36 : 2928 : std::vector<S> real(2);
37 : : // Up to 4 std::bitsets with the same corresponding contents.
38 [ + - ][ + - ]: 2928 : std::vector<Sim> sim(2);
[ + - ][ + - ]
[ + - ]
39 : :
40 : : /* Compare sim[idx] with real[idx], using all inspector operations. */
41 : 835596 : auto compare_fn = [&](unsigned idx) {
42 : : /* iterators and operator[] */
43 [ + + + + : 416334 : auto it = real[idx].begin();
+ + ]
44 : 416334 : unsigned first = S::Size();
45 : 416334 : unsigned last = S::Size();
46 : 36695742 : for (unsigned i = 0; i < S::Size(); ++i) {
[ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + ]
47 : 36279408 : bool match = (it != real[idx].end()) && *it == i;
[ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + ]
48 : 36279408 : assert(sim[idx][i] == real[idx][i]);
[ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + ]
49 : 36279408 : assert(match == real[idx][i]);
[ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + ]
50 : 36279408 : assert((it == real[idx].end()) != (it != real[idx].end()));
[ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + ]
51 : 36279408 : if (match) {
[ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + ]
52 : 7259876 : ++it;
53 : 7259876 : if (first == S::Size()) first = i;
[ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + ]
54 : : last = i;
55 : : }
56 : : }
57 : 416334 : assert(it == real[idx].end());
[ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + ]
58 [ - + - + : 416334 : assert(!(it != real[idx].end()));
- + ]
59 : : /* Any / None */
60 : 897904 : assert(sim[idx].any() == real[idx].Any());
[ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + ]
61 : 744263 : assert(sim[idx].none() == real[idx].None());
[ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + ]
62 : : /* First / Last */
63 : 416334 : if (sim[idx].any()) {
[ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + ]
64 : 336904 : assert(first == real[idx].First());
[ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + ]
65 : 336904 : assert(last == real[idx].Last());
[ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + ]
66 : : }
67 : : /* Count */
68 : 569975 : assert(sim[idx].count() == real[idx].Count());
[ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + ]
69 : : };
70 : :
71 [ + + + + ]: 850658 : LIMITED_WHILE (buffer.size() > 0, 1000) {
[ + + + + ]
[ + + + + ]
[ + + + + ]
[ + + + + ]
72 : : // Read one byte to determine which operation to execute on the BitSets.
73 : 847730 : int command = ReadByte(buffer) % 64;
74 : : // Read another byte that determines which bitsets will be involved.
75 : 847730 : unsigned args = ReadByte(buffer);
76 [ - + ]: 847730 : unsigned dest = ((args & 7) * sim.size()) >> 3;
77 : 847730 : unsigned src = (((args >> 3) & 7) * sim.size()) >> 3;
78 : 847730 : unsigned aux = (((args >> 6) & 3) * sim.size()) >> 2;
79 : : // Args are in range for non-empty sim, or sim is completely empty and will be grown
80 : 847730 : assert((sim.empty() && dest == 0 && src == 0 && aux == 0) ||
[ + + - +
+ - + - +
- - + ]
81 : : (!sim.empty() && dest < sim.size() && src < sim.size() && aux < sim.size()));
82 : :
83 : : // Pick one operation based on value of command. Not all operations are always applicable.
84 : : // Loop through the applicable ones until command reaches 0 (which avoids the need to
85 : : // compute the number of applicable commands ahead of time).
86 : : while (true) {
87 [ + + + + ]: 2018766 : if (dest < sim.size() && command-- == 0) {
88 : : /* Set() (true) */
89 : 82487 : unsigned val = ReadByte(buffer) % S::Size();
90 [ - + ]: 82487 : assert(sim[dest][val] == real[dest][val]);
91 [ + - ][ + - ]: 82487 : sim[dest].set(val);
[ + - ][ + - ]
[ + - ]
92 : 82487 : real[dest].Set(val);
93 : 82487 : break;
94 [ + + + + ]: 1936279 : } else if (dest < sim.size() && command-- == 0) {
95 : : /* Reset() */
96 : 26957 : unsigned val = ReadByte(buffer) % S::Size();
97 [ - + ]: 26957 : assert(sim[dest][val] == real[dest][val]);
98 [ + - ][ + - ]: 26957 : sim[dest].reset(val);
[ + - ][ + - ]
[ + - ]
99 : 26957 : real[dest].Reset(val);
100 : 26957 : break;
101 [ + + + + ]: 1909322 : } else if (dest < sim.size() && command-- == 0) {
102 : : /* Set() (conditional) */
103 : 24159 : unsigned val = ReadByte(buffer) % S::Size();
104 [ - + ]: 24159 : assert(sim[dest][val] == real[dest][val]);
105 [ + - ][ + - ]: 24159 : sim[dest].set(val, args >> 7);
[ + - ][ + - ]
[ + - ]
106 : 24159 : real[dest].Set(val, args >> 7);
107 : 24159 : break;
108 [ + + + + ]: 1885163 : } else if (sim.size() < 4 && command-- == 0) {
109 : : /* Construct empty. */
110 [ + - ][ + - ]: 11032 : sim.resize(sim.size() + 1);
[ + - ][ + - ]
[ + - ]
111 [ + - ][ + - ]: 11032 : real.resize(real.size() + 1);
[ + - ][ + - ]
[ + - ]
112 : : break;
113 [ + + + + ]: 1874131 : } else if (sim.size() < 4 && command-- == 0) {
114 : : /* Construct singleton. */
115 : 11361 : unsigned val = ReadByte(buffer) % S::Size();
116 [ + + ]: 13862 : std::bitset<S::Size()> newset;
117 [ + - ][ + - ]: 11361 : newset[val] = true;
[ + - ][ + - ]
[ + - ]
118 [ + - ][ + - ]: 11361 : sim.push_back(newset);
[ + - ][ + - ]
[ + - ]
119 [ + - ][ + - ]: 11361 : real.push_back(S::Singleton(val));
[ + - ][ + - ]
[ + - ]
120 : : break;
121 [ + + + + ]: 1862770 : } else if (dest < sim.size() && command-- == 0) {
[ + + + + ]
122 : : /* Make random. */
123 : 49291 : compare_fn(dest);
124 : 49291 : sim[dest].reset();
125 : 49291 : real[dest] = S{};
126 [ + + ][ + + ]: 4222171 : for (unsigned i = 0; i < S::Size(); ++i) {
[ + + ][ + + ]
[ + + ]
127 [ + + ][ + + ]: 4172880 : if (rng.randbool()) {
128 : 2086884 : sim[dest][i] = true;
129 : 2086884 : real[dest].Set(i);
130 : : }
131 : : }
132 : : break;
133 [ + + + + ]: 1813479 : } else if (dest < sim.size() && command-- == 0) {
[ + + + + ]
134 : : /* Assign initializer list. */
135 : 62152 : unsigned r1 = rng.randrange(S::Size());
136 : 62152 : unsigned r2 = rng.randrange(S::Size());
137 : 62152 : unsigned r3 = rng.randrange(S::Size());
138 : 62152 : compare_fn(dest);
139 : 62152 : sim[dest].reset();
140 : 62152 : real[dest] = {r1, r2, r3};
141 [ + - ][ + - ]: 62152 : sim[dest].set(r1);
[ + - ][ + - ]
[ + - ]
142 [ + - ][ + - ]: 62152 : sim[dest].set(r2);
[ + - ][ + - ]
[ + - ]
143 [ + - ][ + - ]: 62152 : sim[dest].set(r3);
[ + - ][ + - ]
[ + - ]
144 : : break;
145 [ + + + + ]: 1751327 : } else if (!sim.empty() && command-- == 0) {
[ + + + + ]
146 : : /* Destruct. */
147 : 58843 : compare_fn(sim.size() - 1);
148 : 58843 : sim.pop_back();
149 : 58843 : real.pop_back();
150 : : break;
151 [ + + + + : 1692484 : } else if (sim.size() < 4 && src < sim.size() && command-- == 0) {
+ + ][ + +
+ + + + ]
152 : : /* Copy construct. */
153 [ + - ][ + - ]: 10331 : sim.emplace_back(sim[src]);
[ + - ][ + - ]
[ + - ]
154 [ + - ][ + - ]: 10331 : real.emplace_back(real[src]);
[ + - ][ + - ]
[ + - ]
155 : : break;
156 [ + + + - : 1682153 : } else if (src < sim.size() && dest < sim.size() && command-- == 0) {
+ + ][ + +
+ - + + ]
157 : : /* Copy assign. */
158 : 22983 : compare_fn(dest);
159 : 22983 : sim[dest] = sim[src];
160 : 22983 : real[dest] = real[src];
161 : 22983 : break;
162 [ + + + - : 1659170 : } else if (src < sim.size() && dest < sim.size() && command-- == 0) {
+ + ][ + +
+ - + + ]
163 : : /* swap() function. */
164 : 16775 : swap(sim[dest], sim[src]);
165 : 696938 : swap(real[dest], real[src]);
166 : : break;
167 [ + + + + ]: 1642395 : } else if (sim.size() < 4 && command-- == 0) {
[ + + + + ]
[ + + + + ]
168 : : /* Construct with initializer list. */
169 : 28587 : unsigned r1 = rng.randrange(S::Size());
170 : 28587 : unsigned r2 = rng.randrange(S::Size());
171 [ + - ][ + - ]: 28587 : sim.emplace_back();
[ + - ][ + - ]
[ + - ]
172 [ + - ][ + - ]: 28587 : sim.back().set(r1);
[ + - ][ + - ]
[ + - ]
173 [ + - ][ + - ]: 28587 : sim.back().set(r2);
[ + - ][ + - ]
[ + - ]
174 [ + - ][ + - ]: 189700 : real.push_back(S{r1, r2});
[ + - ][ + - ]
[ + - ]
175 : : break;
176 [ + + + + ]: 1613808 : } else if (dest < sim.size() && command-- == 0) {
[ + + + + ]
[ + + + + ]
177 : : /* Fill() + copy assign. */
178 : 57848 : unsigned len = ReadByte(buffer) % S::Size();
179 : 57848 : compare_fn(dest);
180 : 57848 : sim[dest].reset();
181 [ + + ][ + + ]: 1898881 : for (unsigned i = 0; i < len; ++i) sim[dest][i] = true;
[ + + ]
182 : 57848 : real[dest] = S::Fill(len);
183 : 57848 : break;
184 [ + + + + ]: 1555960 : } else if (src < sim.size() && command-- == 0) {
[ + + + + ]
[ + + + + ]
185 : : /* Iterator copy based compare. */
186 : 100660 : unsigned val = ReadByte(buffer) % S::Size();
187 : : /* In a first loop, compare begin..end, and copy to it_copy at some point. */
188 [ + + ]: 100660 : auto it = real[src].begin(), it_copy = it;
189 [ + + ][ + + ]: 9537284 : for (unsigned i = 0; i < S::Size(); ++i) {
[ + + ]
190 [ + + ][ + + ]: 9436624 : if (i == val) it_copy = it;
[ + + ]
191 [ + + + + ]: 9436624 : bool match = (it != real[src].end()) && *it == i;
[ + + + + ]
[ + + + + ]
192 [ - + ][ - + ]: 9436624 : assert(match == sim[src][i]);
[ - + ]
193 [ + + ][ + + ]: 9436624 : if (match) ++it;
[ + + ]
194 : : }
195 [ - + ][ - + ]: 100660 : assert(it == real[src].end());
[ - + ]
196 : : /* Then compare from the copied point again to end. */
197 [ + + ][ + + ]: 4737505 : for (unsigned i = val; i < S::Size(); ++i) {
[ + + ]
198 [ + + + + ]: 4636845 : bool match = (it_copy != real[src].end()) && *it_copy == i;
[ + + + + ]
[ + + + + ]
199 [ - + ][ - + ]: 4636845 : assert(match == sim[src][i]);
[ - + ]
200 [ + + ][ + + ]: 4636845 : if (match) ++it_copy;
[ + + ]
201 : : }
202 [ - + ][ - + ]: 100660 : assert(it_copy == real[src].end());
[ - + ][ - + ]
[ - + ]
203 : : break;
204 [ + + + - : 1455300 : } else if (src < sim.size() && dest < sim.size() && command-- == 0) {
+ + ][ + +
+ - + + ]
[ + + + -
+ + ]
205 : : /* operator|= */
206 : 23198 : compare_fn(dest);
207 : 23198 : sim[dest] |= sim[src];
208 : 23198 : real[dest] |= real[src];
209 : : break;
210 [ + + + - : 1432102 : } else if (src < sim.size() && dest < sim.size() && command-- == 0) {
+ + ][ + +
+ - + + ]
[ + + + -
+ + ][ + +
+ - + + ]
211 : : /* operator&= */
212 : 22598 : compare_fn(dest);
213 : 22598 : sim[dest] &= sim[src];
214 : 22598 : real[dest] &= real[src];
215 : : break;
216 [ + + + - : 1409504 : } else if (src < sim.size() && dest < sim.size() && command-- == 0) {
+ + ][ + +
+ - + + ]
[ + + + -
+ + ][ + +
+ - + + ]
217 : : /* operator-= */
218 : 21056 : compare_fn(dest);
219 : 21056 : sim[dest] &= ~sim[src];
220 : 21056 : real[dest] -= real[src];
221 : 16728 : break;
222 [ + + + - : 1388448 : } else if (src < sim.size() && dest < sim.size() && command-- == 0) {
+ + ][ + +
+ - + + ]
[ + + + -
+ + ][ + +
+ - + + ]
223 : : /* operator^= */
224 : 15271 : compare_fn(dest);
225 : 15271 : sim[dest] ^= sim[src];
226 : 695434 : real[dest] ^= real[src];
227 : : break;
228 [ + + + - : 1373177 : } else if (src < sim.size() && dest < sim.size() && aux < sim.size() && command-- == 0) {
+ - + + ]
[ + + + -
+ - + + ]
[ + + + -
+ - + + ]
[ + + + -
+ - + + ]
229 : : /* operator| */
230 : 19186 : compare_fn(dest);
231 : 22703 : sim[dest] = sim[src] | sim[aux];
232 : 22703 : real[dest] = real[src] | real[aux];
233 : 19186 : break;
234 [ + + + - : 1353991 : } else if (src < sim.size() && dest < sim.size() && aux < sim.size() && command-- == 0) {
+ - + + ]
[ + + + -
+ - + + ]
[ + + + -
+ - + + ]
[ + + + -
+ - + + ]
[ + + + -
+ - + + ]
235 : : /* operator& */
236 : 20845 : compare_fn(dest);
237 : 24833 : sim[dest] = sim[src] & sim[aux];
238 : 24833 : real[dest] = real[src] & real[aux];
239 : 20845 : break;
240 [ + + + - : 1333146 : } else if (src < sim.size() && dest < sim.size() && aux < sim.size() && command-- == 0) {
+ - + + ]
[ + + + -
+ - + + ]
[ + + + -
+ - + + ]
[ + + + -
+ - + + ]
[ + + + -
+ - + + ]
241 : : /* operator- */
242 : 18814 : compare_fn(dest);
243 : 21764 : sim[dest] = sim[src] & ~sim[aux];
244 : 18814 : real[dest] = real[src] - real[aux];
245 : 18814 : break;
246 [ + + + - : 1314332 : } else if (src < sim.size() && dest < sim.size() && aux < sim.size() && command-- == 0) {
+ - + + ]
[ + + + -
+ - + + ]
[ + + + -
+ - + + ]
[ + + + -
+ - + + ]
[ + + + -
+ - + + ]
247 : : /* operator^ */
248 : 15925 : compare_fn(dest);
249 : 18268 : sim[dest] = sim[src] ^ sim[aux];
250 : 18268 : real[dest] = real[src] ^ real[aux];
251 : 15925 : break;
252 [ + + + - : 1298407 : } else if (src < sim.size() && aux < sim.size() && command-- == 0) {
+ + ][ + +
+ - + + ]
[ + + + -
+ + ][ + +
+ - + + ]
[ + + + -
+ + ]
253 : : /* IsSupersetOf() and IsSubsetOf() */
254 [ + + ][ - + ]: 89157 : bool is_superset = (sim[aux] & ~sim[src]).none();
255 [ + + ]: 90561 : bool is_subset = (sim[src] & ~sim[aux]).none();
256 [ - + ][ - + ]: 116071 : assert(real[src].IsSupersetOf(real[aux]) == is_superset);
[ - + ][ - + ]
[ - + ]
257 [ - + ][ - + ]: 63267 : assert(real[src].IsSubsetOf(real[aux]) == is_subset);
[ - + ][ - + ]
[ - + ]
258 [ - + ][ - + ]: 52804 : assert(real[aux].IsSupersetOf(real[src]) == is_subset);
[ - + ][ - + ]
259 [ - + ][ - + ]: 52804 : assert(real[aux].IsSubsetOf(real[src]) == is_superset);
[ - + ][ - + ]
260 : : break;
261 [ + + + - : 1235140 : } else if (src < sim.size() && aux < sim.size() && command-- == 0) {
+ + ][ + +
+ - + + ]
[ + + + -
+ + ][ + +
+ - + + ]
[ + + + -
+ + ]
262 : : /* operator== and operator!= */
263 [ - + ][ - + ]: 25829 : assert((sim[src] == sim[aux]) == (real[src] == real[aux]));
[ - + ][ - + ]
[ + + - + ]
264 [ - + ][ - + ]: 24229 : assert((sim[src] != sim[aux]) == (real[src] != real[aux]));
[ - + ][ - + ]
[ - + ]
265 : : break;
266 [ + + + - : 2034952 : } else if (src < sim.size() && aux < sim.size() && command-- == 0) {
+ + + + ]
[ + + + -
+ + + + ]
[ + + + -
+ + + + ]
[ + + + -
+ + + + ]
[ + + + -
+ + + + ]
267 : : /* Overlaps() */
268 [ - + ][ - + ]: 110300 : assert((sim[src] & sim[aux]).any() == real[src].Overlaps(real[aux]));
[ - + ][ - + ]
[ + + - + ]
269 [ - + ][ - + ]: 110300 : assert((sim[src] & sim[aux]).any() == real[aux].Overlaps(real[src]));
[ - + ][ - + ]
[ + + - + ]
270 : : break;
271 : : }
272 : : }
273 : : }
274 : : /* Fully compare the final state. */
275 [ - + + + ]: 11252 : for (unsigned i = 0; i < sim.size(); ++i) {
[ - + + + ]
[ - + + + ]
[ - + + + ]
[ - + + + ]
276 : 8324 : compare_fn(i);
277 : : }
278 : 2928 : }
279 : :
280 : : } // namespace
281 : :
282 [ + - ]: 2130 : FUZZ_TARGET(bitset)
283 : : {
284 : 1654 : unsigned typdat = ReadByte(buffer) % 8;
285 [ + + ]: 1654 : if (typdat == 0) {
286 : : /* 16 bits */
287 : 182 : TestType<bitset_detail::IntBitSet<uint16_t>>(buffer);
288 : 182 : TestType<bitset_detail::MultiIntBitSet<uint16_t, 1>>(buffer);
289 [ + + ]: 1472 : } else if (typdat == 1) {
290 : : /* 32 bits */
291 : 191 : TestType<bitset_detail::MultiIntBitSet<uint16_t, 2>>(buffer);
292 : 191 : TestType<bitset_detail::IntBitSet<uint32_t>>(buffer);
293 [ + + ]: 1281 : } else if (typdat == 2) {
294 : : /* 48 bits */
295 : 198 : TestType<bitset_detail::MultiIntBitSet<uint16_t, 3>>(buffer);
296 [ + + ]: 1083 : } else if (typdat == 3) {
297 : : /* 64 bits */
298 : 226 : TestType<bitset_detail::IntBitSet<uint64_t>>(buffer);
299 : 226 : TestType<bitset_detail::MultiIntBitSet<uint64_t, 1>>(buffer);
300 : 226 : TestType<bitset_detail::MultiIntBitSet<uint32_t, 2>>(buffer);
301 : 226 : TestType<bitset_detail::MultiIntBitSet<uint16_t, 4>>(buffer);
302 [ + + ]: 857 : } else if (typdat == 4) {
303 : : /* 96 bits */
304 : 196 : TestType<bitset_detail::MultiIntBitSet<uint32_t, 3>>(buffer);
305 [ + + ]: 661 : } else if (typdat == 5) {
306 : : /* 128 bits */
307 : 223 : TestType<bitset_detail::MultiIntBitSet<uint64_t, 2>>(buffer);
308 : 223 : TestType<bitset_detail::MultiIntBitSet<uint32_t, 4>>(buffer);
309 [ + + ]: 438 : } else if (typdat == 6) {
310 : : /* 192 bits */
311 : 213 : TestType<bitset_detail::MultiIntBitSet<uint64_t, 3>>(buffer);
312 [ + - ]: 225 : } else if (typdat == 7) {
313 : : /* 256 bits */
314 : 225 : TestType<bitset_detail::MultiIntBitSet<uint64_t, 4>>(buffer);
315 : : }
316 : 1654 : }
|