etc.git


etc

~ scripts and random things
git clone https://github.com/djnnvx/etc.git
log | files | refs | archive

harness.h (1952B)

      1 /*
      2  * harness.h - shared AFL++/libFuzzer entry boilerplate for the libksba harnesses.
      3  *
      4  * Each fuzz_*.c includes this and defines:  static int fuzz_one(const uint8_t*, size_t);
      5  *
      6  * Build modes (set by setup.sh):
      7  *   afl-clang-fast            -> persistent loop, reads from AFL shared memory
      8  *   clang -DKSBA_FUZZ_STANDALONE -> reads files from argv, for crash replay/triage
      9  *   <libFuzzer>               -> LLVMFuzzerTestOneInput
     10  */
     11 #pragma once
     12 
     13 #include <stddef.h>
     14 #include <stdint.h>
     15 #include <stdio.h>
     16 #include <stdlib.h>
     17 #include <string.h>
     18 
     19 #include <gpg-error.h>
     20 #include "ksba.h"
     21 
     22 /* Bound on idx-style accessor loops so adversarial inputs can't spin forever
     23  * inside the harness; real hangs in the library still surface via afl -t. */
     24 #define KSBA_FUZZ_MAXITER 256
     25 
     26 static int fuzz_one(const uint8_t *data, size_t size);
     27 
     28 /* Walk and free a ksba_name_t (GeneralNames), exercising the name accessors. */
     29 static inline void drain_name(ksba_name_t nm)
     30 {
     31     int i;
     32     if (!nm)
     33         return;
     34     for (i = 0; i < KSBA_FUZZ_MAXITER && ksba_name_enum(nm, i); i++) {
     35         char *uri = ksba_name_get_uri(nm, i);
     36         ksba_free(uri);
     37     }
     38     ksba_name_release(nm);
     39 }
     40 
     41 #ifdef __AFL_FUZZ_TESTCASE_LEN
     42 __AFL_FUZZ_INIT();
     43 int main(void)
     44 {
     45     __AFL_INIT();
     46     unsigned char *buf = __AFL_FUZZ_TESTCASE_BUF;
     47     while (__AFL_LOOP(10000))
     48         fuzz_one(buf, (size_t)__AFL_FUZZ_TESTCASE_LEN);
     49     return 0;
     50 }
     51 #else
     52 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
     53 {
     54     return fuzz_one(data, size);
     55 }
     56 #ifdef KSBA_FUZZ_STANDALONE
     57 int main(int argc, char **argv)
     58 {
     59     static unsigned char buf[1 << 20];
     60     int i;
     61     for (i = 1; i < argc; i++) {
     62         FILE *f = fopen(argv[i], "rb");
     63         size_t n;
     64         if (!f)
     65             continue;
     66         n = fread(buf, 1, sizeof buf, f);
     67         fclose(f);
     68         fprintf(stderr, "[*] %s (%zu bytes)\n", argv[i], n);
     69         fuzz_one(buf, n);
     70     }
     71     return 0;
     72 }
     73 #endif
     74 #endif