blob: 4fb7396eccac7ffb7b84a4f2ad40604ae30aefd4 [file] [log] [blame]
mtklein65e58242016-01-13 12:57:57 -08001/*
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 Reedab273fa2017-01-11 13:58:55 -050012#include "../tools/Registry.h"
Herb Derbyb549cc32017-03-27 13:35:15 -040013#include "SkMalloc.h"
mtklein65e58242016-01-13 12:57:57 -080014#include "SkTypes.h"
mtklein65e58242016-01-13 12:57:57 -080015
Kevin Lubick2f535ce2016-11-01 15:01:12 -040016#include <cmath>
Kevin Lubick2541edf2018-01-11 10:27:14 -050017#include <signal.h>
kjlubick840f12a2016-10-25 06:11:05 -070018
mtklein65e58242016-01-13 12:57:57 -080019class Fuzz : SkNoncopyable {
20public:
Kevin Lubick2541edf2018-01-11 10:27:14 -050021 explicit Fuzz(sk_sp<SkData> bytes) : fBytes(bytes), fNextByte(0) {}
mtklein65e58242016-01-13 12:57:57 -080022
kjlubicke5654502016-07-19 16:50:03 -070023 // Returns the total number of "random" bytes available.
Kevin Lubick2541edf2018-01-11 10:27:14 -050024 size_t size() { return fBytes->size(); }
Kevin Lubick2f535ce2016-11-01 15:01:12 -040025 // Returns if there are no bytes remaining for fuzzing.
Kevin Lubick2541edf2018-01-11 10:27:14 -050026 bool exhausted(){
27 return fBytes->size() == fNextByte;
28 }
kjlubicke5654502016-07-19 16:50:03 -070029
Kevin Lubick416b2482016-11-10 16:17:49 -050030 // next() loads fuzzed bytes into the variable passed in by pointer.
31 // We use this approach instead of T next() because different compilers
32 // evaluate function parameters in different orders. If fuzz->next()
33 // returned 5 and then 7, foo(fuzz->next(), fuzz->next()) would be
34 // foo(5, 7) when compiled on GCC and foo(7, 5) when compiled on Clang.
35 // By requiring params to be passed in, we avoid the temptation to call
36 // next() in a way that does not consume fuzzed bytes in a single
Yuqian Lia63d6902018-02-28 11:46:00 -050037 // platform-independent order.
kjlubicke5654502016-07-19 16:50:03 -070038 template <typename T>
Kevin Lubick416b2482016-11-10 16:17:49 -050039 void next(T* t);
40
41 // This is a convenient way to initialize more than one argument at a time.
42 template <typename Arg, typename... Args>
43 void next(Arg* first, Args... rest);
kjlubicke5654502016-07-19 16:50:03 -070044
Kevin Lubick2f535ce2016-11-01 15:01:12 -040045 // nextRange returns values only in [min, max].
Kevin Lubick416b2482016-11-10 16:17:49 -050046 template <typename T, typename Min, typename Max>
47 void nextRange(T*, Min, Max);
48
49 // nextN loads n * sizeof(T) bytes into ptr
Kevin Lubick2f535ce2016-11-01 15:01:12 -040050 template <typename T>
Kevin Lubick416b2482016-11-10 16:17:49 -050051 void nextN(T* ptr, int n);
kjlubick85d30172016-10-24 11:53:35 -070052
Kevin Lubick2541edf2018-01-11 10:27:14 -050053 void signalBug(){
54 // Tell the fuzzer that these inputs found a bug.
55 SkDebugf("Signal bug\n");
56 raise(SIGSEGV);
57 }
mtkleina1159422016-01-15 05:46:54 -080058
mtklein65e58242016-01-13 12:57:57 -080059private:
mtkleina1159422016-01-15 05:46:54 -080060 template <typename T>
61 T nextT();
62
bungemanffae30d2016-08-03 13:32:32 -070063 sk_sp<SkData> fBytes;
Kevin Lubick2f535ce2016-11-01 15:01:12 -040064 size_t fNextByte;
Kevin Lubicke4be55d2018-03-30 15:05:13 -040065 friend void fuzz__MakeEncoderCorpus(Fuzz*);
mtklein65e58242016-01-13 12:57:57 -080066};
67
Kevin Lubick2f535ce2016-11-01 15:01:12 -040068// UBSAN reminds us that bool can only legally hold 0 or 1.
69template <>
Kevin Lubick416b2482016-11-10 16:17:49 -050070inline void Fuzz::next(bool* b) {
71 uint8_t n;
72 this->next(&n);
73 *b = (n & 1) == 1;
Kevin Lubick2f535ce2016-11-01 15:01:12 -040074}
kjlubicke5654502016-07-19 16:50:03 -070075
Kevin Lubick2f535ce2016-11-01 15:01:12 -040076template <typename T>
Kevin Lubick416b2482016-11-10 16:17:49 -050077inline void Fuzz::next(T* n) {
Kevin Lubick2f535ce2016-11-01 15:01:12 -040078 if ((fNextByte + sizeof(T)) > fBytes->size()) {
Hal Canary24ac42b2017-02-14 13:35:14 -050079 sk_bzero(n, sizeof(T));
Kevin Lubick416b2482016-11-10 16:17:49 -050080 memcpy(n, fBytes->bytes() + fNextByte, fBytes->size() - fNextByte);
Kevin Lubick2f535ce2016-11-01 15:01:12 -040081 fNextByte = fBytes->size();
Kevin Lubick416b2482016-11-10 16:17:49 -050082 return;
Kevin Lubick2f535ce2016-11-01 15:01:12 -040083 }
Kevin Lubick416b2482016-11-10 16:17:49 -050084 memcpy(n, fBytes->bytes() + fNextByte, sizeof(T));
kjlubicke5654502016-07-19 16:50:03 -070085 fNextByte += sizeof(T);
Kevin Lubick416b2482016-11-10 16:17:49 -050086}
87
88template <typename Arg, typename... Args>
89inline void Fuzz::next(Arg* first, Args... rest) {
90 this->next(first);
91 this->next(rest...);
Kevin Lubick2f535ce2016-11-01 15:01:12 -040092}
93
94template <>
Kevin Lubick416b2482016-11-10 16:17:49 -050095inline void Fuzz::nextRange(float* f, float min, float max) {
96 this->next(f);
97 if (!std::isnormal(*f) && *f != 0.0f) {
Kevin Lubick2f535ce2016-11-01 15:01:12 -040098 // Don't deal with infinity or other strange floats.
Kevin Lubick416b2482016-11-10 16:17:49 -050099 *f = max;
Kevin Lubick2f535ce2016-11-01 15:01:12 -0400100 }
Kevin Lubick416b2482016-11-10 16:17:49 -0500101 *f = min + std::fmod(std::abs(*f), (max - min + 1));
102}
103
104template <typename T, typename Min, typename Max>
105inline void Fuzz::nextRange(T* n, Min min, Max max) {
106 this->next<T>(n);
Kevin Lubickd1042662016-11-29 11:25:52 -0500107 if (min == max) {
108 *n = min;
109 return;
110 }
111 if (min > max) {
Kevin Lubickc9f0cc82016-11-15 16:07:02 -0500112 // Avoid misuse of nextRange
Kevin Lubick1991f552018-02-27 10:59:10 -0500113 SkDebugf("min > max (%d > %d) \n", min, max);
Kevin Lubickc9f0cc82016-11-15 16:07:02 -0500114 this->signalBug();
Kevin Lubick416b2482016-11-10 16:17:49 -0500115 }
Kevin Lubickc9f0cc82016-11-15 16:07:02 -0500116 if (*n < 0) { // Handle negatives
117 if (*n != std::numeric_limits<T>::lowest()) {
118 *n *= -1;
119 }
120 else {
121 *n = std::numeric_limits<T>::max();
122 }
123 }
124 *n = min + (*n % ((size_t)max - min + 1));
Kevin Lubick2f535ce2016-11-01 15:01:12 -0400125}
126
127template <typename T>
Kevin Lubick416b2482016-11-10 16:17:49 -0500128inline void Fuzz::nextN(T* ptr, int n) {
129 for (int i = 0; i < n; i++) {
130 this->next(ptr+i);
131 }
kjlubicke5654502016-07-19 16:50:03 -0700132}
133
mtklein65e58242016-01-13 12:57:57 -0800134struct Fuzzable {
135 const char* name;
136 void (*fn)(Fuzz*);
137};
138
Kevin Lubickdb1e5c62018-02-27 08:30:43 -0500139// Not static so that we can link these into oss-fuzz harnesses if we like.
Mike Reedab273fa2017-01-11 13:58:55 -0500140#define DEF_FUZZ(name, f) \
Kevin Lubickdb1e5c62018-02-27 08:30:43 -0500141 void fuzz_##name(Fuzz*); \
Mike Reedab273fa2017-01-11 13:58:55 -0500142 sk_tools::Registry<Fuzzable> register_##name({#name, fuzz_##name}); \
Kevin Lubickdb1e5c62018-02-27 08:30:43 -0500143 void fuzz_##name(Fuzz* f)
mtklein65e58242016-01-13 12:57:57 -0800144
mtklein65e58242016-01-13 12:57:57 -0800145#endif//Fuzz_DEFINED