1 /* $NetBSD: main.c,v 1.3 2020/05/24 19:46:22 christos Exp $ */ 2 3 /* 4 * Copyright (C) Internet Systems Consortium, Inc. ("ISC") 5 * 6 * This Source Code Form is subject to the terms of the Mozilla Public 7 * License, v. 2.0. If a copy of the MPL was not distributed with this 8 * file, You can obtain one at http://mozilla.org/MPL/2.0/. 9 * 10 * See the COPYRIGHT file distributed with this work for additional 11 * information regarding copyright ownership. 12 */ 13 14 #include <errno.h> 15 #include <fcntl.h> 16 #include <stdint.h> 17 #include <stdio.h> 18 #include <stdlib.h> 19 #include <string.h> 20 #include <sys/stat.h> 21 #include <unistd.h> 22 23 #include "fuzz.h" 24 25 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION 26 27 #include <dirent.h> 28 29 static void 30 test_all_from(const char *dirname) { 31 DIR *dirp; 32 struct dirent *dp; 33 34 dirp = opendir(dirname); 35 if (dirp == NULL) { 36 return; 37 } 38 39 while ((dp = readdir(dirp)) != NULL) { 40 char filename[strlen(dirname) + strlen(dp->d_name) + 2]; 41 int fd; 42 struct stat st; 43 char *data; 44 ssize_t n; 45 46 if (dp->d_name[0] == '.') { 47 continue; 48 } 49 snprintf(filename, sizeof(filename), "%s/%s", dirname, 50 dp->d_name); 51 52 if ((fd = open(filename, O_RDONLY)) == -1) { 53 fprintf(stderr, "Failed to open %s: %s\n", filename, 54 strerror(errno)); 55 continue; 56 } 57 58 if (fstat(fd, &st) != 0) { 59 fprintf(stderr, "Failed to stat %s: %s\n", filename, 60 strerror(errno)); 61 goto closefd; 62 } 63 64 data = malloc(st.st_size); 65 n = read(fd, data, st.st_size); 66 if (n == st.st_size) { 67 printf("testing %zd bytes from %s\n", n, filename); 68 fflush(stdout); 69 LLVMFuzzerTestOneInput((const uint8_t *)data, n); 70 fflush(stderr); 71 } else { 72 if (n < 0) { 73 fprintf(stderr, 74 "Failed to read %zd bytes from %s: " 75 "%s\n", 76 (ssize_t)st.st_size, filename, 77 strerror(errno)); 78 } else { 79 fprintf(stderr, 80 "Failed to read %zd bytes from %s" 81 ", got %zd\n", 82 (ssize_t)st.st_size, filename, n); 83 } 84 } 85 free(data); 86 closefd: 87 close(fd); 88 } 89 90 closedir(dirp); 91 } 92 93 int 94 main(int argc, char **argv) { 95 char corpusdir[PATH_MAX]; 96 const char *target = strrchr(argv[0], '/'); 97 98 UNUSED(argc); 99 UNUSED(argv); 100 101 target = (target != NULL) ? target + 1 : argv[0]; 102 if (strncmp(target, "lt-", 3) == 0) { 103 target += 3; 104 } 105 106 snprintf(corpusdir, sizeof(corpusdir), FUZZDIR "/%s.in", target); 107 108 test_all_from(corpusdir); 109 110 return (0); 111 } 112 113 #elif __AFL_COMPILER 114 115 int 116 main(int argc, char **argv) { 117 int ret; 118 unsigned char buf[64 * 1024]; 119 120 UNUSED(argc); 121 UNUSED(argv); 122 123 #ifdef __AFL_LOOP 124 while (__AFL_LOOP(10000)) { /* only works with afl-clang-fast */ 125 #else /* ifdef __AFL_LOOP */ 126 { 127 #endif /* ifdef __AFL_LOOP */ 128 ret = fread(buf, 1, sizeof(buf), stdin); 129 if (ret < 0) { 130 return (0); 131 } 132 133 LLVMFuzzerTestOneInput(buf, ret); 134 } 135 136 return (0); 137 } 138 139 #endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */ 140