comparison fuzz-wrapfd.c @ 1559:92c93b4a3646 fuzz

Fix to be able to compile normal(ish) binaries with --enable-fuzz
author Matt Johnston <matt@ucc.asn.au>
date Wed, 28 Feb 2018 22:02:12 +0800
parents b9e4fd5a0e72
children e75dab5bec71
comparison
equal deleted inserted replaced
1558:2f64cb3d3007 1559:92c93b4a3646
19 int closein; 19 int closein;
20 int closeout; 20 int closeout;
21 }; 21 };
22 22
23 static struct fdwrap wrap_fds[IOWRAP_MAXFD+1]; 23 static struct fdwrap wrap_fds[IOWRAP_MAXFD+1];
24 // for quick selection of in-use descriptors 24 /* for quick selection of in-use descriptors */
25 static int wrap_used[IOWRAP_MAXFD+1]; 25 static int wrap_used[IOWRAP_MAXFD+1];
26 static unsigned int nused; 26 static unsigned int nused;
27 static unsigned short rand_state[3]; 27 static unsigned short rand_state[3];
28 28
29 void wrapfd_setup() { 29 void wrapfd_setup() {
64 assert(fd <= IOWRAP_MAXFD); 64 assert(fd <= IOWRAP_MAXFD);
65 assert(wrap_fds[fd].mode != UNUSED); 65 assert(wrap_fds[fd].mode != UNUSED);
66 wrap_fds[fd].mode = UNUSED; 66 wrap_fds[fd].mode = UNUSED;
67 67
68 68
69 // remove from used list 69 /* remove from used list */
70 for (i = 0, j = 0; i < nused; i++) { 70 for (i = 0, j = 0; i < nused; i++) {
71 if (wrap_used[i] != fd) { 71 if (wrap_used[i] != fd) {
72 wrap_used[j] = wrap_used[i]; 72 wrap_used[j] = wrap_used[i];
73 j++; 73 j++;
74 } 74 }
92 if (!fuzz.wrapfds) { 92 if (!fuzz.wrapfds) {
93 return read(fd, out, count); 93 return read(fd, out, count);
94 } 94 }
95 95
96 if (fd < 0 || fd > IOWRAP_MAXFD || wrap_fds[fd].mode == UNUSED) { 96 if (fd < 0 || fd > IOWRAP_MAXFD || wrap_fds[fd].mode == UNUSED) {
97 // XXX - assertion failure? 97 /* XXX - assertion failure? */
98 TRACE(("Bad read descriptor %d\n", fd)) 98 TRACE(("Bad read descriptor %d\n", fd))
99 errno = EBADF; 99 errno = EBADF;
100 return -1; 100 return -1;
101 } 101 }
102 102
114 } 114 }
115 115
116 buf = wrap_fds[fd].buf; 116 buf = wrap_fds[fd].buf;
117 if (buf) { 117 if (buf) {
118 maxread = MIN(buf->len - buf->pos, count); 118 maxread = MIN(buf->len - buf->pos, count);
119 // returns 0 if buf is EOF, as intended 119 /* returns 0 if buf is EOF, as intended */
120 if (maxread > 0) { 120 if (maxread > 0) {
121 maxread = nrand48(rand_state) % maxread + 1; 121 maxread = nrand48(rand_state) % maxread + 1;
122 } 122 }
123 memcpy(out, buf_getptr(buf, maxread), maxread); 123 memcpy(out, buf_getptr(buf, maxread), maxread);
124 buf_incrpos(buf, maxread); 124 buf_incrpos(buf, maxread);
138 if (!fuzz.wrapfds) { 138 if (!fuzz.wrapfds) {
139 return write(fd, in, count); 139 return write(fd, in, count);
140 } 140 }
141 141
142 if (fd < 0 || fd > IOWRAP_MAXFD || wrap_fds[fd].mode == UNUSED) { 142 if (fd < 0 || fd > IOWRAP_MAXFD || wrap_fds[fd].mode == UNUSED) {
143 // XXX - assertion failure? 143 /* XXX - assertion failure? */
144 TRACE(("Bad read descriptor %d\n", fd)) 144 TRACE(("Bad read descriptor %d\n", fd))
145 errno = EBADF; 145 errno = EBADF;
146 return -1; 146 return -1;
147 } 147 }
148 148
149 assert(count != 0); 149 assert(count != 0);
150 150
151 // force read to exercise sanitisers 151 /* force read to exercise sanitisers */
152 for (i = 0; i < count; i++) { 152 for (i = 0; i < count; i++) {
153 (void)volin[i]; 153 (void)volin[i];
154 } 154 }
155 155
156 if (wrap_fds[fd].closeout || erand48(rand_state) < CHANCE_CLOSE) { 156 if (wrap_fds[fd].closeout || erand48(rand_state) < CHANCE_CLOSE) {
184 if (erand48(rand_state) < CHANCE_INTR) { 184 if (erand48(rand_state) < CHANCE_INTR) {
185 errno = EINTR; 185 errno = EINTR;
186 return -1; 186 return -1;
187 } 187 }
188 188
189 // read 189 /* read */
190 if (readfds != NULL && erand48(rand_state) < CHANCE_READ1) { 190 if (readfds != NULL && erand48(rand_state) < CHANCE_READ1) {
191 for (i = 0, nset = 0; i < nfds; i++) { 191 for (i = 0, nset = 0; i < nfds; i++) {
192 if (FD_ISSET(i, readfds)) { 192 if (FD_ISSET(i, readfds)) {
193 assert(wrap_fds[i].mode != UNUSED); 193 assert(wrap_fds[i].mode != UNUSED);
194 fdlist[nset] = i; 194 fdlist[nset] = i;
196 } 196 }
197 } 197 }
198 FD_ZERO(readfds); 198 FD_ZERO(readfds);
199 199
200 if (nset > 0) { 200 if (nset > 0) {
201 // set one 201 /* set one */
202 sel = fdlist[nrand48(rand_state) % nset]; 202 sel = fdlist[nrand48(rand_state) % nset];
203 FD_SET(sel, readfds); 203 FD_SET(sel, readfds);
204 ret++; 204 ret++;
205 205
206 if (erand48(rand_state) < CHANCE_READ2) { 206 if (erand48(rand_state) < CHANCE_READ2) {
211 } 211 }
212 } 212 }
213 } 213 }
214 } 214 }
215 215
216 // write 216 /* write */
217 if (writefds != NULL && erand48(rand_state) < CHANCE_WRITE1) { 217 if (writefds != NULL && erand48(rand_state) < CHANCE_WRITE1) {
218 for (i = 0, nset = 0; i < nfds; i++) { 218 for (i = 0, nset = 0; i < nfds; i++) {
219 if (FD_ISSET(i, writefds)) { 219 if (FD_ISSET(i, writefds)) {
220 assert(wrap_fds[i].mode != UNUSED); 220 assert(wrap_fds[i].mode != UNUSED);
221 fdlist[nset] = i; 221 fdlist[nset] = i;
222 nset++; 222 nset++;
223 } 223 }
224 } 224 }
225 FD_ZERO(writefds); 225 FD_ZERO(writefds);
226 226
227 // set one 227 /* set one */
228 if (nset > 0) { 228 if (nset > 0) {
229 sel = fdlist[nrand48(rand_state) % nset]; 229 sel = fdlist[nrand48(rand_state) % nset];
230 FD_SET(sel, writefds); 230 FD_SET(sel, writefds);
231 ret++; 231 ret++;
232 232