blob: 71e14a41a16c7957a7fa67bb8e66c5aceb239a54 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#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 "tools/Registry.h"
mtklein65e58242016-01-13 12:57:57 -080017
Hal Canaryc640d0d2018-06-13 09:59:02 -040018#include <limits>
Kevin Lubick2f535ce2016-11-01 15:01:12 -040019#include <cmath>
Kevin Lubick2541edf2018-01-11 10:27:14 -050020#include <signal.h>
Mike Kleinbf45c702018-06-11 11:56:57 -040021#include <limits>
kjlubick840f12a2016-10-25 06:11:05 -070022
mtklein65e58242016-01-13 12:57:57 -080023class Fuzz : SkNoncopyable {
24public:
Kevin Lubick2541edf2018-01-11 10:27:14 -050025 explicit Fuzz(sk_sp<SkData> bytes) : fBytes(bytes), fNextByte(0) {}
mtklein65e58242016-01-13 12:57:57 -080026
kjlubicke5654502016-07-19 16:50:03 -070027 // Returns the total number of "random" bytes available.
Kevin Lubick2541edf2018-01-11 10:27:14 -050028 size_t size() { return fBytes->size(); }
Kevin Lubick2f535ce2016-11-01 15:01:12 -040029 // Returns if there are no bytes remaining for fuzzing.
Kevin Lubickf84ded22018-10-23 09:28:48 -040030 bool exhausted() {
Kevin Lubick2541edf2018-01-11 10:27:14 -050031 return fBytes->size() == fNextByte;
32 }
kjlubicke5654502016-07-19 16:50:03 -070033
Kevin Lubickf84ded22018-10-23 09:28:48 -040034 size_t remaining() {
35 return fBytes->size() - fNextByte;
36 }
37
38 void deplete() {
39 fNextByte = fBytes->size();
40 }
41
Kevin Lubick416b2482016-11-10 16:17:49 -050042 // 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 Lia63d6902018-02-28 11:46:00 -050049 // platform-independent order.
kjlubicke5654502016-07-19 16:50:03 -070050 template <typename T>
Hal Canary6d9a51a2018-11-19 13:15:21 -050051 void next(T* t) { this->nextBytes(t, sizeof(T)); }
Kevin Lubick416b2482016-11-10 16:17:49 -050052
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);
kjlubicke5654502016-07-19 16:50:03 -070056
Kevin Lubick2f535ce2016-11-01 15:01:12 -040057 // nextRange returns values only in [min, max].
Kevin Lubick416b2482016-11-10 16:17:49 -050058 template <typename T, typename Min, typename Max>
59 void nextRange(T*, Min, Max);
60
Kevin Lubick00587e32019-06-03 11:27:16 -040061 // nextEnum is a wrapper around nextRange for enums.
62 template <typename T>
63 void nextEnum(T* ptr, T max);
64
Kevin Lubick416b2482016-11-10 16:17:49 -050065 // nextN loads n * sizeof(T) bytes into ptr
Kevin Lubick2f535ce2016-11-01 15:01:12 -040066 template <typename T>
Kevin Lubick416b2482016-11-10 16:17:49 -050067 void nextN(T* ptr, int n);
kjlubick85d30172016-10-24 11:53:35 -070068
Kevin Lubick2541edf2018-01-11 10:27:14 -050069 void signalBug(){
70 // Tell the fuzzer that these inputs found a bug.
71 SkDebugf("Signal bug\n");
72 raise(SIGSEGV);
73 }
mtkleina1159422016-01-15 05:46:54 -080074
Kevin Lubickbc9a1a82018-09-17 14:46:57 -040075 // Specialized versions for when true random doesn't quite make sense
76 void next(bool* b);
77 void next(SkImageFilter::CropRect* cropRect);
78 void next(SkRegion* region);
79
80 void nextRange(float* f, float min, float max);
81
mtklein65e58242016-01-13 12:57:57 -080082private:
mtkleina1159422016-01-15 05:46:54 -080083 template <typename T>
84 T nextT();
85
bungemanffae30d2016-08-03 13:32:32 -070086 sk_sp<SkData> fBytes;
Kevin Lubick2f535ce2016-11-01 15:01:12 -040087 size_t fNextByte;
Kevin Lubicke4be55d2018-03-30 15:05:13 -040088 friend void fuzz__MakeEncoderCorpus(Fuzz*);
mtklein65e58242016-01-13 12:57:57 -080089
Hal Canary6d9a51a2018-11-19 13:15:21 -050090 void nextBytes(void* ptr, size_t size);
91};
Kevin Lubick416b2482016-11-10 16:17:49 -050092
93template <typename Arg, typename... Args>
94inline void Fuzz::next(Arg* first, Args... rest) {
95 this->next(first);
96 this->next(rest...);
Kevin Lubick2f535ce2016-11-01 15:01:12 -040097}
98
Kevin Lubick416b2482016-11-10 16:17:49 -050099template <typename T, typename Min, typename Max>
Mike Kleinf88f5ef2018-11-19 12:21:46 -0500100inline void Fuzz::nextRange(T* value, Min min, Max max) {
101 this->next(value);
102 if (*value < (T)min) { *value = (T)min; }
103 if (*value > (T)max) { *value = (T)max; }
Kevin Lubickbc9a1a82018-09-17 14:46:57 -0400104}
105
Kevin Lubick2f535ce2016-11-01 15:01:12 -0400106template <typename T>
Kevin Lubick00587e32019-06-03 11:27:16 -0400107inline void Fuzz::nextEnum(T* value, T max) {
108 // This works around the fact that UBSAN will assert if we put an invalid
109 // value into an enum. We might see issues with enums being represented
110 // on Windows differently than Linux, but that's not a thing we can fix here.
111 using U = typename std::underlying_type<T>::type;
112 U v;
113 this->next(&v);
114 if (v < (U)0) { *value = (T)0; return;}
115 if (v > (U)max) { *value = (T)max; return;}
116 *value = (T)v;
117}
118
119template <typename T>
Kevin Lubick416b2482016-11-10 16:17:49 -0500120inline void Fuzz::nextN(T* ptr, int n) {
121 for (int i = 0; i < n; i++) {
122 this->next(ptr+i);
123 }
kjlubicke5654502016-07-19 16:50:03 -0700124}
125
mtklein65e58242016-01-13 12:57:57 -0800126struct Fuzzable {
127 const char* name;
128 void (*fn)(Fuzz*);
129};
130
Kevin Lubickdb1e5c62018-02-27 08:30:43 -0500131// Not static so that we can link these into oss-fuzz harnesses if we like.
Mike Reedab273fa2017-01-11 13:58:55 -0500132#define DEF_FUZZ(name, f) \
Kevin Lubickdb1e5c62018-02-27 08:30:43 -0500133 void fuzz_##name(Fuzz*); \
Mike Reedab273fa2017-01-11 13:58:55 -0500134 sk_tools::Registry<Fuzzable> register_##name({#name, fuzz_##name}); \
Kevin Lubickdb1e5c62018-02-27 08:30:43 -0500135 void fuzz_##name(Fuzz* f)
mtklein65e58242016-01-13 12:57:57 -0800136
mtklein65e58242016-01-13 12:57:57 -0800137#endif//Fuzz_DEFINED