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