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" |
Mike Reed | ab273fa | 2017-01-11 13:58:55 -0500 | [diff] [blame] | 12 | #include "../tools/Registry.h" |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 13 | #include "SkTypes.h" |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 14 | |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 15 | #include <cmath> |
kjlubick | 840f12a | 2016-10-25 06:11:05 -0700 | [diff] [blame] | 16 | |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 17 | class Fuzz : SkNoncopyable { |
| 18 | public: |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 19 | explicit Fuzz(sk_sp<SkData>); |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 20 | |
kjlubick | e565450 | 2016-07-19 16:50:03 -0700 | [diff] [blame] | 21 | // Returns the total number of "random" bytes available. |
| 22 | size_t size(); |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 23 | // Returns if there are no bytes remaining for fuzzing. |
| 24 | bool exhausted(); |
kjlubick | e565450 | 2016-07-19 16:50:03 -0700 | [diff] [blame] | 25 | |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 26 | // next() loads fuzzed bytes into the variable passed in by pointer. |
| 27 | // We use this approach instead of T next() because different compilers |
| 28 | // evaluate function parameters in different orders. If fuzz->next() |
| 29 | // returned 5 and then 7, foo(fuzz->next(), fuzz->next()) would be |
| 30 | // foo(5, 7) when compiled on GCC and foo(7, 5) when compiled on Clang. |
| 31 | // By requiring params to be passed in, we avoid the temptation to call |
| 32 | // next() in a way that does not consume fuzzed bytes in a single |
| 33 | // uplatform-independent order. |
kjlubick | e565450 | 2016-07-19 16:50:03 -0700 | [diff] [blame] | 34 | template <typename T> |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 35 | void next(T* t); |
| 36 | |
| 37 | // This is a convenient way to initialize more than one argument at a time. |
| 38 | template <typename Arg, typename... Args> |
| 39 | void next(Arg* first, Args... rest); |
kjlubick | e565450 | 2016-07-19 16:50:03 -0700 | [diff] [blame] | 40 | |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 41 | // nextRange returns values only in [min, max]. |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 42 | template <typename T, typename Min, typename Max> |
| 43 | void nextRange(T*, Min, Max); |
| 44 | |
| 45 | // nextN loads n * sizeof(T) bytes into ptr |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 46 | template <typename T> |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 47 | void nextN(T* ptr, int n); |
kjlubick | 85d3017 | 2016-10-24 11:53:35 -0700 | [diff] [blame] | 48 | |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 49 | void signalBug(); // Tell afl-fuzz these inputs found a bug. |
mtklein | a115942 | 2016-01-15 05:46:54 -0800 | [diff] [blame] | 50 | |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 51 | private: |
mtklein | a115942 | 2016-01-15 05:46:54 -0800 | [diff] [blame] | 52 | template <typename T> |
| 53 | T nextT(); |
| 54 | |
bungeman | ffae30d | 2016-08-03 13:32:32 -0700 | [diff] [blame] | 55 | sk_sp<SkData> fBytes; |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 56 | size_t fNextByte; |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 57 | }; |
| 58 | |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 59 | // UBSAN reminds us that bool can only legally hold 0 or 1. |
| 60 | template <> |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 61 | inline void Fuzz::next(bool* b) { |
| 62 | uint8_t n; |
| 63 | this->next(&n); |
| 64 | *b = (n & 1) == 1; |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 65 | } |
kjlubick | e565450 | 2016-07-19 16:50:03 -0700 | [diff] [blame] | 66 | |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 67 | template <typename T> |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 68 | inline void Fuzz::next(T* n) { |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 69 | if ((fNextByte + sizeof(T)) > fBytes->size()) { |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 70 | *n = 0; |
| 71 | memcpy(n, fBytes->bytes() + fNextByte, fBytes->size() - fNextByte); |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 72 | fNextByte = fBytes->size(); |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 73 | return; |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 74 | } |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 75 | memcpy(n, fBytes->bytes() + fNextByte, sizeof(T)); |
kjlubick | e565450 | 2016-07-19 16:50:03 -0700 | [diff] [blame] | 76 | fNextByte += sizeof(T); |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | template <typename Arg, typename... Args> |
| 80 | inline void Fuzz::next(Arg* first, Args... rest) { |
| 81 | this->next(first); |
| 82 | this->next(rest...); |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | template <> |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 86 | inline void Fuzz::nextRange(float* f, float min, float max) { |
| 87 | this->next(f); |
| 88 | if (!std::isnormal(*f) && *f != 0.0f) { |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 89 | // Don't deal with infinity or other strange floats. |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 90 | *f = max; |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 91 | } |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 92 | *f = min + std::fmod(std::abs(*f), (max - min + 1)); |
| 93 | } |
| 94 | |
| 95 | template <typename T, typename Min, typename Max> |
| 96 | inline void Fuzz::nextRange(T* n, Min min, Max max) { |
| 97 | this->next<T>(n); |
Kevin Lubick | d104266 | 2016-11-29 11:25:52 -0500 | [diff] [blame] | 98 | if (min == max) { |
| 99 | *n = min; |
| 100 | return; |
| 101 | } |
| 102 | if (min > max) { |
Kevin Lubick | c9f0cc8 | 2016-11-15 16:07:02 -0500 | [diff] [blame] | 103 | // Avoid misuse of nextRange |
| 104 | this->signalBug(); |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 105 | } |
Kevin Lubick | c9f0cc8 | 2016-11-15 16:07:02 -0500 | [diff] [blame] | 106 | if (*n < 0) { // Handle negatives |
| 107 | if (*n != std::numeric_limits<T>::lowest()) { |
| 108 | *n *= -1; |
| 109 | } |
| 110 | else { |
| 111 | *n = std::numeric_limits<T>::max(); |
| 112 | } |
| 113 | } |
| 114 | *n = min + (*n % ((size_t)max - min + 1)); |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | template <typename T> |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 118 | inline void Fuzz::nextN(T* ptr, int n) { |
| 119 | for (int i = 0; i < n; i++) { |
| 120 | this->next(ptr+i); |
| 121 | } |
kjlubick | e565450 | 2016-07-19 16:50:03 -0700 | [diff] [blame] | 122 | } |
| 123 | |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 124 | struct Fuzzable { |
| 125 | const char* name; |
| 126 | void (*fn)(Fuzz*); |
| 127 | }; |
| 128 | |
Mike Reed | ab273fa | 2017-01-11 13:58:55 -0500 | [diff] [blame] | 129 | #define DEF_FUZZ(name, f) \ |
| 130 | static void fuzz_##name(Fuzz*); \ |
| 131 | sk_tools::Registry<Fuzzable> register_##name({#name, fuzz_##name}); \ |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 132 | static void fuzz_##name(Fuzz* f) |
| 133 | |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 134 | #endif//Fuzz_DEFINED |