blob: e6399a8a7944548c25888149a03628612c739295 [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"
mtklein65e58242016-01-13 12:57:57 -080013#include "SkTypes.h"
mtklein65e58242016-01-13 12:57:57 -080014
Kevin Lubick2f535ce2016-11-01 15:01:12 -040015#include <cmath>
kjlubick840f12a2016-10-25 06:11:05 -070016
mtklein65e58242016-01-13 12:57:57 -080017class Fuzz : SkNoncopyable {
18public:
reed42943c82016-09-12 12:01:44 -070019 explicit Fuzz(sk_sp<SkData>);
mtklein65e58242016-01-13 12:57:57 -080020
kjlubicke5654502016-07-19 16:50:03 -070021 // Returns the total number of "random" bytes available.
22 size_t size();
Kevin Lubick2f535ce2016-11-01 15:01:12 -040023 // Returns if there are no bytes remaining for fuzzing.
24 bool exhausted();
kjlubicke5654502016-07-19 16:50:03 -070025
Kevin Lubick416b2482016-11-10 16:17:49 -050026 // next() loads fuzzed bytes into the variable passed in by pointer.
27 // We use this approach instead of T next() because different compilers
28 // evaluate function parameters in different orders. If fuzz->next()
29 // returned 5 and then 7, foo(fuzz->next(), fuzz->next()) would be
30 // foo(5, 7) when compiled on GCC and foo(7, 5) when compiled on Clang.
31 // By requiring params to be passed in, we avoid the temptation to call
32 // next() in a way that does not consume fuzzed bytes in a single
33 // uplatform-independent order.
kjlubicke5654502016-07-19 16:50:03 -070034 template <typename T>
Kevin Lubick416b2482016-11-10 16:17:49 -050035 void next(T* t);
36
37 // This is a convenient way to initialize more than one argument at a time.
38 template <typename Arg, typename... Args>
39 void next(Arg* first, Args... rest);
kjlubicke5654502016-07-19 16:50:03 -070040
Kevin Lubick2f535ce2016-11-01 15:01:12 -040041 // nextRange returns values only in [min, max].
Kevin Lubick416b2482016-11-10 16:17:49 -050042 template <typename T, typename Min, typename Max>
43 void nextRange(T*, Min, Max);
44
45 // nextN loads n * sizeof(T) bytes into ptr
Kevin Lubick2f535ce2016-11-01 15:01:12 -040046 template <typename T>
Kevin Lubick416b2482016-11-10 16:17:49 -050047 void nextN(T* ptr, int n);
kjlubick85d30172016-10-24 11:53:35 -070048
Kevin Lubick2f535ce2016-11-01 15:01:12 -040049 void signalBug(); // Tell afl-fuzz these inputs found a bug.
mtkleina1159422016-01-15 05:46:54 -080050
mtklein65e58242016-01-13 12:57:57 -080051private:
mtkleina1159422016-01-15 05:46:54 -080052 template <typename T>
53 T nextT();
54
bungemanffae30d2016-08-03 13:32:32 -070055 sk_sp<SkData> fBytes;
Kevin Lubick2f535ce2016-11-01 15:01:12 -040056 size_t fNextByte;
mtklein65e58242016-01-13 12:57:57 -080057};
58
Kevin Lubick2f535ce2016-11-01 15:01:12 -040059// UBSAN reminds us that bool can only legally hold 0 or 1.
60template <>
Kevin Lubick416b2482016-11-10 16:17:49 -050061inline void Fuzz::next(bool* b) {
62 uint8_t n;
63 this->next(&n);
64 *b = (n & 1) == 1;
Kevin Lubick2f535ce2016-11-01 15:01:12 -040065}
kjlubicke5654502016-07-19 16:50:03 -070066
Kevin Lubick2f535ce2016-11-01 15:01:12 -040067template <typename T>
Kevin Lubick416b2482016-11-10 16:17:49 -050068inline void Fuzz::next(T* n) {
Kevin Lubick2f535ce2016-11-01 15:01:12 -040069 if ((fNextByte + sizeof(T)) > fBytes->size()) {
Kevin Lubick416b2482016-11-10 16:17:49 -050070 *n = 0;
71 memcpy(n, fBytes->bytes() + fNextByte, fBytes->size() - fNextByte);
Kevin Lubick2f535ce2016-11-01 15:01:12 -040072 fNextByte = fBytes->size();
Kevin Lubick416b2482016-11-10 16:17:49 -050073 return;
Kevin Lubick2f535ce2016-11-01 15:01:12 -040074 }
Kevin Lubick416b2482016-11-10 16:17:49 -050075 memcpy(n, fBytes->bytes() + fNextByte, sizeof(T));
kjlubicke5654502016-07-19 16:50:03 -070076 fNextByte += sizeof(T);
Kevin Lubick416b2482016-11-10 16:17:49 -050077}
78
79template <typename Arg, typename... Args>
80inline void Fuzz::next(Arg* first, Args... rest) {
81 this->next(first);
82 this->next(rest...);
Kevin Lubick2f535ce2016-11-01 15:01:12 -040083}
84
85template <>
Kevin Lubick416b2482016-11-10 16:17:49 -050086inline void Fuzz::nextRange(float* f, float min, float max) {
87 this->next(f);
88 if (!std::isnormal(*f) && *f != 0.0f) {
Kevin Lubick2f535ce2016-11-01 15:01:12 -040089 // Don't deal with infinity or other strange floats.
Kevin Lubick416b2482016-11-10 16:17:49 -050090 *f = max;
Kevin Lubick2f535ce2016-11-01 15:01:12 -040091 }
Kevin Lubick416b2482016-11-10 16:17:49 -050092 *f = min + std::fmod(std::abs(*f), (max - min + 1));
93}
94
95template <typename T, typename Min, typename Max>
96inline void Fuzz::nextRange(T* n, Min min, Max max) {
97 this->next<T>(n);
Kevin Lubickd1042662016-11-29 11:25:52 -050098 if (min == max) {
99 *n = min;
100 return;
101 }
102 if (min > max) {
Kevin Lubickc9f0cc82016-11-15 16:07:02 -0500103 // Avoid misuse of nextRange
104 this->signalBug();
Kevin Lubick416b2482016-11-10 16:17:49 -0500105 }
Kevin Lubickc9f0cc82016-11-15 16:07:02 -0500106 if (*n < 0) { // Handle negatives
107 if (*n != std::numeric_limits<T>::lowest()) {
108 *n *= -1;
109 }
110 else {
111 *n = std::numeric_limits<T>::max();
112 }
113 }
114 *n = min + (*n % ((size_t)max - min + 1));
Kevin Lubick2f535ce2016-11-01 15:01:12 -0400115}
116
117template <typename T>
Kevin Lubick416b2482016-11-10 16:17:49 -0500118inline void Fuzz::nextN(T* ptr, int n) {
119 for (int i = 0; i < n; i++) {
120 this->next(ptr+i);
121 }
kjlubicke5654502016-07-19 16:50:03 -0700122}
123
mtklein65e58242016-01-13 12:57:57 -0800124struct Fuzzable {
125 const char* name;
126 void (*fn)(Fuzz*);
127};
128
Mike Reedab273fa2017-01-11 13:58:55 -0500129#define DEF_FUZZ(name, f) \
130 static void fuzz_##name(Fuzz*); \
131 sk_tools::Registry<Fuzzable> register_##name({#name, fuzz_##name}); \
mtklein65e58242016-01-13 12:57:57 -0800132 static void fuzz_##name(Fuzz* f)
133
mtklein65e58242016-01-13 12:57:57 -0800134#endif//Fuzz_DEFINED