mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #ifndef Fuzz_DEFINED |
| 9 | #define Fuzz_DEFINED |
| 10 | |
| 11 | #include "SkData.h" |
| 12 | #include "SkTRegistry.h" |
| 13 | #include "SkTypes.h" |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 14 | |
| 15 | class Fuzz : SkNoncopyable { |
| 16 | public: |
| 17 | explicit Fuzz(SkData*); |
| 18 | |
kjlubick | e565450 | 2016-07-19 16:50:03 -0700 | [diff] [blame] | 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 | |
kjlubick | 5bd98a2 | 2016-02-18 06:27:38 -0800 | [diff] [blame] | 27 | bool nextBool(); |
mtklein | 24a22c7 | 2016-01-14 04:59:42 -0800 | [diff] [blame] | 28 | uint8_t nextB(); |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 29 | uint32_t nextU(); |
kjlubick | 5bd98a2 | 2016-02-18 06:27:38 -0800 | [diff] [blame] | 30 | // This can be nan, +- infinity, 0, anything. |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 31 | float nextF(); |
kjlubick | 4319593 | 2016-04-05 12:48:47 -0700 | [diff] [blame] | 32 | // Returns a float between [0..1) as a IEEE float |
| 33 | float nextF1(); |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 34 | |
kjlubick | 5bd98a2 | 2016-02-18 06:27:38 -0800 | [diff] [blame] | 35 | // Return the next fuzzed value [min, max) as an unsigned 32bit integer. |
| 36 | uint32_t nextRangeU(uint32_t min, uint32_t max); |
| 37 | /** |
| 38 | * Returns next fuzzed value [min...max) as a float. |
| 39 | * Will not be Infinity or NaN. |
| 40 | */ |
| 41 | float nextRangeF(float min, float max); |
| 42 | |
mtklein | a115942 | 2016-01-15 05:46:54 -0800 | [diff] [blame] | 43 | void signalBug (); // Tell afl-fuzz these inputs found a bug. |
| 44 | void signalBoring(); // Tell afl-fuzz these inputs are not worth testing. |
| 45 | |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 46 | private: |
mtklein | a115942 | 2016-01-15 05:46:54 -0800 | [diff] [blame] | 47 | template <typename T> |
| 48 | T nextT(); |
| 49 | |
bungeman | ffae30d | 2016-08-03 13:32:32 -0700 | [diff] [blame] | 50 | sk_sp<SkData> fBytes; |
mtklein | 24a22c7 | 2016-01-14 04:59:42 -0800 | [diff] [blame] | 51 | int fNextByte; |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 52 | }; |
| 53 | |
kjlubick | e565450 | 2016-07-19 16:50:03 -0700 | [diff] [blame] | 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 | |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 65 | struct Fuzzable { |
| 66 | const char* name; |
| 67 | void (*fn)(Fuzz*); |
| 68 | }; |
| 69 | |
| 70 | #define DEF_FUZZ(name, f) \ |
| 71 | static void fuzz_##name(Fuzz*); \ |
| 72 | SkTRegistry<Fuzzable> register_##name({#name, fuzz_##name}); \ |
| 73 | static void fuzz_##name(Fuzz* f) |
| 74 | |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 75 | #endif//Fuzz_DEFINED |