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;
50 static int stdout_enable;
53 if (!adns__vbuf_append(&vb,"",1)) Tnomem();
54 if (fprintf(stdout," %s\n",vb.buf) == EOF) Toutputerr();
55 if (fflush(stdout)) Toutputerr();
58 static void Pformat(const char *what) {
59 fprintf(stderr,"adns test harness: format error in raw log input file: %s\n",what);
63 extern void Tshutdown(void) {
64 int c= fgetc(Tinputfile);
65 if (c!=EOF) Pformat("unwanted additional syscall reply data");
66 if (ferror(Tinputfile)) Tfailed("read test log input (at end)");
69 static void Pcheckinput(void) {
70 if (ferror(Tinputfile)) Tfailed("read test log input file");
71 if (feof(Tinputfile)) Pformat("eof at syscall reply");
74 void Tensurerecordfile(void) {
84 Tinputfile= fdopen(fd,"rb");
85 if (!Tinputfile) Tfailed("fdopen record fd");
87 while (fdtab.used < 3) {
88 const char fdfstd = FDF_OPEN;
89 if (!adns__vbuf_append(&fdtab,&fdfstd,1)) Tnomem();
92 const char *proutstr= getenv("ADNS_TEST_FUZZRAW_STDOUT_ENABLE");
93 if (proutstr) stdout_enable= atoi(proutstr);
96 static void P_read(void *p, size_t sz) {
97 ssize_t got = fread(p,1,sz,Tinputfile);
102 #define P_READ(x) (P_read(&(x), sizeof((x))))
104 static unsigned P_fdf(int fd) {
105 assert(fd>=0 && fd<fdtab.used);
106 return fdtab.buf[fd];
109 void T_gettimeofday_hook(void) {
110 struct timeval delta, sum;
112 timeradd(&delta, ¤ttime, &sum);
116 static void Paddr(struct sockaddr *addr, int *lenr) {
120 socklen_t sl = *lenr;
123 if (l<0 || l>=sizeof(buf)-1) Pformat("bad addr len");
126 r= adns_text2addr(buf,port, adns_qf_addrlit_scope_numeric, addr, &sl);
127 if (r==EINVAL) Pformat("bad addr text");
132 static int Pbytes(byte *buf, int maxlen) {
135 if (l<0 || l>maxlen) Pformat("bad byte block len");
140 static void Pfdset(fd_set *set, int max) {
144 for (fd=0; fd<max; fd++) {
145 if (!FD_ISSET(fd,set)) continue;
160 static void Ppollfds(struct pollfd *fds, int nfds) {
162 for (fd=0; fd<nfds; fd++) {
163 if (!fds[fd].events) continue;
165 P_READ(fds[fd].revents);
170 static int P_succfail(void) {
173 if (r<0 && -r<Tnerrnos) {
174 errno= Terrnos[-r].v;
176 } else if (r>0 && r<=255) {
180 Pformat("wrong errno value");
185 m4_define(`hm_syscall', `
187 int H$1(hm_args_massage($3,void)) {
192 hm_create_hqcall_vars
195 hm_create_hqcall_init($1)
201 hm_create_hqcall_args
202 Q$1(hm_args_massage($3));
205 m4_define(`hm_rv_succfail',`
210 m4_define(`hm_rv_any',`
214 if (r<0) Pformat("negative nonerror syscall return");
217 m4_define(`hm_rv_len',`
219 if (r>($'`1)) Pformat("syscall length return is excessive");
221 m4_define(`hm_rv_must',`
224 m4_define(`hm_rv_fcntl',`
225 unsigned flg = P_fdf(fd);
226 if (cmd == F_GETFL) {
227 r= (flg & FDF_NONBLOCK) ? O_NONBLOCK : 0;
228 } else if (cmd == F_SETFL) {
229 flg &= ~FDF_NONBLOCK;
230 if (arg & O_NONBLOCK)
238 m4_define(`hm_rv_fd',`
243 if (newfd<0 || newfd>1000) Pformat("new fd out of range");
244 adns__vbuf_ensure(&fdtab, newfd+1);
245 if (fdtab.used <= newfd) {
246 memset(fdtab.buf+fdtab.used, 0, newfd+1-fdtab.used);
249 if (fdtab.buf[newfd]) Pformat("new fd already in use");
250 fdtab.buf[newfd] |= FDF_OPEN;
257 m4_define(`hm_arg_fdset_io',`Pfdset($'`1,$'`2);')
258 m4_define(`hm_arg_pollfds_io',`Ppollfds($'`1,$'`2);')
259 m4_define(`hm_arg_addr_out',`Paddr($'`1,$'`2);')
263 m4_define(`hm_arg_bytes_out',`r= Pbytes($'`2,$'`4);')
270 m4_define(`hm_specsyscall', `')
272 m4_include(`hsyscalls.i4')