OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef Fuzz_DEFINED | 8 #ifndef Fuzz_DEFINED |
9 #define Fuzz_DEFINED | 9 #define Fuzz_DEFINED |
10 | 10 |
11 #include "SkData.h" | 11 #include "SkData.h" |
12 #include "SkTRegistry.h" | 12 #include "SkTRegistry.h" |
13 #include "SkTypes.h" | 13 #include "SkTypes.h" |
14 | 14 |
15 class Fuzz : SkNoncopyable { | 15 class Fuzz : SkNoncopyable { |
16 public: | 16 public: |
17 explicit Fuzz(SkData*); | 17 explicit Fuzz(SkData*); |
18 | 18 |
| 19 // Returns the total number of "random" bytes available. |
| 20 size_t size(); |
| 21 // Returns the total number of "random" bytes remaining for randomness. |
| 22 size_t remaining(); |
| 23 |
| 24 template <typename T> |
| 25 bool next(T* n); |
| 26 |
19 bool nextBool(); | 27 bool nextBool(); |
20 uint8_t nextB(); | 28 uint8_t nextB(); |
21 uint32_t nextU(); | 29 uint32_t nextU(); |
22 // This can be nan, +- infinity, 0, anything. | 30 // This can be nan, +- infinity, 0, anything. |
23 float nextF(); | 31 float nextF(); |
24 // Returns a float between [0..1) as a IEEE float | 32 // Returns a float between [0..1) as a IEEE float |
25 float nextF1(); | 33 float nextF1(); |
26 | 34 |
27 // Return the next fuzzed value [min, max) as an unsigned 32bit integer. | 35 // Return the next fuzzed value [min, max) as an unsigned 32bit integer. |
28 uint32_t nextRangeU(uint32_t min, uint32_t max); | 36 uint32_t nextRangeU(uint32_t min, uint32_t max); |
29 /** | 37 /** |
30 * Returns next fuzzed value [min...max) as a float. | 38 * Returns next fuzzed value [min...max) as a float. |
31 * Will not be Infinity or NaN. | 39 * Will not be Infinity or NaN. |
32 */ | 40 */ |
33 float nextRangeF(float min, float max); | 41 float nextRangeF(float min, float max); |
34 | 42 |
35 void signalBug (); // Tell afl-fuzz these inputs found a bug. | 43 void signalBug (); // Tell afl-fuzz these inputs found a bug. |
36 void signalBoring(); // Tell afl-fuzz these inputs are not worth testing. | 44 void signalBoring(); // Tell afl-fuzz these inputs are not worth testing. |
37 | 45 |
38 private: | 46 private: |
39 template <typename T> | 47 template <typename T> |
40 T nextT(); | 48 T nextT(); |
41 | 49 |
42 SkAutoTUnref<SkData> fBytes; | 50 SkAutoTUnref<SkData> fBytes; |
43 int fNextByte; | 51 int fNextByte; |
44 }; | 52 }; |
45 | 53 |
| 54 template <typename T> |
| 55 bool Fuzz::next(T* n) { |
| 56 if (fNextByte + sizeof(T) > fBytes->size()) { |
| 57 return false; |
| 58 } |
| 59 |
| 60 memcpy(n, fBytes->bytes() + fNextByte, sizeof(T)); |
| 61 fNextByte += sizeof(T); |
| 62 return true; |
| 63 } |
| 64 |
46 struct Fuzzable { | 65 struct Fuzzable { |
47 const char* name; | 66 const char* name; |
48 void (*fn)(Fuzz*); | 67 void (*fn)(Fuzz*); |
49 }; | 68 }; |
50 | 69 |
51 #define DEF_FUZZ(name, f) \ | 70 #define DEF_FUZZ(name, f) \ |
52 static void fuzz_##name(Fuzz*); \ | 71 static void fuzz_##name(Fuzz*); \ |
53 SkTRegistry<Fuzzable> register_##name({#name, fuzz_##name}); \ | 72 SkTRegistry<Fuzzable> register_##name({#name, fuzz_##name}); \ |
54 static void fuzz_##name(Fuzz* f) | 73 static void fuzz_##name(Fuzz* f) |
55 | 74 |
56 #endif//Fuzz_DEFINED | 75 #endif//Fuzz_DEFINED |
OLD | NEW |