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, *traceout;
50 static int traceprint;
52 static void Tflushtrace( void) {
53 if (fflush(traceout)) Toutputerr();
56 void Tensuresetup(void) {
66 Tinputfile= fdopen(fd,"rb");
67 if (!Tinputfile) Tfailed("fdopen record fd");
69 while (fdtab.used < 3) {
70 const char fdfstd = FDF_OPEN;
71 if (!adns__vbuf_append(&fdtab,&fdfstd,1)) Tnomem();
74 const char *traceprintstr= getenv("ADNS_TEST_FUZZRAW_TRACEPRINT");
76 traceprint= atoi(traceprintstr);
78 if (tracefd<0) Tfailed("dup for tracefd");
79 traceout= fdopen(tracefd,"w");
80 if (!traceout) Tfailed("fdopen for traceout");
85 if (!traceprint) return; /* hcommon.c.m4 can call Q_vb directly */
86 if (!adns__vbuf_append(&vb,"",1)) Tnomem();
87 if (fprintf(traceout," %s\n",vb.buf) == EOF) Toutputerr();
91 static void Pformat(const char *what) {
92 fprintf(stderr,"adns test harness: format error in raw log input file: %s\n",what);
96 extern void Tshutdown(void) {
97 if (!Tinputfile) return;
98 int c= fgetc(Tinputfile);
99 if (c!=EOF) Pformat("unwanted additional syscall reply data");
100 if (ferror(Tinputfile)) Tfailed("read test log input (at end)");
103 static void Pcheckinput(void) {
104 if (ferror(Tinputfile)) Tfailed("read test log input file");
105 if (feof(Tinputfile)) Pformat("eof at syscall reply");
108 static void P_read_dump(const unsigned char *p0, size_t count, ssize_t d) {
109 fputs(" | ",traceout);
111 fprintf(traceout,"%02x", *p0);
117 static void P_read(void *p, size_t sz, const char *what) {
118 long pos = ftell(Tinputfile);
119 ssize_t got = fread(p,1,sz,Tinputfile);
122 if (traceprint>1 && sz) {
123 fprintf(traceout,"%8lx %8s:",pos,what);
124 P_read_dump(p, sz, +1);
126 P_read_dump((const unsigned char *)p+sz-1, sz, -1);
128 fputs(" |\n",traceout);
133 #define P_READ(x) (P_read(&(x), sizeof((x)), #x))
135 static unsigned P_fdf(int fd) {
136 assert(fd>=0 && fd<fdtab.used);
137 return fdtab.buf[fd];
140 void T_gettimeofday_hook(void) {
141 struct timeval delta, sum;
143 timeradd(&delta, ¤ttime, &sum);
147 static void Paddr(struct sockaddr *addr, int *lenr) {
151 socklen_t sl = *lenr;
154 if (al<0 || al>=sizeof(buf)-1) Pformat("bad addr len");
155 P_read(buf,al,"addrtext");
158 r= adns_text2addr(buf,port, adns_qf_addrlit_scope_numeric, addr, &sl);
159 if (r==EINVAL) Pformat("bad addr text");
160 assert(r==0 || r==ENOSPC);
164 static int Pbytes(byte *buf, int maxlen) {
167 if (bl<0 || bl>maxlen) Pformat("bad byte block len");
168 P_read(buf, bl, "bytes");
172 static void Pfdset(fd_set *set, int max, int *r_io) {
179 for (fd=max-1; fd>=0; fd--) {
184 _Bool y = fdmap & 1u;
188 if (!FD_ISSET(fd,set)) continue;
201 static void Psync(const char *exp, char *got, size_t sz, const char *what) {
202 P_read(got,sz,"syscall");
203 if (memcmp(exp,got,sz)) Pformat(what);
206 m4_define(`syscall_sync',`
208 hm_fr_syscall_ident($'`1)
209 static char sync_got[sizeof(sync_expect)];
210 Psync(sync_expect, sync_got, sizeof(sync_got), "sync lost: program did $1");
215 static void Ppollfds(struct pollfd *fds, int nfds, int *r_io) {
217 for (fd=0; fd<nfds; fd++) {
218 if (!fds[fd].events) continue;
220 P_READ(fds[fd].revents);
227 static int P_succfail(void) {
234 Pformat("wrong errno value");
239 m4_define(`hm_syscall', `
241 int H$1(hm_args_massage($3,void)) {
246 hm_create_hqcall_vars
249 hm_create_hqcall_init($1)
255 hm_create_hqcall_args
256 Q$1(hm_args_massage($3));
261 m4_define(`hm_rv_succfail',`
266 m4_define(`hm_rv_any',`
270 if (r<0) Pformat("negative nonerror syscall return");
273 m4_define(`hm_rv_len',`
276 m4_define(`hm_rv_must',`
279 m4_define(`hm_rv_select',`hm_rv_succfail')
280 m4_define(`hm_rv_poll',`hm_rv_succfail')
281 m4_define(`hm_rv_fcntl',`
282 unsigned flg = P_fdf(fd);
283 if (cmd == F_GETFL) {
284 r= (flg & FDF_NONBLOCK) ? O_NONBLOCK : 0;
285 } else if (cmd == F_SETFL) {
286 flg &= ~FDF_NONBLOCK;
287 if (arg & O_NONBLOCK)
295 m4_define(`hm_rv_fd',`
300 if (newfd<0 || newfd>1000) Pformat("new fd out of range");
301 adns__vbuf_ensure(&fdtab, newfd+1);
302 if (fdtab.used <= newfd) {
303 memset(fdtab.buf+fdtab.used, 0, newfd+1-fdtab.used);
306 if (fdtab.buf[newfd]) Pformat("new fd already in use");
307 fdtab.buf[newfd] |= FDF_OPEN;
314 m4_define(`hm_arg_fdset_io',`Pfdset($'`1,$'`2,&r);')
315 m4_define(`hm_arg_pollfds_io',`Ppollfds($'`1,$'`2,&r);')
316 m4_define(`hm_arg_addr_out',`Paddr($'`1,$'`2);')
320 m4_define(`hm_arg_bytes_out',`r= Pbytes($'`2,$'`4);')
324 m4_define(`hm_rv_selectpoll',`
325 if (($'`1) && !r) Pformat("select/poll returning 0 but infinite timeout");
327 m4_define(`hm_rv_select',`hm_rv_selectpoll(!to)')
328 m4_define(`hm_rv_poll',`hm_rv_selectpoll(timeout<0)')
335 m4_define(`hm_specsyscall', `')
337 m4_include(`hsyscalls.i4')