Coverage Report

Created: 2025-06-13 06:09

/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
3.24k
static inline const uint8_t *makePadded(const uint8_t *data, size_t size) {
12
3.24k
    static int paddedLength = 512 * 1024;
13
3.24k
    static char *padded = new char[128 + paddedLength + 128];
14
15
    /* Increase landing area if required */
16
3.24k
    if (paddedLength < size) {
17
18
        delete [] padded;
18
18
        paddedLength = size;
19
18
        padded = new char [128 + paddedLength + 128];
20
18
    }
21
22
3.24k
    memcpy(padded + 128, data, size);
23
24
3.24k
    return (uint8_t *) padded + 128;
25
3.24k
}
26
27
/* Splits the fuzz data in one or many chunks */
28
3.24k
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
4.28M
    for (int i = 0; i < size; ) {
31
4.28M
        unsigned int chunkSize = data[i++];
32
4.28M
        if (!chunkSize) {
33
283
            chunkSize = size - i;
34
4.28M
        } else {
35
4.28M
            chunkSize = std::min<int>(chunkSize, size - i);
36
4.28M
        }
37
38
4.28M
        cb(data + i, chunkSize);
39
4.28M
        i += chunkSize;
40
4.28M
    }
41
3.24k
}
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