It now makes use of the __AFL_LOOP macro to save on expensive exec(3)
calls.
---
 contrib/hpack/decode.c | 77 +++++++++++++++++++++++-------------------
 1 file changed, 43 insertions(+), 34 deletions(-)

diff --git a/contrib/hpack/decode.c b/contrib/hpack/decode.c
index 9ef80f03..880c4571 100644
--- a/contrib/hpack/decode.c
+++ b/contrib/hpack/decode.c
@@ -22,6 +22,11 @@
 #define MAX_RQ_SIZE 65536
 #define MAX_HDR_NUM 1000
 
+#ifndef __AFL_LOOP
+int __AFL_LOOP_POLYFILL = 1;
+#define __AFL_LOOP(_) (__AFL_LOOP_POLYFILL--)
+#endif
+
 char hex[MAX_RQ_SIZE*3+3]; // enough for "[ XX]* <CR> <LF> \0"
 uint8_t buf[MAX_RQ_SIZE];
 
@@ -164,47 +169,51 @@ int main(int argc, char **argv)
                argv++; argc--;
        }
 
-       dht = hpack_dht_alloc(dht_size);
-       if (!dht) {
-               die(1, "cannot initialize dht\n");
-               return 1;
-       }
-
-       for (line = 1; fgets(hex, sizeof(hex), stdin); line++) {
-               len = hex2bin(hex, buf, sizeof(buf));
-               if (len <= 0)
-                       continue;
-               printf("###### line %d : frame len=%d #######\n", line, len);
-               debug_hexdump(stdout, "   ", (const char *)buf, 0, len);
-
-               outlen = hpack_decode_frame(dht, buf, len, list,
-                                           sizeof(list)/sizeof(list[0]), &tmp);
-               if (outlen <= 0) {
-                       printf("   HPACK decoding failed: %d\n", outlen);
-                       continue;
+       while (__AFL_LOOP(1000)) {
+               dht = hpack_dht_alloc(dht_size);
+               if (!dht) {
+                       die(1, "cannot initialize dht\n");
+                       return 1;
                }
 
-               printf("<<< Found %d headers :\n", outlen);
-               for (idx = 0; idx < outlen - 1; idx++) {
-                       //printf("      \e[1;34m%s\e[0m: ",
-                       //       list[idx].n.ptr ? istpad(trash.str, 
list[idx].n).ptr : h2_phdr_to_str(list[idx].n.len));
+               for (line = 1; fgets(hex, sizeof(hex), stdin); line++) {
+                       len = hex2bin(hex, buf, sizeof(buf));
+                       if (len <= 0)
+                               continue;
+                       printf("###### line %d : frame len=%d #######\n", line, 
len);
+                       debug_hexdump(stdout, "   ", (const char *)buf, 0, len);
+
+                       outlen = hpack_decode_frame(dht, buf, len, list,
+                                                   
sizeof(list)/sizeof(list[0]), &tmp);
+                       if (outlen <= 0) {
+                               printf("   HPACK decoding failed: %d\n", 
outlen);
+                               continue;
+                       }
 
-                       //printf("\e[1;35m%s\e[0m\n", istpad(trash.str, 
list[idx].v).ptr);
+                       printf("<<< Found %d headers :\n", outlen);
+                       for (idx = 0; idx < outlen - 1; idx++) {
+                               //printf("      \e[1;34m%s\e[0m: ",
+                               //       list[idx].n.ptr ? istpad(trash.str, 
list[idx].n).ptr : h2_phdr_to_str(list[idx].n.len));
 
-                       printf("      %s: ", list[idx].n.ptr ?
-                              istpad(trash.area, list[idx].n).ptr :
-                              h2_phdr_to_str(list[idx].n.len));
+                               //printf("\e[1;35m%s\e[0m\n", istpad(trash.str, 
list[idx].v).ptr);
 
-                       printf("%s [n=(%p,%d) v=(%p,%d)]\n",
-                              istpad(trash.area, list[idx].v).ptr,
-                              list[idx].n.ptr, (int)list[idx].n.len, 
list[idx].v.ptr, (int)list[idx].v.len);
-               }
-               puts(">>>");
+                               printf("      %s: ", list[idx].n.ptr ?
+                                      istpad(trash.area, list[idx].n).ptr :
+                                      h2_phdr_to_str(list[idx].n.len));
+
+                               printf("%s [n=(%p,%d) v=(%p,%d)]\n",
+                                      istpad(trash.area, list[idx].v).ptr,
+                                      list[idx].n.ptr, (int)list[idx].n.len, 
list[idx].v.ptr, (int)list[idx].v.len);
+                       }
+                       puts(">>>");
 #ifdef DEBUG_HPACK
-               printf("<<=== DHT dump [ptr=%p]:\n", dht);
-               hpack_dht_dump(stdout, dht);
-               puts("===>>");
+                       printf("<<=== DHT dump [ptr=%p]:\n", dht);
+                       hpack_dht_dump(stdout, dht);
+                       puts("===>>");
 #endif
+               }
+               hpack_dht_free(dht);
        }
+
        return 0;
 }
-- 
2.19.0


Reply via email to