standalone_driver.c 1.7 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * Copyright (C) the libgit2 contributors. All rights reserved.
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */

#include <stdio.h>

10
#include "git2.h"
11
#include "futils.h"
12 13 14 15 16 17 18 19 20 21 22
#include "path.h"

extern int LLVMFuzzerTestOneInput(const unsigned char *data, size_t size);
extern int LLVMFuzzerInitialize(int *argc, char ***argv);

static int run_one_file(const char *filename)
{
	git_buf buf = GIT_BUF_INIT;
	int error = 0;

	if (git_futils_readbuffer(&buf, filename) < 0) {
23
		fprintf(stderr, "Failed to read %s: %s\n", filename, git_error_last()->message);
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
		error = -1;
		goto exit;
	}

	LLVMFuzzerTestOneInput((const unsigned char *)buf.ptr, buf.size);
exit:
	git_buf_dispose(&buf);
	return error;
}

int main(int argc, char **argv)
{
	git_vector corpus_files = GIT_VECTOR_INIT;
	char *filename = NULL;
	unsigned i = 0;
	int error = 0;

41 42 43 44 45
	if (git_libgit2_init() < 0) {
		fprintf(stderr, "Failed to initialize libgit2\n");
		abort();
	}

46 47 48 49 50 51 52 53 54 55
	if (argc != 2) {
		fprintf(stderr, "Usage: %s <corpus directory>\n", argv[0]);
		error = -1;
		goto exit;
	}

	fprintf(stderr, "Running %s against %s\n", argv[0], argv[1]);
	LLVMFuzzerInitialize(&argc, &argv);

	if (git_path_dirload(&corpus_files, argv[1], 0, 0x0) < 0) {
56 57
		fprintf(stderr, "Failed to scan corpus directory '%s': %s\n",
			argv[1], git_error_last()->message);
58 59 60 61 62 63 64 65 66 67 68 69 70 71
		error = -1;
		goto exit;
	}
	git_vector_foreach(&corpus_files, i, filename) {
		fprintf(stderr, "\tRunning %s...\n", filename);
		if (run_one_file(filename) < 0) {
			error = -1;
			goto exit;
		}
	}
	fprintf(stderr, "Done %d runs\n", i);

exit:
	git_vector_free_deep(&corpus_files);
72
	git_libgit2_shutdown();
73 74
	return error;
}