* Improved zfd_register() and zfd_* function performances.

* Changed function names here and there.
This commit is contained in:
Sam Hocevar
2006-12-28 20:22:32 +00:00
committed by sam
parent 8911c02237
commit 53157d4fbb
11 changed files with 211 additions and 142 deletions
+3 -3
View File
@@ -30,14 +30,14 @@
#include "debug.h"
extern int _zzuf_debug;
extern int _zz_hasdebug;
void zzuf_debug(const char *format, ...)
void _zz_debug(const char *format, ...)
{
va_list args;
int saved_errno;
if(!_zzuf_debug)
if(!_zz_hasdebug)
return;
saved_errno = errno;
+2 -2
View File
@@ -16,8 +16,8 @@
* debug.h: debugging support
*/
extern void zzuf_debug(const char *format, ...)
extern void _zz_debug(const char *format, ...)
__attribute__((__format__(__printf__, 1, 2)));
#define debug zzuf_debug
#define debug _zz_debug
+7 -7
View File
@@ -35,7 +35,7 @@
#define MAGIC1 0x33ea84f7
#define MAGIC2 0x783bc31f
void zzuf_fuzz(int fd, uint8_t *buf, uint64_t len)
void _zz_fuzz(int fd, uint8_t *buf, uint64_t len)
{
uint64_t start, stop;
struct fuzz *fuzz;
@@ -59,15 +59,15 @@ void zzuf_fuzz(int fd, uint8_t *buf, uint64_t len)
memset(fuzz->data, 0, CHUNKBYTES);
/* Add some random dithering to handle ratio < 1.0/CHUNKBYTES */
zzuf_srand(_zzuf_seed ^ chunkseed);
todo = (int)((_zzuf_ratio * (8 * CHUNKBYTES * 1000)
+ zzuf_rand(1000)) / 1000.0);
zzuf_srand(_zzuf_seed ^ chunkseed ^ (todo * MAGIC2));
_zz_srand(_zz_seed ^ chunkseed);
todo = (int)((_zz_ratio * (8 * CHUNKBYTES * 1000)
+ _zz_rand(1000)) / 1000.0);
_zz_srand(_zz_seed ^ chunkseed ^ (todo * MAGIC2));
while(todo--)
{
unsigned int idx = zzuf_rand(CHUNKBYTES);
uint8_t byte = (1 << zzuf_rand(8));
unsigned int idx = _zz_rand(CHUNKBYTES);
uint8_t byte = (1 << _zz_rand(8));
fuzz->data[idx] ^= byte;
}
+1 -1
View File
@@ -16,5 +16,5 @@
* fuzz.h: fuzz functions
*/
extern void zzuf_fuzz(int, uint8_t *, uint64_t);
extern void _zz_fuzz(int, uint8_t *, uint64_t);
+137 -70
View File
@@ -38,15 +38,66 @@
#include "load.h"
/* Global variables */
int _zzuf_ready = 0;
int _zzuf_debug = 0;
int _zzuf_seed = 0;
float _zzuf_ratio = 0.004f;
regex_t * _zzuf_include = NULL;
regex_t * _zzuf_exclude = NULL;
int _zz_ready = 0;
int _zz_hasdebug = 0;
int _zz_seed = 0;
float _zz_ratio = 0.004f;
regex_t * _zz_include = NULL;
regex_t * _zz_exclude = NULL;
#define MAXFD 1024
struct zzuf
/* Library initialisation shit */
void _zz_init(void)
{
char *tmp;
tmp = getenv("ZZUF_DEBUG");
if(tmp && *tmp)
_zz_hasdebug = 1;
tmp = getenv("ZZUF_SEED");
if(tmp && *tmp)
_zz_seed = atol(tmp);
tmp = getenv("ZZUF_RATIO");
if(tmp && *tmp)
_zz_ratio = atof(tmp);
if(_zz_ratio < 0.0f)
_zz_ratio = 0.0f;
else if(_zz_ratio > 5.0f)
_zz_ratio = 5.0f;
tmp = getenv("ZZUF_INCLUDE");
if(tmp && *tmp)
{
_zz_include = malloc(sizeof(*_zz_include));
regcomp(_zz_include, tmp, 0);
}
tmp = getenv("ZZUF_EXCLUDE");
if(tmp && *tmp)
{
_zz_exclude = malloc(sizeof(*_zz_exclude));
regcomp(_zz_exclude, tmp, 0);
}
zfd_init();
_zz_load_fd();
_zz_load_stream();
_zz_ready = 1;
debug("libzzuf initialised");
}
/* Deinitialisation */
void _zz_fini(void)
{
zfd_fini();
}
/* File descriptor stuff */
struct files
{
int managed;
uint64_t seed;
@@ -54,107 +105,123 @@ struct zzuf
/* Public stuff */
struct fuzz fuzz;
}
files[MAXFD];
*files;
/* Library initialisation shit */
void zzuf_init(void)
int *fds;
int maxfd, nfiles;
void zfd_init(void)
{
char *tmp;
int i;
files = NULL;
nfiles = 0;
tmp = getenv("ZZUF_DEBUG");
if(tmp && *tmp)
_zzuf_debug = 1;
tmp = getenv("ZZUF_SEED");
if(tmp && *tmp)
_zzuf_seed = atol(tmp);
tmp = getenv("ZZUF_RATIO");
if(tmp && *tmp)
_zzuf_ratio = atof(tmp);
if(_zzuf_ratio < 0.0f)
_zzuf_ratio = 0.0f;
else if(_zzuf_ratio > 5.0f)
_zzuf_ratio = 5.0f;
tmp = getenv("ZZUF_INCLUDE");
if(tmp && *tmp)
{
_zzuf_include = malloc(sizeof(*_zzuf_include));
regcomp(_zzuf_include, tmp, 0);
}
tmp = getenv("ZZUF_EXCLUDE");
if(tmp && *tmp)
{
_zzuf_exclude = malloc(sizeof(*_zzuf_exclude));
regcomp(_zzuf_exclude, tmp, 0);
}
for(i = 0; i < MAXFD; i++)
files[i].managed = 0;
zzuf_load_fd();
zzuf_load_stream();
_zzuf_ready = 1;
debug("libzzuf initialised");
/* Start with one fd in the lookup table */
fds = malloc(1 * sizeof(int));
for(maxfd = 0; maxfd < 1; maxfd++)
fds[maxfd] = -1;
}
/* Deinitialisation */
void zzuf_fini(void)
void zfd_fini(void)
{
int i;
for(i = 0; i < MAXFD; i++)
for(i = 0; i < maxfd; i++)
{
if(!files[i].managed)
if(!files[fds[i]].managed)
continue;
/* XXX: What are we supposed to do? If filedescriptors weren't
* closed properly, there's a leak, but it's not our problem. */
}
free(files);
free(fds);
}
/* fd stuff */
int zfd_ismanaged(int fd)
{
return files[fd].managed;
if(fd < 0 || fd >= maxfd || fds[fd] == -1)
return 0;
return 1;
}
void zfd_manage(int fd)
void zfd_register(int fd)
{
files[fd].managed = 1;
files[fd].pos = 0;
files[fd].fuzz.cur = -1;
files[fd].fuzz.data = malloc(CHUNKBYTES);
int i;
if(fd < 0 || fd > 65535 || (fd < maxfd && fds[fd] != -1))
return;
while(fd >= maxfd)
{
fds = realloc(fds, 2 * maxfd * sizeof(int));
for(i = maxfd; i < maxfd * 2; i++)
fds[i] = -1;
maxfd *= 2;
}
/* Find an empty slot */
for(i = 0; i < nfiles; i++)
if(files[i].managed == 0)
break;
/* No slot found, allocate memory */
if(i == nfiles)
{
nfiles++;
files = realloc(files, nfiles * sizeof(struct files));
}
files[i].managed = 1;
files[i].pos = 0;
files[i].fuzz.cur = -1;
files[i].fuzz.data = malloc(CHUNKBYTES);
fds[fd] = i;
}
void zfd_unmanage(int fd)
void zfd_unregister(int fd)
{
files[fd].managed = 0;
free(files[fd].fuzz.data);
if(fd < 0 || fd >= maxfd || fds[fd] == -1)
return;
files[fds[fd]].managed = 0;
free(files[fds[fd]].fuzz.data);
fds[fd] = -1;
}
long int zfd_getpos(int fd)
{
return files[fd].pos;
if(fd < 0 || fd >= maxfd || fds[fd] == -1)
return 0;
return files[fds[fd]].pos;
}
void zfd_setpos(int fd, long int pos)
{
files[fd].pos = pos;
if(fd < 0 || fd >= maxfd || fds[fd] == -1)
return;
files[fds[fd]].pos = pos;
}
void zfd_addpos(int fd, long int off)
{
files[fd].pos += off;
if(fd < 0 || fd >= maxfd || fds[fd] == -1)
return;
files[fds[fd]].pos += off;
}
struct fuzz *zfd_getfuzz(int fd)
{
return &files[fd].fuzz;
if(fd < 0 || fd >= maxfd || fds[fd] == -1)
return NULL;
return &files[fds[fd]].fuzz;
}
+12 -10
View File
@@ -29,21 +29,23 @@ struct fuzz
};
/* Internal variables */
extern int _zzuf_ready;
extern int _zzuf_debug;
extern int _zzuf_seed;
extern float _zzuf_ratio;
extern regex_t * _zzuf_include;
extern regex_t * _zzuf_exclude;
extern int _zz_ready;
extern int _zz_hasdebug;
extern int _zz_seed;
extern float _zz_ratio;
extern regex_t * _zz_include;
extern regex_t * _zz_exclude;
/* Library initialisation shit */
extern void zzuf_init(void) __attribute__((constructor));
extern void zzuf_fini(void) __attribute__((destructor));
extern void _zz_init(void) __attribute__((constructor));
extern void _zz_fini(void) __attribute__((destructor));
/* File descriptor handling */
extern void zfd_init(void);
extern void zfd_fini(void);
extern int zfd_ismanaged(int);
extern void zfd_manage(int);
extern void zfd_unmanage(int);
extern void zfd_register(int);
extern void zfd_unregister(int);
extern long int zfd_getpos(int);
extern void zfd_setpos(int, long int);
extern void zfd_addpos(int, long int);
+16 -16
View File
@@ -50,7 +50,7 @@ static off_t (*lseek_orig) (int fd, off_t offset, int whence);
static off64_t (*lseek64_orig) (int fd, off64_t offset, int whence);
static int (*close_orig) (int fd);
void zzuf_load_fd(void)
void _zz_load_fd(void)
{
LOADSYM(open);
LOADSYM(open64);
@@ -64,7 +64,7 @@ void zzuf_load_fd(void)
do \
{ \
int mode = 0; \
if(!_zzuf_ready) \
if(!_zz_ready) \
LOADSYM(fn); \
if(oflag & O_CREAT) \
{ \
@@ -78,16 +78,16 @@ void zzuf_load_fd(void)
{ \
ret = ORIG(fn)(file, oflag); \
} \
if(!_zzuf_ready) \
if(!_zz_ready) \
return ret; \
if(ret >= 0 \
&& ((oflag & (O_RDONLY | O_RDWR | O_WRONLY)) != O_WRONLY)) \
{ \
if(_zzuf_include && \
regexec(_zzuf_include, file, 0, NULL, 0) == REG_NOMATCH) \
if(_zz_include && \
regexec(_zz_include, file, 0, NULL, 0) == REG_NOMATCH) \
/* not included: ignore */ ; \
else if(_zzuf_exclude && \
regexec(_zzuf_exclude, file, 0, NULL, 0) != REG_NOMATCH) \
else if(_zz_exclude && \
regexec(_zz_exclude, file, 0, NULL, 0) != REG_NOMATCH) \
/* excluded: ignore */ ; \
else \
{ \
@@ -96,7 +96,7 @@ void zzuf_load_fd(void)
file, oflag, mode, ret); \
else \
debug(STR(fn) "(\"%s\", %i) = %i", file, oflag, ret); \
zfd_manage(ret); \
zfd_register(ret); \
} \
} \
} while(0)
@@ -115,16 +115,16 @@ ssize_t read(int fd, void *buf, size_t count)
{
int ret;
if(!_zzuf_ready)
if(!_zz_ready)
LOADSYM(read);
ret = read_orig(fd, buf, count);
if(!_zzuf_ready || !zfd_ismanaged(fd))
if(!_zz_ready || !zfd_ismanaged(fd))
return ret;
debug("read(%i, %p, %li) = %i", fd, buf, (long int)count, ret);
if(ret > 0)
{
zzuf_fuzz(fd, buf, ret);
_zz_fuzz(fd, buf, ret);
zfd_addpos(fd, ret);
}
@@ -137,10 +137,10 @@ ssize_t read(int fd, void *buf, size_t count)
#define LSEEK(fn, off_t) \
do { \
if(!_zzuf_ready) \
if(!_zz_ready) \
LOADSYM(fn); \
ret = ORIG(fn)(fd, offset, whence); \
if(!_zzuf_ready || !zfd_ismanaged(fd)) \
if(!_zz_ready || !zfd_ismanaged(fd)) \
return ret; \
debug(STR(fn)"(%i, %lli, %i) = %lli", \
fd, (long long int)offset, whence, (long long int)ret); \
@@ -166,14 +166,14 @@ int close(int fd)
{
int ret;
if(!_zzuf_ready)
if(!_zz_ready)
LOADSYM(close);
ret = close_orig(fd);
if(!_zzuf_ready || !zfd_ismanaged(fd))
if(!_zz_ready || !zfd_ismanaged(fd))
return ret;
debug("close(%i) = %i", fd, ret);
zfd_unmanage(fd);
zfd_unregister(fd);
return ret;
}
+27 -27
View File
@@ -55,7 +55,7 @@ static ssize_t (*getdelim_orig) (char **lineptr, size_t *n, int delim,
static ssize_t (*__getdelim_orig) (char **lineptr, size_t *n, int delim,
FILE *stream);
void zzuf_load_stream(void)
void _zz_load_stream(void)
{
LOADSYM(fopen);
LOADSYM(fopen64);
@@ -76,7 +76,7 @@ void zzuf_load_stream(void)
#define FOPEN(fn) \
do \
{ \
if(!_zzuf_ready) \
if(!_zz_ready) \
{ \
LOADSYM(fn); \
return ORIG(fn)(path, mode); \
@@ -84,16 +84,16 @@ void zzuf_load_stream(void)
ret = ORIG(fn)(path, mode); \
if(ret) \
{ \
if(_zzuf_include && \
regexec(_zzuf_include, path, 0, NULL, 0) == REG_NOMATCH) \
if(_zz_include && \
regexec(_zz_include, path, 0, NULL, 0) == REG_NOMATCH) \
/* not included: ignore */ ; \
else if(_zzuf_exclude && \
regexec(_zzuf_exclude, path, 0, NULL, 0) != REG_NOMATCH) \
else if(_zz_exclude && \
regexec(_zz_exclude, path, 0, NULL, 0) != REG_NOMATCH) \
/* excluded: ignore */ ; \
else \
{ \
int fd = fileno(ret); \
zfd_manage(fd); \
zfd_register(fd); \
debug(STR(fn) "(\"%s\", \"%s\") = %p", path, mode, ret); \
} \
} \
@@ -113,10 +113,10 @@ int fseek(FILE *stream, long offset, int whence)
{
int ret, fd;
if(!_zzuf_ready)
if(!_zz_ready)
LOADSYM(fseek);
fd = fileno(stream);
if(!_zzuf_ready || !zfd_ismanaged(fd))
if(!_zz_ready || !zfd_ismanaged(fd))
return fseek_orig(stream, offset, whence);
ret = fseek_orig(stream, offset, whence);
@@ -145,10 +145,10 @@ size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
size_t ret;
int fd;
if(!_zzuf_ready)
if(!_zz_ready)
LOADSYM(fread);
fd = fileno(stream);
if(!_zzuf_ready || !zfd_ismanaged(fd))
if(!_zz_ready || !zfd_ismanaged(fd))
return fread_orig(ptr, size, nmemb, stream);
pos = ftell(stream);
@@ -160,7 +160,7 @@ size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
/* XXX: the number of bytes read is not ret * size, because
* a partial read may have advanced the stream pointer */
long int newpos = ftell(stream);
zzuf_fuzz(fd, ptr, newpos - pos);
_zz_fuzz(fd, ptr, newpos - pos);
zfd_setpos(fd, newpos);
}
return ret;
@@ -169,16 +169,16 @@ size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
#define FGETC(fn) \
do { \
int fd; \
if(!_zzuf_ready) \
if(!_zz_ready) \
LOADSYM(fn); \
fd = fileno(stream); \
if(!_zzuf_ready || !zfd_ismanaged(fd)) \
if(!_zz_ready || !zfd_ismanaged(fd)) \
return ORIG(fn)(stream); \
ret = ORIG(fn)(stream); \
if(ret != EOF) \
{ \
uint8_t ch = ret; \
zzuf_fuzz(fd, &ch, 1); \
_zz_fuzz(fd, &ch, 1); \
zfd_addpos(fd, 1); \
ret = ch; \
} \
@@ -200,10 +200,10 @@ char *fgets(char *s, int size, FILE *stream)
char *ret = s;
int i, fd;
if(!_zzuf_ready)
if(!_zz_ready)
LOADSYM(fgets);
fd = fileno(stream);
if(!_zzuf_ready || !zfd_ismanaged(fd))
if(!_zz_ready || !zfd_ismanaged(fd))
return fgets_orig(s, size, stream);
if(size <= 0)
@@ -224,7 +224,7 @@ char *fgets(char *s, int size, FILE *stream)
break;
}
s[i] = (char)(unsigned char)ch;
zzuf_fuzz(fd, (uint8_t *)s + i, 1); /* rather inefficient */
_zz_fuzz(fd, (uint8_t *)s + i, 1); /* rather inefficient */
zfd_addpos(fd, 1);
if(s[i] == '\n')
{
@@ -243,14 +243,14 @@ int ungetc(int c, FILE *stream)
unsigned char ch = c;
int ret, fd;
if(!_zzuf_ready)
if(!_zz_ready)
LOADSYM(ungetc);
fd = fileno(stream);
if(!_zzuf_ready || !zfd_ismanaged(fd))
if(!_zz_ready || !zfd_ismanaged(fd))
return ungetc_orig(c, stream);
zfd_addpos(fd, -1);
zzuf_fuzz(fd, &ch, 1);
_zz_fuzz(fd, &ch, 1);
ret = ungetc_orig((int)ch, stream);
if(ret >= 0)
ret = c;
@@ -264,15 +264,15 @@ int fclose(FILE *fp)
{
int ret, fd;
if(!_zzuf_ready)
if(!_zz_ready)
LOADSYM(fclose);
fd = fileno(fp);
if(!_zzuf_ready || !zfd_ismanaged(fd))
if(!_zz_ready || !zfd_ismanaged(fd))
return fclose_orig(fp);
ret = fclose_orig(fp);
debug("fclose(%p) = %i", fp, ret);
zfd_unmanage(fd);
zfd_unregister(fd);
return ret;
}
@@ -282,10 +282,10 @@ int fclose(FILE *fp)
char *line; \
ssize_t done, size; \
int fd, finished = 0; \
if(!_zzuf_ready) \
if(!_zz_ready) \
LOADSYM(fn); \
fd = fileno(stream); \
if(!_zzuf_ready || !zfd_ismanaged(fd)) \
if(!_zz_ready || !zfd_ismanaged(fd)) \
return getdelim_orig(lineptr, n, delim, stream); \
line = *lineptr; \
size = line ? *n : 0; \
@@ -311,7 +311,7 @@ int fclose(FILE *fp)
else \
{ \
unsigned char c = ch; \
zzuf_fuzz(fd, &c, 1); /* even more inefficient */ \
_zz_fuzz(fd, &c, 1); /* even more inefficient */ \
line[done++] = c; \
zfd_addpos(fd, 1); \
if(c == delim) \
+2 -2
View File
@@ -26,6 +26,6 @@
abort(); \
} while(0)
extern void zzuf_load_fd(void);
extern void zzuf_load_stream(void);
extern void _zz_load_fd(void);
extern void _zz_load_stream(void);
+2 -2
View File
@@ -27,12 +27,12 @@
#include "random.h"
void zzuf_srand(uint32_t seed)
void _zz_srand(uint32_t seed)
{
srand(seed ^ 0x12345678);
}
uint32_t zzuf_rand(uint32_t max)
uint32_t _zz_rand(uint32_t max)
{
if(max <= RAND_MAX)
return rand() % max;
+2 -2
View File
@@ -16,6 +16,6 @@
* random.h: pseudorandom number generator
*/
void zzuf_srand(uint32_t);
uint32_t zzuf_rand(uint32_t);
void _zz_srand(uint32_t);
uint32_t _zz_rand(uint32_t);