]>
Commit | Line | Data |
---|---|---|
ac3d33df JK |
1 | /* |
2 | * Copyright (C) the libgit2 contributors. All rights reserved. | |
3 | * | |
4 | * This file is part of libgit2, distributed under the GNU GPL v2 with | |
5 | * a Linking Exception. For full terms see the included COPYING file. | |
6 | */ | |
7 | ||
ac3d33df | 8 | #include <stdio.h> |
ac3d33df JK |
9 | |
10 | #include "git2.h" | |
0c9c969a | 11 | #include "futils.h" |
ac3d33df JK |
12 | #include "path.h" |
13 | ||
14 | extern int LLVMFuzzerTestOneInput(const unsigned char *data, size_t size); | |
15 | extern int LLVMFuzzerInitialize(int *argc, char ***argv); | |
16 | ||
17 | static int run_one_file(const char *filename) | |
18 | { | |
19 | git_buf buf = GIT_BUF_INIT; | |
20 | int error = 0; | |
21 | ||
22 | if (git_futils_readbuffer(&buf, filename) < 0) { | |
0c9c969a | 23 | fprintf(stderr, "Failed to read %s: %s\n", filename, git_error_last()->message); |
ac3d33df JK |
24 | error = -1; |
25 | goto exit; | |
26 | } | |
27 | ||
28 | LLVMFuzzerTestOneInput((const unsigned char *)buf.ptr, buf.size); | |
29 | exit: | |
30 | git_buf_dispose(&buf); | |
31 | return error; | |
32 | } | |
33 | ||
34 | int main(int argc, char **argv) | |
35 | { | |
36 | git_vector corpus_files = GIT_VECTOR_INIT; | |
37 | char *filename = NULL; | |
38 | unsigned i = 0; | |
39 | int error = 0; | |
40 | ||
41 | if (git_libgit2_init() < 0) { | |
42 | fprintf(stderr, "Failed to initialize libgit2\n"); | |
43 | abort(); | |
44 | } | |
45 | ||
46 | if (argc != 2) { | |
47 | fprintf(stderr, "Usage: %s <corpus directory>\n", argv[0]); | |
48 | error = -1; | |
49 | goto exit; | |
50 | } | |
51 | ||
52 | fprintf(stderr, "Running %s against %s\n", argv[0], argv[1]); | |
53 | LLVMFuzzerInitialize(&argc, &argv); | |
54 | ||
55 | if (git_path_dirload(&corpus_files, argv[1], 0, 0x0) < 0) { | |
0c9c969a UG |
56 | fprintf(stderr, "Failed to scan corpus directory '%s': %s\n", |
57 | argv[1], git_error_last()->message); | |
ac3d33df JK |
58 | error = -1; |
59 | goto exit; | |
60 | } | |
61 | git_vector_foreach(&corpus_files, i, filename) { | |
62 | fprintf(stderr, "\tRunning %s...\n", filename); | |
63 | if (run_one_file(filename) < 0) { | |
64 | error = -1; | |
65 | goto exit; | |
66 | } | |
67 | } | |
68 | fprintf(stderr, "Done %d runs\n", i); | |
69 | ||
70 | exit: | |
71 | git_vector_free_deep(&corpus_files); | |
72 | git_libgit2_shutdown(); | |
73 | return error; | |
74 | } |