-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
example_dict_fuzzer.c
165 lines (136 loc) · 4.72 KB
/
example_dict_fuzzer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <stdio.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include <inttypes.h>
#include "zlib.h"
#define CHECK_ERR(err, msg) { \
if (err != Z_OK) { \
fprintf(stderr, "%s error: %d\n", msg, err); \
return 0; \
} \
}
static const uint8_t *data;
static size_t dataLen;
static alloc_func zalloc = NULL;
static free_func zfree = NULL;
static size_t dictionaryLen = 0;
static unsigned long dictId; /* Adler32 value of the dictionary */
/* ===========================================================================
* Test deflate() with preset dictionary
*/
int test_dict_deflate(unsigned char **compr, size_t *comprLen)
{
z_stream c_stream; /* compression stream */
int err;
int level = data[0] % 11 - 1; /* [-1..9]
compression levels
#define Z_NO_COMPRESSION 0
#define Z_BEST_SPEED 1
#define Z_BEST_COMPRESSION 9
#define Z_DEFAULT_COMPRESSION (-1) */
int method = Z_DEFLATED; /* The deflate compression method (the only one
supported in this version) */
int windowBits = 8 + data[0] % 8; /* The windowBits parameter is the base
two logarithm of the window size (the size of the history buffer). It
should be in the range 8..15 for this version of the library. */
int memLevel = 1 + data[0] % 9; /* memLevel=1 uses minimum memory but is
slow and reduces compression ratio; memLevel=9 uses maximum memory for
optimal speed. */
int strategy = data[0] % 5; /* [0..4]
#define Z_FILTERED 1
#define Z_HUFFMAN_ONLY 2
#define Z_RLE 3
#define Z_FIXED 4
#define Z_DEFAULT_STRATEGY 0 */
/* deflate would fail for no-compression or for speed levels. */
if (level == 0 || level == 1)
level = -1;
c_stream.zalloc = zalloc;
c_stream.zfree = zfree;
c_stream.opaque = (void *)0;
err = deflateInit2(&c_stream, level, method, windowBits, memLevel, strategy);
CHECK_ERR(err, "deflateInit");
err = deflateSetDictionary(
&c_stream, (const unsigned char *)data, dictionaryLen);
CHECK_ERR(err, "deflateSetDictionary");
/* deflateBound does not provide enough space for low compression levels. */
*comprLen = 100 + 2 * deflateBound(&c_stream, dataLen);
*compr = (uint8_t *)calloc(1, *comprLen);
dictId = c_stream.adler;
c_stream.next_out = *compr;
c_stream.avail_out = (unsigned int)(*comprLen);
c_stream.next_in = (Bytef *)data;
c_stream.avail_in = dataLen;
err = deflate(&c_stream, Z_FINISH);
if (err != Z_STREAM_END) {
fprintf(stderr, "deflate dict should report Z_STREAM_END\n");
return 0;
}
err = deflateEnd(&c_stream);
CHECK_ERR(err, "deflateEnd");
return 0;
}
/* ===========================================================================
* Test inflate() with a preset dictionary
*/
int test_dict_inflate(unsigned char *compr, size_t comprLen) {
int err;
z_stream d_stream; /* decompression stream */
unsigned char *uncompr;
d_stream.zalloc = zalloc;
d_stream.zfree = zfree;
d_stream.opaque = (void *)0;
d_stream.next_in = compr;
d_stream.avail_in = (unsigned int)comprLen;
err = inflateInit(&d_stream);
CHECK_ERR(err, "inflateInit");
uncompr = (uint8_t *)calloc(1, dataLen);
d_stream.next_out = uncompr;
d_stream.avail_out = (unsigned int)dataLen;
for (;;) {
err = inflate(&d_stream, Z_NO_FLUSH);
if (err == Z_STREAM_END)
break;
if (err == Z_NEED_DICT) {
if (d_stream.adler != dictId) {
fprintf(stderr, "unexpected dictionary");
return 0;
}
err = inflateSetDictionary(
&d_stream, (const unsigned char *)data, dictionaryLen);
}
CHECK_ERR(err, "inflate with dict");
}
err = inflateEnd(&d_stream);
CHECK_ERR(err, "inflateEnd");
if (memcmp(uncompr, data, dataLen)) {
fprintf(stderr, "bad inflate with dict\n");
return 0;
}
free(uncompr);
return 0;
}
int LLVMFuzzerTestOneInput(const uint8_t *d, size_t size) {
size_t comprLen = 0;
uint8_t *compr;
/* Discard inputs larger than 100Kb. */
static size_t kMaxSize = 100 * 1024;
if (size < 1 || size > kMaxSize)
return 0;
data = d;
dataLen = size;
/* Set up the contents of the dictionary. The size of the dictionary is
intentionally selected to be of unusual size. To help cover more corner
cases, the size of the dictionary is read from the input data. */
dictionaryLen = data[0];
if (dictionaryLen > dataLen)
dictionaryLen = dataLen;
test_dict_deflate(&compr, &comprLen);
test_dict_inflate(compr, comprLen);
free(compr);
/* This function must return 0. */
return 0;
}