aboutsummaryrefslogtreecommitdiffhomepage
path: root/fuzz/Fuzz.h
blob: 0ab3c1bc6e313c9cc3bd91c0d2c48f4af3078252 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
 * Copyright 2016 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef Fuzz_DEFINED
#define Fuzz_DEFINED

#include "SkData.h"
#include "SkTRegistry.h"
#include "SkTypes.h"

class Fuzz : SkNoncopyable {
public:
    explicit Fuzz(sk_sp<SkData>);

    // Returns the total number of "random" bytes available.
    size_t size();
    // Returns the total number of "random" bytes remaining for randomness.
    size_t remaining();

    template <typename T>
    bool next(T* n);

    bool nextBool();
    uint8_t  nextB();
    uint32_t nextU();
    // This can be nan, +- infinity, 0, anything.
    float    nextF();
    // Returns a float between [0..1) as a IEEE float
    float    nextF1();

    // Return the next fuzzed value [min, max) as an unsigned 32bit integer.
    uint32_t nextRangeU(uint32_t min, uint32_t max);
    /**
     *  Returns next fuzzed value [min...max) as a float.
     *  Will not be Infinity or NaN.
     */
    float    nextRangeF(float    min, float    max);

    void signalBug   ();  // Tell afl-fuzz these inputs found a bug.
    void signalBoring();  // Tell afl-fuzz these inputs are not worth testing.

private:
    template <typename T>
    T nextT();

    sk_sp<SkData> fBytes;
    int fNextByte;
};

template <typename T>
bool Fuzz::next(T* n) {
    if (fNextByte + sizeof(T) > fBytes->size()) {
        return false;
    }

    memcpy(n, fBytes->bytes() + fNextByte, sizeof(T));
    fNextByte += sizeof(T);
    return true;
}

struct Fuzzable {
    const char* name;
    void (*fn)(Fuzz*);
};

#define DEF_FUZZ(name, f)                                        \
    static void fuzz_##name(Fuzz*);                              \
    SkTRegistry<Fuzzable> register_##name({#name, fuzz_##name}); \
    static void fuzz_##name(Fuzz* f)

#endif//Fuzz_DEFINED