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