Mercurial > dropbear
comparison fuzz-wrapfd.c @ 1745:a6824c54962a
Merge fuzz branch
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sun, 18 Oct 2020 22:53:44 +0800 |
parents | 6cf465af5d9f |
children | 28ab2cdb84bf |
comparison
equal
deleted
inserted
replaced
1738:4f13df974cf4 | 1745:a6824c54962a |
---|---|
15 static const double CHANCE_WRITE1 = 0.96; | 15 static const double CHANCE_WRITE1 = 0.96; |
16 static const double CHANCE_WRITE2 = 0.5; | 16 static const double CHANCE_WRITE2 = 0.5; |
17 | 17 |
18 struct fdwrap { | 18 struct fdwrap { |
19 enum wrapfd_mode mode; | 19 enum wrapfd_mode mode; |
20 buffer *buf; | |
21 int closein; | 20 int closein; |
22 int closeout; | 21 int closeout; |
23 }; | 22 }; |
24 | 23 |
25 static struct fdwrap wrap_fds[IOWRAP_MAXFD+1]; | 24 static struct fdwrap wrap_fds[IOWRAP_MAXFD+1] = {0}; |
26 /* for quick selection of in-use descriptors */ | 25 static int wrapfd_maxfd = -1; |
27 static int wrap_used[IOWRAP_MAXFD+1]; | |
28 static unsigned int nused; | |
29 static unsigned short rand_state[3]; | 26 static unsigned short rand_state[3]; |
30 | 27 static buffer *input_buf; |
31 void wrapfd_setup(void) { | 28 static int devnull_fd = -1; |
29 | |
30 static void wrapfd_remove(int fd); | |
31 | |
32 void wrapfd_setup(buffer *buf) { | |
32 TRACE(("wrapfd_setup")) | 33 TRACE(("wrapfd_setup")) |
33 nused = 0; | 34 |
34 memset(wrap_fds, 0x0, sizeof(wrap_fds)); | 35 // clean old ones |
35 memset(wrap_used, 0x0, sizeof(wrap_used)); | 36 int i; |
37 for (i = 0; i <= wrapfd_maxfd; i++) { | |
38 if (wrap_fds[i].mode == COMMONBUF) { | |
39 wrapfd_remove(i); | |
40 } | |
41 } | |
42 wrapfd_maxfd = -1; | |
36 | 43 |
37 memset(rand_state, 0x0, sizeof(rand_state)); | 44 memset(rand_state, 0x0, sizeof(rand_state)); |
38 wrapfd_setseed(50); | 45 wrapfd_setseed(50); |
46 input_buf = buf; | |
39 } | 47 } |
40 | 48 |
41 void wrapfd_setseed(uint32_t seed) { | 49 void wrapfd_setseed(uint32_t seed) { |
42 memcpy(rand_state, &seed, sizeof(seed)); | 50 memcpy(rand_state, &seed, sizeof(seed)); |
43 nrand48(rand_state); | 51 nrand48(rand_state); |
44 } | 52 } |
45 | 53 |
46 void wrapfd_add(int fd, buffer *buf, enum wrapfd_mode mode) { | 54 int wrapfd_new() { |
47 TRACE(("wrapfd_add %d buf %p mode %d", fd, buf, mode)) | 55 if (devnull_fd == -1) { |
48 assert(fd >= 0); | 56 devnull_fd = open("/dev/null", O_RDONLY); |
49 assert(fd <= IOWRAP_MAXFD); | 57 assert(devnull_fd != -1); |
58 } | |
59 | |
60 int fd = dup(devnull_fd); | |
61 assert(fd != -1); | |
50 assert(wrap_fds[fd].mode == UNUSED); | 62 assert(wrap_fds[fd].mode == UNUSED); |
51 assert(buf || mode == RANDOMIN); | 63 wrap_fds[fd].mode = COMMONBUF; |
52 | |
53 wrap_fds[fd].mode = mode; | |
54 wrap_fds[fd].buf = buf; | |
55 wrap_fds[fd].closein = 0; | 64 wrap_fds[fd].closein = 0; |
56 wrap_fds[fd].closeout = 0; | 65 wrap_fds[fd].closeout = 0; |
57 wrap_used[nused] = fd; | 66 wrapfd_maxfd = MAX(fd, wrapfd_maxfd); |
58 | 67 |
59 nused++; | 68 return fd; |
60 } | 69 } |
61 | 70 |
62 void wrapfd_remove(int fd) { | 71 static void wrapfd_remove(int fd) { |
63 unsigned int i, j; | |
64 TRACE(("wrapfd_remove %d", fd)) | 72 TRACE(("wrapfd_remove %d", fd)) |
65 assert(fd >= 0); | 73 assert(fd >= 0); |
66 assert(fd <= IOWRAP_MAXFD); | 74 assert(fd <= IOWRAP_MAXFD); |
67 assert(wrap_fds[fd].mode != UNUSED); | 75 assert(wrap_fds[fd].mode != UNUSED); |
68 wrap_fds[fd].mode = UNUSED; | 76 wrap_fds[fd].mode = UNUSED; |
69 | 77 m_close(fd); |
70 | |
71 /* remove from used list */ | |
72 for (i = 0, j = 0; i < nused; i++) { | |
73 if (wrap_used[i] != fd) { | |
74 wrap_used[j] = wrap_used[i]; | |
75 j++; | |
76 } | |
77 } | |
78 nused--; | |
79 } | 78 } |
80 | 79 |
81 int wrapfd_close(int fd) { | 80 int wrapfd_close(int fd) { |
82 if (fd >= 0 && fd <= IOWRAP_MAXFD && wrap_fds[fd].mode != UNUSED) { | 81 if (fd >= 0 && fd <= IOWRAP_MAXFD && wrap_fds[fd].mode != UNUSED) { |
83 wrapfd_remove(fd); | 82 wrapfd_remove(fd); |
113 if (erand48(rand_state) < CHANCE_INTR) { | 112 if (erand48(rand_state) < CHANCE_INTR) { |
114 errno = EINTR; | 113 errno = EINTR; |
115 return -1; | 114 return -1; |
116 } | 115 } |
117 | 116 |
118 buf = wrap_fds[fd].buf; | 117 if (input_buf) { |
119 if (buf) { | 118 maxread = MIN(input_buf->len - input_buf->pos, count); |
120 maxread = MIN(buf->len - buf->pos, count); | |
121 /* returns 0 if buf is EOF, as intended */ | 119 /* returns 0 if buf is EOF, as intended */ |
122 if (maxread > 0) { | 120 if (maxread > 0) { |
123 maxread = nrand48(rand_state) % maxread + 1; | 121 maxread = nrand48(rand_state) % maxread + 1; |
124 } | 122 } |
125 memcpy(out, buf_getptr(buf, maxread), maxread); | 123 memcpy(out, buf_getptr(input_buf, maxread), maxread); |
126 buf_incrpos(buf, maxread); | 124 buf_incrpos(input_buf, maxread); |
127 return maxread; | 125 return maxread; |
128 } | 126 } |
129 | 127 |
130 maxread = MIN(MAX_RANDOM_IN, count); | 128 maxread = MIN(MAX_RANDOM_IN, count); |
131 maxread = nrand48(rand_state) % maxread + 1; | 129 maxread = nrand48(rand_state) % maxread + 1; |
172 int wrapfd_select(int nfds, fd_set *readfds, fd_set *writefds, | 170 int wrapfd_select(int nfds, fd_set *readfds, fd_set *writefds, |
173 fd_set *exceptfds, struct timeval *timeout) { | 171 fd_set *exceptfds, struct timeval *timeout) { |
174 int i, nset, sel; | 172 int i, nset, sel; |
175 int ret = 0; | 173 int ret = 0; |
176 int fdlist[IOWRAP_MAXFD+1]; | 174 int fdlist[IOWRAP_MAXFD+1]; |
177 | |
178 memset(fdlist, 0x0, sizeof(fdlist)); | |
179 | 175 |
180 if (!fuzz.wrapfds) { | 176 if (!fuzz.wrapfds) { |
181 return select(nfds, readfds, writefds, exceptfds, timeout); | 177 return select(nfds, readfds, writefds, exceptfds, timeout); |
182 } | 178 } |
183 | 179 |