ares-fuzz.c
Go to the documentation of this file.
1 /*
2  * General driver to allow command-line fuzzer (i.e. afl) to
3  * exercise the libFuzzer entrypoint.
4  */
5 
6 #include <sys/types.h>
7 #include <fcntl.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #ifdef WIN32
12 #include <io.h>
13 #else
14 #include <unistd.h>
15 #endif
16 
17 #define kMaxAflInputSize (1 << 20)
18 static unsigned char afl_buffer[kMaxAflInputSize];
19 
20 #ifdef __AFL_LOOP
21 /* If we are built with afl-clang-fast, use persistent mode */
22 #define KEEP_FUZZING(count) __AFL_LOOP(1000)
23 #else
24 /* If we are built with afl-clang, execute each input once */
25 #define KEEP_FUZZING(count) ((count) < 1)
26 #endif
27 
28 /* In ares-test-fuzz.c and ares-test-fuzz-name.c: */
29 int LLVMFuzzerTestOneInput(const unsigned char *data, unsigned long size);
30 
31 static void ProcessFile(int fd) {
33  /*
34  * Make a copy of the data so that it's not part of a larger
35  * buffer (where buffer overflows would go unnoticed).
36  */
37  unsigned char *copied_data = (unsigned char *)malloc(count);
38  memcpy(copied_data, afl_buffer, count);
39  LLVMFuzzerTestOneInput(copied_data, count);
40  free(copied_data);
41 }
42 
43 int main(int argc, char *argv[]) {
44  if (argc == 1) {
45  int count = 0;
46  while (KEEP_FUZZING(count)) {
47  ProcessFile(fileno(stdin));
48  count++;
49  }
50  } else {
51  int ii;
52  for (ii = 1; ii < argc; ++ii) {
53  int fd = open(argv[ii], O_RDONLY);
54  if (fd < 0) {
55  fprintf(stderr, "Failed to open '%s'\n", argv[ii]);
56  continue;
57  }
58  ProcessFile(fd);
59  close(fd);
60  }
61  }
62  return 0;
63 }
LLVMFuzzerTestOneInput
int LLVMFuzzerTestOneInput(const unsigned char *data, unsigned long size)
Definition: ares-test-fuzz-name.c:10
kMaxAflInputSize
#define kMaxAflInputSize
Definition: ares-fuzz.c:17
string.h
demumble_test.stdin
stdin
Definition: demumble_test.py:37
python_utils.port_server.stderr
stderr
Definition: port_server.py:51
main
int main(int argc, char *argv[])
Definition: ares-fuzz.c:43
memcpy
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
KEEP_FUZZING
#define KEEP_FUZZING(count)
Definition: ares-fuzz.c:25
close
#define close
Definition: test-fs.c:48
data
char data[kBufferLength]
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1006
read
int read(izstream &zs, T *x, Items items)
Definition: bloaty/third_party/zlib/contrib/iostream2/zstream.h:115
count
int * count
Definition: bloaty/third_party/googletest/googlemock/test/gmock_stress_test.cc:96
ProcessFile
static void ProcessFile(int fd)
Definition: ares-fuzz.c:31
afl_buffer
static unsigned char afl_buffer[kMaxAflInputSize]
Definition: ares-fuzz.c:18
open
#define open
Definition: test-fs.c:46
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:32