blob: 26a8d429b36e7621f321c63cdd6618228fb51d10 [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:
17 explicit Fuzz(SkData*);
18
kjlubick5bd98a22016-02-18 06:27:38 -080019 bool nextBool();
mtklein24a22c72016-01-14 04:59:42 -080020 uint8_t nextB();
mtklein65e58242016-01-13 12:57:57 -080021 uint32_t nextU();
kjlubick5bd98a22016-02-18 06:27:38 -080022 // This can be nan, +- infinity, 0, anything.
mtklein65e58242016-01-13 12:57:57 -080023 float nextF();
24
kjlubick5bd98a22016-02-18 06:27:38 -080025 // Return the next fuzzed value [min, max) as an unsigned 32bit integer.
26 uint32_t nextRangeU(uint32_t min, uint32_t max);
27 /**
28 * Returns next fuzzed value [min...max) as a float.
29 * Will not be Infinity or NaN.
30 */
31 float nextRangeF(float min, float max);
32
mtkleina1159422016-01-15 05:46:54 -080033 void signalBug (); // Tell afl-fuzz these inputs found a bug.
34 void signalBoring(); // Tell afl-fuzz these inputs are not worth testing.
35
mtklein65e58242016-01-13 12:57:57 -080036private:
mtkleina1159422016-01-15 05:46:54 -080037 template <typename T>
38 T nextT();
39
mtklein65e58242016-01-13 12:57:57 -080040 SkAutoTUnref<SkData> fBytes;
mtklein24a22c72016-01-14 04:59:42 -080041 int fNextByte;
mtklein65e58242016-01-13 12:57:57 -080042};
43
44struct Fuzzable {
45 const char* name;
46 void (*fn)(Fuzz*);
47};
48
49#define DEF_FUZZ(name, f) \
50 static void fuzz_##name(Fuzz*); \
51 SkTRegistry<Fuzzable> register_##name({#name, fuzz_##name}); \
52 static void fuzz_##name(Fuzz* f)
53
mtklein65e58242016-01-13 12:57:57 -080054#endif//Fuzz_DEFINED