source: zzuf/trunk/src/load-stream.c @ 1494

Last change on this file since 1494 was 1494, checked in by Sam Hocevar, 16 years ago
  • Split preload.c into load-fd.c and load-stream.c.
  • Property svn:keywords set to Id
File size: 3.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: load-stream.c 1494 2006-12-17 17:17:31Z 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/* Can't remember what that's for */
22#define _GNU_SOURCE
23
24#if defined HAVE_STDINT_H
25#   include <stdint.h>
26#elif defined HAVE_INTTYPES_H
27#   include <inttypes.h>
28#endif
29#include <stdlib.h>
30#include <regex.h>
31#include <dlfcn.h>
32
33#include <stdio.h>
34
35#include "libzzuf.h"
36#include "debug.h"
37#include "fuzz.h"
38#include "load.h"
39
40/* Library functions that we divert */
41static FILE *  (*fopen_orig)   (const char *path, const char *mode);
42static FILE *  (*fopen64_orig) (const char *path, const char *mode);
43static int     (*fseek_orig)   (FILE *stream, long offset, int whence);
44static size_t  (*fread_orig)   (void *ptr, size_t size, size_t nmemb,
45                                FILE *stream);
46static int     (*fclose_orig)  (FILE *fp);
47
48void zzuf_load_stream(void)
49{
50    LOADSYM(fopen);
51    LOADSYM(fopen64);
52    LOADSYM(fseek);
53    LOADSYM(fread);
54    LOADSYM(fclose);
55}
56
57/* Our function wrappers */
58#define FOPEN(fn) \
59    do \
60    { \
61        if(!_zzuf_ready) \
62            LOADSYM(fn); \
63        ret = ORIG(fn)(path, mode); \
64        if(!_zzuf_ready) \
65            return ret; \
66        if(ret) \
67        { \
68            if(_zzuf_include && \
69                regexec(_zzuf_include, path, 0, NULL, 0) == REG_NOMATCH) \
70                /* not included: ignore */ ; \
71            else if(_zzuf_exclude && \
72                    regexec(_zzuf_exclude, path, 0, NULL, 0) != REG_NOMATCH) \
73                /* excluded: ignore */ ; \
74            else \
75            { \
76                int fd = fileno(ret); \
77                files[fd].managed = 1; \
78                files[fd].pos = 0; \
79                debug(STR(fn) "(\"%s\", \"%s\") = %p", path, mode, ret); \
80            } \
81        } \
82    } while(0)
83
84FILE *fopen(const char *path, const char *mode)
85{
86    FILE *ret; FOPEN(fopen); return ret;
87}
88
89FILE *fopen64(const char *path, const char *mode)
90{
91    FILE *ret; FOPEN(fopen64); return ret;
92}
93
94int fseek(FILE *stream, long offset, int whence)
95{
96    int ret, fd;
97
98    if(!_zzuf_ready)
99        LOADSYM(fseek);
100    ret = fseek_orig(stream, offset, whence);
101    if(!_zzuf_ready)
102        return ret;
103
104    fd = fileno(stream);
105    if(!files[fd].managed)
106        return ret;
107
108    debug("fseek(%p, %li, %i) = %i", stream, offset, whence, ret);
109    if(ret == 0)
110    {
111        switch(whence)
112        {
113            case SEEK_SET: files[fd].pos = offset; break;
114            case SEEK_CUR: files[fd].pos += offset; break;
115            case SEEK_END: files[fd].pos = ftell(stream); break;
116        }
117    }
118    return ret;
119}
120
121size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
122{
123    size_t ret;
124    int fd;
125
126    if(!_zzuf_ready)
127        LOADSYM(fread);
128    ret = fread_orig(ptr, size, nmemb, stream);
129    if(!_zzuf_ready)
130        return ret;
131
132    fd = fileno(stream);
133    if(!files[fd].managed)
134        return ret;
135
136    debug("fread(%p, %li, %li, %p) = %li",
137          ptr, (long int)size, (long int)nmemb, stream, (long int)ret);
138    if(ret > 0)
139    {
140        zzuf_fuzz(fd, ptr, ret * size);
141        files[fd].pos += ret * size;
142    }
143    return ret;
144}
145
146int fclose(FILE *fp)
147{
148    int ret, fd;
149
150    if(!_zzuf_ready)
151        LOADSYM(fclose);
152    fd = fileno(fp);
153    ret = fclose_orig(fp);
154    if(!_zzuf_ready)
155        return ret;
156
157    if(!files[fd].managed)
158        return ret;
159
160    debug("fclose(%p) = %i", fp, ret);
161    files[fd].managed = 0;
162
163    return ret;
164}
165
Note: See TracBrowser for help on using the repository browser.