chiark / gitweb /
regress: fuzzraw: Handle select and poll return value specially
[adns.git] / regress / hfuzzraw.c.m4
1 m4_dnl hfuzzraw.c.m4
2 m4_dnl (part of complex test harness, not of the library)
3 m4_dnl - routines for fuzzing
4
5 m4_dnl  This file is part of adns, which is
6 m4_dnl    Copyright (C) 1997-2000,2003,2006,2014-2016  Ian Jackson
7 m4_dnl    Copyright (C) 2014  Mark Wooding
8 m4_dnl    Copyright (C) 1999-2000,2003,2006  Tony Finch
9 m4_dnl    Copyright (C) 1991 Massachusetts Institute of Technology
10 m4_dnl  (See the file INSTALL for full details.)
11 m4_dnl  
12 m4_dnl  This program is free software; you can redistribute it and/or modify
13 m4_dnl  it under the terms of the GNU General Public License as published by
14 m4_dnl  the Free Software Foundation; either version 3, or (at your option)
15 m4_dnl  any later version.
16 m4_dnl  
17 m4_dnl  This program is distributed in the hope that it will be useful,
18 m4_dnl  but WITHOUT ANY WARRANTY; without even the implied warranty of
19 m4_dnl  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 m4_dnl  GNU General Public License for more details.
21 m4_dnl  
22 m4_dnl  You should have received a copy of the GNU General Public License
23 m4_dnl  along with this program; if not, write to the Free Software Foundation.
24
25 m4_include(hmacros.i4)
26
27 #include <assert.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <stdlib.h>
31
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
36 #include <sys/time.h>
37
38 #include <unistd.h>
39 #include <fcntl.h>
40 #include <limits.h>
41
42
43 #include "harness.h"
44
45 static vbuf fdtab;
46 #define FDF_OPEN     001u
47 #define FDF_NONBLOCK 002u
48
49 static FILE *Tinputfile;
50 static int stdout_enable;
51
52 void Q_vb(void) {
53   if (!adns__vbuf_append(&vb,"",1)) Tnomem();
54   if (fprintf(stdout," %s\n",vb.buf) == EOF) Toutputerr();
55   if (fflush(stdout)) Toutputerr();
56 }
57
58 static void Pformat(const char *what) {
59   fprintf(stderr,"adns test harness: format error in raw log input file: %s\n",what);
60   exit(-1);
61 }
62
63 extern void Tshutdown(void) {
64   int c= fgetc(Tinputfile);
65   if (c!=EOF) Pformat("unwanted additional syscall reply data");
66   if (ferror(Tinputfile)) Tfailed("read test log input (at end)");
67 }
68
69 static void Pcheckinput(void) {
70   if (ferror(Tinputfile)) Tfailed("read test log input file");
71   if (feof(Tinputfile)) Pformat("eof at syscall reply");
72 }
73
74 void Tensurerecordfile(void) {
75   static int done;
76
77   if (done) return;
78   done++;
79
80   int fd;
81
82   fd = Ttestinputfd();
83   assert(fd >= 0);
84   Tinputfile= fdopen(fd,"rb");
85     if (!Tinputfile) Tfailed("fdopen record fd");
86
87   while (fdtab.used < 3) {
88     const char fdfstd = FDF_OPEN;
89     if (!adns__vbuf_append(&fdtab,&fdfstd,1)) Tnomem();
90   }
91
92   const char *proutstr= getenv("ADNS_TEST_FUZZRAW_STDOUT_ENABLE");
93   if (proutstr) stdout_enable= atoi(proutstr);
94 }
95
96 static void P_read(void *p, size_t sz) {
97   ssize_t got = fread(p,1,sz,Tinputfile);
98   Pcheckinput();
99   assert(got==sz);
100 }
101
102 #define P_READ(x) (P_read(&(x), sizeof((x))))
103
104 static unsigned P_fdf(int fd) {
105   assert(fd>=0 && fd<fdtab.used);
106   return fdtab.buf[fd];
107 }
108
109 void T_gettimeofday_hook(void) {
110   struct timeval delta, sum;
111   P_READ(delta);
112   timeradd(&delta, &currenttime, &sum);
113   currenttime= sum;
114 }
115
116 static void Paddr(struct sockaddr *addr, int *lenr) {
117   int l, r;
118   uint16_t port;
119   char buf[512];
120   socklen_t sl = *lenr;
121
122   P_READ(l);
123   if (l<0 || l>=sizeof(buf)-1) Pformat("bad addr len");
124   buf[l]= 0;
125   P_READ(port);
126   r= adns_text2addr(buf,port, adns_qf_addrlit_scope_numeric, addr, &sl);
127   if (r==EINVAL) Pformat("bad addr text");
128   assert(r==ENOSPC);
129   *lenr = sl;
130 }
131
132 static int Pbytes(byte *buf, int maxlen) {
133   int l;
134   P_READ(l);
135   if (l<0 || l>maxlen) Pformat("bad byte block len");
136   P_read(buf, l);
137   return l;
138 }
139
140 static void Pfdset(fd_set *set, int max, int *r_io) {
141   uint16_t got;
142   int fd, ngot=0;
143
144   for (fd=0; fd<max; fd++) {
145     if (!FD_ISSET(fd,set)) continue;
146     P_fdf(fd);
147     if (ngot==0) {
148       P_READ(got);
149       ngot= 16;
150     }
151     if (got & 1u) {
152       (*r_io)++;
153     } else {
154       FD_CLR(fd,set);
155     }
156     got >>= 1;
157     ngot--;
158   }
159 }
160
161 #ifdef HAVE_POLL
162 static void Ppollfds(struct pollfd *fds, int nfds, int *r_io) {
163   int fd;
164   for (fd=0; fd<nfds; fd++) {
165     if (!fds[fd].events) continue;
166     P_fdf(fd);
167     P_READ(fds[fd].revents);
168     if (fds[fd].revents)
169       (*r_io)++;
170   }
171 }
172 #endif
173
174 static int P_succfail(void) {
175   int r;
176   P_READ(r);
177   if (r<0 && -r<Tnerrnos) {
178     errno= Terrnos[-r].v;
179     return -1;
180   } else if (r>0 && r<=255) {
181     errno= r;
182     return -1;
183   } else if (r) {
184     Pformat("wrong errno value");
185   }
186   return 0;
187 }
188
189 m4_define(`hm_syscall', `
190  hm_create_proto_h
191 int H$1(hm_args_massage($3,void)) {
192  int r;
193  hm_create_nothing
194  $2
195
196  hm_create_hqcall_vars
197  $3
198
199  hm_create_hqcall_init($1)
200  $3
201
202  Tensurerecordfile();
203
204  if (stdout_enable) {
205    hm_create_hqcall_args
206    Q$1(hm_args_massage($3));
207  }
208
209  m4_define(`hm_rv_succfail',`
210   r= P_succfail();
211   if (r<0) return r;
212  ')
213
214  m4_define(`hm_rv_any',`
215   hm_rv_succfail
216   if (!r) {
217     P_READ(r);
218     if (r<0) Pformat("negative nonerror syscall return");
219   }
220  ')
221  m4_define(`hm_rv_len',`
222   hm_rv_any
223   if (r>($'`1)) Pformat("syscall length return is excessive");
224  ')
225  m4_define(`hm_rv_must',`
226   r= 0;
227  ')
228  m4_define(`hm_rv_select',`hm_rv_succfail')
229  m4_define(`hm_rv_poll',`hm_rv_succfail')
230  m4_define(`hm_rv_fcntl',`
231   unsigned flg = P_fdf(fd);
232   if (cmd == F_GETFL) {
233     r= (flg & FDF_NONBLOCK) ? O_NONBLOCK : 0;
234   } else if (cmd == F_SETFL) {
235     flg &= ~FDF_NONBLOCK;
236     if (arg & O_NONBLOCK)
237       flg |= FDF_NONBLOCK;
238     fdtab.buf[fd]= flg;
239     r= 0;
240   } else {
241     abort();
242   }
243  ')
244  m4_define(`hm_rv_fd',`
245   hm_rv_succfail
246   if (!r) {
247     int newfd;
248     P_READ(newfd);
249     if (newfd<0 || newfd>1000) Pformat("new fd out of range");
250     adns__vbuf_ensure(&fdtab, newfd+1);
251     if (fdtab.used <= newfd) {
252       memset(fdtab.buf+fdtab.used, 0, newfd+1-fdtab.used);
253       fdtab.used= newfd+1;
254     }
255     if (fdtab.buf[newfd]) Pformat("new fd already in use");
256     fdtab.buf[newfd] |= FDF_OPEN;
257     r= newfd;
258  }
259  ')
260  $2
261
262  hm_create_nothing
263  m4_define(`hm_arg_fdset_io',`Pfdset($'`1,$'`2,&r);')
264  m4_define(`hm_arg_pollfds_io',`Ppollfds($'`1,$'`2,&r);')
265  m4_define(`hm_arg_addr_out',`Paddr($'`1,$'`2);')
266  $3
267
268  hm_create_nothing
269  m4_define(`hm_arg_bytes_out',`r= Pbytes($'`2,$'`4);')
270  $3
271
272  hm_create_nothing
273  m4_define(`hm_rv_selectpoll',`
274   if (($'`1) && !r) Pformat("select/poll returning 0 but infinite timeout");
275  ')
276  m4_define(`hm_rv_select',`hm_rv_selectpoll(!to)')
277  m4_define(`hm_rv_poll',`hm_rv_selectpoll(timeout<0)')
278  $2
279
280  return r;
281 }
282 ')
283
284 m4_define(`hm_specsyscall', `')
285
286 m4_include(`hsyscalls.i4')
287
288 int Hclose(int fd) {
289   P_fdf(fd);
290   fdtab.buf[fd]= 0;
291   return P_succfail();
292 }