/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 | 13.7k | static inline const uint8_t *makePadded(const uint8_t *data, size_t size) { |
12 | 13.7k | static int paddedLength = 512 * 1024; |
13 | 13.7k | static char *padded = new char[128 + paddedLength + 128]; |
14 | | |
15 | | /* Increase landing area if required */ |
16 | 13.7k | if (paddedLength < size) { |
17 | 143 | delete [] padded; |
18 | 143 | paddedLength = size; |
19 | 143 | padded = new char [128 + paddedLength + 128]; |
20 | 143 | } |
21 | | |
22 | 13.7k | memcpy(padded + 128, data, size); |
23 | | |
24 | 13.7k | return (uint8_t *) padded + 128; |
25 | 13.7k | } WebSocket.cpp:makePadded(unsigned char const*, unsigned long) Line | Count | Source | 11 | 1.44k | static inline const uint8_t *makePadded(const uint8_t *data, size_t size) { | 12 | 1.44k | static int paddedLength = 512 * 1024; | 13 | 1.44k | static char *padded = new char[128 + paddedLength + 128]; | 14 | | | 15 | | /* Increase landing area if required */ | 16 | 1.44k | if (paddedLength < size) { | 17 | 26 | delete [] padded; | 18 | 26 | paddedLength = size; | 19 | 26 | padded = new char [128 + paddedLength + 128]; | 20 | 26 | } | 21 | | | 22 | 1.44k | memcpy(padded + 128, data, size); | 23 | | | 24 | 1.44k | return (uint8_t *) padded + 128; | 25 | 1.44k | } |
TopicTree.cpp:makePadded(unsigned char const*, unsigned long) Line | Count | Source | 11 | 3.11k | static inline const uint8_t *makePadded(const uint8_t *data, size_t size) { | 12 | 3.11k | static int paddedLength = 512 * 1024; | 13 | 3.11k | static char *padded = new char[128 + paddedLength + 128]; | 14 | | | 15 | | /* Increase landing area if required */ | 16 | 3.11k | if (paddedLength < size) { | 17 | 35 | delete [] padded; | 18 | 35 | paddedLength = size; | 19 | 35 | padded = new char [128 + paddedLength + 128]; | 20 | 35 | } | 21 | | | 22 | 3.11k | memcpy(padded + 128, data, size); | 23 | | | 24 | 3.11k | return (uint8_t *) padded + 128; | 25 | 3.11k | } |
PerMessageDeflate.cpp:makePadded(unsigned char const*, unsigned long) Line | Count | Source | 11 | 302 | static inline const uint8_t *makePadded(const uint8_t *data, size_t size) { | 12 | 302 | static int paddedLength = 512 * 1024; | 13 | 302 | static char *padded = new char[128 + paddedLength + 128]; | 14 | | | 15 | | /* Increase landing area if required */ | 16 | 302 | if (paddedLength < size) { | 17 | 20 | delete [] padded; | 18 | 20 | paddedLength = size; | 19 | 20 | padded = new char [128 + paddedLength + 128]; | 20 | 20 | } | 21 | | | 22 | 302 | memcpy(padded + 128, data, size); | 23 | | | 24 | 302 | return (uint8_t *) padded + 128; | 25 | 302 | } |
Http.cpp:makePadded(unsigned char const*, unsigned long) Line | Count | Source | 11 | 8.92k | static inline const uint8_t *makePadded(const uint8_t *data, size_t size) { | 12 | 8.92k | static int paddedLength = 512 * 1024; | 13 | 8.92k | static char *padded = new char[128 + paddedLength + 128]; | 14 | | | 15 | | /* Increase landing area if required */ | 16 | 8.92k | if (paddedLength < size) { | 17 | 62 | delete [] padded; | 18 | 62 | paddedLength = size; | 19 | 62 | padded = new char [128 + paddedLength + 128]; | 20 | 62 | } | 21 | | | 22 | 8.92k | memcpy(padded + 128, data, size); | 23 | | | 24 | 8.92k | return (uint8_t *) padded + 128; | 25 | 8.92k | } |
|
26 | | |
27 | | /* Splits the fuzz data in one or many chunks */ |
28 | 13.7k | 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 | 6.93M | for (int i = 0; i < size; ) { |
31 | 6.92M | unsigned int chunkSize = data[i++]; |
32 | 6.92M | if (!chunkSize) { |
33 | 4.04k | chunkSize = size - i; |
34 | 6.91M | } else { |
35 | 6.91M | chunkSize = std::min<int>(chunkSize, size - i); |
36 | 6.91M | } |
37 | | |
38 | 6.92M | cb(data + i, chunkSize); |
39 | 6.92M | i += chunkSize; |
40 | 6.92M | } |
41 | 13.7k | } WebSocket.cpp:makeChunked(unsigned char const*, unsigned long, std::__1::function<void (unsigned char const*, unsigned long)>) Line | Count | Source | 28 | 1.44k | 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 | 234k | for (int i = 0; i < size; ) { | 31 | 233k | unsigned int chunkSize = data[i++]; | 32 | 233k | if (!chunkSize) { | 33 | 467 | chunkSize = size - i; | 34 | 232k | } else { | 35 | 232k | chunkSize = std::min<int>(chunkSize, size - i); | 36 | 232k | } | 37 | | | 38 | 233k | cb(data + i, chunkSize); | 39 | 233k | i += chunkSize; | 40 | 233k | } | 41 | 1.44k | } |
TopicTree.cpp:makeChunked(unsigned char const*, unsigned long, std::__1::function<void (unsigned char const*, unsigned long)>) Line | Count | Source | 28 | 3.11k | 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 | 6.41M | for (int i = 0; i < size; ) { | 31 | 6.41M | unsigned int chunkSize = data[i++]; | 32 | 6.41M | if (!chunkSize) { | 33 | 300 | chunkSize = size - i; | 34 | 6.41M | } else { | 35 | 6.41M | chunkSize = std::min<int>(chunkSize, size - i); | 36 | 6.41M | } | 37 | | | 38 | 6.41M | cb(data + i, chunkSize); | 39 | 6.41M | i += chunkSize; | 40 | 6.41M | } | 41 | 3.11k | } |
PerMessageDeflate.cpp:makeChunked(unsigned char const*, unsigned long, std::__1::function<void (unsigned char const*, unsigned long)>) Line | Count | Source | 28 | 302 | 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 | 128k | for (int i = 0; i < size; ) { | 31 | 128k | unsigned int chunkSize = data[i++]; | 32 | 128k | if (!chunkSize) { | 33 | 176 | chunkSize = size - i; | 34 | 128k | } else { | 35 | 128k | chunkSize = std::min<int>(chunkSize, size - i); | 36 | 128k | } | 37 | | | 38 | 128k | cb(data + i, chunkSize); | 39 | 128k | i += chunkSize; | 40 | 128k | } | 41 | 302 | } |
Http.cpp:makeChunked(unsigned char const*, unsigned long, std::__1::function<void (unsigned char const*, unsigned long)>) Line | Count | Source | 28 | 8.92k | 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 | 157k | for (int i = 0; i < size; ) { | 31 | 148k | unsigned int chunkSize = data[i++]; | 32 | 148k | if (!chunkSize) { | 33 | 3.10k | chunkSize = size - i; | 34 | 145k | } else { | 35 | 145k | chunkSize = std::min<int>(chunkSize, size - i); | 36 | 145k | } | 37 | | | 38 | 148k | cb(data + i, chunkSize); | 39 | 148k | i += chunkSize; | 40 | 148k | } | 41 | 8.92k | } |
|
42 | | |
43 | | /* Reads all bytes to trigger invalid reads */ |
44 | 186k | static inline void readBytes(std::string_view s) { |
45 | 186k | volatile int sum = 0; |
46 | 622k | for (int i = 0; i < s.size(); i++) { |
47 | 436k | sum += s[i]; |
48 | 436k | } |
49 | 186k | } Unexecuted instantiation: WebSocket.cpp:readBytes(std::__1::basic_string_view<char, std::__1::char_traits<char> >) Unexecuted instantiation: TopicTree.cpp:readBytes(std::__1::basic_string_view<char, std::__1::char_traits<char> >) Unexecuted instantiation: PerMessageDeflate.cpp:readBytes(std::__1::basic_string_view<char, std::__1::char_traits<char> >) Http.cpp:readBytes(std::__1::basic_string_view<char, std::__1::char_traits<char> >) Line | Count | Source | 44 | 186k | static inline void readBytes(std::string_view s) { | 45 | 186k | volatile int sum = 0; | 46 | 622k | for (int i = 0; i < s.size(); i++) { | 47 | 436k | sum += s[i]; | 48 | 436k | } | 49 | 186k | } |
|
50 | | |
51 | | #endif |