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

Last change on this file since 4672 was 4672, checked in by Sam Hocevar, 11 years ago

Get the debug channel to work on Win32.

  • Property svn:keywords set to Id
File size: 5.3 KB
Line 
1/*
2 *  zzuf - general purpose fuzzer
3 *  Copyright (c) 2006-2010 Sam Hocevar <sam@hocevar.net>
4 *                All Rights Reserved
5 *
6 *  This program is free software. It comes without any warranty, to
7 *  the extent permitted by applicable law. You can redistribute it
8 *  and/or modify it under the terms of the Do What The Fuck You Want
9 *  To Public License, Version 2, as published by Sam Hocevar. See
10 *  http://sam.zoy.org/wtfpl/COPYING for more details.
11 */
12
13/*
14 *  libzzuf.c: preloaded wrapper library
15 */
16
17#include "config.h"
18#define _GNU_SOURCE
19
20#if defined HAVE_STDINT_H
21#   include <stdint.h>
22#elif defined HAVE_INTTYPES_H
23#   include <inttypes.h>
24#endif
25#if defined HAVE_WINDOWS_H
26#   include <windows.h>
27#endif
28#if defined HAVE_PROCESS_H
29#   include <process.h>
30#endif
31#include <stdio.h>
32#include <sys/types.h>
33#if defined HAVE_UNISTD_H
34#   include <unistd.h>
35#endif
36#include <stdlib.h>
37#include <string.h>
38#include <fcntl.h>
39
40#include <stdarg.h>
41
42#include "libzzuf.h"
43#include "debug.h"
44#include "fd.h"
45#include "network.h"
46#include "sys.h"
47#include "fuzz.h"
48
49#if defined HAVE_WINDOWS_H
50BOOL WINAPI DllMain(HINSTANCE, DWORD, PVOID);
51#endif
52
53/**
54 * Is libzzuf fully initialised?
55 */
56int _zz_ready = 0;
57
58/**
59 * The debugging level that libzzuf should use. 0 means no debugging,
60 * 1 means minimal debugging, 2 means verbose debugging. Its value is set
61 * by the ZZUF_DEBUG environment variable.
62 */
63int _zz_debuglevel = 0;
64
65/**
66 * The file descriptor used by libzzuf for communication with the main
67 * zzuf program in debug mode. Its value is set by the ZZUF_DEBUGFD
68 * environment variable.
69 */
70int _zz_debugfd = -1;
71
72/**
73 * If set to 1, this boolean variable will prevent the called application
74 * from installing signal handlers that would prevent it from really crashing.
75 * SDL applications often do that when not using SDL_INIT_NOPARACHUTE, for
76 * instance. Its value is set by the ZZUF_SIGNAL environment variable.
77 */
78int _zz_signal = 0;
79
80/**
81 * If set to a positive value, this value will indicate the maximum number
82 * of mebibytes (1 MiB = 1,048,576 bytes) that the called application will be
83 * allowed to allocate. Its value is set by the ZZUF_MEMORY environment
84 * variable.
85 */
86uint64_t _zz_memory = 0;
87
88/**
89 * If set to 1, this boolean will tell libzzuf to fuzz network file
90 * descriptors, too. Its value is set by the ZZUF_NETWORK environment
91 * variable.
92 */
93int _zz_network = 0;
94
95/**
96 * Library initialisation routine.
97 *
98 * This function reads all configuration variables put by zzuf in the
99 * called process's environment and initialises diversions for the three
100 * main function families: memory functions (initialised very early because
101 * other functions we need such as dlsym() require them), file descriptor
102 * functions and stream functions.
103 */
104void _zz_init(void)
105{
106    static int initializing = 0;
107    char *tmp, *tmp2;
108
109    /* Make sure we don't get initialised more than once */
110    if (initializing++)
111        return;
112
113    tmp = getenv("ZZUF_DEBUG");
114    if(tmp)
115        _zz_debuglevel = atoi(tmp);
116
117    tmp = getenv("ZZUF_DEBUGFD");
118    if(tmp)
119#if defined _WIN32
120        _zz_debugfd = _open_osfhandle((HANDLE)atoi(tmp));
121#else
122        _zz_debugfd = atoi(tmp);
123#endif
124
125    /* We need this as soon as possible */
126    _zz_mem_init();
127
128    tmp = getenv("ZZUF_SEED");
129    if(tmp && *tmp)
130        _zz_setseed(atol(tmp));
131
132    tmp = getenv("ZZUF_MINRATIO");
133    tmp2 = getenv("ZZUF_MAXRATIO");
134    if(tmp && *tmp && tmp2 && *tmp2)
135        _zz_setratio(atof(tmp), atof(tmp2));
136
137    tmp = getenv("ZZUF_AUTOINC");
138    if(tmp && *tmp == '1')
139        _zz_setautoinc();
140
141    tmp = getenv("ZZUF_BYTES");
142    if(tmp && *tmp)
143        _zz_bytes(tmp);
144
145    tmp = getenv("ZZUF_LIST");
146    if(tmp && *tmp)
147        _zz_list(tmp);
148
149    tmp = getenv("ZZUF_PORTS");
150    if(tmp && *tmp)
151        _zz_ports(tmp);
152
153    tmp = getenv("ZZUF_ALLOW");
154    if(tmp && *tmp)
155        _zz_allow(tmp);
156
157    tmp = getenv("ZZUF_DENY");
158    if(tmp && *tmp)
159        _zz_deny(tmp);
160
161    tmp = getenv("ZZUF_PROTECT");
162    if(tmp && *tmp)
163        _zz_protect(tmp);
164
165    tmp = getenv("ZZUF_REFUSE");
166    if(tmp && *tmp)
167        _zz_refuse(tmp);
168
169    tmp = getenv("ZZUF_INCLUDE");
170    if(tmp && *tmp)
171        _zz_include(tmp);
172
173    tmp = getenv("ZZUF_EXCLUDE");
174    if(tmp && *tmp)
175        _zz_exclude(tmp);
176
177    tmp = getenv("ZZUF_SIGNAL");
178    if(tmp && *tmp == '1')
179        _zz_signal = 1;
180
181    tmp = getenv("ZZUF_MEMORY");
182    if(tmp)
183        _zz_memory = atoi(tmp);
184
185    tmp = getenv("ZZUF_NETWORK");
186    if(tmp && *tmp == '1')
187        _zz_network = 1;
188
189    _zz_fd_init();
190    _zz_network_init();
191    _zz_sys_init();
192
193    tmp = getenv("ZZUF_STDIN");
194    if(tmp && *tmp == '1')
195        _zz_register(0);
196
197    _zz_ready = 1;
198
199    debug("libzzuf initialised for PID %li", (long int)getpid());
200}
201
202/**
203 * Library deinitialisation routine.
204 *
205 * Free all the memory allocated by libzzuf during its lifetime.
206 */
207void _zz_fini(void)
208{
209    if (!_zz_ready)
210        return;
211
212    debug("libzzuf finishing for PID %li", (long int)getpid());
213
214    _zz_fd_fini();
215    _zz_network_fini();
216
217    _zz_ready = 0;
218}
219
220#if defined HAVE_WINDOWS_H
221BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, PVOID impLoad)
222{
223    (void)hinst;   /* unused */
224    (void)impLoad; /* unused */
225
226    switch(reason)
227    {
228        case DLL_PROCESS_ATTACH:
229            _zz_init();
230            break;
231        case DLL_PROCESS_DETACH:
232            _zz_fini();
233            break;
234    }
235
236    return TRUE;
237}
238#endif
239
Note: See TracBrowser for help on using the repository browser.