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

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

Add a second level of debugging for slightly less useful information.

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