Blame fuzz/main.c

Packit aea12f
/*
Packit aea12f
 * Copyright(c) 2017 Tim Ruehsen
Packit aea12f
 *
Packit aea12f
 * Permission is hereby granted, free of charge, to any person obtaining a
Packit aea12f
 * copy of this software and associated documentation files (the "Software"),
Packit aea12f
 * to deal in the Software without restriction, including without limitation
Packit aea12f
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
Packit aea12f
 * and/or sell copies of the Software, and to permit persons to whom the
Packit aea12f
 * Software is furnished to do so, subject to the following conditions:
Packit aea12f
 *
Packit aea12f
 * The above copyright notice and this permission notice shall be included in
Packit aea12f
 * all copies or substantial portions of the Software.
Packit aea12f
 *
Packit aea12f
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit aea12f
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit aea12f
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Packit aea12f
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Packit aea12f
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
Packit aea12f
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
Packit aea12f
 * DEALINGS IN THE SOFTWARE.
Packit aea12f
 */
Packit aea12f
Packit aea12f
#include <config.h>
Packit aea12f
Packit aea12f
#include <stdio.h>
Packit aea12f
#include <unistd.h>
Packit aea12f
#include <stdlib.h>
Packit aea12f
#include <stdint.h>
Packit aea12f
#include <string.h>
Packit aea12f
#include <fcntl.h>
Packit aea12f
#include <errno.h>
Packit aea12f
#include <sys/stat.h>
Packit aea12f
Packit aea12f
#include "fuzzer.h"
Packit aea12f
Packit aea12f
#ifdef TEST_RUN
Packit aea12f
Packit aea12f
#include <dirent.h>
Packit aea12f
Packit aea12f
#ifdef _WIN32
Packit aea12f
#  define SLASH '\\'
Packit aea12f
#else
Packit aea12f
#  define SLASH '/'
Packit aea12f
#endif
Packit aea12f
Packit Service 991b93
static int test_single_file(const char *fname)
Packit Service 991b93
{
Packit Service 991b93
	int fd, ret;
Packit Service 991b93
	struct stat st;
Packit Service 991b93
	uint8_t *data;
Packit Service 991b93
	ssize_t n;
Packit Service 991b93
Packit Service 991b93
	if ((fd = open(fname, O_RDONLY)) == -1) {
Packit Service 991b93
		fprintf(stderr, "Failed to open %s (%d)\n", fname, errno);
Packit Service 991b93
		return -1;
Packit Service 991b93
	}
Packit Service 991b93
Packit Service 991b93
	if (fstat(fd, &st) != 0) {
Packit Service 991b93
		fprintf(stderr, "Failed to stat %d (%d)\n", fd, errno);
Packit Service 991b93
		close(fd);
Packit Service 991b93
		return -1;
Packit Service 991b93
	}
Packit Service 991b93
Packit Service 991b93
	data = malloc(st.st_size);
Packit Service 991b93
	if ((n = read(fd, data, st.st_size)) == st.st_size) {
Packit Service 991b93
		printf("testing %llu bytes from '%s'\n", (unsigned long long) st.st_size, fname);
Packit Service 991b93
		fflush(stdout);
Packit Service 991b93
		LLVMFuzzerTestOneInput(data, st.st_size);
Packit Service 991b93
		fflush(stderr);
Packit Service 991b93
		ret = 0;
Packit Service 991b93
	} else {
Packit Service 991b93
		fprintf(stderr, "Failed to read %llu bytes from %s (%d), got %zd\n", (unsigned long long) st.st_size, fname, errno, n);
Packit Service 991b93
		ret = -1;
Packit Service 991b93
	}
Packit Service 991b93
Packit Service 991b93
	free(data);
Packit Service 991b93
	close(fd);
Packit Service 991b93
	return ret;
Packit Service 991b93
}
Packit Service 991b93
Packit aea12f
static int test_all_from(const char *dirname)
Packit aea12f
{
Packit aea12f
	DIR *dirp;
Packit aea12f
	struct dirent *dp;
Packit aea12f
Packit aea12f
	if ((dirp = opendir(dirname))) {
Packit aea12f
		while ((dp = readdir(dirp))) {
Packit aea12f
			if (*dp->d_name == '.') continue;
Packit aea12f
Packit aea12f
			char fname[strlen(dirname) + strlen(dp->d_name) + 2];
Packit aea12f
			snprintf(fname, sizeof(fname), "%s/%s", dirname, dp->d_name);
Packit aea12f
Packit Service 991b93
			if (test_single_file(fname) < 0)
Packit aea12f
				continue;
Packit aea12f
		}
Packit aea12f
		closedir(dirp);
Packit aea12f
		return 0;
Packit aea12f
	}
Packit aea12f
Packit aea12f
	return 1;
Packit aea12f
}
Packit aea12f
Packit aea12f
int main(int argc, char **argv)
Packit aea12f
{
Packit aea12f
	const char *target;
Packit aea12f
	size_t target_len;
Packit aea12f
Packit aea12f
	if ((target = strrchr(argv[0], SLASH)))
Packit aea12f
		target = strrchr(target, '/');
Packit aea12f
	else
Packit aea12f
		target = strrchr(argv[0], '/');
Packit aea12f
	target = target ? target + 1 : argv[0];
Packit aea12f
Packit aea12f
	if (strncmp(target, "lt-", 3) == 0)
Packit aea12f
		target += 3;
Packit aea12f
Packit aea12f
	target_len = strlen(target);
Packit aea12f
Packit aea12f
#ifdef _WIN32
Packit aea12f
	target_len -= 4; // ignore .exe
Packit aea12f
#endif
Packit aea12f
Packit Service 991b93
	if (argc > 1) { /* test a single file */
Packit Service 991b93
		test_single_file(argv[1]);
Packit Service 991b93
	} else { /* test the target directory */
Packit aea12f
		int rc;
Packit aea12f
		char corporadir[sizeof(SRCDIR) + 1 + target_len + 8];
Packit aea12f
		snprintf(corporadir, sizeof(corporadir), SRCDIR "/%.*s.in", (int) target_len, target);
Packit aea12f
Packit aea12f
		rc = test_all_from(corporadir);
Packit aea12f
		if (rc)
Packit aea12f
			fprintf(stderr, "Failed to find %s\n", corporadir);
Packit aea12f
Packit aea12f
		snprintf(corporadir, sizeof(corporadir), SRCDIR "/%.*s.repro", (int) target_len, target);
Packit aea12f
Packit aea12f
		if (test_all_from(corporadir) && rc)
Packit aea12f
			return 77;
Packit aea12f
	}
Packit aea12f
Packit aea12f
	return 0;
Packit aea12f
}
Packit aea12f
Packit aea12f
#else
Packit aea12f
Packit aea12f
#ifndef __AFL_LOOP
Packit aea12f
static int __AFL_LOOP(int n)
Packit aea12f
{
Packit aea12f
	static int first = 1;
Packit aea12f
Packit aea12f
	if (first) {
Packit aea12f
		first = 0;
Packit aea12f
		return 1;
Packit aea12f
	}
Packit aea12f
Packit aea12f
	return 0;
Packit aea12f
}
Packit aea12f
#endif
Packit aea12f
Packit aea12f
int main(int argc, char **argv)
Packit aea12f
{
Packit aea12f
	int ret;
Packit aea12f
	unsigned char buf[64 * 1024];
Packit aea12f
Packit aea12f
	while (__AFL_LOOP(10000)) { // only works with afl-clang-fast
Packit aea12f
		ret = fread(buf, 1, sizeof(buf), stdin);
Packit aea12f
		if (ret < 0)
Packit aea12f
			return 0;
Packit aea12f
Packit aea12f
		LLVMFuzzerTestOneInput(buf, ret);
Packit aea12f
	}
Packit aea12f
Packit aea12f
	return 0;
Packit aea12f
}
Packit aea12f
Packit aea12f
#endif /* TEST_RUN */