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" |
Kevin Lubick | bc9a1a8 | 2018-09-17 14:46:57 -0400 | [diff] [blame] | 13 | #include "SkImageFilter.h" |
Herb Derby | b549cc3 | 2017-03-27 13:35:15 -0400 | [diff] [blame] | 14 | #include "SkMalloc.h" |
Kevin Lubick | bc9a1a8 | 2018-09-17 14:46:57 -0400 | [diff] [blame] | 15 | #include "SkRegion.h" |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 16 | #include "SkTypes.h" |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 17 | |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 18 | #include <limits> |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 19 | #include <cmath> |
Kevin Lubick | 2541edf | 2018-01-11 10:27:14 -0500 | [diff] [blame] | 20 | #include <signal.h> |
Mike Klein | bf45c70 | 2018-06-11 11:56:57 -0400 | [diff] [blame] | 21 | #include <limits> |
kjlubick | 840f12a | 2016-10-25 06:11:05 -0700 | [diff] [blame] | 22 | |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 23 | class Fuzz : SkNoncopyable { |
| 24 | public: |
Kevin Lubick | 2541edf | 2018-01-11 10:27:14 -0500 | [diff] [blame] | 25 | explicit Fuzz(sk_sp<SkData> bytes) : fBytes(bytes), fNextByte(0) {} |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 26 | |
kjlubick | e565450 | 2016-07-19 16:50:03 -0700 | [diff] [blame] | 27 | // Returns the total number of "random" bytes available. |
Kevin Lubick | 2541edf | 2018-01-11 10:27:14 -0500 | [diff] [blame] | 28 | size_t size() { return fBytes->size(); } |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 29 | // Returns if there are no bytes remaining for fuzzing. |
Kevin Lubick | f84ded2 | 2018-10-23 09:28:48 -0400 | [diff] [blame] | 30 | bool exhausted() { |
Kevin Lubick | 2541edf | 2018-01-11 10:27:14 -0500 | [diff] [blame] | 31 | return fBytes->size() == fNextByte; |
| 32 | } |
kjlubick | e565450 | 2016-07-19 16:50:03 -0700 | [diff] [blame] | 33 | |
Kevin Lubick | f84ded2 | 2018-10-23 09:28:48 -0400 | [diff] [blame] | 34 | size_t remaining() { |
| 35 | return fBytes->size() - fNextByte; |
| 36 | } |
| 37 | |
| 38 | void deplete() { |
| 39 | fNextByte = fBytes->size(); |
| 40 | } |
| 41 | |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 42 | // next() loads fuzzed bytes into the variable passed in by pointer. |
| 43 | // We use this approach instead of T next() because different compilers |
| 44 | // evaluate function parameters in different orders. If fuzz->next() |
| 45 | // returned 5 and then 7, foo(fuzz->next(), fuzz->next()) would be |
| 46 | // foo(5, 7) when compiled on GCC and foo(7, 5) when compiled on Clang. |
| 47 | // By requiring params to be passed in, we avoid the temptation to call |
| 48 | // 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] | 49 | // platform-independent order. |
kjlubick | e565450 | 2016-07-19 16:50:03 -0700 | [diff] [blame] | 50 | template <typename T> |
Hal Canary | 6d9a51a | 2018-11-19 13:15:21 -0500 | [diff] [blame] | 51 | void next(T* t) { this->nextBytes(t, sizeof(T)); } |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 52 | |
| 53 | // This is a convenient way to initialize more than one argument at a time. |
| 54 | template <typename Arg, typename... Args> |
| 55 | void next(Arg* first, Args... rest); |
kjlubick | e565450 | 2016-07-19 16:50:03 -0700 | [diff] [blame] | 56 | |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 57 | // nextRange returns values only in [min, max]. |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 58 | template <typename T, typename Min, typename Max> |
| 59 | void nextRange(T*, Min, Max); |
| 60 | |
| 61 | // nextN loads n * sizeof(T) bytes into ptr |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 62 | template <typename T> |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 63 | void nextN(T* ptr, int n); |
kjlubick | 85d3017 | 2016-10-24 11:53:35 -0700 | [diff] [blame] | 64 | |
Kevin Lubick | 2541edf | 2018-01-11 10:27:14 -0500 | [diff] [blame] | 65 | void signalBug(){ |
| 66 | // Tell the fuzzer that these inputs found a bug. |
| 67 | SkDebugf("Signal bug\n"); |
| 68 | raise(SIGSEGV); |
| 69 | } |
mtklein | a115942 | 2016-01-15 05:46:54 -0800 | [diff] [blame] | 70 | |
Kevin Lubick | bc9a1a8 | 2018-09-17 14:46:57 -0400 | [diff] [blame] | 71 | // Specialized versions for when true random doesn't quite make sense |
| 72 | void next(bool* b); |
| 73 | void next(SkImageFilter::CropRect* cropRect); |
| 74 | void next(SkRegion* region); |
| 75 | |
| 76 | void nextRange(float* f, float min, float max); |
| 77 | |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 78 | private: |
mtklein | a115942 | 2016-01-15 05:46:54 -0800 | [diff] [blame] | 79 | template <typename T> |
| 80 | T nextT(); |
| 81 | |
bungeman | ffae30d | 2016-08-03 13:32:32 -0700 | [diff] [blame] | 82 | sk_sp<SkData> fBytes; |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 83 | size_t fNextByte; |
Kevin Lubick | e4be55d | 2018-03-30 15:05:13 -0400 | [diff] [blame] | 84 | friend void fuzz__MakeEncoderCorpus(Fuzz*); |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 85 | |
Hal Canary | 6d9a51a | 2018-11-19 13:15:21 -0500 | [diff] [blame] | 86 | void nextBytes(void* ptr, size_t size); |
| 87 | }; |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 88 | |
| 89 | template <typename Arg, typename... Args> |
| 90 | inline void Fuzz::next(Arg* first, Args... rest) { |
| 91 | this->next(first); |
| 92 | this->next(rest...); |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 93 | } |
| 94 | |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 95 | template <typename T, typename Min, typename Max> |
Mike Klein | f88f5ef | 2018-11-19 12:21:46 -0500 | [diff] [blame] | 96 | inline void Fuzz::nextRange(T* value, Min min, Max max) { |
| 97 | this->next(value); |
| 98 | if (*value < (T)min) { *value = (T)min; } |
| 99 | if (*value > (T)max) { *value = (T)max; } |
Kevin Lubick | bc9a1a8 | 2018-09-17 14:46:57 -0400 | [diff] [blame] | 100 | } |
| 101 | |
Kevin Lubick | 2f535ce | 2016-11-01 15:01:12 -0400 | [diff] [blame] | 102 | template <typename T> |
Kevin Lubick | 416b248 | 2016-11-10 16:17:49 -0500 | [diff] [blame] | 103 | inline void Fuzz::nextN(T* ptr, int n) { |
| 104 | for (int i = 0; i < n; i++) { |
| 105 | this->next(ptr+i); |
| 106 | } |
kjlubick | e565450 | 2016-07-19 16:50:03 -0700 | [diff] [blame] | 107 | } |
| 108 | |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 109 | struct Fuzzable { |
| 110 | const char* name; |
| 111 | void (*fn)(Fuzz*); |
| 112 | }; |
| 113 | |
Kevin Lubick | db1e5c6 | 2018-02-27 08:30:43 -0500 | [diff] [blame] | 114 | // 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] | 115 | #define DEF_FUZZ(name, f) \ |
Kevin Lubick | db1e5c6 | 2018-02-27 08:30:43 -0500 | [diff] [blame] | 116 | void fuzz_##name(Fuzz*); \ |
Mike Reed | ab273fa | 2017-01-11 13:58:55 -0500 | [diff] [blame] | 117 | sk_tools::Registry<Fuzzable> register_##name({#name, fuzz_##name}); \ |
Kevin Lubick | db1e5c6 | 2018-02-27 08:30:43 -0500 | [diff] [blame] | 118 | void fuzz_##name(Fuzz* f) |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 119 | |
mtklein | 65e5824 | 2016-01-13 12:57:57 -0800 | [diff] [blame] | 120 | #endif//Fuzz_DEFINED |