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