source: zzuf/trunk/src/libzzuf.c @ 1523

Last change on this file since 1523 was 1523, checked in by Sam Hocevar, 14 years ago
  • Improved zfd_register() and zfd_* function performances.
  • Changed function names here and there.
  • Property svn:keywords set to Id
File size: 4.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: libzzuf.c 1523 2006-12-28 20:22:32Z 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 <stdio.h>
28#include <unistd.h>
29#include <stdlib.h>
30#include <fcntl.h>
31#include <regex.h>
32
33#include <stdarg.h>
34#include <dlfcn.h>
35
36#include "libzzuf.h"
37#include "debug.h"
38#include "load.h"
39
40/* Global variables */
41int   _zz_ready    = 0;
42int   _zz_hasdebug = 0;
43int   _zz_seed     = 0;
44float _zz_ratio    = 0.004f;
45regex_t * _zz_include = NULL;
46regex_t * _zz_exclude = NULL;
47
48/* Library initialisation shit */
49void _zz_init(void)
50{
51    char *tmp;
52
53    tmp = getenv("ZZUF_DEBUG");
54    if(tmp && *tmp)
55        _zz_hasdebug = 1;
56
57    tmp = getenv("ZZUF_SEED");
58    if(tmp && *tmp)
59        _zz_seed = atol(tmp);
60
61    tmp = getenv("ZZUF_RATIO");
62    if(tmp && *tmp)
63        _zz_ratio = atof(tmp);
64    if(_zz_ratio < 0.0f)
65        _zz_ratio = 0.0f;
66    else if(_zz_ratio > 5.0f)
67        _zz_ratio = 5.0f;
68
69    tmp = getenv("ZZUF_INCLUDE");
70    if(tmp && *tmp)
71    {
72        _zz_include = malloc(sizeof(*_zz_include));
73        regcomp(_zz_include, tmp, 0);
74    }
75
76    tmp = getenv("ZZUF_EXCLUDE");
77    if(tmp && *tmp)
78    {
79        _zz_exclude = malloc(sizeof(*_zz_exclude));
80        regcomp(_zz_exclude, tmp, 0);
81    }
82
83    zfd_init();
84
85    _zz_load_fd();
86    _zz_load_stream();
87
88    _zz_ready = 1;
89
90    debug("libzzuf initialised");
91}
92
93/* Deinitialisation */
94void _zz_fini(void)
95{
96    zfd_fini();
97}
98
99/* File descriptor stuff */
100struct files
101{
102    int managed;
103    uint64_t seed;
104    uint64_t pos;
105    /* Public stuff */
106    struct fuzz fuzz;
107}
108*files;
109
110int *fds;
111
112int maxfd, nfiles;
113
114void zfd_init(void)
115{
116    files = NULL;
117    nfiles = 0;
118
119    /* Start with one fd in the lookup table */
120    fds = malloc(1 * sizeof(int));
121    for(maxfd = 0; maxfd < 1; maxfd++)
122        fds[maxfd] = -1;
123}
124
125void zfd_fini(void)
126{
127    int i;
128
129    for(i = 0; i < maxfd; i++)
130    {
131        if(!files[fds[i]].managed)
132            continue;
133
134        /* XXX: What are we supposed to do? If filedescriptors weren't
135         * closed properly, there's a leak, but it's not our problem. */
136    }
137
138    free(files);
139    free(fds);
140}
141
142int zfd_ismanaged(int fd)
143{
144    if(fd < 0 || fd >= maxfd || fds[fd] == -1)
145        return 0;
146
147    return 1;
148}
149
150void zfd_register(int fd)
151{
152    int i;
153
154    if(fd < 0 || fd > 65535 || (fd < maxfd && fds[fd] != -1))
155        return;
156
157    while(fd >= maxfd)
158    {
159        fds = realloc(fds, 2 * maxfd * sizeof(int));
160        for(i = maxfd; i < maxfd * 2; i++)
161            fds[i] = -1;
162        maxfd *= 2;
163    }
164           
165    /* Find an empty slot */
166    for(i = 0; i < nfiles; i++)
167        if(files[i].managed == 0)
168            break;
169
170    /* No slot found, allocate memory */
171    if(i == nfiles)
172    {
173        nfiles++;
174        files = realloc(files, nfiles * sizeof(struct files));
175    }
176
177    files[i].managed = 1;
178    files[i].pos = 0;
179    files[i].fuzz.cur = -1;
180    files[i].fuzz.data = malloc(CHUNKBYTES);
181
182    fds[fd] = i;
183}
184
185void zfd_unregister(int fd)
186{
187    if(fd < 0 || fd >= maxfd || fds[fd] == -1)
188        return;
189
190    files[fds[fd]].managed = 0;
191    free(files[fds[fd]].fuzz.data);
192
193    fds[fd] = -1;
194}
195
196long int zfd_getpos(int fd)
197{
198    if(fd < 0 || fd >= maxfd || fds[fd] == -1)
199        return 0;
200
201    return files[fds[fd]].pos;
202}
203
204void zfd_setpos(int fd, long int pos)
205{
206    if(fd < 0 || fd >= maxfd || fds[fd] == -1)
207        return;
208
209    files[fds[fd]].pos = pos;
210}
211
212void zfd_addpos(int fd, long int off)
213{
214    if(fd < 0 || fd >= maxfd || fds[fd] == -1)
215        return;
216
217    files[fds[fd]].pos += off;
218}
219
220struct fuzz *zfd_getfuzz(int fd)
221{
222    if(fd < 0 || fd >= maxfd || fds[fd] == -1)
223        return NULL;
224
225    return &files[fds[fd]].fuzz;
226}
227
Note: See TracBrowser for help on using the repository browser.