* Replaced _zz_disabled with a less stinky API. Still not thread safe, does
not recover very well from errors, but why care?
This commit is contained in:
parent
b0d8650bec
commit
52bdf5ff3e
41
src/fd.c
41
src/fd.c
@ -45,7 +45,7 @@ static int has_include = 0, has_exclude = 0;
|
||||
#define STATIC_FILES 32
|
||||
static struct files
|
||||
{
|
||||
int managed;
|
||||
int managed, locked;
|
||||
uint64_t pos;
|
||||
/* Public stuff */
|
||||
struct fuzz fuzz;
|
||||
@ -54,6 +54,11 @@ static struct files
|
||||
static int *fds, static_fds[STATIC_FILES];
|
||||
static int maxfd, nfiles;
|
||||
|
||||
/* Create lock. This lock variable is used to disable file descriptor
|
||||
* creation wrappers. For instance on Mac OS X, fopen() calls open()
|
||||
* and we don’t want open() to do any zzuf-related stuff. */
|
||||
static int create_lock;
|
||||
|
||||
static int32_t seed = DEFAULT_SEED;
|
||||
static double minratio = DEFAULT_RATIO;
|
||||
static double maxratio = DEFAULT_RATIO;
|
||||
@ -219,6 +224,7 @@ void _zz_register(int fd)
|
||||
}
|
||||
|
||||
files[i].managed = 1;
|
||||
files[i].locked = 0;
|
||||
files[i].pos = 0;
|
||||
files[i].fuzz.seed = seed;
|
||||
files[i].fuzz.ratio = _zz_getratio();
|
||||
@ -247,6 +253,39 @@ void _zz_unregister(int fd)
|
||||
fds[fd] = -1;
|
||||
}
|
||||
|
||||
void _zz_lock(int fd)
|
||||
{
|
||||
if(fd < -1 || fd >= maxfd || fds[fd] == -1)
|
||||
return;
|
||||
|
||||
if(fd == -1)
|
||||
create_lock = 1;
|
||||
else
|
||||
files[fds[fd]].locked = 1;
|
||||
}
|
||||
|
||||
void _zz_unlock(int fd)
|
||||
{
|
||||
if(fd < -1 || fd >= maxfd || fds[fd] == -1)
|
||||
return;
|
||||
|
||||
if(fd == -1)
|
||||
create_lock = 0;
|
||||
else
|
||||
files[fds[fd]].locked = 0;
|
||||
}
|
||||
|
||||
int _zz_islocked(int fd)
|
||||
{
|
||||
if(fd < -1 || fd >= maxfd || fds[fd] == -1)
|
||||
return 0;
|
||||
|
||||
if(fd == -1)
|
||||
return create_lock;
|
||||
else
|
||||
return files[fds[fd]].locked;
|
||||
}
|
||||
|
||||
long int _zz_getpos(int fd)
|
||||
{
|
||||
if(fd < 0 || fd >= maxfd || fds[fd] == -1)
|
||||
|
||||
3
src/fd.h
3
src/fd.h
@ -29,6 +29,9 @@ extern int _zz_mustwatch(char const *);
|
||||
extern int _zz_iswatched(int);
|
||||
extern void _zz_register(int);
|
||||
extern void _zz_unregister(int);
|
||||
extern void _zz_lock(int);
|
||||
extern void _zz_unlock(int);
|
||||
extern int _zz_islocked(int);
|
||||
extern long int _zz_getpos(int);
|
||||
extern void _zz_setpos(int, long int);
|
||||
extern void _zz_addpos(int, long int);
|
||||
|
||||
28
src/lib-fd.c
28
src/lib-fd.c
@ -99,7 +99,7 @@ static int (*close_orig) (int fd);
|
||||
{ \
|
||||
ret = ORIG(fn)(file, oflag); \
|
||||
} \
|
||||
if(!_zz_ready || _zz_disabled) \
|
||||
if(!_zz_ready || _zz_islocked(-1)) \
|
||||
return ret; \
|
||||
if(ret >= 0 \
|
||||
&& ((oflag & (O_RDONLY | O_RDWR | O_WRONLY)) != O_WRONLY) \
|
||||
@ -132,7 +132,7 @@ int accept(int sockfd, struct sockaddr *addr, SOCKLEN_T *addrlen)
|
||||
|
||||
LOADSYM(accept);
|
||||
ret = accept_orig(sockfd, addr, addrlen);
|
||||
if(!_zz_ready || _zz_disabled || !_zz_network)
|
||||
if(!_zz_ready || _zz_islocked(-1) || !_zz_network)
|
||||
return ret;
|
||||
|
||||
if(ret >= 0)
|
||||
@ -150,7 +150,7 @@ int socket(int domain, int type, int protocol)
|
||||
|
||||
LOADSYM(socket);
|
||||
ret = socket_orig(domain, type, protocol);
|
||||
if(!_zz_ready || _zz_disabled || !_zz_network)
|
||||
if(!_zz_ready || _zz_islocked(-1) || !_zz_network)
|
||||
return ret;
|
||||
|
||||
if(ret >= 0)
|
||||
@ -168,7 +168,7 @@ int recv(int s, void *buf, size_t len, int flags)
|
||||
|
||||
LOADSYM(recv);
|
||||
ret = recv_orig(s, buf, len, flags);
|
||||
if(!_zz_ready || _zz_disabled || !_zz_network)
|
||||
if(!_zz_ready || !_zz_iswatched(s) || _zz_islocked(s))
|
||||
return ret;
|
||||
|
||||
if(ret > 0)
|
||||
@ -199,7 +199,7 @@ int recvfrom(int s, void *buf, size_t len, int flags,
|
||||
|
||||
LOADSYM(recvfrom);
|
||||
ret = recvfrom_orig(s, buf, len, flags, from, fromlen);
|
||||
if(!_zz_ready || _zz_disabled || !_zz_network)
|
||||
if(!_zz_ready || !_zz_iswatched(s) || _zz_islocked(s))
|
||||
return ret;
|
||||
|
||||
if(ret > 0)
|
||||
@ -230,7 +230,7 @@ int recvmsg(int s, struct msghdr *hdr, int flags)
|
||||
|
||||
LOADSYM(recvmsg);
|
||||
ret = recvmsg_orig(s, hdr, flags);
|
||||
if(!_zz_ready || !_zz_iswatched(s) || _zz_disabled)
|
||||
if(!_zz_ready || !_zz_iswatched(s) || _zz_islocked(s))
|
||||
return ret;
|
||||
|
||||
fuzz_iovec(s, hdr->msg_iov, ret);
|
||||
@ -245,7 +245,7 @@ ssize_t read(int fd, void *buf, size_t count)
|
||||
|
||||
LOADSYM(read);
|
||||
ret = read_orig(fd, buf, count);
|
||||
if(!_zz_ready || !_zz_iswatched(fd) || _zz_disabled)
|
||||
if(!_zz_ready || !_zz_iswatched(fd) || _zz_islocked(fd))
|
||||
return ret;
|
||||
|
||||
if(ret > 0)
|
||||
@ -275,7 +275,7 @@ ssize_t readv(int fd, const struct iovec *iov, int count)
|
||||
|
||||
LOADSYM(readv);
|
||||
ret = readv_orig(fd, iov, count);
|
||||
if(!_zz_ready || !_zz_iswatched(fd) || _zz_disabled)
|
||||
if(!_zz_ready || !_zz_iswatched(fd) || _zz_islocked(fd))
|
||||
return ret;
|
||||
|
||||
fuzz_iovec(fd, iov, ret);
|
||||
@ -291,7 +291,7 @@ ssize_t pread(int fd, void *buf, size_t count, off_t offset)
|
||||
|
||||
LOADSYM(pread);
|
||||
ret = pread_orig(fd, buf, count, offset);
|
||||
if(!_zz_ready || !_zz_iswatched(fd) || _zz_disabled)
|
||||
if(!_zz_ready || !_zz_iswatched(fd) || _zz_islocked(fd))
|
||||
return ret;
|
||||
|
||||
if(ret > 0)
|
||||
@ -323,7 +323,7 @@ ssize_t pread(int fd, void *buf, size_t count, off_t offset)
|
||||
{ \
|
||||
LOADSYM(fn); \
|
||||
ret = ORIG(fn)(fd, offset, whence); \
|
||||
if(!_zz_ready || !_zz_iswatched(fd) || _zz_disabled) \
|
||||
if(!_zz_ready || !_zz_iswatched(fd) || _zz_islocked(fd)) \
|
||||
return ret; \
|
||||
debug("%s(%i, %lli, %i) = %lli", __func__, fd, \
|
||||
(long long int)offset, whence, (long long int)ret); \
|
||||
@ -353,10 +353,10 @@ int aio_read(struct aiocb *aiocbp)
|
||||
int fd = aiocbp->aio_fildes;
|
||||
|
||||
LOADSYM(aio_read);
|
||||
if(!_zz_ready || !_zz_iswatched(fd) || _zz_disabled)
|
||||
if(!_zz_ready || !_zz_iswatched(fd))
|
||||
return aio_read_orig(aiocbp);
|
||||
|
||||
_zz_disabled = 1;
|
||||
_zz_lock(fd);
|
||||
ret = aio_read_orig(aiocbp);
|
||||
|
||||
debug("%s({%i, %i, %i, %p, %li, ..., %li}) = %i", __func__,
|
||||
@ -376,7 +376,7 @@ ssize_t aio_return(struct aiocb *aiocbp)
|
||||
return aio_return_orig(aiocbp);
|
||||
|
||||
ret = aio_return_orig(aiocbp);
|
||||
_zz_disabled = 0;
|
||||
_zz_unlock(fd);
|
||||
|
||||
/* FIXME: make sure we’re actually *reading* */
|
||||
if(ret > 0)
|
||||
@ -404,7 +404,7 @@ int close(int fd)
|
||||
|
||||
LOADSYM(close);
|
||||
ret = close_orig(fd);
|
||||
if(!_zz_ready || !_zz_iswatched(fd) || _zz_disabled)
|
||||
if(!_zz_ready || !_zz_iswatched(fd) || _zz_islocked(fd))
|
||||
return ret;
|
||||
|
||||
debug("%s(%i) = %i", __func__, fd, ret);
|
||||
|
||||
@ -189,7 +189,7 @@ int nbmaps = 0;
|
||||
do { \
|
||||
LOADSYM(fn); \
|
||||
ret = ORIG(fn)(start, length, prot, flags, fd, offset); \
|
||||
if(!_zz_ready || !_zz_iswatched(fd) || _zz_disabled) \
|
||||
if(!_zz_ready || !_zz_iswatched(fd) || _zz_islocked(fd)) \
|
||||
return ret; \
|
||||
if(ret && length) \
|
||||
{ \
|
||||
@ -269,7 +269,7 @@ kern_return_t map_fd(int fd, vm_offset_t offset, vm_offset_t *addr,
|
||||
|
||||
LOADSYM(map_fd);
|
||||
ret = map_fd_orig(fd, offset, addr, find_space, numbytes);
|
||||
if(!_zz_ready || !_zz_iswatched(fd) || _zz_disabled)
|
||||
if(!_zz_ready || !_zz_iswatched(fd) || _zz_islocked(fd))
|
||||
return ret;
|
||||
|
||||
if(ret == 0 && numbytes)
|
||||
|
||||
@ -95,9 +95,9 @@ int (*__srefill_orig) (FILE *fp);
|
||||
LOADSYM(fn); \
|
||||
if(!_zz_ready) \
|
||||
return ORIG(fn)(path, mode); \
|
||||
_zz_disabled = 1; \
|
||||
_zz_lock(-1); \
|
||||
ret = ORIG(fn)(path, mode); \
|
||||
_zz_disabled = 0; \
|
||||
_zz_unlock(-1); \
|
||||
if(ret && _zz_mustwatch(path)) \
|
||||
{ \
|
||||
int fd = fileno(ret); \
|
||||
@ -130,9 +130,9 @@ FILE *freopen(const char *path, const char *mode, FILE *stream)
|
||||
disp = 1;
|
||||
}
|
||||
|
||||
_zz_disabled = 1;
|
||||
_zz_lock(-1);
|
||||
ret = freopen_orig(path, mode, stream);
|
||||
_zz_disabled = 0;
|
||||
_zz_unlock(-1);
|
||||
|
||||
if(ret && _zz_mustwatch(path))
|
||||
{
|
||||
@ -178,9 +178,9 @@ FILE *freopen(const char *path, const char *mode, FILE *stream)
|
||||
fd = fileno(stream); \
|
||||
if(!_zz_ready || !_zz_iswatched(fd)) \
|
||||
return ORIG(fn)(stream, offset, whence); \
|
||||
_zz_disabled = 1; \
|
||||
_zz_lock(fd); \
|
||||
ret = ORIG(fn)(stream, offset, whence); \
|
||||
_zz_disabled = 0; \
|
||||
_zz_unlock(fd); \
|
||||
debug("%s([%i], %lli, %i) = %i", __func__, \
|
||||
fd, (long long int)offset, whence, ret); \
|
||||
FSEEK_FUZZ(fn2) \
|
||||
@ -210,9 +210,9 @@ void rewind(FILE *stream)
|
||||
return;
|
||||
}
|
||||
|
||||
_zz_disabled = 1;
|
||||
_zz_lock(fd);
|
||||
rewind_orig(stream);
|
||||
_zz_disabled = 0;
|
||||
_zz_unlock(fd);
|
||||
debug("%s([%i])", __func__, fd);
|
||||
|
||||
#if defined HAVE___SREFILL /* Don't fuzz or seek if we have __srefill() */
|
||||
@ -238,9 +238,9 @@ size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
|
||||
return fread_orig(ptr, size, nmemb, stream);
|
||||
|
||||
pos = ftell(stream);
|
||||
_zz_disabled = 1;
|
||||
_zz_lock(fd);
|
||||
ret = fread_orig(ptr, size, nmemb, stream);
|
||||
_zz_disabled = 0;
|
||||
_zz_unlock(fd);
|
||||
debug("%s(%p, %li, %li, [%i]) = %li", __func__, ptr,
|
||||
(long int)size, (long int)nmemb, fd, (long int)ret);
|
||||
|
||||
@ -283,9 +283,9 @@ size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
|
||||
fd = fileno(stream); \
|
||||
if(!_zz_ready || !_zz_iswatched(fd)) \
|
||||
return ORIG(fn)(stream); \
|
||||
_zz_disabled = 1; \
|
||||
_zz_lock(fd); \
|
||||
ret = ORIG(fn)(stream); \
|
||||
_zz_disabled = 0; \
|
||||
_zz_unlock(fd); \
|
||||
FGETC_FUZZ \
|
||||
debug("%s([%i]) = '%c'", __func__, fd, ret); \
|
||||
} while(0)
|
||||
@ -320,9 +320,9 @@ char *fgets(char *s, int size, FILE *stream)
|
||||
return fgets_orig(s, size, stream);
|
||||
|
||||
#if defined HAVE___SREFILL /* Don't fuzz or seek if we have __srefill() */
|
||||
_zz_disabled = 1;
|
||||
_zz_lock(fd);
|
||||
ret = fgets_orig(s, size, stream);
|
||||
_zz_disabled = 0;
|
||||
_zz_unlock(fd);
|
||||
#else
|
||||
if(size <= 0)
|
||||
ret = NULL;
|
||||
@ -336,9 +336,9 @@ char *fgets(char *s, int size, FILE *stream)
|
||||
{
|
||||
int ch;
|
||||
|
||||
_zz_disabled = 1;
|
||||
_zz_lock(fd);
|
||||
ch = fgetc_orig(stream);
|
||||
_zz_disabled = 0;
|
||||
_zz_unlock(fd);
|
||||
|
||||
if(ch == EOF)
|
||||
{
|
||||
@ -378,9 +378,9 @@ int ungetc(int c, FILE *stream)
|
||||
_zz_addpos(fd, -1);
|
||||
_zz_fuzz(fd, &ch, 1);
|
||||
#endif
|
||||
_zz_disabled = 1;
|
||||
_zz_lock(fd);
|
||||
ret = ungetc_orig((int)ch, stream);
|
||||
_zz_disabled = 0;
|
||||
_zz_unlock(fd);
|
||||
|
||||
if(ret >= 0)
|
||||
ret = c;
|
||||
@ -403,9 +403,9 @@ int fclose(FILE *fp)
|
||||
if(!_zz_ready || !_zz_iswatched(fd))
|
||||
return fclose_orig(fp);
|
||||
|
||||
_zz_disabled = 1;
|
||||
_zz_lock(fd);
|
||||
ret = fclose_orig(fp);
|
||||
_zz_disabled = 0;
|
||||
_zz_unlock(fd);
|
||||
debug("%s([%i]) = %i", __func__, fd, ret);
|
||||
_zz_unregister(fd);
|
||||
|
||||
@ -438,9 +438,9 @@ int fclose(FILE *fp)
|
||||
*lineptr = line; \
|
||||
break; \
|
||||
} \
|
||||
_zz_disabled = 1; \
|
||||
_zz_lock(fd); \
|
||||
ch = fgetc_orig(stream); \
|
||||
_zz_disabled = 0; \
|
||||
_zz_unlock(fd); \
|
||||
if(ch == EOF) \
|
||||
{ \
|
||||
finished = 1; \
|
||||
@ -507,9 +507,9 @@ char *fgetln(FILE *stream, size_t *len)
|
||||
return fgetln_orig(stream, len);
|
||||
|
||||
#if defined HAVE___SREFILL /* Don't fuzz or seek if we have __srefill() */
|
||||
_zz_disabled = 1;
|
||||
_zz_lock(fd);
|
||||
ret = fgetln_orig(stream, len);
|
||||
_zz_disabled = 0;
|
||||
_zz_unlock(fd);
|
||||
#else
|
||||
fuzz = _zz_getfuzz(fd);
|
||||
|
||||
@ -517,9 +517,9 @@ char *fgetln(FILE *stream, size_t *len)
|
||||
{
|
||||
int ch;
|
||||
|
||||
_zz_disabled = 1;
|
||||
_zz_lock(fd);
|
||||
ch = fgetc_orig(stream);
|
||||
_zz_disabled = 0;
|
||||
_zz_unlock(fd);
|
||||
|
||||
if(ch == EOF)
|
||||
break;
|
||||
@ -555,11 +555,12 @@ int __srefill(FILE *fp)
|
||||
if(!_zz_ready || !_zz_iswatched(fd))
|
||||
return __srefill_orig(fp);
|
||||
|
||||
tmp = _zz_disabled;
|
||||
_zz_disabled = 1;
|
||||
tmp = _zz_islocked(fd);
|
||||
_zz_lock(fd);
|
||||
ret = __srefill_orig(fp);
|
||||
newpos = lseek(fd, 0, SEEK_CUR);
|
||||
_zz_disabled = tmp;
|
||||
if(!tmp)
|
||||
_zz_unlock(fd);
|
||||
if(ret != EOF)
|
||||
{
|
||||
if(newpos != -1)
|
||||
@ -568,7 +569,7 @@ int __srefill(FILE *fp)
|
||||
_zz_addpos(fd, fp->_r);
|
||||
}
|
||||
|
||||
if(!_zz_disabled)
|
||||
if(!_zz_islocked(fd))
|
||||
debug("%s([%i]) = %i", __func__, fd, ret);
|
||||
|
||||
return ret;
|
||||
|
||||
@ -41,7 +41,6 @@
|
||||
|
||||
/* Global variables */
|
||||
int _zz_ready = 0;
|
||||
int _zz_disabled = 0;
|
||||
int _zz_hasdebug = 0;
|
||||
int _zz_signal = 0;
|
||||
int _zz_memory = 0;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user