misc: factor several common tests into one must_fuzz_fd() function.

This commit is contained in:
Sam Hocevar
2015-01-16 10:04:06 +01:00
parent 4f7a76385a
commit 21cb084712
6 changed files with 50 additions and 44 deletions
+2
View File
@@ -11,6 +11,8 @@
* See http://www.wtfpl.net/ for more details.
*/
#pragma once
/*
* fd.h: file descriptor functions
*/
+11 -14
View File
@@ -378,9 +378,9 @@ int NEW(socket)(int domain, int type, int protocol)
LOADSYM(myrecv); \
\
ret = ORIG(myrecv) myargs; \
if (!_zz_ready || !_zz_iswatched(s) || !_zz_hostwatched(s) \
|| _zz_islocked(s) || !_zz_isactive(s)) \
if (!must_fuzz_fd(s) || !_zz_hostwatched(s)) \
return ret; \
\
if (ret > 0) \
{ \
char *b = buf; \
@@ -421,9 +421,9 @@ RECV_T NEW(__recv_chk)(int s, void *buf, size_t len, size_t buflen, int flags)
LOADSYM(myrecvfrom); \
\
ret = ORIG(myrecvfrom) myargs; \
if (!_zz_ready || !_zz_iswatched(s) || !_zz_hostwatched(s) \
|| _zz_islocked(s) || !_zz_isactive(s)) \
if (!must_fuzz_fd(s) || !_zz_hostwatched(s)) \
return ret; \
\
if (ret > 0) \
{ \
char tmp[128]; \
@@ -477,8 +477,7 @@ RECV_T NEW(recvmsg)(int s, struct msghdr *hdr, int flags)
LOADSYM(recvmsg);
ssize_t ret = ORIG(recvmsg)(s, hdr, flags);
if (!_zz_ready || !_zz_iswatched(s) || !_zz_hostwatched(s)
|| _zz_islocked(s) || !_zz_isactive(s))
if (!must_fuzz_fd(s) || !_zz_hostwatched(s))
return ret;
fuzz_iovec(s, hdr->msg_iov, ret);
@@ -494,9 +493,9 @@ RECV_T NEW(recvmsg)(int s, struct msghdr *hdr, int flags)
LOADSYM(myread); \
\
ret = ORIG(myread) myargs; \
if (!_zz_ready || !_zz_iswatched(fd) || !_zz_hostwatched(fd) \
|| _zz_islocked(fd) || !_zz_isactive(fd)) \
if (!must_fuzz_fd(fd) || !_zz_hostwatched(fd)) \
return ret; \
\
if (ret > 0) \
{ \
char *b = buf; \
@@ -544,8 +543,7 @@ ssize_t NEW(readv)(int fd, const struct iovec *iov, int count)
LOADSYM(readv);
ssize_t ret = ORIG(readv)(fd, iov, count);
if (!_zz_ready || !_zz_iswatched(fd) || _zz_islocked(fd)
|| !_zz_isactive(fd))
if (!must_fuzz_fd(fd))
return ret;
fuzz_iovec(fd, iov, ret);
@@ -563,8 +561,7 @@ ssize_t NEW(pread)(int fd, void *buf, size_t count, off_t offset)
LOADSYM(pread);
int ret = ORIG(pread)(fd, buf, count, offset);
if (!_zz_ready || !_zz_iswatched(fd) || _zz_islocked(fd)
|| !_zz_isactive(fd))
if (!must_fuzz_fd(fd))
return ret;
if (ret > 0)
@@ -598,9 +595,9 @@ ssize_t NEW(pread)(int fd, void *buf, size_t count, off_t offset)
LOADSYM(mylseek); \
\
ret = ORIG(mylseek)(fd, offset, whence); \
if (!_zz_ready || !_zz_iswatched(fd) || _zz_islocked(fd) \
|| !_zz_isactive(fd)) \
if (!must_fuzz_fd(fd)) \
return ret; \
\
debug("%s(%i, %lli, %i) = %lli", __func__, fd, \
(long long int)offset, whence, (long long int)ret); \
if (ret != (off_t)-1) \
+4 -5
View File
@@ -298,10 +298,10 @@ int nbmaps = 0;
do { \
LOADSYM(mymmap); \
\
char *b = MAP_FAILED; \
if (!_zz_ready || !_zz_iswatched(fd) || _zz_islocked(fd) \
|| !_zz_isactive(fd)) \
if (!must_fuzz_fd(fd)) \
return ORIG(mymmap)(start, length, prot, flags, fd, offset); \
\
char *b = MAP_FAILED; \
ret = ORIG(mymmap)(NULL, length, prot, flags, fd, offset); \
if (ret != MAP_FAILED && length) \
{ \
@@ -396,8 +396,7 @@ kern_return_t NEW(map_fd)(int fd, vm_offset_t offset, vm_offset_t *addr,
LOADSYM(map_fd);
kern_return_t ret = ORIG(map_fd)(fd, offset, addr, find_space, numbytes);
if (!_zz_ready || !_zz_iswatched(fd) || _zz_islocked(fd)
|| !_zz_isactive(fd))
if (!must_fuzz_fd(fd))
return ret;
if (ret == 0 && numbytes)
+17 -20
View File
@@ -368,9 +368,9 @@ FILE *NEW(__freopen64)(const char *path, const char *mode, FILE *stream)
LOADSYM(myfseek); \
\
int fd = fileno(stream); \
if (!_zz_ready || !_zz_iswatched(fd) || !_zz_isactive(fd) \
|| _zz_islocked(fd)) \
if (!must_fuzz_fd(fd)) \
return ORIG(myfseek)(stream, offset, whence); \
\
debug_stream("before", stream); \
/* FIXME: ftell() will return -1 on a pipe such as stdin */ \
int64_t oldpos = ZZ_FTELL(stream); \
@@ -404,9 +404,9 @@ FILE *NEW(__freopen64)(const char *path, const char *mode, FILE *stream)
LOADSYM(myfsetpos); \
\
int fd = fileno(stream); \
if (!_zz_ready || !_zz_iswatched(fd) || !_zz_isactive(fd) \
|| _zz_islocked(fd)) \
if (!must_fuzz_fd(fd)) \
return ORIG(myfsetpos)(stream, pos); \
\
debug_stream("before", stream); \
/* FIXME: ftell() will return -1 on a pipe such as stdin */ \
int64_t oldpos = ZZ_FTELL(stream); \
@@ -435,8 +435,7 @@ FILE *NEW(__freopen64)(const char *path, const char *mode, FILE *stream)
LOADSYM(rewind); \
\
int fd = fileno(stream); \
if (!_zz_ready || !_zz_iswatched(fd) || !_zz_isactive(fd) \
|| _zz_islocked(fd)) \
if (!must_fuzz_fd(fd)) \
{ \
ORIG(rewind)(stream); \
return; \
@@ -529,9 +528,9 @@ void NEW(rewind)(FILE *stream)
\
uint8_t *b = (uint8_t *)ptr; \
int fd = fileno(stream); \
if (!_zz_ready || !_zz_iswatched(fd) || !_zz_isactive(fd) \
|| _zz_islocked(fd)) \
if (!must_fuzz_fd(fd)) \
return ORIG(myfread) myargs; \
\
debug_stream("before", stream); \
/* FIXME: ftell() will return -1 on a pipe such as stdin */ \
int64_t oldpos = ZZ_FTELL(stream); \
@@ -617,9 +616,9 @@ size_t NEW(__fread_unlocked_chk)(void *ptr, size_t ptrlen, size_t size,
LOADSYM(myfgetc); \
\
int fd = fileno(s); \
if (!_zz_ready || !_zz_iswatched(fd) || !_zz_isactive(fd) \
|| _zz_islocked(fd)) \
if (!must_fuzz_fd(fd)) \
return ORIG(myfgetc)(arg); \
\
debug_stream("before", s); \
int64_t oldpos = ZZ_FTELL(s); \
int oldcnt = get_stream_cnt(s); \
@@ -712,9 +711,9 @@ int NEW(fgetc_unlocked)(FILE *stream)
\
ret = s; \
int fd = fileno(stream); \
if (!_zz_ready || !_zz_iswatched(fd) || !_zz_isactive(fd) \
|| _zz_islocked(fd)) \
if (!must_fuzz_fd(fd)) \
return ORIG(myfgets) myargs; \
\
debug_stream("before", stream); \
int64_t oldpos = ZZ_FTELL(stream); \
int oldcnt = get_stream_cnt(stream); \
@@ -815,8 +814,7 @@ int NEW(ungetc)(int c, FILE *stream)
LOADSYM(ungetc);
int fd = fileno(stream);
if (!_zz_ready || !_zz_iswatched(fd) || !_zz_isactive(fd)
|| _zz_islocked(fd))
if (!must_fuzz_fd(fd))
return ORIG(ungetc)(c, stream);
debug_stream("before", stream);
@@ -868,9 +866,9 @@ int NEW(fclose)(FILE *fp)
LOADSYM(fgetc); \
\
int fd = fileno(stream); \
if (!_zz_ready || !_zz_iswatched(fd) || !_zz_isactive(fd) \
|| _zz_islocked(fd)) \
if (!must_fuzz_fd(fd)) \
return ORIG(getdelim)(lineptr, n, delim, stream); \
\
debug_stream("before", stream); \
int64_t oldpos = ZZ_FTELL(stream); \
int oldcnt = get_stream_cnt(stream); \
@@ -976,8 +974,7 @@ char *NEW(fgetln)(FILE *stream, size_t *len)
LOADSYM(fgetc);
int fd = fileno(stream);
if (!_zz_ready || !_zz_iswatched(fd) || !_zz_isactive(fd)
|| _zz_islocked(fd))
if (!must_fuzz_fd(fd))
return ORIG(fgetln)(stream, len);
debug_stream("before", stream);
@@ -1052,9 +1049,9 @@ char *NEW(fgetln)(FILE *stream, size_t *len)
LOADSYM(myrefill); \
\
int fd = fileno(fp); \
if (!_zz_ready || !_zz_iswatched(fd) || !_zz_isactive(fd) \
|| _zz_islocked(fd)) \
if (!must_fuzz_fd(fd)) \
return ORIG(myrefill)(fp); \
\
debug_stream("before", fp); \
int64_t pos = _zz_getpos(fd); \
_zz_lockfd(fd); \
+5 -5
View File
@@ -192,7 +192,7 @@ BOOL __stdcall NEW(ReadFile)(HANDLE hFile, LPVOID lpBuffer,
debug("ReadFile(%#08x, %p, %#08x, %#08x, %p) = %s",
hFile, lpBuffer, nNumberOfBytesToRead, lpNumberOfBytesRead, lpOverlapped, (ret ? "TRUE" : "FALSE"));
if (!_zz_ready || !_zz_iswatched(hFile) /*|| !_zz_hostwatched(hFile)*/ || _zz_islocked(hFile) || !_zz_isactive(hFile))
if (!must_fuzz_fd(hFile) /*|| !_zz_hostwatched(hFile)*/)
return ret;
if (ret)
@@ -219,7 +219,7 @@ BOOL __stdcall NEW(ReadFileEx)(HANDLE hFile, LPVOID lpBuffer,
debug("ReadFileEx(%#08x, %p, %#08x, %p, %p, %p) = %s",
hFile, lpBuffer, nNumberOfBytesToRead, lpNumberOfBytesRead, lpOverlapped, lpCompletionRoutine, (ret ? "TRUE" : "FALSE"));
if (!_zz_ready || !_zz_iswatched(hFile) /*|| !_zz_hostwatched(hFile)*/ || _zz_islocked(hFile) || !_zz_isactive(hFile))
if (!must_fuzz_fd(hFile) /*|| !_zz_hostwatched(hFile)*/)
return ret;
if (ret)
@@ -245,7 +245,7 @@ HANDLE __stdcall NEW(CreateIoCompletionPort)(HANDLE FileHandle, HANDLE ExistingC
FileHandle, ExistingCompletionPort, CompletionKey,
NumberOfConcurrentThreads, ret);
if (!_zz_ready || !_zz_iswatched(FileHandle) /*|| !_zz_hostwatched(hFile)*/ || _zz_islocked(FileHandle) || !_zz_isactive(FileHandle))
if (!must_fuzz_fd(FileHandle) /*|| !_zz_hostwatched(FileHandle)*/)
return ret;
if (ret != NULL)
@@ -302,7 +302,7 @@ HANDLE __stdcall NEW(CreateFileMappingA)(HANDLE hFile, LPSECURITY_ATTRIBUTES lpA
if (ret == NULL)
return ret;
if (!_zz_ready || !_zz_iswatched(hFile) /*|| !_zz_hostwatched(hFile)*/ || _zz_islocked(hFile) || !_zz_isactive(hFile) || _zz_islocked(-1))
if (!must_fuzz_fd(hFile) /*|| !_zz_hostwatched(hFile)*/ || _zz_islocked(-1))
return ret;
debug("handle %#08x is registered", ret);
@@ -328,7 +328,7 @@ HANDLE __stdcall NEW(CreateFileMappingW)(HANDLE hFile, LPSECURITY_ATTRIBUTES lpA
if (ret == NULL)
return ret;
if (!_zz_ready || !_zz_iswatched(hFile) /*|| !_zz_hostwatched(hFile)*/ || _zz_islocked(hFile) || !_zz_isactive(hFile) || _zz_islocked(-1))
if (!must_fuzz_fd(hFile) /*|| !_zz_hostwatched(hFile)*/ || _zz_islocked(-1))
return ret;
debug("handle %#08x is registered", ret);
+11
View File
@@ -10,10 +10,14 @@
* See http://www.wtfpl.net/ for more details.
*/
#pragma once
/*
* libzzuf.h: preloaded wrapper library
*/
#include "fd.h"
/* Internal variables */
extern int _zz_ready;
extern int _zz_disabled;
@@ -40,3 +44,10 @@ extern void _zz_mem_init(void);
# include <windows.h>
extern CRITICAL_SECTION _zz_pipe_cs;
#endif
static inline int must_fuzz_fd(int fd)
{
return _zz_ready && _zz_iswatched(fd)
&& !_zz_islocked(fd) && _zz_isactive(fd);
}