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