Coverage Report

Created: 2023-06-06 06:17

/src/uWebSockets/fuzzing/helpers.h
Line
Count
Source (jump to first uncovered line)
1
#ifndef HELPERS_H
2
#define HELPERS_H
3
4
/* Common helpers for fuzzing */
5
6
#include <functional>
7
#include <string_view>
8
#include <cstring>
9
10
/* We use this to pad the fuzz */
11
1.49k
static inline const uint8_t *makePadded(const uint8_t *data, size_t size) {
12
1.49k
    static int paddedLength = 512 * 1024;
13
1.49k
    static char *padded = new char[128 + paddedLength + 128];
14
15
    /* Increase landing area if required */
16
1.49k
    if (paddedLength < size) {
17
12
        delete [] padded;
18
12
        paddedLength = size;
19
12
        padded = new char [128 + paddedLength + 128];
20
12
    }
21
22
1.49k
    memcpy(padded + 128, data, size);
23
24
1.49k
    return (uint8_t *) padded + 128;
25
1.49k
}
26
27
/* Splits the fuzz data in one or many chunks */
28
1.49k
static inline void makeChunked(const uint8_t *data, size_t size, std::function<void(const uint8_t *data, size_t size)> cb) {
29
    /* First byte determines chunk size; 0 is all that remains, 1-255 is small chunk */
30
61.7k
    for (int i = 0; i < size; ) {
31
60.2k
        unsigned int chunkSize = data[i++];
32
60.2k
        if (!chunkSize) {
33
490
            chunkSize = size - i;
34
59.7k
        } else {
35
59.7k
            chunkSize = std::min<int>(chunkSize, size - i);
36
59.7k
        }
37
38
60.2k
        cb(data + i, chunkSize);
39
60.2k
        i += chunkSize;
40
60.2k
    }
41
1.49k
}
42
43
/* Reads all bytes to trigger invalid reads */
44
0
static inline void readBytes(std::string_view s) {
45
0
    volatile int sum = 0;
46
0
    for (int i = 0; i < s.size(); i++) {
47
0
        sum += s[i];
48
0
    }
49
0
}
50
51
#endif