source: zzuf/trunk/src/lib-stream.c @ 1688

Last change on this file since 1688 was 1688, checked in by Sam Hocevar, 15 years ago
  • Improved %c usage in our custom fprintf() implementation.
  • Property svn:keywords set to Id
File size: 13.8 KB
Line 
1/*
2 *  zzuf - general purpose fuzzer
3 *  Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: lib-stream.c 1688 2007-01-17 15:31:40Z sam $
7 *
8 *  This program is free software. It comes without any warranty, to
9 *  the extent permitted by applicable law. You can redistribute it
10 *  and/or modify it under the terms of the Do What The Fuck You Want
11 *  To Public License, Version 2, as published by Sam Hocevar. See
12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
13 */
14
15/*
16 *  load-stream.c: loaded stream functions
17 */
18
19#include "config.h"
20
21#define _GNU_SOURCE /* for getline() and getdelim() */
22
23#if defined HAVE_STDINT_H
24#   include <stdint.h>
25#elif defined HAVE_INTTYPES_H
26#   include <inttypes.h>
27#endif
28#include <stdlib.h>
29#include <dlfcn.h>
30
31#include <stdio.h>
32#include <sys/types.h>
33#ifdef HAVE___SREFILL
34#   include <unistd.h> /* Needed for __srefill’s lseek() call */
35#endif
36
37#include "libzzuf.h"
38#include "lib-load.h"
39#include "debug.h"
40#include "fuzz.h"
41#include "fd.h"
42
43#ifdef HAVE___SREFILL
44int __srefill(FILE *fp);
45#endif
46
47/* Library functions that we divert */
48static FILE *  (*fopen_orig)    (const char *path, const char *mode);
49#ifdef HAVE_FOPEN64
50static FILE *  (*fopen64_orig)  (const char *path, const char *mode);
51#endif
52static FILE *  (*freopen_orig)  (const char *path, const char *mode,
53                                 FILE *stream);
54static int     (*fseek_orig)    (FILE *stream, long offset, int whence);
55#ifdef HAVE_FSEEKO
56static int     (*fseeko_orig)   (FILE *stream, off_t offset, int whence);
57#endif
58static void    (*rewind_orig)   (FILE *stream);
59static size_t  (*fread_orig)    (void *ptr, size_t size, size_t nmemb,
60                                 FILE *stream);
61static int     (*getc_orig)     (FILE *stream);
62static int     (*fgetc_orig)    (FILE *stream);
63#ifdef HAVE__IO_GETC
64static int     (*_IO_getc_orig) (FILE *stream);
65#endif
66static char *  (*fgets_orig)    (char *s, int size, FILE *stream);
67static int     (*ungetc_orig)   (int c, FILE *stream);
68static int     (*fclose_orig)   (FILE *fp);
69
70/* Additional GNUisms */
71#ifdef HAVE_GETLINE
72static ssize_t (*getline_orig)    (char **lineptr, size_t *n, FILE *stream);
73#endif
74#ifdef HAVE_GETDELIM
75static ssize_t (*getdelim_orig)   (char **lineptr, size_t *n, int delim,
76                                   FILE *stream);
77#endif
78#ifdef HAVE___GETDELIM
79static ssize_t (*__getdelim_orig) (char **lineptr, size_t *n, int delim,
80                                   FILE *stream);
81#endif
82
83/* Additional BSDisms */
84#ifdef HAVE_FGETLN
85static char *  (*fgetln_orig)    (FILE *stream, size_t *len);
86#endif
87#ifdef HAVE___SREFILL
88int            (*__srefill_orig) (FILE *fp);
89#endif
90
91/* Our function wrappers */
92#define FOPEN(fn) \
93    do \
94    { \
95        LOADSYM(fn); \
96        if(!_zz_ready) \
97            return ORIG(fn)(path, mode); \
98        _zz_disabled = 1; \
99        ret = ORIG(fn)(path, mode); \
100        _zz_disabled = 0; \
101        if(ret && _zz_mustwatch(path)) \
102        { \
103            int fd = fileno(ret); \
104            _zz_register(fd); \
105            debug("%s(\"%s\", \"%s\") = [%i]", __func__, path, mode, fd); \
106        } \
107    } while(0)
108
109FILE *fopen(const char *path, const char *mode)
110{
111    FILE *ret; FOPEN(fopen); return ret;
112}
113
114#ifdef HAVE_FOPEN64
115FILE *fopen64(const char *path, const char *mode)
116{
117    FILE *ret; FOPEN(fopen64); return ret;
118}
119#endif
120
121FILE *freopen(const char *path, const char *mode, FILE *stream)
122{
123    FILE *ret;
124    int fd0 = -1, fd1 = -1, disp = 0;
125
126    LOADSYM(freopen);
127    if(_zz_ready && (fd0 = fileno(stream)) >= 0 && _zz_iswatched(fd0))
128    {
129        _zz_unregister(fd0);
130        disp = 1;
131    }
132
133    _zz_disabled = 1;
134    ret = freopen_orig(path, mode, stream);
135    _zz_disabled = 0;
136
137    if(ret && _zz_mustwatch(path))
138    {
139        fd1 = fileno(ret);
140        _zz_register(fd1);
141        disp = 1;
142    }
143
144    if(disp)
145        debug("%s(\"%s\", \"%s\", [%i]) = [%i]", __func__,
146              path, mode, fd0, fd1);
147
148    return ret;
149}
150
151#if defined HAVE___SREFILL /* Don't fuzz or seek if we have __srefill() */
152#   define FSEEK_FUZZ(fn2)
153#else
154#   define FSEEK_FUZZ(fn2) \
155        if(ret == 0) \
156        { \
157            /* FIXME: check what happens when fseek()ing a pipe */ \
158            switch(whence) \
159            { \
160                case SEEK_END: \
161                    offset = fn2(stream); \
162                    /* fall through */ \
163                case SEEK_SET: \
164                    _zz_setpos(fd, offset); \
165                    break; \
166                case SEEK_CUR: \
167                    _zz_addpos(fd, offset); \
168                    break; \
169            } \
170        }
171#endif
172
173#define FSEEK(fn, fn2) \
174    do \
175    { \
176        int fd; \
177        LOADSYM(fn); \
178        fd = fileno(stream); \
179        if(!_zz_ready || !_zz_iswatched(fd)) \
180            return ORIG(fn)(stream, offset, whence); \
181        _zz_disabled = 1; \
182        ret = ORIG(fn)(stream, offset, whence); \
183        _zz_disabled = 0; \
184        debug("%s([%i], %lli, %i) = %i", __func__, \
185              fd, (long long int)offset, whence, ret); \
186        FSEEK_FUZZ(fn2) \
187    } while(0)
188
189int fseek(FILE *stream, long offset, int whence)
190{
191    int ret; FSEEK(fseek, ftell); return ret;
192}
193
194#ifdef HAVE_FSEEKO
195int fseeko(FILE *stream, off_t offset, int whence)
196{
197    int ret; FSEEK(fseeko, ftello); return ret;
198}
199#endif
200
201void rewind(FILE *stream)
202{
203    int fd;
204
205    LOADSYM(rewind);
206    fd = fileno(stream);
207    if(!_zz_ready || !_zz_iswatched(fd))
208    {
209        rewind_orig(stream);
210        return;
211    }
212
213    _zz_disabled = 1;
214    rewind_orig(stream);
215    _zz_disabled = 0;
216    debug("%s([%i])", __func__, fd);
217
218#if defined HAVE___SREFILL /* Don't fuzz or seek if we have __srefill() */
219#else
220    /* FIXME: check what happens when rewind()ing a pipe */
221    _zz_setpos(fd, 0);
222#endif
223}
224
225size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
226{
227    long int pos;
228#if defined HAVE___SREFILL /* Don't fuzz or seek if we have __srefill() */
229#else
230    long int newpos;
231#endif
232    size_t ret;
233    int fd;
234
235    LOADSYM(fread);
236    fd = fileno(stream);
237    if(!_zz_ready || !_zz_iswatched(fd))
238        return fread_orig(ptr, size, nmemb, stream);
239
240    pos = ftell(stream);
241    _zz_disabled = 1;
242    ret = fread_orig(ptr, size, nmemb, stream);
243    _zz_disabled = 0;
244    debug("%s(%p, %li, %li, [%i]) = %li", __func__, ptr,
245          (long int)size, (long int)nmemb, fd, (long int)ret);
246
247#if defined HAVE___SREFILL /* Don't fuzz or seek if we have __srefill() */
248#else
249    newpos = ftell(stream);
250    /* XXX: the number of bytes read is not ret * size, because
251     * a partial read may have advanced the stream pointer. However,
252     * when reading from a pipe ftell() will return 0, and ret * size
253     * is then better than nothing. */
254    if(newpos <= 0)
255        newpos = ret * size;
256    if(newpos != pos)
257    {
258        _zz_fuzz(fd, ptr, newpos - pos);
259        _zz_setpos(fd, newpos);
260    }
261#endif
262
263    return ret;
264}
265
266#if defined HAVE___SREFILL /* Don't fuzz or seek if we have __srefill() */
267#   define FGETC_FUZZ
268#else
269#   define FGETC_FUZZ \
270        if(ret != EOF) \
271        { \
272            uint8_t ch = ret; \
273            _zz_fuzz(fd, &ch, 1); \
274            _zz_addpos(fd, 1); \
275            ret = ch; \
276        }
277#endif
278
279#define FGETC(fn) \
280    do { \
281        int fd; \
282        LOADSYM(fn); \
283        fd = fileno(stream); \
284        if(!_zz_ready || !_zz_iswatched(fd)) \
285            return ORIG(fn)(stream); \
286        _zz_disabled = 1; \
287        ret = ORIG(fn)(stream); \
288        _zz_disabled = 0; \
289        FGETC_FUZZ \
290        debug("%s([%i]) = '%c'", __func__, fd, ret); \
291    } while(0)
292
293#undef getc /* can be a macro; we don’t want that */
294int getc(FILE *stream)
295{
296    int ret; FGETC(getc); return ret;
297}
298
299int fgetc(FILE *stream)
300{
301    int ret; FGETC(fgetc); return ret;
302}
303
304#ifdef HAVE__IO_GETC
305int _IO_getc(FILE *stream)
306{
307    int ret; FGETC(_IO_getc); return ret;
308}
309#endif
310
311char *fgets(char *s, int size, FILE *stream)
312{
313    char *ret = s;
314    int fd;
315
316    LOADSYM(fgets);
317    LOADSYM(fgetc);
318    fd = fileno(stream);
319    if(!_zz_ready || !_zz_iswatched(fd))
320        return fgets_orig(s, size, stream);
321
322#if defined HAVE___SREFILL /* Don't fuzz or seek if we have __srefill() */
323    _zz_disabled = 1;
324    ret = fgets_orig(s, size, stream);
325    _zz_disabled = 0;
326#else
327    if(size <= 0)
328        ret = NULL;
329    else if(size == 1)
330        s[0] = '\0';
331    else
332    {
333        int i;
334
335        for(i = 0; i < size - 1; i++)
336        {
337            int ch;
338
339            _zz_disabled = 1;
340            ch = fgetc_orig(stream);
341            _zz_disabled = 0;
342
343            if(ch == EOF)
344            {
345                s[i] = '\0';
346                if(!i)
347                    ret = NULL;
348                break;
349            }
350            s[i] = (char)(unsigned char)ch;
351            _zz_fuzz(fd, (uint8_t *)s + i, 1); /* rather inefficient */
352            _zz_addpos(fd, 1);
353            if(s[i] == '\n')
354            {
355                s[i + 1] = '\0';
356                break;
357            }
358        }
359    }
360#endif
361
362    debug("%s(%p, %i, [%i]) = %p", __func__, s, size, fd, ret);
363    return ret;
364}
365
366int ungetc(int c, FILE *stream)
367{
368    unsigned char ch = c;
369    int ret, fd;
370
371    LOADSYM(ungetc);
372    fd = fileno(stream);
373    if(!_zz_ready || !_zz_iswatched(fd))
374        return ungetc_orig(c, stream);
375
376#if defined HAVE___SREFILL /* Don't fuzz or seek if we have __srefill() */
377#else
378    _zz_addpos(fd, -1);
379    _zz_fuzz(fd, &ch, 1);
380#endif
381    _zz_disabled = 1;
382    ret = ungetc_orig((int)ch, stream);
383    _zz_disabled = 0;
384
385    if(ret >= 0)
386        ret = c;
387#if defined HAVE___SREFILL /* Don't fuzz or seek if we have __srefill() */
388#else
389    else
390        _zz_addpos(fd, 1); /* revert what we did */
391#endif
392
393    debug("%s(0x%02x, [%i]) = '%c'", __func__, c, fd, ret);
394    return ret;
395}
396
397int fclose(FILE *fp)
398{
399    int ret, fd;
400
401    LOADSYM(fclose);
402    fd = fileno(fp);
403    if(!_zz_ready || !_zz_iswatched(fd))
404        return fclose_orig(fp);
405
406    _zz_disabled = 1;
407    ret = fclose_orig(fp);
408    _zz_disabled = 0;
409    debug("%s([%i]) = %i", __func__, fd, ret);
410    _zz_unregister(fd);
411
412    return ret;
413}
414
415#define GETDELIM(fn, delim, need_delim) \
416    do { \
417        char *line; \
418        ssize_t done, size; \
419        int fd, finished = 0; \
420        LOADSYM(fn); \
421        LOADSYM(getdelim); \
422        LOADSYM(fgetc); \
423        fd = fileno(stream); \
424        if(!_zz_ready || !_zz_iswatched(fd)) \
425            return getdelim_orig(lineptr, n, delim, stream); \
426        line = *lineptr; \
427        size = line ? *n : 0; \
428        ret = done = finished = 0; \
429        for(;;) \
430        { \
431            int ch; \
432            if(done >= size) /* highly inefficient but I don't care */ \
433                line = realloc(line, size = done + 1); \
434            if(finished) \
435            { \
436                line[done] = '\0'; \
437                *n = size; \
438                *lineptr = line; \
439                break; \
440            } \
441            _zz_disabled = 1; \
442            ch = fgetc_orig(stream); \
443            _zz_disabled = 0; \
444            if(ch == EOF) \
445            { \
446                finished = 1; \
447                ret = done; \
448            } \
449            else \
450            { \
451                unsigned char c = ch; \
452                _zz_fuzz(fd, &c, 1); /* even more inefficient */ \
453                line[done++] = c; \
454                _zz_addpos(fd, 1); \
455                if(c == delim) \
456                { \
457                    finished = 1; \
458                    ret = done; \
459                } \
460            } \
461        } \
462        if(need_delim) \
463            debug("%s(%p, %p, '%c', [%i]) = %li", __func__, \
464                  lineptr, n, delim, fd, (long int)ret); \
465        else \
466            debug("%s(%p, %p, [%i]) = %li", __func__, \
467                  lineptr, n, fd, (long int)ret); \
468        return ret; \
469    } while(0)
470
471#ifdef HAVE_GETLINE
472ssize_t getline(char **lineptr, size_t *n, FILE *stream)
473{
474    ssize_t ret; GETDELIM(getline, '\n', 0); return ret;
475}
476#endif
477
478#ifdef HAVE_GETDELIM
479ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream)
480{
481    ssize_t ret; GETDELIM(getdelim, delim, 1); return ret;
482}
483#endif
484
485#ifdef HAVE___GETDELIM
486ssize_t __getdelim(char **lineptr, size_t *n, int delim, FILE *stream)
487{
488    ssize_t ret; GETDELIM(__getdelim, delim, 1); return ret;
489}
490#endif
491
492#ifdef HAVE_FGETLN
493char *fgetln(FILE *stream, size_t *len)
494{
495    char *ret;
496#if defined HAVE___SREFILL /* Don't fuzz or seek if we have __srefill() */
497#else
498    struct fuzz *fuzz;
499    size_t i, size;
500#endif
501    int fd;
502
503    LOADSYM(fgetln);
504    LOADSYM(fgetc);
505    fd = fileno(stream);
506    if(!_zz_ready || !_zz_iswatched(fd))
507        return fgetln_orig(stream, len);
508
509#if defined HAVE___SREFILL /* Don't fuzz or seek if we have __srefill() */
510    _zz_disabled = 1;
511    ret = fgetln_orig(stream, len);
512    _zz_disabled = 0;
513#else
514    fuzz = _zz_getfuzz(fd);
515
516    for(i = size = 0; ; /* i is incremented below */)
517    {
518        int ch;
519
520        _zz_disabled = 1;
521        ch = fgetc_orig(stream);
522        _zz_disabled = 0;
523
524        if(ch == EOF)
525            break;
526
527        if(i >= size)
528            fuzz->tmp = realloc(fuzz->tmp, (size += 80));
529
530        fuzz->tmp[i] = (char)(unsigned char)ch;
531        _zz_fuzz(fd, (uint8_t *)fuzz->tmp + i, 1); /* rather inefficient */
532        _zz_addpos(fd, 1);
533
534        if(fuzz->tmp[i++] == '\n')
535            break;
536    }
537
538    *len = i;
539    ret = fuzz->tmp;
540#endif
541
542    debug("%s([%i], &%li) = %p", __func__, fd, (long int)*len, ret);
543    return ret;
544}
545#endif
546
547#ifdef HAVE___SREFILL
548int __srefill(FILE *fp)
549{
550    off_t newpos;
551    int ret, fd, tmp;
552
553    LOADSYM(__srefill);
554    fd = fileno(fp);
555    if(!_zz_ready || !_zz_iswatched(fd))
556        return __srefill_orig(fp);
557
558    tmp = _zz_disabled;
559    _zz_disabled = 1;
560    ret = __srefill_orig(fp);
561    newpos = lseek(fd, 0, SEEK_CUR);
562    _zz_disabled = tmp;
563    if(ret != EOF)
564    {
565        if(newpos != -1)
566            _zz_setpos(fd, newpos - fp->_r);
567        _zz_fuzz(fd, fp->_p, fp->_r);
568        _zz_addpos(fd, fp->_r);
569    }
570
571    if(!_zz_disabled)
572        debug("%s([%i]) = %i", __func__, fd, ret);
573
574    return ret;
575}
576#endif
577
Note: See TracBrowser for help on using the repository browser.