blob: 0ab3c1bc6e313c9cc3bd91c0d2c48f4af3078252 [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"
12#include "SkTRegistry.h"
13#include "SkTypes.h"
mtklein65e58242016-01-13 12:57:57 -080014
15class Fuzz : SkNoncopyable {
16public:
reed42943c82016-09-12 12:01:44 -070017 explicit Fuzz(sk_sp<SkData>);
mtklein65e58242016-01-13 12:57:57 -080018
kjlubicke5654502016-07-19 16:50:03 -070019 // Returns the total number of "random" bytes available.
20 size_t size();
21 // Returns the total number of "random" bytes remaining for randomness.
22 size_t remaining();
23
24 template <typename T>
25 bool next(T* n);
26
kjlubick5bd98a22016-02-18 06:27:38 -080027 bool nextBool();
mtklein24a22c72016-01-14 04:59:42 -080028 uint8_t nextB();
mtklein65e58242016-01-13 12:57:57 -080029 uint32_t nextU();
kjlubick5bd98a22016-02-18 06:27:38 -080030 // This can be nan, +- infinity, 0, anything.
mtklein65e58242016-01-13 12:57:57 -080031 float nextF();
kjlubick43195932016-04-05 12:48:47 -070032 // Returns a float between [0..1) as a IEEE float
33 float nextF1();
mtklein65e58242016-01-13 12:57:57 -080034
kjlubick5bd98a22016-02-18 06:27:38 -080035 // Return the next fuzzed value [min, max) as an unsigned 32bit integer.
36 uint32_t nextRangeU(uint32_t min, uint32_t max);
37 /**
38 * Returns next fuzzed value [min...max) as a float.
39 * Will not be Infinity or NaN.
40 */
41 float nextRangeF(float min, float max);
42
mtkleina1159422016-01-15 05:46:54 -080043 void signalBug (); // Tell afl-fuzz these inputs found a bug.
44 void signalBoring(); // Tell afl-fuzz these inputs are not worth testing.
45
mtklein65e58242016-01-13 12:57:57 -080046private:
mtkleina1159422016-01-15 05:46:54 -080047 template <typename T>
48 T nextT();
49
bungemanffae30d2016-08-03 13:32:32 -070050 sk_sp<SkData> fBytes;
mtklein24a22c72016-01-14 04:59:42 -080051 int fNextByte;
mtklein65e58242016-01-13 12:57:57 -080052};
53
kjlubicke5654502016-07-19 16:50:03 -070054template <typename T>
55bool Fuzz::next(T* n) {
56 if (fNextByte + sizeof(T) > fBytes->size()) {
57 return false;
58 }
59
60 memcpy(n, fBytes->bytes() + fNextByte, sizeof(T));
61 fNextByte += sizeof(T);
62 return true;
63}
64
mtklein65e58242016-01-13 12:57:57 -080065struct Fuzzable {
66 const char* name;
67 void (*fn)(Fuzz*);
68};
69
70#define DEF_FUZZ(name, f) \
71 static void fuzz_##name(Fuzz*); \
72 SkTRegistry<Fuzzable> register_##name({#name, fuzz_##name}); \
73 static void fuzz_##name(Fuzz* f)
74
mtklein65e58242016-01-13 12:57:57 -080075#endif//Fuzz_DEFINED