summaryrefslogtreecommitdiff
path: root/fuzzers/standalone_driver.c
blob: cd4f71751ae52a133df69d5dbc6e0e8f8577a8d5 (plain)
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
/*
 * 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>

#include "git2.h"
#include "futils.h"
#include "path.h"

#include "standalone_driver.h"

static int run_one_file(const char *filename)
{
	git_str buf = GIT_STR_INIT;
	int error = 0;

	if (git_futils_readbuffer(&buf, filename) < 0) {
		fprintf(stderr, "Failed to read %s: %s\n", filename, git_error_last()->message);
		error = -1;
		goto exit;
	}

	LLVMFuzzerTestOneInput((const unsigned char *)buf.ptr, buf.size);
exit:
	git_str_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;

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

	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_fs_path_dirload(&corpus_files, argv[1], 0, 0x0) < 0) {
		fprintf(stderr, "Failed to scan corpus directory '%s': %s\n",
			argv[1], git_error_last()->message);
		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);
	git_libgit2_shutdown();
	return error;
}