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

Last change on this file since 4258 was 4258, checked in by Sam Hocevar, 10 years ago

Add a workaround for OS X ignoring RLIMIT_RSS / RLIMIT_AS and getrusage()
not filling in RSS information. After each memory allocation, we ask the
Mach for the RSS value.

  • 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        _zz_debugfd = atoi(tmp);
120
121    /* We need this as soon as possible */
122    _zz_mem_init();
123
124    tmp = getenv("ZZUF_SEED");
125    if(tmp && *tmp)
126        _zz_setseed(atol(tmp));
127
128    tmp = getenv("ZZUF_MINRATIO");
129    tmp2 = getenv("ZZUF_MAXRATIO");
130    if(tmp && *tmp && tmp2 && *tmp2)
131        _zz_setratio(atof(tmp), atof(tmp2));
132
133    tmp = getenv("ZZUF_AUTOINC");
134    if(tmp && *tmp == '1')
135        _zz_setautoinc();
136
137    tmp = getenv("ZZUF_BYTES");
138    if(tmp && *tmp)
139        _zz_bytes(tmp);
140
141    tmp = getenv("ZZUF_LIST");
142    if(tmp && *tmp)
143        _zz_list(tmp);
144
145    tmp = getenv("ZZUF_PORTS");
146    if(tmp && *tmp)
147        _zz_ports(tmp);
148
149    tmp = getenv("ZZUF_ALLOW");
150    if(tmp && *tmp)
151        _zz_allow(tmp);
152
153    tmp = getenv("ZZUF_DENY");
154    if(tmp && *tmp)
155        _zz_deny(tmp);
156
157    tmp = getenv("ZZUF_PROTECT");
158    if(tmp && *tmp)
159        _zz_protect(tmp);
160
161    tmp = getenv("ZZUF_REFUSE");
162    if(tmp && *tmp)
163        _zz_refuse(tmp);
164
165    tmp = getenv("ZZUF_INCLUDE");
166    if(tmp && *tmp)
167        _zz_include(tmp);
168
169    tmp = getenv("ZZUF_EXCLUDE");
170    if(tmp && *tmp)
171        _zz_exclude(tmp);
172
173    tmp = getenv("ZZUF_SIGNAL");
174    if(tmp && *tmp == '1')
175        _zz_signal = 1;
176
177    tmp = getenv("ZZUF_MEMORY");
178    if(tmp)
179        _zz_memory = atoll(tmp);
180
181    tmp = getenv("ZZUF_NETWORK");
182    if(tmp && *tmp == '1')
183        _zz_network = 1;
184
185    _zz_fd_init();
186    _zz_network_init();
187    _zz_sys_init();
188
189    tmp = getenv("ZZUF_STDIN");
190    if(tmp && *tmp == '1')
191        _zz_register(0);
192
193    _zz_ready = 1;
194
195    debug("libzzuf initialised for PID %li", (long int)getpid());
196}
197
198/**
199 * Library deinitialisation routine.
200 *
201 * Free all the memory allocated by libzzuf during its lifetime.
202 */
203void _zz_fini(void)
204{
205    if (!_zz_ready)
206        return;
207
208    debug("libzzuf finishing for PID %li", (long int)getpid());
209
210    _zz_fd_fini();
211    _zz_network_fini();
212
213    _zz_ready = 0;
214}
215
216#if defined HAVE_WINDOWS_H
217BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, PVOID impLoad)
218{
219    (void)hinst;   /* unused */
220    (void)impLoad; /* unused */
221
222    switch(reason)
223    {
224        case DLL_PROCESS_ATTACH:
225            _zz_init();
226            break;
227        case DLL_PROCESS_DETACH:
228            _zz_fini();
229            break;
230    }
231
232    return TRUE;
233}
234#endif
235
Note: See TracBrowser for help on using the repository browser.