chiark / gitweb /
regress: hfuzzraw work in progress
[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
51 void Q_vb(void) { abort(); /* we neve called Q_anythig */ }
52
53 static void Pformat(const char *what) {
54   fprintf(stderr,"adns test harness: format error in raw log input file: %s\n",what);
55   exit(-1);
56 }
57
58 extern void Tshutdown(void) {
59 }
60
61 static void Pcheckinput(void) {
62   if (ferror(Tinputfile)) Tfailed("read test log input file");
63   if (feof(Tinputfile)) Pformat("eof at syscall reply");
64 }
65
66 void Tensurerecordfile(void) {
67   int fd;
68
69   fd = Ttestinputfd();
70   assert(fd >= 0);
71   Tinputfile= fdopen(fd,"rb");
72     if (!Tinputfile) Tfailed("fdopen record fd");
73
74   while (fdtab.used < 3) {
75     const char fdfstd = FDF_OPEN;
76     if (!adns__vbuf_append(&fdtab,&fdfstd,1)) Tnomem();
77   }
78 }
79
80 static void P_read(void *p, size_t sz) {
81   ssize_t got = fread(&p,1,sz,Tinputfile);
82   Pcheckinput();
83   assert(got==sz);
84 }
85
86 #define P_READ(x) (P_read(&(x), sizeof((x))))
87
88 static void P_updatetime(void) {
89 m4_dnl xxx
90 }
91
92 static unsigned P_fdf(int fd) {
93   assert(fd>=0 && fd<fdtab.used);
94   return fdtab.buf[fd];
95 }
96
97 static void Paddr(struct sockaddr *addr, int *lenr) {
98   int l, r;
99   uint16_t port;
100   char buf[512];
101   socklen_t sl = *lenr;
102
103   P_READ(l);
104   if (l<0 || l>=sizeof(buf)-1) Pformat("bad addr len");
105   buf[l]= 0;
106   P_READ(port);
107   r= adns_text2addr(buf,port, adns_qf_addrlit_scope_numeric, addr, &sl);
108   if (r==EINVAL) Pformat("bad addr text");
109   assert(r==ENOSPC);
110   *lenr = sl;
111 }
112
113 static int Pbytes(byte *buf, int maxlen) {
114   int l;
115   P_READ(l);
116   if (l<0 || l>maxlen) Pformat("bad byte block len");
117   P_read(buf, l);
118   return l;
119 }
120
121 static void Pfdset(fd_set *set, int max) {
122   uint16_t got;
123   int fd, ngot=0;
124
125   for (fd=0; fd<max; fd++) {
126     if (!FD_ISSET(fd,set)) continue;
127     P_fdf(fd);
128     if (ngot==0) {
129       P_READ(got);
130       ngot= 16;
131     }
132     if (!(got & 1u)) {
133       FD_CLR(fd,set);
134     }
135     got >>= 1;
136     ngot--;
137   }
138 }
139
140 #ifdef HAVE_POLL
141 static void Ppollfds(struct pollfd *fds, int nfds) {
142 int fd;
143   for (fd=0; fd<nfds; fd++) {
144     if (!fds[fd].events) continue;
145     P_fdf(fd);
146     P_READ(fds[fd].revents);
147   }
148 }
149 #endif
150
151 m4_define(`hm_syscall', `
152  hm_create_proto_h
153 int H$1(hm_args_massage($3,void)) {
154  int r;
155  m4_define(`hm_rv_fd',`')
156  m4_define(`hm_rv_any',`')
157  m4_define(`hm_rv_len',`')
158  m4_define(`hm_rv_must',`')
159  m4_define(`hm_rv_succfail',`')
160  m4_define(`hm_rv_fcntl',`')
161  $2
162
163  hm_create_hqcall_vars
164  $3
165
166  hm_create_hqcall_init($1)
167  $3
168
169  Tensurerecordfile();
170  P_updatetime();
171
172  m4_define(`hm_rv_succfail',`
173   P_READ(r);
174   if (r<0 && -r<Tnerrnos) {
175     errno= Terrnos[-r].v;
176     return -1;
177   } else if (r>0 && r<=255) {
178     errno= r;
179     return -1;
180   } else if (r) {
181     Pformat("wrong errno value");
182   }
183   r= 0;
184  ')
185
186  m4_define(`hm_rv_any',`
187   hm_rv_succfail
188   if (!r) {
189     P_READ(r);
190     if (r<0) Pformat("negative nonerror syscall return");
191   }
192  ')
193  m4_define(`hm_rv_len',`
194   hm_rv_any
195   if (r>($'`1)) Pformat("syscall length return is excessive");
196  ')
197  m4_define(`hm_rv_must',`
198   r= 0;
199  ')
200  m4_define(`hm_rv_fcntl',`
201   unsigned flg = P_fdf(fd);
202   if (cmd == F_GETFL) {
203     r= (flg & FDF_NONBLOCK) ? O_NONBLOCK : 0;
204   } else if (cmd == F_SETFL) {
205     flg &= ~FDF_NONBLOCK;
206     if (arg & O_NONBLOCK)
207       flg |= FDF_NONBLOCK;
208     fdtab.buf[fd]= flg;
209     r= 0;
210   } else {
211     abort();
212   }
213  ')
214  m4_define(`hm_rv_fd',`
215   hm_rv_succfail
216   if (!r) {
217     for (;;) {
218       assert(r < 1000);
219       if (r >= fdtab.used)
220         if (!adns__vbuf_append(&fdtab,"\0",1)) Tnomem();
221       assert(r < fdtab.used);
222       if (!(fdtab.buf[r] & FDF_OPEN)) break;
223       r++;
224     }
225     fdtab.buf[r] |= FDF_OPEN;
226  }
227  ')
228  $2
229
230  hm_create_nothing
231  m4_define(`hm_arg_fdset_io',`Pfdset($'`1,$'`2);')
232  m4_define(`hm_arg_pollfds_io',`Ppollfds($'`1,$'`2);')
233  m4_define(`hm_arg_addr_out',`Paddr($'`1,$'`2);')
234  $3
235
236  hm_create_nothing
237  m4_define(`hm_arg_bytes_out',`r= Pbytes($'`2,$'`4);')
238  $3
239
240  P_updatetime();
241  return r;
242 }
243 ')
244
245 m4_define(`hm_specsyscall', `')
246
247 m4_include(`hsyscalls.i4')
248