source: zzuf/trunk/src/fd.c @ 1697

Last change on this file since 1697 was 1697, checked in by Sam Hocevar, 16 years ago
  • Minor improvement in the locking mechanism.
  • Property svn:keywords set to Id
File size: 7.1 KB
Line 
1/*
2 *  zzuf - general purpose fuzzer
3 *  Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: fd.c 1697 2007-01-18 18:45:58Z 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 *  libzzuf.c: preloaded wrapper library
17 */
18
19#include "config.h"
20#define _GNU_SOURCE
21
22#if defined HAVE_STDINT_H
23#   include <stdint.h>
24#elif defined HAVE_INTTYPES_H
25#   include <inttypes.h>
26#endif
27#include <stdlib.h>
28#if defined HAVE_REGEX_H
29#   include <regex.h>
30#endif
31#include <string.h>
32#include <math.h>
33
34#include "debug.h"
35#include "libzzuf.h"
36#include "fd.h"
37
38/* Regex stuff */
39#if defined HAVE_REGEX_H
40static regex_t re_include, re_exclude;
41static int has_include = 0, has_exclude = 0;
42#endif
43
44/* File descriptor stuff. When program is launched, we use the static array of
45 * 32 structures, which ought to be enough for most programs. If it happens
46 * not to be the case, ie. if the process opens more than 32 file descriptors
47 * at the same time, a bigger array is malloc()ed and replaces the static one.
48 */
49#define STATIC_FILES 32
50static struct files
51{
52    int managed, locked;
53    uint64_t pos;
54    /* Public stuff */
55    struct fuzz fuzz;
56}
57*files, static_files[STATIC_FILES];
58static int *fds, static_fds[STATIC_FILES];
59static int maxfd, nfiles;
60
61/* Create lock. This lock variable is used to disable file descriptor
62 * creation wrappers. For instance on Mac OS X, fopen() calls open()
63 * and we don’t want open() to do any zzuf-related stuff, fopen() takes
64 * care of everything. */
65static int create_lock = 0;
66
67static int32_t seed = DEFAULT_SEED;
68static double  minratio = DEFAULT_RATIO;
69static double  maxratio = DEFAULT_RATIO;
70static int     autoinc = 0;
71
72void _zz_include(char const *regex)
73{
74#if defined HAVE_REGEX_H
75    if(regcomp(&re_include, regex, REG_EXTENDED) == 0)
76        has_include = 1;
77#else
78    (void)regex;
79#endif
80}
81
82void _zz_exclude(char const *regex)
83{
84#if defined HAVE_REGEX_H
85    if(regcomp(&re_exclude, regex, REG_EXTENDED) == 0)
86        has_exclude = 1;
87#else
88    (void)regex;
89#endif
90}
91
92void _zz_setseed(int32_t s)
93{
94    seed = s;
95}
96
97void _zz_setratio(double r0, double r1)
98{
99    if(r0 == 0.0 && r1 == 0.0)
100    {
101        maxratio = minratio = 0.0;
102        return;
103    }
104
105    minratio = r0 < MIN_RATIO ? MIN_RATIO : r0 > MAX_RATIO ? MAX_RATIO : r0;
106    maxratio = r1 < MIN_RATIO ? MIN_RATIO : r1 > MAX_RATIO ? MAX_RATIO : r1;
107    if(maxratio < minratio)
108        maxratio = minratio;
109}
110
111double _zz_getratio(void)
112{
113    uint8_t const shuffle[16] =
114    { 0, 12, 2, 10,
115      14, 8, 15, 7,
116      9, 13, 3, 6,
117      4, 1, 11, 5 };
118    uint16_t rate;
119    double min, max, cur;
120
121    if(minratio == maxratio)
122        return minratio; /* this also takes care of 0.0 */
123
124    rate = shuffle[seed & 0xf] << 12;
125    rate |= (seed & 0xf0) << 4;
126    rate |= (seed & 0xf00) >> 4;
127    rate |= (seed & 0xf000) >> 12;
128
129    min = log(minratio);
130    max = log(maxratio);
131
132    cur = min + (max - min) * rate / 0xffff;
133
134    return exp(cur);
135}
136
137void _zz_setautoinc(void)
138{
139    autoinc = 1;
140}
141
142void _zz_fd_init(void)
143{
144    /* We start with 32 file descriptors. This is to reduce the number of
145     * calls to malloc() that we do, so we get better chances that memory
146     * corruption errors are reproducible */
147    files = static_files;
148    for(nfiles = 0; nfiles < 32; nfiles++)
149        files[nfiles].managed = 0;
150
151    fds = static_fds;
152    for(maxfd = 0; maxfd < 32; maxfd++)
153        fds[maxfd] = -1;
154}
155
156void _zz_fd_fini(void)
157{
158    int i;
159
160    for(i = 0; i < maxfd; i++)
161    {
162        if(!files[fds[i]].managed)
163            continue;
164
165        /* XXX: What are we supposed to do? If filedescriptors weren't
166         * closed properly, there's a leak, but it's not our problem. */
167    }
168
169    if(files != static_files)
170       free(files);
171    if(fds != static_fds)
172        free(fds);
173}
174
175int _zz_mustwatch(char const *file)
176{
177#if defined HAVE_REGEX_H
178    if(has_include && regexec(&re_include, file, 0, NULL, 0) == REG_NOMATCH)
179        return 0; /* not included: ignore */
180
181    if(has_exclude && regexec(&re_exclude, file, 0, NULL, 0) != REG_NOMATCH)
182        return 0; /* excluded: ignore */
183#else
184    (void)file;
185#endif
186
187    return 1; /* default */
188}
189
190int _zz_iswatched(int fd)
191{
192    if(fd < 0 || fd >= maxfd || fds[fd] == -1)
193        return 0;
194
195    return 1;
196}
197
198void _zz_register(int fd)
199{
200    int i;
201
202    if(fd < 0 || fd > 65535 || (fd < maxfd && fds[fd] != -1))
203        return;
204
205#if 0
206    if(autoinc)
207        debug("using seed %li", (long int)seed);
208#endif
209
210    /* If filedescriptor is outside our bounds */
211    while(fd >= maxfd)
212    {
213        if(fds == static_fds)
214        {
215            fds = malloc(2 * maxfd * sizeof(*fds));
216            memcpy(fds, static_fds, maxfd * sizeof(*fds));
217        }
218        else
219            fds = realloc(fds, 2 * maxfd * sizeof(*fds));
220        for(i = maxfd; i < maxfd * 2; i++)
221            fds[i] = -1;
222        maxfd *= 2;
223    }
224           
225    /* Find an empty slot */
226    for(i = 0; i < nfiles; i++)
227        if(files[i].managed == 0)
228            break;
229
230    /* No slot found, allocate memory */
231    if(i == nfiles)
232    {
233        nfiles++;
234        if(files == static_files)
235        {
236            files = malloc(nfiles * sizeof(*files));
237            memcpy(files, static_files, nfiles * sizeof(*files));
238        }
239        else
240            files = realloc(files, nfiles * sizeof(*files));
241    }
242
243    files[i].managed = 1;
244    files[i].locked = 0;
245    files[i].pos = 0;
246    files[i].fuzz.seed = seed;
247    files[i].fuzz.ratio = _zz_getratio();
248    files[i].fuzz.cur = -1;
249#if defined HAVE_FGETLN
250    files[i].fuzz.tmp = NULL;
251#endif
252
253    if(autoinc)
254        seed++;
255
256    fds[fd] = i;
257}
258
259void _zz_unregister(int fd)
260{
261    if(fd < 0 || fd >= maxfd || fds[fd] == -1)
262        return;
263
264    files[fds[fd]].managed = 0;
265#if defined HAVE_FGETLN
266    if(files[fds[fd]].fuzz.tmp)
267        free(files[fds[fd]].fuzz.tmp);
268#endif
269
270    fds[fd] = -1;
271}
272
273void _zz_lock(int fd)
274{
275    if(fd < -1 || fd >= maxfd || fds[fd] == -1)
276        return;
277
278    if(fd == -1)
279        create_lock++;
280    else
281        files[fds[fd]].locked++;
282}
283
284void _zz_unlock(int fd)
285{
286    if(fd < -1 || fd >= maxfd || fds[fd] == -1)
287        return;
288
289    if(fd == -1)
290        create_lock--;
291    else
292        files[fds[fd]].locked--;
293}
294
295int _zz_islocked(int fd)
296{
297    if(fd < -1 || fd >= maxfd || fds[fd] == -1)
298        return 0;
299
300    if(fd == -1)
301        return create_lock;
302    else
303        return files[fds[fd]].locked;
304}
305
306long int _zz_getpos(int fd)
307{
308    if(fd < 0 || fd >= maxfd || fds[fd] == -1)
309        return 0;
310
311    return files[fds[fd]].pos;
312}
313
314void _zz_setpos(int fd, long int pos)
315{
316    if(fd < 0 || fd >= maxfd || fds[fd] == -1)
317        return;
318
319    files[fds[fd]].pos = pos;
320}
321
322void _zz_addpos(int fd, long int off)
323{
324    if(fd < 0 || fd >= maxfd || fds[fd] == -1)
325        return;
326
327    files[fds[fd]].pos += off;
328}
329
330struct fuzz *_zz_getfuzz(int fd)
331{
332    if(fd < 0 || fd >= maxfd || fds[fd] == -1)
333        return NULL;
334
335    return &files[fds[fd]].fuzz;
336}
337
Note: See TracBrowser for help on using the repository browser.