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