-
Notifications
You must be signed in to change notification settings - Fork 67
/
fuzzer.c
119 lines (97 loc) · 1.94 KB
/
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
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <time.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/mman.h>
#include "map.h"
#include "util.h"
int seed;
int bits = 1;
int fuzz = 20;
void fuzzbuf(char *file, size_t size)
{
int i;
for (i = 0; i < fuzz; i++) {
unsigned r = rand();
int mode = r & 3;
r >>= 2;
int offset = r % (size*8);
int w;
switch (mode) {
case 0:
case 1:
for (w = offset; w < offset + bits; w++)
file[w / 8] ^= (1U << (w%8));
break;
case 2:
for (w = offset; w < offset + bits; w++)
file[w / 8] |= (1U << (w%8));
break;
case 3: {
unsigned val = rand();
for (w = offset; w < offset + bits; w++)
file[w / 8] = (file[w / 8] & ~(1U << (w%8))) |
((val >> (w-offset)) << (w%8));
break;
}
}
}
}
int runs1 = 5000;
int runs2 = 100;
#define BENCH(name,names,fn,arg) \
int k; \
size_t orig_outlen = outlen; \
for (k = 0; k < runs1; k++) { \
outlen = orig_outlen; \
err = c_##name(map, size, out, &outlen, arg); \
if (err) \
printf("%s: compression of %s failed: %d\n", \
names, fn, err); \
for (i = 0; i < runs2; i++) { \
fuzzbuf(out, outlen); \
d_##name(out, outlen, buf2, size, arg); \
} \
}
#include "glue.c"
void fuzzfile(char *fn)
{
size_t size;
char *map = mapfile_flag(fn, O_RDONLY, &size, MAP_PRIVATE);
if (!map) {
perror(fn);
return;
}
#ifdef SNAPREF
printf("snappy-ref\n");
test_snapref(map, size, fn);
#endif
printf("snappy\n");
test_snappy(map, size, fn);
#ifdef COMP
printf("lzo\n");
test_lzo(map, size, fn);
printf("zlib1\n");
test_zlib(map, size, fn, 1);
printf("zlib3\n");
test_zlib(map, size, fn, 3);
#endif
unmap_file(map, size);
}
void usage(void)
{
printf("usage\n");
exit(1);
}
int main(int ac, char **av)
{
//seed = time(0);
seed = 1312816253;
printf("seed %u\n", seed);
srand(seed);
while (*++av)
fuzzfile(*av);
return 0;
}