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