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 "include/core/SkData.h"
12 #include "include/core/SkImageFilter.h"
13 #include "include/core/SkRegion.h"
14 #include "include/core/SkTypes.h"
15 #include "include/private/SkMalloc.h"
16 #include "include/private/SkTFitsIn.h"
17 #include "tools/Registry.h"
18
19 #include <limits>
20 #include <cmath>
21 #include <signal.h>
22 #include <limits>
23
24 class Fuzz : SkNoncopyable {
25 public:
Fuzz(sk_sp<SkData> bytes)26 explicit Fuzz(sk_sp<SkData> bytes) : fBytes(bytes), fNextByte(0) {}
27
28 // Returns the total number of "random" bytes available.
size()29 size_t size() { return fBytes->size(); }
30 // Returns if there are no bytes remaining for fuzzing.
exhausted()31 bool exhausted() {
32 return fBytes->size() == fNextByte;
33 }
34
remaining()35 size_t remaining() {
36 return fBytes->size() - fNextByte;
37 }
38
deplete()39 void deplete() {
40 fNextByte = fBytes->size();
41 }
42
43 // next() loads fuzzed bytes into the variable passed in by pointer.
44 // We use this approach instead of T next() because different compilers
45 // evaluate function parameters in different orders. If fuzz->next()
46 // returned 5 and then 7, foo(fuzz->next(), fuzz->next()) would be
47 // foo(5, 7) when compiled on GCC and foo(7, 5) when compiled on Clang.
48 // By requiring params to be passed in, we avoid the temptation to call
49 // next() in a way that does not consume fuzzed bytes in a single
50 // platform-independent order.
51 template <typename T>
next(T * t)52 void next(T* t) { this->nextBytes(t, sizeof(T)); }
53
54 // This is a convenient way to initialize more than one argument at a time.
55 template <typename Arg, typename... Args>
56 void next(Arg* first, Args... rest);
57
58 // nextRange returns values only in [min, max].
59 template <typename T, typename Min, typename Max>
60 void nextRange(T*, Min, Max);
61
62 // nextEnum is a wrapper around nextRange for enums.
63 template <typename T>
64 void nextEnum(T* ptr, T max);
65
66 // nextN loads n * sizeof(T) bytes into ptr
67 template <typename T>
68 void nextN(T* ptr, int n);
69
signalBug()70 void signalBug(){
71 // Tell the fuzzer that these inputs found a bug.
72 SkDebugf("Signal bug\n");
73 raise(SIGSEGV);
74 }
75
76 // Specialized versions for when true random doesn't quite make sense
77 void next(bool* b);
78 void next(SkRegion* region);
79
nextBool()80 bool nextBool() {
81 bool b;
82 this->next(&b);
83 return b;
84 }
85
86 void nextRange(float* f, float min, float max);
87
88 private:
89 template <typename T>
90 T nextT();
91
92 sk_sp<SkData> fBytes;
93 size_t fNextByte;
94 friend void fuzz__MakeEncoderCorpus(Fuzz*);
95
96 void nextBytes(void* ptr, size_t size);
97 };
98
99 template <typename Arg, typename... Args>
next(Arg * first,Args...rest)100 inline void Fuzz::next(Arg* first, Args... rest) {
101 this->next(first);
102 this->next(rest...);
103 }
104
105 template <typename T, typename Min, typename Max>
nextRange(T * value,Min min,Max max)106 inline void Fuzz::nextRange(T* value, Min min, Max max) {
107 // UBSAN worries if we make an enum with out of range values, even temporarily.
108 using Raw = typename sk_strip_enum<T>::type;
109 Raw raw;
110 this->next(&raw);
111
112 if (raw < (Raw)min) { raw = (Raw)min; }
113 if (raw > (Raw)max) { raw = (Raw)max; }
114 *value = (T)raw;
115 }
116
117 template <typename T>
nextEnum(T * value,T max)118 inline void Fuzz::nextEnum(T* value, T max) {
119 // This works around the fact that UBSAN will assert if we put an invalid
120 // value into an enum. We might see issues with enums being represented
121 // on Windows differently than Linux, but that's not a thing we can fix here.
122 using U = typename std::underlying_type<T>::type;
123 U v;
124 this->next(&v);
125 if (v < (U)0) { *value = (T)0; return;}
126 if (v > (U)max) { *value = (T)max; return;}
127 *value = (T)v;
128 }
129
130 template <typename T>
nextN(T * ptr,int n)131 inline void Fuzz::nextN(T* ptr, int n) {
132 for (int i = 0; i < n; i++) {
133 this->next(ptr+i);
134 }
135 }
136
137 struct Fuzzable {
138 const char* name;
139 void (*fn)(Fuzz*);
140 };
141
142 // Not static so that we can link these into oss-fuzz harnesses if we like.
143 #define DEF_FUZZ(name, f) \
144 void fuzz_##name(Fuzz*); \
145 sk_tools::Registry<Fuzzable> register_##name({#name, fuzz_##name}); \
146 void fuzz_##name(Fuzz* f)
147
148 #endif//Fuzz_DEFINED
149