/src/uWebSockets/fuzzing/helpers.h
Line | Count | Source |
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 | 2.89k | static inline const uint8_t *makePadded(const uint8_t *data, size_t size) { |
12 | 2.89k | static int paddedLength = 512 * 1024; |
13 | 2.89k | static char *padded = new char[128 + paddedLength + 128]; |
14 | | |
15 | | /* Increase landing area if required */ |
16 | 2.89k | if (paddedLength < size) { |
17 | 37 | delete [] padded; |
18 | 37 | paddedLength = size; |
19 | 37 | padded = new char [128 + paddedLength + 128]; |
20 | 37 | } |
21 | | |
22 | 2.89k | memcpy(padded + 128, data, size); |
23 | | |
24 | 2.89k | return (uint8_t *) padded + 128; |
25 | 2.89k | } |
26 | | |
27 | | /* Splits the fuzz data in one or many chunks */ |
28 | 2.89k | 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 | 134k | for (int i = 0; i < size; ) { |
31 | 131k | unsigned int chunkSize = data[i++]; |
32 | 131k | if (!chunkSize) { |
33 | 1.52k | chunkSize = size - i; |
34 | 130k | } else { |
35 | 130k | chunkSize = std::min<int>(chunkSize, size - i); |
36 | 130k | } |
37 | | |
38 | 131k | cb(data + i, chunkSize); |
39 | 131k | i += chunkSize; |
40 | 131k | } |
41 | 2.89k | } |
42 | | |
43 | | /* Reads all bytes to trigger invalid reads */ |
44 | 60.0k | static inline void readBytes(std::string_view s) { |
45 | 60.0k | volatile int sum = 0; |
46 | 31.3M | for (int i = 0; i < s.size(); i++) { |
47 | 31.2M | sum += s[i]; |
48 | 31.2M | } |
49 | 60.0k | } |
50 | | |
51 | | #endif |