/src/uWebSockets/fuzzing/PerMessageDeflate.cpp
Line | Count | Source |
1 | | /* This is a fuzz test of the permessage-deflate module */ |
2 | | |
3 | | #define WIN32_EXPORT |
4 | | |
5 | | #include <cstdio> |
6 | | #include <string> |
7 | | #include <bitset> |
8 | | |
9 | | /* We test the permessage deflate module */ |
10 | | #include "../src/PerMessageDeflate.h" |
11 | | |
12 | | #include "helpers.h" |
13 | | |
14 | 146 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
15 | | |
16 | | /* First byte determines what compressor to use */ |
17 | 146 | if (size >= 1) { |
18 | | |
19 | 146 | uWS::CompressOptions compressors[] = { |
20 | 146 | uWS::DEDICATED_COMPRESSOR_3KB, |
21 | 146 | uWS::DEDICATED_COMPRESSOR_4KB, |
22 | 146 | uWS::DEDICATED_COMPRESSOR_8KB, |
23 | 146 | uWS::DEDICATED_COMPRESSOR_16KB, |
24 | 146 | uWS::DEDICATED_COMPRESSOR_32KB, |
25 | 146 | uWS::DEDICATED_COMPRESSOR_64KB, |
26 | 146 | uWS::DEDICATED_COMPRESSOR_128KB, |
27 | 146 | uWS::DEDICATED_COMPRESSOR_256KB |
28 | 146 | }; |
29 | | |
30 | 146 | auto compressor = compressors[data[0] % 8]; |
31 | 146 | data++; |
32 | 146 | size--; |
33 | | |
34 | | /* Bits 0 - 256 are okay */ |
35 | 146 | std::bitset<257> b; |
36 | | |
37 | | /* If we could specify LARGE_BUFFER_SIZE small here we could force it to inflate in chunks, |
38 | | * triggering more line coverage. Currently it is set to 16kb which is always too much */ |
39 | 146 | struct StaticData { |
40 | 146 | uWS::DeflationStream deflationStream; |
41 | 146 | uWS::InflationStream inflationStream; |
42 | 146 | uWS::ZlibContext zlibContext; |
43 | 146 | } staticData = {compressor, compressor}; |
44 | | |
45 | | /* Why is this padded? */ |
46 | 47.6k | makeChunked(makePadded(data, size), size, [&staticData, &b](const uint8_t *data, size_t size) { |
47 | 47.6k | auto inflation = staticData.inflationStream.inflate(&staticData.zlibContext, std::string_view((char *) data, size), 256, true); |
48 | | |
49 | | /* Trigger ASAN flaws if length is more than 256 */ |
50 | 47.6k | if (inflation.has_value()) { |
51 | 47.6k | b.set(inflation->length()); |
52 | 47.6k | } |
53 | 47.6k | }); |
54 | | |
55 | 47.6k | makeChunked(makePadded(data, size), size, [&staticData](const uint8_t *data, size_t size) { |
56 | | /* Always reset */ |
57 | 47.6k | staticData.deflationStream.deflate(&staticData.zlibContext, std::string_view((char *) data, size), true); |
58 | 47.6k | }); |
59 | | |
60 | 146 | } |
61 | | |
62 | 146 | return 0; |
63 | 146 | } |
64 | | |