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

Last change on this file since 4827 was 4827, checked in by wisk, 9 years ago

on win32, use a named pipe and IOCP to read stdout, stderr and debugfd correctly.

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