/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 | 12.9k | static inline const uint8_t *makePadded(const uint8_t *data, size_t size) { |
12 | 12.9k | static int paddedLength = 512 * 1024; |
13 | 12.9k | static char *padded = new char[128 + paddedLength + 128]; |
14 | | |
15 | | /* Increase landing area if required */ |
16 | 12.9k | if (paddedLength < size) { |
17 | 139 | delete [] padded; |
18 | 139 | paddedLength = size; |
19 | 139 | padded = new char [128 + paddedLength + 128]; |
20 | 139 | } |
21 | | |
22 | 12.9k | memcpy(padded + 128, data, size); |
23 | | |
24 | 12.9k | return (uint8_t *) padded + 128; |
25 | 12.9k | } 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 | 27 | delete [] padded; | 18 | 27 | paddedLength = size; | 19 | 27 | padded = new char [128 + paddedLength + 128]; | 20 | 27 | } | 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.08k | static inline const uint8_t *makePadded(const uint8_t *data, size_t size) { | 12 | 3.08k | static int paddedLength = 512 * 1024; | 13 | 3.08k | static char *padded = new char[128 + paddedLength + 128]; | 14 | | | 15 | | /* Increase landing area if required */ | 16 | 3.08k | if (paddedLength < size) { | 17 | 34 | delete [] padded; | 18 | 34 | paddedLength = size; | 19 | 34 | padded = new char [128 + paddedLength + 128]; | 20 | 34 | } | 21 | | | 22 | 3.08k | memcpy(padded + 128, data, size); | 23 | | | 24 | 3.08k | return (uint8_t *) padded + 128; | 25 | 3.08k | } |
PerMessageDeflate.cpp:makePadded(unsigned char const*, unsigned long) Line | Count | Source | 11 | 312 | static inline const uint8_t *makePadded(const uint8_t *data, size_t size) { | 12 | 312 | static int paddedLength = 512 * 1024; | 13 | 312 | static char *padded = new char[128 + paddedLength + 128]; | 14 | | | 15 | | /* Increase landing area if required */ | 16 | 312 | if (paddedLength < size) { | 17 | 24 | delete [] padded; | 18 | 24 | paddedLength = size; | 19 | 24 | padded = new char [128 + paddedLength + 128]; | 20 | 24 | } | 21 | | | 22 | 312 | memcpy(padded + 128, data, size); | 23 | | | 24 | 312 | return (uint8_t *) padded + 128; | 25 | 312 | } |
Http.cpp:makePadded(unsigned char const*, unsigned long) Line | Count | Source | 11 | 8.07k | static inline const uint8_t *makePadded(const uint8_t *data, size_t size) { | 12 | 8.07k | static int paddedLength = 512 * 1024; | 13 | 8.07k | static char *padded = new char[128 + paddedLength + 128]; | 14 | | | 15 | | /* Increase landing area if required */ | 16 | 8.07k | if (paddedLength < size) { | 17 | 54 | delete [] padded; | 18 | 54 | paddedLength = size; | 19 | 54 | padded = new char [128 + paddedLength + 128]; | 20 | 54 | } | 21 | | | 22 | 8.07k | memcpy(padded + 128, data, size); | 23 | | | 24 | 8.07k | return (uint8_t *) padded + 128; | 25 | 8.07k | } |
|
26 | | |
27 | | /* Splits the fuzz data in one or many chunks */ |
28 | 12.9k | 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 | 5.97M | for (int i = 0; i < size; ) { |
31 | 5.96M | unsigned int chunkSize = data[i++]; |
32 | 5.96M | if (!chunkSize) { |
33 | 3.37k | chunkSize = size - i; |
34 | 5.95M | } else { |
35 | 5.95M | chunkSize = std::min<int>(chunkSize, size - i); |
36 | 5.95M | } |
37 | | |
38 | 5.96M | cb(data + i, chunkSize); |
39 | 5.96M | i += chunkSize; |
40 | 5.96M | } |
41 | 12.9k | } 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 | 219k | for (int i = 0; i < size; ) { | 31 | 218k | unsigned int chunkSize = data[i++]; | 32 | 218k | if (!chunkSize) { | 33 | 492 | chunkSize = size - i; | 34 | 217k | } else { | 35 | 217k | chunkSize = std::min<int>(chunkSize, size - i); | 36 | 217k | } | 37 | | | 38 | 218k | cb(data + i, chunkSize); | 39 | 218k | i += chunkSize; | 40 | 218k | } | 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.08k | 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 | 5.47M | for (int i = 0; i < size; ) { | 31 | 5.46M | unsigned int chunkSize = data[i++]; | 32 | 5.46M | if (!chunkSize) { | 33 | 293 | chunkSize = size - i; | 34 | 5.46M | } else { | 35 | 5.46M | chunkSize = std::min<int>(chunkSize, size - i); | 36 | 5.46M | } | 37 | | | 38 | 5.46M | cb(data + i, chunkSize); | 39 | 5.46M | i += chunkSize; | 40 | 5.46M | } | 41 | 3.08k | } |
PerMessageDeflate.cpp:makeChunked(unsigned char const*, unsigned long, std::__1::function<void (unsigned char const*, unsigned long)>) Line | Count | Source | 28 | 312 | 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 | 115k | for (int i = 0; i < size; ) { | 31 | 115k | unsigned int chunkSize = data[i++]; | 32 | 115k | if (!chunkSize) { | 33 | 180 | 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 | 312 | } |
Http.cpp:makeChunked(unsigned char const*, unsigned long, std::__1::function<void (unsigned char const*, unsigned long)>) Line | Count | Source | 28 | 8.07k | 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 | 165k | for (int i = 0; i < size; ) { | 31 | 157k | unsigned int chunkSize = data[i++]; | 32 | 157k | if (!chunkSize) { | 33 | 2.40k | chunkSize = size - i; | 34 | 155k | } else { | 35 | 155k | chunkSize = std::min<int>(chunkSize, size - i); | 36 | 155k | } | 37 | | | 38 | 157k | cb(data + i, chunkSize); | 39 | 157k | i += chunkSize; | 40 | 157k | } | 41 | 8.07k | } |
|
42 | | |
43 | | /* Reads all bytes to trigger invalid reads */ |
44 | 122k | static inline void readBytes(std::string_view s) { |
45 | 122k | volatile int sum = 0; |
46 | 666k | for (int i = 0; i < s.size(); i++) { |
47 | 544k | sum += s[i]; |
48 | 544k | } |
49 | 122k | } 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 | 122k | static inline void readBytes(std::string_view s) { | 45 | 122k | volatile int sum = 0; | 46 | 666k | for (int i = 0; i < s.size(); i++) { | 47 | 544k | sum += s[i]; | 48 | 544k | } | 49 | 122k | } |
|
50 | | |
51 | | #endif |