comparison fuzz-wrapfd.c @ 1357:08f4fa4dc6a0 fuzz

closer to working
author Matt Johnston <matt@ucc.asn.au>
date Sat, 20 May 2017 13:23:16 +0800
parents 3677a510f545
children 6b89eb92f872
comparison
equal deleted inserted replaced
1356:3677a510f545 1357:08f4fa4dc6a0
1 #define FUZZ_SKIP_WRAP 1
1 #include "includes.h" 2 #include "includes.h"
2 #include "fuzz-wrapfd.h" 3 #include "fuzz-wrapfd.h"
4
5 #include "fuzz.h"
3 6
4 static const int IOWRAP_MAXFD = FD_SETSIZE-1; 7 static const int IOWRAP_MAXFD = FD_SETSIZE-1;
5 static const int MAX_RANDOM_IN = 50000; 8 static const int MAX_RANDOM_IN = 50000;
6 static const double CHANCE_CLOSE = 1.0 / 300; 9 static const double CHANCE_CLOSE = 1.0 / 300;
7 static const double CHANCE_INTR = 1.0 / 200; 10 static const double CHANCE_INTR = 1.0 / 200;
20 static int wrap_used[IOWRAP_MAXFD+1]; 23 static int wrap_used[IOWRAP_MAXFD+1];
21 static unsigned int nused; 24 static unsigned int nused;
22 static unsigned short rand_state[3]; 25 static unsigned short rand_state[3];
23 26
24 void wrapfd_setup(uint32_t seed) { 27 void wrapfd_setup(uint32_t seed) {
28 TRACE(("wrapfd_setup %x", seed))
25 nused = 0; 29 nused = 0;
26 memset(wrap_fds, 0x0, sizeof(wrap_fds)); 30 memset(wrap_fds, 0x0, sizeof(wrap_fds));
27 31
28 *((uint32_t*)rand_state) = seed; 32 *((uint32_t*)rand_state) = seed;
29 nrand48(rand_state); 33 nrand48(rand_state);
32 void wrapfd_add(int fd, buffer *buf, enum wrapfd_mode mode) { 36 void wrapfd_add(int fd, buffer *buf, enum wrapfd_mode mode) {
33 assert(fd >= 0); 37 assert(fd >= 0);
34 assert(fd <= IOWRAP_MAXFD); 38 assert(fd <= IOWRAP_MAXFD);
35 assert(wrap_fds[fd].mode == UNUSED); 39 assert(wrap_fds[fd].mode == UNUSED);
36 assert(buf || mode == RANDOMIN); 40 assert(buf || mode == RANDOMIN);
41
42 TRACE(("wrapfd_add %d buf %p mode %d", fd, buf, mode))
37 43
38 wrap_fds[fd].mode = mode; 44 wrap_fds[fd].mode = mode;
39 wrap_fds[fd].buf = buf; 45 wrap_fds[fd].buf = buf;
40 wrap_used[nused] = fd; 46 wrap_used[nused] = fd;
41 47
47 assert(fd >= 0); 53 assert(fd >= 0);
48 assert(fd <= IOWRAP_MAXFD); 54 assert(fd <= IOWRAP_MAXFD);
49 assert(wrap_fds[fd].mode != UNUSED); 55 assert(wrap_fds[fd].mode != UNUSED);
50 wrap_fds[fd].mode = UNUSED; 56 wrap_fds[fd].mode = UNUSED;
51 57
58 TRACE(("wrapfd_remove %d", fd))
59
52 // remove from used list 60 // remove from used list
53 for (i = 0, j = 0; i < nused; i++) { 61 for (i = 0, j = 0; i < nused; i++) {
54 if (wrap_used[i] != fd) { 62 if (wrap_used[i] != fd) {
55 wrap_used[j] = wrap_used[i]; 63 wrap_used[j] = wrap_used[i];
56 j++; 64 j++;
62 70
63 int wrapfd_read(int fd, void *out, size_t count) { 71 int wrapfd_read(int fd, void *out, size_t count) {
64 size_t maxread; 72 size_t maxread;
65 buffer *buf; 73 buffer *buf;
66 74
67 if (fd < 0 || fd > IOWRAP_MAXFD || wrap_fds[fd].mode != UNUSED) { 75 if (!fuzz.wrapfds) {
76 return read(fd, out, count);
77 }
78
79 if (fd < 0 || fd > IOWRAP_MAXFD || wrap_fds[fd].mode == UNUSED) {
80 // XXX - assertion failure?
68 TRACE(("Bad read descriptor %d\n", fd)) 81 TRACE(("Bad read descriptor %d\n", fd))
69 errno = EBADF; 82 errno = EBADF;
70 return -1; 83 return -1;
71 } 84 }
72 85
84 97
85 buf = wrap_fds[fd].buf; 98 buf = wrap_fds[fd].buf;
86 if (buf) { 99 if (buf) {
87 maxread = MIN(buf->len - buf->pos, count); 100 maxread = MIN(buf->len - buf->pos, count);
88 // returns 0 if buf is EOF, as intended 101 // returns 0 if buf is EOF, as intended
89 maxread = nrand48(rand_state) % maxread + 1; 102 if (maxread > 0) {
103 maxread = nrand48(rand_state) % maxread + 1;
104 }
90 memcpy(out, buf_getptr(buf, maxread), maxread); 105 memcpy(out, buf_getptr(buf, maxread), maxread);
91 buf_incrpos(buf, maxread); 106 buf_incrpos(buf, maxread);
92 return maxread; 107 return maxread;
93 } 108 }
94 109
99 } 114 }
100 115
101 int wrapfd_write(int fd, const void* in, size_t count) { 116 int wrapfd_write(int fd, const void* in, size_t count) {
102 unsigned const volatile char* volin = in; 117 unsigned const volatile char* volin = in;
103 unsigned int i; 118 unsigned int i;
104 if (fd < 0 || fd > IOWRAP_MAXFD || wrap_fds[fd].mode != UNUSED) { 119
120 if (!fuzz.wrapfds) {
121 return write(fd, in, count);
122 }
123
124 if (fd < 0 || fd > IOWRAP_MAXFD || wrap_fds[fd].mode == UNUSED) {
125 // XXX - assertion failure?
105 TRACE(("Bad read descriptor %d\n", fd)) 126 TRACE(("Bad read descriptor %d\n", fd))
106 errno = EBADF; 127 errno = EBADF;
107 return -1; 128 return -1;
108 } 129 }
109 130
126 147
127 return nrand48(rand_state) % (count+1); 148 return nrand48(rand_state) % (count+1);
128 } 149 }
129 150
130 int wrapfd_select(int nfds, fd_set *readfds, fd_set *writefds, 151 int wrapfd_select(int nfds, fd_set *readfds, fd_set *writefds,
131 fd_set *UNUSED(exceptfds), struct timeval *UNUSED(timeout)) { 152 fd_set *exceptfds, struct timeval *timeout) {
132 int i, nset; 153 int i, nset, sel;
133 int ret = 0; 154 int ret = 0;
134 int fdlist[IOWRAP_MAXFD+1] = {0}; 155 int fdlist[IOWRAP_MAXFD+1] = {0};
135 156
157 if (!fuzz.wrapfds) {
158 return select(nfds, readfds, writefds, exceptfds, timeout);
159 }
160
136 assert(nfds <= IOWRAP_MAXFD+1); 161 assert(nfds <= IOWRAP_MAXFD+1);
137 162
138 if (erand48(rand_state) < CHANCE_INTR) { 163 if (erand48(rand_state) < CHANCE_INTR) {
139 errno = EINTR; 164 errno = EINTR;
140 return -1; 165 return -1;
141 } 166 }
142 167
143 // read 168 // read
144 if (erand48(rand_state) < CHANCE_READ1) { 169 if (readfds != NULL && erand48(rand_state) < CHANCE_READ1) {
145 for (i = 0, nset = 0; i < nfds; i++) { 170 for (i = 0, nset = 0; i < nfds; i++) {
146 if (FD_ISSET(i, readfds)) { 171 if (FD_ISSET(i, readfds)) {
147 assert(wrap_fds[i].mode != UNUSED); 172 assert(wrap_fds[i].mode != UNUSED);
148 fdlist[nset] = i; 173 fdlist[nset] = i;
174 nset++;
149 } 175 }
150 } 176 }
151 FD_ZERO(readfds); 177 FD_ZERO(readfds);
152 178
153 if (nset > 0) { 179 if (nset > 0) {
154 // set one 180 // set one
155 FD_SET(fdlist[random() % nset], readfds); 181 sel = fdlist[nrand48(rand_state) % nset];
182 FD_SET(sel, readfds);
156 ret++; 183 ret++;
157 184
158 if (erand48(rand_state) < CHANCE_READ2) { 185 if (erand48(rand_state) < CHANCE_READ2) {
159 i = fdlist[random() % nset]; 186 sel = fdlist[nrand48(rand_state) % nset];
160 if (!FD_ISSET(i, readfds)) { 187 if (!FD_ISSET(sel, readfds)) {
161 FD_SET(i, readfds); 188 FD_SET(sel, readfds);
162 ret++; 189 ret++;
163 } 190 }
164 } 191 }
165 } 192 }
166 } 193 }
167 194
168 // write 195 // write
169 if (erand48(rand_state) < CHANCE_WRITE1) { 196 if (writefds != NULL && erand48(rand_state) < CHANCE_WRITE1) {
170 for (i = 0, nset = 0; i < nfds; i++) { 197 for (i = 0, nset = 0; i < nfds; i++) {
171 if (FD_ISSET(i, writefds)) { 198 if (FD_ISSET(i, writefds)) {
172 assert(wrap_fds[i].mode != UNUSED); 199 assert(wrap_fds[i].mode != UNUSED);
173 fdlist[nset] = i; 200 fdlist[nset] = i;
201 nset++;
174 } 202 }
175 } 203 }
176 FD_ZERO(writefds); 204 FD_ZERO(writefds);
177 205
178 // set one 206 // set one
179 if (nset > 0) { 207 if (nset > 0) {
180 FD_SET(fdlist[nrand48(rand_state) % nset], writefds); 208 sel = fdlist[nrand48(rand_state) % nset];
209 FD_SET(sel, writefds);
181 ret++; 210 ret++;
182 211
183 if (erand48(rand_state) < CHANCE_WRITE2) { 212 if (erand48(rand_state) < CHANCE_WRITE2) {
184 i = fdlist[nrand48(rand_state) % nset]; 213 sel = fdlist[nrand48(rand_state) % nset];
185 if (!FD_ISSET(i, writefds)) { 214 if (!FD_ISSET(sel, writefds)) {
186 FD_SET(i, writefds); 215 FD_SET(sel, writefds);
187 ret++; 216 ret++;
188 } 217 }
189 } 218 }
190 } 219 }
191 } 220 }
192 return ret; 221 return ret;
193 } 222 }
223