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