X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=regress%2Fhfuzzraw.c.m4;h=0ed71a2c326232cba12bb4e72fdac6b58ea575ec;hb=a469269abe5aefc86ca0ef6344eb1359985759f5;hp=587f7f75268d4e7566ae4d13d2a1a3c276b96ac9;hpb=08bd26d700620541d547b1cc96089f5cbc98119f;p=adns.git diff --git a/regress/hfuzzraw.c.m4 b/regress/hfuzzraw.c.m4 index 587f7f7..0ed71a2 100644 --- a/regress/hfuzzraw.c.m4 +++ b/regress/hfuzzraw.c.m4 @@ -46,13 +46,43 @@ static vbuf fdtab; #define FDF_OPEN 001u #define FDF_NONBLOCK 002u -static FILE *Tinputfile; -static int stdout_enable; +static FILE *Tinputfile, *traceout; +static int traceprint; + +static void Tflushtrace( void) { + if (fflush(traceout)) Toutputerr(); +} + +void Tensuresetup(void) { + static int done; + + if (done) return; + done++; + + int fd; + + fd = Ttestinputfd(); + assert(fd >= 0); + Tinputfile= fdopen(fd,"rb"); + if (!Tinputfile) Tfailed("fdopen record fd"); + + while (fdtab.used < 3) { + const char fdfstd = FDF_OPEN; + if (!adns__vbuf_append(&fdtab,&fdfstd,1)) Tnomem(); + } + + const char *traceprintstr= getenv("ADNS_TEST_FUZZRAW_TRACEPRINT"); + if (traceprintstr) { + traceprint= atoi(traceprintstr); + traceout= fdopen(2,"w"); if (!traceout) Tfailed("fdopen for traceout"); + } +} void Q_vb(void) { + if (!traceprint) return; /* hcommon.c.m4 can call Q_vb directly */ if (!adns__vbuf_append(&vb,"",1)) Tnomem(); - if (fprintf(stdout," %s\n",vb.buf) == EOF) Toutputerr(); - if (fflush(stdout)) Toutputerr(); + if (fprintf(traceout," %s\n",vb.buf) == EOF) Toutputerr(); + Tflushtrace(); } static void Pformat(const char *what) { @@ -71,35 +101,32 @@ static void Pcheckinput(void) { if (feof(Tinputfile)) Pformat("eof at syscall reply"); } -void Tensurerecordfile(void) { - static int done; - - if (done) return; - done++; - - int fd; - - fd = Ttestinputfd(); - assert(fd >= 0); - Tinputfile= fdopen(fd,"rb"); - if (!Tinputfile) Tfailed("fdopen record fd"); - - while (fdtab.used < 3) { - const char fdfstd = FDF_OPEN; - if (!adns__vbuf_append(&fdtab,&fdfstd,1)) Tnomem(); +static void P_read_dump(const unsigned char *p0, size_t count, ssize_t d) { + fputs(" | ",traceout); + while (count) { + fprintf(traceout,"%02x", *p0); + p0 += d; + count--; } - - const char *proutstr= getenv("ADNS_TEST_FUZZRAW_STDOUT_ENABLE"); - if (proutstr) stdout_enable= atoi(proutstr); } - -static void P_read(void *p, size_t sz) { + +static void P_read(void *p, size_t sz, const char *what) { + long pos = ftell(Tinputfile); ssize_t got = fread(p,1,sz,Tinputfile); Pcheckinput(); assert(got==sz); + if (traceprint>1 && sz) { + fprintf(traceout,"%8lx %8s:",pos,what); + P_read_dump(p, sz, +1); + if (sz<=16) { + P_read_dump((const unsigned char *)p+sz-1, sz, -1); + } + fputs(" |\n",traceout); + Tflushtrace(); + } } -#define P_READ(x) (P_read(&(x), sizeof((x)))) +#define P_READ(x) (P_read(&(x), sizeof((x)), #x)) static unsigned P_fdf(int fd) { assert(fd>=0 && fd=sizeof(buf)-1) Pformat("bad addr len"); - buf[l]= 0; + P_READ(al); + if (al<0 || al>=sizeof(buf)-1) Pformat("bad addr len"); + P_read(buf,al,"addrtext"); + buf[al]= 0; P_READ(port); r= adns_text2addr(buf,port, adns_qf_addrlit_scope_numeric, addr, &sl); if (r==EINVAL) Pformat("bad addr text"); - assert(r==ENOSPC); + assert(r==0 || r==ENOSPC); *lenr = sl; } static int Pbytes(byte *buf, int maxlen) { - int l; - P_READ(l); - if (l<0 || l>maxlen) Pformat("bad byte block len"); - P_read(buf, l); - return l; + int bl; + P_READ(bl); + if (bl<0 || bl>maxlen) Pformat("bad byte block len"); + P_read(buf, bl, "bytes"); + return bl; } -static void Pfdset(fd_set *set, int max) { - uint16_t got; - int fd, ngot=0; +static void Pfdset(fd_set *set, int max, int *r_io) { + uint16_t fdmap; + int fd, nfdmap=0; + + if (!set) + return; + + for (fd=max-1; fd>=0; fd--) { + if (nfdmap==0) { + P_READ(fdmap); + nfdmap= 16; + } + _Bool y = fdmap & 1u; + fdmap >>= 1; + nfdmap--; - for (fd=0; fd>= 1; - ngot--; } } +#ifdef FUZZRAW_SYNC +static void Psync(const char *exp, char *got, size_t sz, const char *what) { + P_read(got,sz,"syscall"); + if (memcmp(exp,got,sz)) Pformat(what); +} +#endif +m4_define(`syscall_sync',` +#ifdef FUZZRAW_SYNC + hm_fr_syscall_ident($'`1) + static char sync_got[sizeof(sync_expect)]; + Psync(sync_expect, sync_got, sizeof(sync_got), "sync lost: program did $1"); +#endif +') + #ifdef HAVE_POLL -static void Ppollfds(struct pollfd *fds, int nfds) { -int fd; +static void Ppollfds(struct pollfd *fds, int nfds, int *r_io) { + int fd; for (fd=0; fd0) { + errno= e; return -1; - } else if (r>0 && r<=255) { - errno= r; - return -1; - } else if (r) { + } else if (e) { Pformat("wrong errno value"); } return 0; @@ -186,12 +236,7 @@ m4_define(`hm_syscall', ` hm_create_proto_h int H$1(hm_args_massage($3,void)) { int r; - m4_define(`hm_rv_fd',`') - m4_define(`hm_rv_any',`') - m4_define(`hm_rv_len',`') - m4_define(`hm_rv_must',`') - m4_define(`hm_rv_succfail',`') - m4_define(`hm_rv_fcntl',`') + hm_create_nothing $2 hm_create_hqcall_vars @@ -200,13 +245,15 @@ int H$1(hm_args_massage($3,void)) { hm_create_hqcall_init($1) $3 - Tensurerecordfile(); + Tensuresetup(); - if (stdout_enable) { + if (traceprint) { hm_create_hqcall_args Q$1(hm_args_massage($3)); } + syscall_sync($'`1) + m4_define(`hm_rv_succfail',` r= P_succfail(); if (r<0) return r; @@ -220,12 +267,13 @@ int H$1(hm_args_massage($3,void)) { } ') m4_define(`hm_rv_len',` - hm_rv_any - if (r>($'`1)) Pformat("syscall length return is excessive"); + hm_rv_succfail ') m4_define(`hm_rv_must',` r= 0; ') + m4_define(`hm_rv_select',`hm_rv_succfail') + m4_define(`hm_rv_poll',`hm_rv_succfail') m4_define(`hm_rv_fcntl',` unsigned flg = P_fdf(fd); if (cmd == F_GETFL) { @@ -259,8 +307,8 @@ int H$1(hm_args_massage($3,void)) { $2 hm_create_nothing - m4_define(`hm_arg_fdset_io',`Pfdset($'`1,$'`2);') - m4_define(`hm_arg_pollfds_io',`Ppollfds($'`1,$'`2);') + m4_define(`hm_arg_fdset_io',`Pfdset($'`1,$'`2,&r);') + m4_define(`hm_arg_pollfds_io',`Ppollfds($'`1,$'`2,&r);') m4_define(`hm_arg_addr_out',`Paddr($'`1,$'`2);') $3 @@ -268,6 +316,14 @@ int H$1(hm_args_massage($3,void)) { m4_define(`hm_arg_bytes_out',`r= Pbytes($'`2,$'`4);') $3 + hm_create_nothing + m4_define(`hm_rv_selectpoll',` + if (($'`1) && !r) Pformat("select/poll returning 0 but infinite timeout"); + ') + m4_define(`hm_rv_select',`hm_rv_selectpoll(!to)') + m4_define(`hm_rv_poll',`hm_rv_selectpoll(timeout<0)') + $2 + return r; } ') @@ -277,9 +333,8 @@ m4_define(`hm_specsyscall', `') m4_include(`hsyscalls.i4') int Hclose(int fd) { - int r; + syscall_sync(close) P_fdf(fd); fdtab.buf[fd]= 0; - hm_rv_succfail - return r; + return P_succfail(); }