| 1 |
/* |
| 2 |
* Copyright (c) 2017-2024 Free Software Foundation, Inc. |
| 3 |
* |
| 4 |
* This file is part of GNU Wget. |
| 5 |
* |
| 6 |
* GNU Wget is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU General Public License as published by |
| 8 |
* the Free Software Foundation; either version 3 of the License, or |
| 9 |
* (at your option) any later version. |
| 10 |
* |
| 11 |
* GNU Wget is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with Wget. If not, see <https://www.gnu.org/licenses/>. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <config.h> |
| 21 |
|
| 22 |
#include <stdio.h> |
| 23 |
#include <stdlib.h> |
| 24 |
#include <stdint.h> |
| 25 |
#include <string.h> |
| 26 |
#include <fcntl.h> |
| 27 |
|
| 28 |
#include "fuzzer.h" |
| 29 |
#include "utils.h" |
| 30 |
|
| 31 |
#ifdef TEST_RUN |
| 32 |
|
| 33 |
#include <dirent.h> |
| 34 |
|
| 35 |
#ifdef _WIN32 |
| 36 |
# define SLASH '\\' |
| 37 |
#else |
| 38 |
# define SLASH '/' |
| 39 |
#endif |
| 40 |
|
| 41 |
static int test_all_from(const char *dirname) |
| 42 |
{ |
| 43 |
DIR *dirp; |
| 44 |
|
| 45 |
if ((dirp = opendir(dirname))) { |
| 46 |
struct dirent *dp; |
| 47 |
|
| 48 |
while ((dp = readdir(dirp))) { |
| 49 |
if (*dp->d_name == '.') continue; |
| 50 |
|
| 51 |
char fname[strlen(dirname) + strlen(dp->d_name) + 2]; |
| 52 |
snprintf(fname, sizeof(fname), "%s/%s", dirname, dp->d_name); |
| 53 |
|
| 54 |
struct file_memory *fmem; |
| 55 |
if ((fmem = wget_read_file(fname))) { |
| 56 |
printf("testing %ld bytes from '%s'\n", fmem->length, fname); |
| 57 |
fflush(stdout); |
| 58 |
LLVMFuzzerTestOneInput((uint8_t *)fmem->content, fmem->length); |
| 59 |
wget_read_file_free(fmem); |
| 60 |
} |
| 61 |
} |
| 62 |
closedir(dirp); |
| 63 |
return 0; |
| 64 |
} |
| 65 |
|
| 66 |
return 1; |
| 67 |
} |
| 68 |
|
| 69 |
int main(int argc, char **argv) |
| 70 |
{ |
| 71 |
const char *target; |
| 72 |
size_t target_len; |
| 73 |
|
| 74 |
if ((target = strrchr(argv[0], SLASH))) { |
| 75 |
if (strrchr(target, '/')) |
| 76 |
target = strrchr(target, '/'); |
| 77 |
} else |
| 78 |
target = strrchr(argv[0], '/'); |
| 79 |
|
| 80 |
target = target ? target + 1 : argv[0]; |
| 81 |
|
| 82 |
if (strncmp(target, "lt-", 3) == 0) |
| 83 |
target += 3; |
| 84 |
|
| 85 |
target_len = strlen(target); |
| 86 |
|
| 87 |
#ifdef _WIN32 |
| 88 |
target_len -= 4; // ignore .exe |
| 89 |
#endif |
| 90 |
|
| 91 |
{ |
| 92 |
int rc; |
| 93 |
char corporadir[sizeof(SRCDIR) + 1 + target_len + 8]; |
| 94 |
snprintf(corporadir, sizeof(corporadir), SRCDIR "/%.*s.in", (int) target_len, target); |
| 95 |
|
| 96 |
rc = test_all_from(corporadir); |
| 97 |
if (rc) |
| 98 |
fprintf(stderr, "Failed to find %s\n", corporadir); |
| 99 |
|
| 100 |
snprintf(corporadir, sizeof(corporadir), SRCDIR "/%.*s.repro", (int) target_len, target); |
| 101 |
if (test_all_from(corporadir) && rc) |
| 102 |
return 77; // SKIP |
| 103 |
} |
| 104 |
|
| 105 |
return 0; |
| 106 |
} |
| 107 |
|
| 108 |
#else |
| 109 |
|
| 110 |
#ifndef __AFL_LOOP |
| 111 |
static int __AFL_LOOP(int n) |
| 112 |
{ |
| 113 |
static int first = 1; |
| 114 |
|
| 115 |
if (first) { |
| 116 |
first = 0; |
| 117 |
return n && --n > 0; |
| 118 |
} |
| 119 |
|
| 120 |
return 0; |
| 121 |
} |
| 122 |
#endif |
| 123 |
|
| 124 |
int main(int argc, char **argv) |
| 125 |
{ |
| 126 |
int ret; |
| 127 |
unsigned char buf[64 * 1024]; |
| 128 |
|
| 129 |
while (__AFL_LOOP(10000)) { // only works with clang - we have to use 1 because static/global vars in wget |
| 130 |
ret = fread(buf, 1, sizeof(buf), stdin); |
| 131 |
if (ret < 0) |
| 132 |
return 0; |
| 133 |
|
| 134 |
LLVMFuzzerTestOneInput(buf, ret); |
| 135 |
} |
| 136 |
|
| 137 |
return 0; |
| 138 |
} |
| 139 |
|
| 140 |
#endif /* #ifdef TEST_RUN */ |