• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <stdint.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 
5 #include "testinput.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         size_t result;
42 
43         /* Read all the text from the file into the buffer. */
44         result = fread(buffer, sizeof(uint8_t), buffer_len, infile);
45 
46         if (result == buffer_len)
47         {
48           printf("Read %zu bytes, fuzzing.. ", buffer_len);
49           /* Call the fuzzer with the data. */
50           LLVMFuzzerTestOneInput(buffer, buffer_len);
51 
52           printf("complete !!");
53         }
54         else
55         {
56           fprintf(stderr,
57                   "Failed to read %zu bytes (result %zu)\n",
58                   buffer_len,
59                   result);
60         }
61 
62         /* Free the buffer as it's no longer needed. */
63         free(buffer);
64         buffer = NULL;
65       }
66       else
67       {
68         fprintf(stderr,
69                 "[%s] Failed to allocate %zu bytes \n",
70                 argv[ii],
71                 buffer_len);
72       }
73 
74       /* Close the file as it's no longer needed. */
75       fclose(infile);
76       infile = NULL;
77     }
78     else
79     {
80       /* Failed to open the file. Maybe wrong name or wrong permissions? */
81       fprintf(stderr, "[%s] Open failed. \n", argv[ii]);
82     }
83 
84     printf("\n");
85   }
86 }
87