StandaloneFuzzTargetMain.c (1905B)
1 /*===- StandaloneFuzzTargetMain.c - standalone main() for fuzz targets. ---===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // This main() function can be linked to a fuzz target (i.e. a library 9 // that exports LLVMFuzzerTestOneInput() and possibly LLVMFuzzerInitialize()) 10 // instead of libFuzzer. This main() function will not perform any fuzzing 11 // but will simply feed all input files one by one to the fuzz target. 12 // 13 // Use this file to provide reproducers for bugs when linking against libFuzzer 14 // or other fuzzing engine is undesirable. 15 //===----------------------------------------------------------------------===*/ 16 #include <assert.h> 17 #include <stdio.h> 18 #include <stdlib.h> 19 #include <string.h> 20 21 extern int LLVMFuzzerTestOneInput(const unsigned char *data, size_t size); 22 extern int LLVMFuzzerInitialize(int *argc, char ***argv); 23 24 int main(int argc, char **argv) 25 { 26 const char *progname; 27 if ((progname = strrchr(argv[0], '/'))) 28 progname++; 29 else 30 progname = argv[0]; 31 fprintf(stderr, "%s: running %d inputs\n", progname, argc - 1); 32 LLVMFuzzerInitialize(&argc, &argv); 33 for (int i = 1; i < argc; i++) 34 { 35 fprintf(stderr, "Running: %s\n", argv[i]); 36 FILE *f = fopen(argv[i], "r+"); 37 assert(f); 38 fseek(f, 0, SEEK_END); 39 const long len = ftell(f); 40 fseek(f, 0, SEEK_SET); 41 unsigned char *buf = (unsigned char *)malloc(len); 42 const size_t n_read = fread(buf, 1, len, f); 43 fclose(f); 44 assert(n_read == len); 45 LLVMFuzzerTestOneInput(buf, len); 46 free(buf); 47 fprintf(stderr, "Done: %s: (%zd bytes)\n", argv[i], n_read); 48 } 49 }