From c0ca87d5b2321d3c7d8bba638704bf4e722d7fa4 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Tue, 6 Jan 2015 00:36:24 +0100 Subject: [PATCH] misc: move a lot of generic stuff to a new util/ source subdirectory. --- msvc/libzzuf.vcxproj | 14 +- msvc/zzat.vcxproj | 11 +- msvc/zzuf.vcxproj | 22 ++- src/Makefile.am | 12 +- src/common/fd.c | 86 ++++------- src/common/fd.h | 2 +- src/common/random.c | 8 +- src/libzzuf/lib-fd.c | 2 +- src/libzzuf/lib-stream.c | 26 ++-- src/myfork.c | 1 - src/opts.c | 11 +- src/opts.h | 15 +- src/{caca_getopt.c => util/getopt.c} | 42 +++--- src/{caca_getopt.h => util/getopt.h} | 21 +-- src/{ => util}/md5.c | 12 +- src/{ => util}/md5.h | 6 +- src/util/mutex.h | 45 ++++++ src/{com_regexp.cpp => util/regex.cpp} | 2 +- src/{com_regexp.hpp => util/regex.h} | 0 src/zzat.c | 18 +-- src/zzuf.c | 200 ++++++++++++------------- 21 files changed, 283 insertions(+), 273 deletions(-) rename src/{caca_getopt.c => util/getopt.c} (75%) rename src/{caca_getopt.h => util/getopt.h} (52%) rename src/{ => util}/md5.c (96%) rename src/{ => util}/md5.h (73%) create mode 100644 src/util/mutex.h rename src/{com_regexp.cpp => util/regex.cpp} (96%) rename src/{com_regexp.hpp => util/regex.h} (100%) diff --git a/msvc/libzzuf.vcxproj b/msvc/libzzuf.vcxproj index 9dc03f0..535615c 100644 --- a/msvc/libzzuf.vcxproj +++ b/msvc/libzzuf.vcxproj @@ -199,12 +199,13 @@ - + + @@ -212,12 +213,6 @@ - - CompileAsCpp - CompileAsCpp - CompileAsCpp - CompileAsCpp - @@ -227,8 +222,11 @@ + + CompileAsCpp + - \ No newline at end of file + diff --git a/msvc/zzat.vcxproj b/msvc/zzat.vcxproj index 9f1c30d..6939103 100644 --- a/msvc/zzat.vcxproj +++ b/msvc/zzat.vcxproj @@ -173,18 +173,15 @@ - + - + - $(IntDir)%(FileName)1.obj - $(IntDir)%(FileName)1.obj - $(IntDir)%(FileName)1.obj - $(IntDir)%(FileName)1.obj + $(IntDir)%(FileName)1.obj - \ No newline at end of file + diff --git a/msvc/zzuf.vcxproj b/msvc/zzuf.vcxproj index bc2672f..93df3ce 100644 --- a/msvc/zzuf.vcxproj +++ b/msvc/zzuf.vcxproj @@ -172,38 +172,36 @@ - - - + + + + - - - CompileAsCpp - CompileAsCpp - CompileAsCpp - CompileAsCpp - - + + + + CompileAsCpp + - \ No newline at end of file + diff --git a/src/Makefile.am b/src/Makefile.am index 15d065c..81a6662 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -3,12 +3,12 @@ bin_PROGRAMS = zzuf zzat pkglib_LTLIBRARIES = libzzuf.la ZZUF = \ - zzuf.c opts.c opts.h md5.c md5.h timer.c timer.h myfork.c myfork.h \ - caca_getopt.c caca_getopt.h + zzuf.c opts.c opts.h timer.c timer.h myfork.c myfork.h \ + util/getopt.c util/getopt.h util/md5.c util/md5.h ZZAT = \ zzat.c \ - caca_getopt.c caca_getopt.h + util/getopt.c util/getopt.h LIBZZUF = \ libzzuf/libzzuf.c libzzuf/libzzuf.h \ @@ -23,7 +23,11 @@ COMMON = \ common/random.c common/random.h \ common/ranges.c common/ranges.h \ common/fd.c common/fd.h \ - common/fuzz.c common/fuzz.h + common/fuzz.c common/fuzz.h \ + util/mutex.h + +EXTRA_DIST = \ + util/regex.cpp util/regex.h zzuf_SOURCES = $(ZZUF) $(COMMON) zzuf_CFLAGS = -DLIBDIR=\"$(libdir)/zzuf\" -I$(srcdir)/common diff --git a/src/common/fd.c b/src/common/fd.c index 43a9c0d..12881e4 100644 --- a/src/common/fd.c +++ b/src/common/fd.c @@ -26,7 +26,7 @@ #include #if defined HAVE_REGEX_H # if _WIN32 -# include "../com_regexp.hpp" +# include "util/regex.h" # else # include # endif @@ -45,6 +45,7 @@ # include "debug.h" # include "network.h" #endif +#include "util/mutex.h" /* Regex stuff */ #if defined HAVE_REGEX_H @@ -74,34 +75,7 @@ static int *fds, static_fds[STATIC_FILES]; static int maxfd, nfiles; /* Spinlock. This variable protects the fds variable. */ -#if _WIN32 -static volatile LONG fd_spinlock = 0; -#elif __GNUC__ || __clang__ -static volatile int fd_spinlock = 0; -#else -# error "No known atomic operations for this platform" -#endif - -static void fd_lock(void) -{ -#if _WIN32 - do {} - while (InterlockedExchange(&fd_spinlock, 1)); -#elif __GNUC__ || __clang__ - do {} - while (__sync_lock_test_and_set(&fd_spinlock, 1)); -#endif -} - -static void fd_unlock(void) -{ -#if _WIN32 - InterlockedExchange(&fd_spinlock, 0); -#elif __GNUC__ || __clang__ - fd_spinlock = 0; - __sync_synchronize(); -#endif -} +static zz_mutex 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() @@ -262,7 +236,7 @@ int _zz_mustwatchw(wchar_t const *file) int _zz_iswatched(int fd) { int ret = 0; - fd_lock(); + zz_lock(&fds_mutex); if (fd < 0 || fd >= maxfd || fds[fd] == -1) goto early_exit; @@ -270,7 +244,7 @@ int _zz_iswatched(int fd) ret = 1; early_exit: - fd_unlock(); + zz_unlock(&fds_mutex); return ret; } @@ -278,7 +252,7 @@ void _zz_register(int fd) { int i; - fd_lock(); + zz_lock(&fds_mutex); if (fd < 0 || fd > 65535 || (fd < maxfd && fds[fd] != -1)) goto early_exit; @@ -348,12 +322,12 @@ void _zz_register(int fd) fds[fd] = i; early_exit: - fd_unlock(); + zz_unlock(&fds_mutex); } void _zz_unregister(int fd) { - fd_lock(); + zz_lock(&fds_mutex); if (fd < 0 || fd >= maxfd || fds[fd] == -1) goto early_exit; @@ -367,12 +341,12 @@ void _zz_unregister(int fd) fds[fd] = -1; early_exit: - fd_unlock(); + zz_unlock(&fds_mutex); } -void _zz_lock(int fd) +void _zz_lockfd(int fd) { - fd_lock(); + zz_lock(&fds_mutex); if (fd < -1 || fd >= maxfd || fds[fd] == -1) goto early_exit; @@ -383,12 +357,12 @@ void _zz_lock(int fd) files[fds[fd]].locked++; early_exit: - fd_unlock(); + zz_unlock(&fds_mutex); } void _zz_unlock(int fd) { - fd_lock(); + zz_lock(&fds_mutex); if (fd < -1 || fd >= maxfd || fds[fd] == -1) goto early_exit; @@ -399,13 +373,13 @@ void _zz_unlock(int fd) files[fds[fd]].locked--; early_exit: - fd_unlock(); + zz_unlock(&fds_mutex); } int _zz_islocked(int fd) { int ret = 0; - fd_lock(); + zz_lock(&fds_mutex); if (fd < -1 || fd >= maxfd || fds[fd] == -1) goto early_exit; @@ -416,14 +390,14 @@ int _zz_islocked(int fd) ret = files[fds[fd]].locked; early_exit: - fd_unlock(); + zz_unlock(&fds_mutex); return ret; } int _zz_isactive(int fd) { int ret = 1; - fd_lock(); + zz_lock(&fds_mutex); if (fd < 0 || fd >= maxfd || fds[fd] == -1) goto early_exit; @@ -431,14 +405,14 @@ int _zz_isactive(int fd) ret = files[fds[fd]].active; early_exit: - fd_unlock(); + zz_unlock(&fds_mutex); return ret; } int64_t _zz_getpos(int fd) { int64_t ret = 0; - fd_lock(); + zz_lock(&fds_mutex); if (fd < 0 || fd >= maxfd || fds[fd] == -1) goto early_exit; @@ -446,13 +420,13 @@ int64_t _zz_getpos(int fd) ret = files[fds[fd]].pos; early_exit: - fd_unlock(); + zz_unlock(&fds_mutex); return ret; } void _zz_setpos(int fd, int64_t pos) { - fd_lock(); + zz_lock(&fds_mutex); if (fd < 0 || fd >= maxfd || fds[fd] == -1) goto early_exit; @@ -460,12 +434,12 @@ void _zz_setpos(int fd, int64_t pos) files[fds[fd]].pos = pos; early_exit: - fd_unlock(); + zz_unlock(&fds_mutex); } void _zz_addpos(int fd, int64_t off) { - fd_lock(); + zz_lock(&fds_mutex); if (fd < 0 || fd >= maxfd || fds[fd] == -1) goto early_exit; @@ -473,12 +447,12 @@ void _zz_addpos(int fd, int64_t off) files[fds[fd]].pos += off; early_exit: - fd_unlock(); + zz_unlock(&fds_mutex); } void _zz_setfuzzed(int fd, int count) { - fd_lock(); + zz_lock(&fds_mutex); if (fd < 0 || fd >= maxfd || fds[fd] == -1) goto early_exit; @@ -496,13 +470,13 @@ void _zz_setfuzzed(int fd, int count) files[fds[fd]].already_fuzzed = count; early_exit: - fd_unlock(); + zz_unlock(&fds_mutex); } int _zz_getfuzzed(int fd) { int ret = 0; - fd_lock(); + zz_lock(&fds_mutex); if (fd < 0 || fd >= maxfd || fds[fd] == -1) goto early_exit; @@ -518,14 +492,14 @@ int _zz_getfuzzed(int fd) - files[fds[fd]].pos); early_exit: - fd_unlock(); + zz_unlock(&fds_mutex); return ret; } struct fuzz *_zz_getfuzz(int fd) { struct fuzz *ret = NULL; - fd_lock(); + zz_lock(&fds_mutex); if (fd < 0 || fd >= maxfd || fds[fd] == -1) goto early_exit; @@ -533,7 +507,7 @@ struct fuzz *_zz_getfuzz(int fd) ret = &files[fds[fd]].fuzz; early_exit: - fd_unlock(); + zz_unlock(&fds_mutex); return ret; } diff --git a/src/common/fd.h b/src/common/fd.h index 9aad80c..9dbba48 100644 --- a/src/common/fd.h +++ b/src/common/fd.h @@ -32,7 +32,7 @@ extern int _zz_mustwatchw(wchar_t const *); extern int _zz_iswatched(int); extern void _zz_register(int); extern void _zz_unregister(int); -extern void _zz_lock(int); +extern void _zz_lockfd(int); extern void _zz_unlock(int); extern int _zz_islocked(int); extern int _zz_isactive(int); diff --git a/src/common/random.c b/src/common/random.c index a08c751..4344405 100644 --- a/src/common/random.c +++ b/src/common/random.c @@ -35,11 +35,9 @@ void _zz_srand(uint32_t seed) uint32_t _zz_rand(uint32_t max) { /* Could be better, but do we care? */ - long hi, lo, x; - - hi = ctx / 12773L; - lo = ctx % 12773L; - x = 16807L * lo - 2836L * hi; + long hi = ctx / 12773L; + long lo = ctx % 12773L; + long x = 16807L * lo - 2836L * hi; if (x <= 0) x += 0x7fffffffL; return (ctx = x) % (unsigned long)max; diff --git a/src/libzzuf/lib-fd.c b/src/libzzuf/lib-fd.c index ab97f45..32a13c5 100644 --- a/src/libzzuf/lib-fd.c +++ b/src/libzzuf/lib-fd.c @@ -643,7 +643,7 @@ int NEW(aio_read)(struct aiocb *aiocbp) if (!_zz_ready || !_zz_iswatched(fd) || !_zz_isactive(fd)) return ORIG(aio_read)(aiocbp); - _zz_lock(fd); + _zz_lockfd(fd); ret = ORIG(aio_read)(aiocbp); debug("%s({%i, %i, %i, %p, %li, ..., %li}) = %i", __func__, diff --git a/src/libzzuf/lib-stream.c b/src/libzzuf/lib-stream.c index 3a119f0..4ee1058 100644 --- a/src/libzzuf/lib-stream.c +++ b/src/libzzuf/lib-stream.c @@ -267,7 +267,7 @@ static inline void debug_stream(char const *prefix, FILE *stream) LOADSYM(myfopen); \ if (!_zz_ready) \ return ORIG(myfopen)(path, mode); \ - _zz_lock(-1); \ + _zz_lockfd(-1); \ ret = ORIG(myfopen)(path, mode); \ _zz_unlock(-1); \ if (ret && _zz_mustwatch(path)) \ @@ -290,7 +290,7 @@ static inline void debug_stream(char const *prefix, FILE *stream) _zz_unregister(fd0); \ disp = 1; \ } \ - _zz_lock(-1); \ + _zz_lockfd(-1); \ ret = ORIG(myfreopen)(path, mode, stream); \ _zz_unlock(-1); \ if (ret && _zz_mustwatch(path)) \ @@ -376,7 +376,7 @@ FILE *NEW(__freopen64)(const char *path, const char *mode, FILE *stream) oldpos = ZZ_FTELL(stream); \ oldoff = get_stream_off(stream); \ oldcnt = get_stream_cnt(stream); \ - _zz_lock(fd); \ + _zz_lockfd(fd); \ ret = ORIG(myfseek)(stream, offset, whence); \ _zz_unlock(fd); \ newpos = ZZ_FTELL(stream); \ @@ -414,7 +414,7 @@ FILE *NEW(__freopen64)(const char *path, const char *mode, FILE *stream) oldpos = ZZ_FTELL(stream); \ oldoff = get_stream_off(stream); \ oldcnt = get_stream_cnt(stream); \ - _zz_lock(fd); \ + _zz_lockfd(fd); \ ret = ORIG(myfsetpos)(stream, pos); \ _zz_unlock(fd); \ newpos = ZZ_FTELL(stream); \ @@ -450,7 +450,7 @@ FILE *NEW(__freopen64)(const char *path, const char *mode, FILE *stream) oldpos = ZZ_FTELL(stream); \ oldoff = get_stream_off(stream); \ oldcnt = get_stream_cnt(stream); \ - _zz_lock(fd); \ + _zz_lockfd(fd); \ ORIG(rewind)(stream); \ _zz_unlock(fd); \ newpos = ZZ_FTELL(stream); \ @@ -542,7 +542,7 @@ void NEW(rewind)(FILE *stream) /* FIXME: ftell() will return -1 on a pipe such as stdin */ \ oldpos = ZZ_FTELL(stream); \ oldcnt = get_stream_cnt(stream); \ - _zz_lock(fd); \ + _zz_lockfd(fd); \ ret = ORIG(myfread) myargs; \ _zz_unlock(fd); \ newpos = ZZ_FTELL(stream); \ @@ -631,7 +631,7 @@ size_t NEW(__fread_unlocked_chk)(void *ptr, size_t ptrlen, size_t size, debug_stream("before", s); \ oldpos = ZZ_FTELL(s); \ oldcnt = get_stream_cnt(s); \ - _zz_lock(fd); \ + _zz_lockfd(fd); \ ret = ORIG(myfgetc)(arg); \ _zz_unlock(fd); \ newpos = ZZ_FTELL(s); \ @@ -738,7 +738,7 @@ int NEW(fgetc_unlocked)(FILE *stream) for (int i = 0; i < size - 1; ++i) \ { \ int chr; \ - _zz_lock(fd); \ + _zz_lockfd(fd); \ chr = ORIG(myfgetc)(stream); \ _zz_unlock(fd); \ newpos = oldpos + 1; \ @@ -832,7 +832,7 @@ int NEW(ungetc)(int c, FILE *stream) debug_stream("before", stream); oldpos = ZZ_FTELL(stream); - _zz_lock(fd); + _zz_lockfd(fd); ret = ORIG(ungetc)(c, stream); _zz_unlock(fd); _zz_setpos(fd, oldpos - 1); @@ -860,7 +860,7 @@ int NEW(fclose)(FILE *fp) return ORIG(fclose)(fp); debug_stream("before", fp); - _zz_lock(fd); + _zz_lockfd(fd); ret = ORIG(fclose)(fp); _zz_unlock(fd); debug("%s([%i]) = %i", __func__, fd, ret); @@ -906,7 +906,7 @@ int NEW(fclose)(FILE *fp) *lineptr = line; \ break; \ } \ - _zz_lock(fd); \ + _zz_lockfd(fd); \ chr = ORIG(fgetc)(stream); \ _zz_unlock(fd); \ newpos = oldpos + 1; \ @@ -1011,7 +1011,7 @@ char *NEW(fgetln)(FILE *stream, size_t *len) { int chr; - _zz_lock(fd); + _zz_lockfd(fd); chr = ORIG(fgetc)(stream); _zz_unlock(fd); @@ -1079,7 +1079,7 @@ char *NEW(fgetln)(FILE *stream, size_t *len) return ORIG(myrefill)(fp); \ debug_stream("before", fp); \ pos = _zz_getpos(fd); \ - _zz_lock(fd); \ + _zz_lockfd(fd); \ ret = ORIG(myrefill)(fp); \ newpos = lseek(fd, 0, SEEK_CUR); \ _zz_unlock(fd); \ diff --git a/src/myfork.c b/src/myfork.c index 3f738b3..5cb3264 100644 --- a/src/myfork.c +++ b/src/myfork.c @@ -50,7 +50,6 @@ #include "fd.h" #include "fuzz.h" #include "myfork.h" -#include "md5.h" #include "timer.h" /* Handle old libtool versions */ diff --git a/src/opts.c b/src/opts.c index 70ab5fc..ce8e5e6 100644 --- a/src/opts.c +++ b/src/opts.c @@ -36,14 +36,17 @@ void _zz_opts_init(struct opts *opts) opts->fuzzing = opts->bytes = opts->list = opts->ports = NULL; opts->allow = NULL; opts->protect = opts->refuse = NULL; + opts->seed = DEFAULT_SEED; opts->endseed = DEFAULT_SEED + 1; opts->minratio = opts->maxratio = DEFAULT_RATIO; - opts->quiet = 0; + + opts->b_quiet = 0; + opts->b_md5 = 0; + opts->b_checkexit = 0; + opts->b_verbose = 0; + opts->maxbytes = -1; - opts->md5 = 0; - opts->checkexit = 0; - opts->verbose = 0; opts->maxmem = DEFAULT_MEM; opts->starttime = _zz_time(); opts->maxtime = 0; diff --git a/src/opts.h b/src/opts.h index 4b6dc23..f902730 100644 --- a/src/opts.h +++ b/src/opts.h @@ -28,17 +28,22 @@ struct opts char **oldargv; int oldargc; char *fuzzing, *bytes, *list, *ports, *protect, *refuse, *allow; + uint32_t seed; uint32_t endseed; + double minratio; double maxratio; - int quiet; + + int b_md5; + int b_checkexit; + int b_verbose; + int b_quiet; + int maxbytes; int maxcpu; - int md5; - int checkexit; - int verbose; int maxmem; + int64_t starttime; int64_t maxtime; int64_t maxusertime; @@ -65,7 +70,7 @@ struct opts int bytes, seed; double ratio; int64_t date; - struct md5 *ctx; + struct zz_md5 *md5; char **newargv; } *child; }; diff --git a/src/caca_getopt.c b/src/util/getopt.c similarity index 75% rename from src/caca_getopt.c rename to src/util/getopt.c index a7749a7..8b2597f 100644 --- a/src/caca_getopt.c +++ b/src/util/getopt.c @@ -24,21 +24,21 @@ #endif #include -#include "caca_getopt.h" +#include "util/getopt.h" -int caca_optind = 1; -char *caca_optarg = NULL; +int zz_optind = 1; +char *zz_optarg = NULL; -int caca_getopt(int argc, char * const _argv[], char const *optstring, - struct caca_option const *longopts, int *longindex) +int zz_getopt(int argc, char * const _argv[], char const *optstring, + struct zz_option const *longopts, int *longindex) { #if defined HAVE_GETOPT_LONG - optind = caca_optind; - optarg = caca_optarg; + optind = zz_optind; + optarg = zz_optarg; int ret = getopt_long(argc, _argv, optstring, - (struct option const *)longopts, longindex); - caca_optind = optind; - caca_optarg = optarg; + (struct zz_option const *)longopts, longindex); + zz_optind = optind; + zz_optarg = optarg; return ret; #else @@ -48,10 +48,10 @@ int caca_getopt(int argc, char * const _argv[], char const *optstring, * programs. */ char **argv = (char **)(uintptr_t)_argv; - if (caca_optind >= argc) + if (zz_optind >= argc) return -1; - char *flag = argv[caca_optind]; + char *flag = argv[zz_optind]; if (flag[0] == '-' && flag[1] != '-') { @@ -63,21 +63,21 @@ int caca_getopt(int argc, char * const _argv[], char const *optstring, if (!tmp || ret == ':') return '?'; - caca_optind++; + zz_optind++; if (tmp[1] == ':') { if (flag[2] != '\0') - caca_optarg = flag + 2; + zz_optarg = flag + 2; else - caca_optarg = argv[caca_optind++]; + zz_optarg = argv[zz_optind++]; return ret; } if (flag[2] != '\0') { flag[1] = '-'; - caca_optind--; - argv[caca_optind]++; + zz_optind--; + argv[zz_optind]++; } return ret; @@ -102,15 +102,15 @@ int caca_getopt(int argc, char * const _argv[], char const *optstring, goto bad_opt; if (longindex) *longindex = i; - caca_optind++; - caca_optarg = flag + 2 + l + 1; + zz_optind++; + zz_optarg = flag + 2 + l + 1; return longopts[i].val; case '\0': if (longindex) *longindex = i; - caca_optind++; + zz_optind++; if (longopts[i].has_arg) - caca_optarg = argv[caca_optind++]; + zz_optarg = argv[zz_optind++]; return longopts[i].val; default: break; diff --git a/src/caca_getopt.h b/src/util/getopt.h similarity index 52% rename from src/caca_getopt.h rename to src/util/getopt.h index 3d33e6a..9a2b703 100644 --- a/src/caca_getopt.h +++ b/src/util/getopt.h @@ -14,12 +14,7 @@ * getopt.h: getopt_long reimplementation */ -/** \brief Option parsing. - * - * This structure contains commandline parsing information for systems - * where getopt_long() is unavailable. - */ -struct caca_option +struct zz_option { char const *name; int has_arg; @@ -27,14 +22,8 @@ struct caca_option int val; }; -/** \defgroup caca_process libcaca process management - * - * These functions help with various process handling tasks such as - * option parsing, DLL injection. - * - * @{ */ -extern int caca_optind; -extern char *caca_optarg; -extern int caca_getopt(int, char * const[], char const *, - struct caca_option const *, int *); +extern int zz_optind; +extern char *zz_optarg; +extern int zz_getopt(int, char * const[], char const *, + struct zz_option const *, int *); diff --git a/src/md5.c b/src/util/md5.c similarity index 96% rename from src/md5.c rename to src/util/md5.c index 2b8a581..e8fda74 100644 --- a/src/md5.c +++ b/src/util/md5.c @@ -28,9 +28,9 @@ #include #include -#include "md5.h" +#include "util/md5.h" -struct md5 +struct zz_md5 { uint32_t buf[4]; uint32_t bits[2]; @@ -40,9 +40,9 @@ struct md5 static void swapwords(uint32_t *buf, unsigned words); static void transform(uint32_t buf[4], uint32_t in[16]); -struct md5 *_zz_md5_init(void) +struct zz_md5 *zz_md5_init(void) { - struct md5 *ctx = malloc(sizeof(struct md5)); + struct zz_md5 *ctx = malloc(sizeof(struct zz_md5)); ctx->buf[0] = 0x67452301; ctx->buf[1] = 0xefcdab89; @@ -55,7 +55,7 @@ struct md5 *_zz_md5_init(void) return ctx; } -void _zz_md5_add(struct md5 *ctx, uint8_t *buf, unsigned len) +void zz_md5_add(struct zz_md5 *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 md5 *ctx, uint8_t *buf, unsigned len) memcpy(ctx->in, buf, len); } -void _zz_md5_fini(uint8_t *digest, struct md5 *ctx) +void zz_md5_fini(uint8_t *digest, struct zz_md5 *ctx) { unsigned count = (ctx->bits[0] >> 3) & 0x3F; uint8_t *p = (uint8_t *)ctx->in + count; diff --git a/src/md5.h b/src/util/md5.h similarity index 73% rename from src/md5.h rename to src/util/md5.h index a358610..2483ee2 100644 --- a/src/md5.h +++ b/src/util/md5.h @@ -16,7 +16,7 @@ struct md5; -extern struct md5 *_zz_md5_init(void); -extern void _zz_md5_add(struct md5 *ctx, uint8_t *buf, unsigned len); -extern void _zz_md5_fini(uint8_t *digest, struct md5 *ctx); +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); diff --git a/src/util/mutex.h b/src/util/mutex.h new file mode 100644 index 0000000..ca8731f --- /dev/null +++ b/src/util/mutex.h @@ -0,0 +1,45 @@ +/* + * zzuf - general purpose fuzzer + * + * Copyright © 2002—2015 Sam Hocevar + * + * This program is free software. It comes without any warranty, to + * the extent permitted by applicable law. You can redistribute it + * and/or modify it under the terms of the Do What the Fuck You Want + * to Public License, Version 2, as published by the WTFPL Task Force. + * See http://www.wtfpl.net/ for more details. + */ + +/* + * mutex.h: very simple spinlock routines + */ + +#if _WIN32 +typedef volatile LONG zz_mutex; +#elif __GNUC__ || __clang__ +typedef volatile int zz_mutex; +#else +# error "No known atomic operations for this platform" +#endif + +static inline void zz_lock(zz_mutex *l) +{ +#if _WIN32 + do {} + while (InterlockedExchange(l, 1)); +#elif __GNUC__ || __clang__ + do {} + while (__sync_lock_test_and_set(l, 1)); +#endif +} + +static inline void zz_unlock(zz_mutex *l) +{ +#if _WIN32 + InterlockedExchange(l, 0); +#elif __GNUC__ || __clang__ + *l = 0; + __sync_synchronize(); +#endif +} + diff --git a/src/com_regexp.cpp b/src/util/regex.cpp similarity index 96% rename from src/com_regexp.cpp rename to src/util/regex.cpp index 9968301..6adbe77 100644 --- a/src/com_regexp.cpp +++ b/src/util/regex.cpp @@ -1,4 +1,4 @@ -#include "com_regexp.hpp" +#include "util/regexp.h" #pragma once #pragma pack(push, 8) diff --git a/src/com_regexp.hpp b/src/util/regex.h similarity index 100% rename from src/com_regexp.hpp rename to src/util/regex.h diff --git a/src/zzat.c b/src/zzat.c index 0d1316e..0221cbb 100644 --- a/src/zzat.c +++ b/src/zzat.c @@ -50,7 +50,7 @@ typedef int ssize_t; #include #include -#include "caca_getopt.h" +#include "util/getopt.h" static int run(char const *sequence, char const *file); static void output(char const *buf, size_t len); @@ -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 caca_option long_options[] = + static struct zz_option long_options[] = { { "show-all", 0, NULL, 'A' }, { "number-nonblank", 0, NULL, 'b' }, @@ -104,7 +104,7 @@ int main(int argc, char *argv[]) { "version", 0, NULL, 'V' }, { NULL, 0, NULL, 0 } }; - int c = caca_getopt(argc, argv, OPTSTR, long_options, &option_index); + int c = zz_getopt(argc, argv, OPTSTR, long_options, &option_index); if (c == -1) break; @@ -130,7 +130,7 @@ int main(int argc, char *argv[]) g_number_lines = 1; break; case 'r': /* --repeat */ - g_repeat = atoi(caca_optarg); + g_repeat = atoi(zz_optarg); break; case 's': /* --squeeze-blank */ g_squeeze_lines = 1; @@ -145,9 +145,9 @@ int main(int argc, char *argv[]) g_escape_tabs = 1; break; case 'x': /* --execute */ - if (caca_optarg[0] == '=') - caca_optarg++; - sequence = caca_optarg; + if (zz_optarg[0] == '=') + zz_optarg++; + sequence = zz_optarg; break; case 'l': /* --list */ syntax(); @@ -165,14 +165,14 @@ int main(int argc, char *argv[]) } } - if (caca_optind >= argc) + if (zz_optind >= argc) { fprintf(stderr, "E: zzat: too few arguments\n"); return EXIT_FAILURE; } while (g_repeat-- > 0) - for (int i = caca_optind; i < argc; ++i) + for (int i = zz_optind; i < argc; ++i) { int ret = run(sequence, argv[i]); if (ret) diff --git a/src/zzuf.c b/src/zzuf.c index 9d55b5c..894d981 100644 --- a/src/zzuf.c +++ b/src/zzuf.c @@ -33,7 +33,7 @@ #endif #if defined HAVE_REGEX_H # if _WIN32 -# include "com_regexp.hpp" +# include "util/regex.h" # else # include # endif @@ -63,9 +63,9 @@ #include "fd.h" #include "fuzz.h" #include "myfork.h" -#include "md5.h" #include "timer.h" -#include "caca_getopt.h" +#include "util/getopt.h" +#include "util/md5.h" #if !defined SIGKILL # define SIGKILL 9 @@ -131,9 +131,9 @@ int main(int argc, char *argv[]) char *tmp; #if defined HAVE_REGEX_H char *include = NULL, *exclude = NULL; - int has_cmdline = 0; + int b_cmdline = 0; #endif - int debug = 0, has_network = 0; + int debug = 0, b_network = 0; #if defined _WIN32 InitializeCriticalSection(&_zz_pipe_cs); @@ -162,7 +162,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:vxhV" #define MOREINFO "Try `%s --help' for more information.\n" int option_index = 0; - static struct caca_option long_options[] = + static struct zz_option long_options[] = { /* Long option, needs arg, flag, short option */ { "allow", 1, NULL, 'a' }, @@ -205,7 +205,7 @@ int main(int argc, char *argv[]) { "version", 0, NULL, 'V' }, { NULL, 0, NULL, 0 } }; - int c = caca_getopt(argc, argv, OPTSTR, long_options, &option_index); + int c = zz_getopt(argc, argv, OPTSTR, long_options, &option_index); if (c == -1) break; @@ -213,28 +213,28 @@ int main(int argc, char *argv[]) switch (c) { case 'a': /* --allow */ - opts->allow = caca_optarg; + opts->allow = zz_optarg; break; case 'A': /* --autoinc */ setenv("ZZUF_AUTOINC", "1", 1); break; case 'b': /* --bytes */ - opts->bytes = caca_optarg; + opts->bytes = zz_optarg; break; case 'B': /* --max-bytes */ - if (caca_optarg[0] == '=') - caca_optarg++; - opts->maxbytes = atoi(caca_optarg); + if (zz_optarg[0] == '=') + zz_optarg++; + opts->maxbytes = atoi(zz_optarg); break; #if defined HAVE_REGEX_H case 'c': /* --cmdline */ - has_cmdline = 1; + b_cmdline = 1; break; #endif case 'C': /* --max-crashes */ - if (caca_optarg[0] == '=') - caca_optarg++; - opts->maxcrashes = atoi(caca_optarg); + if (zz_optarg[0] == '=') + zz_optarg++; + opts->maxcrashes = atoi(zz_optarg); if (opts->maxcrashes <= 0) opts->maxcrashes = 0; break; @@ -242,24 +242,24 @@ int main(int argc, char *argv[]) debug++; break; case 'D': /* --delay */ - if (caca_optarg[0] == '=') - caca_optarg++; - opts->delay = (int64_t)(atof(caca_optarg) * 1000000.0); + if (zz_optarg[0] == '=') + zz_optarg++; + opts->delay = (int64_t)(atof(zz_optarg) * 1000000.0); break; #if defined HAVE_REGEX_H case 'E': /* --exclude */ - exclude = merge_regex(exclude, caca_optarg); + exclude = merge_regex(exclude, zz_optarg); if (!exclude) { fprintf(stderr, "%s: invalid regex -- `%s'\n", - argv[0], caca_optarg); + argv[0], zz_optarg); _zz_opts_fini(opts); return EXIT_FAILURE; } break; #endif case 'f': /* --fuzzing */ - opts->fuzzing = caca_optarg; + opts->fuzzing = zz_optarg; break; case 'F': fprintf(stderr, "%s: `-F' is deprecated, use `-j'\n", argv[0]); @@ -270,77 +270,77 @@ int main(int argc, char *argv[]) break; #if defined HAVE_REGEX_H case 'I': /* --include */ - include = merge_regex(include, caca_optarg); + include = merge_regex(include, zz_optarg); if (!include) { fprintf(stderr, "%s: invalid regex -- `%s'\n", - argv[0], caca_optarg); + argv[0], zz_optarg); _zz_opts_fini(opts); return EXIT_FAILURE; } break; #endif case 'j': /* --jobs */ - if (caca_optarg[0] == '=') - caca_optarg++; - opts->maxchild = atoi(caca_optarg) > 1 ? atoi(caca_optarg) : 1; + if (zz_optarg[0] == '=') + zz_optarg++; + opts->maxchild = atoi(zz_optarg) > 1 ? atoi(zz_optarg) : 1; break; case 'l': /* --list */ - opts->list = caca_optarg; + opts->list = zz_optarg; break; case 'm': /* --md5 */ - opts->md5 = 1; + opts->b_md5 = 1; break; #if defined HAVE_SETRLIMIT && defined ZZUF_RLIMIT_MEM case 'M': /* --max-memory */ - if (caca_optarg[0] == '=') - caca_optarg++; - opts->maxmem = atoi(caca_optarg); + if (zz_optarg[0] == '=') + zz_optarg++; + opts->maxmem = atoi(zz_optarg); break; #endif case 'n': /* --network */ setenv("ZZUF_NETWORK", "1", 1); - has_network = 1; + b_network = 1; break; case 'O': /* --opmode */ - if (caca_optarg[0] == '=') - caca_optarg++; - if (!strcmp(caca_optarg, "preload")) + if (zz_optarg[0] == '=') + zz_optarg++; + if (!strcmp(zz_optarg, "preload")) opts->opmode = OPMODE_PRELOAD; - else if (!strcmp(caca_optarg, "copy")) + else if (!strcmp(zz_optarg, "copy")) opts->opmode = OPMODE_COPY; else { fprintf(stderr, "%s: invalid operating mode -- `%s'\n", - argv[0], caca_optarg); + argv[0], zz_optarg); _zz_opts_fini(opts); return EXIT_FAILURE; } break; case 'p': /* --ports */ - opts->ports = caca_optarg; + opts->ports = zz_optarg; break; case 'P': /* --protect */ - opts->protect = caca_optarg; + opts->protect = zz_optarg; break; case 'q': /* --quiet */ - opts->quiet = 1; + opts->b_quiet = 1; break; case 'r': /* --ratio */ - if (caca_optarg[0] == '=') - caca_optarg++; - tmp = strchr(caca_optarg, ':'); - opts->minratio = atof(caca_optarg); + if (zz_optarg[0] == '=') + zz_optarg++; + tmp = strchr(zz_optarg, ':'); + opts->minratio = atof(zz_optarg); opts->maxratio = tmp ? atof(tmp + 1) : opts->minratio; break; case 'R': /* --refuse */ - opts->refuse = caca_optarg; + opts->refuse = zz_optarg; break; case 's': /* --seed */ - if (caca_optarg[0] == '=') - caca_optarg++; - tmp = strchr(caca_optarg, ':'); - opts->seed = atol(caca_optarg); + if (zz_optarg[0] == '=') + zz_optarg++; + tmp = strchr(zz_optarg, ':'); + opts->seed = atol(zz_optarg); opts->endseed = tmp ? tmp[1] ? (uint32_t)atol(tmp + 1) : (uint32_t)-1L : opts->seed + 1; @@ -349,27 +349,27 @@ int main(int argc, char *argv[]) setenv("ZZUF_SIGNAL", "1", 1); break; case 't': /* --max-time */ - if (caca_optarg[0] == '=') - caca_optarg++; - opts->maxtime = (int64_t)atoi(caca_optarg) * 1000000; + if (zz_optarg[0] == '=') + zz_optarg++; + opts->maxtime = (int64_t)atoi(zz_optarg) * 1000000; break; #if defined HAVE_SETRLIMIT && defined ZZUF_RLIMIT_CPU case 'T': /* --max-cputime */ - if (caca_optarg[0] == '=') - caca_optarg++; - opts->maxcpu = (int)(atof(caca_optarg) + 0.5); + if (zz_optarg[0] == '=') + zz_optarg++; + opts->maxcpu = (int)(atof(zz_optarg) + 0.5); break; #endif case 'U': /* --max-usertime */ - if (caca_optarg[0] == '=') - caca_optarg++; - opts->maxusertime = (int64_t)(atof(caca_optarg) * 1000000.0); + if (zz_optarg[0] == '=') + zz_optarg++; + opts->maxusertime = (int64_t)(atof(zz_optarg) * 1000000.0); break; case 'x': /* --check-exit */ - opts->checkexit = 1; + opts->b_checkexit = 1; break; case 'v': /* --verbose */ - opts->verbose = 1; + opts->b_verbose = 1; break; case 'h': /* --help */ usage(); @@ -387,7 +387,7 @@ int main(int argc, char *argv[]) } } - if (opts->ports && !has_network) + if (opts->ports && !b_network) { fprintf(stderr, "%s: port option (-p) requires network fuzzing (-n)\n", argv[0]); @@ -396,7 +396,7 @@ int main(int argc, char *argv[]) return EXIT_FAILURE; } - if (opts->allow && !has_network) + if (opts->allow && !b_network) { fprintf(stderr, "%s: allow option (-a) requires network fuzzing (-n)\n", argv[0]); @@ -425,9 +425,9 @@ int main(int argc, char *argv[]) /* * Mode 1: asked to read from the standard input */ - if (caca_optind >= argc) + if (zz_optind >= argc) { - if (opts->verbose) + if (opts->b_verbose) { finfo(stderr, opts, opts->seed); fprintf(stderr, "reading from stdin\n"); @@ -450,11 +450,11 @@ int main(int argc, char *argv[]) else { #if defined HAVE_REGEX_H - if (has_cmdline) + if (b_cmdline) { int dashdash = 0; - for (int i = caca_optind + 1; i < argc; ++i) + for (int i = zz_optind + 1; i < argc; ++i) { if (dashdash) include = merge_file(include, argv[i]); @@ -512,9 +512,9 @@ int main(int argc, char *argv[]) opts->oldargv = argv; for (int i = 0; i < opts->maxchild; ++i) { - int len = argc - caca_optind; + int len = argc - zz_optind; opts->child[i].newargv = malloc((len + 1) * sizeof(char *)); - memcpy(opts->child[i].newargv, argv + caca_optind, + memcpy(opts->child[i].newargv, argv + zz_optind, len * sizeof(char *)); opts->child[i].newargv[len] = (char *)NULL; } @@ -534,7 +534,7 @@ int main(int argc, char *argv[]) if (opts->maxcrashes && opts->crashes >= opts->maxcrashes && opts->nchild == 0) { - if (opts->verbose) + if (opts->b_verbose) fprintf(stderr, "zzuf: maximum crash count reached, exiting\n"); break; @@ -543,7 +543,7 @@ int main(int argc, char *argv[]) if (opts->maxtime && _zz_time() - opts->starttime >= opts->maxtime && opts->nchild == 0) { - if (opts->verbose) + if (opts->b_verbose) fprintf(stderr, "zzuf: maximum running time reached, exiting\n"); break; @@ -564,10 +564,10 @@ int main(int argc, char *argv[]) static void loop_stdin(struct opts *opts) { - struct md5 *ctx = NULL; + struct zz_md5 *md5 = NULL; - if (opts->md5) - ctx = _zz_md5_init(); + if (opts->b_md5) + md5 = zz_md5_init(); _zz_register(0); @@ -593,8 +593,8 @@ static void loop_stdin(struct opts *opts) _zz_fuzz(0, buf, ret); _zz_addpos(0, ret); - if (opts->md5) - _zz_md5_add(ctx, buf, ret); + if (opts->b_md5) + zz_md5_add(md5, buf, ret); else while (ret) { int nw = 0; @@ -605,10 +605,10 @@ static void loop_stdin(struct opts *opts) } } - if (opts->md5) + if (opts->b_md5) { uint8_t md5sum[16]; - _zz_md5_fini(md5sum, ctx); + zz_md5_fini(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], @@ -715,7 +715,7 @@ static void spawn_children(struct opts *opts) int k = 0; - for (int j = caca_optind + 1; j < opts->oldargc; ++j) + for (int j = zz_optind + 1; j < opts->oldargc; ++j) { FILE *fpin = fopen(opts->oldargv[j], "r"); if (!fpin) @@ -734,7 +734,7 @@ static void spawn_children(struct opts *opts) continue; } - opts->child[slot].newargv[j - caca_optind] = strdup(tmpname); + opts->child[slot].newargv[j - zz_optind] = strdup(tmpname); _zz_register(k); while (!feof(fpin)) @@ -771,10 +771,10 @@ static void spawn_children(struct opts *opts) opts->child[slot].seed = opts->seed; opts->child[slot].ratio = _zz_getratio(); opts->child[slot].status = STATUS_RUNNING; - if (opts->md5) - opts->child[slot].ctx = _zz_md5_init(); + if (opts->b_md5) + opts->child[slot].md5 = zz_md5_init(); - if (opts->verbose) + if (opts->b_verbose) { finfo(stderr, opts, opts->child[slot].seed); fprintf(stderr, "launched `%s'\n", opts->child[slot].newargv[0]); @@ -801,7 +801,7 @@ static void clean_children(struct opts *opts) && opts->maxbytes >= 0 && opts->child[i].bytes > opts->maxbytes) { - if (opts->verbose) + if (opts->b_verbose) { finfo(stderr, opts, opts->child[i].seed); fprintf(stderr, "data output exceeded, sending SIGTERM\n"); @@ -821,7 +821,7 @@ static void clean_children(struct opts *opts) && opts->maxusertime >= 0 && now > opts->child[i].date + opts->maxusertime) { - if (opts->verbose) + if (opts->b_verbose) { finfo(stderr, opts, opts->child[i].seed); fprintf(stderr, "running time exceeded, sending SIGTERM\n"); @@ -844,7 +844,7 @@ static void clean_children(struct opts *opts) if (opts->child[i].status == STATUS_SIGTERM && now > opts->child[i].date + 2000000) { - if (opts->verbose) + if (opts->b_verbose) { finfo(stderr, opts, opts->child[i].seed); fprintf(stderr, "not responding, sending SIGKILL\n"); @@ -878,7 +878,7 @@ static void clean_children(struct opts *opts) if (pid <= 0) continue; - if (opts->checkexit && WIFEXITED(status) && WEXITSTATUS(status)) + if (opts->b_checkexit && WIFEXITED(status) && WEXITSTATUS(status)) { finfo(stderr, opts, opts->child[i].seed); fprintf(stderr, "exit %i\n", WEXITSTATUS(status)); @@ -904,7 +904,7 @@ static void clean_children(struct opts *opts) WTERMSIG(status), sig2name(WTERMSIG(status)), message); opts->crashes++; } - else if (opts->verbose) + else if (opts->b_verbose) { finfo(stderr, opts, opts->child[i].seed); if (WIFSIGNALED(status)) @@ -947,20 +947,20 @@ static void clean_children(struct opts *opts) if (opts->opmode == OPMODE_COPY) { - for (int j = caca_optind + 1; j < opts->oldargc; ++j) + for (int j = zz_optind + 1; j < opts->oldargc; ++j) { - if (opts->child[i].newargv[j - caca_optind] != opts->oldargv[j]) + if (opts->child[i].newargv[j - zz_optind] != opts->oldargv[j]) { - unlink(opts->child[i].newargv[j - caca_optind]); - free(opts->child[i].newargv[j - caca_optind]); - opts->child[i].newargv[j - caca_optind] = opts->oldargv[j]; + unlink(opts->child[i].newargv[j - zz_optind]); + free(opts->child[i].newargv[j - zz_optind]); + opts->child[i].newargv[j - zz_optind] = opts->oldargv[j]; } } } - if (opts->md5) + if (opts->b_md5) { - _zz_md5_fini(md5sum, opts->child[i].ctx); + zz_md5_fini(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], @@ -1012,8 +1012,8 @@ static void __stdcall read_child(DWORD err_code, DWORD nbr_of_bytes_transfered, if (co->fd_no != 0) /* either out or err fd */ co->opts->child[co->child_no].bytes += nbr_of_bytes_transfered; - if (co->opts->md5 && co->fd_no == 2) - _zz_md5_add(co->opts->child[co->child_no].ctx, co->buf, nbr_of_bytes_transfered); + if (co->opts->b_md5 && co->fd_no == 2) + zz_md5_add(co->opts->child[co->child_no].md5, co->buf, nbr_of_bytes_transfered); free(co); /* clean up allocated data */ } @@ -1116,9 +1116,9 @@ static void read_children(struct opts *opts) if (j != 0) opts->child[i].bytes += ret; - if (opts->md5 && j == 2) - _zz_md5_add(opts->child[i].ctx, buf, ret); - else if (!opts->quiet || j == 0) + if (opts->b_md5 && j == 2) + zz_md5_add(opts->child[i].md5, buf, ret); + else if (!opts->b_quiet || j == 0) write((j < 2) ? STDERR_FILENO : STDOUT_FILENO, buf, ret); } else if (ret == 0)