blob: 1a2aa381d04d04f77d6966fc0bb4f03f0cc17476 [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>
kjlubick840f12a2016-10-25 06:11:05 -070017
mtklein65e58242016-01-13 12:57:57 -080018class Fuzz : SkNoncopyable {
19public:
reed42943c82016-09-12 12:01:44 -070020 explicit Fuzz(sk_sp<SkData>);
mtklein65e58242016-01-13 12:57:57 -080021
kjlubicke5654502016-07-19 16:50:03 -070022 // Returns the total number of "random" bytes available.
23 size_t size();
Kevin Lubick2f535ce2016-11-01 15:01:12 -040024 // Returns if there are no bytes remaining for fuzzing.
25 bool exhausted();
kjlubicke5654502016-07-19 16:50:03 -070026
Kevin Lubick416b2482016-11-10 16:17:49 -050027 // next() loads fuzzed bytes into the variable passed in by pointer.
28 // We use this approach instead of T next() because different compilers
29 // evaluate function parameters in different orders. If fuzz->next()
30 // returned 5 and then 7, foo(fuzz->next(), fuzz->next()) would be
31 // foo(5, 7) when compiled on GCC and foo(7, 5) when compiled on Clang.
32 // By requiring params to be passed in, we avoid the temptation to call
33 // next() in a way that does not consume fuzzed bytes in a single
34 // uplatform-independent order.
kjlubicke5654502016-07-19 16:50:03 -070035 template <typename T>
Kevin Lubick416b2482016-11-10 16:17:49 -050036 void next(T* t);
37
38 // This is a convenient way to initialize more than one argument at a time.
39 template <typename Arg, typename... Args>
40 void next(Arg* first, Args... rest);
kjlubicke5654502016-07-19 16:50:03 -070041
Kevin Lubick2f535ce2016-11-01 15:01:12 -040042 // nextRange returns values only in [min, max].
Kevin Lubick416b2482016-11-10 16:17:49 -050043 template <typename T, typename Min, typename Max>
44 void nextRange(T*, Min, Max);
45
46 // nextN loads n * sizeof(T) bytes into ptr
Kevin Lubick2f535ce2016-11-01 15:01:12 -040047 template <typename T>
Kevin Lubick416b2482016-11-10 16:17:49 -050048 void nextN(T* ptr, int n);
kjlubick85d30172016-10-24 11:53:35 -070049
Kevin Lubick2f535ce2016-11-01 15:01:12 -040050 void signalBug(); // Tell afl-fuzz these inputs found a bug.
mtkleina1159422016-01-15 05:46:54 -080051
mtklein65e58242016-01-13 12:57:57 -080052private:
mtkleina1159422016-01-15 05:46:54 -080053 template <typename T>
54 T nextT();
55
bungemanffae30d2016-08-03 13:32:32 -070056 sk_sp<SkData> fBytes;
Kevin Lubick2f535ce2016-11-01 15:01:12 -040057 size_t fNextByte;
mtklein65e58242016-01-13 12:57:57 -080058};
59
Kevin Lubick2f535ce2016-11-01 15:01:12 -040060// UBSAN reminds us that bool can only legally hold 0 or 1.
61template <>
Kevin Lubick416b2482016-11-10 16:17:49 -050062inline void Fuzz::next(bool* b) {
63 uint8_t n;
64 this->next(&n);
65 *b = (n & 1) == 1;
Kevin Lubick2f535ce2016-11-01 15:01:12 -040066}
kjlubicke5654502016-07-19 16:50:03 -070067
Kevin Lubick2f535ce2016-11-01 15:01:12 -040068template <typename T>
Kevin Lubick416b2482016-11-10 16:17:49 -050069inline void Fuzz::next(T* n) {
Kevin Lubick2f535ce2016-11-01 15:01:12 -040070 if ((fNextByte + sizeof(T)) > fBytes->size()) {
Hal Canary24ac42b2017-02-14 13:35:14 -050071 sk_bzero(n, sizeof(T));
Kevin Lubick416b2482016-11-10 16:17:49 -050072 memcpy(n, fBytes->bytes() + fNextByte, fBytes->size() - fNextByte);
Kevin Lubick2f535ce2016-11-01 15:01:12 -040073 fNextByte = fBytes->size();
Kevin Lubick416b2482016-11-10 16:17:49 -050074 return;
Kevin Lubick2f535ce2016-11-01 15:01:12 -040075 }
Kevin Lubick416b2482016-11-10 16:17:49 -050076 memcpy(n, fBytes->bytes() + fNextByte, sizeof(T));
kjlubicke5654502016-07-19 16:50:03 -070077 fNextByte += sizeof(T);
Kevin Lubick416b2482016-11-10 16:17:49 -050078}
79
80template <typename Arg, typename... Args>
81inline void Fuzz::next(Arg* first, Args... rest) {
82 this->next(first);
83 this->next(rest...);
Kevin Lubick2f535ce2016-11-01 15:01:12 -040084}
85
86template <>
Kevin Lubick416b2482016-11-10 16:17:49 -050087inline void Fuzz::nextRange(float* f, float min, float max) {
88 this->next(f);
89 if (!std::isnormal(*f) && *f != 0.0f) {
Kevin Lubick2f535ce2016-11-01 15:01:12 -040090 // Don't deal with infinity or other strange floats.
Kevin Lubick416b2482016-11-10 16:17:49 -050091 *f = max;
Kevin Lubick2f535ce2016-11-01 15:01:12 -040092 }
Kevin Lubick416b2482016-11-10 16:17:49 -050093 *f = min + std::fmod(std::abs(*f), (max - min + 1));
94}
95
96template <typename T, typename Min, typename Max>
97inline void Fuzz::nextRange(T* n, Min min, Max max) {
98 this->next<T>(n);
Kevin Lubickd1042662016-11-29 11:25:52 -050099 if (min == max) {
100 *n = min;
101 return;
102 }
103 if (min > max) {
Kevin Lubickc9f0cc82016-11-15 16:07:02 -0500104 // Avoid misuse of nextRange
105 this->signalBug();
Kevin Lubick416b2482016-11-10 16:17:49 -0500106 }
Kevin Lubickc9f0cc82016-11-15 16:07:02 -0500107 if (*n < 0) { // Handle negatives
108 if (*n != std::numeric_limits<T>::lowest()) {
109 *n *= -1;
110 }
111 else {
112 *n = std::numeric_limits<T>::max();
113 }
114 }
115 *n = min + (*n % ((size_t)max - min + 1));
Kevin Lubick2f535ce2016-11-01 15:01:12 -0400116}
117
118template <typename T>
Kevin Lubick416b2482016-11-10 16:17:49 -0500119inline void Fuzz::nextN(T* ptr, int n) {
120 for (int i = 0; i < n; i++) {
121 this->next(ptr+i);
122 }
kjlubicke5654502016-07-19 16:50:03 -0700123}
124
mtklein65e58242016-01-13 12:57:57 -0800125struct Fuzzable {
126 const char* name;
127 void (*fn)(Fuzz*);
128};
129
Mike Reedab273fa2017-01-11 13:58:55 -0500130#define DEF_FUZZ(name, f) \
131 static void fuzz_##name(Fuzz*); \
132 sk_tools::Registry<Fuzzable> register_##name({#name, fuzz_##name}); \
mtklein65e58242016-01-13 12:57:57 -0800133 static void fuzz_##name(Fuzz* f)
134
mtklein65e58242016-01-13 12:57:57 -0800135#endif//Fuzz_DEFINED