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