summaryrefslogtreecommitdiff
path: root/test/ares-fuzz.c
diff options
context:
space:
mode:
authorYu Jiung <jiung.yu@samsung.com>2016-11-09 11:23:06 +0900
committerYu Jiung <jiung.yu@samsung.com>2016-11-09 11:23:15 +0900
commit8e609b5f488d486a9e066ed494218d966f489938 (patch)
tree0a169eb7025401e0a14ab5d5e74db5b27954f591 /test/ares-fuzz.c
parenteb886f120599b2a184db20b527db6dfdfcb7852e (diff)
downloadc-ares-8e609b5f488d486a9e066ed494218d966f489938.tar.gz
c-ares-8e609b5f488d486a9e066ed494218d966f489938.tar.bz2
c-ares-8e609b5f488d486a9e066ed494218d966f489938.zip
Imported Upstream version 1.12.0upstream/1.12.0
Change-Id: I6a571bddd24d1cb7d64b74bc66e878ccba2ce638
Diffstat (limited to 'test/ares-fuzz.c')
-rw-r--r--test/ares-fuzz.c58
1 files changed, 58 insertions, 0 deletions
diff --git a/test/ares-fuzz.c b/test/ares-fuzz.c
new file mode 100644
index 0000000..9276128
--- /dev/null
+++ b/test/ares-fuzz.c
@@ -0,0 +1,58 @@
+/*
+ * General driver to allow command-line fuzzer (i.e. afl) to
+ * exercise the libFuzzer entrypoint.
+ */
+
+#include <sys/types.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define kMaxAflInputSize (1 << 20)
+static unsigned char afl_buffer[kMaxAflInputSize];
+
+#ifdef __AFL_LOOP
+/* If we are built with afl-clang-fast, use persistent mode */
+#define KEEP_FUZZING(count) __AFL_LOOP(1000)
+#else
+/* If we are built with afl-clang, execute each input once */
+#define KEEP_FUZZING(count) ((count) < 1)
+#endif
+
+/* In ares-test-fuzz.c: */
+int LLVMFuzzerTestOneInput(const unsigned char *data, unsigned long size);
+
+static void ProcessFile(int fd) {
+ ssize_t count = read(fd, afl_buffer, kMaxAflInputSize);
+ /*
+ * Make a copy of the data so that it's not part of a larger
+ * buffer (where buffer overflows would go unnoticed).
+ */
+ unsigned char *copied_data = (unsigned char *)malloc(count);
+ LLVMFuzzerTestOneInput(copied_data, count);
+ free(copied_data);
+}
+
+int main(int argc, char *argv[]) {
+ if (argc == 1) {
+ int count = 0;
+ while (KEEP_FUZZING(count)) {
+ ProcessFile(fileno(stdin));
+ count++;
+ }
+ } else {
+ int ii;
+ for (ii = 1; ii < argc; ++ii) {
+ int fd = open(argv[ii], O_RDONLY);
+ if (fd < 0) {
+ fprintf(stderr, "Failed to open '%s'\n", argv[ii]);
+ continue;
+ }
+ ProcessFile(fd);
+ close(fd);
+ }
+ }
+ return 0;
+}