chiark / gitweb /
regress: hfuzzraw: Introduce P_read_dump
[adns.git] / regress / hfuzzraw.c.m4
index 726b1b9a1572613481971515c66f6e8b5f388821..a1a9d0fb7645520e2ad8cefadfa2e12ba76c2ca3 100644 (file)
@@ -49,10 +49,14 @@ static vbuf fdtab;
 static FILE *Tinputfile;
 static int stdout_enable;
 
+static void Tflushstdout( void) {
+  if (fflush(stdout)) Toutputerr();
+}
+
 void Q_vb(void) {
   if (!adns__vbuf_append(&vb,"",1)) Tnomem();
   if (fprintf(stdout," %s\n",vb.buf) == EOF) Toutputerr();
-  if (fflush(stdout)) Toutputerr();
+  Tflushstdout();
 }
 
 static void Pformat(const char *what) {
@@ -93,13 +97,32 @@ void Tensurerecordfile(void) {
   if (proutstr) stdout_enable= atoi(proutstr);
 }
 
-static void P_read(void *p, size_t sz) {
+
+static void P_read_dump(const unsigned char *p0, size_t count, ssize_t d) {
+  fputs(" | ",stdout);
+  while (count) {
+    fprintf(stdout,"%02x", *p0);
+    p0 += d;
+    count--;
+  }
+}
+    
+static void P_read(void *p, size_t sz, const char *what) {
   ssize_t got = fread(p,1,sz,Tinputfile);
   Pcheckinput();
   assert(got==sz);
+  if (stdout_enable && sz) {
+    fprintf(stdout,"%s:",what);
+    P_read_dump(p, sz, +1);
+    if (sz<=16) {
+      P_read_dump((const unsigned char *)p+sz-1, sz, -1);
+    }
+    fputs(" |\n",stdout);
+    Tflushstdout();
+  }
 }
 
-#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<fdtab.used);
@@ -133,11 +156,11 @@ 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);
+  P_read(buf, l, "bytes");
   return l;
 }
 
-static void Pfdset(fd_set *set, int max) {
+static void Pfdset(fd_set *set, int max, int *r_io) {
   uint16_t got;
   int fd, ngot=0;
 
@@ -148,7 +171,9 @@ static void Pfdset(fd_set *set, int max) {
       P_READ(got);
       ngot= 16;
     }
-    if (!(got & 1u)) {
+    if (got & 1u) {
+      (*r_io)++;
+    } else {
       FD_CLR(fd,set);
     }
     got >>= 1;
@@ -157,12 +182,14 @@ static void Pfdset(fd_set *set, int max) {
 }
 
 #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; fd<nfds; fd++) {
     if (!fds[fd].events) continue;
     P_fdf(fd);
     P_READ(fds[fd].revents);
+    if (fds[fd].revents)
+      (*r_io)++;
   }
 }
 #endif
@@ -186,12 +213,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
@@ -226,6 +248,8 @@ int H$1(hm_args_massage($3,void)) {
  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 +283,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 +292,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;
 }
 ')