chiark / gitweb /
regress: hfuzzraw: Break out Tflushstdout
[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 static void Tflushstdout( void) {
53   if (fflush(stdout)) Toutputerr();
54 }
55
56 void Q_vb(void) {
57   if (!adns__vbuf_append(&vb,"",1)) Tnomem();
58   if (fprintf(stdout," %s\n",vb.buf) == EOF) Toutputerr();
59   Tflushstdout();
60 }
61
62 static void Pformat(const char *what) {
63   fprintf(stderr,"adns test harness: format error in raw log input file: %s\n",what);
64   exit(-1);
65 }
66
67 extern void Tshutdown(void) {
68   int c= fgetc(Tinputfile);
69   if (c!=EOF) Pformat("unwanted additional syscall reply data");
70   if (ferror(Tinputfile)) Tfailed("read test log input (at end)");
71 }
72
73 static void Pcheckinput(void) {
74   if (ferror(Tinputfile)) Tfailed("read test log input file");
75   if (feof(Tinputfile)) Pformat("eof at syscall reply");
76 }
77
78 void Tensurerecordfile(void) {
79   static int done;
80
81   if (done) return;
82   done++;
83
84   int fd;
85
86   fd = Ttestinputfd();
87   assert(fd >= 0);
88   Tinputfile= fdopen(fd,"rb");
89     if (!Tinputfile) Tfailed("fdopen record fd");
90
91   while (fdtab.used < 3) {
92     const char fdfstd = FDF_OPEN;
93     if (!adns__vbuf_append(&fdtab,&fdfstd,1)) Tnomem();
94   }
95
96   const char *proutstr= getenv("ADNS_TEST_FUZZRAW_STDOUT_ENABLE");
97   if (proutstr) stdout_enable= atoi(proutstr);
98 }
99
100 static void P_read(void *p, size_t sz) {
101   ssize_t got = fread(p,1,sz,Tinputfile);
102   Pcheckinput();
103   assert(got==sz);
104 }
105
106 #define P_READ(x) (P_read(&(x), sizeof((x))))
107
108 static unsigned P_fdf(int fd) {
109   assert(fd>=0 && fd<fdtab.used);
110   return fdtab.buf[fd];
111 }
112
113 void T_gettimeofday_hook(void) {
114   struct timeval delta, sum;
115   P_READ(delta);
116   timeradd(&delta, &currenttime, &sum);
117   currenttime= sum;
118 }
119
120 static void Paddr(struct sockaddr *addr, int *lenr) {
121   int l, r;
122   uint16_t port;
123   char buf[512];
124   socklen_t sl = *lenr;
125
126   P_READ(l);
127   if (l<0 || l>=sizeof(buf)-1) Pformat("bad addr len");
128   buf[l]= 0;
129   P_READ(port);
130   r= adns_text2addr(buf,port, adns_qf_addrlit_scope_numeric, addr, &sl);
131   if (r==EINVAL) Pformat("bad addr text");
132   assert(r==ENOSPC);
133   *lenr = sl;
134 }
135
136 static int Pbytes(byte *buf, int maxlen) {
137   int l;
138   P_READ(l);
139   if (l<0 || l>maxlen) Pformat("bad byte block len");
140   P_read(buf, l);
141   return l;
142 }
143
144 static void Pfdset(fd_set *set, int max, int *r_io) {
145   uint16_t got;
146   int fd, ngot=0;
147
148   for (fd=0; fd<max; fd++) {
149     if (!FD_ISSET(fd,set)) continue;
150     P_fdf(fd);
151     if (ngot==0) {
152       P_READ(got);
153       ngot= 16;
154     }
155     if (got & 1u) {
156       (*r_io)++;
157     } else {
158       FD_CLR(fd,set);
159     }
160     got >>= 1;
161     ngot--;
162   }
163 }
164
165 #ifdef HAVE_POLL
166 static void Ppollfds(struct pollfd *fds, int nfds, int *r_io) {
167   int fd;
168   for (fd=0; fd<nfds; fd++) {
169     if (!fds[fd].events) continue;
170     P_fdf(fd);
171     P_READ(fds[fd].revents);
172     if (fds[fd].revents)
173       (*r_io)++;
174   }
175 }
176 #endif
177
178 static int P_succfail(void) {
179   int r;
180   P_READ(r);
181   if (r<0 && -r<Tnerrnos) {
182     errno= Terrnos[-r].v;
183     return -1;
184   } else if (r>0 && r<=255) {
185     errno= r;
186     return -1;
187   } else if (r) {
188     Pformat("wrong errno value");
189   }
190   return 0;
191 }
192
193 m4_define(`hm_syscall', `
194  hm_create_proto_h
195 int H$1(hm_args_massage($3,void)) {
196  int r;
197  hm_create_nothing
198  $2
199
200  hm_create_hqcall_vars
201  $3
202
203  hm_create_hqcall_init($1)
204  $3
205
206  Tensurerecordfile();
207
208  if (stdout_enable) {
209    hm_create_hqcall_args
210    Q$1(hm_args_massage($3));
211  }
212
213  m4_define(`hm_rv_succfail',`
214   r= P_succfail();
215   if (r<0) return r;
216  ')
217
218  m4_define(`hm_rv_any',`
219   hm_rv_succfail
220   if (!r) {
221     P_READ(r);
222     if (r<0) Pformat("negative nonerror syscall return");
223   }
224  ')
225  m4_define(`hm_rv_len',`
226   hm_rv_any
227   if (r>($'`1)) Pformat("syscall length return is excessive");
228  ')
229  m4_define(`hm_rv_must',`
230   r= 0;
231  ')
232  m4_define(`hm_rv_select',`hm_rv_succfail')
233  m4_define(`hm_rv_poll',`hm_rv_succfail')
234  m4_define(`hm_rv_fcntl',`
235   unsigned flg = P_fdf(fd);
236   if (cmd == F_GETFL) {
237     r= (flg & FDF_NONBLOCK) ? O_NONBLOCK : 0;
238   } else if (cmd == F_SETFL) {
239     flg &= ~FDF_NONBLOCK;
240     if (arg & O_NONBLOCK)
241       flg |= FDF_NONBLOCK;
242     fdtab.buf[fd]= flg;
243     r= 0;
244   } else {
245     abort();
246   }
247  ')
248  m4_define(`hm_rv_fd',`
249   hm_rv_succfail
250   if (!r) {
251     int newfd;
252     P_READ(newfd);
253     if (newfd<0 || newfd>1000) Pformat("new fd out of range");
254     adns__vbuf_ensure(&fdtab, newfd+1);
255     if (fdtab.used <= newfd) {
256       memset(fdtab.buf+fdtab.used, 0, newfd+1-fdtab.used);
257       fdtab.used= newfd+1;
258     }
259     if (fdtab.buf[newfd]) Pformat("new fd already in use");
260     fdtab.buf[newfd] |= FDF_OPEN;
261     r= newfd;
262  }
263  ')
264  $2
265
266  hm_create_nothing
267  m4_define(`hm_arg_fdset_io',`Pfdset($'`1,$'`2,&r);')
268  m4_define(`hm_arg_pollfds_io',`Ppollfds($'`1,$'`2,&r);')
269  m4_define(`hm_arg_addr_out',`Paddr($'`1,$'`2);')
270  $3
271
272  hm_create_nothing
273  m4_define(`hm_arg_bytes_out',`r= Pbytes($'`2,$'`4);')
274  $3
275
276  hm_create_nothing
277  m4_define(`hm_rv_selectpoll',`
278   if (($'`1) && !r) Pformat("select/poll returning 0 but infinite timeout");
279  ')
280  m4_define(`hm_rv_select',`hm_rv_selectpoll(!to)')
281  m4_define(`hm_rv_poll',`hm_rv_selectpoll(timeout<0)')
282  $2
283
284  return r;
285 }
286 ')
287
288 m4_define(`hm_specsyscall', `')
289
290 m4_include(`hsyscalls.i4')
291
292 int Hclose(int fd) {
293   P_fdf(fd);
294   fdtab.buf[fd]= 0;
295   return P_succfail();
296 }