File: main.c

package info (click to toggle)
wget2 2.2.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 22,248 kB
  • sloc: ansic: 121,144; sh: 11,559; makefile: 878; xml: 182; sed: 16
file content (167 lines) | stat: -rw-r--r-- 3,697 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 * Copyright (c) 2017-2024 Free Software Foundation, Inc.
 *
 * This file is part of libwget.
 *
 * Libwget is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Libwget is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with libwget.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "../config.h"

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>

#include "wget.h"
#include "fuzzer.h"

#ifdef TEST_RUN

#include <dirent.h>

#ifdef _WIN32
#  define SLASH '\\'
#else
#  define SLASH '/'
#endif

static int test_all_from(const char *dirname)
{
	DIR *dirp;

	if ((dirp = opendir(dirname))) {
		struct dirent *dp;

		while ((dp = readdir(dirp))) {
			if (*dp->d_name == '.') continue;

			char *fname = wget_aprintf("%s/%s", dirname, dp->d_name);

			uint8_t *data;
			size_t size;
			if ((data = (uint8_t *) wget_read_file(fname, &size))) {
				printf("testing %zu bytes from '%s'\n", size, fname);
				LLVMFuzzerTestOneInput(data, size);
				wget_free(data);
			}

			wget_xfree(fname);
		}
		closedir(dirp);
		return 0;
	}

	return 1;
}

int main(WGET_GCC_UNUSED int argc, char **argv)
{
	// if VALGRIND testing is enabled, we have to call ourselves with valgrind checking
	const char *valgrind = getenv("VALGRIND_TESTS");
	const char *target;
	size_t target_len;

	if (!valgrind || !*valgrind || !strcmp(valgrind, "0")) {
		// fallthrough
	}
	else if (!strcmp(valgrind, "1")) {
		char *cmd = wget_aprintf("VALGRIND_TESTS=\"\" valgrind --error-exitcode=301 --leak-check=yes --show-reachable=yes --track-origins=yes --suppressions=" SRCDIR "/valgrind-suppressions --gen-suppressions=all %s", argv[0]);
		int result = system(cmd) != 0;
		wget_xfree(cmd);
		return result;
	} else {
		char *cmd = wget_aprintf("VALGRIND_TESTS="" %s %s", valgrind, argv[0]);
		int result = system(cmd) != 0;
		wget_xfree(cmd);
		return result;
	}

	wget_global_init(
		// WGET_DEBUG_STREAM, stdout,
		WGET_ERROR_STREAM, stdout,
		WGET_INFO_STREAM, stdout,
		0);

	if ((target = strrchr(argv[0], SLASH))) {
		if (strrchr(target, '/'))
			target = strrchr(target, '/');
	} else
		target = strrchr(argv[0], '/');
	target = target ? target + 1 : argv[0];

	if (strncmp(target, "lt-", 3) == 0)
		target += 3;

	target_len = strlen(target);

#ifdef _WIN32
	target_len -= 4; // ignore .exe
#endif

	{
		char *corporadir;

		corporadir = wget_aprintf(SRCDIR "/%.*s.in", (int) target_len, target);
		int rc = test_all_from(corporadir);
		if (rc)
			wget_error_printf("Failed to find %s\n", corporadir);
		wget_xfree(corporadir);

		corporadir = wget_aprintf(SRCDIR "/%.*s.repro", (int) target_len, target);
		bool failure = test_all_from(corporadir) && rc;
		wget_xfree(corporadir);
		if (failure)
			return 77;
	}

	wget_global_deinit();

	return 0;
}

#else

#ifndef __AFL_LOOP
static int __AFL_LOOP(int n)
{
	static int first = 1;

	if (first) {
		first = 0;
		return 1;
	}

	return 0;
}
#endif

int main(int argc, char **argv)
{
	int ret;
	unsigned char buf[64 * 1024];

	while (__AFL_LOOP(10000)) { // only works with afl-clang-fast
		ret = fread(buf, 1, sizeof(buf), stdin);
		if (ret < 0)
			return 0;

		LLVMFuzzerTestOneInput(buf, ret);
	}

	return 0;
}

#endif /* #ifdef TEST_RUN */