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