OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include <stddef.h> | |
6 | |
7 #include "base/hash.h" | |
8 #include "base/numerics/safe_conversions.h" | |
9 #include "media/base/bit_reader.h" | |
10 #include "media/base/test_random.h" | |
11 | |
12 // Given |value|, return a number between |min| and |max|, inclusive. | |
13 int DetermineNumBits(int value, int min, int max) { | |
14 DCHECK_GT(max, min); | |
15 int range = max - min + 1; | |
16 return (value % range) + min; | |
17 } | |
18 | |
19 // Entry point for LibFuzzer. | |
20 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { | |
21 media::BitReader reader(data, base::checked_cast<int>(size)); | |
22 | |
23 // Need a simple random number generator to generate the number of bits to | |
24 // read/skip in a reproducible way (given the same |data|). | |
DaleCurtis
2016/03/04 18:54:36
Add a comment that Hash() is used to ensure the se
jrummell
2016/03/04 23:47:13
Done.
| |
25 media::TestRandom rnd(base::Hash(reinterpret_cast<const char*>(data), size)); | |
26 | |
27 // Read and skip through the data in |reader|. | |
28 while (reader.bits_available() > 0) { | |
29 if (rnd.Rand() & 1) { | |
30 // Read up to 64 bits. This may fail if there is not enough bits | |
31 // remaining, but it doesn't matter (testing for failures is also good). | |
32 uint64_t value; | |
33 if (!reader.ReadBits(DetermineNumBits(rnd.Rand(), 1, 64), &value)) | |
DaleCurtis
2016/03/04 18:54:36
I think this is clearer w/o the helper function ac
jrummell
2016/03/04 23:47:13
Done.
| |
34 break; | |
35 } else { | |
36 // Skip up to 128 bits. As above, this may fail. | |
37 if (!reader.SkipBits(DetermineNumBits(rnd.Rand(), 1, 128))) | |
DaleCurtis
2016/03/04 18:54:36
Ditto:
ReadBits(rnd.Rand() % 128 + 1);
jrummell
2016/03/04 23:47:13
Done.
| |
38 break; | |
39 } | |
40 } | |
41 return 0; | |
42 } | |
OLD | NEW |