chiark / gitweb /
regress: hfuzzraw: Fix a wrong pointer passed to fread
[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   static int done;
68
69   if (done) return;
70   done++;
71
72   int fd;
73
74   fd = Ttestinputfd();
75   assert(fd >= 0);
76   Tinputfile= fdopen(fd,"rb");
77     if (!Tinputfile) Tfailed("fdopen record fd");
78
79   while (fdtab.used < 3) {
80     const char fdfstd = FDF_OPEN;
81     if (!adns__vbuf_append(&fdtab,&fdfstd,1)) Tnomem();
82   }
83 }
84
85 static void P_read(void *p, size_t sz) {
86   ssize_t got = fread(p,1,sz,Tinputfile);
87   Pcheckinput();
88   assert(got==sz);
89 }
90
91 #define P_READ(x) (P_read(&(x), sizeof((x))))
92
93 static unsigned P_fdf(int fd) {
94   assert(fd>=0 && fd<fdtab.used);
95   return fdtab.buf[fd];
96 }
97
98 void T_gettimeofday_hook(void) {
99   struct timeval delta, sum;
100   P_READ(delta);
101   timeradd(&delta, &currenttime, &sum);
102   currenttime= sum;
103 }
104
105 static void Paddr(struct sockaddr *addr, int *lenr) {
106   int l, r;
107   uint16_t port;
108   char buf[512];
109   socklen_t sl = *lenr;
110
111   P_READ(l);
112   if (l<0 || l>=sizeof(buf)-1) Pformat("bad addr len");
113   buf[l]= 0;
114   P_READ(port);
115   r= adns_text2addr(buf,port, adns_qf_addrlit_scope_numeric, addr, &sl);
116   if (r==EINVAL) Pformat("bad addr text");
117   assert(r==ENOSPC);
118   *lenr = sl;
119 }
120
121 static int Pbytes(byte *buf, int maxlen) {
122   int l;
123   P_READ(l);
124   if (l<0 || l>maxlen) Pformat("bad byte block len");
125   P_read(buf, l);
126   return l;
127 }
128
129 static void Pfdset(fd_set *set, int max) {
130   uint16_t got;
131   int fd, ngot=0;
132
133   for (fd=0; fd<max; fd++) {
134     if (!FD_ISSET(fd,set)) continue;
135     P_fdf(fd);
136     if (ngot==0) {
137       P_READ(got);
138       ngot= 16;
139     }
140     if (!(got & 1u)) {
141       FD_CLR(fd,set);
142     }
143     got >>= 1;
144     ngot--;
145   }
146 }
147
148 #ifdef HAVE_POLL
149 static void Ppollfds(struct pollfd *fds, int nfds) {
150 int fd;
151   for (fd=0; fd<nfds; fd++) {
152     if (!fds[fd].events) continue;
153     P_fdf(fd);
154     P_READ(fds[fd].revents);
155   }
156 }
157 #endif
158
159 m4_define(`hm_syscall', `
160  hm_create_proto_h
161 int H$1(hm_args_massage($3,void)) {
162  int r;
163  m4_define(`hm_rv_fd',`')
164  m4_define(`hm_rv_any',`')
165  m4_define(`hm_rv_len',`')
166  m4_define(`hm_rv_must',`')
167  m4_define(`hm_rv_succfail',`')
168  m4_define(`hm_rv_fcntl',`')
169  $2
170
171  hm_create_hqcall_vars
172  $3
173
174  hm_create_hqcall_init($1)
175  $3
176
177  Tensurerecordfile();
178
179  m4_define(`hm_rv_succfail',`
180   P_READ(r);
181   if (r<0 && -r<Tnerrnos) {
182     errno= Terrnos[-r].v;
183     return -1;
184   } else if (r>0 && r<=255) {
185     errno= r;
186     return -1;
187   } else if (r) {
188     Pformat("wrong errno value");
189   }
190   r= 0;
191  ')
192
193  m4_define(`hm_rv_any',`
194   hm_rv_succfail
195   if (!r) {
196     P_READ(r);
197     if (r<0) Pformat("negative nonerror syscall return");
198   }
199  ')
200  m4_define(`hm_rv_len',`
201   hm_rv_any
202   if (r>($'`1)) Pformat("syscall length return is excessive");
203  ')
204  m4_define(`hm_rv_must',`
205   r= 0;
206  ')
207  m4_define(`hm_rv_fcntl',`
208   unsigned flg = P_fdf(fd);
209   if (cmd == F_GETFL) {
210     r= (flg & FDF_NONBLOCK) ? O_NONBLOCK : 0;
211   } else if (cmd == F_SETFL) {
212     flg &= ~FDF_NONBLOCK;
213     if (arg & O_NONBLOCK)
214       flg |= FDF_NONBLOCK;
215     fdtab.buf[fd]= flg;
216     r= 0;
217   } else {
218     abort();
219   }
220  ')
221  m4_define(`hm_rv_fd',`
222   hm_rv_succfail
223   if (!r) {
224     int newfd;
225     P_READ(newfd);
226     if (newfd<0 || newfd>1000) Pformat("new fd out of range");
227     adns__vbuf_ensure(&fdtab, newfd+1);
228     if (fdtab.used <= newfd) {
229       memset(fdtab.buf+fdtab.used, 0, newfd+1-fdtab.used);
230       fdtab.used= newfd+1;
231     }
232     if (fdtab.buf[newfd]) Pformat("new fd already in use");
233     fdtab.buf[newfd] |= FDF_OPEN;
234     r= newfd;
235  }
236  ')
237  $2
238
239  hm_create_nothing
240  m4_define(`hm_arg_fdset_io',`Pfdset($'`1,$'`2);')
241  m4_define(`hm_arg_pollfds_io',`Ppollfds($'`1,$'`2);')
242  m4_define(`hm_arg_addr_out',`Paddr($'`1,$'`2);')
243  $3
244
245  hm_create_nothing
246  m4_define(`hm_arg_bytes_out',`r= Pbytes($'`2,$'`4);')
247  $3
248
249  return r;
250 }
251 ')
252
253 m4_define(`hm_specsyscall', `')
254
255 m4_include(`hsyscalls.i4')
256
257 int Hclose(int fd) {
258   int r;
259   P_fdf(fd);
260   fdtab.buf[fd]= 0;
261   hm_rv_succfail
262   return r;
263 }