-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfile_fuzz.c
436 lines (401 loc) · 9.21 KB
/
file_fuzz.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <assert.h>
#include <unistd.h>
#include <string.h>
#include <sys/time.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#define REPS (5*1000*1000)
#define SEEK_MAX 100
#define BUFLEN 100
#define DISK_FILES 5
#define OPEN_FILES 5
// multiple offsets into same file? 1 is better BUT there does not
// appear to be a way to synchronize multiple file streams into the
// same file (POSIX appears to support it but OS X doesn't -- this is
// probably a bug but it doesn't seem clear they'll be interested in
// fixing it)
static const int MULTIPLE = 0;
// WARNING: in addition to printing more information this flag can
// change the behavior since it makes side-effecting calls; no way to
// avoid this that I know of
static const int VERBOSE = 0;
#include "rand48.h"
struct _rand48_state rs;
static int choose(int arg) { return _lrand48(&rs) % arg; }
static FILE *files[OPEN_FILES];
static int last_was_write[OPEN_FILES];
static int last_was_read[OPEN_FILES];
static int update_mode[OPEN_FILES];
// for supporting ungetc() -- just a single character is guaranteed so
// that all we'll test
static int havec[OPEN_FILES];
static int whichc[OPEN_FILES];
static const char *custom_strerror(int errnum) {
// FIXME
return "x";
switch (errnum) {
case 0:
return "Success";
case EINVAL:
return "Invalid argument";
case EBADF:
return "Bad file descriptor";
case ENOENT:
return "No such file or directory";
case EEXIST:
return "File exists";
default:
printf("oops unknown errno %d\n", errnum);
assert(0);
}
return "xxx";
}
static void do_fclose(int which) {
if (files[which] == 0)
return;
int ret = fclose(files[which]);
printf("<<< fclose(%d) = %d, %s >>>\n", which, ret, custom_strerror(errno));
files[which] = 0;
}
static void do_fopen(int which) {
int n;
if (MULTIPLE) {
n = choose(DISK_FILES);
} else {
n = which;
}
if (files[which]) {
printf("<<< close for open >>>\n");
fclose(files[which]);
errno = 0;
files[which] = 0;
}
last_was_write[which] = 0;
last_was_read[which] = 0;
update_mode[which] = 0;
havec[which] = 0;
char fn[30];
sprintf(fn, "data-%d", n);
char *mode = 0;
// FIXME -- should be choose(6) but append mode in POSIX is too screwed to test!
switch (choose(4)) {
case 0:
if (choose(2))
mode = "r";
else
mode = "rb";
break;
case 1:
switch (choose(3)) {
case 0:
mode = "r+";
break;
case 1:
mode = "rb+";
break;
case 2:
mode = "r+b";
break;
}
update_mode[which] = 1;
break;
case 2:
if (choose(2)) {
mode = "w";
} else {
mode = "wb";
}
break;
case 3:
switch (choose(3)) {
case 0:
mode = "w+";
break;
case 1:
mode = "wb+";
break;
case 2:
mode = "w+b";
break;
}
update_mode[which] = 1;
break;
case 4:
if (choose(2)) {
mode = "a";
} else {
mode = "ab";
}
break;
case 5:
switch (choose(3)) {
case 0:
mode = "a+";
break;
case 1:
mode = "ab+";
break;
case 2:
mode = "a+b";
break;
}
update_mode[which] = 1;
break;
}
FILE *f = fopen(fn, mode);
if (f != 0) {
files[which] = f;
}
printf("<<< fopen(%d, \"%s\", \"%s\") = %s, %s >>>\n", which, fn, mode,
f ? "FILE *" : "NULL", custom_strerror(errno));
}
char *strdata(unsigned char *buf, int len) {
static char _strdata[1 + BUFLEN];
int i;
for (i = 0; i < len; i++) {
assert(buf[i] == 0 || (buf[i] >= '0' && buf[i] <= '9'));
if (buf[i] == 0)
_strdata[i] = '.';
else
_strdata[i] = buf[i];
}
_strdata[i] = 0;
return _strdata;
}
static void do_fwrite(int which) {
if (files[which] == 0)
return;
if (update_mode[which] && last_was_read[which]) {
// FIXME two other positioning functions we can call here
int res = fseek(files[which], 0, SEEK_CUR);
printf("<<< fseek for write of %d = %d >>>\n", which, res);
last_was_read[which] = 0;
havec[which] = 0;
}
unsigned char buf[BUFLEN];
int len = choose(BUFLEN);
int i;
for (i = 0; i < len; i++)
buf[i] = '0' + choose(10);
int ret = fwrite(buf, 1, len, files[which]);
printf("<<< fwrite(%d, %d) = %d, %s >>>\n", which, len, ret,
custom_strerror(errno));
last_was_write[which] = 1;
// necessary for testing against a buffering implementation
if (MULTIPLE)
fflush(files[which]);
havec[which] = 0;
}
static void do_fread(int which) {
if (files[which] == 0)
return;
if (update_mode[which] && last_was_write[which]) {
// FIXME can also call file positioning functions here
fflush(files[which]);
// NOT checking the return value, fflush is too loosely specified
last_was_write[which] = 0;
}
unsigned char buf[BUFLEN];
int len = choose(BUFLEN);
int ret = fread(buf, 1, len, files[which]);
printf("<<< fread(%d, %d) = %d, %s, %s >>>\n", which, len, ret,
custom_strerror(errno), (ret == -1) ? "" : strdata(buf, ret));
last_was_read[which] = 1;
havec[which] = 0;
}
size_t get_size(int which) {
if (files[which] == 0)
return -1;
FILE *f = files[which];
long pos = ftell(f);
fseek(f, 0, SEEK_END);
long length = ftell(f);
fseek(f, pos, SEEK_SET);
havec[which] = 0;
return length;
}
static void do_size(int which) {
printf("<<< size(%d) = %ld >>>\n", which, (long)get_size(which));
}
static void do_feof(int which) {
if (files[which]) {
printf("<<< feof(%d) = %d >>>\n", which, feof(files[which]));
}
}
static void do_ferror(int which) {
if (files[which]) {
printf("<<< ferror(%d) = %d >>>\n", which, ferror(files[which]));
}
}
static void do_clearerr(int which) {
if (files[which]) {
clearerr(files[which]);
printf("<<< clearerr(%d) >>>\n", which);
}
}
static void do_fgetc(int which) {
if (files[which]) {
int r = fgetc(files[which]);
printf("<<< fgetc(%d) = %d >>>\n", which, r);
if (r >= 0) {
havec[which] = 1;
whichc[which] = r;
} else {
havec[which] = 0;
}
}
last_was_read[which] = 1;
}
static void do_ungetc(int which) {
if (files[which]) {
if (havec[which]) {
int r = ungetc(whichc[which], files[which]);
printf("<<< ungetc(%d, %d) = %d >>>\n", which, whichc[which], r);
havec[which] = 0;
} else {
do_fgetc(which);
}
}
last_was_write[which] = 1;
}
void do_fseek(int which) {
if (files[which] == 0)
return;
int whence;
switch (choose(4)) {
case 0:
whence = SEEK_SET;
break;
case 1:
whence = SEEK_CUR;
break;
case 2:
whence = SEEK_END;
break;
case 3:
whence = _lrand48(&rs);
break;
default:
assert(0);
}
off_t off = choose(SEEK_MAX) - (SEEK_MAX / 2);
int ret = fseek(files[which], off, whence);
printf("<<< fseek(%d, %ld, %d) = %d, %s >>>\n", which, (long)off, whence, ret,
custom_strerror(errno));
havec[which] = 0;
}
void do_unlink(int which) {
int n;
if (MULTIPLE) {
n = choose(DISK_FILES);
} else {
n = which;
}
char fn[30];
sprintf(fn, "data-%d", n);
int res = unlink(fn);
printf("<<< unlink(%d, \"%s\") = %d, %s >>>\n", which, fn, res,
custom_strerror(errno));
}
void do_fflush(int which) {
if (files[which]) {
int res = fflush(files[which]);
printf("<<< fflush(%d) = %d, %s >>>\n", which, res, custom_strerror(errno));
}
}
void print_verbose(int which, const char *when) {
if (files[which]) {
long pos = ftell(files[which]);
long size = get_size(which);
printf("<<< at %s size of %d is %ld pos %ld eof %d error %d >>>\n",
when, which, size, pos, feof(files[which]), ferror(files[which]));
}
}
#ifdef RANDOM_SEED
int main(void) {
#else
int main(int argc, char *argv[]) {
int RANDOM_SEED;
if (argc != 2) {
printf("either #define RANDOM_SEED to be the seed or else provide seed on command line\n");
exit(-1);
}
RANDOM_SEED = atoi(argv[1]);
#endif
printf("random seed = %d\n", RANDOM_SEED);
_srand48(&rs, RANDOM_SEED);
int i;
int useless = 0;
for (i = 0; i < OPEN_FILES; i++) {
files[i] = 0;
if (VERBOSE)
print_verbose(i, "init");
}
for (i = 0; i < REPS; i++) {
unsigned op = choose(13);
int which = choose(OPEN_FILES);
if (VERBOSE)
print_verbose(which, "before");
errno = 0;
switch (op) {
case 0:
switch (choose(2)) {
case 0:
do_fopen(which);
break;
case 1:
do_unlink(which);
break;
}
break;
case 1:
do_fclose(which);
break;
case 2:
do_fwrite(which);
break;
case 3:
do_fread(which);
break;
case 4:
do_fseek(which);
break;
case 5:
do_size(which);
break;
case 6:
do_feof(which);
break;
case 7:
do_ferror(which);
break;
case 8:
do_clearerr(which);
break;
case 9:
do_fgetc(which);
break;
case 10:
do_ungetc(which);
break;
case 11:
do_ungetc(which);
break;
case 12:
useless++;
break;
default:
assert(0);
}
if (VERBOSE)
print_verbose(which, "after");
}
assert(useless > 0);
printf("<<< Done >>>\n");
return 0;
}