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

Last change on this file since 4152 was 4152, checked in by sam, 5 years ago

Try to call _zz_init() as soon as possible. Otherwise, preloaded libraries
might be confused about half the calls actually working.

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