2 m4_dnl (part of complex test harness, not of the library)
3 m4_dnl - routines for fuzzing
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.)
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.
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.
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.
25 m4_include(hmacros.i4)
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
47 #define FDF_NONBLOCK 002u
49 static FILE *Tinputfile;
51 void Q_vb(void) { abort(); /* we neve called Q_anythig */ }
53 static void Pformat(const char *what) {
54 fprintf(stderr,"adns test harness: format error in raw log input file: %s\n",what);
58 extern void Tshutdown(void) {
61 static void Pcheckinput(void) {
62 if (ferror(Tinputfile)) Tfailed("read test log input file");
63 if (feof(Tinputfile)) Pformat("eof at syscall reply");
66 void Tensurerecordfile(void) {
71 Tinputfile= fdopen(fd,"rb");
72 if (!Tinputfile) Tfailed("fdopen record fd");
74 while (fdtab.used < 3) {
75 const char fdfstd = FDF_OPEN;
76 if (!adns__vbuf_append(&fdtab,&fdfstd,1)) Tnomem();
80 static void P_read(void *p, size_t sz) {
81 ssize_t got = fread(&p,1,sz,Tinputfile);
86 #define P_READ(x) (P_read(&(x), sizeof((x))))
88 static unsigned P_fdf(int fd) {
89 assert(fd>=0 && fd<fdtab.used);
93 void T_gettimeofday_hook(void) {
94 struct timeval delta, sum;
96 timeradd(&delta, ¤ttime, &sum);
100 static void Paddr(struct sockaddr *addr, int *lenr) {
104 socklen_t sl = *lenr;
107 if (l<0 || l>=sizeof(buf)-1) Pformat("bad addr len");
110 r= adns_text2addr(buf,port, adns_qf_addrlit_scope_numeric, addr, &sl);
111 if (r==EINVAL) Pformat("bad addr text");
116 static int Pbytes(byte *buf, int maxlen) {
119 if (l<0 || l>maxlen) Pformat("bad byte block len");
124 static void Pfdset(fd_set *set, int max) {
128 for (fd=0; fd<max; fd++) {
129 if (!FD_ISSET(fd,set)) continue;
144 static void Ppollfds(struct pollfd *fds, int nfds) {
146 for (fd=0; fd<nfds; fd++) {
147 if (!fds[fd].events) continue;
149 P_READ(fds[fd].revents);
154 m4_define(`hm_syscall', `
156 int H$1(hm_args_massage($3,void)) {
158 m4_define(`hm_rv_fd',`')
159 m4_define(`hm_rv_any',`')
160 m4_define(`hm_rv_len',`')
161 m4_define(`hm_rv_must',`')
162 m4_define(`hm_rv_succfail',`')
163 m4_define(`hm_rv_fcntl',`')
166 hm_create_hqcall_vars
169 hm_create_hqcall_init($1)
174 m4_define(`hm_rv_succfail',`
176 if (r<0 && -r<Tnerrnos) {
177 errno= Terrnos[-r].v;
179 } else if (r>0 && r<=255) {
183 Pformat("wrong errno value");
188 m4_define(`hm_rv_any',`
192 if (r<0) Pformat("negative nonerror syscall return");
195 m4_define(`hm_rv_len',`
197 if (r>($'`1)) Pformat("syscall length return is excessive");
199 m4_define(`hm_rv_must',`
202 m4_define(`hm_rv_fcntl',`
203 unsigned flg = P_fdf(fd);
204 if (cmd == F_GETFL) {
205 r= (flg & FDF_NONBLOCK) ? O_NONBLOCK : 0;
206 } else if (cmd == F_SETFL) {
207 flg &= ~FDF_NONBLOCK;
208 if (arg & O_NONBLOCK)
216 m4_define(`hm_rv_fd',`
222 if (!adns__vbuf_append(&fdtab,"\0",1)) Tnomem();
223 assert(r < fdtab.used);
224 if (!(fdtab.buf[r] & FDF_OPEN)) break;
227 fdtab.buf[r] |= FDF_OPEN;
233 m4_define(`hm_arg_fdset_io',`Pfdset($'`1,$'`2);')
234 m4_define(`hm_arg_pollfds_io',`Ppollfds($'`1,$'`2);')
235 m4_define(`hm_arg_addr_out',`Paddr($'`1,$'`2);')
239 m4_define(`hm_arg_bytes_out',`r= Pbytes($'`2,$'`4);')
246 m4_define(`hm_specsyscall', `')
248 m4_include(`hsyscalls.i4')