comparison fuzz-wrapfd.c @ 1358:6b89eb92f872 fuzz

glaring wrapfd problems fixed
author Matt Johnston <matt@ucc.asn.au>
date Sat, 20 May 2017 22:47:19 +0800
parents 08f4fa4dc6a0
children 16f45f2df38f
comparison
equal deleted inserted replaced
1357:08f4fa4dc6a0 1358:6b89eb92f872
14 static const double CHANCE_WRITE2 = 0.3; 14 static const double CHANCE_WRITE2 = 0.3;
15 15
16 struct fdwrap { 16 struct fdwrap {
17 enum wrapfd_mode mode; 17 enum wrapfd_mode mode;
18 buffer *buf; 18 buffer *buf;
19 int closein;
20 int closeout;
19 }; 21 };
20 22
21 static struct fdwrap wrap_fds[IOWRAP_MAXFD+1]; 23 static struct fdwrap wrap_fds[IOWRAP_MAXFD+1];
22 // for quick selection of in-use descriptors 24 // for quick selection of in-use descriptors
23 static int wrap_used[IOWRAP_MAXFD+1]; 25 static int wrap_used[IOWRAP_MAXFD+1];
26 28
27 void wrapfd_setup(uint32_t seed) { 29 void wrapfd_setup(uint32_t seed) {
28 TRACE(("wrapfd_setup %x", seed)) 30 TRACE(("wrapfd_setup %x", seed))
29 nused = 0; 31 nused = 0;
30 memset(wrap_fds, 0x0, sizeof(wrap_fds)); 32 memset(wrap_fds, 0x0, sizeof(wrap_fds));
31 33 memset(wrap_used, 0x0, sizeof(wrap_used));
34
35 memset(rand_state, 0x0, sizeof(rand_state));
32 *((uint32_t*)rand_state) = seed; 36 *((uint32_t*)rand_state) = seed;
33 nrand48(rand_state); 37 nrand48(rand_state);
34 } 38 }
35 39
36 void wrapfd_add(int fd, buffer *buf, enum wrapfd_mode mode) { 40 void wrapfd_add(int fd, buffer *buf, enum wrapfd_mode mode) {
41 TRACE(("wrapfd_add %d buf %p mode %d", fd, buf, mode))
37 assert(fd >= 0); 42 assert(fd >= 0);
38 assert(fd <= IOWRAP_MAXFD); 43 assert(fd <= IOWRAP_MAXFD);
39 assert(wrap_fds[fd].mode == UNUSED); 44 assert(wrap_fds[fd].mode == UNUSED);
40 assert(buf || mode == RANDOMIN); 45 assert(buf || mode == RANDOMIN);
41 46
42 TRACE(("wrapfd_add %d buf %p mode %d", fd, buf, mode))
43 47
44 wrap_fds[fd].mode = mode; 48 wrap_fds[fd].mode = mode;
45 wrap_fds[fd].buf = buf; 49 wrap_fds[fd].buf = buf;
50 wrap_fds[fd].closein = 0;
51 wrap_fds[fd].closeout = 0;
46 wrap_used[nused] = fd; 52 wrap_used[nused] = fd;
47 53
48 nused++; 54 nused++;
49 } 55 }
50 56
51 void wrapfd_remove(int fd) { 57 void wrapfd_remove(int fd) {
52 unsigned int i, j; 58 unsigned int i, j;
59 TRACE(("wrapfd_remove %d", fd))
53 assert(fd >= 0); 60 assert(fd >= 0);
54 assert(fd <= IOWRAP_MAXFD); 61 assert(fd <= IOWRAP_MAXFD);
55 assert(wrap_fds[fd].mode != UNUSED); 62 assert(wrap_fds[fd].mode != UNUSED);
56 wrap_fds[fd].mode = UNUSED; 63 wrap_fds[fd].mode = UNUSED;
57 64
58 TRACE(("wrapfd_remove %d", fd))
59 65
60 // remove from used list 66 // remove from used list
61 for (i = 0, j = 0; i < nused; i++) { 67 for (i = 0, j = 0; i < nused; i++) {
62 if (wrap_used[i] != fd) { 68 if (wrap_used[i] != fd) {
63 wrap_used[j] = wrap_used[i]; 69 wrap_used[j] = wrap_used[i];
65 } 71 }
66 } 72 }
67 nused--; 73 nused--;
68 } 74 }
69 75
76 void wrapfd_close(int fd) {
77 wrapfd_remove(fd);
78 }
70 79
71 int wrapfd_read(int fd, void *out, size_t count) { 80 int wrapfd_read(int fd, void *out, size_t count) {
72 size_t maxread; 81 size_t maxread;
73 buffer *buf; 82 buffer *buf;
74 83
83 return -1; 92 return -1;
84 } 93 }
85 94
86 assert(count != 0); 95 assert(count != 0);
87 96
88 if (erand48(rand_state) < CHANCE_CLOSE) { 97 if (wrap_fds[fd].closein || erand48(rand_state) < CHANCE_CLOSE) {
89 wrapfd_remove(fd); 98 wrap_fds[fd].closein = 1;
90 return 0; 99 errno = ECONNRESET;
100 return -1;
91 } 101 }
92 102
93 if (erand48(rand_state) < CHANCE_INTR) { 103 if (erand48(rand_state) < CHANCE_INTR) {
94 errno = EINTR; 104 errno = EINTR;
95 return -1; 105 return -1;
133 // force read to exercise sanitisers 143 // force read to exercise sanitisers
134 for (i = 0; i < count; i++) { 144 for (i = 0; i < count; i++) {
135 (void)volin[i]; 145 (void)volin[i];
136 } 146 }
137 147
138 if (erand48(rand_state) < CHANCE_CLOSE) { 148 if (wrap_fds[fd].closeout || erand48(rand_state) < CHANCE_CLOSE) {
139 wrapfd_remove(fd); 149 wrap_fds[fd].closeout = 1;
140 return 0; 150 errno = ECONNRESET;
151 return -1;
141 } 152 }
142 153
143 if (erand48(rand_state) < CHANCE_INTR) { 154 if (erand48(rand_state) < CHANCE_INTR) {
144 errno = EINTR; 155 errno = EINTR;
145 return -1; 156 return -1;