• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <stdint.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <dirent.h>
5 #include <unistd.h>
6 
7 int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size);
8 
main(int argc,char ** argv)9 int main(int argc, char** argv)
10 {
11     FILE * fp;
12     uint8_t Data[0x1000];
13     size_t Size;
14     DIR *d;
15     struct dirent *dir;
16     int r = 0;
17 
18     if (argc != 2) {
19         return 1;
20     }
21 
22     d = opendir(argv[1]);
23     if (d == NULL) {
24         printf("Invalid directory\n");
25         return 2;
26     }
27     if (chdir(argv[1]) != 0) {
28         closedir(d);
29         printf("Invalid directory\n");
30         return 2;
31     }
32 
33     while((dir = readdir(d)) != NULL) {
34         //opens the file, get its size, and reads it into a buffer
35         if (dir->d_type != DT_REG) {
36             continue;
37         }
38         //printf("Running %s\n", dir->d_name);
39         fp = fopen(dir->d_name, "rb");
40         if (fp == NULL) {
41             r = 3;
42             break;
43         }
44         if (fseek(fp, 0L, SEEK_END) != 0) {
45             fclose(fp);
46             r = 4;
47             break;
48         }
49         Size = ftell(fp);
50         if (Size == (size_t) -1) {
51             fclose(fp);
52             r = 5;
53             break;
54         } else if (Size > 0x1000) {
55             fclose(fp);
56             continue;
57         }
58         if (fseek(fp, 0L, SEEK_SET) != 0) {
59             fclose(fp);
60             r = 7;
61             break;
62         }
63         if (fread(Data, Size, 1, fp) != 1) {
64             fclose(fp);
65             r = 8;
66             break;
67         }
68 
69         //lauch fuzzer
70         LLVMFuzzerTestOneInput(Data, Size);
71         fclose(fp);
72     }
73     closedir(d);
74     return r;
75 }
76 
77