1 #include <stdint.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 
5 #include "fuzz.h"
6 
7 /**
8  * Main procedure for standalone fuzzing engine.
9  *
10  * Reads filenames from the argument array. For each filename, read the file
11  * into memory and then call the fuzzing interface with the data.
12  */
main(int argc,char ** argv)13 int main(int argc, char **argv)
14 {
15   int ii;
16   for(ii = 1; ii < argc; ii++)
17   {
18     FILE *infile;
19     printf("[%s] ", argv[ii]);
20 
21     /* Try and open the file. */
22     infile = fopen(argv[ii], "rb");
23     if(infile)
24     {
25       uint8_t *buffer = NULL;
26       size_t buffer_len;
27 
28       printf("Opened.. ");
29 
30       /* Get the length of the file. */
31       fseek(infile, 0L, SEEK_END);
32       buffer_len = ftell(infile);
33 
34       /* Reset the file indicator to the beginning of the file. */
35       fseek(infile, 0L, SEEK_SET);
36 
37       /* Allocate a buffer for the file contents. */
38       buffer = (uint8_t *)calloc(buffer_len, sizeof(uint8_t));
39       if(buffer)
40       {
41         /* Read all the text from the file into the buffer. */
42         fread(buffer, sizeof(uint8_t), buffer_len, infile);
43         printf("Read %zu bytes, fuzzing.. ", buffer_len);
44 
45         /* Call the fuzzer with the data. */
46         LLVMFuzzerTestOneInput(buffer, buffer_len);
47 
48         printf("complete !!");
49 
50         /* Free the buffer as it's no longer needed. */
51         free(buffer);
52         buffer = NULL;
53       }
54       else
55       {
56         fprintf(stderr,
57                 "[%s] Failed to allocate %zu bytes \n",
58                 argv[ii],
59                 buffer_len);
60       }
61 
62       /* Close the file as it's no longer needed. */
63       fclose(infile);
64       infile = NULL;
65     }
66     else
67     {
68       /* Failed to open the file. Maybe wrong name or wrong permissions? */
69       fprintf(stderr, "[%s] Open failed. \n", argv[ii]);
70     }
71 
72     printf("\n");
73   }
74 }
75