suricata
onefile.c
Go to the documentation of this file.
1 #include "suricata-common.h"
2 
3 int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size);
4 
5 static int runOneFile(const char *fname)
6 {
7  // opens the file, get its size, and reads it into a buffer
8  uint8_t *data;
9  size_t size;
10  FILE *fp = fopen(fname, "rb");
11  if (fp == NULL) {
12  return 2;
13  }
14  if (fseek(fp, 0L, SEEK_END) != 0) {
15  fclose(fp);
16  return 2;
17  }
18  size = ftell(fp);
19  if (size == (size_t) -1) {
20  fclose(fp);
21  return 2;
22  }
23  if (fseek(fp, 0L, SEEK_SET) != 0) {
24  fclose(fp);
25  return 2;
26  }
27  data = malloc(size);
28  if (data == NULL) {
29  fclose(fp);
30  return 2;
31  }
32  if (fread(data, size, 1, fp) != 1) {
33  fclose(fp);
34  free(data);
35  return 2;
36  }
37 
38  // launch fuzzer
39  LLVMFuzzerTestOneInput(data, size);
40  free(data);
41  fclose(fp);
42  return 0;
43 }
44 
45 int main(int argc, char **argv)
46 {
47  DIR *d;
48  struct dirent *dir;
49  int r;
50 
51  if (argc != 2) {
52  return 1;
53  }
54 #ifdef AFLFUZZ_PERSISTENT_MODE
55  while (__AFL_LOOP(1000)) {
56 #endif /* AFLFUZZ_PERSISTENT_MODE */
57 
58  d = opendir(argv[1]);
59  if (d == NULL) {
60  // run one file
61  r = runOneFile(argv[1]);
62  if (r != 0) {
63  return r;
64  }
65  } else {
66  // run every file in one directory
67  if (chdir(argv[1]) != 0) {
68  closedir(d);
69  printf("Invalid directory\n");
70  return 2;
71  }
72  while ((dir = readdir(d)) != NULL) {
73  if (dir->d_type != DT_REG) {
74  continue;
75  }
76  r = runOneFile(dir->d_name);
77  if (r != 0) {
78  return r;
79  }
80  }
81  closedir(d);
82  }
83 #ifdef AFLFUZZ_PERSISTENT_MODE
84  }
85 #endif /* AFLFUZZ_PERSISTENT_MODE */
86 
87  return 0;
88 }
main
int main(int argc, char **argv)
Definition: onefile.c:45
LLVMFuzzerTestOneInput
int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
Definition: fuzz_applayerparserparse.c:72
suricata-common.h