diff --git a/src/common/common.h b/src/common/common.h index 03df675..0142f1f 100644 --- a/src/common/common.h +++ b/src/common/common.h @@ -10,6 +10,8 @@ * See http://www.wtfpl.net/ for more details. */ +#pragma once + /* * common.h: default fuzzing settings */ @@ -36,7 +38,7 @@ /* We use file descriptor 17 as the debug channel on Unix */ #define DEBUG_FILENO 17 -struct fuzz +struct fuzz_context { uint32_t seed; double ratio; @@ -48,3 +50,5 @@ struct fuzz uint8_t data[CHUNKBYTES]; }; +typedef struct fuzz_context fuzz_context_t; + diff --git a/src/common/fd.c b/src/common/fd.c index 12881e4..020de92 100644 --- a/src/common/fd.c +++ b/src/common/fd.c @@ -68,14 +68,14 @@ static struct files int managed, locked, active, already_fuzzed; int64_t pos, already_pos; /* Public stuff */ - struct fuzz fuzz; + fuzz_context_t fuzz; } *files, static_files[STATIC_FILES]; static int *fds, static_fds[STATIC_FILES]; static int maxfd, nfiles; /* Spinlock. This variable protects the fds variable. */ -static zz_mutex fds_mutex = 0; +static zzuf_mutex_t fds_mutex = 0; /* Create lock. This lock variable is used to disable file descriptor * creation wrappers. For instance on Mac OS X, fopen() calls open() @@ -88,7 +88,7 @@ static double minratio = DEFAULT_RATIO; static double maxratio = DEFAULT_RATIO; static int autoinc = 0; -void _zz_include(char const *regex) +void zzuf_include_pattern(char const *regex) { #if defined HAVE_REGEX_H if (regcomp(&re_include, regex, REG_EXTENDED) == 0) @@ -98,7 +98,7 @@ void _zz_include(char const *regex) #endif } -void _zz_exclude(char const *regex) +void zzuf_exclude_pattern(char const *regex) { #if defined HAVE_REGEX_H if (regcomp(&re_exclude, regex, REG_EXTENDED) == 0) @@ -113,12 +113,12 @@ void _zz_list(char const *fdlist) list = _zz_allocrange(fdlist, static_list); } -void _zz_setseed(int32_t s) +void zzuf_set_seed(int32_t s) { seed = s; } -void _zz_setratio(double r0, double r1) +void zzuf_set_ratio(double r0, double r1) { if (r0 == 0.0 && r1 == 0.0) { @@ -132,7 +132,7 @@ void _zz_setratio(double r0, double r1) maxratio = minratio; } -double _zz_getratio(void) +double zzuf_get_ratio(void) { uint8_t const shuffle[16] = { 0, 12, 2, 10, @@ -158,7 +158,7 @@ double _zz_getratio(void) return exp(cur); } -void _zz_setautoinc(void) +void zzuf_set_auto_increment(void) { autoinc = 1; } @@ -236,7 +236,7 @@ int _zz_mustwatchw(wchar_t const *file) int _zz_iswatched(int fd) { int ret = 0; - zz_lock(&fds_mutex); + zzuf_mutex_lock(&fds_mutex); if (fd < 0 || fd >= maxfd || fds[fd] == -1) goto early_exit; @@ -244,7 +244,7 @@ int _zz_iswatched(int fd) ret = 1; early_exit: - zz_unlock(&fds_mutex); + zzuf_mutex_unlock(&fds_mutex); return ret; } @@ -252,7 +252,7 @@ void _zz_register(int fd) { int i; - zz_lock(&fds_mutex); + zzuf_mutex_lock(&fds_mutex); if (fd < 0 || fd > 65535 || (fd < maxfd && fds[fd] != -1)) goto early_exit; @@ -299,7 +299,7 @@ void _zz_register(int fd) files[i].locked = 0; files[i].pos = 0; files[i].fuzz.seed = seed; - files[i].fuzz.ratio = _zz_getratio(); + files[i].fuzz.ratio = zzuf_get_ratio(); files[i].fuzz.cur = -1; #if defined HAVE_FGETLN files[i].fuzz.tmp = NULL; @@ -322,192 +322,180 @@ void _zz_register(int fd) fds[fd] = i; early_exit: - zz_unlock(&fds_mutex); + zzuf_mutex_unlock(&fds_mutex); } void _zz_unregister(int fd) { - zz_lock(&fds_mutex); + zzuf_mutex_lock(&fds_mutex); - if (fd < 0 || fd >= maxfd || fds[fd] == -1) - goto early_exit; - - files[fds[fd]].managed = 0; + if (fd >= 0 && fd < maxfd && fds[fd] != -1) + { + files[fds[fd]].managed = 0; #if defined HAVE_FGETLN - if (files[fds[fd]].fuzz.tmp) - free(files[fds[fd]].fuzz.tmp); + if (files[fds[fd]].fuzz.tmp) + free(files[fds[fd]].fuzz.tmp); #endif - fds[fd] = -1; + fds[fd] = -1; + } -early_exit: - zz_unlock(&fds_mutex); + zzuf_mutex_unlock(&fds_mutex); } void _zz_lockfd(int fd) { - zz_lock(&fds_mutex); + zzuf_mutex_lock(&fds_mutex); - if (fd < -1 || fd >= maxfd || fds[fd] == -1) - goto early_exit; + if (fd >= 0 && fd < maxfd && fds[fd] != -1) + { + if (fd == -1) + ++create_lock; + else + ++files[fds[fd]].locked; + } - if (fd == -1) - create_lock++; - else - files[fds[fd]].locked++; - -early_exit: - zz_unlock(&fds_mutex); + zzuf_mutex_unlock(&fds_mutex); } void _zz_unlock(int fd) { - zz_lock(&fds_mutex); + zzuf_mutex_lock(&fds_mutex); - if (fd < -1 || fd >= maxfd || fds[fd] == -1) - goto early_exit; + if (fd >= 0 && fd < maxfd && fds[fd] != -1) + { + if (fd == -1) + --create_lock; + else + --files[fds[fd]].locked; + } - if (fd == -1) - create_lock--; - else - files[fds[fd]].locked--; - -early_exit: - zz_unlock(&fds_mutex); + zzuf_mutex_unlock(&fds_mutex); } int _zz_islocked(int fd) { int ret = 0; - zz_lock(&fds_mutex); + zzuf_mutex_lock(&fds_mutex); - if (fd < -1 || fd >= maxfd || fds[fd] == -1) - goto early_exit; + if (fd >= 0 && fd < maxfd && fds[fd] != -1) + { + if (fd == -1) + ret = create_lock; + else + ret = files[fds[fd]].locked; + } - if (fd == -1) - ret = create_lock; - else - ret = files[fds[fd]].locked; - -early_exit: - zz_unlock(&fds_mutex); + zzuf_mutex_unlock(&fds_mutex); return ret; } int _zz_isactive(int fd) { int ret = 1; - zz_lock(&fds_mutex); + zzuf_mutex_lock(&fds_mutex); - if (fd < 0 || fd >= maxfd || fds[fd] == -1) - goto early_exit; + if (fd >= 0 && fd < maxfd && fds[fd] != -1) + { + ret = files[fds[fd]].active; + } - ret = files[fds[fd]].active; - -early_exit: - zz_unlock(&fds_mutex); + zzuf_mutex_unlock(&fds_mutex); return ret; } int64_t _zz_getpos(int fd) { int64_t ret = 0; - zz_lock(&fds_mutex); + zzuf_mutex_lock(&fds_mutex); - if (fd < 0 || fd >= maxfd || fds[fd] == -1) - goto early_exit; + if (fd >= 0 && fd < maxfd && fds[fd] != -1) + { + ret = files[fds[fd]].pos; + } - ret = files[fds[fd]].pos; - -early_exit: - zz_unlock(&fds_mutex); + zzuf_mutex_unlock(&fds_mutex); return ret; } void _zz_setpos(int fd, int64_t pos) { - zz_lock(&fds_mutex); + zzuf_mutex_lock(&fds_mutex); - if (fd < 0 || fd >= maxfd || fds[fd] == -1) - goto early_exit; + if (fd >= 0 && fd < maxfd && fds[fd] != -1) + { + files[fds[fd]].pos = pos; + } - files[fds[fd]].pos = pos; - -early_exit: - zz_unlock(&fds_mutex); + zzuf_mutex_unlock(&fds_mutex); } void _zz_addpos(int fd, int64_t off) { - zz_lock(&fds_mutex); + zzuf_mutex_lock(&fds_mutex); - if (fd < 0 || fd >= maxfd || fds[fd] == -1) - goto early_exit; + if (fd >= 0 && fd < maxfd && fds[fd] != -1) + { + files[fds[fd]].pos += off; + } - files[fds[fd]].pos += off; - -early_exit: - zz_unlock(&fds_mutex); + zzuf_mutex_unlock(&fds_mutex); } void _zz_setfuzzed(int fd, int count) { - zz_lock(&fds_mutex); - - if (fd < 0 || fd >= maxfd || fds[fd] == -1) - goto early_exit; - - /* FIXME: what if we just slightly advanced? */ - if (files[fds[fd]].pos == files[fds[fd]].already_pos - && count <= files[fds[fd]].already_fuzzed) - goto early_exit; + zzuf_mutex_lock(&fds_mutex); + if (fd >= 0 && fd < maxfd && fds[fd] != -1) + { + /* FIXME: what if we just slightly advanced? */ + if (files[fds[fd]].pos != files[fds[fd]].already_pos + || count > files[fds[fd]].already_fuzzed) + { #if defined LIBZZUF - debug2("setfuzzed(%i, %i)", fd, count); + debug2("setfuzzed(%i, %i)", fd, count); #endif - files[fds[fd]].already_pos = files[fds[fd]].pos; - files[fds[fd]].already_fuzzed = count; + files[fds[fd]].already_pos = files[fds[fd]].pos; + files[fds[fd]].already_fuzzed = count; + } + } -early_exit: - zz_unlock(&fds_mutex); + zzuf_mutex_unlock(&fds_mutex); } int _zz_getfuzzed(int fd) { int ret = 0; - zz_lock(&fds_mutex); + zzuf_mutex_lock(&fds_mutex); - if (fd < 0 || fd >= maxfd || fds[fd] == -1) - goto early_exit; + if (fd >= 0 && fd < maxfd && fds[fd] != -1) + { + if (files[fds[fd]].pos >= files[fds[fd]].already_pos + && files[fds[fd]].pos < files[fds[fd]].already_pos + + files[fds[fd]].already_fuzzed) + ret = (int)(files[fds[fd]].already_fuzzed + + files[fds[fd]].already_pos + - files[fds[fd]].pos); + } - if (files[fds[fd]].pos < files[fds[fd]].already_pos) - goto early_exit; - - if (files[fds[fd]].pos >= files[fds[fd]].already_pos - + files[fds[fd]].already_fuzzed) - goto early_exit; - - ret = (int)(files[fds[fd]].already_fuzzed + files[fds[fd]].already_pos - - files[fds[fd]].pos); - -early_exit: - zz_unlock(&fds_mutex); + zzuf_mutex_unlock(&fds_mutex); return ret; } -struct fuzz *_zz_getfuzz(int fd) +/* FIXME: this is not safe because once we unlock fds_mutex the structure + * may become invalid */ +fuzz_context_t *_zz_getfuzz(int fd) { - struct fuzz *ret = NULL; - zz_lock(&fds_mutex); + fuzz_context_t *ret = NULL; + zzuf_mutex_lock(&fds_mutex); - if (fd < 0 || fd >= maxfd || fds[fd] == -1) - goto early_exit; + if (fd >= 0 && fd < maxfd && fds[fd] != -1) + { + ret = &files[fds[fd]].fuzz; + } - ret = &files[fds[fd]].fuzz; - -early_exit: - zz_unlock(&fds_mutex); + zzuf_mutex_unlock(&fds_mutex); return ret; } diff --git a/src/common/fd.h b/src/common/fd.h index c7fc373..51a828b 100644 --- a/src/common/fd.h +++ b/src/common/fd.h @@ -17,15 +17,17 @@ * fd.h: file descriptor functions */ +#include "common/common.h" + #include #include -extern void _zz_include(char const *); -extern void _zz_exclude(char const *); -extern void _zz_setseed(int32_t); -extern void _zz_setratio(double, double); -extern double _zz_getratio(void); -extern void _zz_setautoinc(void); +extern void zzuf_include_pattern(char const *); +extern void zzuf_exclude_pattern(char const *); +extern void zzuf_set_seed(int32_t); +extern void zzuf_set_ratio(double, double); +extern double zzuf_get_ratio(void); +extern void zzuf_set_auto_increment(void); extern void _zz_fd_init(void); extern void _zz_fd_fini(void); @@ -44,5 +46,5 @@ extern void _zz_addpos(int, int64_t); extern void _zz_setfuzzed(int, int); extern int _zz_getfuzzed(int); -extern struct fuzz *_zz_getfuzz(int); +extern fuzz_context_t *_zz_getfuzz(int); diff --git a/src/common/fuzz.c b/src/common/fuzz.c index 113a07a..54074e5 100644 --- a/src/common/fuzz.c +++ b/src/common/fuzz.c @@ -54,7 +54,7 @@ static unsigned char protect[256]; static unsigned char refuse[256]; /* Local prototypes */ -static void readchars(unsigned char *, char const *); +static void add_char_range(unsigned char *, char const *); extern void _zz_fuzzing(char const *mode) { @@ -72,14 +72,14 @@ void _zz_bytes(char const *list) ranges = _zz_allocrange(list, static_ranges); } -void _zz_protect(char const *list) +void zzuf_protect_range(char const *list) { - readchars(protect, list); + add_char_range(protect, list); } -void _zz_refuse(char const *list) +void zzuf_refuse_range(char const *list) { - readchars(refuse, list); + add_char_range(refuse, list); } void _zz_fuzz(int fd, volatile uint8_t *buf, int64_t len) @@ -92,7 +92,7 @@ void _zz_fuzz(int fd, volatile uint8_t *buf, int64_t len) #endif volatile uint8_t *aligned_buf = buf - pos; - struct fuzz *fuzz = _zz_getfuzz(fd); + fuzz_context_t *fuzz = _zz_getfuzz(fd); for (int64_t i = pos / CHUNKBYTES; i < (pos + len + CHUNKBYTES - 1) / CHUNKBYTES; @@ -109,17 +109,17 @@ void _zz_fuzz(int fd, volatile uint8_t *buf, int64_t len) chunkseed ^= fuzz->seed; chunkseed += (uint32_t)(i * MAGIC3); - _zz_srand(chunkseed); + zzuf_srand(chunkseed); memset(fuzz->data, 0, CHUNKBYTES); /* Add some random dithering to handle ratio < 1.0/CHUNKBYTES */ int todo = (int)((fuzz->ratio * (8 * CHUNKBYTES) * 1000000.0 - + _zz_rand(1000000)) / 1000000.0); + + zzuf_rand(1000000)) / 1000000.0); while (todo--) { - unsigned int idx = _zz_rand(CHUNKBYTES); - uint8_t bit = (1 << _zz_rand(8)); + unsigned int idx = zzuf_rand(CHUNKBYTES); + uint8_t bit = (1 << zzuf_rand(8)); fuzz->data[idx] ^= bit; } @@ -149,7 +149,7 @@ void _zz_fuzz(int fd, volatile uint8_t *buf, int64_t len) if (!fuzzbyte) continue; - switch(fuzzing) + switch (fuzzing) { case FUZZING_XOR: byte ^= fuzzbyte; @@ -178,7 +178,7 @@ void _zz_fuzz(int fd, volatile uint8_t *buf, int64_t len) } } -static void readchars(unsigned char *table, char const *list) +static void add_char_range(unsigned char *table, char const *list) { static char const hex[] = "0123456789abcdef0123456789ABCDEF"; char const *tmp; diff --git a/src/common/fuzz.h b/src/common/fuzz.h index 9334891..2b35fc8 100644 --- a/src/common/fuzz.h +++ b/src/common/fuzz.h @@ -17,8 +17,8 @@ extern void _zz_fuzzing(char const *); extern void _zz_bytes(char const *); extern void _zz_list(char const *); -extern void _zz_protect(char const *); -extern void _zz_refuse(char const *); +extern void zzuf_protect_range(char const *); +extern void zzuf_refuse_range(char const *); extern void _zz_fuzz(int, volatile uint8_t *, int64_t); diff --git a/src/common/random.c b/src/common/random.c index 4344405..9267308 100644 --- a/src/common/random.c +++ b/src/common/random.c @@ -27,12 +27,12 @@ static unsigned long ctx = 1; -void _zz_srand(uint32_t seed) +void zzuf_srand(uint32_t seed) { ctx = (seed ^ 0x12345678); } -uint32_t _zz_rand(uint32_t max) +uint32_t zzuf_rand(uint32_t max) { /* Could be better, but do we care? */ long hi = ctx / 12773L; diff --git a/src/common/random.h b/src/common/random.h index 8dc4c50..35d91e4 100644 --- a/src/common/random.h +++ b/src/common/random.h @@ -14,6 +14,6 @@ * random.h: pseudorandom number generator */ -void _zz_srand(uint32_t); -uint32_t _zz_rand(uint32_t); +void zzuf_srand(uint32_t); +uint32_t zzuf_rand(uint32_t); diff --git a/src/common/ranges.c b/src/common/ranges.c index 7347167..9714a56 100644 --- a/src/common/ranges.c +++ b/src/common/ranges.c @@ -32,7 +32,7 @@ * If more than 256 slots are required, new memory is allocated, otherwise * the static array static_ranges is used. It is the caller's duty to call * free() if the returned value is not static_ranges. */ -int64_t *_zz_allocrange(char const *list, int64_t *static_ranges) +int64_t *_zz_allocrange(char const *list, int64_t static_ranges[256]) { char const *parser; int64_t *ranges; diff --git a/src/common/ranges.h b/src/common/ranges.h index 09c88ef..411171f 100644 --- a/src/common/ranges.h +++ b/src/common/ranges.h @@ -14,6 +14,6 @@ * ranges.c: range handling helper functions */ -int64_t *_zz_allocrange(char const *, int64_t *); +int64_t *_zz_allocrange(char const *, int64_t[256]); int _zz_isinrange(int64_t, int64_t const *); diff --git a/src/libzzuf/debug.c b/src/libzzuf/debug.c index 68fc9cc..ad027e7 100644 --- a/src/libzzuf/debug.c +++ b/src/libzzuf/debug.c @@ -63,23 +63,20 @@ static void mydebug(char const *format, va_list args); } while (0) /* Temporary buffer for deferred output */ -static zz_mutex debug_mutex = 0; +static zzuf_mutex_t debug_mutex = 0; static char debug_buffer[BUFSIZ]; static size_t debug_count = 1; #ifdef _WIN32 - -CRITICAL_SECTION _zz_pipe_cs; /* Initialized in DllMain */ - -void _zz_debug(char const *format, ...) +void zzuf_debug(char const *format, ...) { va_list args; char buf[0x100]; DWORD written; va_start(args, format); - //if (_zz_debuglevel >= 1) // LATER: + //if (g_debug_level >= 1) // LATER: { - HANDLE dbg_hdl = (HANDLE)_get_osfhandle(_zz_debugfd); + HANDLE dbg_hdl = (HANDLE)_get_osfhandle(g_debug_fd); int ret = _vsnprintf(buf, sizeof(buf), format, args); if (ret <= 0) @@ -93,23 +90,23 @@ void _zz_debug(char const *format, ...) ret = (int)sizeof(buf) - 1; buf[ret++] = '\n'; - EnterCriticalSection(&_zz_pipe_cs); + zzuf_mutex_lock(&debug_mutex); WriteFile(dbg_hdl, buf, ret, &written, NULL); - LeaveCriticalSection(&_zz_pipe_cs); + zzuf_mutex_unlock(&debug_mutex); } va_end(args); fflush(NULL); /* flush all streams to make sure zzuf gotta catch 'em all */ } -void _zz_debug2(char const *format, ...) +void zzuf_debug2(char const *format, ...) { va_list args; char buf[0x100]; DWORD written; va_start(args, format); - //if (_zz_debuglevel >= 1) // LATER: + //if (g_debug_level >= 1) // LATER: { - HANDLE dbg_hdl = (HANDLE)_get_osfhandle(_zz_debugfd); + HANDLE dbg_hdl = (HANDLE)_get_osfhandle(g_debug_fd); int ret = _vsnprintf(buf, sizeof(buf), format, args); if (ret <= 0) @@ -123,28 +120,28 @@ void _zz_debug2(char const *format, ...) ret = (int)sizeof(buf) - 1; buf[ret++] = '\n'; - EnterCriticalSection(&_zz_pipe_cs); + zzuf_mutex_lock(&debug_mutex); WriteFile(dbg_hdl, buf, ret, &written, NULL); - LeaveCriticalSection(&_zz_pipe_cs); + zzuf_mutex_unlock(&debug_mutex); } va_end(args); fflush(NULL); /* flush all streams to make sure zzuf gotta catch 'em all */ } #else -void _zz_debug(char const *format, ...) +void zzuf_debug(char const *format, ...) { va_list args; va_start(args, format); - if (_zz_debuglevel >= 1) + if (g_debug_level >= 1) mydebug(format, args); va_end(args); } -void _zz_debug2(char const *format, ...) +void zzuf_debug2(char const *format, ...) { va_list args; va_start(args, format); - if (_zz_debuglevel >= 2) + if (g_debug_level >= 2) mydebug(format, args); va_end(args); } @@ -152,7 +149,7 @@ void _zz_debug2(char const *format, ...) /** * Format a string, printf-like, and write the resulting data to zzuf's - * debug file descriptor _zz_debugfd. If the debug file descriptor is + * debug file descriptor g_debug_fd. If the debug file descriptor is * still -1, this function does nothing. * * This function's code is roughly equivalent to the following *printf @@ -177,14 +174,14 @@ static void mydebug(char const *format, va_list args) { static char const *hex2char = "0123456789abcdef"; - zz_lock(&debug_mutex); + zzuf_mutex_lock(&debug_mutex); int saved_errno = errno; /* If there is spare data and the debug fd is open, we send the data */ - if (debug_count && _zz_debugfd >= 0) + if (debug_count && g_debug_fd >= 0) { - write(_zz_debugfd, debug_buffer, debug_count); + write(g_debug_fd, debug_buffer, debug_count); debug_count = 0; } @@ -323,13 +320,13 @@ static void mydebug(char const *format, va_list args) append("\n", 1); /* If the debug fd is open, we send the data */ - if (_zz_debugfd >= 0) + if (g_debug_fd >= 0) { - write(_zz_debugfd, debug_buffer, debug_count); + write(g_debug_fd, debug_buffer, debug_count); debug_count = 0; } - zz_unlock(&debug_mutex); + zzuf_mutex_unlock(&debug_mutex); errno = saved_errno; } diff --git a/src/libzzuf/debug.h b/src/libzzuf/debug.h index 46fb6b7..6476f63 100644 --- a/src/libzzuf/debug.h +++ b/src/libzzuf/debug.h @@ -14,12 +14,12 @@ * debug.h: debugging support */ -extern void _zz_debug(const char *format, ...) ATTRIBUTE_PRINTF(1,2); -extern void _zz_debug2(const char *format, ...) ATTRIBUTE_PRINTF(1,2); +extern void zzuf_debug(const char *format, ...) ATTRIBUTE_PRINTF(1,2); +extern void zzuf_debug2(const char *format, ...) ATTRIBUTE_PRINTF(1,2); #ifdef LIBZZUF -# define debug _zz_debug -# define debug2 _zz_debug2 +# define debug zzuf_debug +# define debug2 zzuf_debug2 #else # define debug(...) do {} while (0) # define debug2(...) do {} while (0) diff --git a/src/libzzuf/lib-fd.c b/src/libzzuf/lib-fd.c index f2b0a81..6a03fc4 100644 --- a/src/libzzuf/lib-fd.c +++ b/src/libzzuf/lib-fd.c @@ -184,7 +184,7 @@ static int (*ORIG(close)) (int fd); { \ ret = ORIG(myopen)(file, oflag); \ } \ - if (!_zz_ready || _zz_islocked(-1)) \ + if (!g_libzzuf_ready || _zz_islocked(-1)) \ return ret; \ if (ret >= 0 \ && ((oflag & (O_RDONLY | O_RDWR | O_WRONLY)) != O_WRONLY) \ @@ -228,7 +228,7 @@ int NEW(dup)(int oldfd) LOADSYM(dup); int ret = ORIG(dup)(oldfd); - if (!_zz_ready || _zz_islocked(-1) || !_zz_iswatched(oldfd) + if (!g_libzzuf_ready || _zz_islocked(-1) || !_zz_iswatched(oldfd) || !_zz_isactive(oldfd)) return ret; @@ -249,7 +249,7 @@ int NEW(dup2)(int oldfd, int newfd) LOADSYM(dup2); int ret = ORIG(dup2)(oldfd, newfd); - if (!_zz_ready || _zz_islocked(-1) || !_zz_iswatched(oldfd) + if (!g_libzzuf_ready || _zz_islocked(-1) || !_zz_iswatched(oldfd) || !_zz_isactive(oldfd)) return ret; @@ -275,7 +275,7 @@ int NEW(accept)(int sockfd, SOCKADDR_T *addr, SOCKLEN_T *addrlen) LOADSYM(accept); int ret = ORIG(accept)(sockfd, addr, addrlen); - if (!_zz_ready || _zz_islocked(-1) || !_zz_network + if (!g_libzzuf_ready || _zz_islocked(-1) || !g_network_fuzzing || !_zz_iswatched(sockfd) || !_zz_isactive(sockfd)) return ret; @@ -305,13 +305,13 @@ int NEW(accept)(int sockfd, SOCKADDR_T *addr, SOCKLEN_T *addrlen) LOADSYM(myconnect); \ \ ret = ORIG(myconnect)(sockfd, addr, addrlen); \ - if (!_zz_ready || _zz_islocked(-1) || !_zz_network) \ + if (!g_libzzuf_ready || _zz_islocked(-1) || !g_network_fuzzing) \ return ret; \ if (ret >= 0) \ { \ struct sockaddr_in in; \ long int port; \ - switch(addr->sa_family) \ + switch (addr->sa_family) \ { \ case AF_INET: \ case_AF_INET6 \ @@ -359,7 +359,7 @@ int NEW(socket)(int domain, int type, int protocol) LOADSYM(socket); int ret = ORIG(socket)(domain, type, protocol); - if (!_zz_ready || _zz_islocked(-1) || !_zz_network) + if (!g_libzzuf_ready || _zz_islocked(-1) || !g_network_fuzzing) return ret; if (ret >= 0) @@ -635,7 +635,7 @@ int NEW(aio_read)(struct aiocb *aiocbp) LOADSYM(aio_read); int fd = aiocbp->aio_fildes; - if (!_zz_ready || !_zz_iswatched(fd) || !_zz_isactive(fd)) + if (!g_libzzuf_ready || !_zz_iswatched(fd) || !_zz_isactive(fd)) return ORIG(aio_read)(aiocbp); _zz_lockfd(fd); @@ -654,7 +654,7 @@ ssize_t NEW(aio_return)(struct aiocb *aiocbp) LOADSYM(aio_return); int fd = aiocbp->aio_fildes; - if (!_zz_ready || !_zz_iswatched(fd) || !_zz_isactive(fd)) + if (!g_libzzuf_ready || !_zz_iswatched(fd) || !_zz_isactive(fd)) return ORIG(aio_return)(aiocbp); ssize_t ret = ORIG(aio_return)(aiocbp); @@ -683,11 +683,11 @@ int NEW(close)(int fd) LOADSYM(close); /* Hey, it’s our debug channel! Silently pretend we closed it. */ - if (fd == _zz_debugfd) + if (fd == g_debug_fd) return 0; int ret = ORIG(close)(fd); - if (!_zz_ready || !_zz_iswatched(fd) || _zz_islocked(fd)) + if (!g_libzzuf_ready || !_zz_iswatched(fd) || _zz_islocked(fd)) return ret; debug("%s(%i) = %i", __func__, fd, ret); diff --git a/src/libzzuf/lib-load.h b/src/libzzuf/lib-load.h index 6712060..1f2300b 100644 --- a/src/libzzuf/lib-load.h +++ b/src/libzzuf/lib-load.h @@ -29,7 +29,7 @@ extern void *_zz_dl_lib; /* XXX: we try to initialise libzzuf as soon as possible, \ * otherwise we may miss a lot of stuff if we wait for \ * the linker to load us fully. */ \ - _zz_init(); \ + libzzuf_init(); \ ORIG(x) = dlsym(_zz_dl_lib, STR(x)); \ } \ if (!ORIG(x)) \ diff --git a/src/libzzuf/lib-mem.c b/src/libzzuf/lib-mem.c index 03604b8..441369e 100644 --- a/src/libzzuf/lib-mem.c +++ b/src/libzzuf/lib-mem.c @@ -139,7 +139,7 @@ static int memory_exceeded(void) if (task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&tbi, &mmtn) == KERN_SUCCESS - && (int64_t)tbi.resident_size / 1048576 > (int64_t)_zz_memory) + && (int64_t)tbi.resident_size / 1048576 > (int64_t)g_memory_limit) return 1; #endif return 0; @@ -172,7 +172,7 @@ void *NEW(calloc)(size_t nmemb, size_t size) } void *ret = ORIG(calloc)(nmemb, size); - if (ret == NULL && _zz_memory && errno == ENOMEM) + if (ret == NULL && g_memory_limit && errno == ENOMEM) raise(SIGKILL); return ret; } @@ -193,7 +193,7 @@ void *NEW(malloc)(size_t size) return ret; } ret = ORIG(malloc)(size); - if (_zz_memory && ((!ret && errno == ENOMEM) + if (g_memory_limit && ((!ret && errno == ENOMEM) || (ret && memory_exceeded()))) raise(SIGKILL); return ret; @@ -242,7 +242,7 @@ void *NEW(realloc)(void *ptr, size_t size) LOADSYM(realloc); void *ret = ORIG(realloc)(ptr, size); - if (_zz_memory && ((!ret && errno == ENOMEM) + if (g_memory_limit && ((!ret && errno == ENOMEM) || (ret && memory_exceeded()))) raise(SIGKILL); return ret; @@ -255,7 +255,7 @@ void *NEW(valloc)(size_t size) LOADSYM(valloc); void *ret = ORIG(valloc)(size); - if (_zz_memory && ((!ret && errno == ENOMEM) + if (g_memory_limit && ((!ret && errno == ENOMEM) || (ret && memory_exceeded()))) raise(SIGKILL); return ret; @@ -269,7 +269,7 @@ void *NEW(memalign)(size_t boundary, size_t size) LOADSYM(memalign); void *ret = ORIG(memalign)(boundary, size); - if (_zz_memory && ((!ret && errno == ENOMEM) + if (g_memory_limit && ((!ret && errno == ENOMEM) || (ret && memory_exceeded()))) raise(SIGKILL); return ret; @@ -283,7 +283,7 @@ int NEW(posix_memalign)(void **memptr, size_t alignment, size_t size) LOADSYM(posix_memalign); int ret = ORIG(posix_memalign)(memptr, alignment, size); - if (_zz_memory && ((!ret && errno == ENOMEM) + if (g_memory_limit && ((!ret && errno == ENOMEM) || (ret && memory_exceeded()))) raise(SIGKILL); return ret; diff --git a/src/libzzuf/lib-signal.c b/src/libzzuf/lib-signal.c index b3675cf..5d38700 100644 --- a/src/libzzuf/lib-signal.c +++ b/src/libzzuf/lib-signal.c @@ -57,7 +57,7 @@ static int isfatal(int signum); static int isfatal(int signum) { - switch(signum) + switch (signum) { case SIGABRT: case SIGFPE: @@ -95,7 +95,7 @@ SIG_T NEW(signal)(int signum, SIG_T handler) { LOADSYM(signal); - if (!_zz_signal) + if (!g_disable_sighandlers) return ORIG(signal)(signum, handler); SIG_T ret = ORIG(signal)(signum, isfatal(signum) ? SIG_DFL : handler); @@ -112,7 +112,7 @@ int NEW(sigaction)(int signum, const struct sigaction *act, { LOADSYM(sigaction); - if (!_zz_signal) + if (!g_disable_sighandlers) return ORIG(sigaction)(signum, act, oldact); int ret; diff --git a/src/libzzuf/lib-stream.c b/src/libzzuf/lib-stream.c index 2c6f2a1..184c4dc 100644 --- a/src/libzzuf/lib-stream.c +++ b/src/libzzuf/lib-stream.c @@ -275,7 +275,7 @@ static inline void debug_stream(char const *prefix, FILE *stream) { \ LOADSYM(myfopen); \ \ - if (!_zz_ready) \ + if (!g_libzzuf_ready) \ return ORIG(myfopen)(path, mode); \ _zz_lockfd(-1); \ ret = ORIG(myfopen)(path, mode); \ @@ -296,7 +296,7 @@ static inline void debug_stream(char const *prefix, FILE *stream) LOADSYM(myfreopen); \ \ int fd0 = -1, fd1 = -1, disp = 0; \ - if (_zz_ready && (fd0 = fileno(stream)) >= 0 && _zz_iswatched(fd0)) \ + if (g_libzzuf_ready && (fd0 = fileno(stream)) >= 0 && _zz_iswatched(fd0)) \ { \ _zz_unregister(fd0); \ disp = 1; \ @@ -852,7 +852,7 @@ int NEW(fclose)(FILE *fp) LOADSYM(fclose); int fd = fileno(fp); - if (!_zz_ready || !_zz_iswatched(fd)) + if (!g_libzzuf_ready || !_zz_iswatched(fd)) return ORIG(fclose)(fp); debug_stream("before", fp); @@ -993,7 +993,7 @@ char *NEW(fgetln)(FILE *stream, size_t *len) int oldcnt = get_streambuf_count(stream); int64_t newpos = oldpos; - struct fuzz *fuzz = _zz_getfuzz(fd); + fuzz_context_t *fuzz = _zz_getfuzz(fd); size_t i = 0, size = 0; do diff --git a/src/libzzuf/lib-win32.c b/src/libzzuf/lib-win32.c index 9a33ecc..42b4972 100644 --- a/src/libzzuf/lib-win32.c +++ b/src/libzzuf/lib-win32.c @@ -125,7 +125,7 @@ HANDLE __stdcall NEW(CreateFileA)(LPCSTR lpFileName, DWORD dwDesiredAccess, lpFileName, dwDesiredAccess, dwShareMode, dwCreationDisposition, dwFlagsAndAttributes, (int)ret); - if (!_zz_ready || _zz_islocked(-1)) + if (!g_libzzuf_ready || _zz_islocked(-1)) return ret; if (ret != INVALID_HANDLE_VALUE && dwCreationDisposition == OPEN_EXISTING && _zz_mustwatch(lpFileName)) { @@ -150,7 +150,7 @@ HANDLE __stdcall NEW(CreateFileW)(LPCWSTR lpFileName, DWORD dwDesiredAccess, lpFileName, dwDesiredAccess, dwShareMode, dwCreationDisposition, dwFlagsAndAttributes, (int)ret); - if (!_zz_ready || _zz_islocked(-1)) + if (!g_libzzuf_ready || _zz_islocked(-1)) return ret; if (ret != INVALID_HANDLE_VALUE && dwCreationDisposition == OPEN_EXISTING && _zz_mustwatchw(lpFileName)) @@ -368,7 +368,7 @@ BOOL __stdcall NEW(CloseHandle)(HANDLE hObject) ret = ORIG(CloseHandle)(hObject); debug("CloseHandle(%#08x) = %s", (int)hObject, (ret ? "TRUE" : "FALSE")); - if (!_zz_ready || !_zz_iswatched(hObject) || _zz_islocked(hObject)) + if (!g_libzzuf_ready || !_zz_iswatched(hObject) || _zz_islocked(hObject)) return ret; _zz_unregister(hObject); return ret; diff --git a/src/libzzuf/libzzuf.c b/src/libzzuf/libzzuf.c index b39e758..abec638 100644 --- a/src/libzzuf/libzzuf.c +++ b/src/libzzuf/libzzuf.c @@ -57,21 +57,21 @@ BOOL WINAPI DllMain(HINSTANCE, DWORD, PVOID); /** * Is libzzuf fully initialised? */ -int _zz_ready = 0; +int g_libzzuf_ready = 0; /** * The debugging level that libzzuf should use. 0 means no debugging, * 1 means minimal debugging, 2 means verbose debugging. Its value is set * by the ZZUF_DEBUG environment variable. */ -int _zz_debuglevel = 0; +int g_debug_level = 0; /** * The file descriptor used by libzzuf for communication with the main * zzuf program in debug mode. Its value is set by the ZZUF_DEBUGFD * environment variable. */ -int _zz_debugfd = -1; +int g_debug_fd = -1; /** * If set to 1, this boolean variable will prevent the called application @@ -79,7 +79,7 @@ int _zz_debugfd = -1; * SDL applications often do that when not using SDL_INIT_NOPARACHUTE, for * instance. Its value is set by the ZZUF_SIGNAL environment variable. */ -int _zz_signal = 0; +int g_disable_sighandlers = 0; /** * If set to a positive value, this value will indicate the maximum number @@ -87,14 +87,14 @@ int _zz_signal = 0; * allowed to allocate. Its value is set by the ZZUF_MEMORY environment * variable. */ -uint64_t _zz_memory = 0; +uint64_t g_memory_limit = 0; /** * If set to 1, this boolean will tell libzzuf to fuzz network file * descriptors, too. Its value is set by the ZZUF_NETWORK environment * variable. */ -int _zz_network = 0; +int g_network_fuzzing = 0; /** * Library initialisation routine. @@ -105,7 +105,7 @@ int _zz_network = 0; * other functions we need such as dlsym() require them), file descriptor * functions and stream functions. */ -void _zz_init(void) +void libzzuf_init(void) { static int initializing = 0; char *tmp, *tmp2; @@ -116,14 +116,14 @@ void _zz_init(void) tmp = getenv("ZZUF_DEBUG"); if (tmp) - _zz_debuglevel = atoi(tmp); + g_debug_level = atoi(tmp); tmp = getenv("ZZUF_DEBUGFD"); if (tmp) #if defined _WIN32 - _zz_debugfd = _open_osfhandle((long)atoi(tmp), 0); + g_debug_fd = _open_osfhandle((long)atoi(tmp), 0); #else - _zz_debugfd = atoi(tmp); + g_debug_fd = atoi(tmp); #endif /* We need this as soon as possible */ @@ -131,16 +131,16 @@ void _zz_init(void) tmp = getenv("ZZUF_SEED"); if (tmp && *tmp) - _zz_setseed(atol(tmp)); + zzuf_set_seed(atol(tmp)); tmp = getenv("ZZUF_MINRATIO"); tmp2 = getenv("ZZUF_MAXRATIO"); if (tmp && *tmp && tmp2 && *tmp2) - _zz_setratio(atof(tmp), atof(tmp2)); + zzuf_set_ratio(atof(tmp), atof(tmp2)); tmp = getenv("ZZUF_AUTOINC"); if (tmp && *tmp == '1') - _zz_setautoinc(); + zzuf_set_auto_increment(); tmp = getenv("ZZUF_BYTES"); if (tmp && *tmp) @@ -164,31 +164,31 @@ void _zz_init(void) tmp = getenv("ZZUF_PROTECT"); if (tmp && *tmp) - _zz_protect(tmp); + zzuf_protect_range(tmp); tmp = getenv("ZZUF_REFUSE"); if (tmp && *tmp) - _zz_refuse(tmp); + zzuf_refuse_range(tmp); tmp = getenv("ZZUF_INCLUDE"); if (tmp && *tmp) - _zz_include(tmp); + zzuf_include_pattern(tmp); tmp = getenv("ZZUF_EXCLUDE"); if (tmp && *tmp) - _zz_exclude(tmp); + zzuf_exclude_pattern(tmp); tmp = getenv("ZZUF_SIGNAL"); if (tmp && *tmp == '1') - _zz_signal = 1; + g_disable_sighandlers = 1; tmp = getenv("ZZUF_MEMORY"); if (tmp) - _zz_memory = atoi(tmp); + g_memory_limit = atoi(tmp); tmp = getenv("ZZUF_NETWORK"); if (tmp && *tmp == '1') - _zz_network = 1; + g_network_fuzzing = 1; _zz_fd_init(); _zz_network_init(); @@ -198,7 +198,7 @@ void _zz_init(void) if (tmp && *tmp == '1') _zz_register(0); - _zz_ready = 1; + g_libzzuf_ready = 1; debug("libzzuf initialised for PID %li", (long int)getpid()); } @@ -208,9 +208,9 @@ void _zz_init(void) * * Free all the memory allocated by libzzuf during its lifetime. */ -void _zz_fini(void) +void libzzuf_fini(void) { - if (!_zz_ready) + if (!g_libzzuf_ready) return; debug("libzzuf finishing for PID %li", (long int)getpid()); @@ -218,7 +218,7 @@ void _zz_fini(void) _zz_fd_fini(); _zz_network_fini(); - _zz_ready = 0; + g_libzzuf_ready = 0; } #if defined HAVE_WINDOWS_H @@ -227,15 +227,13 @@ BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, PVOID impLoad) (void)hinst; /* unused */ (void)impLoad; /* unused */ - switch(reason) + switch (reason) { case DLL_PROCESS_ATTACH: - InitializeCriticalSection(&_zz_pipe_cs); - _zz_init(); + libzzuf_init(); break; case DLL_PROCESS_DETACH: - //_zz_fini(); - DeleteCriticalSection(&_zz_pipe_cs); + //libzzuf_fini(); break; } diff --git a/src/libzzuf/libzzuf.h b/src/libzzuf/libzzuf.h index e2478e3..94c20c8 100644 --- a/src/libzzuf/libzzuf.h +++ b/src/libzzuf/libzzuf.h @@ -19,22 +19,21 @@ #include "fd.h" /* Internal variables */ -extern int _zz_ready; -extern int _zz_disabled; -extern int _zz_debuglevel; -extern int _zz_debugfd; -extern int _zz_signal; -extern uint64_t _zz_memory; -extern int _zz_network; -extern int _zz_autoinc; +extern int g_libzzuf_ready; +extern int g_debug_level; +extern int g_debug_fd; +extern int g_disable_sighandlers; +extern uint64_t g_memory_limit; +extern int g_network_fuzzing; +extern int g_auto_increment; /* Library initialisation shit */ #if defined __GNUC__ -extern void _zz_init(void) __attribute__((constructor)); -extern void _zz_fini(void) __attribute__((destructor)); +extern void libzzuf_init(void) __attribute__((constructor)); +extern void libzzuf_fini(void) __attribute__((destructor)); #elif defined HAVE_PRAGMA_INIT -# pragma INIT "_zz_init" -# pragma FINI "_zz_fini" +# pragma INIT "libzzuf_init" +# pragma FINI "libzzuf_fini" #endif /* This function is needed to initialise memory functions */ @@ -43,14 +42,9 @@ extern void _zz_mem_init(void); /* This function lets us know where the end of a file is. */ extern size_t _zz_bytes_until_eof(int fd, size_t offset); -#ifdef _WIN32 -# include -extern CRITICAL_SECTION _zz_pipe_cs; -#endif - static inline int must_fuzz_fd(int fd) { - return _zz_ready && _zz_iswatched(fd) + return g_libzzuf_ready && _zz_iswatched(fd) && !_zz_islocked(fd) && _zz_isactive(fd); } diff --git a/src/myfork.c b/src/myfork.c index 5cb3264..12633e0 100644 --- a/src/myfork.c +++ b/src/myfork.c @@ -74,7 +74,7 @@ #endif static int mypipe(int pipefd[2]); -static int run_process(struct child *child, struct opts *, int[][2]); +static int run_process(zzuf_child_t *child, zzuf_opts_t *, int[][2]); #if defined HAVE_WINDOWS_H static int dll_inject(PROCESS_INFORMATION *, char const *); @@ -86,7 +86,7 @@ static void *get_proc_address(void *, DWORD, char const *); * child PID is stored in the child structure, and a communication channel * is set up using pipes. */ -int myfork(struct child *child, struct opts *opts) +int myfork(zzuf_child_t *child, zzuf_opts_t *opts) { /* Prepare communication pipes */ int pipefds[3][2]; @@ -201,7 +201,7 @@ static void setenv(char const *name, char const *value, int overwrite) } #endif -static int run_process(struct child *child, struct opts *opts, int pipes[][2]) +static int run_process(zzuf_child_t *child, zzuf_opts_t *opts, int pipes[][2]) { #if defined HAVE_FORK # if defined __APPLE__ diff --git a/src/myfork.h b/src/myfork.h index d173d51..679183c 100644 --- a/src/myfork.h +++ b/src/myfork.h @@ -14,5 +14,5 @@ * myfork.h: process handling functions */ -int myfork(struct child *child, struct opts *opts); +int myfork(zzuf_child_t *child, zzuf_opts_t *opts); diff --git a/src/opts.c b/src/opts.c index 47640a8..764be26 100644 --- a/src/opts.c +++ b/src/opts.c @@ -30,8 +30,10 @@ #include "timer.h" #include "opts.h" -void _zz_opts_init(struct opts *opts) +zzuf_opts_t *zzuf_create_opts(void) { + zzuf_opts_t *opts = malloc(sizeof(zzuf_opts_t)); + opts->opmode = OPMODE_PRELOAD; opts->fuzzing = opts->bytes = opts->list = opts->ports = NULL; opts->allow = NULL; @@ -49,7 +51,7 @@ void _zz_opts_init(struct opts *opts) opts->maxbytes = -1; opts->maxmem = DEFAULT_MEM; - opts->starttime = _zz_time(); + opts->starttime = zzuf_time(); opts->maxtime = 0; opts->maxusertime = -1; opts->maxcpu = -1; @@ -61,9 +63,11 @@ void _zz_opts_init(struct opts *opts) opts->maxcrashes = 1; opts->crashes = 0; opts->child = NULL; + + return opts; } -void _zz_opts_fini(struct opts *opts) +void zzuf_destroy_opts(zzuf_opts_t *opts) { if (opts->child) { @@ -72,5 +76,7 @@ void _zz_opts_fini(struct opts *opts) free(opts->child[i].newargv); free(opts->child); } + + free(opts); } diff --git a/src/opts.h b/src/opts.h index 7f9d38f..619e1ed 100644 --- a/src/opts.h +++ b/src/opts.h @@ -10,15 +10,50 @@ * See http://www.wtfpl.net/ for more details. */ +#pragma once + /* * opts.h: configuration handling */ +#include "util/hex.h" +#include "util/md5.h" + #ifdef _WIN32 # include #endif -struct opts +typedef struct zzuf_opts zzuf_opts_t; +typedef struct zzuf_child zzuf_child_t; + +zzuf_opts_t *zzuf_create_opts(void); +void zzuf_destroy_opts(zzuf_opts_t *); + +struct zzuf_child +{ + enum status + { + STATUS_FREE, + STATUS_RUNNING, + STATUS_SIGTERM, + STATUS_SIGKILL, + STATUS_EOF, + } status; + + pid_t pid; +#ifdef _WIN32 + HANDLE process_handle; +#endif + int fd[3]; /* 0 is debug, 1 is stderr, 2 is stdout */ + int bytes, seed; + double ratio; + int64_t date; + zzuf_md5sum_t *md5; + zzuf_hexdump_t *hex; + char **newargv; +}; + +struct zzuf_opts { enum opmode { @@ -52,31 +87,7 @@ struct opts int64_t lastlaunch; int maxchild, nchild, maxcrashes, crashes; - struct child - { - enum status - { - STATUS_FREE, - STATUS_RUNNING, - STATUS_SIGTERM, - STATUS_SIGKILL, - STATUS_EOF, - } status; - pid_t pid; -#ifdef _WIN32 - HANDLE process_handle; -#endif - int fd[3]; /* 0 is debug, 1 is stderr, 2 is stdout */ - int bytes, seed; - double ratio; - int64_t date; - struct zz_md5 *md5; - struct zz_hex *hex; - char **newargv; - } *child; + zzuf_child_t *child; }; -void _zz_opts_init(struct opts *); -void _zz_opts_fini(struct opts *); - diff --git a/src/timer.c b/src/timer.c index 205fa8b..667a35f 100644 --- a/src/timer.c +++ b/src/timer.c @@ -33,23 +33,23 @@ #include "timer.h" #include "util/mutex.h" -int64_t _zz_time(void) +int64_t zzuf_time(void) { #if defined HAVE_GETTIMEOFDAY struct timeval tv; gettimeofday(&tv, NULL); return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec; #else - static zz_mutex mutex = 0; + static zzuf_mutex_t mutex = 0; static unsigned long int prev = 0; static uint64_t tv_base = 0; - zz_lock(&mutex); + zzuf_mutex_lock(&mutex); unsigned long int tv_msec = GetTickCount(); if (tv_msec < prev) tv_base += 0x100000000LL; /* We wrapped */ prev = tv_msec; - zz_unlock(&mutex); + zzuf_mutex_unlock(&mutex); return (tv_base + tv_msec) * 1000; #endif diff --git a/src/timer.h b/src/timer.h index c0afa01..feeab48 100644 --- a/src/timer.h +++ b/src/timer.h @@ -14,5 +14,5 @@ * timer.h: timing functions */ -int64_t _zz_time(void); +int64_t zzuf_time(void); diff --git a/src/util/getopt.c b/src/util/getopt.c index 8b2597f..444d672 100644 --- a/src/util/getopt.c +++ b/src/util/getopt.c @@ -30,13 +30,13 @@ int zz_optind = 1; char *zz_optarg = NULL; int zz_getopt(int argc, char * const _argv[], char const *optstring, - struct zz_option const *longopts, int *longindex) + zzuf_option_t const *longopts, int *longindex) { #if defined HAVE_GETOPT_LONG optind = zz_optind; optarg = zz_optarg; int ret = getopt_long(argc, _argv, optstring, - (struct zz_option const *)longopts, longindex); + (zzuf_option_t const *)longopts, longindex); zz_optind = optind; zz_optarg = optarg; return ret; diff --git a/src/util/getopt.h b/src/util/getopt.h index 9a2b703..dfebc67 100644 --- a/src/util/getopt.h +++ b/src/util/getopt.h @@ -14,7 +14,7 @@ * getopt.h: getopt_long reimplementation */ -struct zz_option +struct zzuf_option { char const *name; int has_arg; @@ -22,8 +22,10 @@ struct zz_option int val; }; +typedef struct zzuf_option zzuf_option_t; + extern int zz_optind; extern char *zz_optarg; extern int zz_getopt(int, char * const[], char const *, - struct zz_option const *, int *); + zzuf_option_t const *, int *); diff --git a/src/util/hex.c b/src/util/hex.c index 1e89565..1e5c39a 100644 --- a/src/util/hex.c +++ b/src/util/hex.c @@ -30,7 +30,7 @@ #include "util/hex.h" -struct zz_hex +struct zzuf_hexdump { /* Buffered line */ uint8_t current_line[16]; @@ -40,16 +40,16 @@ struct zz_hex int64_t count; }; -struct zz_hex *zz_hex_init(void) +zzuf_hexdump_t *zzuf_create_hex(void) { - struct zz_hex *ctx = malloc(sizeof(struct zz_hex)); + zzuf_hexdump_t *ctx = malloc(sizeof(zzuf_hexdump_t)); ctx->count = 0; return ctx; } -static void print_hex(struct zz_hex *ctx, unsigned len) +static void print_hex(zzuf_hexdump_t *ctx, unsigned len) { uint8_t *buf = ctx->current_line; uint32_t address = (uint32_t)(ctx->count - len); @@ -73,7 +73,7 @@ static void print_hex(struct zz_hex *ctx, unsigned len) printf("%08x %s |%s|\n", address, hex, ascii); } -void zz_hex_add(struct zz_hex *ctx, uint8_t *buf, unsigned len) +void zz_hex_add(zzuf_hexdump_t *ctx, uint8_t *buf, unsigned len) { unsigned buffered_len = (unsigned)(ctx->count & 15); @@ -101,7 +101,7 @@ void zz_hex_add(struct zz_hex *ctx, uint8_t *buf, unsigned len) fflush(stdout); } -void zz_hex_fini(struct zz_hex *ctx) +void zzuf_destroy_hex(zzuf_hexdump_t *ctx) { /* Print the last line, if non-empty */ if (ctx->count & 15) diff --git a/src/util/hex.h b/src/util/hex.h index d9c2f10..af5c729 100644 --- a/src/util/hex.h +++ b/src/util/hex.h @@ -14,7 +14,9 @@ * hex.h: hexadecimal data dump */ -extern struct zz_hex *zz_hex_init(void); -extern void zz_hex_add(struct zz_hex *ctx, uint8_t *buf, unsigned len); -extern void zz_hex_fini(struct zz_hex *ctx); +typedef struct zzuf_hexdump zzuf_hexdump_t; + +extern zzuf_hexdump_t *zzuf_create_hex(void); +extern void zz_hex_add(zzuf_hexdump_t *ctx, uint8_t *buf, unsigned len); +extern void zzuf_destroy_hex(zzuf_hexdump_t *ctx); diff --git a/src/util/md5.c b/src/util/md5.c index e8fda74..0eba1c4 100644 --- a/src/util/md5.c +++ b/src/util/md5.c @@ -30,7 +30,7 @@ #include "util/md5.h" -struct zz_md5 +struct zzuf_md5sum { uint32_t buf[4]; uint32_t bits[2]; @@ -40,9 +40,9 @@ struct zz_md5 static void swapwords(uint32_t *buf, unsigned words); static void transform(uint32_t buf[4], uint32_t in[16]); -struct zz_md5 *zz_md5_init(void) +zzuf_md5sum_t *zzuf_create_md5(void) { - struct zz_md5 *ctx = malloc(sizeof(struct zz_md5)); + zzuf_md5sum_t *ctx = malloc(sizeof(zzuf_md5sum_t)); ctx->buf[0] = 0x67452301; ctx->buf[1] = 0xefcdab89; @@ -55,7 +55,7 @@ struct zz_md5 *zz_md5_init(void) return ctx; } -void zz_md5_add(struct zz_md5 *ctx, uint8_t *buf, unsigned len) +void zz_md5_add(zzuf_md5sum_t *ctx, uint8_t *buf, unsigned len) { uint32_t t = ctx->bits[0]; if ((ctx->bits[0] = t + ((uint32_t)len << 3)) < t) @@ -93,7 +93,7 @@ void zz_md5_add(struct zz_md5 *ctx, uint8_t *buf, unsigned len) memcpy(ctx->in, buf, len); } -void zz_md5_fini(uint8_t *digest, struct zz_md5 *ctx) +void zzuf_destroy_md5(uint8_t *digest, zzuf_md5sum_t *ctx) { unsigned count = (ctx->bits[0] >> 3) & 0x3F; uint8_t *p = (uint8_t *)ctx->in + count; diff --git a/src/util/md5.h b/src/util/md5.h index 4f558ec..b1a4791 100644 --- a/src/util/md5.h +++ b/src/util/md5.h @@ -14,7 +14,9 @@ * md5.h: MD5 computation */ -extern struct zz_md5 *zz_md5_init(void); -extern void zz_md5_add(struct zz_md5 *ctx, uint8_t *buf, unsigned len); -extern void zz_md5_fini(uint8_t *digest, struct zz_md5 *ctx); +typedef struct zzuf_md5sum zzuf_md5sum_t; + +extern zzuf_md5sum_t *zzuf_create_md5(void); +extern void zz_md5_add(zzuf_md5sum_t *ctx, uint8_t *buf, unsigned len); +extern void zzuf_destroy_md5(uint8_t *digest, zzuf_md5sum_t *ctx); diff --git a/src/util/mutex.h b/src/util/mutex.h index ca8731f..a342bd1 100644 --- a/src/util/mutex.h +++ b/src/util/mutex.h @@ -15,14 +15,14 @@ */ #if _WIN32 -typedef volatile LONG zz_mutex; +typedef volatile LONG zzuf_mutex_t; #elif __GNUC__ || __clang__ -typedef volatile int zz_mutex; +typedef volatile int zzuf_mutex_t; #else # error "No known atomic operations for this platform" #endif -static inline void zz_lock(zz_mutex *l) +static inline void zzuf_mutex_lock(zzuf_mutex_t *l) { #if _WIN32 do {} @@ -33,7 +33,7 @@ static inline void zz_lock(zz_mutex *l) #endif } -static inline void zz_unlock(zz_mutex *l) +static inline void zzuf_mutex_unlock(zzuf_mutex_t *l) { #if _WIN32 InterlockedExchange(l, 0); diff --git a/src/zzat.c b/src/zzat.c index 0221cbb..f9c4043 100644 --- a/src/zzat.c +++ b/src/zzat.c @@ -87,7 +87,7 @@ int main(int argc, char *argv[]) #define OPTSTR "+AbdeEnr:stTvx:lhV" #define MOREINFO "Try `%s --help' for more information.\n" int option_index = 0; - static struct zz_option long_options[] = + static zzuf_option_t long_options[] = { { "show-all", 0, NULL, 'A' }, { "number-nonblank", 0, NULL, 'b' }, diff --git a/src/zzuf.c b/src/zzuf.c index 0e7d6cf..dcded4a 100644 --- a/src/zzuf.c +++ b/src/zzuf.c @@ -88,11 +88,11 @@ # undef ZZUF_RLIMIT_CPU #endif -static void loop_stdin(struct opts *); +static void loop_stdin(zzuf_opts_t *); -static void spawn_children(struct opts *); -static void clean_children(struct opts *); -static void read_children(struct opts *); +static void spawn_children(zzuf_opts_t *); +static void clean_children(zzuf_opts_t *); +static void read_children(zzuf_opts_t *); #if !defined HAVE_SETENV static void setenv(char const *, char const *, int); @@ -100,7 +100,7 @@ static void setenv(char const *, char const *, int); #if defined HAVE_WAITPID static char const *sig2name(int); #endif -static void finfo(FILE *, struct opts *, uint32_t); +static void finfo(FILE *, zzuf_opts_t *, uint32_t); #if defined HAVE_REGEX_H static char *merge_regex(char *, char *); static char *merge_file(char *, char *); @@ -120,15 +120,11 @@ static void usage(void); ((fd >= 0) && (FD_ISSET(fd, p_fdset))) #if defined _WIN32 -# include -# include /* _O_RDWR */ -# include /* _open */ -static CRITICAL_SECTION _zz_pipe_cs; +static zzuf_mutex_t pipe_mutex = 0; #endif int main(int argc, char *argv[]) { - struct opts _opts, *opts = &_opts; char *tmp; #if defined HAVE_REGEX_H char *include = NULL, *exclude = NULL; @@ -136,11 +132,7 @@ int main(int argc, char *argv[]) #endif int debug = 0, b_network = 0; -#if defined _WIN32 - InitializeCriticalSection(&_zz_pipe_cs); -#endif - - _zz_opts_init(opts); + zzuf_opts_t *opts = zzuf_create_opts(); for (;;) { @@ -163,7 +155,7 @@ int main(int argc, char *argv[]) "a:Ab:B:C:dD:e:f:F:ij:l:mnO:p:P:qr:R:s:St:U:vxXhV" #define MOREINFO "Try `%s --help' for more information.\n" int option_index = 0; - static struct zz_option long_options[] = + static zzuf_option_t long_options[] = { /* Long option, needs arg, flag, short option */ { "allow", 1, NULL, 'a' }, @@ -255,7 +247,7 @@ int main(int argc, char *argv[]) { fprintf(stderr, "%s: invalid regex -- `%s'\n", argv[0], zz_optarg); - _zz_opts_fini(opts); + zzuf_destroy_opts(opts); return EXIT_FAILURE; } break; @@ -265,7 +257,7 @@ int main(int argc, char *argv[]) break; case 'F': fprintf(stderr, "%s: `-F' is deprecated, use `-j'\n", argv[0]); - _zz_opts_fini(opts); + zzuf_destroy_opts(opts); return EXIT_FAILURE; case 'i': /* --stdin */ setenv("ZZUF_STDIN", "1", 1); @@ -277,7 +269,7 @@ int main(int argc, char *argv[]) { fprintf(stderr, "%s: invalid regex -- `%s'\n", argv[0], zz_optarg); - _zz_opts_fini(opts); + zzuf_destroy_opts(opts); return EXIT_FAILURE; } break; @@ -315,7 +307,7 @@ int main(int argc, char *argv[]) { fprintf(stderr, "%s: invalid operating mode -- `%s'\n", argv[0], zz_optarg); - _zz_opts_fini(opts); + zzuf_destroy_opts(opts); return EXIT_FAILURE; } break; @@ -378,16 +370,16 @@ int main(int argc, char *argv[]) break; case 'h': /* --help */ usage(); - _zz_opts_fini(opts); + zzuf_destroy_opts(opts); return 0; case 'V': /* --version */ version(); - _zz_opts_fini(opts); + zzuf_destroy_opts(opts); return 0; default: fprintf(stderr, "%s: invalid option -- %c\n", argv[0], c); printf(MOREINFO, argv[0]); - _zz_opts_fini(opts); + zzuf_destroy_opts(opts); return EXIT_FAILURE; } } @@ -397,7 +389,7 @@ int main(int argc, char *argv[]) fprintf(stderr, "%s: MD5 hash (-m) and hexadecimal dump (-X) are " "incompatible\n", argv[0]); printf(MOREINFO, argv[0]); - _zz_opts_fini(opts); + zzuf_destroy_opts(opts); return EXIT_FAILURE; } @@ -406,7 +398,7 @@ int main(int argc, char *argv[]) fprintf(stderr, "%s: port option (-p) requires network fuzzing (-n)\n", argv[0]); printf(MOREINFO, argv[0]); - _zz_opts_fini(opts); + zzuf_destroy_opts(opts); return EXIT_FAILURE; } @@ -415,12 +407,12 @@ int main(int argc, char *argv[]) fprintf(stderr, "%s: allow option (-a) requires network fuzzing (-n)\n", argv[0]); printf(MOREINFO, argv[0]); - _zz_opts_fini(opts); + zzuf_destroy_opts(opts); return EXIT_FAILURE; } - _zz_setratio(opts->minratio, opts->maxratio); - _zz_setseed(opts->seed); + zzuf_set_ratio(opts->minratio, opts->maxratio); + zzuf_set_seed(opts->seed); if (opts->fuzzing) _zz_fuzzing(opts->fuzzing); @@ -429,9 +421,9 @@ int main(int argc, char *argv[]) if (opts->list) _zz_list(opts->list); if (opts->protect) - _zz_protect(opts->protect); + zzuf_protect_range(opts->protect); if (opts->refuse) - _zz_refuse(opts->refuse); + zzuf_refuse_range(opts->refuse); /* Needed for stdin mode and for copy opmode. */ _zz_fd_init(); @@ -452,7 +444,7 @@ int main(int argc, char *argv[]) fprintf(stderr, "%s: seed ranges are incompatible with " "stdin fuzzing\n", argv[0]); printf(MOREINFO, argv[0]); - _zz_opts_fini(opts); + zzuf_destroy_opts(opts); return EXIT_FAILURE; } @@ -513,7 +505,7 @@ int main(int argc, char *argv[]) #endif /* Allocate memory for children handling */ - opts->child = malloc(opts->maxchild * sizeof(struct child)); + opts->child = malloc(opts->maxchild * sizeof(zzuf_child_t)); for (int i = 0; i < opts->maxchild; ++i) { opts->child[i].status = STATUS_FREE; @@ -554,7 +546,7 @@ int main(int argc, char *argv[]) break; } - if (opts->maxtime && _zz_time() - opts->starttime >= opts->maxtime + if (opts->maxtime && zzuf_time() - opts->starttime >= opts->maxtime && opts->nchild == 0) { if (opts->b_verbose) @@ -565,26 +557,24 @@ int main(int argc, char *argv[]) } } + int ret = opts->crashes ? EXIT_FAILURE : EXIT_SUCCESS; + /* Clean up */ _zz_fd_fini(); - _zz_opts_fini(opts); + zzuf_destroy_opts(opts); -#if defined _WIN32 - DeleteCriticalSection(&_zz_pipe_cs); -#endif - - return opts->crashes ? EXIT_FAILURE : EXIT_SUCCESS; + return ret; } -static void loop_stdin(struct opts *opts) +static void loop_stdin(zzuf_opts_t *opts) { - struct zz_md5 *md5 = NULL; - struct zz_hex *hex = NULL; + zzuf_md5sum_t *md5 = NULL; + zzuf_hexdump_t *hex = NULL; if (opts->b_md5) - md5 = zz_md5_init(); + md5 = zzuf_create_md5(); else if (opts->b_hex) - hex = zz_hex_init(); + hex = zzuf_create_hex(); _zz_register(0); @@ -627,7 +617,7 @@ static void loop_stdin(struct opts *opts) if (opts->b_md5) { uint8_t md5sum[16]; - zz_md5_fini(md5sum, md5); + zzuf_destroy_md5(md5sum, md5); finfo(stdout, opts, opts->seed); fprintf(stdout, "%.02x%.02x%.02x%.02x%.02x%.02x%.02x%.02x%.02x%.02x" "%.02x%.02x%.02x%.02x%.02x%.02x\n", md5sum[0], md5sum[1], @@ -638,13 +628,13 @@ static void loop_stdin(struct opts *opts) } else if (opts->b_hex) { - zz_hex_fini(hex); + zzuf_destroy_hex(hex); } _zz_unregister(0); } -static void finfo(FILE *fp, struct opts *opts, uint32_t seed) +static void finfo(FILE *fp, zzuf_opts_t *opts, uint32_t seed) { if (opts->minratio == opts->maxratio) fprintf(fp, "zzuf[s=%i,r=%g]: ", seed, opts->minratio); @@ -703,9 +693,9 @@ static char *merge_regex(char *regex, char *string) } #endif -static void spawn_children(struct opts *opts) +static void spawn_children(zzuf_opts_t *opts) { - int64_t now = _zz_time(); + int64_t now = zzuf_time(); if (opts->nchild == opts->maxchild) return; /* no slot */ @@ -792,12 +782,12 @@ static void spawn_children(struct opts *opts) opts->child[slot].date = now; opts->child[slot].bytes = 0; opts->child[slot].seed = opts->seed; - opts->child[slot].ratio = _zz_getratio(); + opts->child[slot].ratio = zzuf_get_ratio(); opts->child[slot].status = STATUS_RUNNING; if (opts->b_md5) - opts->child[slot].md5 = zz_md5_init(); + opts->child[slot].md5 = zzuf_create_md5(); else if (opts->b_hex) - opts->child[slot].hex = zz_hex_init(); + opts->child[slot].hex = zzuf_create_hex(); if (opts->b_verbose) { @@ -809,13 +799,13 @@ static void spawn_children(struct opts *opts) opts->nchild++; opts->seed++; - _zz_setseed(opts->seed); + zzuf_set_seed(opts->seed); } -static void clean_children(struct opts *opts) +static void clean_children(zzuf_opts_t *opts) { #if defined HAVE_KILL || defined HAVE_WINDOWS_H - int64_t now = _zz_time(); + int64_t now = zzuf_time(); #endif #if defined HAVE_KILL || defined HAVE_WINDOWS_H @@ -985,7 +975,7 @@ static void clean_children(struct opts *opts) if (opts->b_md5) { - zz_md5_fini(md5sum, opts->child[i].md5); + zzuf_destroy_md5(md5sum, opts->child[i].md5); finfo(stdout, opts, opts->child[i].seed); fprintf(stdout, "%.02x%.02x%.02x%.02x%.02x%.02x%.02x%.02x%.02x" "%.02x%.02x%.02x%.02x%.02x%.02x%.02x\n", md5sum[0], @@ -996,7 +986,7 @@ static void clean_children(struct opts *opts) } else if (opts->b_hex) { - zz_hex_fini(opts->child[i].hex); + zzuf_destroy_hex(opts->child[i].hex); } opts->child[i].status = STATUS_FREE; opts->nchild--; @@ -1010,7 +1000,7 @@ struct child_overlapped { OVERLAPPED overlapped; uint8_t buf[BUFSIZ]; - struct opts * opts; + zzuf_opts_t * opts; int child_no; int fd_no; }; @@ -1025,7 +1015,7 @@ static void __stdcall read_child(DWORD err_code, DWORD nbr_of_bytes_transfered, if (err_code != ERROR_SUCCESS) return; - EnterCriticalSection(&_zz_pipe_cs); + zzuf_mutex_lock(&pipe_mutex); switch (co->fd_no) { case 0: /* debug fd */ @@ -1036,7 +1026,7 @@ static void __stdcall read_child(DWORD err_code, DWORD nbr_of_bytes_transfered, write(2, co->buf, nbr_of_bytes_transfered); break; default: break; } - LeaveCriticalSection(&_zz_pipe_cs); + zzuf_mutex_unlock(&pipe_mutex); if (co->fd_no != 0) /* either out or err fd */ co->opts->child[co->child_no].bytes += nbr_of_bytes_transfered; @@ -1050,7 +1040,7 @@ static void __stdcall read_child(DWORD err_code, DWORD nbr_of_bytes_transfered, } /* Since on windows select doesn't support file HANDLE, we use IOCP */ -static void read_children(struct opts *opts) +static void read_children(zzuf_opts_t *opts) { HANDLE *children_handle, * cur_child_handle; size_t fd_number = opts->maxchild * 3; @@ -1103,7 +1093,7 @@ static void read_children(struct opts *opts) } #else -static void read_children(struct opts *opts) +static void read_children(zzuf_opts_t *opts) { struct timeval tv; fd_set fdset;