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

Last change on this file since 2345 was 2345, checked in by Sam Hocevar, 12 years ago
  • Change -M to mean mebibytes instead of megabytes, because I'm a fucking pedant.
  • Property svn:keywords set to Id
File size: 4.6 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 2345 2008-05-19 11:25:05Z 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#if defined HAVE_WINDOWS_H
28#   include <windows.h>
29#endif
30#if defined HAVE_PROCESS_H
31#   include <process.h>
32#endif
33#include <stdio.h>
34#include <sys/types.h>
35#if defined HAVE_UNISTD_H
36#   include <unistd.h>
37#endif
38#include <stdlib.h>
39#include <string.h>
40#include <fcntl.h>
41
42#include <stdarg.h>
43
44#include "libzzuf.h"
45#include "debug.h"
46#include "fd.h"
47#include "sys.h"
48#include "fuzz.h"
49
50/* Library initialisation shit */
51void _zz_init(void) __attribute__((constructor));
52void _zz_fini(void) __attribute__((destructor));
53#if defined HAVE_WINDOWS_H
54BOOL WINAPI DllMain(HINSTANCE, DWORD, PVOID);
55#endif
56
57/**
58 * Is libzzuf fully initialised?
59 */
60int   _zz_ready    = 0;
61
62/**
63 * The file descriptor used by libzzuf for communication with the main
64 * zzuf program in debug mode. Its value is set by the ZZUF_DEBUG
65 * environment variable.
66 */
67int   _zz_debugfd  = -1;
68
69/**
70 * If set to 1, this boolean variable will prevent the called application
71 * from installing signal handlers that would prevent it from really crashing.
72 * SDL applications often do that when not using SDL_INIT_NOPARACHUTE, for
73 * instance. Its value is set by the ZZUF_SIGNAL environment variable.
74 */
75int   _zz_signal   = 0;
76
77/**
78 * If set to a positive value, this value will indicate the maximum number
79 * of mebibytes (1 MiB = 1,048,576 bytes) that the called application will be
80 * allowed to allocate. Its value is set by the ZZUF_MEMORY environment
81 * variable.
82 */
83int   _zz_memory   = 0;
84
85/**
86 * If set to 1, this boolean will tell libzzuf to fuzz network file
87 * descriptors, too. Its value is set by the ZZUF_NETWORK environment
88 * variable.
89 */
90int   _zz_network  = 0;
91
92/**
93 * Library initialisation routine.
94 *
95 * This function reads all configuration variables put by zzuf in the
96 * called process's environment and initialises diversions for the three
97 * main function families: memory functions (initialised very early because
98 * other functions we need such as dlsym() require them), file descriptor
99 * functions and stream functions.
100 */
101void _zz_init(void)
102{
103    char *tmp, *tmp2;
104
105    /* We need this as soon as possible */
106    _zz_mem_init();
107
108    tmp = getenv("ZZUF_DEBUG");
109    if(tmp)
110        _zz_debugfd = atoi(tmp);
111
112    tmp = getenv("ZZUF_SEED");
113    if(tmp && *tmp)
114        _zz_setseed(atol(tmp));
115
116    tmp = getenv("ZZUF_MINRATIO");
117    tmp2 = getenv("ZZUF_MAXRATIO");
118    if(tmp && *tmp && tmp2 && *tmp2)
119        _zz_setratio(atof(tmp), atof(tmp2));
120
121    tmp = getenv("ZZUF_AUTOINC");
122    if(tmp && *tmp == '1')
123        _zz_setautoinc();
124
125    tmp = getenv("ZZUF_BYTES");
126    if(tmp && *tmp)
127        _zz_bytes(tmp);
128
129    tmp = getenv("ZZUF_LIST");
130    if(tmp && *tmp)
131        _zz_list(tmp);
132
133    tmp = getenv("ZZUF_PORTS");
134    if(tmp && *tmp)
135        _zz_ports(tmp);
136
137    tmp = getenv("ZZUF_PROTECT");
138    if(tmp && *tmp)
139        _zz_protect(tmp);
140
141    tmp = getenv("ZZUF_REFUSE");
142    if(tmp && *tmp)
143        _zz_refuse(tmp);
144
145    tmp = getenv("ZZUF_INCLUDE");
146    if(tmp && *tmp)
147        _zz_include(tmp);
148
149    tmp = getenv("ZZUF_EXCLUDE");
150    if(tmp && *tmp)
151        _zz_exclude(tmp);
152
153    tmp = getenv("ZZUF_SIGNAL");
154    if(tmp && *tmp == '1')
155        _zz_signal = 1;
156
157    tmp = getenv("ZZUF_MEMORY");
158    if(tmp && *tmp == '1')
159        _zz_memory = 1;
160
161    tmp = getenv("ZZUF_NETWORK");
162    if(tmp && *tmp == '1')
163        _zz_network = 1;
164
165    _zz_fd_init();
166    _zz_sys_init();
167
168    tmp = getenv("ZZUF_STDIN");
169    if(tmp && *tmp == '1')
170        _zz_register(0);
171
172    _zz_ready = 1;
173
174    debug("libzzuf initialised for PID %li", (long int)getpid());
175}
176
177/**
178 * Library deinitialisation routine.
179 *
180 * Free all the memory allocated by libzzuf during its lifetime.
181 */
182void _zz_fini(void)
183{
184    _zz_fd_fini();
185}
186
187#if defined HAVE_WINDOWS_H
188BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, PVOID impLoad)
189{
190    (void)hinst;   /* unused */
191    (void)impLoad; /* unused */
192
193    switch(reason)
194    {
195        case DLL_PROCESS_ATTACH:
196            _zz_init();
197            break;
198        case DLL_PROCESS_DETACH:
199            _zz_fini();
200            break;
201    }
202
203    return TRUE;
204}
205#endif
206
Note: See TracBrowser for help on using the repository browser.