Home | History | Annotate | Line # | Download | only in standalone
StandaloneFuzzTargetMain.c revision 1.1.1.1
      1 /*===- StandaloneFuzzTargetMain.c - standalone main() for fuzz targets. ---===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 // This main() function can be linked to a fuzz target (i.e. a library
     10 // that exports LLVMFuzzerTestOneInput() and possibly LLVMFuzzerInitialize())
     11 // instead of libFuzzer. This main() function will not perform any fuzzing
     12 // but will simply feed all input files one by one to the fuzz target.
     13 //
     14 // Use this file to provide reproducers for bugs when linking against libFuzzer
     15 // or other fuzzing engine is undesirable.
     16 //===----------------------------------------------------------------------===*/
     17 #include <assert.h>
     18 #include <stdio.h>
     19 #include <stdlib.h>
     20 
     21 extern int LLVMFuzzerTestOneInput(const unsigned char *data, size_t size);
     22 __attribute__((weak)) extern int LLVMFuzzerInitialize(int *argc, char ***argv);
     23 int main(int argc, char **argv) {
     24   fprintf(stderr, "StandaloneFuzzTargetMain: running %d inputs\n", argc - 1);
     25   if (LLVMFuzzerInitialize)
     26     LLVMFuzzerInitialize(&argc, &argv);
     27   for (int i = 1; i < argc; i++) {
     28     fprintf(stderr, "Running: %s\n", argv[i]);
     29     FILE *f = fopen(argv[i], "r");
     30     assert(f);
     31     fseek(f, 0, SEEK_END);
     32     size_t len = ftell(f);
     33     fseek(f, 0, SEEK_SET);
     34     unsigned char *buf = (unsigned char*)malloc(len);
     35     size_t n_read = fread(buf, 1, len, f);
     36     assert(n_read == len);
     37     LLVMFuzzerTestOneInput(buf, len);
     38     free(buf);
     39     fprintf(stderr, "Done:    %s: (%zd bytes)\n", argv[i], n_read);
     40   }
     41 }
     42