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