source: zzuf/trunk/src/fuzz.c @ 1732

Last change on this file since 1732 was 1732, checked in by Sam Hocevar, 16 years ago
  • Fixed a few signed/unsigned confusions.
  • Property svn:keywords set to Id
File size: 6.7 KB
Line 
1/*
2 *  zzuf - general purpose fuzzer
3 *  Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: fuzz.c 1732 2007-02-01 22:20:48Z 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 *  fuzz.c: fuzz functions
17 */
18
19#include "config.h"
20
21#if defined HAVE_STDINT_H
22#   include <stdint.h>
23#elif defined HAVE_INTTYPES_H
24#   include <inttypes.h>
25#endif
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29
30#include "libzzuf.h"
31#include "debug.h"
32#include "random.h"
33#include "fuzz.h"
34#include "fd.h"
35
36#define MAGIC1 0x33ea84f7
37#define MAGIC2 0x783bc31f
38
39/* Fuzzing mode */
40static enum fuzzing
41{
42    FUZZING_XOR = 0, FUZZING_SET, FUZZING_UNSET
43}
44fuzzing;
45
46/* Per-offset byte protection */
47static int *ranges = NULL;
48static int ranges_static[512];
49
50/* Per-value byte protection */
51static int protect[256];
52static int refuse[256];
53
54/* Local prototypes */
55static void readchars(int *, char const *);
56
57extern void _zz_fuzzing(char const *mode)
58{
59    if(!strcmp(mode, "xor"))
60        fuzzing = FUZZING_XOR;
61    else if(!strcmp(mode, "set"))
62        fuzzing = FUZZING_SET;
63    else if(!strcmp(mode, "unset"))
64        fuzzing = FUZZING_UNSET;
65}
66
67void _zz_bytes(char const *list)
68{
69    char const *parser;
70    unsigned int i, chunks;
71
72    /* Count commas */
73    for(parser = list, chunks = 1; *parser; parser++)
74        if(*parser == ',')
75            chunks++;
76
77    /* TODO: free(ranges) if ranges != ranges_static */
78    if(chunks >= 256)
79        ranges = malloc((chunks + 1) * 2 * sizeof(unsigned int));
80    else
81        ranges = ranges_static;
82
83    /* Fill ranges list */
84    for(parser = list, i = 0; i < chunks; i++)
85    {
86        char const *comma = strchr(parser, ',');
87        char const *dash = strchr(parser, '-');
88
89        ranges[i * 2] = (dash == parser) ? 0 : atoi(parser);
90        if(dash && (dash + 1 == comma || dash[1] == '\0'))
91            ranges[i * 2 + 1] = ranges[i * 2]; /* special case */
92        else if(dash && (!comma || dash < comma))
93            ranges[i * 2 + 1] = atoi(dash + 1) + 1;
94        else
95            ranges[i * 2 + 1] = ranges[i * 2] + 1;
96        parser = comma + 1;
97    }
98
99    ranges[i * 2] = ranges[i * 2 + 1] = 0;
100}
101
102void _zz_protect(char const *list)
103{
104    readchars(protect, list);
105}
106
107void _zz_refuse(char const *list)
108{
109    readchars(refuse, list);
110}
111
112void _zz_fuzz(int fd, volatile uint8_t *buf, int64_t len)
113{
114    int64_t start, stop;
115    int64_t pos = _zz_getpos(fd);
116    struct fuzz *fuzz;
117    volatile uint8_t *aligned_buf;
118    int i, j, todo;
119
120#if 0
121    debug("fuzz(%i, %lli@%lli)", fd, (long long int)len,
122          (long long int)pos);
123#endif
124
125    aligned_buf = buf - pos;
126    fuzz = _zz_getfuzz(fd);
127
128    for(i = pos / CHUNKBYTES;
129        i < (pos + len + CHUNKBYTES - 1) / CHUNKBYTES;
130        i++)
131    {
132        /* Cache bitmask array */
133        if(fuzz->cur != (int)i)
134        {
135            uint32_t chunkseed = (i + (int)(fuzz->ratio * MAGIC1)) ^ MAGIC2;
136            _zz_srand(fuzz->seed ^ chunkseed);
137
138            memset(fuzz->data, 0, CHUNKBYTES);
139
140            /* Add some random dithering to handle ratio < 1.0/CHUNKBYTES */
141            todo = (int)((fuzz->ratio * (8 * CHUNKBYTES * 1000)
142                                             + _zz_rand(1000)) / 1000.0);
143            while(todo--)
144            {
145                unsigned int idx = _zz_rand(CHUNKBYTES);
146                uint8_t bit = (1 << _zz_rand(8));
147
148                fuzz->data[idx] ^= bit;
149            }
150
151            fuzz->cur = i;
152        }
153
154        /* Apply our bitmask array to the buffer */
155        start = (i * CHUNKBYTES > pos) ? i * CHUNKBYTES : pos;
156
157        stop = ((i + 1) * CHUNKBYTES < pos + len)
158              ? (i + 1) * CHUNKBYTES : pos + len;
159
160        for(j = start; j < stop; j++)
161        {
162            int *r;
163            uint8_t byte, fuzzbyte;
164
165            if(!ranges)
166                goto range_ok;
167
168            for(r = ranges; r[1]; r += 2)
169                if(j >= r[0] && (r[0] == r[1] || j < r[1]))
170                    goto range_ok;
171
172            continue; /* Not in one of the ranges, skip byte */
173
174        range_ok:
175            byte = aligned_buf[j];
176
177            if(protect[byte])
178                continue;
179
180            fuzzbyte = fuzz->data[j % CHUNKBYTES];
181
182            if(!fuzzbyte)
183                continue;
184
185            switch(fuzzing)
186            {
187            case FUZZING_XOR:
188                byte ^= fuzzbyte;
189                break;
190            case FUZZING_SET:
191                byte |= fuzzbyte;
192                break;
193            case FUZZING_UNSET:
194                byte &= ~fuzzbyte;
195                break;
196            }
197
198            if(refuse[byte])
199                continue;
200
201            aligned_buf[j] = byte;
202        }
203    }
204
205    /* Handle ungetc() */
206    if(fuzz->uflag)
207    {
208        fuzz->uflag = 0;
209        if(fuzz->upos == pos)
210            buf[0] = fuzz->uchar;
211    }
212}
213
214static void readchars(int *table, char const *list)
215{
216    static char const hex[] = "0123456789abcdef0123456789ABCDEF";
217    char const *tmp;
218    int a, b;
219
220    memset(table, 0, 256 * sizeof(int));
221
222    for(tmp = list, a = b = -1; *tmp; tmp++)
223    {
224        int new;
225
226        if(*tmp == '\\' && tmp[1] == '\0')
227            new = '\\';
228        else if(*tmp == '\\')
229        {
230            tmp++;
231            if(*tmp == 'n')
232                new = '\n';
233            else if(*tmp == 'r')
234                new = '\r';
235            else if(*tmp == 't')
236                new = '\t';
237            else if(tmp[0] >= '0' && tmp[0] <= '7' && tmp[1] >= '0'
238                     && tmp[1] <= '7' && tmp[2] >= '0' && tmp[2] <= '7')
239            {
240                new = tmp[2] - '0';
241                new |= (int)(tmp[1] - '0') << 3;
242                new |= (int)(tmp[0] - '0') << 6;
243                tmp += 2;
244            }
245            else if((*tmp == 'x' || *tmp == 'X')
246                     && tmp[1] && strchr(hex, tmp[1])
247                     && tmp[2] && strchr(hex, tmp[2]))
248            {
249                new = ((strchr(hex, tmp[1]) - hex) & 0xf) << 4;
250                new |= (strchr(hex, tmp[2]) - hex) & 0xf;
251                tmp += 2;
252            }
253            else
254                new = (unsigned char)*tmp; /* XXX: OK for \\, but what else? */
255        }
256        else
257            new = (unsigned char)*tmp;
258
259        if(a != -1 && b == '-' && a <= new)
260        {
261            while(a <= new)
262                table[a++] = 1;
263            a = b = -1;
264        }
265        else
266        {
267            if(a != -1)
268                table[a] = 1;
269            a = b;
270            b = new;
271        }
272    }
273
274    if(a != -1)
275        table[a] = 1;
276    if(b != -1)
277        table[b] = 1;
278}
279
Note: See TracBrowser for help on using the repository browser.