From 606d50b39f1cfdf7a71aa819a49f84d5f5e1de8c Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Sat, 12 Dec 2009 22:19:47 +0000 Subject: [PATCH] Reorganise source code to better separate zzuf and libzzuf. Note: the Win32 build is now broken. --- src/Makefile.am | 34 +++++++++++++++++++++--------- src/{libzzuf.h => common/common.h} | 15 +------------ src/{ => common}/fd.c | 12 ++++++++--- src/{ => common}/fd.h | 0 src/{ => common}/fuzz.c | 8 +++++-- src/{ => common}/fuzz.h | 0 src/{ => common}/random.c | 0 src/{ => common}/random.h | 0 src/{ => common}/ranges.c | 2 +- src/{ => common}/ranges.h | 0 src/{ => libzzuf}/debug.c | 0 src/{ => libzzuf}/debug.h | 0 src/{ => libzzuf}/lib-fd.c | 0 src/{ => libzzuf}/lib-load.h | 0 src/{ => libzzuf}/lib-mem.c | 0 src/{ => libzzuf}/lib-signal.c | 0 src/{ => libzzuf}/lib-stream.c | 1 + src/{ => libzzuf}/libzzuf.c | 0 src/libzzuf/libzzuf.h | 31 +++++++++++++++++++++++++++ src/{ => libzzuf}/network.c | 0 src/{ => libzzuf}/network.h | 0 src/{ => libzzuf}/sys.c | 0 src/{ => libzzuf}/sys.h | 0 src/opts.c | 2 +- src/zzuf.c | 4 ++-- 25 files changed, 76 insertions(+), 33 deletions(-) rename src/{libzzuf.h => common/common.h} (80%) rename src/{ => common}/fd.c (98%) rename src/{ => common}/fd.h (100%) rename src/{ => common}/fuzz.c (98%) rename src/{ => common}/fuzz.h (100%) rename src/{ => common}/random.c (100%) rename src/{ => common}/random.h (100%) rename src/{ => common}/ranges.c (99%) rename src/{ => common}/ranges.h (100%) rename src/{ => libzzuf}/debug.c (100%) rename src/{ => libzzuf}/debug.h (100%) rename src/{ => libzzuf}/lib-fd.c (100%) rename src/{ => libzzuf}/lib-load.h (100%) rename src/{ => libzzuf}/lib-mem.c (100%) rename src/{ => libzzuf}/lib-signal.c (100%) rename src/{ => libzzuf}/lib-stream.c (99%) rename src/{ => libzzuf}/libzzuf.c (100%) create mode 100644 src/libzzuf/libzzuf.h rename src/{ => libzzuf}/network.c (100%) rename src/{ => libzzuf}/network.h (100%) rename src/{ => libzzuf}/sys.c (100%) rename src/{ => libzzuf}/sys.h (100%) diff --git a/src/Makefile.am b/src/Makefile.am index 485bddb..011d97b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,22 +1,36 @@ # $Id$ -COMMON = random.c random.h ranges.c ranges.h fd.c fd.h fuzz.c fuzz.h +bin_PROGRAMS = zzuf +pkglib_LTLIBRARIES = libzzuf.la + +ZZUF = \ + zzuf.c opts.c opts.h md5.c md5.h timer.c timer.h + +LIBZZUF = \ + libzzuf/libzzuf.c libzzuf/libzzuf.h \ + libzzuf/debug.c libzzuf/debug.h \ + libzzuf/sys.c libzzuf/sys.h \ + libzzuf/network.c libzzuf/network.h \ + libzzuf/lib-fd.c libzzuf/lib-mem.c libzzuf/lib-signal.c \ + libzzuf/lib-stream.c libzzuf/lib-load.h + +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 + if NEED_GETOPT_LONG GETOPT = mygetopt.c mygetopt.h endif -bin_PROGRAMS = zzuf -zzuf_SOURCES = zzuf.c $(COMMON) $(GETOPT) \ - opts.c opts.h md5.c md5.h timer.c timer.h -zzuf_CFLAGS = -DLIBDIR=\"$(libdir)/zzuf\" +zzuf_SOURCES = $(ZZUF) $(COMMON) $(GETOPT) +zzuf_CFLAGS = -DLIBDIR=\"$(libdir)/zzuf\" -Icommon/ zzuf_LDFLAGS = $(MATH_LIBS) $(WINSOCK2_LIBS) zzuf_DEPENDENCIES = libzzuf.la -pkglib_LTLIBRARIES = libzzuf.la -libzzuf_la_SOURCES = libzzuf.c libzzuf.h $(COMMON) debug.c debug.h \ - sys.c sys.h network.c network.h \ - lib-fd.c lib-mem.c lib-signal.c lib-stream.c lib-load.h -libzzuf_la_CFLAGS = -DLIBZZUF +libzzuf_la_SOURCES = $(LIBZZUF) $(COMMON) +libzzuf_la_CFLAGS = -DLIBZZUF -Ilibzzuf/ -Icommon/ libzzuf_la_LDFLAGS = -avoid-version -no-undefined $(DLL_LDFLAGS) libzzuf_la_LIBADD = $(GETOPT_LIBS) $(DL_LIBS) $(MATH_LIBS) $(WINSOCK2_LIBS) diff --git a/src/libzzuf.h b/src/common/common.h similarity index 80% rename from src/libzzuf.h rename to src/common/common.h index 15297d3..a7448a5 100644 --- a/src/libzzuf.h +++ b/src/common/common.h @@ -13,7 +13,7 @@ */ /* - * libzzuf.h: preloaded wrapper library + * common.h: default fuzzing settings */ /* We arbitrarily split files into 1024-byte chunks. Each chunk has an @@ -47,16 +47,3 @@ struct fuzz uint8_t data[CHUNKBYTES]; }; -/* Internal variables */ -extern int _zz_ready; -extern int _zz_disabled; -extern int _zz_debuglevel; -extern int _zz_debugfd; -extern int _zz_signal; -extern int _zz_memory; -extern int _zz_network; -extern int _zz_autoinc; - -/* This function is needed to initialise memory functions */ -extern void _zz_mem_init(void); - diff --git a/src/fd.c b/src/common/fd.c similarity index 98% rename from src/fd.c rename to src/common/fd.c index bfa28eb..6193779 100644 --- a/src/fd.c +++ b/src/common/fd.c @@ -31,12 +31,14 @@ #include #include -#include "debug.h" -#include "libzzuf.h" +#include "common.h" #include "fd.h" #include "fuzz.h" -#include "network.h" #include "ranges.h" +#if defined LIBZZUF +# include "debug.h" +# include "network.h" +#endif /* Regex stuff */ #if defined HAVE_REGEX_H @@ -223,8 +225,10 @@ void _zz_register(int fd) if(fd < 0 || fd > 65535 || (fd < maxfd && fds[fd] != -1)) return; +#if defined LIBZZUF if(autoinc) debug2("using seed %li", (long int)seed); +#endif /* If filedescriptor is outside our bounds */ while(fd >= maxfd) @@ -375,7 +379,9 @@ void _zz_setfuzzed(int fd, int count) && count <= files[fds[fd]].already_fuzzed) return; +#if defined LIBZZUF debug2("setfuzzed(%i, %i)", fd, count); +#endif files[fds[fd]].already_pos = files[fds[fd]].pos; files[fds[fd]].already_fuzzed = count; diff --git a/src/fd.h b/src/common/fd.h similarity index 100% rename from src/fd.h rename to src/common/fd.h diff --git a/src/fuzz.c b/src/common/fuzz.c similarity index 98% rename from src/fuzz.c rename to src/common/fuzz.c index 7312277..d2ac35b 100644 --- a/src/fuzz.c +++ b/src/common/fuzz.c @@ -27,12 +27,14 @@ #include #include -#include "libzzuf.h" -#include "debug.h" +#include "common.h" #include "random.h" #include "fuzz.h" #include "fd.h" #include "ranges.h" +#if defined LIBZZUF +# include "debug.h" +#endif #define MAGIC1 0x33ea84f7 #define MAGIC2 0x783bc31f @@ -91,7 +93,9 @@ void _zz_fuzz(int fd, volatile uint8_t *buf, int64_t len) int64_t i, j; int todo; +#if defined LIBZZUF debug2("fuzz(%i, @%lli, %lli)", fd, (long long int)pos, (long long int)len); +#endif aligned_buf = buf - pos; fuzz = _zz_getfuzz(fd); diff --git a/src/fuzz.h b/src/common/fuzz.h similarity index 100% rename from src/fuzz.h rename to src/common/fuzz.h diff --git a/src/random.c b/src/common/random.c similarity index 100% rename from src/random.c rename to src/common/random.c diff --git a/src/random.h b/src/common/random.h similarity index 100% rename from src/random.h rename to src/common/random.h diff --git a/src/ranges.c b/src/common/ranges.c similarity index 99% rename from src/ranges.c rename to src/common/ranges.c index 6c56acf..adc66d5 100644 --- a/src/ranges.c +++ b/src/common/ranges.c @@ -26,7 +26,7 @@ #include #include -#include "libzzuf.h" +#include "common.h" #include "ranges.h" /* This function converts a string containing a list of ranges in the format diff --git a/src/ranges.h b/src/common/ranges.h similarity index 100% rename from src/ranges.h rename to src/common/ranges.h diff --git a/src/debug.c b/src/libzzuf/debug.c similarity index 100% rename from src/debug.c rename to src/libzzuf/debug.c diff --git a/src/debug.h b/src/libzzuf/debug.h similarity index 100% rename from src/debug.h rename to src/libzzuf/debug.h diff --git a/src/lib-fd.c b/src/libzzuf/lib-fd.c similarity index 100% rename from src/lib-fd.c rename to src/libzzuf/lib-fd.c diff --git a/src/lib-load.h b/src/libzzuf/lib-load.h similarity index 100% rename from src/lib-load.h rename to src/libzzuf/lib-load.h diff --git a/src/lib-mem.c b/src/libzzuf/lib-mem.c similarity index 100% rename from src/lib-mem.c rename to src/libzzuf/lib-mem.c diff --git a/src/lib-signal.c b/src/libzzuf/lib-signal.c similarity index 100% rename from src/lib-signal.c rename to src/libzzuf/lib-signal.c diff --git a/src/lib-stream.c b/src/libzzuf/lib-stream.c similarity index 99% rename from src/lib-stream.c rename to src/libzzuf/lib-stream.c index b352fa9..dc37e7a 100644 --- a/src/lib-stream.c +++ b/src/libzzuf/lib-stream.c @@ -54,6 +54,7 @@ # include /* Needed for __srefill’s lseek() call */ #endif +#include "common.h" #include "libzzuf.h" #include "lib-load.h" #include "debug.h" diff --git a/src/libzzuf.c b/src/libzzuf/libzzuf.c similarity index 100% rename from src/libzzuf.c rename to src/libzzuf/libzzuf.c diff --git a/src/libzzuf/libzzuf.h b/src/libzzuf/libzzuf.h new file mode 100644 index 0000000..8d06510 --- /dev/null +++ b/src/libzzuf/libzzuf.h @@ -0,0 +1,31 @@ +/* + * zzuf - general purpose fuzzer + * Copyright (c) 2006-2009 Sam Hocevar + * All Rights Reserved + * + * $Id$ + * + * 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 Sam Hocevar. See + * http://sam.zoy.org/wtfpl/COPYING for more details. + */ + +/* + * libzzuf.h: preloaded wrapper library + */ + +/* Internal variables */ +extern int _zz_ready; +extern int _zz_disabled; +extern int _zz_debuglevel; +extern int _zz_debugfd; +extern int _zz_signal; +extern int _zz_memory; +extern int _zz_network; +extern int _zz_autoinc; + +/* This function is needed to initialise memory functions */ +extern void _zz_mem_init(void); + diff --git a/src/network.c b/src/libzzuf/network.c similarity index 100% rename from src/network.c rename to src/libzzuf/network.c diff --git a/src/network.h b/src/libzzuf/network.h similarity index 100% rename from src/network.h rename to src/libzzuf/network.h diff --git a/src/sys.c b/src/libzzuf/sys.c similarity index 100% rename from src/sys.c rename to src/libzzuf/sys.c diff --git a/src/sys.h b/src/libzzuf/sys.h similarity index 100% rename from src/sys.h rename to src/libzzuf/sys.h diff --git a/src/opts.c b/src/opts.c index 0899ebc..a750bf5 100644 --- a/src/opts.c +++ b/src/opts.c @@ -28,7 +28,7 @@ #include #include -#include "libzzuf.h" +#include "common.h" #include "opts.h" void _zz_opts_init(struct opts *opts) diff --git a/src/zzuf.c b/src/zzuf.c index 179059f..7847957 100644 --- a/src/zzuf.c +++ b/src/zzuf.c @@ -64,7 +64,7 @@ # include #endif -#include "libzzuf.h" +#include "common.h" #include "opts.h" #include "random.h" #include "fd.h" @@ -1256,7 +1256,7 @@ static intptr_t get_entry_point_offset(char const *name) nt = (PIMAGE_NT_HEADERS)((char *)base + dos->e_lfanew); if(dos->e_magic == IMAGE_DOS_SIGNATURE /* 0x5A4D */ && nt->Signature == IMAGE_NT_SIGNATURE /* 0x00004550 */ - && nt->FileHeader.Machine == IMAGE_FILE_MACHINE_I386 + && nt->FileHeader.Machine == IMAGE_FILE_MACHINE_I386 && nt->OptionalHeader.Magic == 0x10b /* IMAGE_NT_OPTIONAL_HDR32_MAGIC */) { ret = (intptr_t)nt->OptionalHeader.AddressOfEntryPoint;